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 struct sysfs_dirent *parent_sd;
53 parent_sd = sd->s_parent;
55 if (sd->s_type & SYSFS_KOBJ_LINK)
56 kobject_put(sd->s_elem.symlink.target_kobj);
57 if (sd->s_type & SYSFS_COPY_NAME)
60 sysfs_free_ino(sd->s_ino);
61 kmem_cache_free(sysfs_dir_cachep, sd);
64 if (sd && atomic_dec_and_test(&sd->s_count))
68 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
70 struct sysfs_dirent * sd = dentry->d_fsdata;
73 /* sd->s_dentry is protected with sysfs_lock. This
74 * allows sysfs_drop_dentry() to dereference it.
76 spin_lock(&sysfs_lock);
78 /* The dentry might have been deleted or another
79 * lookup could have happened updating sd->s_dentry to
80 * point the new dentry. Ignore if it isn't pointing
83 if (sd->s_dentry == dentry)
85 spin_unlock(&sysfs_lock);
91 static struct dentry_operations sysfs_dentry_ops = {
92 .d_iput = sysfs_d_iput,
95 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
97 char *dup_name = NULL;
98 struct sysfs_dirent *sd = NULL;
100 if (type & SYSFS_COPY_NAME) {
101 name = dup_name = kstrdup(name, GFP_KERNEL);
106 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
110 if (sysfs_alloc_ino(&sd->s_ino))
113 atomic_set(&sd->s_count, 1);
114 atomic_set(&sd->s_event, 1);
115 INIT_LIST_HEAD(&sd->s_children);
116 INIT_LIST_HEAD(&sd->s_sibling);
126 kmem_cache_free(sysfs_dir_cachep, sd);
130 void sysfs_attach_dirent(struct sysfs_dirent *sd,
131 struct sysfs_dirent *parent_sd, struct dentry *dentry)
134 sd->s_dentry = dentry;
135 dentry->d_fsdata = sysfs_get(sd);
136 dentry->d_op = &sysfs_dentry_ops;
140 sd->s_parent = sysfs_get(parent_sd);
141 list_add(&sd->s_sibling, &parent_sd->s_children);
147 * Return -EEXIST if there is already a sysfs element with the same name for
150 * called with parent inode's i_mutex held
152 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
153 const unsigned char *new)
155 struct sysfs_dirent * sd;
157 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
159 if (strcmp(sd->s_name, new))
169 static int init_dir(struct inode * inode)
171 inode->i_op = &sysfs_dir_inode_operations;
172 inode->i_fop = &sysfs_dir_operations;
174 /* directory inodes start off with i_nlink == 2 (for "." entry) */
179 static int init_file(struct inode * inode)
181 inode->i_size = PAGE_SIZE;
182 inode->i_fop = &sysfs_file_operations;
186 static int init_symlink(struct inode * inode)
188 inode->i_op = &sysfs_symlink_inode_operations;
192 static int create_dir(struct kobject *kobj, struct dentry *parent,
193 const char *name, struct dentry **p_dentry)
196 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
197 struct dentry *dentry;
198 struct sysfs_dirent *sd;
200 mutex_lock(&parent->d_inode->i_mutex);
202 dentry = lookup_one_len(name, parent, strlen(name));
203 if (IS_ERR(dentry)) {
204 error = PTR_ERR(dentry);
209 if (sysfs_dirent_exist(parent->d_fsdata, name))
213 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
216 sd->s_elem.dir.kobj = kobj;
217 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
219 error = sysfs_create(dentry, mode, init_dir);
223 inc_nlink(parent->d_inode);
224 dentry->d_op = &sysfs_dentry_ops;
232 list_del_init(&sd->s_sibling);
239 mutex_unlock(&parent->d_inode->i_mutex);
244 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
246 return create_dir(k,k->dentry,n,d);
250 * sysfs_create_dir - create a directory for an object.
251 * @kobj: object we're creating directory for.
252 * @shadow_parent: parent parent object.
255 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
257 struct dentry * dentry = NULL;
258 struct dentry * parent;
264 parent = shadow_parent;
265 else if (kobj->parent)
266 parent = kobj->parent->dentry;
267 else if (sysfs_mount && sysfs_mount->mnt_sb)
268 parent = sysfs_mount->mnt_sb->s_root;
272 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
274 kobj->dentry = dentry;
278 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
281 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
283 struct attribute * attr = NULL;
284 struct bin_attribute * bin_attr = NULL;
285 int (* init) (struct inode *) = NULL;
288 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
289 bin_attr = sd->s_elem.bin_attr.bin_attr;
290 attr = &bin_attr->attr;
292 attr = sd->s_elem.attr.attr;
296 dentry->d_fsdata = sysfs_get(sd);
297 /* protect sd->s_dentry against sysfs_d_iput */
298 spin_lock(&sysfs_lock);
299 sd->s_dentry = dentry;
300 spin_unlock(&sysfs_lock);
301 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
308 dentry->d_inode->i_size = bin_attr->size;
309 dentry->d_inode->i_fop = &bin_fops;
311 dentry->d_op = &sysfs_dentry_ops;
317 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
321 dentry->d_fsdata = sysfs_get(sd);
322 /* protect sd->s_dentry against sysfs_d_iput */
323 spin_lock(&sysfs_lock);
324 sd->s_dentry = dentry;
325 spin_unlock(&sysfs_lock);
326 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
328 dentry->d_op = &sysfs_dentry_ops;
336 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
337 struct nameidata *nd)
339 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
340 struct sysfs_dirent * sd;
343 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
344 if (sd->s_type & SYSFS_NOT_PINNED) {
345 if (strcmp(sd->s_name, dentry->d_name.name))
348 if (sd->s_type & SYSFS_KOBJ_LINK)
349 err = sysfs_attach_link(sd, dentry);
351 err = sysfs_attach_attr(sd, dentry);
359 const struct inode_operations sysfs_dir_inode_operations = {
360 .lookup = sysfs_lookup,
361 .setattr = sysfs_setattr,
364 static void remove_dir(struct dentry * d)
366 struct dentry * parent = dget(d->d_parent);
367 struct sysfs_dirent * sd;
369 mutex_lock(&parent->d_inode->i_mutex);
372 list_del_init(&sd->s_sibling);
375 simple_rmdir(parent->d_inode,d);
377 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
378 atomic_read(&d->d_count));
380 mutex_unlock(&parent->d_inode->i_mutex);
384 void sysfs_remove_subdir(struct dentry * d)
390 static void __sysfs_remove_dir(struct dentry *dentry)
392 struct sysfs_dirent * parent_sd;
393 struct sysfs_dirent * sd, * tmp;
399 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
400 mutex_lock(&dentry->d_inode->i_mutex);
401 parent_sd = dentry->d_fsdata;
402 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
403 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
405 list_del_init(&sd->s_sibling);
406 sysfs_drop_dentry(sd, dentry);
409 mutex_unlock(&dentry->d_inode->i_mutex);
413 * Drop reference from dget() on entrance.
419 * sysfs_remove_dir - remove an object's directory.
422 * The only thing special about this is that we remove any files in
423 * the directory before we remove the directory, and we've inlined
424 * what used to be sysfs_rmdir() below, instead of calling separately.
427 void sysfs_remove_dir(struct kobject * kobj)
429 __sysfs_remove_dir(kobj->dentry);
433 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
434 const char *new_name)
436 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
437 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
438 struct dentry *new_dentry;
445 down_write(&sysfs_rename_sem);
446 mutex_lock(&new_parent->d_inode->i_mutex);
448 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
449 if (IS_ERR(new_dentry)) {
450 error = PTR_ERR(new_dentry);
454 /* By allowing two different directories with the same
455 * d_parent we allow this routine to move between different
456 * shadows of the same directory
459 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
460 new_dentry->d_parent->d_inode != new_parent->d_inode ||
461 new_dentry == kobj->dentry)
465 if (new_dentry->d_inode)
468 /* rename kobject and sysfs_dirent */
470 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
474 error = kobject_set_name(kobj, "%s", new_name);
479 sd->s_name = new_name;
481 /* move under the new parent */
482 d_add(new_dentry, NULL);
483 d_move(kobj->dentry, new_dentry);
485 list_del_init(&sd->s_sibling);
486 list_add(&sd->s_sibling, &parent_sd->s_children);
498 mutex_unlock(&new_parent->d_inode->i_mutex);
499 up_write(&sysfs_rename_sem);
503 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
505 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
506 struct sysfs_dirent *new_parent_sd, *sd;
509 old_parent_dentry = kobj->parent ?
510 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
511 new_parent_dentry = new_parent ?
512 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
514 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
515 return 0; /* nothing to move */
517 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
518 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
519 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
523 new_parent_sd = new_parent_dentry->d_fsdata;
524 sd = kobj->dentry->d_fsdata;
526 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
528 if (IS_ERR(new_dentry)) {
529 error = PTR_ERR(new_dentry);
533 d_add(new_dentry, NULL);
534 d_move(kobj->dentry, new_dentry);
537 /* Remove from old parent's list and insert into new parent's list. */
538 list_del_init(&sd->s_sibling);
539 list_add(&sd->s_sibling, &new_parent_sd->s_children);
542 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
543 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
548 static int sysfs_dir_open(struct inode *inode, struct file *file)
550 struct dentry * dentry = file->f_path.dentry;
551 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
552 struct sysfs_dirent * sd;
554 mutex_lock(&dentry->d_inode->i_mutex);
555 sd = sysfs_new_dirent("_DIR_", 0, 0);
557 sysfs_attach_dirent(sd, parent_sd, NULL);
558 mutex_unlock(&dentry->d_inode->i_mutex);
560 file->private_data = sd;
561 return sd ? 0 : -ENOMEM;
564 static int sysfs_dir_close(struct inode *inode, struct file *file)
566 struct dentry * dentry = file->f_path.dentry;
567 struct sysfs_dirent * cursor = file->private_data;
569 mutex_lock(&dentry->d_inode->i_mutex);
570 list_del_init(&cursor->s_sibling);
571 mutex_unlock(&dentry->d_inode->i_mutex);
573 release_sysfs_dirent(cursor);
578 /* Relationship between s_mode and the DT_xxx types */
579 static inline unsigned char dt_type(struct sysfs_dirent *sd)
581 return (sd->s_mode >> 12) & 15;
584 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
586 struct dentry *dentry = filp->f_path.dentry;
587 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
588 struct sysfs_dirent *cursor = filp->private_data;
589 struct list_head *p, *q = &cursor->s_sibling;
595 ino = parent_sd->s_ino;
596 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
602 if (parent_sd->s_parent)
603 ino = parent_sd->s_parent->s_ino;
605 ino = parent_sd->s_ino;
606 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
612 if (filp->f_pos == 2)
613 list_move(q, &parent_sd->s_children);
615 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
616 struct sysfs_dirent *next;
620 next = list_entry(p, struct sysfs_dirent,
629 if (filldir(dirent, name, len, filp->f_pos, ino,
641 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
643 struct dentry * dentry = file->f_path.dentry;
645 mutex_lock(&dentry->d_inode->i_mutex);
648 offset += file->f_pos;
653 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
656 if (offset != file->f_pos) {
657 file->f_pos = offset;
658 if (file->f_pos >= 2) {
659 struct sysfs_dirent *sd = dentry->d_fsdata;
660 struct sysfs_dirent *cursor = file->private_data;
662 loff_t n = file->f_pos - 2;
664 list_del(&cursor->s_sibling);
665 p = sd->s_children.next;
666 while (n && p != &sd->s_children) {
667 struct sysfs_dirent *next;
668 next = list_entry(p, struct sysfs_dirent,
674 list_add_tail(&cursor->s_sibling, p);
677 mutex_unlock(&dentry->d_inode->i_mutex);
683 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
684 * @kobj: object we're creating shadow of.
687 int sysfs_make_shadowed_dir(struct kobject *kobj,
688 void * (*follow_link)(struct dentry *, struct nameidata *))
691 struct inode_operations *i_op;
693 inode = kobj->dentry->d_inode;
694 if (inode->i_op != &sysfs_dir_inode_operations)
697 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
701 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
702 i_op->follow_link = follow_link;
704 /* Locking of inode->i_op?
705 * Since setting i_op is a single word write and they
706 * are atomic we should be ok here.
713 * sysfs_create_shadow_dir - create a shadow directory for an object.
714 * @kobj: object we're creating directory for.
716 * sysfs_make_shadowed_dir must already have been called on this
720 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
722 struct dentry *dir = kobj->dentry;
723 struct inode *inode = dir->d_inode;
724 struct dentry *parent = dir->d_parent;
725 struct sysfs_dirent *parent_sd = parent->d_fsdata;
726 struct dentry *shadow;
727 struct sysfs_dirent *sd;
729 shadow = ERR_PTR(-EINVAL);
730 if (!sysfs_is_shadowed_inode(inode))
733 shadow = d_alloc(parent, &dir->d_name);
737 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
740 sd->s_elem.dir.kobj = kobj;
741 /* point to parent_sd but don't attach to it */
742 sd->s_parent = sysfs_get(parent_sd);
743 sysfs_attach_dirent(sd, NULL, shadow);
745 d_instantiate(shadow, igrab(inode));
747 inc_nlink(parent->d_inode);
748 shadow->d_op = &sysfs_dentry_ops;
750 dget(shadow); /* Extra count - pin the dentry in core */
756 shadow = ERR_PTR(-ENOMEM);
761 * sysfs_remove_shadow_dir - remove an object's directory.
762 * @shadow: dentry of shadow directory
764 * The only thing special about this is that we remove any files in
765 * the directory before we remove the directory, and we've inlined
766 * what used to be sysfs_rmdir() below, instead of calling separately.
769 void sysfs_remove_shadow_dir(struct dentry *shadow)
771 __sysfs_remove_dir(shadow);
774 const struct file_operations sysfs_dir_operations = {
775 .open = sysfs_dir_open,
776 .release = sysfs_dir_close,
777 .llseek = sysfs_dir_lseek,
778 .read = generic_read_dir,
779 .readdir = sysfs_readdir,