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