xref: /openbmc/linux/fs/sysfs/mount.c (revision 0edbfea5)
1 /*
2  * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
3  *
4  * Copyright (c) 2001-3 Patrick Mochel
5  * Copyright (c) 2007 SUSE Linux Products GmbH
6  * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7  *
8  * This file is released under the GPLv2.
9  *
10  * Please see Documentation/filesystems/sysfs.txt for more information.
11  */
12 
13 #define DEBUG
14 
15 #include <linux/fs.h>
16 #include <linux/magic.h>
17 #include <linux/mount.h>
18 #include <linux/init.h>
19 #include <linux/user_namespace.h>
20 
21 #include "sysfs.h"
22 
23 static struct kernfs_root *sysfs_root;
24 struct kernfs_node *sysfs_root_kn;
25 
26 static struct dentry *sysfs_mount(struct file_system_type *fs_type,
27 	int flags, const char *dev_name, void *data)
28 {
29 	struct dentry *root;
30 	void *ns;
31 	bool new_sb;
32 
33 	if (!(flags & MS_KERNMOUNT)) {
34 		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
35 			return ERR_PTR(-EPERM);
36 	}
37 
38 	ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
39 	root = kernfs_mount_ns(fs_type, flags, sysfs_root,
40 				SYSFS_MAGIC, &new_sb, ns);
41 	if (IS_ERR(root) || !new_sb)
42 		kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
43 	else if (new_sb)
44 		/* Userspace would break if executables appear on sysfs */
45 		root->d_sb->s_iflags |= SB_I_NOEXEC;
46 
47 	return root;
48 }
49 
50 static void sysfs_kill_sb(struct super_block *sb)
51 {
52 	void *ns = (void *)kernfs_super_ns(sb);
53 
54 	kernfs_kill_sb(sb);
55 	kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
56 }
57 
58 static struct file_system_type sysfs_fs_type = {
59 	.name		= "sysfs",
60 	.mount		= sysfs_mount,
61 	.kill_sb	= sysfs_kill_sb,
62 	.fs_flags	= FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
63 };
64 
65 int __init sysfs_init(void)
66 {
67 	int err;
68 
69 	sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
70 					NULL);
71 	if (IS_ERR(sysfs_root))
72 		return PTR_ERR(sysfs_root);
73 
74 	sysfs_root_kn = sysfs_root->kn;
75 
76 	err = register_filesystem(&sysfs_fs_type);
77 	if (err) {
78 		kernfs_destroy_root(sysfs_root);
79 		return err;
80 	}
81 
82 	return 0;
83 }
84