1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
10 #include "recovery_passes.h"
14 #define x(t, n, ...) [n] = #t,
16 const char * const bch2_error_actions[] = {
21 const char * const bch2_fsck_fix_opts[] = {
26 const char * const bch2_version_upgrade_opts[] = {
27 BCH_VERSION_UPGRADE_OPTS()
31 const char * const bch2_sb_features[] = {
36 const char * const bch2_sb_compat[] = {
41 const char * const __bch2_btree_ids[] = {
46 static const char * const __bch2_csum_types[] = {
51 const char * const bch2_csum_opts[] = {
56 static const char * const __bch2_compression_types[] = {
57 BCH_COMPRESSION_TYPES()
61 const char * const bch2_compression_opts[] = {
62 BCH_COMPRESSION_OPTS()
66 const char * const bch2_str_hash_types[] = {
71 const char * const bch2_str_hash_opts[] = {
76 const char * const __bch2_data_types[] = {
81 const char * const bch2_member_states[] = {
86 static const char * const __bch2_jset_entry_types[] = {
87 BCH_JSET_ENTRY_TYPES()
91 static const char * const __bch2_fs_usage_types[] = {
98 static void prt_str_opt_boundscheck(struct printbuf *out, const char * const opts[],
99 unsigned nr, const char *type, unsigned idx)
102 prt_str(out, opts[idx]);
104 prt_printf(out, "(unknown %s %u)", type, idx);
107 #define PRT_STR_OPT_BOUNDSCHECKED(name, type) \
108 void bch2_prt_##name(struct printbuf *out, type t) \
110 prt_str_opt_boundscheck(out, __bch2_##name##s, ARRAY_SIZE(__bch2_##name##s) - 1, #name, t);\
113 PRT_STR_OPT_BOUNDSCHECKED(jset_entry_type, enum bch_jset_entry_type);
114 PRT_STR_OPT_BOUNDSCHECKED(fs_usage_type, enum bch_fs_usage_type);
115 PRT_STR_OPT_BOUNDSCHECKED(data_type, enum bch_data_type);
116 PRT_STR_OPT_BOUNDSCHECKED(csum_type, enum bch_csum_type);
117 PRT_STR_OPT_BOUNDSCHECKED(compression_type, enum bch_compression_type);
119 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
120 struct printbuf *err)
125 int ret = match_string(bch2_fsck_fix_opts, -1, val);
128 prt_str(err, "fix_errors: invalid selection");
137 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
142 prt_str(out, bch2_fsck_fix_opts[v]);
145 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
146 .parse = bch2_opt_fix_errors_parse, \
147 .to_text = bch2_opt_fix_errors_to_text, \
150 const char * const bch2_d_types[BCH_DT_MAX] = {
151 [DT_UNKNOWN] = "unknown",
159 [DT_WHT] = "whiteout",
160 [DT_SUBVOL] = "subvol",
163 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
168 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
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 const struct bch_option bch2_opt_table[] = {
224 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
225 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
226 .min = _min, .max = _max
227 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
228 .min = 0, .max = ARRAY_SIZE(_choices), \
230 #define OPT_STR_NOLIMIT(_choices) .type = BCH_OPT_STR, \
231 .min = 0, .max = U64_MAX, \
233 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
235 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
239 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
245 .set_sb = SET_##_sb_opt, \
253 int bch2_opt_lookup(const char *name)
255 const struct bch_option *i;
257 for (i = bch2_opt_table;
258 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
260 if (!strcmp(name, i->attr.name))
261 return i - bch2_opt_table;
270 static const struct synonym bch_opt_synonyms[] = {
271 { "quota", "usrquota" },
274 static int bch2_mount_opt_lookup(const char *name)
276 const struct synonym *i;
278 for (i = bch_opt_synonyms;
279 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
281 if (!strcmp(name, i->s1))
284 return bch2_opt_lookup(name);
287 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
291 prt_printf(err, "%s: too small (min %llu)",
292 opt->attr.name, opt->min);
293 return -BCH_ERR_ERANGE_option_too_small;
296 if (opt->max && v >= opt->max) {
298 prt_printf(err, "%s: too big (max %llu)",
299 opt->attr.name, opt->max);
300 return -BCH_ERR_ERANGE_option_too_big;
303 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
305 prt_printf(err, "%s: not a multiple of 512",
307 return -BCH_ERR_opt_parse_error;
310 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
312 prt_printf(err, "%s: must be a power of two",
314 return -BCH_ERR_opt_parse_error;
317 if (opt->fn.validate)
318 return opt->fn.validate(v, err);
323 int bch2_opt_parse(struct bch_fs *c,
324 const struct bch_option *opt,
325 const char *val, u64 *res,
326 struct printbuf *err)
333 ret = kstrtou64(val, 10, res);
339 if (ret < 0 || (*res != 0 && *res != 1)) {
341 prt_printf(err, "%s: must be bool", opt->attr.name);
342 return ret < 0 ? ret : -BCH_ERR_option_not_bool;
347 prt_printf(err, "%s: required value",
352 ret = opt->flags & OPT_HUMAN_READABLE
353 ? bch2_strtou64_h(val, res)
354 : kstrtou64(val, 10, res);
357 prt_printf(err, "%s: must be a number",
364 prt_printf(err, "%s: required value",
369 ret = match_string(opt->choices, -1, val);
372 prt_printf(err, "%s: invalid selection",
380 ret = opt->fn.parse(c, val, res, err);
383 prt_printf(err, "%s: parse error",
389 return bch2_opt_validate(opt, *res, err);
392 void bch2_opt_to_text(struct printbuf *out,
393 struct bch_fs *c, struct bch_sb *sb,
394 const struct bch_option *opt, u64 v,
397 if (flags & OPT_SHOW_MOUNT_STYLE) {
398 if (opt->type == BCH_OPT_BOOL) {
399 prt_printf(out, "%s%s",
405 prt_printf(out, "%s=", opt->attr.name);
411 if (opt->flags & OPT_HUMAN_READABLE)
412 prt_human_readable_u64(out, v);
414 prt_printf(out, "%lli", v);
417 if (flags & OPT_SHOW_FULL_LIST)
418 prt_string_option(out, opt->choices, v);
420 prt_str(out, opt->choices[v]);
423 opt->fn.to_text(out, c, sb, v);
430 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
435 case Opt_compression:
436 case Opt_background_compression:
437 ret = bch2_check_set_has_compressed_data(c, v);
439 case Opt_erasure_code:
441 bch2_check_set_feature(c, BCH_FEATURE_ec);
448 int bch2_opts_check_may_set(struct bch_fs *c)
453 for (i = 0; i < bch2_opts_nr; i++) {
454 ret = bch2_opt_check_may_set(c, i,
455 bch2_opt_get_by_id(&c->opts, i));
463 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
466 char *copied_opts, *copied_opts_start;
467 char *opt, *name, *val;
469 struct printbuf err = PRINTBUF;
476 * sys_fsconfig() is now occasionally providing us with option lists
477 * starting with a comma - weird.
482 copied_opts = kstrdup(options, GFP_KERNEL);
485 copied_opts_start = copied_opts;
487 while ((opt = strsep(&copied_opts, ",")) != NULL) {
488 name = strsep(&opt, "=");
491 id = bch2_mount_opt_lookup(name);
493 /* Check for the form "noopt", negation of a boolean opt: */
496 !strncmp("no", name, 2)) {
497 id = bch2_mount_opt_lookup(name + 2);
501 /* Unknown options are ignored: */
505 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
509 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
512 if ((id == Opt_usrquota ||
513 id == Opt_grpquota) &&
514 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
517 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
521 bch2_opt_set_by_id(opts, id, v);
528 pr_err("Bad mount option %s", name);
529 ret = -BCH_ERR_option_name;
532 pr_err("Invalid mount option %s", err.buf);
533 ret = -BCH_ERR_option_value;
536 kfree(copied_opts_start);
541 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
543 const struct bch_option *opt = bch2_opt_table + id;
548 if (opt->flags & OPT_SB_FIELD_ILOG2)
551 if (opt->flags & OPT_SB_FIELD_SECTORS)
558 * Initial options from superblock - here we don't want any options undefined,
559 * any options the superblock doesn't specify are set to 0:
561 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
565 for (id = 0; id < bch2_opts_nr; id++) {
566 const struct bch_option *opt = bch2_opt_table + id;
568 if (opt->get_sb == BCH2_NO_SB_OPT)
571 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
577 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
579 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
582 if (opt->flags & OPT_SB_FIELD_SECTORS)
585 if (opt->flags & OPT_SB_FIELD_ILOG2)
591 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
593 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
596 mutex_lock(&c->sb_lock);
597 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
599 mutex_unlock(&c->sb_lock);
604 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
606 return (struct bch_io_opts) {
607 #define x(_name, _bits) ._name = src._name,
613 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
615 static const enum bch_opt_id inode_opt_list[] = {
616 #define x(_name, _bits) Opt_##_name,
622 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
623 if (inode_opt_list[i] == id)