[PATCH] briq_panel: read() and write() get __user pointers, damnit
[linux-2.6/verdex.git] / fs / configfs / dir.c
blob816e8ef645605a5cdd505b7bdd6be8e8bf38345a
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dir.c - Operations for configfs directories.
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.
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.
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.
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
27 #undef DEBUG
29 #include <linux/fs.h>
30 #include <linux/mount.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
34 #include <linux/configfs.h>
35 #include "configfs_internal.h"
37 DECLARE_RWSEM(configfs_rename_sem);
39 static void configfs_d_iput(struct dentry * dentry,
40 struct inode * inode)
42 struct configfs_dirent * sd = dentry->d_fsdata;
44 if (sd) {
45 BUG_ON(sd->s_dentry != dentry);
46 sd->s_dentry = NULL;
47 configfs_put(sd);
49 iput(inode);
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.
56 static int configfs_d_delete(struct dentry *dentry)
58 return 1;
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,
68 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
70 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
71 void * element)
73 struct configfs_dirent * sd;
75 sd = kmem_cache_alloc(configfs_dir_cachep, GFP_KERNEL);
76 if (!sd)
77 return NULL;
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;
86 return sd;
91 * Return -EEXIST if there is already a configfs element with the same
92 * name for the same parent.
94 * called with parent inode's i_mutex held
96 int configfs_dirent_exists(struct configfs_dirent *parent_sd,
97 const unsigned char *new)
99 struct configfs_dirent * sd;
101 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
102 if (sd->s_element) {
103 const unsigned char *existing = configfs_get_name(sd);
104 if (strcmp(existing, new))
105 continue;
106 else
107 return -EEXIST;
111 return 0;
115 int configfs_make_dirent(struct configfs_dirent * parent_sd,
116 struct dentry * dentry, void * element,
117 umode_t mode, int type)
119 struct configfs_dirent * sd;
121 sd = configfs_new_dirent(parent_sd, element);
122 if (!sd)
123 return -ENOMEM;
125 sd->s_mode = mode;
126 sd->s_type = type;
127 sd->s_dentry = dentry;
128 if (dentry) {
129 dentry->d_fsdata = configfs_get(sd);
130 dentry->d_op = &configfs_dentry_ops;
133 return 0;
136 static int init_dir(struct inode * inode)
138 inode->i_op = &configfs_dir_inode_operations;
139 inode->i_fop = &configfs_dir_operations;
141 /* directory inodes start off with i_nlink == 2 (for "." entry) */
142 inode->i_nlink++;
143 return 0;
146 static int init_file(struct inode * inode)
148 inode->i_size = PAGE_SIZE;
149 inode->i_fop = &configfs_file_operations;
150 return 0;
153 static int init_symlink(struct inode * inode)
155 inode->i_op = &configfs_symlink_inode_operations;
156 return 0;
159 static int create_dir(struct config_item * k, struct dentry * p,
160 struct dentry * d)
162 int error;
163 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
165 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
166 if (!error)
167 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
168 CONFIGFS_DIR);
169 if (!error) {
170 error = configfs_create(d, mode, init_dir);
171 if (!error) {
172 p->d_inode->i_nlink++;
173 (d)->d_op = &configfs_dentry_ops;
174 } else {
175 struct configfs_dirent *sd = d->d_fsdata;
176 if (sd) {
177 list_del_init(&sd->s_sibling);
178 configfs_put(sd);
182 return error;
187 * configfs_create_dir - create a directory for an config_item.
188 * @item: config_itemwe're creating directory for.
189 * @dentry: config_item's dentry.
192 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
194 struct dentry * parent;
195 int error = 0;
197 BUG_ON(!item);
199 if (item->ci_parent)
200 parent = item->ci_parent->ci_dentry;
201 else if (configfs_mount && configfs_mount->mnt_sb)
202 parent = configfs_mount->mnt_sb->s_root;
203 else
204 return -EFAULT;
206 error = create_dir(item,parent,dentry);
207 if (!error)
208 item->ci_dentry = dentry;
209 return error;
212 int configfs_create_link(struct configfs_symlink *sl,
213 struct dentry *parent,
214 struct dentry *dentry)
216 int err = 0;
217 umode_t mode = S_IFLNK | S_IRWXUGO;
219 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
220 CONFIGFS_ITEM_LINK);
221 if (!err) {
222 err = configfs_create(dentry, mode, init_symlink);
223 if (!err)
224 dentry->d_op = &configfs_dentry_ops;
225 else {
226 struct configfs_dirent *sd = dentry->d_fsdata;
227 if (sd) {
228 list_del_init(&sd->s_sibling);
229 configfs_put(sd);
233 return err;
236 static void remove_dir(struct dentry * d)
238 struct dentry * parent = dget(d->d_parent);
239 struct configfs_dirent * sd;
241 sd = d->d_fsdata;
242 list_del_init(&sd->s_sibling);
243 configfs_put(sd);
244 if (d->d_inode)
245 simple_rmdir(parent->d_inode,d);
247 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
248 atomic_read(&d->d_count));
250 dput(parent);
254 * configfs_remove_dir - remove an config_item's directory.
255 * @item: config_item we're removing.
257 * The only thing special about this is that we remove any files in
258 * the directory before we remove the directory, and we've inlined
259 * what used to be configfs_rmdir() below, instead of calling separately.
262 static void configfs_remove_dir(struct config_item * item)
264 struct dentry * dentry = dget(item->ci_dentry);
266 if (!dentry)
267 return;
269 remove_dir(dentry);
271 * Drop reference from dget() on entrance.
273 dput(dentry);
277 /* attaches attribute's configfs_dirent to the dentry corresponding to the
278 * attribute file
280 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
282 struct configfs_attribute * attr = sd->s_element;
283 int error;
285 dentry->d_fsdata = configfs_get(sd);
286 sd->s_dentry = dentry;
287 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
288 if (error) {
289 configfs_put(sd);
290 return error;
293 dentry->d_op = &configfs_dentry_ops;
294 d_rehash(dentry);
296 return 0;
299 static struct dentry * configfs_lookup(struct inode *dir,
300 struct dentry *dentry,
301 struct nameidata *nd)
303 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
304 struct configfs_dirent * sd;
305 int found = 0;
306 int err = 0;
308 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
309 if (sd->s_type & CONFIGFS_NOT_PINNED) {
310 const unsigned char * name = configfs_get_name(sd);
312 if (strcmp(name, dentry->d_name.name))
313 continue;
315 found = 1;
316 err = configfs_attach_attr(sd, dentry);
317 break;
321 if (!found) {
323 * If it doesn't exist and it isn't a NOT_PINNED item,
324 * it must be negative.
326 return simple_lookup(dir, dentry, nd);
329 return ERR_PTR(err);
333 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
334 * attributes and are removed by rmdir(). We recurse, taking i_mutex
335 * on all children that are candidates for default detach. If the
336 * result is clean, then configfs_detach_group() will handle dropping
337 * i_mutex. If there is an error, the caller will clean up the i_mutex
338 * holders via configfs_detach_rollback().
340 static int configfs_detach_prep(struct dentry *dentry)
342 struct configfs_dirent *parent_sd = dentry->d_fsdata;
343 struct configfs_dirent *sd;
344 int ret;
346 ret = -EBUSY;
347 if (!list_empty(&parent_sd->s_links))
348 goto out;
350 ret = 0;
351 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
352 if (sd->s_type & CONFIGFS_NOT_PINNED)
353 continue;
354 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
355 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
356 /* Mark that we've taken i_mutex */
357 sd->s_type |= CONFIGFS_USET_DROPPING;
359 ret = configfs_detach_prep(sd->s_dentry);
360 if (!ret)
361 continue;
362 } else
363 ret = -ENOTEMPTY;
365 break;
368 out:
369 return ret;
373 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
374 * set.
376 static void configfs_detach_rollback(struct dentry *dentry)
378 struct configfs_dirent *parent_sd = dentry->d_fsdata;
379 struct configfs_dirent *sd;
381 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
382 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
383 configfs_detach_rollback(sd->s_dentry);
385 if (sd->s_type & CONFIGFS_USET_DROPPING) {
386 sd->s_type &= ~CONFIGFS_USET_DROPPING;
387 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
393 static void detach_attrs(struct config_item * item)
395 struct dentry * dentry = dget(item->ci_dentry);
396 struct configfs_dirent * parent_sd;
397 struct configfs_dirent * sd, * tmp;
399 if (!dentry)
400 return;
402 pr_debug("configfs %s: dropping attrs for dir\n",
403 dentry->d_name.name);
405 parent_sd = dentry->d_fsdata;
406 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
407 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
408 continue;
409 list_del_init(&sd->s_sibling);
410 configfs_drop_dentry(sd, dentry);
411 configfs_put(sd);
415 * Drop reference from dget() on entrance.
417 dput(dentry);
420 static int populate_attrs(struct config_item *item)
422 struct config_item_type *t = item->ci_type;
423 struct configfs_attribute *attr;
424 int error = 0;
425 int i;
427 if (!t)
428 return -EINVAL;
429 if (t->ct_attrs) {
430 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
431 if ((error = configfs_create_file(item, attr)))
432 break;
436 if (error)
437 detach_attrs(item);
439 return error;
442 static int configfs_attach_group(struct config_item *parent_item,
443 struct config_item *item,
444 struct dentry *dentry);
445 static void configfs_detach_group(struct config_item *item);
447 static void detach_groups(struct config_group *group)
449 struct dentry * dentry = dget(group->cg_item.ci_dentry);
450 struct dentry *child;
451 struct configfs_dirent *parent_sd;
452 struct configfs_dirent *sd, *tmp;
454 if (!dentry)
455 return;
457 parent_sd = dentry->d_fsdata;
458 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
459 if (!sd->s_element ||
460 !(sd->s_type & CONFIGFS_USET_DEFAULT))
461 continue;
463 child = sd->s_dentry;
465 configfs_detach_group(sd->s_element);
466 child->d_inode->i_flags |= S_DEAD;
469 * From rmdir/unregister, a configfs_detach_prep() pass
470 * has taken our i_mutex for us. Drop it.
471 * From mkdir/register cleanup, there is no sem held.
473 if (sd->s_type & CONFIGFS_USET_DROPPING)
474 mutex_unlock(&child->d_inode->i_mutex);
476 d_delete(child);
477 dput(child);
481 * Drop reference from dget() on entrance.
483 dput(dentry);
487 * This fakes mkdir(2) on a default_groups[] entry. It
488 * creates a dentry, attachs it, and then does fixup
489 * on the sd->s_type.
491 * We could, perhaps, tweak our parent's ->mkdir for a minute and
492 * try using vfs_mkdir. Just a thought.
494 static int create_default_group(struct config_group *parent_group,
495 struct config_group *group)
497 int ret;
498 struct qstr name;
499 struct configfs_dirent *sd;
500 /* We trust the caller holds a reference to parent */
501 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
503 if (!group->cg_item.ci_name)
504 group->cg_item.ci_name = group->cg_item.ci_namebuf;
505 name.name = group->cg_item.ci_name;
506 name.len = strlen(name.name);
507 name.hash = full_name_hash(name.name, name.len);
509 ret = -ENOMEM;
510 child = d_alloc(parent, &name);
511 if (child) {
512 d_add(child, NULL);
514 ret = configfs_attach_group(&parent_group->cg_item,
515 &group->cg_item, child);
516 if (!ret) {
517 sd = child->d_fsdata;
518 sd->s_type |= CONFIGFS_USET_DEFAULT;
519 } else {
520 d_delete(child);
521 dput(child);
525 return ret;
528 static int populate_groups(struct config_group *group)
530 struct config_group *new_group;
531 struct dentry *dentry = group->cg_item.ci_dentry;
532 int ret = 0;
533 int i;
535 if (group->default_groups) {
537 * FYI, we're faking mkdir here
538 * I'm not sure we need this semaphore, as we're called
539 * from our parent's mkdir. That holds our parent's
540 * i_mutex, so afaik lookup cannot continue through our
541 * parent to find us, let alone mess with our tree.
542 * That said, taking our i_mutex is closer to mkdir
543 * emulation, and shouldn't hurt.
545 mutex_lock(&dentry->d_inode->i_mutex);
547 for (i = 0; group->default_groups[i]; i++) {
548 new_group = group->default_groups[i];
550 ret = create_default_group(group, new_group);
551 if (ret)
552 break;
555 mutex_unlock(&dentry->d_inode->i_mutex);
558 if (ret)
559 detach_groups(group);
561 return ret;
565 * All of link_obj/unlink_obj/link_group/unlink_group require that
566 * subsys->su_sem is held.
569 static void unlink_obj(struct config_item *item)
571 struct config_group *group;
573 group = item->ci_group;
574 if (group) {
575 list_del_init(&item->ci_entry);
577 item->ci_group = NULL;
578 item->ci_parent = NULL;
580 /* Drop the reference for ci_entry */
581 config_item_put(item);
583 /* Drop the reference for ci_parent */
584 config_group_put(group);
588 static void link_obj(struct config_item *parent_item, struct config_item *item)
591 * Parent seems redundant with group, but it makes certain
592 * traversals much nicer.
594 item->ci_parent = parent_item;
597 * We hold a reference on the parent for the child's ci_parent
598 * link.
600 item->ci_group = config_group_get(to_config_group(parent_item));
601 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
604 * We hold a reference on the child for ci_entry on the parent's
605 * cg_children
607 config_item_get(item);
610 static void unlink_group(struct config_group *group)
612 int i;
613 struct config_group *new_group;
615 if (group->default_groups) {
616 for (i = 0; group->default_groups[i]; i++) {
617 new_group = group->default_groups[i];
618 unlink_group(new_group);
622 group->cg_subsys = NULL;
623 unlink_obj(&group->cg_item);
626 static void link_group(struct config_group *parent_group, struct config_group *group)
628 int i;
629 struct config_group *new_group;
630 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
632 link_obj(&parent_group->cg_item, &group->cg_item);
634 if (parent_group->cg_subsys)
635 subsys = parent_group->cg_subsys;
636 else if (configfs_is_root(&parent_group->cg_item))
637 subsys = to_configfs_subsystem(group);
638 else
639 BUG();
640 group->cg_subsys = subsys;
642 if (group->default_groups) {
643 for (i = 0; group->default_groups[i]; i++) {
644 new_group = group->default_groups[i];
645 link_group(group, new_group);
651 * The goal is that configfs_attach_item() (and
652 * configfs_attach_group()) can be called from either the VFS or this
653 * module. That is, they assume that the items have been created,
654 * the dentry allocated, and the dcache is all ready to go.
656 * If they fail, they must clean up after themselves as if they
657 * had never been called. The caller (VFS or local function) will
658 * handle cleaning up the dcache bits.
660 * configfs_detach_group() and configfs_detach_item() behave similarly on
661 * the way out. They assume that the proper semaphores are held, they
662 * clean up the configfs items, and they expect their callers will
663 * handle the dcache bits.
665 static int configfs_attach_item(struct config_item *parent_item,
666 struct config_item *item,
667 struct dentry *dentry)
669 int ret;
671 ret = configfs_create_dir(item, dentry);
672 if (!ret) {
673 ret = populate_attrs(item);
674 if (ret) {
675 configfs_remove_dir(item);
676 d_delete(dentry);
680 return ret;
683 static void configfs_detach_item(struct config_item *item)
685 detach_attrs(item);
686 configfs_remove_dir(item);
689 static int configfs_attach_group(struct config_item *parent_item,
690 struct config_item *item,
691 struct dentry *dentry)
693 int ret;
694 struct configfs_dirent *sd;
696 ret = configfs_attach_item(parent_item, item, dentry);
697 if (!ret) {
698 sd = dentry->d_fsdata;
699 sd->s_type |= CONFIGFS_USET_DIR;
701 ret = populate_groups(to_config_group(item));
702 if (ret) {
703 configfs_detach_item(item);
704 d_delete(dentry);
708 return ret;
711 static void configfs_detach_group(struct config_item *item)
713 detach_groups(to_config_group(item));
714 configfs_detach_item(item);
718 * Drop the initial reference from make_item()/make_group()
719 * This function assumes that reference is held on item
720 * and that item holds a valid reference to the parent. Also, it
721 * assumes the caller has validated ci_type.
723 static void client_drop_item(struct config_item *parent_item,
724 struct config_item *item)
726 struct config_item_type *type;
728 type = parent_item->ci_type;
729 BUG_ON(!type);
732 * If ->drop_item() exists, it is responsible for the
733 * config_item_put().
735 if (type->ct_group_ops && type->ct_group_ops->drop_item)
736 type->ct_group_ops->drop_item(to_config_group(parent_item),
737 item);
738 else
739 config_item_put(item);
743 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
745 int ret, module_got = 0;
746 struct config_group *group;
747 struct config_item *item;
748 struct config_item *parent_item;
749 struct configfs_subsystem *subsys;
750 struct configfs_dirent *sd;
751 struct config_item_type *type;
752 struct module *owner = NULL;
753 char *name;
755 if (dentry->d_parent == configfs_sb->s_root) {
756 ret = -EPERM;
757 goto out;
760 sd = dentry->d_parent->d_fsdata;
761 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
762 ret = -EPERM;
763 goto out;
766 /* Get a working ref for the duration of this function */
767 parent_item = configfs_get_config_item(dentry->d_parent);
768 type = parent_item->ci_type;
769 subsys = to_config_group(parent_item)->cg_subsys;
770 BUG_ON(!subsys);
772 if (!type || !type->ct_group_ops ||
773 (!type->ct_group_ops->make_group &&
774 !type->ct_group_ops->make_item)) {
775 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
776 goto out_put;
779 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
780 if (!name) {
781 ret = -ENOMEM;
782 goto out_put;
785 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
787 down(&subsys->su_sem);
788 group = NULL;
789 item = NULL;
790 if (type->ct_group_ops->make_group) {
791 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
792 if (group) {
793 link_group(to_config_group(parent_item), group);
794 item = &group->cg_item;
796 } else {
797 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
798 if (item)
799 link_obj(parent_item, item);
801 up(&subsys->su_sem);
803 kfree(name);
804 if (!item) {
806 * If item == NULL, then link_obj() was never called.
807 * There are no extra references to clean up.
809 ret = -ENOMEM;
810 goto out_put;
814 * link_obj() has been called (via link_group() for groups).
815 * From here on out, errors must clean that up.
818 type = item->ci_type;
819 if (!type) {
820 ret = -EINVAL;
821 goto out_unlink;
824 owner = type->ct_owner;
825 if (!try_module_get(owner)) {
826 ret = -EINVAL;
827 goto out_unlink;
831 * I hate doing it this way, but if there is
832 * an error, module_put() probably should
833 * happen after any cleanup.
835 module_got = 1;
837 if (group)
838 ret = configfs_attach_group(parent_item, item, dentry);
839 else
840 ret = configfs_attach_item(parent_item, item, dentry);
842 out_unlink:
843 if (ret) {
844 /* Tear down everything we built up */
845 down(&subsys->su_sem);
846 if (group)
847 unlink_group(group);
848 else
849 unlink_obj(item);
850 client_drop_item(parent_item, item);
851 up(&subsys->su_sem);
853 if (module_got)
854 module_put(owner);
857 out_put:
859 * link_obj()/link_group() took a reference from child->parent,
860 * so the parent is safely pinned. We can drop our working
861 * reference.
863 config_item_put(parent_item);
865 out:
866 return ret;
869 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
871 struct config_item *parent_item;
872 struct config_item *item;
873 struct configfs_subsystem *subsys;
874 struct configfs_dirent *sd;
875 struct module *owner = NULL;
876 int ret;
878 if (dentry->d_parent == configfs_sb->s_root)
879 return -EPERM;
881 sd = dentry->d_fsdata;
882 if (sd->s_type & CONFIGFS_USET_DEFAULT)
883 return -EPERM;
885 /* Get a working ref until we have the child */
886 parent_item = configfs_get_config_item(dentry->d_parent);
887 subsys = to_config_group(parent_item)->cg_subsys;
888 BUG_ON(!subsys);
890 if (!parent_item->ci_type) {
891 config_item_put(parent_item);
892 return -EINVAL;
895 ret = configfs_detach_prep(dentry);
896 if (ret) {
897 configfs_detach_rollback(dentry);
898 config_item_put(parent_item);
899 return ret;
902 /* Get a working ref for the duration of this function */
903 item = configfs_get_config_item(dentry);
905 /* Drop reference from above, item already holds one. */
906 config_item_put(parent_item);
908 if (item->ci_type)
909 owner = item->ci_type->ct_owner;
911 if (sd->s_type & CONFIGFS_USET_DIR) {
912 configfs_detach_group(item);
914 down(&subsys->su_sem);
915 unlink_group(to_config_group(item));
916 } else {
917 configfs_detach_item(item);
919 down(&subsys->su_sem);
920 unlink_obj(item);
923 client_drop_item(parent_item, item);
924 up(&subsys->su_sem);
926 /* Drop our reference from above */
927 config_item_put(item);
929 module_put(owner);
931 return 0;
934 struct inode_operations configfs_dir_inode_operations = {
935 .mkdir = configfs_mkdir,
936 .rmdir = configfs_rmdir,
937 .symlink = configfs_symlink,
938 .unlink = configfs_unlink,
939 .lookup = configfs_lookup,
940 .setattr = configfs_setattr,
943 #if 0
944 int configfs_rename_dir(struct config_item * item, const char *new_name)
946 int error = 0;
947 struct dentry * new_dentry, * parent;
949 if (!strcmp(config_item_name(item), new_name))
950 return -EINVAL;
952 if (!item->parent)
953 return -EINVAL;
955 down_write(&configfs_rename_sem);
956 parent = item->parent->dentry;
958 mutex_lock(&parent->d_inode->i_mutex);
960 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
961 if (!IS_ERR(new_dentry)) {
962 if (!new_dentry->d_inode) {
963 error = config_item_set_name(item, "%s", new_name);
964 if (!error) {
965 d_add(new_dentry, NULL);
966 d_move(item->dentry, new_dentry);
968 else
969 d_delete(new_dentry);
970 } else
971 error = -EEXIST;
972 dput(new_dentry);
974 mutex_unlock(&parent->d_inode->i_mutex);
975 up_write(&configfs_rename_sem);
977 return error;
979 #endif
981 static int configfs_dir_open(struct inode *inode, struct file *file)
983 struct dentry * dentry = file->f_dentry;
984 struct configfs_dirent * parent_sd = dentry->d_fsdata;
986 mutex_lock(&dentry->d_inode->i_mutex);
987 file->private_data = configfs_new_dirent(parent_sd, NULL);
988 mutex_unlock(&dentry->d_inode->i_mutex);
990 return file->private_data ? 0 : -ENOMEM;
994 static int configfs_dir_close(struct inode *inode, struct file *file)
996 struct dentry * dentry = file->f_dentry;
997 struct configfs_dirent * cursor = file->private_data;
999 mutex_lock(&dentry->d_inode->i_mutex);
1000 list_del_init(&cursor->s_sibling);
1001 mutex_unlock(&dentry->d_inode->i_mutex);
1003 release_configfs_dirent(cursor);
1005 return 0;
1008 /* Relationship between s_mode and the DT_xxx types */
1009 static inline unsigned char dt_type(struct configfs_dirent *sd)
1011 return (sd->s_mode >> 12) & 15;
1014 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1016 struct dentry *dentry = filp->f_dentry;
1017 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1018 struct configfs_dirent *cursor = filp->private_data;
1019 struct list_head *p, *q = &cursor->s_sibling;
1020 ino_t ino;
1021 int i = filp->f_pos;
1023 switch (i) {
1024 case 0:
1025 ino = dentry->d_inode->i_ino;
1026 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1027 break;
1028 filp->f_pos++;
1029 i++;
1030 /* fallthrough */
1031 case 1:
1032 ino = parent_ino(dentry);
1033 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1034 break;
1035 filp->f_pos++;
1036 i++;
1037 /* fallthrough */
1038 default:
1039 if (filp->f_pos == 2) {
1040 list_move(q, &parent_sd->s_children);
1042 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1043 struct configfs_dirent *next;
1044 const char * name;
1045 int len;
1047 next = list_entry(p, struct configfs_dirent,
1048 s_sibling);
1049 if (!next->s_element)
1050 continue;
1052 name = configfs_get_name(next);
1053 len = strlen(name);
1054 if (next->s_dentry)
1055 ino = next->s_dentry->d_inode->i_ino;
1056 else
1057 ino = iunique(configfs_sb, 2);
1059 if (filldir(dirent, name, len, filp->f_pos, ino,
1060 dt_type(next)) < 0)
1061 return 0;
1063 list_move(q, p);
1064 p = q;
1065 filp->f_pos++;
1068 return 0;
1071 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1073 struct dentry * dentry = file->f_dentry;
1075 mutex_lock(&dentry->d_inode->i_mutex);
1076 switch (origin) {
1077 case 1:
1078 offset += file->f_pos;
1079 case 0:
1080 if (offset >= 0)
1081 break;
1082 default:
1083 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
1084 return -EINVAL;
1086 if (offset != file->f_pos) {
1087 file->f_pos = offset;
1088 if (file->f_pos >= 2) {
1089 struct configfs_dirent *sd = dentry->d_fsdata;
1090 struct configfs_dirent *cursor = file->private_data;
1091 struct list_head *p;
1092 loff_t n = file->f_pos - 2;
1094 list_del(&cursor->s_sibling);
1095 p = sd->s_children.next;
1096 while (n && p != &sd->s_children) {
1097 struct configfs_dirent *next;
1098 next = list_entry(p, struct configfs_dirent,
1099 s_sibling);
1100 if (next->s_element)
1101 n--;
1102 p = p->next;
1104 list_add_tail(&cursor->s_sibling, p);
1107 mutex_unlock(&dentry->d_inode->i_mutex);
1108 return offset;
1111 const struct file_operations configfs_dir_operations = {
1112 .open = configfs_dir_open,
1113 .release = configfs_dir_close,
1114 .llseek = configfs_dir_lseek,
1115 .read = generic_read_dir,
1116 .readdir = configfs_readdir,
1119 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1121 int err;
1122 struct config_group *group = &subsys->su_group;
1123 struct qstr name;
1124 struct dentry *dentry;
1125 struct configfs_dirent *sd;
1127 err = configfs_pin_fs();
1128 if (err)
1129 return err;
1131 if (!group->cg_item.ci_name)
1132 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1134 sd = configfs_sb->s_root->d_fsdata;
1135 link_group(to_config_group(sd->s_element), group);
1137 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
1139 name.name = group->cg_item.ci_name;
1140 name.len = strlen(name.name);
1141 name.hash = full_name_hash(name.name, name.len);
1143 err = -ENOMEM;
1144 dentry = d_alloc(configfs_sb->s_root, &name);
1145 if (!dentry)
1146 goto out_release;
1148 d_add(dentry, NULL);
1150 err = configfs_attach_group(sd->s_element, &group->cg_item,
1151 dentry);
1152 if (!err)
1153 dentry = NULL;
1154 else
1155 d_delete(dentry);
1157 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1159 if (dentry) {
1160 dput(dentry);
1161 out_release:
1162 unlink_group(group);
1163 configfs_release_fs();
1166 return err;
1169 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1171 struct config_group *group = &subsys->su_group;
1172 struct dentry *dentry = group->cg_item.ci_dentry;
1174 if (dentry->d_parent != configfs_sb->s_root) {
1175 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1176 return;
1179 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
1180 mutex_lock(&dentry->d_inode->i_mutex);
1181 if (configfs_detach_prep(dentry)) {
1182 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1184 configfs_detach_group(&group->cg_item);
1185 dentry->d_inode->i_flags |= S_DEAD;
1186 mutex_unlock(&dentry->d_inode->i_mutex);
1188 d_delete(dentry);
1190 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1192 dput(dentry);
1194 unlink_group(group);
1195 configfs_release_fs();
1198 EXPORT_SYMBOL(configfs_register_subsystem);
1199 EXPORT_SYMBOL(configfs_unregister_subsystem);