aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAnand Jain <anand.jain@oracle.com>2025-01-02 02:06:32 +0800
committerDavid Sterba <dsterba@suse.com>2025-01-13 14:53:21 +0100
commit38cae63137d5e13dc3c2ba88c4f393be4a6bf4bb (patch)
tree34a7b0ac6b018a3e8016e5d1ba21472fa3c63c2d
parentbtrfs: sysfs: refactor output formatting in btrfs_read_policy_show() (diff)
downloadwireguard-linux-38cae63137d5e13dc3c2ba88c4f393be4a6bf4bb.tar.xz
wireguard-linux-38cae63137d5e13dc3c2ba88c4f393be4a6bf4bb.zip
btrfs: sysfs: add btrfs_read_policy_to_enum() helper and refactor read policy store
Introduce btrfs_read_policy_to_enum() helper to simplify the conversion of a string read policy to its corresponding enum value. This reduces duplication and improves code clarity in btrfs_read_policy_store(). The parameter is copied locally to allow modification, enabling the separation of the method and its value. This prepares for the addition of more functionality in subsequent patches. Signed-off-by: Anand Jain <anand.jain@oracle.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--fs/btrfs/sysfs.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index ab18b4e59468..78b4af72997b 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -1307,6 +1307,18 @@ BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show);
static const char * const btrfs_read_policy_name[] = { "pid" };
+static int btrfs_read_policy_to_enum(const char *str)
+{
+ char param[32] = { 0 };
+
+ if (!str || strlen(str) == 0)
+ return 0;
+
+ strncpy(param, str, sizeof(param) - 1);
+
+ return sysfs_match_string(btrfs_read_policy_name, param);
+}
+
static ssize_t btrfs_read_policy_show(struct kobject *kobj,
struct kobj_attribute *a, char *buf)
{
@@ -1338,21 +1350,19 @@ static ssize_t btrfs_read_policy_store(struct kobject *kobj,
const char *buf, size_t len)
{
struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
- int i;
+ int index;
- for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
- if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
- if (i != READ_ONCE(fs_devices->read_policy)) {
- WRITE_ONCE(fs_devices->read_policy, i);
- btrfs_info(fs_devices->fs_info,
- "read policy set to '%s'",
- btrfs_read_policy_name[i]);
- }
- return len;
- }
+ index = btrfs_read_policy_to_enum(buf);
+ if (index < 0)
+ return -EINVAL;
+
+ if (index != READ_ONCE(fs_devices->read_policy)) {
+ WRITE_ONCE(fs_devices->read_policy, index);
+ btrfs_info(fs_devices->fs_info, "read policy set to '%s'",
+ btrfs_read_policy_name[index]);
}
- return -EINVAL;
+ return len;
}
BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);