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 <asm/semaphore.h>
15 DECLARE_RWSEM(sysfs_rename_sem
);
16 spinlock_t sysfs_lock
= SPIN_LOCK_UNLOCKED
;
18 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
20 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
23 /* sd->s_dentry is protected with sysfs_lock. This
24 * allows sysfs_drop_dentry() to dereference it.
26 spin_lock(&sysfs_lock
);
28 /* The dentry might have been deleted or another
29 * lookup could have happened updating sd->s_dentry to
30 * point the new dentry. Ignore if it isn't pointing
33 if (sd
->s_dentry
== dentry
)
35 spin_unlock(&sysfs_lock
);
41 static struct dentry_operations sysfs_dentry_ops
= {
42 .d_iput
= sysfs_d_iput
,
45 static unsigned int sysfs_inode_counter
;
46 ino_t
sysfs_get_inum(void)
48 if (unlikely(sysfs_inode_counter
< 3))
49 sysfs_inode_counter
= 3;
50 return sysfs_inode_counter
++;
54 * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
56 static struct sysfs_dirent
* __sysfs_new_dirent(void * element
)
58 struct sysfs_dirent
* sd
;
60 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
64 sd
->s_ino
= sysfs_get_inum();
65 atomic_set(&sd
->s_count
, 1);
66 atomic_set(&sd
->s_event
, 1);
67 INIT_LIST_HEAD(&sd
->s_children
);
68 INIT_LIST_HEAD(&sd
->s_sibling
);
69 sd
->s_element
= element
;
74 static void __sysfs_list_dirent(struct sysfs_dirent
*parent_sd
,
75 struct sysfs_dirent
*sd
)
78 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
81 static struct sysfs_dirent
* sysfs_new_dirent(struct sysfs_dirent
*parent_sd
,
84 struct sysfs_dirent
*sd
;
85 sd
= __sysfs_new_dirent(element
);
86 __sysfs_list_dirent(parent_sd
, sd
);
92 * Return -EEXIST if there is already a sysfs element with the same name for
95 * called with parent inode's i_mutex held
97 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
98 const unsigned char *new)
100 struct sysfs_dirent
* sd
;
102 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
104 const unsigned char *existing
= sysfs_get_name(sd
);
105 if (strcmp(existing
, new))
116 static struct sysfs_dirent
*
117 __sysfs_make_dirent(struct dentry
*dentry
, void *element
, mode_t mode
, int type
)
119 struct sysfs_dirent
* sd
;
121 sd
= __sysfs_new_dirent(element
);
127 sd
->s_dentry
= dentry
;
129 dentry
->d_fsdata
= sysfs_get(sd
);
130 dentry
->d_op
= &sysfs_dentry_ops
;
137 int sysfs_make_dirent(struct sysfs_dirent
* parent_sd
, struct dentry
* dentry
,
138 void * element
, umode_t mode
, int type
)
140 struct sysfs_dirent
*sd
;
142 sd
= __sysfs_make_dirent(dentry
, element
, mode
, type
);
143 __sysfs_list_dirent(parent_sd
, sd
);
145 return sd
? 0 : -ENOMEM
;
148 static int init_dir(struct inode
* inode
)
150 inode
->i_op
= &sysfs_dir_inode_operations
;
151 inode
->i_fop
= &sysfs_dir_operations
;
153 /* directory inodes start off with i_nlink == 2 (for "." entry) */
158 static int init_file(struct inode
* inode
)
160 inode
->i_size
= PAGE_SIZE
;
161 inode
->i_fop
= &sysfs_file_operations
;
165 static int init_symlink(struct inode
* inode
)
167 inode
->i_op
= &sysfs_symlink_inode_operations
;
171 static int create_dir(struct kobject
* k
, struct dentry
* p
,
172 const char * n
, struct dentry
** d
)
175 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
177 mutex_lock(&p
->d_inode
->i_mutex
);
178 *d
= lookup_one_len(n
, p
, strlen(n
));
180 if (sysfs_dirent_exist(p
->d_fsdata
, n
))
183 error
= sysfs_make_dirent(p
->d_fsdata
, *d
, k
, mode
,
186 error
= sysfs_create(*d
, mode
, init_dir
);
188 inc_nlink(p
->d_inode
);
189 (*d
)->d_op
= &sysfs_dentry_ops
;
193 if (error
&& (error
!= -EEXIST
)) {
194 struct sysfs_dirent
*sd
= (*d
)->d_fsdata
;
196 list_del_init(&sd
->s_sibling
);
204 mutex_unlock(&p
->d_inode
->i_mutex
);
209 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
211 return create_dir(k
,k
->dentry
,n
,d
);
215 * sysfs_create_dir - create a directory for an object.
216 * @kobj: object we're creating directory for.
217 * @shadow_parent: parent parent object.
220 int sysfs_create_dir(struct kobject
* kobj
, struct dentry
*shadow_parent
)
222 struct dentry
* dentry
= NULL
;
223 struct dentry
* parent
;
229 parent
= shadow_parent
;
230 else if (kobj
->parent
)
231 parent
= kobj
->parent
->dentry
;
232 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
233 parent
= sysfs_mount
->mnt_sb
->s_root
;
237 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
239 kobj
->dentry
= dentry
;
243 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
246 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
248 struct attribute
* attr
= NULL
;
249 struct bin_attribute
* bin_attr
= NULL
;
250 int (* init
) (struct inode
*) = NULL
;
253 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
254 bin_attr
= sd
->s_element
;
255 attr
= &bin_attr
->attr
;
257 attr
= sd
->s_element
;
261 dentry
->d_fsdata
= sysfs_get(sd
);
262 /* protect sd->s_dentry against sysfs_d_iput */
263 spin_lock(&sysfs_lock
);
264 sd
->s_dentry
= dentry
;
265 spin_unlock(&sysfs_lock
);
266 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
273 dentry
->d_inode
->i_size
= bin_attr
->size
;
274 dentry
->d_inode
->i_fop
= &bin_fops
;
276 dentry
->d_op
= &sysfs_dentry_ops
;
282 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
286 dentry
->d_fsdata
= sysfs_get(sd
);
287 /* protect sd->s_dentry against sysfs_d_iput */
288 spin_lock(&sysfs_lock
);
289 sd
->s_dentry
= dentry
;
290 spin_unlock(&sysfs_lock
);
291 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
293 dentry
->d_op
= &sysfs_dentry_ops
;
301 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
302 struct nameidata
*nd
)
304 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
305 struct sysfs_dirent
* sd
;
308 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
309 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
310 const unsigned char * name
= sysfs_get_name(sd
);
312 if (strcmp(name
, dentry
->d_name
.name
))
315 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
316 err
= sysfs_attach_link(sd
, dentry
);
318 err
= sysfs_attach_attr(sd
, dentry
);
326 const struct inode_operations sysfs_dir_inode_operations
= {
327 .lookup
= sysfs_lookup
,
328 .setattr
= sysfs_setattr
,
331 static void remove_dir(struct dentry
* d
)
333 struct dentry
* parent
= dget(d
->d_parent
);
334 struct sysfs_dirent
* sd
;
336 mutex_lock(&parent
->d_inode
->i_mutex
);
339 list_del_init(&sd
->s_sibling
);
342 simple_rmdir(parent
->d_inode
,d
);
344 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
345 atomic_read(&d
->d_count
));
347 mutex_unlock(&parent
->d_inode
->i_mutex
);
351 void sysfs_remove_subdir(struct dentry
* d
)
357 static void __sysfs_remove_dir(struct dentry
*dentry
)
359 struct sysfs_dirent
* parent_sd
;
360 struct sysfs_dirent
* sd
, * tmp
;
366 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
367 mutex_lock(&dentry
->d_inode
->i_mutex
);
368 parent_sd
= dentry
->d_fsdata
;
369 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
370 if (!sd
->s_element
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
372 list_del_init(&sd
->s_sibling
);
373 sysfs_drop_dentry(sd
, dentry
);
376 mutex_unlock(&dentry
->d_inode
->i_mutex
);
380 * Drop reference from dget() on entrance.
386 * sysfs_remove_dir - remove an object's directory.
389 * The only thing special about this is that we remove any files in
390 * the directory before we remove the directory, and we've inlined
391 * what used to be sysfs_rmdir() below, instead of calling separately.
394 void sysfs_remove_dir(struct kobject
* kobj
)
396 __sysfs_remove_dir(kobj
->dentry
);
400 int sysfs_rename_dir(struct kobject
* kobj
, struct dentry
*new_parent
,
401 const char *new_name
)
404 struct dentry
* new_dentry
;
409 down_write(&sysfs_rename_sem
);
410 mutex_lock(&new_parent
->d_inode
->i_mutex
);
412 new_dentry
= lookup_one_len(new_name
, new_parent
, strlen(new_name
));
413 if (!IS_ERR(new_dentry
)) {
414 /* By allowing two different directories with the
415 * same d_parent we allow this routine to move
416 * between different shadows of the same directory
418 if (kobj
->dentry
->d_parent
->d_inode
!= new_parent
->d_inode
)
420 else if (new_dentry
->d_parent
->d_inode
!= new_parent
->d_inode
)
422 else if (new_dentry
== kobj
->dentry
)
424 else if (!new_dentry
->d_inode
) {
425 error
= kobject_set_name(kobj
, "%s", new_name
);
427 struct sysfs_dirent
*sd
, *parent_sd
;
429 d_add(new_dentry
, NULL
);
430 d_move(kobj
->dentry
, new_dentry
);
432 sd
= kobj
->dentry
->d_fsdata
;
433 parent_sd
= new_parent
->d_fsdata
;
435 list_del_init(&sd
->s_sibling
);
436 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
444 mutex_unlock(&new_parent
->d_inode
->i_mutex
);
445 up_write(&sysfs_rename_sem
);
450 int sysfs_move_dir(struct kobject
*kobj
, struct kobject
*new_parent
)
452 struct dentry
*old_parent_dentry
, *new_parent_dentry
, *new_dentry
;
453 struct sysfs_dirent
*new_parent_sd
, *sd
;
456 old_parent_dentry
= kobj
->parent
?
457 kobj
->parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
458 new_parent_dentry
= new_parent
?
459 new_parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
461 if (old_parent_dentry
->d_inode
== new_parent_dentry
->d_inode
)
462 return 0; /* nothing to move */
464 mutex_lock(&old_parent_dentry
->d_inode
->i_mutex
);
465 if (!mutex_trylock(&new_parent_dentry
->d_inode
->i_mutex
)) {
466 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
470 new_parent_sd
= new_parent_dentry
->d_fsdata
;
471 sd
= kobj
->dentry
->d_fsdata
;
473 new_dentry
= lookup_one_len(kobj
->name
, new_parent_dentry
,
475 if (IS_ERR(new_dentry
)) {
476 error
= PTR_ERR(new_dentry
);
480 d_add(new_dentry
, NULL
);
481 d_move(kobj
->dentry
, new_dentry
);
484 /* Remove from old parent's list and insert into new parent's list. */
485 list_del_init(&sd
->s_sibling
);
486 list_add(&sd
->s_sibling
, &new_parent_sd
->s_children
);
489 mutex_unlock(&new_parent_dentry
->d_inode
->i_mutex
);
490 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
495 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
497 struct dentry
* dentry
= file
->f_path
.dentry
;
498 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
500 mutex_lock(&dentry
->d_inode
->i_mutex
);
501 file
->private_data
= sysfs_new_dirent(parent_sd
, NULL
);
502 mutex_unlock(&dentry
->d_inode
->i_mutex
);
504 return file
->private_data
? 0 : -ENOMEM
;
508 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
510 struct dentry
* dentry
= file
->f_path
.dentry
;
511 struct sysfs_dirent
* cursor
= file
->private_data
;
513 mutex_lock(&dentry
->d_inode
->i_mutex
);
514 list_del_init(&cursor
->s_sibling
);
515 mutex_unlock(&dentry
->d_inode
->i_mutex
);
517 release_sysfs_dirent(cursor
);
522 /* Relationship between s_mode and the DT_xxx types */
523 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
525 return (sd
->s_mode
>> 12) & 15;
528 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
530 struct dentry
*dentry
= filp
->f_path
.dentry
;
531 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
532 struct sysfs_dirent
*cursor
= filp
->private_data
;
533 struct list_head
*p
, *q
= &cursor
->s_sibling
;
539 ino
= parent_sd
->s_ino
;
540 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
546 ino
= parent_ino(dentry
);
547 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
553 if (filp
->f_pos
== 2)
554 list_move(q
, &parent_sd
->s_children
);
556 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
557 struct sysfs_dirent
*next
;
561 next
= list_entry(p
, struct sysfs_dirent
,
563 if (!next
->s_element
)
566 name
= sysfs_get_name(next
);
570 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
582 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
584 struct dentry
* dentry
= file
->f_path
.dentry
;
586 mutex_lock(&dentry
->d_inode
->i_mutex
);
589 offset
+= file
->f_pos
;
594 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
597 if (offset
!= file
->f_pos
) {
598 file
->f_pos
= offset
;
599 if (file
->f_pos
>= 2) {
600 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
601 struct sysfs_dirent
*cursor
= file
->private_data
;
603 loff_t n
= file
->f_pos
- 2;
605 list_del(&cursor
->s_sibling
);
606 p
= sd
->s_children
.next
;
607 while (n
&& p
!= &sd
->s_children
) {
608 struct sysfs_dirent
*next
;
609 next
= list_entry(p
, struct sysfs_dirent
,
615 list_add_tail(&cursor
->s_sibling
, p
);
618 mutex_unlock(&dentry
->d_inode
->i_mutex
);
624 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
625 * @kobj: object we're creating shadow of.
628 int sysfs_make_shadowed_dir(struct kobject
*kobj
,
629 void * (*follow_link
)(struct dentry
*, struct nameidata
*))
632 struct inode_operations
*i_op
;
634 inode
= kobj
->dentry
->d_inode
;
635 if (inode
->i_op
!= &sysfs_dir_inode_operations
)
638 i_op
= kmalloc(sizeof(*i_op
), GFP_KERNEL
);
642 memcpy(i_op
, &sysfs_dir_inode_operations
, sizeof(*i_op
));
643 i_op
->follow_link
= follow_link
;
645 /* Locking of inode->i_op?
646 * Since setting i_op is a single word write and they
647 * are atomic we should be ok here.
654 * sysfs_create_shadow_dir - create a shadow directory for an object.
655 * @kobj: object we're creating directory for.
657 * sysfs_make_shadowed_dir must already have been called on this
661 struct dentry
*sysfs_create_shadow_dir(struct kobject
*kobj
)
663 struct sysfs_dirent
*sd
;
664 struct dentry
*parent
, *dir
, *shadow
;
668 inode
= dir
->d_inode
;
669 parent
= dir
->d_parent
;
670 shadow
= ERR_PTR(-EINVAL
);
671 if (!sysfs_is_shadowed_inode(inode
))
674 shadow
= d_alloc(parent
, &dir
->d_name
);
678 sd
= __sysfs_make_dirent(shadow
, kobj
, inode
->i_mode
, SYSFS_DIR
);
682 d_instantiate(shadow
, igrab(inode
));
684 inc_nlink(parent
->d_inode
);
685 shadow
->d_op
= &sysfs_dentry_ops
;
687 dget(shadow
); /* Extra count - pin the dentry in core */
693 shadow
= ERR_PTR(-ENOMEM
);
698 * sysfs_remove_shadow_dir - remove an object's directory.
699 * @shadow: dentry of shadow directory
701 * The only thing special about this is that we remove any files in
702 * the directory before we remove the directory, and we've inlined
703 * what used to be sysfs_rmdir() below, instead of calling separately.
706 void sysfs_remove_shadow_dir(struct dentry
*shadow
)
708 __sysfs_remove_dir(shadow
);
711 const struct file_operations sysfs_dir_operations
= {
712 .open
= sysfs_dir_open
,
713 .release
= sysfs_dir_close
,
714 .llseek
= sysfs_dir_lseek
,
715 .read
= generic_read_dir
,
716 .readdir
= sysfs_readdir
,