sysfs: separate out sysfs_attach_dentry()
[safe/jmp/linux-2.6] / fs / sysfs / dir.c
1 /*
2  * dir.c - Operations for sysfs directories.
3  */
4
5 #undef DEBUG
6
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"
15
16 DECLARE_RWSEM(sysfs_rename_sem);
17 spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
18 spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
19
20 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21 static DEFINE_IDA(sysfs_ino_ida);
22
23 int sysfs_alloc_ino(ino_t *pino)
24 {
25         int ino, rc;
26
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);
31
32         if (rc == -EAGAIN) {
33                 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
34                         goto retry;
35                 rc = -ENOMEM;
36         }
37
38         *pino = ino;
39         return rc;
40 }
41
42 static void sysfs_free_ino(ino_t ino)
43 {
44         spin_lock(&sysfs_ino_lock);
45         ida_remove(&sysfs_ino_ida, ino);
46         spin_unlock(&sysfs_ino_lock);
47 }
48
49 void release_sysfs_dirent(struct sysfs_dirent * sd)
50 {
51         struct sysfs_dirent *parent_sd;
52
53  repeat:
54         parent_sd = sd->s_parent;
55
56         /* If @sd is being released after deletion, s_active is write
57          * locked.  If @sd is cursor for directory walk or being
58          * released prematurely, s_active has no reader or writer.
59          *
60          * sysfs_deactivate() lies to lockdep that s_active is
61          * unlocked immediately.  Lie one more time to cover the
62          * previous lie.
63          */
64         if (!down_write_trylock(&sd->s_active))
65                 rwsem_acquire(&sd->s_active.dep_map,
66                               SYSFS_S_ACTIVE_DEACTIVATE, 0, _RET_IP_);
67         up_write(&sd->s_active);
68
69         if (sd->s_type & SYSFS_KOBJ_LINK)
70                 sysfs_put(sd->s_elem.symlink.target_sd);
71         if (sd->s_type & SYSFS_COPY_NAME)
72                 kfree(sd->s_name);
73         kfree(sd->s_iattr);
74         sysfs_free_ino(sd->s_ino);
75         kmem_cache_free(sysfs_dir_cachep, sd);
76
77         sd = parent_sd;
78         if (sd && atomic_dec_and_test(&sd->s_count))
79                 goto repeat;
80 }
81
82 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
83 {
84         struct sysfs_dirent * sd = dentry->d_fsdata;
85
86         if (sd) {
87                 /* sd->s_dentry is protected with sysfs_lock.  This
88                  * allows sysfs_drop_dentry() to dereference it.
89                  */
90                 spin_lock(&sysfs_lock);
91
92                 /* The dentry might have been deleted or another
93                  * lookup could have happened updating sd->s_dentry to
94                  * point the new dentry.  Ignore if it isn't pointing
95                  * to this dentry.
96                  */
97                 if (sd->s_dentry == dentry)
98                         sd->s_dentry = NULL;
99                 spin_unlock(&sysfs_lock);
100                 sysfs_put(sd);
101         }
102         iput(inode);
103 }
104
105 static struct dentry_operations sysfs_dentry_ops = {
106         .d_iput         = sysfs_d_iput,
107 };
108
109 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
110 {
111         char *dup_name = NULL;
112         struct sysfs_dirent *sd = NULL;
113
114         if (type & SYSFS_COPY_NAME) {
115                 name = dup_name = kstrdup(name, GFP_KERNEL);
116                 if (!name)
117                         goto err_out;
118         }
119
120         sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
121         if (!sd)
122                 goto err_out;
123
124         if (sysfs_alloc_ino(&sd->s_ino))
125                 goto err_out;
126
127         atomic_set(&sd->s_count, 1);
128         atomic_set(&sd->s_event, 1);
129         init_rwsem(&sd->s_active);
130         INIT_LIST_HEAD(&sd->s_children);
131         INIT_LIST_HEAD(&sd->s_sibling);
132
133         sd->s_name = name;
134         sd->s_mode = mode;
135         sd->s_type = type;
136
137         return sd;
138
139  err_out:
140         kfree(dup_name);
141         kmem_cache_free(sysfs_dir_cachep, sd);
142         return NULL;
143 }
144
145 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
146 {
147         dentry->d_op = &sysfs_dentry_ops;
148         dentry->d_fsdata = sysfs_get(sd);
149
150         /* protect sd->s_dentry against sysfs_d_iput */
151         spin_lock(&sysfs_lock);
152         sd->s_dentry = dentry;
153         spin_unlock(&sysfs_lock);
154
155         d_rehash(dentry);
156 }
157
158 void sysfs_attach_dirent(struct sysfs_dirent *sd,
159                          struct sysfs_dirent *parent_sd, struct dentry *dentry)
160 {
161         if (dentry)
162                 sysfs_attach_dentry(sd, dentry);
163
164         if (parent_sd) {
165                 sd->s_parent = sysfs_get(parent_sd);
166                 list_add(&sd->s_sibling, &parent_sd->s_children);
167         }
168 }
169
170 /*
171  *
172  * Return -EEXIST if there is already a sysfs element with the same name for
173  * the same parent.
174  *
175  * called with parent inode's i_mutex held
176  */
177 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
178                           const unsigned char *new)
179 {
180         struct sysfs_dirent * sd;
181
182         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
183                 if (sd->s_type) {
184                         if (strcmp(sd->s_name, new))
185                                 continue;
186                         else
187                                 return -EEXIST;
188                 }
189         }
190
191         return 0;
192 }
193
194 static int init_dir(struct inode * inode)
195 {
196         inode->i_op = &sysfs_dir_inode_operations;
197         inode->i_fop = &sysfs_dir_operations;
198
199         /* directory inodes start off with i_nlink == 2 (for "." entry) */
200         inc_nlink(inode);
201         return 0;
202 }
203
204 static int init_file(struct inode * inode)
205 {
206         inode->i_size = PAGE_SIZE;
207         inode->i_fop = &sysfs_file_operations;
208         return 0;
209 }
210
211 static int init_symlink(struct inode * inode)
212 {
213         inode->i_op = &sysfs_symlink_inode_operations;
214         return 0;
215 }
216
217 static int create_dir(struct kobject *kobj, struct dentry *parent,
218                       const char *name, struct dentry **p_dentry)
219 {
220         int error;
221         umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
222         struct dentry *dentry;
223         struct sysfs_dirent *sd;
224
225         mutex_lock(&parent->d_inode->i_mutex);
226
227         dentry = lookup_one_len(name, parent, strlen(name));
228         if (IS_ERR(dentry)) {
229                 error = PTR_ERR(dentry);
230                 goto out_unlock;
231         }
232
233         error = -EEXIST;
234         if (sysfs_dirent_exist(parent->d_fsdata, name))
235                 goto out_dput;
236
237         error = -ENOMEM;
238         sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
239         if (!sd)
240                 goto out_drop;
241         sd->s_elem.dir.kobj = kobj;
242
243         error = sysfs_create(sd, dentry, mode, init_dir);
244         if (error)
245                 goto out_sput;
246
247         inc_nlink(parent->d_inode);
248         sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
249
250         *p_dentry = dentry;
251         error = 0;
252         goto out_dput;
253
254  out_sput:
255         list_del_init(&sd->s_sibling);
256         sysfs_put(sd);
257  out_drop:
258         d_drop(dentry);
259  out_dput:
260         dput(dentry);
261  out_unlock:
262         mutex_unlock(&parent->d_inode->i_mutex);
263         return error;
264 }
265
266
267 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
268 {
269         return create_dir(k,k->dentry,n,d);
270 }
271
272 /**
273  *      sysfs_create_dir - create a directory for an object.
274  *      @kobj:          object we're creating directory for. 
275  *      @shadow_parent: parent parent object.
276  */
277
278 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
279 {
280         struct dentry * dentry = NULL;
281         struct dentry * parent;
282         int error = 0;
283
284         BUG_ON(!kobj);
285
286         if (shadow_parent)
287                 parent = shadow_parent;
288         else if (kobj->parent)
289                 parent = kobj->parent->dentry;
290         else if (sysfs_mount && sysfs_mount->mnt_sb)
291                 parent = sysfs_mount->mnt_sb->s_root;
292         else
293                 return -EFAULT;
294
295         error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
296         if (!error)
297                 kobj->dentry = dentry;
298         return error;
299 }
300
301 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
302  * attribute file
303  */
304 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
305 {
306         struct attribute * attr = NULL;
307         struct bin_attribute * bin_attr = NULL;
308         int (* init) (struct inode *) = NULL;
309         int error = 0;
310
311         if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
312                 bin_attr = sd->s_elem.bin_attr.bin_attr;
313                 attr = &bin_attr->attr;
314         } else {
315                 attr = sd->s_elem.attr.attr;
316                 init = init_file;
317         }
318
319         error = sysfs_create(sd, dentry,
320                              (attr->mode & S_IALLUGO) | S_IFREG, init);
321         if (error)
322                 return error;
323
324         if (bin_attr) {
325                 dentry->d_inode->i_size = bin_attr->size;
326                 dentry->d_inode->i_fop = &bin_fops;
327         }
328
329         sysfs_attach_dentry(sd, dentry);
330
331         return 0;
332 }
333
334 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
335 {
336         int err;
337
338         err = sysfs_create(sd, dentry, S_IFLNK|S_IRWXUGO, init_symlink);
339         if (!err)
340                 sysfs_attach_dentry(sd, dentry);
341
342         return err;
343 }
344
345 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
346                                 struct nameidata *nd)
347 {
348         struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
349         struct sysfs_dirent * sd;
350         int err = 0;
351
352         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
353                 if (sd->s_type & SYSFS_NOT_PINNED) {
354                         if (strcmp(sd->s_name, dentry->d_name.name))
355                                 continue;
356
357                         if (sd->s_type & SYSFS_KOBJ_LINK)
358                                 err = sysfs_attach_link(sd, dentry);
359                         else
360                                 err = sysfs_attach_attr(sd, dentry);
361                         break;
362                 }
363         }
364
365         return ERR_PTR(err);
366 }
367
368 const struct inode_operations sysfs_dir_inode_operations = {
369         .lookup         = sysfs_lookup,
370         .setattr        = sysfs_setattr,
371 };
372
373 static void remove_dir(struct dentry * d)
374 {
375         struct dentry * parent = dget(d->d_parent);
376         struct sysfs_dirent * sd;
377
378         mutex_lock(&parent->d_inode->i_mutex);
379         d_delete(d);
380         sd = d->d_fsdata;
381         list_del_init(&sd->s_sibling);
382         if (d->d_inode)
383                 simple_rmdir(parent->d_inode,d);
384
385         pr_debug(" o %s removing done (%d)\n",d->d_name.name,
386                  atomic_read(&d->d_count));
387
388         mutex_unlock(&parent->d_inode->i_mutex);
389         dput(parent);
390
391         sysfs_deactivate(sd);
392         sysfs_put(sd);
393 }
394
395 void sysfs_remove_subdir(struct dentry * d)
396 {
397         remove_dir(d);
398 }
399
400
401 static void __sysfs_remove_dir(struct dentry *dentry)
402 {
403         LIST_HEAD(removed);
404         struct sysfs_dirent * parent_sd;
405         struct sysfs_dirent * sd, * tmp;
406
407         dget(dentry);
408         if (!dentry)
409                 return;
410
411         pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
412         mutex_lock(&dentry->d_inode->i_mutex);
413         parent_sd = dentry->d_fsdata;
414         list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
415                 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
416                         continue;
417                 list_move(&sd->s_sibling, &removed);
418                 sysfs_drop_dentry(sd, dentry);
419         }
420         mutex_unlock(&dentry->d_inode->i_mutex);
421
422         list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
423                 list_del_init(&sd->s_sibling);
424                 sysfs_deactivate(sd);
425                 sysfs_put(sd);
426         }
427
428         remove_dir(dentry);
429         /**
430          * Drop reference from dget() on entrance.
431          */
432         dput(dentry);
433 }
434
435 /**
436  *      sysfs_remove_dir - remove an object's directory.
437  *      @kobj:  object.
438  *
439  *      The only thing special about this is that we remove any files in
440  *      the directory before we remove the directory, and we've inlined
441  *      what used to be sysfs_rmdir() below, instead of calling separately.
442  */
443
444 void sysfs_remove_dir(struct kobject * kobj)
445 {
446         struct dentry *d = kobj->dentry;
447
448         spin_lock(&kobj_sysfs_assoc_lock);
449         kobj->dentry = NULL;
450         spin_unlock(&kobj_sysfs_assoc_lock);
451
452         __sysfs_remove_dir(d);
453 }
454
455 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
456                      const char *new_name)
457 {
458         struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
459         struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
460         struct dentry *new_dentry;
461         char *dup_name;
462         int error;
463
464         if (!new_parent)
465                 return -EFAULT;
466
467         down_write(&sysfs_rename_sem);
468         mutex_lock(&new_parent->d_inode->i_mutex);
469
470         new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
471         if (IS_ERR(new_dentry)) {
472                 error = PTR_ERR(new_dentry);
473                 goto out_unlock;
474         }
475
476         /* By allowing two different directories with the same
477          * d_parent we allow this routine to move between different
478          * shadows of the same directory
479          */
480         error = -EINVAL;
481         if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
482             new_dentry->d_parent->d_inode != new_parent->d_inode ||
483             new_dentry == kobj->dentry)
484                 goto out_dput;
485
486         error = -EEXIST;
487         if (new_dentry->d_inode)
488                 goto out_dput;
489
490         /* rename kobject and sysfs_dirent */
491         error = -ENOMEM;
492         new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
493         if (!new_name)
494                 goto out_drop;
495
496         error = kobject_set_name(kobj, "%s", new_name);
497         if (error)
498                 goto out_free;
499
500         kfree(sd->s_name);
501         sd->s_name = new_name;
502
503         /* move under the new parent */
504         d_add(new_dentry, NULL);
505         d_move(kobj->dentry, new_dentry);
506
507         list_del_init(&sd->s_sibling);
508         list_add(&sd->s_sibling, &parent_sd->s_children);
509
510         error = 0;
511         goto out_unlock;
512
513  out_free:
514         kfree(dup_name);
515  out_drop:
516         d_drop(new_dentry);
517  out_dput:
518         dput(new_dentry);
519  out_unlock:
520         mutex_unlock(&new_parent->d_inode->i_mutex);
521         up_write(&sysfs_rename_sem);
522         return error;
523 }
524
525 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
526 {
527         struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
528         struct sysfs_dirent *new_parent_sd, *sd;
529         int error;
530
531         old_parent_dentry = kobj->parent ?
532                 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
533         new_parent_dentry = new_parent ?
534                 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
535
536         if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
537                 return 0;       /* nothing to move */
538 again:
539         mutex_lock(&old_parent_dentry->d_inode->i_mutex);
540         if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
541                 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
542                 goto again;
543         }
544
545         new_parent_sd = new_parent_dentry->d_fsdata;
546         sd = kobj->dentry->d_fsdata;
547
548         new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
549                                     strlen(kobj->name));
550         if (IS_ERR(new_dentry)) {
551                 error = PTR_ERR(new_dentry);
552                 goto out;
553         } else
554                 error = 0;
555         d_add(new_dentry, NULL);
556         d_move(kobj->dentry, new_dentry);
557         dput(new_dentry);
558
559         /* Remove from old parent's list and insert into new parent's list. */
560         list_del_init(&sd->s_sibling);
561         list_add(&sd->s_sibling, &new_parent_sd->s_children);
562
563 out:
564         mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
565         mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
566
567         return error;
568 }
569
570 static int sysfs_dir_open(struct inode *inode, struct file *file)
571 {
572         struct dentry * dentry = file->f_path.dentry;
573         struct sysfs_dirent * parent_sd = dentry->d_fsdata;
574         struct sysfs_dirent * sd;
575
576         mutex_lock(&dentry->d_inode->i_mutex);
577         sd = sysfs_new_dirent("_DIR_", 0, 0);
578         if (sd)
579                 sysfs_attach_dirent(sd, parent_sd, NULL);
580         mutex_unlock(&dentry->d_inode->i_mutex);
581
582         file->private_data = sd;
583         return sd ? 0 : -ENOMEM;
584 }
585
586 static int sysfs_dir_close(struct inode *inode, struct file *file)
587 {
588         struct dentry * dentry = file->f_path.dentry;
589         struct sysfs_dirent * cursor = file->private_data;
590
591         mutex_lock(&dentry->d_inode->i_mutex);
592         list_del_init(&cursor->s_sibling);
593         mutex_unlock(&dentry->d_inode->i_mutex);
594
595         release_sysfs_dirent(cursor);
596
597         return 0;
598 }
599
600 /* Relationship between s_mode and the DT_xxx types */
601 static inline unsigned char dt_type(struct sysfs_dirent *sd)
602 {
603         return (sd->s_mode >> 12) & 15;
604 }
605
606 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
607 {
608         struct dentry *dentry = filp->f_path.dentry;
609         struct sysfs_dirent * parent_sd = dentry->d_fsdata;
610         struct sysfs_dirent *cursor = filp->private_data;
611         struct list_head *p, *q = &cursor->s_sibling;
612         ino_t ino;
613         int i = filp->f_pos;
614
615         switch (i) {
616                 case 0:
617                         ino = parent_sd->s_ino;
618                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
619                                 break;
620                         filp->f_pos++;
621                         i++;
622                         /* fallthrough */
623                 case 1:
624                         if (parent_sd->s_parent)
625                                 ino = parent_sd->s_parent->s_ino;
626                         else
627                                 ino = parent_sd->s_ino;
628                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
629                                 break;
630                         filp->f_pos++;
631                         i++;
632                         /* fallthrough */
633                 default:
634                         if (filp->f_pos == 2)
635                                 list_move(q, &parent_sd->s_children);
636
637                         for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
638                                 struct sysfs_dirent *next;
639                                 const char * name;
640                                 int len;
641
642                                 next = list_entry(p, struct sysfs_dirent,
643                                                    s_sibling);
644                                 if (!next->s_type)
645                                         continue;
646
647                                 name = next->s_name;
648                                 len = strlen(name);
649                                 ino = next->s_ino;
650
651                                 if (filldir(dirent, name, len, filp->f_pos, ino,
652                                                  dt_type(next)) < 0)
653                                         return 0;
654
655                                 list_move(q, p);
656                                 p = q;
657                                 filp->f_pos++;
658                         }
659         }
660         return 0;
661 }
662
663 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
664 {
665         struct dentry * dentry = file->f_path.dentry;
666
667         mutex_lock(&dentry->d_inode->i_mutex);
668         switch (origin) {
669                 case 1:
670                         offset += file->f_pos;
671                 case 0:
672                         if (offset >= 0)
673                                 break;
674                 default:
675                         mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
676                         return -EINVAL;
677         }
678         if (offset != file->f_pos) {
679                 file->f_pos = offset;
680                 if (file->f_pos >= 2) {
681                         struct sysfs_dirent *sd = dentry->d_fsdata;
682                         struct sysfs_dirent *cursor = file->private_data;
683                         struct list_head *p;
684                         loff_t n = file->f_pos - 2;
685
686                         list_del(&cursor->s_sibling);
687                         p = sd->s_children.next;
688                         while (n && p != &sd->s_children) {
689                                 struct sysfs_dirent *next;
690                                 next = list_entry(p, struct sysfs_dirent,
691                                                    s_sibling);
692                                 if (next->s_type)
693                                         n--;
694                                 p = p->next;
695                         }
696                         list_add_tail(&cursor->s_sibling, p);
697                 }
698         }
699         mutex_unlock(&dentry->d_inode->i_mutex);
700         return offset;
701 }
702
703
704 /**
705  *      sysfs_make_shadowed_dir - Setup so a directory can be shadowed
706  *      @kobj:  object we're creating shadow of.
707  */
708
709 int sysfs_make_shadowed_dir(struct kobject *kobj,
710         void * (*follow_link)(struct dentry *, struct nameidata *))
711 {
712         struct inode *inode;
713         struct inode_operations *i_op;
714
715         inode = kobj->dentry->d_inode;
716         if (inode->i_op != &sysfs_dir_inode_operations)
717                 return -EINVAL;
718
719         i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
720         if (!i_op)
721                 return -ENOMEM;
722
723         memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
724         i_op->follow_link = follow_link;
725
726         /* Locking of inode->i_op?
727          * Since setting i_op is a single word write and they
728          * are atomic we should be ok here.
729          */
730         inode->i_op = i_op;
731         return 0;
732 }
733
734 /**
735  *      sysfs_create_shadow_dir - create a shadow directory for an object.
736  *      @kobj:  object we're creating directory for.
737  *
738  *      sysfs_make_shadowed_dir must already have been called on this
739  *      directory.
740  */
741
742 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
743 {
744         struct dentry *dir = kobj->dentry;
745         struct inode *inode = dir->d_inode;
746         struct dentry *parent = dir->d_parent;
747         struct sysfs_dirent *parent_sd = parent->d_fsdata;
748         struct dentry *shadow;
749         struct sysfs_dirent *sd;
750
751         shadow = ERR_PTR(-EINVAL);
752         if (!sysfs_is_shadowed_inode(inode))
753                 goto out;
754
755         shadow = d_alloc(parent, &dir->d_name);
756         if (!shadow)
757                 goto nomem;
758
759         sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
760         if (!sd)
761                 goto nomem;
762         sd->s_elem.dir.kobj = kobj;
763         /* point to parent_sd but don't attach to it */
764         sd->s_parent = sysfs_get(parent_sd);
765         sysfs_attach_dirent(sd, NULL, shadow);
766
767         d_instantiate(shadow, igrab(inode));
768         inc_nlink(inode);
769         inc_nlink(parent->d_inode);
770
771         dget(shadow);           /* Extra count - pin the dentry in core */
772
773 out:
774         return shadow;
775 nomem:
776         dput(shadow);
777         shadow = ERR_PTR(-ENOMEM);
778         goto out;
779 }
780
781 /**
782  *      sysfs_remove_shadow_dir - remove an object's directory.
783  *      @shadow: dentry of shadow directory
784  *
785  *      The only thing special about this is that we remove any files in
786  *      the directory before we remove the directory, and we've inlined
787  *      what used to be sysfs_rmdir() below, instead of calling separately.
788  */
789
790 void sysfs_remove_shadow_dir(struct dentry *shadow)
791 {
792         __sysfs_remove_dir(shadow);
793 }
794
795 const struct file_operations sysfs_dir_operations = {
796         .open           = sysfs_dir_open,
797         .release        = sysfs_dir_close,
798         .llseek         = sysfs_dir_lseek,
799         .read           = generic_read_dir,
800         .readdir        = sysfs_readdir,
801 };