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);
54 * All of link_obj/unlink_obj/link_group/unlink_group require that
55 * subsys->su_mutex is held.
56 * But parent configfs_subsystem is NULL when config_item is root.
57 * Use this mutex when config_item is root.
59 static DEFINE_MUTEX(configfs_subsystem_mutex);
61 static void configfs_d_iput(struct dentry * dentry,
64 struct configfs_dirent *sd = dentry->d_fsdata;
67 /* Coordinate with configfs_readdir */
68 spin_lock(&configfs_dirent_lock);
70 * Set sd->s_dentry to null only when this dentry is the one
71 * that is going to be killed. Otherwise configfs_d_iput may
72 * run just after configfs_attach_attr and set sd->s_dentry to
73 * NULL even it's still in use.
75 if (sd->s_dentry == dentry)
78 spin_unlock(&configfs_dirent_lock);
84 const struct dentry_operations configfs_dentry_ops = {
85 .d_iput = configfs_d_iput,
86 .d_delete = always_delete_dentry,
92 * Helpers to make lockdep happy with our recursive locking of default groups'
93 * inodes (see configfs_attach_group() and configfs_detach_group()).
94 * We put default groups i_mutexes in separate classes according to their depth
95 * from the youngest non-default group ancestor.
97 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
98 * groups A/B and A/C will have their inode's mutex in class
99 * default_group_class[0], and default group A/C/D will be in
100 * default_group_class[1].
102 * The lock classes are declared and assigned in inode.c, according to the
104 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
105 * default groups, and reset to -1 when all default groups are attached. During
106 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
107 * inode's mutex is set to default_group_class[s_depth - 1].
110 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
115 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
116 struct configfs_dirent *sd)
118 int parent_depth = parent_sd->s_depth;
120 if (parent_depth >= 0)
121 sd->s_depth = parent_depth + 1;
125 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
128 * item's i_mutex class is already setup, so s_depth is now only
129 * used to set new sub-directories s_depth, which is always done
130 * with item's i_mutex locked.
133 * sd->s_depth == -1 iff we are a non default group.
134 * else (we are a default group) sd->s_depth > 0 (see
137 if (sd->s_depth == -1)
139 * We are a non default group and we are going to create
146 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
148 /* We will not create default groups anymore. */
152 #else /* CONFIG_LOCKDEP */
154 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
158 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
159 struct configfs_dirent *sd)
164 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
169 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
173 #endif /* CONFIG_LOCKDEP */
175 static struct configfs_fragment *new_fragment(void)
177 struct configfs_fragment *p;
179 p = kmalloc(sizeof(struct configfs_fragment), GFP_KERNEL);
181 atomic_set(&p->frag_count, 1);
182 init_rwsem(&p->frag_sem);
183 p->frag_dead = false;
188 void put_fragment(struct configfs_fragment *frag)
190 if (frag && atomic_dec_and_test(&frag->frag_count))
194 struct configfs_fragment *get_fragment(struct configfs_fragment *frag)
197 atomic_inc(&frag->frag_count);
202 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
204 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
205 void *element, int type,
206 struct configfs_fragment *frag)
208 struct configfs_dirent * sd;
210 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
212 return ERR_PTR(-ENOMEM);
214 atomic_set(&sd->s_count, 1);
215 INIT_LIST_HEAD(&sd->s_links);
216 INIT_LIST_HEAD(&sd->s_children);
217 sd->s_element = element;
219 configfs_init_dirent_depth(sd);
220 spin_lock(&configfs_dirent_lock);
221 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
222 spin_unlock(&configfs_dirent_lock);
223 kmem_cache_free(configfs_dir_cachep, sd);
224 return ERR_PTR(-ENOENT);
226 sd->s_frag = get_fragment(frag);
227 list_add(&sd->s_sibling, &parent_sd->s_children);
228 spin_unlock(&configfs_dirent_lock);
235 * Return -EEXIST if there is already a configfs element with the same
236 * name for the same parent.
238 * called with parent inode's i_mutex held
240 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
241 const unsigned char *new)
243 struct configfs_dirent * sd;
245 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
247 const unsigned char *existing = configfs_get_name(sd);
248 if (strcmp(existing, new))
259 int configfs_make_dirent(struct configfs_dirent * parent_sd,
260 struct dentry * dentry, void * element,
261 umode_t mode, int type, struct configfs_fragment *frag)
263 struct configfs_dirent * sd;
265 sd = configfs_new_dirent(parent_sd, element, type, frag);
270 sd->s_dentry = dentry;
272 dentry->d_fsdata = configfs_get(sd);
277 static void init_dir(struct inode * inode)
279 inode->i_op = &configfs_dir_inode_operations;
280 inode->i_fop = &configfs_dir_operations;
282 /* directory inodes start off with i_nlink == 2 (for "." entry) */
286 static void configfs_init_file(struct inode * inode)
288 inode->i_size = PAGE_SIZE;
289 inode->i_fop = &configfs_file_operations;
292 static void configfs_init_bin_file(struct inode *inode)
295 inode->i_fop = &configfs_bin_file_operations;
298 static void init_symlink(struct inode * inode)
300 inode->i_op = &configfs_symlink_inode_operations;
304 * configfs_create_dir - create a directory for an config_item.
305 * @item: config_itemwe're creating directory for.
306 * @dentry: config_item's dentry.
308 * Note: user-created entries won't be allowed under this new directory
309 * until it is validated by configfs_dir_set_ready()
312 static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
313 struct configfs_fragment *frag)
316 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
317 struct dentry *p = dentry->d_parent;
321 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
325 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
326 CONFIGFS_DIR | CONFIGFS_USET_CREATING,
331 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
332 error = configfs_create(dentry, mode, init_dir);
334 inc_nlink(d_inode(p));
335 item->ci_dentry = dentry;
337 struct configfs_dirent *sd = dentry->d_fsdata;
339 spin_lock(&configfs_dirent_lock);
340 list_del_init(&sd->s_sibling);
341 spin_unlock(&configfs_dirent_lock);
349 * Allow userspace to create new entries under a new directory created with
350 * configfs_create_dir(), and under all of its chidlren directories recursively.
351 * @sd configfs_dirent of the new directory to validate
353 * Caller must hold configfs_dirent_lock.
355 static void configfs_dir_set_ready(struct configfs_dirent *sd)
357 struct configfs_dirent *child_sd;
359 sd->s_type &= ~CONFIGFS_USET_CREATING;
360 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
361 if (child_sd->s_type & CONFIGFS_USET_CREATING)
362 configfs_dir_set_ready(child_sd);
366 * Check that a directory does not belong to a directory hierarchy being
367 * attached and not validated yet.
368 * @sd configfs_dirent of the directory to check
370 * @return non-zero iff the directory was validated
372 * Note: takes configfs_dirent_lock, so the result may change from false to true
373 * in two consecutive calls, but never from true to false.
375 int configfs_dirent_is_ready(struct configfs_dirent *sd)
379 spin_lock(&configfs_dirent_lock);
380 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
381 spin_unlock(&configfs_dirent_lock);
386 int configfs_create_link(struct configfs_symlink *sl,
387 struct dentry *parent,
388 struct dentry *dentry)
391 umode_t mode = S_IFLNK | S_IRWXUGO;
392 struct configfs_dirent *p = parent->d_fsdata;
394 err = configfs_make_dirent(p, dentry, sl, mode,
395 CONFIGFS_ITEM_LINK, p->s_frag);
397 err = configfs_create(dentry, mode, init_symlink);
399 struct configfs_dirent *sd = dentry->d_fsdata;
401 spin_lock(&configfs_dirent_lock);
402 list_del_init(&sd->s_sibling);
403 spin_unlock(&configfs_dirent_lock);
411 static void remove_dir(struct dentry * d)
413 struct dentry * parent = dget(d->d_parent);
414 struct configfs_dirent * sd;
417 spin_lock(&configfs_dirent_lock);
418 list_del_init(&sd->s_sibling);
419 spin_unlock(&configfs_dirent_lock);
421 if (d_really_is_positive(d))
422 simple_rmdir(d_inode(parent),d);
424 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
430 * configfs_remove_dir - remove an config_item's directory.
431 * @item: config_item we're removing.
433 * The only thing special about this is that we remove any files in
434 * the directory before we remove the directory, and we've inlined
435 * what used to be configfs_rmdir() below, instead of calling separately.
437 * Caller holds the mutex of the item's inode
440 static void configfs_remove_dir(struct config_item * item)
442 struct dentry * dentry = dget(item->ci_dentry);
449 * Drop reference from dget() on entrance.
455 /* attaches attribute's configfs_dirent to the dentry corresponding to the
458 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
460 struct configfs_attribute * attr = sd->s_element;
463 spin_lock(&configfs_dirent_lock);
464 dentry->d_fsdata = configfs_get(sd);
465 sd->s_dentry = dentry;
466 spin_unlock(&configfs_dirent_lock);
468 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
469 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
470 configfs_init_bin_file :
477 static struct dentry * configfs_lookup(struct inode *dir,
478 struct dentry *dentry,
481 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
482 struct configfs_dirent * sd;
487 * Fake invisibility if dir belongs to a group/default groups hierarchy
490 * This forbids userspace to read/write attributes of items which may
491 * not complete their initialization, since the dentries of the
492 * attributes won't be instantiated.
495 if (!configfs_dirent_is_ready(parent_sd))
498 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
499 if (sd->s_type & CONFIGFS_NOT_PINNED) {
500 const unsigned char * name = configfs_get_name(sd);
502 if (strcmp(name, dentry->d_name.name))
506 err = configfs_attach_attr(sd, dentry);
513 * If it doesn't exist and it isn't a NOT_PINNED item,
514 * it must be negative.
516 if (dentry->d_name.len > NAME_MAX)
517 return ERR_PTR(-ENAMETOOLONG);
527 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
528 * attributes and are removed by rmdir(). We recurse, setting
529 * CONFIGFS_USET_DROPPING on all children that are candidates for
531 * If there is an error, the caller will reset the flags via
532 * configfs_detach_rollback().
534 static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
536 struct configfs_dirent *parent_sd = dentry->d_fsdata;
537 struct configfs_dirent *sd;
540 /* Mark that we're trying to drop the group */
541 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
544 if (!list_empty(&parent_sd->s_links))
548 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
549 if (!sd->s_element ||
550 (sd->s_type & CONFIGFS_NOT_PINNED))
552 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
553 /* Abort if racing with mkdir() */
554 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
556 *wait= dget(sd->s_dentry);
561 * Yup, recursive. If there's a problem, blame
562 * deep nesting of default_groups
564 ret = configfs_detach_prep(sd->s_dentry, wait);
578 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
581 static void configfs_detach_rollback(struct dentry *dentry)
583 struct configfs_dirent *parent_sd = dentry->d_fsdata;
584 struct configfs_dirent *sd;
586 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
588 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
589 if (sd->s_type & CONFIGFS_USET_DEFAULT)
590 configfs_detach_rollback(sd->s_dentry);
593 static void detach_attrs(struct config_item * item)
595 struct dentry * dentry = dget(item->ci_dentry);
596 struct configfs_dirent * parent_sd;
597 struct configfs_dirent * sd, * tmp;
602 pr_debug("configfs %s: dropping attrs for dir\n",
603 dentry->d_name.name);
605 parent_sd = dentry->d_fsdata;
606 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
607 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
609 spin_lock(&configfs_dirent_lock);
610 list_del_init(&sd->s_sibling);
611 spin_unlock(&configfs_dirent_lock);
612 configfs_drop_dentry(sd, dentry);
617 * Drop reference from dget() on entrance.
622 static int populate_attrs(struct config_item *item)
624 const struct config_item_type *t = item->ci_type;
625 struct configfs_attribute *attr;
626 struct configfs_bin_attribute *bin_attr;
633 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
634 if ((error = configfs_create_file(item, attr)))
638 if (t->ct_bin_attrs) {
639 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
640 error = configfs_create_bin_file(item, bin_attr);
652 static int configfs_attach_group(struct config_item *parent_item,
653 struct config_item *item,
654 struct dentry *dentry,
655 struct configfs_fragment *frag);
656 static void configfs_detach_group(struct config_item *item);
658 static void detach_groups(struct config_group *group)
660 struct dentry * dentry = dget(group->cg_item.ci_dentry);
661 struct dentry *child;
662 struct configfs_dirent *parent_sd;
663 struct configfs_dirent *sd, *tmp;
668 parent_sd = dentry->d_fsdata;
669 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
670 if (!sd->s_element ||
671 !(sd->s_type & CONFIGFS_USET_DEFAULT))
674 child = sd->s_dentry;
676 inode_lock(d_inode(child));
678 configfs_detach_group(sd->s_element);
679 d_inode(child)->i_flags |= S_DEAD;
682 inode_unlock(d_inode(child));
689 * Drop reference from dget() on entrance.
695 * This fakes mkdir(2) on a default_groups[] entry. It
696 * creates a dentry, attachs it, and then does fixup
699 * We could, perhaps, tweak our parent's ->mkdir for a minute and
700 * try using vfs_mkdir. Just a thought.
702 static int create_default_group(struct config_group *parent_group,
703 struct config_group *group,
704 struct configfs_fragment *frag)
707 struct configfs_dirent *sd;
708 /* We trust the caller holds a reference to parent */
709 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
711 if (!group->cg_item.ci_name)
712 group->cg_item.ci_name = group->cg_item.ci_namebuf;
715 child = d_alloc_name(parent, group->cg_item.ci_name);
719 ret = configfs_attach_group(&parent_group->cg_item,
720 &group->cg_item, child, frag);
722 sd = child->d_fsdata;
723 sd->s_type |= CONFIGFS_USET_DEFAULT;
725 BUG_ON(d_inode(child));
734 static int populate_groups(struct config_group *group,
735 struct configfs_fragment *frag)
737 struct config_group *new_group;
740 list_for_each_entry(new_group, &group->default_groups, group_entry) {
741 ret = create_default_group(group, new_group, frag);
743 detach_groups(group);
751 void configfs_remove_default_groups(struct config_group *group)
753 struct config_group *g, *n;
755 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
756 list_del(&g->group_entry);
757 config_item_put(&g->cg_item);
760 EXPORT_SYMBOL(configfs_remove_default_groups);
763 * All of link_obj/unlink_obj/link_group/unlink_group require that
764 * subsys->su_mutex is held.
767 static void unlink_obj(struct config_item *item)
769 struct config_group *group;
771 group = item->ci_group;
773 list_del_init(&item->ci_entry);
775 item->ci_group = NULL;
776 item->ci_parent = NULL;
778 /* Drop the reference for ci_entry */
779 config_item_put(item);
781 /* Drop the reference for ci_parent */
782 config_group_put(group);
786 static void link_obj(struct config_item *parent_item, struct config_item *item)
789 * Parent seems redundant with group, but it makes certain
790 * traversals much nicer.
792 item->ci_parent = parent_item;
795 * We hold a reference on the parent for the child's ci_parent
798 item->ci_group = config_group_get(to_config_group(parent_item));
799 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
802 * We hold a reference on the child for ci_entry on the parent's
805 config_item_get(item);
808 static void unlink_group(struct config_group *group)
810 struct config_group *new_group;
812 list_for_each_entry(new_group, &group->default_groups, group_entry)
813 unlink_group(new_group);
815 group->cg_subsys = NULL;
816 unlink_obj(&group->cg_item);
819 static void link_group(struct config_group *parent_group, struct config_group *group)
821 struct config_group *new_group;
822 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
824 link_obj(&parent_group->cg_item, &group->cg_item);
826 if (parent_group->cg_subsys)
827 subsys = parent_group->cg_subsys;
828 else if (configfs_is_root(&parent_group->cg_item))
829 subsys = to_configfs_subsystem(group);
832 group->cg_subsys = subsys;
834 list_for_each_entry(new_group, &group->default_groups, group_entry)
835 link_group(group, new_group);
839 * The goal is that configfs_attach_item() (and
840 * configfs_attach_group()) can be called from either the VFS or this
841 * module. That is, they assume that the items have been created,
842 * the dentry allocated, and the dcache is all ready to go.
844 * If they fail, they must clean up after themselves as if they
845 * had never been called. The caller (VFS or local function) will
846 * handle cleaning up the dcache bits.
848 * configfs_detach_group() and configfs_detach_item() behave similarly on
849 * the way out. They assume that the proper semaphores are held, they
850 * clean up the configfs items, and they expect their callers will
851 * handle the dcache bits.
853 static int configfs_attach_item(struct config_item *parent_item,
854 struct config_item *item,
855 struct dentry *dentry,
856 struct configfs_fragment *frag)
860 ret = configfs_create_dir(item, dentry, frag);
862 ret = populate_attrs(item);
865 * We are going to remove an inode and its dentry but
866 * the VFS may already have hit and used them. Thus,
867 * we must lock them as rmdir() would.
869 inode_lock(d_inode(dentry));
870 configfs_remove_dir(item);
871 d_inode(dentry)->i_flags |= S_DEAD;
873 inode_unlock(d_inode(dentry));
881 /* Caller holds the mutex of the item's inode */
882 static void configfs_detach_item(struct config_item *item)
885 configfs_remove_dir(item);
888 static int configfs_attach_group(struct config_item *parent_item,
889 struct config_item *item,
890 struct dentry *dentry,
891 struct configfs_fragment *frag)
894 struct configfs_dirent *sd;
896 ret = configfs_attach_item(parent_item, item, dentry, frag);
898 sd = dentry->d_fsdata;
899 sd->s_type |= CONFIGFS_USET_DIR;
902 * FYI, we're faking mkdir in populate_groups()
903 * We must lock the group's inode to avoid races with the VFS
904 * which can already hit the inode and try to add/remove entries
907 * We must also lock the inode to remove it safely in case of
908 * error, as rmdir() would.
910 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
911 configfs_adjust_dir_dirent_depth_before_populate(sd);
912 ret = populate_groups(to_config_group(item), frag);
914 configfs_detach_item(item);
915 d_inode(dentry)->i_flags |= S_DEAD;
918 configfs_adjust_dir_dirent_depth_after_populate(sd);
919 inode_unlock(d_inode(dentry));
927 /* Caller holds the mutex of the group's inode */
928 static void configfs_detach_group(struct config_item *item)
930 detach_groups(to_config_group(item));
931 configfs_detach_item(item);
935 * After the item has been detached from the filesystem view, we are
936 * ready to tear it out of the hierarchy. Notify the client before
937 * we do that so they can perform any cleanup that requires
938 * navigating the hierarchy. A client does not need to provide this
939 * callback. The subsystem semaphore MUST be held by the caller, and
940 * references must be valid for both items. It also assumes the
941 * caller has validated ci_type.
943 static void client_disconnect_notify(struct config_item *parent_item,
944 struct config_item *item)
946 const struct config_item_type *type;
948 type = parent_item->ci_type;
951 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
952 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
957 * Drop the initial reference from make_item()/make_group()
958 * This function assumes that reference is held on item
959 * and that item holds a valid reference to the parent. Also, it
960 * assumes the caller has validated ci_type.
962 static void client_drop_item(struct config_item *parent_item,
963 struct config_item *item)
965 const struct config_item_type *type;
967 type = parent_item->ci_type;
971 * If ->drop_item() exists, it is responsible for the
974 if (type->ct_group_ops && type->ct_group_ops->drop_item)
975 type->ct_group_ops->drop_item(to_config_group(parent_item),
978 config_item_put(item);
982 static void configfs_dump_one(struct configfs_dirent *sd, int level)
984 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
986 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
987 type_print(CONFIGFS_ROOT);
988 type_print(CONFIGFS_DIR);
989 type_print(CONFIGFS_ITEM_ATTR);
990 type_print(CONFIGFS_ITEM_LINK);
991 type_print(CONFIGFS_USET_DIR);
992 type_print(CONFIGFS_USET_DEFAULT);
993 type_print(CONFIGFS_USET_DROPPING);
997 static int configfs_dump(struct configfs_dirent *sd, int level)
999 struct configfs_dirent *child_sd;
1002 configfs_dump_one(sd, level);
1004 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
1007 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1008 ret = configfs_dump(child_sd, level + 2);
1019 * configfs_depend_item() and configfs_undepend_item()
1021 * WARNING: Do not call these from a configfs callback!
1023 * This describes these functions and their helpers.
1025 * Allow another kernel system to depend on a config_item. If this
1026 * happens, the item cannot go away until the dependent can live without
1027 * it. The idea is to give client modules as simple an interface as
1028 * possible. When a system asks them to depend on an item, they just
1029 * call configfs_depend_item(). If the item is live and the client
1030 * driver is in good shape, we'll happily do the work for them.
1032 * Why is the locking complex? Because configfs uses the VFS to handle
1033 * all locking, but this function is called outside the normal
1034 * VFS->configfs path. So it must take VFS locks to prevent the
1035 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1036 * why you can't call these functions underneath configfs callbacks.
1038 * Note, btw, that this can be called at *any* time, even when a configfs
1039 * subsystem isn't registered, or when configfs is loading or unloading.
1040 * Just like configfs_register_subsystem(). So we take the same
1041 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1042 * If we can find the target item in the
1043 * configfs tree, it must be part of the subsystem tree as well, so we
1044 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1045 * locking out mkdir() and rmdir(), who might be racing us.
1049 * configfs_depend_prep()
1051 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1052 * attributes. This is similar but not the same to configfs_detach_prep().
1053 * Note that configfs_detach_prep() expects the parent to be locked when it
1054 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1055 * do that so we can unlock it if we find nothing.
1057 * Here we do a depth-first search of the dentry hierarchy looking for
1059 * We deliberately ignore items tagged as dropping since they are virtually
1060 * dead, as well as items in the middle of attachment since they virtually
1061 * do not exist yet. This completes the locking out of racing mkdir() and
1063 * Note: subdirectories in the middle of attachment start with s_type =
1064 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1065 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1066 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1068 * If the target is not found, -ENOENT is bubbled up.
1070 * This adds a requirement that all config_items be unique!
1072 * This is recursive. There isn't
1073 * much on the stack, though, so folks that need this function - be careful
1074 * about your stack! Patches will be accepted to make it iterative.
1076 static int configfs_depend_prep(struct dentry *origin,
1077 struct config_item *target)
1079 struct configfs_dirent *child_sd, *sd;
1082 BUG_ON(!origin || !origin->d_fsdata);
1083 sd = origin->d_fsdata;
1085 if (sd->s_element == target) /* Boo-yah */
1088 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1089 if ((child_sd->s_type & CONFIGFS_DIR) &&
1090 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1091 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1092 ret = configfs_depend_prep(child_sd->s_dentry,
1095 goto out; /* Child path boo-yah */
1099 /* We looped all our children and didn't find target */
1106 static int configfs_do_depend_item(struct dentry *subsys_dentry,
1107 struct config_item *target)
1109 struct configfs_dirent *p;
1112 spin_lock(&configfs_dirent_lock);
1113 /* Scan the tree, return 0 if found */
1114 ret = configfs_depend_prep(subsys_dentry, target);
1116 goto out_unlock_dirent_lock;
1119 * We are sure that the item is not about to be removed by rmdir(), and
1120 * not in the middle of attachment by mkdir().
1122 p = target->ci_dentry->d_fsdata;
1123 p->s_dependent_count += 1;
1125 out_unlock_dirent_lock:
1126 spin_unlock(&configfs_dirent_lock);
1131 static inline struct configfs_dirent *
1132 configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1133 struct config_item *subsys_item)
1135 struct configfs_dirent *p;
1136 struct configfs_dirent *ret = NULL;
1138 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1139 if (p->s_type & CONFIGFS_DIR &&
1140 p->s_element == subsys_item) {
1150 int configfs_depend_item(struct configfs_subsystem *subsys,
1151 struct config_item *target)
1154 struct configfs_dirent *subsys_sd;
1155 struct config_item *s_item = &subsys->su_group.cg_item;
1156 struct dentry *root;
1159 * Pin the configfs filesystem. This means we can safely access
1160 * the root of the configfs filesystem.
1162 root = configfs_pin_fs();
1164 return PTR_ERR(root);
1167 * Next, lock the root directory. We're going to check that the
1168 * subsystem is really registered, and so we need to lock out
1169 * configfs_[un]register_subsystem().
1171 inode_lock(d_inode(root));
1173 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
1179 /* Ok, now we can trust subsys/s_item */
1180 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1183 inode_unlock(d_inode(root));
1186 * If we succeeded, the fs is pinned via other methods. If not,
1187 * we're done with it anyway. So release_fs() is always right.
1189 configfs_release_fs();
1193 EXPORT_SYMBOL(configfs_depend_item);
1196 * Release the dependent linkage. This is much simpler than
1197 * configfs_depend_item() because we know that that the client driver is
1198 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1200 void configfs_undepend_item(struct config_item *target)
1202 struct configfs_dirent *sd;
1205 * Since we can trust everything is pinned, we just need
1206 * configfs_dirent_lock.
1208 spin_lock(&configfs_dirent_lock);
1210 sd = target->ci_dentry->d_fsdata;
1211 BUG_ON(sd->s_dependent_count < 1);
1213 sd->s_dependent_count -= 1;
1216 * After this unlock, we cannot trust the item to stay alive!
1217 * DO NOT REFERENCE item after this unlock.
1219 spin_unlock(&configfs_dirent_lock);
1221 EXPORT_SYMBOL(configfs_undepend_item);
1224 * caller_subsys is a caller's subsystem not target's. This is used to
1225 * determine if we should lock root and check subsys or not. When we are
1226 * in the same subsystem as our target there is no need to do locking as
1227 * we know that subsys is valid and is not unregistered during this function
1228 * as we are called from callback of one of his children and VFS holds a lock
1229 * on some inode. Otherwise we have to lock our root to ensure that target's
1230 * subsystem it is not unregistered during this function.
1232 int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1233 struct config_item *target)
1235 struct configfs_subsystem *target_subsys;
1236 struct config_group *root, *parent;
1237 struct configfs_dirent *subsys_sd;
1240 /* Disallow this function for configfs root */
1241 if (configfs_is_root(target))
1244 parent = target->ci_group;
1246 * This may happen when someone is trying to depend root
1247 * directory of some subsystem
1249 if (configfs_is_root(&parent->cg_item)) {
1250 target_subsys = to_configfs_subsystem(to_config_group(target));
1253 target_subsys = parent->cg_subsys;
1254 /* Find a cofnigfs root as we may need it for locking */
1255 for (root = parent; !configfs_is_root(&root->cg_item);
1256 root = root->cg_item.ci_group)
1260 if (target_subsys != caller_subsys) {
1262 * We are in other configfs subsystem, so we have to do
1263 * additional locking to prevent other subsystem from being
1266 inode_lock(d_inode(root->cg_item.ci_dentry));
1269 * As we are trying to depend item from other subsystem
1270 * we have to check if this subsystem is still registered
1272 subsys_sd = configfs_find_subsys_dentry(
1273 root->cg_item.ci_dentry->d_fsdata,
1274 &target_subsys->su_group.cg_item);
1276 goto out_root_unlock;
1278 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1281 /* Now we can execute core of depend item */
1282 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1284 if (target_subsys != caller_subsys)
1287 * We were called from subsystem other than our target so we
1288 * took some locks so now it's time to release them
1290 inode_unlock(d_inode(root->cg_item.ci_dentry));
1294 EXPORT_SYMBOL(configfs_depend_item_unlocked);
1296 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1300 struct config_group *group = NULL;
1301 struct config_item *item = NULL;
1302 struct config_item *parent_item;
1303 struct configfs_subsystem *subsys;
1304 struct configfs_dirent *sd;
1305 const struct config_item_type *type;
1306 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1307 struct configfs_fragment *frag;
1310 sd = dentry->d_parent->d_fsdata;
1313 * Fake invisibility if dir belongs to a group/default groups hierarchy
1316 if (!configfs_dirent_is_ready(sd)) {
1321 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1326 frag = new_fragment();
1332 /* Get a working ref for the duration of this function */
1333 parent_item = configfs_get_config_item(dentry->d_parent);
1334 type = parent_item->ci_type;
1335 subsys = to_config_group(parent_item)->cg_subsys;
1338 if (!type || !type->ct_group_ops ||
1339 (!type->ct_group_ops->make_group &&
1340 !type->ct_group_ops->make_item)) {
1341 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1346 * The subsystem may belong to a different module than the item
1347 * being created. We don't want to safely pin the new item but
1348 * fail to pin the subsystem it sits under.
1350 if (!subsys->su_group.cg_item.ci_type) {
1354 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1355 if (!try_module_get(subsys_owner)) {
1360 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1363 goto out_subsys_put;
1366 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1368 mutex_lock(&subsys->su_mutex);
1369 if (type->ct_group_ops->make_group) {
1370 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1372 group = ERR_PTR(-ENOMEM);
1373 if (!IS_ERR(group)) {
1374 link_group(to_config_group(parent_item), group);
1375 item = &group->cg_item;
1377 ret = PTR_ERR(group);
1379 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1381 item = ERR_PTR(-ENOMEM);
1383 link_obj(parent_item, item);
1385 ret = PTR_ERR(item);
1387 mutex_unlock(&subsys->su_mutex);
1392 * If ret != 0, then link_obj() was never called.
1393 * There are no extra references to clean up.
1395 goto out_subsys_put;
1399 * link_obj() has been called (via link_group() for groups).
1400 * From here on out, errors must clean that up.
1403 type = item->ci_type;
1409 new_item_owner = type->ct_owner;
1410 if (!try_module_get(new_item_owner)) {
1416 * I hate doing it this way, but if there is
1417 * an error, module_put() probably should
1418 * happen after any cleanup.
1423 * Make racing rmdir() fail if it did not tag parent with
1424 * CONFIGFS_USET_DROPPING
1425 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1426 * fail and let rmdir() terminate correctly
1428 spin_lock(&configfs_dirent_lock);
1429 /* This will make configfs_detach_prep() fail */
1430 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1431 spin_unlock(&configfs_dirent_lock);
1434 ret = configfs_attach_group(parent_item, item, dentry, frag);
1436 ret = configfs_attach_item(parent_item, item, dentry, frag);
1438 spin_lock(&configfs_dirent_lock);
1439 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1441 configfs_dir_set_ready(dentry->d_fsdata);
1442 spin_unlock(&configfs_dirent_lock);
1446 /* Tear down everything we built up */
1447 mutex_lock(&subsys->su_mutex);
1449 client_disconnect_notify(parent_item, item);
1451 unlink_group(group);
1454 client_drop_item(parent_item, item);
1456 mutex_unlock(&subsys->su_mutex);
1459 module_put(new_item_owner);
1464 module_put(subsys_owner);
1468 * link_obj()/link_group() took a reference from child->parent,
1469 * so the parent is safely pinned. We can drop our working
1472 config_item_put(parent_item);
1479 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1481 struct config_item *parent_item;
1482 struct config_item *item;
1483 struct configfs_subsystem *subsys;
1484 struct configfs_dirent *sd;
1485 struct configfs_fragment *frag;
1486 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1489 sd = dentry->d_fsdata;
1490 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1493 /* Get a working ref until we have the child */
1494 parent_item = configfs_get_config_item(dentry->d_parent);
1495 subsys = to_config_group(parent_item)->cg_subsys;
1498 if (!parent_item->ci_type) {
1499 config_item_put(parent_item);
1503 /* configfs_mkdir() shouldn't have allowed this */
1504 BUG_ON(!subsys->su_group.cg_item.ci_type);
1505 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1508 * Ensure that no racing symlink() will make detach_prep() fail while
1509 * the new link is temporarily attached
1512 struct dentry *wait;
1514 mutex_lock(&configfs_symlink_mutex);
1515 spin_lock(&configfs_dirent_lock);
1517 * Here's where we check for dependents. We're protected by
1518 * configfs_dirent_lock.
1519 * If no dependent, atomically tag the item as dropping.
1521 ret = sd->s_dependent_count ? -EBUSY : 0;
1523 ret = configfs_detach_prep(dentry, &wait);
1525 configfs_detach_rollback(dentry);
1527 spin_unlock(&configfs_dirent_lock);
1528 mutex_unlock(&configfs_symlink_mutex);
1531 if (ret != -EAGAIN) {
1532 config_item_put(parent_item);
1536 /* Wait until the racing operation terminates */
1537 inode_lock(d_inode(wait));
1538 inode_unlock(d_inode(wait));
1541 } while (ret == -EAGAIN);
1544 if (down_write_killable(&frag->frag_sem)) {
1545 spin_lock(&configfs_dirent_lock);
1546 configfs_detach_rollback(dentry);
1547 spin_unlock(&configfs_dirent_lock);
1548 config_item_put(parent_item);
1551 frag->frag_dead = true;
1552 up_write(&frag->frag_sem);
1554 /* Get a working ref for the duration of this function */
1555 item = configfs_get_config_item(dentry);
1557 /* Drop reference from above, item already holds one. */
1558 config_item_put(parent_item);
1561 dead_item_owner = item->ci_type->ct_owner;
1563 if (sd->s_type & CONFIGFS_USET_DIR) {
1564 configfs_detach_group(item);
1566 mutex_lock(&subsys->su_mutex);
1567 client_disconnect_notify(parent_item, item);
1568 unlink_group(to_config_group(item));
1570 configfs_detach_item(item);
1572 mutex_lock(&subsys->su_mutex);
1573 client_disconnect_notify(parent_item, item);
1577 client_drop_item(parent_item, item);
1578 mutex_unlock(&subsys->su_mutex);
1580 /* Drop our reference from above */
1581 config_item_put(item);
1583 module_put(dead_item_owner);
1584 module_put(subsys_owner);
1589 const struct inode_operations configfs_dir_inode_operations = {
1590 .mkdir = configfs_mkdir,
1591 .rmdir = configfs_rmdir,
1592 .symlink = configfs_symlink,
1593 .unlink = configfs_unlink,
1594 .lookup = configfs_lookup,
1595 .setattr = configfs_setattr,
1598 const struct inode_operations configfs_root_inode_operations = {
1599 .lookup = configfs_lookup,
1600 .setattr = configfs_setattr,
1604 int configfs_rename_dir(struct config_item * item, const char *new_name)
1607 struct dentry * new_dentry, * parent;
1609 if (!strcmp(config_item_name(item), new_name))
1615 down_write(&configfs_rename_sem);
1616 parent = item->parent->dentry;
1618 inode_lock(d_inode(parent));
1620 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1621 if (!IS_ERR(new_dentry)) {
1622 if (d_really_is_negative(new_dentry)) {
1623 error = config_item_set_name(item, "%s", new_name);
1625 d_add(new_dentry, NULL);
1626 d_move(item->dentry, new_dentry);
1629 d_delete(new_dentry);
1634 inode_unlock(d_inode(parent));
1635 up_write(&configfs_rename_sem);
1641 static int configfs_dir_open(struct inode *inode, struct file *file)
1643 struct dentry * dentry = file->f_path.dentry;
1644 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1647 inode_lock(d_inode(dentry));
1649 * Fake invisibility if dir belongs to a group/default groups hierarchy
1653 if (configfs_dirent_is_ready(parent_sd)) {
1654 file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
1655 if (IS_ERR(file->private_data))
1656 err = PTR_ERR(file->private_data);
1660 inode_unlock(d_inode(dentry));
1665 static int configfs_dir_close(struct inode *inode, struct file *file)
1667 struct dentry * dentry = file->f_path.dentry;
1668 struct configfs_dirent * cursor = file->private_data;
1670 inode_lock(d_inode(dentry));
1671 spin_lock(&configfs_dirent_lock);
1672 list_del_init(&cursor->s_sibling);
1673 spin_unlock(&configfs_dirent_lock);
1674 inode_unlock(d_inode(dentry));
1676 release_configfs_dirent(cursor);
1681 /* Relationship between s_mode and the DT_xxx types */
1682 static inline unsigned char dt_type(struct configfs_dirent *sd)
1684 return (sd->s_mode >> 12) & 15;
1687 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1689 struct dentry *dentry = file->f_path.dentry;
1690 struct super_block *sb = dentry->d_sb;
1691 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1692 struct configfs_dirent *cursor = file->private_data;
1693 struct list_head *p, *q = &cursor->s_sibling;
1696 if (!dir_emit_dots(file, ctx))
1698 spin_lock(&configfs_dirent_lock);
1700 list_move(q, &parent_sd->s_children);
1701 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1702 struct configfs_dirent *next;
1705 struct inode *inode = NULL;
1707 next = list_entry(p, struct configfs_dirent, s_sibling);
1708 if (!next->s_element)
1712 * We'll have a dentry and an inode for
1713 * PINNED items and for open attribute
1714 * files. We lock here to prevent a race
1715 * with configfs_d_iput() clearing
1716 * s_dentry before calling iput().
1718 * Why do we go to the trouble? If
1719 * someone has an attribute file open,
1720 * the inode number should match until
1721 * they close it. Beyond that, we don't
1724 dentry = next->s_dentry;
1726 inode = d_inode(dentry);
1729 spin_unlock(&configfs_dirent_lock);
1731 ino = iunique(sb, 2);
1733 name = configfs_get_name(next);
1736 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1739 spin_lock(&configfs_dirent_lock);
1744 spin_unlock(&configfs_dirent_lock);
1748 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1750 struct dentry * dentry = file->f_path.dentry;
1754 offset += file->f_pos;
1761 if (offset != file->f_pos) {
1762 file->f_pos = offset;
1763 if (file->f_pos >= 2) {
1764 struct configfs_dirent *sd = dentry->d_fsdata;
1765 struct configfs_dirent *cursor = file->private_data;
1766 struct list_head *p;
1767 loff_t n = file->f_pos - 2;
1769 spin_lock(&configfs_dirent_lock);
1770 list_del(&cursor->s_sibling);
1771 p = sd->s_children.next;
1772 while (n && p != &sd->s_children) {
1773 struct configfs_dirent *next;
1774 next = list_entry(p, struct configfs_dirent,
1776 if (next->s_element)
1780 list_add_tail(&cursor->s_sibling, p);
1781 spin_unlock(&configfs_dirent_lock);
1787 const struct file_operations configfs_dir_operations = {
1788 .open = configfs_dir_open,
1789 .release = configfs_dir_close,
1790 .llseek = configfs_dir_lseek,
1791 .read = generic_read_dir,
1792 .iterate_shared = configfs_readdir,
1796 * configfs_register_group - creates a parent-child relation between two groups
1797 * @parent_group: parent group
1798 * @group: child group
1800 * link groups, creates dentry for the child and attaches it to the
1803 * Return: 0 on success, negative errno code on error
1805 int configfs_register_group(struct config_group *parent_group,
1806 struct config_group *group)
1808 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1809 struct dentry *parent;
1810 struct configfs_fragment *frag;
1813 frag = new_fragment();
1817 mutex_lock(&subsys->su_mutex);
1818 link_group(parent_group, group);
1819 mutex_unlock(&subsys->su_mutex);
1821 parent = parent_group->cg_item.ci_dentry;
1823 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1824 ret = create_default_group(parent_group, group, frag);
1828 spin_lock(&configfs_dirent_lock);
1829 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1830 spin_unlock(&configfs_dirent_lock);
1831 inode_unlock(d_inode(parent));
1835 inode_unlock(d_inode(parent));
1836 mutex_lock(&subsys->su_mutex);
1837 unlink_group(group);
1838 mutex_unlock(&subsys->su_mutex);
1842 EXPORT_SYMBOL(configfs_register_group);
1845 * configfs_unregister_group() - unregisters a child group from its parent
1846 * @group: parent group to be unregistered
1848 * Undoes configfs_register_group()
1850 void configfs_unregister_group(struct config_group *group)
1852 struct configfs_subsystem *subsys = group->cg_subsys;
1853 struct dentry *dentry = group->cg_item.ci_dentry;
1854 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1855 struct configfs_dirent *sd = dentry->d_fsdata;
1856 struct configfs_fragment *frag = sd->s_frag;
1858 down_write(&frag->frag_sem);
1859 frag->frag_dead = true;
1860 up_write(&frag->frag_sem);
1862 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1863 spin_lock(&configfs_dirent_lock);
1864 configfs_detach_prep(dentry, NULL);
1865 spin_unlock(&configfs_dirent_lock);
1867 configfs_detach_group(&group->cg_item);
1868 d_inode(dentry)->i_flags |= S_DEAD;
1871 inode_unlock(d_inode(parent));
1875 mutex_lock(&subsys->su_mutex);
1876 unlink_group(group);
1877 mutex_unlock(&subsys->su_mutex);
1879 EXPORT_SYMBOL(configfs_unregister_group);
1882 * configfs_register_default_group() - allocates and registers a child group
1883 * @parent_group: parent group
1884 * @name: child group name
1885 * @item_type: child item type description
1887 * boilerplate to allocate and register a child group with its parent. We need
1888 * kzalloc'ed memory because child's default_group is initially empty.
1890 * Return: allocated config group or ERR_PTR() on error
1892 struct config_group *
1893 configfs_register_default_group(struct config_group *parent_group,
1895 const struct config_item_type *item_type)
1898 struct config_group *group;
1900 group = kzalloc(sizeof(*group), GFP_KERNEL);
1902 return ERR_PTR(-ENOMEM);
1903 config_group_init_type_name(group, name, item_type);
1905 ret = configfs_register_group(parent_group, group);
1908 return ERR_PTR(ret);
1912 EXPORT_SYMBOL(configfs_register_default_group);
1915 * configfs_unregister_default_group() - unregisters and frees a child group
1916 * @group: the group to act on
1918 void configfs_unregister_default_group(struct config_group *group)
1920 configfs_unregister_group(group);
1923 EXPORT_SYMBOL(configfs_unregister_default_group);
1925 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1928 struct config_group *group = &subsys->su_group;
1929 struct dentry *dentry;
1930 struct dentry *root;
1931 struct configfs_dirent *sd;
1932 struct configfs_fragment *frag;
1934 frag = new_fragment();
1938 root = configfs_pin_fs();
1941 return PTR_ERR(root);
1944 if (!group->cg_item.ci_name)
1945 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1947 sd = root->d_fsdata;
1948 mutex_lock(&configfs_subsystem_mutex);
1949 link_group(to_config_group(sd->s_element), group);
1950 mutex_unlock(&configfs_subsystem_mutex);
1952 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
1955 dentry = d_alloc_name(root, group->cg_item.ci_name);
1957 d_add(dentry, NULL);
1959 err = configfs_attach_group(sd->s_element, &group->cg_item,
1962 BUG_ON(d_inode(dentry));
1966 spin_lock(&configfs_dirent_lock);
1967 configfs_dir_set_ready(dentry->d_fsdata);
1968 spin_unlock(&configfs_dirent_lock);
1972 inode_unlock(d_inode(root));
1975 mutex_lock(&configfs_subsystem_mutex);
1976 unlink_group(group);
1977 mutex_unlock(&configfs_subsystem_mutex);
1978 configfs_release_fs();
1985 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1987 struct config_group *group = &subsys->su_group;
1988 struct dentry *dentry = group->cg_item.ci_dentry;
1989 struct dentry *root = dentry->d_sb->s_root;
1990 struct configfs_dirent *sd = dentry->d_fsdata;
1991 struct configfs_fragment *frag = sd->s_frag;
1993 if (dentry->d_parent != root) {
1994 pr_err("Tried to unregister non-subsystem!\n");
1998 down_write(&frag->frag_sem);
1999 frag->frag_dead = true;
2000 up_write(&frag->frag_sem);
2002 inode_lock_nested(d_inode(root),
2004 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
2005 mutex_lock(&configfs_symlink_mutex);
2006 spin_lock(&configfs_dirent_lock);
2007 if (configfs_detach_prep(dentry, NULL)) {
2008 pr_err("Tried to unregister non-empty subsystem!\n");
2010 spin_unlock(&configfs_dirent_lock);
2011 mutex_unlock(&configfs_symlink_mutex);
2012 configfs_detach_group(&group->cg_item);
2013 d_inode(dentry)->i_flags |= S_DEAD;
2015 inode_unlock(d_inode(dentry));
2019 inode_unlock(d_inode(root));
2023 mutex_lock(&configfs_subsystem_mutex);
2024 unlink_group(group);
2025 mutex_unlock(&configfs_subsystem_mutex);
2026 configfs_release_fs();
2029 EXPORT_SYMBOL(configfs_register_subsystem);
2030 EXPORT_SYMBOL(configfs_unregister_subsystem);