xref: /openbmc/linux/fs/kernfs/mount.c (revision 3213486f)
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  */
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>
17 #include <linux/namei.h>
18 #include <linux/seq_file.h>
19 #include <linux/exportfs.h>
20 
21 #include "kernfs-internal.h"
22 
23 struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
24 
25 static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
26 {
27 	struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry));
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 
35 static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
36 {
37 	struct kernfs_node *node = kernfs_dentry_node(dentry);
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 
44 	seq_dentry(sf, dentry, " \t\n\\");
45 	return 0;
46 }
47 
48 const struct super_operations kernfs_sops = {
49 	.statfs		= simple_statfs,
50 	.drop_inode	= generic_delete_inode,
51 	.evict_inode	= kernfs_evict_inode,
52 
53 	.show_options	= kernfs_sop_show_options,
54 	.show_path	= kernfs_sop_show_path,
55 };
56 
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 
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);
91 	if (!inode)
92 		return ERR_PTR(-ESTALE);
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 
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 
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);
188 	if (WARN_ON(!knparent)) {
189 		dput(dentry);
190 		return ERR_PTR(-EINVAL);
191 	}
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);
200 		if (WARN_ON(!kntmp)) {
201 			dput(dentry);
202 			return ERR_PTR(-EINVAL);
203 		}
204 		dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
205 					       strlen(kntmp->name));
206 		dput(dentry);
207 		if (IS_ERR(dtmp))
208 			return dtmp;
209 		knparent = kntmp;
210 		dentry = dtmp;
211 	} while (true);
212 }
213 
214 static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc)
215 {
216 	struct kernfs_super_info *info = kernfs_info(sb);
217 	struct inode *inode;
218 	struct dentry *root;
219 
220 	info->sb = sb;
221 	/* Userspace would break if executables or devices appear on sysfs */
222 	sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
223 	sb->s_blocksize = PAGE_SIZE;
224 	sb->s_blocksize_bits = PAGE_SHIFT;
225 	sb->s_magic = kfc->magic;
226 	sb->s_op = &kernfs_sops;
227 	sb->s_xattr = kernfs_xattr_handlers;
228 	if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
229 		sb->s_export_op = &kernfs_export_ops;
230 	sb->s_time_gran = 1;
231 
232 	/* sysfs dentries and inodes don't require IO to create */
233 	sb->s_shrink.seeks = 0;
234 
235 	/* get root inode, initialize and unlock it */
236 	mutex_lock(&kernfs_mutex);
237 	inode = kernfs_get_inode(sb, info->root->kn);
238 	mutex_unlock(&kernfs_mutex);
239 	if (!inode) {
240 		pr_debug("kernfs: could not get root inode\n");
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 	}
250 	sb->s_root = root;
251 	sb->s_d_op = &kernfs_dops;
252 	return 0;
253 }
254 
255 static int kernfs_test_super(struct super_block *sb, struct fs_context *fc)
256 {
257 	struct kernfs_super_info *sb_info = kernfs_info(sb);
258 	struct kernfs_super_info *info = fc->s_fs_info;
259 
260 	return sb_info->root == info->root && sb_info->ns == info->ns;
261 }
262 
263 static int kernfs_set_super(struct super_block *sb, struct fs_context *fc)
264 {
265 	struct kernfs_fs_context *kfc = fc->fs_private;
266 
267 	kfc->ns_tag = NULL;
268 	return set_anon_super_fc(sb, fc);
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 {
279 	struct kernfs_super_info *info = kernfs_info(sb);
280 
281 	return info->ns;
282 }
283 
284 /**
285  * kernfs_get_tree - kernfs filesystem access/retrieval helper
286  * @fc: The filesystem context.
287  *
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.
292  */
293 int kernfs_get_tree(struct fs_context *fc)
294 {
295 	struct kernfs_fs_context *kfc = fc->fs_private;
296 	struct super_block *sb;
297 	struct kernfs_super_info *info;
298 	int error;
299 
300 	info = kzalloc(sizeof(*info), GFP_KERNEL);
301 	if (!info)
302 		return -ENOMEM;
303 
304 	info->root = kfc->root;
305 	info->ns = kfc->ns_tag;
306 	INIT_LIST_HEAD(&info->node);
307 
308 	fc->s_fs_info = info;
309 	sb = sget_fc(fc, kernfs_test_super, kernfs_set_super);
310 	if (IS_ERR(sb))
311 		return PTR_ERR(sb);
312 
313 	if (!sb->s_root) {
314 		struct kernfs_super_info *info = kernfs_info(sb);
315 
316 		kfc->new_sb_created = true;
317 
318 		error = kernfs_fill_super(sb, kfc);
319 		if (error) {
320 			deactivate_locked_super(sb);
321 			return error;
322 		}
323 		sb->s_flags |= SB_ACTIVE;
324 
325 		mutex_lock(&kernfs_mutex);
326 		list_add(&info->node, &info->root->supers);
327 		mutex_unlock(&kernfs_mutex);
328 	}
329 
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;
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 {
351 	struct kernfs_super_info *info = kernfs_info(sb);
352 
353 	mutex_lock(&kernfs_mutex);
354 	list_del(&info->node);
355 	mutex_unlock(&kernfs_mutex);
356 
357 	/*
358 	 * Remove the superblock from fs_supers/s_instances
359 	 * so we can't find it, before freeing kernfs_super_info.
360 	 */
361 	kill_anon_super(sb);
362 	kfree(info);
363 }
364 
365 void __init kernfs_init(void)
366 {
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 	 */
374 	kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
375 					      sizeof(struct kernfs_node),
376 					      0,
377 					      SLAB_PANIC | SLAB_TYPESAFE_BY_RCU,
378 					      NULL);
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);
384 }
385