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 if (kn
->flags
& KERNFS_LOCKDEP
)
191 rwsem_acquire(&kn
->dep_map
, 0, 0, _RET_IP_
);
192 /* atomic_add_return() is a mb(), put_active() will always see
193 * the updated kn->u.completion.
195 v
= atomic_add_return(KN_DEACTIVATED_BIAS
, &kn
->active
);
197 if (v
!= KN_DEACTIVATED_BIAS
) {
198 if (kn
->flags
& KERNFS_LOCKDEP
)
199 lock_contended(&kn
->dep_map
, _RET_IP_
);
200 wait_for_completion(&wait
);
203 if (kn
->flags
& KERNFS_LOCKDEP
) {
204 lock_acquired(&kn
->dep_map
, _RET_IP_
);
205 rwsem_release(&kn
->dep_map
, 1, _RET_IP_
);
210 * kernfs_get - get a reference count on a kernfs_node
211 * @kn: the target kernfs_node
213 void kernfs_get(struct kernfs_node
*kn
)
216 WARN_ON(!atomic_read(&kn
->count
));
217 atomic_inc(&kn
->count
);
220 EXPORT_SYMBOL_GPL(kernfs_get
);
223 * kernfs_put - put a reference count on a kernfs_node
224 * @kn: the target kernfs_node
226 * Put a reference count of @kn and destroy it if it reached zero.
228 void kernfs_put(struct kernfs_node
*kn
)
230 struct kernfs_node
*parent
;
231 struct kernfs_root
*root
;
233 if (!kn
|| !atomic_dec_and_test(&kn
->count
))
235 root
= kernfs_root(kn
);
237 /* Moving/renaming is always done while holding reference.
238 * kn->parent won't change beneath us.
242 WARN(!(kn
->flags
& KERNFS_REMOVED
), "kernfs: free using entry: %s/%s\n",
243 parent
? parent
->name
: "", kn
->name
);
245 if (kernfs_type(kn
) == KERNFS_LINK
)
246 kernfs_put(kn
->symlink
.target_kn
);
247 if (!(kn
->flags
& KERNFS_STATIC_NAME
))
250 if (kn
->iattr
->ia_secdata
)
251 security_release_secctx(kn
->iattr
->ia_secdata
,
252 kn
->iattr
->ia_secdata_len
);
253 simple_xattrs_free(&kn
->iattr
->xattrs
);
256 ida_simple_remove(&root
->ino_ida
, kn
->ino
);
257 kmem_cache_free(kernfs_node_cache
, kn
);
261 if (atomic_dec_and_test(&kn
->count
))
264 /* just released the root kn, free @root too */
265 ida_destroy(&root
->ino_ida
);
269 EXPORT_SYMBOL_GPL(kernfs_put
);
271 static int kernfs_dop_revalidate(struct dentry
*dentry
, unsigned int flags
)
273 struct kernfs_node
*kn
;
275 if (flags
& LOOKUP_RCU
)
278 /* Always perform fresh lookup for negatives */
279 if (!dentry
->d_inode
)
280 goto out_bad_unlocked
;
282 kn
= dentry
->d_fsdata
;
283 mutex_lock(&kernfs_mutex
);
285 /* The kernfs node has been deleted */
286 if (kn
->flags
& KERNFS_REMOVED
)
289 /* The kernfs node has been moved? */
290 if (dentry
->d_parent
->d_fsdata
!= kn
->parent
)
293 /* The kernfs node has been renamed */
294 if (strcmp(dentry
->d_name
.name
, kn
->name
) != 0)
297 /* The kernfs node has been moved to a different namespace */
298 if (kn
->parent
&& kernfs_ns_enabled(kn
->parent
) &&
299 kernfs_info(dentry
->d_sb
)->ns
!= kn
->ns
)
302 mutex_unlock(&kernfs_mutex
);
306 mutex_unlock(&kernfs_mutex
);
309 * @dentry doesn't match the underlying kernfs node, drop the
310 * dentry and force lookup. If we have submounts we must allow the
311 * vfs caches to lie about the state of the filesystem to prevent
312 * leaks and other nasty things, so use check_submounts_and_drop()
313 * instead of d_drop().
315 if (check_submounts_and_drop(dentry
) != 0)
321 static void kernfs_dop_release(struct dentry
*dentry
)
323 kernfs_put(dentry
->d_fsdata
);
326 const struct dentry_operations kernfs_dops
= {
327 .d_revalidate
= kernfs_dop_revalidate
,
328 .d_release
= kernfs_dop_release
,
331 static struct kernfs_node
*__kernfs_new_node(struct kernfs_root
*root
,
332 const char *name
, umode_t mode
,
335 char *dup_name
= NULL
;
336 struct kernfs_node
*kn
;
339 if (!(flags
& KERNFS_STATIC_NAME
)) {
340 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
345 kn
= kmem_cache_zalloc(kernfs_node_cache
, GFP_KERNEL
);
349 ret
= ida_simple_get(&root
->ino_ida
, 1, 0, GFP_KERNEL
);
354 atomic_set(&kn
->count
, 1);
355 atomic_set(&kn
->active
, 0);
359 kn
->flags
= flags
| KERNFS_REMOVED
;
364 kmem_cache_free(kernfs_node_cache
, kn
);
370 struct kernfs_node
*kernfs_new_node(struct kernfs_node
*parent
,
371 const char *name
, umode_t mode
,
374 struct kernfs_node
*kn
;
376 kn
= __kernfs_new_node(kernfs_root(parent
), name
, mode
, flags
);
385 * kernfs_addrm_start - prepare for kernfs_node add/remove
386 * @acxt: pointer to kernfs_addrm_cxt to be used
388 * This function is called when the caller is about to add or remove
389 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
390 * to keep and pass context to other addrm functions.
393 * Kernel thread context (may sleep). kernfs_mutex is locked on
396 void kernfs_addrm_start(struct kernfs_addrm_cxt
*acxt
)
397 __acquires(kernfs_mutex
)
399 memset(acxt
, 0, sizeof(*acxt
));
401 mutex_lock(&kernfs_mutex
);
405 * kernfs_add_one - add kernfs_node to parent without warning
406 * @acxt: addrm context to use
407 * @kn: kernfs_node to be added
409 * The caller must already have initialized @kn->parent. This
410 * function increments nlink of the parent's inode if @kn is a
411 * directory and link into the children list of the parent.
413 * This function should be called between calls to
414 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
415 * the same @acxt as passed to kernfs_addrm_start().
418 * Determined by kernfs_addrm_start().
421 * 0 on success, -EEXIST if entry with the given name already
424 int kernfs_add_one(struct kernfs_addrm_cxt
*acxt
, struct kernfs_node
*kn
)
426 struct kernfs_node
*parent
= kn
->parent
;
427 bool has_ns
= kernfs_ns_enabled(parent
);
428 struct kernfs_iattrs
*ps_iattr
;
431 if (has_ns
!= (bool)kn
->ns
) {
432 WARN(1, KERN_WARNING
"kernfs: ns %s in '%s' for '%s'\n",
433 has_ns
? "required" : "invalid", parent
->name
, kn
->name
);
437 if (kernfs_type(parent
) != KERNFS_DIR
)
440 if (parent
->flags
& KERNFS_REMOVED
)
443 kn
->hash
= kernfs_name_hash(kn
->name
, kn
->ns
);
445 ret
= kernfs_link_sibling(kn
);
449 /* Update timestamps on the parent */
450 ps_iattr
= parent
->iattr
;
452 struct iattr
*ps_iattrs
= &ps_iattr
->ia_iattr
;
453 ps_iattrs
->ia_ctime
= ps_iattrs
->ia_mtime
= CURRENT_TIME
;
456 /* Mark the entry added into directory tree */
457 kn
->flags
&= ~KERNFS_REMOVED
;
463 * kernfs_remove_one - remove kernfs_node from parent
464 * @acxt: addrm context to use
465 * @kn: kernfs_node to be removed
467 * Mark @kn removed and drop nlink of parent inode if @kn is a
468 * directory. @kn is unlinked from the children list.
470 * This function should be called between calls to
471 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
472 * passed the same @acxt as passed to kernfs_addrm_start().
475 * Determined by kernfs_addrm_start().
477 static void kernfs_remove_one(struct kernfs_addrm_cxt
*acxt
,
478 struct kernfs_node
*kn
)
480 struct kernfs_iattrs
*ps_iattr
;
483 * Removal can be called multiple times on the same node. Only the
484 * first invocation is effective and puts the base ref.
486 if (kn
->flags
& KERNFS_REMOVED
)
490 kernfs_unlink_sibling(kn
);
492 /* Update timestamps on the parent */
493 ps_iattr
= kn
->parent
->iattr
;
495 ps_iattr
->ia_iattr
.ia_ctime
= CURRENT_TIME
;
496 ps_iattr
->ia_iattr
.ia_mtime
= CURRENT_TIME
;
500 kn
->flags
|= KERNFS_REMOVED
;
501 kn
->u
.removed_list
= acxt
->removed
;
506 * kernfs_addrm_finish - finish up kernfs_node add/remove
507 * @acxt: addrm context to finish up
509 * Finish up kernfs_node add/remove. Resources acquired by
510 * kernfs_addrm_start() are released and removed kernfs_nodes are
514 * kernfs_mutex is released.
516 void kernfs_addrm_finish(struct kernfs_addrm_cxt
*acxt
)
517 __releases(kernfs_mutex
)
519 /* release resources acquired by kernfs_addrm_start() */
520 mutex_unlock(&kernfs_mutex
);
522 /* kill removed kernfs_nodes */
523 while (acxt
->removed
) {
524 struct kernfs_node
*kn
= acxt
->removed
;
526 acxt
->removed
= kn
->u
.removed_list
;
528 kernfs_deactivate(kn
);
529 kernfs_unmap_bin_file(kn
);
535 * kernfs_find_ns - find kernfs_node with the given name
536 * @parent: kernfs_node to search under
537 * @name: name to look for
538 * @ns: the namespace tag to use
540 * Look for kernfs_node with name @name under @parent. Returns pointer to
541 * the found kernfs_node on success, %NULL on failure.
543 static struct kernfs_node
*kernfs_find_ns(struct kernfs_node
*parent
,
544 const unsigned char *name
,
547 struct rb_node
*node
= parent
->dir
.children
.rb_node
;
548 bool has_ns
= kernfs_ns_enabled(parent
);
551 lockdep_assert_held(&kernfs_mutex
);
553 if (has_ns
!= (bool)ns
) {
554 WARN(1, KERN_WARNING
"kernfs: ns %s in '%s' for '%s'\n",
555 has_ns
? "required" : "invalid", parent
->name
, name
);
559 hash
= kernfs_name_hash(name
, ns
);
561 struct kernfs_node
*kn
;
565 result
= kernfs_name_compare(hash
, name
, ns
, kn
);
567 node
= node
->rb_left
;
569 node
= node
->rb_right
;
577 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
578 * @parent: kernfs_node to search under
579 * @name: name to look for
580 * @ns: the namespace tag to use
582 * Look for kernfs_node with name @name under @parent and get a reference
583 * if found. This function may sleep and returns pointer to the found
584 * kernfs_node on success, %NULL on failure.
586 struct kernfs_node
*kernfs_find_and_get_ns(struct kernfs_node
*parent
,
587 const char *name
, const void *ns
)
589 struct kernfs_node
*kn
;
591 mutex_lock(&kernfs_mutex
);
592 kn
= kernfs_find_ns(parent
, name
, ns
);
594 mutex_unlock(&kernfs_mutex
);
598 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns
);
601 * kernfs_create_root - create a new kernfs hierarchy
602 * @kdops: optional directory syscall operations for the hierarchy
603 * @priv: opaque data associated with the new directory
605 * Returns the root of the new hierarchy on success, ERR_PTR() value on
608 struct kernfs_root
*kernfs_create_root(struct kernfs_dir_ops
*kdops
, void *priv
)
610 struct kernfs_root
*root
;
611 struct kernfs_node
*kn
;
613 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
615 return ERR_PTR(-ENOMEM
);
617 ida_init(&root
->ino_ida
);
619 kn
= __kernfs_new_node(root
, "", S_IFDIR
| S_IRUGO
| S_IXUGO
,
622 ida_destroy(&root
->ino_ida
);
624 return ERR_PTR(-ENOMEM
);
627 kn
->flags
&= ~KERNFS_REMOVED
;
631 root
->dir_ops
= kdops
;
638 * kernfs_destroy_root - destroy a kernfs hierarchy
639 * @root: root of the hierarchy to destroy
641 * Destroy the hierarchy anchored at @root by removing all existing
642 * directories and destroying @root.
644 void kernfs_destroy_root(struct kernfs_root
*root
)
646 kernfs_remove(root
->kn
); /* will also free @root */
650 * kernfs_create_dir_ns - create a directory
651 * @parent: parent in which to create a new directory
652 * @name: name of the new directory
653 * @mode: mode of the new directory
654 * @priv: opaque data associated with the new directory
655 * @ns: optional namespace tag of the directory
657 * Returns the created node on success, ERR_PTR() value on failure.
659 struct kernfs_node
*kernfs_create_dir_ns(struct kernfs_node
*parent
,
660 const char *name
, umode_t mode
,
661 void *priv
, const void *ns
)
663 struct kernfs_addrm_cxt acxt
;
664 struct kernfs_node
*kn
;
668 kn
= kernfs_new_node(parent
, name
, mode
| S_IFDIR
, KERNFS_DIR
);
670 return ERR_PTR(-ENOMEM
);
672 kn
->dir
.root
= parent
->dir
.root
;
677 kernfs_addrm_start(&acxt
);
678 rc
= kernfs_add_one(&acxt
, kn
);
679 kernfs_addrm_finish(&acxt
);
688 static struct dentry
*kernfs_iop_lookup(struct inode
*dir
,
689 struct dentry
*dentry
,
693 struct kernfs_node
*parent
= dentry
->d_parent
->d_fsdata
;
694 struct kernfs_node
*kn
;
696 const void *ns
= NULL
;
698 mutex_lock(&kernfs_mutex
);
700 if (kernfs_ns_enabled(parent
))
701 ns
= kernfs_info(dir
->i_sb
)->ns
;
703 kn
= kernfs_find_ns(parent
, dentry
->d_name
.name
, ns
);
711 dentry
->d_fsdata
= kn
;
713 /* attach dentry and inode */
714 inode
= kernfs_get_inode(dir
->i_sb
, kn
);
716 ret
= ERR_PTR(-ENOMEM
);
720 /* instantiate and hash dentry */
721 ret
= d_materialise_unique(dentry
, inode
);
723 mutex_unlock(&kernfs_mutex
);
727 static int kernfs_iop_mkdir(struct inode
*dir
, struct dentry
*dentry
,
730 struct kernfs_node
*parent
= dir
->i_private
;
731 struct kernfs_dir_ops
*kdops
= kernfs_root(parent
)->dir_ops
;
733 if (!kdops
|| !kdops
->mkdir
)
736 return kdops
->mkdir(parent
, dentry
->d_name
.name
, mode
);
739 static int kernfs_iop_rmdir(struct inode
*dir
, struct dentry
*dentry
)
741 struct kernfs_node
*kn
= dentry
->d_fsdata
;
742 struct kernfs_dir_ops
*kdops
= kernfs_root(kn
)->dir_ops
;
744 if (!kdops
|| !kdops
->rmdir
)
747 return kdops
->rmdir(kn
);
750 static int kernfs_iop_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
751 struct inode
*new_dir
, struct dentry
*new_dentry
)
753 struct kernfs_node
*kn
= old_dentry
->d_fsdata
;
754 struct kernfs_node
*new_parent
= new_dir
->i_private
;
755 struct kernfs_dir_ops
*kdops
= kernfs_root(kn
)->dir_ops
;
757 if (!kdops
|| !kdops
->rename
)
760 return kdops
->rename(kn
, new_parent
, new_dentry
->d_name
.name
);
763 const struct inode_operations kernfs_dir_iops
= {
764 .lookup
= kernfs_iop_lookup
,
765 .permission
= kernfs_iop_permission
,
766 .setattr
= kernfs_iop_setattr
,
767 .getattr
= kernfs_iop_getattr
,
768 .setxattr
= kernfs_iop_setxattr
,
769 .removexattr
= kernfs_iop_removexattr
,
770 .getxattr
= kernfs_iop_getxattr
,
771 .listxattr
= kernfs_iop_listxattr
,
773 .mkdir
= kernfs_iop_mkdir
,
774 .rmdir
= kernfs_iop_rmdir
,
775 .rename
= kernfs_iop_rename
,
778 static struct kernfs_node
*kernfs_leftmost_descendant(struct kernfs_node
*pos
)
780 struct kernfs_node
*last
;
787 if (kernfs_type(pos
) != KERNFS_DIR
)
790 rbn
= rb_first(&pos
->dir
.children
);
801 * kernfs_next_descendant_post - find the next descendant for post-order walk
802 * @pos: the current position (%NULL to initiate traversal)
803 * @root: kernfs_node whose descendants to walk
805 * Find the next descendant to visit for post-order traversal of @root's
806 * descendants. @root is included in the iteration and the last node to be
809 static struct kernfs_node
*kernfs_next_descendant_post(struct kernfs_node
*pos
,
810 struct kernfs_node
*root
)
814 lockdep_assert_held(&kernfs_mutex
);
816 /* if first iteration, visit leftmost descendant which may be root */
818 return kernfs_leftmost_descendant(root
);
820 /* if we visited @root, we're done */
824 /* if there's an unvisited sibling, visit its leftmost descendant */
825 rbn
= rb_next(&pos
->rb
);
827 return kernfs_leftmost_descendant(rb_to_kn(rbn
));
829 /* no sibling left, visit parent */
833 static void __kernfs_remove(struct kernfs_addrm_cxt
*acxt
,
834 struct kernfs_node
*kn
)
836 struct kernfs_node
*pos
, *next
;
841 pr_debug("kernfs %s: removing\n", kn
->name
);
846 next
= kernfs_next_descendant_post(pos
, kn
);
848 kernfs_remove_one(acxt
, pos
);
853 * kernfs_remove - remove a kernfs_node recursively
854 * @kn: the kernfs_node to remove
856 * Remove @kn along with all its subdirectories and files.
858 void kernfs_remove(struct kernfs_node
*kn
)
860 struct kernfs_addrm_cxt acxt
;
862 kernfs_addrm_start(&acxt
);
863 __kernfs_remove(&acxt
, kn
);
864 kernfs_addrm_finish(&acxt
);
868 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
869 * @parent: parent of the target
870 * @name: name of the kernfs_node to remove
871 * @ns: namespace tag of the kernfs_node to remove
873 * Look for the kernfs_node with @name and @ns under @parent and remove it.
874 * Returns 0 on success, -ENOENT if such entry doesn't exist.
876 int kernfs_remove_by_name_ns(struct kernfs_node
*parent
, const char *name
,
879 struct kernfs_addrm_cxt acxt
;
880 struct kernfs_node
*kn
;
883 WARN(1, KERN_WARNING
"kernfs: can not remove '%s', no directory\n",
888 kernfs_addrm_start(&acxt
);
890 kn
= kernfs_find_ns(parent
, name
, ns
);
892 __kernfs_remove(&acxt
, kn
);
894 kernfs_addrm_finish(&acxt
);
903 * kernfs_rename_ns - move and rename a kernfs_node
905 * @new_parent: new parent to put @sd under
906 * @new_name: new name
907 * @new_ns: new namespace tag
909 int kernfs_rename_ns(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
910 const char *new_name
, const void *new_ns
)
914 mutex_lock(&kernfs_mutex
);
917 if ((kn
->flags
| new_parent
->flags
) & KERNFS_REMOVED
)
921 if ((kn
->parent
== new_parent
) && (kn
->ns
== new_ns
) &&
922 (strcmp(kn
->name
, new_name
) == 0))
923 goto out
; /* nothing to rename */
926 if (kernfs_find_ns(new_parent
, new_name
, new_ns
))
929 /* rename kernfs_node */
930 if (strcmp(kn
->name
, new_name
) != 0) {
932 new_name
= kstrdup(new_name
, GFP_KERNEL
);
936 if (kn
->flags
& KERNFS_STATIC_NAME
)
937 kn
->flags
&= ~KERNFS_STATIC_NAME
;
945 * Move to the appropriate place in the appropriate directories rbtree.
947 kernfs_unlink_sibling(kn
);
948 kernfs_get(new_parent
);
949 kernfs_put(kn
->parent
);
951 kn
->hash
= kernfs_name_hash(kn
->name
, kn
->ns
);
952 kn
->parent
= new_parent
;
953 kernfs_link_sibling(kn
);
957 mutex_unlock(&kernfs_mutex
);
961 /* Relationship between s_mode and the DT_xxx types */
962 static inline unsigned char dt_type(struct kernfs_node
*kn
)
964 return (kn
->mode
>> 12) & 15;
967 static int kernfs_dir_fop_release(struct inode
*inode
, struct file
*filp
)
969 kernfs_put(filp
->private_data
);
973 static struct kernfs_node
*kernfs_dir_pos(const void *ns
,
974 struct kernfs_node
*parent
, loff_t hash
, struct kernfs_node
*pos
)
977 int valid
= !(pos
->flags
& KERNFS_REMOVED
) &&
978 pos
->parent
== parent
&& hash
== pos
->hash
;
983 if (!pos
&& (hash
> 1) && (hash
< INT_MAX
)) {
984 struct rb_node
*node
= parent
->dir
.children
.rb_node
;
986 pos
= rb_to_kn(node
);
988 if (hash
< pos
->hash
)
989 node
= node
->rb_left
;
990 else if (hash
> pos
->hash
)
991 node
= node
->rb_right
;
996 /* Skip over entries in the wrong namespace */
997 while (pos
&& pos
->ns
!= ns
) {
998 struct rb_node
*node
= rb_next(&pos
->rb
);
1002 pos
= rb_to_kn(node
);
1007 static struct kernfs_node
*kernfs_dir_next_pos(const void *ns
,
1008 struct kernfs_node
*parent
, ino_t ino
, struct kernfs_node
*pos
)
1010 pos
= kernfs_dir_pos(ns
, parent
, ino
, pos
);
1013 struct rb_node
*node
= rb_next(&pos
->rb
);
1017 pos
= rb_to_kn(node
);
1018 } while (pos
&& pos
->ns
!= ns
);
1022 static int kernfs_fop_readdir(struct file
*file
, struct dir_context
*ctx
)
1024 struct dentry
*dentry
= file
->f_path
.dentry
;
1025 struct kernfs_node
*parent
= dentry
->d_fsdata
;
1026 struct kernfs_node
*pos
= file
->private_data
;
1027 const void *ns
= NULL
;
1029 if (!dir_emit_dots(file
, ctx
))
1031 mutex_lock(&kernfs_mutex
);
1033 if (kernfs_ns_enabled(parent
))
1034 ns
= kernfs_info(dentry
->d_sb
)->ns
;
1036 for (pos
= kernfs_dir_pos(ns
, parent
, ctx
->pos
, pos
);
1038 pos
= kernfs_dir_next_pos(ns
, parent
, ctx
->pos
, pos
)) {
1039 const char *name
= pos
->name
;
1040 unsigned int type
= dt_type(pos
);
1041 int len
= strlen(name
);
1042 ino_t ino
= pos
->ino
;
1044 ctx
->pos
= pos
->hash
;
1045 file
->private_data
= pos
;
1048 mutex_unlock(&kernfs_mutex
);
1049 if (!dir_emit(ctx
, name
, len
, ino
, type
))
1051 mutex_lock(&kernfs_mutex
);
1053 mutex_unlock(&kernfs_mutex
);
1054 file
->private_data
= NULL
;
1059 static loff_t
kernfs_dir_fop_llseek(struct file
*file
, loff_t offset
,
1062 struct inode
*inode
= file_inode(file
);
1065 mutex_lock(&inode
->i_mutex
);
1066 ret
= generic_file_llseek(file
, offset
, whence
);
1067 mutex_unlock(&inode
->i_mutex
);
1072 const struct file_operations kernfs_dir_fops
= {
1073 .read
= generic_read_dir
,
1074 .iterate
= kernfs_fop_readdir
,
1075 .release
= kernfs_dir_fop_release
,
1076 .llseek
= kernfs_dir_fop_llseek
,