31 #include <linux/module.h>
32 #include <linux/slab.h>
61 spin_lock(&configfs_dirent_lock);
63 spin_unlock(&configfs_dirent_lock);
73 static int configfs_d_delete(
const struct dentry *dentry)
79 .d_iput = configfs_d_iput,
81 .d_delete = configfs_d_delete,
110 static void configfs_set_dir_dirent_depth(
struct configfs_dirent *parent_sd,
113 int parent_depth = parent_sd->s_depth;
115 if (parent_depth >= 0)
116 sd->s_depth = parent_depth + 1;
120 configfs_adjust_dir_dirent_depth_before_populate(
struct configfs_dirent *sd)
132 if (sd->s_depth == -1)
141 configfs_adjust_dir_dirent_depth_after_populate(
struct configfs_dirent *sd)
153 static void configfs_set_dir_dirent_depth(
struct configfs_dirent *parent_sd,
159 configfs_adjust_dir_dirent_depth_before_populate(
struct configfs_dirent *sd)
164 configfs_adjust_dir_dirent_depth_after_populate(
struct configfs_dirent *sd)
187 configfs_init_dirent_depth(sd);
188 spin_lock(&configfs_dirent_lock);
190 spin_unlock(&configfs_dirent_lock);
195 spin_unlock(&configfs_dirent_lock);
208 const unsigned char *
new)
215 if (
strcmp(existing,
new))
227 struct dentry * dentry,
void * element,
232 sd = configfs_new_dirent(parent_sd, element, type);
239 dentry->
d_fsdata = configfs_get(sd);
244 static int init_dir(
struct inode * inode)
254 static int configfs_init_file(
struct inode * inode)
261 static int init_symlink(
struct inode * inode)
267 static int create_dir(
struct config_item *
k,
struct dentry *
d)
287 spin_lock(&configfs_dirent_lock);
289 spin_unlock(&configfs_dirent_lock);
307 static int configfs_create_dir(
struct config_item *
item,
struct dentry *dentry)
309 int error = create_dir(item, dentry);
329 configfs_dir_set_ready(child_sd);
354 struct dentry *parent,
355 struct dentry *dentry)
377 static void remove_dir(
struct dentry * d)
383 spin_lock(&configfs_dirent_lock);
385 spin_unlock(&configfs_dirent_lock);
406 static void configfs_remove_dir(
struct config_item * item)
408 struct dentry * dentry = dget(item->
ci_dentry);
424 static int configfs_attach_attr(
struct configfs_dirent * sd,
struct dentry * dentry)
429 dentry->
d_fsdata = configfs_get(sd);
443 static struct dentry * configfs_lookup(
struct inode *dir,
444 struct dentry *dentry,
472 err = configfs_attach_attr(sd, dentry);
500 static int configfs_detach_prep(
struct dentry *dentry,
struct mutex **wait_mutex)
510 if (!list_empty(&parent_sd->
s_links))
522 *wait_mutex = &sd->
s_dentry->d_inode->i_mutex;
530 ret = configfs_detach_prep(sd->
s_dentry, wait_mutex);
547 static void configfs_detach_rollback(
struct dentry *dentry)
556 configfs_detach_rollback(sd->
s_dentry);
561 struct dentry * dentry = dget(item->ci_dentry);
568 pr_debug(
"configfs %s: dropping attrs for dir\n",
575 spin_lock(&configfs_dirent_lock);
577 spin_unlock(&configfs_dirent_lock);
588 static int populate_attrs(
struct config_item *item)
610 static int configfs_attach_group(
struct config_item *parent_item,
612 struct dentry *dentry);
613 static void configfs_detach_group(
struct config_item *item);
617 struct dentry * dentry = dget(group->
cg_item.ci_dentry);
618 struct dentry *
child;
659 static int create_default_group(
struct config_group *parent_group,
666 struct dentry *child, *parent = parent_group->
cg_item.ci_dentry;
670 name.name = group->
cg_item.ci_name;
671 name.len =
strlen(name.name);
675 child =
d_alloc(parent, &name);
679 ret = configfs_attach_group(&parent_group->
cg_item,
704 ret = create_default_group(group, new_group);
706 detach_groups(group);
735 config_group_put(group);
751 item->
ci_group = config_group_get(to_config_group(parent_item));
769 unlink_group(new_group);
788 subsys = to_configfs_subsystem(group);
796 link_group(group, new_group);
816 static int configfs_attach_item(
struct config_item *parent_item,
818 struct dentry *dentry)
822 ret = configfs_create_dir(item, dentry);
824 ret = populate_attrs(item);
832 configfs_remove_dir(item);
844 static void configfs_detach_item(
struct config_item *item)
847 configfs_remove_dir(item);
850 static int configfs_attach_group(
struct config_item *parent_item,
852 struct dentry *dentry)
857 ret = configfs_attach_item(parent_item, item, dentry);
872 configfs_adjust_dir_dirent_depth_before_populate(sd);
873 ret = populate_groups(to_config_group(item));
875 configfs_detach_item(item);
879 configfs_adjust_dir_dirent_depth_after_populate(sd);
889 static void configfs_detach_group(
struct config_item *item)
891 detach_groups(to_config_group(item));
892 configfs_detach_item(item);
904 static void client_disconnect_notify(
struct config_item *parent_item,
913 type->
ct_group_ops->disconnect_notify(to_config_group(parent_item),
923 static void client_drop_item(
struct config_item *parent_item,
936 type->
ct_group_ops->drop_item(to_config_group(parent_item),
947 #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
963 configfs_dump_one(sd, level);
969 ret = configfs_dump(child_sd, level + 2);
1037 static int configfs_depend_prep(
struct dentry *
origin,
1052 ret = configfs_depend_prep(child_sd->
s_dentry,
1072 struct dentry *
root;
1080 return PTR_ERR(root);
1109 ret = configfs_depend_prep(subsys_sd->
s_dentry, target);
1111 goto out_unlock_dirent_lock;
1120 out_unlock_dirent_lock:
1164 static int configfs_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
1194 parent_item = configfs_get_config_item(dentry->
d_parent);
1196 subsys = to_config_group(parent_item)->cg_subsys;
1211 if (!subsys->
su_group.cg_item.ci_type) {
1215 subsys_owner = subsys->
su_group.cg_item.ci_type->ct_owner;
1216 if (!try_module_get(subsys_owner)) {
1224 goto out_subsys_put;
1231 group = type->
ct_group_ops->make_group(to_config_group(parent_item), name);
1233 group = ERR_PTR(-
ENOMEM);
1234 if (!IS_ERR(group)) {
1235 link_group(to_config_group(parent_item), group);
1238 ret = PTR_ERR(group);
1240 item = type->
ct_group_ops->make_item(to_config_group(parent_item), name);
1244 link_obj(parent_item, item);
1246 ret = PTR_ERR(item);
1256 goto out_subsys_put;
1271 if (!try_module_get(new_item_owner)) {
1289 spin_lock(&configfs_dirent_lock);
1292 spin_unlock(&configfs_dirent_lock);
1295 ret = configfs_attach_group(parent_item, item, dentry);
1297 ret = configfs_attach_item(parent_item, item, dentry);
1299 spin_lock(&configfs_dirent_lock);
1302 configfs_dir_set_ready(dentry->
d_fsdata);
1303 spin_unlock(&configfs_dirent_lock);
1310 client_disconnect_notify(parent_item, item);
1312 unlink_group(group);
1315 client_drop_item(parent_item, item);
1320 module_put(new_item_owner);
1325 module_put(subsys_owner);
1339 static int configfs_rmdir(
struct inode *dir,
struct dentry *dentry)
1353 parent_item = configfs_get_config_item(dentry->
d_parent);
1354 subsys = to_config_group(parent_item)->cg_subsys;
1364 subsys_owner = subsys->
su_group.cg_item.ci_type->ct_owner;
1371 struct mutex *wait_mutex;
1374 spin_lock(&configfs_dirent_lock);
1382 ret = configfs_detach_prep(dentry, &wait_mutex);
1384 configfs_detach_rollback(dentry);
1386 spin_unlock(&configfs_dirent_lock);
1399 }
while (ret == -
EAGAIN);
1402 item = configfs_get_config_item(dentry);
1408 dead_item_owner = item->
ci_type->ct_owner;
1411 configfs_detach_group(item);
1414 client_disconnect_notify(parent_item, item);
1415 unlink_group(to_config_group(item));
1417 configfs_detach_item(item);
1420 client_disconnect_notify(parent_item, item);
1424 client_drop_item(parent_item, item);
1430 module_put(dead_item_owner);
1431 module_put(subsys_owner);
1437 .mkdir = configfs_mkdir,
1438 .rmdir = configfs_rmdir,
1441 .lookup = configfs_lookup,
1446 .lookup = configfs_lookup,
1451 int configfs_rename_dir(
struct config_item * item,
const char *new_name)
1454 struct dentry * new_dentry, * parent;
1456 if (!
strcmp(config_item_name(item), new_name))
1463 parent = item->parent->dentry;
1468 if (!IS_ERR(new_dentry)) {
1472 d_add(new_dentry,
NULL);
1473 d_move(item->dentry, new_dentry);
1488 static int configfs_dir_open(
struct inode *inode,
struct file *
file)
1490 struct dentry * dentry = file->
f_path.dentry;
1512 static int configfs_dir_close(
struct inode *inode,
struct file *file)
1514 struct dentry * dentry = file->
f_path.dentry;
1518 spin_lock(&configfs_dirent_lock);
1520 spin_unlock(&configfs_dirent_lock);
1523 release_configfs_dirent(cursor);
1531 return (sd->
s_mode >> 12) & 15;
1534 static int configfs_readdir(
struct file * filp,
void *
dirent,
filldir_t filldir)
1536 struct dentry *dentry = filp->
f_path.dentry;
1542 int i = filp->
f_pos;
1547 if (filldir(dirent,
".", 1, i, ino,
DT_DIR) < 0)
1554 if (filldir(dirent,
"..", 2, i, ino,
DT_DIR) < 0)
1560 if (filp->
f_pos == 2) {
1561 spin_lock(&configfs_dirent_lock);
1563 spin_unlock(&configfs_dirent_lock);
1569 struct inode *inode =
NULL;
1592 spin_lock(&configfs_dirent_lock);
1598 spin_unlock(&configfs_dirent_lock);
1602 if (filldir(dirent, name, len, filp->
f_pos, ino,
1606 spin_lock(&configfs_dirent_lock);
1608 spin_unlock(&configfs_dirent_lock);
1616 static loff_t configfs_dir_lseek(
struct file * file, loff_t
offset,
int origin)
1618 struct dentry * dentry = file->
f_path.dentry;
1623 offset += file->
f_pos;
1631 if (offset != file->
f_pos) {
1633 if (file->
f_pos >= 2) {
1637 loff_t
n = file->
f_pos - 2;
1639 spin_lock(&configfs_dirent_lock);
1651 spin_unlock(&configfs_dirent_lock);
1659 .open = configfs_dir_open,
1660 .release = configfs_dir_close,
1661 .llseek = configfs_dir_lseek,
1663 .readdir = configfs_readdir,
1671 struct dentry *dentry;
1672 struct dentry *
root;
1677 return PTR_ERR(root);
1683 link_group(to_config_group(sd->
s_element), group);
1692 dentry =
d_alloc(root, &name);
1694 d_add(dentry,
NULL);
1704 configfs_dir_set_ready(dentry->
d_fsdata);
1712 unlink_group(group);
1722 struct dentry *dentry = group->
cg_item.ci_dentry;
1723 struct dentry *
root = dentry->
d_sb->s_root;
1726 printk(
KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1735 if (configfs_detach_prep(dentry,
NULL)) {
1736 printk(
KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1740 configfs_detach_group(&group->
cg_item);
1751 unlink_group(group);