Skip to contentSkip to conversation

libbcachefs: add support for true/false & yes/no in bool-type options

PATCH In review Integral 4 participants 26 messages Jul 18, 2024 – Nov 7, 2024 VFS

lore
Share & tools
lore

Integral

author
#0 lore raw

PATCH 1/1 libbcachefs: add support for true/false & yes/no in bool-type options

This is a resend of my previous patch with code style optimized as I forgot to add Kent Overstreet in "To". The previous patch is
(https://lore.kernel.org/linux-bcachefs/[email protected]/T/#u).

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
 libbcachefs/opts.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
libbcachefs/opts.c +9 -7
index 0770aebe..e6ebc4c1 100644
@@ -332,17 +332,19 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			if (!strcmp(val, "1") || !strcmp(val, "true") || !strcmp(val, "yes")) {
+				*res = 1;
+			} else if (!strcmp(val, "0") || !strcmp(val, "false") || !strcmp(val, "no")) {
+				*res = 0;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return -BCH_ERR_option_not_bool;
+			}
 		} else {
-			ret = 0;
 			*res = 1;
 		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
 		break;
 	case BCH_OPT_UINT:
 		if (!val) {
Signature · 1 line
2.45.2
#1 lore raw
Quoted 10 lines · Integral

This is a resend of my previous patch with code style optimized as I forgot to add Kent Overstreet in "To". The previous patch is
(https://lore.kernel.org/linux-bcachefs/[email protected]/T/#u).

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
  libbcachefs/opts.c | 16 +++++++++-------

Generating diff on bcachefs tree (not bcachefs-tools)?

libbcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 335

Quoted 12 lines
  1 file changed, 9 insertions(+), 7 deletions(-)
libbcachefs/opts.c +1 -1
index 0770aebe..e6ebc4c1 100644
@@ -332,17 +332,19 @@ int bch2_opt_parse(struct bch_fs *c,
  	switch (opt->type) {
  	case BCH_OPT_BOOL:
  		if (val) {
-			ret = kstrtou64(val, 10, res);
+			if (!strcmp(val, "1") || !strcmp(val, "true") || !strcmp(val, "yes")) {

Should we use the strcasecmp replace the strcmp?

Thanks,
Hongbo

Quoted 21 lines
libbcachefs/opts.c +8 -6
+				*res = 1;
+			} else if (!strcmp(val, "0") || !strcmp(val, "false") || !strcmp(val, "no")) {
+				*res = 0;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return -BCH_ERR_option_not_bool;
+			}
  		} else {
-			ret = 0;
  			*res = 1;
  		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
  		break;
  	case BCH_OPT_UINT:
  		if (!val) {
#2 lore raw
Quoted 2 lines · Hongbo Li

+            } else if (!strcmp(val, "0") || !strcmp(val, "false") || !strcmp(val, "no")) {

How about also parse on/off into bool type.

Thanks,
Hongbo

author
#3 lore raw

PATCH 1/1 Add support for true/false, yes/no & on/off in bool-type options

Here is the new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false, yes/no & on/off for these options.

---
 fs/bcachefs/opts.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +11 -7
index 0770aebef6d8..0e4cd6984eda 100644
@@ -332,17 +332,21 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			if (!strcasecmp(val, "1") || !strcasecmp(val, "true") ||
+				!strcasecmp(val, "yes") || !strcasecmp(val, "on")) {
+				*res = 1;
+			} else if (!strcasecmp(val, "0") || !strcasecmp(val, "false") ||
+				!strcasecmp(val, "no") || !strcasecmp(val, "off")) {
+				*res = 0;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return -BCH_ERR_option_not_bool;
+			}
 		} else {
-			ret = 0;
 			*res = 1;
 		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
 		break;
 	case BCH_OPT_UINT:
 		if (!val) {
Signature · 1 line
2.45.2
#4 lore raw

Re: [PATCH] Add support for true/false, yes/no & on/off in bool-type options

Quoted 21 lines · Integral

Here is the new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false, yes/no & on/off for these options.

---
 fs/bcachefs/opts.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +1 -1
index 0770aebef6d8..0e4cd6984eda 100644
@@ -332,17 +332,21 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			if (!strcasecmp(val, "1") || !strcasecmp(val, "true") ||

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 345

Quoted 10 lines
fs/bcachefs/opts.c +10 -0
+				!strcasecmp(val, "yes") || !strcasecmp(val, "on")) {
+				*res = 1;
+			} else if (!strcasecmp(val, "0") || !strcasecmp(val, "false") ||
+				!strcasecmp(val, "no") || !strcasecmp(val, "off")) {
+				*res = 0;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return -BCH_ERR_option_not_bool;
+			}

There _should_ be something more standard in the kernel for parsing bool options (commandline options, module options) - please hunt around for that and see if it works; report back on what you find.

We want to be consistent on how we parse bools throughout the kernel.

#5 lore raw

Re: [PATCH] Add support for true/false, yes/no & on/off in bool-type options

Quoted 12 lines · Kent Overstreet
Quoted 11 lines · Integral

Here is the new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false, yes/no & on/off for these options.

---
  fs/bcachefs/opts.c | 18 +++++++++++-------
  1 file changed, 11 insertions(+), 7 deletions(-)
Quoted 14 lines
Quoted 5 lines
fs/bcachefs/opts.c +0 -0
index 0770aebef6d8..0e4cd6984eda 100644

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 339

Quoted 9 lines
fs/bcachefs/opts.c +5 -1
@@ -332,17 +332,21 @@ int bch2_opt_parse(struct bch_fs *c,
  	switch (opt->type) {
  	case BCH_OPT_BOOL:
  		if (val) {
-			ret = kstrtou64(val, 10, res);
+			if (!strcasecmp(val, "1") || !strcasecmp(val, "true") ||
+				!strcasecmp(val, "yes") || !strcasecmp(val, "on")) {
+				*res = 1;
+			} else if (!strcasecmp(val, "0") || !strcasecmp(val, "false") ||
+				!strcasecmp(val, "no") || !strcasecmp(val, "off")) {

Sorry, may be I was wrong.

Quoted 10 lines

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 345

Quoted 6 lines
fs/bcachefs/opts.c +6 -0
+				*res = 0;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return -BCH_ERR_option_not_bool;
+			}

There _should_ be something more standard in the kernel for parsing bool options (commandline options, module options) - please hunt around for that and see if it works; report back on what you find.

I find in Documentation/filesystems/mount_api.rst, it give an explanation.

```
Note that if the value is of fs_param_is_bool type, fs_parse() will try 


to match any string value against "0", "1", "no", "yes", "false", "true".
```

But this has been handled in fs_param_is_bool with macro fsparam_bool. Many file systems do this in fs_parse. I think the original patch seems ok.

Thanks,
Hongbo

Quoted 2 lines

We want to be consistent on how we parse bools throughout the kernel.

author
#6 lore raw

Re: [PATCH] Add support for true/false, yes/no & on/off in bool-type options

Therefore, if my original patch is okay, could you merge it?

Thanks,

Integral

author
#7 lore raw

Re: [PATCH] Add support for true/false, yes/no & on/off in bool-type options

In reply to Hongbo Li

Therefore, if my original patch is okay, could you merge it?

Thanks,

Integral

Quoted 57 lines · Hongbo Li
Quoted 12 lines · Kent Overstreet
Quoted 11 lines · Integral

Here is the new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false, yes/no & on/off for these options.

---
  fs/bcachefs/opts.c | 18 +++++++++++-------

  1 file changed, 11 insertions(+), 7 deletions(-)
Quoted 15 lines
Quoted 7 lines
fs/bcachefs/opts.c +0 -0
index 0770aebef6d8..0e4cd6984eda 100644
@@ -332,17 +332,21 @@ int bch2_opt_parse(struct bch_fs *c,
       switch (opt->type) {

      case BCH_OPT_BOOL:

Quoted 8 lines

          if (val) {
-            ret = kstrtou64(val, 10, res);
+            if (!strcasecmp(val, "1") || !strcasecmp(val, "true") || +                !strcasecmp(val, "yes") || !strcasecmp(val, "on")) { +                *res = 1;
+            } else if (!strcasecmp(val, "0") || !strcasecmp(val, "false") ||
+                !strcasecmp(val, "no") || !strcasecmp(val, "off")) {

Sorry, may be I was wrong.

Quoted 11 lines
Quoted 7 lines

+                *res = 0;
+            } else {
+                if (err)
+                    prt_printf(err, "%s: must be bool", opt->attr.name);
+                return -BCH_ERR_option_not_bool; +            }

There _should_ be something more standard in the kernel for parsing bool options (commandline options, module options) - please hunt around for that and see if it works; report back on what you find.

I find in Documentation/filesystems/mount_api.rst, it give an explanation.

```
Note that if the value is of fs_param_is_bool type, fs_parse() will try

to match any string value against "0", "1", "no", "yes", "false", "true".
```

But this has been handled in fs_param_is_bool with macro fsparam_bool. Many file systems do this in fs_parse. I think the original patch seems ok.

Thanks,
Hongbo

Quoted 2 lines

We want to be consistent on how we parse bools throughout the kernel.

#8 lore raw

Re: [PATCH] Add support for true/false, yes/no & on/off in bool-type options

Quoted 3 lines · Integral

Therefore, if my original patch is okay, could you merge it?

I took a look just now, I think you can use lookup_constant.

Quoted 64 lines

Thanks,

Integral

Quoted 55 lines · Hongbo Li
Quoted 12 lines · Kent Overstreet
Quoted 11 lines · Integral

Here is the new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false, yes/no & on/off for these options.

---
  fs/bcachefs/opts.c | 18 +++++++++++-------
  1 file changed, 11 insertions(+), 7 deletions(-)
Quoted 14 lines
Quoted 5 lines
fs/bcachefs/opts.c +0 -0
index 0770aebef6d8..0e4cd6984eda 100644

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 339

Quoted 9 lines
fs/bcachefs/opts.c +5 -1
@@ -332,17 +332,21 @@ int bch2_opt_parse(struct bch_fs *c,
      switch (opt->type) {
      case BCH_OPT_BOOL:
          if (val) {
-            ret = kstrtou64(val, 10, res);
+            if (!strcasecmp(val, "1") || !strcasecmp(val, "true") ||
+                !strcasecmp(val, "yes") || !strcasecmp(val, "on")) {
+                *res = 1;
+            } else if (!strcasecmp(val, "0") || !strcasecmp(val, "false") ||
+                !strcasecmp(val, "no") || !strcasecmp(val, "off")) {

Sorry, may be I was wrong.

Quoted 10 lines

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 345

Quoted 6 lines
fs/bcachefs/opts.c +6 -0
+                *res = 0;
+            } else {
+                if (err)
+                    prt_printf(err, "%s: must be bool", opt->attr.name);
+                return -BCH_ERR_option_not_bool;
+            }

There _should_ be something more standard in the kernel for parsing bool options (commandline options, module options) - please hunt around for that and see if it works; report back on what you find.

I find in Documentation/filesystems/mount_api.rst, it give an explanation.

```
Note that if the value is of fs_param_is_bool type, fs_parse() will try

to match any string value against "0", "1", "no", "yes", "false", "true".
```

But this has been handled in fs_param_is_bool with macro fsparam_bool. Many file systems do this in fs_parse. I think the original patch seems ok.

Thanks,
Hongbo

Quoted 2 lines

We want to be consistent on how we parse bools throughout the kernel.

author
#9 lore raw

PATCH v2 1/1 bcachefs: add support for true/false & yes/no in bool-type options

Here is my new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
 fs/bcachefs/opts.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +21 -7
index 232be8a44051..659536d00cb9 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
 #include "bcachefs.h"
 #include "compress.h"
@@ -322,6 +323,18 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
 	return 0;
 }
 
+static const struct constant_table bch2_opt_bool_names[] = {
+	{ "0",		false },
+	{ "false",	false },
+	{ "no",		false },
+	{ "off",	false },
+	{ "1",		true },
+	{ "true",	true },
+	{ "yes",	true },
+	{ "on",		true },
+	{ },
+};
+
 int bch2_opt_parse(struct bch_fs *c,
 		   const struct bch_option *opt,
 		   const char *val, u64 *res,
@@ -332,17 +345,18 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bch2_opt_bool_names, val, BCH_ERR_option_not_bool);
+			if (ret != BCH_ERR_option_not_bool) {
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
 		} else {
-			ret = 0;
 			*res = 1;
 		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
 		break;
 	case BCH_OPT_UINT:
 		if (!val) {
Signature · 1 line
2.46.2
author
#10 lore raw

Re: [PATCH v2] bcachefs: add support for true/false & yes/no in bool-type options

Any progress on this?

#11 lore raw

Re: [PATCH v2] bcachefs: add support for true/false & yes/no in bool-type options

In reply to Integral
Quoted 43 lines · Integral

Here is my new patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
  fs/bcachefs/opts.c | 28 +++++++++++++++++++++-------
  1 file changed, 21 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +13 -0
index 232be8a44051..659536d00cb9 100644
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
 
  #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
  #include "bcachefs.h"
  #include "compress.h"
@@ -322,6 +323,18 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
  	return 0;
  }
 
+static const struct constant_table bch2_opt_bool_names[] = {
+	{ "0",		false },
+	{ "false",	false },
+	{ "no",		false },
+	{ "off",	false },
+	{ "1",		true },
+	{ "true",	true },
+	{ "yes",	true },
+	{ "on",		true },
+	{ },
+};
+
  int bch2_opt_parse(struct bch_fs *c,
  		   const struct bch_option *opt,
  		   const char *val, u64 *res,

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 349

Quoted 6 lines
fs/bcachefs/opts.c +2 -1
@@ -332,17 +345,18 @@ int bch2_opt_parse(struct bch_fs *c,
  	switch (opt->type) {
  	case BCH_OPT_BOOL:
  		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bch2_opt_bool_names, val, BCH_ERR_option_not_bool);
+			if (ret != BCH_ERR_option_not_bool) {

I apologize for looking at your patch so late. But this seems something wrong. The third parameter should be -BCH_ERR_option_not_bool. And also as noted in [1]. The only accepted bool value are: "0", "1", "no", "yes", "false", "true". This just like what has been done in fs_param_is_bool helper.

[1] Documentation/filesystems/mount_api.rst

Thanks,
Hongbo

Quoted 19 lines
fs/bcachefs/opts.c +6 -6
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
  		} else {
-			ret = 0;
  			*res = 1;
  		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
  		break;
  	case BCH_OPT_UINT:
  		if (!val) {
author
#12 lore raw

PATCH v3 1/1 bcachefs: add support for true/false & yes/no in bool-type options

Here is my modified patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
 fs/bcachefs/opts.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +19 -7
index 68d240126522..26319e7a1c4b 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
 #include "bcachefs.h"
 #include "compress.h"
@@ -322,6 +323,16 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
 	return 0;
 }
 
+static const struct constant_table bch2_opt_bool_names[] = {
+	{ "0",		false },
+	{ "false",	false },
+	{ "no",		false },
+	{ "1",		true },
+	{ "true",	true },
+	{ "yes",	true },
+	{ },
+};
+
 int bch2_opt_parse(struct bch_fs *c,
 		   const struct bch_option *opt,
 		   const char *val, u64 *res,
@@ -332,17 +343,18 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bch2_opt_bool_names, val, -BCH_ERR_option_not_bool);
+			if (ret != BCH_ERR_option_not_bool) {
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
 		} else {
-			ret = 0;
 			*res = 1;
 		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
 		break;
 	case BCH_OPT_UINT:
 		if (!val) {
Signature · 1 line
2.47.0
author
#13 lore raw

Re: [PATCH v3] bcachefs: add support for true/false & yes/no in bool-type options

Is my new patch acceptable?

#14 lore raw

Re: [PATCH v3] bcachefs: add support for true/false & yes/no in bool-type options

In reply to Integral
Quoted 41 lines · Integral

Here is my modified patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
  fs/bcachefs/opts.c | 26 +++++++++++++++++++-------
  1 file changed, 19 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +11 -0
index 68d240126522..26319e7a1c4b 100644
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
 
  #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
  #include "bcachefs.h"
  #include "compress.h"
@@ -322,6 +323,16 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
  	return 0;
  }
 
+static const struct constant_table bch2_opt_bool_names[] = {
+	{ "0",		false },
+	{ "false",	false },
+	{ "no",		false },
+	{ "1",		true },
+	{ "true",	true },
+	{ "yes",	true },
+	{ },
+};
+
  int bch2_opt_parse(struct bch_fs *c,
  		   const struct bch_option *opt,
  		   const char *val, u64 *res,

fs/bcachefs/opts.c int bch2_opt_parse(struct bch_fs *c, · line 347

Quoted 6 lines
fs/bcachefs/opts.c +2 -1
@@ -332,17 +343,18 @@ int bch2_opt_parse(struct bch_fs *c,
  	switch (opt->type) {
  	case BCH_OPT_BOOL:
  		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bch2_opt_bool_names, val, -BCH_ERR_option_not_bool);
+			if (ret != BCH_ERR_option_not_bool) {

Should it also be modified? Other seems good for me. :)

Thanks,
Hongbo

Quoted 19 lines
fs/bcachefs/opts.c +6 -6
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
  		} else {
-			ret = 0;
  			*res = 1;
  		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
  		break;
  	case BCH_OPT_UINT:
  		if (!val) {
author
#15 lore raw

PATCH v4 1/1 libbcachefs: add support for true/false & yes/no in bool-type options

Here is my latest patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
 fs/bcachefs/opts.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +19 -7
index 68d240126522..2d81e39ddd73 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
 #include "bcachefs.h"
 #include "compress.h"
@@ -322,6 +323,16 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
 	return 0;
 }
 
+static const struct constant_table bch2_opt_bool_names[] = {
+	{ "0",		false },
+	{ "false",	false },
+	{ "no",		false },
+	{ "1",		true },
+	{ "true",	true },
+	{ "yes",	true },
+	{ },
+};
+
 int bch2_opt_parse(struct bch_fs *c,
 		   const struct bch_option *opt,
 		   const char *val, u64 *res,
@@ -332,17 +343,18 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bch2_opt_bool_names, val, -BCH_ERR_option_not_bool);
+			if (ret != -BCH_ERR_option_not_bool) {
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
 		} else {
-			ret = 0;
 			*res = 1;
 		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
 		break;
 	case BCH_OPT_UINT:
 		if (!val) {
Signature · 1 line
2.47.0
#16 lore raw
Quoted 6 lines · Integral

Here is my latest patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

Looks good to me! :)

Thanks
Hongbo

Quoted 60 lines
---
  fs/bcachefs/opts.c | 26 +++++++++++++++++++-------
  1 file changed, 19 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +19 -7
index 68d240126522..2d81e39ddd73 100644
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
 
  #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
  #include "bcachefs.h"
  #include "compress.h"
@@ -322,6 +323,16 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
  	return 0;
  }
 
+static const struct constant_table bch2_opt_bool_names[] = {
+	{ "0",		false },
+	{ "false",	false },
+	{ "no",		false },
+	{ "1",		true },
+	{ "true",	true },
+	{ "yes",	true },
+	{ },
+};
+
  int bch2_opt_parse(struct bch_fs *c,
  		   const struct bch_option *opt,
  		   const char *val, u64 *res,
@@ -332,17 +343,18 @@ int bch2_opt_parse(struct bch_fs *c,
  	switch (opt->type) {
  	case BCH_OPT_BOOL:
  		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bch2_opt_bool_names, val, -BCH_ERR_option_not_bool);
+			if (ret != -BCH_ERR_option_not_bool) {
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
  		} else {
-			ret = 0;
  			*res = 1;
  		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
  		break;
  	case BCH_OPT_UINT:
  		if (!val) {
#17 lore raw
In reply to Integral
Quoted 28 lines · Integral

Here is my latest patch:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
fs/bcachefs/opts.c | 26 +++++++++++++++++++-------

1 file changed, 19 insertions(+), 7 deletions(-)
fs/bcachefs/opts.c +1 -0
index 68d240126522..2d81e39ddd73 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
 #include "bcachefs.h"
 #include "compress.h"
@@ -322,6 +323,16 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
 return 0;
 }
 

fs/bcachefs/opts.c int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err) · line 335

Quoted 10 lines
fs/bcachefs/opts.c +10 -0
+static const struct constant_table bch2_opt_bool_names[] = {
+ { "0", false },
+ { "false", false },
+ { "no", false },
+ { "1", true },
+ { "true", true },
+ { "yes", true },
+ { },
+};
+

Can you use the existing constant table?

Quoted 32 lines
fs/bcachefs/opts.c +0 -0
 int bch2_opt_parse(struct bch_fs *c,
  const struct bch_option *opt,
  const char *val, u64 *res,
@@ -332,17 +343,18 @@ int bch2_opt_parse(struct bch_fs *c,
 switch (opt->type) {

case BCH_OPT_BOOL:
if (val) {
- ret = kstrtou64(val, 10, res);
+ ret = lookup_constant(bch2_opt_bool_names, val, -BCH_ERR_option_not_bool);
+ if (ret != -BCH_ERR_option_not_bool) {
+ *res = ret;
+ } else {
+ if (err)
+ prt_printf(err, "%s: must be bool", opt->attr.name);
+ return ret;
+ }
} else {
- ret = 0;
*res = 1;
}

- if (ret < 0 || (*res != 0 && *res != 1)) {
- if (err)
- prt_printf(err, "%s: must be bool", opt->attr.name);
- return ret < 0 ? ret : -BCH_ERR_option_not_bool;
- }
break;
case BCH_OPT_UINT:
if (!val) {

Signature · 1 line
2.47.0
author
#18 lore raw

PATCH v5 1/1 bcachefs: add support for true/false & yes/no in bool-type options

Here is the patch which uses existing constant table:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

---
 fs/bcachefs/opts.c | 18 +++++++++++-------
 fs/fs_parser.c     |  3 ++-
 2 files changed, 13 insertions(+), 8 deletions(-)
fs/bcachefs/opts.c +11 -7
index 68d240126522..b564c42d62f8 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
 #include "bcachefs.h"
 #include "compress.h"
@@ -322,6 +323,8 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
 	return 0;
 }
 
+extern const struct constant_table bool_names[];
+
 int bch2_opt_parse(struct bch_fs *c,
 		   const struct bch_option *opt,
 		   const char *val, u64 *res,
@@ -332,17 +335,18 @@ int bch2_opt_parse(struct bch_fs *c,
 	switch (opt->type) {
 	case BCH_OPT_BOOL:
 		if (val) {
-			ret = kstrtou64(val, 10, res);
+			ret = lookup_constant(bool_names, val, -BCH_ERR_option_not_bool);
+			if (ret != -BCH_ERR_option_not_bool) {
+				*res = ret;
+			} else {
+				if (err)
+					prt_printf(err, "%s: must be bool", opt->attr.name);
+				return ret;
+			}
 		} else {
-			ret = 0;
 			*res = 1;
 		}
 
-		if (ret < 0 || (*res != 0 && *res != 1)) {
-			if (err)
-				prt_printf(err, "%s: must be bool", opt->attr.name);
-			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
-		}
 		break;
 	case BCH_OPT_UINT:
 		if (!val) {
fs/fs_parser.c +2 -1
index 24727ec34e5a..6521e9a9d6ef 100644
@@ -13,7 +13,7 @@
 #include <linux/namei.h>
 #include "internal.h"
 
-static const struct constant_table bool_names[] = {
+const struct constant_table bool_names[] = {
 	{ "0",		false },
 	{ "1",		true },
 	{ "false",	false },
@@ -22,6 +22,7 @@ static const struct constant_table bool_names[] = {
 	{ "yes",	true },
 	{ },
 };
+EXPORT_SYMBOL(bool_names);
 
 static const struct constant_table *
 __lookup_constant(const struct constant_table *tbl, const char *name)
Signature · 1 line
2.47.0
author
#19 lore raw

Re: [PATCH v5] bcachefs: add support for true/false & yes/no in bool-type options

Is my v5 patch more acceptable?

#20 lore raw

Re: [PATCH v5] bcachefs: add support for true/false & yes/no in bool-type options

In reply to Integral
Quoted 8 lines · Integral

Here is the patch which uses existing constant table:

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

The code changes part looks good.

Quoted 76 lines

---
fs/bcachefs/opts.c | 18 +++++++++++-------
fs/fs_parser.c | 3 ++-

2 files changed, 13 insertions(+), 8 deletions(-)
fs/bcachefs/opts.c +3 -0
index 68d240126522..b564c42d62f8 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/fs_parser.h>
 
 #include "bcachefs.h"
 #include "compress.h"
@@ -322,6 +323,8 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
 return 0;
 }
 
+extern const struct constant_table bool_names[];
+
 int bch2_opt_parse(struct bch_fs *c,
  const struct bch_option *opt,
  const char *val, u64 *res,
@@ -332,17 +335,18 @@ int bch2_opt_parse(struct bch_fs *c,
 switch (opt->type) {

case BCH_OPT_BOOL:
if (val) {
- ret = kstrtou64(val, 10, res);
+ ret = lookup_constant(bool_names, val, -BCH_ERR_option_not_bool);
+ if (ret != -BCH_ERR_option_not_bool) {
+ *res = ret;
+ } else {
+ if (err)
+ prt_printf(err, "%s: must be bool", opt->attr.name);
+ return ret;
+ }
} else {
- ret = 0;
*res = 1;
}

- if (ret < 0 || (*res != 0 && *res != 1)) {
- if (err)
- prt_printf(err, "%s: must be bool", opt->attr.name);
- return ret < 0 ? ret : -BCH_ERR_option_not_bool;
- }
break;
case BCH_OPT_UINT:
if (!val) {

fs/fs_parser.c +2 -1
index 24727ec34e5a..6521e9a9d6ef 100644
@@ -13,7 +13,7 @@
 #include <linux/namei.h>
 #include "internal.h"
 
-static const struct constant_table bool_names[] = {
+const struct constant_table bool_names[] = {
 { "0", false },
 { "1", true },
 { "false", false },
@@ -22,6 +22,7 @@ static const struct constant_table bool_names[] = {
 { "yes", true },
 { },
 };
+EXPORT_SYMBOL(bool_names);
 

static const struct constant_table * __lookup_constant(const struct constant_table *tbl, const char *name)

Signature · 1 line
2.47.0
author
#21 lore raw

Re: [PATCH v5] bcachefs: add support for true/false & yes/no in bool-type options

If my latest patch is acceptable, could you please apply my patch? :)

author
#22 lore raw

Re: [PATCH v5] bcachefs: add support for true/false & yes/no in bool-type options

Hi, Alan Huang

Give you a friendly ping :)

Quoted 1 line · Integral

If my latest patch is acceptable, could you please apply my patch? :)

#23 lore raw
In reply to Integral
Quoted 7 lines · Integral

This is a resend of my previous patch with code style optimized as I forgot to add Kent Overstreet in "To". The previous patch is
(https://lore.kernel.org/linux-bcachefs/[email protected]/T/#u).

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

Sorry I missed this before - you just need a signed-off-by line and I can apply

#24 lore raw
Quoted 6 lines · Kent Overstreet
Quoted 4 lines · Integral

This is a resend of my previous patch with code style optimized as I forgot to add Kent Overstreet in "To". The previous patch is
(https://lore.kernel.org/linux-bcachefs/[email protected]/T/#u).

Quoted 7 lines
Quoted 3 lines

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

Sorry I missed this before - you just need a signed-off-by line and I can apply

There is a v5 now:

https://lore.kernel.org/all/[email protected]/T/#mea54884e20fa63d4ee32132a1980749e9f6283b9

#25 lore raw
Quoted 11 lines · Alan Huang
Quoted 10 lines · Kent Overstreet
Quoted 7 lines · Integral

This is a resend of my previous patch with code style optimized as I forgot to add Kent Overstreet in "To". The previous patch is
(https://lore.kernel.org/linux-bcachefs/[email protected]/T/#u).

Currently, when using bcachefs-tools to set options, bool-type options can only accept 1 or 0. Add support for accepting true/false and yes/no for these options.

Quoted 7 lines
Quoted 3 lines

Sorry I missed this before - you just need a signed-off-by line and I can apply

There is a v5 now:

https://lore.kernel.org/all/[email protected]/T/#mea54884e20fa63d4ee32132a1980749e9f6283b9

Thanks, I've applied v5