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
);
62 * Set sd->s_dentry to null only when this dentry is the one
63 * that is going to be killed. Otherwise configfs_d_iput may
64 * run just after configfs_attach_attr and set sd->s_dentry to
65 * NULL even it's still in use.
67 if (sd
->s_dentry
== dentry
)
70 spin_unlock(&configfs_dirent_lock
);
76 const struct dentry_operations configfs_dentry_ops
= {
77 .d_iput
= configfs_d_iput
,
78 .d_delete
= always_delete_dentry
,
84 * Helpers to make lockdep happy with our recursive locking of default groups'
85 * inodes (see configfs_attach_group() and configfs_detach_group()).
86 * We put default groups i_mutexes in separate classes according to their depth
87 * from the youngest non-default group ancestor.
89 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
90 * groups A/B and A/C will have their inode's mutex in class
91 * default_group_class[0], and default group A/C/D will be in
92 * default_group_class[1].
94 * The lock classes are declared and assigned in inode.c, according to the
96 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
97 * default groups, and reset to -1 when all default groups are attached. During
98 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
99 * inode's mutex is set to default_group_class[s_depth - 1].
102 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
107 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
108 struct configfs_dirent
*sd
)
110 int parent_depth
= parent_sd
->s_depth
;
112 if (parent_depth
>= 0)
113 sd
->s_depth
= parent_depth
+ 1;
117 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
120 * item's i_mutex class is already setup, so s_depth is now only
121 * used to set new sub-directories s_depth, which is always done
122 * with item's i_mutex locked.
125 * sd->s_depth == -1 iff we are a non default group.
126 * else (we are a default group) sd->s_depth > 0 (see
129 if (sd
->s_depth
== -1)
131 * We are a non default group and we are going to create
138 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
140 /* We will not create default groups anymore. */
144 #else /* CONFIG_LOCKDEP */
146 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
150 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
151 struct configfs_dirent
*sd
)
156 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
161 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
165 #endif /* CONFIG_LOCKDEP */
168 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
170 static struct configfs_dirent
*configfs_new_dirent(struct configfs_dirent
*parent_sd
,
171 void *element
, int type
)
173 struct configfs_dirent
* sd
;
175 sd
= kmem_cache_zalloc(configfs_dir_cachep
, GFP_KERNEL
);
177 return ERR_PTR(-ENOMEM
);
179 atomic_set(&sd
->s_count
, 1);
180 INIT_LIST_HEAD(&sd
->s_links
);
181 INIT_LIST_HEAD(&sd
->s_children
);
182 sd
->s_element
= element
;
184 configfs_init_dirent_depth(sd
);
185 spin_lock(&configfs_dirent_lock
);
186 if (parent_sd
->s_type
& CONFIGFS_USET_DROPPING
) {
187 spin_unlock(&configfs_dirent_lock
);
188 kmem_cache_free(configfs_dir_cachep
, sd
);
189 return ERR_PTR(-ENOENT
);
191 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
192 spin_unlock(&configfs_dirent_lock
);
199 * Return -EEXIST if there is already a configfs element with the same
200 * name for the same parent.
202 * called with parent inode's i_mutex held
204 static int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
205 const unsigned char *new)
207 struct configfs_dirent
* sd
;
209 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
211 const unsigned char *existing
= configfs_get_name(sd
);
212 if (strcmp(existing
, new))
223 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
224 struct dentry
* dentry
, void * element
,
225 umode_t mode
, int type
)
227 struct configfs_dirent
* sd
;
229 sd
= configfs_new_dirent(parent_sd
, element
, type
);
234 sd
->s_dentry
= dentry
;
236 dentry
->d_fsdata
= configfs_get(sd
);
241 static void init_dir(struct inode
* inode
)
243 inode
->i_op
= &configfs_dir_inode_operations
;
244 inode
->i_fop
= &configfs_dir_operations
;
246 /* directory inodes start off with i_nlink == 2 (for "." entry) */
250 static void configfs_init_file(struct inode
* inode
)
252 inode
->i_size
= PAGE_SIZE
;
253 inode
->i_fop
= &configfs_file_operations
;
256 static void init_symlink(struct inode
* inode
)
258 inode
->i_op
= &configfs_symlink_inode_operations
;
262 * configfs_create_dir - create a directory for an config_item.
263 * @item: config_itemwe're creating directory for.
264 * @dentry: config_item's dentry.
266 * Note: user-created entries won't be allowed under this new directory
267 * until it is validated by configfs_dir_set_ready()
270 static int configfs_create_dir(struct config_item
*item
, struct dentry
*dentry
)
273 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
274 struct dentry
*p
= dentry
->d_parent
;
278 error
= configfs_dirent_exists(p
->d_fsdata
, dentry
->d_name
.name
);
282 error
= configfs_make_dirent(p
->d_fsdata
, dentry
, item
, mode
,
283 CONFIGFS_DIR
| CONFIGFS_USET_CREATING
);
287 configfs_set_dir_dirent_depth(p
->d_fsdata
, dentry
->d_fsdata
);
288 error
= configfs_create(dentry
, mode
, init_dir
);
290 inc_nlink(d_inode(p
));
291 item
->ci_dentry
= dentry
;
293 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
295 spin_lock(&configfs_dirent_lock
);
296 list_del_init(&sd
->s_sibling
);
297 spin_unlock(&configfs_dirent_lock
);
305 * Allow userspace to create new entries under a new directory created with
306 * configfs_create_dir(), and under all of its chidlren directories recursively.
307 * @sd configfs_dirent of the new directory to validate
309 * Caller must hold configfs_dirent_lock.
311 static void configfs_dir_set_ready(struct configfs_dirent
*sd
)
313 struct configfs_dirent
*child_sd
;
315 sd
->s_type
&= ~CONFIGFS_USET_CREATING
;
316 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
)
317 if (child_sd
->s_type
& CONFIGFS_USET_CREATING
)
318 configfs_dir_set_ready(child_sd
);
322 * Check that a directory does not belong to a directory hierarchy being
323 * attached and not validated yet.
324 * @sd configfs_dirent of the directory to check
326 * @return non-zero iff the directory was validated
328 * Note: takes configfs_dirent_lock, so the result may change from false to true
329 * in two consecutive calls, but never from true to false.
331 int configfs_dirent_is_ready(struct configfs_dirent
*sd
)
335 spin_lock(&configfs_dirent_lock
);
336 ret
= !(sd
->s_type
& CONFIGFS_USET_CREATING
);
337 spin_unlock(&configfs_dirent_lock
);
342 int configfs_create_link(struct configfs_symlink
*sl
,
343 struct dentry
*parent
,
344 struct dentry
*dentry
)
347 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
349 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
352 err
= configfs_create(dentry
, mode
, init_symlink
);
354 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
356 spin_lock(&configfs_dirent_lock
);
357 list_del_init(&sd
->s_sibling
);
358 spin_unlock(&configfs_dirent_lock
);
366 static void remove_dir(struct dentry
* d
)
368 struct dentry
* parent
= dget(d
->d_parent
);
369 struct configfs_dirent
* sd
;
372 spin_lock(&configfs_dirent_lock
);
373 list_del_init(&sd
->s_sibling
);
374 spin_unlock(&configfs_dirent_lock
);
376 if (d_really_is_positive(d
))
377 simple_rmdir(d_inode(parent
),d
);
379 pr_debug(" o %pd removing done (%d)\n", d
, d_count(d
));
385 * configfs_remove_dir - remove an config_item's directory.
386 * @item: config_item we're removing.
388 * The only thing special about this is that we remove any files in
389 * the directory before we remove the directory, and we've inlined
390 * what used to be configfs_rmdir() below, instead of calling separately.
392 * Caller holds the mutex of the item's inode
395 static void configfs_remove_dir(struct config_item
* item
)
397 struct dentry
* dentry
= dget(item
->ci_dentry
);
404 * Drop reference from dget() on entrance.
410 /* attaches attribute's configfs_dirent to the dentry corresponding to the
413 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
415 struct configfs_attribute
* attr
= sd
->s_element
;
418 spin_lock(&configfs_dirent_lock
);
419 dentry
->d_fsdata
= configfs_get(sd
);
420 sd
->s_dentry
= dentry
;
421 spin_unlock(&configfs_dirent_lock
);
423 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
,
435 static struct dentry
* configfs_lookup(struct inode
*dir
,
436 struct dentry
*dentry
,
439 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
440 struct configfs_dirent
* sd
;
445 * Fake invisibility if dir belongs to a group/default groups hierarchy
448 * This forbids userspace to read/write attributes of items which may
449 * not complete their initialization, since the dentries of the
450 * attributes won't be instantiated.
453 if (!configfs_dirent_is_ready(parent_sd
))
456 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
457 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
458 const unsigned char * name
= configfs_get_name(sd
);
460 if (strcmp(name
, dentry
->d_name
.name
))
464 err
= configfs_attach_attr(sd
, dentry
);
471 * If it doesn't exist and it isn't a NOT_PINNED item,
472 * it must be negative.
474 if (dentry
->d_name
.len
> NAME_MAX
)
475 return ERR_PTR(-ENAMETOOLONG
);
485 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
486 * attributes and are removed by rmdir(). We recurse, setting
487 * CONFIGFS_USET_DROPPING on all children that are candidates for
489 * If there is an error, the caller will reset the flags via
490 * configfs_detach_rollback().
492 static int configfs_detach_prep(struct dentry
*dentry
, struct mutex
**wait_mutex
)
494 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
495 struct configfs_dirent
*sd
;
498 /* Mark that we're trying to drop the group */
499 parent_sd
->s_type
|= CONFIGFS_USET_DROPPING
;
502 if (!list_empty(&parent_sd
->s_links
))
506 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
507 if (!sd
->s_element
||
508 (sd
->s_type
& CONFIGFS_NOT_PINNED
))
510 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
511 /* Abort if racing with mkdir() */
512 if (sd
->s_type
& CONFIGFS_USET_IN_MKDIR
) {
514 *wait_mutex
= &d_inode(sd
->s_dentry
)->i_mutex
;
519 * Yup, recursive. If there's a problem, blame
520 * deep nesting of default_groups
522 ret
= configfs_detach_prep(sd
->s_dentry
, wait_mutex
);
536 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
539 static void configfs_detach_rollback(struct dentry
*dentry
)
541 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
542 struct configfs_dirent
*sd
;
544 parent_sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
546 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
)
547 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
548 configfs_detach_rollback(sd
->s_dentry
);
551 static void detach_attrs(struct config_item
* item
)
553 struct dentry
* dentry
= dget(item
->ci_dentry
);
554 struct configfs_dirent
* parent_sd
;
555 struct configfs_dirent
* sd
, * tmp
;
560 pr_debug("configfs %s: dropping attrs for dir\n",
561 dentry
->d_name
.name
);
563 parent_sd
= dentry
->d_fsdata
;
564 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
565 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
567 spin_lock(&configfs_dirent_lock
);
568 list_del_init(&sd
->s_sibling
);
569 spin_unlock(&configfs_dirent_lock
);
570 configfs_drop_dentry(sd
, dentry
);
575 * Drop reference from dget() on entrance.
580 static int populate_attrs(struct config_item
*item
)
582 struct config_item_type
*t
= item
->ci_type
;
583 struct configfs_attribute
*attr
;
590 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
591 if ((error
= configfs_create_file(item
, attr
)))
602 static int configfs_attach_group(struct config_item
*parent_item
,
603 struct config_item
*item
,
604 struct dentry
*dentry
);
605 static void configfs_detach_group(struct config_item
*item
);
607 static void detach_groups(struct config_group
*group
)
609 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
610 struct dentry
*child
;
611 struct configfs_dirent
*parent_sd
;
612 struct configfs_dirent
*sd
, *tmp
;
617 parent_sd
= dentry
->d_fsdata
;
618 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
619 if (!sd
->s_element
||
620 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
623 child
= sd
->s_dentry
;
625 mutex_lock(&d_inode(child
)->i_mutex
);
627 configfs_detach_group(sd
->s_element
);
628 d_inode(child
)->i_flags
|= S_DEAD
;
631 mutex_unlock(&d_inode(child
)->i_mutex
);
638 * Drop reference from dget() on entrance.
644 * This fakes mkdir(2) on a default_groups[] entry. It
645 * creates a dentry, attachs it, and then does fixup
648 * We could, perhaps, tweak our parent's ->mkdir for a minute and
649 * try using vfs_mkdir. Just a thought.
651 static int create_default_group(struct config_group
*parent_group
,
652 struct config_group
*group
)
655 struct configfs_dirent
*sd
;
656 /* We trust the caller holds a reference to parent */
657 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
659 if (!group
->cg_item
.ci_name
)
660 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
663 child
= d_alloc_name(parent
, group
->cg_item
.ci_name
);
667 ret
= configfs_attach_group(&parent_group
->cg_item
,
668 &group
->cg_item
, child
);
670 sd
= child
->d_fsdata
;
671 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
673 BUG_ON(d_inode(child
));
682 static int populate_groups(struct config_group
*group
)
684 struct config_group
*new_group
;
688 if (group
->default_groups
) {
689 for (i
= 0; group
->default_groups
[i
]; i
++) {
690 new_group
= group
->default_groups
[i
];
692 ret
= create_default_group(group
, new_group
);
694 detach_groups(group
);
704 * All of link_obj/unlink_obj/link_group/unlink_group require that
705 * subsys->su_mutex is held.
708 static void unlink_obj(struct config_item
*item
)
710 struct config_group
*group
;
712 group
= item
->ci_group
;
714 list_del_init(&item
->ci_entry
);
716 item
->ci_group
= NULL
;
717 item
->ci_parent
= NULL
;
719 /* Drop the reference for ci_entry */
720 config_item_put(item
);
722 /* Drop the reference for ci_parent */
723 config_group_put(group
);
727 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
730 * Parent seems redundant with group, but it makes certain
731 * traversals much nicer.
733 item
->ci_parent
= parent_item
;
736 * We hold a reference on the parent for the child's ci_parent
739 item
->ci_group
= config_group_get(to_config_group(parent_item
));
740 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
743 * We hold a reference on the child for ci_entry on the parent's
746 config_item_get(item
);
749 static void unlink_group(struct config_group
*group
)
752 struct config_group
*new_group
;
754 if (group
->default_groups
) {
755 for (i
= 0; group
->default_groups
[i
]; i
++) {
756 new_group
= group
->default_groups
[i
];
757 unlink_group(new_group
);
761 group
->cg_subsys
= NULL
;
762 unlink_obj(&group
->cg_item
);
765 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
768 struct config_group
*new_group
;
769 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
771 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
773 if (parent_group
->cg_subsys
)
774 subsys
= parent_group
->cg_subsys
;
775 else if (configfs_is_root(&parent_group
->cg_item
))
776 subsys
= to_configfs_subsystem(group
);
779 group
->cg_subsys
= subsys
;
781 if (group
->default_groups
) {
782 for (i
= 0; group
->default_groups
[i
]; i
++) {
783 new_group
= group
->default_groups
[i
];
784 link_group(group
, new_group
);
790 * The goal is that configfs_attach_item() (and
791 * configfs_attach_group()) can be called from either the VFS or this
792 * module. That is, they assume that the items have been created,
793 * the dentry allocated, and the dcache is all ready to go.
795 * If they fail, they must clean up after themselves as if they
796 * had never been called. The caller (VFS or local function) will
797 * handle cleaning up the dcache bits.
799 * configfs_detach_group() and configfs_detach_item() behave similarly on
800 * the way out. They assume that the proper semaphores are held, they
801 * clean up the configfs items, and they expect their callers will
802 * handle the dcache bits.
804 static int configfs_attach_item(struct config_item
*parent_item
,
805 struct config_item
*item
,
806 struct dentry
*dentry
)
810 ret
= configfs_create_dir(item
, dentry
);
812 ret
= populate_attrs(item
);
815 * We are going to remove an inode and its dentry but
816 * the VFS may already have hit and used them. Thus,
817 * we must lock them as rmdir() would.
819 mutex_lock(&d_inode(dentry
)->i_mutex
);
820 configfs_remove_dir(item
);
821 d_inode(dentry
)->i_flags
|= S_DEAD
;
823 mutex_unlock(&d_inode(dentry
)->i_mutex
);
831 /* Caller holds the mutex of the item's inode */
832 static void configfs_detach_item(struct config_item
*item
)
835 configfs_remove_dir(item
);
838 static int configfs_attach_group(struct config_item
*parent_item
,
839 struct config_item
*item
,
840 struct dentry
*dentry
)
843 struct configfs_dirent
*sd
;
845 ret
= configfs_attach_item(parent_item
, item
, dentry
);
847 sd
= dentry
->d_fsdata
;
848 sd
->s_type
|= CONFIGFS_USET_DIR
;
851 * FYI, we're faking mkdir in populate_groups()
852 * We must lock the group's inode to avoid races with the VFS
853 * which can already hit the inode and try to add/remove entries
856 * We must also lock the inode to remove it safely in case of
857 * error, as rmdir() would.
859 mutex_lock_nested(&d_inode(dentry
)->i_mutex
, I_MUTEX_CHILD
);
860 configfs_adjust_dir_dirent_depth_before_populate(sd
);
861 ret
= populate_groups(to_config_group(item
));
863 configfs_detach_item(item
);
864 d_inode(dentry
)->i_flags
|= S_DEAD
;
867 configfs_adjust_dir_dirent_depth_after_populate(sd
);
868 mutex_unlock(&d_inode(dentry
)->i_mutex
);
876 /* Caller holds the mutex of the group's inode */
877 static void configfs_detach_group(struct config_item
*item
)
879 detach_groups(to_config_group(item
));
880 configfs_detach_item(item
);
884 * After the item has been detached from the filesystem view, we are
885 * ready to tear it out of the hierarchy. Notify the client before
886 * we do that so they can perform any cleanup that requires
887 * navigating the hierarchy. A client does not need to provide this
888 * callback. The subsystem semaphore MUST be held by the caller, and
889 * references must be valid for both items. It also assumes the
890 * caller has validated ci_type.
892 static void client_disconnect_notify(struct config_item
*parent_item
,
893 struct config_item
*item
)
895 struct config_item_type
*type
;
897 type
= parent_item
->ci_type
;
900 if (type
->ct_group_ops
&& type
->ct_group_ops
->disconnect_notify
)
901 type
->ct_group_ops
->disconnect_notify(to_config_group(parent_item
),
906 * Drop the initial reference from make_item()/make_group()
907 * This function assumes that reference is held on item
908 * and that item holds a valid reference to the parent. Also, it
909 * assumes the caller has validated ci_type.
911 static void client_drop_item(struct config_item
*parent_item
,
912 struct config_item
*item
)
914 struct config_item_type
*type
;
916 type
= parent_item
->ci_type
;
920 * If ->drop_item() exists, it is responsible for the
923 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
924 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
927 config_item_put(item
);
931 static void configfs_dump_one(struct configfs_dirent
*sd
, int level
)
933 pr_info("%*s\"%s\":\n", level
, " ", configfs_get_name(sd
));
935 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
936 type_print(CONFIGFS_ROOT
);
937 type_print(CONFIGFS_DIR
);
938 type_print(CONFIGFS_ITEM_ATTR
);
939 type_print(CONFIGFS_ITEM_LINK
);
940 type_print(CONFIGFS_USET_DIR
);
941 type_print(CONFIGFS_USET_DEFAULT
);
942 type_print(CONFIGFS_USET_DROPPING
);
946 static int configfs_dump(struct configfs_dirent
*sd
, int level
)
948 struct configfs_dirent
*child_sd
;
951 configfs_dump_one(sd
, level
);
953 if (!(sd
->s_type
& (CONFIGFS_DIR
|CONFIGFS_ROOT
)))
956 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
957 ret
= configfs_dump(child_sd
, level
+ 2);
968 * configfs_depend_item() and configfs_undepend_item()
970 * WARNING: Do not call these from a configfs callback!
972 * This describes these functions and their helpers.
974 * Allow another kernel system to depend on a config_item. If this
975 * happens, the item cannot go away until the dependent can live without
976 * it. The idea is to give client modules as simple an interface as
977 * possible. When a system asks them to depend on an item, they just
978 * call configfs_depend_item(). If the item is live and the client
979 * driver is in good shape, we'll happily do the work for them.
981 * Why is the locking complex? Because configfs uses the VFS to handle
982 * all locking, but this function is called outside the normal
983 * VFS->configfs path. So it must take VFS locks to prevent the
984 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
985 * why you can't call these functions underneath configfs callbacks.
987 * Note, btw, that this can be called at *any* time, even when a configfs
988 * subsystem isn't registered, or when configfs is loading or unloading.
989 * Just like configfs_register_subsystem(). So we take the same
990 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
991 * If we can find the target item in the
992 * configfs tree, it must be part of the subsystem tree as well, so we
993 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
994 * locking out mkdir() and rmdir(), who might be racing us.
998 * configfs_depend_prep()
1000 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1001 * attributes. This is similar but not the same to configfs_detach_prep().
1002 * Note that configfs_detach_prep() expects the parent to be locked when it
1003 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1004 * do that so we can unlock it if we find nothing.
1006 * Here we do a depth-first search of the dentry hierarchy looking for
1008 * We deliberately ignore items tagged as dropping since they are virtually
1009 * dead, as well as items in the middle of attachment since they virtually
1010 * do not exist yet. This completes the locking out of racing mkdir() and
1012 * Note: subdirectories in the middle of attachment start with s_type =
1013 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1014 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1015 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1017 * If the target is not found, -ENOENT is bubbled up.
1019 * This adds a requirement that all config_items be unique!
1021 * This is recursive. There isn't
1022 * much on the stack, though, so folks that need this function - be careful
1023 * about your stack! Patches will be accepted to make it iterative.
1025 static int configfs_depend_prep(struct dentry
*origin
,
1026 struct config_item
*target
)
1028 struct configfs_dirent
*child_sd
, *sd
;
1031 BUG_ON(!origin
|| !origin
->d_fsdata
);
1032 sd
= origin
->d_fsdata
;
1034 if (sd
->s_element
== target
) /* Boo-yah */
1037 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
1038 if ((child_sd
->s_type
& CONFIGFS_DIR
) &&
1039 !(child_sd
->s_type
& CONFIGFS_USET_DROPPING
) &&
1040 !(child_sd
->s_type
& CONFIGFS_USET_CREATING
)) {
1041 ret
= configfs_depend_prep(child_sd
->s_dentry
,
1044 goto out
; /* Child path boo-yah */
1048 /* We looped all our children and didn't find target */
1055 int configfs_depend_item(struct configfs_subsystem
*subsys
,
1056 struct config_item
*target
)
1059 struct configfs_dirent
*p
, *root_sd
, *subsys_sd
= NULL
;
1060 struct config_item
*s_item
= &subsys
->su_group
.cg_item
;
1061 struct dentry
*root
;
1064 * Pin the configfs filesystem. This means we can safely access
1065 * the root of the configfs filesystem.
1067 root
= configfs_pin_fs();
1069 return PTR_ERR(root
);
1072 * Next, lock the root directory. We're going to check that the
1073 * subsystem is really registered, and so we need to lock out
1074 * configfs_[un]register_subsystem().
1076 mutex_lock(&d_inode(root
)->i_mutex
);
1078 root_sd
= root
->d_fsdata
;
1080 list_for_each_entry(p
, &root_sd
->s_children
, s_sibling
) {
1081 if (p
->s_type
& CONFIGFS_DIR
) {
1082 if (p
->s_element
== s_item
) {
1094 /* Ok, now we can trust subsys/s_item */
1096 spin_lock(&configfs_dirent_lock
);
1097 /* Scan the tree, return 0 if found */
1098 ret
= configfs_depend_prep(subsys_sd
->s_dentry
, target
);
1100 goto out_unlock_dirent_lock
;
1103 * We are sure that the item is not about to be removed by rmdir(), and
1104 * not in the middle of attachment by mkdir().
1106 p
= target
->ci_dentry
->d_fsdata
;
1107 p
->s_dependent_count
+= 1;
1109 out_unlock_dirent_lock
:
1110 spin_unlock(&configfs_dirent_lock
);
1112 mutex_unlock(&d_inode(root
)->i_mutex
);
1115 * If we succeeded, the fs is pinned via other methods. If not,
1116 * we're done with it anyway. So release_fs() is always right.
1118 configfs_release_fs();
1122 EXPORT_SYMBOL(configfs_depend_item
);
1125 * Release the dependent linkage. This is much simpler than
1126 * configfs_depend_item() because we know that that the client driver is
1127 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1129 void configfs_undepend_item(struct configfs_subsystem
*subsys
,
1130 struct config_item
*target
)
1132 struct configfs_dirent
*sd
;
1135 * Since we can trust everything is pinned, we just need
1136 * configfs_dirent_lock.
1138 spin_lock(&configfs_dirent_lock
);
1140 sd
= target
->ci_dentry
->d_fsdata
;
1141 BUG_ON(sd
->s_dependent_count
< 1);
1143 sd
->s_dependent_count
-= 1;
1146 * After this unlock, we cannot trust the item to stay alive!
1147 * DO NOT REFERENCE item after this unlock.
1149 spin_unlock(&configfs_dirent_lock
);
1151 EXPORT_SYMBOL(configfs_undepend_item
);
1153 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
1157 struct config_group
*group
= NULL
;
1158 struct config_item
*item
= NULL
;
1159 struct config_item
*parent_item
;
1160 struct configfs_subsystem
*subsys
;
1161 struct configfs_dirent
*sd
;
1162 struct config_item_type
*type
;
1163 struct module
*subsys_owner
= NULL
, *new_item_owner
= NULL
;
1166 sd
= dentry
->d_parent
->d_fsdata
;
1169 * Fake invisibility if dir belongs to a group/default groups hierarchy
1172 if (!configfs_dirent_is_ready(sd
)) {
1177 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
1182 /* Get a working ref for the duration of this function */
1183 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1184 type
= parent_item
->ci_type
;
1185 subsys
= to_config_group(parent_item
)->cg_subsys
;
1188 if (!type
|| !type
->ct_group_ops
||
1189 (!type
->ct_group_ops
->make_group
&&
1190 !type
->ct_group_ops
->make_item
)) {
1191 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
1196 * The subsystem may belong to a different module than the item
1197 * being created. We don't want to safely pin the new item but
1198 * fail to pin the subsystem it sits under.
1200 if (!subsys
->su_group
.cg_item
.ci_type
) {
1204 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1205 if (!try_module_get(subsys_owner
)) {
1210 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
1213 goto out_subsys_put
;
1216 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
1218 mutex_lock(&subsys
->su_mutex
);
1219 if (type
->ct_group_ops
->make_group
) {
1220 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
1222 group
= ERR_PTR(-ENOMEM
);
1223 if (!IS_ERR(group
)) {
1224 link_group(to_config_group(parent_item
), group
);
1225 item
= &group
->cg_item
;
1227 ret
= PTR_ERR(group
);
1229 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
1231 item
= ERR_PTR(-ENOMEM
);
1233 link_obj(parent_item
, item
);
1235 ret
= PTR_ERR(item
);
1237 mutex_unlock(&subsys
->su_mutex
);
1242 * If ret != 0, then link_obj() was never called.
1243 * There are no extra references to clean up.
1245 goto out_subsys_put
;
1249 * link_obj() has been called (via link_group() for groups).
1250 * From here on out, errors must clean that up.
1253 type
= item
->ci_type
;
1259 new_item_owner
= type
->ct_owner
;
1260 if (!try_module_get(new_item_owner
)) {
1266 * I hate doing it this way, but if there is
1267 * an error, module_put() probably should
1268 * happen after any cleanup.
1273 * Make racing rmdir() fail if it did not tag parent with
1274 * CONFIGFS_USET_DROPPING
1275 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1276 * fail and let rmdir() terminate correctly
1278 spin_lock(&configfs_dirent_lock
);
1279 /* This will make configfs_detach_prep() fail */
1280 sd
->s_type
|= CONFIGFS_USET_IN_MKDIR
;
1281 spin_unlock(&configfs_dirent_lock
);
1284 ret
= configfs_attach_group(parent_item
, item
, dentry
);
1286 ret
= configfs_attach_item(parent_item
, item
, dentry
);
1288 spin_lock(&configfs_dirent_lock
);
1289 sd
->s_type
&= ~CONFIGFS_USET_IN_MKDIR
;
1291 configfs_dir_set_ready(dentry
->d_fsdata
);
1292 spin_unlock(&configfs_dirent_lock
);
1296 /* Tear down everything we built up */
1297 mutex_lock(&subsys
->su_mutex
);
1299 client_disconnect_notify(parent_item
, item
);
1301 unlink_group(group
);
1304 client_drop_item(parent_item
, item
);
1306 mutex_unlock(&subsys
->su_mutex
);
1309 module_put(new_item_owner
);
1314 module_put(subsys_owner
);
1318 * link_obj()/link_group() took a reference from child->parent,
1319 * so the parent is safely pinned. We can drop our working
1322 config_item_put(parent_item
);
1328 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1330 struct config_item
*parent_item
;
1331 struct config_item
*item
;
1332 struct configfs_subsystem
*subsys
;
1333 struct configfs_dirent
*sd
;
1334 struct module
*subsys_owner
= NULL
, *dead_item_owner
= NULL
;
1337 sd
= dentry
->d_fsdata
;
1338 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
1341 /* Get a working ref until we have the child */
1342 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1343 subsys
= to_config_group(parent_item
)->cg_subsys
;
1346 if (!parent_item
->ci_type
) {
1347 config_item_put(parent_item
);
1351 /* configfs_mkdir() shouldn't have allowed this */
1352 BUG_ON(!subsys
->su_group
.cg_item
.ci_type
);
1353 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1356 * Ensure that no racing symlink() will make detach_prep() fail while
1357 * the new link is temporarily attached
1360 struct mutex
*wait_mutex
;
1362 mutex_lock(&configfs_symlink_mutex
);
1363 spin_lock(&configfs_dirent_lock
);
1365 * Here's where we check for dependents. We're protected by
1366 * configfs_dirent_lock.
1367 * If no dependent, atomically tag the item as dropping.
1369 ret
= sd
->s_dependent_count
? -EBUSY
: 0;
1371 ret
= configfs_detach_prep(dentry
, &wait_mutex
);
1373 configfs_detach_rollback(dentry
);
1375 spin_unlock(&configfs_dirent_lock
);
1376 mutex_unlock(&configfs_symlink_mutex
);
1379 if (ret
!= -EAGAIN
) {
1380 config_item_put(parent_item
);
1384 /* Wait until the racing operation terminates */
1385 mutex_lock(wait_mutex
);
1386 mutex_unlock(wait_mutex
);
1388 } while (ret
== -EAGAIN
);
1390 /* Get a working ref for the duration of this function */
1391 item
= configfs_get_config_item(dentry
);
1393 /* Drop reference from above, item already holds one. */
1394 config_item_put(parent_item
);
1397 dead_item_owner
= item
->ci_type
->ct_owner
;
1399 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
1400 configfs_detach_group(item
);
1402 mutex_lock(&subsys
->su_mutex
);
1403 client_disconnect_notify(parent_item
, item
);
1404 unlink_group(to_config_group(item
));
1406 configfs_detach_item(item
);
1408 mutex_lock(&subsys
->su_mutex
);
1409 client_disconnect_notify(parent_item
, item
);
1413 client_drop_item(parent_item
, item
);
1414 mutex_unlock(&subsys
->su_mutex
);
1416 /* Drop our reference from above */
1417 config_item_put(item
);
1419 module_put(dead_item_owner
);
1420 module_put(subsys_owner
);
1425 const struct inode_operations configfs_dir_inode_operations
= {
1426 .mkdir
= configfs_mkdir
,
1427 .rmdir
= configfs_rmdir
,
1428 .symlink
= configfs_symlink
,
1429 .unlink
= configfs_unlink
,
1430 .lookup
= configfs_lookup
,
1431 .setattr
= configfs_setattr
,
1434 const struct inode_operations configfs_root_inode_operations
= {
1435 .lookup
= configfs_lookup
,
1436 .setattr
= configfs_setattr
,
1440 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
1443 struct dentry
* new_dentry
, * parent
;
1445 if (!strcmp(config_item_name(item
), new_name
))
1451 down_write(&configfs_rename_sem
);
1452 parent
= item
->parent
->dentry
;
1454 mutex_lock(&d_inode(parent
)->i_mutex
);
1456 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
1457 if (!IS_ERR(new_dentry
)) {
1458 if (d_really_is_negative(new_dentry
)) {
1459 error
= config_item_set_name(item
, "%s", new_name
);
1461 d_add(new_dentry
, NULL
);
1462 d_move(item
->dentry
, new_dentry
);
1465 d_delete(new_dentry
);
1470 mutex_unlock(&d_inode(parent
)->i_mutex
);
1471 up_write(&configfs_rename_sem
);
1477 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
1479 struct dentry
* dentry
= file
->f_path
.dentry
;
1480 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1483 mutex_lock(&d_inode(dentry
)->i_mutex
);
1485 * Fake invisibility if dir belongs to a group/default groups hierarchy
1489 if (configfs_dirent_is_ready(parent_sd
)) {
1490 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
, 0);
1491 if (IS_ERR(file
->private_data
))
1492 err
= PTR_ERR(file
->private_data
);
1496 mutex_unlock(&d_inode(dentry
)->i_mutex
);
1501 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
1503 struct dentry
* dentry
= file
->f_path
.dentry
;
1504 struct configfs_dirent
* cursor
= file
->private_data
;
1506 mutex_lock(&d_inode(dentry
)->i_mutex
);
1507 spin_lock(&configfs_dirent_lock
);
1508 list_del_init(&cursor
->s_sibling
);
1509 spin_unlock(&configfs_dirent_lock
);
1510 mutex_unlock(&d_inode(dentry
)->i_mutex
);
1512 release_configfs_dirent(cursor
);
1517 /* Relationship between s_mode and the DT_xxx types */
1518 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1520 return (sd
->s_mode
>> 12) & 15;
1523 static int configfs_readdir(struct file
*file
, struct dir_context
*ctx
)
1525 struct dentry
*dentry
= file
->f_path
.dentry
;
1526 struct super_block
*sb
= dentry
->d_sb
;
1527 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1528 struct configfs_dirent
*cursor
= file
->private_data
;
1529 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1532 if (!dir_emit_dots(file
, ctx
))
1534 if (ctx
->pos
== 2) {
1535 spin_lock(&configfs_dirent_lock
);
1536 list_move(q
, &parent_sd
->s_children
);
1537 spin_unlock(&configfs_dirent_lock
);
1539 for (p
= q
->next
; p
!= &parent_sd
->s_children
; p
= p
->next
) {
1540 struct configfs_dirent
*next
;
1543 struct inode
*inode
= NULL
;
1545 next
= list_entry(p
, struct configfs_dirent
, s_sibling
);
1546 if (!next
->s_element
)
1549 name
= configfs_get_name(next
);
1553 * We'll have a dentry and an inode for
1554 * PINNED items and for open attribute
1555 * files. We lock here to prevent a race
1556 * with configfs_d_iput() clearing
1557 * s_dentry before calling iput().
1559 * Why do we go to the trouble? If
1560 * someone has an attribute file open,
1561 * the inode number should match until
1562 * they close it. Beyond that, we don't
1565 spin_lock(&configfs_dirent_lock
);
1566 dentry
= next
->s_dentry
;
1568 inode
= d_inode(dentry
);
1571 spin_unlock(&configfs_dirent_lock
);
1573 ino
= iunique(sb
, 2);
1575 if (!dir_emit(ctx
, name
, len
, ino
, dt_type(next
)))
1578 spin_lock(&configfs_dirent_lock
);
1580 spin_unlock(&configfs_dirent_lock
);
1587 static loff_t
configfs_dir_lseek(struct file
*file
, loff_t offset
, int whence
)
1589 struct dentry
* dentry
= file
->f_path
.dentry
;
1591 mutex_lock(&d_inode(dentry
)->i_mutex
);
1594 offset
+= file
->f_pos
;
1599 mutex_unlock(&d_inode(dentry
)->i_mutex
);
1602 if (offset
!= file
->f_pos
) {
1603 file
->f_pos
= offset
;
1604 if (file
->f_pos
>= 2) {
1605 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1606 struct configfs_dirent
*cursor
= file
->private_data
;
1607 struct list_head
*p
;
1608 loff_t n
= file
->f_pos
- 2;
1610 spin_lock(&configfs_dirent_lock
);
1611 list_del(&cursor
->s_sibling
);
1612 p
= sd
->s_children
.next
;
1613 while (n
&& p
!= &sd
->s_children
) {
1614 struct configfs_dirent
*next
;
1615 next
= list_entry(p
, struct configfs_dirent
,
1617 if (next
->s_element
)
1621 list_add_tail(&cursor
->s_sibling
, p
);
1622 spin_unlock(&configfs_dirent_lock
);
1625 mutex_unlock(&d_inode(dentry
)->i_mutex
);
1629 const struct file_operations configfs_dir_operations
= {
1630 .open
= configfs_dir_open
,
1631 .release
= configfs_dir_close
,
1632 .llseek
= configfs_dir_lseek
,
1633 .read
= generic_read_dir
,
1634 .iterate
= configfs_readdir
,
1638 * configfs_register_group - creates a parent-child relation between two groups
1639 * @parent_group: parent group
1640 * @group: child group
1642 * link groups, creates dentry for the child and attaches it to the
1645 * Return: 0 on success, negative errno code on error
1647 int configfs_register_group(struct config_group
*parent_group
,
1648 struct config_group
*group
)
1650 struct configfs_subsystem
*subsys
= parent_group
->cg_subsys
;
1651 struct dentry
*parent
;
1654 mutex_lock(&subsys
->su_mutex
);
1655 link_group(parent_group
, group
);
1656 mutex_unlock(&subsys
->su_mutex
);
1658 parent
= parent_group
->cg_item
.ci_dentry
;
1660 mutex_lock_nested(&d_inode(parent
)->i_mutex
, I_MUTEX_PARENT
);
1661 ret
= create_default_group(parent_group
, group
);
1663 spin_lock(&configfs_dirent_lock
);
1664 configfs_dir_set_ready(group
->cg_item
.ci_dentry
->d_fsdata
);
1665 spin_unlock(&configfs_dirent_lock
);
1667 mutex_unlock(&d_inode(parent
)->i_mutex
);
1670 EXPORT_SYMBOL(configfs_register_group
);
1673 * configfs_unregister_group() - unregisters a child group from its parent
1674 * @group: parent group to be unregistered
1676 * Undoes configfs_register_group()
1678 void configfs_unregister_group(struct config_group
*group
)
1680 struct configfs_subsystem
*subsys
= group
->cg_subsys
;
1681 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1682 struct dentry
*parent
= group
->cg_item
.ci_parent
->ci_dentry
;
1684 mutex_lock_nested(&d_inode(parent
)->i_mutex
, I_MUTEX_PARENT
);
1685 spin_lock(&configfs_dirent_lock
);
1686 configfs_detach_prep(dentry
, NULL
);
1687 spin_unlock(&configfs_dirent_lock
);
1689 configfs_detach_group(&group
->cg_item
);
1690 d_inode(dentry
)->i_flags
|= S_DEAD
;
1693 mutex_unlock(&d_inode(parent
)->i_mutex
);
1697 mutex_lock(&subsys
->su_mutex
);
1698 unlink_group(group
);
1699 mutex_unlock(&subsys
->su_mutex
);
1701 EXPORT_SYMBOL(configfs_unregister_group
);
1704 * configfs_register_default_group() - allocates and registers a child group
1705 * @parent_group: parent group
1706 * @name: child group name
1707 * @item_type: child item type description
1709 * boilerplate to allocate and register a child group with its parent. We need
1710 * kzalloc'ed memory because child's default_group is initially empty.
1712 * Return: allocated config group or ERR_PTR() on error
1714 struct config_group
*
1715 configfs_register_default_group(struct config_group
*parent_group
,
1717 struct config_item_type
*item_type
)
1720 struct config_group
*group
;
1722 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
1724 return ERR_PTR(-ENOMEM
);
1725 config_group_init_type_name(group
, name
, item_type
);
1727 ret
= configfs_register_group(parent_group
, group
);
1730 return ERR_PTR(ret
);
1734 EXPORT_SYMBOL(configfs_register_default_group
);
1737 * configfs_unregister_default_group() - unregisters and frees a child group
1738 * @group: the group to act on
1740 void configfs_unregister_default_group(struct config_group
*group
)
1742 configfs_unregister_group(group
);
1745 EXPORT_SYMBOL(configfs_unregister_default_group
);
1747 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1750 struct config_group
*group
= &subsys
->su_group
;
1751 struct dentry
*dentry
;
1752 struct dentry
*root
;
1753 struct configfs_dirent
*sd
;
1755 root
= configfs_pin_fs();
1757 return PTR_ERR(root
);
1759 if (!group
->cg_item
.ci_name
)
1760 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1762 sd
= root
->d_fsdata
;
1763 link_group(to_config_group(sd
->s_element
), group
);
1765 mutex_lock_nested(&d_inode(root
)->i_mutex
, I_MUTEX_PARENT
);
1768 dentry
= d_alloc_name(root
, group
->cg_item
.ci_name
);
1770 d_add(dentry
, NULL
);
1772 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1775 BUG_ON(d_inode(dentry
));
1779 spin_lock(&configfs_dirent_lock
);
1780 configfs_dir_set_ready(dentry
->d_fsdata
);
1781 spin_unlock(&configfs_dirent_lock
);
1785 mutex_unlock(&d_inode(root
)->i_mutex
);
1788 unlink_group(group
);
1789 configfs_release_fs();
1795 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1797 struct config_group
*group
= &subsys
->su_group
;
1798 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1799 struct dentry
*root
= dentry
->d_sb
->s_root
;
1801 if (dentry
->d_parent
!= root
) {
1802 pr_err("Tried to unregister non-subsystem!\n");
1806 mutex_lock_nested(&d_inode(root
)->i_mutex
,
1808 mutex_lock_nested(&d_inode(dentry
)->i_mutex
, I_MUTEX_CHILD
);
1809 mutex_lock(&configfs_symlink_mutex
);
1810 spin_lock(&configfs_dirent_lock
);
1811 if (configfs_detach_prep(dentry
, NULL
)) {
1812 pr_err("Tried to unregister non-empty subsystem!\n");
1814 spin_unlock(&configfs_dirent_lock
);
1815 mutex_unlock(&configfs_symlink_mutex
);
1816 configfs_detach_group(&group
->cg_item
);
1817 d_inode(dentry
)->i_flags
|= S_DEAD
;
1819 mutex_unlock(&d_inode(dentry
)->i_mutex
);
1823 mutex_unlock(&d_inode(root
)->i_mutex
);
1827 unlink_group(group
);
1828 configfs_release_fs();
1831 EXPORT_SYMBOL(configfs_register_subsystem
);
1832 EXPORT_SYMBOL(configfs_unregister_subsystem
);