sysfs: slim down sysfs_dirent->s_active
[linux-2.6/mini2440.git] / fs / sysfs / dir.c
blob40596a0eee526db8fac44d8667bcb83e638c9e34
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_get_active - get an active reference to sysfs_dirent
26 * @sd: sysfs_dirent to get an active reference to
28 * Get an active reference of @sd. This function is noop if @sd
29 * is NULL.
31 * RETURNS:
32 * Pointer to @sd on success, NULL on failure.
34 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
36 if (unlikely(!sd))
37 return NULL;
39 while (1) {
40 int v, t;
42 v = atomic_read(&sd->s_active);
43 if (unlikely(v < 0))
44 return NULL;
46 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
47 if (likely(t == v))
48 return sd;
49 if (t < 0)
50 return NULL;
52 cpu_relax();
56 /**
57 * sysfs_put_active - put an active reference to sysfs_dirent
58 * @sd: sysfs_dirent to put an active reference to
60 * Put an active reference to @sd. This function is noop if @sd
61 * is NULL.
63 void sysfs_put_active(struct sysfs_dirent *sd)
65 struct completion *cmpl;
66 int v;
68 if (unlikely(!sd))
69 return;
71 v = atomic_dec_return(&sd->s_active);
72 if (likely(v != SD_DEACTIVATED_BIAS))
73 return;
75 /* atomic_dec_return() is a mb(), we'll always see the updated
76 * sd->s_sibling.next.
78 cmpl = (void *)sd->s_sibling.next;
79 complete(cmpl);
82 /**
83 * sysfs_get_active_two - get active references to sysfs_dirent and parent
84 * @sd: sysfs_dirent of interest
86 * Get active reference to @sd and its parent. Parent's active
87 * reference is grabbed first. This function is noop if @sd is
88 * NULL.
90 * RETURNS:
91 * Pointer to @sd on success, NULL on failure.
93 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
95 if (sd) {
96 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
97 return NULL;
98 if (unlikely(!sysfs_get_active(sd))) {
99 sysfs_put_active(sd->s_parent);
100 return NULL;
103 return sd;
107 * sysfs_put_active_two - put active references to sysfs_dirent and parent
108 * @sd: sysfs_dirent of interest
110 * Put active references to @sd and its parent. This function is
111 * noop if @sd is NULL.
113 void sysfs_put_active_two(struct sysfs_dirent *sd)
115 if (sd) {
116 sysfs_put_active(sd);
117 sysfs_put_active(sd->s_parent);
122 * sysfs_deactivate - deactivate sysfs_dirent
123 * @sd: sysfs_dirent to deactivate
125 * Deny new active references and drain existing ones.
127 void sysfs_deactivate(struct sysfs_dirent *sd)
129 DECLARE_COMPLETION_ONSTACK(wait);
130 int v;
132 BUG_ON(!list_empty(&sd->s_sibling));
133 sd->s_sibling.next = (void *)&wait;
135 /* atomic_add_return() is a mb(), put_active() will always see
136 * the updated sd->s_sibling.next.
138 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
140 if (v != SD_DEACTIVATED_BIAS)
141 wait_for_completion(&wait);
143 INIT_LIST_HEAD(&sd->s_sibling);
146 static int sysfs_alloc_ino(ino_t *pino)
148 int ino, rc;
150 retry:
151 spin_lock(&sysfs_ino_lock);
152 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
153 spin_unlock(&sysfs_ino_lock);
155 if (rc == -EAGAIN) {
156 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
157 goto retry;
158 rc = -ENOMEM;
161 *pino = ino;
162 return rc;
165 static void sysfs_free_ino(ino_t ino)
167 spin_lock(&sysfs_ino_lock);
168 ida_remove(&sysfs_ino_ida, ino);
169 spin_unlock(&sysfs_ino_lock);
172 void release_sysfs_dirent(struct sysfs_dirent * sd)
174 struct sysfs_dirent *parent_sd;
176 repeat:
177 parent_sd = sd->s_parent;
179 if (sd->s_type & SYSFS_KOBJ_LINK)
180 sysfs_put(sd->s_elem.symlink.target_sd);
181 if (sd->s_type & SYSFS_COPY_NAME)
182 kfree(sd->s_name);
183 kfree(sd->s_iattr);
184 sysfs_free_ino(sd->s_ino);
185 kmem_cache_free(sysfs_dir_cachep, sd);
187 sd = parent_sd;
188 if (sd && atomic_dec_and_test(&sd->s_count))
189 goto repeat;
192 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
194 struct sysfs_dirent * sd = dentry->d_fsdata;
196 if (sd) {
197 /* sd->s_dentry is protected with sysfs_lock. This
198 * allows sysfs_drop_dentry() to dereference it.
200 spin_lock(&sysfs_lock);
202 /* The dentry might have been deleted or another
203 * lookup could have happened updating sd->s_dentry to
204 * point the new dentry. Ignore if it isn't pointing
205 * to this dentry.
207 if (sd->s_dentry == dentry)
208 sd->s_dentry = NULL;
209 spin_unlock(&sysfs_lock);
210 sysfs_put(sd);
212 iput(inode);
215 static struct dentry_operations sysfs_dentry_ops = {
216 .d_iput = sysfs_d_iput,
219 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
221 char *dup_name = NULL;
222 struct sysfs_dirent *sd = NULL;
224 if (type & SYSFS_COPY_NAME) {
225 name = dup_name = kstrdup(name, GFP_KERNEL);
226 if (!name)
227 goto err_out;
230 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
231 if (!sd)
232 goto err_out;
234 if (sysfs_alloc_ino(&sd->s_ino))
235 goto err_out;
237 atomic_set(&sd->s_count, 1);
238 atomic_set(&sd->s_active, 0);
239 atomic_set(&sd->s_event, 1);
240 INIT_LIST_HEAD(&sd->s_children);
241 INIT_LIST_HEAD(&sd->s_sibling);
243 sd->s_name = name;
244 sd->s_mode = mode;
245 sd->s_type = type;
247 return sd;
249 err_out:
250 kfree(dup_name);
251 kmem_cache_free(sysfs_dir_cachep, sd);
252 return NULL;
255 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
257 dentry->d_op = &sysfs_dentry_ops;
258 dentry->d_fsdata = sysfs_get(sd);
260 /* protect sd->s_dentry against sysfs_d_iput */
261 spin_lock(&sysfs_lock);
262 sd->s_dentry = dentry;
263 spin_unlock(&sysfs_lock);
265 d_rehash(dentry);
268 void sysfs_attach_dirent(struct sysfs_dirent *sd,
269 struct sysfs_dirent *parent_sd, struct dentry *dentry)
271 if (dentry)
272 sysfs_attach_dentry(sd, dentry);
274 if (parent_sd) {
275 sd->s_parent = sysfs_get(parent_sd);
276 list_add(&sd->s_sibling, &parent_sd->s_children);
282 * Return -EEXIST if there is already a sysfs element with the same name for
283 * the same parent.
285 * called with parent inode's i_mutex held
287 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
288 const unsigned char *new)
290 struct sysfs_dirent * sd;
292 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
293 if (sd->s_type) {
294 if (strcmp(sd->s_name, new))
295 continue;
296 else
297 return -EEXIST;
301 return 0;
304 static int create_dir(struct kobject *kobj, struct dentry *parent,
305 const char *name, struct dentry **p_dentry)
307 int error;
308 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
309 struct dentry *dentry;
310 struct inode *inode;
311 struct sysfs_dirent *sd;
313 mutex_lock(&parent->d_inode->i_mutex);
315 /* allocate */
316 dentry = lookup_one_len(name, parent, strlen(name));
317 if (IS_ERR(dentry)) {
318 error = PTR_ERR(dentry);
319 goto out_unlock;
322 error = -EEXIST;
323 if (dentry->d_inode)
324 goto out_dput;
326 error = -ENOMEM;
327 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
328 if (!sd)
329 goto out_drop;
330 sd->s_elem.dir.kobj = kobj;
332 inode = sysfs_get_inode(sd);
333 if (!inode)
334 goto out_sput;
336 if (inode->i_state & I_NEW) {
337 inode->i_op = &sysfs_dir_inode_operations;
338 inode->i_fop = &sysfs_dir_operations;
339 /* directory inodes start off with i_nlink == 2 (for ".") */
340 inc_nlink(inode);
343 /* link in */
344 error = -EEXIST;
345 if (sysfs_dirent_exist(parent->d_fsdata, name))
346 goto out_iput;
348 sysfs_instantiate(dentry, inode);
349 inc_nlink(parent->d_inode);
350 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
352 *p_dentry = dentry;
353 error = 0;
354 goto out_unlock; /* pin directory dentry in core */
356 out_iput:
357 iput(inode);
358 out_sput:
359 sysfs_put(sd);
360 out_drop:
361 d_drop(dentry);
362 out_dput:
363 dput(dentry);
364 out_unlock:
365 mutex_unlock(&parent->d_inode->i_mutex);
366 return error;
370 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
372 return create_dir(k,k->dentry,n,d);
376 * sysfs_create_dir - create a directory for an object.
377 * @kobj: object we're creating directory for.
378 * @shadow_parent: parent parent object.
381 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
383 struct dentry * dentry = NULL;
384 struct dentry * parent;
385 int error = 0;
387 BUG_ON(!kobj);
389 if (shadow_parent)
390 parent = shadow_parent;
391 else if (kobj->parent)
392 parent = kobj->parent->dentry;
393 else if (sysfs_mount && sysfs_mount->mnt_sb)
394 parent = sysfs_mount->mnt_sb->s_root;
395 else
396 return -EFAULT;
398 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
399 if (!error)
400 kobj->dentry = dentry;
401 return error;
404 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
405 struct nameidata *nd)
407 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
408 struct sysfs_dirent * sd;
409 struct inode *inode;
410 int found = 0;
412 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
413 if ((sd->s_type & SYSFS_NOT_PINNED) &&
414 !strcmp(sd->s_name, dentry->d_name.name)) {
415 found = 1;
416 break;
420 /* no such entry */
421 if (!found)
422 return NULL;
424 /* attach dentry and inode */
425 inode = sysfs_get_inode(sd);
426 if (!inode)
427 return ERR_PTR(-ENOMEM);
429 if (inode->i_state & I_NEW) {
430 /* initialize inode according to type */
431 if (sd->s_type & SYSFS_KOBJ_ATTR) {
432 inode->i_size = PAGE_SIZE;
433 inode->i_fop = &sysfs_file_operations;
434 } else if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
435 struct bin_attribute *bin_attr =
436 sd->s_elem.bin_attr.bin_attr;
437 inode->i_size = bin_attr->size;
438 inode->i_fop = &bin_fops;
439 } else if (sd->s_type & SYSFS_KOBJ_LINK)
440 inode->i_op = &sysfs_symlink_inode_operations;
443 sysfs_instantiate(dentry, inode);
444 sysfs_attach_dentry(sd, dentry);
446 return NULL;
449 const struct inode_operations sysfs_dir_inode_operations = {
450 .lookup = sysfs_lookup,
451 .setattr = sysfs_setattr,
454 static void remove_dir(struct dentry * d)
456 struct dentry *parent = d->d_parent;
457 struct sysfs_dirent *sd = d->d_fsdata;
459 mutex_lock(&parent->d_inode->i_mutex);
461 list_del_init(&sd->s_sibling);
463 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
464 atomic_read(&d->d_count));
466 mutex_unlock(&parent->d_inode->i_mutex);
468 sysfs_drop_dentry(sd);
469 sysfs_deactivate(sd);
470 sysfs_put(sd);
473 void sysfs_remove_subdir(struct dentry * d)
475 remove_dir(d);
479 static void __sysfs_remove_dir(struct dentry *dentry)
481 LIST_HEAD(removed);
482 struct sysfs_dirent * parent_sd;
483 struct sysfs_dirent * sd, * tmp;
485 if (!dentry)
486 return;
488 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
489 mutex_lock(&dentry->d_inode->i_mutex);
490 parent_sd = dentry->d_fsdata;
491 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
492 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
493 continue;
494 list_move(&sd->s_sibling, &removed);
496 mutex_unlock(&dentry->d_inode->i_mutex);
498 list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
499 list_del_init(&sd->s_sibling);
500 sysfs_drop_dentry(sd);
501 sysfs_deactivate(sd);
502 sysfs_put(sd);
505 remove_dir(dentry);
509 * sysfs_remove_dir - remove an object's directory.
510 * @kobj: object.
512 * The only thing special about this is that we remove any files in
513 * the directory before we remove the directory, and we've inlined
514 * what used to be sysfs_rmdir() below, instead of calling separately.
517 void sysfs_remove_dir(struct kobject * kobj)
519 struct dentry *d = kobj->dentry;
521 spin_lock(&kobj_sysfs_assoc_lock);
522 kobj->dentry = NULL;
523 spin_unlock(&kobj_sysfs_assoc_lock);
525 __sysfs_remove_dir(d);
528 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
529 const char *new_name)
531 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
532 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
533 struct dentry *new_dentry;
534 char *dup_name;
535 int error;
537 if (!new_parent)
538 return -EFAULT;
540 down_write(&sysfs_rename_sem);
541 mutex_lock(&new_parent->d_inode->i_mutex);
543 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
544 if (IS_ERR(new_dentry)) {
545 error = PTR_ERR(new_dentry);
546 goto out_unlock;
549 /* By allowing two different directories with the same
550 * d_parent we allow this routine to move between different
551 * shadows of the same directory
553 error = -EINVAL;
554 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
555 new_dentry->d_parent->d_inode != new_parent->d_inode ||
556 new_dentry == kobj->dentry)
557 goto out_dput;
559 error = -EEXIST;
560 if (new_dentry->d_inode)
561 goto out_dput;
563 /* rename kobject and sysfs_dirent */
564 error = -ENOMEM;
565 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
566 if (!new_name)
567 goto out_drop;
569 error = kobject_set_name(kobj, "%s", new_name);
570 if (error)
571 goto out_free;
573 kfree(sd->s_name);
574 sd->s_name = new_name;
576 /* move under the new parent */
577 d_add(new_dentry, NULL);
578 d_move(kobj->dentry, new_dentry);
580 list_del_init(&sd->s_sibling);
581 sysfs_get(parent_sd);
582 sysfs_put(sd->s_parent);
583 sd->s_parent = parent_sd;
584 list_add(&sd->s_sibling, &parent_sd->s_children);
586 error = 0;
587 goto out_unlock;
589 out_free:
590 kfree(dup_name);
591 out_drop:
592 d_drop(new_dentry);
593 out_dput:
594 dput(new_dentry);
595 out_unlock:
596 mutex_unlock(&new_parent->d_inode->i_mutex);
597 up_write(&sysfs_rename_sem);
598 return error;
601 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
603 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
604 struct sysfs_dirent *new_parent_sd, *sd;
605 int error;
607 old_parent_dentry = kobj->parent ?
608 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
609 new_parent_dentry = new_parent ?
610 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
612 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
613 return 0; /* nothing to move */
614 again:
615 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
616 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
617 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
618 goto again;
621 new_parent_sd = new_parent_dentry->d_fsdata;
622 sd = kobj->dentry->d_fsdata;
624 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
625 strlen(kobj->name));
626 if (IS_ERR(new_dentry)) {
627 error = PTR_ERR(new_dentry);
628 goto out;
629 } else
630 error = 0;
631 d_add(new_dentry, NULL);
632 d_move(kobj->dentry, new_dentry);
633 dput(new_dentry);
635 /* Remove from old parent's list and insert into new parent's list. */
636 list_del_init(&sd->s_sibling);
637 sysfs_get(new_parent_sd);
638 sysfs_put(sd->s_parent);
639 sd->s_parent = new_parent_sd;
640 list_add(&sd->s_sibling, &new_parent_sd->s_children);
642 out:
643 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
644 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
646 return error;
649 static int sysfs_dir_open(struct inode *inode, struct file *file)
651 struct dentry * dentry = file->f_path.dentry;
652 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
653 struct sysfs_dirent * sd;
655 mutex_lock(&dentry->d_inode->i_mutex);
656 sd = sysfs_new_dirent("_DIR_", 0, 0);
657 if (sd)
658 sysfs_attach_dirent(sd, parent_sd, NULL);
659 mutex_unlock(&dentry->d_inode->i_mutex);
661 file->private_data = sd;
662 return sd ? 0 : -ENOMEM;
665 static int sysfs_dir_close(struct inode *inode, struct file *file)
667 struct dentry * dentry = file->f_path.dentry;
668 struct sysfs_dirent * cursor = file->private_data;
670 mutex_lock(&dentry->d_inode->i_mutex);
671 list_del_init(&cursor->s_sibling);
672 mutex_unlock(&dentry->d_inode->i_mutex);
674 release_sysfs_dirent(cursor);
676 return 0;
679 /* Relationship between s_mode and the DT_xxx types */
680 static inline unsigned char dt_type(struct sysfs_dirent *sd)
682 return (sd->s_mode >> 12) & 15;
685 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
687 struct dentry *dentry = filp->f_path.dentry;
688 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
689 struct sysfs_dirent *cursor = filp->private_data;
690 struct list_head *p, *q = &cursor->s_sibling;
691 ino_t ino;
692 int i = filp->f_pos;
694 switch (i) {
695 case 0:
696 ino = parent_sd->s_ino;
697 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
698 break;
699 filp->f_pos++;
700 i++;
701 /* fallthrough */
702 case 1:
703 if (parent_sd->s_parent)
704 ino = parent_sd->s_parent->s_ino;
705 else
706 ino = parent_sd->s_ino;
707 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
708 break;
709 filp->f_pos++;
710 i++;
711 /* fallthrough */
712 default:
713 if (filp->f_pos == 2)
714 list_move(q, &parent_sd->s_children);
716 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
717 struct sysfs_dirent *next;
718 const char * name;
719 int len;
721 next = list_entry(p, struct sysfs_dirent,
722 s_sibling);
723 if (!next->s_type)
724 continue;
726 name = next->s_name;
727 len = strlen(name);
728 ino = next->s_ino;
730 if (filldir(dirent, name, len, filp->f_pos, ino,
731 dt_type(next)) < 0)
732 return 0;
734 list_move(q, p);
735 p = q;
736 filp->f_pos++;
739 return 0;
742 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
744 struct dentry * dentry = file->f_path.dentry;
746 mutex_lock(&dentry->d_inode->i_mutex);
747 switch (origin) {
748 case 1:
749 offset += file->f_pos;
750 case 0:
751 if (offset >= 0)
752 break;
753 default:
754 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
755 return -EINVAL;
757 if (offset != file->f_pos) {
758 file->f_pos = offset;
759 if (file->f_pos >= 2) {
760 struct sysfs_dirent *sd = dentry->d_fsdata;
761 struct sysfs_dirent *cursor = file->private_data;
762 struct list_head *p;
763 loff_t n = file->f_pos - 2;
765 list_del(&cursor->s_sibling);
766 p = sd->s_children.next;
767 while (n && p != &sd->s_children) {
768 struct sysfs_dirent *next;
769 next = list_entry(p, struct sysfs_dirent,
770 s_sibling);
771 if (next->s_type)
772 n--;
773 p = p->next;
775 list_add_tail(&cursor->s_sibling, p);
778 mutex_unlock(&dentry->d_inode->i_mutex);
779 return offset;
784 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
785 * @kobj: object we're creating shadow of.
788 int sysfs_make_shadowed_dir(struct kobject *kobj,
789 void * (*follow_link)(struct dentry *, struct nameidata *))
791 struct inode *inode;
792 struct inode_operations *i_op;
794 inode = kobj->dentry->d_inode;
795 if (inode->i_op != &sysfs_dir_inode_operations)
796 return -EINVAL;
798 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
799 if (!i_op)
800 return -ENOMEM;
802 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
803 i_op->follow_link = follow_link;
805 /* Locking of inode->i_op?
806 * Since setting i_op is a single word write and they
807 * are atomic we should be ok here.
809 inode->i_op = i_op;
810 return 0;
814 * sysfs_create_shadow_dir - create a shadow directory for an object.
815 * @kobj: object we're creating directory for.
817 * sysfs_make_shadowed_dir must already have been called on this
818 * directory.
821 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
823 struct dentry *dir = kobj->dentry;
824 struct inode *inode = dir->d_inode;
825 struct dentry *parent = dir->d_parent;
826 struct sysfs_dirent *parent_sd = parent->d_fsdata;
827 struct dentry *shadow;
828 struct sysfs_dirent *sd;
830 shadow = ERR_PTR(-EINVAL);
831 if (!sysfs_is_shadowed_inode(inode))
832 goto out;
834 shadow = d_alloc(parent, &dir->d_name);
835 if (!shadow)
836 goto nomem;
838 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
839 if (!sd)
840 goto nomem;
841 sd->s_elem.dir.kobj = kobj;
842 /* point to parent_sd but don't attach to it */
843 sd->s_parent = sysfs_get(parent_sd);
844 sysfs_attach_dirent(sd, NULL, shadow);
846 d_instantiate(shadow, igrab(inode));
847 inc_nlink(inode);
848 inc_nlink(parent->d_inode);
850 dget(shadow); /* Extra count - pin the dentry in core */
852 out:
853 return shadow;
854 nomem:
855 dput(shadow);
856 shadow = ERR_PTR(-ENOMEM);
857 goto out;
861 * sysfs_remove_shadow_dir - remove an object's directory.
862 * @shadow: dentry of shadow directory
864 * The only thing special about this is that we remove any files in
865 * the directory before we remove the directory, and we've inlined
866 * what used to be sysfs_rmdir() below, instead of calling separately.
869 void sysfs_remove_shadow_dir(struct dentry *shadow)
871 __sysfs_remove_dir(shadow);
874 const struct file_operations sysfs_dir_operations = {
875 .open = sysfs_dir_open,
876 .release = sysfs_dir_close,
877 .llseek = sysfs_dir_lseek,
878 .read = generic_read_dir,
879 .readdir = sysfs_readdir,