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/mount.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
21 #include <linux/configfs.h>
22 #include "configfs_internal.h"
24 DECLARE_RWSEM(configfs_rename_sem);
26 * Protects mutations of configfs_dirent linkage together with proper i_mutex
27 * Also protects mutations of symlinks linkage to target configfs_dirent
28 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
29 * and configfs_dirent_lock locked, in that order.
30 * This allows one to safely traverse configfs_dirent trees and symlinks without
31 * having to lock inodes.
33 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
34 * unlocked is not reliable unless in detach_groups() called from
35 * rmdir()/unregister() and from configfs_attach_group()
37 DEFINE_SPINLOCK(configfs_dirent_lock);
39 static void configfs_d_iput(struct dentry * dentry,
42 struct configfs_dirent *sd = dentry->d_fsdata;
45 /* Coordinate with configfs_readdir */
46 spin_lock(&configfs_dirent_lock);
47 /* Coordinate with configfs_attach_attr where will increase
48 * sd->s_count and update sd->s_dentry to new allocated one.
49 * Only set sd->dentry to null when this dentry is the only
51 * If not do so, configfs_d_iput may run just after
52 * configfs_attach_attr and set sd->s_dentry to null
53 * even it's still in use.
55 if (atomic_read(&sd->s_count) <= 2)
58 spin_unlock(&configfs_dirent_lock);
64 const struct dentry_operations configfs_dentry_ops = {
65 .d_iput = configfs_d_iput,
66 .d_delete = always_delete_dentry,
72 * Helpers to make lockdep happy with our recursive locking of default groups'
73 * inodes (see configfs_attach_group() and configfs_detach_group()).
74 * We put default groups i_mutexes in separate classes according to their depth
75 * from the youngest non-default group ancestor.
77 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
78 * groups A/B and A/C will have their inode's mutex in class
79 * default_group_class[0], and default group A/C/D will be in
80 * default_group_class[1].
82 * The lock classes are declared and assigned in inode.c, according to the
84 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
85 * default groups, and reset to -1 when all default groups are attached. During
86 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
87 * inode's mutex is set to default_group_class[s_depth - 1].
90 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
95 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
96 struct configfs_dirent *sd)
98 int parent_depth = parent_sd->s_depth;
100 if (parent_depth >= 0)
101 sd->s_depth = parent_depth + 1;
105 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
108 * item's i_mutex class is already setup, so s_depth is now only
109 * used to set new sub-directories s_depth, which is always done
110 * with item's i_mutex locked.
113 * sd->s_depth == -1 iff we are a non default group.
114 * else (we are a default group) sd->s_depth > 0 (see
117 if (sd->s_depth == -1)
119 * We are a non default group and we are going to create
126 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
128 /* We will not create default groups anymore. */
132 #else /* CONFIG_LOCKDEP */
134 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
138 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
139 struct configfs_dirent *sd)
144 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
149 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
153 #endif /* CONFIG_LOCKDEP */
156 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
158 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
159 void *element, int type)
161 struct configfs_dirent * sd;
163 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
165 return ERR_PTR(-ENOMEM);
167 atomic_set(&sd->s_count, 1);
168 INIT_LIST_HEAD(&sd->s_links);
169 INIT_LIST_HEAD(&sd->s_children);
170 sd->s_element = element;
172 configfs_init_dirent_depth(sd);
173 spin_lock(&configfs_dirent_lock);
174 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
175 spin_unlock(&configfs_dirent_lock);
176 kmem_cache_free(configfs_dir_cachep, sd);
177 return ERR_PTR(-ENOENT);
179 list_add(&sd->s_sibling, &parent_sd->s_children);
180 spin_unlock(&configfs_dirent_lock);
187 * Return -EEXIST if there is already a configfs element with the same
188 * name for the same parent.
190 * called with parent inode's i_mutex held
192 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
193 const unsigned char *new)
195 struct configfs_dirent * sd;
197 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
199 const unsigned char *existing = configfs_get_name(sd);
200 if (strcmp(existing, new))
211 int configfs_make_dirent(struct configfs_dirent * parent_sd,
212 struct dentry * dentry, void * element,
213 umode_t mode, int type)
215 struct configfs_dirent * sd;
217 sd = configfs_new_dirent(parent_sd, element, type);
222 sd->s_dentry = dentry;
224 dentry->d_fsdata = configfs_get(sd);
229 static void init_dir(struct inode * inode)
231 inode->i_op = &configfs_dir_inode_operations;
232 inode->i_fop = &configfs_dir_operations;
234 /* directory inodes start off with i_nlink == 2 (for "." entry) */
238 static void configfs_init_file(struct inode * inode)
240 inode->i_size = PAGE_SIZE;
241 inode->i_fop = &configfs_file_operations;
244 static void configfs_init_bin_file(struct inode *inode)
247 inode->i_fop = &configfs_bin_file_operations;
250 static void init_symlink(struct inode * inode)
252 inode->i_op = &configfs_symlink_inode_operations;
256 * configfs_create_dir - create a directory for an config_item.
257 * @item: config_itemwe're creating directory for.
258 * @dentry: config_item's dentry.
260 * Note: user-created entries won't be allowed under this new directory
261 * until it is validated by configfs_dir_set_ready()
264 static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
267 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
268 struct dentry *p = dentry->d_parent;
272 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
276 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
277 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
281 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
282 error = configfs_create(dentry, mode, init_dir);
284 inc_nlink(d_inode(p));
285 item->ci_dentry = dentry;
287 struct configfs_dirent *sd = dentry->d_fsdata;
289 spin_lock(&configfs_dirent_lock);
290 list_del_init(&sd->s_sibling);
291 spin_unlock(&configfs_dirent_lock);
299 * Allow userspace to create new entries under a new directory created with
300 * configfs_create_dir(), and under all of its chidlren directories recursively.
301 * @sd configfs_dirent of the new directory to validate
303 * Caller must hold configfs_dirent_lock.
305 static void configfs_dir_set_ready(struct configfs_dirent *sd)
307 struct configfs_dirent *child_sd;
309 sd->s_type &= ~CONFIGFS_USET_CREATING;
310 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
311 if (child_sd->s_type & CONFIGFS_USET_CREATING)
312 configfs_dir_set_ready(child_sd);
316 * Check that a directory does not belong to a directory hierarchy being
317 * attached and not validated yet.
318 * @sd configfs_dirent of the directory to check
320 * @return non-zero iff the directory was validated
322 * Note: takes configfs_dirent_lock, so the result may change from false to true
323 * in two consecutive calls, but never from true to false.
325 int configfs_dirent_is_ready(struct configfs_dirent *sd)
329 spin_lock(&configfs_dirent_lock);
330 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
331 spin_unlock(&configfs_dirent_lock);
336 int configfs_create_link(struct configfs_symlink *sl,
337 struct dentry *parent,
338 struct dentry *dentry)
341 umode_t mode = S_IFLNK | S_IRWXUGO;
343 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
346 err = configfs_create(dentry, mode, init_symlink);
348 struct configfs_dirent *sd = dentry->d_fsdata;
350 spin_lock(&configfs_dirent_lock);
351 list_del_init(&sd->s_sibling);
352 spin_unlock(&configfs_dirent_lock);
360 static void remove_dir(struct dentry * d)
362 struct dentry * parent = dget(d->d_parent);
363 struct configfs_dirent * sd;
366 spin_lock(&configfs_dirent_lock);
367 list_del_init(&sd->s_sibling);
368 spin_unlock(&configfs_dirent_lock);
370 if (d_really_is_positive(d))
371 simple_rmdir(d_inode(parent),d);
373 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
379 * configfs_remove_dir - remove an config_item's directory.
380 * @item: config_item we're removing.
382 * The only thing special about this is that we remove any files in
383 * the directory before we remove the directory, and we've inlined
384 * what used to be configfs_rmdir() below, instead of calling separately.
386 * Caller holds the mutex of the item's inode
389 static void configfs_remove_dir(struct config_item * item)
391 struct dentry * dentry = dget(item->ci_dentry);
398 * Drop reference from dget() on entrance.
404 /* attaches attribute's configfs_dirent to the dentry corresponding to the
407 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
409 struct configfs_attribute * attr = sd->s_element;
412 spin_lock(&configfs_dirent_lock);
413 dentry->d_fsdata = configfs_get(sd);
414 sd->s_dentry = dentry;
415 spin_unlock(&configfs_dirent_lock);
417 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
418 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
419 configfs_init_bin_file :
426 static struct dentry * configfs_lookup(struct inode *dir,
427 struct dentry *dentry,
430 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
431 struct configfs_dirent * sd;
436 * Fake invisibility if dir belongs to a group/default groups hierarchy
439 * This forbids userspace to read/write attributes of items which may
440 * not complete their initialization, since the dentries of the
441 * attributes won't be instantiated.
444 if (!configfs_dirent_is_ready(parent_sd))
447 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
448 if (sd->s_type & CONFIGFS_NOT_PINNED) {
449 const unsigned char * name = configfs_get_name(sd);
451 if (strcmp(name, dentry->d_name.name))
455 err = configfs_attach_attr(sd, dentry);
462 * If it doesn't exist and it isn't a NOT_PINNED item,
463 * it must be negative.
465 if (dentry->d_name.len > NAME_MAX)
466 return ERR_PTR(-ENAMETOOLONG);
476 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
477 * attributes and are removed by rmdir(). We recurse, setting
478 * CONFIGFS_USET_DROPPING on all children that are candidates for
480 * If there is an error, the caller will reset the flags via
481 * configfs_detach_rollback().
483 static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
485 struct configfs_dirent *parent_sd = dentry->d_fsdata;
486 struct configfs_dirent *sd;
489 /* Mark that we're trying to drop the group */
490 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
493 if (!list_empty(&parent_sd->s_links))
497 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
498 if (!sd->s_element ||
499 (sd->s_type & CONFIGFS_NOT_PINNED))
501 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
502 /* Abort if racing with mkdir() */
503 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
505 *wait= dget(sd->s_dentry);
510 * Yup, recursive. If there's a problem, blame
511 * deep nesting of default_groups
513 ret = configfs_detach_prep(sd->s_dentry, wait);
527 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
530 static void configfs_detach_rollback(struct dentry *dentry)
532 struct configfs_dirent *parent_sd = dentry->d_fsdata;
533 struct configfs_dirent *sd;
535 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
537 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
538 if (sd->s_type & CONFIGFS_USET_DEFAULT)
539 configfs_detach_rollback(sd->s_dentry);
542 static void detach_attrs(struct config_item * item)
544 struct dentry * dentry = dget(item->ci_dentry);
545 struct configfs_dirent * parent_sd;
546 struct configfs_dirent * sd, * tmp;
551 pr_debug("configfs %s: dropping attrs for dir\n",
552 dentry->d_name.name);
554 parent_sd = dentry->d_fsdata;
555 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
556 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
558 spin_lock(&configfs_dirent_lock);
559 list_del_init(&sd->s_sibling);
560 spin_unlock(&configfs_dirent_lock);
561 configfs_drop_dentry(sd, dentry);
566 * Drop reference from dget() on entrance.
571 static int populate_attrs(struct config_item *item)
573 const struct config_item_type *t = item->ci_type;
574 struct configfs_attribute *attr;
575 struct configfs_bin_attribute *bin_attr;
582 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
583 if ((error = configfs_create_file(item, attr)))
587 if (t->ct_bin_attrs) {
588 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
589 error = configfs_create_bin_file(item, bin_attr);
601 static int configfs_attach_group(struct config_item *parent_item,
602 struct config_item *item,
603 struct dentry *dentry);
604 static void configfs_detach_group(struct config_item *item);
606 static void detach_groups(struct config_group *group)
608 struct dentry * dentry = dget(group->cg_item.ci_dentry);
609 struct dentry *child;
610 struct configfs_dirent *parent_sd;
611 struct configfs_dirent *sd, *tmp;
616 parent_sd = dentry->d_fsdata;
617 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
618 if (!sd->s_element ||
619 !(sd->s_type & CONFIGFS_USET_DEFAULT))
622 child = sd->s_dentry;
624 inode_lock(d_inode(child));
626 configfs_detach_group(sd->s_element);
627 d_inode(child)->i_flags |= S_DEAD;
630 inode_unlock(d_inode(child));
637 * Drop reference from dget() on entrance.
643 * This fakes mkdir(2) on a default_groups[] entry. It
644 * creates a dentry, attachs it, and then does fixup
647 * We could, perhaps, tweak our parent's ->mkdir for a minute and
648 * try using vfs_mkdir. Just a thought.
650 static int create_default_group(struct config_group *parent_group,
651 struct config_group *group)
654 struct configfs_dirent *sd;
655 /* We trust the caller holds a reference to parent */
656 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
658 if (!group->cg_item.ci_name)
659 group->cg_item.ci_name = group->cg_item.ci_namebuf;
662 child = d_alloc_name(parent, group->cg_item.ci_name);
666 ret = configfs_attach_group(&parent_group->cg_item,
667 &group->cg_item, child);
669 sd = child->d_fsdata;
670 sd->s_type |= CONFIGFS_USET_DEFAULT;
672 BUG_ON(d_inode(child));
681 static int populate_groups(struct config_group *group)
683 struct config_group *new_group;
686 list_for_each_entry(new_group, &group->default_groups, group_entry) {
687 ret = create_default_group(group, new_group);
689 detach_groups(group);
697 void configfs_remove_default_groups(struct config_group *group)
699 struct config_group *g, *n;
701 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
702 list_del(&g->group_entry);
703 config_item_put(&g->cg_item);
706 EXPORT_SYMBOL(configfs_remove_default_groups);
709 * All of link_obj/unlink_obj/link_group/unlink_group require that
710 * subsys->su_mutex is held.
713 static void unlink_obj(struct config_item *item)
715 struct config_group *group;
717 group = item->ci_group;
719 list_del_init(&item->ci_entry);
721 item->ci_group = NULL;
722 item->ci_parent = NULL;
724 /* Drop the reference for ci_entry */
725 config_item_put(item);
727 /* Drop the reference for ci_parent */
728 config_group_put(group);
732 static void link_obj(struct config_item *parent_item, struct config_item *item)
735 * Parent seems redundant with group, but it makes certain
736 * traversals much nicer.
738 item->ci_parent = parent_item;
741 * We hold a reference on the parent for the child's ci_parent
744 item->ci_group = config_group_get(to_config_group(parent_item));
745 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
748 * We hold a reference on the child for ci_entry on the parent's
751 config_item_get(item);
754 static void unlink_group(struct config_group *group)
756 struct config_group *new_group;
758 list_for_each_entry(new_group, &group->default_groups, group_entry)
759 unlink_group(new_group);
761 group->cg_subsys = NULL;
762 unlink_obj(&group->cg_item);
765 static void link_group(struct config_group *parent_group, struct config_group *group)
767 struct config_group *new_group;
768 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
770 link_obj(&parent_group->cg_item, &group->cg_item);
772 if (parent_group->cg_subsys)
773 subsys = parent_group->cg_subsys;
774 else if (configfs_is_root(&parent_group->cg_item))
775 subsys = to_configfs_subsystem(group);
778 group->cg_subsys = subsys;
780 list_for_each_entry(new_group, &group->default_groups, group_entry)
781 link_group(group, new_group);
785 * The goal is that configfs_attach_item() (and
786 * configfs_attach_group()) can be called from either the VFS or this
787 * module. That is, they assume that the items have been created,
788 * the dentry allocated, and the dcache is all ready to go.
790 * If they fail, they must clean up after themselves as if they
791 * had never been called. The caller (VFS or local function) will
792 * handle cleaning up the dcache bits.
794 * configfs_detach_group() and configfs_detach_item() behave similarly on
795 * the way out. They assume that the proper semaphores are held, they
796 * clean up the configfs items, and they expect their callers will
797 * handle the dcache bits.
799 static int configfs_attach_item(struct config_item *parent_item,
800 struct config_item *item,
801 struct dentry *dentry)
805 ret = configfs_create_dir(item, dentry);
807 ret = populate_attrs(item);
810 * We are going to remove an inode and its dentry but
811 * the VFS may already have hit and used them. Thus,
812 * we must lock them as rmdir() would.
814 inode_lock(d_inode(dentry));
815 configfs_remove_dir(item);
816 d_inode(dentry)->i_flags |= S_DEAD;
818 inode_unlock(d_inode(dentry));
826 /* Caller holds the mutex of the item's inode */
827 static void configfs_detach_item(struct config_item *item)
830 configfs_remove_dir(item);
833 static int configfs_attach_group(struct config_item *parent_item,
834 struct config_item *item,
835 struct dentry *dentry)
838 struct configfs_dirent *sd;
840 ret = configfs_attach_item(parent_item, item, dentry);
842 sd = dentry->d_fsdata;
843 sd->s_type |= CONFIGFS_USET_DIR;
846 * FYI, we're faking mkdir in populate_groups()
847 * We must lock the group's inode to avoid races with the VFS
848 * which can already hit the inode and try to add/remove entries
851 * We must also lock the inode to remove it safely in case of
852 * error, as rmdir() would.
854 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
855 configfs_adjust_dir_dirent_depth_before_populate(sd);
856 ret = populate_groups(to_config_group(item));
858 configfs_detach_item(item);
859 d_inode(dentry)->i_flags |= S_DEAD;
862 configfs_adjust_dir_dirent_depth_after_populate(sd);
863 inode_unlock(d_inode(dentry));
871 /* Caller holds the mutex of the group's inode */
872 static void configfs_detach_group(struct config_item *item)
874 detach_groups(to_config_group(item));
875 configfs_detach_item(item);
879 * After the item has been detached from the filesystem view, we are
880 * ready to tear it out of the hierarchy. Notify the client before
881 * we do that so they can perform any cleanup that requires
882 * navigating the hierarchy. A client does not need to provide this
883 * callback. The subsystem semaphore MUST be held by the caller, and
884 * references must be valid for both items. It also assumes the
885 * caller has validated ci_type.
887 static void client_disconnect_notify(struct config_item *parent_item,
888 struct config_item *item)
890 const struct config_item_type *type;
892 type = parent_item->ci_type;
895 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
896 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
901 * Drop the initial reference from make_item()/make_group()
902 * This function assumes that reference is held on item
903 * and that item holds a valid reference to the parent. Also, it
904 * assumes the caller has validated ci_type.
906 static void client_drop_item(struct config_item *parent_item,
907 struct config_item *item)
909 const struct config_item_type *type;
911 type = parent_item->ci_type;
915 * If ->drop_item() exists, it is responsible for the
918 if (type->ct_group_ops && type->ct_group_ops->drop_item)
919 type->ct_group_ops->drop_item(to_config_group(parent_item),
922 config_item_put(item);
926 static void configfs_dump_one(struct configfs_dirent *sd, int level)
928 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
930 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
931 type_print(CONFIGFS_ROOT);
932 type_print(CONFIGFS_DIR);
933 type_print(CONFIGFS_ITEM_ATTR);
934 type_print(CONFIGFS_ITEM_LINK);
935 type_print(CONFIGFS_USET_DIR);
936 type_print(CONFIGFS_USET_DEFAULT);
937 type_print(CONFIGFS_USET_DROPPING);
941 static int configfs_dump(struct configfs_dirent *sd, int level)
943 struct configfs_dirent *child_sd;
946 configfs_dump_one(sd, level);
948 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
951 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
952 ret = configfs_dump(child_sd, level + 2);
963 * configfs_depend_item() and configfs_undepend_item()
965 * WARNING: Do not call these from a configfs callback!
967 * This describes these functions and their helpers.
969 * Allow another kernel system to depend on a config_item. If this
970 * happens, the item cannot go away until the dependent can live without
971 * it. The idea is to give client modules as simple an interface as
972 * possible. When a system asks them to depend on an item, they just
973 * call configfs_depend_item(). If the item is live and the client
974 * driver is in good shape, we'll happily do the work for them.
976 * Why is the locking complex? Because configfs uses the VFS to handle
977 * all locking, but this function is called outside the normal
978 * VFS->configfs path. So it must take VFS locks to prevent the
979 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
980 * why you can't call these functions underneath configfs callbacks.
982 * Note, btw, that this can be called at *any* time, even when a configfs
983 * subsystem isn't registered, or when configfs is loading or unloading.
984 * Just like configfs_register_subsystem(). So we take the same
985 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
986 * If we can find the target item in the
987 * configfs tree, it must be part of the subsystem tree as well, so we
988 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
989 * locking out mkdir() and rmdir(), who might be racing us.
993 * configfs_depend_prep()
995 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
996 * attributes. This is similar but not the same to configfs_detach_prep().
997 * Note that configfs_detach_prep() expects the parent to be locked when it
998 * is called, but we lock the parent *inside* configfs_depend_prep(). We
999 * do that so we can unlock it if we find nothing.
1001 * Here we do a depth-first search of the dentry hierarchy looking for
1003 * We deliberately ignore items tagged as dropping since they are virtually
1004 * dead, as well as items in the middle of attachment since they virtually
1005 * do not exist yet. This completes the locking out of racing mkdir() and
1007 * Note: subdirectories in the middle of attachment start with s_type =
1008 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1009 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1010 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1012 * If the target is not found, -ENOENT is bubbled up.
1014 * This adds a requirement that all config_items be unique!
1016 * This is recursive. There isn't
1017 * much on the stack, though, so folks that need this function - be careful
1018 * about your stack! Patches will be accepted to make it iterative.
1020 static int configfs_depend_prep(struct dentry *origin,
1021 struct config_item *target)
1023 struct configfs_dirent *child_sd, *sd;
1026 BUG_ON(!origin || !origin->d_fsdata);
1027 sd = origin->d_fsdata;
1029 if (sd->s_element == target) /* Boo-yah */
1032 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1033 if ((child_sd->s_type & CONFIGFS_DIR) &&
1034 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1035 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1036 ret = configfs_depend_prep(child_sd->s_dentry,
1039 goto out; /* Child path boo-yah */
1043 /* We looped all our children and didn't find target */
1050 static int configfs_do_depend_item(struct dentry *subsys_dentry,
1051 struct config_item *target)
1053 struct configfs_dirent *p;
1056 spin_lock(&configfs_dirent_lock);
1057 /* Scan the tree, return 0 if found */
1058 ret = configfs_depend_prep(subsys_dentry, target);
1060 goto out_unlock_dirent_lock;
1063 * We are sure that the item is not about to be removed by rmdir(), and
1064 * not in the middle of attachment by mkdir().
1066 p = target->ci_dentry->d_fsdata;
1067 p->s_dependent_count += 1;
1069 out_unlock_dirent_lock:
1070 spin_unlock(&configfs_dirent_lock);
1075 static inline struct configfs_dirent *
1076 configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1077 struct config_item *subsys_item)
1079 struct configfs_dirent *p;
1080 struct configfs_dirent *ret = NULL;
1082 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1083 if (p->s_type & CONFIGFS_DIR &&
1084 p->s_element == subsys_item) {
1094 int configfs_depend_item(struct configfs_subsystem *subsys,
1095 struct config_item *target)
1098 struct configfs_dirent *subsys_sd;
1099 struct config_item *s_item = &subsys->su_group.cg_item;
1100 struct dentry *root;
1103 * Pin the configfs filesystem. This means we can safely access
1104 * the root of the configfs filesystem.
1106 root = configfs_pin_fs();
1108 return PTR_ERR(root);
1111 * Next, lock the root directory. We're going to check that the
1112 * subsystem is really registered, and so we need to lock out
1113 * configfs_[un]register_subsystem().
1115 inode_lock(d_inode(root));
1117 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
1123 /* Ok, now we can trust subsys/s_item */
1124 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1127 inode_unlock(d_inode(root));
1130 * If we succeeded, the fs is pinned via other methods. If not,
1131 * we're done with it anyway. So release_fs() is always right.
1133 configfs_release_fs();
1137 EXPORT_SYMBOL(configfs_depend_item);
1140 * Release the dependent linkage. This is much simpler than
1141 * configfs_depend_item() because we know that that the client driver is
1142 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1144 void configfs_undepend_item(struct config_item *target)
1146 struct configfs_dirent *sd;
1149 * Since we can trust everything is pinned, we just need
1150 * configfs_dirent_lock.
1152 spin_lock(&configfs_dirent_lock);
1154 sd = target->ci_dentry->d_fsdata;
1155 BUG_ON(sd->s_dependent_count < 1);
1157 sd->s_dependent_count -= 1;
1160 * After this unlock, we cannot trust the item to stay alive!
1161 * DO NOT REFERENCE item after this unlock.
1163 spin_unlock(&configfs_dirent_lock);
1165 EXPORT_SYMBOL(configfs_undepend_item);
1168 * caller_subsys is a caller's subsystem not target's. This is used to
1169 * determine if we should lock root and check subsys or not. When we are
1170 * in the same subsystem as our target there is no need to do locking as
1171 * we know that subsys is valid and is not unregistered during this function
1172 * as we are called from callback of one of his children and VFS holds a lock
1173 * on some inode. Otherwise we have to lock our root to ensure that target's
1174 * subsystem it is not unregistered during this function.
1176 int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1177 struct config_item *target)
1179 struct configfs_subsystem *target_subsys;
1180 struct config_group *root, *parent;
1181 struct configfs_dirent *subsys_sd;
1184 /* Disallow this function for configfs root */
1185 if (configfs_is_root(target))
1188 parent = target->ci_group;
1190 * This may happen when someone is trying to depend root
1191 * directory of some subsystem
1193 if (configfs_is_root(&parent->cg_item)) {
1194 target_subsys = to_configfs_subsystem(to_config_group(target));
1197 target_subsys = parent->cg_subsys;
1198 /* Find a cofnigfs root as we may need it for locking */
1199 for (root = parent; !configfs_is_root(&root->cg_item);
1200 root = root->cg_item.ci_group)
1204 if (target_subsys != caller_subsys) {
1206 * We are in other configfs subsystem, so we have to do
1207 * additional locking to prevent other subsystem from being
1210 inode_lock(d_inode(root->cg_item.ci_dentry));
1213 * As we are trying to depend item from other subsystem
1214 * we have to check if this subsystem is still registered
1216 subsys_sd = configfs_find_subsys_dentry(
1217 root->cg_item.ci_dentry->d_fsdata,
1218 &target_subsys->su_group.cg_item);
1220 goto out_root_unlock;
1222 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1225 /* Now we can execute core of depend item */
1226 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1228 if (target_subsys != caller_subsys)
1231 * We were called from subsystem other than our target so we
1232 * took some locks so now it's time to release them
1234 inode_unlock(d_inode(root->cg_item.ci_dentry));
1238 EXPORT_SYMBOL(configfs_depend_item_unlocked);
1240 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1244 struct config_group *group = NULL;
1245 struct config_item *item = NULL;
1246 struct config_item *parent_item;
1247 struct configfs_subsystem *subsys;
1248 struct configfs_dirent *sd;
1249 const struct config_item_type *type;
1250 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1253 sd = dentry->d_parent->d_fsdata;
1256 * Fake invisibility if dir belongs to a group/default groups hierarchy
1259 if (!configfs_dirent_is_ready(sd)) {
1264 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1269 /* Get a working ref for the duration of this function */
1270 parent_item = configfs_get_config_item(dentry->d_parent);
1271 type = parent_item->ci_type;
1272 subsys = to_config_group(parent_item)->cg_subsys;
1275 if (!type || !type->ct_group_ops ||
1276 (!type->ct_group_ops->make_group &&
1277 !type->ct_group_ops->make_item)) {
1278 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1283 * The subsystem may belong to a different module than the item
1284 * being created. We don't want to safely pin the new item but
1285 * fail to pin the subsystem it sits under.
1287 if (!subsys->su_group.cg_item.ci_type) {
1291 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1292 if (!try_module_get(subsys_owner)) {
1297 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1300 goto out_subsys_put;
1303 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1305 mutex_lock(&subsys->su_mutex);
1306 if (type->ct_group_ops->make_group) {
1307 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1309 group = ERR_PTR(-ENOMEM);
1310 if (!IS_ERR(group)) {
1311 link_group(to_config_group(parent_item), group);
1312 item = &group->cg_item;
1314 ret = PTR_ERR(group);
1316 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1318 item = ERR_PTR(-ENOMEM);
1320 link_obj(parent_item, item);
1322 ret = PTR_ERR(item);
1324 mutex_unlock(&subsys->su_mutex);
1329 * If ret != 0, then link_obj() was never called.
1330 * There are no extra references to clean up.
1332 goto out_subsys_put;
1336 * link_obj() has been called (via link_group() for groups).
1337 * From here on out, errors must clean that up.
1340 type = item->ci_type;
1346 new_item_owner = type->ct_owner;
1347 if (!try_module_get(new_item_owner)) {
1353 * I hate doing it this way, but if there is
1354 * an error, module_put() probably should
1355 * happen after any cleanup.
1360 * Make racing rmdir() fail if it did not tag parent with
1361 * CONFIGFS_USET_DROPPING
1362 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1363 * fail and let rmdir() terminate correctly
1365 spin_lock(&configfs_dirent_lock);
1366 /* This will make configfs_detach_prep() fail */
1367 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1368 spin_unlock(&configfs_dirent_lock);
1371 ret = configfs_attach_group(parent_item, item, dentry);
1373 ret = configfs_attach_item(parent_item, item, dentry);
1375 spin_lock(&configfs_dirent_lock);
1376 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1378 configfs_dir_set_ready(dentry->d_fsdata);
1379 spin_unlock(&configfs_dirent_lock);
1383 /* Tear down everything we built up */
1384 mutex_lock(&subsys->su_mutex);
1386 client_disconnect_notify(parent_item, item);
1388 unlink_group(group);
1391 client_drop_item(parent_item, item);
1393 mutex_unlock(&subsys->su_mutex);
1396 module_put(new_item_owner);
1401 module_put(subsys_owner);
1405 * link_obj()/link_group() took a reference from child->parent,
1406 * so the parent is safely pinned. We can drop our working
1409 config_item_put(parent_item);
1415 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1417 struct config_item *parent_item;
1418 struct config_item *item;
1419 struct configfs_subsystem *subsys;
1420 struct configfs_dirent *sd;
1421 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1424 sd = dentry->d_fsdata;
1425 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1428 /* Get a working ref until we have the child */
1429 parent_item = configfs_get_config_item(dentry->d_parent);
1430 subsys = to_config_group(parent_item)->cg_subsys;
1433 if (!parent_item->ci_type) {
1434 config_item_put(parent_item);
1438 /* configfs_mkdir() shouldn't have allowed this */
1439 BUG_ON(!subsys->su_group.cg_item.ci_type);
1440 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1443 * Ensure that no racing symlink() will make detach_prep() fail while
1444 * the new link is temporarily attached
1447 struct dentry *wait;
1449 mutex_lock(&configfs_symlink_mutex);
1450 spin_lock(&configfs_dirent_lock);
1452 * Here's where we check for dependents. We're protected by
1453 * configfs_dirent_lock.
1454 * If no dependent, atomically tag the item as dropping.
1456 ret = sd->s_dependent_count ? -EBUSY : 0;
1458 ret = configfs_detach_prep(dentry, &wait);
1460 configfs_detach_rollback(dentry);
1462 spin_unlock(&configfs_dirent_lock);
1463 mutex_unlock(&configfs_symlink_mutex);
1466 if (ret != -EAGAIN) {
1467 config_item_put(parent_item);
1471 /* Wait until the racing operation terminates */
1472 inode_lock(d_inode(wait));
1473 inode_unlock(d_inode(wait));
1476 } while (ret == -EAGAIN);
1478 /* Get a working ref for the duration of this function */
1479 item = configfs_get_config_item(dentry);
1481 /* Drop reference from above, item already holds one. */
1482 config_item_put(parent_item);
1485 dead_item_owner = item->ci_type->ct_owner;
1487 if (sd->s_type & CONFIGFS_USET_DIR) {
1488 configfs_detach_group(item);
1490 mutex_lock(&subsys->su_mutex);
1491 client_disconnect_notify(parent_item, item);
1492 unlink_group(to_config_group(item));
1494 configfs_detach_item(item);
1496 mutex_lock(&subsys->su_mutex);
1497 client_disconnect_notify(parent_item, item);
1501 client_drop_item(parent_item, item);
1502 mutex_unlock(&subsys->su_mutex);
1504 /* Drop our reference from above */
1505 config_item_put(item);
1507 module_put(dead_item_owner);
1508 module_put(subsys_owner);
1513 const struct inode_operations configfs_dir_inode_operations = {
1514 .mkdir = configfs_mkdir,
1515 .rmdir = configfs_rmdir,
1516 .symlink = configfs_symlink,
1517 .unlink = configfs_unlink,
1518 .lookup = configfs_lookup,
1519 .setattr = configfs_setattr,
1522 const struct inode_operations configfs_root_inode_operations = {
1523 .lookup = configfs_lookup,
1524 .setattr = configfs_setattr,
1528 int configfs_rename_dir(struct config_item * item, const char *new_name)
1531 struct dentry * new_dentry, * parent;
1533 if (!strcmp(config_item_name(item), new_name))
1539 down_write(&configfs_rename_sem);
1540 parent = item->parent->dentry;
1542 inode_lock(d_inode(parent));
1544 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1545 if (!IS_ERR(new_dentry)) {
1546 if (d_really_is_negative(new_dentry)) {
1547 error = config_item_set_name(item, "%s", new_name);
1549 d_add(new_dentry, NULL);
1550 d_move(item->dentry, new_dentry);
1553 d_delete(new_dentry);
1558 inode_unlock(d_inode(parent));
1559 up_write(&configfs_rename_sem);
1565 static int configfs_dir_open(struct inode *inode, struct file *file)
1567 struct dentry * dentry = file->f_path.dentry;
1568 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1571 inode_lock(d_inode(dentry));
1573 * Fake invisibility if dir belongs to a group/default groups hierarchy
1577 if (configfs_dirent_is_ready(parent_sd)) {
1578 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1579 if (IS_ERR(file->private_data))
1580 err = PTR_ERR(file->private_data);
1584 inode_unlock(d_inode(dentry));
1589 static int configfs_dir_close(struct inode *inode, struct file *file)
1591 struct dentry * dentry = file->f_path.dentry;
1592 struct configfs_dirent * cursor = file->private_data;
1594 inode_lock(d_inode(dentry));
1595 spin_lock(&configfs_dirent_lock);
1596 list_del_init(&cursor->s_sibling);
1597 spin_unlock(&configfs_dirent_lock);
1598 inode_unlock(d_inode(dentry));
1600 release_configfs_dirent(cursor);
1605 /* Relationship between s_mode and the DT_xxx types */
1606 static inline unsigned char dt_type(struct configfs_dirent *sd)
1608 return (sd->s_mode >> 12) & 15;
1611 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1613 struct dentry *dentry = file->f_path.dentry;
1614 struct super_block *sb = dentry->d_sb;
1615 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1616 struct configfs_dirent *cursor = file->private_data;
1617 struct list_head *p, *q = &cursor->s_sibling;
1620 if (!dir_emit_dots(file, ctx))
1622 spin_lock(&configfs_dirent_lock);
1624 list_move(q, &parent_sd->s_children);
1625 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1626 struct configfs_dirent *next;
1629 struct inode *inode = NULL;
1631 next = list_entry(p, struct configfs_dirent, s_sibling);
1632 if (!next->s_element)
1636 * We'll have a dentry and an inode for
1637 * PINNED items and for open attribute
1638 * files. We lock here to prevent a race
1639 * with configfs_d_iput() clearing
1640 * s_dentry before calling iput().
1642 * Why do we go to the trouble? If
1643 * someone has an attribute file open,
1644 * the inode number should match until
1645 * they close it. Beyond that, we don't
1648 dentry = next->s_dentry;
1650 inode = d_inode(dentry);
1653 spin_unlock(&configfs_dirent_lock);
1655 ino = iunique(sb, 2);
1657 name = configfs_get_name(next);
1660 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1663 spin_lock(&configfs_dirent_lock);
1668 spin_unlock(&configfs_dirent_lock);
1672 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1674 struct dentry * dentry = file->f_path.dentry;
1678 offset += file->f_pos;
1687 if (offset != file->f_pos) {
1688 file->f_pos = offset;
1689 if (file->f_pos >= 2) {
1690 struct configfs_dirent *sd = dentry->d_fsdata;
1691 struct configfs_dirent *cursor = file->private_data;
1692 struct list_head *p;
1693 loff_t n = file->f_pos - 2;
1695 spin_lock(&configfs_dirent_lock);
1696 list_del(&cursor->s_sibling);
1697 p = sd->s_children.next;
1698 while (n && p != &sd->s_children) {
1699 struct configfs_dirent *next;
1700 next = list_entry(p, struct configfs_dirent,
1702 if (next->s_element)
1706 list_add_tail(&cursor->s_sibling, p);
1707 spin_unlock(&configfs_dirent_lock);
1713 const struct file_operations configfs_dir_operations = {
1714 .open = configfs_dir_open,
1715 .release = configfs_dir_close,
1716 .llseek = configfs_dir_lseek,
1717 .read = generic_read_dir,
1718 .iterate_shared = configfs_readdir,
1722 * configfs_register_group - creates a parent-child relation between two groups
1723 * @parent_group: parent group
1724 * @group: child group
1726 * link groups, creates dentry for the child and attaches it to the
1729 * Return: 0 on success, negative errno code on error
1731 int configfs_register_group(struct config_group *parent_group,
1732 struct config_group *group)
1734 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1735 struct dentry *parent;
1738 mutex_lock(&subsys->su_mutex);
1739 link_group(parent_group, group);
1740 mutex_unlock(&subsys->su_mutex);
1742 parent = parent_group->cg_item.ci_dentry;
1744 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1745 ret = create_default_group(parent_group, group);
1749 spin_lock(&configfs_dirent_lock);
1750 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1751 spin_unlock(&configfs_dirent_lock);
1752 inode_unlock(d_inode(parent));
1755 inode_unlock(d_inode(parent));
1756 mutex_lock(&subsys->su_mutex);
1757 unlink_group(group);
1758 mutex_unlock(&subsys->su_mutex);
1761 EXPORT_SYMBOL(configfs_register_group);
1764 * configfs_unregister_group() - unregisters a child group from its parent
1765 * @group: parent group to be unregistered
1767 * Undoes configfs_register_group()
1769 void configfs_unregister_group(struct config_group *group)
1771 struct configfs_subsystem *subsys = group->cg_subsys;
1772 struct dentry *dentry = group->cg_item.ci_dentry;
1773 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1775 mutex_lock(&subsys->su_mutex);
1776 if (!group->cg_item.ci_parent->ci_group) {
1778 * The parent has already been unlinked and detached
1783 mutex_unlock(&subsys->su_mutex);
1785 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1786 spin_lock(&configfs_dirent_lock);
1787 configfs_detach_prep(dentry, NULL);
1788 spin_unlock(&configfs_dirent_lock);
1790 configfs_detach_group(&group->cg_item);
1791 d_inode(dentry)->i_flags |= S_DEAD;
1794 inode_unlock(d_inode(parent));
1798 mutex_lock(&subsys->su_mutex);
1800 unlink_group(group);
1801 mutex_unlock(&subsys->su_mutex);
1803 EXPORT_SYMBOL(configfs_unregister_group);
1806 * configfs_register_default_group() - allocates and registers a child group
1807 * @parent_group: parent group
1808 * @name: child group name
1809 * @item_type: child item type description
1811 * boilerplate to allocate and register a child group with its parent. We need
1812 * kzalloc'ed memory because child's default_group is initially empty.
1814 * Return: allocated config group or ERR_PTR() on error
1816 struct config_group *
1817 configfs_register_default_group(struct config_group *parent_group,
1819 const struct config_item_type *item_type)
1822 struct config_group *group;
1824 group = kzalloc(sizeof(*group), GFP_KERNEL);
1826 return ERR_PTR(-ENOMEM);
1827 config_group_init_type_name(group, name, item_type);
1829 ret = configfs_register_group(parent_group, group);
1832 return ERR_PTR(ret);
1836 EXPORT_SYMBOL(configfs_register_default_group);
1839 * configfs_unregister_default_group() - unregisters and frees a child group
1840 * @group: the group to act on
1842 void configfs_unregister_default_group(struct config_group *group)
1844 configfs_unregister_group(group);
1847 EXPORT_SYMBOL(configfs_unregister_default_group);
1849 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1852 struct config_group *group = &subsys->su_group;
1853 struct dentry *dentry;
1854 struct dentry *root;
1855 struct configfs_dirent *sd;
1857 root = configfs_pin_fs();
1859 return PTR_ERR(root);
1861 if (!group->cg_item.ci_name)
1862 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1864 sd = root->d_fsdata;
1865 link_group(to_config_group(sd->s_element), group);
1867 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
1870 dentry = d_alloc_name(root, group->cg_item.ci_name);
1872 d_add(dentry, NULL);
1874 err = configfs_attach_group(sd->s_element, &group->cg_item,
1877 BUG_ON(d_inode(dentry));
1881 spin_lock(&configfs_dirent_lock);
1882 configfs_dir_set_ready(dentry->d_fsdata);
1883 spin_unlock(&configfs_dirent_lock);
1887 inode_unlock(d_inode(root));
1890 unlink_group(group);
1891 configfs_release_fs();
1897 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1899 struct config_group *group = &subsys->su_group;
1900 struct dentry *dentry = group->cg_item.ci_dentry;
1901 struct dentry *root = dentry->d_sb->s_root;
1903 if (dentry->d_parent != root) {
1904 pr_err("Tried to unregister non-subsystem!\n");
1908 inode_lock_nested(d_inode(root),
1910 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
1911 mutex_lock(&configfs_symlink_mutex);
1912 spin_lock(&configfs_dirent_lock);
1913 if (configfs_detach_prep(dentry, NULL)) {
1914 pr_err("Tried to unregister non-empty subsystem!\n");
1916 spin_unlock(&configfs_dirent_lock);
1917 mutex_unlock(&configfs_symlink_mutex);
1918 configfs_detach_group(&group->cg_item);
1919 d_inode(dentry)->i_flags |= S_DEAD;
1921 inode_unlock(d_inode(dentry));
1925 inode_unlock(d_inode(root));
1929 unlink_group(group);
1930 configfs_release_fs();
1933 EXPORT_SYMBOL(configfs_register_subsystem);
1934 EXPORT_SYMBOL(configfs_unregister_subsystem);