xref: /linux/fs/bcachefs/opts.c (revision 06a130e42a5bfc84795464bff023bff4c16f58c5)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kernel.h>
4 
5 #include "bcachefs.h"
6 #include "compress.h"
7 #include "disk_groups.h"
8 #include "error.h"
9 #include "opts.h"
10 #include "recovery_passes.h"
11 #include "super-io.h"
12 #include "util.h"
13 
14 #define x(t, n, ...) [n] = #t,
15 
16 const char * const bch2_error_actions[] = {
17 	BCH_ERROR_ACTIONS()
18 	NULL
19 };
20 
21 const char * const bch2_fsck_fix_opts[] = {
22 	BCH_FIX_ERRORS_OPTS()
23 	NULL
24 };
25 
26 const char * const bch2_version_upgrade_opts[] = {
27 	BCH_VERSION_UPGRADE_OPTS()
28 	NULL
29 };
30 
31 const char * const bch2_sb_features[] = {
32 	BCH_SB_FEATURES()
33 	NULL
34 };
35 
36 const char * const bch2_sb_compat[] = {
37 	BCH_SB_COMPAT()
38 	NULL
39 };
40 
41 const char * const __bch2_btree_ids[] = {
42 	BCH_BTREE_IDS()
43 	NULL
44 };
45 
46 static const char * const __bch2_csum_types[] = {
47 	BCH_CSUM_TYPES()
48 	NULL
49 };
50 
51 const char * const bch2_csum_opts[] = {
52 	BCH_CSUM_OPTS()
53 	NULL
54 };
55 
56 static const char * const __bch2_compression_types[] = {
57 	BCH_COMPRESSION_TYPES()
58 	NULL
59 };
60 
61 const char * const bch2_compression_opts[] = {
62 	BCH_COMPRESSION_OPTS()
63 	NULL
64 };
65 
66 const char * const bch2_str_hash_types[] = {
67 	BCH_STR_HASH_TYPES()
68 	NULL
69 };
70 
71 const char * const bch2_str_hash_opts[] = {
72 	BCH_STR_HASH_OPTS()
73 	NULL
74 };
75 
76 const char * const __bch2_data_types[] = {
77 	BCH_DATA_TYPES()
78 	NULL
79 };
80 
81 const char * const bch2_member_states[] = {
82 	BCH_MEMBER_STATES()
83 	NULL
84 };
85 
86 static const char * const __bch2_jset_entry_types[] = {
87 	BCH_JSET_ENTRY_TYPES()
88 	NULL
89 };
90 
91 static const char * const __bch2_fs_usage_types[] = {
92 	BCH_FS_USAGE_TYPES()
93 	NULL
94 };
95 
96 #undef x
97 
98 static void prt_str_opt_boundscheck(struct printbuf *out, const char * const opts[],
99 				    unsigned nr, const char *type, unsigned idx)
100 {
101 	if (idx < nr)
102 		prt_str(out, opts[idx]);
103 	else
104 		prt_printf(out, "(unknown %s %u)", type, idx);
105 }
106 
107 #define PRT_STR_OPT_BOUNDSCHECKED(name, type)					\
108 void bch2_prt_##name(struct printbuf *out, type t)				\
109 {										\
110 	prt_str_opt_boundscheck(out, __bch2_##name##s, ARRAY_SIZE(__bch2_##name##s) - 1, #name, t);\
111 }
112 
113 PRT_STR_OPT_BOUNDSCHECKED(jset_entry_type,	enum bch_jset_entry_type);
114 PRT_STR_OPT_BOUNDSCHECKED(fs_usage_type,	enum bch_fs_usage_type);
115 PRT_STR_OPT_BOUNDSCHECKED(data_type,		enum bch_data_type);
116 PRT_STR_OPT_BOUNDSCHECKED(csum_type,		enum bch_csum_type);
117 PRT_STR_OPT_BOUNDSCHECKED(compression_type,	enum bch_compression_type);
118 
119 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
120 				     struct printbuf *err)
121 {
122 	if (!val) {
123 		*res = FSCK_FIX_yes;
124 	} else {
125 		int ret = match_string(bch2_fsck_fix_opts, -1, val);
126 
127 		if (ret < 0 && err)
128 			prt_str(err, "fix_errors: invalid selection");
129 		if (ret < 0)
130 			return ret;
131 		*res = ret;
132 	}
133 
134 	return 0;
135 }
136 
137 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
138 					struct bch_fs *c,
139 					struct bch_sb *sb,
140 					u64 v)
141 {
142 	prt_str(out, bch2_fsck_fix_opts[v]);
143 }
144 
145 #define bch2_opt_fix_errors (struct bch_opt_fn) {	\
146 	.parse = bch2_opt_fix_errors_parse,		\
147 	.to_text = bch2_opt_fix_errors_to_text,		\
148 }
149 
150 const char * const bch2_d_types[BCH_DT_MAX] = {
151 	[DT_UNKNOWN]	= "unknown",
152 	[DT_FIFO]	= "fifo",
153 	[DT_CHR]	= "chr",
154 	[DT_DIR]	= "dir",
155 	[DT_BLK]	= "blk",
156 	[DT_REG]	= "reg",
157 	[DT_LNK]	= "lnk",
158 	[DT_SOCK]	= "sock",
159 	[DT_WHT]	= "whiteout",
160 	[DT_SUBVOL]	= "subvol",
161 };
162 
163 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
164 {
165 	BUG();
166 }
167 
168 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
169 {
170 	BUG();
171 }
172 
173 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
174 {
175 #define x(_name, ...)						\
176 	if (opt_defined(src, _name))					\
177 		opt_set(*dst, _name, src._name);
178 
179 	BCH_OPTS()
180 #undef x
181 }
182 
183 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
184 {
185 	switch (id) {
186 #define x(_name, ...)						\
187 	case Opt_##_name:						\
188 		return opt_defined(*opts, _name);
189 	BCH_OPTS()
190 #undef x
191 	default:
192 		BUG();
193 	}
194 }
195 
196 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
197 {
198 	switch (id) {
199 #define x(_name, ...)						\
200 	case Opt_##_name:						\
201 		return opts->_name;
202 	BCH_OPTS()
203 #undef x
204 	default:
205 		BUG();
206 	}
207 }
208 
209 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
210 {
211 	switch (id) {
212 #define x(_name, ...)						\
213 	case Opt_##_name:						\
214 		opt_set(*opts, _name, v);				\
215 		break;
216 	BCH_OPTS()
217 #undef x
218 	default:
219 		BUG();
220 	}
221 }
222 
223 const struct bch_option bch2_opt_table[] = {
224 #define OPT_BOOL()		.type = BCH_OPT_BOOL, .min = 0, .max = 2
225 #define OPT_UINT(_min, _max)	.type = BCH_OPT_UINT,			\
226 				.min = _min, .max = _max
227 #define OPT_STR(_choices)	.type = BCH_OPT_STR,			\
228 				.min = 0, .max = ARRAY_SIZE(_choices),	\
229 				.choices = _choices
230 #define OPT_STR_NOLIMIT(_choices)	.type = BCH_OPT_STR,		\
231 				.min = 0, .max = U64_MAX,		\
232 				.choices = _choices
233 #define OPT_BITFIELD(_choices)	.type = BCH_OPT_BITFIELD,		\
234 				.choices = _choices
235 #define OPT_FN(_fn)		.type = BCH_OPT_FN, .fn	= _fn
236 
237 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help)	\
238 	[Opt_##_name] = {						\
239 		.attr	= {						\
240 			.name	= #_name,				\
241 			.mode = (_flags) & OPT_RUNTIME ? 0644 : 0444,	\
242 		},							\
243 		.flags	= _flags,					\
244 		.hint	= _hint,					\
245 		.help	= _help,					\
246 		.get_sb = _sb_opt,					\
247 		.set_sb	= SET_##_sb_opt,				\
248 		_type							\
249 	},
250 
251 	BCH_OPTS()
252 #undef x
253 };
254 
255 int bch2_opt_lookup(const char *name)
256 {
257 	const struct bch_option *i;
258 
259 	for (i = bch2_opt_table;
260 	     i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
261 	     i++)
262 		if (!strcmp(name, i->attr.name))
263 			return i - bch2_opt_table;
264 
265 	return -1;
266 }
267 
268 struct synonym {
269 	const char	*s1, *s2;
270 };
271 
272 static const struct synonym bch_opt_synonyms[] = {
273 	{ "quota",	"usrquota" },
274 };
275 
276 static int bch2_mount_opt_lookup(const char *name)
277 {
278 	const struct synonym *i;
279 
280 	for (i = bch_opt_synonyms;
281 	     i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
282 	     i++)
283 		if (!strcmp(name, i->s1))
284 			name = i->s2;
285 
286 	return bch2_opt_lookup(name);
287 }
288 
289 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
290 {
291 	if (v < opt->min) {
292 		if (err)
293 			prt_printf(err, "%s: too small (min %llu)",
294 			       opt->attr.name, opt->min);
295 		return -BCH_ERR_ERANGE_option_too_small;
296 	}
297 
298 	if (opt->max && v >= opt->max) {
299 		if (err)
300 			prt_printf(err, "%s: too big (max %llu)",
301 			       opt->attr.name, opt->max);
302 		return -BCH_ERR_ERANGE_option_too_big;
303 	}
304 
305 	if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
306 		if (err)
307 			prt_printf(err, "%s: not a multiple of 512",
308 			       opt->attr.name);
309 		return -BCH_ERR_opt_parse_error;
310 	}
311 
312 	if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
313 		if (err)
314 			prt_printf(err, "%s: must be a power of two",
315 			       opt->attr.name);
316 		return -BCH_ERR_opt_parse_error;
317 	}
318 
319 	if (opt->fn.validate)
320 		return opt->fn.validate(v, err);
321 
322 	return 0;
323 }
324 
325 int bch2_opt_parse(struct bch_fs *c,
326 		   const struct bch_option *opt,
327 		   const char *val, u64 *res,
328 		   struct printbuf *err)
329 {
330 	ssize_t ret;
331 
332 	switch (opt->type) {
333 	case BCH_OPT_BOOL:
334 		if (val) {
335 			ret = kstrtou64(val, 10, res);
336 		} else {
337 			ret = 0;
338 			*res = 1;
339 		}
340 
341 		if (ret < 0 || (*res != 0 && *res != 1)) {
342 			if (err)
343 				prt_printf(err, "%s: must be bool", opt->attr.name);
344 			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
345 		}
346 		break;
347 	case BCH_OPT_UINT:
348 		if (!val) {
349 			prt_printf(err, "%s: required value",
350 				   opt->attr.name);
351 			return -EINVAL;
352 		}
353 
354 		ret = opt->flags & OPT_HUMAN_READABLE
355 			? bch2_strtou64_h(val, res)
356 			: kstrtou64(val, 10, res);
357 		if (ret < 0) {
358 			if (err)
359 				prt_printf(err, "%s: must be a number",
360 					   opt->attr.name);
361 			return ret;
362 		}
363 		break;
364 	case BCH_OPT_STR:
365 		if (!val) {
366 			prt_printf(err, "%s: required value",
367 				   opt->attr.name);
368 			return -EINVAL;
369 		}
370 
371 		ret = match_string(opt->choices, -1, val);
372 		if (ret < 0) {
373 			if (err)
374 				prt_printf(err, "%s: invalid selection",
375 					   opt->attr.name);
376 			return ret;
377 		}
378 
379 		*res = ret;
380 		break;
381 	case BCH_OPT_BITFIELD: {
382 		s64 v = bch2_read_flag_list(val, opt->choices);
383 		if (v < 0)
384 			return v;
385 		*res = v;
386 		break;
387 	}
388 	case BCH_OPT_FN:
389 		ret = opt->fn.parse(c, val, res, err);
390 
391 		if (ret == -BCH_ERR_option_needs_open_fs)
392 			return ret;
393 
394 		if (ret < 0) {
395 			if (err)
396 				prt_printf(err, "%s: parse error",
397 					   opt->attr.name);
398 			return ret;
399 		}
400 	}
401 
402 	return bch2_opt_validate(opt, *res, err);
403 }
404 
405 void bch2_opt_to_text(struct printbuf *out,
406 		      struct bch_fs *c, struct bch_sb *sb,
407 		      const struct bch_option *opt, u64 v,
408 		      unsigned flags)
409 {
410 	if (flags & OPT_SHOW_MOUNT_STYLE) {
411 		if (opt->type == BCH_OPT_BOOL) {
412 			prt_printf(out, "%s%s",
413 			       v ? "" : "no",
414 			       opt->attr.name);
415 			return;
416 		}
417 
418 		prt_printf(out, "%s=", opt->attr.name);
419 	}
420 
421 	switch (opt->type) {
422 	case BCH_OPT_BOOL:
423 	case BCH_OPT_UINT:
424 		if (opt->flags & OPT_HUMAN_READABLE)
425 			prt_human_readable_u64(out, v);
426 		else
427 			prt_printf(out, "%lli", v);
428 		break;
429 	case BCH_OPT_STR:
430 		if (v < opt->min || v >= opt->max - 1)
431 			prt_printf(out, "(invalid option %lli)", v);
432 		else if (flags & OPT_SHOW_FULL_LIST)
433 			prt_string_option(out, opt->choices, v);
434 		else
435 			prt_str(out, opt->choices[v]);
436 		break;
437 	case BCH_OPT_BITFIELD:
438 		prt_bitflags(out, opt->choices, v);
439 		break;
440 	case BCH_OPT_FN:
441 		opt->fn.to_text(out, c, sb, v);
442 		break;
443 	default:
444 		BUG();
445 	}
446 }
447 
448 void bch2_opts_to_text(struct printbuf *out,
449 		       struct bch_opts opts,
450 		       struct bch_fs *c, struct bch_sb *sb,
451 		       unsigned show_mask, unsigned hide_mask,
452 		       unsigned flags)
453 {
454 	bool first = true;
455 
456 	for (enum bch_opt_id i = 0; i < bch2_opts_nr; i++) {
457 		const struct bch_option *opt = &bch2_opt_table[i];
458 
459 		if ((opt->flags & hide_mask) || !(opt->flags & show_mask))
460 			continue;
461 
462 		u64 v = bch2_opt_get_by_id(&opts, i);
463 		if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
464 			continue;
465 
466 		if (!first)
467 			prt_char(out, ',');
468 		first = false;
469 
470 		bch2_opt_to_text(out, c, sb, opt, v, flags);
471 	}
472 }
473 
474 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
475 {
476 	int ret = 0;
477 
478 	switch (id) {
479 	case Opt_compression:
480 	case Opt_background_compression:
481 		ret = bch2_check_set_has_compressed_data(c, v);
482 		break;
483 	case Opt_erasure_code:
484 		if (v)
485 			bch2_check_set_feature(c, BCH_FEATURE_ec);
486 		break;
487 	}
488 
489 	return ret;
490 }
491 
492 int bch2_opts_check_may_set(struct bch_fs *c)
493 {
494 	unsigned i;
495 	int ret;
496 
497 	for (i = 0; i < bch2_opts_nr; i++) {
498 		ret = bch2_opt_check_may_set(c, i,
499 				bch2_opt_get_by_id(&c->opts, i));
500 		if (ret)
501 			return ret;
502 	}
503 
504 	return 0;
505 }
506 
507 int bch2_parse_one_mount_opt(struct bch_fs *c, struct bch_opts *opts,
508 			     struct printbuf *parse_later,
509 			     const char *name, const char *val)
510 {
511 	struct printbuf err = PRINTBUF;
512 	u64 v;
513 	int ret, id;
514 
515 	id = bch2_mount_opt_lookup(name);
516 
517 	/* Check for the form "noopt", negation of a boolean opt: */
518 	if (id < 0 &&
519 	    !val &&
520 	    !strncmp("no", name, 2)) {
521 		id = bch2_mount_opt_lookup(name + 2);
522 		val = "0";
523 	}
524 
525 	/* Unknown options are ignored: */
526 	if (id < 0)
527 		return 0;
528 
529 	if (!(bch2_opt_table[id].flags & OPT_MOUNT))
530 		goto bad_opt;
531 
532 	if (id == Opt_acl &&
533 	    !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
534 		goto bad_opt;
535 
536 	if ((id == Opt_usrquota ||
537 	     id == Opt_grpquota) &&
538 	    !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
539 		goto bad_opt;
540 
541 	ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
542 	if (ret == -BCH_ERR_option_needs_open_fs && parse_later) {
543 		prt_printf(parse_later, "%s=%s,", name, val);
544 		if (parse_later->allocation_failure) {
545 			ret = -ENOMEM;
546 			goto out;
547 		}
548 
549 		ret = 0;
550 		goto out;
551 	}
552 
553 	if (ret < 0)
554 		goto bad_val;
555 
556 	if (opts)
557 		bch2_opt_set_by_id(opts, id, v);
558 
559 	ret = 0;
560 	goto out;
561 
562 bad_opt:
563 	pr_err("Bad mount option %s", name);
564 	ret = -BCH_ERR_option_name;
565 	goto out;
566 
567 bad_val:
568 	pr_err("Invalid mount option %s", err.buf);
569 	ret = -BCH_ERR_option_value;
570 
571 out:
572 	printbuf_exit(&err);
573 	return ret;
574 }
575 
576 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
577 			  struct printbuf *parse_later, char *options)
578 {
579 	char *copied_opts, *copied_opts_start;
580 	char *opt, *name, *val;
581 	int ret;
582 
583 	if (!options)
584 		return 0;
585 
586 	/*
587 	 * sys_fsconfig() is now occasionally providing us with option lists
588 	 * starting with a comma - weird.
589 	 */
590 	if (*options == ',')
591 		options++;
592 
593 	copied_opts = kstrdup(options, GFP_KERNEL);
594 	if (!copied_opts)
595 		return -ENOMEM;
596 	copied_opts_start = copied_opts;
597 
598 	while ((opt = strsep(&copied_opts, ",")) != NULL) {
599 		name	= strsep(&opt, "=");
600 		val	= opt;
601 
602 		ret = bch2_parse_one_mount_opt(c, opts, parse_later, name, val);
603 		if (ret < 0)
604 			goto out;
605 	}
606 
607 	ret = 0;
608 	goto out;
609 
610 out:
611 	kfree(copied_opts_start);
612 	return ret;
613 }
614 
615 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
616 {
617 	const struct bch_option *opt = bch2_opt_table + id;
618 	u64 v;
619 
620 	v = opt->get_sb(sb);
621 
622 	if (opt->flags & OPT_SB_FIELD_ILOG2)
623 		v = 1ULL << v;
624 
625 	if (opt->flags & OPT_SB_FIELD_SECTORS)
626 		v <<= 9;
627 
628 	return v;
629 }
630 
631 /*
632  * Initial options from superblock - here we don't want any options undefined,
633  * any options the superblock doesn't specify are set to 0:
634  */
635 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
636 {
637 	unsigned id;
638 
639 	for (id = 0; id < bch2_opts_nr; id++) {
640 		const struct bch_option *opt = bch2_opt_table + id;
641 
642 		if (opt->get_sb == BCH2_NO_SB_OPT)
643 			continue;
644 
645 		bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
646 	}
647 
648 	return 0;
649 }
650 
651 struct bch_dev_sb_opt_set {
652 	void			(*set_sb)(struct bch_member *, u64);
653 };
654 
655 static const struct bch_dev_sb_opt_set bch2_dev_sb_opt_setters [] = {
656 #define x(n, set)	[Opt_##n] = { .set_sb = SET_##set },
657 	BCH_DEV_OPT_SETTERS()
658 #undef x
659 };
660 
661 void __bch2_opt_set_sb(struct bch_sb *sb, int dev_idx,
662 		       const struct bch_option *opt, u64 v)
663 {
664 	enum bch_opt_id id = opt - bch2_opt_table;
665 
666 	if (opt->flags & OPT_SB_FIELD_SECTORS)
667 		v >>= 9;
668 
669 	if (opt->flags & OPT_SB_FIELD_ILOG2)
670 		v = ilog2(v);
671 
672 	if (opt->flags & OPT_SB_FIELD_ONE_BIAS)
673 		v++;
674 
675 	if (opt->flags & OPT_FS) {
676 		if (opt->set_sb != SET_BCH2_NO_SB_OPT)
677 			opt->set_sb(sb, v);
678 	}
679 
680 	if ((opt->flags & OPT_DEVICE) && dev_idx >= 0) {
681 		if (WARN(!bch2_member_exists(sb, dev_idx),
682 			 "tried to set device option %s on nonexistent device %i",
683 			 opt->attr.name, dev_idx))
684 			return;
685 
686 		struct bch_member *m = bch2_members_v2_get_mut(sb, dev_idx);
687 
688 		const struct bch_dev_sb_opt_set *set = bch2_dev_sb_opt_setters + id;
689 		if (set->set_sb)
690 			set->set_sb(m, v);
691 		else
692 			pr_err("option %s cannot be set via opt_set_sb()", opt->attr.name);
693 	}
694 }
695 
696 void bch2_opt_set_sb(struct bch_fs *c, struct bch_dev *ca,
697 		     const struct bch_option *opt, u64 v)
698 {
699 	mutex_lock(&c->sb_lock);
700 	__bch2_opt_set_sb(c->disk_sb.sb, ca ? ca->dev_idx : -1, opt, v);
701 	bch2_write_super(c);
702 	mutex_unlock(&c->sb_lock);
703 }
704 
705 /* io opts: */
706 
707 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
708 {
709 	return (struct bch_io_opts) {
710 #define x(_name, _bits)	._name = src._name,
711 	BCH_INODE_OPTS()
712 #undef x
713 	};
714 }
715 
716 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
717 {
718 	static const enum bch_opt_id inode_opt_list[] = {
719 #define x(_name, _bits)	Opt_##_name,
720 	BCH_INODE_OPTS()
721 #undef x
722 	};
723 	unsigned i;
724 
725 	for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
726 		if (inode_opt_list[i] == id)
727 			return true;
728 
729 	return false;
730 }
731