2 * fs/sysfs/dir.c - sysfs core and dir operation implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
16 #include <linux/mount.h>
17 #include <linux/module.h>
18 #include <linux/kobject.h>
19 #include <linux/namei.h>
20 #include <linux/idr.h>
21 #include <linux/completion.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/security.h>
25 #include <linux/hash.h>
28 DEFINE_MUTEX(sysfs_mutex);
29 DEFINE_SPINLOCK(sysfs_assoc_lock);
31 #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
33 static DEFINE_SPINLOCK(sysfs_ino_lock);
34 static DEFINE_IDA(sysfs_ino_ida);
38 * @name: Null terminated string to hash
39 * @ns: Namespace tag to hash
41 * Returns 31 bit hash of ns + name (so it fits in an off_t )
43 static unsigned int sysfs_name_hash(const char *name, const void *ns)
45 unsigned long hash = init_name_hash();
46 unsigned int len = strlen(name);
48 hash = partial_name_hash(*name++, hash);
49 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
51 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
59 static int sysfs_name_compare(unsigned int hash, const char *name,
60 const void *ns, const struct sysfs_dirent *sd)
62 if (hash != sd->s_hash)
63 return hash - sd->s_hash;
66 return strcmp(name, sd->s_name);
69 static int sysfs_sd_compare(const struct sysfs_dirent *left,
70 const struct sysfs_dirent *right)
72 return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns,
77 * sysfs_link_sibling - link sysfs_dirent into sibling rbtree
78 * @sd: sysfs_dirent of interest
80 * Link @sd into its sibling rbtree which starts from
81 * sd->s_parent->s_dir.children.
84 * mutex_lock(sysfs_mutex)
87 * 0 on susccess -EEXIST on failure.
89 static int sysfs_link_sibling(struct sysfs_dirent *sd)
91 struct rb_node **node = &sd->s_parent->s_dir.children.rb_node;
92 struct rb_node *parent = NULL;
94 if (sysfs_type(sd) == SYSFS_DIR)
95 sd->s_parent->s_dir.subdirs++;
98 struct sysfs_dirent *pos;
101 pos = to_sysfs_dirent(*node);
103 result = sysfs_sd_compare(sd, pos);
105 node = &pos->s_rb.rb_left;
107 node = &pos->s_rb.rb_right;
111 /* add new node and rebalance the tree */
112 rb_link_node(&sd->s_rb, parent, node);
113 rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
115 /* if @sd has ns tag, mark the parent to enable ns filtering */
117 sd->s_parent->s_flags |= SYSFS_FLAG_HAS_NS;
123 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
124 * @sd: sysfs_dirent of interest
126 * Unlink @sd from its sibling rbtree which starts from
127 * sd->s_parent->s_dir.children.
130 * mutex_lock(sysfs_mutex)
132 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
134 if (sysfs_type(sd) == SYSFS_DIR)
135 sd->s_parent->s_dir.subdirs--;
137 rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
140 * Either all or none of the children have tags. Clearing HAS_NS
141 * when there's no child left is enough to keep the flag synced.
143 if (RB_EMPTY_ROOT(&sd->s_parent->s_dir.children))
144 sd->s_parent->s_flags &= ~SYSFS_FLAG_HAS_NS;
148 * sysfs_get_active - get an active reference to sysfs_dirent
149 * @sd: sysfs_dirent to get an active reference to
151 * Get an active reference of @sd. This function is noop if @sd
155 * Pointer to @sd on success, NULL on failure.
157 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
162 if (!atomic_inc_unless_negative(&sd->s_active))
165 if (likely(!sysfs_ignore_lockdep(sd)))
166 rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
171 * sysfs_put_active - put an active reference to sysfs_dirent
172 * @sd: sysfs_dirent to put an active reference to
174 * Put an active reference to @sd. This function is noop if @sd
177 void sysfs_put_active(struct sysfs_dirent *sd)
184 if (likely(!sysfs_ignore_lockdep(sd)))
185 rwsem_release(&sd->dep_map, 1, _RET_IP_);
186 v = atomic_dec_return(&sd->s_active);
187 if (likely(v != SD_DEACTIVATED_BIAS))
190 /* atomic_dec_return() is a mb(), we'll always see the updated
193 complete(sd->u.completion);
197 * sysfs_deactivate - deactivate sysfs_dirent
198 * @sd: sysfs_dirent to deactivate
200 * Deny new active references and drain existing ones.
202 static void sysfs_deactivate(struct sysfs_dirent *sd)
204 DECLARE_COMPLETION_ONSTACK(wait);
207 BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
209 if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
212 sd->u.completion = (void *)&wait;
214 rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
215 /* atomic_add_return() is a mb(), put_active() will always see
216 * the updated sd->u.completion.
218 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
220 if (v != SD_DEACTIVATED_BIAS) {
221 lock_contended(&sd->dep_map, _RET_IP_);
222 wait_for_completion(&wait);
225 lock_acquired(&sd->dep_map, _RET_IP_);
226 rwsem_release(&sd->dep_map, 1, _RET_IP_);
229 static int sysfs_alloc_ino(unsigned int *pino)
234 spin_lock(&sysfs_ino_lock);
235 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
236 spin_unlock(&sysfs_ino_lock);
239 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
248 static void sysfs_free_ino(unsigned int ino)
250 spin_lock(&sysfs_ino_lock);
251 ida_remove(&sysfs_ino_ida, ino);
252 spin_unlock(&sysfs_ino_lock);
255 void release_sysfs_dirent(struct sysfs_dirent *sd)
257 struct sysfs_dirent *parent_sd;
260 /* Moving/renaming is always done while holding reference.
261 * sd->s_parent won't change beneath us.
263 parent_sd = sd->s_parent;
265 WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
266 "sysfs: free using entry: %s/%s\n",
267 parent_sd ? parent_sd->s_name : "", sd->s_name);
269 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
270 sysfs_put(sd->s_symlink.target_sd);
271 if (sysfs_type(sd) & SYSFS_COPY_NAME)
273 if (sd->s_iattr && sd->s_iattr->ia_secdata)
274 security_release_secctx(sd->s_iattr->ia_secdata,
275 sd->s_iattr->ia_secdata_len);
277 sysfs_free_ino(sd->s_ino);
278 kmem_cache_free(sysfs_dir_cachep, sd);
281 if (sd && atomic_dec_and_test(&sd->s_count))
285 static int sysfs_dentry_delete(const struct dentry *dentry)
287 struct sysfs_dirent *sd = dentry->d_fsdata;
288 return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED));
291 static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
293 struct sysfs_dirent *sd;
295 if (flags & LOOKUP_RCU)
298 sd = dentry->d_fsdata;
299 mutex_lock(&sysfs_mutex);
301 /* The sysfs dirent has been deleted */
302 if (sd->s_flags & SYSFS_FLAG_REMOVED)
305 /* The sysfs dirent has been moved? */
306 if (dentry->d_parent->d_fsdata != sd->s_parent)
309 /* The sysfs dirent has been renamed */
310 if (strcmp(dentry->d_name.name, sd->s_name) != 0)
313 /* The sysfs dirent has been moved to a different namespace */
314 if (sd->s_ns && sd->s_ns != sysfs_info(dentry->d_sb)->ns)
317 mutex_unlock(&sysfs_mutex);
321 /* Remove the dentry from the dcache hashes.
322 * If this is a deleted dentry we use d_drop instead of d_delete
323 * so sysfs doesn't need to cope with negative dentries.
325 * If this is a dentry that has simply been renamed we
326 * use d_drop to remove it from the dcache lookup on its
327 * old parent. If this dentry persists later when a lookup
328 * is performed at its new name the dentry will be readded
329 * to the dcache hashes.
331 mutex_unlock(&sysfs_mutex);
333 /* If we have submounts we must allow the vfs caches
334 * to lie about the state of the filesystem to prevent
335 * leaks and other nasty things.
337 if (check_submounts_and_drop(dentry) != 0)
343 static void sysfs_dentry_release(struct dentry *dentry)
345 sysfs_put(dentry->d_fsdata);
348 const struct dentry_operations sysfs_dentry_ops = {
349 .d_revalidate = sysfs_dentry_revalidate,
350 .d_delete = sysfs_dentry_delete,
351 .d_release = sysfs_dentry_release,
354 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
356 char *dup_name = NULL;
357 struct sysfs_dirent *sd;
359 if (type & SYSFS_COPY_NAME) {
360 name = dup_name = kstrdup(name, GFP_KERNEL);
365 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
369 if (sysfs_alloc_ino(&sd->s_ino))
372 atomic_set(&sd->s_count, 1);
373 atomic_set(&sd->s_active, 0);
377 sd->s_flags = type | SYSFS_FLAG_REMOVED;
382 kmem_cache_free(sysfs_dir_cachep, sd);
389 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
390 * @acxt: pointer to sysfs_addrm_cxt to be used
392 * This function is called when the caller is about to add or remove
393 * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used
394 * to keep and pass context to other addrm functions.
397 * Kernel thread context (may sleep). sysfs_mutex is locked on
400 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
401 __acquires(sysfs_mutex)
403 memset(acxt, 0, sizeof(*acxt));
405 mutex_lock(&sysfs_mutex);
409 * __sysfs_add_one - add sysfs_dirent to parent without warning
410 * @acxt: addrm context to use
411 * @sd: sysfs_dirent to be added
412 * @parent_sd: the parent sysfs_dirent to add @sd to
414 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
415 * the parent inode if @sd is a directory and link into the children
416 * list of the parent.
418 * This function should be called between calls to
419 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
420 * passed the same @acxt as passed to sysfs_addrm_start().
423 * Determined by sysfs_addrm_start().
426 * 0 on success, -EEXIST if entry with the given name already
429 int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
430 struct sysfs_dirent *parent_sd)
432 struct sysfs_inode_attrs *ps_iattr;
435 sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
436 sd->s_parent = sysfs_get(parent_sd);
438 ret = sysfs_link_sibling(sd);
442 /* Update timestamps on the parent */
443 ps_iattr = parent_sd->s_iattr;
445 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
446 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
449 /* Mark the entry added into directory tree */
450 sd->s_flags &= ~SYSFS_FLAG_REMOVED;
456 * sysfs_pathname - return full path to sysfs dirent
457 * @sd: sysfs_dirent whose path we want
458 * @path: caller allocated buffer of size PATH_MAX
460 * Gives the name "/" to the sysfs_root entry; any path returned
461 * is relative to wherever sysfs is mounted.
463 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
466 sysfs_pathname(sd->s_parent, path);
467 strlcat(path, "/", PATH_MAX);
469 strlcat(path, sd->s_name, PATH_MAX);
474 * sysfs_add_one - add sysfs_dirent to parent
475 * @acxt: addrm context to use
476 * @sd: sysfs_dirent to be added
477 * @parent_sd: the parent sysfs_dirent to add @sd to
479 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
480 * the parent inode if @sd is a directory and link into the children
481 * list of the parent.
483 * This function should be called between calls to
484 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
485 * passed the same @acxt as passed to sysfs_addrm_start().
488 * Determined by sysfs_addrm_start().
491 * 0 on success, -EEXIST if entry with the given name already
494 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
495 struct sysfs_dirent *parent_sd)
499 ret = __sysfs_add_one(acxt, sd, parent_sd);
500 if (ret == -EEXIST) {
501 char *path = kzalloc(PATH_MAX, GFP_KERNEL);
503 "sysfs: cannot create duplicate filename '%s'\n",
504 (path == NULL) ? sd->s_name
505 : (sysfs_pathname(parent_sd, path),
506 strlcat(path, "/", PATH_MAX),
507 strlcat(path, sd->s_name, PATH_MAX),
516 * sysfs_remove_one - remove sysfs_dirent from parent
517 * @acxt: addrm context to use
518 * @sd: sysfs_dirent to be removed
520 * Mark @sd removed and drop nlink of parent inode if @sd is a
521 * directory. @sd is unlinked from the children list.
523 * This function should be called between calls to
524 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
525 * passed the same @acxt as passed to sysfs_addrm_start().
528 * Determined by sysfs_addrm_start().
530 static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
531 struct sysfs_dirent *sd)
533 struct sysfs_inode_attrs *ps_iattr;
536 * Removal can be called multiple times on the same node. Only the
537 * first invocation is effective and puts the base ref.
539 if (sd->s_flags & SYSFS_FLAG_REMOVED)
542 sysfs_unlink_sibling(sd);
544 /* Update timestamps on the parent */
545 ps_iattr = sd->s_parent->s_iattr;
547 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
548 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
551 sd->s_flags |= SYSFS_FLAG_REMOVED;
552 sd->u.removed_list = acxt->removed;
557 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
558 * @acxt: addrm context to finish up
560 * Finish up sysfs_dirent add/remove. Resources acquired by
561 * sysfs_addrm_start() are released and removed sysfs_dirents are
565 * sysfs_mutex is released.
567 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
568 __releases(sysfs_mutex)
570 /* release resources acquired by sysfs_addrm_start() */
571 mutex_unlock(&sysfs_mutex);
573 /* kill removed sysfs_dirents */
574 while (acxt->removed) {
575 struct sysfs_dirent *sd = acxt->removed;
577 acxt->removed = sd->u.removed_list;
579 sysfs_deactivate(sd);
580 sysfs_unmap_bin_file(sd);
586 * sysfs_find_dirent - find sysfs_dirent with the given name
587 * @parent_sd: sysfs_dirent to search under
588 * @name: name to look for
589 * @ns: the namespace tag to use
591 * Look for sysfs_dirent with name @name under @parent_sd.
594 * mutex_lock(sysfs_mutex)
597 * Pointer to sysfs_dirent if found, NULL if not.
599 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
600 const unsigned char *name,
603 struct rb_node *node = parent_sd->s_dir.children.rb_node;
606 hash = sysfs_name_hash(name, ns);
608 struct sysfs_dirent *sd;
611 sd = to_sysfs_dirent(node);
612 result = sysfs_name_compare(hash, name, ns, sd);
614 node = node->rb_left;
616 node = node->rb_right;
624 * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
625 * @parent_sd: sysfs_dirent to search under
626 * @name: name to look for
627 * @ns: the namespace tag to use
629 * Look for sysfs_dirent with name @name under @parent_sd and get
633 * Kernel thread context (may sleep). Grabs sysfs_mutex.
636 * Pointer to sysfs_dirent if found, NULL if not.
638 struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
639 const unsigned char *name,
642 struct sysfs_dirent *sd;
644 mutex_lock(&sysfs_mutex);
645 sd = sysfs_find_dirent(parent_sd, name, ns);
647 mutex_unlock(&sysfs_mutex);
651 EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
653 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
654 const char *name, const void *ns,
655 struct sysfs_dirent **p_sd)
657 umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
658 struct sysfs_addrm_cxt acxt;
659 struct sysfs_dirent *sd;
663 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
668 sd->s_dir.kobj = kobj;
671 sysfs_addrm_start(&acxt);
672 rc = sysfs_add_one(&acxt, sd, parent_sd);
673 sysfs_addrm_finish(&acxt);
683 int sysfs_create_subdir(struct kobject *kobj, const char *name,
684 struct sysfs_dirent **p_sd)
686 return create_dir(kobj, kobj->sd, name, NULL, p_sd);
690 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
691 * @kobj: object we're creating directory for
692 * @ns: the namespace tag to use
694 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
696 struct sysfs_dirent *parent_sd, *sd;
702 parent_sd = kobj->parent->sd;
704 parent_sd = &sysfs_root;
709 error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
715 static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
718 struct dentry *ret = NULL;
719 struct dentry *parent = dentry->d_parent;
720 struct sysfs_dirent *parent_sd = parent->d_fsdata;
721 struct sysfs_dirent *sd;
723 const void *ns = NULL;
725 mutex_lock(&sysfs_mutex);
727 if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
728 ns = sysfs_info(dir->i_sb)->ns;
730 sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
734 ret = ERR_PTR(-ENOENT);
737 dentry->d_fsdata = sysfs_get(sd);
739 /* attach dentry and inode */
740 inode = sysfs_get_inode(dir->i_sb, sd);
742 ret = ERR_PTR(-ENOMEM);
746 /* instantiate and hash dentry */
747 ret = d_materialise_unique(dentry, inode);
749 mutex_unlock(&sysfs_mutex);
753 const struct inode_operations sysfs_dir_inode_operations = {
754 .lookup = sysfs_lookup,
755 .permission = sysfs_permission,
756 .setattr = sysfs_setattr,
757 .getattr = sysfs_getattr,
758 .setxattr = sysfs_setxattr,
761 static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
763 struct sysfs_dirent *last;
770 if (sysfs_type(pos) != SYSFS_DIR)
773 rbn = rb_first(&pos->s_dir.children);
777 pos = to_sysfs_dirent(rbn);
784 * sysfs_next_descendant_post - find the next descendant for post-order walk
785 * @pos: the current position (%NULL to initiate traversal)
786 * @root: sysfs_dirent whose descendants to walk
788 * Find the next descendant to visit for post-order traversal of @root's
789 * descendants. @root is included in the iteration and the last node to be
792 static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
793 struct sysfs_dirent *root)
797 lockdep_assert_held(&sysfs_mutex);
799 /* if first iteration, visit leftmost descendant which may be root */
801 return sysfs_leftmost_descendant(root);
803 /* if we visited @root, we're done */
807 /* if there's an unvisited sibling, visit its leftmost descendant */
808 rbn = rb_next(&pos->s_rb);
810 return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
812 /* no sibling left, visit parent */
813 return pos->s_parent;
816 void __sysfs_remove(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
818 struct sysfs_dirent *pos, *next;
823 pr_debug("sysfs %s: removing\n", sd->s_name);
828 next = sysfs_next_descendant_post(pos, sd);
830 sysfs_remove_one(acxt, pos);
835 * sysfs_remove - remove a sysfs_dirent recursively
836 * @sd: the sysfs_dirent to remove
838 * Remove @sd along with all its subdirectories and files.
840 void sysfs_remove(struct sysfs_dirent *sd)
842 struct sysfs_addrm_cxt acxt;
844 sysfs_addrm_start(&acxt);
845 __sysfs_remove(&acxt, sd);
846 sysfs_addrm_finish(&acxt);
850 * sysfs_remove_dir - remove an object's directory.
853 * The only thing special about this is that we remove any files in
854 * the directory before we remove the directory, and we've inlined
855 * what used to be sysfs_rmdir() below, instead of calling separately.
857 void sysfs_remove_dir(struct kobject *kobj)
859 struct sysfs_dirent *sd = kobj->sd;
861 spin_lock(&sysfs_assoc_lock);
863 spin_unlock(&sysfs_assoc_lock);
866 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
871 int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
872 const char *new_name, const void *new_ns)
876 mutex_lock(&sysfs_mutex);
879 if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
880 (strcmp(sd->s_name, new_name) == 0))
881 goto out; /* nothing to rename */
884 if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
887 /* rename sysfs_dirent */
888 if (strcmp(sd->s_name, new_name) != 0) {
890 new_name = kstrdup(new_name, GFP_KERNEL);
895 sd->s_name = new_name;
899 * Move to the appropriate place in the appropriate directories rbtree.
901 sysfs_unlink_sibling(sd);
902 sysfs_get(new_parent_sd);
903 sysfs_put(sd->s_parent);
905 sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
906 sd->s_parent = new_parent_sd;
907 sysfs_link_sibling(sd);
911 mutex_unlock(&sysfs_mutex);
915 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
918 struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
920 return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
923 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
926 struct sysfs_dirent *sd = kobj->sd;
927 struct sysfs_dirent *new_parent_sd;
929 BUG_ON(!sd->s_parent);
930 new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
931 new_parent_kobj->sd : &sysfs_root;
933 return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
936 /* Relationship between s_mode and the DT_xxx types */
937 static inline unsigned char dt_type(struct sysfs_dirent *sd)
939 return (sd->s_mode >> 12) & 15;
942 static int sysfs_dir_release(struct inode *inode, struct file *filp)
944 sysfs_put(filp->private_data);
948 static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
949 struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
952 int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
953 pos->s_parent == parent_sd &&
959 if (!pos && (hash > 1) && (hash < INT_MAX)) {
960 struct rb_node *node = parent_sd->s_dir.children.rb_node;
962 pos = to_sysfs_dirent(node);
964 if (hash < pos->s_hash)
965 node = node->rb_left;
966 else if (hash > pos->s_hash)
967 node = node->rb_right;
972 /* Skip over entries in the wrong namespace */
973 while (pos && pos->s_ns != ns) {
974 struct rb_node *node = rb_next(&pos->s_rb);
978 pos = to_sysfs_dirent(node);
983 static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
984 struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
986 pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
989 struct rb_node *node = rb_next(&pos->s_rb);
993 pos = to_sysfs_dirent(node);
994 } while (pos && pos->s_ns != ns);
998 static int sysfs_readdir(struct file *file, struct dir_context *ctx)
1000 struct dentry *dentry = file->f_path.dentry;
1001 struct sysfs_dirent *parent_sd = dentry->d_fsdata;
1002 struct sysfs_dirent *pos = file->private_data;
1003 const void *ns = NULL;
1005 if (!dir_emit_dots(file, ctx))
1007 mutex_lock(&sysfs_mutex);
1009 if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
1010 ns = sysfs_info(dentry->d_sb)->ns;
1012 for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
1014 pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
1015 const char *name = pos->s_name;
1016 unsigned int type = dt_type(pos);
1017 int len = strlen(name);
1018 ino_t ino = pos->s_ino;
1019 ctx->pos = pos->s_hash;
1020 file->private_data = sysfs_get(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,