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