sysfs: In sysfs_lookup don't open code sysfs_find_dirent
[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 <linux/completion.h>
14 #include <linux/mutex.h>
15 #include "sysfs.h"
16
17 DEFINE_MUTEX(sysfs_mutex);
18 spinlock_t 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 /**
24  *      sysfs_link_sibling - link sysfs_dirent into sibling list
25  *      @sd: sysfs_dirent of interest
26  *
27  *      Link @sd into its sibling list which starts from
28  *      sd->s_parent->s_children.
29  *
30  *      Locking:
31  *      mutex_lock(sysfs_mutex)
32  */
33 static void sysfs_link_sibling(struct sysfs_dirent *sd)
34 {
35         struct sysfs_dirent *parent_sd = sd->s_parent;
36
37         BUG_ON(sd->s_sibling);
38         sd->s_sibling = parent_sd->s_children;
39         parent_sd->s_children = sd;
40 }
41
42 /**
43  *      sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
44  *      @sd: sysfs_dirent of interest
45  *
46  *      Unlink @sd from its sibling list which starts from
47  *      sd->s_parent->s_children.
48  *
49  *      Locking:
50  *      mutex_lock(sysfs_mutex)
51  */
52 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
53 {
54         struct sysfs_dirent **pos;
55
56         for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
57                 if (*pos == sd) {
58                         *pos = sd->s_sibling;
59                         sd->s_sibling = NULL;
60                         break;
61                 }
62         }
63 }
64
65 /**
66  *      sysfs_get_dentry - get dentry for the given sysfs_dirent
67  *      @sd: sysfs_dirent of interest
68  *
69  *      Get dentry for @sd.  Dentry is looked up if currently not
70  *      present.  This function climbs sysfs_dirent tree till it
71  *      reaches a sysfs_dirent with valid dentry attached and descends
72  *      down from there looking up dentry for each step.
73  *
74  *      LOCKING:
75  *      Kernel thread context (may sleep)
76  *
77  *      RETURNS:
78  *      Pointer to found dentry on success, ERR_PTR() value on error.
79  */
80 struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
81 {
82         struct sysfs_dirent *cur;
83         struct dentry *parent_dentry, *dentry;
84         int i, depth;
85
86         /* Find the first parent which has valid s_dentry and get the
87          * dentry.
88          */
89         mutex_lock(&sysfs_mutex);
90  restart0:
91         spin_lock(&sysfs_assoc_lock);
92  restart1:
93         spin_lock(&dcache_lock);
94
95         dentry = NULL;
96         depth = 0;
97         cur = sd;
98         while (!cur->s_dentry || !cur->s_dentry->d_inode) {
99                 if (cur->s_flags & SYSFS_FLAG_REMOVED) {
100                         dentry = ERR_PTR(-ENOENT);
101                         depth = 0;
102                         break;
103                 }
104                 cur = cur->s_parent;
105                 depth++;
106         }
107         if (!IS_ERR(dentry))
108                 dentry = dget_locked(cur->s_dentry);
109
110         spin_unlock(&dcache_lock);
111         spin_unlock(&sysfs_assoc_lock);
112
113         /* from the found dentry, look up depth times */
114         while (depth--) {
115                 /* find and get depth'th ancestor */
116                 for (cur = sd, i = 0; cur && i < depth; i++)
117                         cur = cur->s_parent;
118
119                 /* This can happen if tree structure was modified due
120                  * to move/rename.  Restart.
121                  */
122                 if (i != depth) {
123                         dput(dentry);
124                         goto restart0;
125                 }
126
127                 sysfs_get(cur);
128
129                 mutex_unlock(&sysfs_mutex);
130
131                 /* look it up */
132                 parent_dentry = dentry;
133                 mutex_lock(&parent_dentry->d_inode->i_mutex);
134                 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
135                                              strlen(cur->s_name));
136                 mutex_unlock(&parent_dentry->d_inode->i_mutex);
137                 dput(parent_dentry);
138
139                 if (IS_ERR(dentry)) {
140                         sysfs_put(cur);
141                         return dentry;
142                 }
143
144                 mutex_lock(&sysfs_mutex);
145                 spin_lock(&sysfs_assoc_lock);
146
147                 /* This, again, can happen if tree structure has
148                  * changed and we looked up the wrong thing.  Restart.
149                  */
150                 if (cur->s_dentry != dentry) {
151                         dput(dentry);
152                         sysfs_put(cur);
153                         goto restart1;
154                 }
155
156                 spin_unlock(&sysfs_assoc_lock);
157
158                 sysfs_put(cur);
159         }
160
161         mutex_unlock(&sysfs_mutex);
162         return dentry;
163 }
164
165 /**
166  *      sysfs_get_active - get an active reference to sysfs_dirent
167  *      @sd: sysfs_dirent to get an active reference to
168  *
169  *      Get an active reference of @sd.  This function is noop if @sd
170  *      is NULL.
171  *
172  *      RETURNS:
173  *      Pointer to @sd on success, NULL on failure.
174  */
175 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
176 {
177         if (unlikely(!sd))
178                 return NULL;
179
180         while (1) {
181                 int v, t;
182
183                 v = atomic_read(&sd->s_active);
184                 if (unlikely(v < 0))
185                         return NULL;
186
187                 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
188                 if (likely(t == v))
189                         return sd;
190                 if (t < 0)
191                         return NULL;
192
193                 cpu_relax();
194         }
195 }
196
197 /**
198  *      sysfs_put_active - put an active reference to sysfs_dirent
199  *      @sd: sysfs_dirent to put an active reference to
200  *
201  *      Put an active reference to @sd.  This function is noop if @sd
202  *      is NULL.
203  */
204 void sysfs_put_active(struct sysfs_dirent *sd)
205 {
206         struct completion *cmpl;
207         int v;
208
209         if (unlikely(!sd))
210                 return;
211
212         v = atomic_dec_return(&sd->s_active);
213         if (likely(v != SD_DEACTIVATED_BIAS))
214                 return;
215
216         /* atomic_dec_return() is a mb(), we'll always see the updated
217          * sd->s_sibling.
218          */
219         cmpl = (void *)sd->s_sibling;
220         complete(cmpl);
221 }
222
223 /**
224  *      sysfs_get_active_two - get active references to sysfs_dirent and parent
225  *      @sd: sysfs_dirent of interest
226  *
227  *      Get active reference to @sd and its parent.  Parent's active
228  *      reference is grabbed first.  This function is noop if @sd is
229  *      NULL.
230  *
231  *      RETURNS:
232  *      Pointer to @sd on success, NULL on failure.
233  */
234 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
235 {
236         if (sd) {
237                 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
238                         return NULL;
239                 if (unlikely(!sysfs_get_active(sd))) {
240                         sysfs_put_active(sd->s_parent);
241                         return NULL;
242                 }
243         }
244         return sd;
245 }
246
247 /**
248  *      sysfs_put_active_two - put active references to sysfs_dirent and parent
249  *      @sd: sysfs_dirent of interest
250  *
251  *      Put active references to @sd and its parent.  This function is
252  *      noop if @sd is NULL.
253  */
254 void sysfs_put_active_two(struct sysfs_dirent *sd)
255 {
256         if (sd) {
257                 sysfs_put_active(sd);
258                 sysfs_put_active(sd->s_parent);
259         }
260 }
261
262 /**
263  *      sysfs_deactivate - deactivate sysfs_dirent
264  *      @sd: sysfs_dirent to deactivate
265  *
266  *      Deny new active references and drain existing ones.
267  */
268 static void sysfs_deactivate(struct sysfs_dirent *sd)
269 {
270         DECLARE_COMPLETION_ONSTACK(wait);
271         int v;
272
273         BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
274         sd->s_sibling = (void *)&wait;
275
276         /* atomic_add_return() is a mb(), put_active() will always see
277          * the updated sd->s_sibling.
278          */
279         v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
280
281         if (v != SD_DEACTIVATED_BIAS)
282                 wait_for_completion(&wait);
283
284         sd->s_sibling = NULL;
285 }
286
287 static int sysfs_alloc_ino(ino_t *pino)
288 {
289         int ino, rc;
290
291  retry:
292         spin_lock(&sysfs_ino_lock);
293         rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
294         spin_unlock(&sysfs_ino_lock);
295
296         if (rc == -EAGAIN) {
297                 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
298                         goto retry;
299                 rc = -ENOMEM;
300         }
301
302         *pino = ino;
303         return rc;
304 }
305
306 static void sysfs_free_ino(ino_t ino)
307 {
308         spin_lock(&sysfs_ino_lock);
309         ida_remove(&sysfs_ino_ida, ino);
310         spin_unlock(&sysfs_ino_lock);
311 }
312
313 void release_sysfs_dirent(struct sysfs_dirent * sd)
314 {
315         struct sysfs_dirent *parent_sd;
316
317  repeat:
318         /* Moving/renaming is always done while holding reference.
319          * sd->s_parent won't change beneath us.
320          */
321         parent_sd = sd->s_parent;
322
323         if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
324                 sysfs_put(sd->s_elem.symlink.target_sd);
325         if (sysfs_type(sd) & SYSFS_COPY_NAME)
326                 kfree(sd->s_name);
327         kfree(sd->s_iattr);
328         sysfs_free_ino(sd->s_ino);
329         kmem_cache_free(sysfs_dir_cachep, sd);
330
331         sd = parent_sd;
332         if (sd && atomic_dec_and_test(&sd->s_count))
333                 goto repeat;
334 }
335
336 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
337 {
338         struct sysfs_dirent * sd = dentry->d_fsdata;
339
340         if (sd) {
341                 /* sd->s_dentry is protected with sysfs_assoc_lock.
342                  * This allows sysfs_drop_dentry() to dereference it.
343                  */
344                 spin_lock(&sysfs_assoc_lock);
345
346                 /* The dentry might have been deleted or another
347                  * lookup could have happened updating sd->s_dentry to
348                  * point the new dentry.  Ignore if it isn't pointing
349                  * to this dentry.
350                  */
351                 if (sd->s_dentry == dentry)
352                         sd->s_dentry = NULL;
353                 spin_unlock(&sysfs_assoc_lock);
354                 sysfs_put(sd);
355         }
356         iput(inode);
357 }
358
359 static struct dentry_operations sysfs_dentry_ops = {
360         .d_iput         = sysfs_d_iput,
361 };
362
363 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
364 {
365         char *dup_name = NULL;
366         struct sysfs_dirent *sd;
367
368         if (type & SYSFS_COPY_NAME) {
369                 name = dup_name = kstrdup(name, GFP_KERNEL);
370                 if (!name)
371                         return NULL;
372         }
373
374         sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
375         if (!sd)
376                 goto err_out1;
377
378         if (sysfs_alloc_ino(&sd->s_ino))
379                 goto err_out2;
380
381         atomic_set(&sd->s_count, 1);
382         atomic_set(&sd->s_active, 0);
383         atomic_set(&sd->s_event, 1);
384
385         sd->s_name = name;
386         sd->s_mode = mode;
387         sd->s_flags = type;
388
389         return sd;
390
391  err_out2:
392         kmem_cache_free(sysfs_dir_cachep, sd);
393  err_out1:
394         kfree(dup_name);
395         return NULL;
396 }
397
398 /**
399  *      sysfs_attach_dentry - associate sysfs_dirent with dentry
400  *      @sd: target sysfs_dirent
401  *      @dentry: dentry to associate
402  *
403  *      Associate @sd with @dentry.  This is protected by
404  *      sysfs_assoc_lock to avoid race with sysfs_d_iput().
405  *
406  *      LOCKING:
407  *      mutex_lock(sysfs_mutex)
408  */
409 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
410 {
411         dentry->d_op = &sysfs_dentry_ops;
412         dentry->d_fsdata = sysfs_get(sd);
413
414         /* protect sd->s_dentry against sysfs_d_iput */
415         spin_lock(&sysfs_assoc_lock);
416         sd->s_dentry = dentry;
417         spin_unlock(&sysfs_assoc_lock);
418
419         d_rehash(dentry);
420 }
421
422 static int sysfs_ilookup_test(struct inode *inode, void *arg)
423 {
424         struct sysfs_dirent *sd = arg;
425         return inode->i_ino == sd->s_ino;
426 }
427
428 /**
429  *      sysfs_addrm_start - prepare for sysfs_dirent add/remove
430  *      @acxt: pointer to sysfs_addrm_cxt to be used
431  *      @parent_sd: parent sysfs_dirent
432  *
433  *      This function is called when the caller is about to add or
434  *      remove sysfs_dirent under @parent_sd.  This function acquires
435  *      sysfs_mutex, grabs inode for @parent_sd if available and lock
436  *      i_mutex of it.  @acxt is used to keep and pass context to
437  *      other addrm functions.
438  *
439  *      LOCKING:
440  *      Kernel thread context (may sleep).  sysfs_mutex is locked on
441  *      return.  i_mutex of parent inode is locked on return if
442  *      available.
443  */
444 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
445                        struct sysfs_dirent *parent_sd)
446 {
447         struct inode *inode;
448
449         memset(acxt, 0, sizeof(*acxt));
450         acxt->parent_sd = parent_sd;
451
452         /* Lookup parent inode.  inode initialization and I_NEW
453          * clearing are protected by sysfs_mutex.  By grabbing it and
454          * looking up with _nowait variant, inode state can be
455          * determined reliably.
456          */
457         mutex_lock(&sysfs_mutex);
458
459         inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
460                                 parent_sd);
461
462         if (inode && !(inode->i_state & I_NEW)) {
463                 /* parent inode available */
464                 acxt->parent_inode = inode;
465
466                 /* sysfs_mutex is below i_mutex in lock hierarchy.
467                  * First, trylock i_mutex.  If fails, unlock
468                  * sysfs_mutex and lock them in order.
469                  */
470                 if (!mutex_trylock(&inode->i_mutex)) {
471                         mutex_unlock(&sysfs_mutex);
472                         mutex_lock(&inode->i_mutex);
473                         mutex_lock(&sysfs_mutex);
474                 }
475         } else
476                 iput(inode);
477 }
478
479 /**
480  *      sysfs_add_one - add sysfs_dirent to parent
481  *      @acxt: addrm context to use
482  *      @sd: sysfs_dirent to be added
483  *
484  *      Get @acxt->parent_sd and set sd->s_parent to it and increment
485  *      nlink of parent inode if @sd is a directory.  @sd is NOT
486  *      linked into the children list of the parent.  The caller
487  *      should invoke sysfs_link_sibling() after this function
488  *      completes if @sd needs to be on the children list.
489  *
490  *      This function should be called between calls to
491  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
492  *      passed the same @acxt as passed to sysfs_addrm_start().
493  *
494  *      LOCKING:
495  *      Determined by sysfs_addrm_start().
496  *
497  *      RETURNS:
498  *      0 on success, -EEXIST if entry with the given name already
499  *      exists.
500  */
501 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
502 {
503         if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
504                 return -EEXIST;
505
506         sd->s_parent = sysfs_get(acxt->parent_sd);
507
508         if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
509                 inc_nlink(acxt->parent_inode);
510
511         acxt->cnt++;
512
513         sysfs_link_sibling(sd);
514
515         return 0;
516 }
517
518 /**
519  *      sysfs_remove_one - remove sysfs_dirent from parent
520  *      @acxt: addrm context to use
521  *      @sd: sysfs_dirent to be added
522  *
523  *      Mark @sd removed and drop nlink of parent inode if @sd is a
524  *      directory.  @sd is NOT unlinked from the children list of the
525  *      parent.  The caller is repsonsible for removing @sd from the
526  *      children list before calling this function.
527  *
528  *      This function should be called between calls to
529  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
530  *      passed the same @acxt as passed to sysfs_addrm_start().
531  *
532  *      LOCKING:
533  *      Determined by sysfs_addrm_start().
534  */
535 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
536 {
537         BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
538
539         sysfs_unlink_sibling(sd);
540
541         sd->s_flags |= SYSFS_FLAG_REMOVED;
542         sd->s_sibling = acxt->removed;
543         acxt->removed = sd;
544
545         if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
546                 drop_nlink(acxt->parent_inode);
547
548         acxt->cnt++;
549 }
550
551 /**
552  *      sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
553  *      @sd: target sysfs_dirent
554  *
555  *      Drop dentry for @sd.  @sd must have been unlinked from its
556  *      parent on entry to this function such that it can't be looked
557  *      up anymore.
558  *
559  *      @sd->s_dentry which is protected with sysfs_assoc_lock points
560  *      to the currently associated dentry but we're not holding a
561  *      reference to it and racing with dput().  Grab dcache_lock and
562  *      verify dentry before dropping it.  If @sd->s_dentry is NULL or
563  *      dput() beats us, no need to bother.
564  */
565 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
566 {
567         struct dentry *dentry = NULL;
568         struct inode *inode;
569
570         /* We're not holding a reference to ->s_dentry dentry but the
571          * field will stay valid as long as sysfs_assoc_lock is held.
572          */
573         spin_lock(&sysfs_assoc_lock);
574         spin_lock(&dcache_lock);
575
576         /* drop dentry if it's there and dput() didn't kill it yet */
577         if (sd->s_dentry && sd->s_dentry->d_inode) {
578                 dentry = dget_locked(sd->s_dentry);
579                 spin_lock(&dentry->d_lock);
580                 __d_drop(dentry);
581                 spin_unlock(&dentry->d_lock);
582         }
583
584         spin_unlock(&dcache_lock);
585         spin_unlock(&sysfs_assoc_lock);
586
587         dput(dentry);
588
589         /* adjust nlink and update timestamp */
590         inode = ilookup(sysfs_sb, sd->s_ino);
591         if (inode) {
592                 mutex_lock(&inode->i_mutex);
593
594                 inode->i_ctime = CURRENT_TIME;
595                 drop_nlink(inode);
596                 if (sysfs_type(sd) == SYSFS_DIR)
597                         drop_nlink(inode);
598
599                 mutex_unlock(&inode->i_mutex);
600                 iput(inode);
601         }
602 }
603
604 /**
605  *      sysfs_addrm_finish - finish up sysfs_dirent add/remove
606  *      @acxt: addrm context to finish up
607  *
608  *      Finish up sysfs_dirent add/remove.  Resources acquired by
609  *      sysfs_addrm_start() are released and removed sysfs_dirents are
610  *      cleaned up.  Timestamps on the parent inode are updated.
611  *
612  *      LOCKING:
613  *      All mutexes acquired by sysfs_addrm_start() are released.
614  */
615 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
616 {
617         /* release resources acquired by sysfs_addrm_start() */
618         mutex_unlock(&sysfs_mutex);
619         if (acxt->parent_inode) {
620                 struct inode *inode = acxt->parent_inode;
621
622                 /* if added/removed, update timestamps on the parent */
623                 if (acxt->cnt)
624                         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
625
626                 mutex_unlock(&inode->i_mutex);
627                 iput(inode);
628         }
629
630         /* kill removed sysfs_dirents */
631         while (acxt->removed) {
632                 struct sysfs_dirent *sd = acxt->removed;
633
634                 acxt->removed = sd->s_sibling;
635                 sd->s_sibling = NULL;
636
637                 sysfs_drop_dentry(sd);
638                 sysfs_deactivate(sd);
639                 sysfs_put(sd);
640         }
641 }
642
643 /**
644  *      sysfs_find_dirent - find sysfs_dirent with the given name
645  *      @parent_sd: sysfs_dirent to search under
646  *      @name: name to look for
647  *
648  *      Look for sysfs_dirent with name @name under @parent_sd.
649  *
650  *      LOCKING:
651  *      mutex_lock(sysfs_mutex)
652  *
653  *      RETURNS:
654  *      Pointer to sysfs_dirent if found, NULL if not.
655  */
656 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
657                                        const unsigned char *name)
658 {
659         struct sysfs_dirent *sd;
660
661         for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
662                 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
663                         return sd;
664         return NULL;
665 }
666
667 /**
668  *      sysfs_get_dirent - find and get sysfs_dirent with the given name
669  *      @parent_sd: sysfs_dirent to search under
670  *      @name: name to look for
671  *
672  *      Look for sysfs_dirent with name @name under @parent_sd and get
673  *      it if found.
674  *
675  *      LOCKING:
676  *      Kernel thread context (may sleep).  Grabs sysfs_mutex.
677  *
678  *      RETURNS:
679  *      Pointer to sysfs_dirent if found, NULL if not.
680  */
681 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
682                                       const unsigned char *name)
683 {
684         struct sysfs_dirent *sd;
685
686         mutex_lock(&sysfs_mutex);
687         sd = sysfs_find_dirent(parent_sd, name);
688         sysfs_get(sd);
689         mutex_unlock(&sysfs_mutex);
690
691         return sd;
692 }
693
694 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
695                       const char *name, struct sysfs_dirent **p_sd)
696 {
697         umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
698         struct sysfs_addrm_cxt acxt;
699         struct sysfs_dirent *sd;
700         int rc;
701
702         /* allocate */
703         sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
704         if (!sd)
705                 return -ENOMEM;
706         sd->s_elem.dir.kobj = kobj;
707
708         /* link in */
709         sysfs_addrm_start(&acxt, parent_sd);
710         rc = sysfs_add_one(&acxt, sd);
711         sysfs_addrm_finish(&acxt);
712
713         if (rc == 0)
714                 *p_sd = sd;
715         else
716                 sysfs_put(sd);
717
718         return rc;
719 }
720
721 int sysfs_create_subdir(struct kobject *kobj, const char *name,
722                         struct sysfs_dirent **p_sd)
723 {
724         return create_dir(kobj, kobj->sd, name, p_sd);
725 }
726
727 /**
728  *      sysfs_create_dir - create a directory for an object.
729  *      @kobj:          object we're creating directory for. 
730  */
731 int sysfs_create_dir(struct kobject * kobj)
732 {
733         struct sysfs_dirent *parent_sd, *sd;
734         int error = 0;
735
736         BUG_ON(!kobj);
737
738         if (kobj->parent)
739                 parent_sd = kobj->parent->sd;
740         else
741                 parent_sd = &sysfs_root;
742
743         error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
744         if (!error)
745                 kobj->sd = sd;
746         return error;
747 }
748
749 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
750                                 struct nameidata *nd)
751 {
752         struct dentry *ret = NULL;
753         struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
754         struct sysfs_dirent *sd;
755         struct inode *inode;
756
757         mutex_lock(&sysfs_mutex);
758
759         sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
760
761         /* no such entry */
762         if (!sd)
763                 goto out_unlock;
764
765         /* attach dentry and inode */
766         inode = sysfs_get_inode(sd);
767         if (!inode) {
768                 ret = ERR_PTR(-ENOMEM);
769                 goto out_unlock;
770         }
771
772         d_instantiate(dentry, inode);
773         sysfs_attach_dentry(sd, dentry);
774
775  out_unlock:
776         mutex_unlock(&sysfs_mutex);
777         return ret;
778 }
779
780 const struct inode_operations sysfs_dir_inode_operations = {
781         .lookup         = sysfs_lookup,
782         .setattr        = sysfs_setattr,
783 };
784
785 static void remove_dir(struct sysfs_dirent *sd)
786 {
787         struct sysfs_addrm_cxt acxt;
788
789         sysfs_addrm_start(&acxt, sd->s_parent);
790         sysfs_remove_one(&acxt, sd);
791         sysfs_addrm_finish(&acxt);
792 }
793
794 void sysfs_remove_subdir(struct sysfs_dirent *sd)
795 {
796         remove_dir(sd);
797 }
798
799
800 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
801 {
802         struct sysfs_addrm_cxt acxt;
803         struct sysfs_dirent **pos;
804
805         if (!dir_sd)
806                 return;
807
808         pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
809         sysfs_addrm_start(&acxt, dir_sd);
810         pos = &dir_sd->s_children;
811         while (*pos) {
812                 struct sysfs_dirent *sd = *pos;
813
814                 if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
815                         sysfs_remove_one(&acxt, sd);
816                 else
817                         pos = &(*pos)->s_sibling;
818         }
819         sysfs_addrm_finish(&acxt);
820
821         remove_dir(dir_sd);
822 }
823
824 /**
825  *      sysfs_remove_dir - remove an object's directory.
826  *      @kobj:  object.
827  *
828  *      The only thing special about this is that we remove any files in
829  *      the directory before we remove the directory, and we've inlined
830  *      what used to be sysfs_rmdir() below, instead of calling separately.
831  */
832
833 void sysfs_remove_dir(struct kobject * kobj)
834 {
835         struct sysfs_dirent *sd = kobj->sd;
836
837         spin_lock(&sysfs_assoc_lock);
838         kobj->sd = NULL;
839         spin_unlock(&sysfs_assoc_lock);
840
841         __sysfs_remove_dir(sd);
842 }
843
844 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
845 {
846         struct sysfs_dirent *sd;
847         struct dentry *parent = NULL;
848         struct dentry *old_dentry = NULL, *new_dentry = NULL;
849         const char *dup_name = NULL;
850         int error;
851
852         /* get the original dentry */
853         sd = kobj->sd;
854         old_dentry = sysfs_get_dentry(sd);
855         if (IS_ERR(old_dentry)) {
856                 error = PTR_ERR(old_dentry);
857                 goto out_dput;
858         }
859
860         parent = old_dentry->d_parent;
861
862         /* lock parent and get dentry for new name */
863         mutex_lock(&parent->d_inode->i_mutex);
864
865         new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
866         if (IS_ERR(new_dentry)) {
867                 error = PTR_ERR(new_dentry);
868                 goto out_unlock;
869         }
870
871         error = -EINVAL;
872         if (old_dentry == new_dentry)
873                 goto out_unlock;
874
875         error = -EEXIST;
876         if (new_dentry->d_inode)
877                 goto out_unlock;
878
879         /* rename kobject and sysfs_dirent */
880         error = -ENOMEM;
881         new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
882         if (!new_name)
883                 goto out_drop;
884
885         error = kobject_set_name(kobj, "%s", new_name);
886         if (error)
887                 goto out_drop;
888
889         mutex_lock(&sysfs_mutex);
890         dup_name = sd->s_name;
891         sd->s_name = new_name;
892         mutex_unlock(&sysfs_mutex);
893
894         /* rename */
895         d_add(new_dentry, NULL);
896         d_move(sd->s_dentry, new_dentry);
897
898         error = 0;
899         goto out_unlock;
900
901  out_drop:
902         d_drop(new_dentry);
903  out_unlock:
904         mutex_unlock(&parent->d_inode->i_mutex);
905  out_dput:
906         kfree(dup_name);
907         dput(old_dentry);
908         dput(new_dentry);
909         return error;
910 }
911
912 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
913 {
914         struct sysfs_dirent *sd = kobj->sd;
915         struct sysfs_dirent *new_parent_sd;
916         struct dentry *old_parent, *new_parent = NULL;
917         struct dentry *old_dentry = NULL, *new_dentry = NULL;
918         int error;
919
920         BUG_ON(!sd->s_parent);
921         new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
922
923         /* get dentries */
924         old_dentry = sysfs_get_dentry(sd);
925         if (IS_ERR(old_dentry)) {
926                 error = PTR_ERR(old_dentry);
927                 goto out_dput;
928         }
929         old_parent = sd->s_parent->s_dentry;
930
931         new_parent = sysfs_get_dentry(new_parent_sd);
932         if (IS_ERR(new_parent)) {
933                 error = PTR_ERR(new_parent);
934                 goto out_dput;
935         }
936
937         if (old_parent->d_inode == new_parent->d_inode) {
938                 error = 0;
939                 goto out_dput;  /* nothing to move */
940         }
941 again:
942         mutex_lock(&old_parent->d_inode->i_mutex);
943         if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
944                 mutex_unlock(&old_parent->d_inode->i_mutex);
945                 goto again;
946         }
947
948         new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
949         if (IS_ERR(new_dentry)) {
950                 error = PTR_ERR(new_dentry);
951                 goto out_unlock;
952         } else
953                 error = 0;
954         d_add(new_dentry, NULL);
955         d_move(sd->s_dentry, new_dentry);
956         dput(new_dentry);
957
958         /* Remove from old parent's list and insert into new parent's list. */
959         mutex_lock(&sysfs_mutex);
960
961         sysfs_unlink_sibling(sd);
962         sysfs_get(new_parent_sd);
963         sysfs_put(sd->s_parent);
964         sd->s_parent = new_parent_sd;
965         sysfs_link_sibling(sd);
966
967         mutex_unlock(&sysfs_mutex);
968
969  out_unlock:
970         mutex_unlock(&new_parent->d_inode->i_mutex);
971         mutex_unlock(&old_parent->d_inode->i_mutex);
972  out_dput:
973         dput(new_parent);
974         dput(old_dentry);
975         dput(new_dentry);
976         return error;
977 }
978
979 static int sysfs_dir_open(struct inode *inode, struct file *file)
980 {
981         struct dentry * dentry = file->f_path.dentry;
982         struct sysfs_dirent * parent_sd = dentry->d_fsdata;
983         struct sysfs_dirent * sd;
984
985         sd = sysfs_new_dirent("_DIR_", 0, 0);
986         if (sd) {
987                 mutex_lock(&sysfs_mutex);
988                 sd->s_parent = sysfs_get(parent_sd);
989                 sysfs_link_sibling(sd);
990                 mutex_unlock(&sysfs_mutex);
991         }
992
993         file->private_data = sd;
994         return sd ? 0 : -ENOMEM;
995 }
996
997 static int sysfs_dir_close(struct inode *inode, struct file *file)
998 {
999         struct sysfs_dirent * cursor = file->private_data;
1000
1001         mutex_lock(&sysfs_mutex);
1002         sysfs_unlink_sibling(cursor);
1003         mutex_unlock(&sysfs_mutex);
1004
1005         release_sysfs_dirent(cursor);
1006
1007         return 0;
1008 }
1009
1010 /* Relationship between s_mode and the DT_xxx types */
1011 static inline unsigned char dt_type(struct sysfs_dirent *sd)
1012 {
1013         return (sd->s_mode >> 12) & 15;
1014 }
1015
1016 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1017 {
1018         struct dentry *dentry = filp->f_path.dentry;
1019         struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1020         struct sysfs_dirent *cursor = filp->private_data;
1021         struct sysfs_dirent **pos;
1022         ino_t ino;
1023         int i = filp->f_pos;
1024
1025         switch (i) {
1026                 case 0:
1027                         ino = parent_sd->s_ino;
1028                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1029                                 break;
1030                         filp->f_pos++;
1031                         i++;
1032                         /* fallthrough */
1033                 case 1:
1034                         if (parent_sd->s_parent)
1035                                 ino = parent_sd->s_parent->s_ino;
1036                         else
1037                                 ino = parent_sd->s_ino;
1038                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1039                                 break;
1040                         filp->f_pos++;
1041                         i++;
1042                         /* fallthrough */
1043                 default:
1044                         mutex_lock(&sysfs_mutex);
1045
1046                         pos = &parent_sd->s_children;
1047                         while (*pos != cursor)
1048                                 pos = &(*pos)->s_sibling;
1049
1050                         /* unlink cursor */
1051                         *pos = cursor->s_sibling;
1052
1053                         if (filp->f_pos == 2)
1054                                 pos = &parent_sd->s_children;
1055
1056                         for ( ; *pos; pos = &(*pos)->s_sibling) {
1057                                 struct sysfs_dirent *next = *pos;
1058                                 const char * name;
1059                                 int len;
1060
1061                                 if (!sysfs_type(next))
1062                                         continue;
1063
1064                                 name = next->s_name;
1065                                 len = strlen(name);
1066                                 ino = next->s_ino;
1067
1068                                 if (filldir(dirent, name, len, filp->f_pos, ino,
1069                                                  dt_type(next)) < 0)
1070                                         break;
1071
1072                                 filp->f_pos++;
1073                         }
1074
1075                         /* put cursor back in */
1076                         cursor->s_sibling = *pos;
1077                         *pos = cursor;
1078
1079                         mutex_unlock(&sysfs_mutex);
1080         }
1081         return 0;
1082 }
1083
1084 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1085 {
1086         struct dentry * dentry = file->f_path.dentry;
1087
1088         switch (origin) {
1089                 case 1:
1090                         offset += file->f_pos;
1091                 case 0:
1092                         if (offset >= 0)
1093                                 break;
1094                 default:
1095                         return -EINVAL;
1096         }
1097         if (offset != file->f_pos) {
1098                 mutex_lock(&sysfs_mutex);
1099
1100                 file->f_pos = offset;
1101                 if (file->f_pos >= 2) {
1102                         struct sysfs_dirent *sd = dentry->d_fsdata;
1103                         struct sysfs_dirent *cursor = file->private_data;
1104                         struct sysfs_dirent **pos;
1105                         loff_t n = file->f_pos - 2;
1106
1107                         sysfs_unlink_sibling(cursor);
1108
1109                         pos = &sd->s_children;
1110                         while (n && *pos) {
1111                                 struct sysfs_dirent *next = *pos;
1112                                 if (sysfs_type(next))
1113                                         n--;
1114                                 pos = &(*pos)->s_sibling;
1115                         }
1116
1117                         cursor->s_sibling = *pos;
1118                         *pos = cursor;
1119                 }
1120
1121                 mutex_unlock(&sysfs_mutex);
1122         }
1123
1124         return offset;
1125 }
1126
1127 const struct file_operations sysfs_dir_operations = {
1128         .open           = sysfs_dir_open,
1129         .release        = sysfs_dir_close,
1130         .llseek         = sysfs_dir_lseek,
1131         .read           = generic_read_dir,
1132         .readdir        = sysfs_readdir,
1133 };