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