blob: 5f952187fc53575795358785cdb586bd7093b8c9 [file] [log] [blame]
Joel Becker7063fbf2005-12-15 14:29:43 -08001/* -*- 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
Joel Becker3d0f89b2006-01-25 13:31:07 -080075 sd = kmem_cache_alloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -080076 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
Joel Becker3d0f89b2006-01-25 13:31:07 -0800139 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
140 CONFIGFS_DIR);
Joel Becker7063fbf2005-12-15 14:29:43 -0800141 if (!error) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800142 error = configfs_create(d, mode, init_dir);
Joel Becker7063fbf2005-12-15 14:29:43 -0800143 if (!error) {
144 p->d_inode->i_nlink++;
145 (d)->d_op = &configfs_dentry_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800146 } else {
147 struct configfs_dirent *sd = d->d_fsdata;
148 if (sd) {
149 list_del_init(&sd->s_sibling);
150 configfs_put(sd);
151 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800152 }
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
Joel Becker3d0f89b2006-01-25 13:31:07 -0800191 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
192 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800193 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800194 err = configfs_create(dentry, mode, init_symlink);
Joel Becker7063fbf2005-12-15 14:29:43 -0800195 if (!err)
196 dentry->d_op = &configfs_dentry_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800197 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 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800204 }
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
Joel Becker7063fbf2005-12-15 14:29:43 -0800257 dentry->d_fsdata = configfs_get(sd);
258 sd->s_dentry = dentry;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800259 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
260 if (error) {
261 configfs_put(sd);
262 return error;
263 }
264
265 dentry->d_op = &configfs_dentry_ops;
Joel Becker7063fbf2005-12-15 14:29:43 -0800266 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
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800306 * attributes and are removed by rmdir(). We recurse, taking i_mutex
Joel Becker7063fbf2005-12-15 14:29:43 -0800307 * on all children that are candidates for default detach. If the
308 * result is clean, then configfs_detach_group() will handle dropping
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800309 * i_mutex. If there is an error, the caller will clean up the i_mutex
Joel Becker7063fbf2005-12-15 14:29:43 -0800310 * 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) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800327 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
328 /* Mark that we've taken i_mutex */
Joel Becker7063fbf2005-12-15 14:29:43 -0800329 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/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800345 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
Joel Becker7063fbf2005-12-15 14:29:43 -0800346 * 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;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800359 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800360 }
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
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800442 * has taken our i_mutex for us. Drop it.
Joel Becker7063fbf2005-12-15 14:29:43 -0800443 * From mkdir/register cleanup, there is no sem held.
444 */
445 if (sd->s_type & CONFIGFS_USET_DROPPING)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800446 mutex_unlock(&child->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800447
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
Eric Sesterhenncbca6922006-03-23 00:36:54 +0100507 if (group->default_groups) {
Joel Beckereed7a0d2006-04-11 21:37:20 -0700508 /*
509 * FYI, we're faking mkdir here
Joel Becker7063fbf2005-12-15 14:29:43 -0800510 * I'm not sure we need this semaphore, as we're called
511 * from our parent's mkdir. That holds our parent's
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800512 * i_mutex, so afaik lookup cannot continue through our
Joel Becker7063fbf2005-12-15 14:29:43 -0800513 * parent to find us, let alone mess with our tree.
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800514 * That said, taking our i_mutex is closer to mkdir
Joel Beckereed7a0d2006-04-11 21:37:20 -0700515 * emulation, and shouldn't hurt.
516 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800517 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800518
519 for (i = 0; group->default_groups[i]; i++) {
520 new_group = group->default_groups[i];
521
522 ret = create_default_group(group, new_group);
523 if (ret)
524 break;
525 }
526
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800527 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800528 }
529
530 if (ret)
531 detach_groups(group);
532
533 return ret;
534}
535
536/*
537 * All of link_obj/unlink_obj/link_group/unlink_group require that
538 * subsys->su_sem is held.
539 */
540
541static void unlink_obj(struct config_item *item)
542{
543 struct config_group *group;
544
545 group = item->ci_group;
546 if (group) {
547 list_del_init(&item->ci_entry);
548
549 item->ci_group = NULL;
550 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700551
552 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800553 config_item_put(item);
554
Joel Beckereed7a0d2006-04-11 21:37:20 -0700555 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800556 config_group_put(group);
557 }
558}
559
560static void link_obj(struct config_item *parent_item, struct config_item *item)
561{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700562 /*
563 * Parent seems redundant with group, but it makes certain
564 * traversals much nicer.
565 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800566 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700567
568 /*
569 * We hold a reference on the parent for the child's ci_parent
570 * link.
571 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800572 item->ci_group = config_group_get(to_config_group(parent_item));
573 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
574
Joel Beckereed7a0d2006-04-11 21:37:20 -0700575 /*
576 * We hold a reference on the child for ci_entry on the parent's
577 * cg_children
578 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800579 config_item_get(item);
580}
581
582static void unlink_group(struct config_group *group)
583{
584 int i;
585 struct config_group *new_group;
586
587 if (group->default_groups) {
588 for (i = 0; group->default_groups[i]; i++) {
589 new_group = group->default_groups[i];
590 unlink_group(new_group);
591 }
592 }
593
594 group->cg_subsys = NULL;
595 unlink_obj(&group->cg_item);
596}
597
598static void link_group(struct config_group *parent_group, struct config_group *group)
599{
600 int i;
601 struct config_group *new_group;
602 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
603
604 link_obj(&parent_group->cg_item, &group->cg_item);
605
606 if (parent_group->cg_subsys)
607 subsys = parent_group->cg_subsys;
608 else if (configfs_is_root(&parent_group->cg_item))
609 subsys = to_configfs_subsystem(group);
610 else
611 BUG();
612 group->cg_subsys = subsys;
613
614 if (group->default_groups) {
615 for (i = 0; group->default_groups[i]; i++) {
616 new_group = group->default_groups[i];
617 link_group(group, new_group);
618 }
619 }
620}
621
622/*
623 * The goal is that configfs_attach_item() (and
624 * configfs_attach_group()) can be called from either the VFS or this
625 * module. That is, they assume that the items have been created,
626 * the dentry allocated, and the dcache is all ready to go.
627 *
628 * If they fail, they must clean up after themselves as if they
629 * had never been called. The caller (VFS or local function) will
630 * handle cleaning up the dcache bits.
631 *
632 * configfs_detach_group() and configfs_detach_item() behave similarly on
633 * the way out. They assume that the proper semaphores are held, they
634 * clean up the configfs items, and they expect their callers will
635 * handle the dcache bits.
636 */
637static int configfs_attach_item(struct config_item *parent_item,
638 struct config_item *item,
639 struct dentry *dentry)
640{
641 int ret;
642
643 ret = configfs_create_dir(item, dentry);
644 if (!ret) {
645 ret = populate_attrs(item);
646 if (ret) {
647 configfs_remove_dir(item);
648 d_delete(dentry);
649 }
650 }
651
652 return ret;
653}
654
655static void configfs_detach_item(struct config_item *item)
656{
657 detach_attrs(item);
658 configfs_remove_dir(item);
659}
660
661static int configfs_attach_group(struct config_item *parent_item,
662 struct config_item *item,
663 struct dentry *dentry)
664{
665 int ret;
666 struct configfs_dirent *sd;
667
668 ret = configfs_attach_item(parent_item, item, dentry);
669 if (!ret) {
670 sd = dentry->d_fsdata;
671 sd->s_type |= CONFIGFS_USET_DIR;
672
673 ret = populate_groups(to_config_group(item));
674 if (ret) {
675 configfs_detach_item(item);
676 d_delete(dentry);
677 }
678 }
679
680 return ret;
681}
682
683static void configfs_detach_group(struct config_item *item)
684{
685 detach_groups(to_config_group(item));
686 configfs_detach_item(item);
687}
688
689/*
690 * Drop the initial reference from make_item()/make_group()
691 * This function assumes that reference is held on item
692 * and that item holds a valid reference to the parent. Also, it
693 * assumes the caller has validated ci_type.
694 */
695static void client_drop_item(struct config_item *parent_item,
696 struct config_item *item)
697{
698 struct config_item_type *type;
699
700 type = parent_item->ci_type;
701 BUG_ON(!type);
702
Joel Beckereed7a0d2006-04-11 21:37:20 -0700703 /*
704 * If ->drop_item() exists, it is responsible for the
705 * config_item_put().
706 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800707 if (type->ct_group_ops && type->ct_group_ops->drop_item)
708 type->ct_group_ops->drop_item(to_config_group(parent_item),
709 item);
710 else
711 config_item_put(item);
712}
713
714
715static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
716{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700717 int ret, module_got = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800718 struct config_group *group;
719 struct config_item *item;
720 struct config_item *parent_item;
721 struct configfs_subsystem *subsys;
722 struct configfs_dirent *sd;
723 struct config_item_type *type;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700724 struct module *owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800725 char *name;
726
Joel Becker84efad12006-03-27 18:46:09 -0800727 if (dentry->d_parent == configfs_sb->s_root) {
728 ret = -EPERM;
729 goto out;
730 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800731
732 sd = dentry->d_parent->d_fsdata;
Joel Becker84efad12006-03-27 18:46:09 -0800733 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
734 ret = -EPERM;
735 goto out;
736 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800737
Joel Becker84efad12006-03-27 18:46:09 -0800738 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -0800739 parent_item = configfs_get_config_item(dentry->d_parent);
740 type = parent_item->ci_type;
741 subsys = to_config_group(parent_item)->cg_subsys;
742 BUG_ON(!subsys);
743
744 if (!type || !type->ct_group_ops ||
745 (!type->ct_group_ops->make_group &&
746 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -0800747 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
748 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -0800749 }
750
751 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
752 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -0800753 ret = -ENOMEM;
754 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -0800755 }
Joel Becker84efad12006-03-27 18:46:09 -0800756
Joel Becker7063fbf2005-12-15 14:29:43 -0800757 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
758
759 down(&subsys->su_sem);
760 group = NULL;
761 item = NULL;
762 if (type->ct_group_ops->make_group) {
763 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
764 if (group) {
765 link_group(to_config_group(parent_item), group);
766 item = &group->cg_item;
767 }
768 } else {
769 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
770 if (item)
771 link_obj(parent_item, item);
772 }
773 up(&subsys->su_sem);
774
775 kfree(name);
776 if (!item) {
Joel Beckereed7a0d2006-04-11 21:37:20 -0700777 /*
778 * If item == NULL, then link_obj() was never called.
779 * There are no extra references to clean up.
780 */
Joel Becker84efad12006-03-27 18:46:09 -0800781 ret = -ENOMEM;
782 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -0800783 }
784
Joel Beckereed7a0d2006-04-11 21:37:20 -0700785 /*
786 * link_obj() has been called (via link_group() for groups).
787 * From here on out, errors must clean that up.
788 */
789
Joel Becker7063fbf2005-12-15 14:29:43 -0800790 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700791 if (!type) {
792 ret = -EINVAL;
793 goto out_unlink;
794 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800795
Joel Beckereed7a0d2006-04-11 21:37:20 -0700796 owner = type->ct_owner;
797 if (!try_module_get(owner)) {
798 ret = -EINVAL;
799 goto out_unlink;
800 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800801
Joel Beckereed7a0d2006-04-11 21:37:20 -0700802 /*
803 * I hate doing it this way, but if there is
804 * an error, module_put() probably should
805 * happen after any cleanup.
806 */
807 module_got = 1;
808
809 if (group)
810 ret = configfs_attach_group(parent_item, item, dentry);
811 else
812 ret = configfs_attach_item(parent_item, item, dentry);
813
814out_unlink:
815 if (ret) {
816 /* Tear down everything we built up */
817 down(&subsys->su_sem);
818 if (group)
819 unlink_group(group);
820 else
821 unlink_obj(item);
822 client_drop_item(parent_item, item);
823 up(&subsys->su_sem);
824
825 if (module_got)
826 module_put(owner);
Joel Becker7063fbf2005-12-15 14:29:43 -0800827 }
828
Joel Becker84efad12006-03-27 18:46:09 -0800829out_put:
830 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -0700831 * link_obj()/link_group() took a reference from child->parent,
832 * so the parent is safely pinned. We can drop our working
833 * reference.
Joel Becker84efad12006-03-27 18:46:09 -0800834 */
835 config_item_put(parent_item);
836
837out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800838 return ret;
839}
840
841static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
842{
843 struct config_item *parent_item;
844 struct config_item *item;
845 struct configfs_subsystem *subsys;
846 struct configfs_dirent *sd;
847 struct module *owner = NULL;
848 int ret;
849
850 if (dentry->d_parent == configfs_sb->s_root)
851 return -EPERM;
852
853 sd = dentry->d_fsdata;
854 if (sd->s_type & CONFIGFS_USET_DEFAULT)
855 return -EPERM;
856
Joel Becker84efad12006-03-27 18:46:09 -0800857 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -0800858 parent_item = configfs_get_config_item(dentry->d_parent);
859 subsys = to_config_group(parent_item)->cg_subsys;
860 BUG_ON(!subsys);
861
862 if (!parent_item->ci_type) {
863 config_item_put(parent_item);
864 return -EINVAL;
865 }
866
867 ret = configfs_detach_prep(dentry);
868 if (ret) {
869 configfs_detach_rollback(dentry);
870 config_item_put(parent_item);
871 return ret;
872 }
873
Joel Becker84efad12006-03-27 18:46:09 -0800874 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -0800875 item = configfs_get_config_item(dentry);
876
877 /* Drop reference from above, item already holds one. */
878 config_item_put(parent_item);
879
880 if (item->ci_type)
881 owner = item->ci_type->ct_owner;
882
883 if (sd->s_type & CONFIGFS_USET_DIR) {
884 configfs_detach_group(item);
885
886 down(&subsys->su_sem);
887 unlink_group(to_config_group(item));
888 } else {
889 configfs_detach_item(item);
890
891 down(&subsys->su_sem);
892 unlink_obj(item);
893 }
894
895 client_drop_item(parent_item, item);
896 up(&subsys->su_sem);
897
898 /* Drop our reference from above */
899 config_item_put(item);
900
901 module_put(owner);
902
903 return 0;
904}
905
906struct inode_operations configfs_dir_inode_operations = {
907 .mkdir = configfs_mkdir,
908 .rmdir = configfs_rmdir,
909 .symlink = configfs_symlink,
910 .unlink = configfs_unlink,
911 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -0800912 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -0800913};
914
915#if 0
916int configfs_rename_dir(struct config_item * item, const char *new_name)
917{
918 int error = 0;
919 struct dentry * new_dentry, * parent;
920
921 if (!strcmp(config_item_name(item), new_name))
922 return -EINVAL;
923
924 if (!item->parent)
925 return -EINVAL;
926
927 down_write(&configfs_rename_sem);
928 parent = item->parent->dentry;
929
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800930 mutex_lock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800931
932 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
933 if (!IS_ERR(new_dentry)) {
934 if (!new_dentry->d_inode) {
935 error = config_item_set_name(item, "%s", new_name);
936 if (!error) {
937 d_add(new_dentry, NULL);
938 d_move(item->dentry, new_dentry);
939 }
940 else
941 d_delete(new_dentry);
942 } else
943 error = -EEXIST;
944 dput(new_dentry);
945 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800946 mutex_unlock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800947 up_write(&configfs_rename_sem);
948
949 return error;
950}
951#endif
952
953static int configfs_dir_open(struct inode *inode, struct file *file)
954{
955 struct dentry * dentry = file->f_dentry;
956 struct configfs_dirent * parent_sd = dentry->d_fsdata;
957
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800958 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800959 file->private_data = configfs_new_dirent(parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800960 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800961
962 return file->private_data ? 0 : -ENOMEM;
963
964}
965
966static int configfs_dir_close(struct inode *inode, struct file *file)
967{
968 struct dentry * dentry = file->f_dentry;
969 struct configfs_dirent * cursor = file->private_data;
970
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800971 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800972 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800973 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800974
975 release_configfs_dirent(cursor);
976
977 return 0;
978}
979
980/* Relationship between s_mode and the DT_xxx types */
981static inline unsigned char dt_type(struct configfs_dirent *sd)
982{
983 return (sd->s_mode >> 12) & 15;
984}
985
986static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
987{
988 struct dentry *dentry = filp->f_dentry;
989 struct configfs_dirent * parent_sd = dentry->d_fsdata;
990 struct configfs_dirent *cursor = filp->private_data;
991 struct list_head *p, *q = &cursor->s_sibling;
992 ino_t ino;
993 int i = filp->f_pos;
994
995 switch (i) {
996 case 0:
997 ino = dentry->d_inode->i_ino;
998 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
999 break;
1000 filp->f_pos++;
1001 i++;
1002 /* fallthrough */
1003 case 1:
1004 ino = parent_ino(dentry);
1005 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1006 break;
1007 filp->f_pos++;
1008 i++;
1009 /* fallthrough */
1010 default:
1011 if (filp->f_pos == 2) {
1012 list_del(q);
1013 list_add(q, &parent_sd->s_children);
1014 }
1015 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1016 struct configfs_dirent *next;
1017 const char * name;
1018 int len;
1019
1020 next = list_entry(p, struct configfs_dirent,
1021 s_sibling);
1022 if (!next->s_element)
1023 continue;
1024
1025 name = configfs_get_name(next);
1026 len = strlen(name);
1027 if (next->s_dentry)
1028 ino = next->s_dentry->d_inode->i_ino;
1029 else
1030 ino = iunique(configfs_sb, 2);
1031
1032 if (filldir(dirent, name, len, filp->f_pos, ino,
1033 dt_type(next)) < 0)
1034 return 0;
1035
1036 list_del(q);
1037 list_add(q, p);
1038 p = q;
1039 filp->f_pos++;
1040 }
1041 }
1042 return 0;
1043}
1044
1045static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1046{
1047 struct dentry * dentry = file->f_dentry;
1048
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001049 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001050 switch (origin) {
1051 case 1:
1052 offset += file->f_pos;
1053 case 0:
1054 if (offset >= 0)
1055 break;
1056 default:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001057 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001058 return -EINVAL;
1059 }
1060 if (offset != file->f_pos) {
1061 file->f_pos = offset;
1062 if (file->f_pos >= 2) {
1063 struct configfs_dirent *sd = dentry->d_fsdata;
1064 struct configfs_dirent *cursor = file->private_data;
1065 struct list_head *p;
1066 loff_t n = file->f_pos - 2;
1067
1068 list_del(&cursor->s_sibling);
1069 p = sd->s_children.next;
1070 while (n && p != &sd->s_children) {
1071 struct configfs_dirent *next;
1072 next = list_entry(p, struct configfs_dirent,
1073 s_sibling);
1074 if (next->s_element)
1075 n--;
1076 p = p->next;
1077 }
1078 list_add_tail(&cursor->s_sibling, p);
1079 }
1080 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001081 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001082 return offset;
1083}
1084
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001085const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001086 .open = configfs_dir_open,
1087 .release = configfs_dir_close,
1088 .llseek = configfs_dir_lseek,
1089 .read = generic_read_dir,
1090 .readdir = configfs_readdir,
1091};
1092
1093int configfs_register_subsystem(struct configfs_subsystem *subsys)
1094{
1095 int err;
1096 struct config_group *group = &subsys->su_group;
1097 struct qstr name;
1098 struct dentry *dentry;
1099 struct configfs_dirent *sd;
1100
1101 err = configfs_pin_fs();
1102 if (err)
1103 return err;
1104
1105 if (!group->cg_item.ci_name)
1106 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1107
1108 sd = configfs_sb->s_root->d_fsdata;
1109 link_group(to_config_group(sd->s_element), group);
1110
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001111 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001112
1113 name.name = group->cg_item.ci_name;
1114 name.len = strlen(name.name);
1115 name.hash = full_name_hash(name.name, name.len);
1116
1117 err = -ENOMEM;
1118 dentry = d_alloc(configfs_sb->s_root, &name);
1119 if (!dentry)
1120 goto out_release;
1121
1122 d_add(dentry, NULL);
1123
1124 err = configfs_attach_group(sd->s_element, &group->cg_item,
1125 dentry);
1126 if (!err)
1127 dentry = NULL;
1128 else
1129 d_delete(dentry);
1130
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001131 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001132
1133 if (dentry) {
1134 dput(dentry);
1135out_release:
1136 unlink_group(group);
1137 configfs_release_fs();
1138 }
1139
1140 return err;
1141}
1142
1143void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1144{
1145 struct config_group *group = &subsys->su_group;
1146 struct dentry *dentry = group->cg_item.ci_dentry;
1147
1148 if (dentry->d_parent != configfs_sb->s_root) {
1149 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1150 return;
1151 }
1152
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001153 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
1154 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001155 if (configfs_detach_prep(dentry)) {
1156 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1157 }
1158 configfs_detach_group(&group->cg_item);
1159 dentry->d_inode->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001160 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001161
1162 d_delete(dentry);
1163
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001164 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001165
1166 dput(dentry);
1167
1168 unlink_group(group);
1169 configfs_release_fs();
1170}
1171
1172EXPORT_SYMBOL(configfs_register_subsystem);
1173EXPORT_SYMBOL(configfs_unregister_subsystem);