Commit | Line | Data |
---|---|---|
b8441ed2 TH |
1 | /* |
2 | * fs/kernfs/mount.c - kernfs mount implementation | |
3 | * | |
4 | * Copyright (c) 2001-3 Patrick Mochel | |
5 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
6 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> | |
7 | * | |
8 | * This file is released under the GPLv2. | |
9 | */ | |
fa736a95 TH |
10 | |
11 | #include <linux/fs.h> | |
12 | #include <linux/mount.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/magic.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/pagemap.h> | |
fb3c8315 | 17 | #include <linux/namei.h> |
4f41fc59 | 18 | #include <linux/seq_file.h> |
aa818825 | 19 | #include <linux/exportfs.h> |
fa736a95 TH |
20 | |
21 | #include "kernfs-internal.h" | |
22 | ||
26e28d68 | 23 | struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache; |
fa736a95 | 24 | |
6a7fed4e TH |
25 | static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry) |
26 | { | |
319ba91d | 27 | struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry)); |
6a7fed4e TH |
28 | struct kernfs_syscall_ops *scops = root->syscall_ops; |
29 | ||
30 | if (scops && scops->show_options) | |
31 | return scops->show_options(sf, root); | |
32 | return 0; | |
33 | } | |
34 | ||
4f41fc59 SH |
35 | static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry) |
36 | { | |
319ba91d | 37 | struct kernfs_node *node = kernfs_dentry_node(dentry); |
4f41fc59 SH |
38 | struct kernfs_root *root = kernfs_root(node); |
39 | struct kernfs_syscall_ops *scops = root->syscall_ops; | |
40 | ||
41 | if (scops && scops->show_path) | |
42 | return scops->show_path(sf, node, root); | |
43 | ||
3cc9b23c SH |
44 | seq_dentry(sf, dentry, " \t\n\\"); |
45 | return 0; | |
4f41fc59 SH |
46 | } |
47 | ||
f41c5934 | 48 | const struct super_operations kernfs_sops = { |
fa736a95 TH |
49 | .statfs = simple_statfs, |
50 | .drop_inode = generic_delete_inode, | |
c637b8ac | 51 | .evict_inode = kernfs_evict_inode, |
6a7fed4e | 52 | |
6a7fed4e | 53 | .show_options = kernfs_sop_show_options, |
4f41fc59 | 54 | .show_path = kernfs_sop_show_path, |
fa736a95 TH |
55 | }; |
56 | ||
69fd5c39 SL |
57 | /* |
58 | * Similar to kernfs_fh_get_inode, this one gets kernfs node from inode | |
59 | * number and generation | |
60 | */ | |
61 | struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root, | |
62 | const union kernfs_node_id *id) | |
63 | { | |
64 | struct kernfs_node *kn; | |
65 | ||
66 | kn = kernfs_find_and_get_node_by_ino(root, id->ino); | |
67 | if (!kn) | |
68 | return NULL; | |
69 | if (kn->id.generation != id->generation) { | |
70 | kernfs_put(kn); | |
71 | return NULL; | |
72 | } | |
73 | return kn; | |
74 | } | |
75 | ||
aa818825 SL |
76 | static struct inode *kernfs_fh_get_inode(struct super_block *sb, |
77 | u64 ino, u32 generation) | |
78 | { | |
79 | struct kernfs_super_info *info = kernfs_info(sb); | |
80 | struct inode *inode; | |
81 | struct kernfs_node *kn; | |
82 | ||
83 | if (ino == 0) | |
84 | return ERR_PTR(-ESTALE); | |
85 | ||
86 | kn = kernfs_find_and_get_node_by_ino(info->root, ino); | |
87 | if (!kn) | |
88 | return ERR_PTR(-ESTALE); | |
89 | inode = kernfs_get_inode(sb, kn); | |
90 | kernfs_put(kn); | |
ef13ecbc DC |
91 | if (!inode) |
92 | return ERR_PTR(-ESTALE); | |
aa818825 SL |
93 | |
94 | if (generation && inode->i_generation != generation) { | |
95 | /* we didn't find the right inode.. */ | |
96 | iput(inode); | |
97 | return ERR_PTR(-ESTALE); | |
98 | } | |
99 | return inode; | |
100 | } | |
101 | ||
102 | static struct dentry *kernfs_fh_to_dentry(struct super_block *sb, struct fid *fid, | |
103 | int fh_len, int fh_type) | |
104 | { | |
105 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, | |
106 | kernfs_fh_get_inode); | |
107 | } | |
108 | ||
109 | static struct dentry *kernfs_fh_to_parent(struct super_block *sb, struct fid *fid, | |
110 | int fh_len, int fh_type) | |
111 | { | |
112 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, | |
113 | kernfs_fh_get_inode); | |
114 | } | |
115 | ||
116 | static struct dentry *kernfs_get_parent_dentry(struct dentry *child) | |
117 | { | |
118 | struct kernfs_node *kn = kernfs_dentry_node(child); | |
119 | ||
120 | return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent)); | |
121 | } | |
122 | ||
123 | static const struct export_operations kernfs_export_ops = { | |
124 | .fh_to_dentry = kernfs_fh_to_dentry, | |
125 | .fh_to_parent = kernfs_fh_to_parent, | |
126 | .get_parent = kernfs_get_parent_dentry, | |
127 | }; | |
128 | ||
0c23b225 TH |
129 | /** |
130 | * kernfs_root_from_sb - determine kernfs_root associated with a super_block | |
131 | * @sb: the super_block in question | |
132 | * | |
133 | * Return the kernfs_root associated with @sb. If @sb is not a kernfs one, | |
134 | * %NULL is returned. | |
135 | */ | |
136 | struct kernfs_root *kernfs_root_from_sb(struct super_block *sb) | |
137 | { | |
138 | if (sb->s_op == &kernfs_sops) | |
139 | return kernfs_info(sb)->root; | |
140 | return NULL; | |
141 | } | |
142 | ||
fb3c8315 AK |
143 | /* |
144 | * find the next ancestor in the path down to @child, where @parent was the | |
145 | * ancestor whose descendant we want to find. | |
146 | * | |
147 | * Say the path is /a/b/c/d. @child is d, @parent is NULL. We return the root | |
148 | * node. If @parent is b, then we return the node for c. | |
149 | * Passing in d as @parent is not ok. | |
150 | */ | |
151 | static struct kernfs_node *find_next_ancestor(struct kernfs_node *child, | |
152 | struct kernfs_node *parent) | |
153 | { | |
154 | if (child == parent) { | |
155 | pr_crit_once("BUG in find_next_ancestor: called with parent == child"); | |
156 | return NULL; | |
157 | } | |
158 | ||
159 | while (child->parent != parent) { | |
160 | if (!child->parent) | |
161 | return NULL; | |
162 | child = child->parent; | |
163 | } | |
164 | ||
165 | return child; | |
166 | } | |
167 | ||
168 | /** | |
169 | * kernfs_node_dentry - get a dentry for the given kernfs_node | |
170 | * @kn: kernfs_node for which a dentry is needed | |
171 | * @sb: the kernfs super_block | |
172 | */ | |
173 | struct dentry *kernfs_node_dentry(struct kernfs_node *kn, | |
174 | struct super_block *sb) | |
175 | { | |
176 | struct dentry *dentry; | |
177 | struct kernfs_node *knparent = NULL; | |
178 | ||
179 | BUG_ON(sb->s_op != &kernfs_sops); | |
180 | ||
181 | dentry = dget(sb->s_root); | |
182 | ||
183 | /* Check if this is the root kernfs_node */ | |
184 | if (!kn->parent) | |
185 | return dentry; | |
186 | ||
187 | knparent = find_next_ancestor(kn, NULL); | |
399504e2 AV |
188 | if (WARN_ON(!knparent)) { |
189 | dput(dentry); | |
fb3c8315 | 190 | return ERR_PTR(-EINVAL); |
399504e2 | 191 | } |
fb3c8315 AK |
192 | |
193 | do { | |
194 | struct dentry *dtmp; | |
195 | struct kernfs_node *kntmp; | |
196 | ||
197 | if (kn == knparent) | |
198 | return dentry; | |
199 | kntmp = find_next_ancestor(kn, knparent); | |
399504e2 AV |
200 | if (WARN_ON(!kntmp)) { |
201 | dput(dentry); | |
fb3c8315 | 202 | return ERR_PTR(-EINVAL); |
399504e2 | 203 | } |
779b8391 AV |
204 | dtmp = lookup_one_len_unlocked(kntmp->name, dentry, |
205 | strlen(kntmp->name)); | |
fb3c8315 AK |
206 | dput(dentry); |
207 | if (IS_ERR(dtmp)) | |
208 | return dtmp; | |
209 | knparent = kntmp; | |
210 | dentry = dtmp; | |
211 | } while (true); | |
212 | } | |
213 | ||
23bf1b6b | 214 | static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc) |
fa736a95 | 215 | { |
c525aadd | 216 | struct kernfs_super_info *info = kernfs_info(sb); |
fa736a95 TH |
217 | struct inode *inode; |
218 | struct dentry *root; | |
219 | ||
7d568a83 | 220 | info->sb = sb; |
a2982cc9 EB |
221 | /* Userspace would break if executables or devices appear on sysfs */ |
222 | sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV; | |
09cbfeaf KS |
223 | sb->s_blocksize = PAGE_SIZE; |
224 | sb->s_blocksize_bits = PAGE_SHIFT; | |
23bf1b6b | 225 | sb->s_magic = kfc->magic; |
a797bfc3 | 226 | sb->s_op = &kernfs_sops; |
e72a1a8b | 227 | sb->s_xattr = kernfs_xattr_handlers; |
aa818825 SL |
228 | if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP) |
229 | sb->s_export_op = &kernfs_export_ops; | |
fa736a95 TH |
230 | sb->s_time_gran = 1; |
231 | ||
4b85afbd JW |
232 | /* sysfs dentries and inodes don't require IO to create */ |
233 | sb->s_shrink.seeks = 0; | |
234 | ||
fa736a95 | 235 | /* get root inode, initialize and unlock it */ |
a797bfc3 | 236 | mutex_lock(&kernfs_mutex); |
c637b8ac | 237 | inode = kernfs_get_inode(sb, info->root->kn); |
a797bfc3 | 238 | mutex_unlock(&kernfs_mutex); |
fa736a95 | 239 | if (!inode) { |
c637b8ac | 240 | pr_debug("kernfs: could not get root inode\n"); |
fa736a95 TH |
241 | return -ENOMEM; |
242 | } | |
243 | ||
244 | /* instantiate and link root dentry */ | |
245 | root = d_make_root(inode); | |
246 | if (!root) { | |
247 | pr_debug("%s: could not get root dentry!\n", __func__); | |
248 | return -ENOMEM; | |
249 | } | |
fa736a95 | 250 | sb->s_root = root; |
a797bfc3 | 251 | sb->s_d_op = &kernfs_dops; |
fa736a95 TH |
252 | return 0; |
253 | } | |
254 | ||
23bf1b6b | 255 | static int kernfs_test_super(struct super_block *sb, struct fs_context *fc) |
fa736a95 | 256 | { |
c525aadd | 257 | struct kernfs_super_info *sb_info = kernfs_info(sb); |
23bf1b6b | 258 | struct kernfs_super_info *info = fc->s_fs_info; |
fa736a95 TH |
259 | |
260 | return sb_info->root == info->root && sb_info->ns == info->ns; | |
261 | } | |
262 | ||
23bf1b6b | 263 | static int kernfs_set_super(struct super_block *sb, struct fs_context *fc) |
fa736a95 | 264 | { |
23bf1b6b DH |
265 | struct kernfs_fs_context *kfc = fc->fs_private; |
266 | ||
267 | kfc->ns_tag = NULL; | |
268 | return set_anon_super_fc(sb, fc); | |
fa736a95 TH |
269 | } |
270 | ||
271 | /** | |
272 | * kernfs_super_ns - determine the namespace tag of a kernfs super_block | |
273 | * @sb: super_block of interest | |
274 | * | |
275 | * Return the namespace tag associated with kernfs super_block @sb. | |
276 | */ | |
277 | const void *kernfs_super_ns(struct super_block *sb) | |
278 | { | |
c525aadd | 279 | struct kernfs_super_info *info = kernfs_info(sb); |
fa736a95 TH |
280 | |
281 | return info->ns; | |
282 | } | |
283 | ||
284 | /** | |
23bf1b6b DH |
285 | * kernfs_get_tree - kernfs filesystem access/retrieval helper |
286 | * @fc: The filesystem context. | |
fa736a95 | 287 | * |
23bf1b6b DH |
288 | * This is to be called from each kernfs user's fs_context->ops->get_tree() |
289 | * implementation, which should set the specified ->@fs_type and ->@flags, and | |
290 | * specify the hierarchy and namespace tag to mount via ->@root and ->@ns, | |
291 | * respectively. | |
fa736a95 | 292 | */ |
23bf1b6b | 293 | int kernfs_get_tree(struct fs_context *fc) |
fa736a95 | 294 | { |
23bf1b6b | 295 | struct kernfs_fs_context *kfc = fc->fs_private; |
fa736a95 | 296 | struct super_block *sb; |
c525aadd | 297 | struct kernfs_super_info *info; |
fa736a95 TH |
298 | int error; |
299 | ||
300 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
301 | if (!info) | |
23bf1b6b | 302 | return -ENOMEM; |
fa736a95 | 303 | |
23bf1b6b DH |
304 | info->root = kfc->root; |
305 | info->ns = kfc->ns_tag; | |
82382ace | 306 | INIT_LIST_HEAD(&info->node); |
fa736a95 | 307 | |
23bf1b6b DH |
308 | fc->s_fs_info = info; |
309 | sb = sget_fc(fc, kernfs_test_super, kernfs_set_super); | |
fa736a95 | 310 | if (IS_ERR(sb)) |
23bf1b6b | 311 | return PTR_ERR(sb); |
fed95bab | 312 | |
fa736a95 | 313 | if (!sb->s_root) { |
7d568a83 TH |
314 | struct kernfs_super_info *info = kernfs_info(sb); |
315 | ||
23bf1b6b DH |
316 | kfc->new_sb_created = true; |
317 | ||
318 | error = kernfs_fill_super(sb, kfc); | |
fa736a95 TH |
319 | if (error) { |
320 | deactivate_locked_super(sb); | |
23bf1b6b | 321 | return error; |
fa736a95 | 322 | } |
1751e8a6 | 323 | sb->s_flags |= SB_ACTIVE; |
7d568a83 TH |
324 | |
325 | mutex_lock(&kernfs_mutex); | |
23bf1b6b | 326 | list_add(&info->node, &info->root->supers); |
7d568a83 | 327 | mutex_unlock(&kernfs_mutex); |
fa736a95 TH |
328 | } |
329 | ||
23bf1b6b DH |
330 | fc->root = dget(sb->s_root); |
331 | return 0; | |
332 | } | |
333 | ||
334 | void kernfs_free_fs_context(struct fs_context *fc) | |
335 | { | |
336 | /* Note that we don't deal with kfc->ns_tag here. */ | |
337 | kfree(fc->s_fs_info); | |
338 | fc->s_fs_info = NULL; | |
fa736a95 TH |
339 | } |
340 | ||
341 | /** | |
342 | * kernfs_kill_sb - kill_sb for kernfs | |
343 | * @sb: super_block being killed | |
344 | * | |
345 | * This can be used directly for file_system_type->kill_sb(). If a kernfs | |
346 | * user needs extra cleanup, it can implement its own kill_sb() and call | |
347 | * this function at the end. | |
348 | */ | |
349 | void kernfs_kill_sb(struct super_block *sb) | |
350 | { | |
c525aadd | 351 | struct kernfs_super_info *info = kernfs_info(sb); |
fa736a95 | 352 | |
7d568a83 TH |
353 | mutex_lock(&kernfs_mutex); |
354 | list_del(&info->node); | |
355 | mutex_unlock(&kernfs_mutex); | |
356 | ||
fa736a95 TH |
357 | /* |
358 | * Remove the superblock from fs_supers/s_instances | |
c525aadd | 359 | * so we can't find it, before freeing kernfs_super_info. |
fa736a95 TH |
360 | */ |
361 | kill_anon_super(sb); | |
362 | kfree(info); | |
fa736a95 TH |
363 | } |
364 | ||
365 | void __init kernfs_init(void) | |
366 | { | |
ba16b284 SL |
367 | |
368 | /* | |
369 | * the slab is freed in RCU context, so kernfs_find_and_get_node_by_ino | |
370 | * can access the slab lock free. This could introduce stale nodes, | |
371 | * please see how kernfs_find_and_get_node_by_ino filters out stale | |
372 | * nodes. | |
373 | */ | |
a797bfc3 | 374 | kernfs_node_cache = kmem_cache_create("kernfs_node_cache", |
324a56e1 | 375 | sizeof(struct kernfs_node), |
ba16b284 SL |
376 | 0, |
377 | SLAB_PANIC | SLAB_TYPESAFE_BY_RCU, | |
378 | NULL); | |
26e28d68 AM |
379 | |
380 | /* Creates slab cache for kernfs inode attributes */ | |
381 | kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache", | |
382 | sizeof(struct kernfs_iattrs), | |
383 | 0, SLAB_PANIC, NULL); | |
fa736a95 | 384 | } |