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 DEFINE_SPINLOCK(configfs_dirent_lock);
49 static void configfs_d_iput(struct dentry * dentry,
52 struct configfs_dirent * sd = dentry->d_fsdata;
55 BUG_ON(sd->s_dentry != dentry);
63 * We _must_ delete our dentries on last dput, as the chain-to-parent
64 * behavior is required to clear the parents of default_groups.
66 static int configfs_d_delete(struct dentry *dentry)
71 static struct dentry_operations configfs_dentry_ops = {
72 .d_iput = configfs_d_iput,
73 /* simple_delete_dentry() isn't exported */
74 .d_delete = configfs_d_delete,
78 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
80 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
83 struct configfs_dirent * sd;
85 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
87 return ERR_PTR(-ENOMEM);
89 atomic_set(&sd->s_count, 1);
90 INIT_LIST_HEAD(&sd->s_links);
91 INIT_LIST_HEAD(&sd->s_children);
92 sd->s_element = element;
93 spin_lock(&configfs_dirent_lock);
94 list_add(&sd->s_sibling, &parent_sd->s_children);
95 spin_unlock(&configfs_dirent_lock);
102 * Return -EEXIST if there is already a configfs element with the same
103 * name for the same parent.
105 * called with parent inode's i_mutex held
107 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
108 const unsigned char *new)
110 struct configfs_dirent * sd;
112 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
114 const unsigned char *existing = configfs_get_name(sd);
115 if (strcmp(existing, new))
126 int configfs_make_dirent(struct configfs_dirent * parent_sd,
127 struct dentry * dentry, void * element,
128 umode_t mode, int type)
130 struct configfs_dirent * sd;
132 sd = configfs_new_dirent(parent_sd, element);
138 sd->s_dentry = dentry;
140 dentry->d_fsdata = configfs_get(sd);
141 dentry->d_op = &configfs_dentry_ops;
147 static int init_dir(struct inode * inode)
149 inode->i_op = &configfs_dir_inode_operations;
150 inode->i_fop = &configfs_dir_operations;
152 /* directory inodes start off with i_nlink == 2 (for "." entry) */
157 static int configfs_init_file(struct inode * inode)
159 inode->i_size = PAGE_SIZE;
160 inode->i_fop = &configfs_file_operations;
164 static int init_symlink(struct inode * inode)
166 inode->i_op = &configfs_symlink_inode_operations;
170 static int create_dir(struct config_item * k, struct dentry * p,
174 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
176 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
178 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
181 error = configfs_create(d, mode, init_dir);
183 inc_nlink(p->d_inode);
184 (d)->d_op = &configfs_dentry_ops;
186 struct configfs_dirent *sd = d->d_fsdata;
188 spin_lock(&configfs_dirent_lock);
189 list_del_init(&sd->s_sibling);
190 spin_unlock(&configfs_dirent_lock);
200 * configfs_create_dir - create a directory for an config_item.
201 * @item: config_itemwe're creating directory for.
202 * @dentry: config_item's dentry.
205 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
207 struct dentry * parent;
213 parent = item->ci_parent->ci_dentry;
214 else if (configfs_mount && configfs_mount->mnt_sb)
215 parent = configfs_mount->mnt_sb->s_root;
219 error = create_dir(item,parent,dentry);
221 item->ci_dentry = dentry;
225 int configfs_create_link(struct configfs_symlink *sl,
226 struct dentry *parent,
227 struct dentry *dentry)
230 umode_t mode = S_IFLNK | S_IRWXUGO;
232 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
235 err = configfs_create(dentry, mode, init_symlink);
237 dentry->d_op = &configfs_dentry_ops;
239 struct configfs_dirent *sd = dentry->d_fsdata;
241 spin_lock(&configfs_dirent_lock);
242 list_del_init(&sd->s_sibling);
243 spin_unlock(&configfs_dirent_lock);
251 static void remove_dir(struct dentry * d)
253 struct dentry * parent = dget(d->d_parent);
254 struct configfs_dirent * sd;
257 spin_lock(&configfs_dirent_lock);
258 list_del_init(&sd->s_sibling);
259 spin_unlock(&configfs_dirent_lock);
262 simple_rmdir(parent->d_inode,d);
264 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
265 atomic_read(&d->d_count));
271 * configfs_remove_dir - remove an config_item's directory.
272 * @item: config_item we're removing.
274 * The only thing special about this is that we remove any files in
275 * the directory before we remove the directory, and we've inlined
276 * what used to be configfs_rmdir() below, instead of calling separately.
279 static void configfs_remove_dir(struct config_item * item)
281 struct dentry * dentry = dget(item->ci_dentry);
288 * Drop reference from dget() on entrance.
294 /* attaches attribute's configfs_dirent to the dentry corresponding to the
297 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
299 struct configfs_attribute * attr = sd->s_element;
302 dentry->d_fsdata = configfs_get(sd);
303 sd->s_dentry = dentry;
304 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
311 dentry->d_op = &configfs_dentry_ops;
317 static struct dentry * configfs_lookup(struct inode *dir,
318 struct dentry *dentry,
319 struct nameidata *nd)
321 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
322 struct configfs_dirent * sd;
326 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
327 if (sd->s_type & CONFIGFS_NOT_PINNED) {
328 const unsigned char * name = configfs_get_name(sd);
330 if (strcmp(name, dentry->d_name.name))
334 err = configfs_attach_attr(sd, dentry);
341 * If it doesn't exist and it isn't a NOT_PINNED item,
342 * it must be negative.
344 return simple_lookup(dir, dentry, nd);
351 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
352 * attributes and are removed by rmdir(). We recurse, taking i_mutex
353 * on all children that are candidates for default detach. If the
354 * result is clean, then configfs_detach_group() will handle dropping
355 * i_mutex. If there is an error, the caller will clean up the i_mutex
356 * holders via configfs_detach_rollback().
358 static int configfs_detach_prep(struct dentry *dentry)
360 struct configfs_dirent *parent_sd = dentry->d_fsdata;
361 struct configfs_dirent *sd;
365 if (!list_empty(&parent_sd->s_links))
369 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
370 if (sd->s_type & CONFIGFS_NOT_PINNED)
372 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
373 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
374 /* Mark that we've taken i_mutex */
375 sd->s_type |= CONFIGFS_USET_DROPPING;
378 * Yup, recursive. If there's a problem, blame
379 * deep nesting of default_groups
381 ret = configfs_detach_prep(sd->s_dentry);
395 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
398 static void configfs_detach_rollback(struct dentry *dentry)
400 struct configfs_dirent *parent_sd = dentry->d_fsdata;
401 struct configfs_dirent *sd;
403 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
404 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
405 configfs_detach_rollback(sd->s_dentry);
407 if (sd->s_type & CONFIGFS_USET_DROPPING) {
408 sd->s_type &= ~CONFIGFS_USET_DROPPING;
409 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
415 static void detach_attrs(struct config_item * item)
417 struct dentry * dentry = dget(item->ci_dentry);
418 struct configfs_dirent * parent_sd;
419 struct configfs_dirent * sd, * tmp;
424 pr_debug("configfs %s: dropping attrs for dir\n",
425 dentry->d_name.name);
427 parent_sd = dentry->d_fsdata;
428 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
429 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
431 spin_lock(&configfs_dirent_lock);
432 list_del_init(&sd->s_sibling);
433 spin_unlock(&configfs_dirent_lock);
434 configfs_drop_dentry(sd, dentry);
439 * Drop reference from dget() on entrance.
444 static int populate_attrs(struct config_item *item)
446 struct config_item_type *t = item->ci_type;
447 struct configfs_attribute *attr;
454 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
455 if ((error = configfs_create_file(item, attr)))
466 static int configfs_attach_group(struct config_item *parent_item,
467 struct config_item *item,
468 struct dentry *dentry);
469 static void configfs_detach_group(struct config_item *item);
471 static void detach_groups(struct config_group *group)
473 struct dentry * dentry = dget(group->cg_item.ci_dentry);
474 struct dentry *child;
475 struct configfs_dirent *parent_sd;
476 struct configfs_dirent *sd, *tmp;
481 parent_sd = dentry->d_fsdata;
482 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
483 if (!sd->s_element ||
484 !(sd->s_type & CONFIGFS_USET_DEFAULT))
487 child = sd->s_dentry;
489 configfs_detach_group(sd->s_element);
490 child->d_inode->i_flags |= S_DEAD;
493 * From rmdir/unregister, a configfs_detach_prep() pass
494 * has taken our i_mutex for us. Drop it.
495 * From mkdir/register cleanup, there is no sem held.
497 if (sd->s_type & CONFIGFS_USET_DROPPING)
498 mutex_unlock(&child->d_inode->i_mutex);
505 * Drop reference from dget() on entrance.
511 * This fakes mkdir(2) on a default_groups[] entry. It
512 * creates a dentry, attachs it, and then does fixup
515 * We could, perhaps, tweak our parent's ->mkdir for a minute and
516 * try using vfs_mkdir. Just a thought.
518 static int create_default_group(struct config_group *parent_group,
519 struct config_group *group)
523 struct configfs_dirent *sd;
524 /* We trust the caller holds a reference to parent */
525 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
527 if (!group->cg_item.ci_name)
528 group->cg_item.ci_name = group->cg_item.ci_namebuf;
529 name.name = group->cg_item.ci_name;
530 name.len = strlen(name.name);
531 name.hash = full_name_hash(name.name, name.len);
534 child = d_alloc(parent, &name);
538 ret = configfs_attach_group(&parent_group->cg_item,
539 &group->cg_item, child);
541 sd = child->d_fsdata;
542 sd->s_type |= CONFIGFS_USET_DEFAULT;
552 static int populate_groups(struct config_group *group)
554 struct config_group *new_group;
555 struct dentry *dentry = group->cg_item.ci_dentry;
559 if (group->default_groups) {
561 * FYI, we're faking mkdir here
562 * I'm not sure we need this semaphore, as we're called
563 * from our parent's mkdir. That holds our parent's
564 * i_mutex, so afaik lookup cannot continue through our
565 * parent to find us, let alone mess with our tree.
566 * That said, taking our i_mutex is closer to mkdir
567 * emulation, and shouldn't hurt.
569 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
571 for (i = 0; group->default_groups[i]; i++) {
572 new_group = group->default_groups[i];
574 ret = create_default_group(group, new_group);
579 mutex_unlock(&dentry->d_inode->i_mutex);
583 detach_groups(group);
589 * All of link_obj/unlink_obj/link_group/unlink_group require that
590 * subsys->su_mutex is held.
593 static void unlink_obj(struct config_item *item)
595 struct config_group *group;
597 group = item->ci_group;
599 list_del_init(&item->ci_entry);
601 item->ci_group = NULL;
602 item->ci_parent = NULL;
604 /* Drop the reference for ci_entry */
605 config_item_put(item);
607 /* Drop the reference for ci_parent */
608 config_group_put(group);
612 static void link_obj(struct config_item *parent_item, struct config_item *item)
615 * Parent seems redundant with group, but it makes certain
616 * traversals much nicer.
618 item->ci_parent = parent_item;
621 * We hold a reference on the parent for the child's ci_parent
624 item->ci_group = config_group_get(to_config_group(parent_item));
625 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
628 * We hold a reference on the child for ci_entry on the parent's
631 config_item_get(item);
634 static void unlink_group(struct config_group *group)
637 struct config_group *new_group;
639 if (group->default_groups) {
640 for (i = 0; group->default_groups[i]; i++) {
641 new_group = group->default_groups[i];
642 unlink_group(new_group);
646 group->cg_subsys = NULL;
647 unlink_obj(&group->cg_item);
650 static void link_group(struct config_group *parent_group, struct config_group *group)
653 struct config_group *new_group;
654 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
656 link_obj(&parent_group->cg_item, &group->cg_item);
658 if (parent_group->cg_subsys)
659 subsys = parent_group->cg_subsys;
660 else if (configfs_is_root(&parent_group->cg_item))
661 subsys = to_configfs_subsystem(group);
664 group->cg_subsys = subsys;
666 if (group->default_groups) {
667 for (i = 0; group->default_groups[i]; i++) {
668 new_group = group->default_groups[i];
669 link_group(group, new_group);
675 * The goal is that configfs_attach_item() (and
676 * configfs_attach_group()) can be called from either the VFS or this
677 * module. That is, they assume that the items have been created,
678 * the dentry allocated, and the dcache is all ready to go.
680 * If they fail, they must clean up after themselves as if they
681 * had never been called. The caller (VFS or local function) will
682 * handle cleaning up the dcache bits.
684 * configfs_detach_group() and configfs_detach_item() behave similarly on
685 * the way out. They assume that the proper semaphores are held, they
686 * clean up the configfs items, and they expect their callers will
687 * handle the dcache bits.
689 static int configfs_attach_item(struct config_item *parent_item,
690 struct config_item *item,
691 struct dentry *dentry)
695 ret = configfs_create_dir(item, dentry);
697 ret = populate_attrs(item);
699 configfs_remove_dir(item);
707 static void configfs_detach_item(struct config_item *item)
710 configfs_remove_dir(item);
713 static int configfs_attach_group(struct config_item *parent_item,
714 struct config_item *item,
715 struct dentry *dentry)
718 struct configfs_dirent *sd;
720 ret = configfs_attach_item(parent_item, item, dentry);
722 sd = dentry->d_fsdata;
723 sd->s_type |= CONFIGFS_USET_DIR;
725 ret = populate_groups(to_config_group(item));
727 configfs_detach_item(item);
735 static void configfs_detach_group(struct config_item *item)
737 detach_groups(to_config_group(item));
738 configfs_detach_item(item);
742 * After the item has been detached from the filesystem view, we are
743 * ready to tear it out of the hierarchy. Notify the client before
744 * we do that so they can perform any cleanup that requires
745 * navigating the hierarchy. A client does not need to provide this
746 * callback. The subsystem semaphore MUST be held by the caller, and
747 * references must be valid for both items. It also assumes the
748 * caller has validated ci_type.
750 static void client_disconnect_notify(struct config_item *parent_item,
751 struct config_item *item)
753 struct config_item_type *type;
755 type = parent_item->ci_type;
758 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
759 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
764 * Drop the initial reference from make_item()/make_group()
765 * This function assumes that reference is held on item
766 * and that item holds a valid reference to the parent. Also, it
767 * assumes the caller has validated ci_type.
769 static void client_drop_item(struct config_item *parent_item,
770 struct config_item *item)
772 struct config_item_type *type;
774 type = parent_item->ci_type;
778 * If ->drop_item() exists, it is responsible for the
781 if (type->ct_group_ops && type->ct_group_ops->drop_item)
782 type->ct_group_ops->drop_item(to_config_group(parent_item),
785 config_item_put(item);
789 static void configfs_dump_one(struct configfs_dirent *sd, int level)
791 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
793 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
794 type_print(CONFIGFS_ROOT);
795 type_print(CONFIGFS_DIR);
796 type_print(CONFIGFS_ITEM_ATTR);
797 type_print(CONFIGFS_ITEM_LINK);
798 type_print(CONFIGFS_USET_DIR);
799 type_print(CONFIGFS_USET_DEFAULT);
800 type_print(CONFIGFS_USET_DROPPING);
804 static int configfs_dump(struct configfs_dirent *sd, int level)
806 struct configfs_dirent *child_sd;
809 configfs_dump_one(sd, level);
811 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
814 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
815 ret = configfs_dump(child_sd, level + 2);
826 * configfs_depend_item() and configfs_undepend_item()
828 * WARNING: Do not call these from a configfs callback!
830 * This describes these functions and their helpers.
832 * Allow another kernel system to depend on a config_item. If this
833 * happens, the item cannot go away until the dependant can live without
834 * it. The idea is to give client modules as simple an interface as
835 * possible. When a system asks them to depend on an item, they just
836 * call configfs_depend_item(). If the item is live and the client
837 * driver is in good shape, we'll happily do the work for them.
839 * Why is the locking complex? Because configfs uses the VFS to handle
840 * all locking, but this function is called outside the normal
841 * VFS->configfs path. So it must take VFS locks to prevent the
842 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
843 * why you can't call these functions underneath configfs callbacks.
845 * Note, btw, that this can be called at *any* time, even when a configfs
846 * subsystem isn't registered, or when configfs is loading or unloading.
847 * Just like configfs_register_subsystem(). So we take the same
848 * precautions. We pin the filesystem. We lock each i_mutex _in_order_
849 * on our way down the tree. If we can find the target item in the
850 * configfs tree, it must be part of the subsystem tree as well, so we
851 * do not need the subsystem semaphore. Holding the i_mutex chain locks
852 * out mkdir() and rmdir(), who might be racing us.
856 * configfs_depend_prep()
858 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
859 * attributes. This is similar but not the same to configfs_detach_prep().
860 * Note that configfs_detach_prep() expects the parent to be locked when it
861 * is called, but we lock the parent *inside* configfs_depend_prep(). We
862 * do that so we can unlock it if we find nothing.
864 * Here we do a depth-first search of the dentry hierarchy looking for
865 * our object. We take i_mutex on each step of the way down. IT IS
866 * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
867 * we'll drop the i_mutex.
869 * If the target is not found, -ENOENT is bubbled up and we have released
870 * all locks. If the target was found, the locks will be cleared by
871 * configfs_depend_rollback().
873 * This adds a requirement that all config_items be unique!
875 * This is recursive because the locking traversal is tricky. There isn't
876 * much on the stack, though, so folks that need this function - be careful
877 * about your stack! Patches will be accepted to make it iterative.
879 static int configfs_depend_prep(struct dentry *origin,
880 struct config_item *target)
882 struct configfs_dirent *child_sd, *sd = origin->d_fsdata;
885 BUG_ON(!origin || !sd);
887 /* Lock this guy on the way down */
888 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
889 if (sd->s_element == target) /* Boo-yah */
892 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
893 if (child_sd->s_type & CONFIGFS_DIR) {
894 ret = configfs_depend_prep(child_sd->s_dentry,
897 goto out; /* Child path boo-yah */
901 /* We looped all our children and didn't find target */
902 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
910 * This is ONLY called if configfs_depend_prep() did its job. So we can
911 * trust the entire path from item back up to origin.
913 * We walk backwards from item, unlocking each i_mutex. We finish by
916 static void configfs_depend_rollback(struct dentry *origin,
917 struct config_item *item)
919 struct dentry *dentry = item->ci_dentry;
921 while (dentry != origin) {
922 mutex_unlock(&dentry->d_inode->i_mutex);
923 dentry = dentry->d_parent;
926 mutex_unlock(&origin->d_inode->i_mutex);
929 int configfs_depend_item(struct configfs_subsystem *subsys,
930 struct config_item *target)
933 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
934 struct config_item *s_item = &subsys->su_group.cg_item;
937 * Pin the configfs filesystem. This means we can safely access
938 * the root of the configfs filesystem.
940 ret = configfs_pin_fs();
945 * Next, lock the root directory. We're going to check that the
946 * subsystem is really registered, and so we need to lock out
947 * configfs_[un]register_subsystem().
949 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
951 root_sd = configfs_sb->s_root->d_fsdata;
953 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
954 if (p->s_type & CONFIGFS_DIR) {
955 if (p->s_element == s_item) {
967 /* Ok, now we can trust subsys/s_item */
969 /* Scan the tree, locking i_mutex recursively, return 0 if found */
970 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
974 /* We hold all i_mutexes from the subsystem down to the target */
975 p = target->ci_dentry->d_fsdata;
976 p->s_dependent_count += 1;
978 configfs_depend_rollback(subsys_sd->s_dentry, target);
981 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
984 * If we succeeded, the fs is pinned via other methods. If not,
985 * we're done with it anyway. So release_fs() is always right.
987 configfs_release_fs();
991 EXPORT_SYMBOL(configfs_depend_item);
994 * Release the dependent linkage. This is much simpler than
995 * configfs_depend_item() because we know that that the client driver is
996 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
998 void configfs_undepend_item(struct configfs_subsystem *subsys,
999 struct config_item *target)
1001 struct configfs_dirent *sd;
1004 * Since we can trust everything is pinned, we just need i_mutex
1007 mutex_lock(&target->ci_dentry->d_inode->i_mutex);
1009 sd = target->ci_dentry->d_fsdata;
1010 BUG_ON(sd->s_dependent_count < 1);
1012 sd->s_dependent_count -= 1;
1015 * After this unlock, we cannot trust the item to stay alive!
1016 * DO NOT REFERENCE item after this unlock.
1018 mutex_unlock(&target->ci_dentry->d_inode->i_mutex);
1020 EXPORT_SYMBOL(configfs_undepend_item);
1022 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1024 int ret, module_got = 0;
1025 struct config_group *group;
1026 struct config_item *item;
1027 struct config_item *parent_item;
1028 struct configfs_subsystem *subsys;
1029 struct configfs_dirent *sd;
1030 struct config_item_type *type;
1031 struct module *owner = NULL;
1034 if (dentry->d_parent == configfs_sb->s_root) {
1039 sd = dentry->d_parent->d_fsdata;
1040 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1045 /* Get a working ref for the duration of this function */
1046 parent_item = configfs_get_config_item(dentry->d_parent);
1047 type = parent_item->ci_type;
1048 subsys = to_config_group(parent_item)->cg_subsys;
1051 if (!type || !type->ct_group_ops ||
1052 (!type->ct_group_ops->make_group &&
1053 !type->ct_group_ops->make_item)) {
1054 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1058 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1064 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1066 mutex_lock(&subsys->su_mutex);
1069 if (type->ct_group_ops->make_group) {
1070 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1072 link_group(to_config_group(parent_item), group);
1073 item = &group->cg_item;
1076 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1078 link_obj(parent_item, item);
1080 mutex_unlock(&subsys->su_mutex);
1085 * If item == NULL, then link_obj() was never called.
1086 * There are no extra references to clean up.
1093 * link_obj() has been called (via link_group() for groups).
1094 * From here on out, errors must clean that up.
1097 type = item->ci_type;
1103 owner = type->ct_owner;
1104 if (!try_module_get(owner)) {
1110 * I hate doing it this way, but if there is
1111 * an error, module_put() probably should
1112 * happen after any cleanup.
1117 ret = configfs_attach_group(parent_item, item, dentry);
1119 ret = configfs_attach_item(parent_item, item, dentry);
1123 /* Tear down everything we built up */
1124 mutex_lock(&subsys->su_mutex);
1126 client_disconnect_notify(parent_item, item);
1128 unlink_group(group);
1131 client_drop_item(parent_item, item);
1133 mutex_unlock(&subsys->su_mutex);
1141 * link_obj()/link_group() took a reference from child->parent,
1142 * so the parent is safely pinned. We can drop our working
1145 config_item_put(parent_item);
1151 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1153 struct config_item *parent_item;
1154 struct config_item *item;
1155 struct configfs_subsystem *subsys;
1156 struct configfs_dirent *sd;
1157 struct module *owner = NULL;
1160 if (dentry->d_parent == configfs_sb->s_root)
1163 sd = dentry->d_fsdata;
1164 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1168 * Here's where we check for dependents. We're protected by
1171 if (sd->s_dependent_count)
1174 /* Get a working ref until we have the child */
1175 parent_item = configfs_get_config_item(dentry->d_parent);
1176 subsys = to_config_group(parent_item)->cg_subsys;
1179 if (!parent_item->ci_type) {
1180 config_item_put(parent_item);
1184 ret = configfs_detach_prep(dentry);
1186 configfs_detach_rollback(dentry);
1187 config_item_put(parent_item);
1191 /* Get a working ref for the duration of this function */
1192 item = configfs_get_config_item(dentry);
1194 /* Drop reference from above, item already holds one. */
1195 config_item_put(parent_item);
1198 owner = item->ci_type->ct_owner;
1200 if (sd->s_type & CONFIGFS_USET_DIR) {
1201 configfs_detach_group(item);
1203 mutex_lock(&subsys->su_mutex);
1204 client_disconnect_notify(parent_item, item);
1205 unlink_group(to_config_group(item));
1207 configfs_detach_item(item);
1209 mutex_lock(&subsys->su_mutex);
1210 client_disconnect_notify(parent_item, item);
1214 client_drop_item(parent_item, item);
1215 mutex_unlock(&subsys->su_mutex);
1217 /* Drop our reference from above */
1218 config_item_put(item);
1225 const struct inode_operations configfs_dir_inode_operations = {
1226 .mkdir = configfs_mkdir,
1227 .rmdir = configfs_rmdir,
1228 .symlink = configfs_symlink,
1229 .unlink = configfs_unlink,
1230 .lookup = configfs_lookup,
1231 .setattr = configfs_setattr,
1235 int configfs_rename_dir(struct config_item * item, const char *new_name)
1238 struct dentry * new_dentry, * parent;
1240 if (!strcmp(config_item_name(item), new_name))
1246 down_write(&configfs_rename_sem);
1247 parent = item->parent->dentry;
1249 mutex_lock(&parent->d_inode->i_mutex);
1251 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1252 if (!IS_ERR(new_dentry)) {
1253 if (!new_dentry->d_inode) {
1254 error = config_item_set_name(item, "%s", new_name);
1256 d_add(new_dentry, NULL);
1257 d_move(item->dentry, new_dentry);
1260 d_delete(new_dentry);
1265 mutex_unlock(&parent->d_inode->i_mutex);
1266 up_write(&configfs_rename_sem);
1272 static int configfs_dir_open(struct inode *inode, struct file *file)
1274 struct dentry * dentry = file->f_path.dentry;
1275 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1277 mutex_lock(&dentry->d_inode->i_mutex);
1278 file->private_data = configfs_new_dirent(parent_sd, NULL);
1279 mutex_unlock(&dentry->d_inode->i_mutex);
1281 return IS_ERR(file->private_data) ? PTR_ERR(file->private_data) : 0;
1285 static int configfs_dir_close(struct inode *inode, struct file *file)
1287 struct dentry * dentry = file->f_path.dentry;
1288 struct configfs_dirent * cursor = file->private_data;
1290 mutex_lock(&dentry->d_inode->i_mutex);
1291 spin_lock(&configfs_dirent_lock);
1292 list_del_init(&cursor->s_sibling);
1293 spin_unlock(&configfs_dirent_lock);
1294 mutex_unlock(&dentry->d_inode->i_mutex);
1296 release_configfs_dirent(cursor);
1301 /* Relationship between s_mode and the DT_xxx types */
1302 static inline unsigned char dt_type(struct configfs_dirent *sd)
1304 return (sd->s_mode >> 12) & 15;
1307 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1309 struct dentry *dentry = filp->f_path.dentry;
1310 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1311 struct configfs_dirent *cursor = filp->private_data;
1312 struct list_head *p, *q = &cursor->s_sibling;
1314 int i = filp->f_pos;
1318 ino = dentry->d_inode->i_ino;
1319 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1325 ino = parent_ino(dentry);
1326 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1332 if (filp->f_pos == 2) {
1333 spin_lock(&configfs_dirent_lock);
1334 list_move(q, &parent_sd->s_children);
1335 spin_unlock(&configfs_dirent_lock);
1337 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1338 struct configfs_dirent *next;
1342 next = list_entry(p, struct configfs_dirent,
1344 if (!next->s_element)
1347 name = configfs_get_name(next);
1350 ino = next->s_dentry->d_inode->i_ino;
1352 ino = iunique(configfs_sb, 2);
1354 if (filldir(dirent, name, len, filp->f_pos, ino,
1358 spin_lock(&configfs_dirent_lock);
1360 spin_unlock(&configfs_dirent_lock);
1368 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1370 struct dentry * dentry = file->f_path.dentry;
1372 mutex_lock(&dentry->d_inode->i_mutex);
1375 offset += file->f_pos;
1380 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
1383 if (offset != file->f_pos) {
1384 file->f_pos = offset;
1385 if (file->f_pos >= 2) {
1386 struct configfs_dirent *sd = dentry->d_fsdata;
1387 struct configfs_dirent *cursor = file->private_data;
1388 struct list_head *p;
1389 loff_t n = file->f_pos - 2;
1391 spin_lock(&configfs_dirent_lock);
1392 list_del(&cursor->s_sibling);
1393 p = sd->s_children.next;
1394 while (n && p != &sd->s_children) {
1395 struct configfs_dirent *next;
1396 next = list_entry(p, struct configfs_dirent,
1398 if (next->s_element)
1402 list_add_tail(&cursor->s_sibling, p);
1403 spin_unlock(&configfs_dirent_lock);
1406 mutex_unlock(&dentry->d_inode->i_mutex);
1410 const struct file_operations configfs_dir_operations = {
1411 .open = configfs_dir_open,
1412 .release = configfs_dir_close,
1413 .llseek = configfs_dir_lseek,
1414 .read = generic_read_dir,
1415 .readdir = configfs_readdir,
1418 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1421 struct config_group *group = &subsys->su_group;
1423 struct dentry *dentry;
1424 struct configfs_dirent *sd;
1426 err = configfs_pin_fs();
1430 if (!group->cg_item.ci_name)
1431 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1433 sd = configfs_sb->s_root->d_fsdata;
1434 link_group(to_config_group(sd->s_element), group);
1436 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1439 name.name = group->cg_item.ci_name;
1440 name.len = strlen(name.name);
1441 name.hash = full_name_hash(name.name, name.len);
1444 dentry = d_alloc(configfs_sb->s_root, &name);
1446 d_add(dentry, NULL);
1448 err = configfs_attach_group(sd->s_element, &group->cg_item,
1456 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1459 unlink_group(group);
1460 configfs_release_fs();
1466 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1468 struct config_group *group = &subsys->su_group;
1469 struct dentry *dentry = group->cg_item.ci_dentry;
1471 if (dentry->d_parent != configfs_sb->s_root) {
1472 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1476 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1478 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1479 if (configfs_detach_prep(dentry)) {
1480 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1482 configfs_detach_group(&group->cg_item);
1483 dentry->d_inode->i_flags |= S_DEAD;
1484 mutex_unlock(&dentry->d_inode->i_mutex);
1488 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
1492 unlink_group(group);
1493 configfs_release_fs();
1496 EXPORT_SYMBOL(configfs_register_subsystem);
1497 EXPORT_SYMBOL(configfs_unregister_subsystem);