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.
11 #include <linux/sched.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>
19 #include "kernfs-internal.h"
21 DEFINE_MUTEX(kernfs_mutex);
23 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
27 * @name: Null terminated string to hash
28 * @ns: Namespace tag to hash
30 * Returns 31 bit hash of ns + name (so it fits in an off_t )
32 static unsigned int kernfs_name_hash(const char *name, const void *ns)
34 unsigned long hash = init_name_hash();
35 unsigned int len = strlen(name);
37 hash = partial_name_hash(*name++, hash);
38 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
40 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
48 static int kernfs_name_compare(unsigned int hash, const char *name,
49 const void *ns, const struct kernfs_node *kn)
52 return hash - kn->hash;
55 return strcmp(name, kn->name);
58 static int kernfs_sd_compare(const struct kernfs_node *left,
59 const struct kernfs_node *right)
61 return kernfs_name_compare(left->hash, left->name, left->ns, right);
65 * kernfs_link_sibling - link kernfs_node into sibling rbtree
66 * @kn: kernfs_node of interest
68 * Link @kn into its sibling rbtree which starts from
69 * @kn->parent->dir.children.
72 * mutex_lock(kernfs_mutex)
75 * 0 on susccess -EEXIST on failure.
77 static int kernfs_link_sibling(struct kernfs_node *kn)
79 struct rb_node **node = &kn->parent->dir.children.rb_node;
80 struct rb_node *parent = NULL;
82 if (kernfs_type(kn) == KERNFS_DIR)
83 kn->parent->dir.subdirs++;
86 struct kernfs_node *pos;
89 pos = rb_to_kn(*node);
91 result = kernfs_sd_compare(kn, pos);
93 node = &pos->rb.rb_left;
95 node = &pos->rb.rb_right;
99 /* add new node and rebalance the tree */
100 rb_link_node(&kn->rb, parent, node);
101 rb_insert_color(&kn->rb, &kn->parent->dir.children);
106 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
107 * @kn: kernfs_node of interest
109 * Unlink @kn from its sibling rbtree which starts from
110 * kn->parent->dir.children.
113 * mutex_lock(kernfs_mutex)
115 static void kernfs_unlink_sibling(struct kernfs_node *kn)
117 if (kernfs_type(kn) == KERNFS_DIR)
118 kn->parent->dir.subdirs--;
120 rb_erase(&kn->rb, &kn->parent->dir.children);
124 * kernfs_get_active - get an active reference to kernfs_node
125 * @kn: kernfs_node to get an active reference to
127 * Get an active reference of @kn. This function is noop if @kn
131 * Pointer to @kn on success, NULL on failure.
133 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
138 if (!atomic_inc_unless_negative(&kn->active))
141 if (kn->flags & KERNFS_LOCKDEP)
142 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
147 * kernfs_put_active - put an active reference to kernfs_node
148 * @kn: kernfs_node to put an active reference to
150 * Put an active reference to @kn. This function is noop if @kn
153 void kernfs_put_active(struct kernfs_node *kn)
155 struct kernfs_root *root = kernfs_root(kn);
161 if (kn->flags & KERNFS_LOCKDEP)
162 rwsem_release(&kn->dep_map, 1, _RET_IP_);
163 v = atomic_dec_return(&kn->active);
164 if (likely(v != KN_DEACTIVATED_BIAS))
167 wake_up_all(&root->deactivate_waitq);
171 * kernfs_deactivate - deactivate kernfs_node
172 * @kn: kernfs_node to deactivate
174 * Deny new active references and drain existing ones.
176 static void kernfs_deactivate(struct kernfs_node *kn)
178 struct kernfs_root *root = kernfs_root(kn);
180 BUG_ON(!(kn->flags & KERNFS_REMOVED));
182 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
185 if (kn->flags & KERNFS_LOCKDEP)
186 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
188 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
190 if ((kn->flags & KERNFS_LOCKDEP) &&
191 atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
192 lock_contended(&kn->dep_map, _RET_IP_);
194 wait_event(root->deactivate_waitq,
195 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
197 if (kn->flags & KERNFS_LOCKDEP) {
198 lock_acquired(&kn->dep_map, _RET_IP_);
199 rwsem_release(&kn->dep_map, 1, _RET_IP_);
204 * kernfs_get - get a reference count on a kernfs_node
205 * @kn: the target kernfs_node
207 void kernfs_get(struct kernfs_node *kn)
210 WARN_ON(!atomic_read(&kn->count));
211 atomic_inc(&kn->count);
214 EXPORT_SYMBOL_GPL(kernfs_get);
217 * kernfs_put - put a reference count on a kernfs_node
218 * @kn: the target kernfs_node
220 * Put a reference count of @kn and destroy it if it reached zero.
222 void kernfs_put(struct kernfs_node *kn)
224 struct kernfs_node *parent;
225 struct kernfs_root *root;
227 if (!kn || !atomic_dec_and_test(&kn->count))
229 root = kernfs_root(kn);
231 /* Moving/renaming is always done while holding reference.
232 * kn->parent won't change beneath us.
236 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
237 parent ? parent->name : "", kn->name);
239 if (kernfs_type(kn) == KERNFS_LINK)
240 kernfs_put(kn->symlink.target_kn);
241 if (!(kn->flags & KERNFS_STATIC_NAME))
244 if (kn->iattr->ia_secdata)
245 security_release_secctx(kn->iattr->ia_secdata,
246 kn->iattr->ia_secdata_len);
247 simple_xattrs_free(&kn->iattr->xattrs);
250 ida_simple_remove(&root->ino_ida, kn->ino);
251 kmem_cache_free(kernfs_node_cache, kn);
255 if (atomic_dec_and_test(&kn->count))
258 /* just released the root kn, free @root too */
259 ida_destroy(&root->ino_ida);
263 EXPORT_SYMBOL_GPL(kernfs_put);
265 static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
267 struct kernfs_node *kn;
269 if (flags & LOOKUP_RCU)
272 /* Always perform fresh lookup for negatives */
273 if (!dentry->d_inode)
274 goto out_bad_unlocked;
276 kn = dentry->d_fsdata;
277 mutex_lock(&kernfs_mutex);
279 /* The kernfs node has been deleted */
280 if (kn->flags & KERNFS_REMOVED)
283 /* The kernfs node has been moved? */
284 if (dentry->d_parent->d_fsdata != kn->parent)
287 /* The kernfs node has been renamed */
288 if (strcmp(dentry->d_name.name, kn->name) != 0)
291 /* The kernfs node has been moved to a different namespace */
292 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
293 kernfs_info(dentry->d_sb)->ns != kn->ns)
296 mutex_unlock(&kernfs_mutex);
300 mutex_unlock(&kernfs_mutex);
303 * @dentry doesn't match the underlying kernfs node, drop the
304 * dentry and force lookup. If we have submounts we must allow the
305 * vfs caches to lie about the state of the filesystem to prevent
306 * leaks and other nasty things, so use check_submounts_and_drop()
307 * instead of d_drop().
309 if (check_submounts_and_drop(dentry) != 0)
315 static void kernfs_dop_release(struct dentry *dentry)
317 kernfs_put(dentry->d_fsdata);
320 const struct dentry_operations kernfs_dops = {
321 .d_revalidate = kernfs_dop_revalidate,
322 .d_release = kernfs_dop_release,
325 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
326 const char *name, umode_t mode,
329 char *dup_name = NULL;
330 struct kernfs_node *kn;
333 if (!(flags & KERNFS_STATIC_NAME)) {
334 name = dup_name = kstrdup(name, GFP_KERNEL);
339 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
343 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
348 atomic_set(&kn->count, 1);
349 atomic_set(&kn->active, 0);
353 kn->flags = flags | KERNFS_REMOVED;
358 kmem_cache_free(kernfs_node_cache, kn);
364 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
365 const char *name, umode_t mode,
368 struct kernfs_node *kn;
370 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
379 * kernfs_addrm_start - prepare for kernfs_node add/remove
380 * @acxt: pointer to kernfs_addrm_cxt to be used
382 * This function is called when the caller is about to add or remove
383 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
384 * to keep and pass context to other addrm functions.
387 * Kernel thread context (may sleep). kernfs_mutex is locked on
390 void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
391 __acquires(kernfs_mutex)
393 memset(acxt, 0, sizeof(*acxt));
395 mutex_lock(&kernfs_mutex);
399 * kernfs_add_one - add kernfs_node to parent without warning
400 * @acxt: addrm context to use
401 * @kn: kernfs_node to be added
403 * The caller must already have initialized @kn->parent. This
404 * function increments nlink of the parent's inode if @kn is a
405 * directory and link into the children list of the parent.
407 * This function should be called between calls to
408 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
409 * the same @acxt as passed to kernfs_addrm_start().
412 * Determined by kernfs_addrm_start().
415 * 0 on success, -EEXIST if entry with the given name already
418 int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
420 struct kernfs_node *parent = kn->parent;
421 bool has_ns = kernfs_ns_enabled(parent);
422 struct kernfs_iattrs *ps_iattr;
425 if (has_ns != (bool)kn->ns) {
426 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
427 has_ns ? "required" : "invalid", parent->name, kn->name);
431 if (kernfs_type(parent) != KERNFS_DIR)
434 if (parent->flags & KERNFS_REMOVED)
437 kn->hash = kernfs_name_hash(kn->name, kn->ns);
439 ret = kernfs_link_sibling(kn);
443 /* Update timestamps on the parent */
444 ps_iattr = parent->iattr;
446 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
447 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
450 /* Mark the entry added into directory tree */
451 kn->flags &= ~KERNFS_REMOVED;
457 * kernfs_remove_one - remove kernfs_node from parent
458 * @acxt: addrm context to use
459 * @kn: kernfs_node to be removed
461 * Mark @kn removed and drop nlink of parent inode if @kn is a
462 * directory. @kn is unlinked from the children list.
464 * This function should be called between calls to
465 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
466 * passed the same @acxt as passed to kernfs_addrm_start().
469 * Determined by kernfs_addrm_start().
471 static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
472 struct kernfs_node *kn)
474 struct kernfs_iattrs *ps_iattr;
477 * Removal can be called multiple times on the same node. Only the
478 * first invocation is effective and puts the base ref.
480 if (kn->flags & KERNFS_REMOVED)
484 kernfs_unlink_sibling(kn);
486 /* Update timestamps on the parent */
487 ps_iattr = kn->parent->iattr;
489 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
490 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
494 kn->flags |= KERNFS_REMOVED;
495 kn->u.removed_list = acxt->removed;
500 * kernfs_addrm_finish - finish up kernfs_node add/remove
501 * @acxt: addrm context to finish up
503 * Finish up kernfs_node add/remove. Resources acquired by
504 * kernfs_addrm_start() are released and removed kernfs_nodes are
508 * kernfs_mutex is released.
510 void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
511 __releases(kernfs_mutex)
513 /* release resources acquired by kernfs_addrm_start() */
514 mutex_unlock(&kernfs_mutex);
516 /* kill removed kernfs_nodes */
517 while (acxt->removed) {
518 struct kernfs_node *kn = acxt->removed;
520 acxt->removed = kn->u.removed_list;
522 kernfs_deactivate(kn);
523 kernfs_unmap_bin_file(kn);
529 * kernfs_find_ns - find kernfs_node with the given name
530 * @parent: kernfs_node to search under
531 * @name: name to look for
532 * @ns: the namespace tag to use
534 * Look for kernfs_node with name @name under @parent. Returns pointer to
535 * the found kernfs_node on success, %NULL on failure.
537 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
538 const unsigned char *name,
541 struct rb_node *node = parent->dir.children.rb_node;
542 bool has_ns = kernfs_ns_enabled(parent);
545 lockdep_assert_held(&kernfs_mutex);
547 if (has_ns != (bool)ns) {
548 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
549 has_ns ? "required" : "invalid", parent->name, name);
553 hash = kernfs_name_hash(name, ns);
555 struct kernfs_node *kn;
559 result = kernfs_name_compare(hash, name, ns, kn);
561 node = node->rb_left;
563 node = node->rb_right;
571 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
572 * @parent: kernfs_node to search under
573 * @name: name to look for
574 * @ns: the namespace tag to use
576 * Look for kernfs_node with name @name under @parent and get a reference
577 * if found. This function may sleep and returns pointer to the found
578 * kernfs_node on success, %NULL on failure.
580 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
581 const char *name, const void *ns)
583 struct kernfs_node *kn;
585 mutex_lock(&kernfs_mutex);
586 kn = kernfs_find_ns(parent, name, ns);
588 mutex_unlock(&kernfs_mutex);
592 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
595 * kernfs_create_root - create a new kernfs hierarchy
596 * @kdops: optional directory syscall operations for the hierarchy
597 * @priv: opaque data associated with the new directory
599 * Returns the root of the new hierarchy on success, ERR_PTR() value on
602 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
604 struct kernfs_root *root;
605 struct kernfs_node *kn;
607 root = kzalloc(sizeof(*root), GFP_KERNEL);
609 return ERR_PTR(-ENOMEM);
611 ida_init(&root->ino_ida);
613 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
616 ida_destroy(&root->ino_ida);
618 return ERR_PTR(-ENOMEM);
621 kn->flags &= ~KERNFS_REMOVED;
625 root->dir_ops = kdops;
627 init_waitqueue_head(&root->deactivate_waitq);
633 * kernfs_destroy_root - destroy a kernfs hierarchy
634 * @root: root of the hierarchy to destroy
636 * Destroy the hierarchy anchored at @root by removing all existing
637 * directories and destroying @root.
639 void kernfs_destroy_root(struct kernfs_root *root)
641 kernfs_remove(root->kn); /* will also free @root */
645 * kernfs_create_dir_ns - create a directory
646 * @parent: parent in which to create a new directory
647 * @name: name of the new directory
648 * @mode: mode of the new directory
649 * @priv: opaque data associated with the new directory
650 * @ns: optional namespace tag of the directory
652 * Returns the created node on success, ERR_PTR() value on failure.
654 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
655 const char *name, umode_t mode,
656 void *priv, const void *ns)
658 struct kernfs_addrm_cxt acxt;
659 struct kernfs_node *kn;
663 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
665 return ERR_PTR(-ENOMEM);
667 kn->dir.root = parent->dir.root;
672 kernfs_addrm_start(&acxt);
673 rc = kernfs_add_one(&acxt, kn);
674 kernfs_addrm_finish(&acxt);
683 static struct dentry *kernfs_iop_lookup(struct inode *dir,
684 struct dentry *dentry,
688 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
689 struct kernfs_node *kn;
691 const void *ns = NULL;
693 mutex_lock(&kernfs_mutex);
695 if (kernfs_ns_enabled(parent))
696 ns = kernfs_info(dir->i_sb)->ns;
698 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
706 dentry->d_fsdata = kn;
708 /* attach dentry and inode */
709 inode = kernfs_get_inode(dir->i_sb, kn);
711 ret = ERR_PTR(-ENOMEM);
715 /* instantiate and hash dentry */
716 ret = d_materialise_unique(dentry, inode);
718 mutex_unlock(&kernfs_mutex);
722 static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
725 struct kernfs_node *parent = dir->i_private;
726 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
728 if (!kdops || !kdops->mkdir)
731 return kdops->mkdir(parent, dentry->d_name.name, mode);
734 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
736 struct kernfs_node *kn = dentry->d_fsdata;
737 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
739 if (!kdops || !kdops->rmdir)
742 return kdops->rmdir(kn);
745 static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
746 struct inode *new_dir, struct dentry *new_dentry)
748 struct kernfs_node *kn = old_dentry->d_fsdata;
749 struct kernfs_node *new_parent = new_dir->i_private;
750 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
752 if (!kdops || !kdops->rename)
755 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
758 const struct inode_operations kernfs_dir_iops = {
759 .lookup = kernfs_iop_lookup,
760 .permission = kernfs_iop_permission,
761 .setattr = kernfs_iop_setattr,
762 .getattr = kernfs_iop_getattr,
763 .setxattr = kernfs_iop_setxattr,
764 .removexattr = kernfs_iop_removexattr,
765 .getxattr = kernfs_iop_getxattr,
766 .listxattr = kernfs_iop_listxattr,
768 .mkdir = kernfs_iop_mkdir,
769 .rmdir = kernfs_iop_rmdir,
770 .rename = kernfs_iop_rename,
773 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
775 struct kernfs_node *last;
782 if (kernfs_type(pos) != KERNFS_DIR)
785 rbn = rb_first(&pos->dir.children);
796 * kernfs_next_descendant_post - find the next descendant for post-order walk
797 * @pos: the current position (%NULL to initiate traversal)
798 * @root: kernfs_node whose descendants to walk
800 * Find the next descendant to visit for post-order traversal of @root's
801 * descendants. @root is included in the iteration and the last node to be
804 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
805 struct kernfs_node *root)
809 lockdep_assert_held(&kernfs_mutex);
811 /* if first iteration, visit leftmost descendant which may be root */
813 return kernfs_leftmost_descendant(root);
815 /* if we visited @root, we're done */
819 /* if there's an unvisited sibling, visit its leftmost descendant */
820 rbn = rb_next(&pos->rb);
822 return kernfs_leftmost_descendant(rb_to_kn(rbn));
824 /* no sibling left, visit parent */
828 static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
829 struct kernfs_node *kn)
831 struct kernfs_node *pos, *next;
836 pr_debug("kernfs %s: removing\n", kn->name);
841 next = kernfs_next_descendant_post(pos, kn);
843 kernfs_remove_one(acxt, pos);
848 * kernfs_remove - remove a kernfs_node recursively
849 * @kn: the kernfs_node to remove
851 * Remove @kn along with all its subdirectories and files.
853 void kernfs_remove(struct kernfs_node *kn)
855 struct kernfs_addrm_cxt acxt;
857 kernfs_addrm_start(&acxt);
858 __kernfs_remove(&acxt, kn);
859 kernfs_addrm_finish(&acxt);
863 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
864 * @parent: parent of the target
865 * @name: name of the kernfs_node to remove
866 * @ns: namespace tag of the kernfs_node to remove
868 * Look for the kernfs_node with @name and @ns under @parent and remove it.
869 * Returns 0 on success, -ENOENT if such entry doesn't exist.
871 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
874 struct kernfs_addrm_cxt acxt;
875 struct kernfs_node *kn;
878 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
883 kernfs_addrm_start(&acxt);
885 kn = kernfs_find_ns(parent, name, ns);
887 __kernfs_remove(&acxt, kn);
889 kernfs_addrm_finish(&acxt);
898 * kernfs_rename_ns - move and rename a kernfs_node
900 * @new_parent: new parent to put @sd under
901 * @new_name: new name
902 * @new_ns: new namespace tag
904 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
905 const char *new_name, const void *new_ns)
909 mutex_lock(&kernfs_mutex);
912 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
916 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
917 (strcmp(kn->name, new_name) == 0))
918 goto out; /* nothing to rename */
921 if (kernfs_find_ns(new_parent, new_name, new_ns))
924 /* rename kernfs_node */
925 if (strcmp(kn->name, new_name) != 0) {
927 new_name = kstrdup(new_name, GFP_KERNEL);
931 if (kn->flags & KERNFS_STATIC_NAME)
932 kn->flags &= ~KERNFS_STATIC_NAME;
940 * Move to the appropriate place in the appropriate directories rbtree.
942 kernfs_unlink_sibling(kn);
943 kernfs_get(new_parent);
944 kernfs_put(kn->parent);
946 kn->hash = kernfs_name_hash(kn->name, kn->ns);
947 kn->parent = new_parent;
948 kernfs_link_sibling(kn);
952 mutex_unlock(&kernfs_mutex);
956 /* Relationship between s_mode and the DT_xxx types */
957 static inline unsigned char dt_type(struct kernfs_node *kn)
959 return (kn->mode >> 12) & 15;
962 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
964 kernfs_put(filp->private_data);
968 static struct kernfs_node *kernfs_dir_pos(const void *ns,
969 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
972 int valid = !(pos->flags & KERNFS_REMOVED) &&
973 pos->parent == parent && hash == pos->hash;
978 if (!pos && (hash > 1) && (hash < INT_MAX)) {
979 struct rb_node *node = parent->dir.children.rb_node;
981 pos = rb_to_kn(node);
983 if (hash < pos->hash)
984 node = node->rb_left;
985 else if (hash > pos->hash)
986 node = node->rb_right;
991 /* Skip over entries in the wrong namespace */
992 while (pos && pos->ns != ns) {
993 struct rb_node *node = rb_next(&pos->rb);
997 pos = rb_to_kn(node);
1002 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1003 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1005 pos = kernfs_dir_pos(ns, parent, ino, pos);
1008 struct rb_node *node = rb_next(&pos->rb);
1012 pos = rb_to_kn(node);
1013 } while (pos && pos->ns != ns);
1017 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1019 struct dentry *dentry = file->f_path.dentry;
1020 struct kernfs_node *parent = dentry->d_fsdata;
1021 struct kernfs_node *pos = file->private_data;
1022 const void *ns = NULL;
1024 if (!dir_emit_dots(file, ctx))
1026 mutex_lock(&kernfs_mutex);
1028 if (kernfs_ns_enabled(parent))
1029 ns = kernfs_info(dentry->d_sb)->ns;
1031 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1033 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1034 const char *name = pos->name;
1035 unsigned int type = dt_type(pos);
1036 int len = strlen(name);
1037 ino_t ino = pos->ino;
1039 ctx->pos = pos->hash;
1040 file->private_data = pos;
1043 mutex_unlock(&kernfs_mutex);
1044 if (!dir_emit(ctx, name, len, ino, type))
1046 mutex_lock(&kernfs_mutex);
1048 mutex_unlock(&kernfs_mutex);
1049 file->private_data = NULL;
1054 static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1057 struct inode *inode = file_inode(file);
1060 mutex_lock(&inode->i_mutex);
1061 ret = generic_file_llseek(file, offset, whence);
1062 mutex_unlock(&inode->i_mutex);
1067 const struct file_operations kernfs_dir_fops = {
1068 .read = generic_read_dir,
1069 .iterate = kernfs_fop_readdir,
1070 .release = kernfs_dir_fop_release,
1071 .llseek = kernfs_dir_fop_llseek,