xref: /linux/fs/sysfs/mount.c (revision e756bc5670d0f801ca43dc55b8eacde42a5b818b)
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/mount.h>
17 #include <linux/init.h>
18 #include <linux/user_namespace.h>
19 
20 #include "sysfs.h"
21 
22 static struct kernfs_root *sysfs_root;
23 struct sysfs_dirent *sysfs_root_sd;
24 
25 static struct dentry *sysfs_mount(struct file_system_type *fs_type,
26 	int flags, const char *dev_name, void *data)
27 {
28 	struct dentry *root;
29 	void *ns;
30 
31 	if (!(flags & MS_KERNMOUNT)) {
32 		if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
33 			return ERR_PTR(-EPERM);
34 
35 		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
36 			return ERR_PTR(-EPERM);
37 	}
38 
39 	ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
40 	root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns);
41 	if (IS_ERR(root))
42 		kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
43 	return root;
44 }
45 
46 static void sysfs_kill_sb(struct super_block *sb)
47 {
48 	kernfs_kill_sb(sb);
49 	kobj_ns_drop(KOBJ_NS_TYPE_NET, (void *)kernfs_super_ns(sb));
50 }
51 
52 static struct file_system_type sysfs_fs_type = {
53 	.name		= "sysfs",
54 	.mount		= sysfs_mount,
55 	.kill_sb	= sysfs_kill_sb,
56 	.fs_flags	= FS_USERNS_MOUNT,
57 };
58 
59 int __init sysfs_init(void)
60 {
61 	int err;
62 
63 	sysfs_root = kernfs_create_root(NULL);
64 	if (IS_ERR(sysfs_root))
65 		return PTR_ERR(sysfs_root);
66 
67 	sysfs_root_sd = sysfs_root->sd;
68 
69 	err = register_filesystem(&sysfs_fs_type);
70 	if (err) {
71 		kernfs_destroy_root(sysfs_root);
72 		return err;
73 	}
74 
75 	return 0;
76 }
77