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 if (sd
->s_type
& SYSFS_KOBJ_LINK
) {
51 struct sysfs_symlink
* sl
= sd
->s_element
;
53 kobject_put(sl
->target_kobj
);
57 sysfs_free_ino(sd
->s_ino
);
58 kmem_cache_free(sysfs_dir_cachep
, sd
);
61 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
63 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
66 /* sd->s_dentry is protected with sysfs_lock. This
67 * allows sysfs_drop_dentry() to dereference it.
69 spin_lock(&sysfs_lock
);
71 /* The dentry might have been deleted or another
72 * lookup could have happened updating sd->s_dentry to
73 * point the new dentry. Ignore if it isn't pointing
76 if (sd
->s_dentry
== dentry
)
78 spin_unlock(&sysfs_lock
);
84 static struct dentry_operations sysfs_dentry_ops
= {
85 .d_iput
= sysfs_d_iput
,
88 struct sysfs_dirent
*sysfs_new_dirent(void *element
, umode_t mode
, int type
)
90 struct sysfs_dirent
* sd
;
92 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
96 if (sysfs_alloc_ino(&sd
->s_ino
)) {
97 kmem_cache_free(sysfs_dir_cachep
, sd
);
101 atomic_set(&sd
->s_count
, 1);
102 atomic_set(&sd
->s_event
, 1);
103 INIT_LIST_HEAD(&sd
->s_children
);
104 INIT_LIST_HEAD(&sd
->s_sibling
);
106 sd
->s_element
= element
;
113 void sysfs_attach_dirent(struct sysfs_dirent
*sd
,
114 struct sysfs_dirent
*parent_sd
, struct dentry
*dentry
)
117 sd
->s_dentry
= dentry
;
118 dentry
->d_fsdata
= sysfs_get(sd
);
119 dentry
->d_op
= &sysfs_dentry_ops
;
123 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
128 * Return -EEXIST if there is already a sysfs element with the same name for
131 * called with parent inode's i_mutex held
133 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
134 const unsigned char *new)
136 struct sysfs_dirent
* sd
;
138 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
140 const unsigned char *existing
= sysfs_get_name(sd
);
141 if (strcmp(existing
, new))
151 static int init_dir(struct inode
* inode
)
153 inode
->i_op
= &sysfs_dir_inode_operations
;
154 inode
->i_fop
= &sysfs_dir_operations
;
156 /* directory inodes start off with i_nlink == 2 (for "." entry) */
161 static int init_file(struct inode
* inode
)
163 inode
->i_size
= PAGE_SIZE
;
164 inode
->i_fop
= &sysfs_file_operations
;
168 static int init_symlink(struct inode
* inode
)
170 inode
->i_op
= &sysfs_symlink_inode_operations
;
174 static int create_dir(struct kobject
*kobj
, struct dentry
*parent
,
175 const char *name
, struct dentry
**p_dentry
)
178 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
179 struct dentry
*dentry
;
180 struct sysfs_dirent
*sd
;
182 mutex_lock(&parent
->d_inode
->i_mutex
);
184 dentry
= lookup_one_len(name
, parent
, strlen(name
));
185 if (IS_ERR(dentry
)) {
186 error
= PTR_ERR(dentry
);
191 if (sysfs_dirent_exist(parent
->d_fsdata
, name
))
195 sd
= sysfs_new_dirent(kobj
, mode
, SYSFS_DIR
);
198 sysfs_attach_dirent(sd
, parent
->d_fsdata
, dentry
);
200 error
= sysfs_create(dentry
, mode
, init_dir
);
204 inc_nlink(parent
->d_inode
);
205 dentry
->d_op
= &sysfs_dentry_ops
;
213 list_del_init(&sd
->s_sibling
);
220 mutex_unlock(&parent
->d_inode
->i_mutex
);
225 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
227 return create_dir(k
,k
->dentry
,n
,d
);
231 * sysfs_create_dir - create a directory for an object.
232 * @kobj: object we're creating directory for.
233 * @shadow_parent: parent parent object.
236 int sysfs_create_dir(struct kobject
* kobj
, struct dentry
*shadow_parent
)
238 struct dentry
* dentry
= NULL
;
239 struct dentry
* parent
;
245 parent
= shadow_parent
;
246 else if (kobj
->parent
)
247 parent
= kobj
->parent
->dentry
;
248 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
249 parent
= sysfs_mount
->mnt_sb
->s_root
;
253 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
255 kobj
->dentry
= dentry
;
259 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
262 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
264 struct attribute
* attr
= NULL
;
265 struct bin_attribute
* bin_attr
= NULL
;
266 int (* init
) (struct inode
*) = NULL
;
269 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
270 bin_attr
= sd
->s_element
;
271 attr
= &bin_attr
->attr
;
273 attr
= sd
->s_element
;
277 dentry
->d_fsdata
= sysfs_get(sd
);
278 /* protect sd->s_dentry against sysfs_d_iput */
279 spin_lock(&sysfs_lock
);
280 sd
->s_dentry
= dentry
;
281 spin_unlock(&sysfs_lock
);
282 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
289 dentry
->d_inode
->i_size
= bin_attr
->size
;
290 dentry
->d_inode
->i_fop
= &bin_fops
;
292 dentry
->d_op
= &sysfs_dentry_ops
;
298 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
302 dentry
->d_fsdata
= sysfs_get(sd
);
303 /* protect sd->s_dentry against sysfs_d_iput */
304 spin_lock(&sysfs_lock
);
305 sd
->s_dentry
= dentry
;
306 spin_unlock(&sysfs_lock
);
307 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
309 dentry
->d_op
= &sysfs_dentry_ops
;
317 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
318 struct nameidata
*nd
)
320 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
321 struct sysfs_dirent
* sd
;
324 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
325 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
326 const unsigned char * name
= sysfs_get_name(sd
);
328 if (strcmp(name
, dentry
->d_name
.name
))
331 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
332 err
= sysfs_attach_link(sd
, dentry
);
334 err
= sysfs_attach_attr(sd
, dentry
);
342 const struct inode_operations sysfs_dir_inode_operations
= {
343 .lookup
= sysfs_lookup
,
344 .setattr
= sysfs_setattr
,
347 static void remove_dir(struct dentry
* d
)
349 struct dentry
* parent
= dget(d
->d_parent
);
350 struct sysfs_dirent
* sd
;
352 mutex_lock(&parent
->d_inode
->i_mutex
);
355 list_del_init(&sd
->s_sibling
);
358 simple_rmdir(parent
->d_inode
,d
);
360 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
361 atomic_read(&d
->d_count
));
363 mutex_unlock(&parent
->d_inode
->i_mutex
);
367 void sysfs_remove_subdir(struct dentry
* d
)
373 static void __sysfs_remove_dir(struct dentry
*dentry
)
375 struct sysfs_dirent
* parent_sd
;
376 struct sysfs_dirent
* sd
, * tmp
;
382 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
383 mutex_lock(&dentry
->d_inode
->i_mutex
);
384 parent_sd
= dentry
->d_fsdata
;
385 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
386 if (!sd
->s_element
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
388 list_del_init(&sd
->s_sibling
);
389 sysfs_drop_dentry(sd
, dentry
);
392 mutex_unlock(&dentry
->d_inode
->i_mutex
);
396 * Drop reference from dget() on entrance.
402 * sysfs_remove_dir - remove an object's directory.
405 * The only thing special about this is that we remove any files in
406 * the directory before we remove the directory, and we've inlined
407 * what used to be sysfs_rmdir() below, instead of calling separately.
410 void sysfs_remove_dir(struct kobject
* kobj
)
412 __sysfs_remove_dir(kobj
->dentry
);
416 int sysfs_rename_dir(struct kobject
* kobj
, struct dentry
*new_parent
,
417 const char *new_name
)
420 struct dentry
* new_dentry
;
421 struct sysfs_dirent
*sd
, *parent_sd
;
426 down_write(&sysfs_rename_sem
);
427 mutex_lock(&new_parent
->d_inode
->i_mutex
);
429 new_dentry
= lookup_one_len(new_name
, new_parent
, strlen(new_name
));
430 if (IS_ERR(new_dentry
)) {
431 error
= PTR_ERR(new_dentry
);
435 /* By allowing two different directories with the same
436 * d_parent we allow this routine to move between different
437 * shadows of the same directory
440 if (kobj
->dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
441 new_dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
442 new_dentry
== kobj
->dentry
)
446 if (new_dentry
->d_inode
)
449 error
= kobject_set_name(kobj
, "%s", new_name
);
453 d_add(new_dentry
, NULL
);
454 d_move(kobj
->dentry
, new_dentry
);
456 sd
= kobj
->dentry
->d_fsdata
;
457 parent_sd
= new_parent
->d_fsdata
;
459 list_del_init(&sd
->s_sibling
);
460 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
470 mutex_unlock(&new_parent
->d_inode
->i_mutex
);
471 up_write(&sysfs_rename_sem
);
475 int sysfs_move_dir(struct kobject
*kobj
, struct kobject
*new_parent
)
477 struct dentry
*old_parent_dentry
, *new_parent_dentry
, *new_dentry
;
478 struct sysfs_dirent
*new_parent_sd
, *sd
;
481 old_parent_dentry
= kobj
->parent
?
482 kobj
->parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
483 new_parent_dentry
= new_parent
?
484 new_parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
486 if (old_parent_dentry
->d_inode
== new_parent_dentry
->d_inode
)
487 return 0; /* nothing to move */
489 mutex_lock(&old_parent_dentry
->d_inode
->i_mutex
);
490 if (!mutex_trylock(&new_parent_dentry
->d_inode
->i_mutex
)) {
491 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
495 new_parent_sd
= new_parent_dentry
->d_fsdata
;
496 sd
= kobj
->dentry
->d_fsdata
;
498 new_dentry
= lookup_one_len(kobj
->name
, new_parent_dentry
,
500 if (IS_ERR(new_dentry
)) {
501 error
= PTR_ERR(new_dentry
);
505 d_add(new_dentry
, NULL
);
506 d_move(kobj
->dentry
, new_dentry
);
509 /* Remove from old parent's list and insert into new parent's list. */
510 list_del_init(&sd
->s_sibling
);
511 list_add(&sd
->s_sibling
, &new_parent_sd
->s_children
);
514 mutex_unlock(&new_parent_dentry
->d_inode
->i_mutex
);
515 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
520 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
522 struct dentry
* dentry
= file
->f_path
.dentry
;
523 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
524 struct sysfs_dirent
* sd
;
526 mutex_lock(&dentry
->d_inode
->i_mutex
);
527 sd
= sysfs_new_dirent(NULL
, 0, 0);
529 sysfs_attach_dirent(sd
, parent_sd
, NULL
);
530 mutex_unlock(&dentry
->d_inode
->i_mutex
);
532 file
->private_data
= sd
;
533 return sd
? 0 : -ENOMEM
;
536 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
538 struct dentry
* dentry
= file
->f_path
.dentry
;
539 struct sysfs_dirent
* cursor
= file
->private_data
;
541 mutex_lock(&dentry
->d_inode
->i_mutex
);
542 list_del_init(&cursor
->s_sibling
);
543 mutex_unlock(&dentry
->d_inode
->i_mutex
);
545 release_sysfs_dirent(cursor
);
550 /* Relationship between s_mode and the DT_xxx types */
551 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
553 return (sd
->s_mode
>> 12) & 15;
556 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
558 struct dentry
*dentry
= filp
->f_path
.dentry
;
559 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
560 struct sysfs_dirent
*cursor
= filp
->private_data
;
561 struct list_head
*p
, *q
= &cursor
->s_sibling
;
567 ino
= parent_sd
->s_ino
;
568 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
574 ino
= parent_ino(dentry
);
575 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
581 if (filp
->f_pos
== 2)
582 list_move(q
, &parent_sd
->s_children
);
584 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
585 struct sysfs_dirent
*next
;
589 next
= list_entry(p
, struct sysfs_dirent
,
591 if (!next
->s_element
)
594 name
= sysfs_get_name(next
);
598 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
610 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
612 struct dentry
* dentry
= file
->f_path
.dentry
;
614 mutex_lock(&dentry
->d_inode
->i_mutex
);
617 offset
+= file
->f_pos
;
622 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
625 if (offset
!= file
->f_pos
) {
626 file
->f_pos
= offset
;
627 if (file
->f_pos
>= 2) {
628 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
629 struct sysfs_dirent
*cursor
= file
->private_data
;
631 loff_t n
= file
->f_pos
- 2;
633 list_del(&cursor
->s_sibling
);
634 p
= sd
->s_children
.next
;
635 while (n
&& p
!= &sd
->s_children
) {
636 struct sysfs_dirent
*next
;
637 next
= list_entry(p
, struct sysfs_dirent
,
643 list_add_tail(&cursor
->s_sibling
, p
);
646 mutex_unlock(&dentry
->d_inode
->i_mutex
);
652 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
653 * @kobj: object we're creating shadow of.
656 int sysfs_make_shadowed_dir(struct kobject
*kobj
,
657 void * (*follow_link
)(struct dentry
*, struct nameidata
*))
660 struct inode_operations
*i_op
;
662 inode
= kobj
->dentry
->d_inode
;
663 if (inode
->i_op
!= &sysfs_dir_inode_operations
)
666 i_op
= kmalloc(sizeof(*i_op
), GFP_KERNEL
);
670 memcpy(i_op
, &sysfs_dir_inode_operations
, sizeof(*i_op
));
671 i_op
->follow_link
= follow_link
;
673 /* Locking of inode->i_op?
674 * Since setting i_op is a single word write and they
675 * are atomic we should be ok here.
682 * sysfs_create_shadow_dir - create a shadow directory for an object.
683 * @kobj: object we're creating directory for.
685 * sysfs_make_shadowed_dir must already have been called on this
689 struct dentry
*sysfs_create_shadow_dir(struct kobject
*kobj
)
691 struct sysfs_dirent
*sd
;
692 struct dentry
*parent
, *dir
, *shadow
;
696 inode
= dir
->d_inode
;
697 parent
= dir
->d_parent
;
698 shadow
= ERR_PTR(-EINVAL
);
699 if (!sysfs_is_shadowed_inode(inode
))
702 shadow
= d_alloc(parent
, &dir
->d_name
);
706 sd
= sysfs_new_dirent(kobj
, inode
->i_mode
, SYSFS_DIR
);
709 sysfs_attach_dirent(sd
, NULL
, shadow
);
711 d_instantiate(shadow
, igrab(inode
));
713 inc_nlink(parent
->d_inode
);
714 shadow
->d_op
= &sysfs_dentry_ops
;
716 dget(shadow
); /* Extra count - pin the dentry in core */
722 shadow
= ERR_PTR(-ENOMEM
);
727 * sysfs_remove_shadow_dir - remove an object's directory.
728 * @shadow: dentry of shadow directory
730 * The only thing special about this is that we remove any files in
731 * the directory before we remove the directory, and we've inlined
732 * what used to be sysfs_rmdir() below, instead of calling separately.
735 void sysfs_remove_shadow_dir(struct dentry
*shadow
)
737 __sysfs_remove_dir(shadow
);
740 const struct file_operations sysfs_dir_operations
= {
741 .open
= sysfs_dir_open
,
742 .release
= sysfs_dir_close
,
743 .llseek
= sysfs_dir_lseek
,
744 .read
= generic_read_dir
,
745 .readdir
= sysfs_readdir
,