2 * fs/kernfs/dir.c - kernfs directory implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8 * This file is released under the GPLv2.
11 #include <linux/sched.h>
13 #include <linux/namei.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/security.h>
17 #include <linux/hash.h>
19 #include "kernfs-internal.h"
21 DEFINE_MUTEX(kernfs_mutex
);
23 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
25 static bool kernfs_lockdep(struct kernfs_node
*kn
)
27 #ifdef CONFIG_DEBUG_LOCK_ALLOC
28 return kn
->flags
& KERNFS_LOCKDEP
;
36 * @name: Null terminated string to hash
37 * @ns: Namespace tag to hash
39 * Returns 31 bit hash of ns + name (so it fits in an off_t )
41 static unsigned int kernfs_name_hash(const char *name
, const void *ns
)
43 unsigned long hash
= init_name_hash();
44 unsigned int len
= strlen(name
);
46 hash
= partial_name_hash(*name
++, hash
);
47 hash
= (end_name_hash(hash
) ^ hash_ptr((void *)ns
, 31));
49 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
57 static int kernfs_name_compare(unsigned int hash
, const char *name
,
58 const void *ns
, const struct kernfs_node
*kn
)
61 return hash
- kn
->hash
;
64 return strcmp(name
, kn
->name
);
67 static int kernfs_sd_compare(const struct kernfs_node
*left
,
68 const struct kernfs_node
*right
)
70 return kernfs_name_compare(left
->hash
, left
->name
, left
->ns
, right
);
74 * kernfs_link_sibling - link kernfs_node into sibling rbtree
75 * @kn: kernfs_node of interest
77 * Link @kn into its sibling rbtree which starts from
78 * @kn->parent->dir.children.
81 * mutex_lock(kernfs_mutex)
84 * 0 on susccess -EEXIST on failure.
86 static int kernfs_link_sibling(struct kernfs_node
*kn
)
88 struct rb_node
**node
= &kn
->parent
->dir
.children
.rb_node
;
89 struct rb_node
*parent
= NULL
;
91 if (kernfs_type(kn
) == KERNFS_DIR
)
92 kn
->parent
->dir
.subdirs
++;
95 struct kernfs_node
*pos
;
98 pos
= rb_to_kn(*node
);
100 result
= kernfs_sd_compare(kn
, pos
);
102 node
= &pos
->rb
.rb_left
;
104 node
= &pos
->rb
.rb_right
;
108 /* add new node and rebalance the tree */
109 rb_link_node(&kn
->rb
, parent
, node
);
110 rb_insert_color(&kn
->rb
, &kn
->parent
->dir
.children
);
115 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
116 * @kn: kernfs_node of interest
118 * Unlink @kn from its sibling rbtree which starts from
119 * kn->parent->dir.children.
122 * mutex_lock(kernfs_mutex)
124 static bool kernfs_unlink_sibling(struct kernfs_node
*kn
)
126 if (RB_EMPTY_NODE(&kn
->rb
))
129 if (kernfs_type(kn
) == KERNFS_DIR
)
130 kn
->parent
->dir
.subdirs
--;
132 rb_erase(&kn
->rb
, &kn
->parent
->dir
.children
);
133 RB_CLEAR_NODE(&kn
->rb
);
138 * kernfs_get_active - get an active reference to kernfs_node
139 * @kn: kernfs_node to get an active reference to
141 * Get an active reference of @kn. This function is noop if @kn
145 * Pointer to @kn on success, NULL on failure.
147 struct kernfs_node
*kernfs_get_active(struct kernfs_node
*kn
)
152 if (kernfs_lockdep(kn
))
153 rwsem_acquire_read(&kn
->dep_map
, 0, 1, _RET_IP_
);
156 * Try to obtain an active ref. If @kn is deactivated, we block
157 * till either it's reactivated or killed.
160 if (atomic_inc_unless_negative(&kn
->active
))
163 wait_event(kernfs_root(kn
)->deactivate_waitq
,
164 atomic_read(&kn
->active
) >= 0 ||
165 RB_EMPTY_NODE(&kn
->rb
));
166 } while (!RB_EMPTY_NODE(&kn
->rb
));
168 if (kernfs_lockdep(kn
))
169 rwsem_release(&kn
->dep_map
, 1, _RET_IP_
);
174 * kernfs_put_active - put an active reference to kernfs_node
175 * @kn: kernfs_node to put an active reference to
177 * Put an active reference to @kn. This function is noop if @kn
180 void kernfs_put_active(struct kernfs_node
*kn
)
182 struct kernfs_root
*root
= kernfs_root(kn
);
188 if (kernfs_lockdep(kn
))
189 rwsem_release(&kn
->dep_map
, 1, _RET_IP_
);
190 v
= atomic_dec_return(&kn
->active
);
191 if (likely(v
!= KN_DEACTIVATED_BIAS
))
194 wake_up_all(&root
->deactivate_waitq
);
198 * kernfs_drain - drain kernfs_node
199 * @kn: kernfs_node to drain
201 * Drain existing usages of @kn. Mutiple removers may invoke this function
202 * concurrently on @kn and all will return after draining is complete.
203 * Returns %true if drain is performed and kernfs_mutex was temporarily
204 * released. %false if @kn was already drained and no operation was
207 * The caller is responsible for ensuring @kn stays pinned while this
208 * function is in progress even if it gets removed by someone else.
210 static bool kernfs_drain(struct kernfs_node
*kn
)
211 __releases(&kernfs_mutex
) __acquires(&kernfs_mutex
)
213 struct kernfs_root
*root
= kernfs_root(kn
);
215 lockdep_assert_held(&kernfs_mutex
);
216 WARN_ON_ONCE(atomic_read(&kn
->active
) >= 0);
219 * We want to go through the active ref lockdep annotation at least
220 * once for all node removals, but the lockdep annotation can't be
221 * nested inside kernfs_mutex and deactivation can't make forward
222 * progress if we keep dropping the mutex. Use JUST_ACTIVATED to
223 * force the slow path once for each deactivation if lockdep is
226 if ((!kernfs_lockdep(kn
) || !(kn
->flags
& KERNFS_JUST_DEACTIVATED
)) &&
227 atomic_read(&kn
->active
) == KN_DEACTIVATED_BIAS
)
230 kn
->flags
&= ~KERNFS_JUST_DEACTIVATED
;
231 mutex_unlock(&kernfs_mutex
);
233 if (kernfs_lockdep(kn
)) {
234 rwsem_acquire(&kn
->dep_map
, 0, 0, _RET_IP_
);
235 if (atomic_read(&kn
->active
) != KN_DEACTIVATED_BIAS
)
236 lock_contended(&kn
->dep_map
, _RET_IP_
);
239 wait_event(root
->deactivate_waitq
,
240 atomic_read(&kn
->active
) == KN_DEACTIVATED_BIAS
);
242 if (kernfs_lockdep(kn
)) {
243 lock_acquired(&kn
->dep_map
, _RET_IP_
);
244 rwsem_release(&kn
->dep_map
, 1, _RET_IP_
);
247 mutex_lock(&kernfs_mutex
);
252 * kernfs_get - get a reference count on a kernfs_node
253 * @kn: the target kernfs_node
255 void kernfs_get(struct kernfs_node
*kn
)
258 WARN_ON(!atomic_read(&kn
->count
));
259 atomic_inc(&kn
->count
);
262 EXPORT_SYMBOL_GPL(kernfs_get
);
265 * kernfs_put - put a reference count on a kernfs_node
266 * @kn: the target kernfs_node
268 * Put a reference count of @kn and destroy it if it reached zero.
270 void kernfs_put(struct kernfs_node
*kn
)
272 struct kernfs_node
*parent
;
273 struct kernfs_root
*root
;
275 if (!kn
|| !atomic_dec_and_test(&kn
->count
))
277 root
= kernfs_root(kn
);
280 * Moving/renaming is always done while holding reference.
281 * kn->parent won't change beneath us.
285 WARN_ONCE(atomic_read(&kn
->active
) != KN_DEACTIVATED_BIAS
,
286 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
287 parent
? parent
->name
: "", kn
->name
, atomic_read(&kn
->active
));
289 if (kernfs_type(kn
) == KERNFS_LINK
)
290 kernfs_put(kn
->symlink
.target_kn
);
291 if (!(kn
->flags
& KERNFS_STATIC_NAME
))
294 if (kn
->iattr
->ia_secdata
)
295 security_release_secctx(kn
->iattr
->ia_secdata
,
296 kn
->iattr
->ia_secdata_len
);
297 simple_xattrs_free(&kn
->iattr
->xattrs
);
300 ida_simple_remove(&root
->ino_ida
, kn
->ino
);
301 kmem_cache_free(kernfs_node_cache
, kn
);
305 if (atomic_dec_and_test(&kn
->count
))
308 /* just released the root kn, free @root too */
309 ida_destroy(&root
->ino_ida
);
313 EXPORT_SYMBOL_GPL(kernfs_put
);
315 static int kernfs_dop_revalidate(struct dentry
*dentry
, unsigned int flags
)
317 struct kernfs_node
*kn
;
319 if (flags
& LOOKUP_RCU
)
322 /* Always perform fresh lookup for negatives */
323 if (!dentry
->d_inode
)
324 goto out_bad_unlocked
;
326 kn
= dentry
->d_fsdata
;
327 mutex_lock(&kernfs_mutex
);
329 /* Force fresh lookup if removed */
330 if (kn
->parent
&& RB_EMPTY_NODE(&kn
->rb
))
333 /* The kernfs node has been moved? */
334 if (dentry
->d_parent
->d_fsdata
!= kn
->parent
)
337 /* The kernfs node has been renamed */
338 if (strcmp(dentry
->d_name
.name
, kn
->name
) != 0)
341 /* The kernfs node has been moved to a different namespace */
342 if (kn
->parent
&& kernfs_ns_enabled(kn
->parent
) &&
343 kernfs_info(dentry
->d_sb
)->ns
!= kn
->ns
)
346 mutex_unlock(&kernfs_mutex
);
350 mutex_unlock(&kernfs_mutex
);
353 * @dentry doesn't match the underlying kernfs node, drop the
354 * dentry and force lookup. If we have submounts we must allow the
355 * vfs caches to lie about the state of the filesystem to prevent
356 * leaks and other nasty things, so use check_submounts_and_drop()
357 * instead of d_drop().
359 if (check_submounts_and_drop(dentry
) != 0)
365 static void kernfs_dop_release(struct dentry
*dentry
)
367 kernfs_put(dentry
->d_fsdata
);
370 const struct dentry_operations kernfs_dops
= {
371 .d_revalidate
= kernfs_dop_revalidate
,
372 .d_release
= kernfs_dop_release
,
375 struct kernfs_node
*kernfs_new_node(struct kernfs_root
*root
, const char *name
,
376 umode_t mode
, unsigned flags
)
378 char *dup_name
= NULL
;
379 struct kernfs_node
*kn
;
382 if (!(flags
& KERNFS_STATIC_NAME
)) {
383 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
388 kn
= kmem_cache_zalloc(kernfs_node_cache
, GFP_KERNEL
);
392 ret
= ida_simple_get(&root
->ino_ida
, 1, 0, GFP_KERNEL
);
397 atomic_set(&kn
->count
, 1);
398 atomic_set(&kn
->active
, KN_DEACTIVATED_BIAS
);
399 RB_CLEAR_NODE(&kn
->rb
);
408 kmem_cache_free(kernfs_node_cache
, kn
);
415 * kernfs_add_one - add kernfs_node to parent without warning
416 * @kn: kernfs_node to be added
417 * @parent: the parent kernfs_node to add @kn to
419 * Get @parent and set @kn->parent to it and increment nlink of the
420 * parent inode if @kn is a directory and link into the children list
424 * 0 on success, -EEXIST if entry with the given name already
427 int kernfs_add_one(struct kernfs_node
*kn
, struct kernfs_node
*parent
)
429 struct kernfs_iattrs
*ps_iattr
;
433 if (!kernfs_get_active(parent
))
436 mutex_lock(&kernfs_mutex
);
439 has_ns
= kernfs_ns_enabled(parent
);
440 if (WARN(has_ns
!= (bool)kn
->ns
, KERN_WARNING
"kernfs: ns %s in '%s' for '%s'\n",
441 has_ns
? "required" : "invalid", parent
->name
, kn
->name
))
444 if (kernfs_type(parent
) != KERNFS_DIR
)
447 kn
->hash
= kernfs_name_hash(kn
->name
, kn
->ns
);
451 ret
= kernfs_link_sibling(kn
);
455 /* Update timestamps on the parent */
456 ps_iattr
= parent
->iattr
;
458 struct iattr
*ps_iattrs
= &ps_iattr
->ia_iattr
;
459 ps_iattrs
->ia_ctime
= ps_iattrs
->ia_mtime
= CURRENT_TIME
;
462 /* Mark the entry added into directory tree */
463 atomic_sub(KN_DEACTIVATED_BIAS
, &kn
->active
);
466 mutex_unlock(&kernfs_mutex
);
467 kernfs_put_active(parent
);
472 * kernfs_find_ns - find kernfs_node with the given name
473 * @parent: kernfs_node to search under
474 * @name: name to look for
475 * @ns: the namespace tag to use
477 * Look for kernfs_node with name @name under @parent. Returns pointer to
478 * the found kernfs_node on success, %NULL on failure.
480 static struct kernfs_node
*kernfs_find_ns(struct kernfs_node
*parent
,
481 const unsigned char *name
,
484 struct rb_node
*node
= parent
->dir
.children
.rb_node
;
485 bool has_ns
= kernfs_ns_enabled(parent
);
488 lockdep_assert_held(&kernfs_mutex
);
490 if (has_ns
!= (bool)ns
) {
491 WARN(1, KERN_WARNING
"kernfs: ns %s in '%s' for '%s'\n",
492 has_ns
? "required" : "invalid", parent
->name
, name
);
496 hash
= kernfs_name_hash(name
, ns
);
498 struct kernfs_node
*kn
;
502 result
= kernfs_name_compare(hash
, name
, ns
, kn
);
504 node
= node
->rb_left
;
506 node
= node
->rb_right
;
514 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
515 * @parent: kernfs_node to search under
516 * @name: name to look for
517 * @ns: the namespace tag to use
519 * Look for kernfs_node with name @name under @parent and get a reference
520 * if found. This function may sleep and returns pointer to the found
521 * kernfs_node on success, %NULL on failure.
523 struct kernfs_node
*kernfs_find_and_get_ns(struct kernfs_node
*parent
,
524 const char *name
, const void *ns
)
526 struct kernfs_node
*kn
;
528 mutex_lock(&kernfs_mutex
);
529 kn
= kernfs_find_ns(parent
, name
, ns
);
531 mutex_unlock(&kernfs_mutex
);
535 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns
);
538 * kernfs_create_root - create a new kernfs hierarchy
539 * @kdops: optional directory syscall operations for the hierarchy
540 * @priv: opaque data associated with the new directory
542 * Returns the root of the new hierarchy on success, ERR_PTR() value on
545 struct kernfs_root
*kernfs_create_root(struct kernfs_dir_ops
*kdops
, void *priv
)
547 struct kernfs_root
*root
;
548 struct kernfs_node
*kn
;
550 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
552 return ERR_PTR(-ENOMEM
);
554 ida_init(&root
->ino_ida
);
556 kn
= kernfs_new_node(root
, "", S_IFDIR
| S_IRUGO
| S_IXUGO
, KERNFS_DIR
);
558 ida_destroy(&root
->ino_ida
);
560 return ERR_PTR(-ENOMEM
);
563 atomic_sub(KN_DEACTIVATED_BIAS
, &kn
->active
);
567 root
->dir_ops
= kdops
;
569 init_waitqueue_head(&root
->deactivate_waitq
);
575 * kernfs_destroy_root - destroy a kernfs hierarchy
576 * @root: root of the hierarchy to destroy
578 * Destroy the hierarchy anchored at @root by removing all existing
579 * directories and destroying @root.
581 void kernfs_destroy_root(struct kernfs_root
*root
)
583 kernfs_remove(root
->kn
); /* will also free @root */
587 * kernfs_create_dir_ns - create a directory
588 * @parent: parent in which to create a new directory
589 * @name: name of the new directory
590 * @mode: mode of the new directory
591 * @priv: opaque data associated with the new directory
592 * @ns: optional namespace tag of the directory
594 * Returns the created node on success, ERR_PTR() value on failure.
596 struct kernfs_node
*kernfs_create_dir_ns(struct kernfs_node
*parent
,
597 const char *name
, umode_t mode
,
598 void *priv
, const void *ns
)
600 struct kernfs_node
*kn
;
604 kn
= kernfs_new_node(kernfs_root(parent
), name
, mode
| S_IFDIR
,
607 return ERR_PTR(-ENOMEM
);
609 kn
->dir
.root
= parent
->dir
.root
;
614 rc
= kernfs_add_one(kn
, parent
);
622 static struct dentry
*kernfs_iop_lookup(struct inode
*dir
,
623 struct dentry
*dentry
,
627 struct kernfs_node
*parent
= dentry
->d_parent
->d_fsdata
;
628 struct kernfs_node
*kn
;
630 const void *ns
= NULL
;
632 mutex_lock(&kernfs_mutex
);
634 if (kernfs_ns_enabled(parent
))
635 ns
= kernfs_info(dir
->i_sb
)->ns
;
637 kn
= kernfs_find_ns(parent
, dentry
->d_name
.name
, ns
);
645 dentry
->d_fsdata
= kn
;
647 /* attach dentry and inode */
648 inode
= kernfs_get_inode(dir
->i_sb
, kn
);
650 ret
= ERR_PTR(-ENOMEM
);
654 /* instantiate and hash dentry */
655 ret
= d_materialise_unique(dentry
, inode
);
657 mutex_unlock(&kernfs_mutex
);
661 static int kernfs_iop_mkdir(struct inode
*dir
, struct dentry
*dentry
,
664 struct kernfs_node
*parent
= dir
->i_private
;
665 struct kernfs_dir_ops
*kdops
= kernfs_root(parent
)->dir_ops
;
667 if (!kdops
|| !kdops
->mkdir
)
670 return kdops
->mkdir(parent
, dentry
->d_name
.name
, mode
);
673 static int kernfs_iop_rmdir(struct inode
*dir
, struct dentry
*dentry
)
675 struct kernfs_node
*kn
= dentry
->d_fsdata
;
676 struct kernfs_dir_ops
*kdops
= kernfs_root(kn
)->dir_ops
;
678 if (!kdops
|| !kdops
->rmdir
)
681 return kdops
->rmdir(kn
);
684 static int kernfs_iop_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
685 struct inode
*new_dir
, struct dentry
*new_dentry
)
687 struct kernfs_node
*kn
= old_dentry
->d_fsdata
;
688 struct kernfs_node
*new_parent
= new_dir
->i_private
;
689 struct kernfs_dir_ops
*kdops
= kernfs_root(kn
)->dir_ops
;
691 if (!kdops
|| !kdops
->rename
)
694 return kdops
->rename(kn
, new_parent
, new_dentry
->d_name
.name
);
697 const struct inode_operations kernfs_dir_iops
= {
698 .lookup
= kernfs_iop_lookup
,
699 .permission
= kernfs_iop_permission
,
700 .setattr
= kernfs_iop_setattr
,
701 .getattr
= kernfs_iop_getattr
,
702 .setxattr
= kernfs_iop_setxattr
,
703 .removexattr
= kernfs_iop_removexattr
,
704 .getxattr
= kernfs_iop_getxattr
,
705 .listxattr
= kernfs_iop_listxattr
,
707 .mkdir
= kernfs_iop_mkdir
,
708 .rmdir
= kernfs_iop_rmdir
,
709 .rename
= kernfs_iop_rename
,
712 static struct kernfs_node
*kernfs_leftmost_descendant(struct kernfs_node
*pos
)
714 struct kernfs_node
*last
;
721 if (kernfs_type(pos
) != KERNFS_DIR
)
724 rbn
= rb_first(&pos
->dir
.children
);
735 * kernfs_next_descendant_post - find the next descendant for post-order walk
736 * @pos: the current position (%NULL to initiate traversal)
737 * @root: kernfs_node whose descendants to walk
739 * Find the next descendant to visit for post-order traversal of @root's
740 * descendants. @root is included in the iteration and the last node to be
743 static struct kernfs_node
*kernfs_next_descendant_post(struct kernfs_node
*pos
,
744 struct kernfs_node
*root
)
748 lockdep_assert_held(&kernfs_mutex
);
750 /* if first iteration, visit leftmost descendant which may be root */
752 return kernfs_leftmost_descendant(root
);
754 /* if we visited @root, we're done */
758 /* if there's an unvisited sibling, visit its leftmost descendant */
759 rbn
= rb_next(&pos
->rb
);
761 return kernfs_leftmost_descendant(rb_to_kn(rbn
));
763 /* no sibling left, visit parent */
767 static void __kernfs_deactivate(struct kernfs_node
*kn
)
769 struct kernfs_node
*pos
;
771 lockdep_assert_held(&kernfs_mutex
);
773 /* prevent any new usage under @kn by deactivating all nodes */
775 while ((pos
= kernfs_next_descendant_post(pos
, kn
))) {
776 if (atomic_read(&pos
->active
) >= 0) {
777 atomic_add(KN_DEACTIVATED_BIAS
, &pos
->active
);
778 pos
->flags
|= KERNFS_JUST_DEACTIVATED
;
783 * Drain the subtree. If kernfs_drain() blocked to drain, which is
784 * indicated by %true return, it temporarily released kernfs_mutex
785 * and the rbtree might have been modified inbetween breaking our
786 * future walk. Restart the walk after each %true return.
789 while ((pos
= kernfs_next_descendant_post(pos
, kn
))) {
793 drained
= kernfs_drain(pos
);
800 static void __kernfs_remove(struct kernfs_node
*kn
)
802 struct kernfs_root
*root
= kernfs_root(kn
);
803 struct kernfs_node
*pos
;
805 lockdep_assert_held(&kernfs_mutex
);
810 pr_debug("kernfs %s: removing\n", kn
->name
);
812 __kernfs_deactivate(kn
);
814 /* unlink the subtree node-by-node */
816 pos
= kernfs_leftmost_descendant(kn
);
819 * We're gonna release kernfs_mutex to unmap bin files,
820 * Make sure @pos doesn't go away inbetween.
825 * This must be come before unlinking; otherwise, when
826 * there are multiple removers, some may finish before
827 * unmapping is complete.
829 if (pos
->flags
& KERNFS_HAS_MMAP
) {
830 mutex_unlock(&kernfs_mutex
);
831 kernfs_unmap_file(pos
);
832 mutex_lock(&kernfs_mutex
);
836 * kernfs_unlink_sibling() succeeds once per node. Use it
837 * to decide who's responsible for cleanups.
839 if (!pos
->parent
|| kernfs_unlink_sibling(pos
)) {
840 struct kernfs_iattrs
*ps_iattr
=
841 pos
->parent
? pos
->parent
->iattr
: NULL
;
843 /* update timestamps on the parent */
845 ps_iattr
->ia_iattr
.ia_ctime
= CURRENT_TIME
;
846 ps_iattr
->ia_iattr
.ia_mtime
= CURRENT_TIME
;
855 /* some nodes killed, kick get_active waiters */
856 wake_up_all(&root
->deactivate_waitq
);
860 * kernfs_remove - remove a kernfs_node recursively
861 * @kn: the kernfs_node to remove
863 * Remove @kn along with all its subdirectories and files.
865 void kernfs_remove(struct kernfs_node
*kn
)
867 mutex_lock(&kernfs_mutex
);
869 mutex_unlock(&kernfs_mutex
);
873 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
874 * @parent: parent of the target
875 * @name: name of the kernfs_node to remove
876 * @ns: namespace tag of the kernfs_node to remove
878 * Look for the kernfs_node with @name and @ns under @parent and remove it.
879 * Returns 0 on success, -ENOENT if such entry doesn't exist.
881 int kernfs_remove_by_name_ns(struct kernfs_node
*parent
, const char *name
,
884 struct kernfs_node
*kn
;
887 WARN(1, KERN_WARNING
"kernfs: can not remove '%s', no directory\n",
892 mutex_lock(&kernfs_mutex
);
894 kn
= kernfs_find_ns(parent
, name
, ns
);
898 mutex_unlock(&kernfs_mutex
);
907 * kernfs_rename_ns - move and rename a kernfs_node
909 * @new_parent: new parent to put @sd under
910 * @new_name: new name
911 * @new_ns: new namespace tag
913 int kernfs_rename_ns(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
914 const char *new_name
, const void *new_ns
)
919 if (!kernfs_get_active(new_parent
))
921 if (!kernfs_get_active(kn
))
922 goto out_put_new_parent
;
924 mutex_lock(&kernfs_mutex
);
927 if ((kn
->parent
== new_parent
) && (kn
->ns
== new_ns
) &&
928 (strcmp(kn
->name
, new_name
) == 0))
929 goto out_unlock
; /* nothing to rename */
932 if (kernfs_find_ns(new_parent
, new_name
, new_ns
))
935 /* rename kernfs_node */
936 if (strcmp(kn
->name
, new_name
) != 0) {
938 new_name
= kstrdup(new_name
, GFP_KERNEL
);
942 if (kn
->flags
& KERNFS_STATIC_NAME
)
943 kn
->flags
&= ~KERNFS_STATIC_NAME
;
951 * Move to the appropriate place in the appropriate directories rbtree.
953 kernfs_unlink_sibling(kn
);
954 kernfs_get(new_parent
);
955 kernfs_put(kn
->parent
);
957 kn
->hash
= kernfs_name_hash(kn
->name
, kn
->ns
);
958 kn
->parent
= new_parent
;
959 kernfs_link_sibling(kn
);
963 mutex_unlock(&kernfs_mutex
);
964 kernfs_put_active(kn
);
966 kernfs_put_active(new_parent
);
971 /* Relationship between s_mode and the DT_xxx types */
972 static inline unsigned char dt_type(struct kernfs_node
*kn
)
974 return (kn
->mode
>> 12) & 15;
977 static int kernfs_dir_fop_release(struct inode
*inode
, struct file
*filp
)
979 kernfs_put(filp
->private_data
);
983 static struct kernfs_node
*kernfs_dir_pos(const void *ns
,
984 struct kernfs_node
*parent
, loff_t hash
, struct kernfs_node
*pos
)
987 int valid
= pos
->parent
== parent
&& hash
== pos
->hash
;
992 if (!pos
&& (hash
> 1) && (hash
< INT_MAX
)) {
993 struct rb_node
*node
= parent
->dir
.children
.rb_node
;
995 pos
= rb_to_kn(node
);
997 if (hash
< pos
->hash
)
998 node
= node
->rb_left
;
999 else if (hash
> pos
->hash
)
1000 node
= node
->rb_right
;
1005 /* Skip over entries in the wrong namespace */
1006 while (pos
&& pos
->ns
!= ns
) {
1007 struct rb_node
*node
= rb_next(&pos
->rb
);
1011 pos
= rb_to_kn(node
);
1016 static struct kernfs_node
*kernfs_dir_next_pos(const void *ns
,
1017 struct kernfs_node
*parent
, ino_t ino
, struct kernfs_node
*pos
)
1019 pos
= kernfs_dir_pos(ns
, parent
, ino
, pos
);
1022 struct rb_node
*node
= rb_next(&pos
->rb
);
1026 pos
= rb_to_kn(node
);
1027 } while (pos
&& pos
->ns
!= ns
);
1031 static int kernfs_fop_readdir(struct file
*file
, struct dir_context
*ctx
)
1033 struct dentry
*dentry
= file
->f_path
.dentry
;
1034 struct kernfs_node
*parent
= dentry
->d_fsdata
;
1035 struct kernfs_node
*pos
= file
->private_data
;
1036 const void *ns
= NULL
;
1038 if (!dir_emit_dots(file
, ctx
))
1040 mutex_lock(&kernfs_mutex
);
1042 if (kernfs_ns_enabled(parent
))
1043 ns
= kernfs_info(dentry
->d_sb
)->ns
;
1045 for (pos
= kernfs_dir_pos(ns
, parent
, ctx
->pos
, pos
);
1047 pos
= kernfs_dir_next_pos(ns
, parent
, ctx
->pos
, pos
)) {
1048 const char *name
= pos
->name
;
1049 unsigned int type
= dt_type(pos
);
1050 int len
= strlen(name
);
1051 ino_t ino
= pos
->ino
;
1053 ctx
->pos
= pos
->hash
;
1054 file
->private_data
= pos
;
1057 mutex_unlock(&kernfs_mutex
);
1058 if (!dir_emit(ctx
, name
, len
, ino
, type
))
1060 mutex_lock(&kernfs_mutex
);
1062 mutex_unlock(&kernfs_mutex
);
1063 file
->private_data
= NULL
;
1068 static loff_t
kernfs_dir_fop_llseek(struct file
*file
, loff_t offset
,
1071 struct inode
*inode
= file_inode(file
);
1074 mutex_lock(&inode
->i_mutex
);
1075 ret
= generic_file_llseek(file
, offset
, whence
);
1076 mutex_unlock(&inode
->i_mutex
);
1081 const struct file_operations kernfs_dir_fops
= {
1082 .read
= generic_read_dir
,
1083 .iterate
= kernfs_fop_readdir
,
1084 .release
= kernfs_dir_fop_release
,
1085 .llseek
= kernfs_dir_fop_llseek
,