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);
78 const struct dentry_operations configfs_dentry_ops = {
79 .d_iput = configfs_d_iput,
80 .d_delete = always_delete_dentry,
86 * Helpers to make lockdep happy with our recursive locking of default groups'
87 * inodes (see configfs_attach_group() and configfs_detach_group()).
88 * We put default groups i_mutexes in separate classes according to their depth
89 * from the youngest non-default group ancestor.
91 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
92 * groups A/B and A/C will have their inode's mutex in class
93 * default_group_class[0], and default group A/C/D will be in
94 * default_group_class[1].
96 * The lock classes are declared and assigned in inode.c, according to the
98 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
99 * default groups, and reset to -1 when all default groups are attached. During
100 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
101 * inode's mutex is set to default_group_class[s_depth - 1].
104 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
109 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
110 struct configfs_dirent *sd)
112 int parent_depth = parent_sd->s_depth;
114 if (parent_depth >= 0)
115 sd->s_depth = parent_depth + 1;
119 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
122 * item's i_mutex class is already setup, so s_depth is now only
123 * used to set new sub-directories s_depth, which is always done
124 * with item's i_mutex locked.
127 * sd->s_depth == -1 iff we are a non default group.
128 * else (we are a default group) sd->s_depth > 0 (see
131 if (sd->s_depth == -1)
133 * We are a non default group and we are going to create
140 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
142 /* We will not create default groups anymore. */
146 #else /* CONFIG_LOCKDEP */
148 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
152 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
153 struct configfs_dirent *sd)
158 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
163 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
167 #endif /* CONFIG_LOCKDEP */
170 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
172 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
173 void *element, int type)
175 struct configfs_dirent * sd;
177 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
179 return ERR_PTR(-ENOMEM);
181 atomic_set(&sd->s_count, 1);
182 INIT_LIST_HEAD(&sd->s_links);
183 INIT_LIST_HEAD(&sd->s_children);
184 sd->s_element = element;
186 configfs_init_dirent_depth(sd);
187 spin_lock(&configfs_dirent_lock);
188 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
189 spin_unlock(&configfs_dirent_lock);
190 kmem_cache_free(configfs_dir_cachep, sd);
191 return ERR_PTR(-ENOENT);
193 list_add(&sd->s_sibling, &parent_sd->s_children);
194 spin_unlock(&configfs_dirent_lock);
201 * Return -EEXIST if there is already a configfs element with the same
202 * name for the same parent.
204 * called with parent inode's i_mutex held
206 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
207 const unsigned char *new)
209 struct configfs_dirent * sd;
211 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
213 const unsigned char *existing = configfs_get_name(sd);
214 if (strcmp(existing, new))
225 int configfs_make_dirent(struct configfs_dirent * parent_sd,
226 struct dentry * dentry, void * element,
227 umode_t mode, int type)
229 struct configfs_dirent * sd;
231 sd = configfs_new_dirent(parent_sd, element, type);
236 sd->s_dentry = dentry;
238 dentry->d_fsdata = configfs_get(sd);
243 static void init_dir(struct inode * inode)
245 inode->i_op = &configfs_dir_inode_operations;
246 inode->i_fop = &configfs_dir_operations;
248 /* directory inodes start off with i_nlink == 2 (for "." entry) */
252 static void configfs_init_file(struct inode * inode)
254 inode->i_size = PAGE_SIZE;
255 inode->i_fop = &configfs_file_operations;
258 static void init_symlink(struct inode * inode)
260 inode->i_op = &configfs_symlink_inode_operations;
263 static int create_dir(struct config_item *k, struct dentry *d)
266 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
267 struct dentry *p = d->d_parent;
271 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
273 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
274 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
276 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
277 error = configfs_create(d, mode, init_dir);
279 inc_nlink(p->d_inode);
281 struct configfs_dirent *sd = d->d_fsdata;
283 spin_lock(&configfs_dirent_lock);
284 list_del_init(&sd->s_sibling);
285 spin_unlock(&configfs_dirent_lock);
295 * configfs_create_dir - create a directory for an config_item.
296 * @item: config_itemwe're creating directory for.
297 * @dentry: config_item's dentry.
299 * Note: user-created entries won't be allowed under this new directory
300 * until it is validated by configfs_dir_set_ready()
303 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
305 int error = create_dir(item, dentry);
307 item->ci_dentry = dentry;
312 * Allow userspace to create new entries under a new directory created with
313 * configfs_create_dir(), and under all of its chidlren directories recursively.
314 * @sd configfs_dirent of the new directory to validate
316 * Caller must hold configfs_dirent_lock.
318 static void configfs_dir_set_ready(struct configfs_dirent *sd)
320 struct configfs_dirent *child_sd;
322 sd->s_type &= ~CONFIGFS_USET_CREATING;
323 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
324 if (child_sd->s_type & CONFIGFS_USET_CREATING)
325 configfs_dir_set_ready(child_sd);
329 * Check that a directory does not belong to a directory hierarchy being
330 * attached and not validated yet.
331 * @sd configfs_dirent of the directory to check
333 * @return non-zero iff the directory was validated
335 * Note: takes configfs_dirent_lock, so the result may change from false to true
336 * in two consecutive calls, but never from true to false.
338 int configfs_dirent_is_ready(struct configfs_dirent *sd)
342 spin_lock(&configfs_dirent_lock);
343 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
344 spin_unlock(&configfs_dirent_lock);
349 int configfs_create_link(struct configfs_symlink *sl,
350 struct dentry *parent,
351 struct dentry *dentry)
354 umode_t mode = S_IFLNK | S_IRWXUGO;
356 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
359 err = configfs_create(dentry, mode, init_symlink);
361 struct configfs_dirent *sd = dentry->d_fsdata;
363 spin_lock(&configfs_dirent_lock);
364 list_del_init(&sd->s_sibling);
365 spin_unlock(&configfs_dirent_lock);
373 static void remove_dir(struct dentry * d)
375 struct dentry * parent = dget(d->d_parent);
376 struct configfs_dirent * sd;
379 spin_lock(&configfs_dirent_lock);
380 list_del_init(&sd->s_sibling);
381 spin_unlock(&configfs_dirent_lock);
384 simple_rmdir(parent->d_inode,d);
386 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
392 * configfs_remove_dir - remove an config_item's directory.
393 * @item: config_item we're removing.
395 * The only thing special about this is that we remove any files in
396 * the directory before we remove the directory, and we've inlined
397 * what used to be configfs_rmdir() below, instead of calling separately.
399 * Caller holds the mutex of the item's inode
402 static void configfs_remove_dir(struct config_item * item)
404 struct dentry * dentry = dget(item->ci_dentry);
411 * Drop reference from dget() on entrance.
417 /* attaches attribute's configfs_dirent to the dentry corresponding to the
420 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
422 struct configfs_attribute * attr = sd->s_element;
425 spin_lock(&configfs_dirent_lock);
426 dentry->d_fsdata = configfs_get(sd);
427 sd->s_dentry = dentry;
428 spin_unlock(&configfs_dirent_lock);
430 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
442 static struct dentry * configfs_lookup(struct inode *dir,
443 struct dentry *dentry,
446 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
447 struct configfs_dirent * sd;
452 * Fake invisibility if dir belongs to a group/default groups hierarchy
455 * This forbids userspace to read/write attributes of items which may
456 * not complete their initialization, since the dentries of the
457 * attributes won't be instantiated.
460 if (!configfs_dirent_is_ready(parent_sd))
463 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
464 if (sd->s_type & CONFIGFS_NOT_PINNED) {
465 const unsigned char * name = configfs_get_name(sd);
467 if (strcmp(name, dentry->d_name.name))
471 err = configfs_attach_attr(sd, dentry);
478 * If it doesn't exist and it isn't a NOT_PINNED item,
479 * it must be negative.
481 if (dentry->d_name.len > NAME_MAX)
482 return ERR_PTR(-ENAMETOOLONG);
492 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
493 * attributes and are removed by rmdir(). We recurse, setting
494 * CONFIGFS_USET_DROPPING on all children that are candidates for
496 * If there is an error, the caller will reset the flags via
497 * configfs_detach_rollback().
499 static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
501 struct configfs_dirent *parent_sd = dentry->d_fsdata;
502 struct configfs_dirent *sd;
505 /* Mark that we're trying to drop the group */
506 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
509 if (!list_empty(&parent_sd->s_links))
513 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
514 if (!sd->s_element ||
515 (sd->s_type & CONFIGFS_NOT_PINNED))
517 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
518 /* Abort if racing with mkdir() */
519 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
521 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
526 * Yup, recursive. If there's a problem, blame
527 * deep nesting of default_groups
529 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
543 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
546 static void configfs_detach_rollback(struct dentry *dentry)
548 struct configfs_dirent *parent_sd = dentry->d_fsdata;
549 struct configfs_dirent *sd;
551 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
553 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
554 if (sd->s_type & CONFIGFS_USET_DEFAULT)
555 configfs_detach_rollback(sd->s_dentry);
558 static void detach_attrs(struct config_item * item)
560 struct dentry * dentry = dget(item->ci_dentry);
561 struct configfs_dirent * parent_sd;
562 struct configfs_dirent * sd, * tmp;
567 pr_debug("configfs %s: dropping attrs for dir\n",
568 dentry->d_name.name);
570 parent_sd = dentry->d_fsdata;
571 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
572 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
574 spin_lock(&configfs_dirent_lock);
575 list_del_init(&sd->s_sibling);
576 spin_unlock(&configfs_dirent_lock);
577 configfs_drop_dentry(sd, dentry);
582 * Drop reference from dget() on entrance.
587 static int populate_attrs(struct config_item *item)
589 struct config_item_type *t = item->ci_type;
590 struct configfs_attribute *attr;
597 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
598 if ((error = configfs_create_file(item, attr)))
609 static int configfs_attach_group(struct config_item *parent_item,
610 struct config_item *item,
611 struct dentry *dentry);
612 static void configfs_detach_group(struct config_item *item);
614 static void detach_groups(struct config_group *group)
616 struct dentry * dentry = dget(group->cg_item.ci_dentry);
617 struct dentry *child;
618 struct configfs_dirent *parent_sd;
619 struct configfs_dirent *sd, *tmp;
624 parent_sd = dentry->d_fsdata;
625 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
626 if (!sd->s_element ||
627 !(sd->s_type & CONFIGFS_USET_DEFAULT))
630 child = sd->s_dentry;
632 mutex_lock(&child->d_inode->i_mutex);
634 configfs_detach_group(sd->s_element);
635 child->d_inode->i_flags |= S_DEAD;
638 mutex_unlock(&child->d_inode->i_mutex);
645 * Drop reference from dget() on entrance.
651 * This fakes mkdir(2) on a default_groups[] entry. It
652 * creates a dentry, attachs it, and then does fixup
655 * We could, perhaps, tweak our parent's ->mkdir for a minute and
656 * try using vfs_mkdir. Just a thought.
658 static int create_default_group(struct config_group *parent_group,
659 struct config_group *group)
662 struct configfs_dirent *sd;
663 /* We trust the caller holds a reference to parent */
664 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
666 if (!group->cg_item.ci_name)
667 group->cg_item.ci_name = group->cg_item.ci_namebuf;
670 child = d_alloc_name(parent, group->cg_item.ci_name);
674 ret = configfs_attach_group(&parent_group->cg_item,
675 &group->cg_item, child);
677 sd = child->d_fsdata;
678 sd->s_type |= CONFIGFS_USET_DEFAULT;
680 BUG_ON(child->d_inode);
689 static int populate_groups(struct config_group *group)
691 struct config_group *new_group;
695 if (group->default_groups) {
696 for (i = 0; group->default_groups[i]; i++) {
697 new_group = group->default_groups[i];
699 ret = create_default_group(group, new_group);
701 detach_groups(group);
711 * All of link_obj/unlink_obj/link_group/unlink_group require that
712 * subsys->su_mutex is held.
715 static void unlink_obj(struct config_item *item)
717 struct config_group *group;
719 group = item->ci_group;
721 list_del_init(&item->ci_entry);
723 item->ci_group = NULL;
724 item->ci_parent = NULL;
726 /* Drop the reference for ci_entry */
727 config_item_put(item);
729 /* Drop the reference for ci_parent */
730 config_group_put(group);
734 static void link_obj(struct config_item *parent_item, struct config_item *item)
737 * Parent seems redundant with group, but it makes certain
738 * traversals much nicer.
740 item->ci_parent = parent_item;
743 * We hold a reference on the parent for the child's ci_parent
746 item->ci_group = config_group_get(to_config_group(parent_item));
747 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
750 * We hold a reference on the child for ci_entry on the parent's
753 config_item_get(item);
756 static void unlink_group(struct config_group *group)
759 struct config_group *new_group;
761 if (group->default_groups) {
762 for (i = 0; group->default_groups[i]; i++) {
763 new_group = group->default_groups[i];
764 unlink_group(new_group);
768 group->cg_subsys = NULL;
769 unlink_obj(&group->cg_item);
772 static void link_group(struct config_group *parent_group, struct config_group *group)
775 struct config_group *new_group;
776 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
778 link_obj(&parent_group->cg_item, &group->cg_item);
780 if (parent_group->cg_subsys)
781 subsys = parent_group->cg_subsys;
782 else if (configfs_is_root(&parent_group->cg_item))
783 subsys = to_configfs_subsystem(group);
786 group->cg_subsys = subsys;
788 if (group->default_groups) {
789 for (i = 0; group->default_groups[i]; i++) {
790 new_group = group->default_groups[i];
791 link_group(group, new_group);
797 * The goal is that configfs_attach_item() (and
798 * configfs_attach_group()) can be called from either the VFS or this
799 * module. That is, they assume that the items have been created,
800 * the dentry allocated, and the dcache is all ready to go.
802 * If they fail, they must clean up after themselves as if they
803 * had never been called. The caller (VFS or local function) will
804 * handle cleaning up the dcache bits.
806 * configfs_detach_group() and configfs_detach_item() behave similarly on
807 * the way out. They assume that the proper semaphores are held, they
808 * clean up the configfs items, and they expect their callers will
809 * handle the dcache bits.
811 static int configfs_attach_item(struct config_item *parent_item,
812 struct config_item *item,
813 struct dentry *dentry)
817 ret = configfs_create_dir(item, dentry);
819 ret = populate_attrs(item);
822 * We are going to remove an inode and its dentry but
823 * the VFS may already have hit and used them. Thus,
824 * we must lock them as rmdir() would.
826 mutex_lock(&dentry->d_inode->i_mutex);
827 configfs_remove_dir(item);
828 dentry->d_inode->i_flags |= S_DEAD;
830 mutex_unlock(&dentry->d_inode->i_mutex);
838 /* Caller holds the mutex of the item's inode */
839 static void configfs_detach_item(struct config_item *item)
842 configfs_remove_dir(item);
845 static int configfs_attach_group(struct config_item *parent_item,
846 struct config_item *item,
847 struct dentry *dentry)
850 struct configfs_dirent *sd;
852 ret = configfs_attach_item(parent_item, item, dentry);
854 sd = dentry->d_fsdata;
855 sd->s_type |= CONFIGFS_USET_DIR;
858 * FYI, we're faking mkdir in populate_groups()
859 * We must lock the group's inode to avoid races with the VFS
860 * which can already hit the inode and try to add/remove entries
863 * We must also lock the inode to remove it safely in case of
864 * error, as rmdir() would.
866 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
867 configfs_adjust_dir_dirent_depth_before_populate(sd);
868 ret = populate_groups(to_config_group(item));
870 configfs_detach_item(item);
871 dentry->d_inode->i_flags |= S_DEAD;
874 configfs_adjust_dir_dirent_depth_after_populate(sd);
875 mutex_unlock(&dentry->d_inode->i_mutex);
883 /* Caller holds the mutex of the group's inode */
884 static void configfs_detach_group(struct config_item *item)
886 detach_groups(to_config_group(item));
887 configfs_detach_item(item);
891 * After the item has been detached from the filesystem view, we are
892 * ready to tear it out of the hierarchy. Notify the client before
893 * we do that so they can perform any cleanup that requires
894 * navigating the hierarchy. A client does not need to provide this
895 * callback. The subsystem semaphore MUST be held by the caller, and
896 * references must be valid for both items. It also assumes the
897 * caller has validated ci_type.
899 static void client_disconnect_notify(struct config_item *parent_item,
900 struct config_item *item)
902 struct config_item_type *type;
904 type = parent_item->ci_type;
907 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
908 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
913 * Drop the initial reference from make_item()/make_group()
914 * This function assumes that reference is held on item
915 * and that item holds a valid reference to the parent. Also, it
916 * assumes the caller has validated ci_type.
918 static void client_drop_item(struct config_item *parent_item,
919 struct config_item *item)
921 struct config_item_type *type;
923 type = parent_item->ci_type;
927 * If ->drop_item() exists, it is responsible for the
930 if (type->ct_group_ops && type->ct_group_ops->drop_item)
931 type->ct_group_ops->drop_item(to_config_group(parent_item),
934 config_item_put(item);
938 static void configfs_dump_one(struct configfs_dirent *sd, int level)
940 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
942 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
943 type_print(CONFIGFS_ROOT);
944 type_print(CONFIGFS_DIR);
945 type_print(CONFIGFS_ITEM_ATTR);
946 type_print(CONFIGFS_ITEM_LINK);
947 type_print(CONFIGFS_USET_DIR);
948 type_print(CONFIGFS_USET_DEFAULT);
949 type_print(CONFIGFS_USET_DROPPING);
953 static int configfs_dump(struct configfs_dirent *sd, int level)
955 struct configfs_dirent *child_sd;
958 configfs_dump_one(sd, level);
960 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
963 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
964 ret = configfs_dump(child_sd, level + 2);
975 * configfs_depend_item() and configfs_undepend_item()
977 * WARNING: Do not call these from a configfs callback!
979 * This describes these functions and their helpers.
981 * Allow another kernel system to depend on a config_item. If this
982 * happens, the item cannot go away until the dependent can live without
983 * it. The idea is to give client modules as simple an interface as
984 * possible. When a system asks them to depend on an item, they just
985 * call configfs_depend_item(). If the item is live and the client
986 * driver is in good shape, we'll happily do the work for them.
988 * Why is the locking complex? Because configfs uses the VFS to handle
989 * all locking, but this function is called outside the normal
990 * VFS->configfs path. So it must take VFS locks to prevent the
991 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
992 * why you can't call these functions underneath configfs callbacks.
994 * Note, btw, that this can be called at *any* time, even when a configfs
995 * subsystem isn't registered, or when configfs is loading or unloading.
996 * Just like configfs_register_subsystem(). So we take the same
997 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
998 * If we can find the target item in the
999 * configfs tree, it must be part of the subsystem tree as well, so we
1000 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1001 * locking out mkdir() and rmdir(), who might be racing us.
1005 * configfs_depend_prep()
1007 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1008 * attributes. This is similar but not the same to configfs_detach_prep().
1009 * Note that configfs_detach_prep() expects the parent to be locked when it
1010 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1011 * do that so we can unlock it if we find nothing.
1013 * Here we do a depth-first search of the dentry hierarchy looking for
1015 * We deliberately ignore items tagged as dropping since they are virtually
1016 * dead, as well as items in the middle of attachment since they virtually
1017 * do not exist yet. This completes the locking out of racing mkdir() and
1019 * Note: subdirectories in the middle of attachment start with s_type =
1020 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1021 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1022 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1024 * If the target is not found, -ENOENT is bubbled up.
1026 * This adds a requirement that all config_items be unique!
1028 * This is recursive. There isn't
1029 * much on the stack, though, so folks that need this function - be careful
1030 * about your stack! Patches will be accepted to make it iterative.
1032 static int configfs_depend_prep(struct dentry *origin,
1033 struct config_item *target)
1035 struct configfs_dirent *child_sd, *sd;
1038 BUG_ON(!origin || !origin->d_fsdata);
1039 sd = origin->d_fsdata;
1041 if (sd->s_element == target) /* Boo-yah */
1044 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1045 if ((child_sd->s_type & CONFIGFS_DIR) &&
1046 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1047 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1048 ret = configfs_depend_prep(child_sd->s_dentry,
1051 goto out; /* Child path boo-yah */
1055 /* We looped all our children and didn't find target */
1062 int configfs_depend_item(struct configfs_subsystem *subsys,
1063 struct config_item *target)
1066 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1067 struct config_item *s_item = &subsys->su_group.cg_item;
1068 struct dentry *root;
1071 * Pin the configfs filesystem. This means we can safely access
1072 * the root of the configfs filesystem.
1074 root = configfs_pin_fs();
1076 return PTR_ERR(root);
1079 * Next, lock the root directory. We're going to check that the
1080 * subsystem is really registered, and so we need to lock out
1081 * configfs_[un]register_subsystem().
1083 mutex_lock(&root->d_inode->i_mutex);
1085 root_sd = root->d_fsdata;
1087 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1088 if (p->s_type & CONFIGFS_DIR) {
1089 if (p->s_element == s_item) {
1101 /* Ok, now we can trust subsys/s_item */
1103 spin_lock(&configfs_dirent_lock);
1104 /* Scan the tree, return 0 if found */
1105 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1107 goto out_unlock_dirent_lock;
1110 * We are sure that the item is not about to be removed by rmdir(), and
1111 * not in the middle of attachment by mkdir().
1113 p = target->ci_dentry->d_fsdata;
1114 p->s_dependent_count += 1;
1116 out_unlock_dirent_lock:
1117 spin_unlock(&configfs_dirent_lock);
1119 mutex_unlock(&root->d_inode->i_mutex);
1122 * If we succeeded, the fs is pinned via other methods. If not,
1123 * we're done with it anyway. So release_fs() is always right.
1125 configfs_release_fs();
1129 EXPORT_SYMBOL(configfs_depend_item);
1132 * Release the dependent linkage. This is much simpler than
1133 * configfs_depend_item() because we know that that the client driver is
1134 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1136 void configfs_undepend_item(struct configfs_subsystem *subsys,
1137 struct config_item *target)
1139 struct configfs_dirent *sd;
1142 * Since we can trust everything is pinned, we just need
1143 * configfs_dirent_lock.
1145 spin_lock(&configfs_dirent_lock);
1147 sd = target->ci_dentry->d_fsdata;
1148 BUG_ON(sd->s_dependent_count < 1);
1150 sd->s_dependent_count -= 1;
1153 * After this unlock, we cannot trust the item to stay alive!
1154 * DO NOT REFERENCE item after this unlock.
1156 spin_unlock(&configfs_dirent_lock);
1158 EXPORT_SYMBOL(configfs_undepend_item);
1160 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1164 struct config_group *group = NULL;
1165 struct config_item *item = NULL;
1166 struct config_item *parent_item;
1167 struct configfs_subsystem *subsys;
1168 struct configfs_dirent *sd;
1169 struct config_item_type *type;
1170 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1173 sd = dentry->d_parent->d_fsdata;
1176 * Fake invisibility if dir belongs to a group/default groups hierarchy
1179 if (!configfs_dirent_is_ready(sd)) {
1184 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1189 /* Get a working ref for the duration of this function */
1190 parent_item = configfs_get_config_item(dentry->d_parent);
1191 type = parent_item->ci_type;
1192 subsys = to_config_group(parent_item)->cg_subsys;
1195 if (!type || !type->ct_group_ops ||
1196 (!type->ct_group_ops->make_group &&
1197 !type->ct_group_ops->make_item)) {
1198 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1203 * The subsystem may belong to a different module than the item
1204 * being created. We don't want to safely pin the new item but
1205 * fail to pin the subsystem it sits under.
1207 if (!subsys->su_group.cg_item.ci_type) {
1211 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1212 if (!try_module_get(subsys_owner)) {
1217 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1220 goto out_subsys_put;
1223 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1225 mutex_lock(&subsys->su_mutex);
1226 if (type->ct_group_ops->make_group) {
1227 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1229 group = ERR_PTR(-ENOMEM);
1230 if (!IS_ERR(group)) {
1231 link_group(to_config_group(parent_item), group);
1232 item = &group->cg_item;
1234 ret = PTR_ERR(group);
1236 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1238 item = ERR_PTR(-ENOMEM);
1240 link_obj(parent_item, item);
1242 ret = PTR_ERR(item);
1244 mutex_unlock(&subsys->su_mutex);
1249 * If ret != 0, then link_obj() was never called.
1250 * There are no extra references to clean up.
1252 goto out_subsys_put;
1256 * link_obj() has been called (via link_group() for groups).
1257 * From here on out, errors must clean that up.
1260 type = item->ci_type;
1266 new_item_owner = type->ct_owner;
1267 if (!try_module_get(new_item_owner)) {
1273 * I hate doing it this way, but if there is
1274 * an error, module_put() probably should
1275 * happen after any cleanup.
1280 * Make racing rmdir() fail if it did not tag parent with
1281 * CONFIGFS_USET_DROPPING
1282 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1283 * fail and let rmdir() terminate correctly
1285 spin_lock(&configfs_dirent_lock);
1286 /* This will make configfs_detach_prep() fail */
1287 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1288 spin_unlock(&configfs_dirent_lock);
1291 ret = configfs_attach_group(parent_item, item, dentry);
1293 ret = configfs_attach_item(parent_item, item, dentry);
1295 spin_lock(&configfs_dirent_lock);
1296 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1298 configfs_dir_set_ready(dentry->d_fsdata);
1299 spin_unlock(&configfs_dirent_lock);
1303 /* Tear down everything we built up */
1304 mutex_lock(&subsys->su_mutex);
1306 client_disconnect_notify(parent_item, item);
1308 unlink_group(group);
1311 client_drop_item(parent_item, item);
1313 mutex_unlock(&subsys->su_mutex);
1316 module_put(new_item_owner);
1321 module_put(subsys_owner);
1325 * link_obj()/link_group() took a reference from child->parent,
1326 * so the parent is safely pinned. We can drop our working
1329 config_item_put(parent_item);
1335 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1337 struct config_item *parent_item;
1338 struct config_item *item;
1339 struct configfs_subsystem *subsys;
1340 struct configfs_dirent *sd;
1341 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1344 sd = dentry->d_fsdata;
1345 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1348 /* Get a working ref until we have the child */
1349 parent_item = configfs_get_config_item(dentry->d_parent);
1350 subsys = to_config_group(parent_item)->cg_subsys;
1353 if (!parent_item->ci_type) {
1354 config_item_put(parent_item);
1358 /* configfs_mkdir() shouldn't have allowed this */
1359 BUG_ON(!subsys->su_group.cg_item.ci_type);
1360 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1363 * Ensure that no racing symlink() will make detach_prep() fail while
1364 * the new link is temporarily attached
1367 struct mutex *wait_mutex;
1369 mutex_lock(&configfs_symlink_mutex);
1370 spin_lock(&configfs_dirent_lock);
1372 * Here's where we check for dependents. We're protected by
1373 * configfs_dirent_lock.
1374 * If no dependent, atomically tag the item as dropping.
1376 ret = sd->s_dependent_count ? -EBUSY : 0;
1378 ret = configfs_detach_prep(dentry, &wait_mutex);
1380 configfs_detach_rollback(dentry);
1382 spin_unlock(&configfs_dirent_lock);
1383 mutex_unlock(&configfs_symlink_mutex);
1386 if (ret != -EAGAIN) {
1387 config_item_put(parent_item);
1391 /* Wait until the racing operation terminates */
1392 mutex_lock(wait_mutex);
1393 mutex_unlock(wait_mutex);
1395 } while (ret == -EAGAIN);
1397 /* Get a working ref for the duration of this function */
1398 item = configfs_get_config_item(dentry);
1400 /* Drop reference from above, item already holds one. */
1401 config_item_put(parent_item);
1404 dead_item_owner = item->ci_type->ct_owner;
1406 if (sd->s_type & CONFIGFS_USET_DIR) {
1407 configfs_detach_group(item);
1409 mutex_lock(&subsys->su_mutex);
1410 client_disconnect_notify(parent_item, item);
1411 unlink_group(to_config_group(item));
1413 configfs_detach_item(item);
1415 mutex_lock(&subsys->su_mutex);
1416 client_disconnect_notify(parent_item, item);
1420 client_drop_item(parent_item, item);
1421 mutex_unlock(&subsys->su_mutex);
1423 /* Drop our reference from above */
1424 config_item_put(item);
1426 module_put(dead_item_owner);
1427 module_put(subsys_owner);
1432 const struct inode_operations configfs_dir_inode_operations = {
1433 .mkdir = configfs_mkdir,
1434 .rmdir = configfs_rmdir,
1435 .symlink = configfs_symlink,
1436 .unlink = configfs_unlink,
1437 .lookup = configfs_lookup,
1438 .setattr = configfs_setattr,
1441 const struct inode_operations configfs_root_inode_operations = {
1442 .lookup = configfs_lookup,
1443 .setattr = configfs_setattr,
1447 int configfs_rename_dir(struct config_item * item, const char *new_name)
1450 struct dentry * new_dentry, * parent;
1452 if (!strcmp(config_item_name(item), new_name))
1458 down_write(&configfs_rename_sem);
1459 parent = item->parent->dentry;
1461 mutex_lock(&parent->d_inode->i_mutex);
1463 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1464 if (!IS_ERR(new_dentry)) {
1465 if (!new_dentry->d_inode) {
1466 error = config_item_set_name(item, "%s", new_name);
1468 d_add(new_dentry, NULL);
1469 d_move(item->dentry, new_dentry);
1472 d_delete(new_dentry);
1477 mutex_unlock(&parent->d_inode->i_mutex);
1478 up_write(&configfs_rename_sem);
1484 static int configfs_dir_open(struct inode *inode, struct file *file)
1486 struct dentry * dentry = file->f_path.dentry;
1487 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1490 mutex_lock(&dentry->d_inode->i_mutex);
1492 * Fake invisibility if dir belongs to a group/default groups hierarchy
1496 if (configfs_dirent_is_ready(parent_sd)) {
1497 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1498 if (IS_ERR(file->private_data))
1499 err = PTR_ERR(file->private_data);
1503 mutex_unlock(&dentry->d_inode->i_mutex);
1508 static int configfs_dir_close(struct inode *inode, struct file *file)
1510 struct dentry * dentry = file->f_path.dentry;
1511 struct configfs_dirent * cursor = file->private_data;
1513 mutex_lock(&dentry->d_inode->i_mutex);
1514 spin_lock(&configfs_dirent_lock);
1515 list_del_init(&cursor->s_sibling);
1516 spin_unlock(&configfs_dirent_lock);
1517 mutex_unlock(&dentry->d_inode->i_mutex);
1519 release_configfs_dirent(cursor);
1524 /* Relationship between s_mode and the DT_xxx types */
1525 static inline unsigned char dt_type(struct configfs_dirent *sd)
1527 return (sd->s_mode >> 12) & 15;
1530 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1532 struct dentry *dentry = file->f_path.dentry;
1533 struct super_block *sb = dentry->d_sb;
1534 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1535 struct configfs_dirent *cursor = file->private_data;
1536 struct list_head *p, *q = &cursor->s_sibling;
1539 if (!dir_emit_dots(file, ctx))
1541 if (ctx->pos == 2) {
1542 spin_lock(&configfs_dirent_lock);
1543 list_move(q, &parent_sd->s_children);
1544 spin_unlock(&configfs_dirent_lock);
1546 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1547 struct configfs_dirent *next;
1550 struct inode *inode = NULL;
1552 next = list_entry(p, struct configfs_dirent, s_sibling);
1553 if (!next->s_element)
1556 name = configfs_get_name(next);
1560 * We'll have a dentry and an inode for
1561 * PINNED items and for open attribute
1562 * files. We lock here to prevent a race
1563 * with configfs_d_iput() clearing
1564 * s_dentry before calling iput().
1566 * Why do we go to the trouble? If
1567 * someone has an attribute file open,
1568 * the inode number should match until
1569 * they close it. Beyond that, we don't
1572 spin_lock(&configfs_dirent_lock);
1573 dentry = next->s_dentry;
1575 inode = dentry->d_inode;
1578 spin_unlock(&configfs_dirent_lock);
1580 ino = iunique(sb, 2);
1582 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1585 spin_lock(&configfs_dirent_lock);
1587 spin_unlock(&configfs_dirent_lock);
1594 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1596 struct dentry * dentry = file->f_path.dentry;
1598 mutex_lock(&dentry->d_inode->i_mutex);
1601 offset += file->f_pos;
1606 mutex_unlock(&file_inode(file)->i_mutex);
1609 if (offset != file->f_pos) {
1610 file->f_pos = offset;
1611 if (file->f_pos >= 2) {
1612 struct configfs_dirent *sd = dentry->d_fsdata;
1613 struct configfs_dirent *cursor = file->private_data;
1614 struct list_head *p;
1615 loff_t n = file->f_pos - 2;
1617 spin_lock(&configfs_dirent_lock);
1618 list_del(&cursor->s_sibling);
1619 p = sd->s_children.next;
1620 while (n && p != &sd->s_children) {
1621 struct configfs_dirent *next;
1622 next = list_entry(p, struct configfs_dirent,
1624 if (next->s_element)
1628 list_add_tail(&cursor->s_sibling, p);
1629 spin_unlock(&configfs_dirent_lock);
1632 mutex_unlock(&dentry->d_inode->i_mutex);
1636 const struct file_operations configfs_dir_operations = {
1637 .open = configfs_dir_open,
1638 .release = configfs_dir_close,
1639 .llseek = configfs_dir_lseek,
1640 .read = generic_read_dir,
1641 .iterate = configfs_readdir,
1644 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1647 struct config_group *group = &subsys->su_group;
1648 struct dentry *dentry;
1649 struct dentry *root;
1650 struct configfs_dirent *sd;
1652 root = configfs_pin_fs();
1654 return PTR_ERR(root);
1656 if (!group->cg_item.ci_name)
1657 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1659 sd = root->d_fsdata;
1660 link_group(to_config_group(sd->s_element), group);
1662 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
1665 dentry = d_alloc_name(root, group->cg_item.ci_name);
1667 d_add(dentry, NULL);
1669 err = configfs_attach_group(sd->s_element, &group->cg_item,
1672 BUG_ON(dentry->d_inode);
1676 spin_lock(&configfs_dirent_lock);
1677 configfs_dir_set_ready(dentry->d_fsdata);
1678 spin_unlock(&configfs_dirent_lock);
1682 mutex_unlock(&root->d_inode->i_mutex);
1685 unlink_group(group);
1686 configfs_release_fs();
1692 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1694 struct config_group *group = &subsys->su_group;
1695 struct dentry *dentry = group->cg_item.ci_dentry;
1696 struct dentry *root = dentry->d_sb->s_root;
1698 if (dentry->d_parent != root) {
1699 pr_err("Tried to unregister non-subsystem!\n");
1703 mutex_lock_nested(&root->d_inode->i_mutex,
1705 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1706 mutex_lock(&configfs_symlink_mutex);
1707 spin_lock(&configfs_dirent_lock);
1708 if (configfs_detach_prep(dentry, NULL)) {
1709 pr_err("Tried to unregister non-empty subsystem!\n");
1711 spin_unlock(&configfs_dirent_lock);
1712 mutex_unlock(&configfs_symlink_mutex);
1713 configfs_detach_group(&group->cg_item);
1714 dentry->d_inode->i_flags |= S_DEAD;
1716 mutex_unlock(&dentry->d_inode->i_mutex);
1720 mutex_unlock(&root->d_inode->i_mutex);
1724 unlink_group(group);
1725 configfs_release_fs();
1728 EXPORT_SYMBOL(configfs_register_subsystem);
1729 EXPORT_SYMBOL(configfs_unregister_subsystem);