1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
13 #define x(t, n, ...) [n] = #t,
15 const char * const bch2_error_actions[] = {
20 const char * const bch2_fsck_fix_opts[] = {
25 const char * const bch2_version_upgrade_opts[] = {
26 BCH_VERSION_UPGRADE_OPTS()
30 const char * const bch2_sb_features[] = {
35 const char * const bch2_sb_compat[] = {
40 const char * const bch2_btree_ids[] = {
42 "interior btree node",
46 const char * const bch2_csum_types[] = {
51 const char * const bch2_csum_opts[] = {
56 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 const char * const bch2_jset_entry_types[] = {
87 BCH_JSET_ENTRY_TYPES()
91 const char * const bch2_fs_usage_types[] = {
98 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
104 int ret = match_string(bch2_fsck_fix_opts, -1, val);
107 prt_str(err, "fix_errors: invalid selection");
116 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
121 prt_str(out, bch2_fsck_fix_opts[v]);
124 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
125 .parse = bch2_opt_fix_errors_parse, \
126 .to_text = bch2_opt_fix_errors_to_text, \
129 const char * const bch2_d_types[BCH_DT_MAX] = {
130 [DT_UNKNOWN] = "unknown",
138 [DT_WHT] = "whiteout",
139 [DT_SUBVOL] = "subvol",
142 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
147 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
152 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
154 #define x(_name, ...) \
155 if (opt_defined(src, _name)) \
156 opt_set(*dst, _name, src._name);
162 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
165 #define x(_name, ...) \
167 return opt_defined(*opts, _name);
175 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
178 #define x(_name, ...) \
188 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
191 #define x(_name, ...) \
193 opt_set(*opts, _name, v); \
202 const struct bch_option bch2_opt_table[] = {
203 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
204 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
205 .min = _min, .max = _max
206 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
207 .min = 0, .max = ARRAY_SIZE(_choices), \
209 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
211 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
215 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
221 .set_sb = SET_##_sb_opt, \
229 int bch2_opt_lookup(const char *name)
231 const struct bch_option *i;
233 for (i = bch2_opt_table;
234 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
236 if (!strcmp(name, i->attr.name))
237 return i - bch2_opt_table;
246 static const struct synonym bch_opt_synonyms[] = {
247 { "quota", "usrquota" },
250 static int bch2_mount_opt_lookup(const char *name)
252 const struct synonym *i;
254 for (i = bch_opt_synonyms;
255 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
257 if (!strcmp(name, i->s1))
260 return bch2_opt_lookup(name);
263 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
267 prt_printf(err, "%s: too small (min %llu)",
268 opt->attr.name, opt->min);
272 if (opt->max && v >= opt->max) {
274 prt_printf(err, "%s: too big (max %llu)",
275 opt->attr.name, opt->max);
279 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
281 prt_printf(err, "%s: not a multiple of 512",
286 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
288 prt_printf(err, "%s: must be a power of two",
296 int bch2_opt_parse(struct bch_fs *c,
297 const struct bch_option *opt,
298 const char *val, u64 *res,
299 struct printbuf *err)
306 ret = kstrtou64(val, 10, res);
312 if (ret < 0 || (*res != 0 && *res != 1)) {
314 prt_printf(err, "%s: must be bool", opt->attr.name);
320 prt_printf(err, "%s: required value",
325 ret = opt->flags & OPT_HUMAN_READABLE
326 ? bch2_strtou64_h(val, res)
327 : kstrtou64(val, 10, res);
330 prt_printf(err, "%s: must be a number",
337 prt_printf(err, "%s: required value",
342 ret = match_string(opt->choices, -1, val);
345 prt_printf(err, "%s: invalid selection",
353 ret = opt->fn.parse(c, val, res, err);
356 prt_printf(err, "%s: parse error",
362 return bch2_opt_validate(opt, *res, err);
365 void bch2_opt_to_text(struct printbuf *out,
366 struct bch_fs *c, struct bch_sb *sb,
367 const struct bch_option *opt, u64 v,
370 if (flags & OPT_SHOW_MOUNT_STYLE) {
371 if (opt->type == BCH_OPT_BOOL) {
372 prt_printf(out, "%s%s",
378 prt_printf(out, "%s=", opt->attr.name);
384 if (opt->flags & OPT_HUMAN_READABLE)
385 prt_human_readable_u64(out, v);
387 prt_printf(out, "%lli", v);
390 if (flags & OPT_SHOW_FULL_LIST)
391 prt_string_option(out, opt->choices, v);
393 prt_str(out, opt->choices[v]);
396 opt->fn.to_text(out, c, sb, v);
403 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
408 case Opt_compression:
409 case Opt_background_compression:
410 ret = bch2_check_set_has_compressed_data(c, v);
412 case Opt_erasure_code:
414 bch2_check_set_feature(c, BCH_FEATURE_ec);
421 int bch2_opts_check_may_set(struct bch_fs *c)
426 for (i = 0; i < bch2_opts_nr; i++) {
427 ret = bch2_opt_check_may_set(c, i,
428 bch2_opt_get_by_id(&c->opts, i));
436 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
439 char *copied_opts, *copied_opts_start;
440 char *opt, *name, *val;
442 struct printbuf err = PRINTBUF;
449 * sys_fsconfig() is now occasionally providing us with option lists
450 * starting with a comma - weird.
455 copied_opts = kstrdup(options, GFP_KERNEL);
458 copied_opts_start = copied_opts;
460 while ((opt = strsep(&copied_opts, ",")) != NULL) {
461 name = strsep(&opt, "=");
464 id = bch2_mount_opt_lookup(name);
466 /* Check for the form "noopt", negation of a boolean opt: */
469 !strncmp("no", name, 2)) {
470 id = bch2_mount_opt_lookup(name + 2);
477 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
481 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
484 if ((id == Opt_usrquota ||
485 id == Opt_grpquota) &&
486 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
489 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
493 bch2_opt_set_by_id(opts, id, v);
500 pr_err("Bad mount option %s", name);
504 pr_err("Invalid mount option %s", err.buf);
508 kfree(copied_opts_start);
513 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
515 const struct bch_option *opt = bch2_opt_table + id;
520 if (opt->flags & OPT_SB_FIELD_ILOG2)
523 if (opt->flags & OPT_SB_FIELD_SECTORS)
530 * Initial options from superblock - here we don't want any options undefined,
531 * any options the superblock doesn't specify are set to 0:
533 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
537 for (id = 0; id < bch2_opts_nr; id++) {
538 const struct bch_option *opt = bch2_opt_table + id;
540 if (opt->get_sb == BCH2_NO_SB_OPT)
543 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
549 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
551 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
554 if (opt->flags & OPT_SB_FIELD_SECTORS)
557 if (opt->flags & OPT_SB_FIELD_ILOG2)
563 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
565 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
568 mutex_lock(&c->sb_lock);
569 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
571 mutex_unlock(&c->sb_lock);
576 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
578 return (struct bch_io_opts) {
579 #define x(_name, _bits) ._name = src._name,
585 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
587 static const enum bch_opt_id inode_opt_list[] = {
588 #define x(_name, _bits) Opt_##_name,
594 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
595 if (inode_opt_list[i] == id)