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 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock);
61 /* Coordinate with configfs_attach_attr where will increase
62 * sd->s_count and update sd->s_dentry to new allocated one.
63 * Only set sd->dentry to null when this dentry is the only
65 * If not do so, configfs_d_iput may run just after
66 * configfs_attach_attr and set sd->s_dentry to null
67 * even it's still in use.
69 if (atomic_read(&sd->s_count) <= 2)
72 spin_unlock(&configfs_dirent_lock);
79 * We _must_ delete our dentries on last dput, as the chain-to-parent
80 * behavior is required to clear the parents of default_groups.
82 static int configfs_d_delete(const struct dentry *dentry)
87 const struct dentry_operations configfs_dentry_ops = {
88 .d_iput = configfs_d_iput,
89 /* simple_delete_dentry() isn't exported */
90 .d_delete = configfs_d_delete,
96 * Helpers to make lockdep happy with our recursive locking of default groups'
97 * inodes (see configfs_attach_group() and configfs_detach_group()).
98 * We put default groups i_mutexes in separate classes according to their depth
99 * from the youngest non-default group ancestor.
101 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
102 * groups A/B and A/C will have their inode's mutex in class
103 * default_group_class[0], and default group A/C/D will be in
104 * default_group_class[1].
106 * The lock classes are declared and assigned in inode.c, according to the
108 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
109 * default groups, and reset to -1 when all default groups are attached. During
110 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
111 * inode's mutex is set to default_group_class[s_depth - 1].
114 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
119 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
120 struct configfs_dirent *sd)
122 int parent_depth = parent_sd->s_depth;
124 if (parent_depth >= 0)
125 sd->s_depth = parent_depth + 1;
129 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
132 * item's i_mutex class is already setup, so s_depth is now only
133 * used to set new sub-directories s_depth, which is always done
134 * with item's i_mutex locked.
137 * sd->s_depth == -1 iff we are a non default group.
138 * else (we are a default group) sd->s_depth > 0 (see
141 if (sd->s_depth == -1)
143 * We are a non default group and we are going to create
150 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
152 /* We will not create default groups anymore. */
156 #else /* CONFIG_LOCKDEP */
158 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
162 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
163 struct configfs_dirent *sd)
168 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
173 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
177 #endif /* CONFIG_LOCKDEP */
180 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
182 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
183 void *element, int type)
185 struct configfs_dirent * sd;
187 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
189 return ERR_PTR(-ENOMEM);
191 atomic_set(&sd->s_count, 1);
192 INIT_LIST_HEAD(&sd->s_links);
193 INIT_LIST_HEAD(&sd->s_children);
194 sd->s_element = element;
196 configfs_init_dirent_depth(sd);
197 spin_lock(&configfs_dirent_lock);
198 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
199 spin_unlock(&configfs_dirent_lock);
200 kmem_cache_free(configfs_dir_cachep, sd);
201 return ERR_PTR(-ENOENT);
203 list_add(&sd->s_sibling, &parent_sd->s_children);
204 spin_unlock(&configfs_dirent_lock);
211 * Return -EEXIST if there is already a configfs element with the same
212 * name for the same parent.
214 * called with parent inode's i_mutex held
216 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
217 const unsigned char *new)
219 struct configfs_dirent * sd;
221 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
223 const unsigned char *existing = configfs_get_name(sd);
224 if (strcmp(existing, new))
235 int configfs_make_dirent(struct configfs_dirent * parent_sd,
236 struct dentry * dentry, void * element,
237 umode_t mode, int type)
239 struct configfs_dirent * sd;
241 sd = configfs_new_dirent(parent_sd, element, type);
246 sd->s_dentry = dentry;
248 dentry->d_fsdata = configfs_get(sd);
253 static int init_dir(struct inode * inode)
255 inode->i_op = &configfs_dir_inode_operations;
256 inode->i_fop = &configfs_dir_operations;
258 /* directory inodes start off with i_nlink == 2 (for "." entry) */
263 static int configfs_init_file(struct inode * inode)
265 inode->i_size = PAGE_SIZE;
266 inode->i_fop = &configfs_file_operations;
270 static int init_symlink(struct inode * inode)
272 inode->i_op = &configfs_symlink_inode_operations;
276 static int create_dir(struct config_item *k, struct dentry *d)
279 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
280 struct dentry *p = d->d_parent;
284 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
286 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
287 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
289 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
290 error = configfs_create(d, mode, init_dir);
292 inc_nlink(p->d_inode);
294 struct configfs_dirent *sd = d->d_fsdata;
296 spin_lock(&configfs_dirent_lock);
297 list_del_init(&sd->s_sibling);
298 spin_unlock(&configfs_dirent_lock);
308 * configfs_create_dir - create a directory for an config_item.
309 * @item: config_itemwe're creating directory for.
310 * @dentry: config_item's dentry.
312 * Note: user-created entries won't be allowed under this new directory
313 * until it is validated by configfs_dir_set_ready()
316 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
318 int error = create_dir(item, dentry);
320 item->ci_dentry = dentry;
325 * Allow userspace to create new entries under a new directory created with
326 * configfs_create_dir(), and under all of its chidlren directories recursively.
327 * @sd configfs_dirent of the new directory to validate
329 * Caller must hold configfs_dirent_lock.
331 static void configfs_dir_set_ready(struct configfs_dirent *sd)
333 struct configfs_dirent *child_sd;
335 sd->s_type &= ~CONFIGFS_USET_CREATING;
336 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
337 if (child_sd->s_type & CONFIGFS_USET_CREATING)
338 configfs_dir_set_ready(child_sd);
342 * Check that a directory does not belong to a directory hierarchy being
343 * attached and not validated yet.
344 * @sd configfs_dirent of the directory to check
346 * @return non-zero iff the directory was validated
348 * Note: takes configfs_dirent_lock, so the result may change from false to true
349 * in two consecutive calls, but never from true to false.
351 int configfs_dirent_is_ready(struct configfs_dirent *sd)
355 spin_lock(&configfs_dirent_lock);
356 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
357 spin_unlock(&configfs_dirent_lock);
362 int configfs_create_link(struct configfs_symlink *sl,
363 struct dentry *parent,
364 struct dentry *dentry)
367 umode_t mode = S_IFLNK | S_IRWXUGO;
369 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
372 err = configfs_create(dentry, mode, init_symlink);
374 struct configfs_dirent *sd = dentry->d_fsdata;
376 spin_lock(&configfs_dirent_lock);
377 list_del_init(&sd->s_sibling);
378 spin_unlock(&configfs_dirent_lock);
386 static void remove_dir(struct dentry * d)
388 struct dentry * parent = dget(d->d_parent);
389 struct configfs_dirent * sd;
392 spin_lock(&configfs_dirent_lock);
393 list_del_init(&sd->s_sibling);
394 spin_unlock(&configfs_dirent_lock);
397 simple_rmdir(parent->d_inode,d);
399 pr_debug(" o %s removing done (%d)\n",d->d_name.name, d->d_count);
405 * configfs_remove_dir - remove an config_item's directory.
406 * @item: config_item we're removing.
408 * The only thing special about this is that we remove any files in
409 * the directory before we remove the directory, and we've inlined
410 * what used to be configfs_rmdir() below, instead of calling separately.
412 * Caller holds the mutex of the item's inode
415 static void configfs_remove_dir(struct config_item * item)
417 struct dentry * dentry = dget(item->ci_dentry);
424 * Drop reference from dget() on entrance.
430 /* attaches attribute's configfs_dirent to the dentry corresponding to the
433 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
435 struct configfs_attribute * attr = sd->s_element;
438 spin_lock(&configfs_dirent_lock);
439 dentry->d_fsdata = configfs_get(sd);
440 sd->s_dentry = dentry;
441 spin_unlock(&configfs_dirent_lock);
443 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
455 static struct dentry * configfs_lookup(struct inode *dir,
456 struct dentry *dentry,
459 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
460 struct configfs_dirent * sd;
465 * Fake invisibility if dir belongs to a group/default groups hierarchy
468 * This forbids userspace to read/write attributes of items which may
469 * not complete their initialization, since the dentries of the
470 * attributes won't be instantiated.
473 if (!configfs_dirent_is_ready(parent_sd))
476 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
477 if (sd->s_type & CONFIGFS_NOT_PINNED) {
478 const unsigned char * name = configfs_get_name(sd);
480 if (strcmp(name, dentry->d_name.name))
484 err = configfs_attach_attr(sd, dentry);
491 * If it doesn't exist and it isn't a NOT_PINNED item,
492 * it must be negative.
494 if (dentry->d_name.len > NAME_MAX)
495 return ERR_PTR(-ENAMETOOLONG);
505 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
506 * attributes and are removed by rmdir(). We recurse, setting
507 * CONFIGFS_USET_DROPPING on all children that are candidates for
509 * If there is an error, the caller will reset the flags via
510 * configfs_detach_rollback().
512 static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
514 struct configfs_dirent *parent_sd = dentry->d_fsdata;
515 struct configfs_dirent *sd;
518 /* Mark that we're trying to drop the group */
519 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
522 if (!list_empty(&parent_sd->s_links))
526 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
527 if (!sd->s_element ||
528 (sd->s_type & CONFIGFS_NOT_PINNED))
530 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
531 /* Abort if racing with mkdir() */
532 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
534 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
539 * Yup, recursive. If there's a problem, blame
540 * deep nesting of default_groups
542 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
556 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
559 static void configfs_detach_rollback(struct dentry *dentry)
561 struct configfs_dirent *parent_sd = dentry->d_fsdata;
562 struct configfs_dirent *sd;
564 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
566 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
567 if (sd->s_type & CONFIGFS_USET_DEFAULT)
568 configfs_detach_rollback(sd->s_dentry);
571 static void detach_attrs(struct config_item * item)
573 struct dentry * dentry = dget(item->ci_dentry);
574 struct configfs_dirent * parent_sd;
575 struct configfs_dirent * sd, * tmp;
580 pr_debug("configfs %s: dropping attrs for dir\n",
581 dentry->d_name.name);
583 parent_sd = dentry->d_fsdata;
584 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
585 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
587 spin_lock(&configfs_dirent_lock);
588 list_del_init(&sd->s_sibling);
589 spin_unlock(&configfs_dirent_lock);
590 configfs_drop_dentry(sd, dentry);
595 * Drop reference from dget() on entrance.
600 static int populate_attrs(struct config_item *item)
602 struct config_item_type *t = item->ci_type;
603 struct configfs_attribute *attr;
610 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
611 if ((error = configfs_create_file(item, attr)))
622 static int configfs_attach_group(struct config_item *parent_item,
623 struct config_item *item,
624 struct dentry *dentry);
625 static void configfs_detach_group(struct config_item *item);
627 static void detach_groups(struct config_group *group)
629 struct dentry * dentry = dget(group->cg_item.ci_dentry);
630 struct dentry *child;
631 struct configfs_dirent *parent_sd;
632 struct configfs_dirent *sd, *tmp;
637 parent_sd = dentry->d_fsdata;
638 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
639 if (!sd->s_element ||
640 !(sd->s_type & CONFIGFS_USET_DEFAULT))
643 child = sd->s_dentry;
645 mutex_lock(&child->d_inode->i_mutex);
647 configfs_detach_group(sd->s_element);
648 child->d_inode->i_flags |= S_DEAD;
651 mutex_unlock(&child->d_inode->i_mutex);
658 * Drop reference from dget() on entrance.
664 * This fakes mkdir(2) on a default_groups[] entry. It
665 * creates a dentry, attachs it, and then does fixup
668 * We could, perhaps, tweak our parent's ->mkdir for a minute and
669 * try using vfs_mkdir. Just a thought.
671 static int create_default_group(struct config_group *parent_group,
672 struct config_group *group)
676 struct configfs_dirent *sd;
677 /* We trust the caller holds a reference to parent */
678 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
680 if (!group->cg_item.ci_name)
681 group->cg_item.ci_name = group->cg_item.ci_namebuf;
682 name.name = group->cg_item.ci_name;
683 name.len = strlen(name.name);
684 name.hash = full_name_hash(name.name, name.len);
687 child = d_alloc(parent, &name);
691 ret = configfs_attach_group(&parent_group->cg_item,
692 &group->cg_item, child);
694 sd = child->d_fsdata;
695 sd->s_type |= CONFIGFS_USET_DEFAULT;
697 BUG_ON(child->d_inode);
706 static int populate_groups(struct config_group *group)
708 struct config_group *new_group;
712 if (group->default_groups) {
713 for (i = 0; group->default_groups[i]; i++) {
714 new_group = group->default_groups[i];
716 ret = create_default_group(group, new_group);
718 detach_groups(group);
728 * All of link_obj/unlink_obj/link_group/unlink_group require that
729 * subsys->su_mutex is held.
732 static void unlink_obj(struct config_item *item)
734 struct config_group *group;
736 group = item->ci_group;
738 list_del_init(&item->ci_entry);
740 item->ci_group = NULL;
741 item->ci_parent = NULL;
743 /* Drop the reference for ci_entry */
744 config_item_put(item);
746 /* Drop the reference for ci_parent */
747 config_group_put(group);
751 static void link_obj(struct config_item *parent_item, struct config_item *item)
754 * Parent seems redundant with group, but it makes certain
755 * traversals much nicer.
757 item->ci_parent = parent_item;
760 * We hold a reference on the parent for the child's ci_parent
763 item->ci_group = config_group_get(to_config_group(parent_item));
764 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
767 * We hold a reference on the child for ci_entry on the parent's
770 config_item_get(item);
773 static void unlink_group(struct config_group *group)
776 struct config_group *new_group;
778 if (group->default_groups) {
779 for (i = 0; group->default_groups[i]; i++) {
780 new_group = group->default_groups[i];
781 unlink_group(new_group);
785 group->cg_subsys = NULL;
786 unlink_obj(&group->cg_item);
789 static void link_group(struct config_group *parent_group, struct config_group *group)
792 struct config_group *new_group;
793 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
795 link_obj(&parent_group->cg_item, &group->cg_item);
797 if (parent_group->cg_subsys)
798 subsys = parent_group->cg_subsys;
799 else if (configfs_is_root(&parent_group->cg_item))
800 subsys = to_configfs_subsystem(group);
803 group->cg_subsys = subsys;
805 if (group->default_groups) {
806 for (i = 0; group->default_groups[i]; i++) {
807 new_group = group->default_groups[i];
808 link_group(group, new_group);
814 * The goal is that configfs_attach_item() (and
815 * configfs_attach_group()) can be called from either the VFS or this
816 * module. That is, they assume that the items have been created,
817 * the dentry allocated, and the dcache is all ready to go.
819 * If they fail, they must clean up after themselves as if they
820 * had never been called. The caller (VFS or local function) will
821 * handle cleaning up the dcache bits.
823 * configfs_detach_group() and configfs_detach_item() behave similarly on
824 * the way out. They assume that the proper semaphores are held, they
825 * clean up the configfs items, and they expect their callers will
826 * handle the dcache bits.
828 static int configfs_attach_item(struct config_item *parent_item,
829 struct config_item *item,
830 struct dentry *dentry)
834 ret = configfs_create_dir(item, dentry);
836 ret = populate_attrs(item);
839 * We are going to remove an inode and its dentry but
840 * the VFS may already have hit and used them. Thus,
841 * we must lock them as rmdir() would.
843 mutex_lock(&dentry->d_inode->i_mutex);
844 configfs_remove_dir(item);
845 dentry->d_inode->i_flags |= S_DEAD;
847 mutex_unlock(&dentry->d_inode->i_mutex);
855 /* Caller holds the mutex of the item's inode */
856 static void configfs_detach_item(struct config_item *item)
859 configfs_remove_dir(item);
862 static int configfs_attach_group(struct config_item *parent_item,
863 struct config_item *item,
864 struct dentry *dentry)
867 struct configfs_dirent *sd;
869 ret = configfs_attach_item(parent_item, item, dentry);
871 sd = dentry->d_fsdata;
872 sd->s_type |= CONFIGFS_USET_DIR;
875 * FYI, we're faking mkdir in populate_groups()
876 * We must lock the group's inode to avoid races with the VFS
877 * which can already hit the inode and try to add/remove entries
880 * We must also lock the inode to remove it safely in case of
881 * error, as rmdir() would.
883 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
884 configfs_adjust_dir_dirent_depth_before_populate(sd);
885 ret = populate_groups(to_config_group(item));
887 configfs_detach_item(item);
888 dentry->d_inode->i_flags |= S_DEAD;
891 configfs_adjust_dir_dirent_depth_after_populate(sd);
892 mutex_unlock(&dentry->d_inode->i_mutex);
900 /* Caller holds the mutex of the group's inode */
901 static void configfs_detach_group(struct config_item *item)
903 detach_groups(to_config_group(item));
904 configfs_detach_item(item);
908 * After the item has been detached from the filesystem view, we are
909 * ready to tear it out of the hierarchy. Notify the client before
910 * we do that so they can perform any cleanup that requires
911 * navigating the hierarchy. A client does not need to provide this
912 * callback. The subsystem semaphore MUST be held by the caller, and
913 * references must be valid for both items. It also assumes the
914 * caller has validated ci_type.
916 static void client_disconnect_notify(struct config_item *parent_item,
917 struct config_item *item)
919 struct config_item_type *type;
921 type = parent_item->ci_type;
924 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
925 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
930 * Drop the initial reference from make_item()/make_group()
931 * This function assumes that reference is held on item
932 * and that item holds a valid reference to the parent. Also, it
933 * assumes the caller has validated ci_type.
935 static void client_drop_item(struct config_item *parent_item,
936 struct config_item *item)
938 struct config_item_type *type;
940 type = parent_item->ci_type;
944 * If ->drop_item() exists, it is responsible for the
947 if (type->ct_group_ops && type->ct_group_ops->drop_item)
948 type->ct_group_ops->drop_item(to_config_group(parent_item),
951 config_item_put(item);
955 static void configfs_dump_one(struct configfs_dirent *sd, int level)
957 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
959 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
960 type_print(CONFIGFS_ROOT);
961 type_print(CONFIGFS_DIR);
962 type_print(CONFIGFS_ITEM_ATTR);
963 type_print(CONFIGFS_ITEM_LINK);
964 type_print(CONFIGFS_USET_DIR);
965 type_print(CONFIGFS_USET_DEFAULT);
966 type_print(CONFIGFS_USET_DROPPING);
970 static int configfs_dump(struct configfs_dirent *sd, int level)
972 struct configfs_dirent *child_sd;
975 configfs_dump_one(sd, level);
977 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
980 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
981 ret = configfs_dump(child_sd, level + 2);
992 * configfs_depend_item() and configfs_undepend_item()
994 * WARNING: Do not call these from a configfs callback!
996 * This describes these functions and their helpers.
998 * Allow another kernel system to depend on a config_item. If this
999 * happens, the item cannot go away until the dependent can live without
1000 * it. The idea is to give client modules as simple an interface as
1001 * possible. When a system asks them to depend on an item, they just
1002 * call configfs_depend_item(). If the item is live and the client
1003 * driver is in good shape, we'll happily do the work for them.
1005 * Why is the locking complex? Because configfs uses the VFS to handle
1006 * all locking, but this function is called outside the normal
1007 * VFS->configfs path. So it must take VFS locks to prevent the
1008 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1009 * why you can't call these functions underneath configfs callbacks.
1011 * Note, btw, that this can be called at *any* time, even when a configfs
1012 * subsystem isn't registered, or when configfs is loading or unloading.
1013 * Just like configfs_register_subsystem(). So we take the same
1014 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1015 * If we can find the target item in the
1016 * configfs tree, it must be part of the subsystem tree as well, so we
1017 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1018 * locking out mkdir() and rmdir(), who might be racing us.
1022 * configfs_depend_prep()
1024 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1025 * attributes. This is similar but not the same to configfs_detach_prep().
1026 * Note that configfs_detach_prep() expects the parent to be locked when it
1027 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1028 * do that so we can unlock it if we find nothing.
1030 * Here we do a depth-first search of the dentry hierarchy looking for
1032 * We deliberately ignore items tagged as dropping since they are virtually
1033 * dead, as well as items in the middle of attachment since they virtually
1034 * do not exist yet. This completes the locking out of racing mkdir() and
1036 * Note: subdirectories in the middle of attachment start with s_type =
1037 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1038 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1039 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1041 * If the target is not found, -ENOENT is bubbled up.
1043 * This adds a requirement that all config_items be unique!
1045 * This is recursive. There isn't
1046 * much on the stack, though, so folks that need this function - be careful
1047 * about your stack! Patches will be accepted to make it iterative.
1049 static int configfs_depend_prep(struct dentry *origin,
1050 struct config_item *target)
1052 struct configfs_dirent *child_sd, *sd;
1055 BUG_ON(!origin || !origin->d_fsdata);
1056 sd = origin->d_fsdata;
1058 if (sd->s_element == target) /* Boo-yah */
1061 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1062 if ((child_sd->s_type & CONFIGFS_DIR) &&
1063 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1064 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1065 ret = configfs_depend_prep(child_sd->s_dentry,
1068 goto out; /* Child path boo-yah */
1072 /* We looped all our children and didn't find target */
1079 int configfs_depend_item(struct configfs_subsystem *subsys,
1080 struct config_item *target)
1083 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1084 struct config_item *s_item = &subsys->su_group.cg_item;
1085 struct dentry *root;
1088 * Pin the configfs filesystem. This means we can safely access
1089 * the root of the configfs filesystem.
1091 root = configfs_pin_fs();
1093 return PTR_ERR(root);
1096 * Next, lock the root directory. We're going to check that the
1097 * subsystem is really registered, and so we need to lock out
1098 * configfs_[un]register_subsystem().
1100 mutex_lock(&root->d_inode->i_mutex);
1102 root_sd = root->d_fsdata;
1104 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1105 if (p->s_type & CONFIGFS_DIR) {
1106 if (p->s_element == s_item) {
1118 /* Ok, now we can trust subsys/s_item */
1120 spin_lock(&configfs_dirent_lock);
1121 /* Scan the tree, return 0 if found */
1122 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1124 goto out_unlock_dirent_lock;
1127 * We are sure that the item is not about to be removed by rmdir(), and
1128 * not in the middle of attachment by mkdir().
1130 p = target->ci_dentry->d_fsdata;
1131 p->s_dependent_count += 1;
1133 out_unlock_dirent_lock:
1134 spin_unlock(&configfs_dirent_lock);
1136 mutex_unlock(&root->d_inode->i_mutex);
1139 * If we succeeded, the fs is pinned via other methods. If not,
1140 * we're done with it anyway. So release_fs() is always right.
1142 configfs_release_fs();
1146 EXPORT_SYMBOL(configfs_depend_item);
1149 * Release the dependent linkage. This is much simpler than
1150 * configfs_depend_item() because we know that that the client driver is
1151 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1153 void configfs_undepend_item(struct configfs_subsystem *subsys,
1154 struct config_item *target)
1156 struct configfs_dirent *sd;
1159 * Since we can trust everything is pinned, we just need
1160 * configfs_dirent_lock.
1162 spin_lock(&configfs_dirent_lock);
1164 sd = target->ci_dentry->d_fsdata;
1165 BUG_ON(sd->s_dependent_count < 1);
1167 sd->s_dependent_count -= 1;
1170 * After this unlock, we cannot trust the item to stay alive!
1171 * DO NOT REFERENCE item after this unlock.
1173 spin_unlock(&configfs_dirent_lock);
1175 EXPORT_SYMBOL(configfs_undepend_item);
1177 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1181 struct config_group *group = NULL;
1182 struct config_item *item = NULL;
1183 struct config_item *parent_item;
1184 struct configfs_subsystem *subsys;
1185 struct configfs_dirent *sd;
1186 struct config_item_type *type;
1187 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1190 sd = dentry->d_parent->d_fsdata;
1193 * Fake invisibility if dir belongs to a group/default groups hierarchy
1196 if (!configfs_dirent_is_ready(sd)) {
1201 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1206 /* Get a working ref for the duration of this function */
1207 parent_item = configfs_get_config_item(dentry->d_parent);
1208 type = parent_item->ci_type;
1209 subsys = to_config_group(parent_item)->cg_subsys;
1212 if (!type || !type->ct_group_ops ||
1213 (!type->ct_group_ops->make_group &&
1214 !type->ct_group_ops->make_item)) {
1215 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1220 * The subsystem may belong to a different module than the item
1221 * being created. We don't want to safely pin the new item but
1222 * fail to pin the subsystem it sits under.
1224 if (!subsys->su_group.cg_item.ci_type) {
1228 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1229 if (!try_module_get(subsys_owner)) {
1234 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1237 goto out_subsys_put;
1240 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1242 mutex_lock(&subsys->su_mutex);
1243 if (type->ct_group_ops->make_group) {
1244 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1246 group = ERR_PTR(-ENOMEM);
1247 if (!IS_ERR(group)) {
1248 link_group(to_config_group(parent_item), group);
1249 item = &group->cg_item;
1251 ret = PTR_ERR(group);
1253 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1255 item = ERR_PTR(-ENOMEM);
1257 link_obj(parent_item, item);
1259 ret = PTR_ERR(item);
1261 mutex_unlock(&subsys->su_mutex);
1266 * If ret != 0, then link_obj() was never called.
1267 * There are no extra references to clean up.
1269 goto out_subsys_put;
1273 * link_obj() has been called (via link_group() for groups).
1274 * From here on out, errors must clean that up.
1277 type = item->ci_type;
1283 new_item_owner = type->ct_owner;
1284 if (!try_module_get(new_item_owner)) {
1290 * I hate doing it this way, but if there is
1291 * an error, module_put() probably should
1292 * happen after any cleanup.
1297 * Make racing rmdir() fail if it did not tag parent with
1298 * CONFIGFS_USET_DROPPING
1299 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1300 * fail and let rmdir() terminate correctly
1302 spin_lock(&configfs_dirent_lock);
1303 /* This will make configfs_detach_prep() fail */
1304 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1305 spin_unlock(&configfs_dirent_lock);
1308 ret = configfs_attach_group(parent_item, item, dentry);
1310 ret = configfs_attach_item(parent_item, item, dentry);
1312 spin_lock(&configfs_dirent_lock);
1313 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1315 configfs_dir_set_ready(dentry->d_fsdata);
1316 spin_unlock(&configfs_dirent_lock);
1320 /* Tear down everything we built up */
1321 mutex_lock(&subsys->su_mutex);
1323 client_disconnect_notify(parent_item, item);
1325 unlink_group(group);
1328 client_drop_item(parent_item, item);
1330 mutex_unlock(&subsys->su_mutex);
1333 module_put(new_item_owner);
1338 module_put(subsys_owner);
1342 * link_obj()/link_group() took a reference from child->parent,
1343 * so the parent is safely pinned. We can drop our working
1346 config_item_put(parent_item);
1352 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1354 struct config_item *parent_item;
1355 struct config_item *item;
1356 struct configfs_subsystem *subsys;
1357 struct configfs_dirent *sd;
1358 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1361 sd = dentry->d_fsdata;
1362 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1365 /* Get a working ref until we have the child */
1366 parent_item = configfs_get_config_item(dentry->d_parent);
1367 subsys = to_config_group(parent_item)->cg_subsys;
1370 if (!parent_item->ci_type) {
1371 config_item_put(parent_item);
1375 /* configfs_mkdir() shouldn't have allowed this */
1376 BUG_ON(!subsys->su_group.cg_item.ci_type);
1377 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1380 * Ensure that no racing symlink() will make detach_prep() fail while
1381 * the new link is temporarily attached
1384 struct mutex *wait_mutex;
1386 mutex_lock(&configfs_symlink_mutex);
1387 spin_lock(&configfs_dirent_lock);
1389 * Here's where we check for dependents. We're protected by
1390 * configfs_dirent_lock.
1391 * If no dependent, atomically tag the item as dropping.
1393 ret = sd->s_dependent_count ? -EBUSY : 0;
1395 ret = configfs_detach_prep(dentry, &wait_mutex);
1397 configfs_detach_rollback(dentry);
1399 spin_unlock(&configfs_dirent_lock);
1400 mutex_unlock(&configfs_symlink_mutex);
1403 if (ret != -EAGAIN) {
1404 config_item_put(parent_item);
1408 /* Wait until the racing operation terminates */
1409 mutex_lock(wait_mutex);
1410 mutex_unlock(wait_mutex);
1412 } while (ret == -EAGAIN);
1414 /* Get a working ref for the duration of this function */
1415 item = configfs_get_config_item(dentry);
1417 /* Drop reference from above, item already holds one. */
1418 config_item_put(parent_item);
1421 dead_item_owner = item->ci_type->ct_owner;
1423 if (sd->s_type & CONFIGFS_USET_DIR) {
1424 configfs_detach_group(item);
1426 mutex_lock(&subsys->su_mutex);
1427 client_disconnect_notify(parent_item, item);
1428 unlink_group(to_config_group(item));
1430 configfs_detach_item(item);
1432 mutex_lock(&subsys->su_mutex);
1433 client_disconnect_notify(parent_item, item);
1437 client_drop_item(parent_item, item);
1438 mutex_unlock(&subsys->su_mutex);
1440 /* Drop our reference from above */
1441 config_item_put(item);
1443 module_put(dead_item_owner);
1444 module_put(subsys_owner);
1449 const struct inode_operations configfs_dir_inode_operations = {
1450 .mkdir = configfs_mkdir,
1451 .rmdir = configfs_rmdir,
1452 .symlink = configfs_symlink,
1453 .unlink = configfs_unlink,
1454 .lookup = configfs_lookup,
1455 .setattr = configfs_setattr,
1458 const struct inode_operations configfs_root_inode_operations = {
1459 .lookup = configfs_lookup,
1460 .setattr = configfs_setattr,
1464 int configfs_rename_dir(struct config_item * item, const char *new_name)
1467 struct dentry * new_dentry, * parent;
1469 if (!strcmp(config_item_name(item), new_name))
1475 down_write(&configfs_rename_sem);
1476 parent = item->parent->dentry;
1478 mutex_lock(&parent->d_inode->i_mutex);
1480 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1481 if (!IS_ERR(new_dentry)) {
1482 if (!new_dentry->d_inode) {
1483 error = config_item_set_name(item, "%s", new_name);
1485 d_add(new_dentry, NULL);
1486 d_move(item->dentry, new_dentry);
1489 d_delete(new_dentry);
1494 mutex_unlock(&parent->d_inode->i_mutex);
1495 up_write(&configfs_rename_sem);
1501 static int configfs_dir_open(struct inode *inode, struct file *file)
1503 struct dentry * dentry = file->f_path.dentry;
1504 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1507 mutex_lock(&dentry->d_inode->i_mutex);
1509 * Fake invisibility if dir belongs to a group/default groups hierarchy
1513 if (configfs_dirent_is_ready(parent_sd)) {
1514 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1515 if (IS_ERR(file->private_data))
1516 err = PTR_ERR(file->private_data);
1520 mutex_unlock(&dentry->d_inode->i_mutex);
1525 static int configfs_dir_close(struct inode *inode, struct file *file)
1527 struct dentry * dentry = file->f_path.dentry;
1528 struct configfs_dirent * cursor = file->private_data;
1530 mutex_lock(&dentry->d_inode->i_mutex);
1531 spin_lock(&configfs_dirent_lock);
1532 list_del_init(&cursor->s_sibling);
1533 spin_unlock(&configfs_dirent_lock);
1534 mutex_unlock(&dentry->d_inode->i_mutex);
1536 release_configfs_dirent(cursor);
1541 /* Relationship between s_mode and the DT_xxx types */
1542 static inline unsigned char dt_type(struct configfs_dirent *sd)
1544 return (sd->s_mode >> 12) & 15;
1547 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1549 struct dentry *dentry = filp->f_path.dentry;
1550 struct super_block *sb = dentry->d_sb;
1551 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1552 struct configfs_dirent *cursor = filp->private_data;
1553 struct list_head *p, *q = &cursor->s_sibling;
1555 int i = filp->f_pos;
1559 ino = dentry->d_inode->i_ino;
1560 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1566 ino = parent_ino(dentry);
1567 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1573 if (filp->f_pos == 2) {
1574 spin_lock(&configfs_dirent_lock);
1575 list_move(q, &parent_sd->s_children);
1576 spin_unlock(&configfs_dirent_lock);
1578 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1579 struct configfs_dirent *next;
1582 struct inode *inode = NULL;
1584 next = list_entry(p, struct configfs_dirent,
1586 if (!next->s_element)
1589 name = configfs_get_name(next);
1593 * We'll have a dentry and an inode for
1594 * PINNED items and for open attribute
1595 * files. We lock here to prevent a race
1596 * with configfs_d_iput() clearing
1597 * s_dentry before calling iput().
1599 * Why do we go to the trouble? If
1600 * someone has an attribute file open,
1601 * the inode number should match until
1602 * they close it. Beyond that, we don't
1605 spin_lock(&configfs_dirent_lock);
1606 dentry = next->s_dentry;
1608 inode = dentry->d_inode;
1611 spin_unlock(&configfs_dirent_lock);
1613 ino = iunique(sb, 2);
1615 if (filldir(dirent, name, len, filp->f_pos, ino,
1619 spin_lock(&configfs_dirent_lock);
1621 spin_unlock(&configfs_dirent_lock);
1629 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1631 struct dentry * dentry = file->f_path.dentry;
1633 mutex_lock(&dentry->d_inode->i_mutex);
1636 offset += file->f_pos;
1641 mutex_unlock(&file_inode(file)->i_mutex);
1644 if (offset != file->f_pos) {
1645 file->f_pos = offset;
1646 if (file->f_pos >= 2) {
1647 struct configfs_dirent *sd = dentry->d_fsdata;
1648 struct configfs_dirent *cursor = file->private_data;
1649 struct list_head *p;
1650 loff_t n = file->f_pos - 2;
1652 spin_lock(&configfs_dirent_lock);
1653 list_del(&cursor->s_sibling);
1654 p = sd->s_children.next;
1655 while (n && p != &sd->s_children) {
1656 struct configfs_dirent *next;
1657 next = list_entry(p, struct configfs_dirent,
1659 if (next->s_element)
1663 list_add_tail(&cursor->s_sibling, p);
1664 spin_unlock(&configfs_dirent_lock);
1667 mutex_unlock(&dentry->d_inode->i_mutex);
1671 const struct file_operations configfs_dir_operations = {
1672 .open = configfs_dir_open,
1673 .release = configfs_dir_close,
1674 .llseek = configfs_dir_lseek,
1675 .read = generic_read_dir,
1676 .readdir = configfs_readdir,
1679 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1682 struct config_group *group = &subsys->su_group;
1684 struct dentry *dentry;
1685 struct dentry *root;
1686 struct configfs_dirent *sd;
1688 root = configfs_pin_fs();
1690 return PTR_ERR(root);
1692 if (!group->cg_item.ci_name)
1693 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1695 sd = root->d_fsdata;
1696 link_group(to_config_group(sd->s_element), group);
1698 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
1700 name.name = group->cg_item.ci_name;
1701 name.len = strlen(name.name);
1702 name.hash = full_name_hash(name.name, name.len);
1705 dentry = d_alloc(root, &name);
1707 d_add(dentry, NULL);
1709 err = configfs_attach_group(sd->s_element, &group->cg_item,
1712 BUG_ON(dentry->d_inode);
1716 spin_lock(&configfs_dirent_lock);
1717 configfs_dir_set_ready(dentry->d_fsdata);
1718 spin_unlock(&configfs_dirent_lock);
1722 mutex_unlock(&root->d_inode->i_mutex);
1725 unlink_group(group);
1726 configfs_release_fs();
1732 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1734 struct config_group *group = &subsys->su_group;
1735 struct dentry *dentry = group->cg_item.ci_dentry;
1736 struct dentry *root = dentry->d_sb->s_root;
1738 if (dentry->d_parent != root) {
1739 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1743 mutex_lock_nested(&root->d_inode->i_mutex,
1745 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1746 mutex_lock(&configfs_symlink_mutex);
1747 spin_lock(&configfs_dirent_lock);
1748 if (configfs_detach_prep(dentry, NULL)) {
1749 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1751 spin_unlock(&configfs_dirent_lock);
1752 mutex_unlock(&configfs_symlink_mutex);
1753 configfs_detach_group(&group->cg_item);
1754 dentry->d_inode->i_flags |= S_DEAD;
1756 mutex_unlock(&dentry->d_inode->i_mutex);
1760 mutex_unlock(&root->d_inode->i_mutex);
1764 unlink_group(group);
1765 configfs_release_fs();
1768 EXPORT_SYMBOL(configfs_register_subsystem);
1769 EXPORT_SYMBOL(configfs_unregister_subsystem);