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.
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
30 #include <linux/mount.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/err.h>
35 #include <linux/configfs.h>
36 #include "configfs_internal.h"
38 DECLARE_RWSEM(configfs_rename_sem);
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
41 * Also protects mutations of symlinks linkage to target configfs_dirent
42 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
44 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
47 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
48 * unlocked is not reliable unless in detach_groups() called from
49 * rmdir()/unregister() and from configfs_attach_group()
51 DEFINE_SPINLOCK(configfs_dirent_lock);
53 static void configfs_d_iput(struct dentry * dentry,
56 struct configfs_dirent * sd = dentry->d_fsdata;
59 BUG_ON(sd->s_dentry != dentry);
67 * We _must_ delete our dentries on last dput, as the chain-to-parent
68 * behavior is required to clear the parents of default_groups.
70 static int configfs_d_delete(struct dentry *dentry)
75 static struct dentry_operations configfs_dentry_ops = {
76 .d_iput = configfs_d_iput,
77 /* simple_delete_dentry() isn't exported */
78 .d_delete = configfs_d_delete,
82 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
84 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
87 struct configfs_dirent * sd;
89 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
91 return ERR_PTR(-ENOMEM);
93 atomic_set(&sd->s_count, 1);
94 INIT_LIST_HEAD(&sd->s_links);
95 INIT_LIST_HEAD(&sd->s_children);
96 sd->s_element = element;
97 spin_lock(&configfs_dirent_lock);
98 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
99 spin_unlock(&configfs_dirent_lock);
100 kmem_cache_free(configfs_dir_cachep, sd);
101 return ERR_PTR(-ENOENT);
103 list_add(&sd->s_sibling, &parent_sd->s_children);
104 spin_unlock(&configfs_dirent_lock);
111 * Return -EEXIST if there is already a configfs element with the same
112 * name for the same parent.
114 * called with parent inode's i_mutex held
116 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
117 const unsigned char *new)
119 struct configfs_dirent * sd;
121 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
123 const unsigned char *existing = configfs_get_name(sd);
124 if (strcmp(existing, new))
135 int configfs_make_dirent(struct configfs_dirent * parent_sd,
136 struct dentry * dentry, void * element,
137 umode_t mode, int type)
139 struct configfs_dirent * sd;
141 sd = configfs_new_dirent(parent_sd, element);
147 sd->s_dentry = dentry;
149 dentry->d_fsdata = configfs_get(sd);
150 dentry->d_op = &configfs_dentry_ops;
156 static int init_dir(struct inode * inode)
158 inode->i_op = &configfs_dir_inode_operations;
159 inode->i_fop = &configfs_dir_operations;
161 /* directory inodes start off with i_nlink == 2 (for "." entry) */
166 static int configfs_init_file(struct inode * inode)
168 inode->i_size = PAGE_SIZE;
169 inode->i_fop = &configfs_file_operations;
173 static int init_symlink(struct inode * inode)
175 inode->i_op = &configfs_symlink_inode_operations;
179 static int create_dir(struct config_item * k, struct dentry * p,
183 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
185 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
187 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
190 error = configfs_create(d, mode, init_dir);
192 inc_nlink(p->d_inode);
193 (d)->d_op = &configfs_dentry_ops;
195 struct configfs_dirent *sd = d->d_fsdata;
197 spin_lock(&configfs_dirent_lock);
198 list_del_init(&sd->s_sibling);
199 spin_unlock(&configfs_dirent_lock);
209 * configfs_create_dir - create a directory for an config_item.
210 * @item: config_itemwe're creating directory for.
211 * @dentry: config_item's dentry.
214 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
216 struct dentry * parent;
222 parent = item->ci_parent->ci_dentry;
223 else if (configfs_mount && configfs_mount->mnt_sb)
224 parent = configfs_mount->mnt_sb->s_root;
228 error = create_dir(item,parent,dentry);
230 item->ci_dentry = dentry;
234 int configfs_create_link(struct configfs_symlink *sl,
235 struct dentry *parent,
236 struct dentry *dentry)
239 umode_t mode = S_IFLNK | S_IRWXUGO;
241 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
244 err = configfs_create(dentry, mode, init_symlink);
246 dentry->d_op = &configfs_dentry_ops;
248 struct configfs_dirent *sd = dentry->d_fsdata;
250 spin_lock(&configfs_dirent_lock);
251 list_del_init(&sd->s_sibling);
252 spin_unlock(&configfs_dirent_lock);
260 static void remove_dir(struct dentry * d)
262 struct dentry * parent = dget(d->d_parent);
263 struct configfs_dirent * sd;
266 spin_lock(&configfs_dirent_lock);
267 list_del_init(&sd->s_sibling);
268 spin_unlock(&configfs_dirent_lock);
271 simple_rmdir(parent->d_inode,d);
273 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
274 atomic_read(&d->d_count));
280 * configfs_remove_dir - remove an config_item's directory.
281 * @item: config_item we're removing.
283 * The only thing special about this is that we remove any files in
284 * the directory before we remove the directory, and we've inlined
285 * what used to be configfs_rmdir() below, instead of calling separately.
288 static void configfs_remove_dir(struct config_item * item)
290 struct dentry * dentry = dget(item->ci_dentry);
297 * Drop reference from dget() on entrance.
303 /* attaches attribute's configfs_dirent to the dentry corresponding to the
306 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
308 struct configfs_attribute * attr = sd->s_element;
311 dentry->d_fsdata = configfs_get(sd);
312 sd->s_dentry = dentry;
313 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
320 dentry->d_op = &configfs_dentry_ops;
326 static struct dentry * configfs_lookup(struct inode *dir,
327 struct dentry *dentry,
328 struct nameidata *nd)
330 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
331 struct configfs_dirent * sd;
335 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
336 if (sd->s_type & CONFIGFS_NOT_PINNED) {
337 const unsigned char * name = configfs_get_name(sd);
339 if (strcmp(name, dentry->d_name.name))
343 err = configfs_attach_attr(sd, dentry);
350 * If it doesn't exist and it isn't a NOT_PINNED item,
351 * it must be negative.
353 return simple_lookup(dir, dentry, nd);
360 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
361 * attributes and are removed by rmdir(). We recurse, setting
362 * CONFIGFS_USET_DROPPING on all children that are candidates for
364 * If there is an error, the caller will reset the flags via
365 * configfs_detach_rollback().
367 static int configfs_detach_prep(struct dentry *dentry)
369 struct configfs_dirent *parent_sd = dentry->d_fsdata;
370 struct configfs_dirent *sd;
374 if (!list_empty(&parent_sd->s_links))
378 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
379 if (sd->s_type & CONFIGFS_NOT_PINNED)
381 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
382 /* Mark that we're trying to drop the group */
383 sd->s_type |= CONFIGFS_USET_DROPPING;
386 * Yup, recursive. If there's a problem, blame
387 * deep nesting of default_groups
389 ret = configfs_detach_prep(sd->s_dentry);
403 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
406 static void configfs_detach_rollback(struct dentry *dentry)
408 struct configfs_dirent *parent_sd = dentry->d_fsdata;
409 struct configfs_dirent *sd;
411 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
412 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
413 configfs_detach_rollback(sd->s_dentry);
414 sd->s_type &= ~CONFIGFS_USET_DROPPING;
419 static void detach_attrs(struct config_item * item)
421 struct dentry * dentry = dget(item->ci_dentry);
422 struct configfs_dirent * parent_sd;
423 struct configfs_dirent * sd, * tmp;
428 pr_debug("configfs %s: dropping attrs for dir\n",
429 dentry->d_name.name);
431 parent_sd = dentry->d_fsdata;
432 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
433 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
435 spin_lock(&configfs_dirent_lock);
436 list_del_init(&sd->s_sibling);
437 spin_unlock(&configfs_dirent_lock);
438 configfs_drop_dentry(sd, dentry);
443 * Drop reference from dget() on entrance.
448 static int populate_attrs(struct config_item *item)
450 struct config_item_type *t = item->ci_type;
451 struct configfs_attribute *attr;
458 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
459 if ((error = configfs_create_file(item, attr)))
470 static int configfs_attach_group(struct config_item *parent_item,
471 struct config_item *item,
472 struct dentry *dentry);
473 static void configfs_detach_group(struct config_item *item);
475 static void detach_groups(struct config_group *group)
477 struct dentry * dentry = dget(group->cg_item.ci_dentry);
478 struct dentry *child;
479 struct configfs_dirent *parent_sd;
480 struct configfs_dirent *sd, *tmp;
485 parent_sd = dentry->d_fsdata;
486 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
487 if (!sd->s_element ||
488 !(sd->s_type & CONFIGFS_USET_DEFAULT))
491 child = sd->s_dentry;
493 mutex_lock(&child->d_inode->i_mutex);
495 configfs_detach_group(sd->s_element);
496 child->d_inode->i_flags |= S_DEAD;
498 mutex_unlock(&child->d_inode->i_mutex);
505 * Drop reference from dget() on entrance.
511 * This fakes mkdir(2) on a default_groups[] entry. It
512 * creates a dentry, attachs it, and then does fixup
515 * We could, perhaps, tweak our parent's ->mkdir for a minute and
516 * try using vfs_mkdir. Just a thought.
518 static int create_default_group(struct config_group *parent_group,
519 struct config_group *group)
523 struct configfs_dirent *sd;
524 /* We trust the caller holds a reference to parent */
525 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
527 if (!group->cg_item.ci_name)
528 group->cg_item.ci_name = group->cg_item.ci_namebuf;
529 name.name = group->cg_item.ci_name;
530 name.len = strlen(name.name);
531 name.hash = full_name_hash(name.name, name.len);
534 child = d_alloc(parent, &name);
538 ret = configfs_attach_group(&parent_group->cg_item,
539 &group->cg_item, child);
541 sd = child->d_fsdata;
542 sd->s_type |= CONFIGFS_USET_DEFAULT;
552 static int populate_groups(struct config_group *group)
554 struct config_group *new_group;
555 struct dentry *dentry = group->cg_item.ci_dentry;
559 if (group->default_groups) {
561 * FYI, we're faking mkdir here
562 * I'm not sure we need this semaphore, as we're called
563 * from our parent's mkdir. That holds our parent's
564 * i_mutex, so afaik lookup cannot continue through our
565 * parent to find us, let alone mess with our tree.
566 * That said, taking our i_mutex is closer to mkdir
567 * emulation, and shouldn't hurt.
569 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
571 for (i = 0; group->default_groups[i]; i++) {
572 new_group = group->default_groups[i];
574 ret = create_default_group(group, new_group);
579 mutex_unlock(&dentry->d_inode->i_mutex);
583 detach_groups(group);
589 * All of link_obj/unlink_obj/link_group/unlink_group require that
590 * subsys->su_mutex is held.
593 static void unlink_obj(struct config_item *item)
595 struct config_group *group;
597 group = item->ci_group;
599 list_del_init(&item->ci_entry);
601 item->ci_group = NULL;
602 item->ci_parent = NULL;
604 /* Drop the reference for ci_entry */
605 config_item_put(item);
607 /* Drop the reference for ci_parent */
608 config_group_put(group);
612 static void link_obj(struct config_item *parent_item, struct config_item *item)
615 * Parent seems redundant with group, but it makes certain
616 * traversals much nicer.
618 item->ci_parent = parent_item;
621 * We hold a reference on the parent for the child's ci_parent
624 item->ci_group = config_group_get(to_config_group(parent_item));
625 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
628 * We hold a reference on the child for ci_entry on the parent's
631 config_item_get(item);
634 static void unlink_group(struct config_group *group)
637 struct config_group *new_group;
639 if (group->default_groups) {
640 for (i = 0; group->default_groups[i]; i++) {
641 new_group = group->default_groups[i];
642 unlink_group(new_group);
646 group->cg_subsys = NULL;
647 unlink_obj(&group->cg_item);
650 static void link_group(struct config_group *parent_group, struct config_group *group)
653 struct config_group *new_group;
654 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
656 link_obj(&parent_group->cg_item, &group->cg_item);
658 if (parent_group->cg_subsys)
659 subsys = parent_group->cg_subsys;
660 else if (configfs_is_root(&parent_group->cg_item))
661 subsys = to_configfs_subsystem(group);
664 group->cg_subsys = subsys;
666 if (group->default_groups) {
667 for (i = 0; group->default_groups[i]; i++) {
668 new_group = group->default_groups[i];
669 link_group(group, new_group);
675 * The goal is that configfs_attach_item() (and
676 * configfs_attach_group()) can be called from either the VFS or this
677 * module. That is, they assume that the items have been created,
678 * the dentry allocated, and the dcache is all ready to go.
680 * If they fail, they must clean up after themselves as if they
681 * had never been called. The caller (VFS or local function) will
682 * handle cleaning up the dcache bits.
684 * configfs_detach_group() and configfs_detach_item() behave similarly on
685 * the way out. They assume that the proper semaphores are held, they
686 * clean up the configfs items, and they expect their callers will
687 * handle the dcache bits.
689 static int configfs_attach_item(struct config_item *parent_item,
690 struct config_item *item,
691 struct dentry *dentry)
695 ret = configfs_create_dir(item, dentry);
697 ret = populate_attrs(item);
699 configfs_remove_dir(item);
707 static void configfs_detach_item(struct config_item *item)
710 configfs_remove_dir(item);
713 static int configfs_attach_group(struct config_item *parent_item,
714 struct config_item *item,
715 struct dentry *dentry)
718 struct configfs_dirent *sd;
720 ret = configfs_attach_item(parent_item, item, dentry);
722 sd = dentry->d_fsdata;
723 sd->s_type |= CONFIGFS_USET_DIR;
725 ret = populate_groups(to_config_group(item));
727 configfs_detach_item(item);
735 static void configfs_detach_group(struct config_item *item)
737 detach_groups(to_config_group(item));
738 configfs_detach_item(item);
742 * After the item has been detached from the filesystem view, we are
743 * ready to tear it out of the hierarchy. Notify the client before
744 * we do that so they can perform any cleanup that requires
745 * navigating the hierarchy. A client does not need to provide this
746 * callback. The subsystem semaphore MUST be held by the caller, and
747 * references must be valid for both items. It also assumes the
748 * caller has validated ci_type.
750 static void client_disconnect_notify(struct config_item *parent_item,
751 struct config_item *item)
753 struct config_item_type *type;
755 type = parent_item->ci_type;
758 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
759 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
764 * Drop the initial reference from make_item()/make_group()
765 * This function assumes that reference is held on item
766 * and that item holds a valid reference to the parent. Also, it
767 * assumes the caller has validated ci_type.
769 static void client_drop_item(struct config_item *parent_item,
770 struct config_item *item)
772 struct config_item_type *type;
774 type = parent_item->ci_type;
778 * If ->drop_item() exists, it is responsible for the
781 if (type->ct_group_ops && type->ct_group_ops->drop_item)
782 type->ct_group_ops->drop_item(to_config_group(parent_item),
785 config_item_put(item);
789 static void configfs_dump_one(struct configfs_dirent *sd, int level)
791 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
793 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
794 type_print(CONFIGFS_ROOT);
795 type_print(CONFIGFS_DIR);
796 type_print(CONFIGFS_ITEM_ATTR);
797 type_print(CONFIGFS_ITEM_LINK);
798 type_print(CONFIGFS_USET_DIR);
799 type_print(CONFIGFS_USET_DEFAULT);
800 type_print(CONFIGFS_USET_DROPPING);
804 static int configfs_dump(struct configfs_dirent *sd, int level)
806 struct configfs_dirent *child_sd;
809 configfs_dump_one(sd, level);
811 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
814 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
815 ret = configfs_dump(child_sd, level + 2);
826 * configfs_depend_item() and configfs_undepend_item()
828 * WARNING: Do not call these from a configfs callback!
830 * This describes these functions and their helpers.
832 * Allow another kernel system to depend on a config_item. If this
833 * happens, the item cannot go away until the dependant can live without
834 * it. The idea is to give client modules as simple an interface as
835 * possible. When a system asks them to depend on an item, they just
836 * call configfs_depend_item(). If the item is live and the client
837 * driver is in good shape, we'll happily do the work for them.
839 * Why is the locking complex? Because configfs uses the VFS to handle
840 * all locking, but this function is called outside the normal
841 * VFS->configfs path. So it must take VFS locks to prevent the
842 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
843 * why you can't call these functions underneath configfs callbacks.
845 * Note, btw, that this can be called at *any* time, even when a configfs
846 * subsystem isn't registered, or when configfs is loading or unloading.
847 * Just like configfs_register_subsystem(). So we take the same
848 * precautions. We pin the filesystem. We lock each i_mutex _in_order_
849 * on our way down the tree. If we can find the target item in the
850 * configfs tree, it must be part of the subsystem tree as well, so we
851 * do not need the subsystem semaphore. Holding the i_mutex chain locks
852 * out mkdir() and rmdir(), who might be racing us.
856 * configfs_depend_prep()
858 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
859 * attributes. This is similar but not the same to configfs_detach_prep().
860 * Note that configfs_detach_prep() expects the parent to be locked when it
861 * is called, but we lock the parent *inside* configfs_depend_prep(). We
862 * do that so we can unlock it if we find nothing.
864 * Here we do a depth-first search of the dentry hierarchy looking for
865 * our object. We take i_mutex on each step of the way down. IT IS
866 * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
867 * we'll drop the i_mutex.
869 * If the target is not found, -ENOENT is bubbled up and we have released
870 * all locks. If the target was found, the locks will be cleared by
871 * configfs_depend_rollback().
873 * This adds a requirement that all config_items be unique!
875 * This is recursive because the locking traversal is tricky. There isn't
876 * much on the stack, though, so folks that need this function - be careful
877 * about your stack! Patches will be accepted to make it iterative.
879 static int configfs_depend_prep(struct dentry *origin,
880 struct config_item *target)
882 struct configfs_dirent *child_sd, *sd = origin->d_fsdata;
885 BUG_ON(!origin || !sd);
887 /* Lock this guy on the way down */
888 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
889 if (sd->s_element == target) /* Boo-yah */
892 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
893 if (child_sd->s_type & CONFIGFS_DIR) {
894 ret = configfs_depend_prep(child_sd->s_dentry,
897 goto out; /* Child path boo-yah */
901 /* We looped all our children and didn't find target */
902 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
910 * This is ONLY called if configfs_depend_prep() did its job. So we can
911 * trust the entire path from item back up to origin.
913 * We walk backwards from item, unlocking each i_mutex. We finish by
916 static void configfs_depend_rollback(struct dentry *origin,
917 struct config_item *item)
919 struct dentry *dentry = item->ci_dentry;
921 while (dentry != origin) {
922 mutex_unlock(&dentry->d_inode->i_mutex);
923 dentry = dentry->d_parent;
926 mutex_unlock(&origin->d_inode->i_mutex);
929 int configfs_depend_item(struct configfs_subsystem *subsys,
930 struct config_item *target)
933 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
934 struct config_item *s_item = &subsys->su_group.cg_item;
937 * Pin the configfs filesystem. This means we can safely access
938 * the root of the configfs filesystem.
940 ret = configfs_pin_fs();
945 * Next, lock the root directory. We're going to check that the
946 * subsystem is really registered, and so we need to lock out
947 * configfs_[un]register_subsystem().
949 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
951 root_sd = configfs_sb->s_root->d_fsdata;
953 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
954 if (p->s_type & CONFIGFS_DIR) {
955 if (p->s_element == s_item) {
967 /* Ok, now we can trust subsys/s_item */
969 /* Scan the tree, locking i_mutex recursively, return 0 if found */
970 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
974 /* We hold all i_mutexes from the subsystem down to the target */
975 p = target->ci_dentry->d_fsdata;
976 p->s_dependent_count += 1;
978 configfs_depend_rollback(subsys_sd->s_dentry, target);
981 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
984 * If we succeeded, the fs is pinned via other methods. If not,
985 * we're done with it anyway. So release_fs() is always right.
987 configfs_release_fs();
991 EXPORT_SYMBOL(configfs_depend_item);
994 * Release the dependent linkage. This is much simpler than
995 * configfs_depend_item() because we know that that the client driver is
996 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
998 void configfs_undepend_item(struct configfs_subsystem *subsys,
999 struct config_item *target)
1001 struct configfs_dirent *sd;
1004 * Since we can trust everything is pinned, we just need i_mutex
1007 mutex_lock(&target->ci_dentry->d_inode->i_mutex);
1009 sd = target->ci_dentry->d_fsdata;
1010 BUG_ON(sd->s_dependent_count < 1);
1012 sd->s_dependent_count -= 1;
1015 * After this unlock, we cannot trust the item to stay alive!
1016 * DO NOT REFERENCE item after this unlock.
1018 mutex_unlock(&target->ci_dentry->d_inode->i_mutex);
1020 EXPORT_SYMBOL(configfs_undepend_item);
1022 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1024 int ret, module_got = 0;
1025 struct config_group *group;
1026 struct config_item *item;
1027 struct config_item *parent_item;
1028 struct configfs_subsystem *subsys;
1029 struct configfs_dirent *sd;
1030 struct config_item_type *type;
1031 struct module *owner = NULL;
1034 if (dentry->d_parent == configfs_sb->s_root) {
1039 sd = dentry->d_parent->d_fsdata;
1040 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1045 /* Get a working ref for the duration of this function */
1046 parent_item = configfs_get_config_item(dentry->d_parent);
1047 type = parent_item->ci_type;
1048 subsys = to_config_group(parent_item)->cg_subsys;
1051 if (!type || !type->ct_group_ops ||
1052 (!type->ct_group_ops->make_group &&
1053 !type->ct_group_ops->make_item)) {
1054 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1058 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1064 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1066 mutex_lock(&subsys->su_mutex);
1069 if (type->ct_group_ops->make_group) {
1070 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1072 link_group(to_config_group(parent_item), group);
1073 item = &group->cg_item;
1076 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1078 link_obj(parent_item, item);
1080 mutex_unlock(&subsys->su_mutex);
1085 * If item == NULL, then link_obj() was never called.
1086 * There are no extra references to clean up.
1093 * link_obj() has been called (via link_group() for groups).
1094 * From here on out, errors must clean that up.
1097 type = item->ci_type;
1103 owner = type->ct_owner;
1104 if (!try_module_get(owner)) {
1110 * I hate doing it this way, but if there is
1111 * an error, module_put() probably should
1112 * happen after any cleanup.
1117 ret = configfs_attach_group(parent_item, item, dentry);
1119 ret = configfs_attach_item(parent_item, item, dentry);
1123 /* Tear down everything we built up */
1124 mutex_lock(&subsys->su_mutex);
1126 client_disconnect_notify(parent_item, item);
1128 unlink_group(group);
1131 client_drop_item(parent_item, item);
1133 mutex_unlock(&subsys->su_mutex);
1141 * link_obj()/link_group() took a reference from child->parent,
1142 * so the parent is safely pinned. We can drop our working
1145 config_item_put(parent_item);
1151 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1153 struct config_item *parent_item;
1154 struct config_item *item;
1155 struct configfs_subsystem *subsys;
1156 struct configfs_dirent *sd;
1157 struct module *owner = NULL;
1160 if (dentry->d_parent == configfs_sb->s_root)
1163 sd = dentry->d_fsdata;
1164 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1168 * Here's where we check for dependents. We're protected by
1171 if (sd->s_dependent_count)
1174 /* Get a working ref until we have the child */
1175 parent_item = configfs_get_config_item(dentry->d_parent);
1176 subsys = to_config_group(parent_item)->cg_subsys;
1179 if (!parent_item->ci_type) {
1180 config_item_put(parent_item);
1184 spin_lock(&configfs_dirent_lock);
1185 ret = configfs_detach_prep(dentry);
1187 configfs_detach_rollback(dentry);
1188 spin_unlock(&configfs_dirent_lock);
1189 config_item_put(parent_item);
1192 spin_unlock(&configfs_dirent_lock);
1194 /* Get a working ref for the duration of this function */
1195 item = configfs_get_config_item(dentry);
1197 /* Drop reference from above, item already holds one. */
1198 config_item_put(parent_item);
1201 owner = item->ci_type->ct_owner;
1203 if (sd->s_type & CONFIGFS_USET_DIR) {
1204 configfs_detach_group(item);
1206 mutex_lock(&subsys->su_mutex);
1207 client_disconnect_notify(parent_item, item);
1208 unlink_group(to_config_group(item));
1210 configfs_detach_item(item);
1212 mutex_lock(&subsys->su_mutex);
1213 client_disconnect_notify(parent_item, item);
1217 client_drop_item(parent_item, item);
1218 mutex_unlock(&subsys->su_mutex);
1220 /* Drop our reference from above */
1221 config_item_put(item);
1228 const struct inode_operations configfs_dir_inode_operations = {
1229 .mkdir = configfs_mkdir,
1230 .rmdir = configfs_rmdir,
1231 .symlink = configfs_symlink,
1232 .unlink = configfs_unlink,
1233 .lookup = configfs_lookup,
1234 .setattr = configfs_setattr,
1238 int configfs_rename_dir(struct config_item * item, const char *new_name)
1241 struct dentry * new_dentry, * parent;
1243 if (!strcmp(config_item_name(item), new_name))
1249 down_write(&configfs_rename_sem);
1250 parent = item->parent->dentry;
1252 mutex_lock(&parent->d_inode->i_mutex);
1254 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1255 if (!IS_ERR(new_dentry)) {
1256 if (!new_dentry->d_inode) {
1257 error = config_item_set_name(item, "%s", new_name);
1259 d_add(new_dentry, NULL);
1260 d_move(item->dentry, new_dentry);
1263 d_delete(new_dentry);
1268 mutex_unlock(&parent->d_inode->i_mutex);
1269 up_write(&configfs_rename_sem);
1275 static int configfs_dir_open(struct inode *inode, struct file *file)
1277 struct dentry * dentry = file->f_path.dentry;
1278 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1280 mutex_lock(&dentry->d_inode->i_mutex);
1281 file->private_data = configfs_new_dirent(parent_sd, NULL);
1282 mutex_unlock(&dentry->d_inode->i_mutex);
1284 return IS_ERR(file->private_data) ? PTR_ERR(file->private_data) : 0;
1288 static int configfs_dir_close(struct inode *inode, struct file *file)
1290 struct dentry * dentry = file->f_path.dentry;
1291 struct configfs_dirent * cursor = file->private_data;
1293 mutex_lock(&dentry->d_inode->i_mutex);
1294 spin_lock(&configfs_dirent_lock);
1295 list_del_init(&cursor->s_sibling);
1296 spin_unlock(&configfs_dirent_lock);
1297 mutex_unlock(&dentry->d_inode->i_mutex);
1299 release_configfs_dirent(cursor);
1304 /* Relationship between s_mode and the DT_xxx types */
1305 static inline unsigned char dt_type(struct configfs_dirent *sd)
1307 return (sd->s_mode >> 12) & 15;
1310 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1312 struct dentry *dentry = filp->f_path.dentry;
1313 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1314 struct configfs_dirent *cursor = filp->private_data;
1315 struct list_head *p, *q = &cursor->s_sibling;
1317 int i = filp->f_pos;
1321 ino = dentry->d_inode->i_ino;
1322 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1328 ino = parent_ino(dentry);
1329 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1335 if (filp->f_pos == 2) {
1336 spin_lock(&configfs_dirent_lock);
1337 list_move(q, &parent_sd->s_children);
1338 spin_unlock(&configfs_dirent_lock);
1340 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1341 struct configfs_dirent *next;
1345 next = list_entry(p, struct configfs_dirent,
1347 if (!next->s_element)
1350 name = configfs_get_name(next);
1353 ino = next->s_dentry->d_inode->i_ino;
1355 ino = iunique(configfs_sb, 2);
1357 if (filldir(dirent, name, len, filp->f_pos, ino,
1361 spin_lock(&configfs_dirent_lock);
1363 spin_unlock(&configfs_dirent_lock);
1371 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1373 struct dentry * dentry = file->f_path.dentry;
1375 mutex_lock(&dentry->d_inode->i_mutex);
1378 offset += file->f_pos;
1383 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
1386 if (offset != file->f_pos) {
1387 file->f_pos = offset;
1388 if (file->f_pos >= 2) {
1389 struct configfs_dirent *sd = dentry->d_fsdata;
1390 struct configfs_dirent *cursor = file->private_data;
1391 struct list_head *p;
1392 loff_t n = file->f_pos - 2;
1394 spin_lock(&configfs_dirent_lock);
1395 list_del(&cursor->s_sibling);
1396 p = sd->s_children.next;
1397 while (n && p != &sd->s_children) {
1398 struct configfs_dirent *next;
1399 next = list_entry(p, struct configfs_dirent,
1401 if (next->s_element)
1405 list_add_tail(&cursor->s_sibling, p);
1406 spin_unlock(&configfs_dirent_lock);
1409 mutex_unlock(&dentry->d_inode->i_mutex);
1413 const struct file_operations configfs_dir_operations = {
1414 .open = configfs_dir_open,
1415 .release = configfs_dir_close,
1416 .llseek = configfs_dir_lseek,
1417 .read = generic_read_dir,
1418 .readdir = configfs_readdir,
1421 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1424 struct config_group *group = &subsys->su_group;
1426 struct dentry *dentry;
1427 struct configfs_dirent *sd;
1429 err = configfs_pin_fs();
1433 if (!group->cg_item.ci_name)
1434 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1436 sd = configfs_sb->s_root->d_fsdata;
1437 link_group(to_config_group(sd->s_element), group);
1439 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1442 name.name = group->cg_item.ci_name;
1443 name.len = strlen(name.name);
1444 name.hash = full_name_hash(name.name, name.len);
1447 dentry = d_alloc(configfs_sb->s_root, &name);
1449 d_add(dentry, NULL);
1451 err = configfs_attach_group(sd->s_element, &group->cg_item,
1459 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1462 unlink_group(group);
1463 configfs_release_fs();
1469 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1471 struct config_group *group = &subsys->su_group;
1472 struct dentry *dentry = group->cg_item.ci_dentry;
1474 if (dentry->d_parent != configfs_sb->s_root) {
1475 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1479 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1481 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1482 spin_lock(&configfs_dirent_lock);
1483 if (configfs_detach_prep(dentry)) {
1484 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1486 spin_unlock(&configfs_dirent_lock);
1487 configfs_detach_group(&group->cg_item);
1488 dentry->d_inode->i_flags |= S_DEAD;
1489 mutex_unlock(&dentry->d_inode->i_mutex);
1493 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1497 unlink_group(group);
1498 configfs_release_fs();
1501 EXPORT_SYMBOL(configfs_register_subsystem);
1502 EXPORT_SYMBOL(configfs_unregister_subsystem);