ARM: 7987/1: ARM : unwinder : Prevent data abort due to stack overflow
[linux-2.6/btrfs-unstable.git] / fs / kernfs / dir.c
blobbd6e18be6e1a231c10867bc8f4e292e49353d03f
1 /*
2 * fs/kernfs/dir.c - kernfs directory implementation
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>
8 * This file is released under the GPLv2.
9 */
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>
18 #include "kernfs-internal.h"
20 DEFINE_MUTEX(kernfs_mutex);
22 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
24 /**
25 * kernfs_name_hash
26 * @name: Null terminated string to hash
27 * @ns: Namespace tag to hash
29 * Returns 31 bit hash of ns + name (so it fits in an off_t )
31 static unsigned int kernfs_name_hash(const char *name, const void *ns)
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;
47 static int kernfs_name_compare(unsigned int hash, const char *name,
48 const void *ns, const struct kernfs_node *kn)
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);
57 static int kernfs_sd_compare(const struct kernfs_node *left,
58 const struct kernfs_node *right)
60 return kernfs_name_compare(left->hash, left->name, left->ns, right);
63 /**
64 * kernfs_link_sibling - link kernfs_node into sibling rbtree
65 * @kn: kernfs_node of interest
67 * Link @kn into its sibling rbtree which starts from
68 * @kn->parent->dir.children.
70 * Locking:
71 * mutex_lock(kernfs_mutex)
73 * RETURNS:
74 * 0 on susccess -EEXIST on failure.
76 static int kernfs_link_sibling(struct kernfs_node *kn)
78 struct rb_node **node = &kn->parent->dir.children.rb_node;
79 struct rb_node *parent = NULL;
81 if (kernfs_type(kn) == KERNFS_DIR)
82 kn->parent->dir.subdirs++;
84 while (*node) {
85 struct kernfs_node *pos;
86 int result;
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;
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;
105 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
106 * @kn: kernfs_node of interest
108 * Unlink @kn from its sibling rbtree which starts from
109 * kn->parent->dir.children.
111 * Locking:
112 * mutex_lock(kernfs_mutex)
114 static void kernfs_unlink_sibling(struct kernfs_node *kn)
116 if (kernfs_type(kn) == KERNFS_DIR)
117 kn->parent->dir.subdirs--;
119 rb_erase(&kn->rb, &kn->parent->dir.children);
123 * kernfs_get_active - get an active reference to kernfs_node
124 * @kn: kernfs_node to get an active reference to
126 * Get an active reference of @kn. This function is noop if @kn
127 * is NULL.
129 * RETURNS:
130 * Pointer to @kn on success, NULL on failure.
132 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
134 if (unlikely(!kn))
135 return NULL;
137 if (!atomic_inc_unless_negative(&kn->active))
138 return NULL;
140 if (kn->flags & KERNFS_LOCKDEP)
141 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
142 return kn;
146 * kernfs_put_active - put an active reference to kernfs_node
147 * @kn: kernfs_node to put an active reference to
149 * Put an active reference to @kn. This function is noop if @kn
150 * is NULL.
152 void kernfs_put_active(struct kernfs_node *kn)
154 int v;
156 if (unlikely(!kn))
157 return;
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;
166 * atomic_dec_return() is a mb(), we'll always see the updated
167 * kn->u.completion.
169 complete(kn->u.completion);
173 * kernfs_deactivate - deactivate kernfs_node
174 * @kn: kernfs_node to deactivate
176 * Deny new active references and drain existing ones.
178 static void kernfs_deactivate(struct kernfs_node *kn)
180 DECLARE_COMPLETION_ONSTACK(wait);
181 int v;
183 BUG_ON(!(kn->flags & KERNFS_REMOVED));
185 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
186 return;
188 kn->u.completion = (void *)&wait;
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.
195 v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
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);
203 if (kn->flags & KERNFS_LOCKDEP) {
204 lock_acquired(&kn->dep_map, _RET_IP_);
205 rwsem_release(&kn->dep_map, 1, _RET_IP_);
210 * kernfs_get - get a reference count on a kernfs_node
211 * @kn: the target kernfs_node
213 void kernfs_get(struct kernfs_node *kn)
215 if (kn) {
216 WARN_ON(!atomic_read(&kn->count));
217 atomic_inc(&kn->count);
220 EXPORT_SYMBOL_GPL(kernfs_get);
223 * kernfs_put - put a reference count on a kernfs_node
224 * @kn: the target kernfs_node
226 * Put a reference count of @kn and destroy it if it reached zero.
228 void kernfs_put(struct kernfs_node *kn)
230 struct kernfs_node *parent;
231 struct kernfs_root *root;
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.
240 parent = kn->parent;
242 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
243 parent ? parent->name : "", kn->name);
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);
255 kfree(kn->iattr);
256 ida_simple_remove(&root->ino_ida, kn->ino);
257 kmem_cache_free(kernfs_node_cache, kn);
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);
269 EXPORT_SYMBOL_GPL(kernfs_put);
271 static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
273 struct kernfs_node *kn;
275 if (flags & LOOKUP_RCU)
276 return -ECHILD;
278 /* Always perform fresh lookup for negatives */
279 if (!dentry->d_inode)
280 goto out_bad_unlocked;
282 kn = dentry->d_fsdata;
283 mutex_lock(&kernfs_mutex);
285 /* The kernfs node has been deleted */
286 if (kn->flags & KERNFS_REMOVED)
287 goto out_bad;
289 /* The kernfs node has been moved? */
290 if (dentry->d_parent->d_fsdata != kn->parent)
291 goto out_bad;
293 /* The kernfs node has been renamed */
294 if (strcmp(dentry->d_name.name, kn->name) != 0)
295 goto out_bad;
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;
302 mutex_unlock(&kernfs_mutex);
303 out_valid:
304 return 1;
305 out_bad:
306 mutex_unlock(&kernfs_mutex);
307 out_bad_unlocked:
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().
315 if (check_submounts_and_drop(dentry) != 0)
316 goto out_valid;
318 return 0;
321 static void kernfs_dop_release(struct dentry *dentry)
323 kernfs_put(dentry->d_fsdata);
326 const struct dentry_operations kernfs_dops = {
327 .d_revalidate = kernfs_dop_revalidate,
328 .d_release = kernfs_dop_release,
331 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
332 const char *name, umode_t mode,
333 unsigned flags)
335 char *dup_name = NULL;
336 struct kernfs_node *kn;
337 int ret;
339 if (!(flags & KERNFS_STATIC_NAME)) {
340 name = dup_name = kstrdup(name, GFP_KERNEL);
341 if (!name)
342 return NULL;
345 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
346 if (!kn)
347 goto err_out1;
349 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
350 if (ret < 0)
351 goto err_out2;
352 kn->ino = ret;
354 atomic_set(&kn->count, 1);
355 atomic_set(&kn->active, 0);
357 kn->name = name;
358 kn->mode = mode;
359 kn->flags = flags | KERNFS_REMOVED;
361 return kn;
363 err_out2:
364 kmem_cache_free(kernfs_node_cache, kn);
365 err_out1:
366 kfree(dup_name);
367 return NULL;
370 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
371 const char *name, umode_t mode,
372 unsigned flags)
374 struct kernfs_node *kn;
376 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
377 if (kn) {
378 kernfs_get(parent);
379 kn->parent = parent;
381 return kn;
385 * kernfs_addrm_start - prepare for kernfs_node add/remove
386 * @acxt: pointer to kernfs_addrm_cxt to be used
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.
392 * LOCKING:
393 * Kernel thread context (may sleep). kernfs_mutex is locked on
394 * return.
396 void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
397 __acquires(kernfs_mutex)
399 memset(acxt, 0, sizeof(*acxt));
401 mutex_lock(&kernfs_mutex);
405 * kernfs_add_one - add kernfs_node to parent without warning
406 * @acxt: addrm context to use
407 * @kn: kernfs_node to be added
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.
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().
417 * LOCKING:
418 * Determined by kernfs_addrm_start().
420 * RETURNS:
421 * 0 on success, -EEXIST if entry with the given name already
422 * exists.
424 int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
426 struct kernfs_node *parent = kn->parent;
427 bool has_ns = kernfs_ns_enabled(parent);
428 struct kernfs_iattrs *ps_iattr;
429 int ret;
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;
437 if (kernfs_type(parent) != KERNFS_DIR)
438 return -EINVAL;
440 if (parent->flags & KERNFS_REMOVED)
441 return -ENOENT;
443 kn->hash = kernfs_name_hash(kn->name, kn->ns);
445 ret = kernfs_link_sibling(kn);
446 if (ret)
447 return ret;
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;
456 /* Mark the entry added into directory tree */
457 kn->flags &= ~KERNFS_REMOVED;
459 return 0;
463 * kernfs_remove_one - remove kernfs_node from parent
464 * @acxt: addrm context to use
465 * @kn: kernfs_node to be removed
467 * Mark @kn removed and drop nlink of parent inode if @kn is a
468 * directory. @kn is unlinked from the children list.
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().
474 * LOCKING:
475 * Determined by kernfs_addrm_start().
477 static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
478 struct kernfs_node *kn)
480 struct kernfs_iattrs *ps_iattr;
483 * Removal can be called multiple times on the same node. Only the
484 * first invocation is effective and puts the base ref.
486 if (kn->flags & KERNFS_REMOVED)
487 return;
489 if (kn->parent) {
490 kernfs_unlink_sibling(kn);
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;
500 kn->flags |= KERNFS_REMOVED;
501 kn->u.removed_list = acxt->removed;
502 acxt->removed = kn;
506 * kernfs_addrm_finish - finish up kernfs_node add/remove
507 * @acxt: addrm context to finish up
509 * Finish up kernfs_node add/remove. Resources acquired by
510 * kernfs_addrm_start() are released and removed kernfs_nodes are
511 * cleaned up.
513 * LOCKING:
514 * kernfs_mutex is released.
516 void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
517 __releases(kernfs_mutex)
519 /* release resources acquired by kernfs_addrm_start() */
520 mutex_unlock(&kernfs_mutex);
522 /* kill removed kernfs_nodes */
523 while (acxt->removed) {
524 struct kernfs_node *kn = acxt->removed;
526 acxt->removed = kn->u.removed_list;
528 kernfs_deactivate(kn);
529 kernfs_unmap_bin_file(kn);
530 kernfs_put(kn);
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
540 * Look for kernfs_node with name @name under @parent. Returns pointer to
541 * the found kernfs_node on success, %NULL on failure.
543 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
544 const unsigned char *name,
545 const void *ns)
547 struct rb_node *node = parent->dir.children.rb_node;
548 bool has_ns = kernfs_ns_enabled(parent);
549 unsigned int hash;
551 lockdep_assert_held(&kernfs_mutex);
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;
559 hash = kernfs_name_hash(name, ns);
560 while (node) {
561 struct kernfs_node *kn;
562 int result;
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;
573 return NULL;
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
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.
586 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
587 const char *name, const void *ns)
589 struct kernfs_node *kn;
591 mutex_lock(&kernfs_mutex);
592 kn = kernfs_find_ns(parent, name, ns);
593 kernfs_get(kn);
594 mutex_unlock(&kernfs_mutex);
596 return kn;
598 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
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
605 * Returns the root of the new hierarchy on success, ERR_PTR() value on
606 * failure.
608 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
610 struct kernfs_root *root;
611 struct kernfs_node *kn;
613 root = kzalloc(sizeof(*root), GFP_KERNEL);
614 if (!root)
615 return ERR_PTR(-ENOMEM);
617 ida_init(&root->ino_ida);
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);
627 kn->flags &= ~KERNFS_REMOVED;
628 kn->priv = priv;
629 kn->dir.root = root;
631 root->dir_ops = kdops;
632 root->kn = kn;
634 return root;
638 * kernfs_destroy_root - destroy a kernfs hierarchy
639 * @root: root of the hierarchy to destroy
641 * Destroy the hierarchy anchored at @root by removing all existing
642 * directories and destroying @root.
644 void kernfs_destroy_root(struct kernfs_root *root)
646 kernfs_remove(root->kn); /* will also free @root */
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
657 * Returns the created node on success, ERR_PTR() value on failure.
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)
663 struct kernfs_addrm_cxt acxt;
664 struct kernfs_node *kn;
665 int rc;
667 /* allocate */
668 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
669 if (!kn)
670 return ERR_PTR(-ENOMEM);
672 kn->dir.root = parent->dir.root;
673 kn->ns = ns;
674 kn->priv = priv;
676 /* link in */
677 kernfs_addrm_start(&acxt);
678 rc = kernfs_add_one(&acxt, kn);
679 kernfs_addrm_finish(&acxt);
681 if (!rc)
682 return kn;
684 kernfs_put(kn);
685 return ERR_PTR(rc);
688 static struct dentry *kernfs_iop_lookup(struct inode *dir,
689 struct dentry *dentry,
690 unsigned int flags)
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;
698 mutex_lock(&kernfs_mutex);
700 if (kernfs_ns_enabled(parent))
701 ns = kernfs_info(dir->i_sb)->ns;
703 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
705 /* no such entry */
706 if (!kn) {
707 ret = NULL;
708 goto out_unlock;
710 kernfs_get(kn);
711 dentry->d_fsdata = kn;
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;
720 /* instantiate and hash dentry */
721 ret = d_materialise_unique(dentry, inode);
722 out_unlock:
723 mutex_unlock(&kernfs_mutex);
724 return ret;
727 static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
728 umode_t mode)
730 struct kernfs_node *parent = dir->i_private;
731 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
733 if (!kdops || !kdops->mkdir)
734 return -EPERM;
736 return kdops->mkdir(parent, dentry->d_name.name, mode);
739 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
741 struct kernfs_node *kn = dentry->d_fsdata;
742 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
744 if (!kdops || !kdops->rmdir)
745 return -EPERM;
747 return kdops->rmdir(kn);
750 static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
751 struct inode *new_dir, struct dentry *new_dentry)
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;
757 if (!kdops || !kdops->rename)
758 return -EPERM;
760 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
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,
773 .mkdir = kernfs_iop_mkdir,
774 .rmdir = kernfs_iop_rmdir,
775 .rename = kernfs_iop_rename,
778 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
780 struct kernfs_node *last;
782 while (true) {
783 struct rb_node *rbn;
785 last = pos;
787 if (kernfs_type(pos) != KERNFS_DIR)
788 break;
790 rbn = rb_first(&pos->dir.children);
791 if (!rbn)
792 break;
794 pos = rb_to_kn(rbn);
797 return last;
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
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.
809 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
810 struct kernfs_node *root)
812 struct rb_node *rbn;
814 lockdep_assert_held(&kernfs_mutex);
816 /* if first iteration, visit leftmost descendant which may be root */
817 if (!pos)
818 return kernfs_leftmost_descendant(root);
820 /* if we visited @root, we're done */
821 if (pos == root)
822 return NULL;
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));
829 /* no sibling left, visit parent */
830 return pos->parent;
833 static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
834 struct kernfs_node *kn)
836 struct kernfs_node *pos, *next;
838 if (!kn)
839 return;
841 pr_debug("kernfs %s: removing\n", kn->name);
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);
853 * kernfs_remove - remove a kernfs_node recursively
854 * @kn: the kernfs_node to remove
856 * Remove @kn along with all its subdirectories and files.
858 void kernfs_remove(struct kernfs_node *kn)
860 struct kernfs_addrm_cxt acxt;
862 kernfs_addrm_start(&acxt);
863 __kernfs_remove(&acxt, kn);
864 kernfs_addrm_finish(&acxt);
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
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.
876 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
877 const void *ns)
879 struct kernfs_addrm_cxt acxt;
880 struct kernfs_node *kn;
882 if (!parent) {
883 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
884 name);
885 return -ENOENT;
888 kernfs_addrm_start(&acxt);
890 kn = kernfs_find_ns(parent, name, ns);
891 if (kn)
892 __kernfs_remove(&acxt, kn);
894 kernfs_addrm_finish(&acxt);
896 if (kn)
897 return 0;
898 else
899 return -ENOENT;
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
909 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
910 const char *new_name, const void *new_ns)
912 int error;
914 mutex_lock(&kernfs_mutex);
916 error = -ENOENT;
917 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
918 goto out;
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 */
925 error = -EEXIST;
926 if (kernfs_find_ns(new_parent, new_name, new_ns))
927 goto out;
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;
936 if (kn->flags & KERNFS_STATIC_NAME)
937 kn->flags &= ~KERNFS_STATIC_NAME;
938 else
939 kfree(kn->name);
941 kn->name = new_name;
945 * Move to the appropriate place in the appropriate directories rbtree.
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);
955 error = 0;
956 out:
957 mutex_unlock(&kernfs_mutex);
958 return error;
961 /* Relationship between s_mode and the DT_xxx types */
962 static inline unsigned char dt_type(struct kernfs_node *kn)
964 return (kn->mode >> 12) & 15;
967 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
969 kernfs_put(filp->private_data);
970 return 0;
973 static struct kernfs_node *kernfs_dir_pos(const void *ns,
974 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
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;
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);
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;
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);
1004 return pos;
1007 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1008 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
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;
1022 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
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;
1029 if (!dir_emit_dots(file, ctx))
1030 return 0;
1031 mutex_lock(&kernfs_mutex);
1033 if (kernfs_ns_enabled(parent))
1034 ns = kernfs_info(dentry->d_sb)->ns;
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;
1044 ctx->pos = pos->hash;
1045 file->private_data = pos;
1046 kernfs_get(pos);
1048 mutex_unlock(&kernfs_mutex);
1049 if (!dir_emit(ctx, name, len, ino, type))
1050 return 0;
1051 mutex_lock(&kernfs_mutex);
1053 mutex_unlock(&kernfs_mutex);
1054 file->private_data = NULL;
1055 ctx->pos = INT_MAX;
1056 return 0;
1059 static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1060 int whence)
1062 struct inode *inode = file_inode(file);
1063 loff_t ret;
1065 mutex_lock(&inode->i_mutex);
1066 ret = generic_file_llseek(file, offset, whence);
1067 mutex_unlock(&inode->i_mutex);
1069 return ret;
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,