btrfs: sysfs: handle value associated with read balancing policy
authorAnand Jain <anand.jain@oracle.com>
Wed, 1 Jan 2025 18:06:33 +0000 (02:06 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 13 Jan 2025 13:53:21 +0000 (14:53 +0100)
Enable specifying additional configuration values along the RAID1
balancing read policy in a single input string.

Update btrfs_read_policy_to_enum() to parse and handle a value
associated with the policy in the format "policy:value", the value part
if present is converted to 64-bit integer.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/sysfs.c

index 78b4af72997b38abc90b429cc22965f0456aaa33..2880407d0dd30be6730614bd65da6a48a7b402ef 100644 (file)
@@ -1307,15 +1307,34 @@ 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)
+static int btrfs_read_policy_to_enum(const char *str, s64 *value_ret)
 {
        char param[32] = { 0 };
+       char __maybe_unused *value_str;
 
        if (!str || strlen(str) == 0)
                return 0;
 
        strncpy(param, str, sizeof(param) - 1);
 
+#ifdef CONFIG_BTRFS_EXPERIMENTAL
+       /* Separate value from input in policy:value format. */
+       value_str = strchr(param, ':');
+       if (value_str) {
+               int ret;
+
+               *value_str = 0;
+               value_str++;
+               if (!value_ret)
+                       return -EINVAL;
+               ret = kstrtos64(value_str, 10, value_ret);
+               if (ret)
+                       return -EINVAL;
+               if (*value_ret < 0)
+                       return -ERANGE;
+       }
+#endif
+
        return sysfs_match_string(btrfs_read_policy_name, param);
 }
 
@@ -1351,8 +1370,9 @@ static ssize_t btrfs_read_policy_store(struct kobject *kobj,
 {
        struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
        int index;
+       s64 value = -1;
 
-       index = btrfs_read_policy_to_enum(buf);
+       index = btrfs_read_policy_to_enum(buf, &value);
        if (index < 0)
                return -EINVAL;