sysfs, kernfs: make super_blocks bind to different kernfs_roots
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / sysfs / mount.c
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/pagemap.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/magic.h>
21 #include <linux/slab.h>
22 #include <linux/user_namespace.h>
23
24 #include "sysfs.h"
25
26
27 struct kmem_cache *sysfs_dir_cachep;
28
29 static const struct super_operations sysfs_ops = {
30         .statfs         = simple_statfs,
31         .drop_inode     = generic_delete_inode,
32         .evict_inode    = sysfs_evict_inode,
33 };
34
35 static struct kernfs_root *sysfs_root;
36 struct sysfs_dirent *sysfs_root_sd;
37
38 static int sysfs_fill_super(struct super_block *sb)
39 {
40         struct sysfs_super_info *info = sysfs_info(sb);
41         struct inode *inode;
42         struct dentry *root;
43
44         sb->s_blocksize = PAGE_CACHE_SIZE;
45         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
46         sb->s_magic = SYSFS_MAGIC;
47         sb->s_op = &sysfs_ops;
48         sb->s_time_gran = 1;
49
50         /* get root inode, initialize and unlock it */
51         mutex_lock(&sysfs_mutex);
52         inode = sysfs_get_inode(sb, info->root->sd);
53         mutex_unlock(&sysfs_mutex);
54         if (!inode) {
55                 pr_debug("sysfs: could not get root inode\n");
56                 return -ENOMEM;
57         }
58
59         /* instantiate and link root dentry */
60         root = d_make_root(inode);
61         if (!root) {
62                 pr_debug("%s: could not get root dentry!\n", __func__);
63                 return -ENOMEM;
64         }
65         kernfs_get(info->root->sd);
66         root->d_fsdata = info->root->sd;
67         sb->s_root = root;
68         sb->s_d_op = &sysfs_dentry_ops;
69         return 0;
70 }
71
72 static int sysfs_test_super(struct super_block *sb, void *data)
73 {
74         struct sysfs_super_info *sb_info = sysfs_info(sb);
75         struct sysfs_super_info *info = data;
76
77         return sb_info->root == info->root && sb_info->ns == info->ns;
78 }
79
80 static int sysfs_set_super(struct super_block *sb, void *data)
81 {
82         int error;
83         error = set_anon_super(sb, data);
84         if (!error)
85                 sb->s_fs_info = data;
86         return error;
87 }
88
89 static void free_sysfs_super_info(struct sysfs_super_info *info)
90 {
91         kobj_ns_drop(KOBJ_NS_TYPE_NET, (void *)info->ns);
92         kfree(info);
93 }
94
95 static struct dentry *sysfs_mount(struct file_system_type *fs_type,
96         int flags, const char *dev_name, void *data)
97 {
98         struct sysfs_super_info *info;
99         struct super_block *sb;
100         int error;
101
102         if (!(flags & MS_KERNMOUNT)) {
103                 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
104                         return ERR_PTR(-EPERM);
105
106                 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
107                         return ERR_PTR(-EPERM);
108         }
109
110         info = kzalloc(sizeof(*info), GFP_KERNEL);
111         if (!info)
112                 return ERR_PTR(-ENOMEM);
113
114         info->root = sysfs_root;
115         info->ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
116
117         sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info);
118         if (IS_ERR(sb) || sb->s_fs_info != info)
119                 free_sysfs_super_info(info);
120         if (IS_ERR(sb))
121                 return ERR_CAST(sb);
122         if (!sb->s_root) {
123                 error = sysfs_fill_super(sb);
124                 if (error) {
125                         deactivate_locked_super(sb);
126                         return ERR_PTR(error);
127                 }
128                 sb->s_flags |= MS_ACTIVE;
129         }
130
131         return dget(sb->s_root);
132 }
133
134 static void sysfs_kill_sb(struct super_block *sb)
135 {
136         struct sysfs_super_info *info = sysfs_info(sb);
137         struct sysfs_dirent *root_sd = sb->s_root->d_fsdata;
138
139         /*
140          * Remove the superblock from fs_supers/s_instances
141          * so we can't find it, before freeing sysfs_super_info.
142          */
143         kill_anon_super(sb);
144         free_sysfs_super_info(info);
145         kernfs_put(root_sd);
146 }
147
148 static struct file_system_type sysfs_fs_type = {
149         .name           = "sysfs",
150         .mount          = sysfs_mount,
151         .kill_sb        = sysfs_kill_sb,
152         .fs_flags       = FS_USERNS_MOUNT,
153 };
154
155 int __init sysfs_init(void)
156 {
157         int err;
158
159         sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
160                                               sizeof(struct sysfs_dirent),
161                                               0, 0, NULL);
162         if (!sysfs_dir_cachep)
163                 return -ENOMEM;
164
165         err = sysfs_inode_init();
166         if (err)
167                 goto out_err;
168
169         sysfs_root = kernfs_create_root(NULL);
170         if (IS_ERR(sysfs_root)) {
171                 err = PTR_ERR(sysfs_root);
172                 goto out_err;
173         }
174         sysfs_root_sd = sysfs_root->sd;
175
176         err = register_filesystem(&sysfs_fs_type);
177         if (err)
178                 goto out_destroy_root;
179
180         return 0;
181
182 out_destroy_root:
183         kernfs_destroy_root(sysfs_root);
184 out_err:
185         kmem_cache_destroy(sysfs_dir_cachep);
186         sysfs_dir_cachep = NULL;
187         return err;
188 }