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
);
79 * We _must_ delete our dentries on last dput, as the chain-to-parent
80 * behavior is required to clear the parents of default_groups.
82 static int configfs_d_delete(const struct dentry
*dentry
)
87 const struct dentry_operations configfs_dentry_ops
= {
88 .d_iput
= configfs_d_iput
,
89 /* simple_delete_dentry() isn't exported */
90 .d_delete
= configfs_d_delete
,
96 * Helpers to make lockdep happy with our recursive locking of default groups'
97 * inodes (see configfs_attach_group() and configfs_detach_group()).
98 * We put default groups i_mutexes in separate classes according to their depth
99 * from the youngest non-default group ancestor.
101 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
102 * groups A/B and A/C will have their inode's mutex in class
103 * default_group_class[0], and default group A/C/D will be in
104 * default_group_class[1].
106 * The lock classes are declared and assigned in inode.c, according to the
108 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
109 * default groups, and reset to -1 when all default groups are attached. During
110 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
111 * inode's mutex is set to default_group_class[s_depth - 1].
114 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
119 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
120 struct configfs_dirent
*sd
)
122 int parent_depth
= parent_sd
->s_depth
;
124 if (parent_depth
>= 0)
125 sd
->s_depth
= parent_depth
+ 1;
129 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
132 * item's i_mutex class is already setup, so s_depth is now only
133 * used to set new sub-directories s_depth, which is always done
134 * with item's i_mutex locked.
137 * sd->s_depth == -1 iff we are a non default group.
138 * else (we are a default group) sd->s_depth > 0 (see
141 if (sd
->s_depth
== -1)
143 * We are a non default group and we are going to create
150 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
152 /* We will not create default groups anymore. */
156 #else /* CONFIG_LOCKDEP */
158 static void configfs_init_dirent_depth(struct configfs_dirent
*sd
)
162 static void configfs_set_dir_dirent_depth(struct configfs_dirent
*parent_sd
,
163 struct configfs_dirent
*sd
)
168 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent
*sd
)
173 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent
*sd
)
177 #endif /* CONFIG_LOCKDEP */
180 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
182 static struct configfs_dirent
*configfs_new_dirent(struct configfs_dirent
*parent_sd
,
183 void *element
, int type
)
185 struct configfs_dirent
* sd
;
187 sd
= kmem_cache_zalloc(configfs_dir_cachep
, GFP_KERNEL
);
189 return ERR_PTR(-ENOMEM
);
191 atomic_set(&sd
->s_count
, 1);
192 INIT_LIST_HEAD(&sd
->s_links
);
193 INIT_LIST_HEAD(&sd
->s_children
);
194 sd
->s_element
= element
;
196 configfs_init_dirent_depth(sd
);
197 spin_lock(&configfs_dirent_lock
);
198 if (parent_sd
->s_type
& CONFIGFS_USET_DROPPING
) {
199 spin_unlock(&configfs_dirent_lock
);
200 kmem_cache_free(configfs_dir_cachep
, sd
);
201 return ERR_PTR(-ENOENT
);
203 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
204 spin_unlock(&configfs_dirent_lock
);
211 * Return -EEXIST if there is already a configfs element with the same
212 * name for the same parent.
214 * called with parent inode's i_mutex held
216 static int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
217 const unsigned char *new)
219 struct configfs_dirent
* sd
;
221 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
223 const unsigned char *existing
= configfs_get_name(sd
);
224 if (strcmp(existing
, new))
235 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
236 struct dentry
* dentry
, void * element
,
237 umode_t mode
, int type
)
239 struct configfs_dirent
* sd
;
241 sd
= configfs_new_dirent(parent_sd
, element
, type
);
246 sd
->s_dentry
= dentry
;
248 dentry
->d_fsdata
= configfs_get(sd
);
253 static int init_dir(struct inode
* inode
)
255 inode
->i_op
= &configfs_dir_inode_operations
;
256 inode
->i_fop
= &configfs_dir_operations
;
258 /* directory inodes start off with i_nlink == 2 (for "." entry) */
263 static int configfs_init_file(struct inode
* inode
)
265 inode
->i_size
= PAGE_SIZE
;
266 inode
->i_fop
= &configfs_file_operations
;
270 static int init_symlink(struct inode
* inode
)
272 inode
->i_op
= &configfs_symlink_inode_operations
;
276 static int create_dir(struct config_item
*k
, struct dentry
*d
)
279 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
280 struct dentry
*p
= d
->d_parent
;
284 error
= configfs_dirent_exists(p
->d_fsdata
, d
->d_name
.name
);
286 error
= configfs_make_dirent(p
->d_fsdata
, d
, k
, mode
,
287 CONFIGFS_DIR
| CONFIGFS_USET_CREATING
);
289 configfs_set_dir_dirent_depth(p
->d_fsdata
, d
->d_fsdata
);
290 error
= configfs_create(d
, mode
, init_dir
);
292 inc_nlink(p
->d_inode
);
294 struct configfs_dirent
*sd
= d
->d_fsdata
;
296 spin_lock(&configfs_dirent_lock
);
297 list_del_init(&sd
->s_sibling
);
298 spin_unlock(&configfs_dirent_lock
);
308 * configfs_create_dir - create a directory for an config_item.
309 * @item: config_itemwe're creating directory for.
310 * @dentry: config_item's dentry.
312 * Note: user-created entries won't be allowed under this new directory
313 * until it is validated by configfs_dir_set_ready()
316 static int configfs_create_dir(struct config_item
* item
, struct dentry
*dentry
)
318 int error
= create_dir(item
, dentry
);
320 item
->ci_dentry
= dentry
;
325 * Allow userspace to create new entries under a new directory created with
326 * configfs_create_dir(), and under all of its chidlren directories recursively.
327 * @sd configfs_dirent of the new directory to validate
329 * Caller must hold configfs_dirent_lock.
331 static void configfs_dir_set_ready(struct configfs_dirent
*sd
)
333 struct configfs_dirent
*child_sd
;
335 sd
->s_type
&= ~CONFIGFS_USET_CREATING
;
336 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
)
337 if (child_sd
->s_type
& CONFIGFS_USET_CREATING
)
338 configfs_dir_set_ready(child_sd
);
342 * Check that a directory does not belong to a directory hierarchy being
343 * attached and not validated yet.
344 * @sd configfs_dirent of the directory to check
346 * @return non-zero iff the directory was validated
348 * Note: takes configfs_dirent_lock, so the result may change from false to true
349 * in two consecutive calls, but never from true to false.
351 int configfs_dirent_is_ready(struct configfs_dirent
*sd
)
355 spin_lock(&configfs_dirent_lock
);
356 ret
= !(sd
->s_type
& CONFIGFS_USET_CREATING
);
357 spin_unlock(&configfs_dirent_lock
);
362 int configfs_create_link(struct configfs_symlink
*sl
,
363 struct dentry
*parent
,
364 struct dentry
*dentry
)
367 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
369 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
372 err
= configfs_create(dentry
, mode
, init_symlink
);
374 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
376 spin_lock(&configfs_dirent_lock
);
377 list_del_init(&sd
->s_sibling
);
378 spin_unlock(&configfs_dirent_lock
);
386 static void remove_dir(struct dentry
* d
)
388 struct dentry
* parent
= dget(d
->d_parent
);
389 struct configfs_dirent
* sd
;
392 spin_lock(&configfs_dirent_lock
);
393 list_del_init(&sd
->s_sibling
);
394 spin_unlock(&configfs_dirent_lock
);
397 simple_rmdir(parent
->d_inode
,d
);
399 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
, d_count(d
));
405 * configfs_remove_dir - remove an config_item's directory.
406 * @item: config_item we're removing.
408 * The only thing special about this is that we remove any files in
409 * the directory before we remove the directory, and we've inlined
410 * what used to be configfs_rmdir() below, instead of calling separately.
412 * Caller holds the mutex of the item's inode
415 static void configfs_remove_dir(struct config_item
* item
)
417 struct dentry
* dentry
= dget(item
->ci_dentry
);
424 * Drop reference from dget() on entrance.
430 /* attaches attribute's configfs_dirent to the dentry corresponding to the
433 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
435 struct configfs_attribute
* attr
= sd
->s_element
;
438 spin_lock(&configfs_dirent_lock
);
439 dentry
->d_fsdata
= configfs_get(sd
);
440 sd
->s_dentry
= dentry
;
441 spin_unlock(&configfs_dirent_lock
);
443 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
,
455 static struct dentry
* configfs_lookup(struct inode
*dir
,
456 struct dentry
*dentry
,
459 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
460 struct configfs_dirent
* sd
;
465 * Fake invisibility if dir belongs to a group/default groups hierarchy
468 * This forbids userspace to read/write attributes of items which may
469 * not complete their initialization, since the dentries of the
470 * attributes won't be instantiated.
473 if (!configfs_dirent_is_ready(parent_sd
))
476 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
477 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
478 const unsigned char * name
= configfs_get_name(sd
);
480 if (strcmp(name
, dentry
->d_name
.name
))
484 err
= configfs_attach_attr(sd
, dentry
);
491 * If it doesn't exist and it isn't a NOT_PINNED item,
492 * it must be negative.
494 if (dentry
->d_name
.len
> NAME_MAX
)
495 return ERR_PTR(-ENAMETOOLONG
);
505 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
506 * attributes and are removed by rmdir(). We recurse, setting
507 * CONFIGFS_USET_DROPPING on all children that are candidates for
509 * If there is an error, the caller will reset the flags via
510 * configfs_detach_rollback().
512 static int configfs_detach_prep(struct dentry
*dentry
, struct mutex
**wait_mutex
)
514 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
515 struct configfs_dirent
*sd
;
518 /* Mark that we're trying to drop the group */
519 parent_sd
->s_type
|= CONFIGFS_USET_DROPPING
;
522 if (!list_empty(&parent_sd
->s_links
))
526 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
527 if (!sd
->s_element
||
528 (sd
->s_type
& CONFIGFS_NOT_PINNED
))
530 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
531 /* Abort if racing with mkdir() */
532 if (sd
->s_type
& CONFIGFS_USET_IN_MKDIR
) {
534 *wait_mutex
= &sd
->s_dentry
->d_inode
->i_mutex
;
539 * Yup, recursive. If there's a problem, blame
540 * deep nesting of default_groups
542 ret
= configfs_detach_prep(sd
->s_dentry
, wait_mutex
);
556 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
559 static void configfs_detach_rollback(struct dentry
*dentry
)
561 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
562 struct configfs_dirent
*sd
;
564 parent_sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
566 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
)
567 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
568 configfs_detach_rollback(sd
->s_dentry
);
571 static void detach_attrs(struct config_item
* item
)
573 struct dentry
* dentry
= dget(item
->ci_dentry
);
574 struct configfs_dirent
* parent_sd
;
575 struct configfs_dirent
* sd
, * tmp
;
580 pr_debug("configfs %s: dropping attrs for dir\n",
581 dentry
->d_name
.name
);
583 parent_sd
= dentry
->d_fsdata
;
584 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
585 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
587 spin_lock(&configfs_dirent_lock
);
588 list_del_init(&sd
->s_sibling
);
589 spin_unlock(&configfs_dirent_lock
);
590 configfs_drop_dentry(sd
, dentry
);
595 * Drop reference from dget() on entrance.
600 static int populate_attrs(struct config_item
*item
)
602 struct config_item_type
*t
= item
->ci_type
;
603 struct configfs_attribute
*attr
;
610 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
611 if ((error
= configfs_create_file(item
, attr
)))
622 static int configfs_attach_group(struct config_item
*parent_item
,
623 struct config_item
*item
,
624 struct dentry
*dentry
);
625 static void configfs_detach_group(struct config_item
*item
);
627 static void detach_groups(struct config_group
*group
)
629 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
630 struct dentry
*child
;
631 struct configfs_dirent
*parent_sd
;
632 struct configfs_dirent
*sd
, *tmp
;
637 parent_sd
= dentry
->d_fsdata
;
638 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
639 if (!sd
->s_element
||
640 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
643 child
= sd
->s_dentry
;
645 mutex_lock(&child
->d_inode
->i_mutex
);
647 configfs_detach_group(sd
->s_element
);
648 child
->d_inode
->i_flags
|= S_DEAD
;
651 mutex_unlock(&child
->d_inode
->i_mutex
);
658 * Drop reference from dget() on entrance.
664 * This fakes mkdir(2) on a default_groups[] entry. It
665 * creates a dentry, attachs it, and then does fixup
668 * We could, perhaps, tweak our parent's ->mkdir for a minute and
669 * try using vfs_mkdir. Just a thought.
671 static int create_default_group(struct config_group
*parent_group
,
672 struct config_group
*group
)
675 struct configfs_dirent
*sd
;
676 /* We trust the caller holds a reference to parent */
677 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
679 if (!group
->cg_item
.ci_name
)
680 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
683 child
= d_alloc_name(parent
, group
->cg_item
.ci_name
);
687 ret
= configfs_attach_group(&parent_group
->cg_item
,
688 &group
->cg_item
, child
);
690 sd
= child
->d_fsdata
;
691 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
693 BUG_ON(child
->d_inode
);
702 static int populate_groups(struct config_group
*group
)
704 struct config_group
*new_group
;
708 if (group
->default_groups
) {
709 for (i
= 0; group
->default_groups
[i
]; i
++) {
710 new_group
= group
->default_groups
[i
];
712 ret
= create_default_group(group
, new_group
);
714 detach_groups(group
);
724 * All of link_obj/unlink_obj/link_group/unlink_group require that
725 * subsys->su_mutex is held.
728 static void unlink_obj(struct config_item
*item
)
730 struct config_group
*group
;
732 group
= item
->ci_group
;
734 list_del_init(&item
->ci_entry
);
736 item
->ci_group
= NULL
;
737 item
->ci_parent
= NULL
;
739 /* Drop the reference for ci_entry */
740 config_item_put(item
);
742 /* Drop the reference for ci_parent */
743 config_group_put(group
);
747 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
750 * Parent seems redundant with group, but it makes certain
751 * traversals much nicer.
753 item
->ci_parent
= parent_item
;
756 * We hold a reference on the parent for the child's ci_parent
759 item
->ci_group
= config_group_get(to_config_group(parent_item
));
760 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
763 * We hold a reference on the child for ci_entry on the parent's
766 config_item_get(item
);
769 static void unlink_group(struct config_group
*group
)
772 struct config_group
*new_group
;
774 if (group
->default_groups
) {
775 for (i
= 0; group
->default_groups
[i
]; i
++) {
776 new_group
= group
->default_groups
[i
];
777 unlink_group(new_group
);
781 group
->cg_subsys
= NULL
;
782 unlink_obj(&group
->cg_item
);
785 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
788 struct config_group
*new_group
;
789 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
791 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
793 if (parent_group
->cg_subsys
)
794 subsys
= parent_group
->cg_subsys
;
795 else if (configfs_is_root(&parent_group
->cg_item
))
796 subsys
= to_configfs_subsystem(group
);
799 group
->cg_subsys
= subsys
;
801 if (group
->default_groups
) {
802 for (i
= 0; group
->default_groups
[i
]; i
++) {
803 new_group
= group
->default_groups
[i
];
804 link_group(group
, new_group
);
810 * The goal is that configfs_attach_item() (and
811 * configfs_attach_group()) can be called from either the VFS or this
812 * module. That is, they assume that the items have been created,
813 * the dentry allocated, and the dcache is all ready to go.
815 * If they fail, they must clean up after themselves as if they
816 * had never been called. The caller (VFS or local function) will
817 * handle cleaning up the dcache bits.
819 * configfs_detach_group() and configfs_detach_item() behave similarly on
820 * the way out. They assume that the proper semaphores are held, they
821 * clean up the configfs items, and they expect their callers will
822 * handle the dcache bits.
824 static int configfs_attach_item(struct config_item
*parent_item
,
825 struct config_item
*item
,
826 struct dentry
*dentry
)
830 ret
= configfs_create_dir(item
, dentry
);
832 ret
= populate_attrs(item
);
835 * We are going to remove an inode and its dentry but
836 * the VFS may already have hit and used them. Thus,
837 * we must lock them as rmdir() would.
839 mutex_lock(&dentry
->d_inode
->i_mutex
);
840 configfs_remove_dir(item
);
841 dentry
->d_inode
->i_flags
|= S_DEAD
;
843 mutex_unlock(&dentry
->d_inode
->i_mutex
);
851 /* Caller holds the mutex of the item's inode */
852 static void configfs_detach_item(struct config_item
*item
)
855 configfs_remove_dir(item
);
858 static int configfs_attach_group(struct config_item
*parent_item
,
859 struct config_item
*item
,
860 struct dentry
*dentry
)
863 struct configfs_dirent
*sd
;
865 ret
= configfs_attach_item(parent_item
, item
, dentry
);
867 sd
= dentry
->d_fsdata
;
868 sd
->s_type
|= CONFIGFS_USET_DIR
;
871 * FYI, we're faking mkdir in populate_groups()
872 * We must lock the group's inode to avoid races with the VFS
873 * which can already hit the inode and try to add/remove entries
876 * We must also lock the inode to remove it safely in case of
877 * error, as rmdir() would.
879 mutex_lock_nested(&dentry
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
880 configfs_adjust_dir_dirent_depth_before_populate(sd
);
881 ret
= populate_groups(to_config_group(item
));
883 configfs_detach_item(item
);
884 dentry
->d_inode
->i_flags
|= S_DEAD
;
887 configfs_adjust_dir_dirent_depth_after_populate(sd
);
888 mutex_unlock(&dentry
->d_inode
->i_mutex
);
896 /* Caller holds the mutex of the group's inode */
897 static void configfs_detach_group(struct config_item
*item
)
899 detach_groups(to_config_group(item
));
900 configfs_detach_item(item
);
904 * After the item has been detached from the filesystem view, we are
905 * ready to tear it out of the hierarchy. Notify the client before
906 * we do that so they can perform any cleanup that requires
907 * navigating the hierarchy. A client does not need to provide this
908 * callback. The subsystem semaphore MUST be held by the caller, and
909 * references must be valid for both items. It also assumes the
910 * caller has validated ci_type.
912 static void client_disconnect_notify(struct config_item
*parent_item
,
913 struct config_item
*item
)
915 struct config_item_type
*type
;
917 type
= parent_item
->ci_type
;
920 if (type
->ct_group_ops
&& type
->ct_group_ops
->disconnect_notify
)
921 type
->ct_group_ops
->disconnect_notify(to_config_group(parent_item
),
926 * Drop the initial reference from make_item()/make_group()
927 * This function assumes that reference is held on item
928 * and that item holds a valid reference to the parent. Also, it
929 * assumes the caller has validated ci_type.
931 static void client_drop_item(struct config_item
*parent_item
,
932 struct config_item
*item
)
934 struct config_item_type
*type
;
936 type
= parent_item
->ci_type
;
940 * If ->drop_item() exists, it is responsible for the
943 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
944 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
947 config_item_put(item
);
951 static void configfs_dump_one(struct configfs_dirent
*sd
, int level
)
953 printk(KERN_INFO
"%*s\"%s\":\n", level
, " ", configfs_get_name(sd
));
955 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
956 type_print(CONFIGFS_ROOT
);
957 type_print(CONFIGFS_DIR
);
958 type_print(CONFIGFS_ITEM_ATTR
);
959 type_print(CONFIGFS_ITEM_LINK
);
960 type_print(CONFIGFS_USET_DIR
);
961 type_print(CONFIGFS_USET_DEFAULT
);
962 type_print(CONFIGFS_USET_DROPPING
);
966 static int configfs_dump(struct configfs_dirent
*sd
, int level
)
968 struct configfs_dirent
*child_sd
;
971 configfs_dump_one(sd
, level
);
973 if (!(sd
->s_type
& (CONFIGFS_DIR
|CONFIGFS_ROOT
)))
976 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
977 ret
= configfs_dump(child_sd
, level
+ 2);
988 * configfs_depend_item() and configfs_undepend_item()
990 * WARNING: Do not call these from a configfs callback!
992 * This describes these functions and their helpers.
994 * Allow another kernel system to depend on a config_item. If this
995 * happens, the item cannot go away until the dependent can live without
996 * it. The idea is to give client modules as simple an interface as
997 * possible. When a system asks them to depend on an item, they just
998 * call configfs_depend_item(). If the item is live and the client
999 * driver is in good shape, we'll happily do the work for them.
1001 * Why is the locking complex? Because configfs uses the VFS to handle
1002 * all locking, but this function is called outside the normal
1003 * VFS->configfs path. So it must take VFS locks to prevent the
1004 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1005 * why you can't call these functions underneath configfs callbacks.
1007 * Note, btw, that this can be called at *any* time, even when a configfs
1008 * subsystem isn't registered, or when configfs is loading or unloading.
1009 * Just like configfs_register_subsystem(). So we take the same
1010 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1011 * If we can find the target item in the
1012 * configfs tree, it must be part of the subsystem tree as well, so we
1013 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1014 * locking out mkdir() and rmdir(), who might be racing us.
1018 * configfs_depend_prep()
1020 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1021 * attributes. This is similar but not the same to configfs_detach_prep().
1022 * Note that configfs_detach_prep() expects the parent to be locked when it
1023 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1024 * do that so we can unlock it if we find nothing.
1026 * Here we do a depth-first search of the dentry hierarchy looking for
1028 * We deliberately ignore items tagged as dropping since they are virtually
1029 * dead, as well as items in the middle of attachment since they virtually
1030 * do not exist yet. This completes the locking out of racing mkdir() and
1032 * Note: subdirectories in the middle of attachment start with s_type =
1033 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1034 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1035 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1037 * If the target is not found, -ENOENT is bubbled up.
1039 * This adds a requirement that all config_items be unique!
1041 * This is recursive. There isn't
1042 * much on the stack, though, so folks that need this function - be careful
1043 * about your stack! Patches will be accepted to make it iterative.
1045 static int configfs_depend_prep(struct dentry
*origin
,
1046 struct config_item
*target
)
1048 struct configfs_dirent
*child_sd
, *sd
;
1051 BUG_ON(!origin
|| !origin
->d_fsdata
);
1052 sd
= origin
->d_fsdata
;
1054 if (sd
->s_element
== target
) /* Boo-yah */
1057 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
1058 if ((child_sd
->s_type
& CONFIGFS_DIR
) &&
1059 !(child_sd
->s_type
& CONFIGFS_USET_DROPPING
) &&
1060 !(child_sd
->s_type
& CONFIGFS_USET_CREATING
)) {
1061 ret
= configfs_depend_prep(child_sd
->s_dentry
,
1064 goto out
; /* Child path boo-yah */
1068 /* We looped all our children and didn't find target */
1075 int configfs_depend_item(struct configfs_subsystem
*subsys
,
1076 struct config_item
*target
)
1079 struct configfs_dirent
*p
, *root_sd
, *subsys_sd
= NULL
;
1080 struct config_item
*s_item
= &subsys
->su_group
.cg_item
;
1081 struct dentry
*root
;
1084 * Pin the configfs filesystem. This means we can safely access
1085 * the root of the configfs filesystem.
1087 root
= configfs_pin_fs();
1089 return PTR_ERR(root
);
1092 * Next, lock the root directory. We're going to check that the
1093 * subsystem is really registered, and so we need to lock out
1094 * configfs_[un]register_subsystem().
1096 mutex_lock(&root
->d_inode
->i_mutex
);
1098 root_sd
= root
->d_fsdata
;
1100 list_for_each_entry(p
, &root_sd
->s_children
, s_sibling
) {
1101 if (p
->s_type
& CONFIGFS_DIR
) {
1102 if (p
->s_element
== s_item
) {
1114 /* Ok, now we can trust subsys/s_item */
1116 spin_lock(&configfs_dirent_lock
);
1117 /* Scan the tree, return 0 if found */
1118 ret
= configfs_depend_prep(subsys_sd
->s_dentry
, target
);
1120 goto out_unlock_dirent_lock
;
1123 * We are sure that the item is not about to be removed by rmdir(), and
1124 * not in the middle of attachment by mkdir().
1126 p
= target
->ci_dentry
->d_fsdata
;
1127 p
->s_dependent_count
+= 1;
1129 out_unlock_dirent_lock
:
1130 spin_unlock(&configfs_dirent_lock
);
1132 mutex_unlock(&root
->d_inode
->i_mutex
);
1135 * If we succeeded, the fs is pinned via other methods. If not,
1136 * we're done with it anyway. So release_fs() is always right.
1138 configfs_release_fs();
1142 EXPORT_SYMBOL(configfs_depend_item
);
1145 * Release the dependent linkage. This is much simpler than
1146 * configfs_depend_item() because we know that that the client driver is
1147 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1149 void configfs_undepend_item(struct configfs_subsystem
*subsys
,
1150 struct config_item
*target
)
1152 struct configfs_dirent
*sd
;
1155 * Since we can trust everything is pinned, we just need
1156 * configfs_dirent_lock.
1158 spin_lock(&configfs_dirent_lock
);
1160 sd
= target
->ci_dentry
->d_fsdata
;
1161 BUG_ON(sd
->s_dependent_count
< 1);
1163 sd
->s_dependent_count
-= 1;
1166 * After this unlock, we cannot trust the item to stay alive!
1167 * DO NOT REFERENCE item after this unlock.
1169 spin_unlock(&configfs_dirent_lock
);
1171 EXPORT_SYMBOL(configfs_undepend_item
);
1173 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
1177 struct config_group
*group
= NULL
;
1178 struct config_item
*item
= NULL
;
1179 struct config_item
*parent_item
;
1180 struct configfs_subsystem
*subsys
;
1181 struct configfs_dirent
*sd
;
1182 struct config_item_type
*type
;
1183 struct module
*subsys_owner
= NULL
, *new_item_owner
= NULL
;
1186 sd
= dentry
->d_parent
->d_fsdata
;
1189 * Fake invisibility if dir belongs to a group/default groups hierarchy
1192 if (!configfs_dirent_is_ready(sd
)) {
1197 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
1202 /* Get a working ref for the duration of this function */
1203 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1204 type
= parent_item
->ci_type
;
1205 subsys
= to_config_group(parent_item
)->cg_subsys
;
1208 if (!type
|| !type
->ct_group_ops
||
1209 (!type
->ct_group_ops
->make_group
&&
1210 !type
->ct_group_ops
->make_item
)) {
1211 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
1216 * The subsystem may belong to a different module than the item
1217 * being created. We don't want to safely pin the new item but
1218 * fail to pin the subsystem it sits under.
1220 if (!subsys
->su_group
.cg_item
.ci_type
) {
1224 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1225 if (!try_module_get(subsys_owner
)) {
1230 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
1233 goto out_subsys_put
;
1236 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
1238 mutex_lock(&subsys
->su_mutex
);
1239 if (type
->ct_group_ops
->make_group
) {
1240 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
1242 group
= ERR_PTR(-ENOMEM
);
1243 if (!IS_ERR(group
)) {
1244 link_group(to_config_group(parent_item
), group
);
1245 item
= &group
->cg_item
;
1247 ret
= PTR_ERR(group
);
1249 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
1251 item
= ERR_PTR(-ENOMEM
);
1253 link_obj(parent_item
, item
);
1255 ret
= PTR_ERR(item
);
1257 mutex_unlock(&subsys
->su_mutex
);
1262 * If ret != 0, then link_obj() was never called.
1263 * There are no extra references to clean up.
1265 goto out_subsys_put
;
1269 * link_obj() has been called (via link_group() for groups).
1270 * From here on out, errors must clean that up.
1273 type
= item
->ci_type
;
1279 new_item_owner
= type
->ct_owner
;
1280 if (!try_module_get(new_item_owner
)) {
1286 * I hate doing it this way, but if there is
1287 * an error, module_put() probably should
1288 * happen after any cleanup.
1293 * Make racing rmdir() fail if it did not tag parent with
1294 * CONFIGFS_USET_DROPPING
1295 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1296 * fail and let rmdir() terminate correctly
1298 spin_lock(&configfs_dirent_lock
);
1299 /* This will make configfs_detach_prep() fail */
1300 sd
->s_type
|= CONFIGFS_USET_IN_MKDIR
;
1301 spin_unlock(&configfs_dirent_lock
);
1304 ret
= configfs_attach_group(parent_item
, item
, dentry
);
1306 ret
= configfs_attach_item(parent_item
, item
, dentry
);
1308 spin_lock(&configfs_dirent_lock
);
1309 sd
->s_type
&= ~CONFIGFS_USET_IN_MKDIR
;
1311 configfs_dir_set_ready(dentry
->d_fsdata
);
1312 spin_unlock(&configfs_dirent_lock
);
1316 /* Tear down everything we built up */
1317 mutex_lock(&subsys
->su_mutex
);
1319 client_disconnect_notify(parent_item
, item
);
1321 unlink_group(group
);
1324 client_drop_item(parent_item
, item
);
1326 mutex_unlock(&subsys
->su_mutex
);
1329 module_put(new_item_owner
);
1334 module_put(subsys_owner
);
1338 * link_obj()/link_group() took a reference from child->parent,
1339 * so the parent is safely pinned. We can drop our working
1342 config_item_put(parent_item
);
1348 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1350 struct config_item
*parent_item
;
1351 struct config_item
*item
;
1352 struct configfs_subsystem
*subsys
;
1353 struct configfs_dirent
*sd
;
1354 struct module
*subsys_owner
= NULL
, *dead_item_owner
= NULL
;
1357 sd
= dentry
->d_fsdata
;
1358 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
1361 /* Get a working ref until we have the child */
1362 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1363 subsys
= to_config_group(parent_item
)->cg_subsys
;
1366 if (!parent_item
->ci_type
) {
1367 config_item_put(parent_item
);
1371 /* configfs_mkdir() shouldn't have allowed this */
1372 BUG_ON(!subsys
->su_group
.cg_item
.ci_type
);
1373 subsys_owner
= subsys
->su_group
.cg_item
.ci_type
->ct_owner
;
1376 * Ensure that no racing symlink() will make detach_prep() fail while
1377 * the new link is temporarily attached
1380 struct mutex
*wait_mutex
;
1382 mutex_lock(&configfs_symlink_mutex
);
1383 spin_lock(&configfs_dirent_lock
);
1385 * Here's where we check for dependents. We're protected by
1386 * configfs_dirent_lock.
1387 * If no dependent, atomically tag the item as dropping.
1389 ret
= sd
->s_dependent_count
? -EBUSY
: 0;
1391 ret
= configfs_detach_prep(dentry
, &wait_mutex
);
1393 configfs_detach_rollback(dentry
);
1395 spin_unlock(&configfs_dirent_lock
);
1396 mutex_unlock(&configfs_symlink_mutex
);
1399 if (ret
!= -EAGAIN
) {
1400 config_item_put(parent_item
);
1404 /* Wait until the racing operation terminates */
1405 mutex_lock(wait_mutex
);
1406 mutex_unlock(wait_mutex
);
1408 } while (ret
== -EAGAIN
);
1410 /* Get a working ref for the duration of this function */
1411 item
= configfs_get_config_item(dentry
);
1413 /* Drop reference from above, item already holds one. */
1414 config_item_put(parent_item
);
1417 dead_item_owner
= item
->ci_type
->ct_owner
;
1419 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
1420 configfs_detach_group(item
);
1422 mutex_lock(&subsys
->su_mutex
);
1423 client_disconnect_notify(parent_item
, item
);
1424 unlink_group(to_config_group(item
));
1426 configfs_detach_item(item
);
1428 mutex_lock(&subsys
->su_mutex
);
1429 client_disconnect_notify(parent_item
, item
);
1433 client_drop_item(parent_item
, item
);
1434 mutex_unlock(&subsys
->su_mutex
);
1436 /* Drop our reference from above */
1437 config_item_put(item
);
1439 module_put(dead_item_owner
);
1440 module_put(subsys_owner
);
1445 const struct inode_operations configfs_dir_inode_operations
= {
1446 .mkdir
= configfs_mkdir
,
1447 .rmdir
= configfs_rmdir
,
1448 .symlink
= configfs_symlink
,
1449 .unlink
= configfs_unlink
,
1450 .lookup
= configfs_lookup
,
1451 .setattr
= configfs_setattr
,
1454 const struct inode_operations configfs_root_inode_operations
= {
1455 .lookup
= configfs_lookup
,
1456 .setattr
= configfs_setattr
,
1460 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
1463 struct dentry
* new_dentry
, * parent
;
1465 if (!strcmp(config_item_name(item
), new_name
))
1471 down_write(&configfs_rename_sem
);
1472 parent
= item
->parent
->dentry
;
1474 mutex_lock(&parent
->d_inode
->i_mutex
);
1476 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
1477 if (!IS_ERR(new_dentry
)) {
1478 if (!new_dentry
->d_inode
) {
1479 error
= config_item_set_name(item
, "%s", new_name
);
1481 d_add(new_dentry
, NULL
);
1482 d_move(item
->dentry
, new_dentry
);
1485 d_delete(new_dentry
);
1490 mutex_unlock(&parent
->d_inode
->i_mutex
);
1491 up_write(&configfs_rename_sem
);
1497 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
1499 struct dentry
* dentry
= file
->f_path
.dentry
;
1500 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1503 mutex_lock(&dentry
->d_inode
->i_mutex
);
1505 * Fake invisibility if dir belongs to a group/default groups hierarchy
1509 if (configfs_dirent_is_ready(parent_sd
)) {
1510 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
, 0);
1511 if (IS_ERR(file
->private_data
))
1512 err
= PTR_ERR(file
->private_data
);
1516 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1521 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
1523 struct dentry
* dentry
= file
->f_path
.dentry
;
1524 struct configfs_dirent
* cursor
= file
->private_data
;
1526 mutex_lock(&dentry
->d_inode
->i_mutex
);
1527 spin_lock(&configfs_dirent_lock
);
1528 list_del_init(&cursor
->s_sibling
);
1529 spin_unlock(&configfs_dirent_lock
);
1530 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1532 release_configfs_dirent(cursor
);
1537 /* Relationship between s_mode and the DT_xxx types */
1538 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1540 return (sd
->s_mode
>> 12) & 15;
1543 static int configfs_readdir(struct file
*file
, struct dir_context
*ctx
)
1545 struct dentry
*dentry
= file
->f_path
.dentry
;
1546 struct super_block
*sb
= dentry
->d_sb
;
1547 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1548 struct configfs_dirent
*cursor
= file
->private_data
;
1549 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1552 if (!dir_emit_dots(file
, ctx
))
1554 if (ctx
->pos
== 2) {
1555 spin_lock(&configfs_dirent_lock
);
1556 list_move(q
, &parent_sd
->s_children
);
1557 spin_unlock(&configfs_dirent_lock
);
1559 for (p
= q
->next
; p
!= &parent_sd
->s_children
; p
= p
->next
) {
1560 struct configfs_dirent
*next
;
1563 struct inode
*inode
= NULL
;
1565 next
= list_entry(p
, struct configfs_dirent
, s_sibling
);
1566 if (!next
->s_element
)
1569 name
= configfs_get_name(next
);
1573 * We'll have a dentry and an inode for
1574 * PINNED items and for open attribute
1575 * files. We lock here to prevent a race
1576 * with configfs_d_iput() clearing
1577 * s_dentry before calling iput().
1579 * Why do we go to the trouble? If
1580 * someone has an attribute file open,
1581 * the inode number should match until
1582 * they close it. Beyond that, we don't
1585 spin_lock(&configfs_dirent_lock
);
1586 dentry
= next
->s_dentry
;
1588 inode
= dentry
->d_inode
;
1591 spin_unlock(&configfs_dirent_lock
);
1593 ino
= iunique(sb
, 2);
1595 if (!dir_emit(ctx
, name
, len
, ino
, dt_type(next
)))
1598 spin_lock(&configfs_dirent_lock
);
1600 spin_unlock(&configfs_dirent_lock
);
1607 static loff_t
configfs_dir_lseek(struct file
*file
, loff_t offset
, int whence
)
1609 struct dentry
* dentry
= file
->f_path
.dentry
;
1611 mutex_lock(&dentry
->d_inode
->i_mutex
);
1614 offset
+= file
->f_pos
;
1619 mutex_unlock(&file_inode(file
)->i_mutex
);
1622 if (offset
!= file
->f_pos
) {
1623 file
->f_pos
= offset
;
1624 if (file
->f_pos
>= 2) {
1625 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1626 struct configfs_dirent
*cursor
= file
->private_data
;
1627 struct list_head
*p
;
1628 loff_t n
= file
->f_pos
- 2;
1630 spin_lock(&configfs_dirent_lock
);
1631 list_del(&cursor
->s_sibling
);
1632 p
= sd
->s_children
.next
;
1633 while (n
&& p
!= &sd
->s_children
) {
1634 struct configfs_dirent
*next
;
1635 next
= list_entry(p
, struct configfs_dirent
,
1637 if (next
->s_element
)
1641 list_add_tail(&cursor
->s_sibling
, p
);
1642 spin_unlock(&configfs_dirent_lock
);
1645 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1649 const struct file_operations configfs_dir_operations
= {
1650 .open
= configfs_dir_open
,
1651 .release
= configfs_dir_close
,
1652 .llseek
= configfs_dir_lseek
,
1653 .read
= generic_read_dir
,
1654 .iterate
= configfs_readdir
,
1657 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1660 struct config_group
*group
= &subsys
->su_group
;
1661 struct dentry
*dentry
;
1662 struct dentry
*root
;
1663 struct configfs_dirent
*sd
;
1665 root
= configfs_pin_fs();
1667 return PTR_ERR(root
);
1669 if (!group
->cg_item
.ci_name
)
1670 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1672 sd
= root
->d_fsdata
;
1673 link_group(to_config_group(sd
->s_element
), group
);
1675 mutex_lock_nested(&root
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1678 dentry
= d_alloc_name(root
, group
->cg_item
.ci_name
);
1680 d_add(dentry
, NULL
);
1682 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1685 BUG_ON(dentry
->d_inode
);
1689 spin_lock(&configfs_dirent_lock
);
1690 configfs_dir_set_ready(dentry
->d_fsdata
);
1691 spin_unlock(&configfs_dirent_lock
);
1695 mutex_unlock(&root
->d_inode
->i_mutex
);
1698 unlink_group(group
);
1699 configfs_release_fs();
1705 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1707 struct config_group
*group
= &subsys
->su_group
;
1708 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1709 struct dentry
*root
= dentry
->d_sb
->s_root
;
1711 if (dentry
->d_parent
!= root
) {
1712 printk(KERN_ERR
"configfs: Tried to unregister non-subsystem!\n");
1716 mutex_lock_nested(&root
->d_inode
->i_mutex
,
1718 mutex_lock_nested(&dentry
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1719 mutex_lock(&configfs_symlink_mutex
);
1720 spin_lock(&configfs_dirent_lock
);
1721 if (configfs_detach_prep(dentry
, NULL
)) {
1722 printk(KERN_ERR
"configfs: Tried to unregister non-empty subsystem!\n");
1724 spin_unlock(&configfs_dirent_lock
);
1725 mutex_unlock(&configfs_symlink_mutex
);
1726 configfs_detach_group(&group
->cg_item
);
1727 dentry
->d_inode
->i_flags
|= S_DEAD
;
1729 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1733 mutex_unlock(&root
->d_inode
->i_mutex
);
1737 unlink_group(group
);
1738 configfs_release_fs();
1741 EXPORT_SYMBOL(configfs_register_subsystem
);
1742 EXPORT_SYMBOL(configfs_unregister_subsystem
);