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[] = {
45 const char * const bch2_csum_types[] = {
50 const char * const bch2_csum_opts[] = {
55 const char * const __bch2_compression_types[] = {
56 BCH_COMPRESSION_TYPES()
60 const char * const bch2_compression_opts[] = {
61 BCH_COMPRESSION_OPTS()
65 const char * const bch2_str_hash_types[] = {
70 const char * const bch2_str_hash_opts[] = {
75 const char * const __bch2_data_types[] = {
80 const char * const bch2_member_states[] = {
85 const char * const bch2_jset_entry_types[] = {
86 BCH_JSET_ENTRY_TYPES()
90 const char * const bch2_fs_usage_types[] = {
97 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
103 int ret = match_string(bch2_fsck_fix_opts, -1, val);
106 prt_str(err, "fix_errors: invalid selection");
115 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
120 prt_str(out, bch2_fsck_fix_opts[v]);
123 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
124 .parse = bch2_opt_fix_errors_parse, \
125 .to_text = bch2_opt_fix_errors_to_text, \
128 const char * const bch2_d_types[BCH_DT_MAX] = {
129 [DT_UNKNOWN] = "unknown",
137 [DT_WHT] = "whiteout",
138 [DT_SUBVOL] = "subvol",
141 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
146 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
151 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
153 #define x(_name, ...) \
154 if (opt_defined(src, _name)) \
155 opt_set(*dst, _name, src._name);
161 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
164 #define x(_name, ...) \
166 return opt_defined(*opts, _name);
174 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
177 #define x(_name, ...) \
187 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
190 #define x(_name, ...) \
192 opt_set(*opts, _name, v); \
201 const struct bch_option bch2_opt_table[] = {
202 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
203 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
204 .min = _min, .max = _max
205 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
206 .min = 0, .max = ARRAY_SIZE(_choices), \
208 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
210 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
214 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
220 .set_sb = SET_##_sb_opt, \
228 int bch2_opt_lookup(const char *name)
230 const struct bch_option *i;
232 for (i = bch2_opt_table;
233 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
235 if (!strcmp(name, i->attr.name))
236 return i - bch2_opt_table;
245 static const struct synonym bch_opt_synonyms[] = {
246 { "quota", "usrquota" },
249 static int bch2_mount_opt_lookup(const char *name)
251 const struct synonym *i;
253 for (i = bch_opt_synonyms;
254 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
256 if (!strcmp(name, i->s1))
259 return bch2_opt_lookup(name);
262 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
266 prt_printf(err, "%s: too small (min %llu)",
267 opt->attr.name, opt->min);
268 return -BCH_ERR_ERANGE_option_too_small;
271 if (opt->max && v >= opt->max) {
273 prt_printf(err, "%s: too big (max %llu)",
274 opt->attr.name, opt->max);
275 return -BCH_ERR_ERANGE_option_too_big;
278 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
280 prt_printf(err, "%s: not a multiple of 512",
282 return -BCH_ERR_opt_parse_error;
285 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
287 prt_printf(err, "%s: must be a power of two",
289 return -BCH_ERR_opt_parse_error;
292 if (opt->fn.validate)
293 return opt->fn.validate(v, err);
298 int bch2_opt_parse(struct bch_fs *c,
299 const struct bch_option *opt,
300 const char *val, u64 *res,
301 struct printbuf *err)
308 ret = kstrtou64(val, 10, res);
314 if (ret < 0 || (*res != 0 && *res != 1)) {
316 prt_printf(err, "%s: must be bool", opt->attr.name);
322 prt_printf(err, "%s: required value",
327 ret = opt->flags & OPT_HUMAN_READABLE
328 ? bch2_strtou64_h(val, res)
329 : kstrtou64(val, 10, res);
332 prt_printf(err, "%s: must be a number",
339 prt_printf(err, "%s: required value",
344 ret = match_string(opt->choices, -1, val);
347 prt_printf(err, "%s: invalid selection",
355 ret = opt->fn.parse(c, val, res, err);
358 prt_printf(err, "%s: parse error",
364 return bch2_opt_validate(opt, *res, err);
367 void bch2_opt_to_text(struct printbuf *out,
368 struct bch_fs *c, struct bch_sb *sb,
369 const struct bch_option *opt, u64 v,
372 if (flags & OPT_SHOW_MOUNT_STYLE) {
373 if (opt->type == BCH_OPT_BOOL) {
374 prt_printf(out, "%s%s",
380 prt_printf(out, "%s=", opt->attr.name);
386 if (opt->flags & OPT_HUMAN_READABLE)
387 prt_human_readable_u64(out, v);
389 prt_printf(out, "%lli", v);
392 if (flags & OPT_SHOW_FULL_LIST)
393 prt_string_option(out, opt->choices, v);
395 prt_str(out, opt->choices[v]);
398 opt->fn.to_text(out, c, sb, v);
405 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
410 case Opt_compression:
411 case Opt_background_compression:
412 ret = bch2_check_set_has_compressed_data(c, v);
414 case Opt_erasure_code:
416 bch2_check_set_feature(c, BCH_FEATURE_ec);
423 int bch2_opts_check_may_set(struct bch_fs *c)
428 for (i = 0; i < bch2_opts_nr; i++) {
429 ret = bch2_opt_check_may_set(c, i,
430 bch2_opt_get_by_id(&c->opts, i));
438 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
441 char *copied_opts, *copied_opts_start;
442 char *opt, *name, *val;
444 struct printbuf err = PRINTBUF;
451 * sys_fsconfig() is now occasionally providing us with option lists
452 * starting with a comma - weird.
457 copied_opts = kstrdup(options, GFP_KERNEL);
460 copied_opts_start = copied_opts;
462 while ((opt = strsep(&copied_opts, ",")) != NULL) {
463 name = strsep(&opt, "=");
466 id = bch2_mount_opt_lookup(name);
468 /* Check for the form "noopt", negation of a boolean opt: */
471 !strncmp("no", name, 2)) {
472 id = bch2_mount_opt_lookup(name + 2);
476 /* Unknown options are ignored: */
480 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
484 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
487 if ((id == Opt_usrquota ||
488 id == Opt_grpquota) &&
489 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
492 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
496 bch2_opt_set_by_id(opts, id, v);
503 pr_err("Bad mount option %s", name);
507 pr_err("Invalid mount option %s", err.buf);
511 kfree(copied_opts_start);
516 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
518 const struct bch_option *opt = bch2_opt_table + id;
523 if (opt->flags & OPT_SB_FIELD_ILOG2)
526 if (opt->flags & OPT_SB_FIELD_SECTORS)
533 * Initial options from superblock - here we don't want any options undefined,
534 * any options the superblock doesn't specify are set to 0:
536 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
540 for (id = 0; id < bch2_opts_nr; id++) {
541 const struct bch_option *opt = bch2_opt_table + id;
543 if (opt->get_sb == BCH2_NO_SB_OPT)
546 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
552 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
554 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
557 if (opt->flags & OPT_SB_FIELD_SECTORS)
560 if (opt->flags & OPT_SB_FIELD_ILOG2)
566 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
568 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
571 mutex_lock(&c->sb_lock);
572 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
574 mutex_unlock(&c->sb_lock);
579 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
581 return (struct bch_io_opts) {
582 #define x(_name, _bits) ._name = src._name,
588 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
590 static const enum bch_opt_id inode_opt_list[] = {
591 #define x(_name, _bits) Opt_##_name,
597 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
598 if (inode_opt_list[i] == id)