sysfs: reimplement symlink using sysfs_dirent tree
[linux-2.6/mini2440.git] / fs / sysfs / dir.c
blob2a94dc36d166d3b64b049f3e3ba49e7f7571ddf4
1 /*
2 * dir.c - Operations for sysfs directories.
3 */
5 #undef DEBUG
7 #include <linux/fs.h>
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>
14 #include "sysfs.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 int sysfs_alloc_ino(ino_t *pino)
25 int ino, rc;
27 retry:
28 spin_lock(&sysfs_ino_lock);
29 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
30 spin_unlock(&sysfs_ino_lock);
32 if (rc == -EAGAIN) {
33 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
34 goto retry;
35 rc = -ENOMEM;
38 *pino = ino;
39 return rc;
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;
53 repeat:
54 parent_sd = sd->s_parent;
56 if (sd->s_type & SYSFS_KOBJ_LINK)
57 sysfs_put(sd->s_elem.symlink.target_sd);
58 if (sd->s_type & SYSFS_COPY_NAME)
59 kfree(sd->s_name);
60 kfree(sd->s_iattr);
61 sysfs_free_ino(sd->s_ino);
62 kmem_cache_free(sysfs_dir_cachep, sd);
64 sd = parent_sd;
65 if (sd && atomic_dec_and_test(&sd->s_count))
66 goto repeat;
69 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
71 struct sysfs_dirent * sd = dentry->d_fsdata;
73 if (sd) {
74 /* sd->s_dentry is protected with sysfs_lock. This
75 * allows sysfs_drop_dentry() to dereference it.
77 spin_lock(&sysfs_lock);
79 /* The dentry might have been deleted or another
80 * lookup could have happened updating sd->s_dentry to
81 * point the new dentry. Ignore if it isn't pointing
82 * to this dentry.
84 if (sd->s_dentry == dentry)
85 sd->s_dentry = NULL;
86 spin_unlock(&sysfs_lock);
87 sysfs_put(sd);
89 iput(inode);
92 static struct dentry_operations sysfs_dentry_ops = {
93 .d_iput = sysfs_d_iput,
96 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
98 char *dup_name = NULL;
99 struct sysfs_dirent *sd = NULL;
101 if (type & SYSFS_COPY_NAME) {
102 name = dup_name = kstrdup(name, GFP_KERNEL);
103 if (!name)
104 goto err_out;
107 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
108 if (!sd)
109 goto err_out;
111 if (sysfs_alloc_ino(&sd->s_ino))
112 goto err_out;
114 atomic_set(&sd->s_count, 1);
115 atomic_set(&sd->s_event, 1);
116 INIT_LIST_HEAD(&sd->s_children);
117 INIT_LIST_HEAD(&sd->s_sibling);
119 sd->s_name = name;
120 sd->s_mode = mode;
121 sd->s_type = type;
123 return sd;
125 err_out:
126 kfree(dup_name);
127 kmem_cache_free(sysfs_dir_cachep, sd);
128 return NULL;
131 void sysfs_attach_dirent(struct sysfs_dirent *sd,
132 struct sysfs_dirent *parent_sd, struct dentry *dentry)
134 if (dentry) {
135 sd->s_dentry = dentry;
136 dentry->d_fsdata = sysfs_get(sd);
137 dentry->d_op = &sysfs_dentry_ops;
140 if (parent_sd) {
141 sd->s_parent = sysfs_get(parent_sd);
142 list_add(&sd->s_sibling, &parent_sd->s_children);
148 * Return -EEXIST if there is already a sysfs element with the same name for
149 * the same parent.
151 * called with parent inode's i_mutex held
153 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
154 const unsigned char *new)
156 struct sysfs_dirent * sd;
158 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
159 if (sd->s_type) {
160 if (strcmp(sd->s_name, new))
161 continue;
162 else
163 return -EEXIST;
167 return 0;
170 static int init_dir(struct inode * inode)
172 inode->i_op = &sysfs_dir_inode_operations;
173 inode->i_fop = &sysfs_dir_operations;
175 /* directory inodes start off with i_nlink == 2 (for "." entry) */
176 inc_nlink(inode);
177 return 0;
180 static int init_file(struct inode * inode)
182 inode->i_size = PAGE_SIZE;
183 inode->i_fop = &sysfs_file_operations;
184 return 0;
187 static int init_symlink(struct inode * inode)
189 inode->i_op = &sysfs_symlink_inode_operations;
190 return 0;
193 static int create_dir(struct kobject *kobj, struct dentry *parent,
194 const char *name, struct dentry **p_dentry)
196 int error;
197 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
198 struct dentry *dentry;
199 struct sysfs_dirent *sd;
201 mutex_lock(&parent->d_inode->i_mutex);
203 dentry = lookup_one_len(name, parent, strlen(name));
204 if (IS_ERR(dentry)) {
205 error = PTR_ERR(dentry);
206 goto out_unlock;
209 error = -EEXIST;
210 if (sysfs_dirent_exist(parent->d_fsdata, name))
211 goto out_dput;
213 error = -ENOMEM;
214 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
215 if (!sd)
216 goto out_drop;
217 sd->s_elem.dir.kobj = kobj;
218 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
220 error = sysfs_create(dentry, mode, init_dir);
221 if (error)
222 goto out_sput;
224 inc_nlink(parent->d_inode);
225 dentry->d_op = &sysfs_dentry_ops;
226 d_rehash(dentry);
228 *p_dentry = dentry;
229 error = 0;
230 goto out_dput;
232 out_sput:
233 list_del_init(&sd->s_sibling);
234 sysfs_put(sd);
235 out_drop:
236 d_drop(dentry);
237 out_dput:
238 dput(dentry);
239 out_unlock:
240 mutex_unlock(&parent->d_inode->i_mutex);
241 return error;
245 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
247 return create_dir(k,k->dentry,n,d);
251 * sysfs_create_dir - create a directory for an object.
252 * @kobj: object we're creating directory for.
253 * @shadow_parent: parent parent object.
256 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
258 struct dentry * dentry = NULL;
259 struct dentry * parent;
260 int error = 0;
262 BUG_ON(!kobj);
264 if (shadow_parent)
265 parent = shadow_parent;
266 else if (kobj->parent)
267 parent = kobj->parent->dentry;
268 else if (sysfs_mount && sysfs_mount->mnt_sb)
269 parent = sysfs_mount->mnt_sb->s_root;
270 else
271 return -EFAULT;
273 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
274 if (!error)
275 kobj->dentry = dentry;
276 return error;
279 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
280 * attribute file
282 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
284 struct attribute * attr = NULL;
285 struct bin_attribute * bin_attr = NULL;
286 int (* init) (struct inode *) = NULL;
287 int error = 0;
289 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
290 bin_attr = sd->s_elem.bin_attr.bin_attr;
291 attr = &bin_attr->attr;
292 } else {
293 attr = sd->s_elem.attr.attr;
294 init = init_file;
297 dentry->d_fsdata = sysfs_get(sd);
298 /* protect sd->s_dentry against sysfs_d_iput */
299 spin_lock(&sysfs_lock);
300 sd->s_dentry = dentry;
301 spin_unlock(&sysfs_lock);
302 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
303 if (error) {
304 sysfs_put(sd);
305 return error;
308 if (bin_attr) {
309 dentry->d_inode->i_size = bin_attr->size;
310 dentry->d_inode->i_fop = &bin_fops;
312 dentry->d_op = &sysfs_dentry_ops;
313 d_rehash(dentry);
315 return 0;
318 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
320 int err = 0;
322 dentry->d_fsdata = sysfs_get(sd);
323 /* protect sd->s_dentry against sysfs_d_iput */
324 spin_lock(&sysfs_lock);
325 sd->s_dentry = dentry;
326 spin_unlock(&sysfs_lock);
327 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
328 if (!err) {
329 dentry->d_op = &sysfs_dentry_ops;
330 d_rehash(dentry);
331 } else
332 sysfs_put(sd);
334 return err;
337 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
338 struct nameidata *nd)
340 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
341 struct sysfs_dirent * sd;
342 int err = 0;
344 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
345 if (sd->s_type & SYSFS_NOT_PINNED) {
346 if (strcmp(sd->s_name, dentry->d_name.name))
347 continue;
349 if (sd->s_type & SYSFS_KOBJ_LINK)
350 err = sysfs_attach_link(sd, dentry);
351 else
352 err = sysfs_attach_attr(sd, dentry);
353 break;
357 return ERR_PTR(err);
360 const struct inode_operations sysfs_dir_inode_operations = {
361 .lookup = sysfs_lookup,
362 .setattr = sysfs_setattr,
365 static void remove_dir(struct dentry * d)
367 struct dentry * parent = dget(d->d_parent);
368 struct sysfs_dirent * sd;
370 mutex_lock(&parent->d_inode->i_mutex);
371 d_delete(d);
372 sd = d->d_fsdata;
373 list_del_init(&sd->s_sibling);
374 sysfs_put(sd);
375 if (d->d_inode)
376 simple_rmdir(parent->d_inode,d);
378 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
379 atomic_read(&d->d_count));
381 mutex_unlock(&parent->d_inode->i_mutex);
382 dput(parent);
385 void sysfs_remove_subdir(struct dentry * d)
387 remove_dir(d);
391 static void __sysfs_remove_dir(struct dentry *dentry)
393 struct sysfs_dirent * parent_sd;
394 struct sysfs_dirent * sd, * tmp;
396 dget(dentry);
397 if (!dentry)
398 return;
400 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
401 mutex_lock(&dentry->d_inode->i_mutex);
402 parent_sd = dentry->d_fsdata;
403 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
404 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
405 continue;
406 list_del_init(&sd->s_sibling);
407 sysfs_drop_dentry(sd, dentry);
408 sysfs_put(sd);
410 mutex_unlock(&dentry->d_inode->i_mutex);
412 remove_dir(dentry);
414 * Drop reference from dget() on entrance.
416 dput(dentry);
420 * sysfs_remove_dir - remove an object's directory.
421 * @kobj: object.
423 * The only thing special about this is that we remove any files in
424 * the directory before we remove the directory, and we've inlined
425 * what used to be sysfs_rmdir() below, instead of calling separately.
428 void sysfs_remove_dir(struct kobject * kobj)
430 struct dentry *d = kobj->dentry;
432 spin_lock(&kobj_sysfs_assoc_lock);
433 kobj->dentry = NULL;
434 spin_unlock(&kobj_sysfs_assoc_lock);
436 __sysfs_remove_dir(d);
439 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
440 const char *new_name)
442 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
443 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
444 struct dentry *new_dentry;
445 char *dup_name;
446 int error;
448 if (!new_parent)
449 return -EFAULT;
451 down_write(&sysfs_rename_sem);
452 mutex_lock(&new_parent->d_inode->i_mutex);
454 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
455 if (IS_ERR(new_dentry)) {
456 error = PTR_ERR(new_dentry);
457 goto out_unlock;
460 /* By allowing two different directories with the same
461 * d_parent we allow this routine to move between different
462 * shadows of the same directory
464 error = -EINVAL;
465 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
466 new_dentry->d_parent->d_inode != new_parent->d_inode ||
467 new_dentry == kobj->dentry)
468 goto out_dput;
470 error = -EEXIST;
471 if (new_dentry->d_inode)
472 goto out_dput;
474 /* rename kobject and sysfs_dirent */
475 error = -ENOMEM;
476 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
477 if (!new_name)
478 goto out_drop;
480 error = kobject_set_name(kobj, "%s", new_name);
481 if (error)
482 goto out_free;
484 kfree(sd->s_name);
485 sd->s_name = new_name;
487 /* move under the new parent */
488 d_add(new_dentry, NULL);
489 d_move(kobj->dentry, new_dentry);
491 list_del_init(&sd->s_sibling);
492 list_add(&sd->s_sibling, &parent_sd->s_children);
494 error = 0;
495 goto out_unlock;
497 out_free:
498 kfree(dup_name);
499 out_drop:
500 d_drop(new_dentry);
501 out_dput:
502 dput(new_dentry);
503 out_unlock:
504 mutex_unlock(&new_parent->d_inode->i_mutex);
505 up_write(&sysfs_rename_sem);
506 return error;
509 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
511 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
512 struct sysfs_dirent *new_parent_sd, *sd;
513 int error;
515 old_parent_dentry = kobj->parent ?
516 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
517 new_parent_dentry = new_parent ?
518 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
520 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
521 return 0; /* nothing to move */
522 again:
523 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
524 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
525 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
526 goto again;
529 new_parent_sd = new_parent_dentry->d_fsdata;
530 sd = kobj->dentry->d_fsdata;
532 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
533 strlen(kobj->name));
534 if (IS_ERR(new_dentry)) {
535 error = PTR_ERR(new_dentry);
536 goto out;
537 } else
538 error = 0;
539 d_add(new_dentry, NULL);
540 d_move(kobj->dentry, new_dentry);
541 dput(new_dentry);
543 /* Remove from old parent's list and insert into new parent's list. */
544 list_del_init(&sd->s_sibling);
545 list_add(&sd->s_sibling, &new_parent_sd->s_children);
547 out:
548 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
549 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
551 return error;
554 static int sysfs_dir_open(struct inode *inode, struct file *file)
556 struct dentry * dentry = file->f_path.dentry;
557 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
558 struct sysfs_dirent * sd;
560 mutex_lock(&dentry->d_inode->i_mutex);
561 sd = sysfs_new_dirent("_DIR_", 0, 0);
562 if (sd)
563 sysfs_attach_dirent(sd, parent_sd, NULL);
564 mutex_unlock(&dentry->d_inode->i_mutex);
566 file->private_data = sd;
567 return sd ? 0 : -ENOMEM;
570 static int sysfs_dir_close(struct inode *inode, struct file *file)
572 struct dentry * dentry = file->f_path.dentry;
573 struct sysfs_dirent * cursor = file->private_data;
575 mutex_lock(&dentry->d_inode->i_mutex);
576 list_del_init(&cursor->s_sibling);
577 mutex_unlock(&dentry->d_inode->i_mutex);
579 release_sysfs_dirent(cursor);
581 return 0;
584 /* Relationship between s_mode and the DT_xxx types */
585 static inline unsigned char dt_type(struct sysfs_dirent *sd)
587 return (sd->s_mode >> 12) & 15;
590 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
592 struct dentry *dentry = filp->f_path.dentry;
593 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
594 struct sysfs_dirent *cursor = filp->private_data;
595 struct list_head *p, *q = &cursor->s_sibling;
596 ino_t ino;
597 int i = filp->f_pos;
599 switch (i) {
600 case 0:
601 ino = parent_sd->s_ino;
602 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
603 break;
604 filp->f_pos++;
605 i++;
606 /* fallthrough */
607 case 1:
608 if (parent_sd->s_parent)
609 ino = parent_sd->s_parent->s_ino;
610 else
611 ino = parent_sd->s_ino;
612 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
613 break;
614 filp->f_pos++;
615 i++;
616 /* fallthrough */
617 default:
618 if (filp->f_pos == 2)
619 list_move(q, &parent_sd->s_children);
621 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
622 struct sysfs_dirent *next;
623 const char * name;
624 int len;
626 next = list_entry(p, struct sysfs_dirent,
627 s_sibling);
628 if (!next->s_type)
629 continue;
631 name = next->s_name;
632 len = strlen(name);
633 ino = next->s_ino;
635 if (filldir(dirent, name, len, filp->f_pos, ino,
636 dt_type(next)) < 0)
637 return 0;
639 list_move(q, p);
640 p = q;
641 filp->f_pos++;
644 return 0;
647 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
649 struct dentry * dentry = file->f_path.dentry;
651 mutex_lock(&dentry->d_inode->i_mutex);
652 switch (origin) {
653 case 1:
654 offset += file->f_pos;
655 case 0:
656 if (offset >= 0)
657 break;
658 default:
659 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
660 return -EINVAL;
662 if (offset != file->f_pos) {
663 file->f_pos = offset;
664 if (file->f_pos >= 2) {
665 struct sysfs_dirent *sd = dentry->d_fsdata;
666 struct sysfs_dirent *cursor = file->private_data;
667 struct list_head *p;
668 loff_t n = file->f_pos - 2;
670 list_del(&cursor->s_sibling);
671 p = sd->s_children.next;
672 while (n && p != &sd->s_children) {
673 struct sysfs_dirent *next;
674 next = list_entry(p, struct sysfs_dirent,
675 s_sibling);
676 if (next->s_type)
677 n--;
678 p = p->next;
680 list_add_tail(&cursor->s_sibling, p);
683 mutex_unlock(&dentry->d_inode->i_mutex);
684 return offset;
689 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
690 * @kobj: object we're creating shadow of.
693 int sysfs_make_shadowed_dir(struct kobject *kobj,
694 void * (*follow_link)(struct dentry *, struct nameidata *))
696 struct inode *inode;
697 struct inode_operations *i_op;
699 inode = kobj->dentry->d_inode;
700 if (inode->i_op != &sysfs_dir_inode_operations)
701 return -EINVAL;
703 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
704 if (!i_op)
705 return -ENOMEM;
707 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
708 i_op->follow_link = follow_link;
710 /* Locking of inode->i_op?
711 * Since setting i_op is a single word write and they
712 * are atomic we should be ok here.
714 inode->i_op = i_op;
715 return 0;
719 * sysfs_create_shadow_dir - create a shadow directory for an object.
720 * @kobj: object we're creating directory for.
722 * sysfs_make_shadowed_dir must already have been called on this
723 * directory.
726 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
728 struct dentry *dir = kobj->dentry;
729 struct inode *inode = dir->d_inode;
730 struct dentry *parent = dir->d_parent;
731 struct sysfs_dirent *parent_sd = parent->d_fsdata;
732 struct dentry *shadow;
733 struct sysfs_dirent *sd;
735 shadow = ERR_PTR(-EINVAL);
736 if (!sysfs_is_shadowed_inode(inode))
737 goto out;
739 shadow = d_alloc(parent, &dir->d_name);
740 if (!shadow)
741 goto nomem;
743 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
744 if (!sd)
745 goto nomem;
746 sd->s_elem.dir.kobj = kobj;
747 /* point to parent_sd but don't attach to it */
748 sd->s_parent = sysfs_get(parent_sd);
749 sysfs_attach_dirent(sd, NULL, shadow);
751 d_instantiate(shadow, igrab(inode));
752 inc_nlink(inode);
753 inc_nlink(parent->d_inode);
754 shadow->d_op = &sysfs_dentry_ops;
756 dget(shadow); /* Extra count - pin the dentry in core */
758 out:
759 return shadow;
760 nomem:
761 dput(shadow);
762 shadow = ERR_PTR(-ENOMEM);
763 goto out;
767 * sysfs_remove_shadow_dir - remove an object's directory.
768 * @shadow: dentry of shadow directory
770 * The only thing special about this is that we remove any files in
771 * the directory before we remove the directory, and we've inlined
772 * what used to be sysfs_rmdir() below, instead of calling separately.
775 void sysfs_remove_shadow_dir(struct dentry *shadow)
777 __sysfs_remove_dir(shadow);
780 const struct file_operations sysfs_dir_operations = {
781 .open = sysfs_dir_open,
782 .release = sysfs_dir_close,
783 .llseek = sysfs_dir_lseek,
784 .read = generic_read_dir,
785 .readdir = sysfs_readdir,