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_alloc(configfs_dir_cachep
, GFP_KERNEL
);
79 memset(sd
, 0, sizeof(*sd
));
80 atomic_set(&sd
->s_count
, 1);
81 INIT_LIST_HEAD(&sd
->s_links
);
82 INIT_LIST_HEAD(&sd
->s_children
);
83 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
84 sd
->s_element
= element
;
91 * Return -EEXIST if there is already a configfs element with the same
92 * name for the same parent.
94 * called with parent inode's i_mutex held
96 int configfs_dirent_exists(struct configfs_dirent
*parent_sd
,
97 const unsigned char *new)
99 struct configfs_dirent
* sd
;
101 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
103 const unsigned char *existing
= configfs_get_name(sd
);
104 if (strcmp(existing
, new))
115 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
116 struct dentry
* dentry
, void * element
,
117 umode_t mode
, int type
)
119 struct configfs_dirent
* sd
;
121 sd
= configfs_new_dirent(parent_sd
, element
);
127 sd
->s_dentry
= dentry
;
129 dentry
->d_fsdata
= configfs_get(sd
);
130 dentry
->d_op
= &configfs_dentry_ops
;
136 static int init_dir(struct inode
* inode
)
138 inode
->i_op
= &configfs_dir_inode_operations
;
139 inode
->i_fop
= &configfs_dir_operations
;
141 /* directory inodes start off with i_nlink == 2 (for "." entry) */
146 static int init_file(struct inode
* inode
)
148 inode
->i_size
= PAGE_SIZE
;
149 inode
->i_fop
= &configfs_file_operations
;
153 static int init_symlink(struct inode
* inode
)
155 inode
->i_op
= &configfs_symlink_inode_operations
;
159 static int create_dir(struct config_item
* k
, struct dentry
* p
,
163 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
165 error
= configfs_dirent_exists(p
->d_fsdata
, d
->d_name
.name
);
167 error
= configfs_make_dirent(p
->d_fsdata
, d
, k
, mode
,
170 error
= configfs_create(d
, mode
, init_dir
);
172 p
->d_inode
->i_nlink
++;
173 (d
)->d_op
= &configfs_dentry_ops
;
175 struct configfs_dirent
*sd
= d
->d_fsdata
;
177 list_del_init(&sd
->s_sibling
);
187 * configfs_create_dir - create a directory for an config_item.
188 * @item: config_itemwe're creating directory for.
189 * @dentry: config_item's dentry.
192 static int configfs_create_dir(struct config_item
* item
, struct dentry
*dentry
)
194 struct dentry
* parent
;
200 parent
= item
->ci_parent
->ci_dentry
;
201 else if (configfs_mount
&& configfs_mount
->mnt_sb
)
202 parent
= configfs_mount
->mnt_sb
->s_root
;
206 error
= create_dir(item
,parent
,dentry
);
208 item
->ci_dentry
= dentry
;
212 int configfs_create_link(struct configfs_symlink
*sl
,
213 struct dentry
*parent
,
214 struct dentry
*dentry
)
217 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
219 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
, mode
,
222 err
= configfs_create(dentry
, mode
, init_symlink
);
224 dentry
->d_op
= &configfs_dentry_ops
;
226 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
228 list_del_init(&sd
->s_sibling
);
236 static void remove_dir(struct dentry
* d
)
238 struct dentry
* parent
= dget(d
->d_parent
);
239 struct configfs_dirent
* sd
;
242 list_del_init(&sd
->s_sibling
);
245 simple_rmdir(parent
->d_inode
,d
);
247 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
248 atomic_read(&d
->d_count
));
254 * configfs_remove_dir - remove an config_item's directory.
255 * @item: config_item we're removing.
257 * The only thing special about this is that we remove any files in
258 * the directory before we remove the directory, and we've inlined
259 * what used to be configfs_rmdir() below, instead of calling separately.
262 static void configfs_remove_dir(struct config_item
* item
)
264 struct dentry
* dentry
= dget(item
->ci_dentry
);
271 * Drop reference from dget() on entrance.
277 /* attaches attribute's configfs_dirent to the dentry corresponding to the
280 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
282 struct configfs_attribute
* attr
= sd
->s_element
;
285 dentry
->d_fsdata
= configfs_get(sd
);
286 sd
->s_dentry
= dentry
;
287 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
, init_file
);
293 dentry
->d_op
= &configfs_dentry_ops
;
299 static struct dentry
* configfs_lookup(struct inode
*dir
,
300 struct dentry
*dentry
,
301 struct nameidata
*nd
)
303 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
304 struct configfs_dirent
* sd
;
308 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
309 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
310 const unsigned char * name
= configfs_get_name(sd
);
312 if (strcmp(name
, dentry
->d_name
.name
))
316 err
= configfs_attach_attr(sd
, dentry
);
323 * If it doesn't exist and it isn't a NOT_PINNED item,
324 * it must be negative.
326 return simple_lookup(dir
, dentry
, nd
);
333 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
334 * attributes and are removed by rmdir(). We recurse, taking i_mutex
335 * on all children that are candidates for default detach. If the
336 * result is clean, then configfs_detach_group() will handle dropping
337 * i_mutex. If there is an error, the caller will clean up the i_mutex
338 * holders via configfs_detach_rollback().
340 static int configfs_detach_prep(struct dentry
*dentry
)
342 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
343 struct configfs_dirent
*sd
;
347 if (!list_empty(&parent_sd
->s_links
))
351 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
352 if (sd
->s_type
& CONFIGFS_NOT_PINNED
)
354 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
355 mutex_lock(&sd
->s_dentry
->d_inode
->i_mutex
);
356 /* Mark that we've taken i_mutex */
357 sd
->s_type
|= CONFIGFS_USET_DROPPING
;
359 ret
= configfs_detach_prep(sd
->s_dentry
);
373 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
376 static void configfs_detach_rollback(struct dentry
*dentry
)
378 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
379 struct configfs_dirent
*sd
;
381 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
382 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
383 configfs_detach_rollback(sd
->s_dentry
);
385 if (sd
->s_type
& CONFIGFS_USET_DROPPING
) {
386 sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
387 mutex_unlock(&sd
->s_dentry
->d_inode
->i_mutex
);
393 static void detach_attrs(struct config_item
* item
)
395 struct dentry
* dentry
= dget(item
->ci_dentry
);
396 struct configfs_dirent
* parent_sd
;
397 struct configfs_dirent
* sd
, * tmp
;
402 pr_debug("configfs %s: dropping attrs for dir\n",
403 dentry
->d_name
.name
);
405 parent_sd
= dentry
->d_fsdata
;
406 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
407 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
409 list_del_init(&sd
->s_sibling
);
410 configfs_drop_dentry(sd
, dentry
);
415 * Drop reference from dget() on entrance.
420 static int populate_attrs(struct config_item
*item
)
422 struct config_item_type
*t
= item
->ci_type
;
423 struct configfs_attribute
*attr
;
430 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
431 if ((error
= configfs_create_file(item
, attr
)))
442 static int configfs_attach_group(struct config_item
*parent_item
,
443 struct config_item
*item
,
444 struct dentry
*dentry
);
445 static void configfs_detach_group(struct config_item
*item
);
447 static void detach_groups(struct config_group
*group
)
449 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
450 struct dentry
*child
;
451 struct configfs_dirent
*parent_sd
;
452 struct configfs_dirent
*sd
, *tmp
;
457 parent_sd
= dentry
->d_fsdata
;
458 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
459 if (!sd
->s_element
||
460 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
463 child
= sd
->s_dentry
;
465 configfs_detach_group(sd
->s_element
);
466 child
->d_inode
->i_flags
|= S_DEAD
;
469 * From rmdir/unregister, a configfs_detach_prep() pass
470 * has taken our i_mutex for us. Drop it.
471 * From mkdir/register cleanup, there is no sem held.
473 if (sd
->s_type
& CONFIGFS_USET_DROPPING
)
474 mutex_unlock(&child
->d_inode
->i_mutex
);
481 * Drop reference from dget() on entrance.
487 * This fakes mkdir(2) on a default_groups[] entry. It
488 * creates a dentry, attachs it, and then does fixup
491 * We could, perhaps, tweak our parent's ->mkdir for a minute and
492 * try using vfs_mkdir. Just a thought.
494 static int create_default_group(struct config_group
*parent_group
,
495 struct config_group
*group
)
499 struct configfs_dirent
*sd
;
500 /* We trust the caller holds a reference to parent */
501 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
503 if (!group
->cg_item
.ci_name
)
504 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
505 name
.name
= group
->cg_item
.ci_name
;
506 name
.len
= strlen(name
.name
);
507 name
.hash
= full_name_hash(name
.name
, name
.len
);
510 child
= d_alloc(parent
, &name
);
514 ret
= configfs_attach_group(&parent_group
->cg_item
,
515 &group
->cg_item
, child
);
517 sd
= child
->d_fsdata
;
518 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
528 static int populate_groups(struct config_group
*group
)
530 struct config_group
*new_group
;
531 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
535 if (group
->default_groups
) {
537 * FYI, we're faking mkdir here
538 * I'm not sure we need this semaphore, as we're called
539 * from our parent's mkdir. That holds our parent's
540 * i_mutex, so afaik lookup cannot continue through our
541 * parent to find us, let alone mess with our tree.
542 * That said, taking our i_mutex is closer to mkdir
543 * emulation, and shouldn't hurt.
545 mutex_lock(&dentry
->d_inode
->i_mutex
);
547 for (i
= 0; group
->default_groups
[i
]; i
++) {
548 new_group
= group
->default_groups
[i
];
550 ret
= create_default_group(group
, new_group
);
555 mutex_unlock(&dentry
->d_inode
->i_mutex
);
559 detach_groups(group
);
565 * All of link_obj/unlink_obj/link_group/unlink_group require that
566 * subsys->su_sem is held.
569 static void unlink_obj(struct config_item
*item
)
571 struct config_group
*group
;
573 group
= item
->ci_group
;
575 list_del_init(&item
->ci_entry
);
577 item
->ci_group
= NULL
;
578 item
->ci_parent
= NULL
;
580 /* Drop the reference for ci_entry */
581 config_item_put(item
);
583 /* Drop the reference for ci_parent */
584 config_group_put(group
);
588 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
591 * Parent seems redundant with group, but it makes certain
592 * traversals much nicer.
594 item
->ci_parent
= parent_item
;
597 * We hold a reference on the parent for the child's ci_parent
600 item
->ci_group
= config_group_get(to_config_group(parent_item
));
601 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
604 * We hold a reference on the child for ci_entry on the parent's
607 config_item_get(item
);
610 static void unlink_group(struct config_group
*group
)
613 struct config_group
*new_group
;
615 if (group
->default_groups
) {
616 for (i
= 0; group
->default_groups
[i
]; i
++) {
617 new_group
= group
->default_groups
[i
];
618 unlink_group(new_group
);
622 group
->cg_subsys
= NULL
;
623 unlink_obj(&group
->cg_item
);
626 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
629 struct config_group
*new_group
;
630 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
632 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
634 if (parent_group
->cg_subsys
)
635 subsys
= parent_group
->cg_subsys
;
636 else if (configfs_is_root(&parent_group
->cg_item
))
637 subsys
= to_configfs_subsystem(group
);
640 group
->cg_subsys
= subsys
;
642 if (group
->default_groups
) {
643 for (i
= 0; group
->default_groups
[i
]; i
++) {
644 new_group
= group
->default_groups
[i
];
645 link_group(group
, new_group
);
651 * The goal is that configfs_attach_item() (and
652 * configfs_attach_group()) can be called from either the VFS or this
653 * module. That is, they assume that the items have been created,
654 * the dentry allocated, and the dcache is all ready to go.
656 * If they fail, they must clean up after themselves as if they
657 * had never been called. The caller (VFS or local function) will
658 * handle cleaning up the dcache bits.
660 * configfs_detach_group() and configfs_detach_item() behave similarly on
661 * the way out. They assume that the proper semaphores are held, they
662 * clean up the configfs items, and they expect their callers will
663 * handle the dcache bits.
665 static int configfs_attach_item(struct config_item
*parent_item
,
666 struct config_item
*item
,
667 struct dentry
*dentry
)
671 ret
= configfs_create_dir(item
, dentry
);
673 ret
= populate_attrs(item
);
675 configfs_remove_dir(item
);
683 static void configfs_detach_item(struct config_item
*item
)
686 configfs_remove_dir(item
);
689 static int configfs_attach_group(struct config_item
*parent_item
,
690 struct config_item
*item
,
691 struct dentry
*dentry
)
694 struct configfs_dirent
*sd
;
696 ret
= configfs_attach_item(parent_item
, item
, dentry
);
698 sd
= dentry
->d_fsdata
;
699 sd
->s_type
|= CONFIGFS_USET_DIR
;
701 ret
= populate_groups(to_config_group(item
));
703 configfs_detach_item(item
);
711 static void configfs_detach_group(struct config_item
*item
)
713 detach_groups(to_config_group(item
));
714 configfs_detach_item(item
);
718 * Drop the initial reference from make_item()/make_group()
719 * This function assumes that reference is held on item
720 * and that item holds a valid reference to the parent. Also, it
721 * assumes the caller has validated ci_type.
723 static void client_drop_item(struct config_item
*parent_item
,
724 struct config_item
*item
)
726 struct config_item_type
*type
;
728 type
= parent_item
->ci_type
;
732 * If ->drop_item() exists, it is responsible for the
735 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
736 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
739 config_item_put(item
);
743 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
745 int ret
, module_got
= 0;
746 struct config_group
*group
;
747 struct config_item
*item
;
748 struct config_item
*parent_item
;
749 struct configfs_subsystem
*subsys
;
750 struct configfs_dirent
*sd
;
751 struct config_item_type
*type
;
752 struct module
*owner
= NULL
;
755 if (dentry
->d_parent
== configfs_sb
->s_root
) {
760 sd
= dentry
->d_parent
->d_fsdata
;
761 if (!(sd
->s_type
& CONFIGFS_USET_DIR
)) {
766 /* Get a working ref for the duration of this function */
767 parent_item
= configfs_get_config_item(dentry
->d_parent
);
768 type
= parent_item
->ci_type
;
769 subsys
= to_config_group(parent_item
)->cg_subsys
;
772 if (!type
|| !type
->ct_group_ops
||
773 (!type
->ct_group_ops
->make_group
&&
774 !type
->ct_group_ops
->make_item
)) {
775 ret
= -EPERM
; /* Lack-of-mkdir returns -EPERM */
779 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
785 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
787 down(&subsys
->su_sem
);
790 if (type
->ct_group_ops
->make_group
) {
791 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
793 link_group(to_config_group(parent_item
), group
);
794 item
= &group
->cg_item
;
797 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
799 link_obj(parent_item
, item
);
806 * If item == NULL, then link_obj() was never called.
807 * There are no extra references to clean up.
814 * link_obj() has been called (via link_group() for groups).
815 * From here on out, errors must clean that up.
818 type
= item
->ci_type
;
824 owner
= type
->ct_owner
;
825 if (!try_module_get(owner
)) {
831 * I hate doing it this way, but if there is
832 * an error, module_put() probably should
833 * happen after any cleanup.
838 ret
= configfs_attach_group(parent_item
, item
, dentry
);
840 ret
= configfs_attach_item(parent_item
, item
, dentry
);
844 /* Tear down everything we built up */
845 down(&subsys
->su_sem
);
850 client_drop_item(parent_item
, item
);
859 * link_obj()/link_group() took a reference from child->parent,
860 * so the parent is safely pinned. We can drop our working
863 config_item_put(parent_item
);
869 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
871 struct config_item
*parent_item
;
872 struct config_item
*item
;
873 struct configfs_subsystem
*subsys
;
874 struct configfs_dirent
*sd
;
875 struct module
*owner
= NULL
;
878 if (dentry
->d_parent
== configfs_sb
->s_root
)
881 sd
= dentry
->d_fsdata
;
882 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
885 /* Get a working ref until we have the child */
886 parent_item
= configfs_get_config_item(dentry
->d_parent
);
887 subsys
= to_config_group(parent_item
)->cg_subsys
;
890 if (!parent_item
->ci_type
) {
891 config_item_put(parent_item
);
895 ret
= configfs_detach_prep(dentry
);
897 configfs_detach_rollback(dentry
);
898 config_item_put(parent_item
);
902 /* Get a working ref for the duration of this function */
903 item
= configfs_get_config_item(dentry
);
905 /* Drop reference from above, item already holds one. */
906 config_item_put(parent_item
);
909 owner
= item
->ci_type
->ct_owner
;
911 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
912 configfs_detach_group(item
);
914 down(&subsys
->su_sem
);
915 unlink_group(to_config_group(item
));
917 configfs_detach_item(item
);
919 down(&subsys
->su_sem
);
923 client_drop_item(parent_item
, item
);
926 /* Drop our reference from above */
927 config_item_put(item
);
934 struct inode_operations configfs_dir_inode_operations
= {
935 .mkdir
= configfs_mkdir
,
936 .rmdir
= configfs_rmdir
,
937 .symlink
= configfs_symlink
,
938 .unlink
= configfs_unlink
,
939 .lookup
= configfs_lookup
,
940 .setattr
= configfs_setattr
,
944 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
947 struct dentry
* new_dentry
, * parent
;
949 if (!strcmp(config_item_name(item
), new_name
))
955 down_write(&configfs_rename_sem
);
956 parent
= item
->parent
->dentry
;
958 mutex_lock(&parent
->d_inode
->i_mutex
);
960 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
961 if (!IS_ERR(new_dentry
)) {
962 if (!new_dentry
->d_inode
) {
963 error
= config_item_set_name(item
, "%s", new_name
);
965 d_add(new_dentry
, NULL
);
966 d_move(item
->dentry
, new_dentry
);
969 d_delete(new_dentry
);
974 mutex_unlock(&parent
->d_inode
->i_mutex
);
975 up_write(&configfs_rename_sem
);
981 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
983 struct dentry
* dentry
= file
->f_dentry
;
984 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
986 mutex_lock(&dentry
->d_inode
->i_mutex
);
987 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
);
988 mutex_unlock(&dentry
->d_inode
->i_mutex
);
990 return file
->private_data
? 0 : -ENOMEM
;
994 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
996 struct dentry
* dentry
= file
->f_dentry
;
997 struct configfs_dirent
* cursor
= file
->private_data
;
999 mutex_lock(&dentry
->d_inode
->i_mutex
);
1000 list_del_init(&cursor
->s_sibling
);
1001 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1003 release_configfs_dirent(cursor
);
1008 /* Relationship between s_mode and the DT_xxx types */
1009 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
1011 return (sd
->s_mode
>> 12) & 15;
1014 static int configfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1016 struct dentry
*dentry
= filp
->f_dentry
;
1017 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
1018 struct configfs_dirent
*cursor
= filp
->private_data
;
1019 struct list_head
*p
, *q
= &cursor
->s_sibling
;
1021 int i
= filp
->f_pos
;
1025 ino
= dentry
->d_inode
->i_ino
;
1026 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1032 ino
= parent_ino(dentry
);
1033 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1039 if (filp
->f_pos
== 2) {
1040 list_move(q
, &parent_sd
->s_children
);
1042 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
1043 struct configfs_dirent
*next
;
1047 next
= list_entry(p
, struct configfs_dirent
,
1049 if (!next
->s_element
)
1052 name
= configfs_get_name(next
);
1055 ino
= next
->s_dentry
->d_inode
->i_ino
;
1057 ino
= iunique(configfs_sb
, 2);
1059 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
1071 static loff_t
configfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
1073 struct dentry
* dentry
= file
->f_dentry
;
1075 mutex_lock(&dentry
->d_inode
->i_mutex
);
1078 offset
+= file
->f_pos
;
1083 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
1086 if (offset
!= file
->f_pos
) {
1087 file
->f_pos
= offset
;
1088 if (file
->f_pos
>= 2) {
1089 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
1090 struct configfs_dirent
*cursor
= file
->private_data
;
1091 struct list_head
*p
;
1092 loff_t n
= file
->f_pos
- 2;
1094 list_del(&cursor
->s_sibling
);
1095 p
= sd
->s_children
.next
;
1096 while (n
&& p
!= &sd
->s_children
) {
1097 struct configfs_dirent
*next
;
1098 next
= list_entry(p
, struct configfs_dirent
,
1100 if (next
->s_element
)
1104 list_add_tail(&cursor
->s_sibling
, p
);
1107 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1111 const struct file_operations configfs_dir_operations
= {
1112 .open
= configfs_dir_open
,
1113 .release
= configfs_dir_close
,
1114 .llseek
= configfs_dir_lseek
,
1115 .read
= generic_read_dir
,
1116 .readdir
= configfs_readdir
,
1119 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1122 struct config_group
*group
= &subsys
->su_group
;
1124 struct dentry
*dentry
;
1125 struct configfs_dirent
*sd
;
1127 err
= configfs_pin_fs();
1131 if (!group
->cg_item
.ci_name
)
1132 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1134 sd
= configfs_sb
->s_root
->d_fsdata
;
1135 link_group(to_config_group(sd
->s_element
), group
);
1137 mutex_lock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1139 name
.name
= group
->cg_item
.ci_name
;
1140 name
.len
= strlen(name
.name
);
1141 name
.hash
= full_name_hash(name
.name
, name
.len
);
1144 dentry
= d_alloc(configfs_sb
->s_root
, &name
);
1148 d_add(dentry
, NULL
);
1150 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1157 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1162 unlink_group(group
);
1163 configfs_release_fs();
1169 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1171 struct config_group
*group
= &subsys
->su_group
;
1172 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1174 if (dentry
->d_parent
!= configfs_sb
->s_root
) {
1175 printk(KERN_ERR
"configfs: Tried to unregister non-subsystem!\n");
1179 mutex_lock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1180 mutex_lock(&dentry
->d_inode
->i_mutex
);
1181 if (configfs_detach_prep(dentry
)) {
1182 printk(KERN_ERR
"configfs: Tried to unregister non-empty subsystem!\n");
1184 configfs_detach_group(&group
->cg_item
);
1185 dentry
->d_inode
->i_flags
|= S_DEAD
;
1186 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1190 mutex_unlock(&configfs_sb
->s_root
->d_inode
->i_mutex
);
1194 unlink_group(group
);
1195 configfs_release_fs();
1198 EXPORT_SYMBOL(configfs_register_subsystem
);
1199 EXPORT_SYMBOL(configfs_unregister_subsystem
);