2 * fs/kernfs/dir.c - kernfs directory implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8 * This file is released under the GPLv2.
12 #include <linux/namei.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/security.h>
16 #include <linux/hash.h>
18 #include "kernfs-internal.h"
20 DEFINE_MUTEX(sysfs_mutex
);
22 #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
24 static DEFINE_SPINLOCK(sysfs_ino_lock
);
25 static DEFINE_IDA(sysfs_ino_ida
);
29 * @name: Null terminated string to hash
30 * @ns: Namespace tag to hash
32 * Returns 31 bit hash of ns + name (so it fits in an off_t )
34 static unsigned int sysfs_name_hash(const char *name
, const void *ns
)
36 unsigned long hash
= init_name_hash();
37 unsigned int len
= strlen(name
);
39 hash
= partial_name_hash(*name
++, hash
);
40 hash
= (end_name_hash(hash
) ^ hash_ptr((void *)ns
, 31));
42 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
50 static int sysfs_name_compare(unsigned int hash
, const char *name
,
51 const void *ns
, const struct sysfs_dirent
*sd
)
53 if (hash
!= sd
->s_hash
)
54 return hash
- sd
->s_hash
;
57 return strcmp(name
, sd
->s_name
);
60 static int sysfs_sd_compare(const struct sysfs_dirent
*left
,
61 const struct sysfs_dirent
*right
)
63 return sysfs_name_compare(left
->s_hash
, left
->s_name
, left
->s_ns
,
68 * sysfs_link_sibling - link sysfs_dirent into sibling rbtree
69 * @sd: sysfs_dirent of interest
71 * Link @sd into its sibling rbtree which starts from
72 * sd->s_parent->s_dir.children.
75 * mutex_lock(sysfs_mutex)
78 * 0 on susccess -EEXIST on failure.
80 static int sysfs_link_sibling(struct sysfs_dirent
*sd
)
82 struct rb_node
**node
= &sd
->s_parent
->s_dir
.children
.rb_node
;
83 struct rb_node
*parent
= NULL
;
85 if (sysfs_type(sd
) == SYSFS_DIR
)
86 sd
->s_parent
->s_dir
.subdirs
++;
89 struct sysfs_dirent
*pos
;
92 pos
= to_sysfs_dirent(*node
);
94 result
= sysfs_sd_compare(sd
, pos
);
96 node
= &pos
->s_rb
.rb_left
;
98 node
= &pos
->s_rb
.rb_right
;
102 /* add new node and rebalance the tree */
103 rb_link_node(&sd
->s_rb
, parent
, node
);
104 rb_insert_color(&sd
->s_rb
, &sd
->s_parent
->s_dir
.children
);
109 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
110 * @sd: sysfs_dirent of interest
112 * Unlink @sd from its sibling rbtree which starts from
113 * sd->s_parent->s_dir.children.
116 * mutex_lock(sysfs_mutex)
118 static void sysfs_unlink_sibling(struct sysfs_dirent
*sd
)
120 if (sysfs_type(sd
) == SYSFS_DIR
)
121 sd
->s_parent
->s_dir
.subdirs
--;
123 rb_erase(&sd
->s_rb
, &sd
->s_parent
->s_dir
.children
);
127 * sysfs_get_active - get an active reference to sysfs_dirent
128 * @sd: sysfs_dirent to get an active reference to
130 * Get an active reference of @sd. This function is noop if @sd
134 * Pointer to @sd on success, NULL on failure.
136 struct sysfs_dirent
*sysfs_get_active(struct sysfs_dirent
*sd
)
141 if (!atomic_inc_unless_negative(&sd
->s_active
))
144 if (sd
->s_flags
& SYSFS_FLAG_LOCKDEP
)
145 rwsem_acquire_read(&sd
->dep_map
, 0, 1, _RET_IP_
);
150 * sysfs_put_active - put an active reference to sysfs_dirent
151 * @sd: sysfs_dirent to put an active reference to
153 * Put an active reference to @sd. This function is noop if @sd
156 void sysfs_put_active(struct sysfs_dirent
*sd
)
163 if (sd
->s_flags
& SYSFS_FLAG_LOCKDEP
)
164 rwsem_release(&sd
->dep_map
, 1, _RET_IP_
);
165 v
= atomic_dec_return(&sd
->s_active
);
166 if (likely(v
!= SD_DEACTIVATED_BIAS
))
169 /* atomic_dec_return() is a mb(), we'll always see the updated
172 complete(sd
->u
.completion
);
176 * sysfs_deactivate - deactivate sysfs_dirent
177 * @sd: sysfs_dirent to deactivate
179 * Deny new active references and drain existing ones.
181 static void sysfs_deactivate(struct sysfs_dirent
*sd
)
183 DECLARE_COMPLETION_ONSTACK(wait
);
186 BUG_ON(!(sd
->s_flags
& SYSFS_FLAG_REMOVED
));
188 if (!(sysfs_type(sd
) & SYSFS_ACTIVE_REF
))
191 sd
->u
.completion
= (void *)&wait
;
193 rwsem_acquire(&sd
->dep_map
, 0, 0, _RET_IP_
);
194 /* atomic_add_return() is a mb(), put_active() will always see
195 * the updated sd->u.completion.
197 v
= atomic_add_return(SD_DEACTIVATED_BIAS
, &sd
->s_active
);
199 if (v
!= SD_DEACTIVATED_BIAS
) {
200 lock_contended(&sd
->dep_map
, _RET_IP_
);
201 wait_for_completion(&wait
);
204 lock_acquired(&sd
->dep_map
, _RET_IP_
);
205 rwsem_release(&sd
->dep_map
, 1, _RET_IP_
);
208 static int sysfs_alloc_ino(unsigned int *pino
)
213 spin_lock(&sysfs_ino_lock
);
214 rc
= ida_get_new_above(&sysfs_ino_ida
, 1, &ino
);
215 spin_unlock(&sysfs_ino_lock
);
218 if (ida_pre_get(&sysfs_ino_ida
, GFP_KERNEL
))
227 static void sysfs_free_ino(unsigned int ino
)
229 spin_lock(&sysfs_ino_lock
);
230 ida_remove(&sysfs_ino_ida
, ino
);
231 spin_unlock(&sysfs_ino_lock
);
235 * kernfs_get - get a reference count on a sysfs_dirent
236 * @sd: the target sysfs_dirent
238 void kernfs_get(struct sysfs_dirent
*sd
)
241 WARN_ON(!atomic_read(&sd
->s_count
));
242 atomic_inc(&sd
->s_count
);
245 EXPORT_SYMBOL_GPL(kernfs_get
);
248 * kernfs_put - put a reference count on a sysfs_dirent
249 * @sd: the target sysfs_dirent
251 * Put a reference count of @sd and destroy it if it reached zero.
253 void kernfs_put(struct sysfs_dirent
*sd
)
255 struct sysfs_dirent
*parent_sd
;
256 struct kernfs_root
*root
;
258 if (!sd
|| !atomic_dec_and_test(&sd
->s_count
))
260 root
= kernfs_root(sd
);
262 /* Moving/renaming is always done while holding reference.
263 * sd->s_parent won't change beneath us.
265 parent_sd
= sd
->s_parent
;
267 WARN(!(sd
->s_flags
& SYSFS_FLAG_REMOVED
),
268 "sysfs: free using entry: %s/%s\n",
269 parent_sd
? parent_sd
->s_name
: "", sd
->s_name
);
271 if (sysfs_type(sd
) == SYSFS_KOBJ_LINK
)
272 kernfs_put(sd
->s_symlink
.target_sd
);
273 if (sysfs_type(sd
) & SYSFS_COPY_NAME
)
275 if (sd
->s_iattr
&& sd
->s_iattr
->ia_secdata
)
276 security_release_secctx(sd
->s_iattr
->ia_secdata
,
277 sd
->s_iattr
->ia_secdata_len
);
279 sysfs_free_ino(sd
->s_ino
);
280 kmem_cache_free(sysfs_dir_cachep
, sd
);
284 if (atomic_dec_and_test(&sd
->s_count
))
287 /* just released the root sd, free @root too */
291 EXPORT_SYMBOL_GPL(kernfs_put
);
293 static int sysfs_dentry_delete(const struct dentry
*dentry
)
295 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
296 return !(sd
&& !(sd
->s_flags
& SYSFS_FLAG_REMOVED
));
299 static int sysfs_dentry_revalidate(struct dentry
*dentry
, unsigned int flags
)
301 struct sysfs_dirent
*sd
;
303 if (flags
& LOOKUP_RCU
)
306 sd
= dentry
->d_fsdata
;
307 mutex_lock(&sysfs_mutex
);
309 /* The sysfs dirent has been deleted */
310 if (sd
->s_flags
& SYSFS_FLAG_REMOVED
)
313 /* The sysfs dirent has been moved? */
314 if (dentry
->d_parent
->d_fsdata
!= sd
->s_parent
)
317 /* The sysfs dirent has been renamed */
318 if (strcmp(dentry
->d_name
.name
, sd
->s_name
) != 0)
321 /* The sysfs dirent has been moved to a different namespace */
322 if (sd
->s_parent
&& (sd
->s_parent
->s_flags
& SYSFS_FLAG_NS
) &&
323 sysfs_info(dentry
->d_sb
)->ns
!= sd
->s_ns
)
326 mutex_unlock(&sysfs_mutex
);
330 /* Remove the dentry from the dcache hashes.
331 * If this is a deleted dentry we use d_drop instead of d_delete
332 * so sysfs doesn't need to cope with negative dentries.
334 * If this is a dentry that has simply been renamed we
335 * use d_drop to remove it from the dcache lookup on its
336 * old parent. If this dentry persists later when a lookup
337 * is performed at its new name the dentry will be readded
338 * to the dcache hashes.
340 mutex_unlock(&sysfs_mutex
);
342 /* If we have submounts we must allow the vfs caches
343 * to lie about the state of the filesystem to prevent
344 * leaks and other nasty things.
346 if (check_submounts_and_drop(dentry
) != 0)
352 static void sysfs_dentry_release(struct dentry
*dentry
)
354 kernfs_put(dentry
->d_fsdata
);
357 const struct dentry_operations sysfs_dentry_ops
= {
358 .d_revalidate
= sysfs_dentry_revalidate
,
359 .d_delete
= sysfs_dentry_delete
,
360 .d_release
= sysfs_dentry_release
,
363 struct sysfs_dirent
*sysfs_new_dirent(const char *name
, umode_t mode
, int type
)
365 char *dup_name
= NULL
;
366 struct sysfs_dirent
*sd
;
368 if (type
& SYSFS_COPY_NAME
) {
369 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
374 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
378 if (sysfs_alloc_ino(&sd
->s_ino
))
381 atomic_set(&sd
->s_count
, 1);
382 atomic_set(&sd
->s_active
, 0);
386 sd
->s_flags
= type
| SYSFS_FLAG_REMOVED
;
391 kmem_cache_free(sysfs_dir_cachep
, sd
);
398 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
399 * @acxt: pointer to sysfs_addrm_cxt to be used
401 * This function is called when the caller is about to add or remove
402 * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used
403 * to keep and pass context to other addrm functions.
406 * Kernel thread context (may sleep). sysfs_mutex is locked on
409 void sysfs_addrm_start(struct sysfs_addrm_cxt
*acxt
)
410 __acquires(sysfs_mutex
)
412 memset(acxt
, 0, sizeof(*acxt
));
414 mutex_lock(&sysfs_mutex
);
418 * sysfs_add_one - add sysfs_dirent to parent without warning
419 * @acxt: addrm context to use
420 * @sd: sysfs_dirent to be added
421 * @parent_sd: the parent sysfs_dirent to add @sd to
423 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
424 * the parent inode if @sd is a directory and link into the children
425 * list of the parent.
427 * This function should be called between calls to
428 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
429 * passed the same @acxt as passed to sysfs_addrm_start().
432 * Determined by sysfs_addrm_start().
435 * 0 on success, -EEXIST if entry with the given name already
438 int sysfs_add_one(struct sysfs_addrm_cxt
*acxt
, struct sysfs_dirent
*sd
,
439 struct sysfs_dirent
*parent_sd
)
441 bool has_ns
= parent_sd
->s_flags
& SYSFS_FLAG_NS
;
442 struct sysfs_inode_attrs
*ps_iattr
;
445 if (has_ns
!= (bool)sd
->s_ns
) {
446 WARN(1, KERN_WARNING
"sysfs: ns %s in '%s' for '%s'\n",
447 has_ns
? "required" : "invalid",
448 parent_sd
->s_name
, sd
->s_name
);
452 if (sysfs_type(parent_sd
) != SYSFS_DIR
)
455 sd
->s_hash
= sysfs_name_hash(sd
->s_name
, sd
->s_ns
);
456 sd
->s_parent
= parent_sd
;
457 kernfs_get(parent_sd
);
459 ret
= sysfs_link_sibling(sd
);
463 /* Update timestamps on the parent */
464 ps_iattr
= parent_sd
->s_iattr
;
466 struct iattr
*ps_iattrs
= &ps_iattr
->ia_iattr
;
467 ps_iattrs
->ia_ctime
= ps_iattrs
->ia_mtime
= CURRENT_TIME
;
470 /* Mark the entry added into directory tree */
471 sd
->s_flags
&= ~SYSFS_FLAG_REMOVED
;
477 * sysfs_remove_one - remove sysfs_dirent from parent
478 * @acxt: addrm context to use
479 * @sd: sysfs_dirent to be removed
481 * Mark @sd removed and drop nlink of parent inode if @sd is a
482 * directory. @sd is unlinked from the children list.
484 * This function should be called between calls to
485 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
486 * passed the same @acxt as passed to sysfs_addrm_start().
489 * Determined by sysfs_addrm_start().
491 static void sysfs_remove_one(struct sysfs_addrm_cxt
*acxt
,
492 struct sysfs_dirent
*sd
)
494 struct sysfs_inode_attrs
*ps_iattr
;
497 * Removal can be called multiple times on the same node. Only the
498 * first invocation is effective and puts the base ref.
500 if (sd
->s_flags
& SYSFS_FLAG_REMOVED
)
504 sysfs_unlink_sibling(sd
);
506 /* Update timestamps on the parent */
507 ps_iattr
= sd
->s_parent
->s_iattr
;
509 ps_iattr
->ia_iattr
.ia_ctime
= CURRENT_TIME
;
510 ps_iattr
->ia_iattr
.ia_mtime
= CURRENT_TIME
;
514 sd
->s_flags
|= SYSFS_FLAG_REMOVED
;
515 sd
->u
.removed_list
= acxt
->removed
;
520 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
521 * @acxt: addrm context to finish up
523 * Finish up sysfs_dirent add/remove. Resources acquired by
524 * sysfs_addrm_start() are released and removed sysfs_dirents are
528 * sysfs_mutex is released.
530 void sysfs_addrm_finish(struct sysfs_addrm_cxt
*acxt
)
531 __releases(sysfs_mutex
)
533 /* release resources acquired by sysfs_addrm_start() */
534 mutex_unlock(&sysfs_mutex
);
536 /* kill removed sysfs_dirents */
537 while (acxt
->removed
) {
538 struct sysfs_dirent
*sd
= acxt
->removed
;
540 acxt
->removed
= sd
->u
.removed_list
;
542 sysfs_deactivate(sd
);
543 sysfs_unmap_bin_file(sd
);
549 * kernfs_find_ns - find sysfs_dirent with the given name
550 * @parent: sysfs_dirent to search under
551 * @name: name to look for
552 * @ns: the namespace tag to use
554 * Look for sysfs_dirent with name @name under @parent. Returns pointer to
555 * the found sysfs_dirent on success, %NULL on failure.
557 static struct sysfs_dirent
*kernfs_find_ns(struct sysfs_dirent
*parent
,
558 const unsigned char *name
,
561 struct rb_node
*node
= parent
->s_dir
.children
.rb_node
;
562 bool has_ns
= parent
->s_flags
& SYSFS_FLAG_NS
;
565 lockdep_assert_held(&sysfs_mutex
);
567 if (has_ns
!= (bool)ns
) {
568 WARN(1, KERN_WARNING
"sysfs: ns %s in '%s' for '%s'\n",
569 has_ns
? "required" : "invalid",
570 parent
->s_name
, name
);
574 hash
= sysfs_name_hash(name
, ns
);
576 struct sysfs_dirent
*sd
;
579 sd
= to_sysfs_dirent(node
);
580 result
= sysfs_name_compare(hash
, name
, ns
, sd
);
582 node
= node
->rb_left
;
584 node
= node
->rb_right
;
592 * kernfs_find_and_get_ns - find and get sysfs_dirent with the given name
593 * @parent: sysfs_dirent to search under
594 * @name: name to look for
595 * @ns: the namespace tag to use
597 * Look for sysfs_dirent with name @name under @parent and get a reference
598 * if found. This function may sleep and returns pointer to the found
599 * sysfs_dirent on success, %NULL on failure.
601 struct sysfs_dirent
*kernfs_find_and_get_ns(struct sysfs_dirent
*parent
,
602 const char *name
, const void *ns
)
604 struct sysfs_dirent
*sd
;
606 mutex_lock(&sysfs_mutex
);
607 sd
= kernfs_find_ns(parent
, name
, ns
);
609 mutex_unlock(&sysfs_mutex
);
613 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns
);
616 * kernfs_create_root - create a new kernfs hierarchy
617 * @priv: opaque data associated with the new directory
619 * Returns the root of the new hierarchy on success, ERR_PTR() value on
622 struct kernfs_root
*kernfs_create_root(void *priv
)
624 struct kernfs_root
*root
;
625 struct sysfs_dirent
*sd
;
627 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
629 return ERR_PTR(-ENOMEM
);
631 sd
= sysfs_new_dirent("", S_IFDIR
| S_IRUGO
| S_IXUGO
, SYSFS_DIR
);
634 return ERR_PTR(-ENOMEM
);
637 sd
->s_flags
&= ~SYSFS_FLAG_REMOVED
;
639 sd
->s_dir
.root
= root
;
647 * kernfs_destroy_root - destroy a kernfs hierarchy
648 * @root: root of the hierarchy to destroy
650 * Destroy the hierarchy anchored at @root by removing all existing
651 * directories and destroying @root.
653 void kernfs_destroy_root(struct kernfs_root
*root
)
655 kernfs_remove(root
->sd
); /* will also free @root */
659 * kernfs_create_dir_ns - create a directory
660 * @parent: parent in which to create a new directory
661 * @name: name of the new directory
662 * @priv: opaque data associated with the new directory
663 * @ns: optional namespace tag of the directory
665 * Returns the created node on success, ERR_PTR() value on failure.
667 struct sysfs_dirent
*kernfs_create_dir_ns(struct sysfs_dirent
*parent
,
668 const char *name
, void *priv
,
671 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
672 struct sysfs_addrm_cxt acxt
;
673 struct sysfs_dirent
*sd
;
677 sd
= sysfs_new_dirent(name
, mode
, SYSFS_DIR
);
679 return ERR_PTR(-ENOMEM
);
681 sd
->s_dir
.root
= parent
->s_dir
.root
;
686 sysfs_addrm_start(&acxt
);
687 rc
= sysfs_add_one(&acxt
, sd
, parent
);
688 sysfs_addrm_finish(&acxt
);
697 static struct dentry
*sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
700 struct dentry
*ret
= NULL
;
701 struct dentry
*parent
= dentry
->d_parent
;
702 struct sysfs_dirent
*parent_sd
= parent
->d_fsdata
;
703 struct sysfs_dirent
*sd
;
705 const void *ns
= NULL
;
707 mutex_lock(&sysfs_mutex
);
709 if (parent_sd
->s_flags
& SYSFS_FLAG_NS
)
710 ns
= sysfs_info(dir
->i_sb
)->ns
;
712 sd
= kernfs_find_ns(parent_sd
, dentry
->d_name
.name
, ns
);
716 ret
= ERR_PTR(-ENOENT
);
720 dentry
->d_fsdata
= sd
;
722 /* attach dentry and inode */
723 inode
= sysfs_get_inode(dir
->i_sb
, sd
);
725 ret
= ERR_PTR(-ENOMEM
);
729 /* instantiate and hash dentry */
730 ret
= d_materialise_unique(dentry
, inode
);
732 mutex_unlock(&sysfs_mutex
);
736 const struct inode_operations sysfs_dir_inode_operations
= {
737 .lookup
= sysfs_lookup
,
738 .permission
= sysfs_permission
,
739 .setattr
= sysfs_setattr
,
740 .getattr
= sysfs_getattr
,
741 .setxattr
= sysfs_setxattr
,
744 static struct sysfs_dirent
*sysfs_leftmost_descendant(struct sysfs_dirent
*pos
)
746 struct sysfs_dirent
*last
;
753 if (sysfs_type(pos
) != SYSFS_DIR
)
756 rbn
= rb_first(&pos
->s_dir
.children
);
760 pos
= to_sysfs_dirent(rbn
);
767 * sysfs_next_descendant_post - find the next descendant for post-order walk
768 * @pos: the current position (%NULL to initiate traversal)
769 * @root: sysfs_dirent whose descendants to walk
771 * Find the next descendant to visit for post-order traversal of @root's
772 * descendants. @root is included in the iteration and the last node to be
775 static struct sysfs_dirent
*sysfs_next_descendant_post(struct sysfs_dirent
*pos
,
776 struct sysfs_dirent
*root
)
780 lockdep_assert_held(&sysfs_mutex
);
782 /* if first iteration, visit leftmost descendant which may be root */
784 return sysfs_leftmost_descendant(root
);
786 /* if we visited @root, we're done */
790 /* if there's an unvisited sibling, visit its leftmost descendant */
791 rbn
= rb_next(&pos
->s_rb
);
793 return sysfs_leftmost_descendant(to_sysfs_dirent(rbn
));
795 /* no sibling left, visit parent */
796 return pos
->s_parent
;
799 static void __kernfs_remove(struct sysfs_addrm_cxt
*acxt
,
800 struct sysfs_dirent
*sd
)
802 struct sysfs_dirent
*pos
, *next
;
807 pr_debug("sysfs %s: removing\n", sd
->s_name
);
812 next
= sysfs_next_descendant_post(pos
, sd
);
814 sysfs_remove_one(acxt
, pos
);
819 * kernfs_remove - remove a sysfs_dirent recursively
820 * @sd: the sysfs_dirent to remove
822 * Remove @sd along with all its subdirectories and files.
824 void kernfs_remove(struct sysfs_dirent
*sd
)
826 struct sysfs_addrm_cxt acxt
;
828 sysfs_addrm_start(&acxt
);
829 __kernfs_remove(&acxt
, sd
);
830 sysfs_addrm_finish(&acxt
);
834 * kernfs_remove_by_name_ns - find a sysfs_dirent by name and remove it
835 * @dir_sd: parent of the target
836 * @name: name of the sysfs_dirent to remove
837 * @ns: namespace tag of the sysfs_dirent to remove
839 * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove
840 * it. Returns 0 on success, -ENOENT if such entry doesn't exist.
842 int kernfs_remove_by_name_ns(struct sysfs_dirent
*dir_sd
, const char *name
,
845 struct sysfs_addrm_cxt acxt
;
846 struct sysfs_dirent
*sd
;
849 WARN(1, KERN_WARNING
"sysfs: can not remove '%s', no directory\n",
854 sysfs_addrm_start(&acxt
);
856 sd
= kernfs_find_ns(dir_sd
, name
, ns
);
858 __kernfs_remove(&acxt
, sd
);
860 sysfs_addrm_finish(&acxt
);
869 * kernfs_rename_ns - move and rename a kernfs_node
871 * @new_parent: new parent to put @sd under
872 * @new_name: new name
873 * @new_ns: new namespace tag
875 int kernfs_rename_ns(struct sysfs_dirent
*sd
, struct sysfs_dirent
*new_parent
,
876 const char *new_name
, const void *new_ns
)
880 mutex_lock(&sysfs_mutex
);
883 if ((sd
->s_parent
== new_parent
) && (sd
->s_ns
== new_ns
) &&
884 (strcmp(sd
->s_name
, new_name
) == 0))
885 goto out
; /* nothing to rename */
888 if (kernfs_find_ns(new_parent
, new_name
, new_ns
))
891 /* rename sysfs_dirent */
892 if (strcmp(sd
->s_name
, new_name
) != 0) {
894 new_name
= kstrdup(new_name
, GFP_KERNEL
);
899 sd
->s_name
= new_name
;
903 * Move to the appropriate place in the appropriate directories rbtree.
905 sysfs_unlink_sibling(sd
);
906 kernfs_get(new_parent
);
907 kernfs_put(sd
->s_parent
);
909 sd
->s_hash
= sysfs_name_hash(sd
->s_name
, sd
->s_ns
);
910 sd
->s_parent
= new_parent
;
911 sysfs_link_sibling(sd
);
915 mutex_unlock(&sysfs_mutex
);
920 * kernfs_enable_ns - enable namespace under a directory
921 * @sd: directory of interest, should be empty
923 * This is to be called right after @sd is created to enable namespace
924 * under it. All children of @sd must have non-NULL namespace tags and
925 * only the ones which match the super_block's tag will be visible.
927 void kernfs_enable_ns(struct sysfs_dirent
*sd
)
929 WARN_ON_ONCE(sysfs_type(sd
) != SYSFS_DIR
);
930 WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd
->s_dir
.children
));
931 sd
->s_flags
|= SYSFS_FLAG_NS
;
934 /* Relationship between s_mode and the DT_xxx types */
935 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
937 return (sd
->s_mode
>> 12) & 15;
940 static int sysfs_dir_release(struct inode
*inode
, struct file
*filp
)
942 kernfs_put(filp
->private_data
);
946 static struct sysfs_dirent
*sysfs_dir_pos(const void *ns
,
947 struct sysfs_dirent
*parent_sd
, loff_t hash
, struct sysfs_dirent
*pos
)
950 int valid
= !(pos
->s_flags
& SYSFS_FLAG_REMOVED
) &&
951 pos
->s_parent
== parent_sd
&&
957 if (!pos
&& (hash
> 1) && (hash
< INT_MAX
)) {
958 struct rb_node
*node
= parent_sd
->s_dir
.children
.rb_node
;
960 pos
= to_sysfs_dirent(node
);
962 if (hash
< pos
->s_hash
)
963 node
= node
->rb_left
;
964 else if (hash
> pos
->s_hash
)
965 node
= node
->rb_right
;
970 /* Skip over entries in the wrong namespace */
971 while (pos
&& pos
->s_ns
!= ns
) {
972 struct rb_node
*node
= rb_next(&pos
->s_rb
);
976 pos
= to_sysfs_dirent(node
);
981 static struct sysfs_dirent
*sysfs_dir_next_pos(const void *ns
,
982 struct sysfs_dirent
*parent_sd
, ino_t ino
, struct sysfs_dirent
*pos
)
984 pos
= sysfs_dir_pos(ns
, parent_sd
, ino
, pos
);
987 struct rb_node
*node
= rb_next(&pos
->s_rb
);
991 pos
= to_sysfs_dirent(node
);
992 } while (pos
&& pos
->s_ns
!= ns
);
996 static int sysfs_readdir(struct file
*file
, struct dir_context
*ctx
)
998 struct dentry
*dentry
= file
->f_path
.dentry
;
999 struct sysfs_dirent
*parent_sd
= dentry
->d_fsdata
;
1000 struct sysfs_dirent
*pos
= file
->private_data
;
1001 const void *ns
= NULL
;
1003 if (!dir_emit_dots(file
, ctx
))
1005 mutex_lock(&sysfs_mutex
);
1007 if (parent_sd
->s_flags
& SYSFS_FLAG_NS
)
1008 ns
= sysfs_info(dentry
->d_sb
)->ns
;
1010 for (pos
= sysfs_dir_pos(ns
, parent_sd
, ctx
->pos
, pos
);
1012 pos
= sysfs_dir_next_pos(ns
, parent_sd
, ctx
->pos
, pos
)) {
1013 const char *name
= pos
->s_name
;
1014 unsigned int type
= dt_type(pos
);
1015 int len
= strlen(name
);
1016 ino_t ino
= pos
->s_ino
;
1018 ctx
->pos
= pos
->s_hash
;
1019 file
->private_data
= pos
;
1022 mutex_unlock(&sysfs_mutex
);
1023 if (!dir_emit(ctx
, name
, len
, ino
, type
))
1025 mutex_lock(&sysfs_mutex
);
1027 mutex_unlock(&sysfs_mutex
);
1028 file
->private_data
= NULL
;
1033 static loff_t
sysfs_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
1035 struct inode
*inode
= file_inode(file
);
1038 mutex_lock(&inode
->i_mutex
);
1039 ret
= generic_file_llseek(file
, offset
, whence
);
1040 mutex_unlock(&inode
->i_mutex
);
1045 const struct file_operations sysfs_dir_operations
= {
1046 .read
= generic_read_dir
,
1047 .iterate
= sysfs_readdir
,
1048 .release
= sysfs_dir_release
,
1049 .llseek
= sysfs_dir_llseek
,