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
;
18 spinlock_t kobj_sysfs_assoc_lock
= SPIN_LOCK_UNLOCKED
;
20 static spinlock_t sysfs_ino_lock
= SPIN_LOCK_UNLOCKED
;
21 static DEFINE_IDA(sysfs_ino_ida
);
23 int sysfs_alloc_ino(ino_t
*pino
)
28 spin_lock(&sysfs_ino_lock
);
29 rc
= ida_get_new_above(&sysfs_ino_ida
, 2, &ino
);
30 spin_unlock(&sysfs_ino_lock
);
33 if (ida_pre_get(&sysfs_ino_ida
, GFP_KERNEL
))
42 static void sysfs_free_ino(ino_t ino
)
44 spin_lock(&sysfs_ino_lock
);
45 ida_remove(&sysfs_ino_ida
, ino
);
46 spin_unlock(&sysfs_ino_lock
);
49 void release_sysfs_dirent(struct sysfs_dirent
* sd
)
51 struct sysfs_dirent
*parent_sd
;
54 parent_sd
= sd
->s_parent
;
56 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
57 sysfs_put(sd
->s_elem
.symlink
.target_sd
);
58 if (sd
->s_type
& SYSFS_COPY_NAME
)
61 sysfs_free_ino(sd
->s_ino
);
62 kmem_cache_free(sysfs_dir_cachep
, sd
);
65 if (sd
&& atomic_dec_and_test(&sd
->s_count
))
69 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
71 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
74 /* sd->s_dentry is protected with sysfs_lock. This
75 * allows sysfs_drop_dentry() to dereference it.
77 spin_lock(&sysfs_lock
);
79 /* The dentry might have been deleted or another
80 * lookup could have happened updating sd->s_dentry to
81 * point the new dentry. Ignore if it isn't pointing
84 if (sd
->s_dentry
== dentry
)
86 spin_unlock(&sysfs_lock
);
92 static struct dentry_operations sysfs_dentry_ops
= {
93 .d_iput
= sysfs_d_iput
,
96 struct sysfs_dirent
*sysfs_new_dirent(const char *name
, umode_t mode
, int type
)
98 char *dup_name
= NULL
;
99 struct sysfs_dirent
*sd
= NULL
;
101 if (type
& SYSFS_COPY_NAME
) {
102 name
= dup_name
= kstrdup(name
, GFP_KERNEL
);
107 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
111 if (sysfs_alloc_ino(&sd
->s_ino
))
114 atomic_set(&sd
->s_count
, 1);
115 atomic_set(&sd
->s_event
, 1);
116 INIT_LIST_HEAD(&sd
->s_children
);
117 INIT_LIST_HEAD(&sd
->s_sibling
);
127 kmem_cache_free(sysfs_dir_cachep
, sd
);
131 void sysfs_attach_dirent(struct sysfs_dirent
*sd
,
132 struct sysfs_dirent
*parent_sd
, struct dentry
*dentry
)
135 sd
->s_dentry
= dentry
;
136 dentry
->d_fsdata
= sysfs_get(sd
);
137 dentry
->d_op
= &sysfs_dentry_ops
;
141 sd
->s_parent
= sysfs_get(parent_sd
);
142 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
148 * Return -EEXIST if there is already a sysfs element with the same name for
151 * called with parent inode's i_mutex held
153 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
154 const unsigned char *new)
156 struct sysfs_dirent
* sd
;
158 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
160 if (strcmp(sd
->s_name
, new))
170 static int init_dir(struct inode
* inode
)
172 inode
->i_op
= &sysfs_dir_inode_operations
;
173 inode
->i_fop
= &sysfs_dir_operations
;
175 /* directory inodes start off with i_nlink == 2 (for "." entry) */
180 static int init_file(struct inode
* inode
)
182 inode
->i_size
= PAGE_SIZE
;
183 inode
->i_fop
= &sysfs_file_operations
;
187 static int init_symlink(struct inode
* inode
)
189 inode
->i_op
= &sysfs_symlink_inode_operations
;
193 static int create_dir(struct kobject
*kobj
, struct dentry
*parent
,
194 const char *name
, struct dentry
**p_dentry
)
197 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
198 struct dentry
*dentry
;
199 struct sysfs_dirent
*sd
;
201 mutex_lock(&parent
->d_inode
->i_mutex
);
203 dentry
= lookup_one_len(name
, parent
, strlen(name
));
204 if (IS_ERR(dentry
)) {
205 error
= PTR_ERR(dentry
);
210 if (sysfs_dirent_exist(parent
->d_fsdata
, name
))
214 sd
= sysfs_new_dirent(name
, mode
, SYSFS_DIR
);
217 sd
->s_elem
.dir
.kobj
= kobj
;
218 sysfs_attach_dirent(sd
, parent
->d_fsdata
, dentry
);
220 error
= sysfs_create(dentry
, mode
, init_dir
);
224 inc_nlink(parent
->d_inode
);
225 dentry
->d_op
= &sysfs_dentry_ops
;
233 list_del_init(&sd
->s_sibling
);
240 mutex_unlock(&parent
->d_inode
->i_mutex
);
245 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
247 return create_dir(k
,k
->dentry
,n
,d
);
251 * sysfs_create_dir - create a directory for an object.
252 * @kobj: object we're creating directory for.
253 * @shadow_parent: parent parent object.
256 int sysfs_create_dir(struct kobject
* kobj
, struct dentry
*shadow_parent
)
258 struct dentry
* dentry
= NULL
;
259 struct dentry
* parent
;
265 parent
= shadow_parent
;
266 else if (kobj
->parent
)
267 parent
= kobj
->parent
->dentry
;
268 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
269 parent
= sysfs_mount
->mnt_sb
->s_root
;
273 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
275 kobj
->dentry
= dentry
;
279 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
282 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
284 struct attribute
* attr
= NULL
;
285 struct bin_attribute
* bin_attr
= NULL
;
286 int (* init
) (struct inode
*) = NULL
;
289 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
290 bin_attr
= sd
->s_elem
.bin_attr
.bin_attr
;
291 attr
= &bin_attr
->attr
;
293 attr
= sd
->s_elem
.attr
.attr
;
297 dentry
->d_fsdata
= sysfs_get(sd
);
298 /* protect sd->s_dentry against sysfs_d_iput */
299 spin_lock(&sysfs_lock
);
300 sd
->s_dentry
= dentry
;
301 spin_unlock(&sysfs_lock
);
302 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
309 dentry
->d_inode
->i_size
= bin_attr
->size
;
310 dentry
->d_inode
->i_fop
= &bin_fops
;
312 dentry
->d_op
= &sysfs_dentry_ops
;
318 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
322 dentry
->d_fsdata
= sysfs_get(sd
);
323 /* protect sd->s_dentry against sysfs_d_iput */
324 spin_lock(&sysfs_lock
);
325 sd
->s_dentry
= dentry
;
326 spin_unlock(&sysfs_lock
);
327 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
329 dentry
->d_op
= &sysfs_dentry_ops
;
337 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
338 struct nameidata
*nd
)
340 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
341 struct sysfs_dirent
* sd
;
344 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
345 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
346 if (strcmp(sd
->s_name
, dentry
->d_name
.name
))
349 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
350 err
= sysfs_attach_link(sd
, dentry
);
352 err
= sysfs_attach_attr(sd
, dentry
);
360 const struct inode_operations sysfs_dir_inode_operations
= {
361 .lookup
= sysfs_lookup
,
362 .setattr
= sysfs_setattr
,
365 static void remove_dir(struct dentry
* d
)
367 struct dentry
* parent
= dget(d
->d_parent
);
368 struct sysfs_dirent
* sd
;
370 mutex_lock(&parent
->d_inode
->i_mutex
);
373 list_del_init(&sd
->s_sibling
);
376 simple_rmdir(parent
->d_inode
,d
);
378 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
379 atomic_read(&d
->d_count
));
381 mutex_unlock(&parent
->d_inode
->i_mutex
);
385 void sysfs_remove_subdir(struct dentry
* d
)
391 static void __sysfs_remove_dir(struct dentry
*dentry
)
393 struct sysfs_dirent
* parent_sd
;
394 struct sysfs_dirent
* sd
, * tmp
;
400 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
401 mutex_lock(&dentry
->d_inode
->i_mutex
);
402 parent_sd
= dentry
->d_fsdata
;
403 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
404 if (!sd
->s_type
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
406 list_del_init(&sd
->s_sibling
);
407 sysfs_drop_dentry(sd
, dentry
);
410 mutex_unlock(&dentry
->d_inode
->i_mutex
);
414 * Drop reference from dget() on entrance.
420 * sysfs_remove_dir - remove an object's directory.
423 * The only thing special about this is that we remove any files in
424 * the directory before we remove the directory, and we've inlined
425 * what used to be sysfs_rmdir() below, instead of calling separately.
428 void sysfs_remove_dir(struct kobject
* kobj
)
430 struct dentry
*d
= kobj
->dentry
;
432 spin_lock(&kobj_sysfs_assoc_lock
);
434 spin_unlock(&kobj_sysfs_assoc_lock
);
436 __sysfs_remove_dir(d
);
439 int sysfs_rename_dir(struct kobject
* kobj
, struct dentry
*new_parent
,
440 const char *new_name
)
442 struct sysfs_dirent
*sd
= kobj
->dentry
->d_fsdata
;
443 struct sysfs_dirent
*parent_sd
= new_parent
->d_fsdata
;
444 struct dentry
*new_dentry
;
451 down_write(&sysfs_rename_sem
);
452 mutex_lock(&new_parent
->d_inode
->i_mutex
);
454 new_dentry
= lookup_one_len(new_name
, new_parent
, strlen(new_name
));
455 if (IS_ERR(new_dentry
)) {
456 error
= PTR_ERR(new_dentry
);
460 /* By allowing two different directories with the same
461 * d_parent we allow this routine to move between different
462 * shadows of the same directory
465 if (kobj
->dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
466 new_dentry
->d_parent
->d_inode
!= new_parent
->d_inode
||
467 new_dentry
== kobj
->dentry
)
471 if (new_dentry
->d_inode
)
474 /* rename kobject and sysfs_dirent */
476 new_name
= dup_name
= kstrdup(new_name
, GFP_KERNEL
);
480 error
= kobject_set_name(kobj
, "%s", new_name
);
485 sd
->s_name
= new_name
;
487 /* move under the new parent */
488 d_add(new_dentry
, NULL
);
489 d_move(kobj
->dentry
, new_dentry
);
491 list_del_init(&sd
->s_sibling
);
492 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
504 mutex_unlock(&new_parent
->d_inode
->i_mutex
);
505 up_write(&sysfs_rename_sem
);
509 int sysfs_move_dir(struct kobject
*kobj
, struct kobject
*new_parent
)
511 struct dentry
*old_parent_dentry
, *new_parent_dentry
, *new_dentry
;
512 struct sysfs_dirent
*new_parent_sd
, *sd
;
515 old_parent_dentry
= kobj
->parent
?
516 kobj
->parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
517 new_parent_dentry
= new_parent
?
518 new_parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
520 if (old_parent_dentry
->d_inode
== new_parent_dentry
->d_inode
)
521 return 0; /* nothing to move */
523 mutex_lock(&old_parent_dentry
->d_inode
->i_mutex
);
524 if (!mutex_trylock(&new_parent_dentry
->d_inode
->i_mutex
)) {
525 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
529 new_parent_sd
= new_parent_dentry
->d_fsdata
;
530 sd
= kobj
->dentry
->d_fsdata
;
532 new_dentry
= lookup_one_len(kobj
->name
, new_parent_dentry
,
534 if (IS_ERR(new_dentry
)) {
535 error
= PTR_ERR(new_dentry
);
539 d_add(new_dentry
, NULL
);
540 d_move(kobj
->dentry
, new_dentry
);
543 /* Remove from old parent's list and insert into new parent's list. */
544 list_del_init(&sd
->s_sibling
);
545 list_add(&sd
->s_sibling
, &new_parent_sd
->s_children
);
548 mutex_unlock(&new_parent_dentry
->d_inode
->i_mutex
);
549 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
554 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
556 struct dentry
* dentry
= file
->f_path
.dentry
;
557 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
558 struct sysfs_dirent
* sd
;
560 mutex_lock(&dentry
->d_inode
->i_mutex
);
561 sd
= sysfs_new_dirent("_DIR_", 0, 0);
563 sysfs_attach_dirent(sd
, parent_sd
, NULL
);
564 mutex_unlock(&dentry
->d_inode
->i_mutex
);
566 file
->private_data
= sd
;
567 return sd
? 0 : -ENOMEM
;
570 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
572 struct dentry
* dentry
= file
->f_path
.dentry
;
573 struct sysfs_dirent
* cursor
= file
->private_data
;
575 mutex_lock(&dentry
->d_inode
->i_mutex
);
576 list_del_init(&cursor
->s_sibling
);
577 mutex_unlock(&dentry
->d_inode
->i_mutex
);
579 release_sysfs_dirent(cursor
);
584 /* Relationship between s_mode and the DT_xxx types */
585 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
587 return (sd
->s_mode
>> 12) & 15;
590 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
592 struct dentry
*dentry
= filp
->f_path
.dentry
;
593 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
594 struct sysfs_dirent
*cursor
= filp
->private_data
;
595 struct list_head
*p
, *q
= &cursor
->s_sibling
;
601 ino
= parent_sd
->s_ino
;
602 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
608 if (parent_sd
->s_parent
)
609 ino
= parent_sd
->s_parent
->s_ino
;
611 ino
= parent_sd
->s_ino
;
612 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
618 if (filp
->f_pos
== 2)
619 list_move(q
, &parent_sd
->s_children
);
621 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
622 struct sysfs_dirent
*next
;
626 next
= list_entry(p
, struct sysfs_dirent
,
635 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
647 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
649 struct dentry
* dentry
= file
->f_path
.dentry
;
651 mutex_lock(&dentry
->d_inode
->i_mutex
);
654 offset
+= file
->f_pos
;
659 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
662 if (offset
!= file
->f_pos
) {
663 file
->f_pos
= offset
;
664 if (file
->f_pos
>= 2) {
665 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
666 struct sysfs_dirent
*cursor
= file
->private_data
;
668 loff_t n
= file
->f_pos
- 2;
670 list_del(&cursor
->s_sibling
);
671 p
= sd
->s_children
.next
;
672 while (n
&& p
!= &sd
->s_children
) {
673 struct sysfs_dirent
*next
;
674 next
= list_entry(p
, struct sysfs_dirent
,
680 list_add_tail(&cursor
->s_sibling
, p
);
683 mutex_unlock(&dentry
->d_inode
->i_mutex
);
689 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
690 * @kobj: object we're creating shadow of.
693 int sysfs_make_shadowed_dir(struct kobject
*kobj
,
694 void * (*follow_link
)(struct dentry
*, struct nameidata
*))
697 struct inode_operations
*i_op
;
699 inode
= kobj
->dentry
->d_inode
;
700 if (inode
->i_op
!= &sysfs_dir_inode_operations
)
703 i_op
= kmalloc(sizeof(*i_op
), GFP_KERNEL
);
707 memcpy(i_op
, &sysfs_dir_inode_operations
, sizeof(*i_op
));
708 i_op
->follow_link
= follow_link
;
710 /* Locking of inode->i_op?
711 * Since setting i_op is a single word write and they
712 * are atomic we should be ok here.
719 * sysfs_create_shadow_dir - create a shadow directory for an object.
720 * @kobj: object we're creating directory for.
722 * sysfs_make_shadowed_dir must already have been called on this
726 struct dentry
*sysfs_create_shadow_dir(struct kobject
*kobj
)
728 struct dentry
*dir
= kobj
->dentry
;
729 struct inode
*inode
= dir
->d_inode
;
730 struct dentry
*parent
= dir
->d_parent
;
731 struct sysfs_dirent
*parent_sd
= parent
->d_fsdata
;
732 struct dentry
*shadow
;
733 struct sysfs_dirent
*sd
;
735 shadow
= ERR_PTR(-EINVAL
);
736 if (!sysfs_is_shadowed_inode(inode
))
739 shadow
= d_alloc(parent
, &dir
->d_name
);
743 sd
= sysfs_new_dirent("_SHADOW_", inode
->i_mode
, SYSFS_DIR
);
746 sd
->s_elem
.dir
.kobj
= kobj
;
747 /* point to parent_sd but don't attach to it */
748 sd
->s_parent
= sysfs_get(parent_sd
);
749 sysfs_attach_dirent(sd
, NULL
, shadow
);
751 d_instantiate(shadow
, igrab(inode
));
753 inc_nlink(parent
->d_inode
);
754 shadow
->d_op
= &sysfs_dentry_ops
;
756 dget(shadow
); /* Extra count - pin the dentry in core */
762 shadow
= ERR_PTR(-ENOMEM
);
767 * sysfs_remove_shadow_dir - remove an object's directory.
768 * @shadow: dentry of shadow directory
770 * The only thing special about this is that we remove any files in
771 * the directory before we remove the directory, and we've inlined
772 * what used to be sysfs_rmdir() below, instead of calling separately.
775 void sysfs_remove_shadow_dir(struct dentry
*shadow
)
777 __sysfs_remove_dir(shadow
);
780 const struct file_operations sysfs_dir_operations
= {
781 .open
= sysfs_dir_open
,
782 .release
= sysfs_dir_close
,
783 .llseek
= sysfs_dir_lseek
,
784 .read
= generic_read_dir
,
785 .readdir
= sysfs_readdir
,