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 struct sysfs_symlink * sl = sd->s_element;
58 kobject_put(sl->target_kobj);
62 sysfs_free_ino(sd->s_ino);
63 kmem_cache_free(sysfs_dir_cachep, sd);
66 if (sd && atomic_dec_and_test(&sd->s_count))
70 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
72 struct sysfs_dirent * sd = dentry->d_fsdata;
75 /* sd->s_dentry is protected with sysfs_lock. This
76 * allows sysfs_drop_dentry() to dereference it.
78 spin_lock(&sysfs_lock);
80 /* The dentry might have been deleted or another
81 * lookup could have happened updating sd->s_dentry to
82 * point the new dentry. Ignore if it isn't pointing
85 if (sd->s_dentry == dentry)
87 spin_unlock(&sysfs_lock);
93 static struct dentry_operations sysfs_dentry_ops = {
94 .d_iput = sysfs_d_iput,
97 struct sysfs_dirent *sysfs_new_dirent(void *element, umode_t mode, int type)
99 struct sysfs_dirent * sd;
101 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
105 if (sysfs_alloc_ino(&sd->s_ino)) {
106 kmem_cache_free(sysfs_dir_cachep, sd);
110 atomic_set(&sd->s_count, 1);
111 atomic_set(&sd->s_event, 1);
112 INIT_LIST_HEAD(&sd->s_children);
113 INIT_LIST_HEAD(&sd->s_sibling);
115 sd->s_element = element;
122 void sysfs_attach_dirent(struct sysfs_dirent *sd,
123 struct sysfs_dirent *parent_sd, struct dentry *dentry)
126 sd->s_dentry = dentry;
127 dentry->d_fsdata = sysfs_get(sd);
128 dentry->d_op = &sysfs_dentry_ops;
132 sd->s_parent = sysfs_get(parent_sd);
133 list_add(&sd->s_sibling, &parent_sd->s_children);
139 * Return -EEXIST if there is already a sysfs element with the same name for
142 * called with parent inode's i_mutex held
144 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
145 const unsigned char *new)
147 struct sysfs_dirent * sd;
149 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
151 const unsigned char *existing = sysfs_get_name(sd);
152 if (strcmp(existing, new))
162 static int init_dir(struct inode * inode)
164 inode->i_op = &sysfs_dir_inode_operations;
165 inode->i_fop = &sysfs_dir_operations;
167 /* directory inodes start off with i_nlink == 2 (for "." entry) */
172 static int init_file(struct inode * inode)
174 inode->i_size = PAGE_SIZE;
175 inode->i_fop = &sysfs_file_operations;
179 static int init_symlink(struct inode * inode)
181 inode->i_op = &sysfs_symlink_inode_operations;
185 static int create_dir(struct kobject *kobj, struct dentry *parent,
186 const char *name, struct dentry **p_dentry)
189 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
190 struct dentry *dentry;
191 struct sysfs_dirent *sd;
193 mutex_lock(&parent->d_inode->i_mutex);
195 dentry = lookup_one_len(name, parent, strlen(name));
196 if (IS_ERR(dentry)) {
197 error = PTR_ERR(dentry);
202 if (sysfs_dirent_exist(parent->d_fsdata, name))
206 sd = sysfs_new_dirent(kobj, mode, SYSFS_DIR);
209 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
211 error = sysfs_create(dentry, mode, init_dir);
215 inc_nlink(parent->d_inode);
216 dentry->d_op = &sysfs_dentry_ops;
224 list_del_init(&sd->s_sibling);
231 mutex_unlock(&parent->d_inode->i_mutex);
236 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
238 return create_dir(k,k->dentry,n,d);
242 * sysfs_create_dir - create a directory for an object.
243 * @kobj: object we're creating directory for.
244 * @shadow_parent: parent parent object.
247 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
249 struct dentry * dentry = NULL;
250 struct dentry * parent;
256 parent = shadow_parent;
257 else if (kobj->parent)
258 parent = kobj->parent->dentry;
259 else if (sysfs_mount && sysfs_mount->mnt_sb)
260 parent = sysfs_mount->mnt_sb->s_root;
264 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
266 kobj->dentry = dentry;
270 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
273 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
275 struct attribute * attr = NULL;
276 struct bin_attribute * bin_attr = NULL;
277 int (* init) (struct inode *) = NULL;
280 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
281 bin_attr = sd->s_element;
282 attr = &bin_attr->attr;
284 attr = sd->s_element;
288 dentry->d_fsdata = sysfs_get(sd);
289 /* protect sd->s_dentry against sysfs_d_iput */
290 spin_lock(&sysfs_lock);
291 sd->s_dentry = dentry;
292 spin_unlock(&sysfs_lock);
293 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
300 dentry->d_inode->i_size = bin_attr->size;
301 dentry->d_inode->i_fop = &bin_fops;
303 dentry->d_op = &sysfs_dentry_ops;
309 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
313 dentry->d_fsdata = sysfs_get(sd);
314 /* protect sd->s_dentry against sysfs_d_iput */
315 spin_lock(&sysfs_lock);
316 sd->s_dentry = dentry;
317 spin_unlock(&sysfs_lock);
318 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
320 dentry->d_op = &sysfs_dentry_ops;
328 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
329 struct nameidata *nd)
331 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
332 struct sysfs_dirent * sd;
335 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
336 if (sd->s_type & SYSFS_NOT_PINNED) {
337 const unsigned char * name = sysfs_get_name(sd);
339 if (strcmp(name, dentry->d_name.name))
342 if (sd->s_type & SYSFS_KOBJ_LINK)
343 err = sysfs_attach_link(sd, dentry);
345 err = sysfs_attach_attr(sd, dentry);
353 const struct inode_operations sysfs_dir_inode_operations = {
354 .lookup = sysfs_lookup,
355 .setattr = sysfs_setattr,
358 static void remove_dir(struct dentry * d)
360 struct dentry * parent = dget(d->d_parent);
361 struct sysfs_dirent * sd;
363 mutex_lock(&parent->d_inode->i_mutex);
366 list_del_init(&sd->s_sibling);
369 simple_rmdir(parent->d_inode,d);
371 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
372 atomic_read(&d->d_count));
374 mutex_unlock(&parent->d_inode->i_mutex);
378 void sysfs_remove_subdir(struct dentry * d)
384 static void __sysfs_remove_dir(struct dentry *dentry)
386 struct sysfs_dirent * parent_sd;
387 struct sysfs_dirent * sd, * tmp;
393 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
394 mutex_lock(&dentry->d_inode->i_mutex);
395 parent_sd = dentry->d_fsdata;
396 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
397 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
399 list_del_init(&sd->s_sibling);
400 sysfs_drop_dentry(sd, dentry);
403 mutex_unlock(&dentry->d_inode->i_mutex);
407 * Drop reference from dget() on entrance.
413 * sysfs_remove_dir - remove an object's directory.
416 * The only thing special about this is that we remove any files in
417 * the directory before we remove the directory, and we've inlined
418 * what used to be sysfs_rmdir() below, instead of calling separately.
421 void sysfs_remove_dir(struct kobject * kobj)
423 __sysfs_remove_dir(kobj->dentry);
427 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
428 const char *new_name)
431 struct dentry * new_dentry;
432 struct sysfs_dirent *sd, *parent_sd;
437 down_write(&sysfs_rename_sem);
438 mutex_lock(&new_parent->d_inode->i_mutex);
440 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
441 if (IS_ERR(new_dentry)) {
442 error = PTR_ERR(new_dentry);
446 /* By allowing two different directories with the same
447 * d_parent we allow this routine to move between different
448 * shadows of the same directory
451 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
452 new_dentry->d_parent->d_inode != new_parent->d_inode ||
453 new_dentry == kobj->dentry)
457 if (new_dentry->d_inode)
460 error = kobject_set_name(kobj, "%s", new_name);
464 d_add(new_dentry, NULL);
465 d_move(kobj->dentry, new_dentry);
467 sd = kobj->dentry->d_fsdata;
468 parent_sd = new_parent->d_fsdata;
470 list_del_init(&sd->s_sibling);
471 list_add(&sd->s_sibling, &parent_sd->s_children);
481 mutex_unlock(&new_parent->d_inode->i_mutex);
482 up_write(&sysfs_rename_sem);
486 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
488 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
489 struct sysfs_dirent *new_parent_sd, *sd;
492 old_parent_dentry = kobj->parent ?
493 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
494 new_parent_dentry = new_parent ?
495 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
497 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
498 return 0; /* nothing to move */
500 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
501 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
502 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
506 new_parent_sd = new_parent_dentry->d_fsdata;
507 sd = kobj->dentry->d_fsdata;
509 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
511 if (IS_ERR(new_dentry)) {
512 error = PTR_ERR(new_dentry);
516 d_add(new_dentry, NULL);
517 d_move(kobj->dentry, new_dentry);
520 /* Remove from old parent's list and insert into new parent's list. */
521 list_del_init(&sd->s_sibling);
522 list_add(&sd->s_sibling, &new_parent_sd->s_children);
525 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
526 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
531 static int sysfs_dir_open(struct inode *inode, struct file *file)
533 struct dentry * dentry = file->f_path.dentry;
534 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
535 struct sysfs_dirent * sd;
537 mutex_lock(&dentry->d_inode->i_mutex);
538 sd = sysfs_new_dirent(NULL, 0, 0);
540 sysfs_attach_dirent(sd, parent_sd, NULL);
541 mutex_unlock(&dentry->d_inode->i_mutex);
543 file->private_data = sd;
544 return sd ? 0 : -ENOMEM;
547 static int sysfs_dir_close(struct inode *inode, struct file *file)
549 struct dentry * dentry = file->f_path.dentry;
550 struct sysfs_dirent * cursor = file->private_data;
552 mutex_lock(&dentry->d_inode->i_mutex);
553 list_del_init(&cursor->s_sibling);
554 mutex_unlock(&dentry->d_inode->i_mutex);
556 release_sysfs_dirent(cursor);
561 /* Relationship between s_mode and the DT_xxx types */
562 static inline unsigned char dt_type(struct sysfs_dirent *sd)
564 return (sd->s_mode >> 12) & 15;
567 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
569 struct dentry *dentry = filp->f_path.dentry;
570 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
571 struct sysfs_dirent *cursor = filp->private_data;
572 struct list_head *p, *q = &cursor->s_sibling;
578 ino = parent_sd->s_ino;
579 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
585 if (parent_sd->s_parent)
586 ino = parent_sd->s_parent->s_ino;
588 ino = parent_sd->s_ino;
589 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
595 if (filp->f_pos == 2)
596 list_move(q, &parent_sd->s_children);
598 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
599 struct sysfs_dirent *next;
603 next = list_entry(p, struct sysfs_dirent,
605 if (!next->s_element)
608 name = sysfs_get_name(next);
612 if (filldir(dirent, name, len, filp->f_pos, ino,
624 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
626 struct dentry * dentry = file->f_path.dentry;
628 mutex_lock(&dentry->d_inode->i_mutex);
631 offset += file->f_pos;
636 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
639 if (offset != file->f_pos) {
640 file->f_pos = offset;
641 if (file->f_pos >= 2) {
642 struct sysfs_dirent *sd = dentry->d_fsdata;
643 struct sysfs_dirent *cursor = file->private_data;
645 loff_t n = file->f_pos - 2;
647 list_del(&cursor->s_sibling);
648 p = sd->s_children.next;
649 while (n && p != &sd->s_children) {
650 struct sysfs_dirent *next;
651 next = list_entry(p, struct sysfs_dirent,
657 list_add_tail(&cursor->s_sibling, p);
660 mutex_unlock(&dentry->d_inode->i_mutex);
666 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
667 * @kobj: object we're creating shadow of.
670 int sysfs_make_shadowed_dir(struct kobject *kobj,
671 void * (*follow_link)(struct dentry *, struct nameidata *))
674 struct inode_operations *i_op;
676 inode = kobj->dentry->d_inode;
677 if (inode->i_op != &sysfs_dir_inode_operations)
680 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
684 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
685 i_op->follow_link = follow_link;
687 /* Locking of inode->i_op?
688 * Since setting i_op is a single word write and they
689 * are atomic we should be ok here.
696 * sysfs_create_shadow_dir - create a shadow directory for an object.
697 * @kobj: object we're creating directory for.
699 * sysfs_make_shadowed_dir must already have been called on this
703 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
705 struct dentry *dir = kobj->dentry;
706 struct inode *inode = dir->d_inode;
707 struct dentry *parent = dir->d_parent;
708 struct sysfs_dirent *parent_sd = parent->d_fsdata;
709 struct dentry *shadow;
710 struct sysfs_dirent *sd;
712 shadow = ERR_PTR(-EINVAL);
713 if (!sysfs_is_shadowed_inode(inode))
716 shadow = d_alloc(parent, &dir->d_name);
720 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR);
723 /* point to parent_sd but don't attach to it */
724 sd->s_parent = sysfs_get(parent_sd);
725 sysfs_attach_dirent(sd, NULL, shadow);
727 d_instantiate(shadow, igrab(inode));
729 inc_nlink(parent->d_inode);
730 shadow->d_op = &sysfs_dentry_ops;
732 dget(shadow); /* Extra count - pin the dentry in core */
738 shadow = ERR_PTR(-ENOMEM);
743 * sysfs_remove_shadow_dir - remove an object's directory.
744 * @shadow: dentry of shadow directory
746 * The only thing special about this is that we remove any files in
747 * the directory before we remove the directory, and we've inlined
748 * what used to be sysfs_rmdir() below, instead of calling separately.
751 void sysfs_remove_shadow_dir(struct dentry *shadow)
753 __sysfs_remove_dir(shadow);
756 const struct file_operations sysfs_dir_operations = {
757 .open = sysfs_dir_open,
758 .release = sysfs_dir_close,
759 .llseek = sysfs_dir_lseek,
760 .read = generic_read_dir,
761 .readdir = sysfs_readdir,