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 <asm/semaphore.h>
16 DECLARE_RWSEM(sysfs_rename_sem
);
17 spinlock_t sysfs_lock
= SPIN_LOCK_UNLOCKED
;
19 static spinlock_t sysfs_ino_lock
= SPIN_LOCK_UNLOCKED
;
20 static DEFINE_IDA(sysfs_ino_ida
);
22 int sysfs_alloc_ino(ino_t
*pino
)
27 spin_lock(&sysfs_ino_lock
);
28 rc
= ida_get_new_above(&sysfs_ino_ida
, 2, &ino
);
29 spin_unlock(&sysfs_ino_lock
);
32 if (ida_pre_get(&sysfs_ino_ida
, GFP_KERNEL
))
41 static void sysfs_free_ino(ino_t ino
)
43 spin_lock(&sysfs_ino_lock
);
44 ida_remove(&sysfs_ino_ida
, ino
);
45 spin_unlock(&sysfs_ino_lock
);
48 void release_sysfs_dirent(struct sysfs_dirent
* sd
)
50 struct sysfs_dirent
*parent_sd
;
53 parent_sd
= sd
->s_parent
;
55 if (sd
->s_type
& SYSFS_KOBJ_LINK
) {
56 struct sysfs_symlink
* sl
= sd
->s_element
;
57 kobject_put(sl
->target_kobj
);
60 if (sd
->s_type
& SYSFS_COPY_NAME
)
63 sysfs_free_ino(sd
->s_ino
);
64 kmem_cache_free(sysfs_dir_cachep
, sd
);
67 if (sd
&& atomic_dec_and_test(&sd
->s_count
))
71 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
73 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
76 /* sd->s_dentry is protected with sysfs_lock. This
77 * allows sysfs_drop_dentry() to dereference it.
79 spin_lock(&sysfs_lock
);
81 /* The dentry might have been deleted or another
82 * lookup could have happened updating sd->s_dentry to
83 * point the new dentry. Ignore if it isn't pointing
86 if (sd
->s_dentry
== dentry
)
88 spin_unlock(&sysfs_lock
);
94 static struct dentry_operations sysfs_dentry_ops
= {
95 .d_iput
= sysfs_d_iput
,
98 struct sysfs_dirent
*sysfs_new_dirent(const char *name
, void *element
,
99 umode_t mode
, int type
)
101 char *dup_name
= NULL
;
102 struct sysfs_dirent
*sd
= NULL
;
104 if (type
& SYSFS_COPY_NAME
) {
105 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
110 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
114 if (sysfs_alloc_ino(&sd
->s_ino
))
117 atomic_set(&sd
->s_count
, 1);
118 atomic_set(&sd
->s_event
, 1);
119 INIT_LIST_HEAD(&sd
->s_children
);
120 INIT_LIST_HEAD(&sd
->s_sibling
);
123 sd
->s_element
= element
;
131 kmem_cache_free(sysfs_dir_cachep
, sd
);
135 void sysfs_attach_dirent(struct sysfs_dirent
*sd
,
136 struct sysfs_dirent
*parent_sd
, struct dentry
*dentry
)
139 sd
->s_dentry
= dentry
;
140 dentry
->d_fsdata
= sysfs_get(sd
);
141 dentry
->d_op
= &sysfs_dentry_ops
;
145 sd
->s_parent
= sysfs_get(parent_sd
);
146 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
152 * Return -EEXIST if there is already a sysfs element with the same name for
155 * called with parent inode's i_mutex held
157 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
158 const unsigned char *new)
160 struct sysfs_dirent
* sd
;
162 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
164 if (strcmp(sd
->s_name
, new))
174 static int init_dir(struct inode
* inode
)
176 inode
->i_op
= &sysfs_dir_inode_operations
;
177 inode
->i_fop
= &sysfs_dir_operations
;
179 /* directory inodes start off with i_nlink == 2 (for "." entry) */
184 static int init_file(struct inode
* inode
)
186 inode
->i_size
= PAGE_SIZE
;
187 inode
->i_fop
= &sysfs_file_operations
;
191 static int init_symlink(struct inode
* inode
)
193 inode
->i_op
= &sysfs_symlink_inode_operations
;
197 static int create_dir(struct kobject
*kobj
, struct dentry
*parent
,
198 const char *name
, struct dentry
**p_dentry
)
201 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
202 struct dentry
*dentry
;
203 struct sysfs_dirent
*sd
;
205 mutex_lock(&parent
->d_inode
->i_mutex
);
207 dentry
= lookup_one_len(name
, parent
, strlen(name
));
208 if (IS_ERR(dentry
)) {
209 error
= PTR_ERR(dentry
);
214 if (sysfs_dirent_exist(parent
->d_fsdata
, name
))
218 sd
= sysfs_new_dirent(name
, kobj
, mode
, SYSFS_DIR
);
221 sysfs_attach_dirent(sd
, parent
->d_fsdata
, dentry
);
223 error
= sysfs_create(dentry
, mode
, init_dir
);
227 inc_nlink(parent
->d_inode
);
228 dentry
->d_op
= &sysfs_dentry_ops
;
236 list_del_init(&sd
->s_sibling
);
243 mutex_unlock(&parent
->d_inode
->i_mutex
);
248 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
250 return create_dir(k
,k
->dentry
,n
,d
);
254 * sysfs_create_dir - create a directory for an object.
255 * @kobj: object we're creating directory for.
256 * @shadow_parent: parent parent object.
259 int sysfs_create_dir(struct kobject
* kobj
, struct dentry
*shadow_parent
)
261 struct dentry
* dentry
= NULL
;
262 struct dentry
* parent
;
268 parent
= shadow_parent
;
269 else if (kobj
->parent
)
270 parent
= kobj
->parent
->dentry
;
271 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
272 parent
= sysfs_mount
->mnt_sb
->s_root
;
276 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
278 kobj
->dentry
= dentry
;
282 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
285 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
287 struct attribute
* attr
= NULL
;
288 struct bin_attribute
* bin_attr
= NULL
;
289 int (* init
) (struct inode
*) = NULL
;
292 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
293 bin_attr
= sd
->s_element
;
294 attr
= &bin_attr
->attr
;
296 attr
= sd
->s_element
;
300 dentry
->d_fsdata
= sysfs_get(sd
);
301 /* protect sd->s_dentry against sysfs_d_iput */
302 spin_lock(&sysfs_lock
);
303 sd
->s_dentry
= dentry
;
304 spin_unlock(&sysfs_lock
);
305 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
312 dentry
->d_inode
->i_size
= bin_attr
->size
;
313 dentry
->d_inode
->i_fop
= &bin_fops
;
315 dentry
->d_op
= &sysfs_dentry_ops
;
321 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
325 dentry
->d_fsdata
= sysfs_get(sd
);
326 /* protect sd->s_dentry against sysfs_d_iput */
327 spin_lock(&sysfs_lock
);
328 sd
->s_dentry
= dentry
;
329 spin_unlock(&sysfs_lock
);
330 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
332 dentry
->d_op
= &sysfs_dentry_ops
;
340 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
341 struct nameidata
*nd
)
343 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
344 struct sysfs_dirent
* sd
;
347 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
348 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
349 if (strcmp(sd
->s_name
, dentry
->d_name
.name
))
352 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
353 err
= sysfs_attach_link(sd
, dentry
);
355 err
= sysfs_attach_attr(sd
, dentry
);
363 const struct inode_operations sysfs_dir_inode_operations
= {
364 .lookup
= sysfs_lookup
,
365 .setattr
= sysfs_setattr
,
368 static void remove_dir(struct dentry
* d
)
370 struct dentry
* parent
= dget(d
->d_parent
);
371 struct sysfs_dirent
* sd
;
373 mutex_lock(&parent
->d_inode
->i_mutex
);
376 list_del_init(&sd
->s_sibling
);
379 simple_rmdir(parent
->d_inode
,d
);
381 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
382 atomic_read(&d
->d_count
));
384 mutex_unlock(&parent
->d_inode
->i_mutex
);
388 void sysfs_remove_subdir(struct dentry
* d
)
394 static void __sysfs_remove_dir(struct dentry
*dentry
)
396 struct sysfs_dirent
* parent_sd
;
397 struct sysfs_dirent
* sd
, * tmp
;
403 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
404 mutex_lock(&dentry
->d_inode
->i_mutex
);
405 parent_sd
= dentry
->d_fsdata
;
406 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
407 if (!sd
->s_element
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
409 list_del_init(&sd
->s_sibling
);
410 sysfs_drop_dentry(sd
, dentry
);
413 mutex_unlock(&dentry
->d_inode
->i_mutex
);
417 * Drop reference from dget() on entrance.
423 * sysfs_remove_dir - remove an object's directory.
426 * The only thing special about this is that we remove any files in
427 * the directory before we remove the directory, and we've inlined
428 * what used to be sysfs_rmdir() below, instead of calling separately.
431 void sysfs_remove_dir(struct kobject
* kobj
)
433 __sysfs_remove_dir(kobj
->dentry
);
437 int sysfs_rename_dir(struct kobject
* kobj
, struct dentry
*new_parent
,
438 const char *new_name
)
440 struct sysfs_dirent
*sd
= kobj
->dentry
->d_fsdata
;
441 struct sysfs_dirent
*parent_sd
= new_parent
->d_fsdata
;
442 struct dentry
*new_dentry
;
449 down_write(&sysfs_rename_sem
);
450 mutex_lock(&new_parent
->d_inode
->i_mutex
);
452 new_dentry
= lookup_one_len(new_name
, new_parent
, strlen(new_name
));
453 if (IS_ERR(new_dentry
)) {
454 error
= PTR_ERR(new_dentry
);
458 /* By allowing two different directories with the same
459 * d_parent we allow this routine to move between different
460 * shadows of the same directory
463 if (kobj
->dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
464 new_dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
465 new_dentry
== kobj
->dentry
)
469 if (new_dentry
->d_inode
)
472 /* rename kobject and sysfs_dirent */
474 new_name
= dup_name
= kstrdup(new_name
, GFP_KERNEL
);
478 error
= kobject_set_name(kobj
, "%s", new_name
);
483 sd
->s_name
= new_name
;
485 /* move under the new parent */
486 d_add(new_dentry
, NULL
);
487 d_move(kobj
->dentry
, new_dentry
);
489 list_del_init(&sd
->s_sibling
);
490 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
502 mutex_unlock(&new_parent
->d_inode
->i_mutex
);
503 up_write(&sysfs_rename_sem
);
507 int sysfs_move_dir(struct kobject
*kobj
, struct kobject
*new_parent
)
509 struct dentry
*old_parent_dentry
, *new_parent_dentry
, *new_dentry
;
510 struct sysfs_dirent
*new_parent_sd
, *sd
;
513 old_parent_dentry
= kobj
->parent
?
514 kobj
->parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
515 new_parent_dentry
= new_parent
?
516 new_parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
518 if (old_parent_dentry
->d_inode
== new_parent_dentry
->d_inode
)
519 return 0; /* nothing to move */
521 mutex_lock(&old_parent_dentry
->d_inode
->i_mutex
);
522 if (!mutex_trylock(&new_parent_dentry
->d_inode
->i_mutex
)) {
523 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
527 new_parent_sd
= new_parent_dentry
->d_fsdata
;
528 sd
= kobj
->dentry
->d_fsdata
;
530 new_dentry
= lookup_one_len(kobj
->name
, new_parent_dentry
,
532 if (IS_ERR(new_dentry
)) {
533 error
= PTR_ERR(new_dentry
);
537 d_add(new_dentry
, NULL
);
538 d_move(kobj
->dentry
, new_dentry
);
541 /* Remove from old parent's list and insert into new parent's list. */
542 list_del_init(&sd
->s_sibling
);
543 list_add(&sd
->s_sibling
, &new_parent_sd
->s_children
);
546 mutex_unlock(&new_parent_dentry
->d_inode
->i_mutex
);
547 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
552 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
554 struct dentry
* dentry
= file
->f_path
.dentry
;
555 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
556 struct sysfs_dirent
* sd
;
558 mutex_lock(&dentry
->d_inode
->i_mutex
);
559 sd
= sysfs_new_dirent("_DIR_", NULL
, 0, 0);
561 sysfs_attach_dirent(sd
, parent_sd
, NULL
);
562 mutex_unlock(&dentry
->d_inode
->i_mutex
);
564 file
->private_data
= sd
;
565 return sd
? 0 : -ENOMEM
;
568 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
570 struct dentry
* dentry
= file
->f_path
.dentry
;
571 struct sysfs_dirent
* cursor
= file
->private_data
;
573 mutex_lock(&dentry
->d_inode
->i_mutex
);
574 list_del_init(&cursor
->s_sibling
);
575 mutex_unlock(&dentry
->d_inode
->i_mutex
);
577 release_sysfs_dirent(cursor
);
582 /* Relationship between s_mode and the DT_xxx types */
583 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
585 return (sd
->s_mode
>> 12) & 15;
588 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
590 struct dentry
*dentry
= filp
->f_path
.dentry
;
591 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
592 struct sysfs_dirent
*cursor
= filp
->private_data
;
593 struct list_head
*p
, *q
= &cursor
->s_sibling
;
599 ino
= parent_sd
->s_ino
;
600 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
606 if (parent_sd
->s_parent
)
607 ino
= parent_sd
->s_parent
->s_ino
;
609 ino
= parent_sd
->s_ino
;
610 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
616 if (filp
->f_pos
== 2)
617 list_move(q
, &parent_sd
->s_children
);
619 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
620 struct sysfs_dirent
*next
;
624 next
= list_entry(p
, struct sysfs_dirent
,
626 if (!next
->s_element
)
633 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
645 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
647 struct dentry
* dentry
= file
->f_path
.dentry
;
649 mutex_lock(&dentry
->d_inode
->i_mutex
);
652 offset
+= file
->f_pos
;
657 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
660 if (offset
!= file
->f_pos
) {
661 file
->f_pos
= offset
;
662 if (file
->f_pos
>= 2) {
663 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
664 struct sysfs_dirent
*cursor
= file
->private_data
;
666 loff_t n
= file
->f_pos
- 2;
668 list_del(&cursor
->s_sibling
);
669 p
= sd
->s_children
.next
;
670 while (n
&& p
!= &sd
->s_children
) {
671 struct sysfs_dirent
*next
;
672 next
= list_entry(p
, struct sysfs_dirent
,
678 list_add_tail(&cursor
->s_sibling
, p
);
681 mutex_unlock(&dentry
->d_inode
->i_mutex
);
687 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
688 * @kobj: object we're creating shadow of.
691 int sysfs_make_shadowed_dir(struct kobject
*kobj
,
692 void * (*follow_link
)(struct dentry
*, struct nameidata
*))
695 struct inode_operations
*i_op
;
697 inode
= kobj
->dentry
->d_inode
;
698 if (inode
->i_op
!= &sysfs_dir_inode_operations
)
701 i_op
= kmalloc(sizeof(*i_op
), GFP_KERNEL
);
705 memcpy(i_op
, &sysfs_dir_inode_operations
, sizeof(*i_op
));
706 i_op
->follow_link
= follow_link
;
708 /* Locking of inode->i_op?
709 * Since setting i_op is a single word write and they
710 * are atomic we should be ok here.
717 * sysfs_create_shadow_dir - create a shadow directory for an object.
718 * @kobj: object we're creating directory for.
720 * sysfs_make_shadowed_dir must already have been called on this
724 struct dentry
*sysfs_create_shadow_dir(struct kobject
*kobj
)
726 struct dentry
*dir
= kobj
->dentry
;
727 struct inode
*inode
= dir
->d_inode
;
728 struct dentry
*parent
= dir
->d_parent
;
729 struct sysfs_dirent
*parent_sd
= parent
->d_fsdata
;
730 struct dentry
*shadow
;
731 struct sysfs_dirent
*sd
;
733 shadow
= ERR_PTR(-EINVAL
);
734 if (!sysfs_is_shadowed_inode(inode
))
737 shadow
= d_alloc(parent
, &dir
->d_name
);
741 sd
= sysfs_new_dirent("_SHADOW_", kobj
, inode
->i_mode
, SYSFS_DIR
);
744 /* point to parent_sd but don't attach to it */
745 sd
->s_parent
= sysfs_get(parent_sd
);
746 sysfs_attach_dirent(sd
, NULL
, shadow
);
748 d_instantiate(shadow
, igrab(inode
));
750 inc_nlink(parent
->d_inode
);
751 shadow
->d_op
= &sysfs_dentry_ops
;
753 dget(shadow
); /* Extra count - pin the dentry in core */
759 shadow
= ERR_PTR(-ENOMEM
);
764 * sysfs_remove_shadow_dir - remove an object's directory.
765 * @shadow: dentry of shadow directory
767 * The only thing special about this is that we remove any files in
768 * the directory before we remove the directory, and we've inlined
769 * what used to be sysfs_rmdir() below, instead of calling separately.
772 void sysfs_remove_shadow_dir(struct dentry
*shadow
)
774 __sysfs_remove_dir(shadow
);
777 const struct file_operations sysfs_dir_operations
= {
778 .open
= sysfs_dir_open
,
779 .release
= sysfs_dir_close
,
780 .llseek
= sysfs_dir_lseek
,
781 .read
= generic_read_dir
,
782 .readdir
= sysfs_readdir
,