]>
Commit | Line | Data |
---|---|---|
619daeee | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
6d66f5cd TH |
3 | * fs/sysfs/symlink.c - operations for initializing and mounting sysfs |
4 | * | |
5 | * Copyright (c) 2001-3 Patrick Mochel | |
6 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
7 | * Copyright (c) 2007 Tejun Heo <[email protected]> | |
8 | * | |
6d66f5cd | 9 | * Please see Documentation/filesystems/sysfs.txt for more information. |
1da177e4 LT |
10 | */ |
11 | ||
1da177e4 | 12 | #include <linux/fs.h> |
c9482a5b | 13 | #include <linux/magic.h> |
1da177e4 | 14 | #include <linux/mount.h> |
1da177e4 | 15 | #include <linux/init.h> |
23bf1b6b | 16 | #include <linux/slab.h> |
87a8ebd6 | 17 | #include <linux/user_namespace.h> |
23bf1b6b DH |
18 | #include <linux/fs_context.h> |
19 | #include <net/net_namespace.h> | |
1da177e4 LT |
20 | |
21 | #include "sysfs.h" | |
22 | ||
ba7443bc | 23 | static struct kernfs_root *sysfs_root; |
324a56e1 | 24 | struct kernfs_node *sysfs_root_kn; |
061447a4 | 25 | |
23bf1b6b | 26 | static int sysfs_get_tree(struct fs_context *fc) |
1da177e4 | 27 | { |
23bf1b6b DH |
28 | struct kernfs_fs_context *kfc = fc->fs_private; |
29 | int ret; | |
9e7fdd25 | 30 | |
23bf1b6b DH |
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)) { | |
c84a3b27 | 61 | if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET)) |
23bf1b6b | 62 | return -EPERM; |
7dc5dbc8 | 63 | } |
87a8ebd6 | 64 | |
23bf1b6b DH |
65 | kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL); |
66 | if (!kfc) | |
67 | return -ENOMEM; | |
90f8572b | 68 | |
23bf1b6b DH |
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 (fc->user_ns) | |
75 | put_user_ns(fc->user_ns); | |
76 | fc->user_ns = get_user_ns(netns->user_ns); | |
77 | fc->global = true; | |
78 | return 0; | |
4b93dc9b TH |
79 | } |
80 | ||
9e7fdd25 | 81 | static void sysfs_kill_sb(struct super_block *sb) |
4b93dc9b | 82 | { |
a7560a01 TH |
83 | void *ns = (void *)kernfs_super_ns(sb); |
84 | ||
4b93dc9b | 85 | kernfs_kill_sb(sb); |
a7560a01 | 86 | kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); |
4b93dc9b TH |
87 | } |
88 | ||
1da177e4 | 89 | static struct file_system_type sysfs_fs_type = { |
23bf1b6b DH |
90 | .name = "sysfs", |
91 | .init_fs_context = sysfs_init_fs_context, | |
92 | .kill_sb = sysfs_kill_sb, | |
93 | .fs_flags = FS_USERNS_MOUNT, | |
1da177e4 LT |
94 | }; |
95 | ||
4b93dc9b TH |
96 | int __init sysfs_init(void) |
97 | { | |
98 | int err; | |
e0bf68dd | 99 | |
555724a8 TH |
100 | sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, |
101 | NULL); | |
4b93dc9b TH |
102 | if (IS_ERR(sysfs_root)) |
103 | return PTR_ERR(sysfs_root); | |
104 | ||
324a56e1 | 105 | sysfs_root_kn = sysfs_root->kn; |
ba7443bc | 106 | |
1da177e4 | 107 | err = register_filesystem(&sysfs_fs_type); |
4b93dc9b TH |
108 | if (err) { |
109 | kernfs_destroy_root(sysfs_root); | |
110 | return err; | |
111 | } | |
9e30cc95 TH |
112 | |
113 | return 0; | |
1da177e4 | 114 | } |