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
);
48 * Set sd->s_dentry to null only when this dentry is the one
49 * that is going to be killed. Otherwise configfs_d_iput may
50 * run just after configfs_attach_attr and set sd->s_dentry to
51 * NULL even it's still in use.
53 if (sd
->s_dentry
== dentry
)
56 spin_unlock(&configfs_dirent_lock
);
62 const struct dentry_operations configfs_dentry_ops
= {
63 .d_iput
= configfs_d_iput
,
64 .d_delete
= always_delete_dentry
,
70 * Helpers to make lockdep happy with our recursive locking of default groups'
71 * inodes (see configfs_attach_group() and configfs_detach_group()).
72 * We put default groups i_mutexes in separate classes according to their depth
73 * from the youngest non-default group ancestor.
75 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
76 * groups A/B and A/C will have their inode's mutex in class
77 * default_group_class[0], and default group A/C/D will be in
78 * default_group_class[1].
80 * The lock classes are declared and assigned in inode.c, according to the
82 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
83 * default groups, and reset to -1 when all default groups are attached. During
84 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
85 * inode's mutex is set to default_group_class[s_depth - 1].
88 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
93 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
94 struct configfs_dirent
*sd
)
96 int parent_depth
= parent_sd
->s_depth
;
98 if (parent_depth
>= 0)
99 sd
->s_depth
= parent_depth
+ 1;
103 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
106 * item's i_mutex class is already setup, so s_depth is now only
107 * used to set new sub-directories s_depth, which is always done
108 * with item's i_mutex locked.
111 * sd->s_depth == -1 iff we are a non default group.
112 * else (we are a default group) sd->s_depth > 0 (see
115 if (sd
->s_depth
== -1)
117 * We are a non default group and we are going to create
124 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
126 /* We will not create default groups anymore. */
130 #else /* CONFIG_LOCKDEP */
132 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
136 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
137 struct configfs_dirent
*sd
)
142 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
147 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
151 #endif /* CONFIG_LOCKDEP */
154 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
156 static struct configfs_dirent
*configfs_new_dirent(struct configfs_dirent
*parent_sd
,
157 void *element
, int type
)
159 struct configfs_dirent
* sd
;
161 sd
= kmem_cache_zalloc(configfs_dir_cachep
, GFP_KERNEL
);
163 return ERR_PTR(-ENOMEM
);
165 atomic_set(&sd
->s_count
, 1);
166 INIT_LIST_HEAD(&sd
->s_links
);
167 INIT_LIST_HEAD(&sd
->s_children
);
168 sd
->s_element
= element
;
170 configfs_init_dirent_depth(sd
);
171 spin_lock(&configfs_dirent_lock
);
172 if (parent_sd
->s_type
& CONFIGFS_USET_DROPPING
) {
173 spin_unlock(&configfs_dirent_lock
);
174 kmem_cache_free(configfs_dir_cachep
, sd
);
175 return ERR_PTR(-ENOENT
);
177 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
178 spin_unlock(&configfs_dirent_lock
);
185 * Return -EEXIST if there is already a configfs element with the same
186 * name for the same parent.
188 * called with parent inode's i_mutex held
190 static int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
191 const unsigned char *new)
193 struct configfs_dirent
* sd
;
195 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
197 const unsigned char *existing
= configfs_get_name(sd
);
198 if (strcmp(existing
, new))
209 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
210 struct dentry
* dentry
, void * element
,
211 umode_t mode
, int type
)
213 struct configfs_dirent
* sd
;
215 sd
= configfs_new_dirent(parent_sd
, element
, type
);
220 sd
->s_dentry
= dentry
;
222 dentry
->d_fsdata
= configfs_get(sd
);
227 static void init_dir(struct inode
* inode
)
229 inode
->i_op
= &configfs_dir_inode_operations
;
230 inode
->i_fop
= &configfs_dir_operations
;
232 /* directory inodes start off with i_nlink == 2 (for "." entry) */
236 static void configfs_init_file(struct inode
* inode
)
238 inode
->i_size
= PAGE_SIZE
;
239 inode
->i_fop
= &configfs_file_operations
;
242 static void configfs_init_bin_file(struct inode
*inode
)
245 inode
->i_fop
= &configfs_bin_file_operations
;
248 static void init_symlink(struct inode
* inode
)
250 inode
->i_op
= &configfs_symlink_inode_operations
;
254 * configfs_create_dir - create a directory for an config_item.
255 * @item: config_itemwe're creating directory for.
256 * @dentry: config_item's dentry.
258 * Note: user-created entries won't be allowed under this new directory
259 * until it is validated by configfs_dir_set_ready()
262 static int configfs_create_dir(struct config_item
*item
, struct dentry
*dentry
)
265 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
266 struct dentry
*p
= dentry
->d_parent
;
270 error
= configfs_dirent_exists(p
->d_fsdata
, dentry
->d_name
.name
);
274 error
= configfs_make_dirent(p
->d_fsdata
, dentry
, item
, mode
,
275 CONFIGFS_DIR
| CONFIGFS_USET_CREATING
);
279 configfs_set_dir_dirent_depth(p
->d_fsdata
, dentry
->d_fsdata
);
280 error
= configfs_create(dentry
, mode
, init_dir
);
282 inc_nlink(d_inode(p
));
283 item
->ci_dentry
= dentry
;
285 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
287 spin_lock(&configfs_dirent_lock
);
288 list_del_init(&sd
->s_sibling
);
289 spin_unlock(&configfs_dirent_lock
);
297 * Allow userspace to create new entries under a new directory created with
298 * configfs_create_dir(), and under all of its chidlren directories recursively.
299 * @sd configfs_dirent of the new directory to validate
301 * Caller must hold configfs_dirent_lock.
303 static void configfs_dir_set_ready(struct configfs_dirent
*sd
)
305 struct configfs_dirent
*child_sd
;
307 sd
->s_type
&= ~CONFIGFS_USET_CREATING
;
308 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
)
309 if (child_sd
->s_type
& CONFIGFS_USET_CREATING
)
310 configfs_dir_set_ready(child_sd
);
314 * Check that a directory does not belong to a directory hierarchy being
315 * attached and not validated yet.
316 * @sd configfs_dirent of the directory to check
318 * @return non-zero iff the directory was validated
320 * Note: takes configfs_dirent_lock, so the result may change from false to true
321 * in two consecutive calls, but never from true to false.
323 int configfs_dirent_is_ready(struct configfs_dirent
*sd
)
327 spin_lock(&configfs_dirent_lock
);
328 ret
= !(sd
->s_type
& CONFIGFS_USET_CREATING
);
329 spin_unlock(&configfs_dirent_lock
);
334 int configfs_create_link(struct configfs_symlink
*sl
,
335 struct dentry
*parent
,
336 struct dentry
*dentry
)
339 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
341 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
344 err
= configfs_create(dentry
, mode
, init_symlink
);
346 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
348 spin_lock(&configfs_dirent_lock
);
349 list_del_init(&sd
->s_sibling
);
350 spin_unlock(&configfs_dirent_lock
);
358 static void remove_dir(struct dentry
* d
)
360 struct dentry
* parent
= dget(d
->d_parent
);
361 struct configfs_dirent
* sd
;
364 spin_lock(&configfs_dirent_lock
);
365 list_del_init(&sd
->s_sibling
);
366 spin_unlock(&configfs_dirent_lock
);
368 if (d_really_is_positive(d
))
369 simple_rmdir(d_inode(parent
),d
);
371 pr_debug(" o %pd removing done (%d)\n", d
, d_count(d
));
377 * configfs_remove_dir - remove an config_item's directory.
378 * @item: config_item we're removing.
380 * The only thing special about this is that we remove any files in
381 * the directory before we remove the directory, and we've inlined
382 * what used to be configfs_rmdir() below, instead of calling separately.
384 * Caller holds the mutex of the item's inode
387 static void configfs_remove_dir(struct config_item
* item
)
389 struct dentry
* dentry
= dget(item
->ci_dentry
);
396 * Drop reference from dget() on entrance.
402 /* attaches attribute's configfs_dirent to the dentry corresponding to the
405 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
407 struct configfs_attribute
* attr
= sd
->s_element
;
410 spin_lock(&configfs_dirent_lock
);
411 dentry
->d_fsdata
= configfs_get(sd
);
412 sd
->s_dentry
= dentry
;
413 spin_unlock(&configfs_dirent_lock
);
415 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
,
416 (sd
->s_type
& CONFIGFS_ITEM_BIN_ATTR
) ?
417 configfs_init_bin_file
:
424 static struct dentry
* configfs_lookup(struct inode
*dir
,
425 struct dentry
*dentry
,
428 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
429 struct configfs_dirent
* sd
;
434 * Fake invisibility if dir belongs to a group/default groups hierarchy
437 * This forbids userspace to read/write attributes of items which may
438 * not complete their initialization, since the dentries of the
439 * attributes won't be instantiated.
442 if (!configfs_dirent_is_ready(parent_sd
))
445 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
446 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
447 const unsigned char * name
= configfs_get_name(sd
);
449 if (strcmp(name
, dentry
->d_name
.name
))
453 err
= configfs_attach_attr(sd
, dentry
);
460 * If it doesn't exist and it isn't a NOT_PINNED item,
461 * it must be negative.
463 if (dentry
->d_name
.len
> NAME_MAX
)
464 return ERR_PTR(-ENAMETOOLONG
);
474 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
475 * attributes and are removed by rmdir(). We recurse, setting
476 * CONFIGFS_USET_DROPPING on all children that are candidates for
478 * If there is an error, the caller will reset the flags via
479 * configfs_detach_rollback().
481 static int configfs_detach_prep(struct dentry
*dentry
, struct dentry
**wait
)
483 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
484 struct configfs_dirent
*sd
;
487 /* Mark that we're trying to drop the group */
488 parent_sd
->s_type
|= CONFIGFS_USET_DROPPING
;
491 if (!list_empty(&parent_sd
->s_links
))
495 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
496 if (!sd
->s_element
||
497 (sd
->s_type
& CONFIGFS_NOT_PINNED
))
499 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
500 /* Abort if racing with mkdir() */
501 if (sd
->s_type
& CONFIGFS_USET_IN_MKDIR
) {
503 *wait
= dget(sd
->s_dentry
);
508 * Yup, recursive. If there's a problem, blame
509 * deep nesting of default_groups
511 ret
= configfs_detach_prep(sd
->s_dentry
, wait
);
525 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
528 static void configfs_detach_rollback(struct dentry
*dentry
)
530 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
531 struct configfs_dirent
*sd
;
533 parent_sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
535 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
)
536 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
537 configfs_detach_rollback(sd
->s_dentry
);
540 static void detach_attrs(struct config_item
* item
)
542 struct dentry
* dentry
= dget(item
->ci_dentry
);
543 struct configfs_dirent
* parent_sd
;
544 struct configfs_dirent
* sd
, * tmp
;
549 pr_debug("configfs %s: dropping attrs for dir\n",
550 dentry
->d_name
.name
);
552 parent_sd
= dentry
->d_fsdata
;
553 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
554 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
556 spin_lock(&configfs_dirent_lock
);
557 list_del_init(&sd
->s_sibling
);
558 spin_unlock(&configfs_dirent_lock
);
559 configfs_drop_dentry(sd
, dentry
);
564 * Drop reference from dget() on entrance.
569 static int populate_attrs(struct config_item
*item
)
571 const struct config_item_type
*t
= item
->ci_type
;
572 struct configfs_attribute
*attr
;
573 struct configfs_bin_attribute
*bin_attr
;
580 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
581 if ((error
= configfs_create_file(item
, attr
)))
585 if (t
->ct_bin_attrs
) {
586 for (i
= 0; (bin_attr
= t
->ct_bin_attrs
[i
]) != NULL
; i
++) {
587 error
= configfs_create_bin_file(item
, bin_attr
);
599 static int configfs_attach_group(struct config_item
*parent_item
,
600 struct config_item
*item
,
601 struct dentry
*dentry
);
602 static void configfs_detach_group(struct config_item
*item
);
604 static void detach_groups(struct config_group
*group
)
606 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
607 struct dentry
*child
;
608 struct configfs_dirent
*parent_sd
;
609 struct configfs_dirent
*sd
, *tmp
;
614 parent_sd
= dentry
->d_fsdata
;
615 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
616 if (!sd
->s_element
||
617 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
620 child
= sd
->s_dentry
;
622 inode_lock(d_inode(child
));
624 configfs_detach_group(sd
->s_element
);
625 d_inode(child
)->i_flags
|= S_DEAD
;
628 inode_unlock(d_inode(child
));
635 * Drop reference from dget() on entrance.
641 * This fakes mkdir(2) on a default_groups[] entry. It
642 * creates a dentry, attachs it, and then does fixup
645 * We could, perhaps, tweak our parent's ->mkdir for a minute and
646 * try using vfs_mkdir. Just a thought.
648 static int create_default_group(struct config_group
*parent_group
,
649 struct config_group
*group
)
652 struct configfs_dirent
*sd
;
653 /* We trust the caller holds a reference to parent */
654 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
656 if (!group
->cg_item
.ci_name
)
657 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
660 child
= d_alloc_name(parent
, group
->cg_item
.ci_name
);
664 ret
= configfs_attach_group(&parent_group
->cg_item
,
665 &group
->cg_item
, child
);
667 sd
= child
->d_fsdata
;
668 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
670 BUG_ON(d_inode(child
));
679 static int populate_groups(struct config_group
*group
)
681 struct config_group
*new_group
;
684 list_for_each_entry(new_group
, &group
->default_groups
, group_entry
) {
685 ret
= create_default_group(group
, new_group
);
687 detach_groups(group
);
695 void configfs_remove_default_groups(struct config_group
*group
)
697 struct config_group
*g
, *n
;
699 list_for_each_entry_safe(g
, n
, &group
->default_groups
, group_entry
) {
700 list_del(&g
->group_entry
);
701 config_item_put(&g
->cg_item
);
704 EXPORT_SYMBOL(configfs_remove_default_groups
);
707 * All of link_obj/unlink_obj/link_group/unlink_group require that
708 * subsys->su_mutex is held.
711 static void unlink_obj(struct config_item
*item
)
713 struct config_group
*group
;
715 group
= item
->ci_group
;
717 list_del_init(&item
->ci_entry
);
719 item
->ci_group
= NULL
;
720 item
->ci_parent
= NULL
;
722 /* Drop the reference for ci_entry */
723 config_item_put(item
);
725 /* Drop the reference for ci_parent */
726 config_group_put(group
);
730 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
733 * Parent seems redundant with group, but it makes certain
734 * traversals much nicer.
736 item
->ci_parent
= parent_item
;
739 * We hold a reference on the parent for the child's ci_parent
742 item
->ci_group
= config_group_get(to_config_group(parent_item
));
743 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
746 * We hold a reference on the child for ci_entry on the parent's
749 config_item_get(item
);
752 static void unlink_group(struct config_group
*group
)
754 struct config_group
*new_group
;
756 list_for_each_entry(new_group
, &group
->default_groups
, group_entry
)
757 unlink_group(new_group
);
759 group
->cg_subsys
= NULL
;
760 unlink_obj(&group
->cg_item
);
763 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
765 struct config_group
*new_group
;
766 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
768 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
770 if (parent_group
->cg_subsys
)
771 subsys
= parent_group
->cg_subsys
;
772 else if (configfs_is_root(&parent_group
->cg_item
))
773 subsys
= to_configfs_subsystem(group
);
776 group
->cg_subsys
= subsys
;
778 list_for_each_entry(new_group
, &group
->default_groups
, group_entry
)
779 link_group(group
, new_group
);
783 * The goal is that configfs_attach_item() (and
784 * configfs_attach_group()) can be called from either the VFS or this
785 * module. That is, they assume that the items have been created,
786 * the dentry allocated, and the dcache is all ready to go.
788 * If they fail, they must clean up after themselves as if they
789 * had never been called. The caller (VFS or local function) will
790 * handle cleaning up the dcache bits.
792 * configfs_detach_group() and configfs_detach_item() behave similarly on
793 * the way out. They assume that the proper semaphores are held, they
794 * clean up the configfs items, and they expect their callers will
795 * handle the dcache bits.
797 static int configfs_attach_item(struct config_item
*parent_item
,
798 struct config_item
*item
,
799 struct dentry
*dentry
)
803 ret
= configfs_create_dir(item
, dentry
);
805 ret
= populate_attrs(item
);
808 * We are going to remove an inode and its dentry but
809 * the VFS may already have hit and used them. Thus,
810 * we must lock them as rmdir() would.
812 inode_lock(d_inode(dentry
));
813 configfs_remove_dir(item
);
814 d_inode(dentry
)->i_flags
|= S_DEAD
;
816 inode_unlock(d_inode(dentry
));
824 /* Caller holds the mutex of the item's inode */
825 static void configfs_detach_item(struct config_item
*item
)
828 configfs_remove_dir(item
);
831 static int configfs_attach_group(struct config_item
*parent_item
,
832 struct config_item
*item
,
833 struct dentry
*dentry
)
836 struct configfs_dirent
*sd
;
838 ret
= configfs_attach_item(parent_item
, item
, dentry
);
840 sd
= dentry
->d_fsdata
;
841 sd
->s_type
|= CONFIGFS_USET_DIR
;
844 * FYI, we're faking mkdir in populate_groups()
845 * We must lock the group's inode to avoid races with the VFS
846 * which can already hit the inode and try to add/remove entries
849 * We must also lock the inode to remove it safely in case of
850 * error, as rmdir() would.
852 inode_lock_nested(d_inode(dentry
), I_MUTEX_CHILD
);
853 configfs_adjust_dir_dirent_depth_before_populate(sd
);
854 ret
= populate_groups(to_config_group(item
));
856 configfs_detach_item(item
);
857 d_inode(dentry
)->i_flags
|= S_DEAD
;
860 configfs_adjust_dir_dirent_depth_after_populate(sd
);
861 inode_unlock(d_inode(dentry
));
869 /* Caller holds the mutex of the group's inode */
870 static void configfs_detach_group(struct config_item
*item
)
872 detach_groups(to_config_group(item
));
873 configfs_detach_item(item
);
877 * After the item has been detached from the filesystem view, we are
878 * ready to tear it out of the hierarchy. Notify the client before
879 * we do that so they can perform any cleanup that requires
880 * navigating the hierarchy. A client does not need to provide this
881 * callback. The subsystem semaphore MUST be held by the caller, and
882 * references must be valid for both items. It also assumes the
883 * caller has validated ci_type.
885 static void client_disconnect_notify(struct config_item
*parent_item
,
886 struct config_item
*item
)
888 const struct config_item_type
*type
;
890 type
= parent_item
->ci_type
;
893 if (type
->ct_group_ops
&& type
->ct_group_ops
->disconnect_notify
)
894 type
->ct_group_ops
->disconnect_notify(to_config_group(parent_item
),
899 * Drop the initial reference from make_item()/make_group()
900 * This function assumes that reference is held on item
901 * and that item holds a valid reference to the parent. Also, it
902 * assumes the caller has validated ci_type.
904 static void client_drop_item(struct config_item
*parent_item
,
905 struct config_item
*item
)
907 const struct config_item_type
*type
;
909 type
= parent_item
->ci_type
;
913 * If ->drop_item() exists, it is responsible for the
916 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
917 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
920 config_item_put(item
);
924 static void configfs_dump_one(struct configfs_dirent
*sd
, int level
)
926 pr_info("%*s\"%s\":\n", level
, " ", configfs_get_name(sd
));
928 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
929 type_print(CONFIGFS_ROOT
);
930 type_print(CONFIGFS_DIR
);
931 type_print(CONFIGFS_ITEM_ATTR
);
932 type_print(CONFIGFS_ITEM_LINK
);
933 type_print(CONFIGFS_USET_DIR
);
934 type_print(CONFIGFS_USET_DEFAULT
);
935 type_print(CONFIGFS_USET_DROPPING
);
939 static int configfs_dump(struct configfs_dirent
*sd
, int level
)
941 struct configfs_dirent
*child_sd
;
944 configfs_dump_one(sd
, level
);
946 if (!(sd
->s_type
& (CONFIGFS_DIR
|CONFIGFS_ROOT
)))
949 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
950 ret
= configfs_dump(child_sd
, level
+ 2);
961 * configfs_depend_item() and configfs_undepend_item()
963 * WARNING: Do not call these from a configfs callback!
965 * This describes these functions and their helpers.
967 * Allow another kernel system to depend on a config_item. If this
968 * happens, the item cannot go away until the dependent can live without
969 * it. The idea is to give client modules as simple an interface as
970 * possible. When a system asks them to depend on an item, they just
971 * call configfs_depend_item(). If the item is live and the client
972 * driver is in good shape, we'll happily do the work for them.
974 * Why is the locking complex? Because configfs uses the VFS to handle
975 * all locking, but this function is called outside the normal
976 * VFS->configfs path. So it must take VFS locks to prevent the
977 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
978 * why you can't call these functions underneath configfs callbacks.
980 * Note, btw, that this can be called at *any* time, even when a configfs
981 * subsystem isn't registered, or when configfs is loading or unloading.
982 * Just like configfs_register_subsystem(). So we take the same
983 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
984 * If we can find the target item in the
985 * configfs tree, it must be part of the subsystem tree as well, so we
986 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
987 * locking out mkdir() and rmdir(), who might be racing us.
991 * configfs_depend_prep()
993 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
994 * attributes. This is similar but not the same to configfs_detach_prep().
995 * Note that configfs_detach_prep() expects the parent to be locked when it
996 * is called, but we lock the parent *inside* configfs_depend_prep(). We
997 * do that so we can unlock it if we find nothing.
999 * Here we do a depth-first search of the dentry hierarchy looking for
1001 * We deliberately ignore items tagged as dropping since they are virtually
1002 * dead, as well as items in the middle of attachment since they virtually
1003 * do not exist yet. This completes the locking out of racing mkdir() and
1005 * Note: subdirectories in the middle of attachment start with s_type =
1006 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1007 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1008 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1010 * If the target is not found, -ENOENT is bubbled up.
1012 * This adds a requirement that all config_items be unique!
1014 * This is recursive. There isn't
1015 * much on the stack, though, so folks that need this function - be careful
1016 * about your stack! Patches will be accepted to make it iterative.
1018 static int configfs_depend_prep(struct dentry
*origin
,
1019 struct config_item
*target
)
1021 struct configfs_dirent
*child_sd
, *sd
;
1024 BUG_ON(!origin
|| !origin
->d_fsdata
);
1025 sd
= origin
->d_fsdata
;
1027 if (sd
->s_element
== target
) /* Boo-yah */
1030 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
1031 if ((child_sd
->s_type
& CONFIGFS_DIR
) &&
1032 !(child_sd
->s_type
& CONFIGFS_USET_DROPPING
) &&
1033 !(child_sd
->s_type
& CONFIGFS_USET_CREATING
)) {
1034 ret
= configfs_depend_prep(child_sd
->s_dentry
,
1037 goto out
; /* Child path boo-yah */
1041 /* We looped all our children and didn't find target */
1048 static int configfs_do_depend_item(struct dentry
*subsys_dentry
,
1049 struct config_item
*target
)
1051 struct configfs_dirent
*p
;
1054 spin_lock(&configfs_dirent_lock
);
1055 /* Scan the tree, return 0 if found */
1056 ret
= configfs_depend_prep(subsys_dentry
, target
);
1058 goto out_unlock_dirent_lock
;
1061 * We are sure that the item is not about to be removed by rmdir(), and
1062 * not in the middle of attachment by mkdir().
1064 p
= target
->ci_dentry
->d_fsdata
;
1065 p
->s_dependent_count
+= 1;
1067 out_unlock_dirent_lock
:
1068 spin_unlock(&configfs_dirent_lock
);
1073 static inline struct configfs_dirent
*
1074 configfs_find_subsys_dentry(struct configfs_dirent
*root_sd
,
1075 struct config_item
*subsys_item
)
1077 struct configfs_dirent
*p
;
1078 struct configfs_dirent
*ret
= NULL
;
1080 list_for_each_entry(p
, &root_sd
->s_children
, s_sibling
) {
1081 if (p
->s_type
& CONFIGFS_DIR
&&
1082 p
->s_element
== subsys_item
) {
1092 int configfs_depend_item(struct configfs_subsystem
*subsys
,
1093 struct config_item
*target
)
1096 struct configfs_dirent
*subsys_sd
;
1097 struct config_item
*s_item
= &subsys
->su_group
.cg_item
;
1098 struct dentry
*root
;
1101 * Pin the configfs filesystem. This means we can safely access
1102 * the root of the configfs filesystem.
1104 root
= configfs_pin_fs();
1106 return PTR_ERR(root
);
1109 * Next, lock the root directory. We're going to check that the
1110 * subsystem is really registered, and so we need to lock out
1111 * configfs_[un]register_subsystem().
1113 inode_lock(d_inode(root
));
1115 subsys_sd
= configfs_find_subsys_dentry(root
->d_fsdata
, s_item
);
1121 /* Ok, now we can trust subsys/s_item */
1122 ret
= configfs_do_depend_item(subsys_sd
->s_dentry
, target
);
1125 inode_unlock(d_inode(root
));
1128 * If we succeeded, the fs is pinned via other methods. If not,
1129 * we're done with it anyway. So release_fs() is always right.
1131 configfs_release_fs();
1135 EXPORT_SYMBOL(configfs_depend_item
);
1138 * Release the dependent linkage. This is much simpler than
1139 * configfs_depend_item() because we know that that the client driver is
1140 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1142 void configfs_undepend_item(struct config_item
*target
)
1144 struct configfs_dirent
*sd
;
1147 * Since we can trust everything is pinned, we just need
1148 * configfs_dirent_lock.
1150 spin_lock(&configfs_dirent_lock
);
1152 sd
= target
->ci_dentry
->d_fsdata
;
1153 BUG_ON(sd
->s_dependent_count
< 1);
1155 sd
->s_dependent_count
-= 1;
1158 * After this unlock, we cannot trust the item to stay alive!
1159 * DO NOT REFERENCE item after this unlock.
1161 spin_unlock(&configfs_dirent_lock
);
1163 EXPORT_SYMBOL(configfs_undepend_item
);
1166 * caller_subsys is a caller's subsystem not target's. This is used to
1167 * determine if we should lock root and check subsys or not. When we are
1168 * in the same subsystem as our target there is no need to do locking as
1169 * we know that subsys is valid and is not unregistered during this function
1170 * as we are called from callback of one of his children and VFS holds a lock
1171 * on some inode. Otherwise we have to lock our root to ensure that target's
1172 * subsystem it is not unregistered during this function.
1174 int configfs_depend_item_unlocked(struct configfs_subsystem
*caller_subsys
,
1175 struct config_item
*target
)
1177 struct configfs_subsystem
*target_subsys
;
1178 struct config_group
*root
, *parent
;
1179 struct configfs_dirent
*subsys_sd
;
1182 /* Disallow this function for configfs root */
1183 if (configfs_is_root(target
))
1186 parent
= target
->ci_group
;
1188 * This may happen when someone is trying to depend root
1189 * directory of some subsystem
1191 if (configfs_is_root(&parent
->cg_item
)) {
1192 target_subsys
= to_configfs_subsystem(to_config_group(target
));
1195 target_subsys
= parent
->cg_subsys
;
1196 /* Find a cofnigfs root as we may need it for locking */
1197 for (root
= parent
; !configfs_is_root(&root
->cg_item
);
1198 root
= root
->cg_item
.ci_group
)
1202 if (target_subsys
!= caller_subsys
) {
1204 * We are in other configfs subsystem, so we have to do
1205 * additional locking to prevent other subsystem from being
1208 inode_lock(d_inode(root
->cg_item
.ci_dentry
));
1211 * As we are trying to depend item from other subsystem
1212 * we have to check if this subsystem is still registered
1214 subsys_sd
= configfs_find_subsys_dentry(
1215 root
->cg_item
.ci_dentry
->d_fsdata
,
1216 &target_subsys
->su_group
.cg_item
);
1218 goto out_root_unlock
;
1220 subsys_sd
= target_subsys
->su_group
.cg_item
.ci_dentry
->d_fsdata
;
1223 /* Now we can execute core of depend item */
1224 ret
= configfs_do_depend_item(subsys_sd
->s_dentry
, target
);
1226 if (target_subsys
!= caller_subsys
)
1229 * We were called from subsystem other than our target so we
1230 * took some locks so now it's time to release them
1232 inode_unlock(d_inode(root
->cg_item
.ci_dentry
));
1236 EXPORT_SYMBOL(configfs_depend_item_unlocked
);
1238 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
1242 struct config_group
*group
= NULL
;
1243 struct config_item
*item
= NULL
;
1244 struct config_item
*parent_item
;
1245 struct configfs_subsystem
*subsys
;
1246 struct configfs_dirent
*sd
;
1247 const struct config_item_type
*type
;
1248 struct module
*subsys_owner
= NULL
, *new_item_owner
= NULL
;
1251 sd
= dentry
->d_parent
->d_fsdata
;
1254 * Fake invisibility if dir belongs to a group/default groups hierarchy
1257 if (!configfs_dirent_is_ready(sd
)) {
1262 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
1267 /* Get a working ref for the duration of this function */
1268 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1269 type
= parent_item
->ci_type
;
1270 subsys
= to_config_group(parent_item
)->cg_subsys
;
1273 if (!type
|| !type
->ct_group_ops
||
1274 (!type
->ct_group_ops
->make_group
&&
1275 !type
->ct_group_ops
->make_item
)) {
1276 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
1281 * The subsystem may belong to a different module than the item
1282 * being created. We don't want to safely pin the new item but
1283 * fail to pin the subsystem it sits under.
1285 if (!subsys
->su_group
.cg_item
.ci_type
) {
1289 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1290 if (!try_module_get(subsys_owner
)) {
1295 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
1298 goto out_subsys_put
;
1301 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
1303 mutex_lock(&subsys
->su_mutex
);
1304 if (type
->ct_group_ops
->make_group
) {
1305 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
1307 group
= ERR_PTR(-ENOMEM
);
1308 if (!IS_ERR(group
)) {
1309 link_group(to_config_group(parent_item
), group
);
1310 item
= &group
->cg_item
;
1312 ret
= PTR_ERR(group
);
1314 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
1316 item
= ERR_PTR(-ENOMEM
);
1318 link_obj(parent_item
, item
);
1320 ret
= PTR_ERR(item
);
1322 mutex_unlock(&subsys
->su_mutex
);
1327 * If ret != 0, then link_obj() was never called.
1328 * There are no extra references to clean up.
1330 goto out_subsys_put
;
1334 * link_obj() has been called (via link_group() for groups).
1335 * From here on out, errors must clean that up.
1338 type
= item
->ci_type
;
1344 new_item_owner
= type
->ct_owner
;
1345 if (!try_module_get(new_item_owner
)) {
1351 * I hate doing it this way, but if there is
1352 * an error, module_put() probably should
1353 * happen after any cleanup.
1358 * Make racing rmdir() fail if it did not tag parent with
1359 * CONFIGFS_USET_DROPPING
1360 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1361 * fail and let rmdir() terminate correctly
1363 spin_lock(&configfs_dirent_lock
);
1364 /* This will make configfs_detach_prep() fail */
1365 sd
->s_type
|= CONFIGFS_USET_IN_MKDIR
;
1366 spin_unlock(&configfs_dirent_lock
);
1369 ret
= configfs_attach_group(parent_item
, item
, dentry
);
1371 ret
= configfs_attach_item(parent_item
, item
, dentry
);
1373 spin_lock(&configfs_dirent_lock
);
1374 sd
->s_type
&= ~CONFIGFS_USET_IN_MKDIR
;
1376 configfs_dir_set_ready(dentry
->d_fsdata
);
1377 spin_unlock(&configfs_dirent_lock
);
1381 /* Tear down everything we built up */
1382 mutex_lock(&subsys
->su_mutex
);
1384 client_disconnect_notify(parent_item
, item
);
1386 unlink_group(group
);
1389 client_drop_item(parent_item
, item
);
1391 mutex_unlock(&subsys
->su_mutex
);
1394 module_put(new_item_owner
);
1399 module_put(subsys_owner
);
1403 * link_obj()/link_group() took a reference from child->parent,
1404 * so the parent is safely pinned. We can drop our working
1407 config_item_put(parent_item
);
1413 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1415 struct config_item
*parent_item
;
1416 struct config_item
*item
;
1417 struct configfs_subsystem
*subsys
;
1418 struct configfs_dirent
*sd
;
1419 struct module
*subsys_owner
= NULL
, *dead_item_owner
= NULL
;
1422 sd
= dentry
->d_fsdata
;
1423 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
1426 /* Get a working ref until we have the child */
1427 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1428 subsys
= to_config_group(parent_item
)->cg_subsys
;
1431 if (!parent_item
->ci_type
) {
1432 config_item_put(parent_item
);
1436 /* configfs_mkdir() shouldn't have allowed this */
1437 BUG_ON(!subsys
->su_group
.cg_item
.ci_type
);
1438 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1441 * Ensure that no racing symlink() will make detach_prep() fail while
1442 * the new link is temporarily attached
1445 struct dentry
*wait
;
1447 mutex_lock(&configfs_symlink_mutex
);
1448 spin_lock(&configfs_dirent_lock
);
1450 * Here's where we check for dependents. We're protected by
1451 * configfs_dirent_lock.
1452 * If no dependent, atomically tag the item as dropping.
1454 ret
= sd
->s_dependent_count
? -EBUSY
: 0;
1456 ret
= configfs_detach_prep(dentry
, &wait
);
1458 configfs_detach_rollback(dentry
);
1460 spin_unlock(&configfs_dirent_lock
);
1461 mutex_unlock(&configfs_symlink_mutex
);
1464 if (ret
!= -EAGAIN
) {
1465 config_item_put(parent_item
);
1469 /* Wait until the racing operation terminates */
1470 inode_lock(d_inode(wait
));
1471 inode_unlock(d_inode(wait
));
1474 } while (ret
== -EAGAIN
);
1476 /* Get a working ref for the duration of this function */
1477 item
= configfs_get_config_item(dentry
);
1479 /* Drop reference from above, item already holds one. */
1480 config_item_put(parent_item
);
1483 dead_item_owner
= item
->ci_type
->ct_owner
;
1485 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
1486 configfs_detach_group(item
);
1488 mutex_lock(&subsys
->su_mutex
);
1489 client_disconnect_notify(parent_item
, item
);
1490 unlink_group(to_config_group(item
));
1492 configfs_detach_item(item
);
1494 mutex_lock(&subsys
->su_mutex
);
1495 client_disconnect_notify(parent_item
, item
);
1499 client_drop_item(parent_item
, item
);
1500 mutex_unlock(&subsys
->su_mutex
);
1502 /* Drop our reference from above */
1503 config_item_put(item
);
1505 module_put(dead_item_owner
);
1506 module_put(subsys_owner
);
1511 const struct inode_operations configfs_dir_inode_operations
= {
1512 .mkdir
= configfs_mkdir
,
1513 .rmdir
= configfs_rmdir
,
1514 .symlink
= configfs_symlink
,
1515 .unlink
= configfs_unlink
,
1516 .lookup
= configfs_lookup
,
1517 .setattr
= configfs_setattr
,
1520 const struct inode_operations configfs_root_inode_operations
= {
1521 .lookup
= configfs_lookup
,
1522 .setattr
= configfs_setattr
,
1526 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
1529 struct dentry
* new_dentry
, * parent
;
1531 if (!strcmp(config_item_name(item
), new_name
))
1537 down_write(&configfs_rename_sem
);
1538 parent
= item
->parent
->dentry
;
1540 inode_lock(d_inode(parent
));
1542 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
1543 if (!IS_ERR(new_dentry
)) {
1544 if (d_really_is_negative(new_dentry
)) {
1545 error
= config_item_set_name(item
, "%s", new_name
);
1547 d_add(new_dentry
, NULL
);
1548 d_move(item
->dentry
, new_dentry
);
1551 d_delete(new_dentry
);
1556 inode_unlock(d_inode(parent
));
1557 up_write(&configfs_rename_sem
);
1563 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
1565 struct dentry
* dentry
= file
->f_path
.dentry
;
1566 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1569 inode_lock(d_inode(dentry
));
1571 * Fake invisibility if dir belongs to a group/default groups hierarchy
1575 if (configfs_dirent_is_ready(parent_sd
)) {
1576 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
, 0);
1577 if (IS_ERR(file
->private_data
))
1578 err
= PTR_ERR(file
->private_data
);
1582 inode_unlock(d_inode(dentry
));
1587 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
1589 struct dentry
* dentry
= file
->f_path
.dentry
;
1590 struct configfs_dirent
* cursor
= file
->private_data
;
1592 inode_lock(d_inode(dentry
));
1593 spin_lock(&configfs_dirent_lock
);
1594 list_del_init(&cursor
->s_sibling
);
1595 spin_unlock(&configfs_dirent_lock
);
1596 inode_unlock(d_inode(dentry
));
1598 release_configfs_dirent(cursor
);
1603 /* Relationship between s_mode and the DT_xxx types */
1604 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1606 return (sd
->s_mode
>> 12) & 15;
1609 static int configfs_readdir(struct file
*file
, struct dir_context
*ctx
)
1611 struct dentry
*dentry
= file
->f_path
.dentry
;
1612 struct super_block
*sb
= dentry
->d_sb
;
1613 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1614 struct configfs_dirent
*cursor
= file
->private_data
;
1615 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1618 if (!dir_emit_dots(file
, ctx
))
1620 spin_lock(&configfs_dirent_lock
);
1622 list_move(q
, &parent_sd
->s_children
);
1623 for (p
= q
->next
; p
!= &parent_sd
->s_children
; p
= p
->next
) {
1624 struct configfs_dirent
*next
;
1627 struct inode
*inode
= NULL
;
1629 next
= list_entry(p
, struct configfs_dirent
, s_sibling
);
1630 if (!next
->s_element
)
1634 * We'll have a dentry and an inode for
1635 * PINNED items and for open attribute
1636 * files. We lock here to prevent a race
1637 * with configfs_d_iput() clearing
1638 * s_dentry before calling iput().
1640 * Why do we go to the trouble? If
1641 * someone has an attribute file open,
1642 * the inode number should match until
1643 * they close it. Beyond that, we don't
1646 dentry
= next
->s_dentry
;
1648 inode
= d_inode(dentry
);
1651 spin_unlock(&configfs_dirent_lock
);
1653 ino
= iunique(sb
, 2);
1655 name
= configfs_get_name(next
);
1658 if (!dir_emit(ctx
, name
, len
, ino
, dt_type(next
)))
1661 spin_lock(&configfs_dirent_lock
);
1666 spin_unlock(&configfs_dirent_lock
);
1670 static loff_t
configfs_dir_lseek(struct file
*file
, loff_t offset
, int whence
)
1672 struct dentry
* dentry
= file
->f_path
.dentry
;
1676 offset
+= file
->f_pos
;
1685 if (offset
!= file
->f_pos
) {
1686 file
->f_pos
= offset
;
1687 if (file
->f_pos
>= 2) {
1688 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1689 struct configfs_dirent
*cursor
= file
->private_data
;
1690 struct list_head
*p
;
1691 loff_t n
= file
->f_pos
- 2;
1693 spin_lock(&configfs_dirent_lock
);
1694 list_del(&cursor
->s_sibling
);
1695 p
= sd
->s_children
.next
;
1696 while (n
&& p
!= &sd
->s_children
) {
1697 struct configfs_dirent
*next
;
1698 next
= list_entry(p
, struct configfs_dirent
,
1700 if (next
->s_element
)
1704 list_add_tail(&cursor
->s_sibling
, p
);
1705 spin_unlock(&configfs_dirent_lock
);
1711 const struct file_operations configfs_dir_operations
= {
1712 .open
= configfs_dir_open
,
1713 .release
= configfs_dir_close
,
1714 .llseek
= configfs_dir_lseek
,
1715 .read
= generic_read_dir
,
1716 .iterate_shared
= configfs_readdir
,
1720 * configfs_register_group - creates a parent-child relation between two groups
1721 * @parent_group: parent group
1722 * @group: child group
1724 * link groups, creates dentry for the child and attaches it to the
1727 * Return: 0 on success, negative errno code on error
1729 int configfs_register_group(struct config_group
*parent_group
,
1730 struct config_group
*group
)
1732 struct configfs_subsystem
*subsys
= parent_group
->cg_subsys
;
1733 struct dentry
*parent
;
1736 mutex_lock(&subsys
->su_mutex
);
1737 link_group(parent_group
, group
);
1738 mutex_unlock(&subsys
->su_mutex
);
1740 parent
= parent_group
->cg_item
.ci_dentry
;
1742 inode_lock_nested(d_inode(parent
), I_MUTEX_PARENT
);
1743 ret
= create_default_group(parent_group
, group
);
1747 spin_lock(&configfs_dirent_lock
);
1748 configfs_dir_set_ready(group
->cg_item
.ci_dentry
->d_fsdata
);
1749 spin_unlock(&configfs_dirent_lock
);
1750 inode_unlock(d_inode(parent
));
1753 inode_unlock(d_inode(parent
));
1754 mutex_lock(&subsys
->su_mutex
);
1755 unlink_group(group
);
1756 mutex_unlock(&subsys
->su_mutex
);
1759 EXPORT_SYMBOL(configfs_register_group
);
1762 * configfs_unregister_group() - unregisters a child group from its parent
1763 * @group: parent group to be unregistered
1765 * Undoes configfs_register_group()
1767 void configfs_unregister_group(struct config_group
*group
)
1769 struct configfs_subsystem
*subsys
= group
->cg_subsys
;
1770 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1771 struct dentry
*parent
= group
->cg_item
.ci_parent
->ci_dentry
;
1773 mutex_lock(&subsys
->su_mutex
);
1774 if (!group
->cg_item
.ci_parent
->ci_group
) {
1776 * The parent has already been unlinked and detached
1781 mutex_unlock(&subsys
->su_mutex
);
1783 inode_lock_nested(d_inode(parent
), I_MUTEX_PARENT
);
1784 spin_lock(&configfs_dirent_lock
);
1785 configfs_detach_prep(dentry
, NULL
);
1786 spin_unlock(&configfs_dirent_lock
);
1788 configfs_detach_group(&group
->cg_item
);
1789 d_inode(dentry
)->i_flags
|= S_DEAD
;
1792 inode_unlock(d_inode(parent
));
1796 mutex_lock(&subsys
->su_mutex
);
1798 unlink_group(group
);
1799 mutex_unlock(&subsys
->su_mutex
);
1801 EXPORT_SYMBOL(configfs_unregister_group
);
1804 * configfs_register_default_group() - allocates and registers a child group
1805 * @parent_group: parent group
1806 * @name: child group name
1807 * @item_type: child item type description
1809 * boilerplate to allocate and register a child group with its parent. We need
1810 * kzalloc'ed memory because child's default_group is initially empty.
1812 * Return: allocated config group or ERR_PTR() on error
1814 struct config_group
*
1815 configfs_register_default_group(struct config_group
*parent_group
,
1817 const struct config_item_type
*item_type
)
1820 struct config_group
*group
;
1822 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
1824 return ERR_PTR(-ENOMEM
);
1825 config_group_init_type_name(group
, name
, item_type
);
1827 ret
= configfs_register_group(parent_group
, group
);
1830 return ERR_PTR(ret
);
1834 EXPORT_SYMBOL(configfs_register_default_group
);
1837 * configfs_unregister_default_group() - unregisters and frees a child group
1838 * @group: the group to act on
1840 void configfs_unregister_default_group(struct config_group
*group
)
1842 configfs_unregister_group(group
);
1845 EXPORT_SYMBOL(configfs_unregister_default_group
);
1847 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1850 struct config_group
*group
= &subsys
->su_group
;
1851 struct dentry
*dentry
;
1852 struct dentry
*root
;
1853 struct configfs_dirent
*sd
;
1855 root
= configfs_pin_fs();
1857 return PTR_ERR(root
);
1859 if (!group
->cg_item
.ci_name
)
1860 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1862 sd
= root
->d_fsdata
;
1863 link_group(to_config_group(sd
->s_element
), group
);
1865 inode_lock_nested(d_inode(root
), I_MUTEX_PARENT
);
1868 dentry
= d_alloc_name(root
, group
->cg_item
.ci_name
);
1870 d_add(dentry
, NULL
);
1872 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1875 BUG_ON(d_inode(dentry
));
1879 spin_lock(&configfs_dirent_lock
);
1880 configfs_dir_set_ready(dentry
->d_fsdata
);
1881 spin_unlock(&configfs_dirent_lock
);
1885 inode_unlock(d_inode(root
));
1888 unlink_group(group
);
1889 configfs_release_fs();
1895 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1897 struct config_group
*group
= &subsys
->su_group
;
1898 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1899 struct dentry
*root
= dentry
->d_sb
->s_root
;
1901 if (dentry
->d_parent
!= root
) {
1902 pr_err("Tried to unregister non-subsystem!\n");
1906 inode_lock_nested(d_inode(root
),
1908 inode_lock_nested(d_inode(dentry
), I_MUTEX_CHILD
);
1909 mutex_lock(&configfs_symlink_mutex
);
1910 spin_lock(&configfs_dirent_lock
);
1911 if (configfs_detach_prep(dentry
, NULL
)) {
1912 pr_err("Tried to unregister non-empty subsystem!\n");
1914 spin_unlock(&configfs_dirent_lock
);
1915 mutex_unlock(&configfs_symlink_mutex
);
1916 configfs_detach_group(&group
->cg_item
);
1917 d_inode(dentry
)->i_flags
|= S_DEAD
;
1919 inode_unlock(d_inode(dentry
));
1923 inode_unlock(d_inode(root
));
1927 unlink_group(group
);
1928 configfs_release_fs();
1931 EXPORT_SYMBOL(configfs_register_subsystem
);
1932 EXPORT_SYMBOL(configfs_unregister_subsystem
);