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 mutex_lock(&parent_dentry->d_inode->i_mutex);
134 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
135 strlen(cur->s_name));
136 mutex_unlock(&parent_dentry->d_inode->i_mutex);
139 if (IS_ERR(dentry)) {
144 mutex_lock(&sysfs_mutex);
145 spin_lock(&sysfs_assoc_lock);
147 /* This, again, can happen if tree structure has
148 * changed and we looked up the wrong thing. Restart.
150 if (cur->s_dentry != dentry) {
156 spin_unlock(&sysfs_assoc_lock);
161 mutex_unlock(&sysfs_mutex);
166 * sysfs_get_active - get an active reference to sysfs_dirent
167 * @sd: sysfs_dirent to get an active reference to
169 * Get an active reference of @sd. This function is noop if @sd
173 * Pointer to @sd on success, NULL on failure.
175 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
183 v = atomic_read(&sd->s_active);
187 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
198 * sysfs_put_active - put an active reference to sysfs_dirent
199 * @sd: sysfs_dirent to put an active reference to
201 * Put an active reference to @sd. This function is noop if @sd
204 void sysfs_put_active(struct sysfs_dirent *sd)
206 struct completion *cmpl;
212 v = atomic_dec_return(&sd->s_active);
213 if (likely(v != SD_DEACTIVATED_BIAS))
216 /* atomic_dec_return() is a mb(), we'll always see the updated
219 cmpl = (void *)sd->s_sibling;
224 * sysfs_get_active_two - get active references to sysfs_dirent and parent
225 * @sd: sysfs_dirent of interest
227 * Get active reference to @sd and its parent. Parent's active
228 * reference is grabbed first. This function is noop if @sd is
232 * Pointer to @sd on success, NULL on failure.
234 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
237 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
239 if (unlikely(!sysfs_get_active(sd))) {
240 sysfs_put_active(sd->s_parent);
248 * sysfs_put_active_two - put active references to sysfs_dirent and parent
249 * @sd: sysfs_dirent of interest
251 * Put active references to @sd and its parent. This function is
252 * noop if @sd is NULL.
254 void sysfs_put_active_two(struct sysfs_dirent *sd)
257 sysfs_put_active(sd);
258 sysfs_put_active(sd->s_parent);
263 * sysfs_deactivate - deactivate sysfs_dirent
264 * @sd: sysfs_dirent to deactivate
266 * Deny new active references and drain existing ones.
268 static void sysfs_deactivate(struct sysfs_dirent *sd)
270 DECLARE_COMPLETION_ONSTACK(wait);
273 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
274 sd->s_sibling = (void *)&wait;
276 /* atomic_add_return() is a mb(), put_active() will always see
277 * the updated sd->s_sibling.
279 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
281 if (v != SD_DEACTIVATED_BIAS)
282 wait_for_completion(&wait);
284 sd->s_sibling = NULL;
287 static int sysfs_alloc_ino(ino_t *pino)
292 spin_lock(&sysfs_ino_lock);
293 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
294 spin_unlock(&sysfs_ino_lock);
297 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
306 static void sysfs_free_ino(ino_t ino)
308 spin_lock(&sysfs_ino_lock);
309 ida_remove(&sysfs_ino_ida, ino);
310 spin_unlock(&sysfs_ino_lock);
313 void release_sysfs_dirent(struct sysfs_dirent * sd)
315 struct sysfs_dirent *parent_sd;
318 /* Moving/renaming is always done while holding reference.
319 * sd->s_parent won't change beneath us.
321 parent_sd = sd->s_parent;
323 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
324 sysfs_put(sd->s_elem.symlink.target_sd);
325 if (sysfs_type(sd) & SYSFS_COPY_NAME)
328 sysfs_free_ino(sd->s_ino);
329 kmem_cache_free(sysfs_dir_cachep, sd);
332 if (sd && atomic_dec_and_test(&sd->s_count))
336 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
338 struct sysfs_dirent * sd = dentry->d_fsdata;
341 /* sd->s_dentry is protected with sysfs_assoc_lock.
342 * This allows sysfs_drop_dentry() to dereference it.
344 spin_lock(&sysfs_assoc_lock);
346 /* The dentry might have been deleted or another
347 * lookup could have happened updating sd->s_dentry to
348 * point the new dentry. Ignore if it isn't pointing
351 if (sd->s_dentry == dentry)
353 spin_unlock(&sysfs_assoc_lock);
359 static struct dentry_operations sysfs_dentry_ops = {
360 .d_iput = sysfs_d_iput,
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);
383 atomic_set(&sd->s_event, 1);
392 kmem_cache_free(sysfs_dir_cachep, sd);
399 * sysfs_attach_dentry - associate sysfs_dirent with dentry
400 * @sd: target sysfs_dirent
401 * @dentry: dentry to associate
403 * Associate @sd with @dentry. This is protected by
404 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
407 * mutex_lock(sysfs_mutex)
409 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
411 dentry->d_op = &sysfs_dentry_ops;
412 dentry->d_fsdata = sysfs_get(sd);
414 /* protect sd->s_dentry against sysfs_d_iput */
415 spin_lock(&sysfs_assoc_lock);
416 sd->s_dentry = dentry;
417 spin_unlock(&sysfs_assoc_lock);
422 static int sysfs_ilookup_test(struct inode *inode, void *arg)
424 struct sysfs_dirent *sd = arg;
425 return inode->i_ino == sd->s_ino;
429 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
430 * @acxt: pointer to sysfs_addrm_cxt to be used
431 * @parent_sd: parent sysfs_dirent
433 * This function is called when the caller is about to add or
434 * remove sysfs_dirent under @parent_sd. This function acquires
435 * sysfs_mutex, grabs inode for @parent_sd if available and lock
436 * i_mutex of it. @acxt is used to keep and pass context to
437 * other addrm functions.
440 * Kernel thread context (may sleep). sysfs_mutex is locked on
441 * return. i_mutex of parent inode is locked on return if
444 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
445 struct sysfs_dirent *parent_sd)
449 memset(acxt, 0, sizeof(*acxt));
450 acxt->parent_sd = parent_sd;
452 /* Lookup parent inode. inode initialization and I_NEW
453 * clearing are protected by sysfs_mutex. By grabbing it and
454 * looking up with _nowait variant, inode state can be
455 * determined reliably.
457 mutex_lock(&sysfs_mutex);
459 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
462 if (inode && !(inode->i_state & I_NEW)) {
463 /* parent inode available */
464 acxt->parent_inode = inode;
466 /* sysfs_mutex is below i_mutex in lock hierarchy.
467 * First, trylock i_mutex. If fails, unlock
468 * sysfs_mutex and lock them in order.
470 if (!mutex_trylock(&inode->i_mutex)) {
471 mutex_unlock(&sysfs_mutex);
472 mutex_lock(&inode->i_mutex);
473 mutex_lock(&sysfs_mutex);
480 * sysfs_add_one - add sysfs_dirent to parent
481 * @acxt: addrm context to use
482 * @sd: sysfs_dirent to be added
484 * Get @acxt->parent_sd and set sd->s_parent to it and increment
485 * nlink of parent inode if @sd is a directory. @sd is NOT
486 * linked into the children list of the parent. The caller
487 * should invoke sysfs_link_sibling() after this function
488 * completes if @sd needs to be on the children list.
490 * This function should be called between calls to
491 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
492 * passed the same @acxt as passed to sysfs_addrm_start().
495 * Determined by sysfs_addrm_start().
498 * 0 on success, -EEXIST if entry with the given name already
501 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
503 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
506 sd->s_parent = sysfs_get(acxt->parent_sd);
508 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
509 inc_nlink(acxt->parent_inode);
513 sysfs_link_sibling(sd);
519 * sysfs_remove_one - remove sysfs_dirent from parent
520 * @acxt: addrm context to use
521 * @sd: sysfs_dirent to be added
523 * Mark @sd removed and drop nlink of parent inode if @sd is a
524 * directory. @sd is NOT unlinked from the children list of the
525 * parent. The caller is repsonsible for removing @sd from the
526 * children list before calling this function.
528 * This function should be called between calls to
529 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
530 * passed the same @acxt as passed to sysfs_addrm_start().
533 * Determined by sysfs_addrm_start().
535 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
537 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
539 sysfs_unlink_sibling(sd);
541 sd->s_flags |= SYSFS_FLAG_REMOVED;
542 sd->s_sibling = acxt->removed;
545 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
546 drop_nlink(acxt->parent_inode);
552 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
553 * @sd: target sysfs_dirent
555 * Drop dentry for @sd. @sd must have been unlinked from its
556 * parent on entry to this function such that it can't be looked
559 * @sd->s_dentry which is protected with sysfs_assoc_lock points
560 * to the currently associated dentry but we're not holding a
561 * reference to it and racing with dput(). Grab dcache_lock and
562 * verify dentry before dropping it. If @sd->s_dentry is NULL or
563 * dput() beats us, no need to bother.
565 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
567 struct dentry *dentry = NULL;
570 /* We're not holding a reference to ->s_dentry dentry but the
571 * field will stay valid as long as sysfs_assoc_lock is held.
573 spin_lock(&sysfs_assoc_lock);
574 spin_lock(&dcache_lock);
576 /* drop dentry if it's there and dput() didn't kill it yet */
577 if (sd->s_dentry && sd->s_dentry->d_inode) {
578 dentry = dget_locked(sd->s_dentry);
579 spin_lock(&dentry->d_lock);
581 spin_unlock(&dentry->d_lock);
584 spin_unlock(&dcache_lock);
585 spin_unlock(&sysfs_assoc_lock);
589 /* adjust nlink and update timestamp */
590 inode = ilookup(sysfs_sb, sd->s_ino);
592 mutex_lock(&inode->i_mutex);
594 inode->i_ctime = CURRENT_TIME;
596 if (sysfs_type(sd) == SYSFS_DIR)
599 mutex_unlock(&inode->i_mutex);
605 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
606 * @acxt: addrm context to finish up
608 * Finish up sysfs_dirent add/remove. Resources acquired by
609 * sysfs_addrm_start() are released and removed sysfs_dirents are
610 * cleaned up. Timestamps on the parent inode are updated.
613 * All mutexes acquired by sysfs_addrm_start() are released.
615 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
617 /* release resources acquired by sysfs_addrm_start() */
618 mutex_unlock(&sysfs_mutex);
619 if (acxt->parent_inode) {
620 struct inode *inode = acxt->parent_inode;
622 /* if added/removed, update timestamps on the parent */
624 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
626 mutex_unlock(&inode->i_mutex);
630 /* kill removed sysfs_dirents */
631 while (acxt->removed) {
632 struct sysfs_dirent *sd = acxt->removed;
634 acxt->removed = sd->s_sibling;
635 sd->s_sibling = NULL;
637 sysfs_drop_dentry(sd);
638 sysfs_deactivate(sd);
644 * sysfs_find_dirent - find sysfs_dirent with the given name
645 * @parent_sd: sysfs_dirent to search under
646 * @name: name to look for
648 * Look for sysfs_dirent with name @name under @parent_sd.
651 * mutex_lock(sysfs_mutex)
654 * Pointer to sysfs_dirent if found, NULL if not.
656 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
657 const unsigned char *name)
659 struct sysfs_dirent *sd;
661 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
662 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
668 * sysfs_get_dirent - find and get sysfs_dirent with the given name
669 * @parent_sd: sysfs_dirent to search under
670 * @name: name to look for
672 * Look for sysfs_dirent with name @name under @parent_sd and get
676 * Kernel thread context (may sleep). Grabs sysfs_mutex.
679 * Pointer to sysfs_dirent if found, NULL if not.
681 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
682 const unsigned char *name)
684 struct sysfs_dirent *sd;
686 mutex_lock(&sysfs_mutex);
687 sd = sysfs_find_dirent(parent_sd, name);
689 mutex_unlock(&sysfs_mutex);
694 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
695 const char *name, struct sysfs_dirent **p_sd)
697 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
698 struct sysfs_addrm_cxt acxt;
699 struct sysfs_dirent *sd;
703 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
706 sd->s_elem.dir.kobj = kobj;
709 sysfs_addrm_start(&acxt, parent_sd);
710 rc = sysfs_add_one(&acxt, sd);
711 sysfs_addrm_finish(&acxt);
721 int sysfs_create_subdir(struct kobject *kobj, const char *name,
722 struct sysfs_dirent **p_sd)
724 return create_dir(kobj, kobj->sd, name, p_sd);
728 * sysfs_create_dir - create a directory for an object.
729 * @kobj: object we're creating directory for.
731 int sysfs_create_dir(struct kobject * kobj)
733 struct sysfs_dirent *parent_sd, *sd;
739 parent_sd = kobj->parent->sd;
740 else if (sysfs_mount && sysfs_mount->mnt_sb)
741 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
745 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
751 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
752 struct nameidata *nd)
754 struct dentry *ret = NULL;
755 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
756 struct sysfs_dirent *sd;
759 mutex_lock(&sysfs_mutex);
761 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
762 if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
769 /* attach dentry and inode */
770 inode = sysfs_get_inode(sd);
772 ret = ERR_PTR(-ENOMEM);
776 d_instantiate(dentry, inode);
777 sysfs_attach_dentry(sd, dentry);
780 mutex_unlock(&sysfs_mutex);
784 const struct inode_operations sysfs_dir_inode_operations = {
785 .lookup = sysfs_lookup,
786 .setattr = sysfs_setattr,
789 static void remove_dir(struct sysfs_dirent *sd)
791 struct sysfs_addrm_cxt acxt;
793 sysfs_addrm_start(&acxt, sd->s_parent);
794 sysfs_remove_one(&acxt, sd);
795 sysfs_addrm_finish(&acxt);
798 void sysfs_remove_subdir(struct sysfs_dirent *sd)
804 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
806 struct sysfs_addrm_cxt acxt;
807 struct sysfs_dirent **pos;
812 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
813 sysfs_addrm_start(&acxt, dir_sd);
814 pos = &dir_sd->s_children;
816 struct sysfs_dirent *sd = *pos;
818 if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
819 sysfs_remove_one(&acxt, sd);
821 pos = &(*pos)->s_sibling;
823 sysfs_addrm_finish(&acxt);
829 * sysfs_remove_dir - remove an object's directory.
832 * The only thing special about this is that we remove any files in
833 * the directory before we remove the directory, and we've inlined
834 * what used to be sysfs_rmdir() below, instead of calling separately.
837 void sysfs_remove_dir(struct kobject * kobj)
839 struct sysfs_dirent *sd = kobj->sd;
841 spin_lock(&sysfs_assoc_lock);
843 spin_unlock(&sysfs_assoc_lock);
845 __sysfs_remove_dir(sd);
848 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
850 struct sysfs_dirent *sd;
851 struct dentry *parent = NULL;
852 struct dentry *old_dentry = NULL, *new_dentry = NULL;
853 const char *dup_name = NULL;
856 /* get the original dentry */
858 old_dentry = sysfs_get_dentry(sd);
859 if (IS_ERR(old_dentry)) {
860 error = PTR_ERR(old_dentry);
864 parent = old_dentry->d_parent;
866 /* lock parent and get dentry for new name */
867 mutex_lock(&parent->d_inode->i_mutex);
869 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
870 if (IS_ERR(new_dentry)) {
871 error = PTR_ERR(new_dentry);
876 if (old_dentry == new_dentry)
880 if (new_dentry->d_inode)
883 /* rename kobject and sysfs_dirent */
885 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
889 error = kobject_set_name(kobj, "%s", new_name);
893 mutex_lock(&sysfs_mutex);
894 dup_name = sd->s_name;
895 sd->s_name = new_name;
896 mutex_unlock(&sysfs_mutex);
899 d_add(new_dentry, NULL);
900 d_move(sd->s_dentry, new_dentry);
908 mutex_unlock(&parent->d_inode->i_mutex);
916 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
918 struct sysfs_dirent *sd = kobj->sd;
919 struct sysfs_dirent *new_parent_sd;
920 struct dentry *old_parent, *new_parent = NULL;
921 struct dentry *old_dentry = NULL, *new_dentry = NULL;
924 BUG_ON(!sd->s_parent);
925 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
928 old_dentry = sysfs_get_dentry(sd);
929 if (IS_ERR(old_dentry)) {
930 error = PTR_ERR(old_dentry);
933 old_parent = sd->s_parent->s_dentry;
935 new_parent = sysfs_get_dentry(new_parent_sd);
936 if (IS_ERR(new_parent)) {
937 error = PTR_ERR(new_parent);
941 if (old_parent->d_inode == new_parent->d_inode) {
943 goto out_dput; /* nothing to move */
946 mutex_lock(&old_parent->d_inode->i_mutex);
947 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
948 mutex_unlock(&old_parent->d_inode->i_mutex);
952 new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
953 if (IS_ERR(new_dentry)) {
954 error = PTR_ERR(new_dentry);
958 d_add(new_dentry, NULL);
959 d_move(sd->s_dentry, new_dentry);
962 /* Remove from old parent's list and insert into new parent's list. */
963 mutex_lock(&sysfs_mutex);
965 sysfs_unlink_sibling(sd);
966 sysfs_get(new_parent_sd);
967 sysfs_put(sd->s_parent);
968 sd->s_parent = new_parent_sd;
969 sysfs_link_sibling(sd);
971 mutex_unlock(&sysfs_mutex);
974 mutex_unlock(&new_parent->d_inode->i_mutex);
975 mutex_unlock(&old_parent->d_inode->i_mutex);
983 static int sysfs_dir_open(struct inode *inode, struct file *file)
985 struct dentry * dentry = file->f_path.dentry;
986 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
987 struct sysfs_dirent * sd;
989 sd = sysfs_new_dirent("_DIR_", 0, 0);
991 mutex_lock(&sysfs_mutex);
992 sd->s_parent = sysfs_get(parent_sd);
993 sysfs_link_sibling(sd);
994 mutex_unlock(&sysfs_mutex);
997 file->private_data = sd;
998 return sd ? 0 : -ENOMEM;
1001 static int sysfs_dir_close(struct inode *inode, struct file *file)
1003 struct sysfs_dirent * cursor = file->private_data;
1005 mutex_lock(&sysfs_mutex);
1006 sysfs_unlink_sibling(cursor);
1007 mutex_unlock(&sysfs_mutex);
1009 release_sysfs_dirent(cursor);
1014 /* Relationship between s_mode and the DT_xxx types */
1015 static inline unsigned char dt_type(struct sysfs_dirent *sd)
1017 return (sd->s_mode >> 12) & 15;
1020 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1022 struct dentry *dentry = filp->f_path.dentry;
1023 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1024 struct sysfs_dirent *cursor = filp->private_data;
1025 struct sysfs_dirent **pos;
1027 int i = filp->f_pos;
1031 ino = parent_sd->s_ino;
1032 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1038 if (parent_sd->s_parent)
1039 ino = parent_sd->s_parent->s_ino;
1041 ino = parent_sd->s_ino;
1042 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1048 mutex_lock(&sysfs_mutex);
1050 pos = &parent_sd->s_children;
1051 while (*pos != cursor)
1052 pos = &(*pos)->s_sibling;
1055 *pos = cursor->s_sibling;
1057 if (filp->f_pos == 2)
1058 pos = &parent_sd->s_children;
1060 for ( ; *pos; pos = &(*pos)->s_sibling) {
1061 struct sysfs_dirent *next = *pos;
1065 if (!sysfs_type(next))
1068 name = next->s_name;
1072 if (filldir(dirent, name, len, filp->f_pos, ino,
1079 /* put cursor back in */
1080 cursor->s_sibling = *pos;
1083 mutex_unlock(&sysfs_mutex);
1088 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1090 struct dentry * dentry = file->f_path.dentry;
1094 offset += file->f_pos;
1101 if (offset != file->f_pos) {
1102 mutex_lock(&sysfs_mutex);
1104 file->f_pos = offset;
1105 if (file->f_pos >= 2) {
1106 struct sysfs_dirent *sd = dentry->d_fsdata;
1107 struct sysfs_dirent *cursor = file->private_data;
1108 struct sysfs_dirent **pos;
1109 loff_t n = file->f_pos - 2;
1111 sysfs_unlink_sibling(cursor);
1113 pos = &sd->s_children;
1115 struct sysfs_dirent *next = *pos;
1116 if (sysfs_type(next))
1118 pos = &(*pos)->s_sibling;
1121 cursor->s_sibling = *pos;
1125 mutex_unlock(&sysfs_mutex);
1131 const struct file_operations sysfs_dir_operations = {
1132 .open = sysfs_dir_open,
1133 .release = sysfs_dir_close,
1134 .llseek = sysfs_dir_lseek,
1135 .read = generic_read_dir,
1136 .readdir = sysfs_readdir,