We don't want to hardcode default groups at subsystem
creation time. We export:
        * configfs_register_group
        * configfs_unregister_group
to allow drivers to programatically create/destroy groups
later, after module init time.

This is needed for IIO configfs support.

Suggested-by: Lars-Peter Clausen <[email protected]>
Signed-off-by: Daniel Baluta <[email protected]>
---
 fs/configfs/dir.c        | 102 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/configfs.h |   8 ++++
 2 files changed, 110 insertions(+)

diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index c81ce7f..bf76795 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -1636,6 +1636,108 @@ const struct file_operations configfs_dir_operations = {
        .iterate        = configfs_readdir,
 };
 
+/**
+ * configfs_alloc_group() - allocate a configfs group
+ * @name:      group name
+ * @item_type: config_item associated with the allocated group
+ *
+ * Dynamically allocates and initialize a group specified by name and
+ * config_item_type. We need kzalloc'ed memory because default_group
+ * is empty.
+ *
+ * Return: allocated config group or ERR_PTR() on error
+ */
+struct config_group *
+configfs_alloc_group(const char *name, struct config_item_type *item_type)
+{
+       struct config_group *group;
+
+       group = kzalloc(sizeof(*group), GFP_KERNEL);
+       if (!group)
+               return ERR_PTR(-ENOMEM);
+       config_group_init_type_name(group, name, item_type);
+
+       return group;
+}
+EXPORT_SYMBOL(configfs_alloc_group);
+
+/**
+ * configfs_free_group() - frees memory for a configfs group
+ * @group:     group to be 'eliberated'
+ */
+void configfs_free_group(struct config_group *group)
+{
+       kfree(group);
+}
+EXPORT_SYMBOL(configfs_free_group);
+
+/**
+ * configfs_register_group - creates a parent-child relation between two groups
+ * @parent_group:      parent group
+ * @group:             child group
+ *
+ * link groups, creates dentry for the child and attaches it to the
+ * parent dentry.
+ *
+ * Return: 0 on success, negative errno code on error
+ */
+int configfs_register_group(struct config_group *parent_group,
+                           struct config_group *group)
+{
+       struct configfs_subsystem *subsys = parent_group->cg_subsys;
+       struct dentry *parent;
+       int ret;
+
+       mutex_lock(&subsys->su_mutex);
+       link_group(parent_group, group);
+       mutex_unlock(&subsys->su_mutex);
+
+       parent = parent_group->cg_item.ci_dentry;
+
+       mutex_lock_nested(&d_inode(parent)->i_mutex, I_MUTEX_PARENT);
+       ret = create_default_group(parent_group, group);
+       if (!ret) {
+               spin_lock(&configfs_dirent_lock);
+               configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
+               spin_unlock(&configfs_dirent_lock);
+       }
+       mutex_unlock(&d_inode(parent)->i_mutex);
+       return ret;
+}
+EXPORT_SYMBOL(configfs_register_group);
+
+/**
+ * configfs_unregister_group() - unregisters a child group from its parent
+ *
+ * @group: parent group to be unregistered
+ *
+ * Undoes condigfs_register_group()
+ */
+void configfs_unregister_group(struct config_group *group)
+{
+       struct configfs_subsystem *subsys = group->cg_subsys;
+       struct dentry *dentry = group->cg_item.ci_dentry;
+       struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
+
+       mutex_lock_nested(&d_inode(parent)->i_mutex, I_MUTEX_PARENT);
+       spin_lock(&configfs_dirent_lock);
+       configfs_detach_prep(dentry, NULL);
+       spin_unlock(&configfs_dirent_lock);
+
+       configfs_detach_group(&group->cg_item);
+       d_inode(dentry)->i_flags |= S_DEAD;
+       dont_mount(dentry);
+       d_delete(dentry);
+       mutex_unlock(&d_inode(parent)->i_mutex);
+
+       dput(dentry);
+
+       mutex_lock(&subsys->su_mutex);
+       unlink_group(group);
+       mutex_unlock(&subsys->su_mutex);
+}
+EXPORT_SYMBOL(configfs_unregister_group);
+
 int configfs_register_subsystem(struct configfs_subsystem *subsys)
 {
        int err;
diff --git a/include/linux/configfs.h b/include/linux/configfs.h
index 63a36e8..c522a5e 100644
--- a/include/linux/configfs.h
+++ b/include/linux/configfs.h
@@ -252,6 +252,14 @@ static inline struct configfs_subsystem 
*to_configfs_subsystem(struct config_gro
 int configfs_register_subsystem(struct configfs_subsystem *subsys);
 void configfs_unregister_subsystem(struct configfs_subsystem *subsys);
 
+struct config_group *
+configfs_alloc_group(const char *name, struct config_item_type *item_type);
+void configfs_free_group(struct config_group *group);
+
+int configfs_register_group(struct config_group *parent_group,
+                           struct config_group *group);
+void configfs_unregister_group(struct config_group *group);
+
 /* These functions can sleep and can alloc with GFP_KERNEL */
 /* WARNING: These cannot be called underneath configfs callbacks!! */
 int configfs_depend_item(struct configfs_subsystem *subsys, struct config_item 
*target);
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to