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)) {
274 prt_printf(err, "%s: must be bool",
280 ret = opt->flags & OPT_HUMAN_READABLE
281 ? bch2_strtou64_h(val, res)
282 : kstrtou64(val, 10, res);
285 prt_printf(err, "%s: must be a number",
291 ret = match_string(opt->choices, -1, val);
294 prt_printf(err, "%s: invalid selection",
305 ret = opt->parse(c, val, res);
308 prt_printf(err, "%s: parse error",
314 return bch2_opt_validate(opt, *res, err);
317 void bch2_opt_to_text(struct printbuf *out,
318 struct bch_fs *c, struct bch_sb *sb,
319 const struct bch_option *opt, u64 v,
322 if (flags & OPT_SHOW_MOUNT_STYLE) {
323 if (opt->type == BCH_OPT_BOOL) {
324 prt_printf(out, "%s%s",
330 prt_printf(out, "%s=", opt->attr.name);
336 if (opt->flags & OPT_HUMAN_READABLE)
337 prt_human_readable_u64(out, v);
339 prt_printf(out, "%lli", v);
342 if (flags & OPT_SHOW_FULL_LIST)
343 prt_string_option(out, opt->choices, v);
345 prt_printf(out, "%s", opt->choices[v]);
348 opt->to_text(out, c, sb, v);
355 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
360 case Opt_compression:
361 case Opt_background_compression:
362 ret = bch2_check_set_has_compressed_data(c, v);
364 case Opt_erasure_code:
366 bch2_check_set_feature(c, BCH_FEATURE_ec);
373 int bch2_opts_check_may_set(struct bch_fs *c)
378 for (i = 0; i < bch2_opts_nr; i++) {
379 ret = bch2_opt_check_may_set(c, i,
380 bch2_opt_get_by_id(&c->opts, i));
388 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
391 char *copied_opts, *copied_opts_start;
392 char *opt, *name, *val;
394 struct printbuf err = PRINTBUF;
400 copied_opts = kstrdup(options, GFP_KERNEL);
403 copied_opts_start = copied_opts;
405 while ((opt = strsep(&copied_opts, ",")) != NULL) {
406 name = strsep(&opt, "=");
410 id = bch2_mount_opt_lookup(name);
414 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
418 id = bch2_mount_opt_lookup(name);
422 !strncmp("no", name, 2)) {
423 id = bch2_mount_opt_lookup(name + 2);
430 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
434 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
438 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
441 if ((id == Opt_usrquota ||
442 id == Opt_grpquota) &&
443 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
446 bch2_opt_set_by_id(opts, id, v);
453 pr_err("Bad mount option %s", name);
457 pr_err("Invalid mount option %s", err.buf);
461 pr_err("Mount option %s requires a value", name);
465 kfree(copied_opts_start);
470 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
472 const struct bch_option *opt = bch2_opt_table + id;
477 if (opt->flags & OPT_SB_FIELD_ILOG2)
480 if (opt->flags & OPT_SB_FIELD_SECTORS)
487 * Initial options from superblock - here we don't want any options undefined,
488 * any options the superblock doesn't specify are set to 0:
490 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
494 for (id = 0; id < bch2_opts_nr; id++) {
495 const struct bch_option *opt = bch2_opt_table + id;
497 if (opt->get_sb == BCH2_NO_SB_OPT)
500 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
506 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
508 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
511 if (opt->flags & OPT_SB_FIELD_SECTORS)
514 if (opt->flags & OPT_SB_FIELD_ILOG2)
520 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
522 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
525 mutex_lock(&c->sb_lock);
526 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
528 mutex_unlock(&c->sb_lock);
533 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
535 struct bch_io_opts ret = { 0 };
536 #define x(_name, _bits) \
537 if (opt_defined(src, _name)) \
538 opt_set(ret, _name, src._name);
544 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
546 struct bch_opts ret = { 0 };
547 #define x(_name, _bits) \
548 if (opt_defined(src, _name)) \
549 opt_set(ret, _name, src._name);
555 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
557 #define x(_name, _bits) \
558 if (opt_defined(src, _name)) \
559 opt_set(*dst, _name, src._name);
564 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
566 static const enum bch_opt_id inode_opt_list[] = {
567 #define x(_name, _bits) Opt_##_name,
573 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
574 if (inode_opt_list[i] == id)