xref: /openbmc/linux/fs/kernfs/dir.c (revision 483eb062)
1 /*
2  * fs/kernfs/dir.c - kernfs directory 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/namei.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/security.h>
16 #include <linux/hash.h>
17 
18 #include "kernfs-internal.h"
19 
20 DEFINE_MUTEX(kernfs_mutex);
21 
22 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
23 
24 /**
25  *	kernfs_name_hash
26  *	@name: Null terminated string to hash
27  *	@ns:   Namespace tag to hash
28  *
29  *	Returns 31 bit hash of ns + name (so it fits in an off_t )
30  */
31 static unsigned int kernfs_name_hash(const char *name, const void *ns)
32 {
33 	unsigned long hash = init_name_hash();
34 	unsigned int len = strlen(name);
35 	while (len--)
36 		hash = partial_name_hash(*name++, hash);
37 	hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
38 	hash &= 0x7fffffffU;
39 	/* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
40 	if (hash < 1)
41 		hash += 2;
42 	if (hash >= INT_MAX)
43 		hash = INT_MAX - 1;
44 	return hash;
45 }
46 
47 static int kernfs_name_compare(unsigned int hash, const char *name,
48 			       const void *ns, const struct kernfs_node *kn)
49 {
50 	if (hash != kn->hash)
51 		return hash - kn->hash;
52 	if (ns != kn->ns)
53 		return ns - kn->ns;
54 	return strcmp(name, kn->name);
55 }
56 
57 static int kernfs_sd_compare(const struct kernfs_node *left,
58 			     const struct kernfs_node *right)
59 {
60 	return kernfs_name_compare(left->hash, left->name, left->ns, right);
61 }
62 
63 /**
64  *	kernfs_link_sibling - link kernfs_node into sibling rbtree
65  *	@kn: kernfs_node of interest
66  *
67  *	Link @kn into its sibling rbtree which starts from
68  *	@kn->parent->dir.children.
69  *
70  *	Locking:
71  *	mutex_lock(kernfs_mutex)
72  *
73  *	RETURNS:
74  *	0 on susccess -EEXIST on failure.
75  */
76 static int kernfs_link_sibling(struct kernfs_node *kn)
77 {
78 	struct rb_node **node = &kn->parent->dir.children.rb_node;
79 	struct rb_node *parent = NULL;
80 
81 	if (kernfs_type(kn) == KERNFS_DIR)
82 		kn->parent->dir.subdirs++;
83 
84 	while (*node) {
85 		struct kernfs_node *pos;
86 		int result;
87 
88 		pos = rb_to_kn(*node);
89 		parent = *node;
90 		result = kernfs_sd_compare(kn, pos);
91 		if (result < 0)
92 			node = &pos->rb.rb_left;
93 		else if (result > 0)
94 			node = &pos->rb.rb_right;
95 		else
96 			return -EEXIST;
97 	}
98 	/* add new node and rebalance the tree */
99 	rb_link_node(&kn->rb, parent, node);
100 	rb_insert_color(&kn->rb, &kn->parent->dir.children);
101 	return 0;
102 }
103 
104 /**
105  *	kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
106  *	@kn: kernfs_node of interest
107  *
108  *	Unlink @kn from its sibling rbtree which starts from
109  *	kn->parent->dir.children.
110  *
111  *	Locking:
112  *	mutex_lock(kernfs_mutex)
113  */
114 static void kernfs_unlink_sibling(struct kernfs_node *kn)
115 {
116 	if (kernfs_type(kn) == KERNFS_DIR)
117 		kn->parent->dir.subdirs--;
118 
119 	rb_erase(&kn->rb, &kn->parent->dir.children);
120 }
121 
122 /**
123  *	kernfs_get_active - get an active reference to kernfs_node
124  *	@kn: kernfs_node to get an active reference to
125  *
126  *	Get an active reference of @kn.  This function is noop if @kn
127  *	is NULL.
128  *
129  *	RETURNS:
130  *	Pointer to @kn on success, NULL on failure.
131  */
132 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
133 {
134 	if (unlikely(!kn))
135 		return NULL;
136 
137 	if (!atomic_inc_unless_negative(&kn->active))
138 		return NULL;
139 
140 	if (kn->flags & KERNFS_LOCKDEP)
141 		rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
142 	return kn;
143 }
144 
145 /**
146  *	kernfs_put_active - put an active reference to kernfs_node
147  *	@kn: kernfs_node to put an active reference to
148  *
149  *	Put an active reference to @kn.  This function is noop if @kn
150  *	is NULL.
151  */
152 void kernfs_put_active(struct kernfs_node *kn)
153 {
154 	int v;
155 
156 	if (unlikely(!kn))
157 		return;
158 
159 	if (kn->flags & KERNFS_LOCKDEP)
160 		rwsem_release(&kn->dep_map, 1, _RET_IP_);
161 	v = atomic_dec_return(&kn->active);
162 	if (likely(v != KN_DEACTIVATED_BIAS))
163 		return;
164 
165 	/*
166 	 * atomic_dec_return() is a mb(), we'll always see the updated
167 	 * kn->u.completion.
168 	 */
169 	complete(kn->u.completion);
170 }
171 
172 /**
173  *	kernfs_deactivate - deactivate kernfs_node
174  *	@kn: kernfs_node to deactivate
175  *
176  *	Deny new active references and drain existing ones.
177  */
178 static void kernfs_deactivate(struct kernfs_node *kn)
179 {
180 	DECLARE_COMPLETION_ONSTACK(wait);
181 	int v;
182 
183 	BUG_ON(!(kn->flags & KERNFS_REMOVED));
184 
185 	if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
186 		return;
187 
188 	kn->u.completion = (void *)&wait;
189 
190 	rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
191 	/* atomic_add_return() is a mb(), put_active() will always see
192 	 * the updated kn->u.completion.
193 	 */
194 	v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
195 
196 	if (v != KN_DEACTIVATED_BIAS) {
197 		lock_contended(&kn->dep_map, _RET_IP_);
198 		wait_for_completion(&wait);
199 	}
200 
201 	lock_acquired(&kn->dep_map, _RET_IP_);
202 	rwsem_release(&kn->dep_map, 1, _RET_IP_);
203 }
204 
205 /**
206  * kernfs_get - get a reference count on a kernfs_node
207  * @kn: the target kernfs_node
208  */
209 void kernfs_get(struct kernfs_node *kn)
210 {
211 	if (kn) {
212 		WARN_ON(!atomic_read(&kn->count));
213 		atomic_inc(&kn->count);
214 	}
215 }
216 EXPORT_SYMBOL_GPL(kernfs_get);
217 
218 /**
219  * kernfs_put - put a reference count on a kernfs_node
220  * @kn: the target kernfs_node
221  *
222  * Put a reference count of @kn and destroy it if it reached zero.
223  */
224 void kernfs_put(struct kernfs_node *kn)
225 {
226 	struct kernfs_node *parent;
227 	struct kernfs_root *root;
228 
229 	if (!kn || !atomic_dec_and_test(&kn->count))
230 		return;
231 	root = kernfs_root(kn);
232  repeat:
233 	/* Moving/renaming is always done while holding reference.
234 	 * kn->parent won't change beneath us.
235 	 */
236 	parent = kn->parent;
237 
238 	WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
239 	     parent ? parent->name : "", kn->name);
240 
241 	if (kernfs_type(kn) == KERNFS_LINK)
242 		kernfs_put(kn->symlink.target_kn);
243 	if (!(kn->flags & KERNFS_STATIC_NAME))
244 		kfree(kn->name);
245 	if (kn->iattr) {
246 		if (kn->iattr->ia_secdata)
247 			security_release_secctx(kn->iattr->ia_secdata,
248 						kn->iattr->ia_secdata_len);
249 		simple_xattrs_free(&kn->iattr->xattrs);
250 	}
251 	kfree(kn->iattr);
252 	ida_simple_remove(&root->ino_ida, kn->ino);
253 	kmem_cache_free(kernfs_node_cache, kn);
254 
255 	kn = parent;
256 	if (kn) {
257 		if (atomic_dec_and_test(&kn->count))
258 			goto repeat;
259 	} else {
260 		/* just released the root kn, free @root too */
261 		ida_destroy(&root->ino_ida);
262 		kfree(root);
263 	}
264 }
265 EXPORT_SYMBOL_GPL(kernfs_put);
266 
267 static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
268 {
269 	struct kernfs_node *kn;
270 
271 	if (flags & LOOKUP_RCU)
272 		return -ECHILD;
273 
274 	/* Always perform fresh lookup for negatives */
275 	if (!dentry->d_inode)
276 		goto out_bad_unlocked;
277 
278 	kn = dentry->d_fsdata;
279 	mutex_lock(&kernfs_mutex);
280 
281 	/* The kernfs node has been deleted */
282 	if (kn->flags & KERNFS_REMOVED)
283 		goto out_bad;
284 
285 	/* The kernfs node has been moved? */
286 	if (dentry->d_parent->d_fsdata != kn->parent)
287 		goto out_bad;
288 
289 	/* The kernfs node has been renamed */
290 	if (strcmp(dentry->d_name.name, kn->name) != 0)
291 		goto out_bad;
292 
293 	/* The kernfs node has been moved to a different namespace */
294 	if (kn->parent && kernfs_ns_enabled(kn->parent) &&
295 	    kernfs_info(dentry->d_sb)->ns != kn->ns)
296 		goto out_bad;
297 
298 	mutex_unlock(&kernfs_mutex);
299 out_valid:
300 	return 1;
301 out_bad:
302 	mutex_unlock(&kernfs_mutex);
303 out_bad_unlocked:
304 	/*
305 	 * @dentry doesn't match the underlying kernfs node, drop the
306 	 * dentry and force lookup.  If we have submounts we must allow the
307 	 * vfs caches to lie about the state of the filesystem to prevent
308 	 * leaks and other nasty things, so use check_submounts_and_drop()
309 	 * instead of d_drop().
310 	 */
311 	if (check_submounts_and_drop(dentry) != 0)
312 		goto out_valid;
313 
314 	return 0;
315 }
316 
317 static void kernfs_dop_release(struct dentry *dentry)
318 {
319 	kernfs_put(dentry->d_fsdata);
320 }
321 
322 const struct dentry_operations kernfs_dops = {
323 	.d_revalidate	= kernfs_dop_revalidate,
324 	.d_release	= kernfs_dop_release,
325 };
326 
327 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
328 					     const char *name, umode_t mode,
329 					     unsigned flags)
330 {
331 	char *dup_name = NULL;
332 	struct kernfs_node *kn;
333 	int ret;
334 
335 	if (!(flags & KERNFS_STATIC_NAME)) {
336 		name = dup_name = kstrdup(name, GFP_KERNEL);
337 		if (!name)
338 			return NULL;
339 	}
340 
341 	kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
342 	if (!kn)
343 		goto err_out1;
344 
345 	ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
346 	if (ret < 0)
347 		goto err_out2;
348 	kn->ino = ret;
349 
350 	atomic_set(&kn->count, 1);
351 	atomic_set(&kn->active, 0);
352 
353 	kn->name = name;
354 	kn->mode = mode;
355 	kn->flags = flags | KERNFS_REMOVED;
356 
357 	return kn;
358 
359  err_out2:
360 	kmem_cache_free(kernfs_node_cache, kn);
361  err_out1:
362 	kfree(dup_name);
363 	return NULL;
364 }
365 
366 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
367 				    const char *name, umode_t mode,
368 				    unsigned flags)
369 {
370 	struct kernfs_node *kn;
371 
372 	kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
373 	if (kn) {
374 		kernfs_get(parent);
375 		kn->parent = parent;
376 	}
377 	return kn;
378 }
379 
380 /**
381  *	kernfs_addrm_start - prepare for kernfs_node add/remove
382  *	@acxt: pointer to kernfs_addrm_cxt to be used
383  *
384  *	This function is called when the caller is about to add or remove
385  *	kernfs_node.  This function acquires kernfs_mutex.  @acxt is used
386  *	to keep and pass context to other addrm functions.
387  *
388  *	LOCKING:
389  *	Kernel thread context (may sleep).  kernfs_mutex is locked on
390  *	return.
391  */
392 void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
393 	__acquires(kernfs_mutex)
394 {
395 	memset(acxt, 0, sizeof(*acxt));
396 
397 	mutex_lock(&kernfs_mutex);
398 }
399 
400 /**
401  *	kernfs_add_one - add kernfs_node to parent without warning
402  *	@acxt: addrm context to use
403  *	@kn: kernfs_node to be added
404  *
405  *	The caller must already have initialized @kn->parent.  This
406  *	function increments nlink of the parent's inode if @kn is a
407  *	directory and link into the children list of the parent.
408  *
409  *	This function should be called between calls to
410  *	kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
411  *	the same @acxt as passed to kernfs_addrm_start().
412  *
413  *	LOCKING:
414  *	Determined by kernfs_addrm_start().
415  *
416  *	RETURNS:
417  *	0 on success, -EEXIST if entry with the given name already
418  *	exists.
419  */
420 int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
421 {
422 	struct kernfs_node *parent = kn->parent;
423 	bool has_ns = kernfs_ns_enabled(parent);
424 	struct kernfs_iattrs *ps_iattr;
425 	int ret;
426 
427 	if (has_ns != (bool)kn->ns) {
428 		WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
429 		     has_ns ? "required" : "invalid", parent->name, kn->name);
430 		return -EINVAL;
431 	}
432 
433 	if (kernfs_type(parent) != KERNFS_DIR)
434 		return -EINVAL;
435 
436 	if (parent->flags & KERNFS_REMOVED)
437 		return -ENOENT;
438 
439 	kn->hash = kernfs_name_hash(kn->name, kn->ns);
440 
441 	ret = kernfs_link_sibling(kn);
442 	if (ret)
443 		return ret;
444 
445 	/* Update timestamps on the parent */
446 	ps_iattr = parent->iattr;
447 	if (ps_iattr) {
448 		struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
449 		ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
450 	}
451 
452 	/* Mark the entry added into directory tree */
453 	kn->flags &= ~KERNFS_REMOVED;
454 
455 	return 0;
456 }
457 
458 /**
459  *	kernfs_remove_one - remove kernfs_node from parent
460  *	@acxt: addrm context to use
461  *	@kn: kernfs_node to be removed
462  *
463  *	Mark @kn removed and drop nlink of parent inode if @kn is a
464  *	directory.  @kn is unlinked from the children list.
465  *
466  *	This function should be called between calls to
467  *	kernfs_addrm_start() and kernfs_addrm_finish() and should be
468  *	passed the same @acxt as passed to kernfs_addrm_start().
469  *
470  *	LOCKING:
471  *	Determined by kernfs_addrm_start().
472  */
473 static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
474 			      struct kernfs_node *kn)
475 {
476 	struct kernfs_iattrs *ps_iattr;
477 
478 	/*
479 	 * Removal can be called multiple times on the same node.  Only the
480 	 * first invocation is effective and puts the base ref.
481 	 */
482 	if (kn->flags & KERNFS_REMOVED)
483 		return;
484 
485 	if (kn->parent) {
486 		kernfs_unlink_sibling(kn);
487 
488 		/* Update timestamps on the parent */
489 		ps_iattr = kn->parent->iattr;
490 		if (ps_iattr) {
491 			ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
492 			ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
493 		}
494 	}
495 
496 	kn->flags |= KERNFS_REMOVED;
497 	kn->u.removed_list = acxt->removed;
498 	acxt->removed = kn;
499 }
500 
501 /**
502  *	kernfs_addrm_finish - finish up kernfs_node add/remove
503  *	@acxt: addrm context to finish up
504  *
505  *	Finish up kernfs_node add/remove.  Resources acquired by
506  *	kernfs_addrm_start() are released and removed kernfs_nodes are
507  *	cleaned up.
508  *
509  *	LOCKING:
510  *	kernfs_mutex is released.
511  */
512 void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
513 	__releases(kernfs_mutex)
514 {
515 	/* release resources acquired by kernfs_addrm_start() */
516 	mutex_unlock(&kernfs_mutex);
517 
518 	/* kill removed kernfs_nodes */
519 	while (acxt->removed) {
520 		struct kernfs_node *kn = acxt->removed;
521 
522 		acxt->removed = kn->u.removed_list;
523 
524 		kernfs_deactivate(kn);
525 		kernfs_unmap_bin_file(kn);
526 		kernfs_put(kn);
527 	}
528 }
529 
530 /**
531  * kernfs_find_ns - find kernfs_node with the given name
532  * @parent: kernfs_node to search under
533  * @name: name to look for
534  * @ns: the namespace tag to use
535  *
536  * Look for kernfs_node with name @name under @parent.  Returns pointer to
537  * the found kernfs_node on success, %NULL on failure.
538  */
539 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
540 					  const unsigned char *name,
541 					  const void *ns)
542 {
543 	struct rb_node *node = parent->dir.children.rb_node;
544 	bool has_ns = kernfs_ns_enabled(parent);
545 	unsigned int hash;
546 
547 	lockdep_assert_held(&kernfs_mutex);
548 
549 	if (has_ns != (bool)ns) {
550 		WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
551 		     has_ns ? "required" : "invalid", parent->name, name);
552 		return NULL;
553 	}
554 
555 	hash = kernfs_name_hash(name, ns);
556 	while (node) {
557 		struct kernfs_node *kn;
558 		int result;
559 
560 		kn = rb_to_kn(node);
561 		result = kernfs_name_compare(hash, name, ns, kn);
562 		if (result < 0)
563 			node = node->rb_left;
564 		else if (result > 0)
565 			node = node->rb_right;
566 		else
567 			return kn;
568 	}
569 	return NULL;
570 }
571 
572 /**
573  * kernfs_find_and_get_ns - find and get kernfs_node with the given name
574  * @parent: kernfs_node to search under
575  * @name: name to look for
576  * @ns: the namespace tag to use
577  *
578  * Look for kernfs_node with name @name under @parent and get a reference
579  * if found.  This function may sleep and returns pointer to the found
580  * kernfs_node on success, %NULL on failure.
581  */
582 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
583 					   const char *name, const void *ns)
584 {
585 	struct kernfs_node *kn;
586 
587 	mutex_lock(&kernfs_mutex);
588 	kn = kernfs_find_ns(parent, name, ns);
589 	kernfs_get(kn);
590 	mutex_unlock(&kernfs_mutex);
591 
592 	return kn;
593 }
594 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
595 
596 /**
597  * kernfs_create_root - create a new kernfs hierarchy
598  * @kdops: optional directory syscall operations for the hierarchy
599  * @priv: opaque data associated with the new directory
600  *
601  * Returns the root of the new hierarchy on success, ERR_PTR() value on
602  * failure.
603  */
604 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
605 {
606 	struct kernfs_root *root;
607 	struct kernfs_node *kn;
608 
609 	root = kzalloc(sizeof(*root), GFP_KERNEL);
610 	if (!root)
611 		return ERR_PTR(-ENOMEM);
612 
613 	ida_init(&root->ino_ida);
614 
615 	kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
616 			       KERNFS_DIR);
617 	if (!kn) {
618 		ida_destroy(&root->ino_ida);
619 		kfree(root);
620 		return ERR_PTR(-ENOMEM);
621 	}
622 
623 	kn->flags &= ~KERNFS_REMOVED;
624 	kn->priv = priv;
625 	kn->dir.root = root;
626 
627 	root->dir_ops = kdops;
628 	root->kn = kn;
629 
630 	return root;
631 }
632 
633 /**
634  * kernfs_destroy_root - destroy a kernfs hierarchy
635  * @root: root of the hierarchy to destroy
636  *
637  * Destroy the hierarchy anchored at @root by removing all existing
638  * directories and destroying @root.
639  */
640 void kernfs_destroy_root(struct kernfs_root *root)
641 {
642 	kernfs_remove(root->kn);	/* will also free @root */
643 }
644 
645 /**
646  * kernfs_create_dir_ns - create a directory
647  * @parent: parent in which to create a new directory
648  * @name: name of the new directory
649  * @mode: mode of the new directory
650  * @priv: opaque data associated with the new directory
651  * @ns: optional namespace tag of the directory
652  *
653  * Returns the created node on success, ERR_PTR() value on failure.
654  */
655 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
656 					 const char *name, umode_t mode,
657 					 void *priv, const void *ns)
658 {
659 	struct kernfs_addrm_cxt acxt;
660 	struct kernfs_node *kn;
661 	int rc;
662 
663 	/* allocate */
664 	kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
665 	if (!kn)
666 		return ERR_PTR(-ENOMEM);
667 
668 	kn->dir.root = parent->dir.root;
669 	kn->ns = ns;
670 	kn->priv = priv;
671 
672 	/* link in */
673 	kernfs_addrm_start(&acxt);
674 	rc = kernfs_add_one(&acxt, kn);
675 	kernfs_addrm_finish(&acxt);
676 
677 	if (!rc)
678 		return kn;
679 
680 	kernfs_put(kn);
681 	return ERR_PTR(rc);
682 }
683 
684 static struct dentry *kernfs_iop_lookup(struct inode *dir,
685 					struct dentry *dentry,
686 					unsigned int flags)
687 {
688 	struct dentry *ret;
689 	struct kernfs_node *parent = dentry->d_parent->d_fsdata;
690 	struct kernfs_node *kn;
691 	struct inode *inode;
692 	const void *ns = NULL;
693 
694 	mutex_lock(&kernfs_mutex);
695 
696 	if (kernfs_ns_enabled(parent))
697 		ns = kernfs_info(dir->i_sb)->ns;
698 
699 	kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
700 
701 	/* no such entry */
702 	if (!kn) {
703 		ret = NULL;
704 		goto out_unlock;
705 	}
706 	kernfs_get(kn);
707 	dentry->d_fsdata = kn;
708 
709 	/* attach dentry and inode */
710 	inode = kernfs_get_inode(dir->i_sb, kn);
711 	if (!inode) {
712 		ret = ERR_PTR(-ENOMEM);
713 		goto out_unlock;
714 	}
715 
716 	/* instantiate and hash dentry */
717 	ret = d_materialise_unique(dentry, inode);
718  out_unlock:
719 	mutex_unlock(&kernfs_mutex);
720 	return ret;
721 }
722 
723 static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
724 			    umode_t mode)
725 {
726 	struct kernfs_node *parent = dir->i_private;
727 	struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
728 
729 	if (!kdops || !kdops->mkdir)
730 		return -EPERM;
731 
732 	return kdops->mkdir(parent, dentry->d_name.name, mode);
733 }
734 
735 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
736 {
737 	struct kernfs_node *kn  = dentry->d_fsdata;
738 	struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
739 
740 	if (!kdops || !kdops->rmdir)
741 		return -EPERM;
742 
743 	return kdops->rmdir(kn);
744 }
745 
746 static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
747 			     struct inode *new_dir, struct dentry *new_dentry)
748 {
749 	struct kernfs_node *kn  = old_dentry->d_fsdata;
750 	struct kernfs_node *new_parent = new_dir->i_private;
751 	struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
752 
753 	if (!kdops || !kdops->rename)
754 		return -EPERM;
755 
756 	return kdops->rename(kn, new_parent, new_dentry->d_name.name);
757 }
758 
759 const struct inode_operations kernfs_dir_iops = {
760 	.lookup		= kernfs_iop_lookup,
761 	.permission	= kernfs_iop_permission,
762 	.setattr	= kernfs_iop_setattr,
763 	.getattr	= kernfs_iop_getattr,
764 	.setxattr	= kernfs_iop_setxattr,
765 	.removexattr	= kernfs_iop_removexattr,
766 	.getxattr	= kernfs_iop_getxattr,
767 	.listxattr	= kernfs_iop_listxattr,
768 
769 	.mkdir		= kernfs_iop_mkdir,
770 	.rmdir		= kernfs_iop_rmdir,
771 	.rename		= kernfs_iop_rename,
772 };
773 
774 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
775 {
776 	struct kernfs_node *last;
777 
778 	while (true) {
779 		struct rb_node *rbn;
780 
781 		last = pos;
782 
783 		if (kernfs_type(pos) != KERNFS_DIR)
784 			break;
785 
786 		rbn = rb_first(&pos->dir.children);
787 		if (!rbn)
788 			break;
789 
790 		pos = rb_to_kn(rbn);
791 	}
792 
793 	return last;
794 }
795 
796 /**
797  * kernfs_next_descendant_post - find the next descendant for post-order walk
798  * @pos: the current position (%NULL to initiate traversal)
799  * @root: kernfs_node whose descendants to walk
800  *
801  * Find the next descendant to visit for post-order traversal of @root's
802  * descendants.  @root is included in the iteration and the last node to be
803  * visited.
804  */
805 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
806 						       struct kernfs_node *root)
807 {
808 	struct rb_node *rbn;
809 
810 	lockdep_assert_held(&kernfs_mutex);
811 
812 	/* if first iteration, visit leftmost descendant which may be root */
813 	if (!pos)
814 		return kernfs_leftmost_descendant(root);
815 
816 	/* if we visited @root, we're done */
817 	if (pos == root)
818 		return NULL;
819 
820 	/* if there's an unvisited sibling, visit its leftmost descendant */
821 	rbn = rb_next(&pos->rb);
822 	if (rbn)
823 		return kernfs_leftmost_descendant(rb_to_kn(rbn));
824 
825 	/* no sibling left, visit parent */
826 	return pos->parent;
827 }
828 
829 static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
830 			    struct kernfs_node *kn)
831 {
832 	struct kernfs_node *pos, *next;
833 
834 	if (!kn)
835 		return;
836 
837 	pr_debug("kernfs %s: removing\n", kn->name);
838 
839 	next = NULL;
840 	do {
841 		pos = next;
842 		next = kernfs_next_descendant_post(pos, kn);
843 		if (pos)
844 			kernfs_remove_one(acxt, pos);
845 	} while (next);
846 }
847 
848 /**
849  * kernfs_remove - remove a kernfs_node recursively
850  * @kn: the kernfs_node to remove
851  *
852  * Remove @kn along with all its subdirectories and files.
853  */
854 void kernfs_remove(struct kernfs_node *kn)
855 {
856 	struct kernfs_addrm_cxt acxt;
857 
858 	kernfs_addrm_start(&acxt);
859 	__kernfs_remove(&acxt, kn);
860 	kernfs_addrm_finish(&acxt);
861 }
862 
863 /**
864  * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
865  * @parent: parent of the target
866  * @name: name of the kernfs_node to remove
867  * @ns: namespace tag of the kernfs_node to remove
868  *
869  * Look for the kernfs_node with @name and @ns under @parent and remove it.
870  * Returns 0 on success, -ENOENT if such entry doesn't exist.
871  */
872 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
873 			     const void *ns)
874 {
875 	struct kernfs_addrm_cxt acxt;
876 	struct kernfs_node *kn;
877 
878 	if (!parent) {
879 		WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
880 			name);
881 		return -ENOENT;
882 	}
883 
884 	kernfs_addrm_start(&acxt);
885 
886 	kn = kernfs_find_ns(parent, name, ns);
887 	if (kn)
888 		__kernfs_remove(&acxt, kn);
889 
890 	kernfs_addrm_finish(&acxt);
891 
892 	if (kn)
893 		return 0;
894 	else
895 		return -ENOENT;
896 }
897 
898 /**
899  * kernfs_rename_ns - move and rename a kernfs_node
900  * @kn: target node
901  * @new_parent: new parent to put @sd under
902  * @new_name: new name
903  * @new_ns: new namespace tag
904  */
905 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
906 		     const char *new_name, const void *new_ns)
907 {
908 	int error;
909 
910 	mutex_lock(&kernfs_mutex);
911 
912 	error = -ENOENT;
913 	if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
914 		goto out;
915 
916 	error = 0;
917 	if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
918 	    (strcmp(kn->name, new_name) == 0))
919 		goto out;	/* nothing to rename */
920 
921 	error = -EEXIST;
922 	if (kernfs_find_ns(new_parent, new_name, new_ns))
923 		goto out;
924 
925 	/* rename kernfs_node */
926 	if (strcmp(kn->name, new_name) != 0) {
927 		error = -ENOMEM;
928 		new_name = kstrdup(new_name, GFP_KERNEL);
929 		if (!new_name)
930 			goto out;
931 
932 		if (kn->flags & KERNFS_STATIC_NAME)
933 			kn->flags &= ~KERNFS_STATIC_NAME;
934 		else
935 			kfree(kn->name);
936 
937 		kn->name = new_name;
938 	}
939 
940 	/*
941 	 * Move to the appropriate place in the appropriate directories rbtree.
942 	 */
943 	kernfs_unlink_sibling(kn);
944 	kernfs_get(new_parent);
945 	kernfs_put(kn->parent);
946 	kn->ns = new_ns;
947 	kn->hash = kernfs_name_hash(kn->name, kn->ns);
948 	kn->parent = new_parent;
949 	kernfs_link_sibling(kn);
950 
951 	error = 0;
952  out:
953 	mutex_unlock(&kernfs_mutex);
954 	return error;
955 }
956 
957 /* Relationship between s_mode and the DT_xxx types */
958 static inline unsigned char dt_type(struct kernfs_node *kn)
959 {
960 	return (kn->mode >> 12) & 15;
961 }
962 
963 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
964 {
965 	kernfs_put(filp->private_data);
966 	return 0;
967 }
968 
969 static struct kernfs_node *kernfs_dir_pos(const void *ns,
970 	struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
971 {
972 	if (pos) {
973 		int valid = !(pos->flags & KERNFS_REMOVED) &&
974 			pos->parent == parent && hash == pos->hash;
975 		kernfs_put(pos);
976 		if (!valid)
977 			pos = NULL;
978 	}
979 	if (!pos && (hash > 1) && (hash < INT_MAX)) {
980 		struct rb_node *node = parent->dir.children.rb_node;
981 		while (node) {
982 			pos = rb_to_kn(node);
983 
984 			if (hash < pos->hash)
985 				node = node->rb_left;
986 			else if (hash > pos->hash)
987 				node = node->rb_right;
988 			else
989 				break;
990 		}
991 	}
992 	/* Skip over entries in the wrong namespace */
993 	while (pos && pos->ns != ns) {
994 		struct rb_node *node = rb_next(&pos->rb);
995 		if (!node)
996 			pos = NULL;
997 		else
998 			pos = rb_to_kn(node);
999 	}
1000 	return pos;
1001 }
1002 
1003 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1004 	struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1005 {
1006 	pos = kernfs_dir_pos(ns, parent, ino, pos);
1007 	if (pos)
1008 		do {
1009 			struct rb_node *node = rb_next(&pos->rb);
1010 			if (!node)
1011 				pos = NULL;
1012 			else
1013 				pos = rb_to_kn(node);
1014 		} while (pos && pos->ns != ns);
1015 	return pos;
1016 }
1017 
1018 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1019 {
1020 	struct dentry *dentry = file->f_path.dentry;
1021 	struct kernfs_node *parent = dentry->d_fsdata;
1022 	struct kernfs_node *pos = file->private_data;
1023 	const void *ns = NULL;
1024 
1025 	if (!dir_emit_dots(file, ctx))
1026 		return 0;
1027 	mutex_lock(&kernfs_mutex);
1028 
1029 	if (kernfs_ns_enabled(parent))
1030 		ns = kernfs_info(dentry->d_sb)->ns;
1031 
1032 	for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1033 	     pos;
1034 	     pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1035 		const char *name = pos->name;
1036 		unsigned int type = dt_type(pos);
1037 		int len = strlen(name);
1038 		ino_t ino = pos->ino;
1039 
1040 		ctx->pos = pos->hash;
1041 		file->private_data = pos;
1042 		kernfs_get(pos);
1043 
1044 		mutex_unlock(&kernfs_mutex);
1045 		if (!dir_emit(ctx, name, len, ino, type))
1046 			return 0;
1047 		mutex_lock(&kernfs_mutex);
1048 	}
1049 	mutex_unlock(&kernfs_mutex);
1050 	file->private_data = NULL;
1051 	ctx->pos = INT_MAX;
1052 	return 0;
1053 }
1054 
1055 static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1056 				    int whence)
1057 {
1058 	struct inode *inode = file_inode(file);
1059 	loff_t ret;
1060 
1061 	mutex_lock(&inode->i_mutex);
1062 	ret = generic_file_llseek(file, offset, whence);
1063 	mutex_unlock(&inode->i_mutex);
1064 
1065 	return ret;
1066 }
1067 
1068 const struct file_operations kernfs_dir_fops = {
1069 	.read		= generic_read_dir,
1070 	.iterate	= kernfs_fop_readdir,
1071 	.release	= kernfs_dir_fop_release,
1072 	.llseek		= kernfs_dir_fop_llseek,
1073 };
1074