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)
25 static bool kernfs_lockdep(struct kernfs_node *kn)
27 #ifdef CONFIG_DEBUG_LOCK_ALLOC
28 return kn->flags & KERNFS_LOCKDEP;
36 * @name: Null terminated string to hash
37 * @ns: Namespace tag to hash
39 * Returns 31 bit hash of ns + name (so it fits in an off_t )
41 static unsigned int kernfs_name_hash(const char *name, const void *ns)
43 unsigned long hash = init_name_hash();
44 unsigned int len = strlen(name);
46 hash = partial_name_hash(*name++, hash);
47 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
49 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
57 static int kernfs_name_compare(unsigned int hash, const char *name,
58 const void *ns, const struct kernfs_node *kn)
61 return hash - kn->hash;
64 return strcmp(name, kn->name);
67 static int kernfs_sd_compare(const struct kernfs_node *left,
68 const struct kernfs_node *right)
70 return kernfs_name_compare(left->hash, left->name, left->ns, right);
74 * kernfs_link_sibling - link kernfs_node into sibling rbtree
75 * @kn: kernfs_node of interest
77 * Link @kn into its sibling rbtree which starts from
78 * @kn->parent->dir.children.
81 * mutex_lock(kernfs_mutex)
84 * 0 on susccess -EEXIST on failure.
86 static int kernfs_link_sibling(struct kernfs_node *kn)
88 struct rb_node **node = &kn->parent->dir.children.rb_node;
89 struct rb_node *parent = NULL;
91 if (kernfs_type(kn) == KERNFS_DIR)
92 kn->parent->dir.subdirs++;
95 struct kernfs_node *pos;
98 pos = rb_to_kn(*node);
100 result = kernfs_sd_compare(kn, pos);
102 node = &pos->rb.rb_left;
104 node = &pos->rb.rb_right;
108 /* add new node and rebalance the tree */
109 rb_link_node(&kn->rb, parent, node);
110 rb_insert_color(&kn->rb, &kn->parent->dir.children);
115 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
116 * @kn: kernfs_node of interest
118 * Unlink @kn from its sibling rbtree which starts from
119 * kn->parent->dir.children.
122 * mutex_lock(kernfs_mutex)
124 static void kernfs_unlink_sibling(struct kernfs_node *kn)
126 if (kernfs_type(kn) == KERNFS_DIR)
127 kn->parent->dir.subdirs--;
129 rb_erase(&kn->rb, &kn->parent->dir.children);
130 RB_CLEAR_NODE(&kn->rb);
134 * kernfs_get_active - get an active reference to kernfs_node
135 * @kn: kernfs_node to get an active reference to
137 * Get an active reference of @kn. This function is noop if @kn
141 * Pointer to @kn on success, NULL on failure.
143 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
148 if (!atomic_inc_unless_negative(&kn->active))
151 if (kernfs_lockdep(kn))
152 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
157 * kernfs_put_active - put an active reference to kernfs_node
158 * @kn: kernfs_node to put an active reference to
160 * Put an active reference to @kn. This function is noop if @kn
163 void kernfs_put_active(struct kernfs_node *kn)
165 struct kernfs_root *root = kernfs_root(kn);
171 if (kernfs_lockdep(kn))
172 rwsem_release(&kn->dep_map, 1, _RET_IP_);
173 v = atomic_dec_return(&kn->active);
174 if (likely(v != KN_DEACTIVATED_BIAS))
177 wake_up_all(&root->deactivate_waitq);
181 * kernfs_drain - drain kernfs_node
182 * @kn: kernfs_node to drain
184 * Drain existing usages.
186 static void kernfs_drain(struct kernfs_node *kn)
188 struct kernfs_root *root = kernfs_root(kn);
190 WARN_ON_ONCE(atomic_read(&kn->active) >= 0);
192 if (kernfs_lockdep(kn)) {
193 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
194 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
195 lock_contended(&kn->dep_map, _RET_IP_);
198 wait_event(root->deactivate_waitq,
199 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
201 if (kernfs_lockdep(kn)) {
202 lock_acquired(&kn->dep_map, _RET_IP_);
203 rwsem_release(&kn->dep_map, 1, _RET_IP_);
208 * kernfs_get - get a reference count on a kernfs_node
209 * @kn: the target kernfs_node
211 void kernfs_get(struct kernfs_node *kn)
214 WARN_ON(!atomic_read(&kn->count));
215 atomic_inc(&kn->count);
218 EXPORT_SYMBOL_GPL(kernfs_get);
221 * kernfs_put - put a reference count on a kernfs_node
222 * @kn: the target kernfs_node
224 * Put a reference count of @kn and destroy it if it reached zero.
226 void kernfs_put(struct kernfs_node *kn)
228 struct kernfs_node *parent;
229 struct kernfs_root *root;
231 if (!kn || !atomic_dec_and_test(&kn->count))
233 root = kernfs_root(kn);
236 * Moving/renaming is always done while holding reference.
237 * kn->parent won't change beneath us.
241 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
242 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
243 parent ? parent->name : "", kn->name, atomic_read(&kn->active));
245 if (kernfs_type(kn) == KERNFS_LINK)
246 kernfs_put(kn->symlink.target_kn);
247 if (!(kn->flags & KERNFS_STATIC_NAME))
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);
256 ida_simple_remove(&root->ino_ida, kn->ino);
257 kmem_cache_free(kernfs_node_cache, kn);
261 if (atomic_dec_and_test(&kn->count))
264 /* just released the root kn, free @root too */
265 ida_destroy(&root->ino_ida);
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)
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 /* Force fresh lookup if removed */
286 if (kn->parent && RB_EMPTY_NODE(&kn->rb))
289 /* The kernfs node has been moved? */
290 if (dentry->d_parent->d_fsdata != kn->parent)
293 /* The kernfs node has been renamed */
294 if (strcmp(dentry->d_name.name, kn->name) != 0)
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)
302 mutex_unlock(&kernfs_mutex);
306 mutex_unlock(&kernfs_mutex);
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)
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 struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
332 umode_t mode, unsigned flags)
334 char *dup_name = NULL;
335 struct kernfs_node *kn;
338 if (!(flags & KERNFS_STATIC_NAME)) {
339 name = dup_name = kstrdup(name, GFP_KERNEL);
344 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
348 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
353 atomic_set(&kn->count, 1);
354 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
355 RB_CLEAR_NODE(&kn->rb);
364 kmem_cache_free(kernfs_node_cache, kn);
371 * kernfs_addrm_start - prepare for kernfs_node add/remove
372 * @acxt: pointer to kernfs_addrm_cxt to be used
374 * This function is called when the caller is about to add or remove
375 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
376 * to keep and pass context to other addrm functions.
379 * Kernel thread context (may sleep). kernfs_mutex is locked on
382 void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
383 __acquires(kernfs_mutex)
385 memset(acxt, 0, sizeof(*acxt));
387 mutex_lock(&kernfs_mutex);
391 * kernfs_add_one - add kernfs_node to parent without warning
392 * @acxt: addrm context to use
393 * @kn: kernfs_node to be added
394 * @parent: the parent kernfs_node to add @kn to
396 * Get @parent and set @kn->parent to it and increment nlink of the
397 * parent inode if @kn is a directory and link into the children list
400 * This function should be called between calls to
401 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
402 * the same @acxt as passed to kernfs_addrm_start().
405 * Determined by kernfs_addrm_start().
408 * 0 on success, -EEXIST if entry with the given name already
411 int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
412 struct kernfs_node *parent)
414 bool has_ns = kernfs_ns_enabled(parent);
415 struct kernfs_iattrs *ps_iattr;
418 WARN_ON_ONCE(atomic_read(&parent->active) < 0);
420 if (has_ns != (bool)kn->ns) {
421 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
422 has_ns ? "required" : "invalid", parent->name, kn->name);
426 if (kernfs_type(parent) != KERNFS_DIR)
429 kn->hash = kernfs_name_hash(kn->name, kn->ns);
433 ret = kernfs_link_sibling(kn);
437 /* Update timestamps on the parent */
438 ps_iattr = parent->iattr;
440 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
441 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
444 /* Mark the entry added into directory tree */
445 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
450 * kernfs_remove_one - remove kernfs_node from parent
451 * @acxt: addrm context to use
452 * @kn: kernfs_node to be removed
454 * Mark @kn removed and drop nlink of parent inode if @kn is a
455 * directory. @kn is unlinked from the children list.
457 * This function should be called between calls to
458 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
459 * passed the same @acxt as passed to kernfs_addrm_start().
462 * Determined by kernfs_addrm_start().
464 static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
465 struct kernfs_node *kn)
467 struct kernfs_iattrs *ps_iattr;
470 * Removal can be called multiple times on the same node. Only the
471 * first invocation is effective and puts the base ref.
473 if (atomic_read(&kn->active) < 0)
477 kernfs_unlink_sibling(kn);
479 /* Update timestamps on the parent */
480 ps_iattr = kn->parent->iattr;
482 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
483 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
487 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
488 kn->u.removed_list = acxt->removed;
493 * kernfs_addrm_finish - finish up kernfs_node add/remove
494 * @acxt: addrm context to finish up
496 * Finish up kernfs_node add/remove. Resources acquired by
497 * kernfs_addrm_start() are released and removed kernfs_nodes are
501 * kernfs_mutex is released.
503 void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
504 __releases(kernfs_mutex)
506 /* release resources acquired by kernfs_addrm_start() */
507 mutex_unlock(&kernfs_mutex);
509 /* kill removed kernfs_nodes */
510 while (acxt->removed) {
511 struct kernfs_node *kn = acxt->removed;
513 acxt->removed = kn->u.removed_list;
516 kernfs_unmap_bin_file(kn);
522 * kernfs_find_ns - find kernfs_node with the given name
523 * @parent: kernfs_node to search under
524 * @name: name to look for
525 * @ns: the namespace tag to use
527 * Look for kernfs_node with name @name under @parent. Returns pointer to
528 * the found kernfs_node on success, %NULL on failure.
530 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
531 const unsigned char *name,
534 struct rb_node *node = parent->dir.children.rb_node;
535 bool has_ns = kernfs_ns_enabled(parent);
538 lockdep_assert_held(&kernfs_mutex);
540 if (has_ns != (bool)ns) {
541 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
542 has_ns ? "required" : "invalid", parent->name, name);
546 hash = kernfs_name_hash(name, ns);
548 struct kernfs_node *kn;
552 result = kernfs_name_compare(hash, name, ns, kn);
554 node = node->rb_left;
556 node = node->rb_right;
564 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
565 * @parent: kernfs_node to search under
566 * @name: name to look for
567 * @ns: the namespace tag to use
569 * Look for kernfs_node with name @name under @parent and get a reference
570 * if found. This function may sleep and returns pointer to the found
571 * kernfs_node on success, %NULL on failure.
573 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
574 const char *name, const void *ns)
576 struct kernfs_node *kn;
578 mutex_lock(&kernfs_mutex);
579 kn = kernfs_find_ns(parent, name, ns);
581 mutex_unlock(&kernfs_mutex);
585 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
588 * kernfs_create_root - create a new kernfs hierarchy
589 * @kdops: optional directory syscall operations for the hierarchy
590 * @priv: opaque data associated with the new directory
592 * Returns the root of the new hierarchy on success, ERR_PTR() value on
595 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
597 struct kernfs_root *root;
598 struct kernfs_node *kn;
600 root = kzalloc(sizeof(*root), GFP_KERNEL);
602 return ERR_PTR(-ENOMEM);
604 ida_init(&root->ino_ida);
606 kn = kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, KERNFS_DIR);
608 ida_destroy(&root->ino_ida);
610 return ERR_PTR(-ENOMEM);
613 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
617 root->dir_ops = kdops;
619 init_waitqueue_head(&root->deactivate_waitq);
625 * kernfs_destroy_root - destroy a kernfs hierarchy
626 * @root: root of the hierarchy to destroy
628 * Destroy the hierarchy anchored at @root by removing all existing
629 * directories and destroying @root.
631 void kernfs_destroy_root(struct kernfs_root *root)
633 kernfs_remove(root->kn); /* will also free @root */
637 * kernfs_create_dir_ns - create a directory
638 * @parent: parent in which to create a new directory
639 * @name: name of the new directory
640 * @mode: mode of the new directory
641 * @priv: opaque data associated with the new directory
642 * @ns: optional namespace tag of the directory
644 * Returns the created node on success, ERR_PTR() value on failure.
646 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
647 const char *name, umode_t mode,
648 void *priv, const void *ns)
650 struct kernfs_addrm_cxt acxt;
651 struct kernfs_node *kn;
655 kn = kernfs_new_node(kernfs_root(parent), name, mode | S_IFDIR,
658 return ERR_PTR(-ENOMEM);
660 kn->dir.root = parent->dir.root;
666 if (kernfs_get_active(parent)) {
667 kernfs_addrm_start(&acxt);
668 rc = kernfs_add_one(&acxt, kn, parent);
669 kernfs_addrm_finish(&acxt);
670 kernfs_put_active(parent);
680 static struct dentry *kernfs_iop_lookup(struct inode *dir,
681 struct dentry *dentry,
685 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
686 struct kernfs_node *kn;
688 const void *ns = NULL;
690 mutex_lock(&kernfs_mutex);
692 if (kernfs_ns_enabled(parent))
693 ns = kernfs_info(dir->i_sb)->ns;
695 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
703 dentry->d_fsdata = kn;
705 /* attach dentry and inode */
706 inode = kernfs_get_inode(dir->i_sb, kn);
708 ret = ERR_PTR(-ENOMEM);
712 /* instantiate and hash dentry */
713 ret = d_materialise_unique(dentry, inode);
715 mutex_unlock(&kernfs_mutex);
719 static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
722 struct kernfs_node *parent = dir->i_private;
723 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
725 if (!kdops || !kdops->mkdir)
728 return kdops->mkdir(parent, dentry->d_name.name, mode);
731 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
733 struct kernfs_node *kn = dentry->d_fsdata;
734 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
736 if (!kdops || !kdops->rmdir)
739 return kdops->rmdir(kn);
742 static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
743 struct inode *new_dir, struct dentry *new_dentry)
745 struct kernfs_node *kn = old_dentry->d_fsdata;
746 struct kernfs_node *new_parent = new_dir->i_private;
747 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
749 if (!kdops || !kdops->rename)
752 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
755 const struct inode_operations kernfs_dir_iops = {
756 .lookup = kernfs_iop_lookup,
757 .permission = kernfs_iop_permission,
758 .setattr = kernfs_iop_setattr,
759 .getattr = kernfs_iop_getattr,
760 .setxattr = kernfs_iop_setxattr,
761 .removexattr = kernfs_iop_removexattr,
762 .getxattr = kernfs_iop_getxattr,
763 .listxattr = kernfs_iop_listxattr,
765 .mkdir = kernfs_iop_mkdir,
766 .rmdir = kernfs_iop_rmdir,
767 .rename = kernfs_iop_rename,
770 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
772 struct kernfs_node *last;
779 if (kernfs_type(pos) != KERNFS_DIR)
782 rbn = rb_first(&pos->dir.children);
793 * kernfs_next_descendant_post - find the next descendant for post-order walk
794 * @pos: the current position (%NULL to initiate traversal)
795 * @root: kernfs_node whose descendants to walk
797 * Find the next descendant to visit for post-order traversal of @root's
798 * descendants. @root is included in the iteration and the last node to be
801 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
802 struct kernfs_node *root)
806 lockdep_assert_held(&kernfs_mutex);
808 /* if first iteration, visit leftmost descendant which may be root */
810 return kernfs_leftmost_descendant(root);
812 /* if we visited @root, we're done */
816 /* if there's an unvisited sibling, visit its leftmost descendant */
817 rbn = rb_next(&pos->rb);
819 return kernfs_leftmost_descendant(rb_to_kn(rbn));
821 /* no sibling left, visit parent */
825 static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
826 struct kernfs_node *kn)
828 struct kernfs_node *pos, *next;
833 pr_debug("kernfs %s: removing\n", kn->name);
838 next = kernfs_next_descendant_post(pos, kn);
840 kernfs_remove_one(acxt, pos);
845 * kernfs_remove - remove a kernfs_node recursively
846 * @kn: the kernfs_node to remove
848 * Remove @kn along with all its subdirectories and files.
850 void kernfs_remove(struct kernfs_node *kn)
852 struct kernfs_addrm_cxt acxt;
854 kernfs_addrm_start(&acxt);
855 __kernfs_remove(&acxt, kn);
856 kernfs_addrm_finish(&acxt);
860 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
861 * @parent: parent of the target
862 * @name: name of the kernfs_node to remove
863 * @ns: namespace tag of the kernfs_node to remove
865 * Look for the kernfs_node with @name and @ns under @parent and remove it.
866 * Returns 0 on success, -ENOENT if such entry doesn't exist.
868 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
871 struct kernfs_addrm_cxt acxt;
872 struct kernfs_node *kn;
875 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
880 kernfs_addrm_start(&acxt);
882 kn = kernfs_find_ns(parent, name, ns);
884 __kernfs_remove(&acxt, kn);
886 kernfs_addrm_finish(&acxt);
895 * kernfs_rename_ns - move and rename a kernfs_node
897 * @new_parent: new parent to put @sd under
898 * @new_name: new name
899 * @new_ns: new namespace tag
901 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
902 const char *new_name, const void *new_ns)
907 if (!kernfs_get_active(new_parent))
909 if (!kernfs_get_active(kn))
910 goto out_put_new_parent;
912 mutex_lock(&kernfs_mutex);
915 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
916 (strcmp(kn->name, new_name) == 0))
917 goto out_unlock; /* nothing to rename */
920 if (kernfs_find_ns(new_parent, new_name, new_ns))
923 /* rename kernfs_node */
924 if (strcmp(kn->name, new_name) != 0) {
926 new_name = kstrdup(new_name, GFP_KERNEL);
930 if (kn->flags & KERNFS_STATIC_NAME)
931 kn->flags &= ~KERNFS_STATIC_NAME;
939 * Move to the appropriate place in the appropriate directories rbtree.
941 kernfs_unlink_sibling(kn);
942 kernfs_get(new_parent);
943 kernfs_put(kn->parent);
945 kn->hash = kernfs_name_hash(kn->name, kn->ns);
946 kn->parent = new_parent;
947 kernfs_link_sibling(kn);
951 mutex_unlock(&kernfs_mutex);
952 kernfs_put_active(kn);
954 kernfs_put_active(new_parent);
959 /* Relationship between s_mode and the DT_xxx types */
960 static inline unsigned char dt_type(struct kernfs_node *kn)
962 return (kn->mode >> 12) & 15;
965 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
967 kernfs_put(filp->private_data);
971 static struct kernfs_node *kernfs_dir_pos(const void *ns,
972 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
975 int valid = pos->parent == parent && hash == pos->hash;
980 if (!pos && (hash > 1) && (hash < INT_MAX)) {
981 struct rb_node *node = parent->dir.children.rb_node;
983 pos = rb_to_kn(node);
985 if (hash < pos->hash)
986 node = node->rb_left;
987 else if (hash > pos->hash)
988 node = node->rb_right;
993 /* Skip over entries in the wrong namespace */
994 while (pos && pos->ns != ns) {
995 struct rb_node *node = rb_next(&pos->rb);
999 pos = rb_to_kn(node);
1004 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1005 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1007 pos = kernfs_dir_pos(ns, parent, ino, pos);
1010 struct rb_node *node = rb_next(&pos->rb);
1014 pos = rb_to_kn(node);
1015 } while (pos && pos->ns != ns);
1019 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1021 struct dentry *dentry = file->f_path.dentry;
1022 struct kernfs_node *parent = dentry->d_fsdata;
1023 struct kernfs_node *pos = file->private_data;
1024 const void *ns = NULL;
1026 if (!dir_emit_dots(file, ctx))
1028 mutex_lock(&kernfs_mutex);
1030 if (kernfs_ns_enabled(parent))
1031 ns = kernfs_info(dentry->d_sb)->ns;
1033 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1035 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1036 const char *name = pos->name;
1037 unsigned int type = dt_type(pos);
1038 int len = strlen(name);
1039 ino_t ino = pos->ino;
1041 ctx->pos = pos->hash;
1042 file->private_data = pos;
1045 mutex_unlock(&kernfs_mutex);
1046 if (!dir_emit(ctx, name, len, ino, type))
1048 mutex_lock(&kernfs_mutex);
1050 mutex_unlock(&kernfs_mutex);
1051 file->private_data = NULL;
1056 static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1059 struct inode *inode = file_inode(file);
1062 mutex_lock(&inode->i_mutex);
1063 ret = generic_file_llseek(file, offset, whence);
1064 mutex_unlock(&inode->i_mutex);
1069 const struct file_operations kernfs_dir_fops = {
1070 .read = generic_read_dir,
1071 .iterate = kernfs_fop_readdir,
1072 .release = kernfs_dir_fop_release,
1073 .llseek = kernfs_dir_fop_llseek,