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