configfs: accessing item hierarchy during rmdir(2)
[safe/jmp/linux-2.6] / 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
34 #include <linux/configfs.h>
35 #include "configfs_internal.h"
36
37 DECLARE_RWSEM(configfs_rename_sem);
38
39 static void configfs_d_iput(struct dentry * dentry,
40                             struct inode * inode)
41 {
42         struct configfs_dirent * sd = dentry->d_fsdata;
43
44         if (sd) {
45                 BUG_ON(sd->s_dentry != dentry);
46                 sd->s_dentry = NULL;
47                 configfs_put(sd);
48         }
49         iput(inode);
50 }
51
52 /*
53  * We _must_ delete our dentries on last dput, as the chain-to-parent
54  * behavior is required to clear the parents of default_groups.
55  */
56 static int configfs_d_delete(struct dentry *dentry)
57 {
58         return 1;
59 }
60
61 static struct dentry_operations configfs_dentry_ops = {
62         .d_iput         = configfs_d_iput,
63         /* simple_delete_dentry() isn't exported */
64         .d_delete       = configfs_d_delete,
65 };
66
67 /*
68  * Allocates a new configfs_dirent and links it to the parent configfs_dirent
69  */
70 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
71                                                 void * element)
72 {
73         struct configfs_dirent * sd;
74
75         sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
76         if (!sd)
77                 return NULL;
78
79         atomic_set(&sd->s_count, 1);
80         INIT_LIST_HEAD(&sd->s_links);
81         INIT_LIST_HEAD(&sd->s_children);
82         list_add(&sd->s_sibling, &parent_sd->s_children);
83         sd->s_element = element;
84
85         return sd;
86 }
87
88 /*
89  *
90  * Return -EEXIST if there is already a configfs element with the same
91  * name for the same parent.
92  *
93  * called with parent inode's i_mutex held
94  */
95 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
96                                   const unsigned char *new)
97 {
98         struct configfs_dirent * sd;
99
100         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
101                 if (sd->s_element) {
102                         const unsigned char *existing = configfs_get_name(sd);
103                         if (strcmp(existing, new))
104                                 continue;
105                         else
106                                 return -EEXIST;
107                 }
108         }
109
110         return 0;
111 }
112
113
114 int configfs_make_dirent(struct configfs_dirent * parent_sd,
115                          struct dentry * dentry, void * element,
116                          umode_t mode, int type)
117 {
118         struct configfs_dirent * sd;
119
120         sd = configfs_new_dirent(parent_sd, element);
121         if (!sd)
122                 return -ENOMEM;
123
124         sd->s_mode = mode;
125         sd->s_type = type;
126         sd->s_dentry = dentry;
127         if (dentry) {
128                 dentry->d_fsdata = configfs_get(sd);
129                 dentry->d_op = &configfs_dentry_ops;
130         }
131
132         return 0;
133 }
134
135 static int init_dir(struct inode * inode)
136 {
137         inode->i_op = &configfs_dir_inode_operations;
138         inode->i_fop = &configfs_dir_operations;
139
140         /* directory inodes start off with i_nlink == 2 (for "." entry) */
141         inc_nlink(inode);
142         return 0;
143 }
144
145 static int init_file(struct inode * inode)
146 {
147         inode->i_size = PAGE_SIZE;
148         inode->i_fop = &configfs_file_operations;
149         return 0;
150 }
151
152 static int init_symlink(struct inode * inode)
153 {
154         inode->i_op = &configfs_symlink_inode_operations;
155         return 0;
156 }
157
158 static int create_dir(struct config_item * k, struct dentry * p,
159                       struct dentry * d)
160 {
161         int error;
162         umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
163
164         error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
165         if (!error)
166                 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
167                                              CONFIGFS_DIR);
168         if (!error) {
169                 error = configfs_create(d, mode, init_dir);
170                 if (!error) {
171                         inc_nlink(p->d_inode);
172                         (d)->d_op = &configfs_dentry_ops;
173                 } else {
174                         struct configfs_dirent *sd = d->d_fsdata;
175                         if (sd) {
176                                 list_del_init(&sd->s_sibling);
177                                 configfs_put(sd);
178                         }
179                 }
180         }
181         return error;
182 }
183
184
185 /**
186  *      configfs_create_dir - create a directory for an config_item.
187  *      @item:          config_itemwe're creating directory for.
188  *      @dentry:        config_item's dentry.
189  */
190
191 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
192 {
193         struct dentry * parent;
194         int error = 0;
195
196         BUG_ON(!item);
197
198         if (item->ci_parent)
199                 parent = item->ci_parent->ci_dentry;
200         else if (configfs_mount && configfs_mount->mnt_sb)
201                 parent = configfs_mount->mnt_sb->s_root;
202         else
203                 return -EFAULT;
204
205         error = create_dir(item,parent,dentry);
206         if (!error)
207                 item->ci_dentry = dentry;
208         return error;
209 }
210
211 int configfs_create_link(struct configfs_symlink *sl,
212                          struct dentry *parent,
213                          struct dentry *dentry)
214 {
215         int err = 0;
216         umode_t mode = S_IFLNK | S_IRWXUGO;
217
218         err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
219                                    CONFIGFS_ITEM_LINK);
220         if (!err) {
221                 err = configfs_create(dentry, mode, init_symlink);
222                 if (!err)
223                         dentry->d_op = &configfs_dentry_ops;
224                 else {
225                         struct configfs_dirent *sd = dentry->d_fsdata;
226                         if (sd) {
227                                 list_del_init(&sd->s_sibling);
228                                 configfs_put(sd);
229                         }
230                 }
231         }
232         return err;
233 }
234
235 static void remove_dir(struct dentry * d)
236 {
237         struct dentry * parent = dget(d->d_parent);
238         struct configfs_dirent * sd;
239
240         sd = d->d_fsdata;
241         list_del_init(&sd->s_sibling);
242         configfs_put(sd);
243         if (d->d_inode)
244                 simple_rmdir(parent->d_inode,d);
245
246         pr_debug(" o %s removing done (%d)\n",d->d_name.name,
247                  atomic_read(&d->d_count));
248
249         dput(parent);
250 }
251
252 /**
253  * configfs_remove_dir - remove an config_item's directory.
254  * @item:       config_item we're removing.
255  *
256  * The only thing special about this is that we remove any files in
257  * the directory before we remove the directory, and we've inlined
258  * what used to be configfs_rmdir() below, instead of calling separately.
259  */
260
261 static void configfs_remove_dir(struct config_item * item)
262 {
263         struct dentry * dentry = dget(item->ci_dentry);
264
265         if (!dentry)
266                 return;
267
268         remove_dir(dentry);
269         /**
270          * Drop reference from dget() on entrance.
271          */
272         dput(dentry);
273 }
274
275
276 /* attaches attribute's configfs_dirent to the dentry corresponding to the
277  * attribute file
278  */
279 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
280 {
281         struct configfs_attribute * attr = sd->s_element;
282         int error;
283
284         dentry->d_fsdata = configfs_get(sd);
285         sd->s_dentry = dentry;
286         error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
287         if (error) {
288                 configfs_put(sd);
289                 return error;
290         }
291
292         dentry->d_op = &configfs_dentry_ops;
293         d_rehash(dentry);
294
295         return 0;
296 }
297
298 static struct dentry * configfs_lookup(struct inode *dir,
299                                        struct dentry *dentry,
300                                        struct nameidata *nd)
301 {
302         struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
303         struct configfs_dirent * sd;
304         int found = 0;
305         int err = 0;
306
307         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
308                 if (sd->s_type & CONFIGFS_NOT_PINNED) {
309                         const unsigned char * name = configfs_get_name(sd);
310
311                         if (strcmp(name, dentry->d_name.name))
312                                 continue;
313
314                         found = 1;
315                         err = configfs_attach_attr(sd, dentry);
316                         break;
317                 }
318         }
319
320         if (!found) {
321                 /*
322                  * If it doesn't exist and it isn't a NOT_PINNED item,
323                  * it must be negative.
324                  */
325                 return simple_lookup(dir, dentry, nd);
326         }
327
328         return ERR_PTR(err);
329 }
330
331 /*
332  * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
333  * attributes and are removed by rmdir().  We recurse, taking i_mutex
334  * on all children that are candidates for default detach.  If the
335  * result is clean, then configfs_detach_group() will handle dropping
336  * i_mutex.  If there is an error, the caller will clean up the i_mutex
337  * holders via configfs_detach_rollback().
338  */
339 static int configfs_detach_prep(struct dentry *dentry)
340 {
341         struct configfs_dirent *parent_sd = dentry->d_fsdata;
342         struct configfs_dirent *sd;
343         int ret;
344
345         ret = -EBUSY;
346         if (!list_empty(&parent_sd->s_links))
347                 goto out;
348
349         ret = 0;
350         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
351                 if (sd->s_type & CONFIGFS_NOT_PINNED)
352                         continue;
353                 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
354                         mutex_lock(&sd->s_dentry->d_inode->i_mutex);
355                         /* Mark that we've taken i_mutex */
356                         sd->s_type |= CONFIGFS_USET_DROPPING;
357
358                         ret = configfs_detach_prep(sd->s_dentry);
359                         if (!ret)
360                                 continue;
361                 } else
362                         ret = -ENOTEMPTY;
363
364                 break;
365         }
366
367 out:
368         return ret;
369 }
370
371 /*
372  * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
373  * set.
374  */
375 static void configfs_detach_rollback(struct dentry *dentry)
376 {
377         struct configfs_dirent *parent_sd = dentry->d_fsdata;
378         struct configfs_dirent *sd;
379
380         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
381                 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
382                         configfs_detach_rollback(sd->s_dentry);
383
384                         if (sd->s_type & CONFIGFS_USET_DROPPING) {
385                                 sd->s_type &= ~CONFIGFS_USET_DROPPING;
386                                 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
387                         }
388                 }
389         }
390 }
391
392 static void detach_attrs(struct config_item * item)
393 {
394         struct dentry * dentry = dget(item->ci_dentry);
395         struct configfs_dirent * parent_sd;
396         struct configfs_dirent * sd, * tmp;
397
398         if (!dentry)
399                 return;
400
401         pr_debug("configfs %s: dropping attrs for  dir\n",
402                  dentry->d_name.name);
403
404         parent_sd = dentry->d_fsdata;
405         list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
406                 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
407                         continue;
408                 list_del_init(&sd->s_sibling);
409                 configfs_drop_dentry(sd, dentry);
410                 configfs_put(sd);
411         }
412
413         /**
414          * Drop reference from dget() on entrance.
415          */
416         dput(dentry);
417 }
418
419 static int populate_attrs(struct config_item *item)
420 {
421         struct config_item_type *t = item->ci_type;
422         struct configfs_attribute *attr;
423         int error = 0;
424         int i;
425
426         if (!t)
427                 return -EINVAL;
428         if (t->ct_attrs) {
429                 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
430                         if ((error = configfs_create_file(item, attr)))
431                                 break;
432                 }
433         }
434
435         if (error)
436                 detach_attrs(item);
437
438         return error;
439 }
440
441 static int configfs_attach_group(struct config_item *parent_item,
442                                  struct config_item *item,
443                                  struct dentry *dentry);
444 static void configfs_detach_group(struct config_item *item);
445
446 static void detach_groups(struct config_group *group)
447 {
448         struct dentry * dentry = dget(group->cg_item.ci_dentry);
449         struct dentry *child;
450         struct configfs_dirent *parent_sd;
451         struct configfs_dirent *sd, *tmp;
452
453         if (!dentry)
454                 return;
455
456         parent_sd = dentry->d_fsdata;
457         list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
458                 if (!sd->s_element ||
459                     !(sd->s_type & CONFIGFS_USET_DEFAULT))
460                         continue;
461
462                 child = sd->s_dentry;
463
464                 configfs_detach_group(sd->s_element);
465                 child->d_inode->i_flags |= S_DEAD;
466
467                 /*
468                  * From rmdir/unregister, a configfs_detach_prep() pass
469                  * has taken our i_mutex for us.  Drop it.
470                  * From mkdir/register cleanup, there is no sem held.
471                  */
472                 if (sd->s_type & CONFIGFS_USET_DROPPING)
473                         mutex_unlock(&child->d_inode->i_mutex);
474
475                 d_delete(child);
476                 dput(child);
477         }
478
479         /**
480          * Drop reference from dget() on entrance.
481          */
482         dput(dentry);
483 }
484
485 /*
486  * This fakes mkdir(2) on a default_groups[] entry.  It
487  * creates a dentry, attachs it, and then does fixup
488  * on the sd->s_type.
489  *
490  * We could, perhaps, tweak our parent's ->mkdir for a minute and
491  * try using vfs_mkdir.  Just a thought.
492  */
493 static int create_default_group(struct config_group *parent_group,
494                                 struct config_group *group)
495 {
496         int ret;
497         struct qstr name;
498         struct configfs_dirent *sd;
499         /* We trust the caller holds a reference to parent */
500         struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
501
502         if (!group->cg_item.ci_name)
503                 group->cg_item.ci_name = group->cg_item.ci_namebuf;
504         name.name = group->cg_item.ci_name;
505         name.len = strlen(name.name);
506         name.hash = full_name_hash(name.name, name.len);
507
508         ret = -ENOMEM;
509         child = d_alloc(parent, &name);
510         if (child) {
511                 d_add(child, NULL);
512
513                 ret = configfs_attach_group(&parent_group->cg_item,
514                                             &group->cg_item, child);
515                 if (!ret) {
516                         sd = child->d_fsdata;
517                         sd->s_type |= CONFIGFS_USET_DEFAULT;
518                 } else {
519                         d_delete(child);
520                         dput(child);
521                 }
522         }
523
524         return ret;
525 }
526
527 static int populate_groups(struct config_group *group)
528 {
529         struct config_group *new_group;
530         struct dentry *dentry = group->cg_item.ci_dentry;
531         int ret = 0;
532         int i;
533
534         if (group->default_groups) {
535                 /*
536                  * FYI, we're faking mkdir here
537                  * I'm not sure we need this semaphore, as we're called
538                  * from our parent's mkdir.  That holds our parent's
539                  * i_mutex, so afaik lookup cannot continue through our
540                  * parent to find us, let alone mess with our tree.
541                  * That said, taking our i_mutex is closer to mkdir
542                  * emulation, and shouldn't hurt.
543                  */
544                 mutex_lock(&dentry->d_inode->i_mutex);
545
546                 for (i = 0; group->default_groups[i]; i++) {
547                         new_group = group->default_groups[i];
548
549                         ret = create_default_group(group, new_group);
550                         if (ret)
551                                 break;
552                 }
553
554                 mutex_unlock(&dentry->d_inode->i_mutex);
555         }
556
557         if (ret)
558                 detach_groups(group);
559
560         return ret;
561 }
562
563 /*
564  * All of link_obj/unlink_obj/link_group/unlink_group require that
565  * subsys->su_mutex is held.
566  */
567
568 static void unlink_obj(struct config_item *item)
569 {
570         struct config_group *group;
571
572         group = item->ci_group;
573         if (group) {
574                 list_del_init(&item->ci_entry);
575
576                 item->ci_group = NULL;
577                 item->ci_parent = NULL;
578
579                 /* Drop the reference for ci_entry */
580                 config_item_put(item);
581
582                 /* Drop the reference for ci_parent */
583                 config_group_put(group);
584         }
585 }
586
587 static void link_obj(struct config_item *parent_item, struct config_item *item)
588 {
589         /*
590          * Parent seems redundant with group, but it makes certain
591          * traversals much nicer.
592          */
593         item->ci_parent = parent_item;
594
595         /*
596          * We hold a reference on the parent for the child's ci_parent
597          * link.
598          */
599         item->ci_group = config_group_get(to_config_group(parent_item));
600         list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
601
602         /*
603          * We hold a reference on the child for ci_entry on the parent's
604          * cg_children
605          */
606         config_item_get(item);
607 }
608
609 static void unlink_group(struct config_group *group)
610 {
611         int i;
612         struct config_group *new_group;
613
614         if (group->default_groups) {
615                 for (i = 0; group->default_groups[i]; i++) {
616                         new_group = group->default_groups[i];
617                         unlink_group(new_group);
618                 }
619         }
620
621         group->cg_subsys = NULL;
622         unlink_obj(&group->cg_item);
623 }
624
625 static void link_group(struct config_group *parent_group, struct config_group *group)
626 {
627         int i;
628         struct config_group *new_group;
629         struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
630
631         link_obj(&parent_group->cg_item, &group->cg_item);
632
633         if (parent_group->cg_subsys)
634                 subsys = parent_group->cg_subsys;
635         else if (configfs_is_root(&parent_group->cg_item))
636                 subsys = to_configfs_subsystem(group);
637         else
638                 BUG();
639         group->cg_subsys = subsys;
640
641         if (group->default_groups) {
642                 for (i = 0; group->default_groups[i]; i++) {
643                         new_group = group->default_groups[i];
644                         link_group(group, new_group);
645                 }
646         }
647 }
648
649 /*
650  * The goal is that configfs_attach_item() (and
651  * configfs_attach_group()) can be called from either the VFS or this
652  * module.  That is, they assume that the items have been created,
653  * the dentry allocated, and the dcache is all ready to go.
654  *
655  * If they fail, they must clean up after themselves as if they
656  * had never been called.  The caller (VFS or local function) will
657  * handle cleaning up the dcache bits.
658  *
659  * configfs_detach_group() and configfs_detach_item() behave similarly on
660  * the way out.  They assume that the proper semaphores are held, they
661  * clean up the configfs items, and they expect their callers will
662  * handle the dcache bits.
663  */
664 static int configfs_attach_item(struct config_item *parent_item,
665                                 struct config_item *item,
666                                 struct dentry *dentry)
667 {
668         int ret;
669
670         ret = configfs_create_dir(item, dentry);
671         if (!ret) {
672                 ret = populate_attrs(item);
673                 if (ret) {
674                         configfs_remove_dir(item);
675                         d_delete(dentry);
676                 }
677         }
678
679         return ret;
680 }
681
682 static void configfs_detach_item(struct config_item *item)
683 {
684         detach_attrs(item);
685         configfs_remove_dir(item);
686 }
687
688 static int configfs_attach_group(struct config_item *parent_item,
689                                  struct config_item *item,
690                                  struct dentry *dentry)
691 {
692         int ret;
693         struct configfs_dirent *sd;
694
695         ret = configfs_attach_item(parent_item, item, dentry);
696         if (!ret) {
697                 sd = dentry->d_fsdata;
698                 sd->s_type |= CONFIGFS_USET_DIR;
699
700                 ret = populate_groups(to_config_group(item));
701                 if (ret) {
702                         configfs_detach_item(item);
703                         d_delete(dentry);
704                 }
705         }
706
707         return ret;
708 }
709
710 static void configfs_detach_group(struct config_item *item)
711 {
712         detach_groups(to_config_group(item));
713         configfs_detach_item(item);
714 }
715
716 /*
717  * After the item has been detached from the filesystem view, we are
718  * ready to tear it out of the hierarchy.  Notify the client before
719  * we do that so they can perform any cleanup that requires
720  * navigating the hierarchy.  A client does not need to provide this
721  * callback.  The subsystem semaphore MUST be held by the caller, and
722  * references must be valid for both items.  It also assumes the
723  * caller has validated ci_type.
724  */
725 static void client_disconnect_notify(struct config_item *parent_item,
726                                      struct config_item *item)
727 {
728         struct config_item_type *type;
729
730         type = parent_item->ci_type;
731         BUG_ON(!type);
732
733         if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
734                 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
735                                                       item);
736 }
737
738 /*
739  * Drop the initial reference from make_item()/make_group()
740  * This function assumes that reference is held on item
741  * and that item holds a valid reference to the parent.  Also, it
742  * assumes the caller has validated ci_type.
743  */
744 static void client_drop_item(struct config_item *parent_item,
745                              struct config_item *item)
746 {
747         struct config_item_type *type;
748
749         type = parent_item->ci_type;
750         BUG_ON(!type);
751
752         /*
753          * If ->drop_item() exists, it is responsible for the
754          * config_item_put().
755          */
756         if (type->ct_group_ops && type->ct_group_ops->drop_item)
757                 type->ct_group_ops->drop_item(to_config_group(parent_item),
758                                               item);
759         else
760                 config_item_put(item);
761 }
762
763
764 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
765 {
766         int ret, module_got = 0;
767         struct config_group *group;
768         struct config_item *item;
769         struct config_item *parent_item;
770         struct configfs_subsystem *subsys;
771         struct configfs_dirent *sd;
772         struct config_item_type *type;
773         struct module *owner = NULL;
774         char *name;
775
776         if (dentry->d_parent == configfs_sb->s_root) {
777                 ret = -EPERM;
778                 goto out;
779         }
780
781         sd = dentry->d_parent->d_fsdata;
782         if (!(sd->s_type & CONFIGFS_USET_DIR)) {
783                 ret = -EPERM;
784                 goto out;
785         }
786
787         /* Get a working ref for the duration of this function */
788         parent_item = configfs_get_config_item(dentry->d_parent);
789         type = parent_item->ci_type;
790         subsys = to_config_group(parent_item)->cg_subsys;
791         BUG_ON(!subsys);
792
793         if (!type || !type->ct_group_ops ||
794             (!type->ct_group_ops->make_group &&
795              !type->ct_group_ops->make_item)) {
796                 ret = -EPERM;  /* Lack-of-mkdir returns -EPERM */
797                 goto out_put;
798         }
799
800         name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
801         if (!name) {
802                 ret = -ENOMEM;
803                 goto out_put;
804         }
805
806         snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
807
808         mutex_lock(&subsys->su_mutex);
809         group = NULL;
810         item = NULL;
811         if (type->ct_group_ops->make_group) {
812                 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
813                 if (group) {
814                         link_group(to_config_group(parent_item), group);
815                         item = &group->cg_item;
816                 }
817         } else {
818                 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
819                 if (item)
820                         link_obj(parent_item, item);
821         }
822         mutex_unlock(&subsys->su_mutex);
823
824         kfree(name);
825         if (!item) {
826                 /*
827                  * If item == NULL, then link_obj() was never called.
828                  * There are no extra references to clean up.
829                  */
830                 ret = -ENOMEM;
831                 goto out_put;
832         }
833
834         /*
835          * link_obj() has been called (via link_group() for groups).
836          * From here on out, errors must clean that up.
837          */
838
839         type = item->ci_type;
840         if (!type) {
841                 ret = -EINVAL;
842                 goto out_unlink;
843         }
844
845         owner = type->ct_owner;
846         if (!try_module_get(owner)) {
847                 ret = -EINVAL;
848                 goto out_unlink;
849         }
850
851         /*
852          * I hate doing it this way, but if there is
853          * an error,  module_put() probably should
854          * happen after any cleanup.
855          */
856         module_got = 1;
857
858         if (group)
859                 ret = configfs_attach_group(parent_item, item, dentry);
860         else
861                 ret = configfs_attach_item(parent_item, item, dentry);
862
863 out_unlink:
864         if (ret) {
865                 /* Tear down everything we built up */
866                 mutex_lock(&subsys->su_mutex);
867
868                 client_disconnect_notify(parent_item, item);
869                 if (group)
870                         unlink_group(group);
871                 else
872                         unlink_obj(item);
873                 client_drop_item(parent_item, item);
874
875                 mutex_unlock(&subsys->su_mutex);
876
877                 if (module_got)
878                         module_put(owner);
879         }
880
881 out_put:
882         /*
883          * link_obj()/link_group() took a reference from child->parent,
884          * so the parent is safely pinned.  We can drop our working
885          * reference.
886          */
887         config_item_put(parent_item);
888
889 out:
890         return ret;
891 }
892
893 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
894 {
895         struct config_item *parent_item;
896         struct config_item *item;
897         struct configfs_subsystem *subsys;
898         struct configfs_dirent *sd;
899         struct module *owner = NULL;
900         int ret;
901
902         if (dentry->d_parent == configfs_sb->s_root)
903                 return -EPERM;
904
905         sd = dentry->d_fsdata;
906         if (sd->s_type & CONFIGFS_USET_DEFAULT)
907                 return -EPERM;
908
909         /* Get a working ref until we have the child */
910         parent_item = configfs_get_config_item(dentry->d_parent);
911         subsys = to_config_group(parent_item)->cg_subsys;
912         BUG_ON(!subsys);
913
914         if (!parent_item->ci_type) {
915                 config_item_put(parent_item);
916                 return -EINVAL;
917         }
918
919         ret = configfs_detach_prep(dentry);
920         if (ret) {
921                 configfs_detach_rollback(dentry);
922                 config_item_put(parent_item);
923                 return ret;
924         }
925
926         /* Get a working ref for the duration of this function */
927         item = configfs_get_config_item(dentry);
928
929         /* Drop reference from above, item already holds one. */
930         config_item_put(parent_item);
931
932         if (item->ci_type)
933                 owner = item->ci_type->ct_owner;
934
935         if (sd->s_type & CONFIGFS_USET_DIR) {
936                 configfs_detach_group(item);
937
938                 mutex_lock(&subsys->su_mutex);
939                 client_disconnect_notify(parent_item, item);
940                 unlink_group(to_config_group(item));
941         } else {
942                 configfs_detach_item(item);
943
944                 mutex_lock(&subsys->su_mutex);
945                 client_disconnect_notify(parent_item, item);
946                 unlink_obj(item);
947         }
948
949         client_drop_item(parent_item, item);
950         mutex_unlock(&subsys->su_mutex);
951
952         /* Drop our reference from above */
953         config_item_put(item);
954
955         module_put(owner);
956
957         return 0;
958 }
959
960 const struct inode_operations configfs_dir_inode_operations = {
961         .mkdir          = configfs_mkdir,
962         .rmdir          = configfs_rmdir,
963         .symlink        = configfs_symlink,
964         .unlink         = configfs_unlink,
965         .lookup         = configfs_lookup,
966         .setattr        = configfs_setattr,
967 };
968
969 #if 0
970 int configfs_rename_dir(struct config_item * item, const char *new_name)
971 {
972         int error = 0;
973         struct dentry * new_dentry, * parent;
974
975         if (!strcmp(config_item_name(item), new_name))
976                 return -EINVAL;
977
978         if (!item->parent)
979                 return -EINVAL;
980
981         down_write(&configfs_rename_sem);
982         parent = item->parent->dentry;
983
984         mutex_lock(&parent->d_inode->i_mutex);
985
986         new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
987         if (!IS_ERR(new_dentry)) {
988                 if (!new_dentry->d_inode) {
989                         error = config_item_set_name(item, "%s", new_name);
990                         if (!error) {
991                                 d_add(new_dentry, NULL);
992                                 d_move(item->dentry, new_dentry);
993                         }
994                         else
995                                 d_delete(new_dentry);
996                 } else
997                         error = -EEXIST;
998                 dput(new_dentry);
999         }
1000         mutex_unlock(&parent->d_inode->i_mutex);
1001         up_write(&configfs_rename_sem);
1002
1003         return error;
1004 }
1005 #endif
1006
1007 static int configfs_dir_open(struct inode *inode, struct file *file)
1008 {
1009         struct dentry * dentry = file->f_path.dentry;
1010         struct configfs_dirent * parent_sd = dentry->d_fsdata;
1011
1012         mutex_lock(&dentry->d_inode->i_mutex);
1013         file->private_data = configfs_new_dirent(parent_sd, NULL);
1014         mutex_unlock(&dentry->d_inode->i_mutex);
1015
1016         return file->private_data ? 0 : -ENOMEM;
1017
1018 }
1019
1020 static int configfs_dir_close(struct inode *inode, struct file *file)
1021 {
1022         struct dentry * dentry = file->f_path.dentry;
1023         struct configfs_dirent * cursor = file->private_data;
1024
1025         mutex_lock(&dentry->d_inode->i_mutex);
1026         list_del_init(&cursor->s_sibling);
1027         mutex_unlock(&dentry->d_inode->i_mutex);
1028
1029         release_configfs_dirent(cursor);
1030
1031         return 0;
1032 }
1033
1034 /* Relationship between s_mode and the DT_xxx types */
1035 static inline unsigned char dt_type(struct configfs_dirent *sd)
1036 {
1037         return (sd->s_mode >> 12) & 15;
1038 }
1039
1040 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1041 {
1042         struct dentry *dentry = filp->f_path.dentry;
1043         struct configfs_dirent * parent_sd = dentry->d_fsdata;
1044         struct configfs_dirent *cursor = filp->private_data;
1045         struct list_head *p, *q = &cursor->s_sibling;
1046         ino_t ino;
1047         int i = filp->f_pos;
1048
1049         switch (i) {
1050                 case 0:
1051                         ino = dentry->d_inode->i_ino;
1052                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1053                                 break;
1054                         filp->f_pos++;
1055                         i++;
1056                         /* fallthrough */
1057                 case 1:
1058                         ino = parent_ino(dentry);
1059                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1060                                 break;
1061                         filp->f_pos++;
1062                         i++;
1063                         /* fallthrough */
1064                 default:
1065                         if (filp->f_pos == 2) {
1066                                 list_move(q, &parent_sd->s_children);
1067                         }
1068                         for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1069                                 struct configfs_dirent *next;
1070                                 const char * name;
1071                                 int len;
1072
1073                                 next = list_entry(p, struct configfs_dirent,
1074                                                    s_sibling);
1075                                 if (!next->s_element)
1076                                         continue;
1077
1078                                 name = configfs_get_name(next);
1079                                 len = strlen(name);
1080                                 if (next->s_dentry)
1081                                         ino = next->s_dentry->d_inode->i_ino;
1082                                 else
1083                                         ino = iunique(configfs_sb, 2);
1084
1085                                 if (filldir(dirent, name, len, filp->f_pos, ino,
1086                                                  dt_type(next)) < 0)
1087                                         return 0;
1088
1089                                 list_move(q, p);
1090                                 p = q;
1091                                 filp->f_pos++;
1092                         }
1093         }
1094         return 0;
1095 }
1096
1097 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1098 {
1099         struct dentry * dentry = file->f_path.dentry;
1100
1101         mutex_lock(&dentry->d_inode->i_mutex);
1102         switch (origin) {
1103                 case 1:
1104                         offset += file->f_pos;
1105                 case 0:
1106                         if (offset >= 0)
1107                                 break;
1108                 default:
1109                         mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
1110                         return -EINVAL;
1111         }
1112         if (offset != file->f_pos) {
1113                 file->f_pos = offset;
1114                 if (file->f_pos >= 2) {
1115                         struct configfs_dirent *sd = dentry->d_fsdata;
1116                         struct configfs_dirent *cursor = file->private_data;
1117                         struct list_head *p;
1118                         loff_t n = file->f_pos - 2;
1119
1120                         list_del(&cursor->s_sibling);
1121                         p = sd->s_children.next;
1122                         while (n && p != &sd->s_children) {
1123                                 struct configfs_dirent *next;
1124                                 next = list_entry(p, struct configfs_dirent,
1125                                                    s_sibling);
1126                                 if (next->s_element)
1127                                         n--;
1128                                 p = p->next;
1129                         }
1130                         list_add_tail(&cursor->s_sibling, p);
1131                 }
1132         }
1133         mutex_unlock(&dentry->d_inode->i_mutex);
1134         return offset;
1135 }
1136
1137 const struct file_operations configfs_dir_operations = {
1138         .open           = configfs_dir_open,
1139         .release        = configfs_dir_close,
1140         .llseek         = configfs_dir_lseek,
1141         .read           = generic_read_dir,
1142         .readdir        = configfs_readdir,
1143 };
1144
1145 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1146 {
1147         int err;
1148         struct config_group *group = &subsys->su_group;
1149         struct qstr name;
1150         struct dentry *dentry;
1151         struct configfs_dirent *sd;
1152
1153         err = configfs_pin_fs();
1154         if (err)
1155                 return err;
1156
1157         if (!group->cg_item.ci_name)
1158                 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1159
1160         sd = configfs_sb->s_root->d_fsdata;
1161         link_group(to_config_group(sd->s_element), group);
1162
1163         mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
1164
1165         name.name = group->cg_item.ci_name;
1166         name.len = strlen(name.name);
1167         name.hash = full_name_hash(name.name, name.len);
1168
1169         err = -ENOMEM;
1170         dentry = d_alloc(configfs_sb->s_root, &name);
1171         if (dentry) {
1172                 d_add(dentry, NULL);
1173
1174                 err = configfs_attach_group(sd->s_element, &group->cg_item,
1175                                             dentry);
1176                 if (err) {
1177                         d_delete(dentry);
1178                         dput(dentry);
1179                 }
1180         }
1181
1182         mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1183
1184         if (err) {
1185                 unlink_group(group);
1186                 configfs_release_fs();
1187         }
1188
1189         return err;
1190 }
1191
1192 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1193 {
1194         struct config_group *group = &subsys->su_group;
1195         struct dentry *dentry = group->cg_item.ci_dentry;
1196
1197         if (dentry->d_parent != configfs_sb->s_root) {
1198                 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1199                 return;
1200         }
1201
1202         mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1203                           I_MUTEX_PARENT);
1204         mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1205         if (configfs_detach_prep(dentry)) {
1206                 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1207         }
1208         configfs_detach_group(&group->cg_item);
1209         dentry->d_inode->i_flags |= S_DEAD;
1210         mutex_unlock(&dentry->d_inode->i_mutex);
1211
1212         d_delete(dentry);
1213
1214         mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1215
1216         dput(dentry);
1217
1218         unlink_group(group);
1219         configfs_release_fs();
1220 }
1221
1222 EXPORT_SYMBOL(configfs_register_subsystem);
1223 EXPORT_SYMBOL(configfs_unregister_subsystem);