1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
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[] = {
34 const char * const bch2_csum_types[] = {
39 const char * const bch2_csum_opts[] = {
44 const char * const bch2_compression_types[] = {
45 BCH_COMPRESSION_TYPES()
49 const char * const bch2_compression_opts[] = {
50 BCH_COMPRESSION_OPTS()
54 const char * const bch2_str_hash_types[] = {
59 const char * const bch2_str_hash_opts[] = {
64 const char * const bch2_data_types[] = {
69 const char * const bch2_cache_replacement_policies[] = {
70 BCH_CACHE_REPLACEMENT_POLICIES()
74 const char * const bch2_member_states[] = {
81 const char * const bch2_d_types[BCH_DT_MAX] = {
82 [DT_UNKNOWN] = "unknown",
90 [DT_WHT] = "whiteout",
91 [DT_SUBVOL] = "subvol",
94 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
96 #define x(_name, ...) \
97 if (opt_defined(src, _name)) \
98 opt_set(*dst, _name, src._name);
104 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
107 #define x(_name, ...) \
109 return opt_defined(*opts, _name);
117 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
120 #define x(_name, ...) \
130 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
133 #define x(_name, ...) \
135 opt_set(*opts, _name, v); \
144 const struct bch_option bch2_opt_table[] = {
145 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
146 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
147 .min = _min, .max = _max
148 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
149 .min = 0, .max = ARRAY_SIZE(_choices),\
151 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
152 .parse = _fn##_parse, \
153 .to_text = _fn##_to_text
155 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
159 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
165 .set_sb = SET_##_sb_opt, \
173 int bch2_opt_lookup(const char *name)
175 const struct bch_option *i;
177 for (i = bch2_opt_table;
178 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
180 if (!strcmp(name, i->attr.name))
181 return i - bch2_opt_table;
190 static const struct synonym bch_opt_synonyms[] = {
191 { "quota", "usrquota" },
194 static int bch2_mount_opt_lookup(const char *name)
196 const struct synonym *i;
198 for (i = bch_opt_synonyms;
199 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
201 if (!strcmp(name, i->s1))
204 return bch2_opt_lookup(name);
207 static int bch2_opt_validate(const struct bch_option *opt, const char *msg, u64 v)
211 pr_err("invalid %s%s: too small (min %llu)",
212 msg, opt->attr.name, opt->min);
216 if (opt->max && v >= opt->max) {
218 pr_err("invalid %s%s: too big (max %llu)",
219 msg, opt->attr.name, opt->max);
223 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
225 pr_err("invalid %s %s: not a multiple of 512",
226 msg, opt->attr.name);
230 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
232 pr_err("invalid %s%s: must be a power of two",
233 msg, opt->attr.name);
240 int bch2_opt_parse(struct bch_fs *c, const char *msg,
241 const struct bch_option *opt,
242 const char *val, u64 *res)
248 ret = kstrtou64(val, 10, res);
253 ret = opt->flags & OPT_HUMAN_READABLE
254 ? bch2_strtou64_h(val, res)
255 : kstrtou64(val, 10, res);
260 ret = match_string(opt->choices, -1, val);
270 ret = opt->parse(c, val, res);
275 return bch2_opt_validate(opt, msg, *res);
278 void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
279 const struct bch_option *opt, u64 v,
282 if (flags & OPT_SHOW_MOUNT_STYLE) {
283 if (opt->type == BCH_OPT_BOOL) {
290 pr_buf(out, "%s=", opt->attr.name);
296 if (opt->flags & OPT_HUMAN_READABLE)
299 pr_buf(out, "%lli", v);
302 if (flags & OPT_SHOW_FULL_LIST)
303 bch2_string_opt_to_text(out, opt->choices, v);
305 pr_buf(out, opt->choices[v]);
308 opt->to_text(out, c, v);
315 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
320 case Opt_compression:
321 case Opt_background_compression:
322 ret = bch2_check_set_has_compressed_data(c, v);
324 case Opt_erasure_code:
326 bch2_check_set_feature(c, BCH_FEATURE_ec);
333 int bch2_opts_check_may_set(struct bch_fs *c)
338 for (i = 0; i < bch2_opts_nr; i++) {
339 ret = bch2_opt_check_may_set(c, i,
340 bch2_opt_get_by_id(&c->opts, i));
348 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
351 char *copied_opts, *copied_opts_start;
352 char *opt, *name, *val;
359 copied_opts = kstrdup(options, GFP_KERNEL);
362 copied_opts_start = copied_opts;
364 while ((opt = strsep(&copied_opts, ",")) != NULL) {
365 name = strsep(&opt, "=");
369 id = bch2_mount_opt_lookup(name);
373 ret = bch2_opt_parse(c, "mount option ",
374 &bch2_opt_table[id], val, &v);
378 id = bch2_mount_opt_lookup(name);
382 !strncmp("no", name, 2)) {
383 id = bch2_mount_opt_lookup(name + 2);
390 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
394 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
398 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
401 if ((id == Opt_usrquota ||
402 id == Opt_grpquota) &&
403 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
406 bch2_opt_set_by_id(opts, id, v);
413 pr_err("Bad mount option %s", name);
417 pr_err("Invalid value %s for mount option %s", val, name);
421 pr_err("Mount option %s requires a value", name);
425 kfree(copied_opts_start);
430 * Initial options from superblock - here we don't want any options undefined,
431 * any options the superblock doesn't specify are set to 0:
433 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
438 for (id = 0; id < bch2_opts_nr; id++) {
439 const struct bch_option *opt = bch2_opt_table + id;
442 if (opt->get_sb == NO_SB_OPT)
447 if (opt->flags & OPT_SB_FIELD_ILOG2)
450 if (opt->flags & OPT_SB_FIELD_SECTORS)
453 ret = bch2_opt_validate(opt, "superblock option ", v);
457 bch2_opt_set_by_id(opts, id, v);
463 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
465 if (opt->set_sb == SET_NO_SB_OPT)
468 if (opt->flags & OPT_SB_FIELD_SECTORS)
471 if (opt->flags & OPT_SB_FIELD_ILOG2)
477 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
479 if (opt->set_sb == SET_NO_SB_OPT)
482 mutex_lock(&c->sb_lock);
483 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
485 mutex_unlock(&c->sb_lock);
490 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
492 struct bch_io_opts ret = { 0 };
493 #define x(_name, _bits) \
494 if (opt_defined(src, _name)) \
495 opt_set(ret, _name, src._name);
501 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
503 struct bch_opts ret = { 0 };
504 #define x(_name, _bits) \
505 if (opt_defined(src, _name)) \
506 opt_set(ret, _name, src._name);
512 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
514 #define x(_name, _bits) \
515 if (opt_defined(src, _name)) \
516 opt_set(*dst, _name, src._name);
521 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
523 static const enum bch_opt_id inode_opt_list[] = {
524 #define x(_name, _bits) Opt_##_name,
530 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
531 if (inode_opt_list[i] == id)