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