1 // SPDX-License-Identifier: GPL-2.0
3 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
9 * Please see Documentation/filesystems/sysfs.rst for more information.
13 #include <linux/magic.h>
14 #include <linux/mount.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/user_namespace.h>
18 #include <linux/fs_context.h>
19 #include <net/net_namespace.h>
23 static struct kernfs_root *sysfs_root;
24 struct kernfs_node *sysfs_root_kn;
26 static int sysfs_get_tree(struct fs_context *fc)
28 struct kernfs_fs_context *kfc = fc->fs_private;
31 ret = kernfs_get_tree(fc);
35 if (kfc->new_sb_created)
36 fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
40 static void sysfs_fs_context_free(struct fs_context *fc)
42 struct kernfs_fs_context *kfc = fc->fs_private;
45 kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
46 kernfs_free_fs_context(fc);
50 static const struct fs_context_operations sysfs_fs_context_ops = {
51 .free = sysfs_fs_context_free,
52 .get_tree = sysfs_get_tree,
55 static int sysfs_init_fs_context(struct fs_context *fc)
57 struct kernfs_fs_context *kfc;
60 if (!(fc->sb_flags & SB_KERNMOUNT)) {
61 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
65 kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
69 kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
70 kfc->root = sysfs_root;
71 kfc->magic = SYSFS_MAGIC;
73 fc->ops = &sysfs_fs_context_ops;
75 put_user_ns(fc->user_ns);
76 fc->user_ns = get_user_ns(netns->user_ns);
82 static void sysfs_kill_sb(struct super_block *sb)
84 void *ns = (void *)kernfs_super_ns(sb);
87 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
90 static struct file_system_type sysfs_fs_type = {
92 .init_fs_context = sysfs_init_fs_context,
93 .kill_sb = sysfs_kill_sb,
94 .fs_flags = FS_USERNS_MOUNT,
97 int __init sysfs_init(void)
101 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
103 if (IS_ERR(sysfs_root))
104 return PTR_ERR(sysfs_root);
106 sysfs_root_kn = kernfs_root_to_node(sysfs_root);
108 err = register_filesystem(&sysfs_fs_type);
110 kernfs_destroy_root(sysfs_root);