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 <asm/semaphore.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 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 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 = NULL;
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);
391 kmem_cache_free(sysfs_dir_cachep, sd);
396 * sysfs_attach_dentry - associate sysfs_dirent with dentry
397 * @sd: target sysfs_dirent
398 * @dentry: dentry to associate
400 * Associate @sd with @dentry. This is protected by
401 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
404 * mutex_lock(sysfs_mutex)
406 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
408 dentry->d_op = &sysfs_dentry_ops;
409 dentry->d_fsdata = sysfs_get(sd);
411 /* protect sd->s_dentry against sysfs_d_iput */
412 spin_lock(&sysfs_assoc_lock);
413 sd->s_dentry = dentry;
414 spin_unlock(&sysfs_assoc_lock);
419 static int sysfs_ilookup_test(struct inode *inode, void *arg)
421 struct sysfs_dirent *sd = arg;
422 return inode->i_ino == sd->s_ino;
426 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
427 * @acxt: pointer to sysfs_addrm_cxt to be used
428 * @parent_sd: parent sysfs_dirent
430 * This function is called when the caller is about to add or
431 * remove sysfs_dirent under @parent_sd. This function acquires
432 * sysfs_mutex, grabs inode for @parent_sd if available and lock
433 * i_mutex of it. @acxt is used to keep and pass context to
434 * other addrm functions.
437 * Kernel thread context (may sleep). sysfs_mutex is locked on
438 * return. i_mutex of parent inode is locked on return if
441 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
442 struct sysfs_dirent *parent_sd)
446 memset(acxt, 0, sizeof(*acxt));
447 acxt->parent_sd = parent_sd;
449 /* Lookup parent inode. inode initialization and I_NEW
450 * clearing are protected by sysfs_mutex. By grabbing it and
451 * looking up with _nowait variant, inode state can be
452 * determined reliably.
454 mutex_lock(&sysfs_mutex);
456 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
459 if (inode && !(inode->i_state & I_NEW)) {
460 /* parent inode available */
461 acxt->parent_inode = inode;
463 /* sysfs_mutex is below i_mutex in lock hierarchy.
464 * First, trylock i_mutex. If fails, unlock
465 * sysfs_mutex and lock them in order.
467 if (!mutex_trylock(&inode->i_mutex)) {
468 mutex_unlock(&sysfs_mutex);
469 mutex_lock(&inode->i_mutex);
470 mutex_lock(&sysfs_mutex);
477 * sysfs_add_one - add sysfs_dirent to parent
478 * @acxt: addrm context to use
479 * @sd: sysfs_dirent to be added
481 * Get @acxt->parent_sd and set sd->s_parent to it and increment
482 * nlink of parent inode if @sd is a directory. @sd is NOT
483 * linked into the children list of the parent. The caller
484 * should invoke sysfs_link_sibling() after this function
485 * completes if @sd needs to be on the children list.
487 * This function should be called between calls to
488 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
489 * passed the same @acxt as passed to sysfs_addrm_start().
492 * Determined by sysfs_addrm_start().
494 void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
496 sd->s_parent = sysfs_get(acxt->parent_sd);
498 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
499 inc_nlink(acxt->parent_inode);
505 * sysfs_remove_one - remove sysfs_dirent from parent
506 * @acxt: addrm context to use
507 * @sd: sysfs_dirent to be added
509 * Mark @sd removed and drop nlink of parent inode if @sd is a
510 * directory. @sd is NOT unlinked from the children list of the
511 * parent. The caller is repsonsible for removing @sd from the
512 * children list before calling this function.
514 * This function should be called between calls to
515 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
516 * passed the same @acxt as passed to sysfs_addrm_start().
519 * Determined by sysfs_addrm_start().
521 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
523 BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED));
525 sd->s_flags |= SYSFS_FLAG_REMOVED;
526 sd->s_sibling = acxt->removed;
529 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
530 drop_nlink(acxt->parent_inode);
536 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
537 * @sd: target sysfs_dirent
539 * Drop dentry for @sd. @sd must have been unlinked from its
540 * parent on entry to this function such that it can't be looked
543 * @sd->s_dentry which is protected with sysfs_assoc_lock points
544 * to the currently associated dentry but we're not holding a
545 * reference to it and racing with dput(). Grab dcache_lock and
546 * verify dentry before dropping it. If @sd->s_dentry is NULL or
547 * dput() beats us, no need to bother.
549 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
551 struct dentry *dentry = NULL;
554 /* We're not holding a reference to ->s_dentry dentry but the
555 * field will stay valid as long as sysfs_assoc_lock is held.
557 spin_lock(&sysfs_assoc_lock);
558 spin_lock(&dcache_lock);
560 /* drop dentry if it's there and dput() didn't kill it yet */
561 if (sd->s_dentry && sd->s_dentry->d_inode) {
562 dentry = dget_locked(sd->s_dentry);
563 spin_lock(&dentry->d_lock);
565 spin_unlock(&dentry->d_lock);
568 spin_unlock(&dcache_lock);
569 spin_unlock(&sysfs_assoc_lock);
572 /* XXX: unpin if directory, this will go away soon */
573 if (sysfs_type(sd) == SYSFS_DIR)
576 /* adjust nlink and update timestamp */
577 inode = ilookup(sysfs_sb, sd->s_ino);
579 mutex_lock(&inode->i_mutex);
581 inode->i_ctime = CURRENT_TIME;
583 if (sysfs_type(sd) == SYSFS_DIR)
586 mutex_unlock(&inode->i_mutex);
592 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
593 * @acxt: addrm context to finish up
595 * Finish up sysfs_dirent add/remove. Resources acquired by
596 * sysfs_addrm_start() are released and removed sysfs_dirents are
597 * cleaned up. Timestamps on the parent inode are updated.
600 * All mutexes acquired by sysfs_addrm_start() are released.
603 * Number of added/removed sysfs_dirents since sysfs_addrm_start().
605 int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
607 /* release resources acquired by sysfs_addrm_start() */
608 mutex_unlock(&sysfs_mutex);
609 if (acxt->parent_inode) {
610 struct inode *inode = acxt->parent_inode;
612 /* if added/removed, update timestamps on the parent */
614 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
616 mutex_unlock(&inode->i_mutex);
620 /* kill removed sysfs_dirents */
621 while (acxt->removed) {
622 struct sysfs_dirent *sd = acxt->removed;
624 acxt->removed = sd->s_sibling;
625 sd->s_sibling = NULL;
627 sysfs_drop_dentry(sd);
628 sysfs_deactivate(sd);
636 * sysfs_find_dirent - find sysfs_dirent with the given name
637 * @parent_sd: sysfs_dirent to search under
638 * @name: name to look for
640 * Look for sysfs_dirent with name @name under @parent_sd.
643 * mutex_lock(sysfs_mutex)
646 * Pointer to sysfs_dirent if found, NULL if not.
648 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
649 const unsigned char *name)
651 struct sysfs_dirent *sd;
653 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
654 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
660 * sysfs_get_dirent - find and get sysfs_dirent with the given name
661 * @parent_sd: sysfs_dirent to search under
662 * @name: name to look for
664 * Look for sysfs_dirent with name @name under @parent_sd and get
668 * Kernel thread context (may sleep). Grabs sysfs_mutex.
671 * Pointer to sysfs_dirent if found, NULL if not.
673 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
674 const unsigned char *name)
676 struct sysfs_dirent *sd;
678 mutex_lock(&sysfs_mutex);
679 sd = sysfs_find_dirent(parent_sd, name);
681 mutex_unlock(&sysfs_mutex);
686 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
687 const char *name, struct sysfs_dirent **p_sd)
689 struct dentry *parent = parent_sd->s_dentry;
690 struct sysfs_addrm_cxt acxt;
692 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
693 struct dentry *dentry;
695 struct sysfs_dirent *sd;
697 sysfs_addrm_start(&acxt, parent_sd);
700 dentry = lookup_one_len(name, parent, strlen(name));
701 if (IS_ERR(dentry)) {
702 error = PTR_ERR(dentry);
711 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
714 sd->s_elem.dir.kobj = kobj;
716 inode = sysfs_get_inode(sd);
720 if (inode->i_state & I_NEW) {
721 inode->i_op = &sysfs_dir_inode_operations;
722 inode->i_fop = &sysfs_dir_operations;
723 /* directory inodes start off with i_nlink == 2 (for ".") */
729 if (sysfs_find_dirent(parent_sd, name))
732 sysfs_add_one(&acxt, sd);
733 sysfs_link_sibling(sd);
734 sysfs_instantiate(dentry, inode);
735 sysfs_attach_dentry(sd, dentry);
739 goto out_finish; /* pin directory dentry in core */
750 sysfs_addrm_finish(&acxt);
754 int sysfs_create_subdir(struct kobject *kobj, const char *name,
755 struct sysfs_dirent **p_sd)
757 return create_dir(kobj, kobj->sd, name, p_sd);
761 * sysfs_create_dir - create a directory for an object.
762 * @kobj: object we're creating directory for.
763 * @shadow_parent: parent object.
765 int sysfs_create_dir(struct kobject *kobj,
766 struct sysfs_dirent *shadow_parent_sd)
768 struct sysfs_dirent *parent_sd, *sd;
773 if (shadow_parent_sd)
774 parent_sd = shadow_parent_sd;
775 else if (kobj->parent)
776 parent_sd = kobj->parent->sd;
777 else if (sysfs_mount && sysfs_mount->mnt_sb)
778 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
782 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
788 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
789 struct nameidata *nd)
791 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
792 struct sysfs_dirent * sd;
793 struct bin_attribute *bin_attr;
797 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
798 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
799 !strcmp(sd->s_name, dentry->d_name.name)) {
809 /* attach dentry and inode */
810 inode = sysfs_get_inode(sd);
812 return ERR_PTR(-ENOMEM);
814 mutex_lock(&sysfs_mutex);
816 if (inode->i_state & I_NEW) {
817 /* initialize inode according to type */
818 switch (sysfs_type(sd)) {
819 case SYSFS_KOBJ_ATTR:
820 inode->i_size = PAGE_SIZE;
821 inode->i_fop = &sysfs_file_operations;
823 case SYSFS_KOBJ_BIN_ATTR:
824 bin_attr = sd->s_elem.bin_attr.bin_attr;
825 inode->i_size = bin_attr->size;
826 inode->i_fop = &bin_fops;
828 case SYSFS_KOBJ_LINK:
829 inode->i_op = &sysfs_symlink_inode_operations;
836 sysfs_instantiate(dentry, inode);
837 sysfs_attach_dentry(sd, dentry);
839 mutex_unlock(&sysfs_mutex);
844 const struct inode_operations sysfs_dir_inode_operations = {
845 .lookup = sysfs_lookup,
846 .setattr = sysfs_setattr,
849 static void remove_dir(struct sysfs_dirent *sd)
851 struct sysfs_addrm_cxt acxt;
853 sysfs_addrm_start(&acxt, sd->s_parent);
854 sysfs_unlink_sibling(sd);
855 sysfs_remove_one(&acxt, sd);
856 sysfs_addrm_finish(&acxt);
859 void sysfs_remove_subdir(struct sysfs_dirent *sd)
865 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
867 struct sysfs_addrm_cxt acxt;
868 struct sysfs_dirent **pos;
873 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
874 sysfs_addrm_start(&acxt, dir_sd);
875 pos = &dir_sd->s_children;
877 struct sysfs_dirent *sd = *pos;
879 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
880 *pos = sd->s_sibling;
881 sd->s_sibling = NULL;
882 sysfs_remove_one(&acxt, sd);
884 pos = &(*pos)->s_sibling;
886 sysfs_addrm_finish(&acxt);
892 * sysfs_remove_dir - remove an object's directory.
895 * The only thing special about this is that we remove any files in
896 * the directory before we remove the directory, and we've inlined
897 * what used to be sysfs_rmdir() below, instead of calling separately.
900 void sysfs_remove_dir(struct kobject * kobj)
902 struct sysfs_dirent *sd = kobj->sd;
904 spin_lock(&sysfs_assoc_lock);
906 spin_unlock(&sysfs_assoc_lock);
908 __sysfs_remove_dir(sd);
911 int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
912 const char *new_name)
914 struct sysfs_dirent *sd = kobj->sd;
915 struct dentry *new_parent = new_parent_sd->s_dentry;
916 struct dentry *new_dentry;
923 mutex_lock(&new_parent->d_inode->i_mutex);
925 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
926 if (IS_ERR(new_dentry)) {
927 error = PTR_ERR(new_dentry);
931 /* By allowing two different directories with the same
932 * d_parent we allow this routine to move between different
933 * shadows of the same directory
936 if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
937 new_dentry->d_parent->d_inode != new_parent->d_inode ||
938 new_dentry == sd->s_dentry)
942 if (new_dentry->d_inode)
945 /* rename kobject and sysfs_dirent */
947 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
951 error = kobject_set_name(kobj, "%s", new_name);
956 sd->s_name = new_name;
958 /* move under the new parent */
959 d_add(new_dentry, NULL);
960 d_move(sd->s_dentry, new_dentry);
962 mutex_lock(&sysfs_mutex);
964 sysfs_unlink_sibling(sd);
965 sysfs_get(new_parent_sd);
966 sysfs_put(sd->s_parent);
967 sd->s_parent = new_parent_sd;
968 sysfs_link_sibling(sd);
970 mutex_unlock(&sysfs_mutex);
982 mutex_unlock(&new_parent->d_inode->i_mutex);
986 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
988 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
989 struct sysfs_dirent *new_parent_sd, *sd;
992 old_parent_dentry = kobj->parent ?
993 kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
994 new_parent_dentry = new_parent ?
995 new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
997 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
998 return 0; /* nothing to move */
1000 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
1001 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
1002 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
1006 new_parent_sd = new_parent_dentry->d_fsdata;
1009 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
1010 strlen(kobj->name));
1011 if (IS_ERR(new_dentry)) {
1012 error = PTR_ERR(new_dentry);
1016 d_add(new_dentry, NULL);
1017 d_move(sd->s_dentry, new_dentry);
1020 /* Remove from old parent's list and insert into new parent's list. */
1021 mutex_lock(&sysfs_mutex);
1023 sysfs_unlink_sibling(sd);
1024 sysfs_get(new_parent_sd);
1025 sysfs_put(sd->s_parent);
1026 sd->s_parent = new_parent_sd;
1027 sysfs_link_sibling(sd);
1029 mutex_unlock(&sysfs_mutex);
1031 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
1032 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
1037 static int sysfs_dir_open(struct inode *inode, struct file *file)
1039 struct dentry * dentry = file->f_path.dentry;
1040 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1041 struct sysfs_dirent * sd;
1043 sd = sysfs_new_dirent("_DIR_", 0, 0);
1045 mutex_lock(&sysfs_mutex);
1046 sd->s_parent = sysfs_get(parent_sd);
1047 sysfs_link_sibling(sd);
1048 mutex_unlock(&sysfs_mutex);
1051 file->private_data = sd;
1052 return sd ? 0 : -ENOMEM;
1055 static int sysfs_dir_close(struct inode *inode, struct file *file)
1057 struct sysfs_dirent * cursor = file->private_data;
1059 mutex_lock(&sysfs_mutex);
1060 sysfs_unlink_sibling(cursor);
1061 mutex_unlock(&sysfs_mutex);
1063 release_sysfs_dirent(cursor);
1068 /* Relationship between s_mode and the DT_xxx types */
1069 static inline unsigned char dt_type(struct sysfs_dirent *sd)
1071 return (sd->s_mode >> 12) & 15;
1074 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1076 struct dentry *dentry = filp->f_path.dentry;
1077 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1078 struct sysfs_dirent *cursor = filp->private_data;
1079 struct sysfs_dirent **pos;
1081 int i = filp->f_pos;
1085 ino = parent_sd->s_ino;
1086 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1092 if (parent_sd->s_parent)
1093 ino = parent_sd->s_parent->s_ino;
1095 ino = parent_sd->s_ino;
1096 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1102 mutex_lock(&sysfs_mutex);
1104 pos = &parent_sd->s_children;
1105 while (*pos != cursor)
1106 pos = &(*pos)->s_sibling;
1109 *pos = cursor->s_sibling;
1111 if (filp->f_pos == 2)
1112 pos = &parent_sd->s_children;
1114 for ( ; *pos; pos = &(*pos)->s_sibling) {
1115 struct sysfs_dirent *next = *pos;
1119 if (!sysfs_type(next))
1122 name = next->s_name;
1126 if (filldir(dirent, name, len, filp->f_pos, ino,
1133 /* put cursor back in */
1134 cursor->s_sibling = *pos;
1137 mutex_unlock(&sysfs_mutex);
1142 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1144 struct dentry * dentry = file->f_path.dentry;
1148 offset += file->f_pos;
1155 if (offset != file->f_pos) {
1156 mutex_lock(&sysfs_mutex);
1158 file->f_pos = offset;
1159 if (file->f_pos >= 2) {
1160 struct sysfs_dirent *sd = dentry->d_fsdata;
1161 struct sysfs_dirent *cursor = file->private_data;
1162 struct sysfs_dirent **pos;
1163 loff_t n = file->f_pos - 2;
1165 sysfs_unlink_sibling(cursor);
1167 pos = &sd->s_children;
1169 struct sysfs_dirent *next = *pos;
1170 if (sysfs_type(next))
1172 pos = &(*pos)->s_sibling;
1175 cursor->s_sibling = *pos;
1179 mutex_unlock(&sysfs_mutex);
1187 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
1188 * @kobj: object we're creating shadow of.
1191 int sysfs_make_shadowed_dir(struct kobject *kobj,
1192 void * (*follow_link)(struct dentry *, struct nameidata *))
1194 struct inode *inode;
1195 struct inode_operations *i_op;
1197 inode = kobj->sd->s_dentry->d_inode;
1198 if (inode->i_op != &sysfs_dir_inode_operations)
1201 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
1205 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
1206 i_op->follow_link = follow_link;
1208 /* Locking of inode->i_op?
1209 * Since setting i_op is a single word write and they
1210 * are atomic we should be ok here.
1217 * sysfs_create_shadow_dir - create a shadow directory for an object.
1218 * @kobj: object we're creating directory for.
1220 * sysfs_make_shadowed_dir must already have been called on this
1224 struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
1226 struct dentry *dir = kobj->sd->s_dentry;
1227 struct inode *inode = dir->d_inode;
1228 struct dentry *parent = dir->d_parent;
1229 struct sysfs_dirent *parent_sd = parent->d_fsdata;
1230 struct dentry *shadow;
1231 struct sysfs_dirent *sd;
1232 struct sysfs_addrm_cxt acxt;
1234 sd = ERR_PTR(-EINVAL);
1235 if (!sysfs_is_shadowed_inode(inode))
1238 shadow = d_alloc(parent, &dir->d_name);
1242 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
1245 sd->s_elem.dir.kobj = kobj;
1247 sysfs_addrm_start(&acxt, parent_sd);
1249 /* add but don't link into children list */
1250 sysfs_add_one(&acxt, sd);
1252 /* attach and instantiate dentry */
1253 sysfs_attach_dentry(sd, shadow);
1254 d_instantiate(shadow, igrab(inode));
1255 inc_nlink(inode); /* tj: synchronization? */
1257 sysfs_addrm_finish(&acxt);
1259 dget(shadow); /* Extra count - pin the dentry in core */
1265 sd = ERR_PTR(-ENOMEM);
1270 * sysfs_remove_shadow_dir - remove an object's directory.
1271 * @shadow_sd: sysfs_dirent of shadow directory
1273 * The only thing special about this is that we remove any files in
1274 * the directory before we remove the directory, and we've inlined
1275 * what used to be sysfs_rmdir() below, instead of calling separately.
1278 void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
1280 __sysfs_remove_dir(shadow_sd);
1283 const struct file_operations sysfs_dir_operations = {
1284 .open = sysfs_dir_open,
1285 .release = sysfs_dir_close,
1286 .llseek = sysfs_dir_lseek,
1287 .read = generic_read_dir,
1288 .readdir = sysfs_readdir,