^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2001-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2007 SUSE Linux Products GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Please see Documentation/filesystems/sysfs.rst for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/user_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct kernfs_root *sysfs_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct kernfs_node *sysfs_root_kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int sysfs_get_tree(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct kernfs_fs_context *kfc = fc->fs_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ret = kernfs_get_tree(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (kfc->new_sb_created)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static void sysfs_fs_context_free(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct kernfs_fs_context *kfc = fc->fs_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (kfc->ns_tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) kernfs_free_fs_context(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) kfree(kfc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static const struct fs_context_operations sysfs_fs_context_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .free = sysfs_fs_context_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .get_tree = sysfs_get_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int sysfs_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct kernfs_fs_context *kfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct net *netns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!(fc->sb_flags & SB_KERNMOUNT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!kfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kfc->root = sysfs_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kfc->magic = SYSFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fc->fs_private = kfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) fc->ops = &sysfs_fs_context_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (netns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) put_user_ns(fc->user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fc->user_ns = get_user_ns(netns->user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) fc->global = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void sysfs_kill_sb(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void *ns = (void *)kernfs_super_ns(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) kernfs_kill_sb(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static struct file_system_type sysfs_fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .name = "sysfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .init_fs_context = sysfs_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .kill_sb = sysfs_kill_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .fs_flags = FS_USERNS_MOUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int __init sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (IS_ERR(sysfs_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return PTR_ERR(sysfs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sysfs_root_kn = sysfs_root->kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) err = register_filesystem(&sysfs_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kernfs_destroy_root(sysfs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }