]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bcachefs: add support for true/false & yes/no in bool-type options
authorIntegral <integral@murena.io>
Wed, 23 Oct 2024 10:00:33 +0000 (18:00 +0800)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 21 Dec 2024 06:36:17 +0000 (01:36 -0500)
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.

Signed-off-by: Integral <integral@murena.io>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Acked-by: David Howells <dhowells@redhat.com>
fs/bcachefs/opts.c
fs/fs_parser.c
include/linux/fs_parser.h

index 49c59aec69546031f448addf054182429fc41d47..0ba58d74c21f35dbb3021f01b6fd027bf7e2a635 100644 (file)
@@ -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"
@@ -334,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) {
index 16fa61ef56bf78a11f506736d53eba032c7b6cbe..e635a81e17d965df78ffef27f6885cd70996c6dd 100644 (file)
@@ -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)
index 3cef566088fcf7e04c569acd849a785462c33f17..53e566efd5fd133d19e313e494b975612a227b77 100644 (file)
@@ -84,6 +84,8 @@ extern int fs_lookup_param(struct fs_context *fc,
 
 extern int lookup_constant(const struct constant_table tbl[], const char *name, int not_found);
 
+extern const struct constant_table bool_names[];
+
 #ifdef CONFIG_VALIDATE_FS_PARSER
 extern bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
                                    int low, int high, int special);