1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
5 * dir.c - Operations for configfs directories.
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
10 * configfs Copyright (C) 2005 Oracle. All rights reserved.
16 #include <linux/fsnotify.h>
17 #include <linux/mount.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
22 #include <linux/configfs.h>
23 #include "configfs_internal.h"
25 DECLARE_RWSEM(configfs_rename_sem);
27 * Protects mutations of configfs_dirent linkage together with proper i_mutex
28 * Also protects mutations of symlinks linkage to target configfs_dirent
29 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
30 * and configfs_dirent_lock locked, in that order.
31 * This allows one to safely traverse configfs_dirent trees and symlinks without
32 * having to lock inodes.
34 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
35 * unlocked is not reliable unless in detach_groups() called from
36 * rmdir()/unregister() and from configfs_attach_group()
38 DEFINE_SPINLOCK(configfs_dirent_lock);
40 static void configfs_d_iput(struct dentry * dentry,
43 struct configfs_dirent *sd = dentry->d_fsdata;
46 /* Coordinate with configfs_readdir */
47 spin_lock(&configfs_dirent_lock);
49 * Set sd->s_dentry to null only when this dentry is the one
50 * that is going to be killed. Otherwise configfs_d_iput may
51 * run just after configfs_attach_attr and set sd->s_dentry to
52 * NULL even it's still in use.
54 if (sd->s_dentry == dentry)
57 spin_unlock(&configfs_dirent_lock);
63 const struct dentry_operations configfs_dentry_ops = {
64 .d_iput = configfs_d_iput,
65 .d_delete = always_delete_dentry,
71 * Helpers to make lockdep happy with our recursive locking of default groups'
72 * inodes (see configfs_attach_group() and configfs_detach_group()).
73 * We put default groups i_mutexes in separate classes according to their depth
74 * from the youngest non-default group ancestor.
76 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
77 * groups A/B and A/C will have their inode's mutex in class
78 * default_group_class[0], and default group A/C/D will be in
79 * default_group_class[1].
81 * The lock classes are declared and assigned in inode.c, according to the
83 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
84 * default groups, and reset to -1 when all default groups are attached. During
85 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
86 * inode's mutex is set to default_group_class[s_depth - 1].
89 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
94 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
95 struct configfs_dirent *sd)
97 int parent_depth = parent_sd->s_depth;
99 if (parent_depth >= 0)
100 sd->s_depth = parent_depth + 1;
104 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
107 * item's i_mutex class is already setup, so s_depth is now only
108 * used to set new sub-directories s_depth, which is always done
109 * with item's i_mutex locked.
112 * sd->s_depth == -1 iff we are a non default group.
113 * else (we are a default group) sd->s_depth > 0 (see
116 if (sd->s_depth == -1)
118 * We are a non default group and we are going to create
125 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
127 /* We will not create default groups anymore. */
131 #else /* CONFIG_LOCKDEP */
133 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
137 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
138 struct configfs_dirent *sd)
143 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
148 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
152 #endif /* CONFIG_LOCKDEP */
154 static struct configfs_fragment *new_fragment(void)
156 struct configfs_fragment *p;
158 p = kmalloc(sizeof(struct configfs_fragment), GFP_KERNEL);
160 atomic_set(&p->frag_count, 1);
161 init_rwsem(&p->frag_sem);
162 p->frag_dead = false;
167 void put_fragment(struct configfs_fragment *frag)
169 if (frag && atomic_dec_and_test(&frag->frag_count))
173 struct configfs_fragment *get_fragment(struct configfs_fragment *frag)
176 atomic_inc(&frag->frag_count);
181 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
183 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
184 void *element, int type,
185 struct configfs_fragment *frag)
187 struct configfs_dirent * sd;
189 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
191 return ERR_PTR(-ENOMEM);
193 atomic_set(&sd->s_count, 1);
194 INIT_LIST_HEAD(&sd->s_links);
195 INIT_LIST_HEAD(&sd->s_children);
196 sd->s_element = element;
198 configfs_init_dirent_depth(sd);
199 spin_lock(&configfs_dirent_lock);
200 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
201 spin_unlock(&configfs_dirent_lock);
202 kmem_cache_free(configfs_dir_cachep, sd);
203 return ERR_PTR(-ENOENT);
205 sd->s_frag = get_fragment(frag);
206 list_add(&sd->s_sibling, &parent_sd->s_children);
207 spin_unlock(&configfs_dirent_lock);
214 * Return -EEXIST if there is already a configfs element with the same
215 * name for the same parent.
217 * called with parent inode's i_mutex held
219 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
220 const unsigned char *new)
222 struct configfs_dirent * sd;
224 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
226 const unsigned char *existing = configfs_get_name(sd);
227 if (strcmp(existing, new))
238 int configfs_make_dirent(struct configfs_dirent * parent_sd,
239 struct dentry * dentry, void * element,
240 umode_t mode, int type, struct configfs_fragment *frag)
242 struct configfs_dirent * sd;
244 sd = configfs_new_dirent(parent_sd, element, type, frag);
249 sd->s_dentry = dentry;
251 dentry->d_fsdata = configfs_get(sd);
256 static void init_dir(struct inode * inode)
258 inode->i_op = &configfs_dir_inode_operations;
259 inode->i_fop = &configfs_dir_operations;
261 /* directory inodes start off with i_nlink == 2 (for "." entry) */
265 static void configfs_init_file(struct inode * inode)
267 inode->i_size = PAGE_SIZE;
268 inode->i_fop = &configfs_file_operations;
271 static void configfs_init_bin_file(struct inode *inode)
274 inode->i_fop = &configfs_bin_file_operations;
277 static void init_symlink(struct inode * inode)
279 inode->i_op = &configfs_symlink_inode_operations;
283 * configfs_create_dir - create a directory for an config_item.
284 * @item: config_itemwe're creating directory for.
285 * @dentry: config_item's dentry.
287 * Note: user-created entries won't be allowed under this new directory
288 * until it is validated by configfs_dir_set_ready()
291 static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
292 struct configfs_fragment *frag)
295 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
296 struct dentry *p = dentry->d_parent;
300 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
304 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
305 CONFIGFS_DIR | CONFIGFS_USET_CREATING,
310 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
311 error = configfs_create(dentry, mode, init_dir);
313 inc_nlink(d_inode(p));
314 item->ci_dentry = dentry;
316 struct configfs_dirent *sd = dentry->d_fsdata;
318 spin_lock(&configfs_dirent_lock);
319 list_del_init(&sd->s_sibling);
320 spin_unlock(&configfs_dirent_lock);
328 * Allow userspace to create new entries under a new directory created with
329 * configfs_create_dir(), and under all of its chidlren directories recursively.
330 * @sd configfs_dirent of the new directory to validate
332 * Caller must hold configfs_dirent_lock.
334 static void configfs_dir_set_ready(struct configfs_dirent *sd)
336 struct configfs_dirent *child_sd;
338 sd->s_type &= ~CONFIGFS_USET_CREATING;
339 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
340 if (child_sd->s_type & CONFIGFS_USET_CREATING)
341 configfs_dir_set_ready(child_sd);
345 * Check that a directory does not belong to a directory hierarchy being
346 * attached and not validated yet.
347 * @sd configfs_dirent of the directory to check
349 * @return non-zero iff the directory was validated
351 * Note: takes configfs_dirent_lock, so the result may change from false to true
352 * in two consecutive calls, but never from true to false.
354 int configfs_dirent_is_ready(struct configfs_dirent *sd)
358 spin_lock(&configfs_dirent_lock);
359 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
360 spin_unlock(&configfs_dirent_lock);
365 int configfs_create_link(struct configfs_symlink *sl,
366 struct dentry *parent,
367 struct dentry *dentry)
370 umode_t mode = S_IFLNK | S_IRWXUGO;
371 struct configfs_dirent *p = parent->d_fsdata;
373 err = configfs_make_dirent(p, dentry, sl, mode,
374 CONFIGFS_ITEM_LINK, p->s_frag);
376 err = configfs_create(dentry, mode, init_symlink);
378 struct configfs_dirent *sd = dentry->d_fsdata;
380 spin_lock(&configfs_dirent_lock);
381 list_del_init(&sd->s_sibling);
382 spin_unlock(&configfs_dirent_lock);
390 static void remove_dir(struct dentry * d)
392 struct dentry * parent = dget(d->d_parent);
393 struct configfs_dirent * sd;
396 spin_lock(&configfs_dirent_lock);
397 list_del_init(&sd->s_sibling);
398 spin_unlock(&configfs_dirent_lock);
400 if (d_really_is_positive(d))
401 simple_rmdir(d_inode(parent),d);
403 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
409 * configfs_remove_dir - remove an config_item's directory.
410 * @item: config_item we're removing.
412 * The only thing special about this is that we remove any files in
413 * the directory before we remove the directory, and we've inlined
414 * what used to be configfs_rmdir() below, instead of calling separately.
416 * Caller holds the mutex of the item's inode
419 static void configfs_remove_dir(struct config_item * item)
421 struct dentry * dentry = dget(item->ci_dentry);
428 * Drop reference from dget() on entrance.
434 /* attaches attribute's configfs_dirent to the dentry corresponding to the
437 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
439 struct configfs_attribute * attr = sd->s_element;
442 spin_lock(&configfs_dirent_lock);
443 dentry->d_fsdata = configfs_get(sd);
444 sd->s_dentry = dentry;
445 spin_unlock(&configfs_dirent_lock);
447 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
448 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
449 configfs_init_bin_file :
456 static struct dentry * configfs_lookup(struct inode *dir,
457 struct dentry *dentry,
460 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
461 struct configfs_dirent * sd;
466 * Fake invisibility if dir belongs to a group/default groups hierarchy
469 * This forbids userspace to read/write attributes of items which may
470 * not complete their initialization, since the dentries of the
471 * attributes won't be instantiated.
474 if (!configfs_dirent_is_ready(parent_sd))
477 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
478 if (sd->s_type & CONFIGFS_NOT_PINNED) {
479 const unsigned char * name = configfs_get_name(sd);
481 if (strcmp(name, dentry->d_name.name))
485 err = configfs_attach_attr(sd, dentry);
492 * If it doesn't exist and it isn't a NOT_PINNED item,
493 * it must be negative.
495 if (dentry->d_name.len > NAME_MAX)
496 return ERR_PTR(-ENAMETOOLONG);
506 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
507 * attributes and are removed by rmdir(). We recurse, setting
508 * CONFIGFS_USET_DROPPING on all children that are candidates for
510 * If there is an error, the caller will reset the flags via
511 * configfs_detach_rollback().
513 static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
515 struct configfs_dirent *parent_sd = dentry->d_fsdata;
516 struct configfs_dirent *sd;
519 /* Mark that we're trying to drop the group */
520 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
523 if (!list_empty(&parent_sd->s_links))
527 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
528 if (!sd->s_element ||
529 (sd->s_type & CONFIGFS_NOT_PINNED))
531 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
532 /* Abort if racing with mkdir() */
533 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
535 *wait= dget(sd->s_dentry);
540 * Yup, recursive. If there's a problem, blame
541 * deep nesting of default_groups
543 ret = configfs_detach_prep(sd->s_dentry, wait);
557 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
560 static void configfs_detach_rollback(struct dentry *dentry)
562 struct configfs_dirent *parent_sd = dentry->d_fsdata;
563 struct configfs_dirent *sd;
565 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
567 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
568 if (sd->s_type & CONFIGFS_USET_DEFAULT)
569 configfs_detach_rollback(sd->s_dentry);
572 static void detach_attrs(struct config_item * item)
574 struct dentry * dentry = dget(item->ci_dentry);
575 struct configfs_dirent * parent_sd;
576 struct configfs_dirent * sd, * tmp;
581 pr_debug("configfs %s: dropping attrs for dir\n",
582 dentry->d_name.name);
584 parent_sd = dentry->d_fsdata;
585 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
586 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
588 spin_lock(&configfs_dirent_lock);
589 list_del_init(&sd->s_sibling);
590 spin_unlock(&configfs_dirent_lock);
591 configfs_drop_dentry(sd, dentry);
596 * Drop reference from dget() on entrance.
601 static int populate_attrs(struct config_item *item)
603 const struct config_item_type *t = item->ci_type;
604 struct configfs_attribute *attr;
605 struct configfs_bin_attribute *bin_attr;
612 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
613 if ((error = configfs_create_file(item, attr)))
617 if (t->ct_bin_attrs) {
618 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
619 error = configfs_create_bin_file(item, bin_attr);
631 static int configfs_attach_group(struct config_item *parent_item,
632 struct config_item *item,
633 struct dentry *dentry,
634 struct configfs_fragment *frag);
635 static void configfs_detach_group(struct config_item *item);
637 static void detach_groups(struct config_group *group)
639 struct dentry * dentry = dget(group->cg_item.ci_dentry);
640 struct dentry *child;
641 struct configfs_dirent *parent_sd;
642 struct configfs_dirent *sd, *tmp;
647 parent_sd = dentry->d_fsdata;
648 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
649 if (!sd->s_element ||
650 !(sd->s_type & CONFIGFS_USET_DEFAULT))
653 child = sd->s_dentry;
655 inode_lock(d_inode(child));
657 configfs_detach_group(sd->s_element);
658 d_inode(child)->i_flags |= S_DEAD;
661 inode_unlock(d_inode(child));
668 * Drop reference from dget() on entrance.
674 * This fakes mkdir(2) on a default_groups[] entry. It
675 * creates a dentry, attachs it, and then does fixup
678 * We could, perhaps, tweak our parent's ->mkdir for a minute and
679 * try using vfs_mkdir. Just a thought.
681 static int create_default_group(struct config_group *parent_group,
682 struct config_group *group,
683 struct configfs_fragment *frag)
686 struct configfs_dirent *sd;
687 /* We trust the caller holds a reference to parent */
688 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
690 if (!group->cg_item.ci_name)
691 group->cg_item.ci_name = group->cg_item.ci_namebuf;
694 child = d_alloc_name(parent, group->cg_item.ci_name);
698 ret = configfs_attach_group(&parent_group->cg_item,
699 &group->cg_item, child, frag);
701 sd = child->d_fsdata;
702 sd->s_type |= CONFIGFS_USET_DEFAULT;
704 BUG_ON(d_inode(child));
713 static int populate_groups(struct config_group *group,
714 struct configfs_fragment *frag)
716 struct config_group *new_group;
719 list_for_each_entry(new_group, &group->default_groups, group_entry) {
720 ret = create_default_group(group, new_group, frag);
722 detach_groups(group);
730 void configfs_remove_default_groups(struct config_group *group)
732 struct config_group *g, *n;
734 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
735 list_del(&g->group_entry);
736 config_item_put(&g->cg_item);
739 EXPORT_SYMBOL(configfs_remove_default_groups);
742 * All of link_obj/unlink_obj/link_group/unlink_group require that
743 * subsys->su_mutex is held.
746 static void unlink_obj(struct config_item *item)
748 struct config_group *group;
750 group = item->ci_group;
752 list_del_init(&item->ci_entry);
754 item->ci_group = NULL;
755 item->ci_parent = NULL;
757 /* Drop the reference for ci_entry */
758 config_item_put(item);
760 /* Drop the reference for ci_parent */
761 config_group_put(group);
765 static void link_obj(struct config_item *parent_item, struct config_item *item)
768 * Parent seems redundant with group, but it makes certain
769 * traversals much nicer.
771 item->ci_parent = parent_item;
774 * We hold a reference on the parent for the child's ci_parent
777 item->ci_group = config_group_get(to_config_group(parent_item));
778 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
781 * We hold a reference on the child for ci_entry on the parent's
784 config_item_get(item);
787 static void unlink_group(struct config_group *group)
789 struct config_group *new_group;
791 list_for_each_entry(new_group, &group->default_groups, group_entry)
792 unlink_group(new_group);
794 group->cg_subsys = NULL;
795 unlink_obj(&group->cg_item);
798 static void link_group(struct config_group *parent_group, struct config_group *group)
800 struct config_group *new_group;
801 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
803 link_obj(&parent_group->cg_item, &group->cg_item);
805 if (parent_group->cg_subsys)
806 subsys = parent_group->cg_subsys;
807 else if (configfs_is_root(&parent_group->cg_item))
808 subsys = to_configfs_subsystem(group);
811 group->cg_subsys = subsys;
813 list_for_each_entry(new_group, &group->default_groups, group_entry)
814 link_group(group, new_group);
818 * The goal is that configfs_attach_item() (and
819 * configfs_attach_group()) can be called from either the VFS or this
820 * module. That is, they assume that the items have been created,
821 * the dentry allocated, and the dcache is all ready to go.
823 * If they fail, they must clean up after themselves as if they
824 * had never been called. The caller (VFS or local function) will
825 * handle cleaning up the dcache bits.
827 * configfs_detach_group() and configfs_detach_item() behave similarly on
828 * the way out. They assume that the proper semaphores are held, they
829 * clean up the configfs items, and they expect their callers will
830 * handle the dcache bits.
832 static int configfs_attach_item(struct config_item *parent_item,
833 struct config_item *item,
834 struct dentry *dentry,
835 struct configfs_fragment *frag)
839 ret = configfs_create_dir(item, dentry, frag);
841 ret = populate_attrs(item);
844 * We are going to remove an inode and its dentry but
845 * the VFS may already have hit and used them. Thus,
846 * we must lock them as rmdir() would.
848 inode_lock(d_inode(dentry));
849 configfs_remove_dir(item);
850 d_inode(dentry)->i_flags |= S_DEAD;
852 inode_unlock(d_inode(dentry));
860 /* Caller holds the mutex of the item's inode */
861 static void configfs_detach_item(struct config_item *item)
864 configfs_remove_dir(item);
867 static int configfs_attach_group(struct config_item *parent_item,
868 struct config_item *item,
869 struct dentry *dentry,
870 struct configfs_fragment *frag)
873 struct configfs_dirent *sd;
875 ret = configfs_attach_item(parent_item, item, dentry, frag);
877 sd = dentry->d_fsdata;
878 sd->s_type |= CONFIGFS_USET_DIR;
881 * FYI, we're faking mkdir in populate_groups()
882 * We must lock the group's inode to avoid races with the VFS
883 * which can already hit the inode and try to add/remove entries
886 * We must also lock the inode to remove it safely in case of
887 * error, as rmdir() would.
889 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
890 configfs_adjust_dir_dirent_depth_before_populate(sd);
891 ret = populate_groups(to_config_group(item), frag);
893 configfs_detach_item(item);
894 d_inode(dentry)->i_flags |= S_DEAD;
897 configfs_adjust_dir_dirent_depth_after_populate(sd);
898 inode_unlock(d_inode(dentry));
906 /* Caller holds the mutex of the group's inode */
907 static void configfs_detach_group(struct config_item *item)
909 detach_groups(to_config_group(item));
910 configfs_detach_item(item);
914 * After the item has been detached from the filesystem view, we are
915 * ready to tear it out of the hierarchy. Notify the client before
916 * we do that so they can perform any cleanup that requires
917 * navigating the hierarchy. A client does not need to provide this
918 * callback. The subsystem semaphore MUST be held by the caller, and
919 * references must be valid for both items. It also assumes the
920 * caller has validated ci_type.
922 static void client_disconnect_notify(struct config_item *parent_item,
923 struct config_item *item)
925 const struct config_item_type *type;
927 type = parent_item->ci_type;
930 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
931 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
936 * Drop the initial reference from make_item()/make_group()
937 * This function assumes that reference is held on item
938 * and that item holds a valid reference to the parent. Also, it
939 * assumes the caller has validated ci_type.
941 static void client_drop_item(struct config_item *parent_item,
942 struct config_item *item)
944 const struct config_item_type *type;
946 type = parent_item->ci_type;
950 * If ->drop_item() exists, it is responsible for the
953 if (type->ct_group_ops && type->ct_group_ops->drop_item)
954 type->ct_group_ops->drop_item(to_config_group(parent_item),
957 config_item_put(item);
961 static void configfs_dump_one(struct configfs_dirent *sd, int level)
963 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
965 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
966 type_print(CONFIGFS_ROOT);
967 type_print(CONFIGFS_DIR);
968 type_print(CONFIGFS_ITEM_ATTR);
969 type_print(CONFIGFS_ITEM_LINK);
970 type_print(CONFIGFS_USET_DIR);
971 type_print(CONFIGFS_USET_DEFAULT);
972 type_print(CONFIGFS_USET_DROPPING);
976 static int configfs_dump(struct configfs_dirent *sd, int level)
978 struct configfs_dirent *child_sd;
981 configfs_dump_one(sd, level);
983 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
986 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
987 ret = configfs_dump(child_sd, level + 2);
998 * configfs_depend_item() and configfs_undepend_item()
1000 * WARNING: Do not call these from a configfs callback!
1002 * This describes these functions and their helpers.
1004 * Allow another kernel system to depend on a config_item. If this
1005 * happens, the item cannot go away until the dependent can live without
1006 * it. The idea is to give client modules as simple an interface as
1007 * possible. When a system asks them to depend on an item, they just
1008 * call configfs_depend_item(). If the item is live and the client
1009 * driver is in good shape, we'll happily do the work for them.
1011 * Why is the locking complex? Because configfs uses the VFS to handle
1012 * all locking, but this function is called outside the normal
1013 * VFS->configfs path. So it must take VFS locks to prevent the
1014 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1015 * why you can't call these functions underneath configfs callbacks.
1017 * Note, btw, that this can be called at *any* time, even when a configfs
1018 * subsystem isn't registered, or when configfs is loading or unloading.
1019 * Just like configfs_register_subsystem(). So we take the same
1020 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1021 * If we can find the target item in the
1022 * configfs tree, it must be part of the subsystem tree as well, so we
1023 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1024 * locking out mkdir() and rmdir(), who might be racing us.
1028 * configfs_depend_prep()
1030 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1031 * attributes. This is similar but not the same to configfs_detach_prep().
1032 * Note that configfs_detach_prep() expects the parent to be locked when it
1033 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1034 * do that so we can unlock it if we find nothing.
1036 * Here we do a depth-first search of the dentry hierarchy looking for
1038 * We deliberately ignore items tagged as dropping since they are virtually
1039 * dead, as well as items in the middle of attachment since they virtually
1040 * do not exist yet. This completes the locking out of racing mkdir() and
1042 * Note: subdirectories in the middle of attachment start with s_type =
1043 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1044 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1045 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1047 * If the target is not found, -ENOENT is bubbled up.
1049 * This adds a requirement that all config_items be unique!
1051 * This is recursive. There isn't
1052 * much on the stack, though, so folks that need this function - be careful
1053 * about your stack! Patches will be accepted to make it iterative.
1055 static int configfs_depend_prep(struct dentry *origin,
1056 struct config_item *target)
1058 struct configfs_dirent *child_sd, *sd;
1061 BUG_ON(!origin || !origin->d_fsdata);
1062 sd = origin->d_fsdata;
1064 if (sd->s_element == target) /* Boo-yah */
1067 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1068 if ((child_sd->s_type & CONFIGFS_DIR) &&
1069 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1070 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1071 ret = configfs_depend_prep(child_sd->s_dentry,
1074 goto out; /* Child path boo-yah */
1078 /* We looped all our children and didn't find target */
1085 static int configfs_do_depend_item(struct dentry *subsys_dentry,
1086 struct config_item *target)
1088 struct configfs_dirent *p;
1091 spin_lock(&configfs_dirent_lock);
1092 /* Scan the tree, return 0 if found */
1093 ret = configfs_depend_prep(subsys_dentry, target);
1095 goto out_unlock_dirent_lock;
1098 * We are sure that the item is not about to be removed by rmdir(), and
1099 * not in the middle of attachment by mkdir().
1101 p = target->ci_dentry->d_fsdata;
1102 p->s_dependent_count += 1;
1104 out_unlock_dirent_lock:
1105 spin_unlock(&configfs_dirent_lock);
1110 static inline struct configfs_dirent *
1111 configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1112 struct config_item *subsys_item)
1114 struct configfs_dirent *p;
1115 struct configfs_dirent *ret = NULL;
1117 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1118 if (p->s_type & CONFIGFS_DIR &&
1119 p->s_element == subsys_item) {
1129 int configfs_depend_item(struct configfs_subsystem *subsys,
1130 struct config_item *target)
1133 struct configfs_dirent *subsys_sd;
1134 struct config_item *s_item = &subsys->su_group.cg_item;
1135 struct dentry *root;
1138 * Pin the configfs filesystem. This means we can safely access
1139 * the root of the configfs filesystem.
1141 root = configfs_pin_fs();
1143 return PTR_ERR(root);
1146 * Next, lock the root directory. We're going to check that the
1147 * subsystem is really registered, and so we need to lock out
1148 * configfs_[un]register_subsystem().
1150 inode_lock(d_inode(root));
1152 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
1158 /* Ok, now we can trust subsys/s_item */
1159 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1162 inode_unlock(d_inode(root));
1165 * If we succeeded, the fs is pinned via other methods. If not,
1166 * we're done with it anyway. So release_fs() is always right.
1168 configfs_release_fs();
1172 EXPORT_SYMBOL(configfs_depend_item);
1175 * Release the dependent linkage. This is much simpler than
1176 * configfs_depend_item() because we know that that the client driver is
1177 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1179 void configfs_undepend_item(struct config_item *target)
1181 struct configfs_dirent *sd;
1184 * Since we can trust everything is pinned, we just need
1185 * configfs_dirent_lock.
1187 spin_lock(&configfs_dirent_lock);
1189 sd = target->ci_dentry->d_fsdata;
1190 BUG_ON(sd->s_dependent_count < 1);
1192 sd->s_dependent_count -= 1;
1195 * After this unlock, we cannot trust the item to stay alive!
1196 * DO NOT REFERENCE item after this unlock.
1198 spin_unlock(&configfs_dirent_lock);
1200 EXPORT_SYMBOL(configfs_undepend_item);
1203 * caller_subsys is a caller's subsystem not target's. This is used to
1204 * determine if we should lock root and check subsys or not. When we are
1205 * in the same subsystem as our target there is no need to do locking as
1206 * we know that subsys is valid and is not unregistered during this function
1207 * as we are called from callback of one of his children and VFS holds a lock
1208 * on some inode. Otherwise we have to lock our root to ensure that target's
1209 * subsystem it is not unregistered during this function.
1211 int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1212 struct config_item *target)
1214 struct configfs_subsystem *target_subsys;
1215 struct config_group *root, *parent;
1216 struct configfs_dirent *subsys_sd;
1219 /* Disallow this function for configfs root */
1220 if (configfs_is_root(target))
1223 parent = target->ci_group;
1225 * This may happen when someone is trying to depend root
1226 * directory of some subsystem
1228 if (configfs_is_root(&parent->cg_item)) {
1229 target_subsys = to_configfs_subsystem(to_config_group(target));
1232 target_subsys = parent->cg_subsys;
1233 /* Find a cofnigfs root as we may need it for locking */
1234 for (root = parent; !configfs_is_root(&root->cg_item);
1235 root = root->cg_item.ci_group)
1239 if (target_subsys != caller_subsys) {
1241 * We are in other configfs subsystem, so we have to do
1242 * additional locking to prevent other subsystem from being
1245 inode_lock(d_inode(root->cg_item.ci_dentry));
1248 * As we are trying to depend item from other subsystem
1249 * we have to check if this subsystem is still registered
1251 subsys_sd = configfs_find_subsys_dentry(
1252 root->cg_item.ci_dentry->d_fsdata,
1253 &target_subsys->su_group.cg_item);
1255 goto out_root_unlock;
1257 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1260 /* Now we can execute core of depend item */
1261 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1263 if (target_subsys != caller_subsys)
1266 * We were called from subsystem other than our target so we
1267 * took some locks so now it's time to release them
1269 inode_unlock(d_inode(root->cg_item.ci_dentry));
1273 EXPORT_SYMBOL(configfs_depend_item_unlocked);
1275 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1279 struct config_group *group = NULL;
1280 struct config_item *item = NULL;
1281 struct config_item *parent_item;
1282 struct configfs_subsystem *subsys;
1283 struct configfs_dirent *sd;
1284 const struct config_item_type *type;
1285 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1286 struct configfs_fragment *frag;
1289 sd = dentry->d_parent->d_fsdata;
1292 * Fake invisibility if dir belongs to a group/default groups hierarchy
1295 if (!configfs_dirent_is_ready(sd)) {
1300 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1305 frag = new_fragment();
1311 /* Get a working ref for the duration of this function */
1312 parent_item = configfs_get_config_item(dentry->d_parent);
1313 type = parent_item->ci_type;
1314 subsys = to_config_group(parent_item)->cg_subsys;
1317 if (!type || !type->ct_group_ops ||
1318 (!type->ct_group_ops->make_group &&
1319 !type->ct_group_ops->make_item)) {
1320 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1325 * The subsystem may belong to a different module than the item
1326 * being created. We don't want to safely pin the new item but
1327 * fail to pin the subsystem it sits under.
1329 if (!subsys->su_group.cg_item.ci_type) {
1333 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1334 if (!try_module_get(subsys_owner)) {
1339 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1342 goto out_subsys_put;
1345 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1347 mutex_lock(&subsys->su_mutex);
1348 if (type->ct_group_ops->make_group) {
1349 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1351 group = ERR_PTR(-ENOMEM);
1352 if (!IS_ERR(group)) {
1353 link_group(to_config_group(parent_item), group);
1354 item = &group->cg_item;
1356 ret = PTR_ERR(group);
1358 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1360 item = ERR_PTR(-ENOMEM);
1362 link_obj(parent_item, item);
1364 ret = PTR_ERR(item);
1366 mutex_unlock(&subsys->su_mutex);
1371 * If ret != 0, then link_obj() was never called.
1372 * There are no extra references to clean up.
1374 goto out_subsys_put;
1378 * link_obj() has been called (via link_group() for groups).
1379 * From here on out, errors must clean that up.
1382 type = item->ci_type;
1388 new_item_owner = type->ct_owner;
1389 if (!try_module_get(new_item_owner)) {
1395 * I hate doing it this way, but if there is
1396 * an error, module_put() probably should
1397 * happen after any cleanup.
1402 * Make racing rmdir() fail if it did not tag parent with
1403 * CONFIGFS_USET_DROPPING
1404 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1405 * fail and let rmdir() terminate correctly
1407 spin_lock(&configfs_dirent_lock);
1408 /* This will make configfs_detach_prep() fail */
1409 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1410 spin_unlock(&configfs_dirent_lock);
1413 ret = configfs_attach_group(parent_item, item, dentry, frag);
1415 ret = configfs_attach_item(parent_item, item, dentry, frag);
1417 spin_lock(&configfs_dirent_lock);
1418 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1420 configfs_dir_set_ready(dentry->d_fsdata);
1421 spin_unlock(&configfs_dirent_lock);
1425 /* Tear down everything we built up */
1426 mutex_lock(&subsys->su_mutex);
1428 client_disconnect_notify(parent_item, item);
1430 unlink_group(group);
1433 client_drop_item(parent_item, item);
1435 mutex_unlock(&subsys->su_mutex);
1438 module_put(new_item_owner);
1443 module_put(subsys_owner);
1447 * link_obj()/link_group() took a reference from child->parent,
1448 * so the parent is safely pinned. We can drop our working
1451 config_item_put(parent_item);
1458 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1460 struct config_item *parent_item;
1461 struct config_item *item;
1462 struct configfs_subsystem *subsys;
1463 struct configfs_dirent *sd;
1464 struct configfs_fragment *frag;
1465 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1468 sd = dentry->d_fsdata;
1469 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1472 /* Get a working ref until we have the child */
1473 parent_item = configfs_get_config_item(dentry->d_parent);
1474 subsys = to_config_group(parent_item)->cg_subsys;
1477 if (!parent_item->ci_type) {
1478 config_item_put(parent_item);
1482 /* configfs_mkdir() shouldn't have allowed this */
1483 BUG_ON(!subsys->su_group.cg_item.ci_type);
1484 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1487 * Ensure that no racing symlink() will make detach_prep() fail while
1488 * the new link is temporarily attached
1491 struct dentry *wait;
1493 mutex_lock(&configfs_symlink_mutex);
1494 spin_lock(&configfs_dirent_lock);
1496 * Here's where we check for dependents. We're protected by
1497 * configfs_dirent_lock.
1498 * If no dependent, atomically tag the item as dropping.
1500 ret = sd->s_dependent_count ? -EBUSY : 0;
1502 ret = configfs_detach_prep(dentry, &wait);
1504 configfs_detach_rollback(dentry);
1506 spin_unlock(&configfs_dirent_lock);
1507 mutex_unlock(&configfs_symlink_mutex);
1510 if (ret != -EAGAIN) {
1511 config_item_put(parent_item);
1515 /* Wait until the racing operation terminates */
1516 inode_lock(d_inode(wait));
1517 inode_unlock(d_inode(wait));
1520 } while (ret == -EAGAIN);
1523 if (down_write_killable(&frag->frag_sem)) {
1524 spin_lock(&configfs_dirent_lock);
1525 configfs_detach_rollback(dentry);
1526 spin_unlock(&configfs_dirent_lock);
1529 frag->frag_dead = true;
1530 up_write(&frag->frag_sem);
1532 /* Get a working ref for the duration of this function */
1533 item = configfs_get_config_item(dentry);
1535 /* Drop reference from above, item already holds one. */
1536 config_item_put(parent_item);
1539 dead_item_owner = item->ci_type->ct_owner;
1541 if (sd->s_type & CONFIGFS_USET_DIR) {
1542 configfs_detach_group(item);
1544 mutex_lock(&subsys->su_mutex);
1545 client_disconnect_notify(parent_item, item);
1546 unlink_group(to_config_group(item));
1548 configfs_detach_item(item);
1550 mutex_lock(&subsys->su_mutex);
1551 client_disconnect_notify(parent_item, item);
1555 client_drop_item(parent_item, item);
1556 mutex_unlock(&subsys->su_mutex);
1558 /* Drop our reference from above */
1559 config_item_put(item);
1561 module_put(dead_item_owner);
1562 module_put(subsys_owner);
1567 const struct inode_operations configfs_dir_inode_operations = {
1568 .mkdir = configfs_mkdir,
1569 .rmdir = configfs_rmdir,
1570 .symlink = configfs_symlink,
1571 .unlink = configfs_unlink,
1572 .lookup = configfs_lookup,
1573 .setattr = configfs_setattr,
1576 const struct inode_operations configfs_root_inode_operations = {
1577 .lookup = configfs_lookup,
1578 .setattr = configfs_setattr,
1582 int configfs_rename_dir(struct config_item * item, const char *new_name)
1585 struct dentry * new_dentry, * parent;
1587 if (!strcmp(config_item_name(item), new_name))
1593 down_write(&configfs_rename_sem);
1594 parent = item->parent->dentry;
1596 inode_lock(d_inode(parent));
1598 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1599 if (!IS_ERR(new_dentry)) {
1600 if (d_really_is_negative(new_dentry)) {
1601 error = config_item_set_name(item, "%s", new_name);
1603 d_add(new_dentry, NULL);
1604 d_move(item->dentry, new_dentry);
1607 d_delete(new_dentry);
1612 inode_unlock(d_inode(parent));
1613 up_write(&configfs_rename_sem);
1619 static int configfs_dir_open(struct inode *inode, struct file *file)
1621 struct dentry * dentry = file->f_path.dentry;
1622 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1625 inode_lock(d_inode(dentry));
1627 * Fake invisibility if dir belongs to a group/default groups hierarchy
1631 if (configfs_dirent_is_ready(parent_sd)) {
1632 file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
1633 if (IS_ERR(file->private_data))
1634 err = PTR_ERR(file->private_data);
1638 inode_unlock(d_inode(dentry));
1643 static int configfs_dir_close(struct inode *inode, struct file *file)
1645 struct dentry * dentry = file->f_path.dentry;
1646 struct configfs_dirent * cursor = file->private_data;
1648 inode_lock(d_inode(dentry));
1649 spin_lock(&configfs_dirent_lock);
1650 list_del_init(&cursor->s_sibling);
1651 spin_unlock(&configfs_dirent_lock);
1652 inode_unlock(d_inode(dentry));
1654 release_configfs_dirent(cursor);
1659 /* Relationship between s_mode and the DT_xxx types */
1660 static inline unsigned char dt_type(struct configfs_dirent *sd)
1662 return (sd->s_mode >> 12) & 15;
1665 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1667 struct dentry *dentry = file->f_path.dentry;
1668 struct super_block *sb = dentry->d_sb;
1669 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1670 struct configfs_dirent *cursor = file->private_data;
1671 struct list_head *p, *q = &cursor->s_sibling;
1674 if (!dir_emit_dots(file, ctx))
1676 spin_lock(&configfs_dirent_lock);
1678 list_move(q, &parent_sd->s_children);
1679 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1680 struct configfs_dirent *next;
1683 struct inode *inode = NULL;
1685 next = list_entry(p, struct configfs_dirent, s_sibling);
1686 if (!next->s_element)
1690 * We'll have a dentry and an inode for
1691 * PINNED items and for open attribute
1692 * files. We lock here to prevent a race
1693 * with configfs_d_iput() clearing
1694 * s_dentry before calling iput().
1696 * Why do we go to the trouble? If
1697 * someone has an attribute file open,
1698 * the inode number should match until
1699 * they close it. Beyond that, we don't
1702 dentry = next->s_dentry;
1704 inode = d_inode(dentry);
1707 spin_unlock(&configfs_dirent_lock);
1709 ino = iunique(sb, 2);
1711 name = configfs_get_name(next);
1714 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1717 spin_lock(&configfs_dirent_lock);
1722 spin_unlock(&configfs_dirent_lock);
1726 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1728 struct dentry * dentry = file->f_path.dentry;
1732 offset += file->f_pos;
1741 if (offset != file->f_pos) {
1742 file->f_pos = offset;
1743 if (file->f_pos >= 2) {
1744 struct configfs_dirent *sd = dentry->d_fsdata;
1745 struct configfs_dirent *cursor = file->private_data;
1746 struct list_head *p;
1747 loff_t n = file->f_pos - 2;
1749 spin_lock(&configfs_dirent_lock);
1750 list_del(&cursor->s_sibling);
1751 p = sd->s_children.next;
1752 while (n && p != &sd->s_children) {
1753 struct configfs_dirent *next;
1754 next = list_entry(p, struct configfs_dirent,
1756 if (next->s_element)
1760 list_add_tail(&cursor->s_sibling, p);
1761 spin_unlock(&configfs_dirent_lock);
1767 const struct file_operations configfs_dir_operations = {
1768 .open = configfs_dir_open,
1769 .release = configfs_dir_close,
1770 .llseek = configfs_dir_lseek,
1771 .read = generic_read_dir,
1772 .iterate_shared = configfs_readdir,
1776 * configfs_register_group - creates a parent-child relation between two groups
1777 * @parent_group: parent group
1778 * @group: child group
1780 * link groups, creates dentry for the child and attaches it to the
1783 * Return: 0 on success, negative errno code on error
1785 int configfs_register_group(struct config_group *parent_group,
1786 struct config_group *group)
1788 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1789 struct dentry *parent;
1790 struct configfs_fragment *frag;
1793 frag = new_fragment();
1797 mutex_lock(&subsys->su_mutex);
1798 link_group(parent_group, group);
1799 mutex_unlock(&subsys->su_mutex);
1801 parent = parent_group->cg_item.ci_dentry;
1803 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1804 ret = create_default_group(parent_group, group, frag);
1808 spin_lock(&configfs_dirent_lock);
1809 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1810 spin_unlock(&configfs_dirent_lock);
1811 inode_unlock(d_inode(parent));
1815 inode_unlock(d_inode(parent));
1816 mutex_lock(&subsys->su_mutex);
1817 unlink_group(group);
1818 mutex_unlock(&subsys->su_mutex);
1822 EXPORT_SYMBOL(configfs_register_group);
1825 * configfs_unregister_group() - unregisters a child group from its parent
1826 * @group: parent group to be unregistered
1828 * Undoes configfs_register_group()
1830 void configfs_unregister_group(struct config_group *group)
1832 struct configfs_subsystem *subsys = group->cg_subsys;
1833 struct dentry *dentry = group->cg_item.ci_dentry;
1834 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1835 struct configfs_dirent *sd = dentry->d_fsdata;
1836 struct configfs_fragment *frag = sd->s_frag;
1838 down_write(&frag->frag_sem);
1839 frag->frag_dead = true;
1840 up_write(&frag->frag_sem);
1842 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1843 spin_lock(&configfs_dirent_lock);
1844 configfs_detach_prep(dentry, NULL);
1845 spin_unlock(&configfs_dirent_lock);
1847 configfs_detach_group(&group->cg_item);
1848 d_inode(dentry)->i_flags |= S_DEAD;
1850 fsnotify_rmdir(d_inode(parent), dentry);
1852 inode_unlock(d_inode(parent));
1856 mutex_lock(&subsys->su_mutex);
1857 unlink_group(group);
1858 mutex_unlock(&subsys->su_mutex);
1860 EXPORT_SYMBOL(configfs_unregister_group);
1863 * configfs_register_default_group() - allocates and registers a child group
1864 * @parent_group: parent group
1865 * @name: child group name
1866 * @item_type: child item type description
1868 * boilerplate to allocate and register a child group with its parent. We need
1869 * kzalloc'ed memory because child's default_group is initially empty.
1871 * Return: allocated config group or ERR_PTR() on error
1873 struct config_group *
1874 configfs_register_default_group(struct config_group *parent_group,
1876 const struct config_item_type *item_type)
1879 struct config_group *group;
1881 group = kzalloc(sizeof(*group), GFP_KERNEL);
1883 return ERR_PTR(-ENOMEM);
1884 config_group_init_type_name(group, name, item_type);
1886 ret = configfs_register_group(parent_group, group);
1889 return ERR_PTR(ret);
1893 EXPORT_SYMBOL(configfs_register_default_group);
1896 * configfs_unregister_default_group() - unregisters and frees a child group
1897 * @group: the group to act on
1899 void configfs_unregister_default_group(struct config_group *group)
1901 configfs_unregister_group(group);
1904 EXPORT_SYMBOL(configfs_unregister_default_group);
1906 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1909 struct config_group *group = &subsys->su_group;
1910 struct dentry *dentry;
1911 struct dentry *root;
1912 struct configfs_dirent *sd;
1913 struct configfs_fragment *frag;
1915 frag = new_fragment();
1919 root = configfs_pin_fs();
1922 return PTR_ERR(root);
1925 if (!group->cg_item.ci_name)
1926 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1928 sd = root->d_fsdata;
1929 link_group(to_config_group(sd->s_element), group);
1931 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
1934 dentry = d_alloc_name(root, group->cg_item.ci_name);
1936 d_add(dentry, NULL);
1938 err = configfs_attach_group(sd->s_element, &group->cg_item,
1941 BUG_ON(d_inode(dentry));
1945 spin_lock(&configfs_dirent_lock);
1946 configfs_dir_set_ready(dentry->d_fsdata);
1947 spin_unlock(&configfs_dirent_lock);
1951 inode_unlock(d_inode(root));
1954 unlink_group(group);
1955 configfs_release_fs();
1962 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1964 struct config_group *group = &subsys->su_group;
1965 struct dentry *dentry = group->cg_item.ci_dentry;
1966 struct dentry *root = dentry->d_sb->s_root;
1967 struct configfs_dirent *sd = dentry->d_fsdata;
1968 struct configfs_fragment *frag = sd->s_frag;
1970 if (dentry->d_parent != root) {
1971 pr_err("Tried to unregister non-subsystem!\n");
1975 down_write(&frag->frag_sem);
1976 frag->frag_dead = true;
1977 up_write(&frag->frag_sem);
1979 inode_lock_nested(d_inode(root),
1981 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
1982 mutex_lock(&configfs_symlink_mutex);
1983 spin_lock(&configfs_dirent_lock);
1984 if (configfs_detach_prep(dentry, NULL)) {
1985 pr_err("Tried to unregister non-empty subsystem!\n");
1987 spin_unlock(&configfs_dirent_lock);
1988 mutex_unlock(&configfs_symlink_mutex);
1989 configfs_detach_group(&group->cg_item);
1990 d_inode(dentry)->i_flags |= S_DEAD;
1992 fsnotify_rmdir(d_inode(root), dentry);
1993 inode_unlock(d_inode(dentry));
1997 inode_unlock(d_inode(root));
2001 unlink_group(group);
2002 configfs_release_fs();
2005 EXPORT_SYMBOL(configfs_register_subsystem);
2006 EXPORT_SYMBOL(configfs_unregister_subsystem);