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 BUG_ON(sd
->s_dentry
!= dentry
);
67 * We _must_ delete our dentries on last dput, as the chain-to-parent
68 * behavior is required to clear the parents of default_groups.
70 static int configfs_d_delete(struct dentry
*dentry
)
75 static struct dentry_operations configfs_dentry_ops
= {
76 .d_iput
= configfs_d_iput
,
77 /* simple_delete_dentry() isn't exported */
78 .d_delete
= configfs_d_delete
,
82 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
84 static struct configfs_dirent
*configfs_new_dirent(struct configfs_dirent
* parent_sd
,
87 struct configfs_dirent
* sd
;
89 sd
= kmem_cache_zalloc(configfs_dir_cachep
, GFP_KERNEL
);
91 return ERR_PTR(-ENOMEM
);
93 atomic_set(&sd
->s_count
, 1);
94 INIT_LIST_HEAD(&sd
->s_links
);
95 INIT_LIST_HEAD(&sd
->s_children
);
96 sd
->s_element
= element
;
97 spin_lock(&configfs_dirent_lock
);
98 if (parent_sd
->s_type
& CONFIGFS_USET_DROPPING
) {
99 spin_unlock(&configfs_dirent_lock
);
100 kmem_cache_free(configfs_dir_cachep
, sd
);
101 return ERR_PTR(-ENOENT
);
103 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
104 spin_unlock(&configfs_dirent_lock
);
111 * Return -EEXIST if there is already a configfs element with the same
112 * name for the same parent.
114 * called with parent inode's i_mutex held
116 static int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
117 const unsigned char *new)
119 struct configfs_dirent
* sd
;
121 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
123 const unsigned char *existing
= configfs_get_name(sd
);
124 if (strcmp(existing
, new))
135 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
136 struct dentry
* dentry
, void * element
,
137 umode_t mode
, int type
)
139 struct configfs_dirent
* sd
;
141 sd
= configfs_new_dirent(parent_sd
, element
);
147 sd
->s_dentry
= dentry
;
149 dentry
->d_fsdata
= configfs_get(sd
);
150 dentry
->d_op
= &configfs_dentry_ops
;
156 static int init_dir(struct inode
* inode
)
158 inode
->i_op
= &configfs_dir_inode_operations
;
159 inode
->i_fop
= &configfs_dir_operations
;
161 /* directory inodes start off with i_nlink == 2 (for "." entry) */
166 static int configfs_init_file(struct inode
* inode
)
168 inode
->i_size
= PAGE_SIZE
;
169 inode
->i_fop
= &configfs_file_operations
;
173 static int init_symlink(struct inode
* inode
)
175 inode
->i_op
= &configfs_symlink_inode_operations
;
179 static int create_dir(struct config_item
* k
, struct dentry
* p
,
183 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
185 error
= configfs_dirent_exists(p
->d_fsdata
, d
->d_name
.name
);
187 error
= configfs_make_dirent(p
->d_fsdata
, d
, k
, mode
,
190 error
= configfs_create(d
, mode
, init_dir
);
192 inc_nlink(p
->d_inode
);
193 (d
)->d_op
= &configfs_dentry_ops
;
195 struct configfs_dirent
*sd
= d
->d_fsdata
;
197 spin_lock(&configfs_dirent_lock
);
198 list_del_init(&sd
->s_sibling
);
199 spin_unlock(&configfs_dirent_lock
);
209 * configfs_create_dir - create a directory for an config_item.
210 * @item: config_itemwe're creating directory for.
211 * @dentry: config_item's dentry.
214 static int configfs_create_dir(struct config_item
* item
, struct dentry
*dentry
)
216 struct dentry
* parent
;
222 parent
= item
->ci_parent
->ci_dentry
;
223 else if (configfs_mount
&& configfs_mount
->mnt_sb
)
224 parent
= configfs_mount
->mnt_sb
->s_root
;
228 error
= create_dir(item
,parent
,dentry
);
230 item
->ci_dentry
= dentry
;
234 int configfs_create_link(struct configfs_symlink
*sl
,
235 struct dentry
*parent
,
236 struct dentry
*dentry
)
239 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
241 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
244 err
= configfs_create(dentry
, mode
, init_symlink
);
246 dentry
->d_op
= &configfs_dentry_ops
;
248 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
250 spin_lock(&configfs_dirent_lock
);
251 list_del_init(&sd
->s_sibling
);
252 spin_unlock(&configfs_dirent_lock
);
260 static void remove_dir(struct dentry
* d
)
262 struct dentry
* parent
= dget(d
->d_parent
);
263 struct configfs_dirent
* sd
;
266 spin_lock(&configfs_dirent_lock
);
267 list_del_init(&sd
->s_sibling
);
268 spin_unlock(&configfs_dirent_lock
);
271 simple_rmdir(parent
->d_inode
,d
);
273 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
274 atomic_read(&d
->d_count
));
280 * configfs_remove_dir - remove an config_item's directory.
281 * @item: config_item we're removing.
283 * The only thing special about this is that we remove any files in
284 * the directory before we remove the directory, and we've inlined
285 * what used to be configfs_rmdir() below, instead of calling separately.
288 static void configfs_remove_dir(struct config_item
* item
)
290 struct dentry
* dentry
= dget(item
->ci_dentry
);
297 * Drop reference from dget() on entrance.
303 /* attaches attribute's configfs_dirent to the dentry corresponding to the
306 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
308 struct configfs_attribute
* attr
= sd
->s_element
;
311 dentry
->d_fsdata
= configfs_get(sd
);
312 sd
->s_dentry
= dentry
;
313 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
,
320 dentry
->d_op
= &configfs_dentry_ops
;
326 static struct dentry
* configfs_lookup(struct inode
*dir
,
327 struct dentry
*dentry
,
328 struct nameidata
*nd
)
330 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
331 struct configfs_dirent
* sd
;
335 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
336 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
337 const unsigned char * name
= configfs_get_name(sd
);
339 if (strcmp(name
, dentry
->d_name
.name
))
343 err
= configfs_attach_attr(sd
, dentry
);
350 * If it doesn't exist and it isn't a NOT_PINNED item,
351 * it must be negative.
353 return simple_lookup(dir
, dentry
, nd
);
360 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
361 * attributes and are removed by rmdir(). We recurse, setting
362 * CONFIGFS_USET_DROPPING on all children that are candidates for
364 * If there is an error, the caller will reset the flags via
365 * configfs_detach_rollback().
367 static int configfs_detach_prep(struct dentry
*dentry
, struct mutex
**wait_mutex
)
369 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
370 struct configfs_dirent
*sd
;
374 if (!list_empty(&parent_sd
->s_links
))
378 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
379 if (sd
->s_type
& CONFIGFS_NOT_PINNED
)
381 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
382 /* Abort if racing with mkdir() */
383 if (sd
->s_type
& CONFIGFS_USET_IN_MKDIR
) {
385 *wait_mutex
= &sd
->s_dentry
->d_inode
->i_mutex
;
388 /* Mark that we're trying to drop the group */
389 sd
->s_type
|= CONFIGFS_USET_DROPPING
;
392 * Yup, recursive. If there's a problem, blame
393 * deep nesting of default_groups
395 ret
= configfs_detach_prep(sd
->s_dentry
, wait_mutex
);
409 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
412 static void configfs_detach_rollback(struct dentry
*dentry
)
414 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
415 struct configfs_dirent
*sd
;
417 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
418 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
419 configfs_detach_rollback(sd
->s_dentry
);
420 sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
425 static void detach_attrs(struct config_item
* item
)
427 struct dentry
* dentry
= dget(item
->ci_dentry
);
428 struct configfs_dirent
* parent_sd
;
429 struct configfs_dirent
* sd
, * tmp
;
434 pr_debug("configfs %s: dropping attrs for dir\n",
435 dentry
->d_name
.name
);
437 parent_sd
= dentry
->d_fsdata
;
438 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
439 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
441 spin_lock(&configfs_dirent_lock
);
442 list_del_init(&sd
->s_sibling
);
443 spin_unlock(&configfs_dirent_lock
);
444 configfs_drop_dentry(sd
, dentry
);
449 * Drop reference from dget() on entrance.
454 static int populate_attrs(struct config_item
*item
)
456 struct config_item_type
*t
= item
->ci_type
;
457 struct configfs_attribute
*attr
;
464 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
465 if ((error
= configfs_create_file(item
, attr
)))
476 static int configfs_attach_group(struct config_item
*parent_item
,
477 struct config_item
*item
,
478 struct dentry
*dentry
);
479 static void configfs_detach_group(struct config_item
*item
);
481 static void detach_groups(struct config_group
*group
)
483 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
484 struct dentry
*child
;
485 struct configfs_dirent
*parent_sd
;
486 struct configfs_dirent
*sd
, *tmp
;
491 parent_sd
= dentry
->d_fsdata
;
492 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
493 if (!sd
->s_element
||
494 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
497 child
= sd
->s_dentry
;
499 mutex_lock(&child
->d_inode
->i_mutex
);
501 configfs_detach_group(sd
->s_element
);
502 child
->d_inode
->i_flags
|= S_DEAD
;
504 mutex_unlock(&child
->d_inode
->i_mutex
);
511 * Drop reference from dget() on entrance.
517 * This fakes mkdir(2) on a default_groups[] entry. It
518 * creates a dentry, attachs it, and then does fixup
521 * We could, perhaps, tweak our parent's ->mkdir for a minute and
522 * try using vfs_mkdir. Just a thought.
524 static int create_default_group(struct config_group
*parent_group
,
525 struct config_group
*group
)
529 struct configfs_dirent
*sd
;
530 /* We trust the caller holds a reference to parent */
531 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
533 if (!group
->cg_item
.ci_name
)
534 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
535 name
.name
= group
->cg_item
.ci_name
;
536 name
.len
= strlen(name
.name
);
537 name
.hash
= full_name_hash(name
.name
, name
.len
);
540 child
= d_alloc(parent
, &name
);
544 ret
= configfs_attach_group(&parent_group
->cg_item
,
545 &group
->cg_item
, child
);
547 sd
= child
->d_fsdata
;
548 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
558 static int populate_groups(struct config_group
*group
)
560 struct config_group
*new_group
;
561 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
565 if (group
->default_groups
) {
567 * FYI, we're faking mkdir here
568 * I'm not sure we need this semaphore, as we're called
569 * from our parent's mkdir. That holds our parent's
570 * i_mutex, so afaik lookup cannot continue through our
571 * parent to find us, let alone mess with our tree.
572 * That said, taking our i_mutex is closer to mkdir
573 * emulation, and shouldn't hurt.
575 mutex_lock_nested(&dentry
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
577 for (i
= 0; group
->default_groups
[i
]; i
++) {
578 new_group
= group
->default_groups
[i
];
580 ret
= create_default_group(group
, new_group
);
585 mutex_unlock(&dentry
->d_inode
->i_mutex
);
589 detach_groups(group
);
595 * All of link_obj/unlink_obj/link_group/unlink_group require that
596 * subsys->su_mutex is held.
599 static void unlink_obj(struct config_item
*item
)
601 struct config_group
*group
;
603 group
= item
->ci_group
;
605 list_del_init(&item
->ci_entry
);
607 item
->ci_group
= NULL
;
608 item
->ci_parent
= NULL
;
610 /* Drop the reference for ci_entry */
611 config_item_put(item
);
613 /* Drop the reference for ci_parent */
614 config_group_put(group
);
618 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
621 * Parent seems redundant with group, but it makes certain
622 * traversals much nicer.
624 item
->ci_parent
= parent_item
;
627 * We hold a reference on the parent for the child's ci_parent
630 item
->ci_group
= config_group_get(to_config_group(parent_item
));
631 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
634 * We hold a reference on the child for ci_entry on the parent's
637 config_item_get(item
);
640 static void unlink_group(struct config_group
*group
)
643 struct config_group
*new_group
;
645 if (group
->default_groups
) {
646 for (i
= 0; group
->default_groups
[i
]; i
++) {
647 new_group
= group
->default_groups
[i
];
648 unlink_group(new_group
);
652 group
->cg_subsys
= NULL
;
653 unlink_obj(&group
->cg_item
);
656 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
659 struct config_group
*new_group
;
660 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
662 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
664 if (parent_group
->cg_subsys
)
665 subsys
= parent_group
->cg_subsys
;
666 else if (configfs_is_root(&parent_group
->cg_item
))
667 subsys
= to_configfs_subsystem(group
);
670 group
->cg_subsys
= subsys
;
672 if (group
->default_groups
) {
673 for (i
= 0; group
->default_groups
[i
]; i
++) {
674 new_group
= group
->default_groups
[i
];
675 link_group(group
, new_group
);
681 * The goal is that configfs_attach_item() (and
682 * configfs_attach_group()) can be called from either the VFS or this
683 * module. That is, they assume that the items have been created,
684 * the dentry allocated, and the dcache is all ready to go.
686 * If they fail, they must clean up after themselves as if they
687 * had never been called. The caller (VFS or local function) will
688 * handle cleaning up the dcache bits.
690 * configfs_detach_group() and configfs_detach_item() behave similarly on
691 * the way out. They assume that the proper semaphores are held, they
692 * clean up the configfs items, and they expect their callers will
693 * handle the dcache bits.
695 static int configfs_attach_item(struct config_item
*parent_item
,
696 struct config_item
*item
,
697 struct dentry
*dentry
)
701 ret
= configfs_create_dir(item
, dentry
);
703 ret
= populate_attrs(item
);
705 configfs_remove_dir(item
);
713 static void configfs_detach_item(struct config_item
*item
)
716 configfs_remove_dir(item
);
719 static int configfs_attach_group(struct config_item
*parent_item
,
720 struct config_item
*item
,
721 struct dentry
*dentry
)
724 struct configfs_dirent
*sd
;
726 ret
= configfs_attach_item(parent_item
, item
, dentry
);
728 sd
= dentry
->d_fsdata
;
729 sd
->s_type
|= CONFIGFS_USET_DIR
;
731 ret
= populate_groups(to_config_group(item
));
733 configfs_detach_item(item
);
741 static void configfs_detach_group(struct config_item
*item
)
743 detach_groups(to_config_group(item
));
744 configfs_detach_item(item
);
748 * After the item has been detached from the filesystem view, we are
749 * ready to tear it out of the hierarchy. Notify the client before
750 * we do that so they can perform any cleanup that requires
751 * navigating the hierarchy. A client does not need to provide this
752 * callback. The subsystem semaphore MUST be held by the caller, and
753 * references must be valid for both items. It also assumes the
754 * caller has validated ci_type.
756 static void client_disconnect_notify(struct config_item
*parent_item
,
757 struct config_item
*item
)
759 struct config_item_type
*type
;
761 type
= parent_item
->ci_type
;
764 if (type
->ct_group_ops
&& type
->ct_group_ops
->disconnect_notify
)
765 type
->ct_group_ops
->disconnect_notify(to_config_group(parent_item
),
770 * Drop the initial reference from make_item()/make_group()
771 * This function assumes that reference is held on item
772 * and that item holds a valid reference to the parent. Also, it
773 * assumes the caller has validated ci_type.
775 static void client_drop_item(struct config_item
*parent_item
,
776 struct config_item
*item
)
778 struct config_item_type
*type
;
780 type
= parent_item
->ci_type
;
784 * If ->drop_item() exists, it is responsible for the
787 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
788 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
791 config_item_put(item
);
795 static void configfs_dump_one(struct configfs_dirent
*sd
, int level
)
797 printk(KERN_INFO
"%*s\"%s\":\n", level
, " ", configfs_get_name(sd
));
799 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
800 type_print(CONFIGFS_ROOT
);
801 type_print(CONFIGFS_DIR
);
802 type_print(CONFIGFS_ITEM_ATTR
);
803 type_print(CONFIGFS_ITEM_LINK
);
804 type_print(CONFIGFS_USET_DIR
);
805 type_print(CONFIGFS_USET_DEFAULT
);
806 type_print(CONFIGFS_USET_DROPPING
);
810 static int configfs_dump(struct configfs_dirent
*sd
, int level
)
812 struct configfs_dirent
*child_sd
;
815 configfs_dump_one(sd
, level
);
817 if (!(sd
->s_type
& (CONFIGFS_DIR
|CONFIGFS_ROOT
)))
820 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
821 ret
= configfs_dump(child_sd
, level
+ 2);
832 * configfs_depend_item() and configfs_undepend_item()
834 * WARNING: Do not call these from a configfs callback!
836 * This describes these functions and their helpers.
838 * Allow another kernel system to depend on a config_item. If this
839 * happens, the item cannot go away until the dependant can live without
840 * it. The idea is to give client modules as simple an interface as
841 * possible. When a system asks them to depend on an item, they just
842 * call configfs_depend_item(). If the item is live and the client
843 * driver is in good shape, we'll happily do the work for them.
845 * Why is the locking complex? Because configfs uses the VFS to handle
846 * all locking, but this function is called outside the normal
847 * VFS->configfs path. So it must take VFS locks to prevent the
848 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
849 * why you can't call these functions underneath configfs callbacks.
851 * Note, btw, that this can be called at *any* time, even when a configfs
852 * subsystem isn't registered, or when configfs is loading or unloading.
853 * Just like configfs_register_subsystem(). So we take the same
854 * precautions. We pin the filesystem. We lock each i_mutex _in_order_
855 * on our way down the tree. If we can find the target item in the
856 * configfs tree, it must be part of the subsystem tree as well, so we
857 * do not need the subsystem semaphore. Holding the i_mutex chain locks
858 * out mkdir() and rmdir(), who might be racing us.
862 * configfs_depend_prep()
864 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
865 * attributes. This is similar but not the same to configfs_detach_prep().
866 * Note that configfs_detach_prep() expects the parent to be locked when it
867 * is called, but we lock the parent *inside* configfs_depend_prep(). We
868 * do that so we can unlock it if we find nothing.
870 * Here we do a depth-first search of the dentry hierarchy looking for
871 * our object. We take i_mutex on each step of the way down. IT IS
872 * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
873 * we'll drop the i_mutex.
875 * If the target is not found, -ENOENT is bubbled up and we have released
876 * all locks. If the target was found, the locks will be cleared by
877 * configfs_depend_rollback().
879 * This adds a requirement that all config_items be unique!
881 * This is recursive because the locking traversal is tricky. There isn't
882 * much on the stack, though, so folks that need this function - be careful
883 * about your stack! Patches will be accepted to make it iterative.
885 static int configfs_depend_prep(struct dentry
*origin
,
886 struct config_item
*target
)
888 struct configfs_dirent
*child_sd
, *sd
= origin
->d_fsdata
;
891 BUG_ON(!origin
|| !sd
);
893 /* Lock this guy on the way down */
894 mutex_lock(&sd
->s_dentry
->d_inode
->i_mutex
);
895 if (sd
->s_element
== target
) /* Boo-yah */
898 list_for_each_entry(child_sd
, &sd
->s_children
, s_sibling
) {
899 if (child_sd
->s_type
& CONFIGFS_DIR
) {
900 ret
= configfs_depend_prep(child_sd
->s_dentry
,
903 goto out
; /* Child path boo-yah */
907 /* We looped all our children and didn't find target */
908 mutex_unlock(&sd
->s_dentry
->d_inode
->i_mutex
);
916 * This is ONLY called if configfs_depend_prep() did its job. So we can
917 * trust the entire path from item back up to origin.
919 * We walk backwards from item, unlocking each i_mutex. We finish by
922 static void configfs_depend_rollback(struct dentry
*origin
,
923 struct config_item
*item
)
925 struct dentry
*dentry
= item
->ci_dentry
;
927 while (dentry
!= origin
) {
928 mutex_unlock(&dentry
->d_inode
->i_mutex
);
929 dentry
= dentry
->d_parent
;
932 mutex_unlock(&origin
->d_inode
->i_mutex
);
935 int configfs_depend_item(struct configfs_subsystem
*subsys
,
936 struct config_item
*target
)
939 struct configfs_dirent
*p
, *root_sd
, *subsys_sd
= NULL
;
940 struct config_item
*s_item
= &subsys
->su_group
.cg_item
;
943 * Pin the configfs filesystem. This means we can safely access
944 * the root of the configfs filesystem.
946 ret
= configfs_pin_fs();
951 * Next, lock the root directory. We're going to check that the
952 * subsystem is really registered, and so we need to lock out
953 * configfs_[un]register_subsystem().
955 mutex_lock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
957 root_sd
= configfs_sb
->s_root
->d_fsdata
;
959 list_for_each_entry(p
, &root_sd
->s_children
, s_sibling
) {
960 if (p
->s_type
& CONFIGFS_DIR
) {
961 if (p
->s_element
== s_item
) {
973 /* Ok, now we can trust subsys/s_item */
975 /* Scan the tree, locking i_mutex recursively, return 0 if found */
976 ret
= configfs_depend_prep(subsys_sd
->s_dentry
, target
);
980 /* We hold all i_mutexes from the subsystem down to the target */
981 p
= target
->ci_dentry
->d_fsdata
;
982 p
->s_dependent_count
+= 1;
984 configfs_depend_rollback(subsys_sd
->s_dentry
, target
);
987 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
990 * If we succeeded, the fs is pinned via other methods. If not,
991 * we're done with it anyway. So release_fs() is always right.
993 configfs_release_fs();
997 EXPORT_SYMBOL(configfs_depend_item
);
1000 * Release the dependent linkage. This is much simpler than
1001 * configfs_depend_item() because we know that that the client driver is
1002 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1004 void configfs_undepend_item(struct configfs_subsystem
*subsys
,
1005 struct config_item
*target
)
1007 struct configfs_dirent
*sd
;
1010 * Since we can trust everything is pinned, we just need i_mutex
1013 mutex_lock(&target
->ci_dentry
->d_inode
->i_mutex
);
1015 sd
= target
->ci_dentry
->d_fsdata
;
1016 BUG_ON(sd
->s_dependent_count
< 1);
1018 sd
->s_dependent_count
-= 1;
1021 * After this unlock, we cannot trust the item to stay alive!
1022 * DO NOT REFERENCE item after this unlock.
1024 mutex_unlock(&target
->ci_dentry
->d_inode
->i_mutex
);
1026 EXPORT_SYMBOL(configfs_undepend_item
);
1028 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1032 struct config_group
*group
= NULL
;
1033 struct config_item
*item
= NULL
;
1034 struct config_item
*parent_item
;
1035 struct configfs_subsystem
*subsys
;
1036 struct configfs_dirent
*sd
;
1037 struct config_item_type
*type
;
1038 struct module
*owner
= NULL
;
1041 if (dentry
->d_parent
== configfs_sb
->s_root
) {
1046 sd
= dentry
->d_parent
->d_fsdata
;
1047 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
1052 /* Get a working ref for the duration of this function */
1053 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1054 type
= parent_item
->ci_type
;
1055 subsys
= to_config_group(parent_item
)->cg_subsys
;
1058 if (!type
|| !type
->ct_group_ops
||
1059 (!type
->ct_group_ops
->make_group
&&
1060 !type
->ct_group_ops
->make_item
)) {
1061 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
1065 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
1071 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
1073 mutex_lock(&subsys
->su_mutex
);
1074 if (type
->ct_group_ops
->make_group
) {
1075 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
1077 group
= ERR_PTR(-ENOMEM
);
1078 if (!IS_ERR(group
)) {
1079 link_group(to_config_group(parent_item
), group
);
1080 item
= &group
->cg_item
;
1082 ret
= PTR_ERR(group
);
1084 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
1086 item
= ERR_PTR(-ENOMEM
);
1088 link_obj(parent_item
, item
);
1090 ret
= PTR_ERR(item
);
1092 mutex_unlock(&subsys
->su_mutex
);
1097 * If item == NULL, then link_obj() was never called.
1098 * There are no extra references to clean up.
1104 * link_obj() has been called (via link_group() for groups).
1105 * From here on out, errors must clean that up.
1108 type
= item
->ci_type
;
1114 owner
= type
->ct_owner
;
1115 if (!try_module_get(owner
)) {
1121 * I hate doing it this way, but if there is
1122 * an error, module_put() probably should
1123 * happen after any cleanup.
1128 * Make racing rmdir() fail if it did not tag parent with
1129 * CONFIGFS_USET_DROPPING
1130 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1131 * fail and let rmdir() terminate correctly
1133 spin_lock(&configfs_dirent_lock
);
1134 /* This will make configfs_detach_prep() fail */
1135 sd
->s_type
|= CONFIGFS_USET_IN_MKDIR
;
1136 spin_unlock(&configfs_dirent_lock
);
1139 ret
= configfs_attach_group(parent_item
, item
, dentry
);
1141 ret
= configfs_attach_item(parent_item
, item
, dentry
);
1143 spin_lock(&configfs_dirent_lock
);
1144 sd
->s_type
&= ~CONFIGFS_USET_IN_MKDIR
;
1145 spin_unlock(&configfs_dirent_lock
);
1149 /* Tear down everything we built up */
1150 mutex_lock(&subsys
->su_mutex
);
1152 client_disconnect_notify(parent_item
, item
);
1154 unlink_group(group
);
1157 client_drop_item(parent_item
, item
);
1159 mutex_unlock(&subsys
->su_mutex
);
1167 * link_obj()/link_group() took a reference from child->parent,
1168 * so the parent is safely pinned. We can drop our working
1171 config_item_put(parent_item
);
1177 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1179 struct config_item
*parent_item
;
1180 struct config_item
*item
;
1181 struct configfs_subsystem
*subsys
;
1182 struct configfs_dirent
*sd
;
1183 struct module
*owner
= NULL
;
1186 if (dentry
->d_parent
== configfs_sb
->s_root
)
1189 sd
= dentry
->d_fsdata
;
1190 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
1194 * Here's where we check for dependents. We're protected by
1197 if (sd
->s_dependent_count
)
1200 /* Get a working ref until we have the child */
1201 parent_item
= configfs_get_config_item(dentry
->d_parent
);
1202 subsys
= to_config_group(parent_item
)->cg_subsys
;
1205 if (!parent_item
->ci_type
) {
1206 config_item_put(parent_item
);
1210 spin_lock(&configfs_dirent_lock
);
1212 struct mutex
*wait_mutex
;
1214 ret
= configfs_detach_prep(dentry
, &wait_mutex
);
1216 configfs_detach_rollback(dentry
);
1217 spin_unlock(&configfs_dirent_lock
);
1218 if (ret
!= -EAGAIN
) {
1219 config_item_put(parent_item
);
1223 /* Wait until the racing operation terminates */
1224 mutex_lock(wait_mutex
);
1225 mutex_unlock(wait_mutex
);
1227 spin_lock(&configfs_dirent_lock
);
1229 } while (ret
== -EAGAIN
);
1230 spin_unlock(&configfs_dirent_lock
);
1232 /* Get a working ref for the duration of this function */
1233 item
= configfs_get_config_item(dentry
);
1235 /* Drop reference from above, item already holds one. */
1236 config_item_put(parent_item
);
1239 owner
= item
->ci_type
->ct_owner
;
1241 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
1242 configfs_detach_group(item
);
1244 mutex_lock(&subsys
->su_mutex
);
1245 client_disconnect_notify(parent_item
, item
);
1246 unlink_group(to_config_group(item
));
1248 configfs_detach_item(item
);
1250 mutex_lock(&subsys
->su_mutex
);
1251 client_disconnect_notify(parent_item
, item
);
1255 client_drop_item(parent_item
, item
);
1256 mutex_unlock(&subsys
->su_mutex
);
1258 /* Drop our reference from above */
1259 config_item_put(item
);
1266 const struct inode_operations configfs_dir_inode_operations
= {
1267 .mkdir
= configfs_mkdir
,
1268 .rmdir
= configfs_rmdir
,
1269 .symlink
= configfs_symlink
,
1270 .unlink
= configfs_unlink
,
1271 .lookup
= configfs_lookup
,
1272 .setattr
= configfs_setattr
,
1276 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
1279 struct dentry
* new_dentry
, * parent
;
1281 if (!strcmp(config_item_name(item
), new_name
))
1287 down_write(&configfs_rename_sem
);
1288 parent
= item
->parent
->dentry
;
1290 mutex_lock(&parent
->d_inode
->i_mutex
);
1292 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
1293 if (!IS_ERR(new_dentry
)) {
1294 if (!new_dentry
->d_inode
) {
1295 error
= config_item_set_name(item
, "%s", new_name
);
1297 d_add(new_dentry
, NULL
);
1298 d_move(item
->dentry
, new_dentry
);
1301 d_delete(new_dentry
);
1306 mutex_unlock(&parent
->d_inode
->i_mutex
);
1307 up_write(&configfs_rename_sem
);
1313 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
1315 struct dentry
* dentry
= file
->f_path
.dentry
;
1316 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1318 mutex_lock(&dentry
->d_inode
->i_mutex
);
1319 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
);
1320 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1322 return IS_ERR(file
->private_data
) ? PTR_ERR(file
->private_data
) : 0;
1326 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
1328 struct dentry
* dentry
= file
->f_path
.dentry
;
1329 struct configfs_dirent
* cursor
= file
->private_data
;
1331 mutex_lock(&dentry
->d_inode
->i_mutex
);
1332 spin_lock(&configfs_dirent_lock
);
1333 list_del_init(&cursor
->s_sibling
);
1334 spin_unlock(&configfs_dirent_lock
);
1335 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1337 release_configfs_dirent(cursor
);
1342 /* Relationship between s_mode and the DT_xxx types */
1343 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1345 return (sd
->s_mode
>> 12) & 15;
1348 static int configfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1350 struct dentry
*dentry
= filp
->f_path
.dentry
;
1351 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1352 struct configfs_dirent
*cursor
= filp
->private_data
;
1353 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1355 int i
= filp
->f_pos
;
1359 ino
= dentry
->d_inode
->i_ino
;
1360 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1366 ino
= parent_ino(dentry
);
1367 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1373 if (filp
->f_pos
== 2) {
1374 spin_lock(&configfs_dirent_lock
);
1375 list_move(q
, &parent_sd
->s_children
);
1376 spin_unlock(&configfs_dirent_lock
);
1378 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
1379 struct configfs_dirent
*next
;
1383 next
= list_entry(p
, struct configfs_dirent
,
1385 if (!next
->s_element
)
1388 name
= configfs_get_name(next
);
1391 ino
= next
->s_dentry
->d_inode
->i_ino
;
1393 ino
= iunique(configfs_sb
, 2);
1395 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
1399 spin_lock(&configfs_dirent_lock
);
1401 spin_unlock(&configfs_dirent_lock
);
1409 static loff_t
configfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
1411 struct dentry
* dentry
= file
->f_path
.dentry
;
1413 mutex_lock(&dentry
->d_inode
->i_mutex
);
1416 offset
+= file
->f_pos
;
1421 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
1424 if (offset
!= file
->f_pos
) {
1425 file
->f_pos
= offset
;
1426 if (file
->f_pos
>= 2) {
1427 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1428 struct configfs_dirent
*cursor
= file
->private_data
;
1429 struct list_head
*p
;
1430 loff_t n
= file
->f_pos
- 2;
1432 spin_lock(&configfs_dirent_lock
);
1433 list_del(&cursor
->s_sibling
);
1434 p
= sd
->s_children
.next
;
1435 while (n
&& p
!= &sd
->s_children
) {
1436 struct configfs_dirent
*next
;
1437 next
= list_entry(p
, struct configfs_dirent
,
1439 if (next
->s_element
)
1443 list_add_tail(&cursor
->s_sibling
, p
);
1444 spin_unlock(&configfs_dirent_lock
);
1447 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1451 const struct file_operations configfs_dir_operations
= {
1452 .open
= configfs_dir_open
,
1453 .release
= configfs_dir_close
,
1454 .llseek
= configfs_dir_lseek
,
1455 .read
= generic_read_dir
,
1456 .readdir
= configfs_readdir
,
1459 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1462 struct config_group
*group
= &subsys
->su_group
;
1464 struct dentry
*dentry
;
1465 struct configfs_dirent
*sd
;
1467 err
= configfs_pin_fs();
1471 if (!group
->cg_item
.ci_name
)
1472 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1474 sd
= configfs_sb
->s_root
->d_fsdata
;
1475 link_group(to_config_group(sd
->s_element
), group
);
1477 mutex_lock_nested(&configfs_sb
->s_root
->d_inode
->i_mutex
,
1480 name
.name
= group
->cg_item
.ci_name
;
1481 name
.len
= strlen(name
.name
);
1482 name
.hash
= full_name_hash(name
.name
, name
.len
);
1485 dentry
= d_alloc(configfs_sb
->s_root
, &name
);
1487 d_add(dentry
, NULL
);
1489 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1497 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1500 unlink_group(group
);
1501 configfs_release_fs();
1507 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1509 struct config_group
*group
= &subsys
->su_group
;
1510 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1512 if (dentry
->d_parent
!= configfs_sb
->s_root
) {
1513 printk(KERN_ERR
"configfs: Tried to unregister non-subsystem!\n");
1517 mutex_lock_nested(&configfs_sb
->s_root
->d_inode
->i_mutex
,
1519 mutex_lock_nested(&dentry
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1520 spin_lock(&configfs_dirent_lock
);
1521 if (configfs_detach_prep(dentry
, NULL
)) {
1522 printk(KERN_ERR
"configfs: Tried to unregister non-empty subsystem!\n");
1524 spin_unlock(&configfs_dirent_lock
);
1525 configfs_detach_group(&group
->cg_item
);
1526 dentry
->d_inode
->i_flags
|= S_DEAD
;
1527 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1531 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1535 unlink_group(group
);
1536 configfs_release_fs();
1539 EXPORT_SYMBOL(configfs_register_subsystem
);
1540 EXPORT_SYMBOL(configfs_unregister_subsystem
);