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