2 * dir.c - Operations for sysfs directories.
8 #include <linux/mount.h>
9 #include <linux/module.h>
10 #include <linux/kobject.h>
11 #include <linux/namei.h>
12 #include <linux/idr.h>
13 #include <linux/completion.h>
14 #include <linux/mutex.h>
17 DEFINE_MUTEX(sysfs_mutex);
18 spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
20 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21 static DEFINE_IDA(sysfs_ino_ida);
24 * sysfs_link_sibling - link sysfs_dirent into sibling list
25 * @sd: sysfs_dirent of interest
27 * Link @sd into its sibling list which starts from
28 * sd->s_parent->s_children.
31 * mutex_lock(sysfs_mutex)
33 static void sysfs_link_sibling(struct sysfs_dirent *sd)
35 struct sysfs_dirent *parent_sd = sd->s_parent;
37 BUG_ON(sd->s_sibling);
38 sd->s_sibling = parent_sd->s_children;
39 parent_sd->s_children = sd;
43 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
44 * @sd: sysfs_dirent of interest
46 * Unlink @sd from its sibling list which starts from
47 * sd->s_parent->s_children.
50 * mutex_lock(sysfs_mutex)
52 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
54 struct sysfs_dirent **pos;
56 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
66 * sysfs_get_dentry - get dentry for the given sysfs_dirent
67 * @sd: sysfs_dirent of interest
69 * Get dentry for @sd. Dentry is looked up if currently not
70 * present. This function climbs sysfs_dirent tree till it
71 * reaches a sysfs_dirent with valid dentry attached and descends
72 * down from there looking up dentry for each step.
75 * Kernel thread context (may sleep)
78 * Pointer to found dentry on success, ERR_PTR() value on error.
80 struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
82 struct sysfs_dirent *cur;
83 struct dentry *parent_dentry, *dentry;
86 /* Find the first parent which has valid s_dentry and get the
89 mutex_lock(&sysfs_mutex);
91 spin_lock(&sysfs_assoc_lock);
93 spin_lock(&dcache_lock);
98 while (!cur->s_dentry || !cur->s_dentry->d_inode) {
99 if (cur->s_flags & SYSFS_FLAG_REMOVED) {
100 dentry = ERR_PTR(-ENOENT);
108 dentry = dget_locked(cur->s_dentry);
110 spin_unlock(&dcache_lock);
111 spin_unlock(&sysfs_assoc_lock);
113 /* from the found dentry, look up depth times */
115 /* find and get depth'th ancestor */
116 for (cur = sd, i = 0; cur && i < depth; i++)
119 /* This can happen if tree structure was modified due
120 * to move/rename. Restart.
129 mutex_unlock(&sysfs_mutex);
132 parent_dentry = dentry;
133 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
134 strlen(cur->s_name));
137 if (IS_ERR(dentry)) {
142 mutex_lock(&sysfs_mutex);
143 spin_lock(&sysfs_assoc_lock);
145 /* This, again, can happen if tree structure has
146 * changed and we looked up the wrong thing. Restart.
148 if (cur->s_dentry != dentry) {
154 spin_unlock(&sysfs_assoc_lock);
159 mutex_unlock(&sysfs_mutex);
164 * sysfs_get_active - get an active reference to sysfs_dirent
165 * @sd: sysfs_dirent to get an active reference to
167 * Get an active reference of @sd. This function is noop if @sd
171 * Pointer to @sd on success, NULL on failure.
173 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
181 v = atomic_read(&sd->s_active);
185 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
196 * sysfs_put_active - put an active reference to sysfs_dirent
197 * @sd: sysfs_dirent to put an active reference to
199 * Put an active reference to @sd. This function is noop if @sd
202 void sysfs_put_active(struct sysfs_dirent *sd)
204 struct completion *cmpl;
210 v = atomic_dec_return(&sd->s_active);
211 if (likely(v != SD_DEACTIVATED_BIAS))
214 /* atomic_dec_return() is a mb(), we'll always see the updated
217 cmpl = (void *)sd->s_sibling;
222 * sysfs_get_active_two - get active references to sysfs_dirent and parent
223 * @sd: sysfs_dirent of interest
225 * Get active reference to @sd and its parent. Parent's active
226 * reference is grabbed first. This function is noop if @sd is
230 * Pointer to @sd on success, NULL on failure.
232 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
235 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
237 if (unlikely(!sysfs_get_active(sd))) {
238 sysfs_put_active(sd->s_parent);
246 * sysfs_put_active_two - put active references to sysfs_dirent and parent
247 * @sd: sysfs_dirent of interest
249 * Put active references to @sd and its parent. This function is
250 * noop if @sd is NULL.
252 void sysfs_put_active_two(struct sysfs_dirent *sd)
255 sysfs_put_active(sd);
256 sysfs_put_active(sd->s_parent);
261 * sysfs_deactivate - deactivate sysfs_dirent
262 * @sd: sysfs_dirent to deactivate
264 * Deny new active references and drain existing ones.
266 static void sysfs_deactivate(struct sysfs_dirent *sd)
268 DECLARE_COMPLETION_ONSTACK(wait);
271 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
272 sd->s_sibling = (void *)&wait;
274 /* atomic_add_return() is a mb(), put_active() will always see
275 * the updated sd->s_sibling.
277 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
279 if (v != SD_DEACTIVATED_BIAS)
280 wait_for_completion(&wait);
282 sd->s_sibling = NULL;
285 static int sysfs_alloc_ino(ino_t *pino)
290 spin_lock(&sysfs_ino_lock);
291 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
292 spin_unlock(&sysfs_ino_lock);
295 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
304 static void sysfs_free_ino(ino_t ino)
306 spin_lock(&sysfs_ino_lock);
307 ida_remove(&sysfs_ino_ida, ino);
308 spin_unlock(&sysfs_ino_lock);
311 void release_sysfs_dirent(struct sysfs_dirent * sd)
313 struct sysfs_dirent *parent_sd;
316 /* Moving/renaming is always done while holding reference.
317 * sd->s_parent won't change beneath us.
319 parent_sd = sd->s_parent;
321 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
322 sysfs_put(sd->s_elem.symlink.target_sd);
323 if (sysfs_type(sd) & SYSFS_COPY_NAME)
326 sysfs_free_ino(sd->s_ino);
327 kmem_cache_free(sysfs_dir_cachep, sd);
330 if (sd && atomic_dec_and_test(&sd->s_count))
334 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
336 struct sysfs_dirent * sd = dentry->d_fsdata;
339 /* sd->s_dentry is protected with sysfs_assoc_lock.
340 * This allows sysfs_drop_dentry() to dereference it.
342 spin_lock(&sysfs_assoc_lock);
344 /* The dentry might have been deleted or another
345 * lookup could have happened updating sd->s_dentry to
346 * point the new dentry. Ignore if it isn't pointing
349 if (sd->s_dentry == dentry)
351 spin_unlock(&sysfs_assoc_lock);
357 static struct dentry_operations sysfs_dentry_ops = {
358 .d_iput = sysfs_d_iput,
361 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
363 char *dup_name = NULL;
364 struct sysfs_dirent *sd;
366 if (type & SYSFS_COPY_NAME) {
367 name = dup_name = kstrdup(name, GFP_KERNEL);
372 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
376 if (sysfs_alloc_ino(&sd->s_ino))
379 atomic_set(&sd->s_count, 1);
380 atomic_set(&sd->s_active, 0);
381 atomic_set(&sd->s_event, 1);
390 kmem_cache_free(sysfs_dir_cachep, sd);
397 * sysfs_attach_dentry - associate sysfs_dirent with dentry
398 * @sd: target sysfs_dirent
399 * @dentry: dentry to associate
401 * Associate @sd with @dentry. This is protected by
402 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
405 * mutex_lock(sysfs_mutex)
407 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
409 dentry->d_op = &sysfs_dentry_ops;
410 dentry->d_fsdata = sysfs_get(sd);
412 /* protect sd->s_dentry against sysfs_d_iput */
413 spin_lock(&sysfs_assoc_lock);
414 sd->s_dentry = dentry;
415 spin_unlock(&sysfs_assoc_lock);
420 static int sysfs_ilookup_test(struct inode *inode, void *arg)
422 struct sysfs_dirent *sd = arg;
423 return inode->i_ino == sd->s_ino;
427 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
428 * @acxt: pointer to sysfs_addrm_cxt to be used
429 * @parent_sd: parent sysfs_dirent
431 * This function is called when the caller is about to add or
432 * remove sysfs_dirent under @parent_sd. This function acquires
433 * sysfs_mutex, grabs inode for @parent_sd if available and lock
434 * i_mutex of it. @acxt is used to keep and pass context to
435 * other addrm functions.
438 * Kernel thread context (may sleep). sysfs_mutex is locked on
439 * return. i_mutex of parent inode is locked on return if
442 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
443 struct sysfs_dirent *parent_sd)
447 memset(acxt, 0, sizeof(*acxt));
448 acxt->parent_sd = parent_sd;
450 /* Lookup parent inode. inode initialization and I_NEW
451 * clearing are protected by sysfs_mutex. By grabbing it and
452 * looking up with _nowait variant, inode state can be
453 * determined reliably.
455 mutex_lock(&sysfs_mutex);
457 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
460 if (inode && !(inode->i_state & I_NEW)) {
461 /* parent inode available */
462 acxt->parent_inode = inode;
464 /* sysfs_mutex is below i_mutex in lock hierarchy.
465 * First, trylock i_mutex. If fails, unlock
466 * sysfs_mutex and lock them in order.
468 if (!mutex_trylock(&inode->i_mutex)) {
469 mutex_unlock(&sysfs_mutex);
470 mutex_lock(&inode->i_mutex);
471 mutex_lock(&sysfs_mutex);
478 * sysfs_add_one - add sysfs_dirent to parent
479 * @acxt: addrm context to use
480 * @sd: sysfs_dirent to be added
482 * Get @acxt->parent_sd and set sd->s_parent to it and increment
483 * nlink of parent inode if @sd is a directory. @sd is NOT
484 * linked into the children list of the parent. The caller
485 * should invoke sysfs_link_sibling() after this function
486 * completes if @sd needs to be on the children list.
488 * This function should be called between calls to
489 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
490 * passed the same @acxt as passed to sysfs_addrm_start().
493 * Determined by sysfs_addrm_start().
496 * 0 on success, -EEXIST if entry with the given name already
499 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
501 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
504 sd->s_parent = sysfs_get(acxt->parent_sd);
506 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
507 inc_nlink(acxt->parent_inode);
511 sysfs_link_sibling(sd);
517 * sysfs_remove_one - remove sysfs_dirent from parent
518 * @acxt: addrm context to use
519 * @sd: sysfs_dirent to be added
521 * Mark @sd removed and drop nlink of parent inode if @sd is a
522 * directory. @sd is NOT unlinked from the children list of the
523 * parent. The caller is repsonsible for removing @sd from the
524 * children list before calling this function.
526 * This function should be called between calls to
527 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
528 * passed the same @acxt as passed to sysfs_addrm_start().
531 * Determined by sysfs_addrm_start().
533 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
535 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
537 sysfs_unlink_sibling(sd);
539 sd->s_flags |= SYSFS_FLAG_REMOVED;
540 sd->s_sibling = acxt->removed;
543 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
544 drop_nlink(acxt->parent_inode);
550 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
551 * @sd: target sysfs_dirent
553 * Drop dentry for @sd. @sd must have been unlinked from its
554 * parent on entry to this function such that it can't be looked
557 * @sd->s_dentry which is protected with sysfs_assoc_lock points
558 * to the currently associated dentry but we're not holding a
559 * reference to it and racing with dput(). Grab dcache_lock and
560 * verify dentry before dropping it. If @sd->s_dentry is NULL or
561 * dput() beats us, no need to bother.
563 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
565 struct dentry *dentry = NULL;
568 /* We're not holding a reference to ->s_dentry dentry but the
569 * field will stay valid as long as sysfs_assoc_lock is held.
571 spin_lock(&sysfs_assoc_lock);
572 spin_lock(&dcache_lock);
574 /* drop dentry if it's there and dput() didn't kill it yet */
575 if (sd->s_dentry && sd->s_dentry->d_inode) {
576 dentry = dget_locked(sd->s_dentry);
577 spin_lock(&dentry->d_lock);
579 spin_unlock(&dentry->d_lock);
582 spin_unlock(&dcache_lock);
583 spin_unlock(&sysfs_assoc_lock);
587 /* adjust nlink and update timestamp */
588 inode = ilookup(sysfs_sb, sd->s_ino);
590 mutex_lock(&inode->i_mutex);
592 inode->i_ctime = CURRENT_TIME;
594 if (sysfs_type(sd) == SYSFS_DIR)
597 mutex_unlock(&inode->i_mutex);
603 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
604 * @acxt: addrm context to finish up
606 * Finish up sysfs_dirent add/remove. Resources acquired by
607 * sysfs_addrm_start() are released and removed sysfs_dirents are
608 * cleaned up. Timestamps on the parent inode are updated.
611 * All mutexes acquired by sysfs_addrm_start() are released.
614 * Number of added/removed sysfs_dirents since sysfs_addrm_start().
616 int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
618 /* release resources acquired by sysfs_addrm_start() */
619 mutex_unlock(&sysfs_mutex);
620 if (acxt->parent_inode) {
621 struct inode *inode = acxt->parent_inode;
623 /* if added/removed, update timestamps on the parent */
625 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
627 mutex_unlock(&inode->i_mutex);
631 /* kill removed sysfs_dirents */
632 while (acxt->removed) {
633 struct sysfs_dirent *sd = acxt->removed;
635 acxt->removed = sd->s_sibling;
636 sd->s_sibling = NULL;
638 sysfs_drop_dentry(sd);
639 sysfs_deactivate(sd);
647 * sysfs_find_dirent - find sysfs_dirent with the given name
648 * @parent_sd: sysfs_dirent to search under
649 * @name: name to look for
651 * Look for sysfs_dirent with name @name under @parent_sd.
654 * mutex_lock(sysfs_mutex)
657 * Pointer to sysfs_dirent if found, NULL if not.
659 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
660 const unsigned char *name)
662 struct sysfs_dirent *sd;
664 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
665 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
671 * sysfs_get_dirent - find and get sysfs_dirent with the given name
672 * @parent_sd: sysfs_dirent to search under
673 * @name: name to look for
675 * Look for sysfs_dirent with name @name under @parent_sd and get
679 * Kernel thread context (may sleep). Grabs sysfs_mutex.
682 * Pointer to sysfs_dirent if found, NULL if not.
684 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
685 const unsigned char *name)
687 struct sysfs_dirent *sd;
689 mutex_lock(&sysfs_mutex);
690 sd = sysfs_find_dirent(parent_sd, name);
692 mutex_unlock(&sysfs_mutex);
697 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
698 const char *name, struct sysfs_dirent **p_sd)
700 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
701 struct sysfs_addrm_cxt acxt;
702 struct sysfs_dirent *sd;
706 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
709 sd->s_elem.dir.kobj = kobj;
712 sysfs_addrm_start(&acxt, parent_sd);
713 rc = sysfs_add_one(&acxt, sd);
714 sysfs_addrm_finish(&acxt);
724 int sysfs_create_subdir(struct kobject *kobj, const char *name,
725 struct sysfs_dirent **p_sd)
727 return create_dir(kobj, kobj->sd, name, p_sd);
731 * sysfs_create_dir - create a directory for an object.
732 * @kobj: object we're creating directory for.
734 int sysfs_create_dir(struct kobject * kobj)
736 struct sysfs_dirent *parent_sd, *sd;
742 parent_sd = kobj->parent->sd;
743 else if (sysfs_mount && sysfs_mount->mnt_sb)
744 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
748 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
754 static int sysfs_count_nlink(struct sysfs_dirent *sd)
756 struct sysfs_dirent *child;
759 for (child = sd->s_children; child; child = child->s_sibling)
760 if (sysfs_type(child) == SYSFS_DIR)
765 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
766 struct nameidata *nd)
768 struct dentry *ret = NULL;
769 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
770 struct sysfs_dirent *sd;
771 struct bin_attribute *bin_attr;
774 mutex_lock(&sysfs_mutex);
776 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
777 if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
784 /* attach dentry and inode */
785 inode = sysfs_get_inode(sd);
787 ret = ERR_PTR(-ENOMEM);
791 if (inode->i_state & I_NEW) {
792 /* initialize inode according to type */
793 switch (sysfs_type(sd)) {
795 inode->i_op = &sysfs_dir_inode_operations;
796 inode->i_fop = &sysfs_dir_operations;
797 inode->i_nlink = sysfs_count_nlink(sd);
799 case SYSFS_KOBJ_ATTR:
800 inode->i_size = PAGE_SIZE;
801 inode->i_fop = &sysfs_file_operations;
803 case SYSFS_KOBJ_BIN_ATTR:
804 bin_attr = sd->s_elem.bin_attr.bin_attr;
805 inode->i_size = bin_attr->size;
806 inode->i_fop = &bin_fops;
808 case SYSFS_KOBJ_LINK:
809 inode->i_op = &sysfs_symlink_inode_operations;
816 sysfs_instantiate(dentry, inode);
817 sysfs_attach_dentry(sd, dentry);
820 mutex_unlock(&sysfs_mutex);
824 const struct inode_operations sysfs_dir_inode_operations = {
825 .lookup = sysfs_lookup,
826 .setattr = sysfs_setattr,
829 static void remove_dir(struct sysfs_dirent *sd)
831 struct sysfs_addrm_cxt acxt;
833 sysfs_addrm_start(&acxt, sd->s_parent);
834 sysfs_remove_one(&acxt, sd);
835 sysfs_addrm_finish(&acxt);
838 void sysfs_remove_subdir(struct sysfs_dirent *sd)
844 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
846 struct sysfs_addrm_cxt acxt;
847 struct sysfs_dirent **pos;
852 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
853 sysfs_addrm_start(&acxt, dir_sd);
854 pos = &dir_sd->s_children;
856 struct sysfs_dirent *sd = *pos;
858 if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
859 sysfs_remove_one(&acxt, sd);
861 pos = &(*pos)->s_sibling;
863 sysfs_addrm_finish(&acxt);
869 * sysfs_remove_dir - remove an object's directory.
872 * The only thing special about this is that we remove any files in
873 * the directory before we remove the directory, and we've inlined
874 * what used to be sysfs_rmdir() below, instead of calling separately.
877 void sysfs_remove_dir(struct kobject * kobj)
879 struct sysfs_dirent *sd = kobj->sd;
881 spin_lock(&sysfs_assoc_lock);
883 spin_unlock(&sysfs_assoc_lock);
885 __sysfs_remove_dir(sd);
888 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
890 struct sysfs_dirent *sd;
891 struct dentry *parent = NULL;
892 struct dentry *old_dentry = NULL, *new_dentry = NULL;
893 const char *dup_name = NULL;
896 /* get the original dentry */
898 old_dentry = sysfs_get_dentry(sd);
899 if (IS_ERR(old_dentry)) {
900 error = PTR_ERR(old_dentry);
904 parent = old_dentry->d_parent;
906 /* lock parent and get dentry for new name */
907 mutex_lock(&parent->d_inode->i_mutex);
909 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
910 if (IS_ERR(new_dentry)) {
911 error = PTR_ERR(new_dentry);
916 if (old_dentry == new_dentry)
920 if (new_dentry->d_inode)
923 /* rename kobject and sysfs_dirent */
925 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
929 error = kobject_set_name(kobj, "%s", new_name);
933 mutex_lock(&sysfs_mutex);
934 dup_name = sd->s_name;
935 sd->s_name = new_name;
936 mutex_unlock(&sysfs_mutex);
939 d_add(new_dentry, NULL);
940 d_move(sd->s_dentry, new_dentry);
948 mutex_unlock(&parent->d_inode->i_mutex);
956 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
958 struct sysfs_dirent *sd = kobj->sd;
959 struct sysfs_dirent *new_parent_sd;
960 struct dentry *old_parent, *new_parent = NULL;
961 struct dentry *old_dentry = NULL, *new_dentry = NULL;
964 BUG_ON(!sd->s_parent);
965 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
968 old_dentry = sysfs_get_dentry(sd);
969 if (IS_ERR(old_dentry)) {
970 error = PTR_ERR(old_dentry);
973 old_parent = sd->s_parent->s_dentry;
975 new_parent = sysfs_get_dentry(new_parent_sd);
976 if (IS_ERR(new_parent)) {
977 error = PTR_ERR(new_parent);
981 if (old_parent->d_inode == new_parent->d_inode) {
983 goto out_dput; /* nothing to move */
986 mutex_lock(&old_parent->d_inode->i_mutex);
987 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
988 mutex_unlock(&old_parent->d_inode->i_mutex);
992 new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
993 if (IS_ERR(new_dentry)) {
994 error = PTR_ERR(new_dentry);
998 d_add(new_dentry, NULL);
999 d_move(sd->s_dentry, new_dentry);
1002 /* Remove from old parent's list and insert into new parent's list. */
1003 mutex_lock(&sysfs_mutex);
1005 sysfs_unlink_sibling(sd);
1006 sysfs_get(new_parent_sd);
1007 sysfs_put(sd->s_parent);
1008 sd->s_parent = new_parent_sd;
1009 sysfs_link_sibling(sd);
1011 mutex_unlock(&sysfs_mutex);
1014 mutex_unlock(&new_parent->d_inode->i_mutex);
1015 mutex_unlock(&old_parent->d_inode->i_mutex);
1023 static int sysfs_dir_open(struct inode *inode, struct file *file)
1025 struct dentry * dentry = file->f_path.dentry;
1026 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1027 struct sysfs_dirent * sd;
1029 sd = sysfs_new_dirent("_DIR_", 0, 0);
1031 mutex_lock(&sysfs_mutex);
1032 sd->s_parent = sysfs_get(parent_sd);
1033 sysfs_link_sibling(sd);
1034 mutex_unlock(&sysfs_mutex);
1037 file->private_data = sd;
1038 return sd ? 0 : -ENOMEM;
1041 static int sysfs_dir_close(struct inode *inode, struct file *file)
1043 struct sysfs_dirent * cursor = file->private_data;
1045 mutex_lock(&sysfs_mutex);
1046 sysfs_unlink_sibling(cursor);
1047 mutex_unlock(&sysfs_mutex);
1049 release_sysfs_dirent(cursor);
1054 /* Relationship between s_mode and the DT_xxx types */
1055 static inline unsigned char dt_type(struct sysfs_dirent *sd)
1057 return (sd->s_mode >> 12) & 15;
1060 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1062 struct dentry *dentry = filp->f_path.dentry;
1063 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1064 struct sysfs_dirent *cursor = filp->private_data;
1065 struct sysfs_dirent **pos;
1067 int i = filp->f_pos;
1071 ino = parent_sd->s_ino;
1072 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1078 if (parent_sd->s_parent)
1079 ino = parent_sd->s_parent->s_ino;
1081 ino = parent_sd->s_ino;
1082 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1088 mutex_lock(&sysfs_mutex);
1090 pos = &parent_sd->s_children;
1091 while (*pos != cursor)
1092 pos = &(*pos)->s_sibling;
1095 *pos = cursor->s_sibling;
1097 if (filp->f_pos == 2)
1098 pos = &parent_sd->s_children;
1100 for ( ; *pos; pos = &(*pos)->s_sibling) {
1101 struct sysfs_dirent *next = *pos;
1105 if (!sysfs_type(next))
1108 name = next->s_name;
1112 if (filldir(dirent, name, len, filp->f_pos, ino,
1119 /* put cursor back in */
1120 cursor->s_sibling = *pos;
1123 mutex_unlock(&sysfs_mutex);
1128 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1130 struct dentry * dentry = file->f_path.dentry;
1134 offset += file->f_pos;
1141 if (offset != file->f_pos) {
1142 mutex_lock(&sysfs_mutex);
1144 file->f_pos = offset;
1145 if (file->f_pos >= 2) {
1146 struct sysfs_dirent *sd = dentry->d_fsdata;
1147 struct sysfs_dirent *cursor = file->private_data;
1148 struct sysfs_dirent **pos;
1149 loff_t n = file->f_pos - 2;
1151 sysfs_unlink_sibling(cursor);
1153 pos = &sd->s_children;
1155 struct sysfs_dirent *next = *pos;
1156 if (sysfs_type(next))
1158 pos = &(*pos)->s_sibling;
1161 cursor->s_sibling = *pos;
1165 mutex_unlock(&sysfs_mutex);
1171 const struct file_operations sysfs_dir_operations = {
1172 .open = sysfs_dir_open,
1173 .release = sysfs_dir_close,
1174 .llseek = sysfs_dir_lseek,
1175 .read = generic_read_dir,
1176 .readdir = sysfs_readdir,