Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / fs / configfs / dir.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dir.c - Operations for configfs directories.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA.
20  *
21  * Based on sysfs:
22  *      sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
23  *
24  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
25  */
26
27 #undef DEBUG
28
29 #include <linux/fs.h>
30 #include <linux/mount.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/err.h>
34
35 #include <linux/configfs.h>
36 #include "configfs_internal.h"
37
38 DECLARE_RWSEM(configfs_rename_sem);
39 /*
40  * Protects mutations of configfs_dirent linkage together with proper i_mutex
41  * Also protects mutations of symlinks linkage to target configfs_dirent
42  * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43  * and configfs_dirent_lock locked, in that order.
44  * This allows one to safely traverse configfs_dirent trees and symlinks without
45  * having to lock inodes.
46  *
47  * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
48  * unlocked is not reliable unless in detach_groups() called from
49  * rmdir()/unregister() and from configfs_attach_group()
50  */
51 DEFINE_SPINLOCK(configfs_dirent_lock);
52
53 static void configfs_d_iput(struct dentry * dentry,
54                             struct inode * inode)
55 {
56         struct configfs_dirent *sd = dentry->d_fsdata;
57
58         if (sd) {
59                 /* Coordinate with configfs_readdir */
60                 spin_lock(&configfs_dirent_lock);
61                 /* Coordinate with configfs_attach_attr where will increase
62                  * sd->s_count and update sd->s_dentry to new allocated one.
63                  * Only set sd->dentry to null when this dentry is the only
64                  * sd owner.
65                  * If not do so, configfs_d_iput may run just after
66                  * configfs_attach_attr and set sd->s_dentry to null
67                  * even it's still in use.
68                  */
69                 if (atomic_read(&sd->s_count) <= 2)
70                         sd->s_dentry = NULL;
71
72                 spin_unlock(&configfs_dirent_lock);
73                 configfs_put(sd);
74         }
75         iput(inode);
76 }
77
78 const struct dentry_operations configfs_dentry_ops = {
79         .d_iput         = configfs_d_iput,
80         .d_delete       = always_delete_dentry,
81 };
82
83 #ifdef CONFIG_LOCKDEP
84
85 /*
86  * Helpers to make lockdep happy with our recursive locking of default groups'
87  * inodes (see configfs_attach_group() and configfs_detach_group()).
88  * We put default groups i_mutexes in separate classes according to their depth
89  * from the youngest non-default group ancestor.
90  *
91  * For a non-default group A having default groups A/B, A/C, and A/C/D, default
92  * groups A/B and A/C will have their inode's mutex in class
93  * default_group_class[0], and default group A/C/D will be in
94  * default_group_class[1].
95  *
96  * The lock classes are declared and assigned in inode.c, according to the
97  * s_depth value.
98  * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
99  * default groups, and reset to -1 when all default groups are attached. During
100  * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
101  * inode's mutex is set to default_group_class[s_depth - 1].
102  */
103
104 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
105 {
106         sd->s_depth = -1;
107 }
108
109 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
110                                           struct configfs_dirent *sd)
111 {
112         int parent_depth = parent_sd->s_depth;
113
114         if (parent_depth >= 0)
115                 sd->s_depth = parent_depth + 1;
116 }
117
118 static void
119 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
120 {
121         /*
122          * item's i_mutex class is already setup, so s_depth is now only
123          * used to set new sub-directories s_depth, which is always done
124          * with item's i_mutex locked.
125          */
126         /*
127          *  sd->s_depth == -1 iff we are a non default group.
128          *  else (we are a default group) sd->s_depth > 0 (see
129          *  create_dir()).
130          */
131         if (sd->s_depth == -1)
132                 /*
133                  * We are a non default group and we are going to create
134                  * default groups.
135                  */
136                 sd->s_depth = 0;
137 }
138
139 static void
140 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
141 {
142         /* We will not create default groups anymore. */
143         sd->s_depth = -1;
144 }
145
146 #else /* CONFIG_LOCKDEP */
147
148 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
149 {
150 }
151
152 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
153                                           struct configfs_dirent *sd)
154 {
155 }
156
157 static void
158 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
159 {
160 }
161
162 static void
163 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
164 {
165 }
166
167 #endif /* CONFIG_LOCKDEP */
168
169 /*
170  * Allocates a new configfs_dirent and links it to the parent configfs_dirent
171  */
172 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
173                                                    void *element, int type)
174 {
175         struct configfs_dirent * sd;
176
177         sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
178         if (!sd)
179                 return ERR_PTR(-ENOMEM);
180
181         atomic_set(&sd->s_count, 1);
182         INIT_LIST_HEAD(&sd->s_links);
183         INIT_LIST_HEAD(&sd->s_children);
184         sd->s_element = element;
185         sd->s_type = type;
186         configfs_init_dirent_depth(sd);
187         spin_lock(&configfs_dirent_lock);
188         if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
189                 spin_unlock(&configfs_dirent_lock);
190                 kmem_cache_free(configfs_dir_cachep, sd);
191                 return ERR_PTR(-ENOENT);
192         }
193         list_add(&sd->s_sibling, &parent_sd->s_children);
194         spin_unlock(&configfs_dirent_lock);
195
196         return sd;
197 }
198
199 /*
200  *
201  * Return -EEXIST if there is already a configfs element with the same
202  * name for the same parent.
203  *
204  * called with parent inode's i_mutex held
205  */
206 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
207                                   const unsigned char *new)
208 {
209         struct configfs_dirent * sd;
210
211         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
212                 if (sd->s_element) {
213                         const unsigned char *existing = configfs_get_name(sd);
214                         if (strcmp(existing, new))
215                                 continue;
216                         else
217                                 return -EEXIST;
218                 }
219         }
220
221         return 0;
222 }
223
224
225 int configfs_make_dirent(struct configfs_dirent * parent_sd,
226                          struct dentry * dentry, void * element,
227                          umode_t mode, int type)
228 {
229         struct configfs_dirent * sd;
230
231         sd = configfs_new_dirent(parent_sd, element, type);
232         if (IS_ERR(sd))
233                 return PTR_ERR(sd);
234
235         sd->s_mode = mode;
236         sd->s_dentry = dentry;
237         if (dentry)
238                 dentry->d_fsdata = configfs_get(sd);
239
240         return 0;
241 }
242
243 static int init_dir(struct inode * inode)
244 {
245         inode->i_op = &configfs_dir_inode_operations;
246         inode->i_fop = &configfs_dir_operations;
247
248         /* directory inodes start off with i_nlink == 2 (for "." entry) */
249         inc_nlink(inode);
250         return 0;
251 }
252
253 static int configfs_init_file(struct inode * inode)
254 {
255         inode->i_size = PAGE_SIZE;
256         inode->i_fop = &configfs_file_operations;
257         return 0;
258 }
259
260 static int init_symlink(struct inode * inode)
261 {
262         inode->i_op = &configfs_symlink_inode_operations;
263         return 0;
264 }
265
266 static int create_dir(struct config_item *k, struct dentry *d)
267 {
268         int error;
269         umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
270         struct dentry *p = d->d_parent;
271
272         BUG_ON(!k);
273
274         error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
275         if (!error)
276                 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
277                                              CONFIGFS_DIR | CONFIGFS_USET_CREATING);
278         if (!error) {
279                 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
280                 error = configfs_create(d, mode, init_dir);
281                 if (!error) {
282                         inc_nlink(p->d_inode);
283                 } else {
284                         struct configfs_dirent *sd = d->d_fsdata;
285                         if (sd) {
286                                 spin_lock(&configfs_dirent_lock);
287                                 list_del_init(&sd->s_sibling);
288                                 spin_unlock(&configfs_dirent_lock);
289                                 configfs_put(sd);
290                         }
291                 }
292         }
293         return error;
294 }
295
296
297 /**
298  *      configfs_create_dir - create a directory for an config_item.
299  *      @item:          config_itemwe're creating directory for.
300  *      @dentry:        config_item's dentry.
301  *
302  *      Note: user-created entries won't be allowed under this new directory
303  *      until it is validated by configfs_dir_set_ready()
304  */
305
306 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
307 {
308         int error = create_dir(item, dentry);
309         if (!error)
310                 item->ci_dentry = dentry;
311         return error;
312 }
313
314 /*
315  * Allow userspace to create new entries under a new directory created with
316  * configfs_create_dir(), and under all of its chidlren directories recursively.
317  * @sd          configfs_dirent of the new directory to validate
318  *
319  * Caller must hold configfs_dirent_lock.
320  */
321 static void configfs_dir_set_ready(struct configfs_dirent *sd)
322 {
323         struct configfs_dirent *child_sd;
324
325         sd->s_type &= ~CONFIGFS_USET_CREATING;
326         list_for_each_entry(child_sd, &sd->s_children, s_sibling)
327                 if (child_sd->s_type & CONFIGFS_USET_CREATING)
328                         configfs_dir_set_ready(child_sd);
329 }
330
331 /*
332  * Check that a directory does not belong to a directory hierarchy being
333  * attached and not validated yet.
334  * @sd          configfs_dirent of the directory to check
335  *
336  * @return      non-zero iff the directory was validated
337  *
338  * Note: takes configfs_dirent_lock, so the result may change from false to true
339  * in two consecutive calls, but never from true to false.
340  */
341 int configfs_dirent_is_ready(struct configfs_dirent *sd)
342 {
343         int ret;
344
345         spin_lock(&configfs_dirent_lock);
346         ret = !(sd->s_type & CONFIGFS_USET_CREATING);
347         spin_unlock(&configfs_dirent_lock);
348
349         return ret;
350 }
351
352 int configfs_create_link(struct configfs_symlink *sl,
353                          struct dentry *parent,
354                          struct dentry *dentry)
355 {
356         int err = 0;
357         umode_t mode = S_IFLNK | S_IRWXUGO;
358
359         err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
360                                    CONFIGFS_ITEM_LINK);
361         if (!err) {
362                 err = configfs_create(dentry, mode, init_symlink);
363                 if (err) {
364                         struct configfs_dirent *sd = dentry->d_fsdata;
365                         if (sd) {
366                                 spin_lock(&configfs_dirent_lock);
367                                 list_del_init(&sd->s_sibling);
368                                 spin_unlock(&configfs_dirent_lock);
369                                 configfs_put(sd);
370                         }
371                 }
372         }
373         return err;
374 }
375
376 static void remove_dir(struct dentry * d)
377 {
378         struct dentry * parent = dget(d->d_parent);
379         struct configfs_dirent * sd;
380
381         sd = d->d_fsdata;
382         spin_lock(&configfs_dirent_lock);
383         list_del_init(&sd->s_sibling);
384         spin_unlock(&configfs_dirent_lock);
385         configfs_put(sd);
386         if (d->d_inode)
387                 simple_rmdir(parent->d_inode,d);
388
389         pr_debug(" o %s removing done (%d)\n",d->d_name.name, d_count(d));
390
391         dput(parent);
392 }
393
394 /**
395  * configfs_remove_dir - remove an config_item's directory.
396  * @item:       config_item we're removing.
397  *
398  * The only thing special about this is that we remove any files in
399  * the directory before we remove the directory, and we've inlined
400  * what used to be configfs_rmdir() below, instead of calling separately.
401  *
402  * Caller holds the mutex of the item's inode
403  */
404
405 static void configfs_remove_dir(struct config_item * item)
406 {
407         struct dentry * dentry = dget(item->ci_dentry);
408
409         if (!dentry)
410                 return;
411
412         remove_dir(dentry);
413         /**
414          * Drop reference from dget() on entrance.
415          */
416         dput(dentry);
417 }
418
419
420 /* attaches attribute's configfs_dirent to the dentry corresponding to the
421  * attribute file
422  */
423 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
424 {
425         struct configfs_attribute * attr = sd->s_element;
426         int error;
427
428         spin_lock(&configfs_dirent_lock);
429         dentry->d_fsdata = configfs_get(sd);
430         sd->s_dentry = dentry;
431         spin_unlock(&configfs_dirent_lock);
432
433         error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
434                                 configfs_init_file);
435         if (error) {
436                 configfs_put(sd);
437                 return error;
438         }
439
440         d_rehash(dentry);
441
442         return 0;
443 }
444
445 static struct dentry * configfs_lookup(struct inode *dir,
446                                        struct dentry *dentry,
447                                        unsigned int flags)
448 {
449         struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
450         struct configfs_dirent * sd;
451         int found = 0;
452         int err;
453
454         /*
455          * Fake invisibility if dir belongs to a group/default groups hierarchy
456          * being attached
457          *
458          * This forbids userspace to read/write attributes of items which may
459          * not complete their initialization, since the dentries of the
460          * attributes won't be instantiated.
461          */
462         err = -ENOENT;
463         if (!configfs_dirent_is_ready(parent_sd))
464                 goto out;
465
466         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
467                 if (sd->s_type & CONFIGFS_NOT_PINNED) {
468                         const unsigned char * name = configfs_get_name(sd);
469
470                         if (strcmp(name, dentry->d_name.name))
471                                 continue;
472
473                         found = 1;
474                         err = configfs_attach_attr(sd, dentry);
475                         break;
476                 }
477         }
478
479         if (!found) {
480                 /*
481                  * If it doesn't exist and it isn't a NOT_PINNED item,
482                  * it must be negative.
483                  */
484                 if (dentry->d_name.len > NAME_MAX)
485                         return ERR_PTR(-ENAMETOOLONG);
486                 d_add(dentry, NULL);
487                 return NULL;
488         }
489
490 out:
491         return ERR_PTR(err);
492 }
493
494 /*
495  * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
496  * attributes and are removed by rmdir().  We recurse, setting
497  * CONFIGFS_USET_DROPPING on all children that are candidates for
498  * default detach.
499  * If there is an error, the caller will reset the flags via
500  * configfs_detach_rollback().
501  */
502 static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
503 {
504         struct configfs_dirent *parent_sd = dentry->d_fsdata;
505         struct configfs_dirent *sd;
506         int ret;
507
508         /* Mark that we're trying to drop the group */
509         parent_sd->s_type |= CONFIGFS_USET_DROPPING;
510
511         ret = -EBUSY;
512         if (!list_empty(&parent_sd->s_links))
513                 goto out;
514
515         ret = 0;
516         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
517                 if (!sd->s_element ||
518                     (sd->s_type & CONFIGFS_NOT_PINNED))
519                         continue;
520                 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
521                         /* Abort if racing with mkdir() */
522                         if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
523                                 if (wait_mutex)
524                                         *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
525                                 return -EAGAIN;
526                         }
527
528                         /*
529                          * Yup, recursive.  If there's a problem, blame
530                          * deep nesting of default_groups
531                          */
532                         ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
533                         if (!ret)
534                                 continue;
535                 } else
536                         ret = -ENOTEMPTY;
537
538                 break;
539         }
540
541 out:
542         return ret;
543 }
544
545 /*
546  * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
547  * set.
548  */
549 static void configfs_detach_rollback(struct dentry *dentry)
550 {
551         struct configfs_dirent *parent_sd = dentry->d_fsdata;
552         struct configfs_dirent *sd;
553
554         parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
555
556         list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
557                 if (sd->s_type & CONFIGFS_USET_DEFAULT)
558                         configfs_detach_rollback(sd->s_dentry);
559 }
560
561 static void detach_attrs(struct config_item * item)
562 {
563         struct dentry * dentry = dget(item->ci_dentry);
564         struct configfs_dirent * parent_sd;
565         struct configfs_dirent * sd, * tmp;
566
567         if (!dentry)
568                 return;
569
570         pr_debug("configfs %s: dropping attrs for  dir\n",
571                  dentry->d_name.name);
572
573         parent_sd = dentry->d_fsdata;
574         list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
575                 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
576                         continue;
577                 spin_lock(&configfs_dirent_lock);
578                 list_del_init(&sd->s_sibling);
579                 spin_unlock(&configfs_dirent_lock);
580                 configfs_drop_dentry(sd, dentry);
581                 configfs_put(sd);
582         }
583
584         /**
585          * Drop reference from dget() on entrance.
586          */
587         dput(dentry);
588 }
589
590 static int populate_attrs(struct config_item *item)
591 {
592         struct config_item_type *t = item->ci_type;
593         struct configfs_attribute *attr;
594         int error = 0;
595         int i;
596
597         if (!t)
598                 return -EINVAL;
599         if (t->ct_attrs) {
600                 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
601                         if ((error = configfs_create_file(item, attr)))
602                                 break;
603                 }
604         }
605
606         if (error)
607                 detach_attrs(item);
608
609         return error;
610 }
611
612 static int configfs_attach_group(struct config_item *parent_item,
613                                  struct config_item *item,
614                                  struct dentry *dentry);
615 static void configfs_detach_group(struct config_item *item);
616
617 static void detach_groups(struct config_group *group)
618 {
619         struct dentry * dentry = dget(group->cg_item.ci_dentry);
620         struct dentry *child;
621         struct configfs_dirent *parent_sd;
622         struct configfs_dirent *sd, *tmp;
623
624         if (!dentry)
625                 return;
626
627         parent_sd = dentry->d_fsdata;
628         list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
629                 if (!sd->s_element ||
630                     !(sd->s_type & CONFIGFS_USET_DEFAULT))
631                         continue;
632
633                 child = sd->s_dentry;
634
635                 mutex_lock(&child->d_inode->i_mutex);
636
637                 configfs_detach_group(sd->s_element);
638                 child->d_inode->i_flags |= S_DEAD;
639                 dont_mount(child);
640
641                 mutex_unlock(&child->d_inode->i_mutex);
642
643                 d_delete(child);
644                 dput(child);
645         }
646
647         /**
648          * Drop reference from dget() on entrance.
649          */
650         dput(dentry);
651 }
652
653 /*
654  * This fakes mkdir(2) on a default_groups[] entry.  It
655  * creates a dentry, attachs it, and then does fixup
656  * on the sd->s_type.
657  *
658  * We could, perhaps, tweak our parent's ->mkdir for a minute and
659  * try using vfs_mkdir.  Just a thought.
660  */
661 static int create_default_group(struct config_group *parent_group,
662                                 struct config_group *group)
663 {
664         int ret;
665         struct configfs_dirent *sd;
666         /* We trust the caller holds a reference to parent */
667         struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
668
669         if (!group->cg_item.ci_name)
670                 group->cg_item.ci_name = group->cg_item.ci_namebuf;
671
672         ret = -ENOMEM;
673         child = d_alloc_name(parent, group->cg_item.ci_name);
674         if (child) {
675                 d_add(child, NULL);
676
677                 ret = configfs_attach_group(&parent_group->cg_item,
678                                             &group->cg_item, child);
679                 if (!ret) {
680                         sd = child->d_fsdata;
681                         sd->s_type |= CONFIGFS_USET_DEFAULT;
682                 } else {
683                         BUG_ON(child->d_inode);
684                         d_drop(child);
685                         dput(child);
686                 }
687         }
688
689         return ret;
690 }
691
692 static int populate_groups(struct config_group *group)
693 {
694         struct config_group *new_group;
695         int ret = 0;
696         int i;
697
698         if (group->default_groups) {
699                 for (i = 0; group->default_groups[i]; i++) {
700                         new_group = group->default_groups[i];
701
702                         ret = create_default_group(group, new_group);
703                         if (ret) {
704                                 detach_groups(group);
705                                 break;
706                         }
707                 }
708         }
709
710         return ret;
711 }
712
713 /*
714  * All of link_obj/unlink_obj/link_group/unlink_group require that
715  * subsys->su_mutex is held.
716  */
717
718 static void unlink_obj(struct config_item *item)
719 {
720         struct config_group *group;
721
722         group = item->ci_group;
723         if (group) {
724                 list_del_init(&item->ci_entry);
725
726                 item->ci_group = NULL;
727                 item->ci_parent = NULL;
728
729                 /* Drop the reference for ci_entry */
730                 config_item_put(item);
731
732                 /* Drop the reference for ci_parent */
733                 config_group_put(group);
734         }
735 }
736
737 static void link_obj(struct config_item *parent_item, struct config_item *item)
738 {
739         /*
740          * Parent seems redundant with group, but it makes certain
741          * traversals much nicer.
742          */
743         item->ci_parent = parent_item;
744
745         /*
746          * We hold a reference on the parent for the child's ci_parent
747          * link.
748          */
749         item->ci_group = config_group_get(to_config_group(parent_item));
750         list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
751
752         /*
753          * We hold a reference on the child for ci_entry on the parent's
754          * cg_children
755          */
756         config_item_get(item);
757 }
758
759 static void unlink_group(struct config_group *group)
760 {
761         int i;
762         struct config_group *new_group;
763
764         if (group->default_groups) {
765                 for (i = 0; group->default_groups[i]; i++) {
766                         new_group = group->default_groups[i];
767                         unlink_group(new_group);
768                 }
769         }
770
771         group->cg_subsys = NULL;
772         unlink_obj(&group->cg_item);
773 }
774
775 static void link_group(struct config_group *parent_group, struct config_group *group)
776 {
777         int i;
778         struct config_group *new_group;
779         struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
780
781         link_obj(&parent_group->cg_item, &group->cg_item);
782
783         if (parent_group->cg_subsys)
784                 subsys = parent_group->cg_subsys;
785         else if (configfs_is_root(&parent_group->cg_item))
786                 subsys = to_configfs_subsystem(group);
787         else
788                 BUG();
789         group->cg_subsys = subsys;
790
791         if (group->default_groups) {
792                 for (i = 0; group->default_groups[i]; i++) {
793                         new_group = group->default_groups[i];
794                         link_group(group, new_group);
795                 }
796         }
797 }
798
799 /*
800  * The goal is that configfs_attach_item() (and
801  * configfs_attach_group()) can be called from either the VFS or this
802  * module.  That is, they assume that the items have been created,
803  * the dentry allocated, and the dcache is all ready to go.
804  *
805  * If they fail, they must clean up after themselves as if they
806  * had never been called.  The caller (VFS or local function) will
807  * handle cleaning up the dcache bits.
808  *
809  * configfs_detach_group() and configfs_detach_item() behave similarly on
810  * the way out.  They assume that the proper semaphores are held, they
811  * clean up the configfs items, and they expect their callers will
812  * handle the dcache bits.
813  */
814 static int configfs_attach_item(struct config_item *parent_item,
815                                 struct config_item *item,
816                                 struct dentry *dentry)
817 {
818         int ret;
819
820         ret = configfs_create_dir(item, dentry);
821         if (!ret) {
822                 ret = populate_attrs(item);
823                 if (ret) {
824                         /*
825                          * We are going to remove an inode and its dentry but
826                          * the VFS may already have hit and used them. Thus,
827                          * we must lock them as rmdir() would.
828                          */
829                         mutex_lock(&dentry->d_inode->i_mutex);
830                         configfs_remove_dir(item);
831                         dentry->d_inode->i_flags |= S_DEAD;
832                         dont_mount(dentry);
833                         mutex_unlock(&dentry->d_inode->i_mutex);
834                         d_delete(dentry);
835                 }
836         }
837
838         return ret;
839 }
840
841 /* Caller holds the mutex of the item's inode */
842 static void configfs_detach_item(struct config_item *item)
843 {
844         detach_attrs(item);
845         configfs_remove_dir(item);
846 }
847
848 static int configfs_attach_group(struct config_item *parent_item,
849                                  struct config_item *item,
850                                  struct dentry *dentry)
851 {
852         int ret;
853         struct configfs_dirent *sd;
854
855         ret = configfs_attach_item(parent_item, item, dentry);
856         if (!ret) {
857                 sd = dentry->d_fsdata;
858                 sd->s_type |= CONFIGFS_USET_DIR;
859
860                 /*
861                  * FYI, we're faking mkdir in populate_groups()
862                  * We must lock the group's inode to avoid races with the VFS
863                  * which can already hit the inode and try to add/remove entries
864                  * under it.
865                  *
866                  * We must also lock the inode to remove it safely in case of
867                  * error, as rmdir() would.
868                  */
869                 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
870                 configfs_adjust_dir_dirent_depth_before_populate(sd);
871                 ret = populate_groups(to_config_group(item));
872                 if (ret) {
873                         configfs_detach_item(item);
874                         dentry->d_inode->i_flags |= S_DEAD;
875                         dont_mount(dentry);
876                 }
877                 configfs_adjust_dir_dirent_depth_after_populate(sd);
878                 mutex_unlock(&dentry->d_inode->i_mutex);
879                 if (ret)
880                         d_delete(dentry);
881         }
882
883         return ret;
884 }
885
886 /* Caller holds the mutex of the group's inode */
887 static void configfs_detach_group(struct config_item *item)
888 {
889         detach_groups(to_config_group(item));
890         configfs_detach_item(item);
891 }
892
893 /*
894  * After the item has been detached from the filesystem view, we are
895  * ready to tear it out of the hierarchy.  Notify the client before
896  * we do that so they can perform any cleanup that requires
897  * navigating the hierarchy.  A client does not need to provide this
898  * callback.  The subsystem semaphore MUST be held by the caller, and
899  * references must be valid for both items.  It also assumes the
900  * caller has validated ci_type.
901  */
902 static void client_disconnect_notify(struct config_item *parent_item,
903                                      struct config_item *item)
904 {
905         struct config_item_type *type;
906
907         type = parent_item->ci_type;
908         BUG_ON(!type);
909
910         if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
911                 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
912                                                       item);
913 }
914
915 /*
916  * Drop the initial reference from make_item()/make_group()
917  * This function assumes that reference is held on item
918  * and that item holds a valid reference to the parent.  Also, it
919  * assumes the caller has validated ci_type.
920  */
921 static void client_drop_item(struct config_item *parent_item,
922                              struct config_item *item)
923 {
924         struct config_item_type *type;
925
926         type = parent_item->ci_type;
927         BUG_ON(!type);
928
929         /*
930          * If ->drop_item() exists, it is responsible for the
931          * config_item_put().
932          */
933         if (type->ct_group_ops && type->ct_group_ops->drop_item)
934                 type->ct_group_ops->drop_item(to_config_group(parent_item),
935                                               item);
936         else
937                 config_item_put(item);
938 }
939
940 #ifdef DEBUG
941 static void configfs_dump_one(struct configfs_dirent *sd, int level)
942 {
943         printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
944
945 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
946         type_print(CONFIGFS_ROOT);
947         type_print(CONFIGFS_DIR);
948         type_print(CONFIGFS_ITEM_ATTR);
949         type_print(CONFIGFS_ITEM_LINK);
950         type_print(CONFIGFS_USET_DIR);
951         type_print(CONFIGFS_USET_DEFAULT);
952         type_print(CONFIGFS_USET_DROPPING);
953 #undef type_print
954 }
955
956 static int configfs_dump(struct configfs_dirent *sd, int level)
957 {
958         struct configfs_dirent *child_sd;
959         int ret = 0;
960
961         configfs_dump_one(sd, level);
962
963         if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
964                 return 0;
965
966         list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
967                 ret = configfs_dump(child_sd, level + 2);
968                 if (ret)
969                         break;
970         }
971
972         return ret;
973 }
974 #endif
975
976
977 /*
978  * configfs_depend_item() and configfs_undepend_item()
979  *
980  * WARNING: Do not call these from a configfs callback!
981  *
982  * This describes these functions and their helpers.
983  *
984  * Allow another kernel system to depend on a config_item.  If this
985  * happens, the item cannot go away until the dependent can live without
986  * it.  The idea is to give client modules as simple an interface as
987  * possible.  When a system asks them to depend on an item, they just
988  * call configfs_depend_item().  If the item is live and the client
989  * driver is in good shape, we'll happily do the work for them.
990  *
991  * Why is the locking complex?  Because configfs uses the VFS to handle
992  * all locking, but this function is called outside the normal
993  * VFS->configfs path.  So it must take VFS locks to prevent the
994  * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc).  This is
995  * why you can't call these functions underneath configfs callbacks.
996  *
997  * Note, btw, that this can be called at *any* time, even when a configfs
998  * subsystem isn't registered, or when configfs is loading or unloading.
999  * Just like configfs_register_subsystem().  So we take the same
1000  * precautions.  We pin the filesystem.  We lock configfs_dirent_lock.
1001  * If we can find the target item in the
1002  * configfs tree, it must be part of the subsystem tree as well, so we
1003  * do not need the subsystem semaphore.  Holding configfs_dirent_lock helps
1004  * locking out mkdir() and rmdir(), who might be racing us.
1005  */
1006
1007 /*
1008  * configfs_depend_prep()
1009  *
1010  * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
1011  * attributes.  This is similar but not the same to configfs_detach_prep().
1012  * Note that configfs_detach_prep() expects the parent to be locked when it
1013  * is called, but we lock the parent *inside* configfs_depend_prep().  We
1014  * do that so we can unlock it if we find nothing.
1015  *
1016  * Here we do a depth-first search of the dentry hierarchy looking for
1017  * our object.
1018  * We deliberately ignore items tagged as dropping since they are virtually
1019  * dead, as well as items in the middle of attachment since they virtually
1020  * do not exist yet. This completes the locking out of racing mkdir() and
1021  * rmdir().
1022  * Note: subdirectories in the middle of attachment start with s_type =
1023  * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir().  When
1024  * CONFIGFS_USET_CREATING is set, we ignore the item.  The actual set of
1025  * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1026  *
1027  * If the target is not found, -ENOENT is bubbled up.
1028  *
1029  * This adds a requirement that all config_items be unique!
1030  *
1031  * This is recursive.  There isn't
1032  * much on the stack, though, so folks that need this function - be careful
1033  * about your stack!  Patches will be accepted to make it iterative.
1034  */
1035 static int configfs_depend_prep(struct dentry *origin,
1036                                 struct config_item *target)
1037 {
1038         struct configfs_dirent *child_sd, *sd;
1039         int ret = 0;
1040
1041         BUG_ON(!origin || !origin->d_fsdata);
1042         sd = origin->d_fsdata;
1043
1044         if (sd->s_element == target)  /* Boo-yah */
1045                 goto out;
1046
1047         list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1048                 if ((child_sd->s_type & CONFIGFS_DIR) &&
1049                     !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1050                     !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1051                         ret = configfs_depend_prep(child_sd->s_dentry,
1052                                                    target);
1053                         if (!ret)
1054                                 goto out;  /* Child path boo-yah */
1055                 }
1056         }
1057
1058         /* We looped all our children and didn't find target */
1059         ret = -ENOENT;
1060
1061 out:
1062         return ret;
1063 }
1064
1065 int configfs_depend_item(struct configfs_subsystem *subsys,
1066                          struct config_item *target)
1067 {
1068         int ret;
1069         struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1070         struct config_item *s_item = &subsys->su_group.cg_item;
1071         struct dentry *root;
1072
1073         /*
1074          * Pin the configfs filesystem.  This means we can safely access
1075          * the root of the configfs filesystem.
1076          */
1077         root = configfs_pin_fs();
1078         if (IS_ERR(root))
1079                 return PTR_ERR(root);
1080
1081         /*
1082          * Next, lock the root directory.  We're going to check that the
1083          * subsystem is really registered, and so we need to lock out
1084          * configfs_[un]register_subsystem().
1085          */
1086         mutex_lock(&root->d_inode->i_mutex);
1087
1088         root_sd = root->d_fsdata;
1089
1090         list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1091                 if (p->s_type & CONFIGFS_DIR) {
1092                         if (p->s_element == s_item) {
1093                                 subsys_sd = p;
1094                                 break;
1095                         }
1096                 }
1097         }
1098
1099         if (!subsys_sd) {
1100                 ret = -ENOENT;
1101                 goto out_unlock_fs;
1102         }
1103
1104         /* Ok, now we can trust subsys/s_item */
1105
1106         spin_lock(&configfs_dirent_lock);
1107         /* Scan the tree, return 0 if found */
1108         ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1109         if (ret)
1110                 goto out_unlock_dirent_lock;
1111
1112         /*
1113          * We are sure that the item is not about to be removed by rmdir(), and
1114          * not in the middle of attachment by mkdir().
1115          */
1116         p = target->ci_dentry->d_fsdata;
1117         p->s_dependent_count += 1;
1118
1119 out_unlock_dirent_lock:
1120         spin_unlock(&configfs_dirent_lock);
1121 out_unlock_fs:
1122         mutex_unlock(&root->d_inode->i_mutex);
1123
1124         /*
1125          * If we succeeded, the fs is pinned via other methods.  If not,
1126          * we're done with it anyway.  So release_fs() is always right.
1127          */
1128         configfs_release_fs();
1129
1130         return ret;
1131 }
1132 EXPORT_SYMBOL(configfs_depend_item);
1133
1134 /*
1135  * Release the dependent linkage.  This is much simpler than
1136  * configfs_depend_item() because we know that that the client driver is
1137  * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1138  */
1139 void configfs_undepend_item(struct configfs_subsystem *subsys,
1140                             struct config_item *target)
1141 {
1142         struct configfs_dirent *sd;
1143
1144         /*
1145          * Since we can trust everything is pinned, we just need
1146          * configfs_dirent_lock.
1147          */
1148         spin_lock(&configfs_dirent_lock);
1149
1150         sd = target->ci_dentry->d_fsdata;
1151         BUG_ON(sd->s_dependent_count < 1);
1152
1153         sd->s_dependent_count -= 1;
1154
1155         /*
1156          * After this unlock, we cannot trust the item to stay alive!
1157          * DO NOT REFERENCE item after this unlock.
1158          */
1159         spin_unlock(&configfs_dirent_lock);
1160 }
1161 EXPORT_SYMBOL(configfs_undepend_item);
1162
1163 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1164 {
1165         int ret = 0;
1166         int module_got = 0;
1167         struct config_group *group = NULL;
1168         struct config_item *item = NULL;
1169         struct config_item *parent_item;
1170         struct configfs_subsystem *subsys;
1171         struct configfs_dirent *sd;
1172         struct config_item_type *type;
1173         struct module *subsys_owner = NULL, *new_item_owner = NULL;
1174         char *name;
1175
1176         sd = dentry->d_parent->d_fsdata;
1177
1178         /*
1179          * Fake invisibility if dir belongs to a group/default groups hierarchy
1180          * being attached
1181          */
1182         if (!configfs_dirent_is_ready(sd)) {
1183                 ret = -ENOENT;
1184                 goto out;
1185         }
1186
1187         if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1188                 ret = -EPERM;
1189                 goto out;
1190         }
1191
1192         /* Get a working ref for the duration of this function */
1193         parent_item = configfs_get_config_item(dentry->d_parent);
1194         type = parent_item->ci_type;
1195         subsys = to_config_group(parent_item)->cg_subsys;
1196         BUG_ON(!subsys);
1197
1198         if (!type || !type->ct_group_ops ||
1199             (!type->ct_group_ops->make_group &&
1200              !type->ct_group_ops->make_item)) {
1201                 ret = -EPERM;  /* Lack-of-mkdir returns -EPERM */
1202                 goto out_put;
1203         }
1204
1205         /*
1206          * The subsystem may belong to a different module than the item
1207          * being created.  We don't want to safely pin the new item but
1208          * fail to pin the subsystem it sits under.
1209          */
1210         if (!subsys->su_group.cg_item.ci_type) {
1211                 ret = -EINVAL;
1212                 goto out_put;
1213         }
1214         subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1215         if (!try_module_get(subsys_owner)) {
1216                 ret = -EINVAL;
1217                 goto out_put;
1218         }
1219
1220         name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1221         if (!name) {
1222                 ret = -ENOMEM;
1223                 goto out_subsys_put;
1224         }
1225
1226         snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1227
1228         mutex_lock(&subsys->su_mutex);
1229         if (type->ct_group_ops->make_group) {
1230                 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1231                 if (!group)
1232                         group = ERR_PTR(-ENOMEM);
1233                 if (!IS_ERR(group)) {
1234                         link_group(to_config_group(parent_item), group);
1235                         item = &group->cg_item;
1236                 } else
1237                         ret = PTR_ERR(group);
1238         } else {
1239                 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1240                 if (!item)
1241                         item = ERR_PTR(-ENOMEM);
1242                 if (!IS_ERR(item))
1243                         link_obj(parent_item, item);
1244                 else
1245                         ret = PTR_ERR(item);
1246         }
1247         mutex_unlock(&subsys->su_mutex);
1248
1249         kfree(name);
1250         if (ret) {
1251                 /*
1252                  * If ret != 0, then link_obj() was never called.
1253                  * There are no extra references to clean up.
1254                  */
1255                 goto out_subsys_put;
1256         }
1257
1258         /*
1259          * link_obj() has been called (via link_group() for groups).
1260          * From here on out, errors must clean that up.
1261          */
1262
1263         type = item->ci_type;
1264         if (!type) {
1265                 ret = -EINVAL;
1266                 goto out_unlink;
1267         }
1268
1269         new_item_owner = type->ct_owner;
1270         if (!try_module_get(new_item_owner)) {
1271                 ret = -EINVAL;
1272                 goto out_unlink;
1273         }
1274
1275         /*
1276          * I hate doing it this way, but if there is
1277          * an error,  module_put() probably should
1278          * happen after any cleanup.
1279          */
1280         module_got = 1;
1281
1282         /*
1283          * Make racing rmdir() fail if it did not tag parent with
1284          * CONFIGFS_USET_DROPPING
1285          * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1286          * fail and let rmdir() terminate correctly
1287          */
1288         spin_lock(&configfs_dirent_lock);
1289         /* This will make configfs_detach_prep() fail */
1290         sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1291         spin_unlock(&configfs_dirent_lock);
1292
1293         if (group)
1294                 ret = configfs_attach_group(parent_item, item, dentry);
1295         else
1296                 ret = configfs_attach_item(parent_item, item, dentry);
1297
1298         spin_lock(&configfs_dirent_lock);
1299         sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1300         if (!ret)
1301                 configfs_dir_set_ready(dentry->d_fsdata);
1302         spin_unlock(&configfs_dirent_lock);
1303
1304 out_unlink:
1305         if (ret) {
1306                 /* Tear down everything we built up */
1307                 mutex_lock(&subsys->su_mutex);
1308
1309                 client_disconnect_notify(parent_item, item);
1310                 if (group)
1311                         unlink_group(group);
1312                 else
1313                         unlink_obj(item);
1314                 client_drop_item(parent_item, item);
1315
1316                 mutex_unlock(&subsys->su_mutex);
1317
1318                 if (module_got)
1319                         module_put(new_item_owner);
1320         }
1321
1322 out_subsys_put:
1323         if (ret)
1324                 module_put(subsys_owner);
1325
1326 out_put:
1327         /*
1328          * link_obj()/link_group() took a reference from child->parent,
1329          * so the parent is safely pinned.  We can drop our working
1330          * reference.
1331          */
1332         config_item_put(parent_item);
1333
1334 out:
1335         return ret;
1336 }
1337
1338 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1339 {
1340         struct config_item *parent_item;
1341         struct config_item *item;
1342         struct configfs_subsystem *subsys;
1343         struct configfs_dirent *sd;
1344         struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1345         int ret;
1346
1347         sd = dentry->d_fsdata;
1348         if (sd->s_type & CONFIGFS_USET_DEFAULT)
1349                 return -EPERM;
1350
1351         /* Get a working ref until we have the child */
1352         parent_item = configfs_get_config_item(dentry->d_parent);
1353         subsys = to_config_group(parent_item)->cg_subsys;
1354         BUG_ON(!subsys);
1355
1356         if (!parent_item->ci_type) {
1357                 config_item_put(parent_item);
1358                 return -EINVAL;
1359         }
1360
1361         /* configfs_mkdir() shouldn't have allowed this */
1362         BUG_ON(!subsys->su_group.cg_item.ci_type);
1363         subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1364
1365         /*
1366          * Ensure that no racing symlink() will make detach_prep() fail while
1367          * the new link is temporarily attached
1368          */
1369         do {
1370                 struct mutex *wait_mutex;
1371
1372                 mutex_lock(&configfs_symlink_mutex);
1373                 spin_lock(&configfs_dirent_lock);
1374                 /*
1375                  * Here's where we check for dependents.  We're protected by
1376                  * configfs_dirent_lock.
1377                  * If no dependent, atomically tag the item as dropping.
1378                  */
1379                 ret = sd->s_dependent_count ? -EBUSY : 0;
1380                 if (!ret) {
1381                         ret = configfs_detach_prep(dentry, &wait_mutex);
1382                         if (ret)
1383                                 configfs_detach_rollback(dentry);
1384                 }
1385                 spin_unlock(&configfs_dirent_lock);
1386                 mutex_unlock(&configfs_symlink_mutex);
1387
1388                 if (ret) {
1389                         if (ret != -EAGAIN) {
1390                                 config_item_put(parent_item);
1391                                 return ret;
1392                         }
1393
1394                         /* Wait until the racing operation terminates */
1395                         mutex_lock(wait_mutex);
1396                         mutex_unlock(wait_mutex);
1397                 }
1398         } while (ret == -EAGAIN);
1399
1400         /* Get a working ref for the duration of this function */
1401         item = configfs_get_config_item(dentry);
1402
1403         /* Drop reference from above, item already holds one. */
1404         config_item_put(parent_item);
1405
1406         if (item->ci_type)
1407                 dead_item_owner = item->ci_type->ct_owner;
1408
1409         if (sd->s_type & CONFIGFS_USET_DIR) {
1410                 configfs_detach_group(item);
1411
1412                 mutex_lock(&subsys->su_mutex);
1413                 client_disconnect_notify(parent_item, item);
1414                 unlink_group(to_config_group(item));
1415         } else {
1416                 configfs_detach_item(item);
1417
1418                 mutex_lock(&subsys->su_mutex);
1419                 client_disconnect_notify(parent_item, item);
1420                 unlink_obj(item);
1421         }
1422
1423         client_drop_item(parent_item, item);
1424         mutex_unlock(&subsys->su_mutex);
1425
1426         /* Drop our reference from above */
1427         config_item_put(item);
1428
1429         module_put(dead_item_owner);
1430         module_put(subsys_owner);
1431
1432         return 0;
1433 }
1434
1435 const struct inode_operations configfs_dir_inode_operations = {
1436         .mkdir          = configfs_mkdir,
1437         .rmdir          = configfs_rmdir,
1438         .symlink        = configfs_symlink,
1439         .unlink         = configfs_unlink,
1440         .lookup         = configfs_lookup,
1441         .setattr        = configfs_setattr,
1442 };
1443
1444 const struct inode_operations configfs_root_inode_operations = {
1445         .lookup         = configfs_lookup,
1446         .setattr        = configfs_setattr,
1447 };
1448
1449 #if 0
1450 int configfs_rename_dir(struct config_item * item, const char *new_name)
1451 {
1452         int error = 0;
1453         struct dentry * new_dentry, * parent;
1454
1455         if (!strcmp(config_item_name(item), new_name))
1456                 return -EINVAL;
1457
1458         if (!item->parent)
1459                 return -EINVAL;
1460
1461         down_write(&configfs_rename_sem);
1462         parent = item->parent->dentry;
1463
1464         mutex_lock(&parent->d_inode->i_mutex);
1465
1466         new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1467         if (!IS_ERR(new_dentry)) {
1468                 if (!new_dentry->d_inode) {
1469                         error = config_item_set_name(item, "%s", new_name);
1470                         if (!error) {
1471                                 d_add(new_dentry, NULL);
1472                                 d_move(item->dentry, new_dentry);
1473                         }
1474                         else
1475                                 d_delete(new_dentry);
1476                 } else
1477                         error = -EEXIST;
1478                 dput(new_dentry);
1479         }
1480         mutex_unlock(&parent->d_inode->i_mutex);
1481         up_write(&configfs_rename_sem);
1482
1483         return error;
1484 }
1485 #endif
1486
1487 static int configfs_dir_open(struct inode *inode, struct file *file)
1488 {
1489         struct dentry * dentry = file->f_path.dentry;
1490         struct configfs_dirent * parent_sd = dentry->d_fsdata;
1491         int err;
1492
1493         mutex_lock(&dentry->d_inode->i_mutex);
1494         /*
1495          * Fake invisibility if dir belongs to a group/default groups hierarchy
1496          * being attached
1497          */
1498         err = -ENOENT;
1499         if (configfs_dirent_is_ready(parent_sd)) {
1500                 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1501                 if (IS_ERR(file->private_data))
1502                         err = PTR_ERR(file->private_data);
1503                 else
1504                         err = 0;
1505         }
1506         mutex_unlock(&dentry->d_inode->i_mutex);
1507
1508         return err;
1509 }
1510
1511 static int configfs_dir_close(struct inode *inode, struct file *file)
1512 {
1513         struct dentry * dentry = file->f_path.dentry;
1514         struct configfs_dirent * cursor = file->private_data;
1515
1516         mutex_lock(&dentry->d_inode->i_mutex);
1517         spin_lock(&configfs_dirent_lock);
1518         list_del_init(&cursor->s_sibling);
1519         spin_unlock(&configfs_dirent_lock);
1520         mutex_unlock(&dentry->d_inode->i_mutex);
1521
1522         release_configfs_dirent(cursor);
1523
1524         return 0;
1525 }
1526
1527 /* Relationship between s_mode and the DT_xxx types */
1528 static inline unsigned char dt_type(struct configfs_dirent *sd)
1529 {
1530         return (sd->s_mode >> 12) & 15;
1531 }
1532
1533 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1534 {
1535         struct dentry *dentry = file->f_path.dentry;
1536         struct super_block *sb = dentry->d_sb;
1537         struct configfs_dirent * parent_sd = dentry->d_fsdata;
1538         struct configfs_dirent *cursor = file->private_data;
1539         struct list_head *p, *q = &cursor->s_sibling;
1540         ino_t ino = 0;
1541
1542         if (!dir_emit_dots(file, ctx))
1543                 return 0;
1544         if (ctx->pos == 2) {
1545                 spin_lock(&configfs_dirent_lock);
1546                 list_move(q, &parent_sd->s_children);
1547                 spin_unlock(&configfs_dirent_lock);
1548         }
1549         for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1550                 struct configfs_dirent *next;
1551                 const char *name;
1552                 int len;
1553                 struct inode *inode = NULL;
1554
1555                 next = list_entry(p, struct configfs_dirent, s_sibling);
1556                 if (!next->s_element)
1557                         continue;
1558
1559                 name = configfs_get_name(next);
1560                 len = strlen(name);
1561
1562                 /*
1563                  * We'll have a dentry and an inode for
1564                  * PINNED items and for open attribute
1565                  * files.  We lock here to prevent a race
1566                  * with configfs_d_iput() clearing
1567                  * s_dentry before calling iput().
1568                  *
1569                  * Why do we go to the trouble?  If
1570                  * someone has an attribute file open,
1571                  * the inode number should match until
1572                  * they close it.  Beyond that, we don't
1573                  * care.
1574                  */
1575                 spin_lock(&configfs_dirent_lock);
1576                 dentry = next->s_dentry;
1577                 if (dentry)
1578                         inode = dentry->d_inode;
1579                 if (inode)
1580                         ino = inode->i_ino;
1581                 spin_unlock(&configfs_dirent_lock);
1582                 if (!inode)
1583                         ino = iunique(sb, 2);
1584
1585                 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1586                         return 0;
1587
1588                 spin_lock(&configfs_dirent_lock);
1589                 list_move(q, p);
1590                 spin_unlock(&configfs_dirent_lock);
1591                 p = q;
1592                 ctx->pos++;
1593         }
1594         return 0;
1595 }
1596
1597 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1598 {
1599         struct dentry * dentry = file->f_path.dentry;
1600
1601         mutex_lock(&dentry->d_inode->i_mutex);
1602         switch (whence) {
1603                 case 1:
1604                         offset += file->f_pos;
1605                 case 0:
1606                         if (offset >= 0)
1607                                 break;
1608                 default:
1609                         mutex_unlock(&file_inode(file)->i_mutex);
1610                         return -EINVAL;
1611         }
1612         if (offset != file->f_pos) {
1613                 file->f_pos = offset;
1614                 if (file->f_pos >= 2) {
1615                         struct configfs_dirent *sd = dentry->d_fsdata;
1616                         struct configfs_dirent *cursor = file->private_data;
1617                         struct list_head *p;
1618                         loff_t n = file->f_pos - 2;
1619
1620                         spin_lock(&configfs_dirent_lock);
1621                         list_del(&cursor->s_sibling);
1622                         p = sd->s_children.next;
1623                         while (n && p != &sd->s_children) {
1624                                 struct configfs_dirent *next;
1625                                 next = list_entry(p, struct configfs_dirent,
1626                                                    s_sibling);
1627                                 if (next->s_element)
1628                                         n--;
1629                                 p = p->next;
1630                         }
1631                         list_add_tail(&cursor->s_sibling, p);
1632                         spin_unlock(&configfs_dirent_lock);
1633                 }
1634         }
1635         mutex_unlock(&dentry->d_inode->i_mutex);
1636         return offset;
1637 }
1638
1639 const struct file_operations configfs_dir_operations = {
1640         .open           = configfs_dir_open,
1641         .release        = configfs_dir_close,
1642         .llseek         = configfs_dir_lseek,
1643         .read           = generic_read_dir,
1644         .iterate        = configfs_readdir,
1645 };
1646
1647 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1648 {
1649         int err;
1650         struct config_group *group = &subsys->su_group;
1651         struct dentry *dentry;
1652         struct dentry *root;
1653         struct configfs_dirent *sd;
1654
1655         root = configfs_pin_fs();
1656         if (IS_ERR(root))
1657                 return PTR_ERR(root);
1658
1659         if (!group->cg_item.ci_name)
1660                 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1661
1662         sd = root->d_fsdata;
1663         link_group(to_config_group(sd->s_element), group);
1664
1665         mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
1666
1667         err = -ENOMEM;
1668         dentry = d_alloc_name(root, group->cg_item.ci_name);
1669         if (dentry) {
1670                 d_add(dentry, NULL);
1671
1672                 err = configfs_attach_group(sd->s_element, &group->cg_item,
1673                                             dentry);
1674                 if (err) {
1675                         BUG_ON(dentry->d_inode);
1676                         d_drop(dentry);
1677                         dput(dentry);
1678                 } else {
1679                         spin_lock(&configfs_dirent_lock);
1680                         configfs_dir_set_ready(dentry->d_fsdata);
1681                         spin_unlock(&configfs_dirent_lock);
1682                 }
1683         }
1684
1685         mutex_unlock(&root->d_inode->i_mutex);
1686
1687         if (err) {
1688                 unlink_group(group);
1689                 configfs_release_fs();
1690         }
1691
1692         return err;
1693 }
1694
1695 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1696 {
1697         struct config_group *group = &subsys->su_group;
1698         struct dentry *dentry = group->cg_item.ci_dentry;
1699         struct dentry *root = dentry->d_sb->s_root;
1700
1701         if (dentry->d_parent != root) {
1702                 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1703                 return;
1704         }
1705
1706         mutex_lock_nested(&root->d_inode->i_mutex,
1707                           I_MUTEX_PARENT);
1708         mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1709         mutex_lock(&configfs_symlink_mutex);
1710         spin_lock(&configfs_dirent_lock);
1711         if (configfs_detach_prep(dentry, NULL)) {
1712                 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1713         }
1714         spin_unlock(&configfs_dirent_lock);
1715         mutex_unlock(&configfs_symlink_mutex);
1716         configfs_detach_group(&group->cg_item);
1717         dentry->d_inode->i_flags |= S_DEAD;
1718         dont_mount(dentry);
1719         mutex_unlock(&dentry->d_inode->i_mutex);
1720
1721         d_delete(dentry);
1722
1723         mutex_unlock(&root->d_inode->i_mutex);
1724
1725         dput(dentry);
1726
1727         unlink_group(group);
1728         configfs_release_fs();
1729 }
1730
1731 EXPORT_SYMBOL(configfs_register_subsystem);
1732 EXPORT_SYMBOL(configfs_unregister_subsystem);