Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / fs / sysfs / mount.c
blobb428d317ae92a74d40ddf20ee3e64a830a04433b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.txt for more information.
12 #include <linux/fs.h>
13 #include <linux/magic.h>
14 #include <linux/mount.h>
15 #include <linux/init.h>
16 #include <linux/user_namespace.h>
18 #include "sysfs.h"
20 static struct kernfs_root *sysfs_root;
21 struct kernfs_node *sysfs_root_kn;
23 static struct dentry *sysfs_mount(struct file_system_type *fs_type,
24 int flags, const char *dev_name, void *data)
26 struct dentry *root;
27 void *ns;
28 bool new_sb;
30 if (!(flags & SB_KERNMOUNT)) {
31 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
32 return ERR_PTR(-EPERM);
35 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
36 root = kernfs_mount_ns(fs_type, flags, sysfs_root,
37 SYSFS_MAGIC, &new_sb, ns);
38 if (IS_ERR(root) || !new_sb)
39 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
40 else if (new_sb)
41 root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
43 return root;
46 static void sysfs_kill_sb(struct super_block *sb)
48 void *ns = (void *)kernfs_super_ns(sb);
50 kernfs_kill_sb(sb);
51 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
54 static struct file_system_type sysfs_fs_type = {
55 .name = "sysfs",
56 .mount = sysfs_mount,
57 .kill_sb = sysfs_kill_sb,
58 .fs_flags = FS_USERNS_MOUNT,
61 int __init sysfs_init(void)
63 int err;
65 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
66 NULL);
67 if (IS_ERR(sysfs_root))
68 return PTR_ERR(sysfs_root);
70 sysfs_root_kn = sysfs_root->kn;
72 err = register_filesystem(&sysfs_fs_type);
73 if (err) {
74 kernfs_destroy_root(sysfs_root);
75 return err;
78 return 0;