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[] = {
38 "interior btree node",
42 const char * const bch2_csum_types[] = {
47 const char * const bch2_csum_opts[] = {
52 const char * const bch2_compression_types[] = {
53 BCH_COMPRESSION_TYPES()
57 const char * const bch2_compression_opts[] = {
58 BCH_COMPRESSION_OPTS()
62 const char * const bch2_str_hash_types[] = {
67 const char * const bch2_str_hash_opts[] = {
72 const char * const bch2_data_types[] = {
77 const char * const bch2_member_states[] = {
82 const char * const bch2_jset_entry_types[] = {
83 BCH_JSET_ENTRY_TYPES()
87 const char * const bch2_fs_usage_types[] = {
94 const char * const bch2_d_types[BCH_DT_MAX] = {
95 [DT_UNKNOWN] = "unknown",
103 [DT_WHT] = "whiteout",
104 [DT_SUBVOL] = "subvol",
107 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
112 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
117 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
119 #define x(_name, ...) \
120 if (opt_defined(src, _name)) \
121 opt_set(*dst, _name, src._name);
127 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
130 #define x(_name, ...) \
132 return opt_defined(*opts, _name);
140 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
143 #define x(_name, ...) \
153 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
156 #define x(_name, ...) \
158 opt_set(*opts, _name, v); \
167 const struct bch_option bch2_opt_table[] = {
168 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
169 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
170 .min = _min, .max = _max
171 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
172 .min = 0, .max = ARRAY_SIZE(_choices),\
174 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
175 .parse = _fn##_parse, \
176 .to_text = _fn##_to_text
178 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
182 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
188 .set_sb = SET_##_sb_opt, \
196 int bch2_opt_lookup(const char *name)
198 const struct bch_option *i;
200 for (i = bch2_opt_table;
201 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
203 if (!strcmp(name, i->attr.name))
204 return i - bch2_opt_table;
213 static const struct synonym bch_opt_synonyms[] = {
214 { "quota", "usrquota" },
217 static int bch2_mount_opt_lookup(const char *name)
219 const struct synonym *i;
221 for (i = bch_opt_synonyms;
222 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
224 if (!strcmp(name, i->s1))
227 return bch2_opt_lookup(name);
230 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
234 prt_printf(err, "%s: too small (min %llu)",
235 opt->attr.name, opt->min);
239 if (opt->max && v >= opt->max) {
241 prt_printf(err, "%s: too big (max %llu)",
242 opt->attr.name, opt->max);
246 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
248 prt_printf(err, "%s: not a multiple of 512",
253 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
255 prt_printf(err, "%s: must be a power of two",
263 int bch2_opt_parse(struct bch_fs *c,
264 const struct bch_option *opt,
265 const char *val, u64 *res,
266 struct printbuf *err)
272 ret = kstrtou64(val, 10, res);
273 if (ret < 0 || (*res != 0 && *res != 1)) {
275 prt_printf(err, "%s: must be bool",
281 ret = opt->flags & OPT_HUMAN_READABLE
282 ? bch2_strtou64_h(val, res)
283 : kstrtou64(val, 10, res);
286 prt_printf(err, "%s: must be a number",
292 ret = match_string(opt->choices, -1, val);
295 prt_printf(err, "%s: invalid selection",
306 ret = opt->parse(c, val, res);
309 prt_printf(err, "%s: parse error",
315 return bch2_opt_validate(opt, *res, err);
318 void bch2_opt_to_text(struct printbuf *out,
319 struct bch_fs *c, struct bch_sb *sb,
320 const struct bch_option *opt, u64 v,
323 if (flags & OPT_SHOW_MOUNT_STYLE) {
324 if (opt->type == BCH_OPT_BOOL) {
325 prt_printf(out, "%s%s",
331 prt_printf(out, "%s=", opt->attr.name);
337 if (opt->flags & OPT_HUMAN_READABLE)
338 prt_human_readable_u64(out, v);
340 prt_printf(out, "%lli", v);
343 if (flags & OPT_SHOW_FULL_LIST)
344 prt_string_option(out, opt->choices, v);
346 prt_printf(out, "%s", opt->choices[v]);
349 opt->to_text(out, c, sb, v);
356 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
361 case Opt_compression:
362 case Opt_background_compression:
363 ret = bch2_check_set_has_compressed_data(c, v);
365 case Opt_erasure_code:
367 bch2_check_set_feature(c, BCH_FEATURE_ec);
374 int bch2_opts_check_may_set(struct bch_fs *c)
379 for (i = 0; i < bch2_opts_nr; i++) {
380 ret = bch2_opt_check_may_set(c, i,
381 bch2_opt_get_by_id(&c->opts, i));
389 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
392 char *copied_opts, *copied_opts_start;
393 char *opt, *name, *val;
395 struct printbuf err = PRINTBUF;
401 copied_opts = kstrdup(options, GFP_KERNEL);
404 copied_opts_start = copied_opts;
406 while ((opt = strsep(&copied_opts, ",")) != NULL) {
407 name = strsep(&opt, "=");
411 id = bch2_mount_opt_lookup(name);
415 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
419 id = bch2_mount_opt_lookup(name);
423 !strncmp("no", name, 2)) {
424 id = bch2_mount_opt_lookup(name + 2);
431 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
435 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
439 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
442 if ((id == Opt_usrquota ||
443 id == Opt_grpquota) &&
444 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
447 bch2_opt_set_by_id(opts, id, v);
454 pr_err("Bad mount option %s", name);
458 pr_err("Invalid mount option %s", err.buf);
462 pr_err("Mount option %s requires a value", name);
466 kfree(copied_opts_start);
471 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
473 const struct bch_option *opt = bch2_opt_table + id;
478 if (opt->flags & OPT_SB_FIELD_ILOG2)
481 if (opt->flags & OPT_SB_FIELD_SECTORS)
488 * Initial options from superblock - here we don't want any options undefined,
489 * any options the superblock doesn't specify are set to 0:
491 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
495 for (id = 0; id < bch2_opts_nr; id++) {
496 const struct bch_option *opt = bch2_opt_table + id;
498 if (opt->get_sb == BCH2_NO_SB_OPT)
501 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
507 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
509 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
512 if (opt->flags & OPT_SB_FIELD_SECTORS)
515 if (opt->flags & OPT_SB_FIELD_ILOG2)
521 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
523 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
526 mutex_lock(&c->sb_lock);
527 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
529 mutex_unlock(&c->sb_lock);
534 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
536 return (struct bch_io_opts) {
537 #define x(_name, _bits) ._name = src._name,
543 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
545 static const enum bch_opt_id inode_opt_list[] = {
546 #define x(_name, _bits) Opt_##_name,
552 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
553 if (inode_opt_list[i] == id)