1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/fs_parser.h>
8 #include "disk_groups.h"
12 #include "rebalance.h"
13 #include "recovery_passes.h"
17 #define x(t, n, ...) [n] = #t,
19 const char * const bch2_error_actions[] = {
24 const char * const bch2_degraded_actions[] = {
25 BCH_DEGRADED_ACTIONS()
29 const char * const bch2_fsck_fix_opts[] = {
34 const char * const bch2_version_upgrade_opts[] = {
35 BCH_VERSION_UPGRADE_OPTS()
39 const char * const bch2_sb_features[] = {
44 const char * const bch2_sb_compat[] = {
49 const char * const __bch2_btree_ids[] = {
54 const char * const __bch2_csum_types[] = {
59 const char * const __bch2_csum_opts[] = {
64 const char * const __bch2_compression_types[] = {
65 BCH_COMPRESSION_TYPES()
69 const char * const bch2_compression_opts[] = {
70 BCH_COMPRESSION_OPTS()
74 const char * const __bch2_str_hash_types[] = {
79 const char * const bch2_str_hash_opts[] = {
84 const char * const __bch2_data_types[] = {
89 const char * const bch2_member_states[] = {
94 static const char * const __bch2_jset_entry_types[] = {
95 BCH_JSET_ENTRY_TYPES()
99 static const char * const __bch2_fs_usage_types[] = {
106 static void prt_str_opt_boundscheck(struct printbuf *out, const char * const opts[],
107 unsigned nr, const char *type, unsigned idx)
110 prt_str(out, opts[idx]);
112 prt_printf(out, "(unknown %s %u)", type, idx);
115 #define PRT_STR_OPT_BOUNDSCHECKED(name, type) \
116 void bch2_prt_##name(struct printbuf *out, type t) \
118 prt_str_opt_boundscheck(out, __bch2_##name##s, ARRAY_SIZE(__bch2_##name##s) - 1, #name, t);\
121 PRT_STR_OPT_BOUNDSCHECKED(jset_entry_type, enum bch_jset_entry_type);
122 PRT_STR_OPT_BOUNDSCHECKED(fs_usage_type, enum bch_fs_usage_type);
123 PRT_STR_OPT_BOUNDSCHECKED(data_type, enum bch_data_type);
124 PRT_STR_OPT_BOUNDSCHECKED(csum_opt, enum bch_csum_opt);
125 PRT_STR_OPT_BOUNDSCHECKED(csum_type, enum bch_csum_type);
126 PRT_STR_OPT_BOUNDSCHECKED(compression_type, enum bch_compression_type);
127 PRT_STR_OPT_BOUNDSCHECKED(str_hash_type, enum bch_str_hash_type);
129 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
130 struct printbuf *err)
135 int ret = match_string(bch2_fsck_fix_opts, -1, val);
138 prt_str(err, "fix_errors: invalid selection");
147 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
152 prt_str(out, bch2_fsck_fix_opts[v]);
155 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
156 .parse = bch2_opt_fix_errors_parse, \
157 .to_text = bch2_opt_fix_errors_to_text, \
160 const char * const bch2_d_types[BCH_DT_MAX] = {
161 [DT_UNKNOWN] = "unknown",
169 [DT_WHT] = "whiteout",
170 [DT_SUBVOL] = "subvol",
173 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
175 #define x(_name, ...) \
176 if (opt_defined(src, _name)) \
177 opt_set(*dst, _name, src._name);
183 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
186 #define x(_name, ...) \
188 return opt_defined(*opts, _name);
196 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
199 #define x(_name, ...) \
209 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
212 #define x(_name, ...) \
214 opt_set(*opts, _name, v); \
223 /* dummy option, for options that aren't stored in the superblock */
224 typedef u64 (*sb_opt_get_fn)(const struct bch_sb *);
225 typedef void (*sb_opt_set_fn)(struct bch_sb *, u64);
226 typedef u64 (*member_opt_get_fn)(const struct bch_member *);
227 typedef void (*member_opt_set_fn)(struct bch_member *, u64);
229 __maybe_unused static const sb_opt_get_fn BCH2_NO_SB_OPT = NULL;
230 __maybe_unused static const sb_opt_set_fn SET_BCH2_NO_SB_OPT = NULL;
231 __maybe_unused static const member_opt_get_fn BCH2_NO_MEMBER_OPT = NULL;
232 __maybe_unused static const member_opt_set_fn SET_BCH2_NO_MEMBER_OPT = NULL;
234 #define type_compatible_or_null(_p, _type) \
235 __builtin_choose_expr( \
236 __builtin_types_compatible_p(typeof(_p), typeof(_type)), _p, NULL)
238 const struct bch_option bch2_opt_table[] = {
239 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
240 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
241 .min = _min, .max = _max
242 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
243 .min = 0, .max = ARRAY_SIZE(_choices) - 1, \
245 #define OPT_STR_NOLIMIT(_choices) .type = BCH_OPT_STR, \
246 .min = 0, .max = U64_MAX, \
248 #define OPT_BITFIELD(_choices) .type = BCH_OPT_BITFIELD, \
250 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
252 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
254 .attr.name = #_name, \
255 .attr.mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
259 .get_sb = type_compatible_or_null(_sb_opt, *BCH2_NO_SB_OPT), \
260 .set_sb = type_compatible_or_null(SET_##_sb_opt,*SET_BCH2_NO_SB_OPT), \
261 .get_member = type_compatible_or_null(_sb_opt, *BCH2_NO_MEMBER_OPT), \
262 .set_member = type_compatible_or_null(SET_##_sb_opt,*SET_BCH2_NO_MEMBER_OPT),\
270 int bch2_opt_lookup(const char *name)
272 const struct bch_option *i;
274 for (i = bch2_opt_table;
275 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
277 if (!strcmp(name, i->attr.name))
278 return i - bch2_opt_table;
287 static const struct opt_synonym bch2_opt_synonyms[] = {
288 { "quota", "usrquota" },
291 static int bch2_mount_opt_lookup(const char *name)
293 const struct opt_synonym *i;
295 for (i = bch2_opt_synonyms;
296 i < bch2_opt_synonyms + ARRAY_SIZE(bch2_opt_synonyms);
298 if (!strcmp(name, i->s1))
301 return bch2_opt_lookup(name);
304 struct opt_val_synonym {
305 const char *opt, *v1, *v2;
308 static const struct opt_val_synonym bch2_opt_val_synonyms[] = {
309 { "degraded", "true", "yes" },
310 { "degraded", "false", "no" },
311 { "degraded", "1", "yes" },
312 { "degraded", "0", "no" },
315 static const char *bch2_opt_val_synonym_lookup(const char *opt, const char *val)
317 const struct opt_val_synonym *i;
319 for (i = bch2_opt_val_synonyms;
320 i < bch2_opt_val_synonyms + ARRAY_SIZE(bch2_opt_val_synonyms);
322 if (!strcmp(opt, i->opt) && !strcmp(val, i->v1))
328 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
332 prt_printf(err, "%s: too small (min %llu)",
333 opt->attr.name, opt->min);
334 return -BCH_ERR_ERANGE_option_too_small;
337 if (opt->max && v >= opt->max) {
339 prt_printf(err, "%s: too big (max %llu)",
340 opt->attr.name, opt->max);
341 return -BCH_ERR_ERANGE_option_too_big;
344 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
346 prt_printf(err, "%s: not a multiple of 512",
348 return -BCH_ERR_opt_parse_error;
351 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
353 prt_printf(err, "%s: must be a power of two",
355 return -BCH_ERR_opt_parse_error;
358 if (opt->fn.validate)
359 return opt->fn.validate(v, err);
364 int bch2_opt_parse(struct bch_fs *c,
365 const struct bch_option *opt,
366 const char *val, u64 *res,
367 struct printbuf *err)
372 printbuf_indent_add_nextline(err, 2);
379 ret = lookup_constant(bool_names, val, -BCH_ERR_option_not_bool);
380 if (ret != -BCH_ERR_option_not_bool) {
384 prt_printf(err, "%s: must be bool", opt->attr.name);
390 prt_printf(err, "%s: required value",
396 ret = opt->flags & OPT_HUMAN_READABLE
397 ? bch2_strtou64_h(val, res)
398 : kstrtou64(val, 10, res);
400 prt_printf(err, "%s: must be a non-negative number", opt->attr.name);
401 return -BCH_ERR_option_negative;
406 prt_printf(err, "%s: must be a number",
413 prt_printf(err, "%s: required value",
418 ret = match_string(opt->choices, -1, val);
421 prt_printf(err, "%s: invalid selection",
428 case BCH_OPT_BITFIELD: {
429 s64 v = bch2_read_flag_list(val, opt->choices);
436 ret = opt->fn.parse(c, val, res, err);
438 if (ret == -BCH_ERR_option_needs_open_fs)
443 prt_printf(err, "%s: parse error",
449 return bch2_opt_validate(opt, *res, err);
452 void bch2_opt_to_text(struct printbuf *out,
453 struct bch_fs *c, struct bch_sb *sb,
454 const struct bch_option *opt, u64 v,
457 if (flags & OPT_SHOW_MOUNT_STYLE) {
458 if (opt->type == BCH_OPT_BOOL) {
459 prt_printf(out, "%s%s",
465 prt_printf(out, "%s=", opt->attr.name);
471 if (opt->flags & OPT_HUMAN_READABLE)
472 prt_human_readable_u64(out, v);
474 prt_printf(out, "%lli", v);
477 if (v < opt->min || v >= opt->max)
478 prt_printf(out, "(invalid option %lli)", v);
479 else if (flags & OPT_SHOW_FULL_LIST)
480 prt_string_option(out, opt->choices, v);
482 prt_str(out, opt->choices[v]);
484 case BCH_OPT_BITFIELD:
485 prt_bitflags(out, opt->choices, v);
488 opt->fn.to_text(out, c, sb, v);
495 void bch2_opts_to_text(struct printbuf *out,
496 struct bch_opts opts,
497 struct bch_fs *c, struct bch_sb *sb,
498 unsigned show_mask, unsigned hide_mask,
503 for (enum bch_opt_id i = 0; i < bch2_opts_nr; i++) {
504 const struct bch_option *opt = &bch2_opt_table[i];
506 if ((opt->flags & hide_mask) || !(opt->flags & show_mask))
509 u64 v = bch2_opt_get_by_id(&opts, i);
510 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
517 bch2_opt_to_text(out, c, sb, opt, v, flags);
521 int bch2_opt_hook_pre_set(struct bch_fs *c, struct bch_dev *ca, enum bch_opt_id id, u64 v)
528 return bch2_dev_set_state(c, ca, v, BCH_FORCE_IF_DEGRADED);
531 case Opt_compression:
532 case Opt_background_compression:
533 ret = bch2_check_set_has_compressed_data(c, v);
535 case Opt_erasure_code:
537 bch2_check_set_feature(c, BCH_FEATURE_ec);
546 int bch2_opts_hooks_pre_set(struct bch_fs *c)
548 for (unsigned i = 0; i < bch2_opts_nr; i++) {
549 int ret = bch2_opt_hook_pre_set(c, NULL, i, bch2_opt_get_by_id(&c->opts, i));
557 void bch2_opt_hook_post_set(struct bch_fs *c, struct bch_dev *ca, u64 inum,
558 struct bch_opts *new_opts, enum bch_opt_id id)
561 case Opt_foreground_target:
562 if (new_opts->foreground_target &&
563 !new_opts->background_target)
564 bch2_set_rebalance_needs_scan(c, inum);
566 case Opt_compression:
567 if (new_opts->compression &&
568 !new_opts->background_compression)
569 bch2_set_rebalance_needs_scan(c, inum);
571 case Opt_background_target:
572 if (new_opts->background_target)
573 bch2_set_rebalance_needs_scan(c, inum);
575 case Opt_background_compression:
576 if (new_opts->background_compression)
577 bch2_set_rebalance_needs_scan(c, inum);
579 case Opt_rebalance_enabled:
580 bch2_rebalance_wakeup(c);
582 case Opt_copygc_enabled:
583 bch2_copygc_wakeup(c);
587 mutex_lock(&c->sb_lock);
588 for_each_member_device(c, ca) {
589 struct bch_member *m =
590 bch2_members_v2_get_mut(ca->disk_sb.sb, ca->dev_idx);
591 SET_BCH_MEMBER_DISCARD(m, c->opts.discard);
595 mutex_unlock(&c->sb_lock);
598 case Opt_version_upgrade:
600 * XXX: in the future we'll likely want to do compatible
601 * upgrades at runtime as well, but right now there's nothing
604 if (new_opts->version_upgrade == BCH_VERSION_UPGRADE_incompatible)
605 bch2_sb_upgrade_incompat(c);
612 int bch2_parse_one_mount_opt(struct bch_fs *c, struct bch_opts *opts,
613 struct printbuf *parse_later,
614 const char *name, const char *val)
616 struct printbuf err = PRINTBUF;
620 id = bch2_mount_opt_lookup(name);
622 /* Check for the form "noopt", negation of a boolean opt: */
625 !strncmp("no", name, 2)) {
626 id = bch2_mount_opt_lookup(name + 2);
630 /* Unknown options are ignored: */
634 /* must have a value for synonym lookup - but OPT_FN is weird */
635 if (!val && bch2_opt_table[id].type != BCH_OPT_FN)
638 val = bch2_opt_val_synonym_lookup(name, val);
640 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
644 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
647 if ((id == Opt_usrquota ||
648 id == Opt_grpquota) &&
649 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
652 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
653 if (ret == -BCH_ERR_option_needs_open_fs) {
657 prt_printf(parse_later, "%s=%s,", name, val);
658 if (parse_later->allocation_failure)
669 bch2_opt_set_by_id(opts, id, v);
676 ret = -BCH_ERR_option_name;
679 ret = -BCH_ERR_option_value;
683 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
684 struct printbuf *parse_later, char *options,
687 char *copied_opts, *copied_opts_start;
688 char *opt, *name, *val;
695 * sys_fsconfig() is now occasionally providing us with option lists
696 * starting with a comma - weird.
701 copied_opts = kstrdup(options, GFP_KERNEL);
704 copied_opts_start = copied_opts;
706 while ((opt = strsep(&copied_opts, ",")) != NULL) {
710 name = strsep(&opt, "=");
713 ret = bch2_parse_one_mount_opt(c, opts, parse_later, name, val);
714 if (ret == -BCH_ERR_option_name && ignore_unknown)
717 pr_err("Error parsing option %s: %s", name, bch2_err_str(ret));
722 kfree(copied_opts_start);
726 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id, int dev_idx)
728 const struct bch_option *opt = bch2_opt_table + id;
734 if (WARN(!bch2_member_exists(sb, dev_idx),
735 "tried to set device option %s on nonexistent device %i",
736 opt->attr.name, dev_idx))
739 struct bch_member m = bch2_sb_member_get(sb, dev_idx);
740 v = opt->get_member(&m);
743 if (opt->flags & OPT_SB_FIELD_ONE_BIAS)
746 if (opt->flags & OPT_SB_FIELD_ILOG2)
749 if (opt->flags & OPT_SB_FIELD_SECTORS)
756 * Initial options from superblock - here we don't want any options undefined,
757 * any options the superblock doesn't specify are set to 0:
759 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
761 for (unsigned id = 0; id < bch2_opts_nr; id++) {
762 const struct bch_option *opt = bch2_opt_table + id;
765 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id, -1));
771 bool __bch2_opt_set_sb(struct bch_sb *sb, int dev_idx,
772 const struct bch_option *opt, u64 v)
774 bool changed = false;
776 if (opt->flags & OPT_SB_FIELD_SECTORS)
779 if (opt->flags & OPT_SB_FIELD_ILOG2)
782 if (opt->flags & OPT_SB_FIELD_ONE_BIAS)
785 if ((opt->flags & OPT_FS) && opt->set_sb && dev_idx < 0) {
786 changed = v != opt->get_sb(sb);
791 if ((opt->flags & OPT_DEVICE) && opt->set_member && dev_idx >= 0) {
792 if (WARN(!bch2_member_exists(sb, dev_idx),
793 "tried to set device option %s on nonexistent device %i",
794 opt->attr.name, dev_idx))
797 struct bch_member *m = bch2_members_v2_get_mut(sb, dev_idx);
798 changed = v != opt->get_member(m);
799 opt->set_member(m, v);
805 bool bch2_opt_set_sb(struct bch_fs *c, struct bch_dev *ca,
806 const struct bch_option *opt, u64 v)
808 mutex_lock(&c->sb_lock);
809 bool changed = __bch2_opt_set_sb(c->disk_sb.sb, ca ? ca->dev_idx : -1, opt, v);
812 mutex_unlock(&c->sb_lock);
818 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
820 struct bch_io_opts opts = {
821 #define x(_name, _bits) ._name = src._name,
826 bch2_io_opts_fixups(&opts);
830 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
832 static const enum bch_opt_id inode_opt_list[] = {
833 #define x(_name, _bits) Opt_##_name,
839 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
840 if (inode_opt_list[i] == id)