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 DECLARE_RWSEM(sysfs_rename_sem
);
18 spinlock_t sysfs_lock
= SPIN_LOCK_UNLOCKED
;
19 spinlock_t kobj_sysfs_assoc_lock
= SPIN_LOCK_UNLOCKED
;
21 static spinlock_t sysfs_ino_lock
= SPIN_LOCK_UNLOCKED
;
22 static DEFINE_IDA(sysfs_ino_ida
);
25 * sysfs_get_active - get an active reference to sysfs_dirent
26 * @sd: sysfs_dirent to get an active reference to
28 * Get an active reference of @sd. This function is noop if @sd
32 * Pointer to @sd on success, NULL on failure.
34 struct sysfs_dirent
*sysfs_get_active(struct sysfs_dirent
*sd
)
42 v
= atomic_read(&sd
->s_active
);
46 t
= atomic_cmpxchg(&sd
->s_active
, v
, v
+ 1);
57 * sysfs_put_active - put an active reference to sysfs_dirent
58 * @sd: sysfs_dirent to put an active reference to
60 * Put an active reference to @sd. This function is noop if @sd
63 void sysfs_put_active(struct sysfs_dirent
*sd
)
65 struct completion
*cmpl
;
71 v
= atomic_dec_return(&sd
->s_active
);
72 if (likely(v
!= SD_DEACTIVATED_BIAS
))
75 /* atomic_dec_return() is a mb(), we'll always see the updated
78 cmpl
= (void *)sd
->s_sibling
.next
;
83 * sysfs_get_active_two - get active references to sysfs_dirent and parent
84 * @sd: sysfs_dirent of interest
86 * Get active reference to @sd and its parent. Parent's active
87 * reference is grabbed first. This function is noop if @sd is
91 * Pointer to @sd on success, NULL on failure.
93 struct sysfs_dirent
*sysfs_get_active_two(struct sysfs_dirent
*sd
)
96 if (sd
->s_parent
&& unlikely(!sysfs_get_active(sd
->s_parent
)))
98 if (unlikely(!sysfs_get_active(sd
))) {
99 sysfs_put_active(sd
->s_parent
);
107 * sysfs_put_active_two - put active references to sysfs_dirent and parent
108 * @sd: sysfs_dirent of interest
110 * Put active references to @sd and its parent. This function is
111 * noop if @sd is NULL.
113 void sysfs_put_active_two(struct sysfs_dirent
*sd
)
116 sysfs_put_active(sd
);
117 sysfs_put_active(sd
->s_parent
);
122 * sysfs_deactivate - deactivate sysfs_dirent
123 * @sd: sysfs_dirent to deactivate
125 * Deny new active references and drain existing ones.
127 void sysfs_deactivate(struct sysfs_dirent
*sd
)
129 DECLARE_COMPLETION_ONSTACK(wait
);
132 BUG_ON(!list_empty(&sd
->s_sibling
));
133 sd
->s_sibling
.next
= (void *)&wait
;
135 /* atomic_add_return() is a mb(), put_active() will always see
136 * the updated sd->s_sibling.next.
138 v
= atomic_add_return(SD_DEACTIVATED_BIAS
, &sd
->s_active
);
140 if (v
!= SD_DEACTIVATED_BIAS
)
141 wait_for_completion(&wait
);
143 INIT_LIST_HEAD(&sd
->s_sibling
);
146 static int sysfs_alloc_ino(ino_t
*pino
)
151 spin_lock(&sysfs_ino_lock
);
152 rc
= ida_get_new_above(&sysfs_ino_ida
, 2, &ino
);
153 spin_unlock(&sysfs_ino_lock
);
156 if (ida_pre_get(&sysfs_ino_ida
, GFP_KERNEL
))
165 static void sysfs_free_ino(ino_t ino
)
167 spin_lock(&sysfs_ino_lock
);
168 ida_remove(&sysfs_ino_ida
, ino
);
169 spin_unlock(&sysfs_ino_lock
);
172 void release_sysfs_dirent(struct sysfs_dirent
* sd
)
174 struct sysfs_dirent
*parent_sd
;
177 parent_sd
= sd
->s_parent
;
179 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
180 sysfs_put(sd
->s_elem
.symlink
.target_sd
);
181 if (sd
->s_type
& SYSFS_COPY_NAME
)
184 sysfs_free_ino(sd
->s_ino
);
185 kmem_cache_free(sysfs_dir_cachep
, sd
);
188 if (sd
&& atomic_dec_and_test(&sd
->s_count
))
192 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
194 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
197 /* sd->s_dentry is protected with sysfs_lock. This
198 * allows sysfs_drop_dentry() to dereference it.
200 spin_lock(&sysfs_lock
);
202 /* The dentry might have been deleted or another
203 * lookup could have happened updating sd->s_dentry to
204 * point the new dentry. Ignore if it isn't pointing
207 if (sd
->s_dentry
== dentry
)
209 spin_unlock(&sysfs_lock
);
215 static struct dentry_operations sysfs_dentry_ops
= {
216 .d_iput
= sysfs_d_iput
,
219 struct sysfs_dirent
*sysfs_new_dirent(const char *name
, umode_t mode
, int type
)
221 char *dup_name
= NULL
;
222 struct sysfs_dirent
*sd
= NULL
;
224 if (type
& SYSFS_COPY_NAME
) {
225 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
230 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
234 if (sysfs_alloc_ino(&sd
->s_ino
))
237 atomic_set(&sd
->s_count
, 1);
238 atomic_set(&sd
->s_active
, 0);
239 atomic_set(&sd
->s_event
, 1);
240 INIT_LIST_HEAD(&sd
->s_children
);
241 INIT_LIST_HEAD(&sd
->s_sibling
);
251 kmem_cache_free(sysfs_dir_cachep
, sd
);
255 static void sysfs_attach_dentry(struct sysfs_dirent
*sd
, struct dentry
*dentry
)
257 dentry
->d_op
= &sysfs_dentry_ops
;
258 dentry
->d_fsdata
= sysfs_get(sd
);
260 /* protect sd->s_dentry against sysfs_d_iput */
261 spin_lock(&sysfs_lock
);
262 sd
->s_dentry
= dentry
;
263 spin_unlock(&sysfs_lock
);
268 void sysfs_attach_dirent(struct sysfs_dirent
*sd
,
269 struct sysfs_dirent
*parent_sd
, struct dentry
*dentry
)
272 sysfs_attach_dentry(sd
, dentry
);
275 sd
->s_parent
= sysfs_get(parent_sd
);
276 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
282 * Return -EEXIST if there is already a sysfs element with the same name for
285 * called with parent inode's i_mutex held
287 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
288 const unsigned char *new)
290 struct sysfs_dirent
* sd
;
292 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
294 if (strcmp(sd
->s_name
, new))
304 static int create_dir(struct kobject
*kobj
, struct dentry
*parent
,
305 const char *name
, struct dentry
**p_dentry
)
308 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
309 struct dentry
*dentry
;
311 struct sysfs_dirent
*sd
;
313 mutex_lock(&parent
->d_inode
->i_mutex
);
316 dentry
= lookup_one_len(name
, parent
, strlen(name
));
317 if (IS_ERR(dentry
)) {
318 error
= PTR_ERR(dentry
);
327 sd
= sysfs_new_dirent(name
, mode
, SYSFS_DIR
);
330 sd
->s_elem
.dir
.kobj
= kobj
;
332 inode
= sysfs_get_inode(sd
);
336 if (inode
->i_state
& I_NEW
) {
337 inode
->i_op
= &sysfs_dir_inode_operations
;
338 inode
->i_fop
= &sysfs_dir_operations
;
339 /* directory inodes start off with i_nlink == 2 (for ".") */
345 if (sysfs_dirent_exist(parent
->d_fsdata
, name
))
348 sysfs_instantiate(dentry
, inode
);
349 inc_nlink(parent
->d_inode
);
350 sysfs_attach_dirent(sd
, parent
->d_fsdata
, dentry
);
354 goto out_unlock
; /* pin directory dentry in core */
365 mutex_unlock(&parent
->d_inode
->i_mutex
);
370 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
372 return create_dir(k
,k
->dentry
,n
,d
);
376 * sysfs_create_dir - create a directory for an object.
377 * @kobj: object we're creating directory for.
378 * @shadow_parent: parent parent object.
381 int sysfs_create_dir(struct kobject
* kobj
, struct dentry
*shadow_parent
)
383 struct dentry
* dentry
= NULL
;
384 struct dentry
* parent
;
390 parent
= shadow_parent
;
391 else if (kobj
->parent
)
392 parent
= kobj
->parent
->dentry
;
393 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
394 parent
= sysfs_mount
->mnt_sb
->s_root
;
398 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
400 kobj
->dentry
= dentry
;
404 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
405 struct nameidata
*nd
)
407 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
408 struct sysfs_dirent
* sd
;
412 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
413 if ((sd
->s_type
& SYSFS_NOT_PINNED
) &&
414 !strcmp(sd
->s_name
, dentry
->d_name
.name
)) {
424 /* attach dentry and inode */
425 inode
= sysfs_get_inode(sd
);
427 return ERR_PTR(-ENOMEM
);
429 if (inode
->i_state
& I_NEW
) {
430 /* initialize inode according to type */
431 if (sd
->s_type
& SYSFS_KOBJ_ATTR
) {
432 inode
->i_size
= PAGE_SIZE
;
433 inode
->i_fop
= &sysfs_file_operations
;
434 } else if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
435 struct bin_attribute
*bin_attr
=
436 sd
->s_elem
.bin_attr
.bin_attr
;
437 inode
->i_size
= bin_attr
->size
;
438 inode
->i_fop
= &bin_fops
;
439 } else if (sd
->s_type
& SYSFS_KOBJ_LINK
)
440 inode
->i_op
= &sysfs_symlink_inode_operations
;
443 sysfs_instantiate(dentry
, inode
);
444 sysfs_attach_dentry(sd
, dentry
);
449 const struct inode_operations sysfs_dir_inode_operations
= {
450 .lookup
= sysfs_lookup
,
451 .setattr
= sysfs_setattr
,
454 static void remove_dir(struct dentry
* d
)
456 struct dentry
*parent
= d
->d_parent
;
457 struct sysfs_dirent
*sd
= d
->d_fsdata
;
459 mutex_lock(&parent
->d_inode
->i_mutex
);
461 list_del_init(&sd
->s_sibling
);
463 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
464 atomic_read(&d
->d_count
));
466 mutex_unlock(&parent
->d_inode
->i_mutex
);
468 sysfs_drop_dentry(sd
);
469 sysfs_deactivate(sd
);
473 void sysfs_remove_subdir(struct dentry
* d
)
479 static void __sysfs_remove_dir(struct dentry
*dentry
)
482 struct sysfs_dirent
* parent_sd
;
483 struct sysfs_dirent
* sd
, * tmp
;
488 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
489 mutex_lock(&dentry
->d_inode
->i_mutex
);
490 parent_sd
= dentry
->d_fsdata
;
491 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
492 if (!sd
->s_type
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
494 list_move(&sd
->s_sibling
, &removed
);
496 mutex_unlock(&dentry
->d_inode
->i_mutex
);
498 list_for_each_entry_safe(sd
, tmp
, &removed
, s_sibling
) {
499 list_del_init(&sd
->s_sibling
);
500 sysfs_drop_dentry(sd
);
501 sysfs_deactivate(sd
);
509 * sysfs_remove_dir - remove an object's directory.
512 * The only thing special about this is that we remove any files in
513 * the directory before we remove the directory, and we've inlined
514 * what used to be sysfs_rmdir() below, instead of calling separately.
517 void sysfs_remove_dir(struct kobject
* kobj
)
519 struct dentry
*d
= kobj
->dentry
;
521 spin_lock(&kobj_sysfs_assoc_lock
);
523 spin_unlock(&kobj_sysfs_assoc_lock
);
525 __sysfs_remove_dir(d
);
528 int sysfs_rename_dir(struct kobject
* kobj
, struct dentry
*new_parent
,
529 const char *new_name
)
531 struct sysfs_dirent
*sd
= kobj
->dentry
->d_fsdata
;
532 struct sysfs_dirent
*parent_sd
= new_parent
->d_fsdata
;
533 struct dentry
*new_dentry
;
540 down_write(&sysfs_rename_sem
);
541 mutex_lock(&new_parent
->d_inode
->i_mutex
);
543 new_dentry
= lookup_one_len(new_name
, new_parent
, strlen(new_name
));
544 if (IS_ERR(new_dentry
)) {
545 error
= PTR_ERR(new_dentry
);
549 /* By allowing two different directories with the same
550 * d_parent we allow this routine to move between different
551 * shadows of the same directory
554 if (kobj
->dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
555 new_dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
556 new_dentry
== kobj
->dentry
)
560 if (new_dentry
->d_inode
)
563 /* rename kobject and sysfs_dirent */
565 new_name
= dup_name
= kstrdup(new_name
, GFP_KERNEL
);
569 error
= kobject_set_name(kobj
, "%s", new_name
);
574 sd
->s_name
= new_name
;
576 /* move under the new parent */
577 d_add(new_dentry
, NULL
);
578 d_move(kobj
->dentry
, new_dentry
);
580 list_del_init(&sd
->s_sibling
);
581 sysfs_get(parent_sd
);
582 sysfs_put(sd
->s_parent
);
583 sd
->s_parent
= parent_sd
;
584 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
596 mutex_unlock(&new_parent
->d_inode
->i_mutex
);
597 up_write(&sysfs_rename_sem
);
601 int sysfs_move_dir(struct kobject
*kobj
, struct kobject
*new_parent
)
603 struct dentry
*old_parent_dentry
, *new_parent_dentry
, *new_dentry
;
604 struct sysfs_dirent
*new_parent_sd
, *sd
;
607 old_parent_dentry
= kobj
->parent
?
608 kobj
->parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
609 new_parent_dentry
= new_parent
?
610 new_parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
612 if (old_parent_dentry
->d_inode
== new_parent_dentry
->d_inode
)
613 return 0; /* nothing to move */
615 mutex_lock(&old_parent_dentry
->d_inode
->i_mutex
);
616 if (!mutex_trylock(&new_parent_dentry
->d_inode
->i_mutex
)) {
617 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
621 new_parent_sd
= new_parent_dentry
->d_fsdata
;
622 sd
= kobj
->dentry
->d_fsdata
;
624 new_dentry
= lookup_one_len(kobj
->name
, new_parent_dentry
,
626 if (IS_ERR(new_dentry
)) {
627 error
= PTR_ERR(new_dentry
);
631 d_add(new_dentry
, NULL
);
632 d_move(kobj
->dentry
, new_dentry
);
635 /* Remove from old parent's list and insert into new parent's list. */
636 list_del_init(&sd
->s_sibling
);
637 sysfs_get(new_parent_sd
);
638 sysfs_put(sd
->s_parent
);
639 sd
->s_parent
= new_parent_sd
;
640 list_add(&sd
->s_sibling
, &new_parent_sd
->s_children
);
643 mutex_unlock(&new_parent_dentry
->d_inode
->i_mutex
);
644 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
649 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
651 struct dentry
* dentry
= file
->f_path
.dentry
;
652 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
653 struct sysfs_dirent
* sd
;
655 mutex_lock(&dentry
->d_inode
->i_mutex
);
656 sd
= sysfs_new_dirent("_DIR_", 0, 0);
658 sysfs_attach_dirent(sd
, parent_sd
, NULL
);
659 mutex_unlock(&dentry
->d_inode
->i_mutex
);
661 file
->private_data
= sd
;
662 return sd
? 0 : -ENOMEM
;
665 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
667 struct dentry
* dentry
= file
->f_path
.dentry
;
668 struct sysfs_dirent
* cursor
= file
->private_data
;
670 mutex_lock(&dentry
->d_inode
->i_mutex
);
671 list_del_init(&cursor
->s_sibling
);
672 mutex_unlock(&dentry
->d_inode
->i_mutex
);
674 release_sysfs_dirent(cursor
);
679 /* Relationship between s_mode and the DT_xxx types */
680 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
682 return (sd
->s_mode
>> 12) & 15;
685 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
687 struct dentry
*dentry
= filp
->f_path
.dentry
;
688 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
689 struct sysfs_dirent
*cursor
= filp
->private_data
;
690 struct list_head
*p
, *q
= &cursor
->s_sibling
;
696 ino
= parent_sd
->s_ino
;
697 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
703 if (parent_sd
->s_parent
)
704 ino
= parent_sd
->s_parent
->s_ino
;
706 ino
= parent_sd
->s_ino
;
707 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
713 if (filp
->f_pos
== 2)
714 list_move(q
, &parent_sd
->s_children
);
716 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
717 struct sysfs_dirent
*next
;
721 next
= list_entry(p
, struct sysfs_dirent
,
730 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
742 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
744 struct dentry
* dentry
= file
->f_path
.dentry
;
746 mutex_lock(&dentry
->d_inode
->i_mutex
);
749 offset
+= file
->f_pos
;
754 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
757 if (offset
!= file
->f_pos
) {
758 file
->f_pos
= offset
;
759 if (file
->f_pos
>= 2) {
760 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
761 struct sysfs_dirent
*cursor
= file
->private_data
;
763 loff_t n
= file
->f_pos
- 2;
765 list_del(&cursor
->s_sibling
);
766 p
= sd
->s_children
.next
;
767 while (n
&& p
!= &sd
->s_children
) {
768 struct sysfs_dirent
*next
;
769 next
= list_entry(p
, struct sysfs_dirent
,
775 list_add_tail(&cursor
->s_sibling
, p
);
778 mutex_unlock(&dentry
->d_inode
->i_mutex
);
784 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
785 * @kobj: object we're creating shadow of.
788 int sysfs_make_shadowed_dir(struct kobject
*kobj
,
789 void * (*follow_link
)(struct dentry
*, struct nameidata
*))
792 struct inode_operations
*i_op
;
794 inode
= kobj
->dentry
->d_inode
;
795 if (inode
->i_op
!= &sysfs_dir_inode_operations
)
798 i_op
= kmalloc(sizeof(*i_op
), GFP_KERNEL
);
802 memcpy(i_op
, &sysfs_dir_inode_operations
, sizeof(*i_op
));
803 i_op
->follow_link
= follow_link
;
805 /* Locking of inode->i_op?
806 * Since setting i_op is a single word write and they
807 * are atomic we should be ok here.
814 * sysfs_create_shadow_dir - create a shadow directory for an object.
815 * @kobj: object we're creating directory for.
817 * sysfs_make_shadowed_dir must already have been called on this
821 struct dentry
*sysfs_create_shadow_dir(struct kobject
*kobj
)
823 struct dentry
*dir
= kobj
->dentry
;
824 struct inode
*inode
= dir
->d_inode
;
825 struct dentry
*parent
= dir
->d_parent
;
826 struct sysfs_dirent
*parent_sd
= parent
->d_fsdata
;
827 struct dentry
*shadow
;
828 struct sysfs_dirent
*sd
;
830 shadow
= ERR_PTR(-EINVAL
);
831 if (!sysfs_is_shadowed_inode(inode
))
834 shadow
= d_alloc(parent
, &dir
->d_name
);
838 sd
= sysfs_new_dirent("_SHADOW_", inode
->i_mode
, SYSFS_DIR
);
841 sd
->s_elem
.dir
.kobj
= kobj
;
842 /* point to parent_sd but don't attach to it */
843 sd
->s_parent
= sysfs_get(parent_sd
);
844 sysfs_attach_dirent(sd
, NULL
, shadow
);
846 d_instantiate(shadow
, igrab(inode
));
848 inc_nlink(parent
->d_inode
);
850 dget(shadow
); /* Extra count - pin the dentry in core */
856 shadow
= ERR_PTR(-ENOMEM
);
861 * sysfs_remove_shadow_dir - remove an object's directory.
862 * @shadow: dentry of shadow directory
864 * The only thing special about this is that we remove any files in
865 * the directory before we remove the directory, and we've inlined
866 * what used to be sysfs_rmdir() below, instead of calling separately.
869 void sysfs_remove_shadow_dir(struct dentry
*shadow
)
871 __sysfs_remove_dir(shadow
);
874 const struct file_operations sysfs_dir_operations
= {
875 .open
= sysfs_dir_open
,
876 .release
= sysfs_dir_close
,
877 .llseek
= sysfs_dir_lseek
,
878 .read
= generic_read_dir
,
879 .readdir
= sysfs_readdir
,