1 // SPDX-License-Identifier: GPL-2.0-only
3 * fs/kernfs/dir.c - kernfs directory implementation
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
10 #include <linux/sched.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 static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */
22 * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to
23 * call pr_cont() while holding rename_lock. Because sometimes pr_cont()
24 * will perform wakeups when releasing console_sem. Holding rename_lock
25 * will introduce deadlock if the scheduler reads the kernfs_name in the
28 static DEFINE_SPINLOCK(kernfs_pr_cont_lock);
29 static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */
30 static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */
32 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
34 static bool kernfs_active(struct kernfs_node *kn)
36 lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
37 return atomic_read(&kn->active) >= 0;
40 static bool kernfs_lockdep(struct kernfs_node *kn)
42 #ifdef CONFIG_DEBUG_LOCK_ALLOC
43 return kn->flags & KERNFS_LOCKDEP;
49 static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
52 return strlcpy(buf, "(null)", buflen);
54 return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
57 /* kernfs_node_depth - compute depth from @from to @to */
58 static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
62 while (to->parent && to != from) {
69 static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
70 struct kernfs_node *b)
73 struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b);
78 da = kernfs_depth(ra->kn, a);
79 db = kernfs_depth(rb->kn, b);
90 /* worst case b and a will be the same at root */
100 * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
101 * where kn_from is treated as root of the path.
102 * @kn_from: kernfs node which should be treated as root for the path
103 * @kn_to: kernfs node to which path is needed
104 * @buf: buffer to copy the path into
105 * @buflen: size of @buf
107 * We need to handle couple of scenarios here:
108 * [1] when @kn_from is an ancestor of @kn_to at some level
110 * kn_to: /n1/n2/n3/n4/n5
113 * [2] when @kn_from is on a different hierarchy and we need to find common
114 * ancestor between @kn_from and @kn_to.
115 * kn_from: /n1/n2/n3/n4
119 * kn_from: /n1/n2/n3/n4/n5 [depth=5]
120 * kn_to: /n1/n2/n3 [depth=3]
123 * [3] when @kn_to is NULL result will be "(null)"
125 * Returns the length of the full path. If the full length is equal to or
126 * greater than @buflen, @buf contains the truncated path with the trailing
127 * '\0'. On error, -errno is returned.
129 static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
130 struct kernfs_node *kn_from,
131 char *buf, size_t buflen)
133 struct kernfs_node *kn, *common;
134 const char parent_str[] = "/..";
135 size_t depth_from, depth_to, len = 0;
139 return strlcpy(buf, "(null)", buflen);
142 kn_from = kernfs_root(kn_to)->kn;
144 if (kn_from == kn_to)
145 return strlcpy(buf, "/", buflen);
150 common = kernfs_common_ancestor(kn_from, kn_to);
151 if (WARN_ON(!common))
154 depth_to = kernfs_depth(common, kn_to);
155 depth_from = kernfs_depth(common, kn_from);
159 for (i = 0; i < depth_from; i++)
160 len += strlcpy(buf + len, parent_str,
161 len < buflen ? buflen - len : 0);
163 /* Calculate how many bytes we need for the rest */
164 for (i = depth_to - 1; i >= 0; i--) {
165 for (kn = kn_to, j = 0; j < i; j++)
167 len += strlcpy(buf + len, "/",
168 len < buflen ? buflen - len : 0);
169 len += strlcpy(buf + len, kn->name,
170 len < buflen ? buflen - len : 0);
177 * kernfs_name - obtain the name of a given node
178 * @kn: kernfs_node of interest
179 * @buf: buffer to copy @kn's name into
180 * @buflen: size of @buf
182 * Copies the name of @kn into @buf of @buflen bytes. The behavior is
183 * similar to strlcpy(). It returns the length of @kn's name and if @buf
184 * isn't long enough, it's filled upto @buflen-1 and nul terminated.
186 * Fills buffer with "(null)" if @kn is NULL.
188 * This function can be called from any context.
190 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
195 spin_lock_irqsave(&kernfs_rename_lock, flags);
196 ret = kernfs_name_locked(kn, buf, buflen);
197 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
202 * kernfs_path_from_node - build path of node @to relative to @from.
203 * @from: parent kernfs_node relative to which we need to build the path
204 * @to: kernfs_node of interest
205 * @buf: buffer to copy @to's path into
206 * @buflen: size of @buf
208 * Builds @to's path relative to @from in @buf. @from and @to must
209 * be on the same kernfs-root. If @from is not parent of @to, then a relative
210 * path (which includes '..'s) as needed to reach from @from to @to is
213 * Returns the length of the full path. If the full length is equal to or
214 * greater than @buflen, @buf contains the truncated path with the trailing
215 * '\0'. On error, -errno is returned.
217 int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
218 char *buf, size_t buflen)
223 spin_lock_irqsave(&kernfs_rename_lock, flags);
224 ret = kernfs_path_from_node_locked(to, from, buf, buflen);
225 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
228 EXPORT_SYMBOL_GPL(kernfs_path_from_node);
231 * pr_cont_kernfs_name - pr_cont name of a kernfs_node
232 * @kn: kernfs_node of interest
234 * This function can be called from any context.
236 void pr_cont_kernfs_name(struct kernfs_node *kn)
240 spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
242 kernfs_name(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
243 pr_cont("%s", kernfs_pr_cont_buf);
245 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
249 * pr_cont_kernfs_path - pr_cont path of a kernfs_node
250 * @kn: kernfs_node of interest
252 * This function can be called from any context.
254 void pr_cont_kernfs_path(struct kernfs_node *kn)
259 spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
261 sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf,
262 sizeof(kernfs_pr_cont_buf));
268 if (sz >= sizeof(kernfs_pr_cont_buf)) {
269 pr_cont("(name too long)");
273 pr_cont("%s", kernfs_pr_cont_buf);
276 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
280 * kernfs_get_parent - determine the parent node and pin it
281 * @kn: kernfs_node of interest
283 * Determines @kn's parent, pins and returns it. This function can be
284 * called from any context.
286 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
288 struct kernfs_node *parent;
291 spin_lock_irqsave(&kernfs_rename_lock, flags);
294 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
301 * @name: Null terminated string to hash
302 * @ns: Namespace tag to hash
304 * Returns 31 bit hash of ns + name (so it fits in an off_t )
306 static unsigned int kernfs_name_hash(const char *name, const void *ns)
308 unsigned long hash = init_name_hash(ns);
309 unsigned int len = strlen(name);
311 hash = partial_name_hash(*name++, hash);
312 hash = end_name_hash(hash);
314 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
322 static int kernfs_name_compare(unsigned int hash, const char *name,
323 const void *ns, const struct kernfs_node *kn)
333 return strcmp(name, kn->name);
336 static int kernfs_sd_compare(const struct kernfs_node *left,
337 const struct kernfs_node *right)
339 return kernfs_name_compare(left->hash, left->name, left->ns, right);
343 * kernfs_link_sibling - link kernfs_node into sibling rbtree
344 * @kn: kernfs_node of interest
346 * Link @kn into its sibling rbtree which starts from
347 * @kn->parent->dir.children.
350 * kernfs_rwsem held exclusive
353 * 0 on susccess -EEXIST on failure.
355 static int kernfs_link_sibling(struct kernfs_node *kn)
357 struct rb_node **node = &kn->parent->dir.children.rb_node;
358 struct rb_node *parent = NULL;
361 struct kernfs_node *pos;
364 pos = rb_to_kn(*node);
366 result = kernfs_sd_compare(kn, pos);
368 node = &pos->rb.rb_left;
370 node = &pos->rb.rb_right;
375 /* add new node and rebalance the tree */
376 rb_link_node(&kn->rb, parent, node);
377 rb_insert_color(&kn->rb, &kn->parent->dir.children);
379 /* successfully added, account subdir number */
380 if (kernfs_type(kn) == KERNFS_DIR)
381 kn->parent->dir.subdirs++;
382 kernfs_inc_rev(kn->parent);
388 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
389 * @kn: kernfs_node of interest
391 * Try to unlink @kn from its sibling rbtree which starts from
392 * kn->parent->dir.children. Returns %true if @kn was actually
393 * removed, %false if @kn wasn't on the rbtree.
396 * kernfs_rwsem held exclusive
398 static bool kernfs_unlink_sibling(struct kernfs_node *kn)
400 if (RB_EMPTY_NODE(&kn->rb))
403 if (kernfs_type(kn) == KERNFS_DIR)
404 kn->parent->dir.subdirs--;
405 kernfs_inc_rev(kn->parent);
407 rb_erase(&kn->rb, &kn->parent->dir.children);
408 RB_CLEAR_NODE(&kn->rb);
413 * kernfs_get_active - get an active reference to kernfs_node
414 * @kn: kernfs_node to get an active reference to
416 * Get an active reference of @kn. This function is noop if @kn
420 * Pointer to @kn on success, NULL on failure.
422 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
427 if (!atomic_inc_unless_negative(&kn->active))
430 if (kernfs_lockdep(kn))
431 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
436 * kernfs_put_active - put an active reference to kernfs_node
437 * @kn: kernfs_node to put an active reference to
439 * Put an active reference to @kn. This function is noop if @kn
442 void kernfs_put_active(struct kernfs_node *kn)
449 if (kernfs_lockdep(kn))
450 rwsem_release(&kn->dep_map, _RET_IP_);
451 v = atomic_dec_return(&kn->active);
452 if (likely(v != KN_DEACTIVATED_BIAS))
455 wake_up_all(&kernfs_root(kn)->deactivate_waitq);
459 * kernfs_drain - drain kernfs_node
460 * @kn: kernfs_node to drain
462 * Drain existing usages and nuke all existing mmaps of @kn. Mutiple
463 * removers may invoke this function concurrently on @kn and all will
464 * return after draining is complete.
466 static void kernfs_drain(struct kernfs_node *kn)
467 __releases(&kernfs_root(kn)->kernfs_rwsem)
468 __acquires(&kernfs_root(kn)->kernfs_rwsem)
470 struct kernfs_root *root = kernfs_root(kn);
472 lockdep_assert_held_write(&root->kernfs_rwsem);
473 WARN_ON_ONCE(kernfs_active(kn));
476 * Skip draining if already fully drained. This avoids draining and its
477 * lockdep annotations for nodes which have never been activated
478 * allowing embedding kernfs_remove() in create error paths without
479 * worrying about draining.
481 if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS &&
482 !kernfs_should_drain_open_files(kn))
485 up_write(&root->kernfs_rwsem);
487 if (kernfs_lockdep(kn)) {
488 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
489 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
490 lock_contended(&kn->dep_map, _RET_IP_);
493 wait_event(root->deactivate_waitq,
494 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
496 if (kernfs_lockdep(kn)) {
497 lock_acquired(&kn->dep_map, _RET_IP_);
498 rwsem_release(&kn->dep_map, _RET_IP_);
501 if (kernfs_should_drain_open_files(kn))
502 kernfs_drain_open_files(kn);
504 down_write(&root->kernfs_rwsem);
508 * kernfs_get - get a reference count on a kernfs_node
509 * @kn: the target kernfs_node
511 void kernfs_get(struct kernfs_node *kn)
514 WARN_ON(!atomic_read(&kn->count));
515 atomic_inc(&kn->count);
518 EXPORT_SYMBOL_GPL(kernfs_get);
521 * kernfs_put - put a reference count on a kernfs_node
522 * @kn: the target kernfs_node
524 * Put a reference count of @kn and destroy it if it reached zero.
526 void kernfs_put(struct kernfs_node *kn)
528 struct kernfs_node *parent;
529 struct kernfs_root *root;
531 if (!kn || !atomic_dec_and_test(&kn->count))
533 root = kernfs_root(kn);
536 * Moving/renaming is always done while holding reference.
537 * kn->parent won't change beneath us.
541 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
542 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
543 parent ? parent->name : "", kn->name, atomic_read(&kn->active));
545 if (kernfs_type(kn) == KERNFS_LINK)
546 kernfs_put(kn->symlink.target_kn);
548 kfree_const(kn->name);
551 simple_xattrs_free(&kn->iattr->xattrs);
552 kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
554 spin_lock(&kernfs_idr_lock);
555 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
556 spin_unlock(&kernfs_idr_lock);
557 kmem_cache_free(kernfs_node_cache, kn);
561 if (atomic_dec_and_test(&kn->count))
564 /* just released the root kn, free @root too */
565 idr_destroy(&root->ino_idr);
569 EXPORT_SYMBOL_GPL(kernfs_put);
572 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
573 * @dentry: the dentry in question
575 * Return the kernfs_node associated with @dentry. If @dentry is not a
576 * kernfs one, %NULL is returned.
578 * While the returned kernfs_node will stay accessible as long as @dentry
579 * is accessible, the returned node can be in any state and the caller is
580 * fully responsible for determining what's accessible.
582 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
584 if (dentry->d_sb->s_op == &kernfs_sops)
585 return kernfs_dentry_node(dentry);
589 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
590 struct kernfs_node *parent,
591 const char *name, umode_t mode,
592 kuid_t uid, kgid_t gid,
595 struct kernfs_node *kn;
599 name = kstrdup_const(name, GFP_KERNEL);
603 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
607 idr_preload(GFP_KERNEL);
608 spin_lock(&kernfs_idr_lock);
609 ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
610 if (ret >= 0 && ret < root->last_id_lowbits)
612 id_highbits = root->id_highbits;
613 root->last_id_lowbits = ret;
614 spin_unlock(&kernfs_idr_lock);
619 kn->id = (u64)id_highbits << 32 | ret;
621 atomic_set(&kn->count, 1);
622 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
623 RB_CLEAR_NODE(&kn->rb);
629 if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) {
630 struct iattr iattr = {
631 .ia_valid = ATTR_UID | ATTR_GID,
636 ret = __kernfs_setattr(kn, &iattr);
642 ret = security_kernfs_init_security(parent, kn);
650 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
652 kmem_cache_free(kernfs_node_cache, kn);
658 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
659 const char *name, umode_t mode,
660 kuid_t uid, kgid_t gid,
663 struct kernfs_node *kn;
665 kn = __kernfs_new_node(kernfs_root(parent), parent,
666 name, mode, uid, gid, flags);
675 * kernfs_find_and_get_node_by_id - get kernfs_node from node id
676 * @root: the kernfs root
677 * @id: the target node id
679 * @id's lower 32bits encode ino and upper gen. If the gen portion is
680 * zero, all generations are matched.
683 * NULL on failure. Return a kernfs node with reference counter incremented
685 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
688 struct kernfs_node *kn;
689 ino_t ino = kernfs_id_ino(id);
690 u32 gen = kernfs_id_gen(id);
692 spin_lock(&kernfs_idr_lock);
694 kn = idr_find(&root->ino_idr, (u32)ino);
698 if (sizeof(ino_t) >= sizeof(u64)) {
699 /* we looked up with the low 32bits, compare the whole */
700 if (kernfs_ino(kn) != ino)
703 /* 0 matches all generations */
704 if (unlikely(gen && kernfs_gen(kn) != gen))
708 if (unlikely(!kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
711 spin_unlock(&kernfs_idr_lock);
714 spin_unlock(&kernfs_idr_lock);
719 * kernfs_add_one - add kernfs_node to parent without warning
720 * @kn: kernfs_node to be added
722 * The caller must already have initialized @kn->parent. This
723 * function increments nlink of the parent's inode if @kn is a
724 * directory and link into the children list of the parent.
727 * 0 on success, -EEXIST if entry with the given name already
730 int kernfs_add_one(struct kernfs_node *kn)
732 struct kernfs_node *parent = kn->parent;
733 struct kernfs_root *root = kernfs_root(parent);
734 struct kernfs_iattrs *ps_iattr;
738 down_write(&root->kernfs_rwsem);
741 has_ns = kernfs_ns_enabled(parent);
742 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
743 has_ns ? "required" : "invalid", parent->name, kn->name))
746 if (kernfs_type(parent) != KERNFS_DIR)
750 if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR))
753 kn->hash = kernfs_name_hash(kn->name, kn->ns);
755 ret = kernfs_link_sibling(kn);
759 /* Update timestamps on the parent */
760 ps_iattr = parent->iattr;
762 ktime_get_real_ts64(&ps_iattr->ia_ctime);
763 ps_iattr->ia_mtime = ps_iattr->ia_ctime;
766 up_write(&root->kernfs_rwsem);
769 * Activate the new node unless CREATE_DEACTIVATED is requested.
770 * If not activated here, the kernfs user is responsible for
771 * activating the node with kernfs_activate(). A node which hasn't
772 * been activated is not visible to userland and its removal won't
773 * trigger deactivation.
775 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
780 up_write(&root->kernfs_rwsem);
785 * kernfs_find_ns - find kernfs_node with the given name
786 * @parent: kernfs_node to search under
787 * @name: name to look for
788 * @ns: the namespace tag to use
790 * Look for kernfs_node with name @name under @parent. Returns pointer to
791 * the found kernfs_node on success, %NULL on failure.
793 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
794 const unsigned char *name,
797 struct rb_node *node = parent->dir.children.rb_node;
798 bool has_ns = kernfs_ns_enabled(parent);
801 lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem);
803 if (has_ns != (bool)ns) {
804 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
805 has_ns ? "required" : "invalid", parent->name, name);
809 hash = kernfs_name_hash(name, ns);
811 struct kernfs_node *kn;
815 result = kernfs_name_compare(hash, name, ns, kn);
817 node = node->rb_left;
819 node = node->rb_right;
826 static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
827 const unsigned char *path,
833 lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem);
835 spin_lock_irq(&kernfs_pr_cont_lock);
837 len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
839 if (len >= sizeof(kernfs_pr_cont_buf)) {
840 spin_unlock_irq(&kernfs_pr_cont_lock);
844 p = kernfs_pr_cont_buf;
846 while ((name = strsep(&p, "/")) && parent) {
849 parent = kernfs_find_ns(parent, name, ns);
852 spin_unlock_irq(&kernfs_pr_cont_lock);
858 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
859 * @parent: kernfs_node to search under
860 * @name: name to look for
861 * @ns: the namespace tag to use
863 * Look for kernfs_node with name @name under @parent and get a reference
864 * if found. This function may sleep and returns pointer to the found
865 * kernfs_node on success, %NULL on failure.
867 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
868 const char *name, const void *ns)
870 struct kernfs_node *kn;
871 struct kernfs_root *root = kernfs_root(parent);
873 down_read(&root->kernfs_rwsem);
874 kn = kernfs_find_ns(parent, name, ns);
876 up_read(&root->kernfs_rwsem);
880 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
883 * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
884 * @parent: kernfs_node to search under
885 * @path: path to look for
886 * @ns: the namespace tag to use
888 * Look for kernfs_node with path @path under @parent and get a reference
889 * if found. This function may sleep and returns pointer to the found
890 * kernfs_node on success, %NULL on failure.
892 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
893 const char *path, const void *ns)
895 struct kernfs_node *kn;
896 struct kernfs_root *root = kernfs_root(parent);
898 down_read(&root->kernfs_rwsem);
899 kn = kernfs_walk_ns(parent, path, ns);
901 up_read(&root->kernfs_rwsem);
907 * kernfs_create_root - create a new kernfs hierarchy
908 * @scops: optional syscall operations for the hierarchy
909 * @flags: KERNFS_ROOT_* flags
910 * @priv: opaque data associated with the new directory
912 * Returns the root of the new hierarchy on success, ERR_PTR() value on
915 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
916 unsigned int flags, void *priv)
918 struct kernfs_root *root;
919 struct kernfs_node *kn;
921 root = kzalloc(sizeof(*root), GFP_KERNEL);
923 return ERR_PTR(-ENOMEM);
925 idr_init(&root->ino_idr);
926 init_rwsem(&root->kernfs_rwsem);
927 INIT_LIST_HEAD(&root->supers);
930 * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino.
931 * High bits generation. The starting value for both ino and
932 * genenration is 1. Initialize upper 32bit allocation
935 if (sizeof(ino_t) >= sizeof(u64))
936 root->id_highbits = 0;
938 root->id_highbits = 1;
940 kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO,
941 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
944 idr_destroy(&root->ino_idr);
946 return ERR_PTR(-ENOMEM);
952 root->syscall_ops = scops;
955 init_waitqueue_head(&root->deactivate_waitq);
957 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
964 * kernfs_destroy_root - destroy a kernfs hierarchy
965 * @root: root of the hierarchy to destroy
967 * Destroy the hierarchy anchored at @root by removing all existing
968 * directories and destroying @root.
970 void kernfs_destroy_root(struct kernfs_root *root)
973 * kernfs_remove holds kernfs_rwsem from the root so the root
974 * shouldn't be freed during the operation.
976 kernfs_get(root->kn);
977 kernfs_remove(root->kn);
978 kernfs_put(root->kn); /* will also free @root */
982 * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root
983 * @root: root to use to lookup
985 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root)
991 * kernfs_create_dir_ns - create a directory
992 * @parent: parent in which to create a new directory
993 * @name: name of the new directory
994 * @mode: mode of the new directory
995 * @uid: uid of the new directory
996 * @gid: gid of the new directory
997 * @priv: opaque data associated with the new directory
998 * @ns: optional namespace tag of the directory
1000 * Returns the created node on success, ERR_PTR() value on failure.
1002 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
1003 const char *name, umode_t mode,
1004 kuid_t uid, kgid_t gid,
1005 void *priv, const void *ns)
1007 struct kernfs_node *kn;
1011 kn = kernfs_new_node(parent, name, mode | S_IFDIR,
1012 uid, gid, KERNFS_DIR);
1014 return ERR_PTR(-ENOMEM);
1016 kn->dir.root = parent->dir.root;
1021 rc = kernfs_add_one(kn);
1030 * kernfs_create_empty_dir - create an always empty directory
1031 * @parent: parent in which to create a new directory
1032 * @name: name of the new directory
1034 * Returns the created node on success, ERR_PTR() value on failure.
1036 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
1039 struct kernfs_node *kn;
1043 kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR,
1044 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR);
1046 return ERR_PTR(-ENOMEM);
1048 kn->flags |= KERNFS_EMPTY_DIR;
1049 kn->dir.root = parent->dir.root;
1054 rc = kernfs_add_one(kn);
1062 static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
1064 struct kernfs_node *kn;
1065 struct kernfs_root *root;
1067 if (flags & LOOKUP_RCU)
1070 /* Negative hashed dentry? */
1071 if (d_really_is_negative(dentry)) {
1072 struct kernfs_node *parent;
1074 /* If the kernfs parent node has changed discard and
1075 * proceed to ->lookup.
1077 spin_lock(&dentry->d_lock);
1078 parent = kernfs_dentry_node(dentry->d_parent);
1080 spin_unlock(&dentry->d_lock);
1081 root = kernfs_root(parent);
1082 down_read(&root->kernfs_rwsem);
1083 if (kernfs_dir_changed(parent, dentry)) {
1084 up_read(&root->kernfs_rwsem);
1087 up_read(&root->kernfs_rwsem);
1089 spin_unlock(&dentry->d_lock);
1091 /* The kernfs parent node hasn't changed, leave the
1092 * dentry negative and return success.
1097 kn = kernfs_dentry_node(dentry);
1098 root = kernfs_root(kn);
1099 down_read(&root->kernfs_rwsem);
1101 /* The kernfs node has been deactivated */
1102 if (!kernfs_active(kn))
1105 /* The kernfs node has been moved? */
1106 if (kernfs_dentry_node(dentry->d_parent) != kn->parent)
1109 /* The kernfs node has been renamed */
1110 if (strcmp(dentry->d_name.name, kn->name) != 0)
1113 /* The kernfs node has been moved to a different namespace */
1114 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
1115 kernfs_info(dentry->d_sb)->ns != kn->ns)
1118 up_read(&root->kernfs_rwsem);
1121 up_read(&root->kernfs_rwsem);
1125 const struct dentry_operations kernfs_dops = {
1126 .d_revalidate = kernfs_dop_revalidate,
1129 static struct dentry *kernfs_iop_lookup(struct inode *dir,
1130 struct dentry *dentry,
1133 struct kernfs_node *parent = dir->i_private;
1134 struct kernfs_node *kn;
1135 struct kernfs_root *root;
1136 struct inode *inode = NULL;
1137 const void *ns = NULL;
1139 root = kernfs_root(parent);
1140 down_read(&root->kernfs_rwsem);
1141 if (kernfs_ns_enabled(parent))
1142 ns = kernfs_info(dir->i_sb)->ns;
1144 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
1145 /* attach dentry and inode */
1147 /* Inactive nodes are invisible to the VFS so don't
1148 * create a negative.
1150 if (!kernfs_active(kn)) {
1151 up_read(&root->kernfs_rwsem);
1154 inode = kernfs_get_inode(dir->i_sb, kn);
1156 inode = ERR_PTR(-ENOMEM);
1159 * Needed for negative dentry validation.
1160 * The negative dentry can be created in kernfs_iop_lookup()
1161 * or transforms from positive dentry in dentry_unlink_inode()
1162 * called from vfs_rmdir().
1165 kernfs_set_rev(parent, dentry);
1166 up_read(&root->kernfs_rwsem);
1168 /* instantiate and hash (possibly negative) dentry */
1169 return d_splice_alias(inode, dentry);
1172 static int kernfs_iop_mkdir(struct user_namespace *mnt_userns,
1173 struct inode *dir, struct dentry *dentry,
1176 struct kernfs_node *parent = dir->i_private;
1177 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
1180 if (!scops || !scops->mkdir)
1183 if (!kernfs_get_active(parent))
1186 ret = scops->mkdir(parent, dentry->d_name.name, mode);
1188 kernfs_put_active(parent);
1192 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
1194 struct kernfs_node *kn = kernfs_dentry_node(dentry);
1195 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
1198 if (!scops || !scops->rmdir)
1201 if (!kernfs_get_active(kn))
1204 ret = scops->rmdir(kn);
1206 kernfs_put_active(kn);
1210 static int kernfs_iop_rename(struct user_namespace *mnt_userns,
1211 struct inode *old_dir, struct dentry *old_dentry,
1212 struct inode *new_dir, struct dentry *new_dentry,
1215 struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
1216 struct kernfs_node *new_parent = new_dir->i_private;
1217 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
1223 if (!scops || !scops->rename)
1226 if (!kernfs_get_active(kn))
1229 if (!kernfs_get_active(new_parent)) {
1230 kernfs_put_active(kn);
1234 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
1236 kernfs_put_active(new_parent);
1237 kernfs_put_active(kn);
1241 const struct inode_operations kernfs_dir_iops = {
1242 .lookup = kernfs_iop_lookup,
1243 .permission = kernfs_iop_permission,
1244 .setattr = kernfs_iop_setattr,
1245 .getattr = kernfs_iop_getattr,
1246 .listxattr = kernfs_iop_listxattr,
1248 .mkdir = kernfs_iop_mkdir,
1249 .rmdir = kernfs_iop_rmdir,
1250 .rename = kernfs_iop_rename,
1253 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
1255 struct kernfs_node *last;
1258 struct rb_node *rbn;
1262 if (kernfs_type(pos) != KERNFS_DIR)
1265 rbn = rb_first(&pos->dir.children);
1269 pos = rb_to_kn(rbn);
1276 * kernfs_next_descendant_post - find the next descendant for post-order walk
1277 * @pos: the current position (%NULL to initiate traversal)
1278 * @root: kernfs_node whose descendants to walk
1280 * Find the next descendant to visit for post-order traversal of @root's
1281 * descendants. @root is included in the iteration and the last node to be
1284 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
1285 struct kernfs_node *root)
1287 struct rb_node *rbn;
1289 lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem);
1291 /* if first iteration, visit leftmost descendant which may be root */
1293 return kernfs_leftmost_descendant(root);
1295 /* if we visited @root, we're done */
1299 /* if there's an unvisited sibling, visit its leftmost descendant */
1300 rbn = rb_next(&pos->rb);
1302 return kernfs_leftmost_descendant(rb_to_kn(rbn));
1304 /* no sibling left, visit parent */
1308 static void kernfs_activate_one(struct kernfs_node *kn)
1310 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
1312 kn->flags |= KERNFS_ACTIVATED;
1314 if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING)))
1317 WARN_ON_ONCE(kn->parent && RB_EMPTY_NODE(&kn->rb));
1318 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
1320 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
1324 * kernfs_activate - activate a node which started deactivated
1325 * @kn: kernfs_node whose subtree is to be activated
1327 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
1328 * needs to be explicitly activated. A node which hasn't been activated
1329 * isn't visible to userland and deactivation is skipped during its
1330 * removal. This is useful to construct atomic init sequences where
1331 * creation of multiple nodes should either succeed or fail atomically.
1333 * The caller is responsible for ensuring that this function is not called
1334 * after kernfs_remove*() is invoked on @kn.
1336 void kernfs_activate(struct kernfs_node *kn)
1338 struct kernfs_node *pos;
1339 struct kernfs_root *root = kernfs_root(kn);
1341 down_write(&root->kernfs_rwsem);
1344 while ((pos = kernfs_next_descendant_post(pos, kn)))
1345 kernfs_activate_one(pos);
1347 up_write(&root->kernfs_rwsem);
1351 * kernfs_show - show or hide a node
1352 * @kn: kernfs_node to show or hide
1353 * @show: whether to show or hide
1355 * If @show is %false, @kn is marked hidden and deactivated. A hidden node is
1356 * ignored in future activaitons. If %true, the mark is removed and activation
1357 * state is restored. This function won't implicitly activate a new node in a
1358 * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet.
1360 * To avoid recursion complexities, directories aren't supported for now.
1362 void kernfs_show(struct kernfs_node *kn, bool show)
1364 struct kernfs_root *root = kernfs_root(kn);
1366 if (WARN_ON_ONCE(kernfs_type(kn) == KERNFS_DIR))
1369 down_write(&root->kernfs_rwsem);
1372 kn->flags &= ~KERNFS_HIDDEN;
1373 if (kn->flags & KERNFS_ACTIVATED)
1374 kernfs_activate_one(kn);
1376 kn->flags |= KERNFS_HIDDEN;
1377 if (kernfs_active(kn))
1378 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
1382 up_write(&root->kernfs_rwsem);
1385 static void __kernfs_remove(struct kernfs_node *kn)
1387 struct kernfs_node *pos;
1389 /* Short-circuit if non-root @kn has already finished removal. */
1393 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
1396 * This is for kernfs_remove_self() which plays with active ref
1399 if (kn->parent && RB_EMPTY_NODE(&kn->rb))
1402 pr_debug("kernfs %s: removing\n", kn->name);
1404 /* prevent new usage by marking all nodes removing and deactivating */
1406 while ((pos = kernfs_next_descendant_post(pos, kn))) {
1407 pos->flags |= KERNFS_REMOVING;
1408 if (kernfs_active(pos))
1409 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
1412 /* deactivate and unlink the subtree node-by-node */
1414 pos = kernfs_leftmost_descendant(kn);
1417 * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's
1418 * base ref could have been put by someone else by the time
1419 * the function returns. Make sure it doesn't go away
1427 * kernfs_unlink_sibling() succeeds once per node. Use it
1428 * to decide who's responsible for cleanups.
1430 if (!pos->parent || kernfs_unlink_sibling(pos)) {
1431 struct kernfs_iattrs *ps_iattr =
1432 pos->parent ? pos->parent->iattr : NULL;
1434 /* update timestamps on the parent */
1436 ktime_get_real_ts64(&ps_iattr->ia_ctime);
1437 ps_iattr->ia_mtime = ps_iattr->ia_ctime;
1444 } while (pos != kn);
1448 * kernfs_remove - remove a kernfs_node recursively
1449 * @kn: the kernfs_node to remove
1451 * Remove @kn along with all its subdirectories and files.
1453 void kernfs_remove(struct kernfs_node *kn)
1455 struct kernfs_root *root;
1460 root = kernfs_root(kn);
1462 down_write(&root->kernfs_rwsem);
1463 __kernfs_remove(kn);
1464 up_write(&root->kernfs_rwsem);
1468 * kernfs_break_active_protection - break out of active protection
1469 * @kn: the self kernfs_node
1471 * The caller must be running off of a kernfs operation which is invoked
1472 * with an active reference - e.g. one of kernfs_ops. Each invocation of
1473 * this function must also be matched with an invocation of
1474 * kernfs_unbreak_active_protection().
1476 * This function releases the active reference of @kn the caller is
1477 * holding. Once this function is called, @kn may be removed at any point
1478 * and the caller is solely responsible for ensuring that the objects it
1479 * dereferences are accessible.
1481 void kernfs_break_active_protection(struct kernfs_node *kn)
1484 * Take out ourself out of the active ref dependency chain. If
1485 * we're called without an active ref, lockdep will complain.
1487 kernfs_put_active(kn);
1491 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
1492 * @kn: the self kernfs_node
1494 * If kernfs_break_active_protection() was called, this function must be
1495 * invoked before finishing the kernfs operation. Note that while this
1496 * function restores the active reference, it doesn't and can't actually
1497 * restore the active protection - @kn may already or be in the process of
1498 * being removed. Once kernfs_break_active_protection() is invoked, that
1499 * protection is irreversibly gone for the kernfs operation instance.
1501 * While this function may be called at any point after
1502 * kernfs_break_active_protection() is invoked, its most useful location
1503 * would be right before the enclosing kernfs operation returns.
1505 void kernfs_unbreak_active_protection(struct kernfs_node *kn)
1508 * @kn->active could be in any state; however, the increment we do
1509 * here will be undone as soon as the enclosing kernfs operation
1510 * finishes and this temporary bump can't break anything. If @kn
1511 * is alive, nothing changes. If @kn is being deactivated, the
1512 * soon-to-follow put will either finish deactivation or restore
1513 * deactivated state. If @kn is already removed, the temporary
1514 * bump is guaranteed to be gone before @kn is released.
1516 atomic_inc(&kn->active);
1517 if (kernfs_lockdep(kn))
1518 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
1522 * kernfs_remove_self - remove a kernfs_node from its own method
1523 * @kn: the self kernfs_node to remove
1525 * The caller must be running off of a kernfs operation which is invoked
1526 * with an active reference - e.g. one of kernfs_ops. This can be used to
1527 * implement a file operation which deletes itself.
1529 * For example, the "delete" file for a sysfs device directory can be
1530 * implemented by invoking kernfs_remove_self() on the "delete" file
1531 * itself. This function breaks the circular dependency of trying to
1532 * deactivate self while holding an active ref itself. It isn't necessary
1533 * to modify the usual removal path to use kernfs_remove_self(). The
1534 * "delete" implementation can simply invoke kernfs_remove_self() on self
1535 * before proceeding with the usual removal path. kernfs will ignore later
1536 * kernfs_remove() on self.
1538 * kernfs_remove_self() can be called multiple times concurrently on the
1539 * same kernfs_node. Only the first one actually performs removal and
1540 * returns %true. All others will wait until the kernfs operation which
1541 * won self-removal finishes and return %false. Note that the losers wait
1542 * for the completion of not only the winning kernfs_remove_self() but also
1543 * the whole kernfs_ops which won the arbitration. This can be used to
1544 * guarantee, for example, all concurrent writes to a "delete" file to
1545 * finish only after the whole operation is complete.
1547 bool kernfs_remove_self(struct kernfs_node *kn)
1550 struct kernfs_root *root = kernfs_root(kn);
1552 down_write(&root->kernfs_rwsem);
1553 kernfs_break_active_protection(kn);
1556 * SUICIDAL is used to arbitrate among competing invocations. Only
1557 * the first one will actually perform removal. When the removal
1558 * is complete, SUICIDED is set and the active ref is restored
1559 * while kernfs_rwsem for held exclusive. The ones which lost
1560 * arbitration waits for SUICIDED && drained which can happen only
1561 * after the enclosing kernfs operation which executed the winning
1562 * instance of kernfs_remove_self() finished.
1564 if (!(kn->flags & KERNFS_SUICIDAL)) {
1565 kn->flags |= KERNFS_SUICIDAL;
1566 __kernfs_remove(kn);
1567 kn->flags |= KERNFS_SUICIDED;
1570 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
1574 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
1576 if ((kn->flags & KERNFS_SUICIDED) &&
1577 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
1580 up_write(&root->kernfs_rwsem);
1582 down_write(&root->kernfs_rwsem);
1584 finish_wait(waitq, &wait);
1585 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
1590 * This must be done while kernfs_rwsem held exclusive; otherwise,
1591 * waiting for SUICIDED && deactivated could finish prematurely.
1593 kernfs_unbreak_active_protection(kn);
1595 up_write(&root->kernfs_rwsem);
1600 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1601 * @parent: parent of the target
1602 * @name: name of the kernfs_node to remove
1603 * @ns: namespace tag of the kernfs_node to remove
1605 * Look for the kernfs_node with @name and @ns under @parent and remove it.
1606 * Returns 0 on success, -ENOENT if such entry doesn't exist.
1608 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
1611 struct kernfs_node *kn;
1612 struct kernfs_root *root;
1615 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
1620 root = kernfs_root(parent);
1621 down_write(&root->kernfs_rwsem);
1623 kn = kernfs_find_ns(parent, name, ns);
1626 __kernfs_remove(kn);
1630 up_write(&root->kernfs_rwsem);
1639 * kernfs_rename_ns - move and rename a kernfs_node
1641 * @new_parent: new parent to put @sd under
1642 * @new_name: new name
1643 * @new_ns: new namespace tag
1645 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
1646 const char *new_name, const void *new_ns)
1648 struct kernfs_node *old_parent;
1649 struct kernfs_root *root;
1650 const char *old_name = NULL;
1653 /* can't move or rename root */
1657 root = kernfs_root(kn);
1658 down_write(&root->kernfs_rwsem);
1661 if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
1662 (new_parent->flags & KERNFS_EMPTY_DIR))
1666 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1667 (strcmp(kn->name, new_name) == 0))
1668 goto out; /* nothing to rename */
1671 if (kernfs_find_ns(new_parent, new_name, new_ns))
1674 /* rename kernfs_node */
1675 if (strcmp(kn->name, new_name) != 0) {
1677 new_name = kstrdup_const(new_name, GFP_KERNEL);
1685 * Move to the appropriate place in the appropriate directories rbtree.
1687 kernfs_unlink_sibling(kn);
1688 kernfs_get(new_parent);
1690 /* rename_lock protects ->parent and ->name accessors */
1691 spin_lock_irq(&kernfs_rename_lock);
1693 old_parent = kn->parent;
1694 kn->parent = new_parent;
1698 old_name = kn->name;
1699 kn->name = new_name;
1702 spin_unlock_irq(&kernfs_rename_lock);
1704 kn->hash = kernfs_name_hash(kn->name, kn->ns);
1705 kernfs_link_sibling(kn);
1707 kernfs_put(old_parent);
1708 kfree_const(old_name);
1712 up_write(&root->kernfs_rwsem);
1716 /* Relationship between mode and the DT_xxx types */
1717 static inline unsigned char dt_type(struct kernfs_node *kn)
1719 return (kn->mode >> 12) & 15;
1722 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
1724 kernfs_put(filp->private_data);
1728 static struct kernfs_node *kernfs_dir_pos(const void *ns,
1729 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
1732 int valid = kernfs_active(pos) &&
1733 pos->parent == parent && hash == pos->hash;
1738 if (!pos && (hash > 1) && (hash < INT_MAX)) {
1739 struct rb_node *node = parent->dir.children.rb_node;
1741 pos = rb_to_kn(node);
1743 if (hash < pos->hash)
1744 node = node->rb_left;
1745 else if (hash > pos->hash)
1746 node = node->rb_right;
1751 /* Skip over entries which are dying/dead or in the wrong namespace */
1752 while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
1753 struct rb_node *node = rb_next(&pos->rb);
1757 pos = rb_to_kn(node);
1762 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1763 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1765 pos = kernfs_dir_pos(ns, parent, ino, pos);
1768 struct rb_node *node = rb_next(&pos->rb);
1772 pos = rb_to_kn(node);
1773 } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1778 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1780 struct dentry *dentry = file->f_path.dentry;
1781 struct kernfs_node *parent = kernfs_dentry_node(dentry);
1782 struct kernfs_node *pos = file->private_data;
1783 struct kernfs_root *root;
1784 const void *ns = NULL;
1786 if (!dir_emit_dots(file, ctx))
1789 root = kernfs_root(parent);
1790 down_read(&root->kernfs_rwsem);
1792 if (kernfs_ns_enabled(parent))
1793 ns = kernfs_info(dentry->d_sb)->ns;
1795 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1797 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1798 const char *name = pos->name;
1799 unsigned int type = dt_type(pos);
1800 int len = strlen(name);
1801 ino_t ino = kernfs_ino(pos);
1803 ctx->pos = pos->hash;
1804 file->private_data = pos;
1807 up_read(&root->kernfs_rwsem);
1808 if (!dir_emit(ctx, name, len, ino, type))
1810 down_read(&root->kernfs_rwsem);
1812 up_read(&root->kernfs_rwsem);
1813 file->private_data = NULL;
1818 const struct file_operations kernfs_dir_fops = {
1819 .read = generic_read_dir,
1820 .iterate_shared = kernfs_fop_readdir,
1821 .release = kernfs_dir_fop_release,
1822 .llseek = generic_file_llseek,