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(kernfs_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 kernfs_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 kernfs_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 kernfs_sd_compare(const struct kernfs_node
*left
,
58 const struct kernfs_node
*right
)
60 return kernfs_name_compare(left
->hash
, left
->name
, left
->ns
, right
);
64 * kernfs_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(kernfs_mutex)
74 * 0 on susccess -EEXIST on failure.
76 static int kernfs_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 (kernfs_type(kn
) == KERNFS_DIR
)
82 kn
->parent
->dir
.subdirs
++;
85 struct kernfs_node
*pos
;
88 pos
= rb_to_kn(*node
);
90 result
= kernfs_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 * kernfs_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(kernfs_mutex)
114 static void kernfs_unlink_sibling(struct kernfs_node
*kn
)
116 if (kernfs_type(kn
) == KERNFS_DIR
)
117 kn
->parent
->dir
.subdirs
--;
119 rb_erase(&kn
->rb
, &kn
->parent
->dir
.children
);
123 * kernfs_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
*kernfs_get_active(struct kernfs_node
*kn
)
137 if (!atomic_inc_unless_negative(&kn
->active
))
140 if (kn
->flags
& KERNFS_LOCKDEP
)
141 rwsem_acquire_read(&kn
->dep_map
, 0, 1, _RET_IP_
);
146 * kernfs_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 kernfs_put_active(struct kernfs_node
*kn
)
159 if (kn
->flags
& KERNFS_LOCKDEP
)
160 rwsem_release(&kn
->dep_map
, 1, _RET_IP_
);
161 v
= atomic_dec_return(&kn
->active
);
162 if (likely(v
!= KN_DEACTIVATED_BIAS
))
166 * atomic_dec_return() is a mb(), we'll always see the updated
169 complete(kn
->u
.completion
);
173 * kernfs_deactivate - deactivate kernfs_node
174 * @kn: kernfs_node to deactivate
176 * Deny new active references and drain existing ones.
178 static void kernfs_deactivate(struct kernfs_node
*kn
)
180 DECLARE_COMPLETION_ONSTACK(wait
);
183 BUG_ON(!(kn
->flags
& KERNFS_REMOVED
));
185 if (!(kernfs_type(kn
) & KERNFS_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(KN_DEACTIVATED_BIAS
, &kn
->active
);
196 if (v
!= KN_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
& KERNFS_REMOVED
), "kernfs: free using entry: %s/%s\n",
239 parent
? parent
->name
: "", kn
->name
);
241 if (kernfs_type(kn
) == KERNFS_LINK
)
242 kernfs_put(kn
->symlink
.target_kn
);
243 if (kernfs_type(kn
) & KERNFS_COPY_NAME
)
246 if (kn
->iattr
->ia_secdata
)
247 security_release_secctx(kn
->iattr
->ia_secdata
,
248 kn
->iattr
->ia_secdata_len
);
249 simple_xattrs_free(&kn
->iattr
->xattrs
);
252 ida_simple_remove(&root
->ino_ida
, kn
->ino
);
253 kmem_cache_free(kernfs_node_cache
, kn
);
257 if (atomic_dec_and_test(&kn
->count
))
260 /* just released the root kn, free @root too */
261 ida_destroy(&root
->ino_ida
);
265 EXPORT_SYMBOL_GPL(kernfs_put
);
267 static int kernfs_dop_delete(const struct dentry
*dentry
)
269 struct kernfs_node
*kn
= dentry
->d_fsdata
;
270 return !(kn
&& !(kn
->flags
& KERNFS_REMOVED
));
273 static int kernfs_dop_revalidate(struct dentry
*dentry
, unsigned int flags
)
275 struct kernfs_node
*kn
;
277 if (flags
& LOOKUP_RCU
)
280 kn
= dentry
->d_fsdata
;
281 mutex_lock(&kernfs_mutex
);
283 /* The kernfs node has been deleted */
284 if (kn
->flags
& KERNFS_REMOVED
)
287 /* The kernfs node has been moved? */
288 if (dentry
->d_parent
->d_fsdata
!= kn
->parent
)
291 /* The kernfs node has been renamed */
292 if (strcmp(dentry
->d_name
.name
, kn
->name
) != 0)
295 /* The kernfs node has been moved to a different namespace */
296 if (kn
->parent
&& kernfs_ns_enabled(kn
->parent
) &&
297 kernfs_info(dentry
->d_sb
)->ns
!= kn
->ns
)
300 mutex_unlock(&kernfs_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 kernfs 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(&kernfs_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 kernfs_dop_release(struct dentry
*dentry
)
329 kernfs_put(dentry
->d_fsdata
);
332 const struct dentry_operations kernfs_dops
= {
333 .d_revalidate
= kernfs_dop_revalidate
,
334 .d_delete
= kernfs_dop_delete
,
335 .d_release
= kernfs_dop_release
,
338 struct kernfs_node
*kernfs_new_node(struct kernfs_root
*root
, const char *name
,
339 umode_t mode
, int type
)
341 char *dup_name
= NULL
;
342 struct kernfs_node
*kn
;
345 if (type
& KERNFS_COPY_NAME
) {
346 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
351 kn
= kmem_cache_zalloc(kernfs_node_cache
, 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
| KERNFS_REMOVED
;
370 kmem_cache_free(kernfs_node_cache
, kn
);
377 * kernfs_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 kernfs_mutex. @acxt is used
382 * to keep and pass context to other addrm functions.
385 * Kernel thread context (may sleep). kernfs_mutex is locked on
388 void kernfs_addrm_start(struct kernfs_addrm_cxt
*acxt
)
389 __acquires(kernfs_mutex
)
391 memset(acxt
, 0, sizeof(*acxt
));
393 mutex_lock(&kernfs_mutex
);
397 * kernfs_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 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
408 * the same @acxt as passed to kernfs_addrm_start().
411 * Determined by kernfs_addrm_start().
414 * 0 on success, -EEXIST if entry with the given name already
417 int kernfs_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
"kernfs: ns %s in '%s' for '%s'\n",
426 has_ns
? "required" : "invalid", parent
->name
, kn
->name
);
430 if (kernfs_type(parent
) != KERNFS_DIR
)
433 if (parent
->flags
& KERNFS_REMOVED
)
436 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
;
458 * kernfs_remove_one - remove kernfs_node from parent
459 * @acxt: addrm context to use
460 * @kn: kernfs_node to be removed
462 * Mark @kn removed and drop nlink of parent inode if @kn is a
463 * directory. @kn is unlinked from the children list.
465 * This function should be called between calls to
466 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
467 * passed the same @acxt as passed to kernfs_addrm_start().
470 * Determined by kernfs_addrm_start().
472 static void kernfs_remove_one(struct kernfs_addrm_cxt
*acxt
,
473 struct kernfs_node
*kn
)
475 struct kernfs_iattrs
*ps_iattr
;
478 * Removal can be called multiple times on the same node. Only the
479 * first invocation is effective and puts the base ref.
481 if (kn
->flags
& KERNFS_REMOVED
)
485 kernfs_unlink_sibling(kn
);
487 /* Update timestamps on the parent */
488 ps_iattr
= kn
->parent
->iattr
;
490 ps_iattr
->ia_iattr
.ia_ctime
= CURRENT_TIME
;
491 ps_iattr
->ia_iattr
.ia_mtime
= CURRENT_TIME
;
495 kn
->flags
|= KERNFS_REMOVED
;
496 kn
->u
.removed_list
= acxt
->removed
;
501 * kernfs_addrm_finish - finish up kernfs_node add/remove
502 * @acxt: addrm context to finish up
504 * Finish up kernfs_node add/remove. Resources acquired by
505 * kernfs_addrm_start() are released and removed kernfs_nodes are
509 * kernfs_mutex is released.
511 void kernfs_addrm_finish(struct kernfs_addrm_cxt
*acxt
)
512 __releases(kernfs_mutex
)
514 /* release resources acquired by kernfs_addrm_start() */
515 mutex_unlock(&kernfs_mutex
);
517 /* kill removed kernfs_nodes */
518 while (acxt
->removed
) {
519 struct kernfs_node
*kn
= acxt
->removed
;
521 acxt
->removed
= kn
->u
.removed_list
;
523 kernfs_deactivate(kn
);
524 kernfs_unmap_bin_file(kn
);
530 * kernfs_find_ns - find kernfs_node with the given name
531 * @parent: kernfs_node to search under
532 * @name: name to look for
533 * @ns: the namespace tag to use
535 * Look for kernfs_node with name @name under @parent. Returns pointer to
536 * the found kernfs_node on success, %NULL on failure.
538 static struct kernfs_node
*kernfs_find_ns(struct kernfs_node
*parent
,
539 const unsigned char *name
,
542 struct rb_node
*node
= parent
->dir
.children
.rb_node
;
543 bool has_ns
= kernfs_ns_enabled(parent
);
546 lockdep_assert_held(&kernfs_mutex
);
548 if (has_ns
!= (bool)ns
) {
549 WARN(1, KERN_WARNING
"kernfs: ns %s in '%s' for '%s'\n",
550 has_ns
? "required" : "invalid", parent
->name
, name
);
554 hash
= kernfs_name_hash(name
, ns
);
556 struct kernfs_node
*kn
;
560 result
= kernfs_name_compare(hash
, name
, ns
, kn
);
562 node
= node
->rb_left
;
564 node
= node
->rb_right
;
572 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
573 * @parent: kernfs_node to search under
574 * @name: name to look for
575 * @ns: the namespace tag to use
577 * Look for kernfs_node with name @name under @parent and get a reference
578 * if found. This function may sleep and returns pointer to the found
579 * kernfs_node on success, %NULL on failure.
581 struct kernfs_node
*kernfs_find_and_get_ns(struct kernfs_node
*parent
,
582 const char *name
, const void *ns
)
584 struct kernfs_node
*kn
;
586 mutex_lock(&kernfs_mutex
);
587 kn
= kernfs_find_ns(parent
, name
, ns
);
589 mutex_unlock(&kernfs_mutex
);
593 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns
);
596 * kernfs_create_root - create a new kernfs hierarchy
597 * @priv: opaque data associated with the new directory
599 * Returns the root of the new hierarchy on success, ERR_PTR() value on
602 struct kernfs_root
*kernfs_create_root(void *priv
)
604 struct kernfs_root
*root
;
605 struct kernfs_node
*kn
;
607 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
609 return ERR_PTR(-ENOMEM
);
611 ida_init(&root
->ino_ida
);
613 kn
= kernfs_new_node(root
, "", S_IFDIR
| S_IRUGO
| S_IXUGO
, KERNFS_DIR
);
615 ida_destroy(&root
->ino_ida
);
617 return ERR_PTR(-ENOMEM
);
620 kn
->flags
&= ~KERNFS_REMOVED
;
630 * kernfs_destroy_root - destroy a kernfs hierarchy
631 * @root: root of the hierarchy to destroy
633 * Destroy the hierarchy anchored at @root by removing all existing
634 * directories and destroying @root.
636 void kernfs_destroy_root(struct kernfs_root
*root
)
638 kernfs_remove(root
->kn
); /* will also free @root */
642 * kernfs_create_dir_ns - create a directory
643 * @parent: parent in which to create a new directory
644 * @name: name of the new directory
645 * @mode: mode of the new directory
646 * @priv: opaque data associated with the new directory
647 * @ns: optional namespace tag of the directory
649 * Returns the created node on success, ERR_PTR() value on failure.
651 struct kernfs_node
*kernfs_create_dir_ns(struct kernfs_node
*parent
,
652 const char *name
, umode_t mode
,
653 void *priv
, const void *ns
)
655 struct kernfs_addrm_cxt acxt
;
656 struct kernfs_node
*kn
;
660 kn
= kernfs_new_node(kernfs_root(parent
), name
, mode
| S_IFDIR
,
663 return ERR_PTR(-ENOMEM
);
665 kn
->dir
.root
= parent
->dir
.root
;
670 kernfs_addrm_start(&acxt
);
671 rc
= kernfs_add_one(&acxt
, kn
, parent
);
672 kernfs_addrm_finish(&acxt
);
681 static struct dentry
*kernfs_iop_lookup(struct inode
*dir
,
682 struct dentry
*dentry
,
685 struct dentry
*ret
= NULL
;
686 struct kernfs_node
*parent
= dentry
->d_parent
->d_fsdata
;
687 struct kernfs_node
*kn
;
689 const void *ns
= NULL
;
691 mutex_lock(&kernfs_mutex
);
693 if (kernfs_ns_enabled(parent
))
694 ns
= kernfs_info(dir
->i_sb
)->ns
;
696 kn
= kernfs_find_ns(parent
, dentry
->d_name
.name
, ns
);
700 ret
= ERR_PTR(-ENOENT
);
704 dentry
->d_fsdata
= kn
;
706 /* attach dentry and inode */
707 inode
= kernfs_get_inode(dir
->i_sb
, kn
);
709 ret
= ERR_PTR(-ENOMEM
);
713 /* instantiate and hash dentry */
714 ret
= d_materialise_unique(dentry
, inode
);
716 mutex_unlock(&kernfs_mutex
);
720 const struct inode_operations kernfs_dir_iops
= {
721 .lookup
= kernfs_iop_lookup
,
722 .permission
= kernfs_iop_permission
,
723 .setattr
= kernfs_iop_setattr
,
724 .getattr
= kernfs_iop_getattr
,
725 .setxattr
= kernfs_iop_setxattr
,
726 .removexattr
= kernfs_iop_removexattr
,
727 .getxattr
= kernfs_iop_getxattr
,
728 .listxattr
= kernfs_iop_listxattr
,
731 static struct kernfs_node
*kernfs_leftmost_descendant(struct kernfs_node
*pos
)
733 struct kernfs_node
*last
;
740 if (kernfs_type(pos
) != KERNFS_DIR
)
743 rbn
= rb_first(&pos
->dir
.children
);
754 * kernfs_next_descendant_post - find the next descendant for post-order walk
755 * @pos: the current position (%NULL to initiate traversal)
756 * @root: kernfs_node whose descendants to walk
758 * Find the next descendant to visit for post-order traversal of @root's
759 * descendants. @root is included in the iteration and the last node to be
762 static struct kernfs_node
*kernfs_next_descendant_post(struct kernfs_node
*pos
,
763 struct kernfs_node
*root
)
767 lockdep_assert_held(&kernfs_mutex
);
769 /* if first iteration, visit leftmost descendant which may be root */
771 return kernfs_leftmost_descendant(root
);
773 /* if we visited @root, we're done */
777 /* if there's an unvisited sibling, visit its leftmost descendant */
778 rbn
= rb_next(&pos
->rb
);
780 return kernfs_leftmost_descendant(rb_to_kn(rbn
));
782 /* no sibling left, visit parent */
786 static void __kernfs_remove(struct kernfs_addrm_cxt
*acxt
,
787 struct kernfs_node
*kn
)
789 struct kernfs_node
*pos
, *next
;
794 pr_debug("kernfs %s: removing\n", kn
->name
);
799 next
= kernfs_next_descendant_post(pos
, kn
);
801 kernfs_remove_one(acxt
, pos
);
806 * kernfs_remove - remove a kernfs_node recursively
807 * @kn: the kernfs_node to remove
809 * Remove @kn along with all its subdirectories and files.
811 void kernfs_remove(struct kernfs_node
*kn
)
813 struct kernfs_addrm_cxt acxt
;
815 kernfs_addrm_start(&acxt
);
816 __kernfs_remove(&acxt
, kn
);
817 kernfs_addrm_finish(&acxt
);
821 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
822 * @parent: parent of the target
823 * @name: name of the kernfs_node to remove
824 * @ns: namespace tag of the kernfs_node to remove
826 * Look for the kernfs_node with @name and @ns under @parent and remove it.
827 * Returns 0 on success, -ENOENT if such entry doesn't exist.
829 int kernfs_remove_by_name_ns(struct kernfs_node
*parent
, const char *name
,
832 struct kernfs_addrm_cxt acxt
;
833 struct kernfs_node
*kn
;
836 WARN(1, KERN_WARNING
"kernfs: can not remove '%s', no directory\n",
841 kernfs_addrm_start(&acxt
);
843 kn
= kernfs_find_ns(parent
, name
, ns
);
845 __kernfs_remove(&acxt
, kn
);
847 kernfs_addrm_finish(&acxt
);
856 * kernfs_rename_ns - move and rename a kernfs_node
858 * @new_parent: new parent to put @sd under
859 * @new_name: new name
860 * @new_ns: new namespace tag
862 int kernfs_rename_ns(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
863 const char *new_name
, const void *new_ns
)
867 mutex_lock(&kernfs_mutex
);
870 if ((kn
->flags
| new_parent
->flags
) & KERNFS_REMOVED
)
874 if ((kn
->parent
== new_parent
) && (kn
->ns
== new_ns
) &&
875 (strcmp(kn
->name
, new_name
) == 0))
876 goto out
; /* nothing to rename */
879 if (kernfs_find_ns(new_parent
, new_name
, new_ns
))
882 /* rename kernfs_node */
883 if (strcmp(kn
->name
, new_name
) != 0) {
885 new_name
= kstrdup(new_name
, GFP_KERNEL
);
894 * Move to the appropriate place in the appropriate directories rbtree.
896 kernfs_unlink_sibling(kn
);
897 kernfs_get(new_parent
);
898 kernfs_put(kn
->parent
);
900 kn
->hash
= kernfs_name_hash(kn
->name
, kn
->ns
);
901 kn
->parent
= new_parent
;
902 kernfs_link_sibling(kn
);
906 mutex_unlock(&kernfs_mutex
);
910 /* Relationship between s_mode and the DT_xxx types */
911 static inline unsigned char dt_type(struct kernfs_node
*kn
)
913 return (kn
->mode
>> 12) & 15;
916 static int kernfs_dir_fop_release(struct inode
*inode
, struct file
*filp
)
918 kernfs_put(filp
->private_data
);
922 static struct kernfs_node
*kernfs_dir_pos(const void *ns
,
923 struct kernfs_node
*parent
, loff_t hash
, struct kernfs_node
*pos
)
926 int valid
= !(pos
->flags
& KERNFS_REMOVED
) &&
927 pos
->parent
== parent
&& hash
== pos
->hash
;
932 if (!pos
&& (hash
> 1) && (hash
< INT_MAX
)) {
933 struct rb_node
*node
= parent
->dir
.children
.rb_node
;
935 pos
= rb_to_kn(node
);
937 if (hash
< pos
->hash
)
938 node
= node
->rb_left
;
939 else if (hash
> pos
->hash
)
940 node
= node
->rb_right
;
945 /* Skip over entries in the wrong namespace */
946 while (pos
&& pos
->ns
!= ns
) {
947 struct rb_node
*node
= rb_next(&pos
->rb
);
951 pos
= rb_to_kn(node
);
956 static struct kernfs_node
*kernfs_dir_next_pos(const void *ns
,
957 struct kernfs_node
*parent
, ino_t ino
, struct kernfs_node
*pos
)
959 pos
= kernfs_dir_pos(ns
, parent
, ino
, pos
);
962 struct rb_node
*node
= rb_next(&pos
->rb
);
966 pos
= rb_to_kn(node
);
967 } while (pos
&& pos
->ns
!= ns
);
971 static int kernfs_fop_readdir(struct file
*file
, struct dir_context
*ctx
)
973 struct dentry
*dentry
= file
->f_path
.dentry
;
974 struct kernfs_node
*parent
= dentry
->d_fsdata
;
975 struct kernfs_node
*pos
= file
->private_data
;
976 const void *ns
= NULL
;
978 if (!dir_emit_dots(file
, ctx
))
980 mutex_lock(&kernfs_mutex
);
982 if (kernfs_ns_enabled(parent
))
983 ns
= kernfs_info(dentry
->d_sb
)->ns
;
985 for (pos
= kernfs_dir_pos(ns
, parent
, ctx
->pos
, pos
);
987 pos
= kernfs_dir_next_pos(ns
, parent
, ctx
->pos
, pos
)) {
988 const char *name
= pos
->name
;
989 unsigned int type
= dt_type(pos
);
990 int len
= strlen(name
);
991 ino_t ino
= pos
->ino
;
993 ctx
->pos
= pos
->hash
;
994 file
->private_data
= pos
;
997 mutex_unlock(&kernfs_mutex
);
998 if (!dir_emit(ctx
, name
, len
, ino
, type
))
1000 mutex_lock(&kernfs_mutex
);
1002 mutex_unlock(&kernfs_mutex
);
1003 file
->private_data
= NULL
;
1008 static loff_t
kernfs_dir_fop_llseek(struct file
*file
, loff_t offset
,
1011 struct inode
*inode
= file_inode(file
);
1014 mutex_lock(&inode
->i_mutex
);
1015 ret
= generic_file_llseek(file
, offset
, whence
);
1016 mutex_unlock(&inode
->i_mutex
);
1021 const struct file_operations kernfs_dir_fops
= {
1022 .read
= generic_read_dir
,
1023 .iterate
= kernfs_fop_readdir
,
1024 .release
= kernfs_dir_fop_release
,
1025 .llseek
= kernfs_dir_fop_llseek
,