allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / configfs / dir.c
blob5e6e37e58f36f5369201f0bb0482957beab20fdc
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_zalloc(configfs_dir_cachep, GFP_KERNEL);
76 if (!sd)
77 return NULL;
79 atomic_set(&sd->s_count, 1);
80 INIT_LIST_HEAD(&sd->s_links);
81 INIT_LIST_HEAD(&sd->s_children);
82 list_add(&sd->s_sibling, &parent_sd->s_children);
83 sd->s_element = element;
85 return sd;
90 * Return -EEXIST if there is already a configfs element with the same
91 * name for the same parent.
93 * called with parent inode's i_mutex held
95 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
96 const unsigned char *new)
98 struct configfs_dirent * sd;
100 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
101 if (sd->s_element) {
102 const unsigned char *existing = configfs_get_name(sd);
103 if (strcmp(existing, new))
104 continue;
105 else
106 return -EEXIST;
110 return 0;
114 int configfs_make_dirent(struct configfs_dirent * parent_sd,
115 struct dentry * dentry, void * element,
116 umode_t mode, int type)
118 struct configfs_dirent * sd;
120 sd = configfs_new_dirent(parent_sd, element);
121 if (!sd)
122 return -ENOMEM;
124 sd->s_mode = mode;
125 sd->s_type = type;
126 sd->s_dentry = dentry;
127 if (dentry) {
128 dentry->d_fsdata = configfs_get(sd);
129 dentry->d_op = &configfs_dentry_ops;
132 return 0;
135 static int init_dir(struct inode * inode)
137 inode->i_op = &configfs_dir_inode_operations;
138 inode->i_fop = &configfs_dir_operations;
140 /* directory inodes start off with i_nlink == 2 (for "." entry) */
141 inc_nlink(inode);
142 return 0;
145 static int init_file(struct inode * inode)
147 inode->i_size = PAGE_SIZE;
148 inode->i_fop = &configfs_file_operations;
149 return 0;
152 static int init_symlink(struct inode * inode)
154 inode->i_op = &configfs_symlink_inode_operations;
155 return 0;
158 static int create_dir(struct config_item * k, struct dentry * p,
159 struct dentry * d)
161 int error;
162 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
164 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
165 if (!error)
166 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
167 CONFIGFS_DIR);
168 if (!error) {
169 error = configfs_create(d, mode, init_dir);
170 if (!error) {
171 inc_nlink(p->d_inode);
172 (d)->d_op = &configfs_dentry_ops;
173 } else {
174 struct configfs_dirent *sd = d->d_fsdata;
175 if (sd) {
176 list_del_init(&sd->s_sibling);
177 configfs_put(sd);
181 return error;
186 * configfs_create_dir - create a directory for an config_item.
187 * @item: config_itemwe're creating directory for.
188 * @dentry: config_item's dentry.
191 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
193 struct dentry * parent;
194 int error = 0;
196 BUG_ON(!item);
198 if (item->ci_parent)
199 parent = item->ci_parent->ci_dentry;
200 else if (configfs_mount && configfs_mount->mnt_sb)
201 parent = configfs_mount->mnt_sb->s_root;
202 else
203 return -EFAULT;
205 error = create_dir(item,parent,dentry);
206 if (!error)
207 item->ci_dentry = dentry;
208 return error;
211 int configfs_create_link(struct configfs_symlink *sl,
212 struct dentry *parent,
213 struct dentry *dentry)
215 int err = 0;
216 umode_t mode = S_IFLNK | S_IRWXUGO;
218 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
219 CONFIGFS_ITEM_LINK);
220 if (!err) {
221 err = configfs_create(dentry, mode, init_symlink);
222 if (!err)
223 dentry->d_op = &configfs_dentry_ops;
224 else {
225 struct configfs_dirent *sd = dentry->d_fsdata;
226 if (sd) {
227 list_del_init(&sd->s_sibling);
228 configfs_put(sd);
232 return err;
235 static void remove_dir(struct dentry * d)
237 struct dentry * parent = dget(d->d_parent);
238 struct configfs_dirent * sd;
240 sd = d->d_fsdata;
241 list_del_init(&sd->s_sibling);
242 configfs_put(sd);
243 if (d->d_inode)
244 simple_rmdir(parent->d_inode,d);
246 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
247 atomic_read(&d->d_count));
249 dput(parent);
253 * configfs_remove_dir - remove an config_item's directory.
254 * @item: config_item we're removing.
256 * The only thing special about this is that we remove any files in
257 * the directory before we remove the directory, and we've inlined
258 * what used to be configfs_rmdir() below, instead of calling separately.
261 static void configfs_remove_dir(struct config_item * item)
263 struct dentry * dentry = dget(item->ci_dentry);
265 if (!dentry)
266 return;
268 remove_dir(dentry);
270 * Drop reference from dget() on entrance.
272 dput(dentry);
276 /* attaches attribute's configfs_dirent to the dentry corresponding to the
277 * attribute file
279 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
281 struct configfs_attribute * attr = sd->s_element;
282 int error;
284 dentry->d_fsdata = configfs_get(sd);
285 sd->s_dentry = dentry;
286 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
287 if (error) {
288 configfs_put(sd);
289 return error;
292 dentry->d_op = &configfs_dentry_ops;
293 d_rehash(dentry);
295 return 0;
298 static struct dentry * configfs_lookup(struct inode *dir,
299 struct dentry *dentry,
300 struct nameidata *nd)
302 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
303 struct configfs_dirent * sd;
304 int found = 0;
305 int err = 0;
307 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
308 if (sd->s_type & CONFIGFS_NOT_PINNED) {
309 const unsigned char * name = configfs_get_name(sd);
311 if (strcmp(name, dentry->d_name.name))
312 continue;
314 found = 1;
315 err = configfs_attach_attr(sd, dentry);
316 break;
320 if (!found) {
322 * If it doesn't exist and it isn't a NOT_PINNED item,
323 * it must be negative.
325 return simple_lookup(dir, dentry, nd);
328 return ERR_PTR(err);
332 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
333 * attributes and are removed by rmdir(). We recurse, taking i_mutex
334 * on all children that are candidates for default detach. If the
335 * result is clean, then configfs_detach_group() will handle dropping
336 * i_mutex. If there is an error, the caller will clean up the i_mutex
337 * holders via configfs_detach_rollback().
339 static int configfs_detach_prep(struct dentry *dentry)
341 struct configfs_dirent *parent_sd = dentry->d_fsdata;
342 struct configfs_dirent *sd;
343 int ret;
345 ret = -EBUSY;
346 if (!list_empty(&parent_sd->s_links))
347 goto out;
349 ret = 0;
350 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
351 if (sd->s_type & CONFIGFS_NOT_PINNED)
352 continue;
353 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
354 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
355 /* Mark that we've taken i_mutex */
356 sd->s_type |= CONFIGFS_USET_DROPPING;
358 ret = configfs_detach_prep(sd->s_dentry);
359 if (!ret)
360 continue;
361 } else
362 ret = -ENOTEMPTY;
364 break;
367 out:
368 return ret;
372 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
373 * set.
375 static void configfs_detach_rollback(struct dentry *dentry)
377 struct configfs_dirent *parent_sd = dentry->d_fsdata;
378 struct configfs_dirent *sd;
380 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
381 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
382 configfs_detach_rollback(sd->s_dentry);
384 if (sd->s_type & CONFIGFS_USET_DROPPING) {
385 sd->s_type &= ~CONFIGFS_USET_DROPPING;
386 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
392 static void detach_attrs(struct config_item * item)
394 struct dentry * dentry = dget(item->ci_dentry);
395 struct configfs_dirent * parent_sd;
396 struct configfs_dirent * sd, * tmp;
398 if (!dentry)
399 return;
401 pr_debug("configfs %s: dropping attrs for dir\n",
402 dentry->d_name.name);
404 parent_sd = dentry->d_fsdata;
405 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
406 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
407 continue;
408 list_del_init(&sd->s_sibling);
409 configfs_drop_dentry(sd, dentry);
410 configfs_put(sd);
414 * Drop reference from dget() on entrance.
416 dput(dentry);
419 static int populate_attrs(struct config_item *item)
421 struct config_item_type *t = item->ci_type;
422 struct configfs_attribute *attr;
423 int error = 0;
424 int i;
426 if (!t)
427 return -EINVAL;
428 if (t->ct_attrs) {
429 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
430 if ((error = configfs_create_file(item, attr)))
431 break;
435 if (error)
436 detach_attrs(item);
438 return error;
441 static int configfs_attach_group(struct config_item *parent_item,
442 struct config_item *item,
443 struct dentry *dentry);
444 static void configfs_detach_group(struct config_item *item);
446 static void detach_groups(struct config_group *group)
448 struct dentry * dentry = dget(group->cg_item.ci_dentry);
449 struct dentry *child;
450 struct configfs_dirent *parent_sd;
451 struct configfs_dirent *sd, *tmp;
453 if (!dentry)
454 return;
456 parent_sd = dentry->d_fsdata;
457 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
458 if (!sd->s_element ||
459 !(sd->s_type & CONFIGFS_USET_DEFAULT))
460 continue;
462 child = sd->s_dentry;
464 configfs_detach_group(sd->s_element);
465 child->d_inode->i_flags |= S_DEAD;
468 * From rmdir/unregister, a configfs_detach_prep() pass
469 * has taken our i_mutex for us. Drop it.
470 * From mkdir/register cleanup, there is no sem held.
472 if (sd->s_type & CONFIGFS_USET_DROPPING)
473 mutex_unlock(&child->d_inode->i_mutex);
475 d_delete(child);
476 dput(child);
480 * Drop reference from dget() on entrance.
482 dput(dentry);
486 * This fakes mkdir(2) on a default_groups[] entry. It
487 * creates a dentry, attachs it, and then does fixup
488 * on the sd->s_type.
490 * We could, perhaps, tweak our parent's ->mkdir for a minute and
491 * try using vfs_mkdir. Just a thought.
493 static int create_default_group(struct config_group *parent_group,
494 struct config_group *group)
496 int ret;
497 struct qstr name;
498 struct configfs_dirent *sd;
499 /* We trust the caller holds a reference to parent */
500 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
502 if (!group->cg_item.ci_name)
503 group->cg_item.ci_name = group->cg_item.ci_namebuf;
504 name.name = group->cg_item.ci_name;
505 name.len = strlen(name.name);
506 name.hash = full_name_hash(name.name, name.len);
508 ret = -ENOMEM;
509 child = d_alloc(parent, &name);
510 if (child) {
511 d_add(child, NULL);
513 ret = configfs_attach_group(&parent_group->cg_item,
514 &group->cg_item, child);
515 if (!ret) {
516 sd = child->d_fsdata;
517 sd->s_type |= CONFIGFS_USET_DEFAULT;
518 } else {
519 d_delete(child);
520 dput(child);
524 return ret;
527 static int populate_groups(struct config_group *group)
529 struct config_group *new_group;
530 struct dentry *dentry = group->cg_item.ci_dentry;
531 int ret = 0;
532 int i;
534 if (group->default_groups) {
536 * FYI, we're faking mkdir here
537 * I'm not sure we need this semaphore, as we're called
538 * from our parent's mkdir. That holds our parent's
539 * i_mutex, so afaik lookup cannot continue through our
540 * parent to find us, let alone mess with our tree.
541 * That said, taking our i_mutex is closer to mkdir
542 * emulation, and shouldn't hurt.
544 mutex_lock(&dentry->d_inode->i_mutex);
546 for (i = 0; group->default_groups[i]; i++) {
547 new_group = group->default_groups[i];
549 ret = create_default_group(group, new_group);
550 if (ret)
551 break;
554 mutex_unlock(&dentry->d_inode->i_mutex);
557 if (ret)
558 detach_groups(group);
560 return ret;
564 * All of link_obj/unlink_obj/link_group/unlink_group require that
565 * subsys->su_sem is held.
568 static void unlink_obj(struct config_item *item)
570 struct config_group *group;
572 group = item->ci_group;
573 if (group) {
574 list_del_init(&item->ci_entry);
576 item->ci_group = NULL;
577 item->ci_parent = NULL;
579 /* Drop the reference for ci_entry */
580 config_item_put(item);
582 /* Drop the reference for ci_parent */
583 config_group_put(group);
587 static void link_obj(struct config_item *parent_item, struct config_item *item)
590 * Parent seems redundant with group, but it makes certain
591 * traversals much nicer.
593 item->ci_parent = parent_item;
596 * We hold a reference on the parent for the child's ci_parent
597 * link.
599 item->ci_group = config_group_get(to_config_group(parent_item));
600 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
603 * We hold a reference on the child for ci_entry on the parent's
604 * cg_children
606 config_item_get(item);
609 static void unlink_group(struct config_group *group)
611 int i;
612 struct config_group *new_group;
614 if (group->default_groups) {
615 for (i = 0; group->default_groups[i]; i++) {
616 new_group = group->default_groups[i];
617 unlink_group(new_group);
621 group->cg_subsys = NULL;
622 unlink_obj(&group->cg_item);
625 static void link_group(struct config_group *parent_group, struct config_group *group)
627 int i;
628 struct config_group *new_group;
629 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
631 link_obj(&parent_group->cg_item, &group->cg_item);
633 if (parent_group->cg_subsys)
634 subsys = parent_group->cg_subsys;
635 else if (configfs_is_root(&parent_group->cg_item))
636 subsys = to_configfs_subsystem(group);
637 else
638 BUG();
639 group->cg_subsys = subsys;
641 if (group->default_groups) {
642 for (i = 0; group->default_groups[i]; i++) {
643 new_group = group->default_groups[i];
644 link_group(group, new_group);
650 * The goal is that configfs_attach_item() (and
651 * configfs_attach_group()) can be called from either the VFS or this
652 * module. That is, they assume that the items have been created,
653 * the dentry allocated, and the dcache is all ready to go.
655 * If they fail, they must clean up after themselves as if they
656 * had never been called. The caller (VFS or local function) will
657 * handle cleaning up the dcache bits.
659 * configfs_detach_group() and configfs_detach_item() behave similarly on
660 * the way out. They assume that the proper semaphores are held, they
661 * clean up the configfs items, and they expect their callers will
662 * handle the dcache bits.
664 static int configfs_attach_item(struct config_item *parent_item,
665 struct config_item *item,
666 struct dentry *dentry)
668 int ret;
670 ret = configfs_create_dir(item, dentry);
671 if (!ret) {
672 ret = populate_attrs(item);
673 if (ret) {
674 configfs_remove_dir(item);
675 d_delete(dentry);
679 return ret;
682 static void configfs_detach_item(struct config_item *item)
684 detach_attrs(item);
685 configfs_remove_dir(item);
688 static int configfs_attach_group(struct config_item *parent_item,
689 struct config_item *item,
690 struct dentry *dentry)
692 int ret;
693 struct configfs_dirent *sd;
695 ret = configfs_attach_item(parent_item, item, dentry);
696 if (!ret) {
697 sd = dentry->d_fsdata;
698 sd->s_type |= CONFIGFS_USET_DIR;
700 ret = populate_groups(to_config_group(item));
701 if (ret) {
702 configfs_detach_item(item);
703 d_delete(dentry);
707 return ret;
710 static void configfs_detach_group(struct config_item *item)
712 detach_groups(to_config_group(item));
713 configfs_detach_item(item);
717 * Drop the initial reference from make_item()/make_group()
718 * This function assumes that reference is held on item
719 * and that item holds a valid reference to the parent. Also, it
720 * assumes the caller has validated ci_type.
722 static void client_drop_item(struct config_item *parent_item,
723 struct config_item *item)
725 struct config_item_type *type;
727 type = parent_item->ci_type;
728 BUG_ON(!type);
731 * If ->drop_item() exists, it is responsible for the
732 * config_item_put().
734 if (type->ct_group_ops && type->ct_group_ops->drop_item)
735 type->ct_group_ops->drop_item(to_config_group(parent_item),
736 item);
737 else
738 config_item_put(item);
742 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
744 int ret, module_got = 0;
745 struct config_group *group;
746 struct config_item *item;
747 struct config_item *parent_item;
748 struct configfs_subsystem *subsys;
749 struct configfs_dirent *sd;
750 struct config_item_type *type;
751 struct module *owner = NULL;
752 char *name;
754 if (dentry->d_parent == configfs_sb->s_root) {
755 ret = -EPERM;
756 goto out;
759 sd = dentry->d_parent->d_fsdata;
760 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
761 ret = -EPERM;
762 goto out;
765 /* Get a working ref for the duration of this function */
766 parent_item = configfs_get_config_item(dentry->d_parent);
767 type = parent_item->ci_type;
768 subsys = to_config_group(parent_item)->cg_subsys;
769 BUG_ON(!subsys);
771 if (!type || !type->ct_group_ops ||
772 (!type->ct_group_ops->make_group &&
773 !type->ct_group_ops->make_item)) {
774 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
775 goto out_put;
778 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
779 if (!name) {
780 ret = -ENOMEM;
781 goto out_put;
784 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
786 down(&subsys->su_sem);
787 group = NULL;
788 item = NULL;
789 if (type->ct_group_ops->make_group) {
790 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
791 if (group) {
792 link_group(to_config_group(parent_item), group);
793 item = &group->cg_item;
795 } else {
796 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
797 if (item)
798 link_obj(parent_item, item);
800 up(&subsys->su_sem);
802 kfree(name);
803 if (!item) {
805 * If item == NULL, then link_obj() was never called.
806 * There are no extra references to clean up.
808 ret = -ENOMEM;
809 goto out_put;
813 * link_obj() has been called (via link_group() for groups).
814 * From here on out, errors must clean that up.
817 type = item->ci_type;
818 if (!type) {
819 ret = -EINVAL;
820 goto out_unlink;
823 owner = type->ct_owner;
824 if (!try_module_get(owner)) {
825 ret = -EINVAL;
826 goto out_unlink;
830 * I hate doing it this way, but if there is
831 * an error, module_put() probably should
832 * happen after any cleanup.
834 module_got = 1;
836 if (group)
837 ret = configfs_attach_group(parent_item, item, dentry);
838 else
839 ret = configfs_attach_item(parent_item, item, dentry);
841 out_unlink:
842 if (ret) {
843 /* Tear down everything we built up */
844 down(&subsys->su_sem);
845 if (group)
846 unlink_group(group);
847 else
848 unlink_obj(item);
849 client_drop_item(parent_item, item);
850 up(&subsys->su_sem);
852 if (module_got)
853 module_put(owner);
856 out_put:
858 * link_obj()/link_group() took a reference from child->parent,
859 * so the parent is safely pinned. We can drop our working
860 * reference.
862 config_item_put(parent_item);
864 out:
865 return ret;
868 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
870 struct config_item *parent_item;
871 struct config_item *item;
872 struct configfs_subsystem *subsys;
873 struct configfs_dirent *sd;
874 struct module *owner = NULL;
875 int ret;
877 if (dentry->d_parent == configfs_sb->s_root)
878 return -EPERM;
880 sd = dentry->d_fsdata;
881 if (sd->s_type & CONFIGFS_USET_DEFAULT)
882 return -EPERM;
884 /* Get a working ref until we have the child */
885 parent_item = configfs_get_config_item(dentry->d_parent);
886 subsys = to_config_group(parent_item)->cg_subsys;
887 BUG_ON(!subsys);
889 if (!parent_item->ci_type) {
890 config_item_put(parent_item);
891 return -EINVAL;
894 ret = configfs_detach_prep(dentry);
895 if (ret) {
896 configfs_detach_rollback(dentry);
897 config_item_put(parent_item);
898 return ret;
901 /* Get a working ref for the duration of this function */
902 item = configfs_get_config_item(dentry);
904 /* Drop reference from above, item already holds one. */
905 config_item_put(parent_item);
907 if (item->ci_type)
908 owner = item->ci_type->ct_owner;
910 if (sd->s_type & CONFIGFS_USET_DIR) {
911 configfs_detach_group(item);
913 down(&subsys->su_sem);
914 unlink_group(to_config_group(item));
915 } else {
916 configfs_detach_item(item);
918 down(&subsys->su_sem);
919 unlink_obj(item);
922 client_drop_item(parent_item, item);
923 up(&subsys->su_sem);
925 /* Drop our reference from above */
926 config_item_put(item);
928 module_put(owner);
930 return 0;
933 const struct inode_operations configfs_dir_inode_operations = {
934 .mkdir = configfs_mkdir,
935 .rmdir = configfs_rmdir,
936 .symlink = configfs_symlink,
937 .unlink = configfs_unlink,
938 .lookup = configfs_lookup,
939 .setattr = configfs_setattr,
942 #if 0
943 int configfs_rename_dir(struct config_item * item, const char *new_name)
945 int error = 0;
946 struct dentry * new_dentry, * parent;
948 if (!strcmp(config_item_name(item), new_name))
949 return -EINVAL;
951 if (!item->parent)
952 return -EINVAL;
954 down_write(&configfs_rename_sem);
955 parent = item->parent->dentry;
957 mutex_lock(&parent->d_inode->i_mutex);
959 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
960 if (!IS_ERR(new_dentry)) {
961 if (!new_dentry->d_inode) {
962 error = config_item_set_name(item, "%s", new_name);
963 if (!error) {
964 d_add(new_dentry, NULL);
965 d_move(item->dentry, new_dentry);
967 else
968 d_delete(new_dentry);
969 } else
970 error = -EEXIST;
971 dput(new_dentry);
973 mutex_unlock(&parent->d_inode->i_mutex);
974 up_write(&configfs_rename_sem);
976 return error;
978 #endif
980 static int configfs_dir_open(struct inode *inode, struct file *file)
982 struct dentry * dentry = file->f_path.dentry;
983 struct configfs_dirent * parent_sd = dentry->d_fsdata;
985 mutex_lock(&dentry->d_inode->i_mutex);
986 file->private_data = configfs_new_dirent(parent_sd, NULL);
987 mutex_unlock(&dentry->d_inode->i_mutex);
989 return file->private_data ? 0 : -ENOMEM;
993 static int configfs_dir_close(struct inode *inode, struct file *file)
995 struct dentry * dentry = file->f_path.dentry;
996 struct configfs_dirent * cursor = file->private_data;
998 mutex_lock(&dentry->d_inode->i_mutex);
999 list_del_init(&cursor->s_sibling);
1000 mutex_unlock(&dentry->d_inode->i_mutex);
1002 release_configfs_dirent(cursor);
1004 return 0;
1007 /* Relationship between s_mode and the DT_xxx types */
1008 static inline unsigned char dt_type(struct configfs_dirent *sd)
1010 return (sd->s_mode >> 12) & 15;
1013 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1015 struct dentry *dentry = filp->f_path.dentry;
1016 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1017 struct configfs_dirent *cursor = filp->private_data;
1018 struct list_head *p, *q = &cursor->s_sibling;
1019 ino_t ino;
1020 int i = filp->f_pos;
1022 switch (i) {
1023 case 0:
1024 ino = dentry->d_inode->i_ino;
1025 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1026 break;
1027 filp->f_pos++;
1028 i++;
1029 /* fallthrough */
1030 case 1:
1031 ino = parent_ino(dentry);
1032 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1033 break;
1034 filp->f_pos++;
1035 i++;
1036 /* fallthrough */
1037 default:
1038 if (filp->f_pos == 2) {
1039 list_move(q, &parent_sd->s_children);
1041 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1042 struct configfs_dirent *next;
1043 const char * name;
1044 int len;
1046 next = list_entry(p, struct configfs_dirent,
1047 s_sibling);
1048 if (!next->s_element)
1049 continue;
1051 name = configfs_get_name(next);
1052 len = strlen(name);
1053 if (next->s_dentry)
1054 ino = next->s_dentry->d_inode->i_ino;
1055 else
1056 ino = iunique(configfs_sb, 2);
1058 if (filldir(dirent, name, len, filp->f_pos, ino,
1059 dt_type(next)) < 0)
1060 return 0;
1062 list_move(q, p);
1063 p = q;
1064 filp->f_pos++;
1067 return 0;
1070 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1072 struct dentry * dentry = file->f_path.dentry;
1074 mutex_lock(&dentry->d_inode->i_mutex);
1075 switch (origin) {
1076 case 1:
1077 offset += file->f_pos;
1078 case 0:
1079 if (offset >= 0)
1080 break;
1081 default:
1082 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
1083 return -EINVAL;
1085 if (offset != file->f_pos) {
1086 file->f_pos = offset;
1087 if (file->f_pos >= 2) {
1088 struct configfs_dirent *sd = dentry->d_fsdata;
1089 struct configfs_dirent *cursor = file->private_data;
1090 struct list_head *p;
1091 loff_t n = file->f_pos - 2;
1093 list_del(&cursor->s_sibling);
1094 p = sd->s_children.next;
1095 while (n && p != &sd->s_children) {
1096 struct configfs_dirent *next;
1097 next = list_entry(p, struct configfs_dirent,
1098 s_sibling);
1099 if (next->s_element)
1100 n--;
1101 p = p->next;
1103 list_add_tail(&cursor->s_sibling, p);
1106 mutex_unlock(&dentry->d_inode->i_mutex);
1107 return offset;
1110 const struct file_operations configfs_dir_operations = {
1111 .open = configfs_dir_open,
1112 .release = configfs_dir_close,
1113 .llseek = configfs_dir_lseek,
1114 .read = generic_read_dir,
1115 .readdir = configfs_readdir,
1118 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1120 int err;
1121 struct config_group *group = &subsys->su_group;
1122 struct qstr name;
1123 struct dentry *dentry;
1124 struct configfs_dirent *sd;
1126 err = configfs_pin_fs();
1127 if (err)
1128 return err;
1130 if (!group->cg_item.ci_name)
1131 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1133 sd = configfs_sb->s_root->d_fsdata;
1134 link_group(to_config_group(sd->s_element), group);
1136 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
1138 name.name = group->cg_item.ci_name;
1139 name.len = strlen(name.name);
1140 name.hash = full_name_hash(name.name, name.len);
1142 err = -ENOMEM;
1143 dentry = d_alloc(configfs_sb->s_root, &name);
1144 if (dentry) {
1145 d_add(dentry, NULL);
1147 err = configfs_attach_group(sd->s_element, &group->cg_item,
1148 dentry);
1149 if (err) {
1150 d_delete(dentry);
1151 dput(dentry);
1155 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1157 if (err) {
1158 unlink_group(group);
1159 configfs_release_fs();
1162 return err;
1165 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1167 struct config_group *group = &subsys->su_group;
1168 struct dentry *dentry = group->cg_item.ci_dentry;
1170 if (dentry->d_parent != configfs_sb->s_root) {
1171 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1172 return;
1175 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1176 I_MUTEX_PARENT);
1177 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1178 if (configfs_detach_prep(dentry)) {
1179 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1181 configfs_detach_group(&group->cg_item);
1182 dentry->d_inode->i_flags |= S_DEAD;
1183 mutex_unlock(&dentry->d_inode->i_mutex);
1185 d_delete(dentry);
1187 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1189 dput(dentry);
1191 unlink_group(group);
1192 configfs_release_fs();
1195 EXPORT_SYMBOL(configfs_register_subsystem);
1196 EXPORT_SYMBOL(configfs_unregister_subsystem);