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
= kmalloc(sizeof(*sd
), 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
;
89 int configfs_make_dirent(struct configfs_dirent
* parent_sd
,
90 struct dentry
* dentry
, void * element
,
91 umode_t mode
, int type
)
93 struct configfs_dirent
* sd
;
95 sd
= configfs_new_dirent(parent_sd
, element
);
101 sd
->s_dentry
= dentry
;
103 dentry
->d_fsdata
= configfs_get(sd
);
104 dentry
->d_op
= &configfs_dentry_ops
;
110 static int init_dir(struct inode
* inode
)
112 inode
->i_op
= &configfs_dir_inode_operations
;
113 inode
->i_fop
= &configfs_dir_operations
;
115 /* directory inodes start off with i_nlink == 2 (for "." entry) */
120 static int init_file(struct inode
* inode
)
122 inode
->i_size
= PAGE_SIZE
;
123 inode
->i_fop
= &configfs_file_operations
;
127 static int init_symlink(struct inode
* inode
)
129 inode
->i_op
= &configfs_symlink_inode_operations
;
133 static int create_dir(struct config_item
* k
, struct dentry
* p
,
137 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
139 error
= configfs_create(d
, mode
, init_dir
);
141 error
= configfs_make_dirent(p
->d_fsdata
, d
, k
, mode
,
144 p
->d_inode
->i_nlink
++;
145 (d
)->d_op
= &configfs_dentry_ops
;
153 * configfs_create_dir - create a directory for an config_item.
154 * @item: config_itemwe're creating directory for.
155 * @dentry: config_item's dentry.
158 static int configfs_create_dir(struct config_item
* item
, struct dentry
*dentry
)
160 struct dentry
* parent
;
166 parent
= item
->ci_parent
->ci_dentry
;
167 else if (configfs_mount
&& configfs_mount
->mnt_sb
)
168 parent
= configfs_mount
->mnt_sb
->s_root
;
172 error
= create_dir(item
,parent
,dentry
);
174 item
->ci_dentry
= dentry
;
178 int configfs_create_link(struct configfs_symlink
*sl
,
179 struct dentry
*parent
,
180 struct dentry
*dentry
)
183 umode_t mode
= S_IFLNK
| S_IRWXUGO
;
185 err
= configfs_create(dentry
, mode
, init_symlink
);
187 err
= configfs_make_dirent(parent
->d_fsdata
, dentry
, sl
,
188 mode
, CONFIGFS_ITEM_LINK
);
190 dentry
->d_op
= &configfs_dentry_ops
;
195 static void remove_dir(struct dentry
* d
)
197 struct dentry
* parent
= dget(d
->d_parent
);
198 struct configfs_dirent
* sd
;
201 list_del_init(&sd
->s_sibling
);
204 simple_rmdir(parent
->d_inode
,d
);
206 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
207 atomic_read(&d
->d_count
));
213 * configfs_remove_dir - remove an config_item's directory.
214 * @item: config_item we're removing.
216 * The only thing special about this is that we remove any files in
217 * the directory before we remove the directory, and we've inlined
218 * what used to be configfs_rmdir() below, instead of calling separately.
221 static void configfs_remove_dir(struct config_item
* item
)
223 struct dentry
* dentry
= dget(item
->ci_dentry
);
230 * Drop reference from dget() on entrance.
236 /* attaches attribute's configfs_dirent to the dentry corresponding to the
239 static int configfs_attach_attr(struct configfs_dirent
* sd
, struct dentry
* dentry
)
241 struct configfs_attribute
* attr
= sd
->s_element
;
244 error
= configfs_create(dentry
, (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
, init_file
);
248 dentry
->d_op
= &configfs_dentry_ops
;
249 dentry
->d_fsdata
= configfs_get(sd
);
250 sd
->s_dentry
= dentry
;
256 static struct dentry
* configfs_lookup(struct inode
*dir
,
257 struct dentry
*dentry
,
258 struct nameidata
*nd
)
260 struct configfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
261 struct configfs_dirent
* sd
;
265 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
266 if (sd
->s_type
& CONFIGFS_NOT_PINNED
) {
267 const unsigned char * name
= configfs_get_name(sd
);
269 if (strcmp(name
, dentry
->d_name
.name
))
273 err
= configfs_attach_attr(sd
, dentry
);
280 * If it doesn't exist and it isn't a NOT_PINNED item,
281 * it must be negative.
283 return simple_lookup(dir
, dentry
, nd
);
290 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
291 * attributes and are removed by rmdir(). We recurse, taking i_sem
292 * on all children that are candidates for default detach. If the
293 * result is clean, then configfs_detach_group() will handle dropping
294 * i_sem. If there is an error, the caller will clean up the i_sem
295 * holders via configfs_detach_rollback().
297 static int configfs_detach_prep(struct dentry
*dentry
)
299 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
300 struct configfs_dirent
*sd
;
304 if (!list_empty(&parent_sd
->s_links
))
308 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
309 if (sd
->s_type
& CONFIGFS_NOT_PINNED
)
311 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
312 down(&sd
->s_dentry
->d_inode
->i_sem
);
313 /* Mark that we've taken i_sem */
314 sd
->s_type
|= CONFIGFS_USET_DROPPING
;
316 ret
= configfs_detach_prep(sd
->s_dentry
);
330 * Walk the tree, dropping i_sem wherever CONFIGFS_USET_DROPPING is
333 static void configfs_detach_rollback(struct dentry
*dentry
)
335 struct configfs_dirent
*parent_sd
= dentry
->d_fsdata
;
336 struct configfs_dirent
*sd
;
338 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
339 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
) {
340 configfs_detach_rollback(sd
->s_dentry
);
342 if (sd
->s_type
& CONFIGFS_USET_DROPPING
) {
343 sd
->s_type
&= ~CONFIGFS_USET_DROPPING
;
344 up(&sd
->s_dentry
->d_inode
->i_sem
);
350 static void detach_attrs(struct config_item
* item
)
352 struct dentry
* dentry
= dget(item
->ci_dentry
);
353 struct configfs_dirent
* parent_sd
;
354 struct configfs_dirent
* sd
, * tmp
;
359 pr_debug("configfs %s: dropping attrs for dir\n",
360 dentry
->d_name
.name
);
362 parent_sd
= dentry
->d_fsdata
;
363 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
364 if (!sd
->s_element
|| !(sd
->s_type
& CONFIGFS_NOT_PINNED
))
366 list_del_init(&sd
->s_sibling
);
367 configfs_drop_dentry(sd
, dentry
);
372 * Drop reference from dget() on entrance.
377 static int populate_attrs(struct config_item
*item
)
379 struct config_item_type
*t
= item
->ci_type
;
380 struct configfs_attribute
*attr
;
387 for (i
= 0; (attr
= t
->ct_attrs
[i
]) != NULL
; i
++) {
388 if ((error
= configfs_create_file(item
, attr
)))
399 static int configfs_attach_group(struct config_item
*parent_item
,
400 struct config_item
*item
,
401 struct dentry
*dentry
);
402 static void configfs_detach_group(struct config_item
*item
);
404 static void detach_groups(struct config_group
*group
)
406 struct dentry
* dentry
= dget(group
->cg_item
.ci_dentry
);
407 struct dentry
*child
;
408 struct configfs_dirent
*parent_sd
;
409 struct configfs_dirent
*sd
, *tmp
;
414 parent_sd
= dentry
->d_fsdata
;
415 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
416 if (!sd
->s_element
||
417 !(sd
->s_type
& CONFIGFS_USET_DEFAULT
))
420 child
= sd
->s_dentry
;
422 configfs_detach_group(sd
->s_element
);
423 child
->d_inode
->i_flags
|= S_DEAD
;
426 * From rmdir/unregister, a configfs_detach_prep() pass
427 * has taken our i_sem for us. Drop it.
428 * From mkdir/register cleanup, there is no sem held.
430 if (sd
->s_type
& CONFIGFS_USET_DROPPING
)
431 up(&child
->d_inode
->i_sem
);
438 * Drop reference from dget() on entrance.
444 * This fakes mkdir(2) on a default_groups[] entry. It
445 * creates a dentry, attachs it, and then does fixup
448 * We could, perhaps, tweak our parent's ->mkdir for a minute and
449 * try using vfs_mkdir. Just a thought.
451 static int create_default_group(struct config_group
*parent_group
,
452 struct config_group
*group
)
456 struct configfs_dirent
*sd
;
457 /* We trust the caller holds a reference to parent */
458 struct dentry
*child
, *parent
= parent_group
->cg_item
.ci_dentry
;
460 if (!group
->cg_item
.ci_name
)
461 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
462 name
.name
= group
->cg_item
.ci_name
;
463 name
.len
= strlen(name
.name
);
464 name
.hash
= full_name_hash(name
.name
, name
.len
);
467 child
= d_alloc(parent
, &name
);
471 ret
= configfs_attach_group(&parent_group
->cg_item
,
472 &group
->cg_item
, child
);
474 sd
= child
->d_fsdata
;
475 sd
->s_type
|= CONFIGFS_USET_DEFAULT
;
485 static int populate_groups(struct config_group
*group
)
487 struct config_group
*new_group
;
488 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
492 if (group
&& group
->default_groups
) {
493 /* FYI, we're faking mkdir here
494 * I'm not sure we need this semaphore, as we're called
495 * from our parent's mkdir. That holds our parent's
496 * i_sem, so afaik lookup cannot continue through our
497 * parent to find us, let alone mess with our tree.
498 * That said, taking our i_sem is closer to mkdir
499 * emulation, and shouldn't hurt. */
500 down(&dentry
->d_inode
->i_sem
);
502 for (i
= 0; group
->default_groups
[i
]; i
++) {
503 new_group
= group
->default_groups
[i
];
505 ret
= create_default_group(group
, new_group
);
510 up(&dentry
->d_inode
->i_sem
);
514 detach_groups(group
);
520 * All of link_obj/unlink_obj/link_group/unlink_group require that
521 * subsys->su_sem is held.
524 static void unlink_obj(struct config_item
*item
)
526 struct config_group
*group
;
528 group
= item
->ci_group
;
530 list_del_init(&item
->ci_entry
);
532 item
->ci_group
= NULL
;
533 item
->ci_parent
= NULL
;
534 config_item_put(item
);
536 config_group_put(group
);
540 static void link_obj(struct config_item
*parent_item
, struct config_item
*item
)
542 /* Parent seems redundant with group, but it makes certain
543 * traversals much nicer. */
544 item
->ci_parent
= parent_item
;
545 item
->ci_group
= config_group_get(to_config_group(parent_item
));
546 list_add_tail(&item
->ci_entry
, &item
->ci_group
->cg_children
);
548 config_item_get(item
);
551 static void unlink_group(struct config_group
*group
)
554 struct config_group
*new_group
;
556 if (group
->default_groups
) {
557 for (i
= 0; group
->default_groups
[i
]; i
++) {
558 new_group
= group
->default_groups
[i
];
559 unlink_group(new_group
);
563 group
->cg_subsys
= NULL
;
564 unlink_obj(&group
->cg_item
);
567 static void link_group(struct config_group
*parent_group
, struct config_group
*group
)
570 struct config_group
*new_group
;
571 struct configfs_subsystem
*subsys
= NULL
; /* gcc is a turd */
573 link_obj(&parent_group
->cg_item
, &group
->cg_item
);
575 if (parent_group
->cg_subsys
)
576 subsys
= parent_group
->cg_subsys
;
577 else if (configfs_is_root(&parent_group
->cg_item
))
578 subsys
= to_configfs_subsystem(group
);
581 group
->cg_subsys
= subsys
;
583 if (group
->default_groups
) {
584 for (i
= 0; group
->default_groups
[i
]; i
++) {
585 new_group
= group
->default_groups
[i
];
586 link_group(group
, new_group
);
592 * The goal is that configfs_attach_item() (and
593 * configfs_attach_group()) can be called from either the VFS or this
594 * module. That is, they assume that the items have been created,
595 * the dentry allocated, and the dcache is all ready to go.
597 * If they fail, they must clean up after themselves as if they
598 * had never been called. The caller (VFS or local function) will
599 * handle cleaning up the dcache bits.
601 * configfs_detach_group() and configfs_detach_item() behave similarly on
602 * the way out. They assume that the proper semaphores are held, they
603 * clean up the configfs items, and they expect their callers will
604 * handle the dcache bits.
606 static int configfs_attach_item(struct config_item
*parent_item
,
607 struct config_item
*item
,
608 struct dentry
*dentry
)
612 ret
= configfs_create_dir(item
, dentry
);
614 ret
= populate_attrs(item
);
616 configfs_remove_dir(item
);
624 static void configfs_detach_item(struct config_item
*item
)
627 configfs_remove_dir(item
);
630 static int configfs_attach_group(struct config_item
*parent_item
,
631 struct config_item
*item
,
632 struct dentry
*dentry
)
635 struct configfs_dirent
*sd
;
637 ret
= configfs_attach_item(parent_item
, item
, dentry
);
639 sd
= dentry
->d_fsdata
;
640 sd
->s_type
|= CONFIGFS_USET_DIR
;
642 ret
= populate_groups(to_config_group(item
));
644 configfs_detach_item(item
);
652 static void configfs_detach_group(struct config_item
*item
)
654 detach_groups(to_config_group(item
));
655 configfs_detach_item(item
);
659 * Drop the initial reference from make_item()/make_group()
660 * This function assumes that reference is held on item
661 * and that item holds a valid reference to the parent. Also, it
662 * assumes the caller has validated ci_type.
664 static void client_drop_item(struct config_item
*parent_item
,
665 struct config_item
*item
)
667 struct config_item_type
*type
;
669 type
= parent_item
->ci_type
;
672 if (type
->ct_group_ops
&& type
->ct_group_ops
->drop_item
)
673 type
->ct_group_ops
->drop_item(to_config_group(parent_item
),
676 config_item_put(item
);
680 static int configfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
683 struct config_group
*group
;
684 struct config_item
*item
;
685 struct config_item
*parent_item
;
686 struct configfs_subsystem
*subsys
;
687 struct configfs_dirent
*sd
;
688 struct config_item_type
*type
;
689 struct module
*owner
;
692 if (dentry
->d_parent
== configfs_sb
->s_root
)
695 sd
= dentry
->d_parent
->d_fsdata
;
696 if (!(sd
->s_type
& CONFIGFS_USET_DIR
))
699 parent_item
= configfs_get_config_item(dentry
->d_parent
);
700 type
= parent_item
->ci_type
;
701 subsys
= to_config_group(parent_item
)->cg_subsys
;
704 if (!type
|| !type
->ct_group_ops
||
705 (!type
->ct_group_ops
->make_group
&&
706 !type
->ct_group_ops
->make_item
)) {
707 config_item_put(parent_item
);
708 return -EPERM
; /* What lack-of-mkdir returns */
711 name
= kmalloc(dentry
->d_name
.len
+ 1, GFP_KERNEL
);
713 config_item_put(parent_item
);
716 snprintf(name
, dentry
->d_name
.len
+ 1, "%s", dentry
->d_name
.name
);
718 down(&subsys
->su_sem
);
721 if (type
->ct_group_ops
->make_group
) {
722 group
= type
->ct_group_ops
->make_group(to_config_group(parent_item
), name
);
724 link_group(to_config_group(parent_item
), group
);
725 item
= &group
->cg_item
;
728 item
= type
->ct_group_ops
->make_item(to_config_group(parent_item
), name
);
730 link_obj(parent_item
, item
);
736 config_item_put(parent_item
);
741 type
= item
->ci_type
;
743 owner
= type
->ct_owner
;
744 if (try_module_get(owner
)) {
746 ret
= configfs_attach_group(parent_item
,
750 ret
= configfs_attach_item(parent_item
,
756 down(&subsys
->su_sem
);
761 client_drop_item(parent_item
, item
);
764 config_item_put(parent_item
);
773 static int configfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
775 struct config_item
*parent_item
;
776 struct config_item
*item
;
777 struct configfs_subsystem
*subsys
;
778 struct configfs_dirent
*sd
;
779 struct module
*owner
= NULL
;
782 if (dentry
->d_parent
== configfs_sb
->s_root
)
785 sd
= dentry
->d_fsdata
;
786 if (sd
->s_type
& CONFIGFS_USET_DEFAULT
)
789 parent_item
= configfs_get_config_item(dentry
->d_parent
);
790 subsys
= to_config_group(parent_item
)->cg_subsys
;
793 if (!parent_item
->ci_type
) {
794 config_item_put(parent_item
);
798 ret
= configfs_detach_prep(dentry
);
800 configfs_detach_rollback(dentry
);
801 config_item_put(parent_item
);
805 item
= configfs_get_config_item(dentry
);
807 /* Drop reference from above, item already holds one. */
808 config_item_put(parent_item
);
811 owner
= item
->ci_type
->ct_owner
;
813 if (sd
->s_type
& CONFIGFS_USET_DIR
) {
814 configfs_detach_group(item
);
816 down(&subsys
->su_sem
);
817 unlink_group(to_config_group(item
));
819 configfs_detach_item(item
);
821 down(&subsys
->su_sem
);
825 client_drop_item(parent_item
, item
);
828 /* Drop our reference from above */
829 config_item_put(item
);
836 struct inode_operations configfs_dir_inode_operations
= {
837 .mkdir
= configfs_mkdir
,
838 .rmdir
= configfs_rmdir
,
839 .symlink
= configfs_symlink
,
840 .unlink
= configfs_unlink
,
841 .lookup
= configfs_lookup
,
845 int configfs_rename_dir(struct config_item
* item
, const char *new_name
)
848 struct dentry
* new_dentry
, * parent
;
850 if (!strcmp(config_item_name(item
), new_name
))
856 down_write(&configfs_rename_sem
);
857 parent
= item
->parent
->dentry
;
859 down(&parent
->d_inode
->i_sem
);
861 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
862 if (!IS_ERR(new_dentry
)) {
863 if (!new_dentry
->d_inode
) {
864 error
= config_item_set_name(item
, "%s", new_name
);
866 d_add(new_dentry
, NULL
);
867 d_move(item
->dentry
, new_dentry
);
870 d_delete(new_dentry
);
875 up(&parent
->d_inode
->i_sem
);
876 up_write(&configfs_rename_sem
);
882 static int configfs_dir_open(struct inode
*inode
, struct file
*file
)
884 struct dentry
* dentry
= file
->f_dentry
;
885 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
887 down(&dentry
->d_inode
->i_sem
);
888 file
->private_data
= configfs_new_dirent(parent_sd
, NULL
);
889 up(&dentry
->d_inode
->i_sem
);
891 return file
->private_data
? 0 : -ENOMEM
;
895 static int configfs_dir_close(struct inode
*inode
, struct file
*file
)
897 struct dentry
* dentry
= file
->f_dentry
;
898 struct configfs_dirent
* cursor
= file
->private_data
;
900 down(&dentry
->d_inode
->i_sem
);
901 list_del_init(&cursor
->s_sibling
);
902 up(&dentry
->d_inode
->i_sem
);
904 release_configfs_dirent(cursor
);
909 /* Relationship between s_mode and the DT_xxx types */
910 static inline unsigned char dt_type(struct configfs_dirent
*sd
)
912 return (sd
->s_mode
>> 12) & 15;
915 static int configfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
917 struct dentry
*dentry
= filp
->f_dentry
;
918 struct configfs_dirent
* parent_sd
= dentry
->d_fsdata
;
919 struct configfs_dirent
*cursor
= filp
->private_data
;
920 struct list_head
*p
, *q
= &cursor
->s_sibling
;
926 ino
= dentry
->d_inode
->i_ino
;
927 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
933 ino
= parent_ino(dentry
);
934 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
940 if (filp
->f_pos
== 2) {
942 list_add(q
, &parent_sd
->s_children
);
944 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
945 struct configfs_dirent
*next
;
949 next
= list_entry(p
, struct configfs_dirent
,
951 if (!next
->s_element
)
954 name
= configfs_get_name(next
);
957 ino
= next
->s_dentry
->d_inode
->i_ino
;
959 ino
= iunique(configfs_sb
, 2);
961 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
974 static loff_t
configfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
976 struct dentry
* dentry
= file
->f_dentry
;
978 down(&dentry
->d_inode
->i_sem
);
981 offset
+= file
->f_pos
;
986 up(&file
->f_dentry
->d_inode
->i_sem
);
989 if (offset
!= file
->f_pos
) {
990 file
->f_pos
= offset
;
991 if (file
->f_pos
>= 2) {
992 struct configfs_dirent
*sd
= dentry
->d_fsdata
;
993 struct configfs_dirent
*cursor
= file
->private_data
;
995 loff_t n
= file
->f_pos
- 2;
997 list_del(&cursor
->s_sibling
);
998 p
= sd
->s_children
.next
;
999 while (n
&& p
!= &sd
->s_children
) {
1000 struct configfs_dirent
*next
;
1001 next
= list_entry(p
, struct configfs_dirent
,
1003 if (next
->s_element
)
1007 list_add_tail(&cursor
->s_sibling
, p
);
1010 up(&dentry
->d_inode
->i_sem
);
1014 struct file_operations configfs_dir_operations
= {
1015 .open
= configfs_dir_open
,
1016 .release
= configfs_dir_close
,
1017 .llseek
= configfs_dir_lseek
,
1018 .read
= generic_read_dir
,
1019 .readdir
= configfs_readdir
,
1022 int configfs_register_subsystem(struct configfs_subsystem
*subsys
)
1025 struct config_group
*group
= &subsys
->su_group
;
1027 struct dentry
*dentry
;
1028 struct configfs_dirent
*sd
;
1030 err
= configfs_pin_fs();
1034 if (!group
->cg_item
.ci_name
)
1035 group
->cg_item
.ci_name
= group
->cg_item
.ci_namebuf
;
1037 sd
= configfs_sb
->s_root
->d_fsdata
;
1038 link_group(to_config_group(sd
->s_element
), group
);
1040 down(&configfs_sb
->s_root
->d_inode
->i_sem
);
1042 name
.name
= group
->cg_item
.ci_name
;
1043 name
.len
= strlen(name
.name
);
1044 name
.hash
= full_name_hash(name
.name
, name
.len
);
1047 dentry
= d_alloc(configfs_sb
->s_root
, &name
);
1051 d_add(dentry
, NULL
);
1053 err
= configfs_attach_group(sd
->s_element
, &group
->cg_item
,
1060 up(&configfs_sb
->s_root
->d_inode
->i_sem
);
1065 unlink_group(group
);
1066 configfs_release_fs();
1072 void configfs_unregister_subsystem(struct configfs_subsystem
*subsys
)
1074 struct config_group
*group
= &subsys
->su_group
;
1075 struct dentry
*dentry
= group
->cg_item
.ci_dentry
;
1077 if (dentry
->d_parent
!= configfs_sb
->s_root
) {
1078 printk(KERN_ERR
"configfs: Tried to unregister non-subsystem!\n");
1082 down(&configfs_sb
->s_root
->d_inode
->i_sem
);
1083 down(&dentry
->d_inode
->i_sem
);
1084 if (configfs_detach_prep(dentry
)) {
1085 printk(KERN_ERR
"configfs: Tried to unregister non-empty subsystem!\n");
1087 configfs_detach_group(&group
->cg_item
);
1088 dentry
->d_inode
->i_flags
|= S_DEAD
;
1089 up(&dentry
->d_inode
->i_sem
);
1093 up(&configfs_sb
->s_root
->d_inode
->i_sem
);
1097 unlink_group(group
);
1098 configfs_release_fs();
1101 EXPORT_SYMBOL(configfs_register_subsystem
);
1102 EXPORT_SYMBOL(configfs_unregister_subsystem
);