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>
34 #include <linux/configfs.h>
35 #include "configfs_internal.h"
37 DECLARE_RWSEM(configfs_rename_sem
);
39 static void configfs_d_iput(struct dentry
* dentry
,
42 struct configfs_dirent
* sd
= dentry
->d_fsdata
;
45 BUG_ON(sd
->s_dentry
!= dentry
);
53 * We _must_ delete our dentries on last dput, as the chain-to-parent
54 * behavior is required to clear the parents of default_groups.
56 static int configfs_d_delete(struct dentry
*dentry
)
61 static struct dentry_operations configfs_dentry_ops
= {
62 .d_iput
= configfs_d_iput
,
63 /* simple_delete_dentry() isn't exported */
64 .d_delete
= configfs_d_delete
,
68 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
70 static struct configfs_dirent
*configfs_new_dirent(struct configfs_dirent
* parent_sd
,
73 struct configfs_dirent
* sd
;
75 sd
= kmem_cache_zalloc(configfs_dir_cachep
, GFP_KERNEL
);
79 atomic_set(&sd
->s_count
, 1);
80 INIT_LIST_HEAD(&sd
->s_links
);
81 INIT_LIST_HEAD(&sd
->s_children
);
82 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
83 sd
->s_element
= element
;
90 * Return -EEXIST if there is already a configfs element with the same
91 * name for the same parent.
93 * called with parent inode's i_mutex held
95 static int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
96 const unsigned char *new)
98 struct configfs_dirent
* sd
;
100 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
102 const unsigned char *existing
= configfs_get_name(sd
);
103 if (strcmp(existing
, new))
114 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
115 struct dentry
* dentry
, void * element
,
116 umode_t mode
, int type
)
118 struct configfs_dirent
* sd
;
120 sd
= configfs_new_dirent(parent_sd
, element
);
126 sd
->s_dentry
= dentry
;
128 dentry
->d_fsdata
= configfs_get(sd
);
129 dentry
->d_op
= &configfs_dentry_ops
;
135 static int init_dir(struct inode
* inode
)
137 inode
->i_op
= &configfs_dir_inode_operations
;
138 inode
->i_fop
= &configfs_dir_operations
;
140 /* directory inodes start off with i_nlink == 2 (for "." entry) */
145 static int init_file(struct inode
* inode
)
147 inode
->i_size
= PAGE_SIZE
;
148 inode
->i_fop
= &configfs_file_operations
;
152 static int init_symlink(struct inode
* inode
)
154 inode
->i_op
= &configfs_symlink_inode_operations
;
158 static int create_dir(struct config_item
* k
, struct dentry
* p
,
162 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
164 error
= configfs_dirent_exists(p
->d_fsdata
, d
->d_name
.name
);
166 error
= configfs_make_dirent(p
->d_fsdata
, d
, k
, mode
,
169 error
= configfs_create(d
, mode
, init_dir
);
171 inc_nlink(p
->d_inode
);
172 (d
)->d_op
= &configfs_dentry_ops
;
174 struct configfs_dirent
*sd
= d
->d_fsdata
;
176 list_del_init(&sd
->s_sibling
);
186 * configfs_create_dir - create a directory for an config_item.
187 * @item: config_itemwe're creating directory for.
188 * @dentry: config_item's dentry.
191 static int configfs_create_dir(struct config_item
* item
, struct dentry
*dentry
)
193 struct dentry
* parent
;
199 parent
= item
->ci_parent
->ci_dentry
;
200 else if (configfs_mount
&& configfs_mount
->mnt_sb
)
201 parent
= configfs_mount
->mnt_sb
->s_root
;
205 error
= create_dir(item
,parent
,dentry
);
207 item
->ci_dentry
= dentry
;
211 int configfs_create_link(struct configfs_symlink
*sl
,
212 struct dentry
*parent
,
213 struct dentry
*dentry
)
216 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
218 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
221 err
= configfs_create(dentry
, mode
, init_symlink
);
223 dentry
->d_op
= &configfs_dentry_ops
;
225 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
227 list_del_init(&sd
->s_sibling
);
235 static void remove_dir(struct dentry
* d
)
237 struct dentry
* parent
= dget(d
->d_parent
);
238 struct configfs_dirent
* sd
;
241 list_del_init(&sd
->s_sibling
);
244 simple_rmdir(parent
->d_inode
,d
);
246 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
247 atomic_read(&d
->d_count
));
253 * configfs_remove_dir - remove an config_item's directory.
254 * @item: config_item we're removing.
256 * The only thing special about this is that we remove any files in
257 * the directory before we remove the directory, and we've inlined
258 * what used to be configfs_rmdir() below, instead of calling separately.
261 static void configfs_remove_dir(struct config_item
* item
)
263 struct dentry
* dentry
= dget(item
->ci_dentry
);
270 * Drop reference from dget() on entrance.
276 /* attaches attribute's configfs_dirent to the dentry corresponding to the
279 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
281 struct configfs_attribute
* attr
= sd
->s_element
;
284 dentry
->d_fsdata
= configfs_get(sd
);
285 sd
->s_dentry
= dentry
;
286 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
, init_file
);
292 dentry
->d_op
= &configfs_dentry_ops
;
298 static struct dentry
* configfs_lookup(struct inode
*dir
,
299 struct dentry
*dentry
,
300 struct nameidata
*nd
)
302 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
303 struct configfs_dirent
* sd
;
307 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
308 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
309 const unsigned char * name
= configfs_get_name(sd
);
311 if (strcmp(name
, dentry
->d_name
.name
))
315 err
= configfs_attach_attr(sd
, dentry
);
322 * If it doesn't exist and it isn't a NOT_PINNED item,
323 * it must be negative.
325 return simple_lookup(dir
, dentry
, nd
);
332 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
333 * attributes and are removed by rmdir(). We recurse, taking i_mutex
334 * on all children that are candidates for default detach. If the
335 * result is clean, then configfs_detach_group() will handle dropping
336 * i_mutex. If there is an error, the caller will clean up the i_mutex
337 * holders via configfs_detach_rollback().
339 static int configfs_detach_prep(struct dentry
*dentry
)
341 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
342 struct configfs_dirent
*sd
;
346 if (!list_empty(&parent_sd
->s_links
))
350 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
351 if (sd
->s_type
& CONFIGFS_NOT_PINNED
)
353 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
354 mutex_lock(&sd
->s_dentry
->d_inode
->i_mutex
);
355 /* Mark that we've taken i_mutex */
356 sd
->s_type
|= CONFIGFS_USET_DROPPING
;
358 ret
= configfs_detach_prep(sd
->s_dentry
);
372 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
375 static void configfs_detach_rollback(struct dentry
*dentry
)
377 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
378 struct configfs_dirent
*sd
;
380 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
381 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
382 configfs_detach_rollback(sd
->s_dentry
);
384 if (sd
->s_type
& CONFIGFS_USET_DROPPING
) {
385 sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
386 mutex_unlock(&sd
->s_dentry
->d_inode
->i_mutex
);
392 static void detach_attrs(struct config_item
* item
)
394 struct dentry
* dentry
= dget(item
->ci_dentry
);
395 struct configfs_dirent
* parent_sd
;
396 struct configfs_dirent
* sd
, * tmp
;
401 pr_debug("configfs %s: dropping attrs for dir\n",
402 dentry
->d_name
.name
);
404 parent_sd
= dentry
->d_fsdata
;
405 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
406 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
408 list_del_init(&sd
->s_sibling
);
409 configfs_drop_dentry(sd
, dentry
);
414 * Drop reference from dget() on entrance.
419 static int populate_attrs(struct config_item
*item
)
421 struct config_item_type
*t
= item
->ci_type
;
422 struct configfs_attribute
*attr
;
429 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
430 if ((error
= configfs_create_file(item
, attr
)))
441 static int configfs_attach_group(struct config_item
*parent_item
,
442 struct config_item
*item
,
443 struct dentry
*dentry
);
444 static void configfs_detach_group(struct config_item
*item
);
446 static void detach_groups(struct config_group
*group
)
448 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
449 struct dentry
*child
;
450 struct configfs_dirent
*parent_sd
;
451 struct configfs_dirent
*sd
, *tmp
;
456 parent_sd
= dentry
->d_fsdata
;
457 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
458 if (!sd
->s_element
||
459 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
462 child
= sd
->s_dentry
;
464 configfs_detach_group(sd
->s_element
);
465 child
->d_inode
->i_flags
|= S_DEAD
;
468 * From rmdir/unregister, a configfs_detach_prep() pass
469 * has taken our i_mutex for us. Drop it.
470 * From mkdir/register cleanup, there is no sem held.
472 if (sd
->s_type
& CONFIGFS_USET_DROPPING
)
473 mutex_unlock(&child
->d_inode
->i_mutex
);
480 * Drop reference from dget() on entrance.
486 * This fakes mkdir(2) on a default_groups[] entry. It
487 * creates a dentry, attachs it, and then does fixup
490 * We could, perhaps, tweak our parent's ->mkdir for a minute and
491 * try using vfs_mkdir. Just a thought.
493 static int create_default_group(struct config_group
*parent_group
,
494 struct config_group
*group
)
498 struct configfs_dirent
*sd
;
499 /* We trust the caller holds a reference to parent */
500 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
502 if (!group
->cg_item
.ci_name
)
503 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
504 name
.name
= group
->cg_item
.ci_name
;
505 name
.len
= strlen(name
.name
);
506 name
.hash
= full_name_hash(name
.name
, name
.len
);
509 child
= d_alloc(parent
, &name
);
513 ret
= configfs_attach_group(&parent_group
->cg_item
,
514 &group
->cg_item
, child
);
516 sd
= child
->d_fsdata
;
517 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
527 static int populate_groups(struct config_group
*group
)
529 struct config_group
*new_group
;
530 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
534 if (group
->default_groups
) {
536 * FYI, we're faking mkdir here
537 * I'm not sure we need this semaphore, as we're called
538 * from our parent's mkdir. That holds our parent's
539 * i_mutex, so afaik lookup cannot continue through our
540 * parent to find us, let alone mess with our tree.
541 * That said, taking our i_mutex is closer to mkdir
542 * emulation, and shouldn't hurt.
544 mutex_lock(&dentry
->d_inode
->i_mutex
);
546 for (i
= 0; group
->default_groups
[i
]; i
++) {
547 new_group
= group
->default_groups
[i
];
549 ret
= create_default_group(group
, new_group
);
554 mutex_unlock(&dentry
->d_inode
->i_mutex
);
558 detach_groups(group
);
564 * All of link_obj/unlink_obj/link_group/unlink_group require that
565 * subsys->su_sem is held.
568 static void unlink_obj(struct config_item
*item
)
570 struct config_group
*group
;
572 group
= item
->ci_group
;
574 list_del_init(&item
->ci_entry
);
576 item
->ci_group
= NULL
;
577 item
->ci_parent
= NULL
;
579 /* Drop the reference for ci_entry */
580 config_item_put(item
);
582 /* Drop the reference for ci_parent */
583 config_group_put(group
);
587 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
590 * Parent seems redundant with group, but it makes certain
591 * traversals much nicer.
593 item
->ci_parent
= parent_item
;
596 * We hold a reference on the parent for the child's ci_parent
599 item
->ci_group
= config_group_get(to_config_group(parent_item
));
600 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
603 * We hold a reference on the child for ci_entry on the parent's
606 config_item_get(item
);
609 static void unlink_group(struct config_group
*group
)
612 struct config_group
*new_group
;
614 if (group
->default_groups
) {
615 for (i
= 0; group
->default_groups
[i
]; i
++) {
616 new_group
= group
->default_groups
[i
];
617 unlink_group(new_group
);
621 group
->cg_subsys
= NULL
;
622 unlink_obj(&group
->cg_item
);
625 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
628 struct config_group
*new_group
;
629 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
631 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
633 if (parent_group
->cg_subsys
)
634 subsys
= parent_group
->cg_subsys
;
635 else if (configfs_is_root(&parent_group
->cg_item
))
636 subsys
= to_configfs_subsystem(group
);
639 group
->cg_subsys
= subsys
;
641 if (group
->default_groups
) {
642 for (i
= 0; group
->default_groups
[i
]; i
++) {
643 new_group
= group
->default_groups
[i
];
644 link_group(group
, new_group
);
650 * The goal is that configfs_attach_item() (and
651 * configfs_attach_group()) can be called from either the VFS or this
652 * module. That is, they assume that the items have been created,
653 * the dentry allocated, and the dcache is all ready to go.
655 * If they fail, they must clean up after themselves as if they
656 * had never been called. The caller (VFS or local function) will
657 * handle cleaning up the dcache bits.
659 * configfs_detach_group() and configfs_detach_item() behave similarly on
660 * the way out. They assume that the proper semaphores are held, they
661 * clean up the configfs items, and they expect their callers will
662 * handle the dcache bits.
664 static int configfs_attach_item(struct config_item
*parent_item
,
665 struct config_item
*item
,
666 struct dentry
*dentry
)
670 ret
= configfs_create_dir(item
, dentry
);
672 ret
= populate_attrs(item
);
674 configfs_remove_dir(item
);
682 static void configfs_detach_item(struct config_item
*item
)
685 configfs_remove_dir(item
);
688 static int configfs_attach_group(struct config_item
*parent_item
,
689 struct config_item
*item
,
690 struct dentry
*dentry
)
693 struct configfs_dirent
*sd
;
695 ret
= configfs_attach_item(parent_item
, item
, dentry
);
697 sd
= dentry
->d_fsdata
;
698 sd
->s_type
|= CONFIGFS_USET_DIR
;
700 ret
= populate_groups(to_config_group(item
));
702 configfs_detach_item(item
);
710 static void configfs_detach_group(struct config_item
*item
)
712 detach_groups(to_config_group(item
));
713 configfs_detach_item(item
);
717 * Drop the initial reference from make_item()/make_group()
718 * This function assumes that reference is held on item
719 * and that item holds a valid reference to the parent. Also, it
720 * assumes the caller has validated ci_type.
722 static void client_drop_item(struct config_item
*parent_item
,
723 struct config_item
*item
)
725 struct config_item_type
*type
;
727 type
= parent_item
->ci_type
;
731 * If ->drop_item() exists, it is responsible for the
734 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
735 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
738 config_item_put(item
);
742 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
744 int ret
, module_got
= 0;
745 struct config_group
*group
;
746 struct config_item
*item
;
747 struct config_item
*parent_item
;
748 struct configfs_subsystem
*subsys
;
749 struct configfs_dirent
*sd
;
750 struct config_item_type
*type
;
751 struct module
*owner
= NULL
;
754 if (dentry
->d_parent
== configfs_sb
->s_root
) {
759 sd
= dentry
->d_parent
->d_fsdata
;
760 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
765 /* Get a working ref for the duration of this function */
766 parent_item
= configfs_get_config_item(dentry
->d_parent
);
767 type
= parent_item
->ci_type
;
768 subsys
= to_config_group(parent_item
)->cg_subsys
;
771 if (!type
|| !type
->ct_group_ops
||
772 (!type
->ct_group_ops
->make_group
&&
773 !type
->ct_group_ops
->make_item
)) {
774 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
778 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
784 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
786 down(&subsys
->su_sem
);
789 if (type
->ct_group_ops
->make_group
) {
790 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
792 link_group(to_config_group(parent_item
), group
);
793 item
= &group
->cg_item
;
796 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
798 link_obj(parent_item
, item
);
805 * If item == NULL, then link_obj() was never called.
806 * There are no extra references to clean up.
813 * link_obj() has been called (via link_group() for groups).
814 * From here on out, errors must clean that up.
817 type
= item
->ci_type
;
823 owner
= type
->ct_owner
;
824 if (!try_module_get(owner
)) {
830 * I hate doing it this way, but if there is
831 * an error, module_put() probably should
832 * happen after any cleanup.
837 ret
= configfs_attach_group(parent_item
, item
, dentry
);
839 ret
= configfs_attach_item(parent_item
, item
, dentry
);
843 /* Tear down everything we built up */
844 down(&subsys
->su_sem
);
849 client_drop_item(parent_item
, item
);
858 * link_obj()/link_group() took a reference from child->parent,
859 * so the parent is safely pinned. We can drop our working
862 config_item_put(parent_item
);
868 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
870 struct config_item
*parent_item
;
871 struct config_item
*item
;
872 struct configfs_subsystem
*subsys
;
873 struct configfs_dirent
*sd
;
874 struct module
*owner
= NULL
;
877 if (dentry
->d_parent
== configfs_sb
->s_root
)
880 sd
= dentry
->d_fsdata
;
881 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
884 /* Get a working ref until we have the child */
885 parent_item
= configfs_get_config_item(dentry
->d_parent
);
886 subsys
= to_config_group(parent_item
)->cg_subsys
;
889 if (!parent_item
->ci_type
) {
890 config_item_put(parent_item
);
894 ret
= configfs_detach_prep(dentry
);
896 configfs_detach_rollback(dentry
);
897 config_item_put(parent_item
);
901 /* Get a working ref for the duration of this function */
902 item
= configfs_get_config_item(dentry
);
904 /* Drop reference from above, item already holds one. */
905 config_item_put(parent_item
);
908 owner
= item
->ci_type
->ct_owner
;
910 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
911 configfs_detach_group(item
);
913 down(&subsys
->su_sem
);
914 unlink_group(to_config_group(item
));
916 configfs_detach_item(item
);
918 down(&subsys
->su_sem
);
922 client_drop_item(parent_item
, item
);
925 /* Drop our reference from above */
926 config_item_put(item
);
933 const struct inode_operations configfs_dir_inode_operations
= {
934 .mkdir
= configfs_mkdir
,
935 .rmdir
= configfs_rmdir
,
936 .symlink
= configfs_symlink
,
937 .unlink
= configfs_unlink
,
938 .lookup
= configfs_lookup
,
939 .setattr
= configfs_setattr
,
943 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
946 struct dentry
* new_dentry
, * parent
;
948 if (!strcmp(config_item_name(item
), new_name
))
954 down_write(&configfs_rename_sem
);
955 parent
= item
->parent
->dentry
;
957 mutex_lock(&parent
->d_inode
->i_mutex
);
959 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
960 if (!IS_ERR(new_dentry
)) {
961 if (!new_dentry
->d_inode
) {
962 error
= config_item_set_name(item
, "%s", new_name
);
964 d_add(new_dentry
, NULL
);
965 d_move(item
->dentry
, new_dentry
);
968 d_delete(new_dentry
);
973 mutex_unlock(&parent
->d_inode
->i_mutex
);
974 up_write(&configfs_rename_sem
);
980 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
982 struct dentry
* dentry
= file
->f_path
.dentry
;
983 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
985 mutex_lock(&dentry
->d_inode
->i_mutex
);
986 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
);
987 mutex_unlock(&dentry
->d_inode
->i_mutex
);
989 return file
->private_data
? 0 : -ENOMEM
;
993 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
995 struct dentry
* dentry
= file
->f_path
.dentry
;
996 struct configfs_dirent
* cursor
= file
->private_data
;
998 mutex_lock(&dentry
->d_inode
->i_mutex
);
999 list_del_init(&cursor
->s_sibling
);
1000 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1002 release_configfs_dirent(cursor
);
1007 /* Relationship between s_mode and the DT_xxx types */
1008 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1010 return (sd
->s_mode
>> 12) & 15;
1013 static int configfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1015 struct dentry
*dentry
= filp
->f_path
.dentry
;
1016 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1017 struct configfs_dirent
*cursor
= filp
->private_data
;
1018 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1020 int i
= filp
->f_pos
;
1024 ino
= dentry
->d_inode
->i_ino
;
1025 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1031 ino
= parent_ino(dentry
);
1032 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1038 if (filp
->f_pos
== 2) {
1039 list_move(q
, &parent_sd
->s_children
);
1041 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
1042 struct configfs_dirent
*next
;
1046 next
= list_entry(p
, struct configfs_dirent
,
1048 if (!next
->s_element
)
1051 name
= configfs_get_name(next
);
1054 ino
= next
->s_dentry
->d_inode
->i_ino
;
1056 ino
= iunique(configfs_sb
, 2);
1058 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
1070 static loff_t
configfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
1072 struct dentry
* dentry
= file
->f_path
.dentry
;
1074 mutex_lock(&dentry
->d_inode
->i_mutex
);
1077 offset
+= file
->f_pos
;
1082 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
1085 if (offset
!= file
->f_pos
) {
1086 file
->f_pos
= offset
;
1087 if (file
->f_pos
>= 2) {
1088 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1089 struct configfs_dirent
*cursor
= file
->private_data
;
1090 struct list_head
*p
;
1091 loff_t n
= file
->f_pos
- 2;
1093 list_del(&cursor
->s_sibling
);
1094 p
= sd
->s_children
.next
;
1095 while (n
&& p
!= &sd
->s_children
) {
1096 struct configfs_dirent
*next
;
1097 next
= list_entry(p
, struct configfs_dirent
,
1099 if (next
->s_element
)
1103 list_add_tail(&cursor
->s_sibling
, p
);
1106 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1110 const struct file_operations configfs_dir_operations
= {
1111 .open
= configfs_dir_open
,
1112 .release
= configfs_dir_close
,
1113 .llseek
= configfs_dir_lseek
,
1114 .read
= generic_read_dir
,
1115 .readdir
= configfs_readdir
,
1118 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1121 struct config_group
*group
= &subsys
->su_group
;
1123 struct dentry
*dentry
;
1124 struct configfs_dirent
*sd
;
1126 err
= configfs_pin_fs();
1130 if (!group
->cg_item
.ci_name
)
1131 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1133 sd
= configfs_sb
->s_root
->d_fsdata
;
1134 link_group(to_config_group(sd
->s_element
), group
);
1136 mutex_lock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1138 name
.name
= group
->cg_item
.ci_name
;
1139 name
.len
= strlen(name
.name
);
1140 name
.hash
= full_name_hash(name
.name
, name
.len
);
1143 dentry
= d_alloc(configfs_sb
->s_root
, &name
);
1145 d_add(dentry
, NULL
);
1147 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1155 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1158 unlink_group(group
);
1159 configfs_release_fs();
1165 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1167 struct config_group
*group
= &subsys
->su_group
;
1168 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1170 if (dentry
->d_parent
!= configfs_sb
->s_root
) {
1171 printk(KERN_ERR
"configfs: Tried to unregister non-subsystem!\n");
1175 mutex_lock_nested(&configfs_sb
->s_root
->d_inode
->i_mutex
,
1177 mutex_lock_nested(&dentry
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1178 if (configfs_detach_prep(dentry
)) {
1179 printk(KERN_ERR
"configfs: Tried to unregister non-empty subsystem!\n");
1181 configfs_detach_group(&group
->cg_item
);
1182 dentry
->d_inode
->i_flags
|= S_DEAD
;
1183 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1187 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1191 unlink_group(group
);
1192 configfs_release_fs();
1195 EXPORT_SYMBOL(configfs_register_subsystem
);
1196 EXPORT_SYMBOL(configfs_unregister_subsystem
);