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_iops_measurements[] = {
16 BCH_IOPS_MEASUREMENTS()
20 const char * const bch2_error_actions[] = {
25 const char * const bch2_fsck_fix_opts[] = {
30 const char * const bch2_version_upgrade_opts[] = {
31 BCH_VERSION_UPGRADE_OPTS()
35 const char * const bch2_sb_features[] = {
40 const char * const bch2_sb_compat[] = {
45 const char * const __bch2_btree_ids[] = {
50 const char * const bch2_csum_types[] = {
55 const char * const bch2_csum_opts[] = {
60 const char * const bch2_compression_types[] = {
61 BCH_COMPRESSION_TYPES()
65 const char * const bch2_compression_opts[] = {
66 BCH_COMPRESSION_OPTS()
70 const char * const bch2_str_hash_types[] = {
75 const char * const bch2_str_hash_opts[] = {
80 const char * const bch2_data_types[] = {
85 const char * const bch2_member_states[] = {
90 const char * const bch2_jset_entry_types[] = {
91 BCH_JSET_ENTRY_TYPES()
95 const char * const bch2_fs_usage_types[] = {
102 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
103 struct printbuf *err)
108 int ret = match_string(bch2_fsck_fix_opts, -1, val);
111 prt_str(err, "fix_errors: invalid selection");
120 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
125 prt_str(out, bch2_fsck_fix_opts[v]);
128 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
129 .parse = bch2_opt_fix_errors_parse, \
130 .to_text = bch2_opt_fix_errors_to_text, \
133 const char * const bch2_d_types[BCH_DT_MAX] = {
134 [DT_UNKNOWN] = "unknown",
142 [DT_WHT] = "whiteout",
143 [DT_SUBVOL] = "subvol",
146 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
151 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
156 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
158 #define x(_name, ...) \
159 if (opt_defined(src, _name)) \
160 opt_set(*dst, _name, src._name);
166 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
169 #define x(_name, ...) \
171 return opt_defined(*opts, _name);
179 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
182 #define x(_name, ...) \
192 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
195 #define x(_name, ...) \
197 opt_set(*opts, _name, v); \
206 const struct bch_option bch2_opt_table[] = {
207 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
208 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
209 .min = _min, .max = _max
210 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
211 .min = 0, .max = ARRAY_SIZE(_choices), \
213 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
215 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
219 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
225 .set_sb = SET_##_sb_opt, \
233 int bch2_opt_lookup(const char *name)
235 const struct bch_option *i;
237 for (i = bch2_opt_table;
238 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
240 if (!strcmp(name, i->attr.name))
241 return i - bch2_opt_table;
250 static const struct synonym bch_opt_synonyms[] = {
251 { "quota", "usrquota" },
254 static int bch2_mount_opt_lookup(const char *name)
256 const struct synonym *i;
258 for (i = bch_opt_synonyms;
259 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
261 if (!strcmp(name, i->s1))
264 return bch2_opt_lookup(name);
267 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
271 prt_printf(err, "%s: too small (min %llu)",
272 opt->attr.name, opt->min);
276 if (opt->max && v >= opt->max) {
278 prt_printf(err, "%s: too big (max %llu)",
279 opt->attr.name, opt->max);
283 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
285 prt_printf(err, "%s: not a multiple of 512",
290 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
292 prt_printf(err, "%s: must be a power of two",
300 int bch2_opt_parse(struct bch_fs *c,
301 const struct bch_option *opt,
302 const char *val, u64 *res,
303 struct printbuf *err)
310 ret = kstrtou64(val, 10, res);
316 if (ret < 0 || (*res != 0 && *res != 1)) {
318 prt_printf(err, "%s: must be bool", opt->attr.name);
324 prt_printf(err, "%s: required value",
329 ret = opt->flags & OPT_HUMAN_READABLE
330 ? bch2_strtou64_h(val, res)
331 : kstrtou64(val, 10, res);
334 prt_printf(err, "%s: must be a number",
341 prt_printf(err, "%s: required value",
346 ret = match_string(opt->choices, -1, val);
349 prt_printf(err, "%s: invalid selection",
357 ret = opt->fn.parse(c, val, res, err);
360 prt_printf(err, "%s: parse error",
366 return bch2_opt_validate(opt, *res, err);
369 void bch2_opt_to_text(struct printbuf *out,
370 struct bch_fs *c, struct bch_sb *sb,
371 const struct bch_option *opt, u64 v,
374 if (flags & OPT_SHOW_MOUNT_STYLE) {
375 if (opt->type == BCH_OPT_BOOL) {
376 prt_printf(out, "%s%s",
382 prt_printf(out, "%s=", opt->attr.name);
388 if (opt->flags & OPT_HUMAN_READABLE)
389 prt_human_readable_u64(out, v);
391 prt_printf(out, "%lli", v);
394 if (flags & OPT_SHOW_FULL_LIST)
395 prt_string_option(out, opt->choices, v);
397 prt_str(out, opt->choices[v]);
400 opt->fn.to_text(out, c, sb, v);
407 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
412 case Opt_compression:
413 case Opt_background_compression:
414 ret = bch2_check_set_has_compressed_data(c, v);
416 case Opt_erasure_code:
418 bch2_check_set_feature(c, BCH_FEATURE_ec);
425 int bch2_opts_check_may_set(struct bch_fs *c)
430 for (i = 0; i < bch2_opts_nr; i++) {
431 ret = bch2_opt_check_may_set(c, i,
432 bch2_opt_get_by_id(&c->opts, i));
440 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
443 char *copied_opts, *copied_opts_start;
444 char *opt, *name, *val;
446 struct printbuf err = PRINTBUF;
453 * sys_fsconfig() is now occasionally providing us with option lists
454 * starting with a comma - weird.
459 copied_opts = kstrdup(options, GFP_KERNEL);
462 copied_opts_start = copied_opts;
464 while ((opt = strsep(&copied_opts, ",")) != NULL) {
465 name = strsep(&opt, "=");
468 id = bch2_mount_opt_lookup(name);
470 /* Check for the form "noopt", negation of a boolean opt: */
473 !strncmp("no", name, 2)) {
474 id = bch2_mount_opt_lookup(name + 2);
478 /* Unknown options are ignored: */
482 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
486 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
489 if ((id == Opt_usrquota ||
490 id == Opt_grpquota) &&
491 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
494 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
498 bch2_opt_set_by_id(opts, id, v);
505 pr_err("Bad mount option %s", name);
509 pr_err("Invalid mount option %s", err.buf);
513 kfree(copied_opts_start);
518 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
520 const struct bch_option *opt = bch2_opt_table + id;
525 if (opt->flags & OPT_SB_FIELD_ILOG2)
528 if (opt->flags & OPT_SB_FIELD_SECTORS)
535 * Initial options from superblock - here we don't want any options undefined,
536 * any options the superblock doesn't specify are set to 0:
538 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
542 for (id = 0; id < bch2_opts_nr; id++) {
543 const struct bch_option *opt = bch2_opt_table + id;
545 if (opt->get_sb == BCH2_NO_SB_OPT)
548 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
554 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
556 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
559 if (opt->flags & OPT_SB_FIELD_SECTORS)
562 if (opt->flags & OPT_SB_FIELD_ILOG2)
568 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
570 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
573 mutex_lock(&c->sb_lock);
574 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
576 mutex_unlock(&c->sb_lock);
581 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
583 return (struct bch_io_opts) {
584 #define x(_name, _bits) ._name = src._name,
590 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
592 static const enum bch_opt_id inode_opt_list[] = {
593 #define x(_name, _bits) Opt_##_name,
599 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
600 if (inode_opt_list[i] == id)