2022-09-09 06:44:22

by Shang XiaoJing

[permalink] [raw]
Subject: [PATCH -next 0/3] configfs: Clean up by adding helpers

Some clean up in dir.c, inode.c, and symlink.c.

Shang XiaoJing (3):
configfs: Add configfs_remove_drop_dirent helper
configfs: Add group_to_dentry helper
configfs: Add subsys_to_item helper

fs/configfs/configfs_internal.h | 1 +
fs/configfs/dir.c | 61 +++++++++++++++++++++------------
fs/configfs/inode.c | 6 +---
fs/configfs/symlink.c | 6 +---
4 files changed, 42 insertions(+), 32 deletions(-)

--
2.17.1


2022-09-09 06:57:26

by Shang XiaoJing

[permalink] [raw]
Subject: [PATCH -next 1/3] configfs: Add configfs_remove_drop_dirent helper

Wrap repeated code in helper function configf_remove_drop_dirent, which
remove dirent and drop dentry.

Signed-off-by: Shang XiaoJing <[email protected]>
---
fs/configfs/configfs_internal.h | 1 +
fs/configfs/dir.c | 16 +++++++++++-----
fs/configfs/inode.c | 6 +-----
fs/configfs/symlink.c | 6 +-----
4 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/fs/configfs/configfs_internal.h b/fs/configfs/configfs_internal.h
index c0395363eab9..4905b6d35dda 100644
--- a/fs/configfs/configfs_internal.h
+++ b/fs/configfs/configfs_internal.h
@@ -76,6 +76,7 @@ extern int configfs_dirent_is_ready(struct configfs_dirent *);
extern void configfs_hash_and_remove(struct dentry * dir, const char * name);

extern const unsigned char * configfs_get_name(struct configfs_dirent *sd);
+extern void configfs_remove_drop_dirent(struct configfs_dirent *sd, struct dentry *dentry);
extern void configfs_drop_dentry(struct configfs_dirent *sd, struct dentry *parent);
extern int configfs_setattr(struct user_namespace *mnt_userns,
struct dentry *dentry, struct iattr *iattr);
diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index d1f9d2632202..4e086635b3ed 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -257,6 +257,16 @@ int configfs_make_dirent(struct configfs_dirent * parent_sd,
return 0;
}

+void configfs_remove_drop_dirent(struct configfs_dirent *sd,
+ struct dentry *dentry)
+{
+ spin_lock(&configfs_dirent_lock);
+ list_del_init(&sd->s_sibling);
+ spin_unlock(&configfs_dirent_lock);
+ configfs_drop_dentry(sd, dentry);
+ configfs_put(sd);
+}
+
static void configfs_remove_dirent(struct dentry *dentry)
{
struct configfs_dirent *sd = dentry->d_fsdata;
@@ -562,11 +572,7 @@ static void detach_attrs(struct config_item * item)
list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
continue;
- spin_lock(&configfs_dirent_lock);
- list_del_init(&sd->s_sibling);
- spin_unlock(&configfs_dirent_lock);
- configfs_drop_dentry(sd, dentry);
- configfs_put(sd);
+ configfs_remove_drop_dirent(sd, dentry);
}

/**
diff --git a/fs/configfs/inode.c b/fs/configfs/inode.c
index b601610e9907..c91fe11e4450 100644
--- a/fs/configfs/inode.c
+++ b/fs/configfs/inode.c
@@ -232,11 +232,7 @@ void configfs_hash_and_remove(struct dentry * dir, const char * name)
if (!sd->s_element)
continue;
if (!strcmp(configfs_get_name(sd), name)) {
- spin_lock(&configfs_dirent_lock);
- list_del_init(&sd->s_sibling);
- spin_unlock(&configfs_dirent_lock);
- configfs_drop_dentry(sd, dir);
- configfs_put(sd);
+ configfs_remove_drop_dirent(sd, dir);
break;
}
}
diff --git a/fs/configfs/symlink.c b/fs/configfs/symlink.c
index 0623c3edcfb9..529d204b807f 100644
--- a/fs/configfs/symlink.c
+++ b/fs/configfs/symlink.c
@@ -233,12 +233,8 @@ int configfs_unlink(struct inode *dir, struct dentry *dentry)
parent_item = configfs_get_config_item(dentry->d_parent);
type = parent_item->ci_type;

- spin_lock(&configfs_dirent_lock);
- list_del_init(&sd->s_sibling);
- spin_unlock(&configfs_dirent_lock);
- configfs_drop_dentry(sd, dentry->d_parent);
+ configfs_remove_drop_dirent(sd, dentry->d_parent);
dput(dentry);
- configfs_put(sd);

/*
* drop_link() must be called before
--
2.17.1

2022-09-09 07:33:36

by Shang XiaoJing

[permalink] [raw]
Subject: [PATCH -next 2/3] configfs: Add group_to_dentry helper

Add group_to_dentry helper function for more clear, which get the
corresponding dentry of the config_group.

Space is deleted due to checkpatch.pl.

Signed-off-by: Shang XiaoJing <[email protected]>
---
fs/configfs/dir.c | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index 4e086635b3ed..b5614b66add2 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -611,6 +611,11 @@ static int populate_attrs(struct config_item *item)
return error;
}

+static inline struct dentry *group_to_dentry(struct config_group *group)
+{
+ return group->cg_item.ci_dentry;
+}
+
static int configfs_attach_group(struct config_item *parent_item,
struct config_item *item,
struct dentry *dentry,
@@ -619,7 +624,7 @@ static void configfs_detach_group(struct config_item *item);

static void detach_groups(struct config_group *group)
{
- struct dentry * dentry = dget(group->cg_item.ci_dentry);
+ struct dentry *dentry = dget(group_to_dentry(group));
struct dentry *child;
struct configfs_dirent *parent_sd;
struct configfs_dirent *sd, *tmp;
@@ -668,7 +673,7 @@ static int create_default_group(struct config_group *parent_group,
int ret;
struct configfs_dirent *sd;
/* We trust the caller holds a reference to parent */
- struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
+ struct dentry *child, *parent = group_to_dentry(parent_group);

if (!group->cg_item.ci_name)
group->cg_item.ci_name = group->cg_item.ci_namebuf;
@@ -1225,14 +1230,14 @@ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
* additional locking to prevent other subsystem from being
* unregistered
*/
- inode_lock(d_inode(root->cg_item.ci_dentry));
+ inode_lock(d_inode(group_to_dentry(root)));

/*
* As we are trying to depend item from other subsystem
* we have to check if this subsystem is still registered
*/
subsys_sd = configfs_find_subsys_dentry(
- root->cg_item.ci_dentry->d_fsdata,
+ group_to_dentry(root)->d_fsdata,
&target_subsys->su_group.cg_item);
if (!subsys_sd)
goto out_root_unlock;
@@ -1249,7 +1254,7 @@ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
* We were called from subsystem other than our target so we
* took some locks so now it's time to release them
*/
- inode_unlock(d_inode(root->cg_item.ci_dentry));
+ inode_unlock(d_inode(group_to_dentry(root)));

return ret;
}
@@ -1745,7 +1750,7 @@ int configfs_register_group(struct config_group *parent_group,
link_group(parent_group, group);
mutex_unlock(&subsys->su_mutex);

- parent = parent_group->cg_item.ci_dentry;
+ parent = group_to_dentry(parent_group);

inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
ret = create_default_group(parent_group, group, frag);
@@ -1753,7 +1758,7 @@ int configfs_register_group(struct config_group *parent_group,
goto err_out;

spin_lock(&configfs_dirent_lock);
- configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
+ configfs_dir_set_ready(group_to_dentry(group)->d_fsdata);
spin_unlock(&configfs_dirent_lock);
inode_unlock(d_inode(parent));
put_fragment(frag);
@@ -1777,7 +1782,7 @@ EXPORT_SYMBOL(configfs_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 *dentry = group_to_dentry(group);
struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
struct configfs_dirent *sd = dentry->d_fsdata;
struct configfs_fragment *frag = sd->s_frag;
@@ -1913,7 +1918,7 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
{
struct config_group *group = &subsys->su_group;
- struct dentry *dentry = group->cg_item.ci_dentry;
+ struct dentry *dentry = group_to_dentry(group);
struct dentry *root = dentry->d_sb->s_root;
struct configfs_dirent *sd = dentry->d_fsdata;
struct configfs_fragment *frag = sd->s_frag;
--
2.17.1

2022-09-09 07:34:01

by Shang XiaoJing

[permalink] [raw]
Subject: [PATCH -next 3/3] configfs: Add subsys_to_item helper

Add helper function subsys_to_item, which get the corresponding
config_item of the configfs_subsystem.

Signed-off-by: Shang XiaoJing <[email protected]>
---
fs/configfs/dir.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index b5614b66add2..e4386eab3428 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -281,7 +281,7 @@ static void configfs_remove_dirent(struct dentry *dentry)

/**
* configfs_create_dir - create a directory for an config_item.
- * @item: config_itemwe're creating directory for.
+ * @item: config_item we're creating directory for.
* @dentry: config_item's dentry.
* @frag: config_item's fragment.
*
@@ -616,6 +616,12 @@ static inline struct dentry *group_to_dentry(struct config_group *group)
return group->cg_item.ci_dentry;
}

+static inline
+struct config_item *subsys_to_item(struct configfs_subsystem *subsys)
+{
+ return &subsys->su_group.cg_item;
+}
+
static int configfs_attach_group(struct config_item *parent_item,
struct config_item *item,
struct dentry *dentry,
@@ -1119,7 +1125,7 @@ int configfs_depend_item(struct configfs_subsystem *subsys,
{
int ret;
struct configfs_dirent *subsys_sd;
- struct config_item *s_item = &subsys->su_group.cg_item;
+ struct config_item *s_item = subsys_to_item(subsys);
struct dentry *root;

/*
@@ -1238,11 +1244,11 @@ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
*/
subsys_sd = configfs_find_subsys_dentry(
group_to_dentry(root)->d_fsdata,
- &target_subsys->su_group.cg_item);
+ subsys_to_item(target_subsys));
if (!subsys_sd)
goto out_root_unlock;
} else {
- subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
+ subsys_sd = subsys_to_item(target_subsys)->ci_dentry->d_fsdata;
}

/* Now we can execute core of depend item */
@@ -1315,11 +1321,11 @@ static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
* being created. We don't want to safely pin the new item but
* fail to pin the subsystem it sits under.
*/
- if (!subsys->su_group.cg_item.ci_type) {
+ if (!subsys_to_item(subsys)->ci_type) {
ret = -EINVAL;
goto out_put;
}
- subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
+ subsys_owner = subsys_to_item(subsys)->ci_type->ct_owner;
if (!try_module_get(subsys_owner)) {
ret = -EINVAL;
goto out_put;
@@ -1469,8 +1475,8 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
}

/* configfs_mkdir() shouldn't have allowed this */
- BUG_ON(!subsys->su_group.cg_item.ci_type);
- subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
+ BUG_ON(!subsys_to_item(subsys)->ci_type);
+ subsys_owner = subsys_to_item(subsys)->ci_type->ct_owner;

/*
* Ensure that no racing symlink() will make detach_prep() fail while
--
2.17.1