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