hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/sysfs/mount.c
....@@ -6,41 +6,77 @@
66 * Copyright (c) 2007 SUSE Linux Products GmbH
77 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
88 *
9
- * Please see Documentation/filesystems/sysfs.txt for more information.
9
+ * Please see Documentation/filesystems/sysfs.rst for more information.
1010 */
1111
1212 #include <linux/fs.h>
1313 #include <linux/magic.h>
1414 #include <linux/mount.h>
1515 #include <linux/init.h>
16
+#include <linux/slab.h>
1617 #include <linux/user_namespace.h>
18
+#include <linux/fs_context.h>
19
+#include <net/net_namespace.h>
1720
1821 #include "sysfs.h"
1922
2023 static struct kernfs_root *sysfs_root;
2124 struct kernfs_node *sysfs_root_kn;
2225
23
-static struct dentry *sysfs_mount(struct file_system_type *fs_type,
24
- int flags, const char *dev_name, void *data)
26
+static int sysfs_get_tree(struct fs_context *fc)
2527 {
26
- struct dentry *root;
27
- void *ns;
28
- bool new_sb = false;
28
+ struct kernfs_fs_context *kfc = fc->fs_private;
29
+ int ret;
2930
30
- if (!(flags & SB_KERNMOUNT)) {
31
+ ret = kernfs_get_tree(fc);
32
+ if (ret)
33
+ return ret;
34
+
35
+ if (kfc->new_sb_created)
36
+ fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
37
+ return 0;
38
+}
39
+
40
+static void sysfs_fs_context_free(struct fs_context *fc)
41
+{
42
+ struct kernfs_fs_context *kfc = fc->fs_private;
43
+
44
+ if (kfc->ns_tag)
45
+ kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
46
+ kernfs_free_fs_context(fc);
47
+ kfree(kfc);
48
+}
49
+
50
+static const struct fs_context_operations sysfs_fs_context_ops = {
51
+ .free = sysfs_fs_context_free,
52
+ .get_tree = sysfs_get_tree,
53
+};
54
+
55
+static int sysfs_init_fs_context(struct fs_context *fc)
56
+{
57
+ struct kernfs_fs_context *kfc;
58
+ struct net *netns;
59
+
60
+ if (!(fc->sb_flags & SB_KERNMOUNT)) {
3161 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
32
- return ERR_PTR(-EPERM);
62
+ return -EPERM;
3363 }
3464
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 (!new_sb)
39
- kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
40
- else if (!IS_ERR(root))
41
- root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
65
+ kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
66
+ if (!kfc)
67
+ return -ENOMEM;
4268
43
- return root;
69
+ kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
70
+ kfc->root = sysfs_root;
71
+ kfc->magic = SYSFS_MAGIC;
72
+ fc->fs_private = kfc;
73
+ fc->ops = &sysfs_fs_context_ops;
74
+ if (netns) {
75
+ put_user_ns(fc->user_ns);
76
+ fc->user_ns = get_user_ns(netns->user_ns);
77
+ }
78
+ fc->global = true;
79
+ return 0;
4480 }
4581
4682 static void sysfs_kill_sb(struct super_block *sb)
....@@ -52,10 +88,10 @@
5288 }
5389
5490 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,
91
+ .name = "sysfs",
92
+ .init_fs_context = sysfs_init_fs_context,
93
+ .kill_sb = sysfs_kill_sb,
94
+ .fs_flags = FS_USERNS_MOUNT,
5995 };
6096
6197 int __init sysfs_init(void)