diff mbox series

[v9,3/3] btrfs: create read policy sysfs attribute, pid

Message ID abd366082eeb8b289cd420cb04528a687a250433.1603347462.git.anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show
Series readmirror feature (read_policy sysfs and in-memory only approach) | expand

Commit Message

Anand Jain Oct. 22, 2020, 7:43 a.m. UTC
Add

 /sys/fs/btrfs/UUID/read_policy

attribute so that the read policy for the raid1, raid1c34 and raid10 can
be tuned.

When this attribute is read, it shall show all available policies, with
active policy being with in [ ]. The read_policy attribute can be written
using one of the items listed in there.

For example:
  $cat /sys/fs/btrfs/UUID/read_policy
  [pid]
  $echo pid > /sys/fs/btrfs/UUID/read_policy

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
v9: fix C coding style, static const char*
v5: 
  Title rename: old: btrfs: sysfs, add read_policy attribute
  Uses the btrfs_strmatch() helper (BTRFS_READ_POLICY_NAME_MAX dropped).
  Use the table for the policy names.
  Rename len to ret.
  Use a simple logic to prefix space in btrfs_read_policy_show()
  Reviewed-by: Josef Bacik <josef@toxicpanda.com> dropped.

v4:-
v3: rename [by_pid] to [pid]
v2: v2: check input len before strip and kstrdup

 fs/btrfs/sysfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

Comments

David Sterba Oct. 26, 2020, 5:57 p.m. UTC | #1
On Thu, Oct 22, 2020 at 03:43:37PM +0800, Anand Jain wrote:
> Add
> 
>  /sys/fs/btrfs/UUID/read_policy
> 
> attribute so that the read policy for the raid1, raid1c34 and raid10 can
> be tuned.
> 
> When this attribute is read, it shall show all available policies, with
> active policy being with in [ ]. The read_policy attribute can be written
> using one of the items listed in there.
> 
> For example:
>   $cat /sys/fs/btrfs/UUID/read_policy
>   [pid]
>   $echo pid > /sys/fs/btrfs/UUID/read_policy
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
> v9: fix C coding style, static const char*
> v5: 
>   Title rename: old: btrfs: sysfs, add read_policy attribute
>   Uses the btrfs_strmatch() helper (BTRFS_READ_POLICY_NAME_MAX dropped).
>   Use the table for the policy names.
>   Rename len to ret.
>   Use a simple logic to prefix space in btrfs_read_policy_show()
>   Reviewed-by: Josef Bacik <josef@toxicpanda.com> dropped.
> 
> v4:-
> v3: rename [by_pid] to [pid]
> v2: v2: check input len before strip and kstrdup
> 
>  fs/btrfs/sysfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 49 insertions(+)
> 
> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
> index 5ea262d289c6..e23ae3643527 100644
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -883,6 +883,54 @@ static int btrfs_strmatch(const char *given, const char *golden)
>  	return -EINVAL;
>  }
>  
> +static const char * const btrfs_read_policy_name[] = { "pid" };
> +
> +static ssize_t btrfs_read_policy_show(struct kobject *kobj,
> +				      struct kobj_attribute *a, char *buf)
> +{
> +	int i;
> +	ssize_t ret = 0;
> +	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
> +
> +	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
> +		if (fs_devices->read_policy == i)
> +			ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",

All snprintf have been upgraded to scnprintf as it does have sane
behaviour when the buffer is not large enough.

> +					(ret == 0 ? "" : " "),
> +					btrfs_read_policy_name[i]);
> +		else
> +			ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
> +					(ret == 0 ? "" : " "),
> +					btrfs_read_policy_name[i]);
> +	}
> +
> +	ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
> +
> +	return ret;
> +}
Anand Jain Oct. 28, 2020, 12:37 p.m. UTC | #2
On 27/10/20 1:57 am, David Sterba wrote:
> On Thu, Oct 22, 2020 at 03:43:37PM +0800, Anand Jain wrote:
>> Add
>>
>>   /sys/fs/btrfs/UUID/read_policy
>>
>> attribute so that the read policy for the raid1, raid1c34 and raid10 can
>> be tuned.
>>
>> When this attribute is read, it shall show all available policies, with
>> active policy being with in [ ]. The read_policy attribute can be written
>> using one of the items listed in there.
>>
>> For example:
>>    $cat /sys/fs/btrfs/UUID/read_policy
>>    [pid]
>>    $echo pid > /sys/fs/btrfs/UUID/read_policy
>>
>> Signed-off-by: Anand Jain <anand.jain@oracle.com>
>> ---
>> v9: fix C coding style, static const char*
>> v5:
>>    Title rename: old: btrfs: sysfs, add read_policy attribute
>>    Uses the btrfs_strmatch() helper (BTRFS_READ_POLICY_NAME_MAX dropped).
>>    Use the table for the policy names.
>>    Rename len to ret.
>>    Use a simple logic to prefix space in btrfs_read_policy_show()
>>    Reviewed-by: Josef Bacik <josef@toxicpanda.com> dropped.
>>
>> v4:-
>> v3: rename [by_pid] to [pid]
>> v2: v2: check input len before strip and kstrdup
>>
>>   fs/btrfs/sysfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 49 insertions(+)
>>
>> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
>> index 5ea262d289c6..e23ae3643527 100644
>> --- a/fs/btrfs/sysfs.c
>> +++ b/fs/btrfs/sysfs.c
>> @@ -883,6 +883,54 @@ static int btrfs_strmatch(const char *given, const char *golden)
>>   	return -EINVAL;
>>   }
>>   
>> +static const char * const btrfs_read_policy_name[] = { "pid" };
>> +
>> +static ssize_t btrfs_read_policy_show(struct kobject *kobj,
>> +				      struct kobj_attribute *a, char *buf)
>> +{
>> +	int i;
>> +	ssize_t ret = 0;
>> +	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
>> +
>> +	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
>> +		if (fs_devices->read_policy == i)
>> +			ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
> 
> All snprintf have been upgraded to scnprintf as it does have sane
> behaviour when the buffer is not large enough.

This is taken care in v10.

Thanks, Anand

> 
>> +					(ret == 0 ? "" : " "),
>> +					btrfs_read_policy_name[i]);
>> +		else
>> +			ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
>> +					(ret == 0 ? "" : " "),
>> +					btrfs_read_policy_name[i]);
>> +	}
>> +
>> +	ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
>> +
>> +	return ret;
>> +}
diff mbox series

Patch

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 5ea262d289c6..e23ae3643527 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -883,6 +883,54 @@  static int btrfs_strmatch(const char *given, const char *golden)
 	return -EINVAL;
 }
 
+static const char * const btrfs_read_policy_name[] = { "pid" };
+
+static ssize_t btrfs_read_policy_show(struct kobject *kobj,
+				      struct kobj_attribute *a, char *buf)
+{
+	int i;
+	ssize_t ret = 0;
+	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
+
+	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
+		if (fs_devices->read_policy == i)
+			ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
+					(ret == 0 ? "" : " "),
+					btrfs_read_policy_name[i]);
+		else
+			ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
+					(ret == 0 ? "" : " "),
+					btrfs_read_policy_name[i]);
+	}
+
+	ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
+
+	return ret;
+}
+
+static ssize_t btrfs_read_policy_store(struct kobject *kobj,
+				       struct kobj_attribute *a,
+				       const char *buf, size_t len)
+{
+	int i;
+	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
+
+	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
+		if (btrfs_strmatch(buf, btrfs_read_policy_name[i]) == 0) {
+			if (i != fs_devices->read_policy) {
+				fs_devices->read_policy = i;
+				btrfs_info(fs_devices->fs_info,
+					   "read policy set to '%s'",
+					   btrfs_read_policy_name[i]);
+			}
+			return len;
+		}
+	}
+
+	return -EINVAL;
+}
+BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
+
 static const struct attribute *btrfs_attrs[] = {
 	BTRFS_ATTR_PTR(, label),
 	BTRFS_ATTR_PTR(, nodesize),
@@ -893,6 +941,7 @@  static const struct attribute *btrfs_attrs[] = {
 	BTRFS_ATTR_PTR(, checksum),
 	BTRFS_ATTR_PTR(, exclusive_operation),
 	BTRFS_ATTR_PTR(, generation),
+	BTRFS_ATTR_PTR(, read_policy),
 	NULL,
 };