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
);
17 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
19 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
22 BUG_ON(sd
->s_dentry
!= dentry
);
29 static struct dentry_operations sysfs_dentry_ops
= {
30 .d_iput
= sysfs_d_iput
,
34 * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
36 static struct sysfs_dirent
* __sysfs_new_dirent(void * element
)
38 struct sysfs_dirent
* sd
;
40 sd
= kmem_cache_zalloc(sysfs_dir_cachep
, GFP_KERNEL
);
44 atomic_set(&sd
->s_count
, 1);
45 atomic_set(&sd
->s_event
, 1);
46 INIT_LIST_HEAD(&sd
->s_children
);
47 INIT_LIST_HEAD(&sd
->s_sibling
);
48 sd
->s_element
= element
;
53 static void __sysfs_list_dirent(struct sysfs_dirent
*parent_sd
,
54 struct sysfs_dirent
*sd
)
57 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
60 static struct sysfs_dirent
* sysfs_new_dirent(struct sysfs_dirent
*parent_sd
,
63 struct sysfs_dirent
*sd
;
64 sd
= __sysfs_new_dirent(element
);
65 __sysfs_list_dirent(parent_sd
, sd
);
71 * Return -EEXIST if there is already a sysfs element with the same name for
74 * called with parent inode's i_mutex held
76 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
77 const unsigned char *new)
79 struct sysfs_dirent
* sd
;
81 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
83 const unsigned char *existing
= sysfs_get_name(sd
);
84 if (strcmp(existing
, new))
95 static struct sysfs_dirent
*
96 __sysfs_make_dirent(struct dentry
*dentry
, void *element
, mode_t mode
, int type
)
98 struct sysfs_dirent
* sd
;
100 sd
= __sysfs_new_dirent(element
);
106 sd
->s_dentry
= dentry
;
108 dentry
->d_fsdata
= sysfs_get(sd
);
109 dentry
->d_op
= &sysfs_dentry_ops
;
116 int sysfs_make_dirent(struct sysfs_dirent
* parent_sd
, struct dentry
* dentry
,
117 void * element
, umode_t mode
, int type
)
119 struct sysfs_dirent
*sd
;
121 sd
= __sysfs_make_dirent(dentry
, element
, mode
, type
);
122 __sysfs_list_dirent(parent_sd
, sd
);
124 return sd
? 0 : -ENOMEM
;
127 static int init_dir(struct inode
* inode
)
129 inode
->i_op
= &sysfs_dir_inode_operations
;
130 inode
->i_fop
= &sysfs_dir_operations
;
132 /* directory inodes start off with i_nlink == 2 (for "." entry) */
137 static int init_file(struct inode
* inode
)
139 inode
->i_size
= PAGE_SIZE
;
140 inode
->i_fop
= &sysfs_file_operations
;
144 static int init_symlink(struct inode
* inode
)
146 inode
->i_op
= &sysfs_symlink_inode_operations
;
150 static int create_dir(struct kobject
* k
, struct dentry
* p
,
151 const char * n
, struct dentry
** d
)
154 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
156 mutex_lock(&p
->d_inode
->i_mutex
);
157 *d
= lookup_one_len(n
, p
, strlen(n
));
159 if (sysfs_dirent_exist(p
->d_fsdata
, n
))
162 error
= sysfs_make_dirent(p
->d_fsdata
, *d
, k
, mode
,
165 error
= sysfs_create(*d
, mode
, init_dir
);
167 inc_nlink(p
->d_inode
);
168 (*d
)->d_op
= &sysfs_dentry_ops
;
172 if (error
&& (error
!= -EEXIST
)) {
173 struct sysfs_dirent
*sd
= (*d
)->d_fsdata
;
175 list_del_init(&sd
->s_sibling
);
183 mutex_unlock(&p
->d_inode
->i_mutex
);
188 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
190 return create_dir(k
,k
->dentry
,n
,d
);
194 * sysfs_create_dir - create a directory for an object.
195 * @kobj: object we're creating directory for.
196 * @shadow_parent: parent parent object.
199 int sysfs_create_dir(struct kobject
* kobj
, struct dentry
*shadow_parent
)
201 struct dentry
* dentry
= NULL
;
202 struct dentry
* parent
;
208 parent
= shadow_parent
;
209 else if (kobj
->parent
)
210 parent
= kobj
->parent
->dentry
;
211 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
212 parent
= sysfs_mount
->mnt_sb
->s_root
;
216 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
218 kobj
->dentry
= dentry
;
222 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
225 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
227 struct attribute
* attr
= NULL
;
228 struct bin_attribute
* bin_attr
= NULL
;
229 int (* init
) (struct inode
*) = NULL
;
232 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
233 bin_attr
= sd
->s_element
;
234 attr
= &bin_attr
->attr
;
236 attr
= sd
->s_element
;
240 dentry
->d_fsdata
= sysfs_get(sd
);
241 sd
->s_dentry
= dentry
;
242 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
249 dentry
->d_inode
->i_size
= bin_attr
->size
;
250 dentry
->d_inode
->i_fop
= &bin_fops
;
252 dentry
->d_op
= &sysfs_dentry_ops
;
258 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
262 dentry
->d_fsdata
= sysfs_get(sd
);
263 sd
->s_dentry
= dentry
;
264 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
266 dentry
->d_op
= &sysfs_dentry_ops
;
274 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
275 struct nameidata
*nd
)
277 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
278 struct sysfs_dirent
* sd
;
281 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
282 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
283 const unsigned char * name
= sysfs_get_name(sd
);
285 if (strcmp(name
, dentry
->d_name
.name
))
288 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
289 err
= sysfs_attach_link(sd
, dentry
);
291 err
= sysfs_attach_attr(sd
, dentry
);
299 const struct inode_operations sysfs_dir_inode_operations
= {
300 .lookup
= sysfs_lookup
,
301 .setattr
= sysfs_setattr
,
304 static void remove_dir(struct dentry
* d
)
306 struct dentry
* parent
= dget(d
->d_parent
);
307 struct sysfs_dirent
* sd
;
309 mutex_lock(&parent
->d_inode
->i_mutex
);
312 list_del_init(&sd
->s_sibling
);
315 simple_rmdir(parent
->d_inode
,d
);
317 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
318 atomic_read(&d
->d_count
));
320 mutex_unlock(&parent
->d_inode
->i_mutex
);
324 void sysfs_remove_subdir(struct dentry
* d
)
330 static void __sysfs_remove_dir(struct dentry
*dentry
)
332 struct sysfs_dirent
* parent_sd
;
333 struct sysfs_dirent
* sd
, * tmp
;
339 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
340 mutex_lock(&dentry
->d_inode
->i_mutex
);
341 parent_sd
= dentry
->d_fsdata
;
342 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
343 if (!sd
->s_element
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
345 list_del_init(&sd
->s_sibling
);
346 sysfs_drop_dentry(sd
, dentry
);
349 mutex_unlock(&dentry
->d_inode
->i_mutex
);
353 * Drop reference from dget() on entrance.
359 * sysfs_remove_dir - remove an object's directory.
362 * The only thing special about this is that we remove any files in
363 * the directory before we remove the directory, and we've inlined
364 * what used to be sysfs_rmdir() below, instead of calling separately.
367 void sysfs_remove_dir(struct kobject
* kobj
)
369 __sysfs_remove_dir(kobj
->dentry
);
373 int sysfs_rename_dir(struct kobject
* kobj
, struct dentry
*new_parent
,
374 const char *new_name
)
377 struct dentry
* new_dentry
;
382 down_write(&sysfs_rename_sem
);
383 mutex_lock(&new_parent
->d_inode
->i_mutex
);
385 new_dentry
= lookup_one_len(new_name
, new_parent
, strlen(new_name
));
386 if (!IS_ERR(new_dentry
)) {
387 /* By allowing two different directories with the
388 * same d_parent we allow this routine to move
389 * between different shadows of the same directory
391 if (kobj
->dentry
->d_parent
->d_inode
!= new_parent
->d_inode
)
393 else if (new_dentry
->d_parent
->d_inode
!= new_parent
->d_inode
)
395 else if (new_dentry
== kobj
->dentry
)
397 else if (!new_dentry
->d_inode
) {
398 error
= kobject_set_name(kobj
, "%s", new_name
);
400 struct sysfs_dirent
*sd
, *parent_sd
;
402 d_add(new_dentry
, NULL
);
403 d_move(kobj
->dentry
, new_dentry
);
405 sd
= kobj
->dentry
->d_fsdata
;
406 parent_sd
= new_parent
->d_fsdata
;
408 list_del_init(&sd
->s_sibling
);
409 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
417 mutex_unlock(&new_parent
->d_inode
->i_mutex
);
418 up_write(&sysfs_rename_sem
);
423 int sysfs_move_dir(struct kobject
*kobj
, struct kobject
*new_parent
)
425 struct dentry
*old_parent_dentry
, *new_parent_dentry
, *new_dentry
;
426 struct sysfs_dirent
*new_parent_sd
, *sd
;
429 old_parent_dentry
= kobj
->parent
?
430 kobj
->parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
431 new_parent_dentry
= new_parent
?
432 new_parent
->dentry
: sysfs_mount
->mnt_sb
->s_root
;
434 if (old_parent_dentry
->d_inode
== new_parent_dentry
->d_inode
)
435 return 0; /* nothing to move */
437 mutex_lock(&old_parent_dentry
->d_inode
->i_mutex
);
438 if (!mutex_trylock(&new_parent_dentry
->d_inode
->i_mutex
)) {
439 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
443 new_parent_sd
= new_parent_dentry
->d_fsdata
;
444 sd
= kobj
->dentry
->d_fsdata
;
446 new_dentry
= lookup_one_len(kobj
->name
, new_parent_dentry
,
448 if (IS_ERR(new_dentry
)) {
449 error
= PTR_ERR(new_dentry
);
453 d_add(new_dentry
, NULL
);
454 d_move(kobj
->dentry
, new_dentry
);
457 /* Remove from old parent's list and insert into new parent's list. */
458 list_del_init(&sd
->s_sibling
);
459 list_add(&sd
->s_sibling
, &new_parent_sd
->s_children
);
462 mutex_unlock(&new_parent_dentry
->d_inode
->i_mutex
);
463 mutex_unlock(&old_parent_dentry
->d_inode
->i_mutex
);
468 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
470 struct dentry
* dentry
= file
->f_path
.dentry
;
471 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
473 mutex_lock(&dentry
->d_inode
->i_mutex
);
474 file
->private_data
= sysfs_new_dirent(parent_sd
, NULL
);
475 mutex_unlock(&dentry
->d_inode
->i_mutex
);
477 return file
->private_data
? 0 : -ENOMEM
;
481 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
483 struct dentry
* dentry
= file
->f_path
.dentry
;
484 struct sysfs_dirent
* cursor
= file
->private_data
;
486 mutex_lock(&dentry
->d_inode
->i_mutex
);
487 list_del_init(&cursor
->s_sibling
);
488 mutex_unlock(&dentry
->d_inode
->i_mutex
);
490 release_sysfs_dirent(cursor
);
495 /* Relationship between s_mode and the DT_xxx types */
496 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
498 return (sd
->s_mode
>> 12) & 15;
501 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
503 struct dentry
*dentry
= filp
->f_path
.dentry
;
504 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
505 struct sysfs_dirent
*cursor
= filp
->private_data
;
506 struct list_head
*p
, *q
= &cursor
->s_sibling
;
512 ino
= dentry
->d_inode
->i_ino
;
513 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
519 ino
= parent_ino(dentry
);
520 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
526 if (filp
->f_pos
== 2)
527 list_move(q
, &parent_sd
->s_children
);
529 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
530 struct sysfs_dirent
*next
;
534 next
= list_entry(p
, struct sysfs_dirent
,
536 if (!next
->s_element
)
539 name
= sysfs_get_name(next
);
542 ino
= next
->s_dentry
->d_inode
->i_ino
;
544 ino
= iunique(sysfs_sb
, 2);
546 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
558 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
560 struct dentry
* dentry
= file
->f_path
.dentry
;
562 mutex_lock(&dentry
->d_inode
->i_mutex
);
565 offset
+= file
->f_pos
;
570 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
573 if (offset
!= file
->f_pos
) {
574 file
->f_pos
= offset
;
575 if (file
->f_pos
>= 2) {
576 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
577 struct sysfs_dirent
*cursor
= file
->private_data
;
579 loff_t n
= file
->f_pos
- 2;
581 list_del(&cursor
->s_sibling
);
582 p
= sd
->s_children
.next
;
583 while (n
&& p
!= &sd
->s_children
) {
584 struct sysfs_dirent
*next
;
585 next
= list_entry(p
, struct sysfs_dirent
,
591 list_add_tail(&cursor
->s_sibling
, p
);
594 mutex_unlock(&dentry
->d_inode
->i_mutex
);
600 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
601 * @kobj: object we're creating shadow of.
604 int sysfs_make_shadowed_dir(struct kobject
*kobj
,
605 void * (*follow_link
)(struct dentry
*, struct nameidata
*))
608 struct inode_operations
*i_op
;
610 inode
= kobj
->dentry
->d_inode
;
611 if (inode
->i_op
!= &sysfs_dir_inode_operations
)
614 i_op
= kmalloc(sizeof(*i_op
), GFP_KERNEL
);
618 memcpy(i_op
, &sysfs_dir_inode_operations
, sizeof(*i_op
));
619 i_op
->follow_link
= follow_link
;
621 /* Locking of inode->i_op?
622 * Since setting i_op is a single word write and they
623 * are atomic we should be ok here.
630 * sysfs_create_shadow_dir - create a shadow directory for an object.
631 * @kobj: object we're creating directory for.
633 * sysfs_make_shadowed_dir must already have been called on this
637 struct dentry
*sysfs_create_shadow_dir(struct kobject
*kobj
)
639 struct sysfs_dirent
*sd
;
640 struct dentry
*parent
, *dir
, *shadow
;
644 inode
= dir
->d_inode
;
645 parent
= dir
->d_parent
;
646 shadow
= ERR_PTR(-EINVAL
);
647 if (!sysfs_is_shadowed_inode(inode
))
650 shadow
= d_alloc(parent
, &dir
->d_name
);
654 sd
= __sysfs_make_dirent(shadow
, kobj
, inode
->i_mode
, SYSFS_DIR
);
658 d_instantiate(shadow
, igrab(inode
));
660 inc_nlink(parent
->d_inode
);
661 shadow
->d_op
= &sysfs_dentry_ops
;
663 dget(shadow
); /* Extra count - pin the dentry in core */
669 shadow
= ERR_PTR(-ENOMEM
);
674 * sysfs_remove_shadow_dir - remove an object's directory.
675 * @shadow: dentry of shadow directory
677 * The only thing special about this is that we remove any files in
678 * the directory before we remove the directory, and we've inlined
679 * what used to be sysfs_rmdir() below, instead of calling separately.
682 void sysfs_remove_shadow_dir(struct dentry
*shadow
)
684 __sysfs_remove_dir(shadow
);
687 const struct file_operations sysfs_dir_operations
= {
688 .open
= sysfs_dir_open
,
689 .release
= sysfs_dir_close
,
690 .llseek
= sysfs_dir_lseek
,
691 .read
= generic_read_dir
,
692 .readdir
= sysfs_readdir
,