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