1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
12 #include <linux/pretty-printers.h>
14 #define x(t, n) [n] = #t,
16 const char * const bch2_metadata_versions[] = {
17 BCH_METADATA_VERSIONS()
21 const char * const bch2_error_actions[] = {
26 const char * const bch2_sb_features[] = {
31 const char * const bch2_sb_compat[] = {
36 const char * const bch2_btree_ids[] = {
41 const char * const bch2_csum_types[] = {
46 const char * const bch2_csum_opts[] = {
51 const char * const bch2_compression_types[] = {
52 BCH_COMPRESSION_TYPES()
56 const char * const bch2_compression_opts[] = {
57 BCH_COMPRESSION_OPTS()
61 const char * const bch2_str_hash_types[] = {
66 const char * const bch2_str_hash_opts[] = {
71 const char * const bch2_data_types[] = {
76 const char * const bch2_member_states[] = {
81 const char * const bch2_jset_entry_types[] = {
82 BCH_JSET_ENTRY_TYPES()
86 const char * const bch2_fs_usage_types[] = {
93 const char * const bch2_d_types[BCH_DT_MAX] = {
94 [DT_UNKNOWN] = "unknown",
102 [DT_WHT] = "whiteout",
103 [DT_SUBVOL] = "subvol",
106 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
111 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
116 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
118 #define x(_name, ...) \
119 if (opt_defined(src, _name)) \
120 opt_set(*dst, _name, src._name);
126 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
129 #define x(_name, ...) \
131 return opt_defined(*opts, _name);
139 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
142 #define x(_name, ...) \
152 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
155 #define x(_name, ...) \
157 opt_set(*opts, _name, v); \
166 const struct bch_option bch2_opt_table[] = {
167 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
168 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
169 .min = _min, .max = _max
170 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
171 .min = 0, .max = ARRAY_SIZE(_choices),\
173 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
174 .parse = _fn##_parse, \
175 .to_text = _fn##_to_text
177 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
181 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
187 .set_sb = SET_##_sb_opt, \
195 int bch2_opt_lookup(const char *name)
197 const struct bch_option *i;
199 for (i = bch2_opt_table;
200 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
202 if (!strcmp(name, i->attr.name))
203 return i - bch2_opt_table;
212 static const struct synonym bch_opt_synonyms[] = {
213 { "quota", "usrquota" },
216 static int bch2_mount_opt_lookup(const char *name)
218 const struct synonym *i;
220 for (i = bch_opt_synonyms;
221 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
223 if (!strcmp(name, i->s1))
226 return bch2_opt_lookup(name);
229 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
233 prt_printf(err, "%s: too small (min %llu)",
234 opt->attr.name, opt->min);
238 if (opt->max && v >= opt->max) {
240 prt_printf(err, "%s: too big (max %llu)",
241 opt->attr.name, opt->max);
245 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
247 prt_printf(err, "%s: not a multiple of 512",
252 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
254 prt_printf(err, "%s: must be a power of two",
262 int bch2_opt_parse(struct bch_fs *c,
263 const struct bch_option *opt,
264 const char *val, u64 *res,
265 struct printbuf *err)
271 ret = kstrtou64(val, 10, res);
272 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 struct bch_io_opts ret = { 0 };
535 #define x(_name, _bits) \
536 if (opt_defined(src, _name)) \
537 opt_set(ret, _name, src._name);
543 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
545 struct bch_opts ret = { 0 };
546 #define x(_name, _bits) \
547 if (opt_defined(src, _name)) \
548 opt_set(ret, _name, src._name);
554 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
556 #define x(_name, _bits) \
557 if (opt_defined(src, _name)) \
558 opt_set(*dst, _name, src._name);
563 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
565 static const enum bch_opt_id inode_opt_list[] = {
566 #define x(_name, _bits) Opt_##_name,
572 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
573 if (inode_opt_list[i] == id)