sysfs: rename sysfs_dirent->s_type to s_flags and make room for flags
[linux-2.6/mini2440.git] / fs / sysfs / dir.c
blobeb9bc0a8717b8b57e1905db37773a3e8a918f4f7
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 <linux/completion.h>
14 #include <asm/semaphore.h>
15 #include "sysfs.h"
17 DECLARE_RWSEM(sysfs_rename_sem);
18 spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
19 spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
21 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22 static DEFINE_IDA(sysfs_ino_ida);
24 /**
25 * sysfs_link_sibling - link sysfs_dirent into sibling list
26 * @sd: sysfs_dirent of interest
28 * Link @sd into its sibling list which starts from
29 * sd->s_parent->s_children.
31 * Locking:
32 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
34 static void sysfs_link_sibling(struct sysfs_dirent *sd)
36 struct sysfs_dirent *parent_sd = sd->s_parent;
38 BUG_ON(sd->s_sibling);
39 sd->s_sibling = parent_sd->s_children;
40 parent_sd->s_children = sd;
43 /**
44 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
45 * @sd: sysfs_dirent of interest
47 * Unlink @sd from its sibling list which starts from
48 * sd->s_parent->s_children.
50 * Locking:
51 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
53 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
55 struct sysfs_dirent **pos;
57 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
58 if (*pos == sd) {
59 *pos = sd->s_sibling;
60 sd->s_sibling = NULL;
61 break;
66 /**
67 * sysfs_get_active - get an active reference to sysfs_dirent
68 * @sd: sysfs_dirent to get an active reference to
70 * Get an active reference of @sd. This function is noop if @sd
71 * is NULL.
73 * RETURNS:
74 * Pointer to @sd on success, NULL on failure.
76 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
78 if (unlikely(!sd))
79 return NULL;
81 while (1) {
82 int v, t;
84 v = atomic_read(&sd->s_active);
85 if (unlikely(v < 0))
86 return NULL;
88 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
89 if (likely(t == v))
90 return sd;
91 if (t < 0)
92 return NULL;
94 cpu_relax();
98 /**
99 * sysfs_put_active - put an active reference to sysfs_dirent
100 * @sd: sysfs_dirent to put an active reference to
102 * Put an active reference to @sd. This function is noop if @sd
103 * is NULL.
105 void sysfs_put_active(struct sysfs_dirent *sd)
107 struct completion *cmpl;
108 int v;
110 if (unlikely(!sd))
111 return;
113 v = atomic_dec_return(&sd->s_active);
114 if (likely(v != SD_DEACTIVATED_BIAS))
115 return;
117 /* atomic_dec_return() is a mb(), we'll always see the updated
118 * sd->s_sibling.
120 cmpl = (void *)sd->s_sibling;
121 complete(cmpl);
125 * sysfs_get_active_two - get active references to sysfs_dirent and parent
126 * @sd: sysfs_dirent of interest
128 * Get active reference to @sd and its parent. Parent's active
129 * reference is grabbed first. This function is noop if @sd is
130 * NULL.
132 * RETURNS:
133 * Pointer to @sd on success, NULL on failure.
135 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
137 if (sd) {
138 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
139 return NULL;
140 if (unlikely(!sysfs_get_active(sd))) {
141 sysfs_put_active(sd->s_parent);
142 return NULL;
145 return sd;
149 * sysfs_put_active_two - put active references to sysfs_dirent and parent
150 * @sd: sysfs_dirent of interest
152 * Put active references to @sd and its parent. This function is
153 * noop if @sd is NULL.
155 void sysfs_put_active_two(struct sysfs_dirent *sd)
157 if (sd) {
158 sysfs_put_active(sd);
159 sysfs_put_active(sd->s_parent);
164 * sysfs_deactivate - deactivate sysfs_dirent
165 * @sd: sysfs_dirent to deactivate
167 * Deny new active references and drain existing ones.
169 void sysfs_deactivate(struct sysfs_dirent *sd)
171 DECLARE_COMPLETION_ONSTACK(wait);
172 int v;
174 BUG_ON(sd->s_sibling);
175 sd->s_sibling = (void *)&wait;
177 /* atomic_add_return() is a mb(), put_active() will always see
178 * the updated sd->s_sibling.
180 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
182 if (v != SD_DEACTIVATED_BIAS)
183 wait_for_completion(&wait);
185 sd->s_sibling = NULL;
188 static int sysfs_alloc_ino(ino_t *pino)
190 int ino, rc;
192 retry:
193 spin_lock(&sysfs_ino_lock);
194 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
195 spin_unlock(&sysfs_ino_lock);
197 if (rc == -EAGAIN) {
198 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
199 goto retry;
200 rc = -ENOMEM;
203 *pino = ino;
204 return rc;
207 static void sysfs_free_ino(ino_t ino)
209 spin_lock(&sysfs_ino_lock);
210 ida_remove(&sysfs_ino_ida, ino);
211 spin_unlock(&sysfs_ino_lock);
214 void release_sysfs_dirent(struct sysfs_dirent * sd)
216 struct sysfs_dirent *parent_sd;
218 repeat:
219 parent_sd = sd->s_parent;
221 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
222 sysfs_put(sd->s_elem.symlink.target_sd);
223 if (sysfs_type(sd) & SYSFS_COPY_NAME)
224 kfree(sd->s_name);
225 kfree(sd->s_iattr);
226 sysfs_free_ino(sd->s_ino);
227 kmem_cache_free(sysfs_dir_cachep, sd);
229 sd = parent_sd;
230 if (sd && atomic_dec_and_test(&sd->s_count))
231 goto repeat;
234 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
236 struct sysfs_dirent * sd = dentry->d_fsdata;
238 if (sd) {
239 /* sd->s_dentry is protected with sysfs_lock. This
240 * allows sysfs_drop_dentry() to dereference it.
242 spin_lock(&sysfs_lock);
244 /* The dentry might have been deleted or another
245 * lookup could have happened updating sd->s_dentry to
246 * point the new dentry. Ignore if it isn't pointing
247 * to this dentry.
249 if (sd->s_dentry == dentry)
250 sd->s_dentry = NULL;
251 spin_unlock(&sysfs_lock);
252 sysfs_put(sd);
254 iput(inode);
257 static struct dentry_operations sysfs_dentry_ops = {
258 .d_iput = sysfs_d_iput,
261 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
263 char *dup_name = NULL;
264 struct sysfs_dirent *sd = NULL;
266 if (type & SYSFS_COPY_NAME) {
267 name = dup_name = kstrdup(name, GFP_KERNEL);
268 if (!name)
269 goto err_out;
272 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
273 if (!sd)
274 goto err_out;
276 if (sysfs_alloc_ino(&sd->s_ino))
277 goto err_out;
279 atomic_set(&sd->s_count, 1);
280 atomic_set(&sd->s_active, 0);
281 atomic_set(&sd->s_event, 1);
283 sd->s_name = name;
284 sd->s_mode = mode;
285 sd->s_flags = type;
287 return sd;
289 err_out:
290 kfree(dup_name);
291 kmem_cache_free(sysfs_dir_cachep, sd);
292 return NULL;
295 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
297 dentry->d_op = &sysfs_dentry_ops;
298 dentry->d_fsdata = sysfs_get(sd);
300 /* protect sd->s_dentry against sysfs_d_iput */
301 spin_lock(&sysfs_lock);
302 sd->s_dentry = dentry;
303 spin_unlock(&sysfs_lock);
305 d_rehash(dentry);
308 void sysfs_attach_dirent(struct sysfs_dirent *sd,
309 struct sysfs_dirent *parent_sd, struct dentry *dentry)
311 if (dentry)
312 sysfs_attach_dentry(sd, dentry);
314 if (parent_sd) {
315 sd->s_parent = sysfs_get(parent_sd);
316 sysfs_link_sibling(sd);
322 * Return -EEXIST if there is already a sysfs element with the same name for
323 * the same parent.
325 * called with parent inode's i_mutex held
327 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
328 const unsigned char *new)
330 struct sysfs_dirent * sd;
332 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
333 if (sysfs_type(sd)) {
334 if (strcmp(sd->s_name, new))
335 continue;
336 else
337 return -EEXIST;
341 return 0;
344 static int create_dir(struct kobject *kobj, struct dentry *parent,
345 const char *name, struct dentry **p_dentry)
347 int error;
348 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
349 struct dentry *dentry;
350 struct inode *inode;
351 struct sysfs_dirent *sd;
353 mutex_lock(&parent->d_inode->i_mutex);
355 /* allocate */
356 dentry = lookup_one_len(name, parent, strlen(name));
357 if (IS_ERR(dentry)) {
358 error = PTR_ERR(dentry);
359 goto out_unlock;
362 error = -EEXIST;
363 if (dentry->d_inode)
364 goto out_dput;
366 error = -ENOMEM;
367 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
368 if (!sd)
369 goto out_drop;
370 sd->s_elem.dir.kobj = kobj;
372 inode = sysfs_get_inode(sd);
373 if (!inode)
374 goto out_sput;
376 if (inode->i_state & I_NEW) {
377 inode->i_op = &sysfs_dir_inode_operations;
378 inode->i_fop = &sysfs_dir_operations;
379 /* directory inodes start off with i_nlink == 2 (for ".") */
380 inc_nlink(inode);
383 /* link in */
384 error = -EEXIST;
385 if (sysfs_dirent_exist(parent->d_fsdata, name))
386 goto out_iput;
388 sysfs_instantiate(dentry, inode);
389 inc_nlink(parent->d_inode);
390 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
392 *p_dentry = dentry;
393 error = 0;
394 goto out_unlock; /* pin directory dentry in core */
396 out_iput:
397 iput(inode);
398 out_sput:
399 sysfs_put(sd);
400 out_drop:
401 d_drop(dentry);
402 out_dput:
403 dput(dentry);
404 out_unlock:
405 mutex_unlock(&parent->d_inode->i_mutex);
406 return error;
410 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
412 return create_dir(k,k->dentry,n,d);
416 * sysfs_create_dir - create a directory for an object.
417 * @kobj: object we're creating directory for.
418 * @shadow_parent: parent parent object.
421 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
423 struct dentry * dentry = NULL;
424 struct dentry * parent;
425 int error = 0;
427 BUG_ON(!kobj);
429 if (shadow_parent)
430 parent = shadow_parent;
431 else if (kobj->parent)
432 parent = kobj->parent->dentry;
433 else if (sysfs_mount && sysfs_mount->mnt_sb)
434 parent = sysfs_mount->mnt_sb->s_root;
435 else
436 return -EFAULT;
438 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
439 if (!error)
440 kobj->dentry = dentry;
441 return error;
444 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
445 struct nameidata *nd)
447 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
448 struct sysfs_dirent * sd;
449 struct bin_attribute *bin_attr;
450 struct inode *inode;
451 int found = 0;
453 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
454 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
455 !strcmp(sd->s_name, dentry->d_name.name)) {
456 found = 1;
457 break;
461 /* no such entry */
462 if (!found)
463 return NULL;
465 /* attach dentry and inode */
466 inode = sysfs_get_inode(sd);
467 if (!inode)
468 return ERR_PTR(-ENOMEM);
470 if (inode->i_state & I_NEW) {
471 /* initialize inode according to type */
472 switch (sysfs_type(sd)) {
473 case SYSFS_KOBJ_ATTR:
474 inode->i_size = PAGE_SIZE;
475 inode->i_fop = &sysfs_file_operations;
476 break;
477 case SYSFS_KOBJ_BIN_ATTR:
478 bin_attr = sd->s_elem.bin_attr.bin_attr;
479 inode->i_size = bin_attr->size;
480 inode->i_fop = &bin_fops;
481 break;
482 case SYSFS_KOBJ_LINK:
483 inode->i_op = &sysfs_symlink_inode_operations;
484 break;
485 default:
486 BUG();
490 sysfs_instantiate(dentry, inode);
491 sysfs_attach_dentry(sd, dentry);
493 return NULL;
496 const struct inode_operations sysfs_dir_inode_operations = {
497 .lookup = sysfs_lookup,
498 .setattr = sysfs_setattr,
501 static void remove_dir(struct dentry * d)
503 struct dentry *parent = d->d_parent;
504 struct sysfs_dirent *sd = d->d_fsdata;
506 mutex_lock(&parent->d_inode->i_mutex);
508 sysfs_unlink_sibling(sd);
510 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
511 atomic_read(&d->d_count));
513 mutex_unlock(&parent->d_inode->i_mutex);
515 sysfs_drop_dentry(sd);
516 sysfs_deactivate(sd);
517 sysfs_put(sd);
520 void sysfs_remove_subdir(struct dentry * d)
522 remove_dir(d);
526 static void __sysfs_remove_dir(struct dentry *dentry)
528 struct sysfs_dirent *removed = NULL;
529 struct sysfs_dirent *parent_sd;
530 struct sysfs_dirent **pos;
532 if (!dentry)
533 return;
535 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
536 mutex_lock(&dentry->d_inode->i_mutex);
537 parent_sd = dentry->d_fsdata;
538 pos = &parent_sd->s_children;
539 while (*pos) {
540 struct sysfs_dirent *sd = *pos;
542 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
543 *pos = sd->s_sibling;
544 sd->s_sibling = removed;
545 removed = sd;
546 } else
547 pos = &(*pos)->s_sibling;
549 mutex_unlock(&dentry->d_inode->i_mutex);
551 while (removed) {
552 struct sysfs_dirent *sd = removed;
554 removed = sd->s_sibling;
555 sd->s_sibling = NULL;
557 sysfs_drop_dentry(sd);
558 sysfs_deactivate(sd);
559 sysfs_put(sd);
562 remove_dir(dentry);
566 * sysfs_remove_dir - remove an object's directory.
567 * @kobj: object.
569 * The only thing special about this is that we remove any files in
570 * the directory before we remove the directory, and we've inlined
571 * what used to be sysfs_rmdir() below, instead of calling separately.
574 void sysfs_remove_dir(struct kobject * kobj)
576 struct dentry *d = kobj->dentry;
578 spin_lock(&kobj_sysfs_assoc_lock);
579 kobj->dentry = NULL;
580 spin_unlock(&kobj_sysfs_assoc_lock);
582 __sysfs_remove_dir(d);
585 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
586 const char *new_name)
588 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
589 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
590 struct dentry *new_dentry;
591 char *dup_name;
592 int error;
594 if (!new_parent)
595 return -EFAULT;
597 down_write(&sysfs_rename_sem);
598 mutex_lock(&new_parent->d_inode->i_mutex);
600 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
601 if (IS_ERR(new_dentry)) {
602 error = PTR_ERR(new_dentry);
603 goto out_unlock;
606 /* By allowing two different directories with the same
607 * d_parent we allow this routine to move between different
608 * shadows of the same directory
610 error = -EINVAL;
611 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
612 new_dentry->d_parent->d_inode != new_parent->d_inode ||
613 new_dentry == kobj->dentry)
614 goto out_dput;
616 error = -EEXIST;
617 if (new_dentry->d_inode)
618 goto out_dput;
620 /* rename kobject and sysfs_dirent */
621 error = -ENOMEM;
622 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
623 if (!new_name)
624 goto out_drop;
626 error = kobject_set_name(kobj, "%s", new_name);
627 if (error)
628 goto out_free;
630 kfree(sd->s_name);
631 sd->s_name = new_name;
633 /* move under the new parent */
634 d_add(new_dentry, NULL);
635 d_move(kobj->dentry, new_dentry);
637 sysfs_unlink_sibling(sd);
638 sysfs_get(parent_sd);
639 sysfs_put(sd->s_parent);
640 sd->s_parent = parent_sd;
641 sysfs_link_sibling(sd);
643 error = 0;
644 goto out_unlock;
646 out_free:
647 kfree(dup_name);
648 out_drop:
649 d_drop(new_dentry);
650 out_dput:
651 dput(new_dentry);
652 out_unlock:
653 mutex_unlock(&new_parent->d_inode->i_mutex);
654 up_write(&sysfs_rename_sem);
655 return error;
658 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
660 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
661 struct sysfs_dirent *new_parent_sd, *sd;
662 int error;
664 old_parent_dentry = kobj->parent ?
665 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
666 new_parent_dentry = new_parent ?
667 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
669 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
670 return 0; /* nothing to move */
671 again:
672 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
673 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
674 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
675 goto again;
678 new_parent_sd = new_parent_dentry->d_fsdata;
679 sd = kobj->dentry->d_fsdata;
681 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
682 strlen(kobj->name));
683 if (IS_ERR(new_dentry)) {
684 error = PTR_ERR(new_dentry);
685 goto out;
686 } else
687 error = 0;
688 d_add(new_dentry, NULL);
689 d_move(kobj->dentry, new_dentry);
690 dput(new_dentry);
692 /* Remove from old parent's list and insert into new parent's list. */
693 sysfs_unlink_sibling(sd);
694 sysfs_get(new_parent_sd);
695 sysfs_put(sd->s_parent);
696 sd->s_parent = new_parent_sd;
697 sysfs_link_sibling(sd);
699 out:
700 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
701 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
703 return error;
706 static int sysfs_dir_open(struct inode *inode, struct file *file)
708 struct dentry * dentry = file->f_path.dentry;
709 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
710 struct sysfs_dirent * sd;
712 mutex_lock(&dentry->d_inode->i_mutex);
713 sd = sysfs_new_dirent("_DIR_", 0, 0);
714 if (sd)
715 sysfs_attach_dirent(sd, parent_sd, NULL);
716 mutex_unlock(&dentry->d_inode->i_mutex);
718 file->private_data = sd;
719 return sd ? 0 : -ENOMEM;
722 static int sysfs_dir_close(struct inode *inode, struct file *file)
724 struct dentry * dentry = file->f_path.dentry;
725 struct sysfs_dirent * cursor = file->private_data;
727 mutex_lock(&dentry->d_inode->i_mutex);
728 sysfs_unlink_sibling(cursor);
729 mutex_unlock(&dentry->d_inode->i_mutex);
731 release_sysfs_dirent(cursor);
733 return 0;
736 /* Relationship between s_mode and the DT_xxx types */
737 static inline unsigned char dt_type(struct sysfs_dirent *sd)
739 return (sd->s_mode >> 12) & 15;
742 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
744 struct dentry *dentry = filp->f_path.dentry;
745 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
746 struct sysfs_dirent *cursor = filp->private_data;
747 struct sysfs_dirent **pos;
748 ino_t ino;
749 int i = filp->f_pos;
751 switch (i) {
752 case 0:
753 ino = parent_sd->s_ino;
754 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
755 break;
756 filp->f_pos++;
757 i++;
758 /* fallthrough */
759 case 1:
760 if (parent_sd->s_parent)
761 ino = parent_sd->s_parent->s_ino;
762 else
763 ino = parent_sd->s_ino;
764 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
765 break;
766 filp->f_pos++;
767 i++;
768 /* fallthrough */
769 default:
770 pos = &parent_sd->s_children;
771 while (*pos != cursor)
772 pos = &(*pos)->s_sibling;
774 /* unlink cursor */
775 *pos = cursor->s_sibling;
777 if (filp->f_pos == 2)
778 pos = &parent_sd->s_children;
780 for ( ; *pos; pos = &(*pos)->s_sibling) {
781 struct sysfs_dirent *next = *pos;
782 const char * name;
783 int len;
785 if (!sysfs_type(next))
786 continue;
788 name = next->s_name;
789 len = strlen(name);
790 ino = next->s_ino;
792 if (filldir(dirent, name, len, filp->f_pos, ino,
793 dt_type(next)) < 0)
794 break;
796 filp->f_pos++;
799 /* put cursor back in */
800 cursor->s_sibling = *pos;
801 *pos = cursor;
803 return 0;
806 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
808 struct dentry * dentry = file->f_path.dentry;
810 mutex_lock(&dentry->d_inode->i_mutex);
811 switch (origin) {
812 case 1:
813 offset += file->f_pos;
814 case 0:
815 if (offset >= 0)
816 break;
817 default:
818 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
819 return -EINVAL;
821 if (offset != file->f_pos) {
822 file->f_pos = offset;
823 if (file->f_pos >= 2) {
824 struct sysfs_dirent *sd = dentry->d_fsdata;
825 struct sysfs_dirent *cursor = file->private_data;
826 struct sysfs_dirent **pos;
827 loff_t n = file->f_pos - 2;
829 sysfs_unlink_sibling(cursor);
831 pos = &sd->s_children;
832 while (n && *pos) {
833 struct sysfs_dirent *next = *pos;
834 if (sysfs_type(next))
835 n--;
836 pos = &(*pos)->s_sibling;
839 cursor->s_sibling = *pos;
840 *pos = cursor;
843 mutex_unlock(&dentry->d_inode->i_mutex);
844 return offset;
849 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
850 * @kobj: object we're creating shadow of.
853 int sysfs_make_shadowed_dir(struct kobject *kobj,
854 void * (*follow_link)(struct dentry *, struct nameidata *))
856 struct inode *inode;
857 struct inode_operations *i_op;
859 inode = kobj->dentry->d_inode;
860 if (inode->i_op != &sysfs_dir_inode_operations)
861 return -EINVAL;
863 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
864 if (!i_op)
865 return -ENOMEM;
867 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
868 i_op->follow_link = follow_link;
870 /* Locking of inode->i_op?
871 * Since setting i_op is a single word write and they
872 * are atomic we should be ok here.
874 inode->i_op = i_op;
875 return 0;
879 * sysfs_create_shadow_dir - create a shadow directory for an object.
880 * @kobj: object we're creating directory for.
882 * sysfs_make_shadowed_dir must already have been called on this
883 * directory.
886 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
888 struct dentry *dir = kobj->dentry;
889 struct inode *inode = dir->d_inode;
890 struct dentry *parent = dir->d_parent;
891 struct sysfs_dirent *parent_sd = parent->d_fsdata;
892 struct dentry *shadow;
893 struct sysfs_dirent *sd;
895 shadow = ERR_PTR(-EINVAL);
896 if (!sysfs_is_shadowed_inode(inode))
897 goto out;
899 shadow = d_alloc(parent, &dir->d_name);
900 if (!shadow)
901 goto nomem;
903 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
904 if (!sd)
905 goto nomem;
906 sd->s_elem.dir.kobj = kobj;
907 /* point to parent_sd but don't attach to it */
908 sd->s_parent = sysfs_get(parent_sd);
909 sysfs_attach_dirent(sd, NULL, shadow);
911 d_instantiate(shadow, igrab(inode));
912 inc_nlink(inode);
913 inc_nlink(parent->d_inode);
915 dget(shadow); /* Extra count - pin the dentry in core */
917 out:
918 return shadow;
919 nomem:
920 dput(shadow);
921 shadow = ERR_PTR(-ENOMEM);
922 goto out;
926 * sysfs_remove_shadow_dir - remove an object's directory.
927 * @shadow: dentry of shadow directory
929 * The only thing special about this is that we remove any files in
930 * the directory before we remove the directory, and we've inlined
931 * what used to be sysfs_rmdir() below, instead of calling separately.
934 void sysfs_remove_shadow_dir(struct dentry *shadow)
936 __sysfs_remove_dir(shadow);
939 const struct file_operations sysfs_dir_operations = {
940 .open = sysfs_dir_open,
941 .release = sysfs_dir_close,
942 .llseek = sysfs_dir_lseek,
943 .read = generic_read_dir,
944 .readdir = sysfs_readdir,