Refactor out the function ext4_generic_attr_store() to handle the setting
of values of various common types, with no functional changes.
Signed-off-by: Baokun Li <libaokun1@huawei.com>
---
fs/ext4/sysfs.c | 49 ++++++++++++++++++++++++++++---------------------
1 file changed, 28 insertions(+), 21 deletions(-)
diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
index 3671a8aaf4af..834f9a0eb641 100644
--- a/fs/ext4/sysfs.c
+++ b/fs/ext4/sysfs.c
@@ -443,26 +443,22 @@ static ssize_t ext4_attr_show(struct kobject *kobj,
return 0;
}
-static ssize_t ext4_attr_store(struct kobject *kobj,
- struct attribute *attr,
- const char *buf, size_t len)
+static ssize_t ext4_generic_attr_store(struct ext4_attr *a,
+ struct ext4_sb_info *sbi,
+ const char *buf, size_t len)
{
- struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
- s_kobj);
- struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
- void *ptr = calc_ptr(a, sbi);
- unsigned long t;
int ret;
+ unsigned long t;
+ void *ptr = calc_ptr(a, sbi);
+
+ if (!ptr)
+ return 0;
+ ret = kstrtoul(skip_spaces(buf), 0, &t);
+ if (ret)
+ return ret;
switch (a->attr_id) {
- case attr_reserved_clusters:
- return reserved_clusters_store(sbi, buf, len);
case attr_pointer_ui:
- if (!ptr)
- return 0;
- ret = kstrtoul(skip_spaces(buf), 0, &t);
- if (ret)
- return ret;
if (t != (unsigned int)t)
return -EINVAL;
if (a->attr_ptr == ptr_ext4_super_block_offset)
@@ -471,19 +467,30 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
*((unsigned int *) ptr) = t;
return len;
case attr_pointer_ul:
- if (!ptr)
- return 0;
- ret = kstrtoul(skip_spaces(buf), 0, &t);
- if (ret)
- return ret;
*((unsigned long *) ptr) = t;
return len;
+ }
+ return 0;
+}
+
+static ssize_t ext4_attr_store(struct kobject *kobj,
+ struct attribute *attr,
+ const char *buf, size_t len)
+{
+ struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
+ s_kobj);
+ struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
+
+ switch (a->attr_id) {
+ case attr_reserved_clusters:
+ return reserved_clusters_store(sbi, buf, len);
case attr_inode_readahead:
return inode_readahead_blks_store(sbi, buf, len);
case attr_trigger_test_error:
return trigger_test_error(sbi, buf, len);
+ default:
+ return ext4_generic_attr_store(a, sbi, buf, len);
}
- return 0;
}
static void ext4_sb_release(struct kobject *kobj)
--
2.31.1
On Fri 26-01-24 16:57:11, Baokun Li wrote:
> Refactor out the function ext4_generic_attr_store() to handle the setting
> of values of various common types, with no functional changes.
>
> Signed-off-by: Baokun Li <libaokun1@huawei.com>
...
> -static ssize_t ext4_attr_store(struct kobject *kobj,
> - struct attribute *attr,
> - const char *buf, size_t len)
> +static ssize_t ext4_generic_attr_store(struct ext4_attr *a,
> + struct ext4_sb_info *sbi,
> + const char *buf, size_t len)
> {
> - struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
> - s_kobj);
> - struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
> - void *ptr = calc_ptr(a, sbi);
> - unsigned long t;
> int ret;
> + unsigned long t;
> + void *ptr = calc_ptr(a, sbi);
> +
> + if (!ptr)
> + return 0;
> + ret = kstrtoul(skip_spaces(buf), 0, &t);
The refactorization is nice but I'd keep the string to number conversion
inside the switch so that we can distinguish uint and ulong cases.
Honza
> + if (ret)
> + return ret;
>
> switch (a->attr_id) {
> - case attr_reserved_clusters:
> - return reserved_clusters_store(sbi, buf, len);
> case attr_pointer_ui:
> - if (!ptr)
> - return 0;
> - ret = kstrtoul(skip_spaces(buf), 0, &t);
> - if (ret)
> - return ret;
> if (t != (unsigned int)t)
> return -EINVAL;
> if (a->attr_ptr == ptr_ext4_super_block_offset)
> @@ -471,19 +467,30 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
> *((unsigned int *) ptr) = t;
> return len;
> case attr_pointer_ul:
> - if (!ptr)
> - return 0;
> - ret = kstrtoul(skip_spaces(buf), 0, &t);
> - if (ret)
> - return ret;
> *((unsigned long *) ptr) = t;
> return len;
> + }
> + return 0;
> +}
> +
> +static ssize_t ext4_attr_store(struct kobject *kobj,
> + struct attribute *attr,
> + const char *buf, size_t len)
> +{
> + struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
> + s_kobj);
> + struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
> +
> + switch (a->attr_id) {
> + case attr_reserved_clusters:
> + return reserved_clusters_store(sbi, buf, len);
> case attr_inode_readahead:
> return inode_readahead_blks_store(sbi, buf, len);
> case attr_trigger_test_error:
> return trigger_test_error(sbi, buf, len);
> + default:
> + return ext4_generic_attr_store(a, sbi, buf, len);
> }
> - return 0;
> }
>
> static void ext4_sb_release(struct kobject *kobj)
> --
> 2.31.1
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
On 2024/1/26 16:57, Baokun Li wrote:
> Refactor out the function ext4_generic_attr_store() to handle the setting
> of values of various common types, with no functional changes.
>
> Signed-off-by: Baokun Li <libaokun1@huawei.com>
Looks good to me.
Reviewed-by: Zhang Yi <yi.zhang@huawei.com>
> ---
> fs/ext4/sysfs.c | 49 ++++++++++++++++++++++++++++---------------------
> 1 file changed, 28 insertions(+), 21 deletions(-)
>
> diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
> index 3671a8aaf4af..834f9a0eb641 100644
> --- a/fs/ext4/sysfs.c
> +++ b/fs/ext4/sysfs.c
> @@ -443,26 +443,22 @@ static ssize_t ext4_attr_show(struct kobject *kobj,
> return 0;
> }
>
> -static ssize_t ext4_attr_store(struct kobject *kobj,
> - struct attribute *attr,
> - const char *buf, size_t len)
> +static ssize_t ext4_generic_attr_store(struct ext4_attr *a,
> + struct ext4_sb_info *sbi,
> + const char *buf, size_t len)
> {
> - struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
> - s_kobj);
> - struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
> - void *ptr = calc_ptr(a, sbi);
> - unsigned long t;
> int ret;
> + unsigned long t;
> + void *ptr = calc_ptr(a, sbi);
> +
> + if (!ptr)
> + return 0;
> + ret = kstrtoul(skip_spaces(buf), 0, &t);
> + if (ret)
> + return ret;
>
> switch (a->attr_id) {
> - case attr_reserved_clusters:
> - return reserved_clusters_store(sbi, buf, len);
> case attr_pointer_ui:
> - if (!ptr)
> - return 0;
> - ret = kstrtoul(skip_spaces(buf), 0, &t);
> - if (ret)
> - return ret;
> if (t != (unsigned int)t)
> return -EINVAL;
> if (a->attr_ptr == ptr_ext4_super_block_offset)
> @@ -471,19 +467,30 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
> *((unsigned int *) ptr) = t;
> return len;
> case attr_pointer_ul:
> - if (!ptr)
> - return 0;
> - ret = kstrtoul(skip_spaces(buf), 0, &t);
> - if (ret)
> - return ret;
> *((unsigned long *) ptr) = t;
> return len;
> + }
> + return 0;
> +}
> +
> +static ssize_t ext4_attr_store(struct kobject *kobj,
> + struct attribute *attr,
> + const char *buf, size_t len)
> +{
> + struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
> + s_kobj);
> + struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
> +
> + switch (a->attr_id) {
> + case attr_reserved_clusters:
> + return reserved_clusters_store(sbi, buf, len);
> case attr_inode_readahead:
> return inode_readahead_blks_store(sbi, buf, len);
> case attr_trigger_test_error:
> return trigger_test_error(sbi, buf, len);
> + default:
> + return ext4_generic_attr_store(a, sbi, buf, len);
> }
> - return 0;
> }
>
> static void ext4_sb_release(struct kobject *kobj)
>
© 2016 - 2026 Red Hat, Inc.