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.
12 #include <linux/namei.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/security.h>
16 #include <linux/hash.h>
18 #include "kernfs-internal.h"
20 DEFINE_MUTEX(sysfs_mutex);
22 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
26 * @name: Null terminated string to hash
27 * @ns: Namespace tag to hash
29 * Returns 31 bit hash of ns + name (so it fits in an off_t )
31 static unsigned int sysfs_name_hash(const char *name, const void *ns)
33 unsigned long hash = init_name_hash();
34 unsigned int len = strlen(name);
36 hash = partial_name_hash(*name++, hash);
37 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
39 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
47 static int sysfs_name_compare(unsigned int hash, const char *name,
48 const void *ns, const struct kernfs_node *kn)
51 return hash - kn->hash;
54 return strcmp(name, kn->name);
57 static int sysfs_sd_compare(const struct kernfs_node *left,
58 const struct kernfs_node *right)
60 return sysfs_name_compare(left->hash, left->name, left->ns, right);
64 * sysfs_link_sibling - link kernfs_node into sibling rbtree
65 * @kn: kernfs_node of interest
67 * Link @kn into its sibling rbtree which starts from
68 * @kn->parent->dir.children.
71 * mutex_lock(sysfs_mutex)
74 * 0 on susccess -EEXIST on failure.
76 static int sysfs_link_sibling(struct kernfs_node *kn)
78 struct rb_node **node = &kn->parent->dir.children.rb_node;
79 struct rb_node *parent = NULL;
81 if (sysfs_type(kn) == SYSFS_DIR)
82 kn->parent->dir.subdirs++;
85 struct kernfs_node *pos;
88 pos = rb_to_kn(*node);
90 result = sysfs_sd_compare(kn, pos);
92 node = &pos->rb.rb_left;
94 node = &pos->rb.rb_right;
98 /* add new node and rebalance the tree */
99 rb_link_node(&kn->rb, parent, node);
100 rb_insert_color(&kn->rb, &kn->parent->dir.children);
105 * sysfs_unlink_sibling - unlink kernfs_node from sibling rbtree
106 * @kn: kernfs_node of interest
108 * Unlink @kn from its sibling rbtree which starts from
109 * kn->parent->dir.children.
112 * mutex_lock(sysfs_mutex)
114 static void sysfs_unlink_sibling(struct kernfs_node *kn)
116 if (sysfs_type(kn) == SYSFS_DIR)
117 kn->parent->dir.subdirs--;
119 rb_erase(&kn->rb, &kn->parent->dir.children);
123 * sysfs_get_active - get an active reference to kernfs_node
124 * @kn: kernfs_node to get an active reference to
126 * Get an active reference of @kn. This function is noop if @kn
130 * Pointer to @kn on success, NULL on failure.
132 struct kernfs_node *sysfs_get_active(struct kernfs_node *kn)
137 if (!atomic_inc_unless_negative(&kn->active))
140 if (kn->flags & SYSFS_FLAG_LOCKDEP)
141 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
146 * sysfs_put_active - put an active reference to kernfs_node
147 * @kn: kernfs_node to put an active reference to
149 * Put an active reference to @kn. This function is noop if @kn
152 void sysfs_put_active(struct kernfs_node *kn)
159 if (kn->flags & SYSFS_FLAG_LOCKDEP)
160 rwsem_release(&kn->dep_map, 1, _RET_IP_);
161 v = atomic_dec_return(&kn->active);
162 if (likely(v != SD_DEACTIVATED_BIAS))
166 * atomic_dec_return() is a mb(), we'll always see the updated
169 complete(kn->u.completion);
173 * sysfs_deactivate - deactivate kernfs_node
174 * @kn: kernfs_node to deactivate
176 * Deny new active references and drain existing ones.
178 static void sysfs_deactivate(struct kernfs_node *kn)
180 DECLARE_COMPLETION_ONSTACK(wait);
183 BUG_ON(!(kn->flags & SYSFS_FLAG_REMOVED));
185 if (!(sysfs_type(kn) & SYSFS_ACTIVE_REF))
188 kn->u.completion = (void *)&wait;
190 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
191 /* atomic_add_return() is a mb(), put_active() will always see
192 * the updated kn->u.completion.
194 v = atomic_add_return(SD_DEACTIVATED_BIAS, &kn->active);
196 if (v != SD_DEACTIVATED_BIAS) {
197 lock_contended(&kn->dep_map, _RET_IP_);
198 wait_for_completion(&wait);
201 lock_acquired(&kn->dep_map, _RET_IP_);
202 rwsem_release(&kn->dep_map, 1, _RET_IP_);
206 * kernfs_get - get a reference count on a kernfs_node
207 * @kn: the target kernfs_node
209 void kernfs_get(struct kernfs_node *kn)
212 WARN_ON(!atomic_read(&kn->count));
213 atomic_inc(&kn->count);
216 EXPORT_SYMBOL_GPL(kernfs_get);
219 * kernfs_put - put a reference count on a kernfs_node
220 * @kn: the target kernfs_node
222 * Put a reference count of @kn and destroy it if it reached zero.
224 void kernfs_put(struct kernfs_node *kn)
226 struct kernfs_node *parent;
227 struct kernfs_root *root;
229 if (!kn || !atomic_dec_and_test(&kn->count))
231 root = kernfs_root(kn);
233 /* Moving/renaming is always done while holding reference.
234 * kn->parent won't change beneath us.
238 WARN(!(kn->flags & SYSFS_FLAG_REMOVED),
239 "sysfs: free using entry: %s/%s\n",
240 parent ? parent->name : "", kn->name);
242 if (sysfs_type(kn) == SYSFS_KOBJ_LINK)
243 kernfs_put(kn->symlink.target_kn);
244 if (sysfs_type(kn) & SYSFS_COPY_NAME)
247 if (kn->iattr->ia_secdata)
248 security_release_secctx(kn->iattr->ia_secdata,
249 kn->iattr->ia_secdata_len);
250 simple_xattrs_free(&kn->iattr->xattrs);
253 ida_simple_remove(&root->ino_ida, kn->ino);
254 kmem_cache_free(sysfs_dir_cachep, kn);
258 if (atomic_dec_and_test(&kn->count))
261 /* just released the root kn, free @root too */
262 ida_destroy(&root->ino_ida);
266 EXPORT_SYMBOL_GPL(kernfs_put);
268 static int sysfs_dentry_delete(const struct dentry *dentry)
270 struct kernfs_node *kn = dentry->d_fsdata;
271 return !(kn && !(kn->flags & SYSFS_FLAG_REMOVED));
274 static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
276 struct kernfs_node *kn;
278 if (flags & LOOKUP_RCU)
281 kn = dentry->d_fsdata;
282 mutex_lock(&sysfs_mutex);
284 /* The sysfs dirent has been deleted */
285 if (kn->flags & SYSFS_FLAG_REMOVED)
288 /* The sysfs dirent has been moved? */
289 if (dentry->d_parent->d_fsdata != kn->parent)
292 /* The sysfs dirent has been renamed */
293 if (strcmp(dentry->d_name.name, kn->name) != 0)
296 /* The sysfs dirent has been moved to a different namespace */
297 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
298 kernfs_info(dentry->d_sb)->ns != kn->ns)
301 mutex_unlock(&sysfs_mutex);
305 /* Remove the dentry from the dcache hashes.
306 * If this is a deleted dentry we use d_drop instead of d_delete
307 * so sysfs doesn't need to cope with negative dentries.
309 * If this is a dentry that has simply been renamed we
310 * use d_drop to remove it from the dcache lookup on its
311 * old parent. If this dentry persists later when a lookup
312 * is performed at its new name the dentry will be readded
313 * to the dcache hashes.
315 mutex_unlock(&sysfs_mutex);
317 /* If we have submounts we must allow the vfs caches
318 * to lie about the state of the filesystem to prevent
319 * leaks and other nasty things.
321 if (check_submounts_and_drop(dentry) != 0)
327 static void sysfs_dentry_release(struct dentry *dentry)
329 kernfs_put(dentry->d_fsdata);
332 const struct dentry_operations sysfs_dentry_ops = {
333 .d_revalidate = sysfs_dentry_revalidate,
334 .d_delete = sysfs_dentry_delete,
335 .d_release = sysfs_dentry_release,
338 struct kernfs_node *sysfs_new_dirent(struct kernfs_root *root,
339 const char *name, umode_t mode, int type)
341 char *dup_name = NULL;
342 struct kernfs_node *kn;
345 if (type & SYSFS_COPY_NAME) {
346 name = dup_name = kstrdup(name, GFP_KERNEL);
351 kn = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
355 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
360 atomic_set(&kn->count, 1);
361 atomic_set(&kn->active, 0);
365 kn->flags = type | SYSFS_FLAG_REMOVED;
370 kmem_cache_free(sysfs_dir_cachep, kn);
377 * sysfs_addrm_start - prepare for kernfs_node add/remove
378 * @acxt: pointer to kernfs_addrm_cxt to be used
380 * This function is called when the caller is about to add or remove
381 * kernfs_node. This function acquires sysfs_mutex. @acxt is used to
382 * keep and pass context to other addrm functions.
385 * Kernel thread context (may sleep). sysfs_mutex is locked on
388 void sysfs_addrm_start(struct kernfs_addrm_cxt *acxt)
389 __acquires(sysfs_mutex)
391 memset(acxt, 0, sizeof(*acxt));
393 mutex_lock(&sysfs_mutex);
397 * sysfs_add_one - add kernfs_node to parent without warning
398 * @acxt: addrm context to use
399 * @kn: kernfs_node to be added
400 * @parent: the parent kernfs_node to add @kn to
402 * Get @parent and set @kn->parent to it and increment nlink of the
403 * parent inode if @kn is a directory and link into the children list
406 * This function should be called between calls to
407 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
408 * passed the same @acxt as passed to sysfs_addrm_start().
411 * Determined by sysfs_addrm_start().
414 * 0 on success, -EEXIST if entry with the given name already
417 int sysfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
418 struct kernfs_node *parent)
420 bool has_ns = kernfs_ns_enabled(parent);
421 struct kernfs_iattrs *ps_iattr;
424 if (has_ns != (bool)kn->ns) {
425 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
426 has_ns ? "required" : "invalid", parent->name, kn->name);
430 if (sysfs_type(parent) != SYSFS_DIR)
433 kn->hash = sysfs_name_hash(kn->name, kn->ns);
437 ret = sysfs_link_sibling(kn);
441 /* Update timestamps on the parent */
442 ps_iattr = parent->iattr;
444 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
445 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
448 /* Mark the entry added into directory tree */
449 kn->flags &= ~SYSFS_FLAG_REMOVED;
455 * sysfs_remove_one - remove kernfs_node from parent
456 * @acxt: addrm context to use
457 * @kn: kernfs_node to be removed
459 * Mark @kn removed and drop nlink of parent inode if @kn is a
460 * directory. @kn is unlinked from the children list.
462 * This function should be called between calls to
463 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
464 * passed the same @acxt as passed to sysfs_addrm_start().
467 * Determined by sysfs_addrm_start().
469 static void sysfs_remove_one(struct kernfs_addrm_cxt *acxt,
470 struct kernfs_node *kn)
472 struct kernfs_iattrs *ps_iattr;
475 * Removal can be called multiple times on the same node. Only the
476 * first invocation is effective and puts the base ref.
478 if (kn->flags & SYSFS_FLAG_REMOVED)
482 sysfs_unlink_sibling(kn);
484 /* Update timestamps on the parent */
485 ps_iattr = kn->parent->iattr;
487 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
488 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
492 kn->flags |= SYSFS_FLAG_REMOVED;
493 kn->u.removed_list = acxt->removed;
498 * sysfs_addrm_finish - finish up kernfs_node add/remove
499 * @acxt: addrm context to finish up
501 * Finish up kernfs_node add/remove. Resources acquired by
502 * sysfs_addrm_start() are released and removed kernfs_nodes are
506 * sysfs_mutex is released.
508 void sysfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
509 __releases(sysfs_mutex)
511 /* release resources acquired by sysfs_addrm_start() */
512 mutex_unlock(&sysfs_mutex);
514 /* kill removed kernfs_nodes */
515 while (acxt->removed) {
516 struct kernfs_node *kn = acxt->removed;
518 acxt->removed = kn->u.removed_list;
520 sysfs_deactivate(kn);
521 sysfs_unmap_bin_file(kn);
527 * kernfs_find_ns - find kernfs_node with the given name
528 * @parent: kernfs_node to search under
529 * @name: name to look for
530 * @ns: the namespace tag to use
532 * Look for kernfs_node with name @name under @parent. Returns pointer to
533 * the found kernfs_node on success, %NULL on failure.
535 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
536 const unsigned char *name,
539 struct rb_node *node = parent->dir.children.rb_node;
540 bool has_ns = kernfs_ns_enabled(parent);
543 lockdep_assert_held(&sysfs_mutex);
545 if (has_ns != (bool)ns) {
546 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
547 has_ns ? "required" : "invalid", parent->name, name);
551 hash = sysfs_name_hash(name, ns);
553 struct kernfs_node *kn;
557 result = sysfs_name_compare(hash, name, ns, kn);
559 node = node->rb_left;
561 node = node->rb_right;
569 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
570 * @parent: kernfs_node to search under
571 * @name: name to look for
572 * @ns: the namespace tag to use
574 * Look for kernfs_node with name @name under @parent and get a reference
575 * if found. This function may sleep and returns pointer to the found
576 * kernfs_node on success, %NULL on failure.
578 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
579 const char *name, const void *ns)
581 struct kernfs_node *kn;
583 mutex_lock(&sysfs_mutex);
584 kn = kernfs_find_ns(parent, name, ns);
586 mutex_unlock(&sysfs_mutex);
590 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
593 * kernfs_create_root - create a new kernfs hierarchy
594 * @priv: opaque data associated with the new directory
596 * Returns the root of the new hierarchy on success, ERR_PTR() value on
599 struct kernfs_root *kernfs_create_root(void *priv)
601 struct kernfs_root *root;
602 struct kernfs_node *kn;
604 root = kzalloc(sizeof(*root), GFP_KERNEL);
606 return ERR_PTR(-ENOMEM);
608 ida_init(&root->ino_ida);
610 kn = sysfs_new_dirent(root, "", S_IFDIR | S_IRUGO | S_IXUGO, SYSFS_DIR);
612 ida_destroy(&root->ino_ida);
614 return ERR_PTR(-ENOMEM);
617 kn->flags &= ~SYSFS_FLAG_REMOVED;
627 * kernfs_destroy_root - destroy a kernfs hierarchy
628 * @root: root of the hierarchy to destroy
630 * Destroy the hierarchy anchored at @root by removing all existing
631 * directories and destroying @root.
633 void kernfs_destroy_root(struct kernfs_root *root)
635 kernfs_remove(root->kn); /* will also free @root */
639 * kernfs_create_dir_ns - create a directory
640 * @parent: parent in which to create a new directory
641 * @name: name of the new directory
642 * @priv: opaque data associated with the new directory
643 * @ns: optional namespace tag of the directory
645 * Returns the created node on success, ERR_PTR() value on failure.
647 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
648 const char *name, void *priv,
651 umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
652 struct kernfs_addrm_cxt acxt;
653 struct kernfs_node *kn;
657 kn = sysfs_new_dirent(kernfs_root(parent), name, mode, SYSFS_DIR);
659 return ERR_PTR(-ENOMEM);
661 kn->dir.root = parent->dir.root;
666 sysfs_addrm_start(&acxt);
667 rc = sysfs_add_one(&acxt, kn, parent);
668 sysfs_addrm_finish(&acxt);
677 static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
680 struct dentry *ret = NULL;
681 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
682 struct kernfs_node *kn;
684 const void *ns = NULL;
686 mutex_lock(&sysfs_mutex);
688 if (kernfs_ns_enabled(parent))
689 ns = kernfs_info(dir->i_sb)->ns;
691 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
695 ret = ERR_PTR(-ENOENT);
699 dentry->d_fsdata = kn;
701 /* attach dentry and inode */
702 inode = sysfs_get_inode(dir->i_sb, kn);
704 ret = ERR_PTR(-ENOMEM);
708 /* instantiate and hash dentry */
709 ret = d_materialise_unique(dentry, inode);
711 mutex_unlock(&sysfs_mutex);
715 const struct inode_operations sysfs_dir_inode_operations = {
716 .lookup = sysfs_lookup,
717 .permission = sysfs_permission,
718 .setattr = sysfs_setattr,
719 .getattr = sysfs_getattr,
720 .setxattr = sysfs_setxattr,
721 .removexattr = sysfs_removexattr,
722 .getxattr = sysfs_getxattr,
723 .listxattr = sysfs_listxattr,
726 static struct kernfs_node *sysfs_leftmost_descendant(struct kernfs_node *pos)
728 struct kernfs_node *last;
735 if (sysfs_type(pos) != SYSFS_DIR)
738 rbn = rb_first(&pos->dir.children);
749 * sysfs_next_descendant_post - find the next descendant for post-order walk
750 * @pos: the current position (%NULL to initiate traversal)
751 * @root: kernfs_node whose descendants to walk
753 * Find the next descendant to visit for post-order traversal of @root's
754 * descendants. @root is included in the iteration and the last node to be
757 static struct kernfs_node *sysfs_next_descendant_post(struct kernfs_node *pos,
758 struct kernfs_node *root)
762 lockdep_assert_held(&sysfs_mutex);
764 /* if first iteration, visit leftmost descendant which may be root */
766 return sysfs_leftmost_descendant(root);
768 /* if we visited @root, we're done */
772 /* if there's an unvisited sibling, visit its leftmost descendant */
773 rbn = rb_next(&pos->rb);
775 return sysfs_leftmost_descendant(rb_to_kn(rbn));
777 /* no sibling left, visit parent */
781 static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
782 struct kernfs_node *kn)
784 struct kernfs_node *pos, *next;
789 pr_debug("sysfs %s: removing\n", kn->name);
794 next = sysfs_next_descendant_post(pos, kn);
796 sysfs_remove_one(acxt, pos);
801 * kernfs_remove - remove a kernfs_node recursively
802 * @kn: the kernfs_node to remove
804 * Remove @kn along with all its subdirectories and files.
806 void kernfs_remove(struct kernfs_node *kn)
808 struct kernfs_addrm_cxt acxt;
810 sysfs_addrm_start(&acxt);
811 __kernfs_remove(&acxt, kn);
812 sysfs_addrm_finish(&acxt);
816 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
817 * @parent: parent of the target
818 * @name: name of the kernfs_node to remove
819 * @ns: namespace tag of the kernfs_node to remove
821 * Look for the kernfs_node with @name and @ns under @parent and remove it.
822 * Returns 0 on success, -ENOENT if such entry doesn't exist.
824 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
827 struct kernfs_addrm_cxt acxt;
828 struct kernfs_node *kn;
831 WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
836 sysfs_addrm_start(&acxt);
838 kn = kernfs_find_ns(parent, name, ns);
840 __kernfs_remove(&acxt, kn);
842 sysfs_addrm_finish(&acxt);
851 * kernfs_rename_ns - move and rename a kernfs_node
853 * @new_parent: new parent to put @sd under
854 * @new_name: new name
855 * @new_ns: new namespace tag
857 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
858 const char *new_name, const void *new_ns)
862 mutex_lock(&sysfs_mutex);
865 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
866 (strcmp(kn->name, new_name) == 0))
867 goto out; /* nothing to rename */
870 if (kernfs_find_ns(new_parent, new_name, new_ns))
873 /* rename kernfs_node */
874 if (strcmp(kn->name, new_name) != 0) {
876 new_name = kstrdup(new_name, GFP_KERNEL);
885 * Move to the appropriate place in the appropriate directories rbtree.
887 sysfs_unlink_sibling(kn);
888 kernfs_get(new_parent);
889 kernfs_put(kn->parent);
891 kn->hash = sysfs_name_hash(kn->name, kn->ns);
892 kn->parent = new_parent;
893 sysfs_link_sibling(kn);
897 mutex_unlock(&sysfs_mutex);
901 /* Relationship between s_mode and the DT_xxx types */
902 static inline unsigned char dt_type(struct kernfs_node *kn)
904 return (kn->mode >> 12) & 15;
907 static int sysfs_dir_release(struct inode *inode, struct file *filp)
909 kernfs_put(filp->private_data);
913 static struct kernfs_node *sysfs_dir_pos(const void *ns,
914 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
917 int valid = !(pos->flags & SYSFS_FLAG_REMOVED) &&
918 pos->parent == parent && hash == pos->hash;
923 if (!pos && (hash > 1) && (hash < INT_MAX)) {
924 struct rb_node *node = parent->dir.children.rb_node;
926 pos = rb_to_kn(node);
928 if (hash < pos->hash)
929 node = node->rb_left;
930 else if (hash > pos->hash)
931 node = node->rb_right;
936 /* Skip over entries in the wrong namespace */
937 while (pos && pos->ns != ns) {
938 struct rb_node *node = rb_next(&pos->rb);
942 pos = rb_to_kn(node);
947 static struct kernfs_node *sysfs_dir_next_pos(const void *ns,
948 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
950 pos = sysfs_dir_pos(ns, parent, ino, pos);
953 struct rb_node *node = rb_next(&pos->rb);
957 pos = rb_to_kn(node);
958 } while (pos && pos->ns != ns);
962 static int sysfs_readdir(struct file *file, struct dir_context *ctx)
964 struct dentry *dentry = file->f_path.dentry;
965 struct kernfs_node *parent = dentry->d_fsdata;
966 struct kernfs_node *pos = file->private_data;
967 const void *ns = NULL;
969 if (!dir_emit_dots(file, ctx))
971 mutex_lock(&sysfs_mutex);
973 if (kernfs_ns_enabled(parent))
974 ns = kernfs_info(dentry->d_sb)->ns;
976 for (pos = sysfs_dir_pos(ns, parent, ctx->pos, pos);
978 pos = sysfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
979 const char *name = pos->name;
980 unsigned int type = dt_type(pos);
981 int len = strlen(name);
982 ino_t ino = pos->ino;
984 ctx->pos = pos->hash;
985 file->private_data = pos;
988 mutex_unlock(&sysfs_mutex);
989 if (!dir_emit(ctx, name, len, ino, type))
991 mutex_lock(&sysfs_mutex);
993 mutex_unlock(&sysfs_mutex);
994 file->private_data = NULL;
999 static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1001 struct inode *inode = file_inode(file);
1004 mutex_lock(&inode->i_mutex);
1005 ret = generic_file_llseek(file, offset, whence);
1006 mutex_unlock(&inode->i_mutex);
1011 const struct file_operations sysfs_dir_operations = {
1012 .read = generic_read_dir,
1013 .iterate = sysfs_readdir,
1014 .release = sysfs_dir_release,
1015 .llseek = sysfs_dir_llseek,