]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/opts.c
Update bcachefs sources to da7fefde29 bcachefs: shim for userspace raid library
[bcachefs-tools-debian] / libbcachefs / opts.c
index 8db8096e5ed4c06af9380ea32802626a224cb65f..449cd5bfcfc7317adc615e72a5f4ad385b6cc811 100644 (file)
@@ -2,6 +2,7 @@
 #include <linux/kernel.h>
 
 #include "bcachefs.h"
+#include "compress.h"
 #include "disk_groups.h"
 #include "opts.h"
 #include "super-io.h"
@@ -144,7 +145,7 @@ const struct bch_option bch2_opt_table[] = {
 #define OPT_STR(_choices)      .type = BCH_OPT_STR, .choices = _choices
 #define OPT_FN(_fn)            .type = BCH_OPT_FN,                     \
                                .parse = _fn##_parse,                   \
-                               .print = _fn##_print
+                               .to_text = _fn##_to_text
 
 #define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default)         \
        [Opt_##_name] = {                                               \
@@ -218,7 +219,7 @@ int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
                        return -ERANGE;
                break;
        case BCH_OPT_STR:
-               ret = bch2_read_string_list(val, opt->choices);
+               ret = match_string(opt->choices, -1, val);
                if (ret < 0)
                        return ret;
 
@@ -234,38 +235,78 @@ int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
        return 0;
 }
 
-int bch2_opt_to_text(struct bch_fs *c, char *buf, size_t len,
-                    const struct bch_option *opt, u64 v,
-                    unsigned flags)
+void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
+                     const struct bch_option *opt, u64 v,
+                     unsigned flags)
 {
-       char *out = buf, *end = buf + len;
-
        if (flags & OPT_SHOW_MOUNT_STYLE) {
-               if (opt->type == BCH_OPT_BOOL)
-                       return scnprintf(out, end - out, "%s%s",
-                                        v ? "" : "no",
-                                        opt->attr.name);
+               if (opt->type == BCH_OPT_BOOL) {
+                       pr_buf(out, "%s%s",
+                              v ? "" : "no",
+                              opt->attr.name);
+                       return;
+               }
 
-               out += scnprintf(out, end - out, "%s=", opt->attr.name);
+               pr_buf(out, "%s=", opt->attr.name);
        }
 
        switch (opt->type) {
        case BCH_OPT_BOOL:
        case BCH_OPT_UINT:
-               out += scnprintf(out, end - out, "%lli", v);
+               pr_buf(out, "%lli", v);
                break;
        case BCH_OPT_STR:
-               out += (flags & OPT_SHOW_FULL_LIST)
-                       ? bch2_scnprint_string_list(out, end - out, opt->choices, v)
-                       : scnprintf(out, end - out, opt->choices[v]);
+               if (flags & OPT_SHOW_FULL_LIST)
+                       bch2_string_opt_to_text(out, opt->choices, v);
+               else
+                       pr_buf(out, opt->choices[v]);
                break;
        case BCH_OPT_FN:
-               return opt->print(c, out, end - out, v);
+               opt->to_text(out, c, v);
+               break;
        default:
                BUG();
        }
+}
+
+int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
+{
+       int ret = 0;
+
+       switch (id) {
+       case Opt_compression:
+       case Opt_background_compression:
+               ret = bch2_check_set_has_compressed_data(c, v);
+               break;
+       case Opt_erasure_code:
+               if (v &&
+                   !(c->sb.features & (1ULL << BCH_FEATURE_EC))) {
+                       mutex_lock(&c->sb_lock);
+                       c->disk_sb.sb->features[0] |=
+                               cpu_to_le64(1ULL << BCH_FEATURE_EC);
+
+                       bch2_write_super(c);
+                       mutex_unlock(&c->sb_lock);
+               }
+               break;
+       }
+
+       return ret;
+}
 
-       return out - buf;
+int bch2_opts_check_may_set(struct bch_fs *c)
+{
+       unsigned i;
+       int ret;
+
+       for (i = 0; i < bch2_opts_nr; i++) {
+               ret = bch2_opt_check_may_set(c, i,
+                               bch2_opt_get_by_id(&c->opts, i));
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
 }
 
 int bch2_parse_mount_opts(struct bch_opts *opts, char *options)