sysfs: consolidate sysfs_dirent creation functions
[linux-2.6/openmoko-kernel.git] / fs / sysfs / dir.c
blobf16aa7e3eafc6a62ef0758720d1a2a37cc02a2fd
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;
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)
24 int ino, rc;
26 retry:
27 spin_lock(&sysfs_ino_lock);
28 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
29 spin_unlock(&sysfs_ino_lock);
31 if (rc == -EAGAIN) {
32 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
33 goto retry;
34 rc = -ENOMEM;
37 *pino = ino;
38 return rc;
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 if (sd->s_type & SYSFS_KOBJ_LINK) {
51 struct sysfs_symlink * sl = sd->s_element;
52 kfree(sl->link_name);
53 kobject_put(sl->target_kobj);
54 kfree(sl);
56 kfree(sd->s_iattr);
57 sysfs_free_ino(sd->s_ino);
58 kmem_cache_free(sysfs_dir_cachep, sd);
61 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
63 struct sysfs_dirent * sd = dentry->d_fsdata;
65 if (sd) {
66 /* sd->s_dentry is protected with sysfs_lock. This
67 * allows sysfs_drop_dentry() to dereference it.
69 spin_lock(&sysfs_lock);
71 /* The dentry might have been deleted or another
72 * lookup could have happened updating sd->s_dentry to
73 * point the new dentry. Ignore if it isn't pointing
74 * to this dentry.
76 if (sd->s_dentry == dentry)
77 sd->s_dentry = NULL;
78 spin_unlock(&sysfs_lock);
79 sysfs_put(sd);
81 iput(inode);
84 static struct dentry_operations sysfs_dentry_ops = {
85 .d_iput = sysfs_d_iput,
88 struct sysfs_dirent *sysfs_new_dirent(void *element, umode_t mode, int type)
90 struct sysfs_dirent * sd;
92 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
93 if (!sd)
94 return NULL;
96 if (sysfs_alloc_ino(&sd->s_ino)) {
97 kmem_cache_free(sysfs_dir_cachep, sd);
98 return NULL;
101 atomic_set(&sd->s_count, 1);
102 atomic_set(&sd->s_event, 1);
103 INIT_LIST_HEAD(&sd->s_children);
104 INIT_LIST_HEAD(&sd->s_sibling);
106 sd->s_element = element;
107 sd->s_mode = mode;
108 sd->s_type = type;
110 return sd;
113 void sysfs_attach_dirent(struct sysfs_dirent *sd,
114 struct sysfs_dirent *parent_sd, struct dentry *dentry)
116 if (dentry) {
117 sd->s_dentry = dentry;
118 dentry->d_fsdata = sysfs_get(sd);
119 dentry->d_op = &sysfs_dentry_ops;
122 if (parent_sd)
123 list_add(&sd->s_sibling, &parent_sd->s_children);
128 * Return -EEXIST if there is already a sysfs element with the same name for
129 * the same parent.
131 * called with parent inode's i_mutex held
133 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
134 const unsigned char *new)
136 struct sysfs_dirent * sd;
138 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
139 if (sd->s_element) {
140 const unsigned char *existing = sysfs_get_name(sd);
141 if (strcmp(existing, new))
142 continue;
143 else
144 return -EEXIST;
148 return 0;
151 static int init_dir(struct inode * inode)
153 inode->i_op = &sysfs_dir_inode_operations;
154 inode->i_fop = &sysfs_dir_operations;
156 /* directory inodes start off with i_nlink == 2 (for "." entry) */
157 inc_nlink(inode);
158 return 0;
161 static int init_file(struct inode * inode)
163 inode->i_size = PAGE_SIZE;
164 inode->i_fop = &sysfs_file_operations;
165 return 0;
168 static int init_symlink(struct inode * inode)
170 inode->i_op = &sysfs_symlink_inode_operations;
171 return 0;
174 static int create_dir(struct kobject *kobj, struct dentry *parent,
175 const char *name, struct dentry **p_dentry)
177 int error;
178 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
179 struct dentry *dentry;
180 struct sysfs_dirent *sd;
182 mutex_lock(&parent->d_inode->i_mutex);
184 dentry = lookup_one_len(name, parent, strlen(name));
185 if (IS_ERR(dentry)) {
186 error = PTR_ERR(dentry);
187 goto out_unlock;
190 error = -EEXIST;
191 if (sysfs_dirent_exist(parent->d_fsdata, name))
192 goto out_dput;
194 error = -ENOMEM;
195 sd = sysfs_new_dirent(kobj, mode, SYSFS_DIR);
196 if (!sd)
197 goto out_drop;
198 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
200 error = sysfs_create(dentry, mode, init_dir);
201 if (error)
202 goto out_sput;
204 inc_nlink(parent->d_inode);
205 dentry->d_op = &sysfs_dentry_ops;
206 d_rehash(dentry);
208 *p_dentry = dentry;
209 error = 0;
210 goto out_dput;
212 out_sput:
213 list_del_init(&sd->s_sibling);
214 sysfs_put(sd);
215 out_drop:
216 d_drop(dentry);
217 out_dput:
218 dput(dentry);
219 out_unlock:
220 mutex_unlock(&parent->d_inode->i_mutex);
221 return error;
225 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
227 return create_dir(k,k->dentry,n,d);
231 * sysfs_create_dir - create a directory for an object.
232 * @kobj: object we're creating directory for.
233 * @shadow_parent: parent parent object.
236 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
238 struct dentry * dentry = NULL;
239 struct dentry * parent;
240 int error = 0;
242 BUG_ON(!kobj);
244 if (shadow_parent)
245 parent = shadow_parent;
246 else if (kobj->parent)
247 parent = kobj->parent->dentry;
248 else if (sysfs_mount && sysfs_mount->mnt_sb)
249 parent = sysfs_mount->mnt_sb->s_root;
250 else
251 return -EFAULT;
253 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
254 if (!error)
255 kobj->dentry = dentry;
256 return error;
259 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
260 * attribute file
262 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
264 struct attribute * attr = NULL;
265 struct bin_attribute * bin_attr = NULL;
266 int (* init) (struct inode *) = NULL;
267 int error = 0;
269 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
270 bin_attr = sd->s_element;
271 attr = &bin_attr->attr;
272 } else {
273 attr = sd->s_element;
274 init = init_file;
277 dentry->d_fsdata = sysfs_get(sd);
278 /* protect sd->s_dentry against sysfs_d_iput */
279 spin_lock(&sysfs_lock);
280 sd->s_dentry = dentry;
281 spin_unlock(&sysfs_lock);
282 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
283 if (error) {
284 sysfs_put(sd);
285 return error;
288 if (bin_attr) {
289 dentry->d_inode->i_size = bin_attr->size;
290 dentry->d_inode->i_fop = &bin_fops;
292 dentry->d_op = &sysfs_dentry_ops;
293 d_rehash(dentry);
295 return 0;
298 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
300 int err = 0;
302 dentry->d_fsdata = sysfs_get(sd);
303 /* protect sd->s_dentry against sysfs_d_iput */
304 spin_lock(&sysfs_lock);
305 sd->s_dentry = dentry;
306 spin_unlock(&sysfs_lock);
307 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
308 if (!err) {
309 dentry->d_op = &sysfs_dentry_ops;
310 d_rehash(dentry);
311 } else
312 sysfs_put(sd);
314 return err;
317 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
318 struct nameidata *nd)
320 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
321 struct sysfs_dirent * sd;
322 int err = 0;
324 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
325 if (sd->s_type & SYSFS_NOT_PINNED) {
326 const unsigned char * name = sysfs_get_name(sd);
328 if (strcmp(name, dentry->d_name.name))
329 continue;
331 if (sd->s_type & SYSFS_KOBJ_LINK)
332 err = sysfs_attach_link(sd, dentry);
333 else
334 err = sysfs_attach_attr(sd, dentry);
335 break;
339 return ERR_PTR(err);
342 const struct inode_operations sysfs_dir_inode_operations = {
343 .lookup = sysfs_lookup,
344 .setattr = sysfs_setattr,
347 static void remove_dir(struct dentry * d)
349 struct dentry * parent = dget(d->d_parent);
350 struct sysfs_dirent * sd;
352 mutex_lock(&parent->d_inode->i_mutex);
353 d_delete(d);
354 sd = d->d_fsdata;
355 list_del_init(&sd->s_sibling);
356 sysfs_put(sd);
357 if (d->d_inode)
358 simple_rmdir(parent->d_inode,d);
360 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
361 atomic_read(&d->d_count));
363 mutex_unlock(&parent->d_inode->i_mutex);
364 dput(parent);
367 void sysfs_remove_subdir(struct dentry * d)
369 remove_dir(d);
373 static void __sysfs_remove_dir(struct dentry *dentry)
375 struct sysfs_dirent * parent_sd;
376 struct sysfs_dirent * sd, * tmp;
378 dget(dentry);
379 if (!dentry)
380 return;
382 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
383 mutex_lock(&dentry->d_inode->i_mutex);
384 parent_sd = dentry->d_fsdata;
385 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
386 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
387 continue;
388 list_del_init(&sd->s_sibling);
389 sysfs_drop_dentry(sd, dentry);
390 sysfs_put(sd);
392 mutex_unlock(&dentry->d_inode->i_mutex);
394 remove_dir(dentry);
396 * Drop reference from dget() on entrance.
398 dput(dentry);
402 * sysfs_remove_dir - remove an object's directory.
403 * @kobj: object.
405 * The only thing special about this is that we remove any files in
406 * the directory before we remove the directory, and we've inlined
407 * what used to be sysfs_rmdir() below, instead of calling separately.
410 void sysfs_remove_dir(struct kobject * kobj)
412 __sysfs_remove_dir(kobj->dentry);
413 kobj->dentry = NULL;
416 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
417 const char *new_name)
419 int error;
420 struct dentry * new_dentry;
421 struct sysfs_dirent *sd, *parent_sd;
423 if (!new_parent)
424 return -EFAULT;
426 down_write(&sysfs_rename_sem);
427 mutex_lock(&new_parent->d_inode->i_mutex);
429 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
430 if (IS_ERR(new_dentry)) {
431 error = PTR_ERR(new_dentry);
432 goto out_unlock;
435 /* By allowing two different directories with the same
436 * d_parent we allow this routine to move between different
437 * shadows of the same directory
439 error = -EINVAL;
440 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
441 new_dentry->d_parent->d_inode != new_parent->d_inode ||
442 new_dentry == kobj->dentry)
443 goto out_dput;
445 error = -EEXIST;
446 if (new_dentry->d_inode)
447 goto out_dput;
449 error = kobject_set_name(kobj, "%s", new_name);
450 if (error)
451 goto out_drop;
453 d_add(new_dentry, NULL);
454 d_move(kobj->dentry, new_dentry);
456 sd = kobj->dentry->d_fsdata;
457 parent_sd = new_parent->d_fsdata;
459 list_del_init(&sd->s_sibling);
460 list_add(&sd->s_sibling, &parent_sd->s_children);
462 error = 0;
463 goto out_unlock;
465 out_drop:
466 d_drop(new_dentry);
467 out_dput:
468 dput(new_dentry);
469 out_unlock:
470 mutex_unlock(&new_parent->d_inode->i_mutex);
471 up_write(&sysfs_rename_sem);
472 return error;
475 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
477 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
478 struct sysfs_dirent *new_parent_sd, *sd;
479 int error;
481 old_parent_dentry = kobj->parent ?
482 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
483 new_parent_dentry = new_parent ?
484 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
486 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
487 return 0; /* nothing to move */
488 again:
489 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
490 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
491 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
492 goto again;
495 new_parent_sd = new_parent_dentry->d_fsdata;
496 sd = kobj->dentry->d_fsdata;
498 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
499 strlen(kobj->name));
500 if (IS_ERR(new_dentry)) {
501 error = PTR_ERR(new_dentry);
502 goto out;
503 } else
504 error = 0;
505 d_add(new_dentry, NULL);
506 d_move(kobj->dentry, new_dentry);
507 dput(new_dentry);
509 /* Remove from old parent's list and insert into new parent's list. */
510 list_del_init(&sd->s_sibling);
511 list_add(&sd->s_sibling, &new_parent_sd->s_children);
513 out:
514 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
515 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
517 return error;
520 static int sysfs_dir_open(struct inode *inode, struct file *file)
522 struct dentry * dentry = file->f_path.dentry;
523 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
524 struct sysfs_dirent * sd;
526 mutex_lock(&dentry->d_inode->i_mutex);
527 sd = sysfs_new_dirent(NULL, 0, 0);
528 if (sd)
529 sysfs_attach_dirent(sd, parent_sd, NULL);
530 mutex_unlock(&dentry->d_inode->i_mutex);
532 file->private_data = sd;
533 return sd ? 0 : -ENOMEM;
536 static int sysfs_dir_close(struct inode *inode, struct file *file)
538 struct dentry * dentry = file->f_path.dentry;
539 struct sysfs_dirent * cursor = file->private_data;
541 mutex_lock(&dentry->d_inode->i_mutex);
542 list_del_init(&cursor->s_sibling);
543 mutex_unlock(&dentry->d_inode->i_mutex);
545 release_sysfs_dirent(cursor);
547 return 0;
550 /* Relationship between s_mode and the DT_xxx types */
551 static inline unsigned char dt_type(struct sysfs_dirent *sd)
553 return (sd->s_mode >> 12) & 15;
556 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
558 struct dentry *dentry = filp->f_path.dentry;
559 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
560 struct sysfs_dirent *cursor = filp->private_data;
561 struct list_head *p, *q = &cursor->s_sibling;
562 ino_t ino;
563 int i = filp->f_pos;
565 switch (i) {
566 case 0:
567 ino = parent_sd->s_ino;
568 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
569 break;
570 filp->f_pos++;
571 i++;
572 /* fallthrough */
573 case 1:
574 ino = parent_ino(dentry);
575 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
576 break;
577 filp->f_pos++;
578 i++;
579 /* fallthrough */
580 default:
581 if (filp->f_pos == 2)
582 list_move(q, &parent_sd->s_children);
584 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
585 struct sysfs_dirent *next;
586 const char * name;
587 int len;
589 next = list_entry(p, struct sysfs_dirent,
590 s_sibling);
591 if (!next->s_element)
592 continue;
594 name = sysfs_get_name(next);
595 len = strlen(name);
596 ino = next->s_ino;
598 if (filldir(dirent, name, len, filp->f_pos, ino,
599 dt_type(next)) < 0)
600 return 0;
602 list_move(q, p);
603 p = q;
604 filp->f_pos++;
607 return 0;
610 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
612 struct dentry * dentry = file->f_path.dentry;
614 mutex_lock(&dentry->d_inode->i_mutex);
615 switch (origin) {
616 case 1:
617 offset += file->f_pos;
618 case 0:
619 if (offset >= 0)
620 break;
621 default:
622 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
623 return -EINVAL;
625 if (offset != file->f_pos) {
626 file->f_pos = offset;
627 if (file->f_pos >= 2) {
628 struct sysfs_dirent *sd = dentry->d_fsdata;
629 struct sysfs_dirent *cursor = file->private_data;
630 struct list_head *p;
631 loff_t n = file->f_pos - 2;
633 list_del(&cursor->s_sibling);
634 p = sd->s_children.next;
635 while (n && p != &sd->s_children) {
636 struct sysfs_dirent *next;
637 next = list_entry(p, struct sysfs_dirent,
638 s_sibling);
639 if (next->s_element)
640 n--;
641 p = p->next;
643 list_add_tail(&cursor->s_sibling, p);
646 mutex_unlock(&dentry->d_inode->i_mutex);
647 return offset;
652 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
653 * @kobj: object we're creating shadow of.
656 int sysfs_make_shadowed_dir(struct kobject *kobj,
657 void * (*follow_link)(struct dentry *, struct nameidata *))
659 struct inode *inode;
660 struct inode_operations *i_op;
662 inode = kobj->dentry->d_inode;
663 if (inode->i_op != &sysfs_dir_inode_operations)
664 return -EINVAL;
666 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
667 if (!i_op)
668 return -ENOMEM;
670 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
671 i_op->follow_link = follow_link;
673 /* Locking of inode->i_op?
674 * Since setting i_op is a single word write and they
675 * are atomic we should be ok here.
677 inode->i_op = i_op;
678 return 0;
682 * sysfs_create_shadow_dir - create a shadow directory for an object.
683 * @kobj: object we're creating directory for.
685 * sysfs_make_shadowed_dir must already have been called on this
686 * directory.
689 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
691 struct sysfs_dirent *sd;
692 struct dentry *parent, *dir, *shadow;
693 struct inode *inode;
695 dir = kobj->dentry;
696 inode = dir->d_inode;
697 parent = dir->d_parent;
698 shadow = ERR_PTR(-EINVAL);
699 if (!sysfs_is_shadowed_inode(inode))
700 goto out;
702 shadow = d_alloc(parent, &dir->d_name);
703 if (!shadow)
704 goto nomem;
706 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR);
707 if (!sd)
708 goto nomem;
709 sysfs_attach_dirent(sd, NULL, shadow);
711 d_instantiate(shadow, igrab(inode));
712 inc_nlink(inode);
713 inc_nlink(parent->d_inode);
714 shadow->d_op = &sysfs_dentry_ops;
716 dget(shadow); /* Extra count - pin the dentry in core */
718 out:
719 return shadow;
720 nomem:
721 dput(shadow);
722 shadow = ERR_PTR(-ENOMEM);
723 goto out;
727 * sysfs_remove_shadow_dir - remove an object's directory.
728 * @shadow: dentry of shadow directory
730 * The only thing special about this is that we remove any files in
731 * the directory before we remove the directory, and we've inlined
732 * what used to be sysfs_rmdir() below, instead of calling separately.
735 void sysfs_remove_shadow_dir(struct dentry *shadow)
737 __sysfs_remove_dir(shadow);
740 const struct file_operations sysfs_dir_operations = {
741 .open = sysfs_dir_open,
742 .release = sysfs_dir_close,
743 .llseek = sysfs_dir_lseek,
744 .read = generic_read_dir,
745 .readdir = sysfs_readdir,