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);
320 /* Remove the dentry from the dcache hashes.
321 * If this is a deleted dentry we use d_drop instead of d_delete
322 * so sysfs doesn't need to cope with negative dentries.
324 * If this is a dentry that has simply been renamed we
325 * use d_drop to remove it from the dcache lookup on its
326 * old parent. If this dentry persists later when a lookup
327 * is performed at its new name the dentry will be readded
328 * to the dcache hashes.
330 mutex_unlock(&sysfs_mutex);
331 shrink_submounts_and_drop(dentry);
336 static void sysfs_dentry_release(struct dentry *dentry)
338 sysfs_put(dentry->d_fsdata);
341 const struct dentry_operations sysfs_dentry_ops = {
342 .d_revalidate = sysfs_dentry_revalidate,
343 .d_delete = sysfs_dentry_delete,
344 .d_release = sysfs_dentry_release,
347 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
349 char *dup_name = NULL;
350 struct sysfs_dirent *sd;
352 if (type & SYSFS_COPY_NAME) {
353 name = dup_name = kstrdup(name, GFP_KERNEL);
358 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
362 if (sysfs_alloc_ino(&sd->s_ino))
365 atomic_set(&sd->s_count, 1);
366 atomic_set(&sd->s_active, 0);
370 sd->s_flags = type | SYSFS_FLAG_REMOVED;
375 kmem_cache_free(sysfs_dir_cachep, sd);
382 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
383 * @acxt: pointer to sysfs_addrm_cxt to be used
385 * This function is called when the caller is about to add or remove
386 * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used
387 * to keep and pass context to other addrm functions.
390 * Kernel thread context (may sleep). sysfs_mutex is locked on
393 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
394 __acquires(sysfs_mutex)
396 memset(acxt, 0, sizeof(*acxt));
398 mutex_lock(&sysfs_mutex);
402 * __sysfs_add_one - add sysfs_dirent to parent without warning
403 * @acxt: addrm context to use
404 * @sd: sysfs_dirent to be added
405 * @parent_sd: the parent sysfs_dirent to add @sd to
407 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
408 * the parent inode if @sd is a directory and link into the children
409 * list of the parent.
411 * This function should be called between calls to
412 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
413 * passed the same @acxt as passed to sysfs_addrm_start().
416 * Determined by sysfs_addrm_start().
419 * 0 on success, -EEXIST if entry with the given name already
422 int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
423 struct sysfs_dirent *parent_sd)
425 struct sysfs_inode_attrs *ps_iattr;
428 sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
429 sd->s_parent = sysfs_get(parent_sd);
431 ret = sysfs_link_sibling(sd);
435 /* Update timestamps on the parent */
436 ps_iattr = parent_sd->s_iattr;
438 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
439 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
442 /* Mark the entry added into directory tree */
443 sd->s_flags &= ~SYSFS_FLAG_REMOVED;
449 * sysfs_pathname - return full path to sysfs dirent
450 * @sd: sysfs_dirent whose path we want
451 * @path: caller allocated buffer of size PATH_MAX
453 * Gives the name "/" to the sysfs_root entry; any path returned
454 * is relative to wherever sysfs is mounted.
456 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
459 sysfs_pathname(sd->s_parent, path);
460 strlcat(path, "/", PATH_MAX);
462 strlcat(path, sd->s_name, PATH_MAX);
467 * sysfs_add_one - add sysfs_dirent to parent
468 * @acxt: addrm context to use
469 * @sd: sysfs_dirent to be added
470 * @parent_sd: the parent sysfs_dirent to add @sd to
472 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
473 * the parent inode if @sd is a directory and link into the children
474 * list of the parent.
476 * This function should be called between calls to
477 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
478 * passed the same @acxt as passed to sysfs_addrm_start().
481 * Determined by sysfs_addrm_start().
484 * 0 on success, -EEXIST if entry with the given name already
487 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
488 struct sysfs_dirent *parent_sd)
492 ret = __sysfs_add_one(acxt, sd, parent_sd);
493 if (ret == -EEXIST) {
494 char *path = kzalloc(PATH_MAX, GFP_KERNEL);
496 "sysfs: cannot create duplicate filename '%s'\n",
497 (path == NULL) ? sd->s_name
498 : (sysfs_pathname(parent_sd, path),
499 strlcat(path, "/", PATH_MAX),
500 strlcat(path, sd->s_name, PATH_MAX),
509 * sysfs_remove_one - remove sysfs_dirent from parent
510 * @acxt: addrm context to use
511 * @sd: sysfs_dirent to be removed
513 * Mark @sd removed and drop nlink of parent inode if @sd is a
514 * directory. @sd is unlinked from the children list.
516 * This function should be called between calls to
517 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
518 * passed the same @acxt as passed to sysfs_addrm_start().
521 * Determined by sysfs_addrm_start().
523 static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
524 struct sysfs_dirent *sd)
526 struct sysfs_inode_attrs *ps_iattr;
529 * Removal can be called multiple times on the same node. Only the
530 * first invocation is effective and puts the base ref.
532 if (sd->s_flags & SYSFS_FLAG_REMOVED)
535 sysfs_unlink_sibling(sd);
537 /* Update timestamps on the parent */
538 ps_iattr = sd->s_parent->s_iattr;
540 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
541 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
544 sd->s_flags |= SYSFS_FLAG_REMOVED;
545 sd->u.removed_list = acxt->removed;
550 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
551 * @acxt: addrm context to finish up
553 * Finish up sysfs_dirent add/remove. Resources acquired by
554 * sysfs_addrm_start() are released and removed sysfs_dirents are
558 * sysfs_mutex is released.
560 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
561 __releases(sysfs_mutex)
563 /* release resources acquired by sysfs_addrm_start() */
564 mutex_unlock(&sysfs_mutex);
566 /* kill removed sysfs_dirents */
567 while (acxt->removed) {
568 struct sysfs_dirent *sd = acxt->removed;
570 acxt->removed = sd->u.removed_list;
572 sysfs_deactivate(sd);
573 sysfs_unmap_bin_file(sd);
579 * sysfs_find_dirent - find sysfs_dirent with the given name
580 * @parent_sd: sysfs_dirent to search under
581 * @name: name to look for
582 * @ns: the namespace tag to use
584 * Look for sysfs_dirent with name @name under @parent_sd.
587 * mutex_lock(sysfs_mutex)
590 * Pointer to sysfs_dirent if found, NULL if not.
592 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
593 const unsigned char *name,
596 struct rb_node *node = parent_sd->s_dir.children.rb_node;
599 hash = sysfs_name_hash(name, ns);
601 struct sysfs_dirent *sd;
604 sd = to_sysfs_dirent(node);
605 result = sysfs_name_compare(hash, name, ns, sd);
607 node = node->rb_left;
609 node = node->rb_right;
617 * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
618 * @parent_sd: sysfs_dirent to search under
619 * @name: name to look for
620 * @ns: the namespace tag to use
622 * Look for sysfs_dirent with name @name under @parent_sd and get
626 * Kernel thread context (may sleep). Grabs sysfs_mutex.
629 * Pointer to sysfs_dirent if found, NULL if not.
631 struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
632 const unsigned char *name,
635 struct sysfs_dirent *sd;
637 mutex_lock(&sysfs_mutex);
638 sd = sysfs_find_dirent(parent_sd, name, ns);
640 mutex_unlock(&sysfs_mutex);
644 EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
646 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
647 const char *name, const void *ns,
648 struct sysfs_dirent **p_sd)
650 umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
651 struct sysfs_addrm_cxt acxt;
652 struct sysfs_dirent *sd;
656 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
661 sd->s_dir.kobj = kobj;
664 sysfs_addrm_start(&acxt);
665 rc = sysfs_add_one(&acxt, sd, parent_sd);
666 sysfs_addrm_finish(&acxt);
676 int sysfs_create_subdir(struct kobject *kobj, const char *name,
677 struct sysfs_dirent **p_sd)
679 return create_dir(kobj, kobj->sd, name, NULL, p_sd);
683 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
684 * @kobj: object we're creating directory for
685 * @ns: the namespace tag to use
687 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
689 struct sysfs_dirent *parent_sd, *sd;
695 parent_sd = kobj->parent->sd;
697 parent_sd = &sysfs_root;
702 error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
708 static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
711 struct dentry *ret = NULL;
712 struct dentry *parent = dentry->d_parent;
713 struct sysfs_dirent *parent_sd = parent->d_fsdata;
714 struct sysfs_dirent *sd;
716 const void *ns = NULL;
718 mutex_lock(&sysfs_mutex);
720 if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
721 ns = sysfs_info(dir->i_sb)->ns;
723 sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
727 ret = ERR_PTR(-ENOENT);
730 dentry->d_fsdata = sysfs_get(sd);
732 /* attach dentry and inode */
733 inode = sysfs_get_inode(dir->i_sb, sd);
735 ret = ERR_PTR(-ENOMEM);
739 /* instantiate and hash dentry */
740 ret = d_materialise_unique(dentry, inode);
742 mutex_unlock(&sysfs_mutex);
746 const struct inode_operations sysfs_dir_inode_operations = {
747 .lookup = sysfs_lookup,
748 .permission = sysfs_permission,
749 .setattr = sysfs_setattr,
750 .getattr = sysfs_getattr,
751 .setxattr = sysfs_setxattr,
754 static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
756 struct sysfs_dirent *last;
763 if (sysfs_type(pos) != SYSFS_DIR)
766 rbn = rb_first(&pos->s_dir.children);
770 pos = to_sysfs_dirent(rbn);
777 * sysfs_next_descendant_post - find the next descendant for post-order walk
778 * @pos: the current position (%NULL to initiate traversal)
779 * @root: sysfs_dirent whose descendants to walk
781 * Find the next descendant to visit for post-order traversal of @root's
782 * descendants. @root is included in the iteration and the last node to be
785 static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
786 struct sysfs_dirent *root)
790 lockdep_assert_held(&sysfs_mutex);
792 /* if first iteration, visit leftmost descendant which may be root */
794 return sysfs_leftmost_descendant(root);
796 /* if we visited @root, we're done */
800 /* if there's an unvisited sibling, visit its leftmost descendant */
801 rbn = rb_next(&pos->s_rb);
803 return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
805 /* no sibling left, visit parent */
806 return pos->s_parent;
809 void __sysfs_remove(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
811 struct sysfs_dirent *pos, *next;
816 pr_debug("sysfs %s: removing\n", sd->s_name);
821 next = sysfs_next_descendant_post(pos, sd);
823 sysfs_remove_one(acxt, pos);
828 * sysfs_remove - remove a sysfs_dirent recursively
829 * @sd: the sysfs_dirent to remove
831 * Remove @sd along with all its subdirectories and files.
833 void sysfs_remove(struct sysfs_dirent *sd)
835 struct sysfs_addrm_cxt acxt;
837 sysfs_addrm_start(&acxt);
838 __sysfs_remove(&acxt, sd);
839 sysfs_addrm_finish(&acxt);
843 * sysfs_remove_dir - remove an object's directory.
846 * The only thing special about this is that we remove any files in
847 * the directory before we remove the directory, and we've inlined
848 * what used to be sysfs_rmdir() below, instead of calling separately.
850 void sysfs_remove_dir(struct kobject *kobj)
852 struct sysfs_dirent *sd = kobj->sd;
854 spin_lock(&sysfs_assoc_lock);
856 spin_unlock(&sysfs_assoc_lock);
859 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
864 int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
865 const char *new_name, const void *new_ns)
869 mutex_lock(&sysfs_mutex);
872 if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
873 (strcmp(sd->s_name, new_name) == 0))
874 goto out; /* nothing to rename */
877 if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
880 /* rename sysfs_dirent */
881 if (strcmp(sd->s_name, new_name) != 0) {
883 new_name = kstrdup(new_name, GFP_KERNEL);
888 sd->s_name = new_name;
892 * Move to the appropriate place in the appropriate directories rbtree.
894 sysfs_unlink_sibling(sd);
895 sysfs_get(new_parent_sd);
896 sysfs_put(sd->s_parent);
898 sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
899 sd->s_parent = new_parent_sd;
900 sysfs_link_sibling(sd);
904 mutex_unlock(&sysfs_mutex);
908 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
911 struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
913 return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
916 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
919 struct sysfs_dirent *sd = kobj->sd;
920 struct sysfs_dirent *new_parent_sd;
922 BUG_ON(!sd->s_parent);
923 new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
924 new_parent_kobj->sd : &sysfs_root;
926 return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
929 /* Relationship between s_mode and the DT_xxx types */
930 static inline unsigned char dt_type(struct sysfs_dirent *sd)
932 return (sd->s_mode >> 12) & 15;
935 static int sysfs_dir_release(struct inode *inode, struct file *filp)
937 sysfs_put(filp->private_data);
941 static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
942 struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
945 int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
946 pos->s_parent == parent_sd &&
952 if (!pos && (hash > 1) && (hash < INT_MAX)) {
953 struct rb_node *node = parent_sd->s_dir.children.rb_node;
955 pos = to_sysfs_dirent(node);
957 if (hash < pos->s_hash)
958 node = node->rb_left;
959 else if (hash > pos->s_hash)
960 node = node->rb_right;
965 /* Skip over entries in the wrong namespace */
966 while (pos && pos->s_ns != ns) {
967 struct rb_node *node = rb_next(&pos->s_rb);
971 pos = to_sysfs_dirent(node);
976 static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
977 struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
979 pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
982 struct rb_node *node = rb_next(&pos->s_rb);
986 pos = to_sysfs_dirent(node);
987 } while (pos && pos->s_ns != ns);
991 static int sysfs_readdir(struct file *file, struct dir_context *ctx)
993 struct dentry *dentry = file->f_path.dentry;
994 struct sysfs_dirent *parent_sd = dentry->d_fsdata;
995 struct sysfs_dirent *pos = file->private_data;
996 const void *ns = NULL;
998 if (!dir_emit_dots(file, ctx))
1000 mutex_lock(&sysfs_mutex);
1002 if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
1003 ns = sysfs_info(dentry->d_sb)->ns;
1005 for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
1007 pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
1008 const char *name = pos->s_name;
1009 unsigned int type = dt_type(pos);
1010 int len = strlen(name);
1011 ino_t ino = pos->s_ino;
1012 ctx->pos = pos->s_hash;
1013 file->private_data = sysfs_get(pos);
1015 mutex_unlock(&sysfs_mutex);
1016 if (!dir_emit(ctx, name, len, ino, type))
1018 mutex_lock(&sysfs_mutex);
1020 mutex_unlock(&sysfs_mutex);
1021 file->private_data = NULL;
1026 static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1028 struct inode *inode = file_inode(file);
1031 mutex_lock(&inode->i_mutex);
1032 ret = generic_file_llseek(file, offset, whence);
1033 mutex_unlock(&inode->i_mutex);
1038 const struct file_operations sysfs_dir_operations = {
1039 .read = generic_read_dir,
1040 .iterate = sysfs_readdir,
1041 .release = sysfs_dir_release,
1042 .llseek = sysfs_dir_llseek,