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 static 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 is being released after deletion, s_active is write
57 * locked. If @sd is cursor for directory walk or being
58 * released prematurely, s_active has no reader or writer.
60 * sysfs_deactivate() lies to lockdep that s_active is
61 * unlocked immediately. Lie one more time to cover the
64 if (!down_write_trylock(&sd->s_active))
65 rwsem_acquire(&sd->s_active.dep_map,
66 SYSFS_S_ACTIVE_DEACTIVATE, 0, _RET_IP_);
67 up_write(&sd->s_active);
69 if (sd->s_type & SYSFS_KOBJ_LINK)
70 sysfs_put(sd->s_elem.symlink.target_sd);
71 if (sd->s_type & SYSFS_COPY_NAME)
74 sysfs_free_ino(sd->s_ino);
75 kmem_cache_free(sysfs_dir_cachep, sd);
78 if (sd && atomic_dec_and_test(&sd->s_count))
82 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
84 struct sysfs_dirent * sd = dentry->d_fsdata;
87 /* sd->s_dentry is protected with sysfs_lock. This
88 * allows sysfs_drop_dentry() to dereference it.
90 spin_lock(&sysfs_lock);
92 /* The dentry might have been deleted or another
93 * lookup could have happened updating sd->s_dentry to
94 * point the new dentry. Ignore if it isn't pointing
97 if (sd->s_dentry == dentry)
99 spin_unlock(&sysfs_lock);
105 static struct dentry_operations sysfs_dentry_ops = {
106 .d_iput = sysfs_d_iput,
109 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
111 char *dup_name = NULL;
112 struct sysfs_dirent *sd = NULL;
114 if (type & SYSFS_COPY_NAME) {
115 name = dup_name = kstrdup(name, GFP_KERNEL);
120 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
124 if (sysfs_alloc_ino(&sd->s_ino))
127 atomic_set(&sd->s_count, 1);
128 atomic_set(&sd->s_event, 1);
129 init_rwsem(&sd->s_active);
130 INIT_LIST_HEAD(&sd->s_children);
131 INIT_LIST_HEAD(&sd->s_sibling);
141 kmem_cache_free(sysfs_dir_cachep, sd);
145 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
147 dentry->d_op = &sysfs_dentry_ops;
148 dentry->d_fsdata = sysfs_get(sd);
150 /* protect sd->s_dentry against sysfs_d_iput */
151 spin_lock(&sysfs_lock);
152 sd->s_dentry = dentry;
153 spin_unlock(&sysfs_lock);
158 void sysfs_attach_dirent(struct sysfs_dirent *sd,
159 struct sysfs_dirent *parent_sd, struct dentry *dentry)
162 sysfs_attach_dentry(sd, dentry);
165 sd->s_parent = sysfs_get(parent_sd);
166 list_add(&sd->s_sibling, &parent_sd->s_children);
172 * Return -EEXIST if there is already a sysfs element with the same name for
175 * called with parent inode's i_mutex held
177 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
178 const unsigned char *new)
180 struct sysfs_dirent * sd;
182 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
184 if (strcmp(sd->s_name, new))
194 static int create_dir(struct kobject *kobj, struct dentry *parent,
195 const char *name, struct dentry **p_dentry)
198 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
199 struct dentry *dentry;
201 struct sysfs_dirent *sd;
203 mutex_lock(&parent->d_inode->i_mutex);
206 dentry = lookup_one_len(name, parent, strlen(name));
207 if (IS_ERR(dentry)) {
208 error = PTR_ERR(dentry);
217 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
220 sd->s_elem.dir.kobj = kobj;
222 inode = sysfs_get_inode(sd);
226 if (inode->i_state & I_NEW) {
227 inode->i_op = &sysfs_dir_inode_operations;
228 inode->i_fop = &sysfs_dir_operations;
229 /* directory inodes start off with i_nlink == 2 (for ".") */
235 if (sysfs_dirent_exist(parent->d_fsdata, name))
238 sysfs_instantiate(dentry, inode);
239 inc_nlink(parent->d_inode);
240 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
244 goto out_unlock; /* pin directory dentry in core */
255 mutex_unlock(&parent->d_inode->i_mutex);
260 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
262 return create_dir(k,k->dentry,n,d);
266 * sysfs_create_dir - create a directory for an object.
267 * @kobj: object we're creating directory for.
268 * @shadow_parent: parent parent object.
271 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
273 struct dentry * dentry = NULL;
274 struct dentry * parent;
280 parent = shadow_parent;
281 else if (kobj->parent)
282 parent = kobj->parent->dentry;
283 else if (sysfs_mount && sysfs_mount->mnt_sb)
284 parent = sysfs_mount->mnt_sb->s_root;
288 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
290 kobj->dentry = dentry;
294 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
295 struct nameidata *nd)
297 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
298 struct sysfs_dirent * sd;
302 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
303 if ((sd->s_type & SYSFS_NOT_PINNED) &&
304 !strcmp(sd->s_name, dentry->d_name.name)) {
314 /* attach dentry and inode */
315 inode = sysfs_get_inode(sd);
317 return ERR_PTR(-ENOMEM);
319 if (inode->i_state & I_NEW) {
320 /* initialize inode according to type */
321 if (sd->s_type & SYSFS_KOBJ_ATTR) {
322 inode->i_size = PAGE_SIZE;
323 inode->i_fop = &sysfs_file_operations;
324 } else if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
325 struct bin_attribute *bin_attr =
326 sd->s_elem.bin_attr.bin_attr;
327 inode->i_size = bin_attr->size;
328 inode->i_fop = &bin_fops;
329 } else if (sd->s_type & SYSFS_KOBJ_LINK)
330 inode->i_op = &sysfs_symlink_inode_operations;
333 sysfs_instantiate(dentry, inode);
334 sysfs_attach_dentry(sd, dentry);
339 const struct inode_operations sysfs_dir_inode_operations = {
340 .lookup = sysfs_lookup,
341 .setattr = sysfs_setattr,
344 static void remove_dir(struct dentry * d)
346 struct dentry *parent = d->d_parent;
347 struct sysfs_dirent *sd = d->d_fsdata;
349 mutex_lock(&parent->d_inode->i_mutex);
351 list_del_init(&sd->s_sibling);
353 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
354 atomic_read(&d->d_count));
356 mutex_unlock(&parent->d_inode->i_mutex);
358 sysfs_drop_dentry(sd);
359 sysfs_deactivate(sd);
363 void sysfs_remove_subdir(struct dentry * d)
369 static void __sysfs_remove_dir(struct dentry *dentry)
372 struct sysfs_dirent * parent_sd;
373 struct sysfs_dirent * sd, * tmp;
378 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
379 mutex_lock(&dentry->d_inode->i_mutex);
380 parent_sd = dentry->d_fsdata;
381 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
382 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
384 list_move(&sd->s_sibling, &removed);
386 mutex_unlock(&dentry->d_inode->i_mutex);
388 list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
389 list_del_init(&sd->s_sibling);
390 sysfs_drop_dentry(sd);
391 sysfs_deactivate(sd);
399 * sysfs_remove_dir - remove an object's directory.
402 * The only thing special about this is that we remove any files in
403 * the directory before we remove the directory, and we've inlined
404 * what used to be sysfs_rmdir() below, instead of calling separately.
407 void sysfs_remove_dir(struct kobject * kobj)
409 struct dentry *d = kobj->dentry;
411 spin_lock(&kobj_sysfs_assoc_lock);
413 spin_unlock(&kobj_sysfs_assoc_lock);
415 __sysfs_remove_dir(d);
418 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
419 const char *new_name)
421 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
422 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
423 struct dentry *new_dentry;
430 down_write(&sysfs_rename_sem);
431 mutex_lock(&new_parent->d_inode->i_mutex);
433 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
434 if (IS_ERR(new_dentry)) {
435 error = PTR_ERR(new_dentry);
439 /* By allowing two different directories with the same
440 * d_parent we allow this routine to move between different
441 * shadows of the same directory
444 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
445 new_dentry->d_parent->d_inode != new_parent->d_inode ||
446 new_dentry == kobj->dentry)
450 if (new_dentry->d_inode)
453 /* rename kobject and sysfs_dirent */
455 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
459 error = kobject_set_name(kobj, "%s", new_name);
464 sd->s_name = new_name;
466 /* move under the new parent */
467 d_add(new_dentry, NULL);
468 d_move(kobj->dentry, new_dentry);
470 list_del_init(&sd->s_sibling);
471 sysfs_get(parent_sd);
472 sysfs_put(sd->s_parent);
473 sd->s_parent = parent_sd;
474 list_add(&sd->s_sibling, &parent_sd->s_children);
486 mutex_unlock(&new_parent->d_inode->i_mutex);
487 up_write(&sysfs_rename_sem);
491 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
493 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
494 struct sysfs_dirent *new_parent_sd, *sd;
497 old_parent_dentry = kobj->parent ?
498 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
499 new_parent_dentry = new_parent ?
500 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
502 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
503 return 0; /* nothing to move */
505 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
506 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
507 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
511 new_parent_sd = new_parent_dentry->d_fsdata;
512 sd = kobj->dentry->d_fsdata;
514 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
516 if (IS_ERR(new_dentry)) {
517 error = PTR_ERR(new_dentry);
521 d_add(new_dentry, NULL);
522 d_move(kobj->dentry, new_dentry);
525 /* Remove from old parent's list and insert into new parent's list. */
526 list_del_init(&sd->s_sibling);
527 sysfs_get(new_parent_sd);
528 sysfs_put(sd->s_parent);
529 sd->s_parent = new_parent_sd;
530 list_add(&sd->s_sibling, &new_parent_sd->s_children);
533 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
534 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
539 static int sysfs_dir_open(struct inode *inode, struct file *file)
541 struct dentry * dentry = file->f_path.dentry;
542 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
543 struct sysfs_dirent * sd;
545 mutex_lock(&dentry->d_inode->i_mutex);
546 sd = sysfs_new_dirent("_DIR_", 0, 0);
548 sysfs_attach_dirent(sd, parent_sd, NULL);
549 mutex_unlock(&dentry->d_inode->i_mutex);
551 file->private_data = sd;
552 return sd ? 0 : -ENOMEM;
555 static int sysfs_dir_close(struct inode *inode, struct file *file)
557 struct dentry * dentry = file->f_path.dentry;
558 struct sysfs_dirent * cursor = file->private_data;
560 mutex_lock(&dentry->d_inode->i_mutex);
561 list_del_init(&cursor->s_sibling);
562 mutex_unlock(&dentry->d_inode->i_mutex);
564 release_sysfs_dirent(cursor);
569 /* Relationship between s_mode and the DT_xxx types */
570 static inline unsigned char dt_type(struct sysfs_dirent *sd)
572 return (sd->s_mode >> 12) & 15;
575 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
577 struct dentry *dentry = filp->f_path.dentry;
578 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
579 struct sysfs_dirent *cursor = filp->private_data;
580 struct list_head *p, *q = &cursor->s_sibling;
586 ino = parent_sd->s_ino;
587 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
593 if (parent_sd->s_parent)
594 ino = parent_sd->s_parent->s_ino;
596 ino = parent_sd->s_ino;
597 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
603 if (filp->f_pos == 2)
604 list_move(q, &parent_sd->s_children);
606 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
607 struct sysfs_dirent *next;
611 next = list_entry(p, struct sysfs_dirent,
620 if (filldir(dirent, name, len, filp->f_pos, ino,
632 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
634 struct dentry * dentry = file->f_path.dentry;
636 mutex_lock(&dentry->d_inode->i_mutex);
639 offset += file->f_pos;
644 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
647 if (offset != file->f_pos) {
648 file->f_pos = offset;
649 if (file->f_pos >= 2) {
650 struct sysfs_dirent *sd = dentry->d_fsdata;
651 struct sysfs_dirent *cursor = file->private_data;
653 loff_t n = file->f_pos - 2;
655 list_del(&cursor->s_sibling);
656 p = sd->s_children.next;
657 while (n && p != &sd->s_children) {
658 struct sysfs_dirent *next;
659 next = list_entry(p, struct sysfs_dirent,
665 list_add_tail(&cursor->s_sibling, p);
668 mutex_unlock(&dentry->d_inode->i_mutex);
674 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
675 * @kobj: object we're creating shadow of.
678 int sysfs_make_shadowed_dir(struct kobject *kobj,
679 void * (*follow_link)(struct dentry *, struct nameidata *))
682 struct inode_operations *i_op;
684 inode = kobj->dentry->d_inode;
685 if (inode->i_op != &sysfs_dir_inode_operations)
688 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
692 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
693 i_op->follow_link = follow_link;
695 /* Locking of inode->i_op?
696 * Since setting i_op is a single word write and they
697 * are atomic we should be ok here.
704 * sysfs_create_shadow_dir - create a shadow directory for an object.
705 * @kobj: object we're creating directory for.
707 * sysfs_make_shadowed_dir must already have been called on this
711 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
713 struct dentry *dir = kobj->dentry;
714 struct inode *inode = dir->d_inode;
715 struct dentry *parent = dir->d_parent;
716 struct sysfs_dirent *parent_sd = parent->d_fsdata;
717 struct dentry *shadow;
718 struct sysfs_dirent *sd;
720 shadow = ERR_PTR(-EINVAL);
721 if (!sysfs_is_shadowed_inode(inode))
724 shadow = d_alloc(parent, &dir->d_name);
728 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
731 sd->s_elem.dir.kobj = kobj;
732 /* point to parent_sd but don't attach to it */
733 sd->s_parent = sysfs_get(parent_sd);
734 sysfs_attach_dirent(sd, NULL, shadow);
736 d_instantiate(shadow, igrab(inode));
738 inc_nlink(parent->d_inode);
740 dget(shadow); /* Extra count - pin the dentry in core */
746 shadow = ERR_PTR(-ENOMEM);
751 * sysfs_remove_shadow_dir - remove an object's directory.
752 * @shadow: dentry of shadow directory
754 * The only thing special about this is that we remove any files in
755 * the directory before we remove the directory, and we've inlined
756 * what used to be sysfs_rmdir() below, instead of calling separately.
759 void sysfs_remove_shadow_dir(struct dentry *shadow)
761 __sysfs_remove_dir(shadow);
764 const struct file_operations sysfs_dir_operations = {
765 .open = sysfs_dir_open,
766 .release = sysfs_dir_close,
767 .llseek = sysfs_dir_lseek,
768 .read = generic_read_dir,
769 .readdir = sysfs_readdir,