1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dir.c - Operations for configfs directories.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
30 #include <linux/mount.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/err.h>
35 #include <linux/configfs.h>
36 #include "configfs_internal.h"
38 DECLARE_RWSEM(configfs_rename_sem
);
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
41 * Also protects mutations of symlinks linkage to target configfs_dirent
42 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
44 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
47 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
48 * unlocked is not reliable unless in detach_groups() called from
49 * rmdir()/unregister() and from configfs_attach_group()
51 DEFINE_SPINLOCK(configfs_dirent_lock
);
53 static void configfs_d_iput(struct dentry
* dentry
,
56 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
59 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock
);
61 /* Coordinate with configfs_attach_attr where will increase
62 * sd->s_count and update sd->s_dentry to new allocated one.
63 * Only set sd->dentry to null when this dentry is the only
65 * If not do so, configfs_d_iput may run just after
66 * configfs_attach_attr and set sd->s_dentry to null
67 * even it's still in use.
69 if (atomic_read(&sd
->s_count
) <= 2)
72 spin_unlock(&configfs_dirent_lock
);
78 const struct dentry_operations configfs_dentry_ops
= {
79 .d_iput
= configfs_d_iput
,
80 .d_delete
= always_delete_dentry
,
86 * Helpers to make lockdep happy with our recursive locking of default groups'
87 * inodes (see configfs_attach_group() and configfs_detach_group()).
88 * We put default groups i_mutexes in separate classes according to their depth
89 * from the youngest non-default group ancestor.
91 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
92 * groups A/B and A/C will have their inode's mutex in class
93 * default_group_class[0], and default group A/C/D will be in
94 * default_group_class[1].
96 * The lock classes are declared and assigned in inode.c, according to the
98 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
99 * default groups, and reset to -1 when all default groups are attached. During
100 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
101 * inode's mutex is set to default_group_class[s_depth - 1].
104 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
109 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
110 struct configfs_dirent
*sd
)
112 int parent_depth
= parent_sd
->s_depth
;
114 if (parent_depth
>= 0)
115 sd
->s_depth
= parent_depth
+ 1;
119 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
122 * item's i_mutex class is already setup, so s_depth is now only
123 * used to set new sub-directories s_depth, which is always done
124 * with item's i_mutex locked.
127 * sd->s_depth == -1 iff we are a non default group.
128 * else (we are a default group) sd->s_depth > 0 (see
131 if (sd
->s_depth
== -1)
133 * We are a non default group and we are going to create
140 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
142 /* We will not create default groups anymore. */
146 #else /* CONFIG_LOCKDEP */
148 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
152 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
153 struct configfs_dirent
*sd
)
158 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
163 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
167 #endif /* CONFIG_LOCKDEP */
170 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
172 static struct configfs_dirent
*configfs_new_dirent(struct configfs_dirent
*parent_sd
,
173 void *element
, int type
)
175 struct configfs_dirent
* sd
;
177 sd
= kmem_cache_zalloc(configfs_dir_cachep
, GFP_KERNEL
);
179 return ERR_PTR(-ENOMEM
);
181 atomic_set(&sd
->s_count
, 1);
182 INIT_LIST_HEAD(&sd
->s_links
);
183 INIT_LIST_HEAD(&sd
->s_children
);
184 sd
->s_element
= element
;
186 configfs_init_dirent_depth(sd
);
187 spin_lock(&configfs_dirent_lock
);
188 if (parent_sd
->s_type
& CONFIGFS_USET_DROPPING
) {
189 spin_unlock(&configfs_dirent_lock
);
190 kmem_cache_free(configfs_dir_cachep
, sd
);
191 return ERR_PTR(-ENOENT
);
193 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
194 spin_unlock(&configfs_dirent_lock
);
201 * Return -EEXIST if there is already a configfs element with the same
202 * name for the same parent.
204 * called with parent inode's i_mutex held
206 static int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
207 const unsigned char *new)
209 struct configfs_dirent
* sd
;
211 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
213 const unsigned char *existing
= configfs_get_name(sd
);
214 if (strcmp(existing
, new))
225 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
226 struct dentry
* dentry
, void * element
,
227 umode_t mode
, int type
)
229 struct configfs_dirent
* sd
;
231 sd
= configfs_new_dirent(parent_sd
, element
, type
);
236 sd
->s_dentry
= dentry
;
238 dentry
->d_fsdata
= configfs_get(sd
);
243 static void init_dir(struct inode
* inode
)
245 inode
->i_op
= &configfs_dir_inode_operations
;
246 inode
->i_fop
= &configfs_dir_operations
;
248 /* directory inodes start off with i_nlink == 2 (for "." entry) */
252 static void configfs_init_file(struct inode
* inode
)
254 inode
->i_size
= PAGE_SIZE
;
255 inode
->i_fop
= &configfs_file_operations
;
258 static void configfs_init_bin_file(struct inode
*inode
)
261 inode
->i_fop
= &configfs_bin_file_operations
;
264 static void init_symlink(struct inode
* inode
)
266 inode
->i_op
= &configfs_symlink_inode_operations
;
270 * configfs_create_dir - create a directory for an config_item.
271 * @item: config_itemwe're creating directory for.
272 * @dentry: config_item's dentry.
274 * Note: user-created entries won't be allowed under this new directory
275 * until it is validated by configfs_dir_set_ready()
278 static int configfs_create_dir(struct config_item
*item
, struct dentry
*dentry
)
281 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
282 struct dentry
*p
= dentry
->d_parent
;
286 error
= configfs_dirent_exists(p
->d_fsdata
, dentry
->d_name
.name
);
290 error
= configfs_make_dirent(p
->d_fsdata
, dentry
, item
, mode
,
291 CONFIGFS_DIR
| CONFIGFS_USET_CREATING
);
295 configfs_set_dir_dirent_depth(p
->d_fsdata
, dentry
->d_fsdata
);
296 error
= configfs_create(dentry
, mode
, init_dir
);
298 inc_nlink(d_inode(p
));
299 item
->ci_dentry
= dentry
;
301 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
303 spin_lock(&configfs_dirent_lock
);
304 list_del_init(&sd
->s_sibling
);
305 spin_unlock(&configfs_dirent_lock
);
313 * Allow userspace to create new entries under a new directory created with
314 * configfs_create_dir(), and under all of its chidlren directories recursively.
315 * @sd configfs_dirent of the new directory to validate
317 * Caller must hold configfs_dirent_lock.
319 static void configfs_dir_set_ready(struct configfs_dirent
*sd
)
321 struct configfs_dirent
*child_sd
;
323 sd
->s_type
&= ~CONFIGFS_USET_CREATING
;
324 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
)
325 if (child_sd
->s_type
& CONFIGFS_USET_CREATING
)
326 configfs_dir_set_ready(child_sd
);
330 * Check that a directory does not belong to a directory hierarchy being
331 * attached and not validated yet.
332 * @sd configfs_dirent of the directory to check
334 * @return non-zero iff the directory was validated
336 * Note: takes configfs_dirent_lock, so the result may change from false to true
337 * in two consecutive calls, but never from true to false.
339 int configfs_dirent_is_ready(struct configfs_dirent
*sd
)
343 spin_lock(&configfs_dirent_lock
);
344 ret
= !(sd
->s_type
& CONFIGFS_USET_CREATING
);
345 spin_unlock(&configfs_dirent_lock
);
350 int configfs_create_link(struct configfs_symlink
*sl
,
351 struct dentry
*parent
,
352 struct dentry
*dentry
)
355 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
357 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
360 err
= configfs_create(dentry
, mode
, init_symlink
);
362 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
364 spin_lock(&configfs_dirent_lock
);
365 list_del_init(&sd
->s_sibling
);
366 spin_unlock(&configfs_dirent_lock
);
374 static void remove_dir(struct dentry
* d
)
376 struct dentry
* parent
= dget(d
->d_parent
);
377 struct configfs_dirent
* sd
;
380 spin_lock(&configfs_dirent_lock
);
381 list_del_init(&sd
->s_sibling
);
382 spin_unlock(&configfs_dirent_lock
);
384 if (d_really_is_positive(d
))
385 simple_rmdir(d_inode(parent
),d
);
387 pr_debug(" o %pd removing done (%d)\n", d
, d_count(d
));
393 * configfs_remove_dir - remove an config_item's directory.
394 * @item: config_item we're removing.
396 * The only thing special about this is that we remove any files in
397 * the directory before we remove the directory, and we've inlined
398 * what used to be configfs_rmdir() below, instead of calling separately.
400 * Caller holds the mutex of the item's inode
403 static void configfs_remove_dir(struct config_item
* item
)
405 struct dentry
* dentry
= dget(item
->ci_dentry
);
412 * Drop reference from dget() on entrance.
418 /* attaches attribute's configfs_dirent to the dentry corresponding to the
421 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
423 struct configfs_attribute
* attr
= sd
->s_element
;
426 spin_lock(&configfs_dirent_lock
);
427 dentry
->d_fsdata
= configfs_get(sd
);
428 sd
->s_dentry
= dentry
;
429 spin_unlock(&configfs_dirent_lock
);
431 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
,
432 (sd
->s_type
& CONFIGFS_ITEM_BIN_ATTR
) ?
433 configfs_init_bin_file
:
445 static struct dentry
* configfs_lookup(struct inode
*dir
,
446 struct dentry
*dentry
,
449 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
450 struct configfs_dirent
* sd
;
455 * Fake invisibility if dir belongs to a group/default groups hierarchy
458 * This forbids userspace to read/write attributes of items which may
459 * not complete their initialization, since the dentries of the
460 * attributes won't be instantiated.
463 if (!configfs_dirent_is_ready(parent_sd
))
466 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
467 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
468 const unsigned char * name
= configfs_get_name(sd
);
470 if (strcmp(name
, dentry
->d_name
.name
))
474 err
= configfs_attach_attr(sd
, dentry
);
481 * If it doesn't exist and it isn't a NOT_PINNED item,
482 * it must be negative.
484 if (dentry
->d_name
.len
> NAME_MAX
)
485 return ERR_PTR(-ENAMETOOLONG
);
495 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
496 * attributes and are removed by rmdir(). We recurse, setting
497 * CONFIGFS_USET_DROPPING on all children that are candidates for
499 * If there is an error, the caller will reset the flags via
500 * configfs_detach_rollback().
502 static int configfs_detach_prep(struct dentry
*dentry
, struct mutex
**wait_mutex
)
504 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
505 struct configfs_dirent
*sd
;
508 /* Mark that we're trying to drop the group */
509 parent_sd
->s_type
|= CONFIGFS_USET_DROPPING
;
512 if (!list_empty(&parent_sd
->s_links
))
516 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
517 if (!sd
->s_element
||
518 (sd
->s_type
& CONFIGFS_NOT_PINNED
))
520 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
521 /* Abort if racing with mkdir() */
522 if (sd
->s_type
& CONFIGFS_USET_IN_MKDIR
) {
524 *wait_mutex
= &d_inode(sd
->s_dentry
)->i_mutex
;
529 * Yup, recursive. If there's a problem, blame
530 * deep nesting of default_groups
532 ret
= configfs_detach_prep(sd
->s_dentry
, wait_mutex
);
546 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
549 static void configfs_detach_rollback(struct dentry
*dentry
)
551 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
552 struct configfs_dirent
*sd
;
554 parent_sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
556 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
)
557 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
558 configfs_detach_rollback(sd
->s_dentry
);
561 static void detach_attrs(struct config_item
* item
)
563 struct dentry
* dentry
= dget(item
->ci_dentry
);
564 struct configfs_dirent
* parent_sd
;
565 struct configfs_dirent
* sd
, * tmp
;
570 pr_debug("configfs %s: dropping attrs for dir\n",
571 dentry
->d_name
.name
);
573 parent_sd
= dentry
->d_fsdata
;
574 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
575 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
577 spin_lock(&configfs_dirent_lock
);
578 list_del_init(&sd
->s_sibling
);
579 spin_unlock(&configfs_dirent_lock
);
580 configfs_drop_dentry(sd
, dentry
);
585 * Drop reference from dget() on entrance.
590 static int populate_attrs(struct config_item
*item
)
592 struct config_item_type
*t
= item
->ci_type
;
593 struct configfs_attribute
*attr
;
594 struct configfs_bin_attribute
*bin_attr
;
601 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
602 if ((error
= configfs_create_file(item
, attr
)))
606 if (t
->ct_bin_attrs
) {
607 for (i
= 0; (bin_attr
= t
->ct_bin_attrs
[i
]) != NULL
; i
++) {
608 error
= configfs_create_bin_file(item
, bin_attr
);
620 static int configfs_attach_group(struct config_item
*parent_item
,
621 struct config_item
*item
,
622 struct dentry
*dentry
);
623 static void configfs_detach_group(struct config_item
*item
);
625 static void detach_groups(struct config_group
*group
)
627 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
628 struct dentry
*child
;
629 struct configfs_dirent
*parent_sd
;
630 struct configfs_dirent
*sd
, *tmp
;
635 parent_sd
= dentry
->d_fsdata
;
636 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
637 if (!sd
->s_element
||
638 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
641 child
= sd
->s_dentry
;
643 inode_lock(d_inode(child
));
645 configfs_detach_group(sd
->s_element
);
646 d_inode(child
)->i_flags
|= S_DEAD
;
649 inode_unlock(d_inode(child
));
656 * Drop reference from dget() on entrance.
662 * This fakes mkdir(2) on a default_groups[] entry. It
663 * creates a dentry, attachs it, and then does fixup
666 * We could, perhaps, tweak our parent's ->mkdir for a minute and
667 * try using vfs_mkdir. Just a thought.
669 static int create_default_group(struct config_group
*parent_group
,
670 struct config_group
*group
)
673 struct configfs_dirent
*sd
;
674 /* We trust the caller holds a reference to parent */
675 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
677 if (!group
->cg_item
.ci_name
)
678 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
681 child
= d_alloc_name(parent
, group
->cg_item
.ci_name
);
685 ret
= configfs_attach_group(&parent_group
->cg_item
,
686 &group
->cg_item
, child
);
688 sd
= child
->d_fsdata
;
689 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
691 BUG_ON(d_inode(child
));
700 static int populate_groups(struct config_group
*group
)
702 struct config_group
*new_group
;
705 list_for_each_entry(new_group
, &group
->default_groups
, group_entry
) {
706 ret
= create_default_group(group
, new_group
);
708 detach_groups(group
);
716 void configfs_remove_default_groups(struct config_group
*group
)
718 struct config_group
*g
, *n
;
720 list_for_each_entry_safe(g
, n
, &group
->default_groups
, group_entry
) {
721 list_del(&g
->group_entry
);
722 config_item_put(&g
->cg_item
);
725 EXPORT_SYMBOL(configfs_remove_default_groups
);
728 * All of link_obj/unlink_obj/link_group/unlink_group require that
729 * subsys->su_mutex is held.
732 static void unlink_obj(struct config_item
*item
)
734 struct config_group
*group
;
736 group
= item
->ci_group
;
738 list_del_init(&item
->ci_entry
);
740 item
->ci_group
= NULL
;
741 item
->ci_parent
= NULL
;
743 /* Drop the reference for ci_entry */
744 config_item_put(item
);
746 /* Drop the reference for ci_parent */
747 config_group_put(group
);
751 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
754 * Parent seems redundant with group, but it makes certain
755 * traversals much nicer.
757 item
->ci_parent
= parent_item
;
760 * We hold a reference on the parent for the child's ci_parent
763 item
->ci_group
= config_group_get(to_config_group(parent_item
));
764 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
767 * We hold a reference on the child for ci_entry on the parent's
770 config_item_get(item
);
773 static void unlink_group(struct config_group
*group
)
775 struct config_group
*new_group
;
777 list_for_each_entry(new_group
, &group
->default_groups
, group_entry
)
778 unlink_group(new_group
);
780 group
->cg_subsys
= NULL
;
781 unlink_obj(&group
->cg_item
);
784 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
786 struct config_group
*new_group
;
787 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
789 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
791 if (parent_group
->cg_subsys
)
792 subsys
= parent_group
->cg_subsys
;
793 else if (configfs_is_root(&parent_group
->cg_item
))
794 subsys
= to_configfs_subsystem(group
);
797 group
->cg_subsys
= subsys
;
799 list_for_each_entry(new_group
, &group
->default_groups
, group_entry
)
800 link_group(group
, new_group
);
804 * The goal is that configfs_attach_item() (and
805 * configfs_attach_group()) can be called from either the VFS or this
806 * module. That is, they assume that the items have been created,
807 * the dentry allocated, and the dcache is all ready to go.
809 * If they fail, they must clean up after themselves as if they
810 * had never been called. The caller (VFS or local function) will
811 * handle cleaning up the dcache bits.
813 * configfs_detach_group() and configfs_detach_item() behave similarly on
814 * the way out. They assume that the proper semaphores are held, they
815 * clean up the configfs items, and they expect their callers will
816 * handle the dcache bits.
818 static int configfs_attach_item(struct config_item
*parent_item
,
819 struct config_item
*item
,
820 struct dentry
*dentry
)
824 ret
= configfs_create_dir(item
, dentry
);
826 ret
= populate_attrs(item
);
829 * We are going to remove an inode and its dentry but
830 * the VFS may already have hit and used them. Thus,
831 * we must lock them as rmdir() would.
833 inode_lock(d_inode(dentry
));
834 configfs_remove_dir(item
);
835 d_inode(dentry
)->i_flags
|= S_DEAD
;
837 inode_unlock(d_inode(dentry
));
845 /* Caller holds the mutex of the item's inode */
846 static void configfs_detach_item(struct config_item
*item
)
849 configfs_remove_dir(item
);
852 static int configfs_attach_group(struct config_item
*parent_item
,
853 struct config_item
*item
,
854 struct dentry
*dentry
)
857 struct configfs_dirent
*sd
;
859 ret
= configfs_attach_item(parent_item
, item
, dentry
);
861 sd
= dentry
->d_fsdata
;
862 sd
->s_type
|= CONFIGFS_USET_DIR
;
865 * FYI, we're faking mkdir in populate_groups()
866 * We must lock the group's inode to avoid races with the VFS
867 * which can already hit the inode and try to add/remove entries
870 * We must also lock the inode to remove it safely in case of
871 * error, as rmdir() would.
873 inode_lock_nested(d_inode(dentry
), I_MUTEX_CHILD
);
874 configfs_adjust_dir_dirent_depth_before_populate(sd
);
875 ret
= populate_groups(to_config_group(item
));
877 configfs_detach_item(item
);
878 d_inode(dentry
)->i_flags
|= S_DEAD
;
881 configfs_adjust_dir_dirent_depth_after_populate(sd
);
882 inode_unlock(d_inode(dentry
));
890 /* Caller holds the mutex of the group's inode */
891 static void configfs_detach_group(struct config_item
*item
)
893 detach_groups(to_config_group(item
));
894 configfs_detach_item(item
);
898 * After the item has been detached from the filesystem view, we are
899 * ready to tear it out of the hierarchy. Notify the client before
900 * we do that so they can perform any cleanup that requires
901 * navigating the hierarchy. A client does not need to provide this
902 * callback. The subsystem semaphore MUST be held by the caller, and
903 * references must be valid for both items. It also assumes the
904 * caller has validated ci_type.
906 static void client_disconnect_notify(struct config_item
*parent_item
,
907 struct config_item
*item
)
909 struct config_item_type
*type
;
911 type
= parent_item
->ci_type
;
914 if (type
->ct_group_ops
&& type
->ct_group_ops
->disconnect_notify
)
915 type
->ct_group_ops
->disconnect_notify(to_config_group(parent_item
),
920 * Drop the initial reference from make_item()/make_group()
921 * This function assumes that reference is held on item
922 * and that item holds a valid reference to the parent. Also, it
923 * assumes the caller has validated ci_type.
925 static void client_drop_item(struct config_item
*parent_item
,
926 struct config_item
*item
)
928 struct config_item_type
*type
;
930 type
= parent_item
->ci_type
;
934 * If ->drop_item() exists, it is responsible for the
937 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
938 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
941 config_item_put(item
);
945 static void configfs_dump_one(struct configfs_dirent
*sd
, int level
)
947 pr_info("%*s\"%s\":\n", level
, " ", configfs_get_name(sd
));
949 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
950 type_print(CONFIGFS_ROOT
);
951 type_print(CONFIGFS_DIR
);
952 type_print(CONFIGFS_ITEM_ATTR
);
953 type_print(CONFIGFS_ITEM_LINK
);
954 type_print(CONFIGFS_USET_DIR
);
955 type_print(CONFIGFS_USET_DEFAULT
);
956 type_print(CONFIGFS_USET_DROPPING
);
960 static int configfs_dump(struct configfs_dirent
*sd
, int level
)
962 struct configfs_dirent
*child_sd
;
965 configfs_dump_one(sd
, level
);
967 if (!(sd
->s_type
& (CONFIGFS_DIR
|CONFIGFS_ROOT
)))
970 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
971 ret
= configfs_dump(child_sd
, level
+ 2);
982 * configfs_depend_item() and configfs_undepend_item()
984 * WARNING: Do not call these from a configfs callback!
986 * This describes these functions and their helpers.
988 * Allow another kernel system to depend on a config_item. If this
989 * happens, the item cannot go away until the dependent can live without
990 * it. The idea is to give client modules as simple an interface as
991 * possible. When a system asks them to depend on an item, they just
992 * call configfs_depend_item(). If the item is live and the client
993 * driver is in good shape, we'll happily do the work for them.
995 * Why is the locking complex? Because configfs uses the VFS to handle
996 * all locking, but this function is called outside the normal
997 * VFS->configfs path. So it must take VFS locks to prevent the
998 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
999 * why you can't call these functions underneath configfs callbacks.
1001 * Note, btw, that this can be called at *any* time, even when a configfs
1002 * subsystem isn't registered, or when configfs is loading or unloading.
1003 * Just like configfs_register_subsystem(). So we take the same
1004 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1005 * If we can find the target item in the
1006 * configfs tree, it must be part of the subsystem tree as well, so we
1007 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1008 * locking out mkdir() and rmdir(), who might be racing us.
1012 * configfs_depend_prep()
1014 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1015 * attributes. This is similar but not the same to configfs_detach_prep().
1016 * Note that configfs_detach_prep() expects the parent to be locked when it
1017 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1018 * do that so we can unlock it if we find nothing.
1020 * Here we do a depth-first search of the dentry hierarchy looking for
1022 * We deliberately ignore items tagged as dropping since they are virtually
1023 * dead, as well as items in the middle of attachment since they virtually
1024 * do not exist yet. This completes the locking out of racing mkdir() and
1026 * Note: subdirectories in the middle of attachment start with s_type =
1027 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1028 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1029 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1031 * If the target is not found, -ENOENT is bubbled up.
1033 * This adds a requirement that all config_items be unique!
1035 * This is recursive. There isn't
1036 * much on the stack, though, so folks that need this function - be careful
1037 * about your stack! Patches will be accepted to make it iterative.
1039 static int configfs_depend_prep(struct dentry
*origin
,
1040 struct config_item
*target
)
1042 struct configfs_dirent
*child_sd
, *sd
;
1045 BUG_ON(!origin
|| !origin
->d_fsdata
);
1046 sd
= origin
->d_fsdata
;
1048 if (sd
->s_element
== target
) /* Boo-yah */
1051 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
1052 if ((child_sd
->s_type
& CONFIGFS_DIR
) &&
1053 !(child_sd
->s_type
& CONFIGFS_USET_DROPPING
) &&
1054 !(child_sd
->s_type
& CONFIGFS_USET_CREATING
)) {
1055 ret
= configfs_depend_prep(child_sd
->s_dentry
,
1058 goto out
; /* Child path boo-yah */
1062 /* We looped all our children and didn't find target */
1069 static int configfs_do_depend_item(struct dentry
*subsys_dentry
,
1070 struct config_item
*target
)
1072 struct configfs_dirent
*p
;
1075 spin_lock(&configfs_dirent_lock
);
1076 /* Scan the tree, return 0 if found */
1077 ret
= configfs_depend_prep(subsys_dentry
, target
);
1079 goto out_unlock_dirent_lock
;
1082 * We are sure that the item is not about to be removed by rmdir(), and
1083 * not in the middle of attachment by mkdir().
1085 p
= target
->ci_dentry
->d_fsdata
;
1086 p
->s_dependent_count
+= 1;
1088 out_unlock_dirent_lock
:
1089 spin_unlock(&configfs_dirent_lock
);
1094 static inline struct configfs_dirent
*
1095 configfs_find_subsys_dentry(struct configfs_dirent
*root_sd
,
1096 struct config_item
*subsys_item
)
1098 struct configfs_dirent
*p
;
1099 struct configfs_dirent
*ret
= NULL
;
1101 list_for_each_entry(p
, &root_sd
->s_children
, s_sibling
) {
1102 if (p
->s_type
& CONFIGFS_DIR
&&
1103 p
->s_element
== subsys_item
) {
1113 int configfs_depend_item(struct configfs_subsystem
*subsys
,
1114 struct config_item
*target
)
1117 struct configfs_dirent
*subsys_sd
;
1118 struct config_item
*s_item
= &subsys
->su_group
.cg_item
;
1119 struct dentry
*root
;
1122 * Pin the configfs filesystem. This means we can safely access
1123 * the root of the configfs filesystem.
1125 root
= configfs_pin_fs();
1127 return PTR_ERR(root
);
1130 * Next, lock the root directory. We're going to check that the
1131 * subsystem is really registered, and so we need to lock out
1132 * configfs_[un]register_subsystem().
1134 inode_lock(d_inode(root
));
1136 subsys_sd
= configfs_find_subsys_dentry(root
->d_fsdata
, s_item
);
1142 /* Ok, now we can trust subsys/s_item */
1143 ret
= configfs_do_depend_item(subsys_sd
->s_dentry
, target
);
1146 inode_unlock(d_inode(root
));
1149 * If we succeeded, the fs is pinned via other methods. If not,
1150 * we're done with it anyway. So release_fs() is always right.
1152 configfs_release_fs();
1156 EXPORT_SYMBOL(configfs_depend_item
);
1159 * Release the dependent linkage. This is much simpler than
1160 * configfs_depend_item() because we know that that the client driver is
1161 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1163 void configfs_undepend_item(struct config_item
*target
)
1165 struct configfs_dirent
*sd
;
1168 * Since we can trust everything is pinned, we just need
1169 * configfs_dirent_lock.
1171 spin_lock(&configfs_dirent_lock
);
1173 sd
= target
->ci_dentry
->d_fsdata
;
1174 BUG_ON(sd
->s_dependent_count
< 1);
1176 sd
->s_dependent_count
-= 1;
1179 * After this unlock, we cannot trust the item to stay alive!
1180 * DO NOT REFERENCE item after this unlock.
1182 spin_unlock(&configfs_dirent_lock
);
1184 EXPORT_SYMBOL(configfs_undepend_item
);
1187 * caller_subsys is a caller's subsystem not target's. This is used to
1188 * determine if we should lock root and check subsys or not. When we are
1189 * in the same subsystem as our target there is no need to do locking as
1190 * we know that subsys is valid and is not unregistered during this function
1191 * as we are called from callback of one of his children and VFS holds a lock
1192 * on some inode. Otherwise we have to lock our root to ensure that target's
1193 * subsystem it is not unregistered during this function.
1195 int configfs_depend_item_unlocked(struct configfs_subsystem
*caller_subsys
,
1196 struct config_item
*target
)
1198 struct configfs_subsystem
*target_subsys
;
1199 struct config_group
*root
, *parent
;
1200 struct configfs_dirent
*subsys_sd
;
1203 /* Disallow this function for configfs root */
1204 if (configfs_is_root(target
))
1207 parent
= target
->ci_group
;
1209 * This may happen when someone is trying to depend root
1210 * directory of some subsystem
1212 if (configfs_is_root(&parent
->cg_item
)) {
1213 target_subsys
= to_configfs_subsystem(to_config_group(target
));
1216 target_subsys
= parent
->cg_subsys
;
1217 /* Find a cofnigfs root as we may need it for locking */
1218 for (root
= parent
; !configfs_is_root(&root
->cg_item
);
1219 root
= root
->cg_item
.ci_group
)
1223 if (target_subsys
!= caller_subsys
) {
1225 * We are in other configfs subsystem, so we have to do
1226 * additional locking to prevent other subsystem from being
1229 inode_lock(d_inode(root
->cg_item
.ci_dentry
));
1232 * As we are trying to depend item from other subsystem
1233 * we have to check if this subsystem is still registered
1235 subsys_sd
= configfs_find_subsys_dentry(
1236 root
->cg_item
.ci_dentry
->d_fsdata
,
1237 &target_subsys
->su_group
.cg_item
);
1239 goto out_root_unlock
;
1241 subsys_sd
= target_subsys
->su_group
.cg_item
.ci_dentry
->d_fsdata
;
1244 /* Now we can execute core of depend item */
1245 ret
= configfs_do_depend_item(subsys_sd
->s_dentry
, target
);
1247 if (target_subsys
!= caller_subsys
)
1250 * We were called from subsystem other than our target so we
1251 * took some locks so now it's time to release them
1253 inode_unlock(d_inode(root
->cg_item
.ci_dentry
));
1257 EXPORT_SYMBOL(configfs_depend_item_unlocked
);
1259 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
1263 struct config_group
*group
= NULL
;
1264 struct config_item
*item
= NULL
;
1265 struct config_item
*parent_item
;
1266 struct configfs_subsystem
*subsys
;
1267 struct configfs_dirent
*sd
;
1268 struct config_item_type
*type
;
1269 struct module
*subsys_owner
= NULL
, *new_item_owner
= NULL
;
1272 sd
= dentry
->d_parent
->d_fsdata
;
1275 * Fake invisibility if dir belongs to a group/default groups hierarchy
1278 if (!configfs_dirent_is_ready(sd
)) {
1283 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
1288 /* Get a working ref for the duration of this function */
1289 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1290 type
= parent_item
->ci_type
;
1291 subsys
= to_config_group(parent_item
)->cg_subsys
;
1294 if (!type
|| !type
->ct_group_ops
||
1295 (!type
->ct_group_ops
->make_group
&&
1296 !type
->ct_group_ops
->make_item
)) {
1297 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
1302 * The subsystem may belong to a different module than the item
1303 * being created. We don't want to safely pin the new item but
1304 * fail to pin the subsystem it sits under.
1306 if (!subsys
->su_group
.cg_item
.ci_type
) {
1310 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1311 if (!try_module_get(subsys_owner
)) {
1316 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
1319 goto out_subsys_put
;
1322 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
1324 mutex_lock(&subsys
->su_mutex
);
1325 if (type
->ct_group_ops
->make_group
) {
1326 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
1328 group
= ERR_PTR(-ENOMEM
);
1329 if (!IS_ERR(group
)) {
1330 link_group(to_config_group(parent_item
), group
);
1331 item
= &group
->cg_item
;
1333 ret
= PTR_ERR(group
);
1335 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
1337 item
= ERR_PTR(-ENOMEM
);
1339 link_obj(parent_item
, item
);
1341 ret
= PTR_ERR(item
);
1343 mutex_unlock(&subsys
->su_mutex
);
1348 * If ret != 0, then link_obj() was never called.
1349 * There are no extra references to clean up.
1351 goto out_subsys_put
;
1355 * link_obj() has been called (via link_group() for groups).
1356 * From here on out, errors must clean that up.
1359 type
= item
->ci_type
;
1365 new_item_owner
= type
->ct_owner
;
1366 if (!try_module_get(new_item_owner
)) {
1372 * I hate doing it this way, but if there is
1373 * an error, module_put() probably should
1374 * happen after any cleanup.
1379 * Make racing rmdir() fail if it did not tag parent with
1380 * CONFIGFS_USET_DROPPING
1381 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1382 * fail and let rmdir() terminate correctly
1384 spin_lock(&configfs_dirent_lock
);
1385 /* This will make configfs_detach_prep() fail */
1386 sd
->s_type
|= CONFIGFS_USET_IN_MKDIR
;
1387 spin_unlock(&configfs_dirent_lock
);
1390 ret
= configfs_attach_group(parent_item
, item
, dentry
);
1392 ret
= configfs_attach_item(parent_item
, item
, dentry
);
1394 spin_lock(&configfs_dirent_lock
);
1395 sd
->s_type
&= ~CONFIGFS_USET_IN_MKDIR
;
1397 configfs_dir_set_ready(dentry
->d_fsdata
);
1398 spin_unlock(&configfs_dirent_lock
);
1402 /* Tear down everything we built up */
1403 mutex_lock(&subsys
->su_mutex
);
1405 client_disconnect_notify(parent_item
, item
);
1407 unlink_group(group
);
1410 client_drop_item(parent_item
, item
);
1412 mutex_unlock(&subsys
->su_mutex
);
1415 module_put(new_item_owner
);
1420 module_put(subsys_owner
);
1424 * link_obj()/link_group() took a reference from child->parent,
1425 * so the parent is safely pinned. We can drop our working
1428 config_item_put(parent_item
);
1434 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1436 struct config_item
*parent_item
;
1437 struct config_item
*item
;
1438 struct configfs_subsystem
*subsys
;
1439 struct configfs_dirent
*sd
;
1440 struct module
*subsys_owner
= NULL
, *dead_item_owner
= NULL
;
1443 sd
= dentry
->d_fsdata
;
1444 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
1447 /* Get a working ref until we have the child */
1448 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1449 subsys
= to_config_group(parent_item
)->cg_subsys
;
1452 if (!parent_item
->ci_type
) {
1453 config_item_put(parent_item
);
1457 /* configfs_mkdir() shouldn't have allowed this */
1458 BUG_ON(!subsys
->su_group
.cg_item
.ci_type
);
1459 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1462 * Ensure that no racing symlink() will make detach_prep() fail while
1463 * the new link is temporarily attached
1466 struct mutex
*wait_mutex
;
1468 mutex_lock(&configfs_symlink_mutex
);
1469 spin_lock(&configfs_dirent_lock
);
1471 * Here's where we check for dependents. We're protected by
1472 * configfs_dirent_lock.
1473 * If no dependent, atomically tag the item as dropping.
1475 ret
= sd
->s_dependent_count
? -EBUSY
: 0;
1477 ret
= configfs_detach_prep(dentry
, &wait_mutex
);
1479 configfs_detach_rollback(dentry
);
1481 spin_unlock(&configfs_dirent_lock
);
1482 mutex_unlock(&configfs_symlink_mutex
);
1485 if (ret
!= -EAGAIN
) {
1486 config_item_put(parent_item
);
1490 /* Wait until the racing operation terminates */
1491 mutex_lock(wait_mutex
);
1492 mutex_unlock(wait_mutex
);
1494 } while (ret
== -EAGAIN
);
1496 /* Get a working ref for the duration of this function */
1497 item
= configfs_get_config_item(dentry
);
1499 /* Drop reference from above, item already holds one. */
1500 config_item_put(parent_item
);
1503 dead_item_owner
= item
->ci_type
->ct_owner
;
1505 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
1506 configfs_detach_group(item
);
1508 mutex_lock(&subsys
->su_mutex
);
1509 client_disconnect_notify(parent_item
, item
);
1510 unlink_group(to_config_group(item
));
1512 configfs_detach_item(item
);
1514 mutex_lock(&subsys
->su_mutex
);
1515 client_disconnect_notify(parent_item
, item
);
1519 client_drop_item(parent_item
, item
);
1520 mutex_unlock(&subsys
->su_mutex
);
1522 /* Drop our reference from above */
1523 config_item_put(item
);
1525 module_put(dead_item_owner
);
1526 module_put(subsys_owner
);
1531 const struct inode_operations configfs_dir_inode_operations
= {
1532 .mkdir
= configfs_mkdir
,
1533 .rmdir
= configfs_rmdir
,
1534 .symlink
= configfs_symlink
,
1535 .unlink
= configfs_unlink
,
1536 .lookup
= configfs_lookup
,
1537 .setattr
= configfs_setattr
,
1540 const struct inode_operations configfs_root_inode_operations
= {
1541 .lookup
= configfs_lookup
,
1542 .setattr
= configfs_setattr
,
1546 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
1549 struct dentry
* new_dentry
, * parent
;
1551 if (!strcmp(config_item_name(item
), new_name
))
1557 down_write(&configfs_rename_sem
);
1558 parent
= item
->parent
->dentry
;
1560 inode_lock(d_inode(parent
));
1562 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
1563 if (!IS_ERR(new_dentry
)) {
1564 if (d_really_is_negative(new_dentry
)) {
1565 error
= config_item_set_name(item
, "%s", new_name
);
1567 d_add(new_dentry
, NULL
);
1568 d_move(item
->dentry
, new_dentry
);
1571 d_delete(new_dentry
);
1576 inode_unlock(d_inode(parent
));
1577 up_write(&configfs_rename_sem
);
1583 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
1585 struct dentry
* dentry
= file
->f_path
.dentry
;
1586 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1589 inode_lock(d_inode(dentry
));
1591 * Fake invisibility if dir belongs to a group/default groups hierarchy
1595 if (configfs_dirent_is_ready(parent_sd
)) {
1596 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
, 0);
1597 if (IS_ERR(file
->private_data
))
1598 err
= PTR_ERR(file
->private_data
);
1602 inode_unlock(d_inode(dentry
));
1607 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
1609 struct dentry
* dentry
= file
->f_path
.dentry
;
1610 struct configfs_dirent
* cursor
= file
->private_data
;
1612 inode_lock(d_inode(dentry
));
1613 spin_lock(&configfs_dirent_lock
);
1614 list_del_init(&cursor
->s_sibling
);
1615 spin_unlock(&configfs_dirent_lock
);
1616 inode_unlock(d_inode(dentry
));
1618 release_configfs_dirent(cursor
);
1623 /* Relationship between s_mode and the DT_xxx types */
1624 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1626 return (sd
->s_mode
>> 12) & 15;
1629 static int configfs_readdir(struct file
*file
, struct dir_context
*ctx
)
1631 struct dentry
*dentry
= file
->f_path
.dentry
;
1632 struct super_block
*sb
= dentry
->d_sb
;
1633 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1634 struct configfs_dirent
*cursor
= file
->private_data
;
1635 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1638 if (!dir_emit_dots(file
, ctx
))
1640 if (ctx
->pos
== 2) {
1641 spin_lock(&configfs_dirent_lock
);
1642 list_move(q
, &parent_sd
->s_children
);
1643 spin_unlock(&configfs_dirent_lock
);
1645 for (p
= q
->next
; p
!= &parent_sd
->s_children
; p
= p
->next
) {
1646 struct configfs_dirent
*next
;
1649 struct inode
*inode
= NULL
;
1651 next
= list_entry(p
, struct configfs_dirent
, s_sibling
);
1652 if (!next
->s_element
)
1655 name
= configfs_get_name(next
);
1659 * We'll have a dentry and an inode for
1660 * PINNED items and for open attribute
1661 * files. We lock here to prevent a race
1662 * with configfs_d_iput() clearing
1663 * s_dentry before calling iput().
1665 * Why do we go to the trouble? If
1666 * someone has an attribute file open,
1667 * the inode number should match until
1668 * they close it. Beyond that, we don't
1671 spin_lock(&configfs_dirent_lock
);
1672 dentry
= next
->s_dentry
;
1674 inode
= d_inode(dentry
);
1677 spin_unlock(&configfs_dirent_lock
);
1679 ino
= iunique(sb
, 2);
1681 if (!dir_emit(ctx
, name
, len
, ino
, dt_type(next
)))
1684 spin_lock(&configfs_dirent_lock
);
1686 spin_unlock(&configfs_dirent_lock
);
1693 static loff_t
configfs_dir_lseek(struct file
*file
, loff_t offset
, int whence
)
1695 struct dentry
* dentry
= file
->f_path
.dentry
;
1697 inode_lock(d_inode(dentry
));
1700 offset
+= file
->f_pos
;
1705 inode_unlock(d_inode(dentry
));
1708 if (offset
!= file
->f_pos
) {
1709 file
->f_pos
= offset
;
1710 if (file
->f_pos
>= 2) {
1711 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1712 struct configfs_dirent
*cursor
= file
->private_data
;
1713 struct list_head
*p
;
1714 loff_t n
= file
->f_pos
- 2;
1716 spin_lock(&configfs_dirent_lock
);
1717 list_del(&cursor
->s_sibling
);
1718 p
= sd
->s_children
.next
;
1719 while (n
&& p
!= &sd
->s_children
) {
1720 struct configfs_dirent
*next
;
1721 next
= list_entry(p
, struct configfs_dirent
,
1723 if (next
->s_element
)
1727 list_add_tail(&cursor
->s_sibling
, p
);
1728 spin_unlock(&configfs_dirent_lock
);
1731 inode_unlock(d_inode(dentry
));
1735 const struct file_operations configfs_dir_operations
= {
1736 .open
= configfs_dir_open
,
1737 .release
= configfs_dir_close
,
1738 .llseek
= configfs_dir_lseek
,
1739 .read
= generic_read_dir
,
1740 .iterate
= configfs_readdir
,
1744 * configfs_register_group - creates a parent-child relation between two groups
1745 * @parent_group: parent group
1746 * @group: child group
1748 * link groups, creates dentry for the child and attaches it to the
1751 * Return: 0 on success, negative errno code on error
1753 int configfs_register_group(struct config_group
*parent_group
,
1754 struct config_group
*group
)
1756 struct configfs_subsystem
*subsys
= parent_group
->cg_subsys
;
1757 struct dentry
*parent
;
1760 mutex_lock(&subsys
->su_mutex
);
1761 link_group(parent_group
, group
);
1762 mutex_unlock(&subsys
->su_mutex
);
1764 parent
= parent_group
->cg_item
.ci_dentry
;
1766 inode_lock_nested(d_inode(parent
), I_MUTEX_PARENT
);
1767 ret
= create_default_group(parent_group
, group
);
1769 spin_lock(&configfs_dirent_lock
);
1770 configfs_dir_set_ready(group
->cg_item
.ci_dentry
->d_fsdata
);
1771 spin_unlock(&configfs_dirent_lock
);
1773 inode_unlock(d_inode(parent
));
1776 EXPORT_SYMBOL(configfs_register_group
);
1779 * configfs_unregister_group() - unregisters a child group from its parent
1780 * @group: parent group to be unregistered
1782 * Undoes configfs_register_group()
1784 void configfs_unregister_group(struct config_group
*group
)
1786 struct configfs_subsystem
*subsys
= group
->cg_subsys
;
1787 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1788 struct dentry
*parent
= group
->cg_item
.ci_parent
->ci_dentry
;
1790 inode_lock_nested(d_inode(parent
), I_MUTEX_PARENT
);
1791 spin_lock(&configfs_dirent_lock
);
1792 configfs_detach_prep(dentry
, NULL
);
1793 spin_unlock(&configfs_dirent_lock
);
1795 configfs_detach_group(&group
->cg_item
);
1796 d_inode(dentry
)->i_flags
|= S_DEAD
;
1799 inode_unlock(d_inode(parent
));
1803 mutex_lock(&subsys
->su_mutex
);
1804 unlink_group(group
);
1805 mutex_unlock(&subsys
->su_mutex
);
1807 EXPORT_SYMBOL(configfs_unregister_group
);
1810 * configfs_register_default_group() - allocates and registers a child group
1811 * @parent_group: parent group
1812 * @name: child group name
1813 * @item_type: child item type description
1815 * boilerplate to allocate and register a child group with its parent. We need
1816 * kzalloc'ed memory because child's default_group is initially empty.
1818 * Return: allocated config group or ERR_PTR() on error
1820 struct config_group
*
1821 configfs_register_default_group(struct config_group
*parent_group
,
1823 struct config_item_type
*item_type
)
1826 struct config_group
*group
;
1828 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
1830 return ERR_PTR(-ENOMEM
);
1831 config_group_init_type_name(group
, name
, item_type
);
1833 ret
= configfs_register_group(parent_group
, group
);
1836 return ERR_PTR(ret
);
1840 EXPORT_SYMBOL(configfs_register_default_group
);
1843 * configfs_unregister_default_group() - unregisters and frees a child group
1844 * @group: the group to act on
1846 void configfs_unregister_default_group(struct config_group
*group
)
1848 configfs_unregister_group(group
);
1851 EXPORT_SYMBOL(configfs_unregister_default_group
);
1853 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1856 struct config_group
*group
= &subsys
->su_group
;
1857 struct dentry
*dentry
;
1858 struct dentry
*root
;
1859 struct configfs_dirent
*sd
;
1861 root
= configfs_pin_fs();
1863 return PTR_ERR(root
);
1865 if (!group
->cg_item
.ci_name
)
1866 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1868 sd
= root
->d_fsdata
;
1869 link_group(to_config_group(sd
->s_element
), group
);
1871 inode_lock_nested(d_inode(root
), I_MUTEX_PARENT
);
1874 dentry
= d_alloc_name(root
, group
->cg_item
.ci_name
);
1876 d_add(dentry
, NULL
);
1878 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1881 BUG_ON(d_inode(dentry
));
1885 spin_lock(&configfs_dirent_lock
);
1886 configfs_dir_set_ready(dentry
->d_fsdata
);
1887 spin_unlock(&configfs_dirent_lock
);
1891 inode_unlock(d_inode(root
));
1894 unlink_group(group
);
1895 configfs_release_fs();
1901 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1903 struct config_group
*group
= &subsys
->su_group
;
1904 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1905 struct dentry
*root
= dentry
->d_sb
->s_root
;
1907 if (dentry
->d_parent
!= root
) {
1908 pr_err("Tried to unregister non-subsystem!\n");
1912 inode_lock_nested(d_inode(root
),
1914 inode_lock_nested(d_inode(dentry
), I_MUTEX_CHILD
);
1915 mutex_lock(&configfs_symlink_mutex
);
1916 spin_lock(&configfs_dirent_lock
);
1917 if (configfs_detach_prep(dentry
, NULL
)) {
1918 pr_err("Tried to unregister non-empty subsystem!\n");
1920 spin_unlock(&configfs_dirent_lock
);
1921 mutex_unlock(&configfs_symlink_mutex
);
1922 configfs_detach_group(&group
->cg_item
);
1923 d_inode(dentry
)->i_flags
|= S_DEAD
;
1925 inode_unlock(d_inode(dentry
));
1929 inode_unlock(d_inode(root
));
1933 unlink_group(group
);
1934 configfs_release_fs();
1937 EXPORT_SYMBOL(configfs_register_subsystem
);
1938 EXPORT_SYMBOL(configfs_unregister_subsystem
);