Lines Matching +full:root +full:- +full:node
1 // SPDX-License-Identifier: GPL-2.0-only
3 * fs/kernfs/dir.c - kernfs directory implementation
5 * Copyright (c) 2001-3 Patrick Mochel
18 #include "kernfs-internal.h"
20 static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */
30 static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */
36 return atomic_read(&kn->active) >= 0; in __kernfs_active()
41 lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem); in kernfs_active()
48 return kn->flags & KERNFS_LOCKDEP; in kernfs_lockdep()
59 return strscpy(buf, kn->parent ? kn->name : "/", buflen); in kernfs_name_locked()
62 /* kernfs_node_depth - compute depth from @from to @to */
67 while (to->parent && to != from) { in kernfs_depth()
69 to = to->parent; in kernfs_depth()
83 da = kernfs_depth(ra->kn, a); in kernfs_common_ancestor()
84 db = kernfs_depth(rb->kn, b); in kernfs_common_ancestor()
87 a = a->parent; in kernfs_common_ancestor()
88 da--; in kernfs_common_ancestor()
91 b = b->parent; in kernfs_common_ancestor()
92 db--; in kernfs_common_ancestor()
95 /* worst case b and a will be the same at root */ in kernfs_common_ancestor()
97 b = b->parent; in kernfs_common_ancestor()
98 a = a->parent; in kernfs_common_ancestor()
105 * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
106 * where kn_from is treated as root of the path.
107 * @kn_from: kernfs node which should be treated as root for the path
108 * @kn_to: kernfs node to which path is needed
132 * '\0'. On error, -errno is returned.
148 kn_from = kernfs_root(kn_to)->kn; in kernfs_path_from_node_locked()
155 return -EINVAL; in kernfs_path_from_node_locked()
163 copied = strscpy(buf + len, parent_str, buflen - len); in kernfs_path_from_node_locked()
170 for (i = depth_to - 1; i >= 0; i--) { in kernfs_path_from_node_locked()
172 kn = kn->parent; in kernfs_path_from_node_locked()
174 len += scnprintf(buf + len, buflen - len, "/%s", kn->name); in kernfs_path_from_node_locked()
181 * kernfs_name - obtain the name of a given node
192 * it's filled up to @buflen-1 and nul terminated, and returns -E2BIG.
208 * kernfs_path_from_node - build path of node @to relative to @from.
215 * be on the same kernfs-root. If @from is not parent of @to, then a relative
221 * '\0'. On error, -errno is returned.
237 * pr_cont_kernfs_name - pr_cont name of a kernfs_node
255 * pr_cont_kernfs_path - pr_cont path of a kernfs_node
270 if (sz == -E2BIG) in pr_cont_kernfs_path()
284 * kernfs_get_parent - determine the parent node and pin it
290 * Return: parent node of @kn
298 parent = kn->parent; in kernfs_get_parent()
306 * kernfs_name_hash - calculate hash of @ns + @name
310 * Return: 31-bit hash of ns + name (so it fits in an off_t)
316 while (len--) in kernfs_name_hash()
324 hash = INT_MAX - 1; in kernfs_name_hash()
331 if (hash < kn->hash) in kernfs_name_compare()
332 return -1; in kernfs_name_compare()
333 if (hash > kn->hash) in kernfs_name_compare()
335 if (ns < kn->ns) in kernfs_name_compare()
336 return -1; in kernfs_name_compare()
337 if (ns > kn->ns) in kernfs_name_compare()
339 return strcmp(name, kn->name); in kernfs_name_compare()
345 return kernfs_name_compare(left->hash, left->name, left->ns, right); in kernfs_sd_compare()
349 * kernfs_link_sibling - link kernfs_node into sibling rbtree
353 * @kn->parent->dir.children.
359 * %0 on success, -EEXIST on failure.
363 struct rb_node **node = &kn->parent->dir.children.rb_node; in kernfs_link_sibling() local
366 while (*node) { in kernfs_link_sibling()
370 pos = rb_to_kn(*node); in kernfs_link_sibling()
371 parent = *node; in kernfs_link_sibling()
374 node = &pos->rb.rb_left; in kernfs_link_sibling()
376 node = &pos->rb.rb_right; in kernfs_link_sibling()
378 return -EEXIST; in kernfs_link_sibling()
381 /* add new node and rebalance the tree */ in kernfs_link_sibling()
382 rb_link_node(&kn->rb, parent, node); in kernfs_link_sibling()
383 rb_insert_color(&kn->rb, &kn->parent->dir.children); in kernfs_link_sibling()
386 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); in kernfs_link_sibling()
388 kn->parent->dir.subdirs++; in kernfs_link_sibling()
389 kernfs_inc_rev(kn->parent); in kernfs_link_sibling()
390 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); in kernfs_link_sibling()
396 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
400 * kn->parent->dir.children.
410 if (RB_EMPTY_NODE(&kn->rb)) in kernfs_unlink_sibling()
413 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); in kernfs_unlink_sibling()
415 kn->parent->dir.subdirs--; in kernfs_unlink_sibling()
416 kernfs_inc_rev(kn->parent); in kernfs_unlink_sibling()
417 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); in kernfs_unlink_sibling()
419 rb_erase(&kn->rb, &kn->parent->dir.children); in kernfs_unlink_sibling()
420 RB_CLEAR_NODE(&kn->rb); in kernfs_unlink_sibling()
425 * kernfs_get_active - get an active reference to kernfs_node
439 if (!atomic_inc_unless_negative(&kn->active)) in kernfs_get_active()
443 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); in kernfs_get_active()
448 * kernfs_put_active - put an active reference to kernfs_node
462 rwsem_release(&kn->dep_map, _RET_IP_); in kernfs_put_active()
463 v = atomic_dec_return(&kn->active); in kernfs_put_active()
467 wake_up_all(&kernfs_root(kn)->deactivate_waitq); in kernfs_put_active()
471 * kernfs_drain - drain kernfs_node
479 __releases(&kernfs_root(kn)->kernfs_rwsem) in kernfs_drain()
480 __acquires(&kernfs_root(kn)->kernfs_rwsem) in kernfs_drain()
482 struct kernfs_root *root = kernfs_root(kn); in kernfs_drain() local
484 lockdep_assert_held_write(&root->kernfs_rwsem); in kernfs_drain()
493 if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS && in kernfs_drain()
497 up_write(&root->kernfs_rwsem); in kernfs_drain()
500 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); in kernfs_drain()
501 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS) in kernfs_drain()
502 lock_contended(&kn->dep_map, _RET_IP_); in kernfs_drain()
505 wait_event(root->deactivate_waitq, in kernfs_drain()
506 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS); in kernfs_drain()
509 lock_acquired(&kn->dep_map, _RET_IP_); in kernfs_drain()
510 rwsem_release(&kn->dep_map, _RET_IP_); in kernfs_drain()
516 down_write(&root->kernfs_rwsem); in kernfs_drain()
520 * kernfs_get - get a reference count on a kernfs_node
526 WARN_ON(!atomic_read(&kn->count)); in kernfs_get()
527 atomic_inc(&kn->count); in kernfs_get()
536 kfree_const(kn->name); in kernfs_free_rcu()
538 if (kn->iattr) { in kernfs_free_rcu()
539 simple_xattrs_free(&kn->iattr->xattrs, NULL); in kernfs_free_rcu()
540 kmem_cache_free(kernfs_iattrs_cache, kn->iattr); in kernfs_free_rcu()
547 * kernfs_put - put a reference count on a kernfs_node
555 struct kernfs_root *root; in kernfs_put() local
557 if (!kn || !atomic_dec_and_test(&kn->count)) in kernfs_put()
559 root = kernfs_root(kn); in kernfs_put()
563 * kn->parent won't change beneath us. in kernfs_put()
565 parent = kn->parent; in kernfs_put()
567 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS, in kernfs_put()
569 parent ? parent->name : "", kn->name, atomic_read(&kn->active)); in kernfs_put()
572 kernfs_put(kn->symlink.target_kn); in kernfs_put()
575 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn)); in kernfs_put()
578 call_rcu(&kn->rcu, kernfs_free_rcu); in kernfs_put()
582 if (atomic_dec_and_test(&kn->count)) in kernfs_put()
585 /* just released the root kn, free @root too */ in kernfs_put()
586 idr_destroy(&root->ino_idr); in kernfs_put()
587 kfree_rcu(root, rcu); in kernfs_put()
593 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
600 * is accessible, the returned node can be in any state and the caller is
605 if (dentry->d_sb->s_op == &kernfs_sops) in kernfs_node_from_dentry()
610 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, in __kernfs_new_node() argument
630 ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC); in __kernfs_new_node()
631 if (ret >= 0 && ret < root->last_id_lowbits) in __kernfs_new_node()
632 root->id_highbits++; in __kernfs_new_node()
633 id_highbits = root->id_highbits; in __kernfs_new_node()
634 root->last_id_lowbits = ret; in __kernfs_new_node()
640 kn->id = (u64)id_highbits << 32 | ret; in __kernfs_new_node()
642 atomic_set(&kn->count, 1); in __kernfs_new_node()
643 atomic_set(&kn->active, KN_DEACTIVATED_BIAS); in __kernfs_new_node()
644 RB_CLEAR_NODE(&kn->rb); in __kernfs_new_node()
646 kn->name = name; in __kernfs_new_node()
647 kn->mode = mode; in __kernfs_new_node()
648 kn->flags = flags; in __kernfs_new_node()
672 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn)); in __kernfs_new_node()
688 if (parent->mode & S_ISGID) { in kernfs_new_node()
693 if (parent->iattr) in kernfs_new_node()
694 gid = parent->iattr->ia_gid; in kernfs_new_node()
704 kn->parent = parent; in kernfs_new_node()
710 * kernfs_find_and_get_node_by_id - get kernfs_node from node id
711 * @root: the kernfs root
712 * @id: the target node id
718 * otherwise a kernfs node with reference counter incremented.
720 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root, in kernfs_find_and_get_node_by_id() argument
729 kn = idr_find(&root->ino_idr, (u32)ino); in kernfs_find_and_get_node_by_id()
746 * __kernfs_active() which tests @kn->active without kernfs_rwsem. in kernfs_find_and_get_node_by_id()
748 if (unlikely(!__kernfs_active(kn) || !atomic_inc_not_zero(&kn->count))) in kernfs_find_and_get_node_by_id()
759 * kernfs_add_one - add kernfs_node to parent without warning
762 * The caller must already have initialized @kn->parent. This
767 * %0 on success, -EEXIST if entry with the given name already
772 struct kernfs_node *parent = kn->parent; in kernfs_add_one()
773 struct kernfs_root *root = kernfs_root(parent); in kernfs_add_one() local
778 down_write(&root->kernfs_rwsem); in kernfs_add_one()
780 ret = -EINVAL; in kernfs_add_one()
782 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", in kernfs_add_one()
783 has_ns ? "required" : "invalid", parent->name, kn->name)) in kernfs_add_one()
789 ret = -ENOENT; in kernfs_add_one()
790 if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR)) in kernfs_add_one()
793 kn->hash = kernfs_name_hash(kn->name, kn->ns); in kernfs_add_one()
800 down_write(&root->kernfs_iattr_rwsem); in kernfs_add_one()
802 ps_iattr = parent->iattr; in kernfs_add_one()
804 ktime_get_real_ts64(&ps_iattr->ia_ctime); in kernfs_add_one()
805 ps_iattr->ia_mtime = ps_iattr->ia_ctime; in kernfs_add_one()
808 up_write(&root->kernfs_iattr_rwsem); in kernfs_add_one()
809 up_write(&root->kernfs_rwsem); in kernfs_add_one()
812 * Activate the new node unless CREATE_DEACTIVATED is requested. in kernfs_add_one()
814 * activating the node with kernfs_activate(). A node which hasn't in kernfs_add_one()
818 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED)) in kernfs_add_one()
823 up_write(&root->kernfs_rwsem); in kernfs_add_one()
828 * kernfs_find_ns - find kernfs_node with the given name
841 struct rb_node *node = parent->dir.children.rb_node; in kernfs_find_ns() local
845 lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem); in kernfs_find_ns()
849 has_ns ? "required" : "invalid", parent->name, name); in kernfs_find_ns()
854 while (node) { in kernfs_find_ns()
858 kn = rb_to_kn(node); in kernfs_find_ns()
861 node = node->rb_left; in kernfs_find_ns()
863 node = node->rb_right; in kernfs_find_ns()
877 lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem); in kernfs_walk_ns()
902 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
916 struct kernfs_root *root = kernfs_root(parent); in kernfs_find_and_get_ns() local
918 down_read(&root->kernfs_rwsem); in kernfs_find_and_get_ns()
921 up_read(&root->kernfs_rwsem); in kernfs_find_and_get_ns()
928 * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
942 struct kernfs_root *root = kernfs_root(parent); in kernfs_walk_and_get_ns() local
944 down_read(&root->kernfs_rwsem); in kernfs_walk_and_get_ns()
947 up_read(&root->kernfs_rwsem); in kernfs_walk_and_get_ns()
953 * kernfs_create_root - create a new kernfs hierarchy
958 * Return: the root of the new hierarchy on success, ERR_PTR() value on
964 struct kernfs_root *root; in kernfs_create_root() local
967 root = kzalloc(sizeof(*root), GFP_KERNEL); in kernfs_create_root()
968 if (!root) in kernfs_create_root()
969 return ERR_PTR(-ENOMEM); in kernfs_create_root()
971 idr_init(&root->ino_idr); in kernfs_create_root()
972 init_rwsem(&root->kernfs_rwsem); in kernfs_create_root()
973 init_rwsem(&root->kernfs_iattr_rwsem); in kernfs_create_root()
974 init_rwsem(&root->kernfs_supers_rwsem); in kernfs_create_root()
975 INIT_LIST_HEAD(&root->supers); in kernfs_create_root()
984 root->id_highbits = 0; in kernfs_create_root()
986 root->id_highbits = 1; in kernfs_create_root()
988 kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO, in kernfs_create_root()
992 idr_destroy(&root->ino_idr); in kernfs_create_root()
993 kfree(root); in kernfs_create_root()
994 return ERR_PTR(-ENOMEM); in kernfs_create_root()
997 kn->priv = priv; in kernfs_create_root()
998 kn->dir.root = root; in kernfs_create_root()
1000 root->syscall_ops = scops; in kernfs_create_root()
1001 root->flags = flags; in kernfs_create_root()
1002 root->kn = kn; in kernfs_create_root()
1003 init_waitqueue_head(&root->deactivate_waitq); in kernfs_create_root()
1005 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED)) in kernfs_create_root()
1008 return root; in kernfs_create_root()
1012 * kernfs_destroy_root - destroy a kernfs hierarchy
1013 * @root: root of the hierarchy to destroy
1015 * Destroy the hierarchy anchored at @root by removing all existing
1016 * directories and destroying @root.
1018 void kernfs_destroy_root(struct kernfs_root *root) in kernfs_destroy_root() argument
1021 * kernfs_remove holds kernfs_rwsem from the root so the root in kernfs_destroy_root()
1024 kernfs_get(root->kn); in kernfs_destroy_root()
1025 kernfs_remove(root->kn); in kernfs_destroy_root()
1026 kernfs_put(root->kn); /* will also free @root */ in kernfs_destroy_root()
1030 * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root
1031 * @root: root to use to lookup
1033 * Return: @root's kernfs_node
1035 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root) in kernfs_root_to_node() argument
1037 return root->kn; in kernfs_root_to_node()
1041 * kernfs_create_dir_ns - create a directory
1050 * Return: the created node on success, ERR_PTR() value on failure.
1064 return ERR_PTR(-ENOMEM); in kernfs_create_dir_ns()
1066 kn->dir.root = parent->dir.root; in kernfs_create_dir_ns()
1067 kn->ns = ns; in kernfs_create_dir_ns()
1068 kn->priv = priv; in kernfs_create_dir_ns()
1080 * kernfs_create_empty_dir - create an always empty directory
1084 * Return: the created node on success, ERR_PTR() value on failure.
1096 return ERR_PTR(-ENOMEM); in kernfs_create_empty_dir()
1098 kn->flags |= KERNFS_EMPTY_DIR; in kernfs_create_empty_dir()
1099 kn->dir.root = parent->dir.root; in kernfs_create_empty_dir()
1100 kn->ns = NULL; in kernfs_create_empty_dir()
1101 kn->priv = NULL; in kernfs_create_empty_dir()
1115 struct kernfs_root *root; in kernfs_dop_revalidate() local
1118 return -ECHILD; in kernfs_dop_revalidate()
1124 /* If the kernfs parent node has changed discard and in kernfs_dop_revalidate()
1125 * proceed to ->lookup. in kernfs_dop_revalidate()
1137 * changes and the lookup re-done so that a new positive in kernfs_dop_revalidate()
1140 root = kernfs_root_from_sb(dentry->d_sb); in kernfs_dop_revalidate()
1141 down_read(&root->kernfs_rwsem); in kernfs_dop_revalidate()
1142 parent = kernfs_dentry_node(dentry->d_parent); in kernfs_dop_revalidate()
1145 up_read(&root->kernfs_rwsem); in kernfs_dop_revalidate()
1149 up_read(&root->kernfs_rwsem); in kernfs_dop_revalidate()
1151 /* The kernfs parent node hasn't changed, leave the in kernfs_dop_revalidate()
1158 root = kernfs_root(kn); in kernfs_dop_revalidate()
1159 down_read(&root->kernfs_rwsem); in kernfs_dop_revalidate()
1161 /* The kernfs node has been deactivated */ in kernfs_dop_revalidate()
1165 /* The kernfs node has been moved? */ in kernfs_dop_revalidate()
1166 if (kernfs_dentry_node(dentry->d_parent) != kn->parent) in kernfs_dop_revalidate()
1169 /* The kernfs node has been renamed */ in kernfs_dop_revalidate()
1170 if (strcmp(dentry->d_name.name, kn->name) != 0) in kernfs_dop_revalidate()
1173 /* The kernfs node has been moved to a different namespace */ in kernfs_dop_revalidate()
1174 if (kn->parent && kernfs_ns_enabled(kn->parent) && in kernfs_dop_revalidate()
1175 kernfs_info(dentry->d_sb)->ns != kn->ns) in kernfs_dop_revalidate()
1178 up_read(&root->kernfs_rwsem); in kernfs_dop_revalidate()
1181 up_read(&root->kernfs_rwsem); in kernfs_dop_revalidate()
1193 struct kernfs_node *parent = dir->i_private; in kernfs_iop_lookup()
1195 struct kernfs_root *root; in kernfs_iop_lookup() local
1199 root = kernfs_root(parent); in kernfs_iop_lookup()
1200 down_read(&root->kernfs_rwsem); in kernfs_iop_lookup()
1202 ns = kernfs_info(dir->i_sb)->ns; in kernfs_iop_lookup()
1204 kn = kernfs_find_ns(parent, dentry->d_name.name, ns); in kernfs_iop_lookup()
1211 up_read(&root->kernfs_rwsem); in kernfs_iop_lookup()
1214 inode = kernfs_get_inode(dir->i_sb, kn); in kernfs_iop_lookup()
1216 inode = ERR_PTR(-ENOMEM); in kernfs_iop_lookup()
1226 up_read(&root->kernfs_rwsem); in kernfs_iop_lookup()
1236 struct kernfs_node *parent = dir->i_private; in kernfs_iop_mkdir()
1237 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops; in kernfs_iop_mkdir()
1240 if (!scops || !scops->mkdir) in kernfs_iop_mkdir()
1241 return -EPERM; in kernfs_iop_mkdir()
1244 return -ENODEV; in kernfs_iop_mkdir()
1246 ret = scops->mkdir(parent, dentry->d_name.name, mode); in kernfs_iop_mkdir()
1255 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops; in kernfs_iop_rmdir()
1258 if (!scops || !scops->rmdir) in kernfs_iop_rmdir()
1259 return -EPERM; in kernfs_iop_rmdir()
1262 return -ENODEV; in kernfs_iop_rmdir()
1264 ret = scops->rmdir(kn); in kernfs_iop_rmdir()
1276 struct kernfs_node *new_parent = new_dir->i_private; in kernfs_iop_rename()
1277 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops; in kernfs_iop_rename()
1281 return -EINVAL; in kernfs_iop_rename()
1283 if (!scops || !scops->rename) in kernfs_iop_rename()
1284 return -EPERM; in kernfs_iop_rename()
1287 return -ENODEV; in kernfs_iop_rename()
1291 return -ENODEV; in kernfs_iop_rename()
1294 ret = scops->rename(kn, new_parent, new_dentry->d_name.name); in kernfs_iop_rename()
1325 rbn = rb_first(&pos->dir.children); in kernfs_leftmost_descendant()
1336 * kernfs_next_descendant_post - find the next descendant for post-order walk
1338 * @root: kernfs_node whose descendants to walk
1340 * Find the next descendant to visit for post-order traversal of @root's
1341 * descendants. @root is included in the iteration and the last node to be
1347 struct kernfs_node *root) in kernfs_next_descendant_post() argument
1351 lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem); in kernfs_next_descendant_post()
1353 /* if first iteration, visit leftmost descendant which may be root */ in kernfs_next_descendant_post()
1355 return kernfs_leftmost_descendant(root); in kernfs_next_descendant_post()
1357 /* if we visited @root, we're done */ in kernfs_next_descendant_post()
1358 if (pos == root) in kernfs_next_descendant_post()
1362 rbn = rb_next(&pos->rb); in kernfs_next_descendant_post()
1367 return pos->parent; in kernfs_next_descendant_post()
1372 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); in kernfs_activate_one()
1374 kn->flags |= KERNFS_ACTIVATED; in kernfs_activate_one()
1376 if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING))) in kernfs_activate_one()
1379 WARN_ON_ONCE(kn->parent && RB_EMPTY_NODE(&kn->rb)); in kernfs_activate_one()
1380 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS); in kernfs_activate_one()
1382 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active); in kernfs_activate_one()
1386 * kernfs_activate - activate a node which started deactivated
1389 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
1390 * needs to be explicitly activated. A node which hasn't been activated
1401 struct kernfs_root *root = kernfs_root(kn); in kernfs_activate() local
1403 down_write(&root->kernfs_rwsem); in kernfs_activate()
1409 up_write(&root->kernfs_rwsem); in kernfs_activate()
1413 * kernfs_show - show or hide a node
1417 * If @show is %false, @kn is marked hidden and deactivated. A hidden node is
1419 * state is restored. This function won't implicitly activate a new node in a
1420 * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet.
1426 struct kernfs_root *root = kernfs_root(kn); in kernfs_show() local
1431 down_write(&root->kernfs_rwsem); in kernfs_show()
1434 kn->flags &= ~KERNFS_HIDDEN; in kernfs_show()
1435 if (kn->flags & KERNFS_ACTIVATED) in kernfs_show()
1438 kn->flags |= KERNFS_HIDDEN; in kernfs_show()
1440 atomic_add(KN_DEACTIVATED_BIAS, &kn->active); in kernfs_show()
1444 up_write(&root->kernfs_rwsem); in kernfs_show()
1451 /* Short-circuit if non-root @kn has already finished removal. */ in __kernfs_remove()
1455 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); in __kernfs_remove()
1461 if (kn->parent && RB_EMPTY_NODE(&kn->rb)) in __kernfs_remove()
1464 pr_debug("kernfs %s: removing\n", kn->name); in __kernfs_remove()
1469 pos->flags |= KERNFS_REMOVING; in __kernfs_remove()
1471 atomic_add(KN_DEACTIVATED_BIAS, &pos->active); in __kernfs_remove()
1474 /* deactivate and unlink the subtree node-by-node */ in __kernfs_remove()
1489 * kernfs_unlink_sibling() succeeds once per node. Use it in __kernfs_remove()
1492 if (!pos->parent || kernfs_unlink_sibling(pos)) { in __kernfs_remove()
1494 pos->parent ? pos->parent->iattr : NULL; in __kernfs_remove()
1497 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); in __kernfs_remove()
1500 ktime_get_real_ts64(&ps_iattr->ia_ctime); in __kernfs_remove()
1501 ps_iattr->ia_mtime = ps_iattr->ia_ctime; in __kernfs_remove()
1504 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); in __kernfs_remove()
1513 * kernfs_remove - remove a kernfs_node recursively
1520 struct kernfs_root *root; in kernfs_remove() local
1525 root = kernfs_root(kn); in kernfs_remove()
1527 down_write(&root->kernfs_rwsem); in kernfs_remove()
1529 up_write(&root->kernfs_rwsem); in kernfs_remove()
1533 * kernfs_break_active_protection - break out of active protection
1537 * with an active reference - e.g. one of kernfs_ops. Each invocation of
1556 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
1562 * restore the active protection - @kn may already or be in the process of
1573 * @kn->active could be in any state; however, the increment we do in kernfs_unbreak_active_protection()
1577 * soon-to-follow put will either finish deactivation or restore in kernfs_unbreak_active_protection()
1581 atomic_inc(&kn->active); in kernfs_unbreak_active_protection()
1583 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_); in kernfs_unbreak_active_protection()
1587 * kernfs_remove_self - remove a kernfs_node from its own method
1591 * with an active reference - e.g. one of kernfs_ops. This can be used to
1606 * won self-removal finishes and return %false. Note that the losers wait
1617 struct kernfs_root *root = kernfs_root(kn); in kernfs_remove_self() local
1619 down_write(&root->kernfs_rwsem); in kernfs_remove_self()
1631 if (!(kn->flags & KERNFS_SUICIDAL)) { in kernfs_remove_self()
1632 kn->flags |= KERNFS_SUICIDAL; in kernfs_remove_self()
1634 kn->flags |= KERNFS_SUICIDED; in kernfs_remove_self()
1637 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq; in kernfs_remove_self()
1643 if ((kn->flags & KERNFS_SUICIDED) && in kernfs_remove_self()
1644 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS) in kernfs_remove_self()
1647 up_write(&root->kernfs_rwsem); in kernfs_remove_self()
1649 down_write(&root->kernfs_rwsem); in kernfs_remove_self()
1652 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); in kernfs_remove_self()
1662 up_write(&root->kernfs_rwsem); in kernfs_remove_self()
1667 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1674 * Return: %0 on success, -ENOENT if such entry doesn't exist.
1680 struct kernfs_root *root; in kernfs_remove_by_name_ns() local
1685 return -ENOENT; in kernfs_remove_by_name_ns()
1688 root = kernfs_root(parent); in kernfs_remove_by_name_ns()
1689 down_write(&root->kernfs_rwsem); in kernfs_remove_by_name_ns()
1698 up_write(&root->kernfs_rwsem); in kernfs_remove_by_name_ns()
1703 return -ENOENT; in kernfs_remove_by_name_ns()
1707 * kernfs_rename_ns - move and rename a kernfs_node
1708 * @kn: target node
1713 * Return: %0 on success, -errno on failure.
1719 struct kernfs_root *root; in kernfs_rename_ns() local
1723 /* can't move or rename root */ in kernfs_rename_ns()
1724 if (!kn->parent) in kernfs_rename_ns()
1725 return -EINVAL; in kernfs_rename_ns()
1727 root = kernfs_root(kn); in kernfs_rename_ns()
1728 down_write(&root->kernfs_rwsem); in kernfs_rename_ns()
1730 error = -ENOENT; in kernfs_rename_ns()
1732 (new_parent->flags & KERNFS_EMPTY_DIR)) in kernfs_rename_ns()
1736 if ((kn->parent == new_parent) && (kn->ns == new_ns) && in kernfs_rename_ns()
1737 (strcmp(kn->name, new_name) == 0)) in kernfs_rename_ns()
1740 error = -EEXIST; in kernfs_rename_ns()
1745 if (strcmp(kn->name, new_name) != 0) { in kernfs_rename_ns()
1746 error = -ENOMEM; in kernfs_rename_ns()
1760 /* rename_lock protects ->parent and ->name accessors */ in kernfs_rename_ns()
1763 old_parent = kn->parent; in kernfs_rename_ns()
1764 kn->parent = new_parent; in kernfs_rename_ns()
1766 kn->ns = new_ns; in kernfs_rename_ns()
1768 old_name = kn->name; in kernfs_rename_ns()
1769 kn->name = new_name; in kernfs_rename_ns()
1774 kn->hash = kernfs_name_hash(kn->name, kn->ns); in kernfs_rename_ns()
1782 up_write(&root->kernfs_rwsem); in kernfs_rename_ns()
1788 kernfs_put(filp->private_data); in kernfs_dir_fop_release()
1797 pos->parent == parent && hash == pos->hash; in kernfs_dir_pos()
1803 struct rb_node *node = parent->dir.children.rb_node; in kernfs_dir_pos() local
1804 while (node) { in kernfs_dir_pos()
1805 pos = rb_to_kn(node); in kernfs_dir_pos()
1807 if (hash < pos->hash) in kernfs_dir_pos()
1808 node = node->rb_left; in kernfs_dir_pos()
1809 else if (hash > pos->hash) in kernfs_dir_pos()
1810 node = node->rb_right; in kernfs_dir_pos()
1816 while (pos && (!kernfs_active(pos) || pos->ns != ns)) { in kernfs_dir_pos()
1817 struct rb_node *node = rb_next(&pos->rb); in kernfs_dir_pos() local
1818 if (!node) in kernfs_dir_pos()
1821 pos = rb_to_kn(node); in kernfs_dir_pos()
1832 struct rb_node *node = rb_next(&pos->rb); in kernfs_dir_next_pos() local
1833 if (!node) in kernfs_dir_next_pos()
1836 pos = rb_to_kn(node); in kernfs_dir_next_pos()
1837 } while (pos && (!kernfs_active(pos) || pos->ns != ns)); in kernfs_dir_next_pos()
1844 struct dentry *dentry = file->f_path.dentry; in kernfs_fop_readdir()
1846 struct kernfs_node *pos = file->private_data; in kernfs_fop_readdir()
1847 struct kernfs_root *root; in kernfs_fop_readdir() local
1853 root = kernfs_root(parent); in kernfs_fop_readdir()
1854 down_read(&root->kernfs_rwsem); in kernfs_fop_readdir()
1857 ns = kernfs_info(dentry->d_sb)->ns; in kernfs_fop_readdir()
1859 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); in kernfs_fop_readdir()
1861 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { in kernfs_fop_readdir()
1862 const char *name = pos->name; in kernfs_fop_readdir()
1863 unsigned int type = fs_umode_to_dtype(pos->mode); in kernfs_fop_readdir()
1867 ctx->pos = pos->hash; in kernfs_fop_readdir()
1868 file->private_data = pos; in kernfs_fop_readdir()
1871 up_read(&root->kernfs_rwsem); in kernfs_fop_readdir()
1874 down_read(&root->kernfs_rwsem); in kernfs_fop_readdir()
1876 up_read(&root->kernfs_rwsem); in kernfs_fop_readdir()
1877 file->private_data = NULL; in kernfs_fop_readdir()
1878 ctx->pos = INT_MAX; in kernfs_fop_readdir()