2 #include <linux/kernel.h>
6 #include "disk_groups.h"
11 const char * const bch2_error_actions[] = {
18 const char * const bch2_csum_types[] = {
25 const char * const bch2_compression_types[] = {
33 const char * const bch2_str_hash_types[] = {
40 const char * const bch2_data_types[] = {
50 const char * const bch2_cache_replacement_policies[] = {
57 /* Default is -1; we skip past it for struct cached_dev's cache mode */
58 const char * const bch2_cache_modes[] = {
67 const char * const bch2_dev_state[] = {
75 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
77 #define BCH_OPT(_name, ...) \
78 if (opt_defined(src, _name)) \
79 opt_set(*dst, _name, src._name);
85 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
88 #define BCH_OPT(_name, ...) \
90 return opt_defined(*opts, _name);
98 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
101 #define BCH_OPT(_name, ...) \
111 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
114 #define BCH_OPT(_name, ...) \
116 opt_set(*opts, _name, v); \
126 * Initial options from superblock - here we don't want any options undefined,
127 * any options the superblock doesn't specify are set to 0:
129 struct bch_opts bch2_opts_from_sb(struct bch_sb *sb)
131 struct bch_opts opts = bch2_opts_empty();
133 #define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default) \
134 if (_sb_opt != NO_SB_OPT) \
135 opt_set(opts, _name, _sb_opt(sb));
142 const struct bch_option bch2_opt_table[] = {
143 #define OPT_BOOL() .type = BCH_OPT_BOOL
144 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, .min = _min, .max = _max
145 #define OPT_STR(_choices) .type = BCH_OPT_STR, .choices = _choices
146 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
147 .parse = _fn##_parse, \
148 .to_text = _fn##_to_text
150 #define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default) \
154 .mode = _mode == OPT_RUNTIME ? 0644 : 0444, \
157 .set_sb = SET_##_sb_opt, \
165 int bch2_opt_lookup(const char *name)
167 const struct bch_option *i;
169 for (i = bch2_opt_table;
170 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
172 if (!strcmp(name, i->attr.name))
173 return i - bch2_opt_table;
182 static const struct synonym bch_opt_synonyms[] = {
183 { "quota", "usrquota" },
186 static int bch2_mount_opt_lookup(const char *name)
188 const struct synonym *i;
190 for (i = bch_opt_synonyms;
191 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
193 if (!strcmp(name, i->s1))
196 return bch2_opt_lookup(name);
199 int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
200 const char *val, u64 *res)
206 ret = kstrtou64(val, 10, res);
214 ret = kstrtou64(val, 10, res);
218 if (*res < opt->min || *res >= opt->max)
222 ret = match_string(opt->choices, -1, val);
232 return opt->parse(c, val, res);
238 void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
239 const struct bch_option *opt, u64 v,
242 if (flags & OPT_SHOW_MOUNT_STYLE) {
243 if (opt->type == BCH_OPT_BOOL) {
250 pr_buf(out, "%s=", opt->attr.name);
256 pr_buf(out, "%lli", v);
259 if (flags & OPT_SHOW_FULL_LIST)
260 bch2_string_opt_to_text(out, opt->choices, v);
262 pr_buf(out, opt->choices[v]);
265 opt->to_text(out, c, v);
272 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
277 case Opt_compression:
278 case Opt_background_compression:
279 ret = bch2_check_set_has_compressed_data(c, v);
281 case Opt_erasure_code:
283 !(c->sb.features & (1ULL << BCH_FEATURE_EC))) {
284 mutex_lock(&c->sb_lock);
285 c->disk_sb.sb->features[0] |=
286 cpu_to_le64(1ULL << BCH_FEATURE_EC);
289 mutex_unlock(&c->sb_lock);
297 int bch2_opts_check_may_set(struct bch_fs *c)
302 for (i = 0; i < bch2_opts_nr; i++) {
303 ret = bch2_opt_check_may_set(c, i,
304 bch2_opt_get_by_id(&c->opts, i));
312 int bch2_parse_mount_opts(struct bch_opts *opts, char *options)
314 char *opt, *name, *val;
318 while ((opt = strsep(&options, ",")) != NULL) {
319 name = strsep(&opt, "=");
323 id = bch2_mount_opt_lookup(name);
327 ret = bch2_opt_parse(NULL, &bch2_opt_table[id], val, &v);
331 id = bch2_mount_opt_lookup(name);
335 !strncmp("no", name, 2)) {
336 id = bch2_mount_opt_lookup(name + 2);
343 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
347 if (bch2_opt_table[id].mode < OPT_MOUNT)
351 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
354 if ((id == Opt_usrquota ||
355 id == Opt_grpquota) &&
356 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
359 bch2_opt_set_by_id(opts, id, v);
364 pr_err("Bad mount option %s", name);
367 pr_err("Invalid value %s for mount option %s", val, name);
370 pr_err("Mount option %s requires a value", name);
376 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
378 struct bch_io_opts ret = { 0 };
379 #define x(_name, _bits) \
380 if (opt_defined(src, _name)) \
381 opt_set(ret, _name, src._name);
387 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
389 struct bch_opts ret = { 0 };
390 #define x(_name, _bits) \
391 if (opt_defined(src, _name)) \
392 opt_set(ret, _name, src._name);
398 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
400 #define x(_name, _bits) \
401 if (opt_defined(src, _name)) \
402 opt_set(*dst, _name, src._name);
407 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
409 static const enum bch_opt_id inode_opt_list[] = {
410 #define x(_name, _bits) Opt_##_name,
416 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
417 if (inode_opt_list[i] == id)