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 * Try to unlink @kn from its sibling rbtree which starts from
119 * kn->parent->dir.children. Returns %true if @kn was actually
120 * removed, %false if @kn wasn't on the rbtree.
123 * mutex_lock(kernfs_mutex)
125 static bool kernfs_unlink_sibling(struct kernfs_node *kn)
127 if (RB_EMPTY_NODE(&kn->rb))
130 if (kernfs_type(kn) == KERNFS_DIR)
131 kn->parent->dir.subdirs--;
133 rb_erase(&kn->rb, &kn->parent->dir.children);
134 RB_CLEAR_NODE(&kn->rb);
139 * kernfs_get_active - get an active reference to kernfs_node
140 * @kn: kernfs_node to get an active reference to
142 * Get an active reference of @kn. This function is noop if @kn
146 * Pointer to @kn on success, NULL on failure.
148 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
153 if (!atomic_inc_unless_negative(&kn->active))
156 if (kernfs_lockdep(kn))
157 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
162 * kernfs_put_active - put an active reference to kernfs_node
163 * @kn: kernfs_node to put an active reference to
165 * Put an active reference to @kn. This function is noop if @kn
168 void kernfs_put_active(struct kernfs_node *kn)
170 struct kernfs_root *root = kernfs_root(kn);
176 if (kernfs_lockdep(kn))
177 rwsem_release(&kn->dep_map, 1, _RET_IP_);
178 v = atomic_dec_return(&kn->active);
179 if (likely(v != KN_DEACTIVATED_BIAS))
182 wake_up_all(&root->deactivate_waitq);
186 * kernfs_deactivate - deactivate kernfs_node
187 * @kn: kernfs_node to deactivate
189 * Deny new active references, drain existing ones and nuke all
190 * existing mmaps. Mutiple removers may invoke this function
191 * concurrently on @kn and all will return after deactivation and
192 * draining are complete.
194 static void kernfs_deactivate(struct kernfs_node *kn)
195 __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
197 struct kernfs_root *root = kernfs_root(kn);
199 lockdep_assert_held(&kernfs_mutex);
200 BUG_ON(!(kn->flags & KERNFS_REMOVED));
202 /* only the first invocation on @kn should deactivate it */
203 if (atomic_read(&kn->active) >= 0)
204 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
206 mutex_unlock(&kernfs_mutex);
208 if (kernfs_lockdep(kn)) {
209 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
210 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
211 lock_contended(&kn->dep_map, _RET_IP_);
214 /* but everyone should wait for draining */
215 wait_event(root->deactivate_waitq,
216 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
218 if (kernfs_lockdep(kn)) {
219 lock_acquired(&kn->dep_map, _RET_IP_);
220 rwsem_release(&kn->dep_map, 1, _RET_IP_);
223 kernfs_unmap_bin_file(kn);
225 mutex_lock(&kernfs_mutex);
229 * kernfs_get - get a reference count on a kernfs_node
230 * @kn: the target kernfs_node
232 void kernfs_get(struct kernfs_node *kn)
235 WARN_ON(!atomic_read(&kn->count));
236 atomic_inc(&kn->count);
239 EXPORT_SYMBOL_GPL(kernfs_get);
242 * kernfs_put - put a reference count on a kernfs_node
243 * @kn: the target kernfs_node
245 * Put a reference count of @kn and destroy it if it reached zero.
247 void kernfs_put(struct kernfs_node *kn)
249 struct kernfs_node *parent;
250 struct kernfs_root *root;
252 if (!kn || !atomic_dec_and_test(&kn->count))
254 root = kernfs_root(kn);
256 /* Moving/renaming is always done while holding reference.
257 * kn->parent won't change beneath us.
261 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
262 parent ? parent->name : "", kn->name);
264 if (kernfs_type(kn) == KERNFS_LINK)
265 kernfs_put(kn->symlink.target_kn);
266 if (!(kn->flags & KERNFS_STATIC_NAME))
269 if (kn->iattr->ia_secdata)
270 security_release_secctx(kn->iattr->ia_secdata,
271 kn->iattr->ia_secdata_len);
272 simple_xattrs_free(&kn->iattr->xattrs);
275 ida_simple_remove(&root->ino_ida, kn->ino);
276 kmem_cache_free(kernfs_node_cache, kn);
280 if (atomic_dec_and_test(&kn->count))
283 /* just released the root kn, free @root too */
284 ida_destroy(&root->ino_ida);
288 EXPORT_SYMBOL_GPL(kernfs_put);
290 static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
292 struct kernfs_node *kn;
294 if (flags & LOOKUP_RCU)
297 /* Always perform fresh lookup for negatives */
298 if (!dentry->d_inode)
299 goto out_bad_unlocked;
301 kn = dentry->d_fsdata;
302 mutex_lock(&kernfs_mutex);
304 /* The kernfs node has been deleted */
305 if (kn->flags & KERNFS_REMOVED)
308 /* The kernfs node has been moved? */
309 if (dentry->d_parent->d_fsdata != kn->parent)
312 /* The kernfs node has been renamed */
313 if (strcmp(dentry->d_name.name, kn->name) != 0)
316 /* The kernfs node has been moved to a different namespace */
317 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
318 kernfs_info(dentry->d_sb)->ns != kn->ns)
321 mutex_unlock(&kernfs_mutex);
325 mutex_unlock(&kernfs_mutex);
328 * @dentry doesn't match the underlying kernfs node, drop the
329 * dentry and force lookup. If we have submounts we must allow the
330 * vfs caches to lie about the state of the filesystem to prevent
331 * leaks and other nasty things, so use check_submounts_and_drop()
332 * instead of d_drop().
334 if (check_submounts_and_drop(dentry) != 0)
340 static void kernfs_dop_release(struct dentry *dentry)
342 kernfs_put(dentry->d_fsdata);
345 const struct dentry_operations kernfs_dops = {
346 .d_revalidate = kernfs_dop_revalidate,
347 .d_release = kernfs_dop_release,
350 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
351 const char *name, umode_t mode,
354 char *dup_name = NULL;
355 struct kernfs_node *kn;
358 if (!(flags & KERNFS_STATIC_NAME)) {
359 name = dup_name = kstrdup(name, GFP_KERNEL);
364 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
368 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
373 atomic_set(&kn->count, 1);
374 atomic_set(&kn->active, 0);
375 RB_CLEAR_NODE(&kn->rb);
379 kn->flags = flags | KERNFS_REMOVED;
384 kmem_cache_free(kernfs_node_cache, kn);
390 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
391 const char *name, umode_t mode,
394 struct kernfs_node *kn;
396 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
405 * kernfs_add_one - add kernfs_node to parent without warning
406 * @kn: kernfs_node to be added
408 * The caller must already have initialized @kn->parent. This
409 * function increments nlink of the parent's inode if @kn is a
410 * directory and link into the children list of the parent.
413 * 0 on success, -EEXIST if entry with the given name already
416 int kernfs_add_one(struct kernfs_node *kn)
418 struct kernfs_node *parent = kn->parent;
419 struct kernfs_iattrs *ps_iattr;
423 mutex_lock(&kernfs_mutex);
426 has_ns = kernfs_ns_enabled(parent);
427 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
428 has_ns ? "required" : "invalid", parent->name, kn->name))
431 if (kernfs_type(parent) != KERNFS_DIR)
435 if (parent->flags & KERNFS_REMOVED)
438 kn->hash = kernfs_name_hash(kn->name, kn->ns);
440 ret = kernfs_link_sibling(kn);
444 /* Update timestamps on the parent */
445 ps_iattr = parent->iattr;
447 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
448 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
451 /* Mark the entry added into directory tree */
452 kn->flags &= ~KERNFS_REMOVED;
455 mutex_unlock(&kernfs_mutex);
460 * kernfs_find_ns - find kernfs_node with the given name
461 * @parent: kernfs_node to search under
462 * @name: name to look for
463 * @ns: the namespace tag to use
465 * Look for kernfs_node with name @name under @parent. Returns pointer to
466 * the found kernfs_node on success, %NULL on failure.
468 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
469 const unsigned char *name,
472 struct rb_node *node = parent->dir.children.rb_node;
473 bool has_ns = kernfs_ns_enabled(parent);
476 lockdep_assert_held(&kernfs_mutex);
478 if (has_ns != (bool)ns) {
479 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
480 has_ns ? "required" : "invalid", parent->name, name);
484 hash = kernfs_name_hash(name, ns);
486 struct kernfs_node *kn;
490 result = kernfs_name_compare(hash, name, ns, kn);
492 node = node->rb_left;
494 node = node->rb_right;
502 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
503 * @parent: kernfs_node to search under
504 * @name: name to look for
505 * @ns: the namespace tag to use
507 * Look for kernfs_node with name @name under @parent and get a reference
508 * if found. This function may sleep and returns pointer to the found
509 * kernfs_node on success, %NULL on failure.
511 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
512 const char *name, const void *ns)
514 struct kernfs_node *kn;
516 mutex_lock(&kernfs_mutex);
517 kn = kernfs_find_ns(parent, name, ns);
519 mutex_unlock(&kernfs_mutex);
523 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
526 * kernfs_create_root - create a new kernfs hierarchy
527 * @kdops: optional directory syscall operations for the hierarchy
528 * @priv: opaque data associated with the new directory
530 * Returns the root of the new hierarchy on success, ERR_PTR() value on
533 struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
535 struct kernfs_root *root;
536 struct kernfs_node *kn;
538 root = kzalloc(sizeof(*root), GFP_KERNEL);
540 return ERR_PTR(-ENOMEM);
542 ida_init(&root->ino_ida);
544 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
547 ida_destroy(&root->ino_ida);
549 return ERR_PTR(-ENOMEM);
552 kn->flags &= ~KERNFS_REMOVED;
556 root->dir_ops = kdops;
558 init_waitqueue_head(&root->deactivate_waitq);
564 * kernfs_destroy_root - destroy a kernfs hierarchy
565 * @root: root of the hierarchy to destroy
567 * Destroy the hierarchy anchored at @root by removing all existing
568 * directories and destroying @root.
570 void kernfs_destroy_root(struct kernfs_root *root)
572 kernfs_remove(root->kn); /* will also free @root */
576 * kernfs_create_dir_ns - create a directory
577 * @parent: parent in which to create a new directory
578 * @name: name of the new directory
579 * @mode: mode of the new directory
580 * @priv: opaque data associated with the new directory
581 * @ns: optional namespace tag of the directory
583 * Returns the created node on success, ERR_PTR() value on failure.
585 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
586 const char *name, umode_t mode,
587 void *priv, const void *ns)
589 struct kernfs_node *kn;
593 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
595 return ERR_PTR(-ENOMEM);
597 kn->dir.root = parent->dir.root;
602 rc = kernfs_add_one(kn);
610 static struct dentry *kernfs_iop_lookup(struct inode *dir,
611 struct dentry *dentry,
615 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
616 struct kernfs_node *kn;
618 const void *ns = NULL;
620 mutex_lock(&kernfs_mutex);
622 if (kernfs_ns_enabled(parent))
623 ns = kernfs_info(dir->i_sb)->ns;
625 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
633 dentry->d_fsdata = kn;
635 /* attach dentry and inode */
636 inode = kernfs_get_inode(dir->i_sb, kn);
638 ret = ERR_PTR(-ENOMEM);
642 /* instantiate and hash dentry */
643 ret = d_materialise_unique(dentry, inode);
645 mutex_unlock(&kernfs_mutex);
649 static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
652 struct kernfs_node *parent = dir->i_private;
653 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
655 if (!kdops || !kdops->mkdir)
658 return kdops->mkdir(parent, dentry->d_name.name, mode);
661 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
663 struct kernfs_node *kn = dentry->d_fsdata;
664 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
666 if (!kdops || !kdops->rmdir)
669 return kdops->rmdir(kn);
672 static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
673 struct inode *new_dir, struct dentry *new_dentry)
675 struct kernfs_node *kn = old_dentry->d_fsdata;
676 struct kernfs_node *new_parent = new_dir->i_private;
677 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
679 if (!kdops || !kdops->rename)
682 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
685 const struct inode_operations kernfs_dir_iops = {
686 .lookup = kernfs_iop_lookup,
687 .permission = kernfs_iop_permission,
688 .setattr = kernfs_iop_setattr,
689 .getattr = kernfs_iop_getattr,
690 .setxattr = kernfs_iop_setxattr,
691 .removexattr = kernfs_iop_removexattr,
692 .getxattr = kernfs_iop_getxattr,
693 .listxattr = kernfs_iop_listxattr,
695 .mkdir = kernfs_iop_mkdir,
696 .rmdir = kernfs_iop_rmdir,
697 .rename = kernfs_iop_rename,
700 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
702 struct kernfs_node *last;
709 if (kernfs_type(pos) != KERNFS_DIR)
712 rbn = rb_first(&pos->dir.children);
723 * kernfs_next_descendant_post - find the next descendant for post-order walk
724 * @pos: the current position (%NULL to initiate traversal)
725 * @root: kernfs_node whose descendants to walk
727 * Find the next descendant to visit for post-order traversal of @root's
728 * descendants. @root is included in the iteration and the last node to be
731 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
732 struct kernfs_node *root)
736 lockdep_assert_held(&kernfs_mutex);
738 /* if first iteration, visit leftmost descendant which may be root */
740 return kernfs_leftmost_descendant(root);
742 /* if we visited @root, we're done */
746 /* if there's an unvisited sibling, visit its leftmost descendant */
747 rbn = rb_next(&pos->rb);
749 return kernfs_leftmost_descendant(rb_to_kn(rbn));
751 /* no sibling left, visit parent */
755 static void __kernfs_remove(struct kernfs_node *kn)
757 struct kernfs_node *pos;
759 lockdep_assert_held(&kernfs_mutex);
764 pr_debug("kernfs %s: removing\n", kn->name);
766 /* disable lookup and node creation under @kn */
768 while ((pos = kernfs_next_descendant_post(pos, kn)))
769 pos->flags |= KERNFS_REMOVED;
771 /* deactivate and unlink the subtree node-by-node */
773 pos = kernfs_leftmost_descendant(kn);
776 * kernfs_deactivate() drops kernfs_mutex temporarily and
777 * @pos's base ref could have been put by someone else by
778 * the time the function returns. Make sure it doesn't go
779 * away underneath us.
783 kernfs_deactivate(pos);
786 * kernfs_unlink_sibling() succeeds once per node. Use it
787 * to decide who's responsible for cleanups.
789 if (!pos->parent || kernfs_unlink_sibling(pos)) {
790 struct kernfs_iattrs *ps_iattr =
791 pos->parent ? pos->parent->iattr : NULL;
793 /* update timestamps on the parent */
795 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
796 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
807 * kernfs_remove - remove a kernfs_node recursively
808 * @kn: the kernfs_node to remove
810 * Remove @kn along with all its subdirectories and files.
812 void kernfs_remove(struct kernfs_node *kn)
814 mutex_lock(&kernfs_mutex);
816 mutex_unlock(&kernfs_mutex);
820 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
821 * @parent: parent of the target
822 * @name: name of the kernfs_node to remove
823 * @ns: namespace tag of the kernfs_node to remove
825 * Look for the kernfs_node with @name and @ns under @parent and remove it.
826 * Returns 0 on success, -ENOENT if such entry doesn't exist.
828 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
831 struct kernfs_node *kn;
834 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
839 mutex_lock(&kernfs_mutex);
841 kn = kernfs_find_ns(parent, name, ns);
845 mutex_unlock(&kernfs_mutex);
854 * kernfs_rename_ns - move and rename a kernfs_node
856 * @new_parent: new parent to put @sd under
857 * @new_name: new name
858 * @new_ns: new namespace tag
860 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
861 const char *new_name, const void *new_ns)
865 mutex_lock(&kernfs_mutex);
868 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
872 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
873 (strcmp(kn->name, new_name) == 0))
874 goto out; /* nothing to rename */
877 if (kernfs_find_ns(new_parent, new_name, new_ns))
880 /* rename kernfs_node */
881 if (strcmp(kn->name, new_name) != 0) {
883 new_name = kstrdup(new_name, GFP_KERNEL);
887 if (kn->flags & KERNFS_STATIC_NAME)
888 kn->flags &= ~KERNFS_STATIC_NAME;
896 * Move to the appropriate place in the appropriate directories rbtree.
898 kernfs_unlink_sibling(kn);
899 kernfs_get(new_parent);
900 kernfs_put(kn->parent);
902 kn->hash = kernfs_name_hash(kn->name, kn->ns);
903 kn->parent = new_parent;
904 kernfs_link_sibling(kn);
908 mutex_unlock(&kernfs_mutex);
912 /* Relationship between s_mode and the DT_xxx types */
913 static inline unsigned char dt_type(struct kernfs_node *kn)
915 return (kn->mode >> 12) & 15;
918 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
920 kernfs_put(filp->private_data);
924 static struct kernfs_node *kernfs_dir_pos(const void *ns,
925 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
928 int valid = !(pos->flags & KERNFS_REMOVED) &&
929 pos->parent == parent && hash == pos->hash;
934 if (!pos && (hash > 1) && (hash < INT_MAX)) {
935 struct rb_node *node = parent->dir.children.rb_node;
937 pos = rb_to_kn(node);
939 if (hash < pos->hash)
940 node = node->rb_left;
941 else if (hash > pos->hash)
942 node = node->rb_right;
947 /* Skip over entries in the wrong namespace */
948 while (pos && pos->ns != ns) {
949 struct rb_node *node = rb_next(&pos->rb);
953 pos = rb_to_kn(node);
958 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
959 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
961 pos = kernfs_dir_pos(ns, parent, ino, pos);
964 struct rb_node *node = rb_next(&pos->rb);
968 pos = rb_to_kn(node);
969 } while (pos && pos->ns != ns);
973 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
975 struct dentry *dentry = file->f_path.dentry;
976 struct kernfs_node *parent = dentry->d_fsdata;
977 struct kernfs_node *pos = file->private_data;
978 const void *ns = NULL;
980 if (!dir_emit_dots(file, ctx))
982 mutex_lock(&kernfs_mutex);
984 if (kernfs_ns_enabled(parent))
985 ns = kernfs_info(dentry->d_sb)->ns;
987 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
989 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
990 const char *name = pos->name;
991 unsigned int type = dt_type(pos);
992 int len = strlen(name);
993 ino_t ino = pos->ino;
995 ctx->pos = pos->hash;
996 file->private_data = pos;
999 mutex_unlock(&kernfs_mutex);
1000 if (!dir_emit(ctx, name, len, ino, type))
1002 mutex_lock(&kernfs_mutex);
1004 mutex_unlock(&kernfs_mutex);
1005 file->private_data = NULL;
1010 static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1013 struct inode *inode = file_inode(file);
1016 mutex_lock(&inode->i_mutex);
1017 ret = generic_file_llseek(file, offset, whence);
1018 mutex_unlock(&inode->i_mutex);
1023 const struct file_operations kernfs_dir_fops = {
1024 .read = generic_read_dir,
1025 .iterate = kernfs_fop_readdir,
1026 .release = kernfs_dir_fop_release,
1027 .llseek = kernfs_dir_fop_llseek,