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_sb_features[] = {
24 const char * const bch2_sb_compat[] = {
29 const char * const bch2_btree_ids[] = {
31 "interior btree node",
35 const char * const bch2_csum_types[] = {
40 const char * const bch2_csum_opts[] = {
45 const char * const bch2_compression_types[] = {
46 BCH_COMPRESSION_TYPES()
50 const char * const bch2_compression_opts[] = {
51 BCH_COMPRESSION_OPTS()
55 const char * const bch2_str_hash_types[] = {
60 const char * const bch2_str_hash_opts[] = {
65 const char * const bch2_data_types[] = {
70 const char * const bch2_member_states[] = {
75 const char * const bch2_jset_entry_types[] = {
76 BCH_JSET_ENTRY_TYPES()
80 const char * const bch2_fs_usage_types[] = {
87 const char * const bch2_d_types[BCH_DT_MAX] = {
88 [DT_UNKNOWN] = "unknown",
96 [DT_WHT] = "whiteout",
97 [DT_SUBVOL] = "subvol",
100 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
105 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
110 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
112 #define x(_name, ...) \
113 if (opt_defined(src, _name)) \
114 opt_set(*dst, _name, src._name);
120 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
123 #define x(_name, ...) \
125 return opt_defined(*opts, _name);
133 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
136 #define x(_name, ...) \
146 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
149 #define x(_name, ...) \
151 opt_set(*opts, _name, v); \
160 const struct bch_option bch2_opt_table[] = {
161 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
162 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
163 .min = _min, .max = _max
164 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
165 .min = 0, .max = ARRAY_SIZE(_choices),\
167 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
168 .parse = _fn##_parse, \
169 .to_text = _fn##_to_text
171 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
175 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
181 .set_sb = SET_##_sb_opt, \
189 int bch2_opt_lookup(const char *name)
191 const struct bch_option *i;
193 for (i = bch2_opt_table;
194 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
196 if (!strcmp(name, i->attr.name))
197 return i - bch2_opt_table;
206 static const struct synonym bch_opt_synonyms[] = {
207 { "quota", "usrquota" },
210 static int bch2_mount_opt_lookup(const char *name)
212 const struct synonym *i;
214 for (i = bch_opt_synonyms;
215 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
217 if (!strcmp(name, i->s1))
220 return bch2_opt_lookup(name);
223 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
227 prt_printf(err, "%s: too small (min %llu)",
228 opt->attr.name, opt->min);
232 if (opt->max && v >= opt->max) {
234 prt_printf(err, "%s: too big (max %llu)",
235 opt->attr.name, opt->max);
239 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
241 prt_printf(err, "%s: not a multiple of 512",
246 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
248 prt_printf(err, "%s: must be a power of two",
256 int bch2_opt_parse(struct bch_fs *c,
257 const struct bch_option *opt,
258 const char *val, u64 *res,
259 struct printbuf *err)
265 ret = kstrtou64(val, 10, res);
266 if (ret < 0 || (*res != 0 && *res != 1)) {
268 prt_printf(err, "%s: must be bool",
274 ret = opt->flags & OPT_HUMAN_READABLE
275 ? bch2_strtou64_h(val, res)
276 : kstrtou64(val, 10, res);
279 prt_printf(err, "%s: must be a number",
285 ret = match_string(opt->choices, -1, val);
288 prt_printf(err, "%s: invalid selection",
299 ret = opt->parse(c, val, res);
302 prt_printf(err, "%s: parse error",
308 return bch2_opt_validate(opt, *res, err);
311 void bch2_opt_to_text(struct printbuf *out,
312 struct bch_fs *c, struct bch_sb *sb,
313 const struct bch_option *opt, u64 v,
316 if (flags & OPT_SHOW_MOUNT_STYLE) {
317 if (opt->type == BCH_OPT_BOOL) {
318 prt_printf(out, "%s%s",
324 prt_printf(out, "%s=", opt->attr.name);
330 if (opt->flags & OPT_HUMAN_READABLE)
331 prt_human_readable_u64(out, v);
333 prt_printf(out, "%lli", v);
336 if (flags & OPT_SHOW_FULL_LIST)
337 prt_string_option(out, opt->choices, v);
339 prt_printf(out, "%s", opt->choices[v]);
342 opt->to_text(out, c, sb, v);
349 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
354 case Opt_compression:
355 case Opt_background_compression:
356 ret = bch2_check_set_has_compressed_data(c, v);
358 case Opt_erasure_code:
360 bch2_check_set_feature(c, BCH_FEATURE_ec);
367 int bch2_opts_check_may_set(struct bch_fs *c)
372 for (i = 0; i < bch2_opts_nr; i++) {
373 ret = bch2_opt_check_may_set(c, i,
374 bch2_opt_get_by_id(&c->opts, i));
382 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
385 char *copied_opts, *copied_opts_start;
386 char *opt, *name, *val;
388 struct printbuf err = PRINTBUF;
394 copied_opts = kstrdup(options, GFP_KERNEL);
397 copied_opts_start = copied_opts;
399 while ((opt = strsep(&copied_opts, ",")) != NULL) {
400 name = strsep(&opt, "=");
404 id = bch2_mount_opt_lookup(name);
408 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
412 id = bch2_mount_opt_lookup(name);
416 !strncmp("no", name, 2)) {
417 id = bch2_mount_opt_lookup(name + 2);
424 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
428 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
432 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
435 if ((id == Opt_usrquota ||
436 id == Opt_grpquota) &&
437 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
440 bch2_opt_set_by_id(opts, id, v);
447 pr_err("Bad mount option %s", name);
451 pr_err("Invalid mount option %s", err.buf);
455 pr_err("Mount option %s requires a value", name);
459 kfree(copied_opts_start);
464 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
466 const struct bch_option *opt = bch2_opt_table + id;
471 if (opt->flags & OPT_SB_FIELD_ILOG2)
474 if (opt->flags & OPT_SB_FIELD_SECTORS)
481 * Initial options from superblock - here we don't want any options undefined,
482 * any options the superblock doesn't specify are set to 0:
484 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
488 for (id = 0; id < bch2_opts_nr; id++) {
489 const struct bch_option *opt = bch2_opt_table + id;
491 if (opt->get_sb == BCH2_NO_SB_OPT)
494 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
500 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
502 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
505 if (opt->flags & OPT_SB_FIELD_SECTORS)
508 if (opt->flags & OPT_SB_FIELD_ILOG2)
514 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
516 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
519 mutex_lock(&c->sb_lock);
520 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
522 mutex_unlock(&c->sb_lock);
527 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
529 return (struct bch_io_opts) {
530 #define x(_name, _bits) ._name = src._name,
536 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
538 static const enum bch_opt_id inode_opt_list[] = {
539 #define x(_name, _bits) Opt_##_name,
545 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
546 if (inode_opt_list[i] == id)