1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
12 #define x(t, n) [n] = #t,
14 const char * const bch2_error_actions[] = {
19 const char * const bch2_version_upgrade_opts[] = {
20 BCH_VERSION_UPGRADE_OPTS()
24 const char * const bch2_sb_features[] = {
29 const char * const bch2_sb_compat[] = {
34 const char * const bch2_btree_ids[] = {
36 "interior btree node",
40 const char * const bch2_csum_types[] = {
45 const char * const bch2_csum_opts[] = {
50 const char * const bch2_compression_types[] = {
51 BCH_COMPRESSION_TYPES()
55 const char * const bch2_compression_opts[] = {
56 BCH_COMPRESSION_OPTS()
60 const char * const bch2_str_hash_types[] = {
65 const char * const bch2_str_hash_opts[] = {
70 const char * const bch2_data_types[] = {
75 const char * const bch2_member_states[] = {
80 const char * const bch2_jset_entry_types[] = {
81 BCH_JSET_ENTRY_TYPES()
85 const char * const bch2_fs_usage_types[] = {
92 const char * const bch2_d_types[BCH_DT_MAX] = {
93 [DT_UNKNOWN] = "unknown",
101 [DT_WHT] = "whiteout",
102 [DT_SUBVOL] = "subvol",
105 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
110 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
115 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
117 #define x(_name, ...) \
118 if (opt_defined(src, _name)) \
119 opt_set(*dst, _name, src._name);
125 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
128 #define x(_name, ...) \
130 return opt_defined(*opts, _name);
138 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
141 #define x(_name, ...) \
151 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
154 #define x(_name, ...) \
156 opt_set(*opts, _name, v); \
165 const struct bch_option bch2_opt_table[] = {
166 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
167 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
168 .min = _min, .max = _max
169 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
170 .min = 0, .max = ARRAY_SIZE(_choices),\
172 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
173 .parse = _fn##_parse, \
174 .to_text = _fn##_to_text
176 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
180 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
186 .set_sb = SET_##_sb_opt, \
194 int bch2_opt_lookup(const char *name)
196 const struct bch_option *i;
198 for (i = bch2_opt_table;
199 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
201 if (!strcmp(name, i->attr.name))
202 return i - bch2_opt_table;
211 static const struct synonym bch_opt_synonyms[] = {
212 { "quota", "usrquota" },
215 static int bch2_mount_opt_lookup(const char *name)
217 const struct synonym *i;
219 for (i = bch_opt_synonyms;
220 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
222 if (!strcmp(name, i->s1))
225 return bch2_opt_lookup(name);
228 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
232 prt_printf(err, "%s: too small (min %llu)",
233 opt->attr.name, opt->min);
237 if (opt->max && v >= opt->max) {
239 prt_printf(err, "%s: too big (max %llu)",
240 opt->attr.name, opt->max);
244 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
246 prt_printf(err, "%s: not a multiple of 512",
251 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
253 prt_printf(err, "%s: must be a power of two",
261 int bch2_opt_parse(struct bch_fs *c,
262 const struct bch_option *opt,
263 const char *val, u64 *res,
264 struct printbuf *err)
270 ret = kstrtou64(val, 10, res);
271 if (ret < 0 || (*res != 0 && *res != 1)) {
273 prt_printf(err, "%s: must be bool",
279 ret = opt->flags & OPT_HUMAN_READABLE
280 ? bch2_strtou64_h(val, res)
281 : kstrtou64(val, 10, res);
284 prt_printf(err, "%s: must be a number",
290 ret = match_string(opt->choices, -1, val);
293 prt_printf(err, "%s: invalid selection",
304 ret = opt->parse(c, val, res);
307 prt_printf(err, "%s: parse error",
313 return bch2_opt_validate(opt, *res, err);
316 void bch2_opt_to_text(struct printbuf *out,
317 struct bch_fs *c, struct bch_sb *sb,
318 const struct bch_option *opt, u64 v,
321 if (flags & OPT_SHOW_MOUNT_STYLE) {
322 if (opt->type == BCH_OPT_BOOL) {
323 prt_printf(out, "%s%s",
329 prt_printf(out, "%s=", opt->attr.name);
335 if (opt->flags & OPT_HUMAN_READABLE)
336 prt_human_readable_u64(out, v);
338 prt_printf(out, "%lli", v);
341 if (flags & OPT_SHOW_FULL_LIST)
342 prt_string_option(out, opt->choices, v);
344 prt_printf(out, "%s", opt->choices[v]);
347 opt->to_text(out, c, sb, v);
354 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
359 case Opt_compression:
360 case Opt_background_compression:
361 ret = bch2_check_set_has_compressed_data(c, v);
363 case Opt_erasure_code:
365 bch2_check_set_feature(c, BCH_FEATURE_ec);
372 int bch2_opts_check_may_set(struct bch_fs *c)
377 for (i = 0; i < bch2_opts_nr; i++) {
378 ret = bch2_opt_check_may_set(c, i,
379 bch2_opt_get_by_id(&c->opts, i));
387 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
390 char *copied_opts, *copied_opts_start;
391 char *opt, *name, *val;
393 struct printbuf err = PRINTBUF;
399 copied_opts = kstrdup(options, GFP_KERNEL);
402 copied_opts_start = copied_opts;
404 while ((opt = strsep(&copied_opts, ",")) != NULL) {
405 name = strsep(&opt, "=");
409 id = bch2_mount_opt_lookup(name);
413 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
417 id = bch2_mount_opt_lookup(name);
421 !strncmp("no", name, 2)) {
422 id = bch2_mount_opt_lookup(name + 2);
429 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
433 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
437 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
440 if ((id == Opt_usrquota ||
441 id == Opt_grpquota) &&
442 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
445 bch2_opt_set_by_id(opts, id, v);
452 pr_err("Bad mount option %s", name);
456 pr_err("Invalid mount option %s", err.buf);
460 pr_err("Mount option %s requires a value", name);
464 kfree(copied_opts_start);
469 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
471 const struct bch_option *opt = bch2_opt_table + id;
476 if (opt->flags & OPT_SB_FIELD_ILOG2)
479 if (opt->flags & OPT_SB_FIELD_SECTORS)
486 * Initial options from superblock - here we don't want any options undefined,
487 * any options the superblock doesn't specify are set to 0:
489 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
493 for (id = 0; id < bch2_opts_nr; id++) {
494 const struct bch_option *opt = bch2_opt_table + id;
496 if (opt->get_sb == BCH2_NO_SB_OPT)
499 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
505 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
507 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
510 if (opt->flags & OPT_SB_FIELD_SECTORS)
513 if (opt->flags & OPT_SB_FIELD_ILOG2)
519 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
521 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
524 mutex_lock(&c->sb_lock);
525 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
527 mutex_unlock(&c->sb_lock);
532 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
534 return (struct bch_io_opts) {
535 #define x(_name, _bits) ._name = src._name,
541 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
543 static const enum bch_opt_id inode_opt_list[] = {
544 #define x(_name, _bits) Opt_##_name,
550 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
551 if (inode_opt_list[i] == id)