btrfs: sysfs: use sysfs_streq for string matching
David Sterba
maintainerPATCH 1/1 btrfs: sysfs: use sysfs_streq for string matching
We have own string matching helper that duplicates what sysfs_streq does, with a slight difference that it skips initial whitespace. So far this is used for the drive allocation policy. The initial whitespace of written sysfs values should be rather discouraged and we should use a standard helper.
--- fs/btrfs/sysfs.c | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-)
fs/btrfs/sysfs.c +1 -20
@@ -1150,25 +1150,6 @@ static ssize_t btrfs_generation_show(struct kobject *kobj, } BTRFS_ATTR(, generation, btrfs_generation_show); -/* -* Look for an exact string @string in @buffer with possible leading or -* trailing whitespace -*/ -static bool strmatch (const char *buffer,const char *string) -{ -const size_t len =strlen (string); - -/* Skip leading whitespace */ - buffer =skip_spaces (buffer); - -/* Match entire string, check if the rest is whitespace or empty */ -if (strncmp (string, buffer, len) ==0 && -strlen (skip_spaces (buffer + len)) ==0 ) -return true; - -return false; -} -static const char *const btrfs_read_policy_name[] = {"pid" }; static ssize_t btrfs_read_policy_show(struct kobject *kobj,
@@ -1202,7 +1183,7 @@ static ssize_t btrfs_read_policy_store(struct kobject *kobj,int i;for (i =0 ; i <BTRFS_NR_READ_POLICY ; i++) { -if (strmatch (buf, btrfs_read_policy_name[i])) { +if (sysfs_streq (buf, btrfs_read_policy_name[i])) {if (i != fs_devices->read_policy) { fs_devices->read_policy = i; btrfs_info(fs_devices->fs_info,
Signature · 1 line
2.36.1
Looks good,
David,
We have user API breakage. Are you ok with that?
Before:
$ echo " pid" > ./read_policy
After:
$ echo " pid" > ./read_policy
-bash: echo: write error: Invalid argument
$ echo "pid " > ./read_policy
-bash: echo: write error: Invalid argument-Anand
Quoted 50 lines · David Sterba
We have own string matching helper that duplicates what
sysfs_streqdoes, with a slight difference that it skips initial whitespace. So far this is used for the drive allocation policy. The initial whitespace of written sysfs values should be rather discouraged and we should use a standard helper.Signed-off-by David Sterba <[email protected]>--- fs/btrfs/sysfs.c | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-)fs/btrfs/sysfs.c +1
-20@@ -1150,25 +1150,6 @@ static ssize_t btrfs_generation_show(struct kobject *kobj, } BTRFS_ATTR(, generation, btrfs_generation_show); -/* -* Look for an exact string @string in @buffer with possible leading or -* trailing whitespace -*/ -static bool strmatch (const char *buffer,const char *string) -{ -const size_t len =strlen (string); - -/* Skip leading whitespace */ - buffer =skip_spaces (buffer); - -/* Match entire string, check if the rest is whitespace or empty */ -if (strncmp (string, buffer, len) ==0 && -strlen (skip_spaces (buffer + len)) ==0 ) -return true; - -return false; -} -static const char *const btrfs_read_policy_name[] = {"pid" }; static ssize_t btrfs_read_policy_show(struct kobject *kobj,@@ -1202,7 +1183,7 @@ static ssize_t btrfs_read_policy_store(struct kobject *kobj,int i;for (i =0 ; i <BTRFS_NR_READ_POLICY ; i++) { -if (strmatch (buf, btrfs_read_policy_name[i])) { +if (sysfs_streq (buf, btrfs_read_policy_name[i])) {if (i != fs_devices->read_policy) { fs_devices->read_policy = i; btrfs_info(fs_devices->fs_info,
Quoted 4 lines · Anand Jain
David,
We have user API breakage. Are you ok with that?
So technically it's change of behaviour, OTOH allowing initial/trailing whitespace in the values is not common for other sysfs values. What is accepted is trailing "\n" and that's what the helper provides. I'm not sure why we allowed the extende format for the read policy. It should be safe to do the change now as there are no other values so nobody was writing to the file anyway.
Quoted 9 lines
Before:
$ echo " pid" > ./read_policyAfter:
$ echo " pid" > ./read_policy -bash: echo: write error: Invalid argument $ echo "pid " > ./read_policy -bash: echo: write error: Invalid argument
Yeah, do you have a usecase where the " " must be accepted? It may break tests but I'd rather make it slightly more strict and cosistent.
Quoted 17 lines · David Sterba
Quoted 4 lines · Anand Jain
David,
We have user API breakage. Are you ok with that?
So technically it's change of behaviour, OTOH allowing initial/trailing whitespace in the values is not common for other sysfs values. What is accepted is trailing "\n" and that's what the helper provides. I'm not sure why we allowed the extende format for the read policy. It should be safe to do the change now as there are no other values so nobody was writing to the file anyway.
Quoted 4 lines
Before:
$ echo " pid" > ./read_policy
Quoted 7 lines
Quoted 5 lines
After:
$ echo " pid" > ./read_policy -bash: echo: write error: Invalid argument $ echo "pid " > ./read_policy -bash: echo: write error: Invalid argumentYeah, do you have a usecase where the " " must be accepted?
Nope.
Quoted 2 lines
It may break
tests but I'd rather make it slightly more strict and cosistent.
It is fine with me. The use of sysfs_streq() makes the interface consistent across other sysfs knobs.
Quoted 7 lines · David Sterba
We have own string matching helper that duplicates what
sysfs_streqdoes, with a slight difference that it skips initial whitespace. So far this is used for the drive allocation policy. The initial whitespace of written sysfs values should be rather discouraged and we should use a standard helper.Signed-off-by David Sterba <[email protected]>
Quoted 43 lines
--- fs/btrfs/sysfs.c | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-)fs/btrfs/sysfs.c +1
-20@@ -1150,25 +1150,6 @@ static ssize_t btrfs_generation_show(struct kobject *kobj, } BTRFS_ATTR(, generation, btrfs_generation_show); -/* -* Look for an exact string @string in @buffer with possible leading or -* trailing whitespace -*/ -static bool strmatch (const char *buffer,const char *string) -{ -const size_t len =strlen (string); - -/* Skip leading whitespace */ - buffer =skip_spaces (buffer); - -/* Match entire string, check if the rest is whitespace or empty */ -if (strncmp (string, buffer, len) ==0 && -strlen (skip_spaces (buffer + len)) ==0 ) -return true; - -return false; -} -static const char *const btrfs_read_policy_name[] = {"pid" }; static ssize_t btrfs_read_policy_show(struct kobject *kobj,@@ -1202,7 +1183,7 @@ static ssize_t btrfs_read_policy_store(struct kobject *kobj,int i;for (i =0 ; i <BTRFS_NR_READ_POLICY ; i++) { -if (strmatch (buf, btrfs_read_policy_name[i])) { +if (sysfs_streq (buf, btrfs_read_policy_name[i])) {if (i != fs_devices->read_policy) { fs_devices->read_policy = i; btrfs_info(fs_devices->fs_info,
Quoted 7 lines · David Sterba
We have own string matching helper that duplicates what
sysfs_streqdoes, with a slight difference that it skips initial whitespace. So far this is used for the drive allocation policy. The initial whitespace of written sysfs values should be rather discouraged and we should use a standard helper.Signed-off-by David Sterba <[email protected]>
Added to misc-next.