On 2024/10/23 13:15, Integral wrote:
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

Signed-off-by: Integral <[email protected]>
---
  fs/bcachefs/opts.c | 26 +++++++++++++++++++-------
  1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c
index 68d240126522..2d81e39ddd73 100644
--- a/fs/bcachefs/opts.c
+++ b/fs/bcachefs/opts.c
@@ -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) {

Reply via email to