1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "checksum.h" 5 #include "counters.h" 6 #include "disk_groups.h" 7 #include "ec.h" 8 #include "error.h" 9 #include "journal.h" 10 #include "journal_sb.h" 11 #include "journal_seq_blacklist.h" 12 #include "recovery.h" 13 #include "replicas.h" 14 #include "quota.h" 15 #include "sb-clean.h" 16 #include "sb-downgrade.h" 17 #include "sb-errors.h" 18 #include "sb-members.h" 19 #include "super-io.h" 20 #include "super.h" 21 #include "trace.h" 22 #include "vstructs.h" 23 24 #include <linux/backing-dev.h> 25 #include <linux/sort.h> 26 27 static const struct blk_holder_ops bch2_sb_handle_bdev_ops = { 28 }; 29 30 struct bch2_metadata_version { 31 u16 version; 32 const char *name; 33 u64 recovery_passes; 34 }; 35 36 static const struct bch2_metadata_version bch2_metadata_versions[] = { 37 #define x(n, v, _recovery_passes) { \ 38 .version = v, \ 39 .name = #n, \ 40 .recovery_passes = _recovery_passes, \ 41 }, 42 BCH_METADATA_VERSIONS() 43 #undef x 44 }; 45 46 void bch2_version_to_text(struct printbuf *out, unsigned v) 47 { 48 const char *str = "(unknown version)"; 49 50 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++) 51 if (bch2_metadata_versions[i].version == v) { 52 str = bch2_metadata_versions[i].name; 53 break; 54 } 55 56 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str); 57 } 58 59 unsigned bch2_latest_compatible_version(unsigned v) 60 { 61 if (!BCH_VERSION_MAJOR(v)) 62 return v; 63 64 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++) 65 if (bch2_metadata_versions[i].version > v && 66 BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) == 67 BCH_VERSION_MAJOR(v)) 68 v = bch2_metadata_versions[i].version; 69 70 return v; 71 } 72 73 u64 bch2_upgrade_recovery_passes(struct bch_fs *c, 74 unsigned old_version, 75 unsigned new_version) 76 { 77 u64 ret = 0; 78 79 for (const struct bch2_metadata_version *i = bch2_metadata_versions; 80 i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions); 81 i++) 82 if (i->version > old_version && i->version <= new_version) { 83 if (i->recovery_passes & RECOVERY_PASS_ALL_FSCK) 84 ret |= bch2_fsck_recovery_passes(); 85 ret |= i->recovery_passes; 86 } 87 88 return ret &= ~RECOVERY_PASS_ALL_FSCK; 89 } 90 91 const char * const bch2_sb_fields[] = { 92 #define x(name, nr) #name, 93 BCH_SB_FIELDS() 94 #undef x 95 NULL 96 }; 97 98 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *, 99 struct printbuf *); 100 101 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *sb, 102 enum bch_sb_field_type type) 103 { 104 struct bch_sb_field *f; 105 106 /* XXX: need locking around superblock to access optional fields */ 107 108 vstruct_for_each(sb, f) 109 if (le32_to_cpu(f->type) == type) 110 return f; 111 return NULL; 112 } 113 114 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb, 115 struct bch_sb_field *f, 116 unsigned u64s) 117 { 118 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0; 119 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s; 120 121 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size); 122 123 if (!f && !u64s) { 124 /* nothing to do: */ 125 } else if (!f) { 126 f = vstruct_last(sb->sb); 127 memset(f, 0, sizeof(u64) * u64s); 128 f->u64s = cpu_to_le32(u64s); 129 f->type = 0; 130 } else { 131 void *src, *dst; 132 133 src = vstruct_end(f); 134 135 if (u64s) { 136 f->u64s = cpu_to_le32(u64s); 137 dst = vstruct_end(f); 138 } else { 139 dst = f; 140 } 141 142 memmove(dst, src, vstruct_end(sb->sb) - src); 143 144 if (dst > src) 145 memset(src, 0, dst - src); 146 } 147 148 sb->sb->u64s = cpu_to_le32(sb_u64s); 149 150 return u64s ? f : NULL; 151 } 152 153 void bch2_sb_field_delete(struct bch_sb_handle *sb, 154 enum bch_sb_field_type type) 155 { 156 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type); 157 158 if (f) 159 __bch2_sb_field_resize(sb, f, 0); 160 } 161 162 /* Superblock realloc/free: */ 163 164 void bch2_free_super(struct bch_sb_handle *sb) 165 { 166 kfree(sb->bio); 167 if (!IS_ERR_OR_NULL(sb->bdev)) 168 blkdev_put(sb->bdev, sb->holder); 169 kfree(sb->holder); 170 kfree(sb->sb_name); 171 172 kfree(sb->sb); 173 memset(sb, 0, sizeof(*sb)); 174 } 175 176 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s) 177 { 178 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s); 179 size_t new_buffer_size; 180 struct bch_sb *new_sb; 181 struct bio *bio; 182 183 if (sb->bdev) 184 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev)); 185 186 new_buffer_size = roundup_pow_of_two(new_bytes); 187 188 if (sb->sb && sb->buffer_size >= new_buffer_size) 189 return 0; 190 191 if (sb->sb && sb->have_layout) { 192 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits; 193 194 if (new_bytes > max_bytes) { 195 pr_err("%pg: superblock too big: want %zu but have %llu", 196 sb->bdev, new_bytes, max_bytes); 197 return -BCH_ERR_ENOSPC_sb; 198 } 199 } 200 201 if (sb->buffer_size >= new_buffer_size && sb->sb) 202 return 0; 203 204 if (dynamic_fault("bcachefs:add:super_realloc")) 205 return -BCH_ERR_ENOMEM_sb_realloc_injected; 206 207 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO); 208 if (!new_sb) 209 return -BCH_ERR_ENOMEM_sb_buf_realloc; 210 211 sb->sb = new_sb; 212 213 if (sb->have_bio) { 214 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size); 215 216 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL); 217 if (!bio) 218 return -BCH_ERR_ENOMEM_sb_bio_realloc; 219 220 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0); 221 222 kfree(sb->bio); 223 sb->bio = bio; 224 } 225 226 sb->buffer_size = new_buffer_size; 227 228 return 0; 229 } 230 231 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *sb, 232 enum bch_sb_field_type type, 233 unsigned u64s) 234 { 235 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type); 236 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0; 237 ssize_t d = -old_u64s + u64s; 238 239 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) 240 return NULL; 241 242 if (sb->fs_sb) { 243 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb); 244 struct bch_dev *ca; 245 unsigned i; 246 247 lockdep_assert_held(&c->sb_lock); 248 249 /* XXX: we're not checking that offline device have enough space */ 250 251 for_each_online_member(ca, c, i) { 252 struct bch_sb_handle *dev_sb = &ca->disk_sb; 253 254 if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) { 255 percpu_ref_put(&ca->ref); 256 return NULL; 257 } 258 } 259 } 260 261 f = bch2_sb_field_get_id(sb->sb, type); 262 f = __bch2_sb_field_resize(sb, f, u64s); 263 if (f) 264 f->type = cpu_to_le32(type); 265 return f; 266 } 267 268 struct bch_sb_field *bch2_sb_field_get_minsize_id(struct bch_sb_handle *sb, 269 enum bch_sb_field_type type, 270 unsigned u64s) 271 { 272 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type); 273 274 if (!f || le32_to_cpu(f->u64s) < u64s) 275 f = bch2_sb_field_resize_id(sb, type, u64s); 276 return f; 277 } 278 279 /* Superblock validate: */ 280 281 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out) 282 { 283 u64 offset, prev_offset, max_sectors; 284 unsigned i; 285 286 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512); 287 288 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) && 289 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) { 290 prt_printf(out, "Not a bcachefs superblock layout"); 291 return -BCH_ERR_invalid_sb_layout; 292 } 293 294 if (layout->layout_type != 0) { 295 prt_printf(out, "Invalid superblock layout type %u", 296 layout->layout_type); 297 return -BCH_ERR_invalid_sb_layout_type; 298 } 299 300 if (!layout->nr_superblocks) { 301 prt_printf(out, "Invalid superblock layout: no superblocks"); 302 return -BCH_ERR_invalid_sb_layout_nr_superblocks; 303 } 304 305 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) { 306 prt_printf(out, "Invalid superblock layout: too many superblocks"); 307 return -BCH_ERR_invalid_sb_layout_nr_superblocks; 308 } 309 310 max_sectors = 1 << layout->sb_max_size_bits; 311 312 prev_offset = le64_to_cpu(layout->sb_offset[0]); 313 314 for (i = 1; i < layout->nr_superblocks; i++) { 315 offset = le64_to_cpu(layout->sb_offset[i]); 316 317 if (offset < prev_offset + max_sectors) { 318 prt_printf(out, "Invalid superblock layout: superblocks overlap\n" 319 " (sb %u ends at %llu next starts at %llu", 320 i - 1, prev_offset + max_sectors, offset); 321 return -BCH_ERR_invalid_sb_layout_superblocks_overlap; 322 } 323 prev_offset = offset; 324 } 325 326 return 0; 327 } 328 329 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out) 330 { 331 u16 version = le16_to_cpu(sb->version); 332 u16 version_min = le16_to_cpu(sb->version_min); 333 334 if (!bch2_version_compatible(version)) { 335 prt_str(out, "Unsupported superblock version "); 336 bch2_version_to_text(out, version); 337 prt_str(out, " (min "); 338 bch2_version_to_text(out, bcachefs_metadata_version_min); 339 prt_str(out, ", max "); 340 bch2_version_to_text(out, bcachefs_metadata_version_current); 341 prt_str(out, ")"); 342 return -BCH_ERR_invalid_sb_version; 343 } 344 345 if (!bch2_version_compatible(version_min)) { 346 prt_str(out, "Unsupported superblock version_min "); 347 bch2_version_to_text(out, version_min); 348 prt_str(out, " (min "); 349 bch2_version_to_text(out, bcachefs_metadata_version_min); 350 prt_str(out, ", max "); 351 bch2_version_to_text(out, bcachefs_metadata_version_current); 352 prt_str(out, ")"); 353 return -BCH_ERR_invalid_sb_version; 354 } 355 356 if (version_min > version) { 357 prt_str(out, "Bad minimum version "); 358 bch2_version_to_text(out, version_min); 359 prt_str(out, ", greater than version field "); 360 bch2_version_to_text(out, version); 361 return -BCH_ERR_invalid_sb_version; 362 } 363 364 return 0; 365 } 366 367 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out, 368 int rw) 369 { 370 struct bch_sb *sb = disk_sb->sb; 371 struct bch_sb_field *f; 372 struct bch_sb_field_members_v1 *mi; 373 enum bch_opt_id opt_id; 374 u16 block_size; 375 int ret; 376 377 ret = bch2_sb_compatible(sb, out); 378 if (ret) 379 return ret; 380 381 if (sb->features[1] || 382 (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) { 383 prt_printf(out, "Filesystem has incompatible features"); 384 return -BCH_ERR_invalid_sb_features; 385 } 386 387 block_size = le16_to_cpu(sb->block_size); 388 389 if (block_size > PAGE_SECTORS) { 390 prt_printf(out, "Block size too big (got %u, max %u)", 391 block_size, PAGE_SECTORS); 392 return -BCH_ERR_invalid_sb_block_size; 393 } 394 395 if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) { 396 prt_printf(out, "Bad user UUID (got zeroes)"); 397 return -BCH_ERR_invalid_sb_uuid; 398 } 399 400 if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) { 401 prt_printf(out, "Bad internal UUID (got zeroes)"); 402 return -BCH_ERR_invalid_sb_uuid; 403 } 404 405 if (!sb->nr_devices || 406 sb->nr_devices > BCH_SB_MEMBERS_MAX) { 407 prt_printf(out, "Bad number of member devices %u (max %u)", 408 sb->nr_devices, BCH_SB_MEMBERS_MAX); 409 return -BCH_ERR_invalid_sb_too_many_members; 410 } 411 412 if (sb->dev_idx >= sb->nr_devices) { 413 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)", 414 sb->dev_idx, sb->nr_devices); 415 return -BCH_ERR_invalid_sb_dev_idx; 416 } 417 418 if (!sb->time_precision || 419 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) { 420 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)", 421 le32_to_cpu(sb->time_precision), NSEC_PER_SEC); 422 return -BCH_ERR_invalid_sb_time_precision; 423 } 424 425 if (rw == READ) { 426 /* 427 * Been seeing a bug where these are getting inexplicably 428 * zeroed, so we're now validating them, but we have to be 429 * careful not to preven people's filesystems from mounting: 430 */ 431 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 432 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 433 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 434 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000); 435 436 if (!BCH_SB_VERSION_UPGRADE_COMPLETE(sb)) 437 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version)); 438 } 439 440 for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) { 441 const struct bch_option *opt = bch2_opt_table + opt_id; 442 443 if (opt->get_sb != BCH2_NO_SB_OPT) { 444 u64 v = bch2_opt_from_sb(sb, opt_id); 445 446 prt_printf(out, "Invalid option "); 447 ret = bch2_opt_validate(opt, v, out); 448 if (ret) 449 return ret; 450 451 printbuf_reset(out); 452 } 453 } 454 455 /* validate layout */ 456 ret = validate_sb_layout(&sb->layout, out); 457 if (ret) 458 return ret; 459 460 vstruct_for_each(sb, f) { 461 if (!f->u64s) { 462 prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)", 463 le32_to_cpu(f->type)); 464 return -BCH_ERR_invalid_sb_field_size; 465 } 466 467 if (vstruct_next(f) > vstruct_last(sb)) { 468 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)", 469 le32_to_cpu(f->type)); 470 return -BCH_ERR_invalid_sb_field_size; 471 } 472 } 473 474 /* members must be validated first: */ 475 mi = bch2_sb_field_get(sb, members_v1); 476 if (!mi) { 477 prt_printf(out, "Invalid superblock: member info area missing"); 478 return -BCH_ERR_invalid_sb_members_missing; 479 } 480 481 ret = bch2_sb_field_validate(sb, &mi->field, out); 482 if (ret) 483 return ret; 484 485 vstruct_for_each(sb, f) { 486 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members_v1) 487 continue; 488 489 ret = bch2_sb_field_validate(sb, f, out); 490 if (ret) 491 return ret; 492 } 493 494 return 0; 495 } 496 497 /* device open: */ 498 499 static unsigned long le_ulong_to_cpu(unsigned long v) 500 { 501 return sizeof(unsigned long) == 8 502 ? le64_to_cpu(v) 503 : le32_to_cpu(v); 504 } 505 506 static void le_bitvector_to_cpu(unsigned long *dst, unsigned long *src, unsigned nr) 507 { 508 BUG_ON(nr & (BITS_PER_TYPE(long) - 1)); 509 510 for (unsigned i = 0; i < BITS_TO_LONGS(nr); i++) 511 dst[i] = le_ulong_to_cpu(src[i]); 512 } 513 514 static void bch2_sb_update(struct bch_fs *c) 515 { 516 struct bch_sb *src = c->disk_sb.sb; 517 struct bch_dev *ca; 518 unsigned i; 519 520 lockdep_assert_held(&c->sb_lock); 521 522 c->sb.uuid = src->uuid; 523 c->sb.user_uuid = src->user_uuid; 524 c->sb.version = le16_to_cpu(src->version); 525 c->sb.version_min = le16_to_cpu(src->version_min); 526 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src); 527 c->sb.nr_devices = src->nr_devices; 528 c->sb.clean = BCH_SB_CLEAN(src); 529 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src); 530 531 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision); 532 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit; 533 534 /* XXX this is wrong, we need a 96 or 128 bit integer type */ 535 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo), 536 c->sb.nsec_per_time_unit); 537 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi); 538 539 c->sb.features = le64_to_cpu(src->features[0]); 540 c->sb.compat = le64_to_cpu(src->compat[0]); 541 542 memset(c->sb.errors_silent, 0, sizeof(c->sb.errors_silent)); 543 544 struct bch_sb_field_ext *ext = bch2_sb_field_get(src, ext); 545 if (ext) 546 le_bitvector_to_cpu(c->sb.errors_silent, (void *) ext->errors_silent, 547 sizeof(c->sb.errors_silent) * 8); 548 549 for_each_member_device(ca, c, i) { 550 struct bch_member m = bch2_sb_member_get(src, ca->dev_idx); 551 ca->mi = bch2_mi_to_cpu(&m); 552 } 553 } 554 555 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src) 556 { 557 struct bch_sb_field *src_f, *dst_f; 558 struct bch_sb *dst = dst_handle->sb; 559 unsigned i; 560 561 dst->version = src->version; 562 dst->version_min = src->version_min; 563 dst->seq = src->seq; 564 dst->uuid = src->uuid; 565 dst->user_uuid = src->user_uuid; 566 memcpy(dst->label, src->label, sizeof(dst->label)); 567 568 dst->block_size = src->block_size; 569 dst->nr_devices = src->nr_devices; 570 571 dst->time_base_lo = src->time_base_lo; 572 dst->time_base_hi = src->time_base_hi; 573 dst->time_precision = src->time_precision; 574 575 memcpy(dst->flags, src->flags, sizeof(dst->flags)); 576 memcpy(dst->features, src->features, sizeof(dst->features)); 577 memcpy(dst->compat, src->compat, sizeof(dst->compat)); 578 579 for (i = 0; i < BCH_SB_FIELD_NR; i++) { 580 int d; 581 582 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS) 583 continue; 584 585 src_f = bch2_sb_field_get_id(src, i); 586 dst_f = bch2_sb_field_get_id(dst, i); 587 588 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) - 589 (dst_f ? le32_to_cpu(dst_f->u64s) : 0); 590 if (d > 0) { 591 int ret = bch2_sb_realloc(dst_handle, 592 le32_to_cpu(dst_handle->sb->u64s) + d); 593 594 if (ret) 595 return ret; 596 597 dst = dst_handle->sb; 598 dst_f = bch2_sb_field_get_id(dst, i); 599 } 600 601 dst_f = __bch2_sb_field_resize(dst_handle, dst_f, 602 src_f ? le32_to_cpu(src_f->u64s) : 0); 603 604 if (src_f) 605 memcpy(dst_f, src_f, vstruct_bytes(src_f)); 606 } 607 608 return 0; 609 } 610 611 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src) 612 { 613 int ret; 614 615 lockdep_assert_held(&c->sb_lock); 616 617 ret = bch2_sb_realloc(&c->disk_sb, 0) ?: 618 __copy_super(&c->disk_sb, src) ?: 619 bch2_sb_replicas_to_cpu_replicas(c) ?: 620 bch2_sb_disk_groups_to_cpu(c); 621 if (ret) 622 return ret; 623 624 bch2_sb_update(c); 625 return 0; 626 } 627 628 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca) 629 { 630 return __copy_super(&ca->disk_sb, c->disk_sb.sb); 631 } 632 633 /* read superblock: */ 634 635 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err) 636 { 637 struct bch_csum csum; 638 size_t bytes; 639 int ret; 640 reread: 641 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META); 642 sb->bio->bi_iter.bi_sector = offset; 643 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size); 644 645 ret = submit_bio_wait(sb->bio); 646 if (ret) { 647 prt_printf(err, "IO error: %i", ret); 648 return ret; 649 } 650 651 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) && 652 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) { 653 prt_printf(err, "Not a bcachefs superblock"); 654 return -BCH_ERR_invalid_sb_magic; 655 } 656 657 ret = bch2_sb_compatible(sb->sb, err); 658 if (ret) 659 return ret; 660 661 bytes = vstruct_bytes(sb->sb); 662 663 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) { 664 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)", 665 bytes, 512UL << sb->sb->layout.sb_max_size_bits); 666 return -BCH_ERR_invalid_sb_too_big; 667 } 668 669 if (bytes > sb->buffer_size) { 670 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s)); 671 if (ret) 672 return ret; 673 goto reread; 674 } 675 676 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) { 677 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb)); 678 return -BCH_ERR_invalid_sb_csum_type; 679 } 680 681 /* XXX: verify MACs */ 682 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb), 683 null_nonce(), sb->sb); 684 685 if (bch2_crc_cmp(csum, sb->sb->csum)) { 686 prt_printf(err, "bad checksum"); 687 return -BCH_ERR_invalid_sb_csum; 688 } 689 690 sb->seq = le64_to_cpu(sb->sb->seq); 691 692 return 0; 693 } 694 695 static int __bch2_read_super(const char *path, struct bch_opts *opts, 696 struct bch_sb_handle *sb, bool ignore_notbchfs_msg) 697 { 698 u64 offset = opt_get(*opts, sb); 699 struct bch_sb_layout layout; 700 struct printbuf err = PRINTBUF; 701 struct printbuf err2 = PRINTBUF; 702 __le64 *i; 703 int ret; 704 #ifndef __KERNEL__ 705 retry: 706 #endif 707 memset(sb, 0, sizeof(*sb)); 708 sb->mode = BLK_OPEN_READ; 709 sb->have_bio = true; 710 sb->holder = kmalloc(1, GFP_KERNEL); 711 if (!sb->holder) 712 return -ENOMEM; 713 714 sb->sb_name = kstrdup(path, GFP_KERNEL); 715 if (!sb->sb_name) 716 return -ENOMEM; 717 718 #ifndef __KERNEL__ 719 if (opt_get(*opts, direct_io) == false) 720 sb->mode |= BLK_OPEN_BUFFERED; 721 #endif 722 723 if (!opt_get(*opts, noexcl)) 724 sb->mode |= BLK_OPEN_EXCL; 725 726 if (!opt_get(*opts, nochanges)) 727 sb->mode |= BLK_OPEN_WRITE; 728 729 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); 730 if (IS_ERR(sb->bdev) && 731 PTR_ERR(sb->bdev) == -EACCES && 732 opt_get(*opts, read_only)) { 733 sb->mode &= ~BLK_OPEN_WRITE; 734 735 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); 736 if (!IS_ERR(sb->bdev)) 737 opt_set(*opts, nochanges, true); 738 } 739 740 if (IS_ERR(sb->bdev)) { 741 ret = PTR_ERR(sb->bdev); 742 goto out; 743 } 744 745 ret = bch2_sb_realloc(sb, 0); 746 if (ret) { 747 prt_printf(&err, "error allocating memory for superblock"); 748 goto err; 749 } 750 751 if (bch2_fs_init_fault("read_super")) { 752 prt_printf(&err, "dynamic fault"); 753 ret = -EFAULT; 754 goto err; 755 } 756 757 ret = read_one_super(sb, offset, &err); 758 if (!ret) 759 goto got_super; 760 761 if (opt_defined(*opts, sb)) 762 goto err; 763 764 prt_printf(&err2, "bcachefs (%s): error reading default superblock: %s\n", 765 path, err.buf); 766 if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg) 767 printk(KERN_INFO "%s", err2.buf); 768 else 769 printk(KERN_ERR "%s", err2.buf); 770 771 printbuf_exit(&err2); 772 printbuf_reset(&err); 773 774 /* 775 * Error reading primary superblock - read location of backup 776 * superblocks: 777 */ 778 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META); 779 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR; 780 /* 781 * use sb buffer to read layout, since sb buffer is page aligned but 782 * layout won't be: 783 */ 784 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout)); 785 786 ret = submit_bio_wait(sb->bio); 787 if (ret) { 788 prt_printf(&err, "IO error: %i", ret); 789 goto err; 790 } 791 792 memcpy(&layout, sb->sb, sizeof(layout)); 793 ret = validate_sb_layout(&layout, &err); 794 if (ret) 795 goto err; 796 797 for (i = layout.sb_offset; 798 i < layout.sb_offset + layout.nr_superblocks; i++) { 799 offset = le64_to_cpu(*i); 800 801 if (offset == opt_get(*opts, sb)) 802 continue; 803 804 ret = read_one_super(sb, offset, &err); 805 if (!ret) 806 goto got_super; 807 } 808 809 goto err; 810 811 got_super: 812 if (le16_to_cpu(sb->sb->block_size) << 9 < 813 bdev_logical_block_size(sb->bdev) && 814 opt_get(*opts, direct_io)) { 815 #ifndef __KERNEL__ 816 opt_set(*opts, direct_io, false); 817 bch2_free_super(sb); 818 goto retry; 819 #endif 820 prt_printf(&err, "block size (%u) smaller than device block size (%u)", 821 le16_to_cpu(sb->sb->block_size) << 9, 822 bdev_logical_block_size(sb->bdev)); 823 ret = -BCH_ERR_block_size_too_small; 824 goto err; 825 } 826 827 ret = 0; 828 sb->have_layout = true; 829 830 ret = bch2_sb_validate(sb, &err, READ); 831 if (ret) { 832 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s\n", 833 path, err.buf); 834 goto err_no_print; 835 } 836 out: 837 printbuf_exit(&err); 838 return ret; 839 err: 840 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s\n", 841 path, err.buf); 842 err_no_print: 843 bch2_free_super(sb); 844 goto out; 845 } 846 847 int bch2_read_super(const char *path, struct bch_opts *opts, 848 struct bch_sb_handle *sb) 849 { 850 return __bch2_read_super(path, opts, sb, false); 851 } 852 853 /* provide a silenced version for mount.bcachefs */ 854 855 int bch2_read_super_silent(const char *path, struct bch_opts *opts, 856 struct bch_sb_handle *sb) 857 { 858 return __bch2_read_super(path, opts, sb, true); 859 } 860 861 /* write superblock: */ 862 863 static void write_super_endio(struct bio *bio) 864 { 865 struct bch_dev *ca = bio->bi_private; 866 867 /* XXX: return errors directly */ 868 869 if (bch2_dev_io_err_on(bio->bi_status, ca, 870 bio_data_dir(bio) 871 ? BCH_MEMBER_ERROR_write 872 : BCH_MEMBER_ERROR_read, 873 "superblock %s error: %s", 874 bio_data_dir(bio) ? "write" : "read", 875 bch2_blk_status_to_str(bio->bi_status))) 876 ca->sb_write_error = 1; 877 878 closure_put(&ca->fs->sb_write); 879 percpu_ref_put(&ca->io_ref); 880 } 881 882 static void read_back_super(struct bch_fs *c, struct bch_dev *ca) 883 { 884 struct bch_sb *sb = ca->disk_sb.sb; 885 struct bio *bio = ca->disk_sb.bio; 886 887 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META); 888 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]); 889 bio->bi_end_io = write_super_endio; 890 bio->bi_private = ca; 891 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE); 892 893 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb], 894 bio_sectors(bio)); 895 896 percpu_ref_get(&ca->io_ref); 897 closure_bio_submit(bio, &c->sb_write); 898 } 899 900 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx) 901 { 902 struct bch_sb *sb = ca->disk_sb.sb; 903 struct bio *bio = ca->disk_sb.bio; 904 905 sb->offset = sb->layout.sb_offset[idx]; 906 907 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false)); 908 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb), 909 null_nonce(), sb); 910 911 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META); 912 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset); 913 bio->bi_end_io = write_super_endio; 914 bio->bi_private = ca; 915 bch2_bio_map(bio, sb, 916 roundup((size_t) vstruct_bytes(sb), 917 bdev_logical_block_size(ca->disk_sb.bdev))); 918 919 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb], 920 bio_sectors(bio)); 921 922 percpu_ref_get(&ca->io_ref); 923 closure_bio_submit(bio, &c->sb_write); 924 } 925 926 int bch2_write_super(struct bch_fs *c) 927 { 928 struct closure *cl = &c->sb_write; 929 struct bch_dev *ca; 930 struct printbuf err = PRINTBUF; 931 unsigned i, sb = 0, nr_wrote; 932 struct bch_devs_mask sb_written; 933 bool wrote, can_mount_without_written, can_mount_with_written; 934 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED; 935 int ret = 0; 936 937 trace_and_count(c, write_super, c, _RET_IP_); 938 939 if (c->opts.very_degraded) 940 degraded_flags |= BCH_FORCE_IF_LOST; 941 942 lockdep_assert_held(&c->sb_lock); 943 944 closure_init_stack(cl); 945 memset(&sb_written, 0, sizeof(sb_written)); 946 947 /* Make sure we're using the new magic numbers: */ 948 c->disk_sb.sb->magic = BCHFS_MAGIC; 949 c->disk_sb.sb->layout.magic = BCHFS_MAGIC; 950 951 le64_add_cpu(&c->disk_sb.sb->seq, 1); 952 953 if (test_bit(BCH_FS_error, &c->flags)) 954 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1); 955 if (test_bit(BCH_FS_topology_error, &c->flags)) 956 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1); 957 958 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN); 959 960 bch2_sb_counters_from_cpu(c); 961 bch2_sb_members_from_cpu(c); 962 bch2_sb_members_cpy_v2_v1(&c->disk_sb); 963 bch2_sb_errors_from_cpu(c); 964 bch2_sb_downgrade_update(c); 965 966 for_each_online_member(ca, c, i) 967 bch2_sb_from_fs(c, ca); 968 969 for_each_online_member(ca, c, i) { 970 printbuf_reset(&err); 971 972 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE); 973 if (ret) { 974 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf); 975 percpu_ref_put(&ca->io_ref); 976 goto out; 977 } 978 } 979 980 if (c->opts.nochanges) 981 goto out; 982 983 /* 984 * Defer writing the superblock until filesystem initialization is 985 * complete - don't write out a partly initialized superblock: 986 */ 987 if (!BCH_SB_INITIALIZED(c->disk_sb.sb)) 988 goto out; 989 990 for_each_online_member(ca, c, i) { 991 __set_bit(ca->dev_idx, sb_written.d); 992 ca->sb_write_error = 0; 993 } 994 995 for_each_online_member(ca, c, i) 996 read_back_super(c, ca); 997 closure_sync(cl); 998 999 for_each_online_member(ca, c, i) { 1000 if (ca->sb_write_error) 1001 continue; 1002 1003 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) { 1004 bch2_fs_fatal_error(c, 1005 "Superblock write was silently dropped! (seq %llu expected %llu)", 1006 le64_to_cpu(ca->sb_read_scratch->seq), 1007 ca->disk_sb.seq); 1008 percpu_ref_put(&ca->io_ref); 1009 ret = -BCH_ERR_erofs_sb_err; 1010 goto out; 1011 } 1012 1013 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) { 1014 bch2_fs_fatal_error(c, 1015 "Superblock modified by another process (seq %llu expected %llu)", 1016 le64_to_cpu(ca->sb_read_scratch->seq), 1017 ca->disk_sb.seq); 1018 percpu_ref_put(&ca->io_ref); 1019 ret = -BCH_ERR_erofs_sb_err; 1020 goto out; 1021 } 1022 } 1023 1024 do { 1025 wrote = false; 1026 for_each_online_member(ca, c, i) 1027 if (!ca->sb_write_error && 1028 sb < ca->disk_sb.sb->layout.nr_superblocks) { 1029 write_one_super(c, ca, sb); 1030 wrote = true; 1031 } 1032 closure_sync(cl); 1033 sb++; 1034 } while (wrote); 1035 1036 for_each_online_member(ca, c, i) { 1037 if (ca->sb_write_error) 1038 __clear_bit(ca->dev_idx, sb_written.d); 1039 else 1040 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq); 1041 } 1042 1043 nr_wrote = dev_mask_nr(&sb_written); 1044 1045 can_mount_with_written = 1046 bch2_have_enough_devs(c, sb_written, degraded_flags, false); 1047 1048 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++) 1049 sb_written.d[i] = ~sb_written.d[i]; 1050 1051 can_mount_without_written = 1052 bch2_have_enough_devs(c, sb_written, degraded_flags, false); 1053 1054 /* 1055 * If we would be able to mount _without_ the devices we successfully 1056 * wrote superblocks to, we weren't able to write to enough devices: 1057 * 1058 * Exception: if we can mount without the successes because we haven't 1059 * written anything (new filesystem), we continue if we'd be able to 1060 * mount with the devices we did successfully write to: 1061 */ 1062 if (bch2_fs_fatal_err_on(!nr_wrote || 1063 !can_mount_with_written || 1064 (can_mount_without_written && 1065 !can_mount_with_written), c, 1066 "Unable to write superblock to sufficient devices (from %ps)", 1067 (void *) _RET_IP_)) 1068 ret = -1; 1069 out: 1070 /* Make new options visible after they're persistent: */ 1071 bch2_sb_update(c); 1072 printbuf_exit(&err); 1073 return ret; 1074 } 1075 1076 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat) 1077 { 1078 mutex_lock(&c->sb_lock); 1079 if (!(c->sb.features & (1ULL << feat))) { 1080 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat); 1081 1082 bch2_write_super(c); 1083 } 1084 mutex_unlock(&c->sb_lock); 1085 } 1086 1087 /* Downgrade if superblock is at a higher version than currently supported: */ 1088 bool bch2_check_version_downgrade(struct bch_fs *c) 1089 { 1090 bool ret = bcachefs_metadata_version_current < c->sb.version; 1091 1092 lockdep_assert_held(&c->sb_lock); 1093 1094 /* 1095 * Downgrade, if superblock is at a higher version than currently 1096 * supported: 1097 */ 1098 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current) 1099 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current); 1100 if (c->sb.version > bcachefs_metadata_version_current) 1101 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current); 1102 if (c->sb.version_min > bcachefs_metadata_version_current) 1103 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current); 1104 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1); 1105 return ret; 1106 } 1107 1108 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version) 1109 { 1110 lockdep_assert_held(&c->sb_lock); 1111 1112 if (BCH_VERSION_MAJOR(new_version) > 1113 BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version))) 1114 bch2_sb_field_resize(&c->disk_sb, downgrade, 0); 1115 1116 c->disk_sb.sb->version = cpu_to_le16(new_version); 1117 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL); 1118 } 1119 1120 static int bch2_sb_ext_validate(struct bch_sb *sb, struct bch_sb_field *f, 1121 struct printbuf *err) 1122 { 1123 if (vstruct_bytes(f) < 88) { 1124 prt_printf(err, "field too small (%zu < %u)", vstruct_bytes(f), 88); 1125 return -BCH_ERR_invalid_sb_ext; 1126 } 1127 1128 return 0; 1129 } 1130 1131 static void bch2_sb_ext_to_text(struct printbuf *out, struct bch_sb *sb, 1132 struct bch_sb_field *f) 1133 { 1134 struct bch_sb_field_ext *e = field_to_type(f, ext); 1135 1136 prt_printf(out, "Recovery passes required:"); 1137 prt_tab(out); 1138 prt_bitflags(out, bch2_recovery_passes, 1139 bch2_recovery_passes_from_stable(le64_to_cpu(e->recovery_passes_required[0]))); 1140 prt_newline(out); 1141 1142 unsigned long *errors_silent = kmalloc(sizeof(e->errors_silent), GFP_KERNEL); 1143 if (errors_silent) { 1144 le_bitvector_to_cpu(errors_silent, (void *) e->errors_silent, sizeof(e->errors_silent) * 8); 1145 1146 prt_printf(out, "Errors to silently fix:"); 1147 prt_tab(out); 1148 prt_bitflags_vector(out, bch2_sb_error_strs, errors_silent, sizeof(e->errors_silent) * 8); 1149 prt_newline(out); 1150 1151 kfree(errors_silent); 1152 } 1153 } 1154 1155 static const struct bch_sb_field_ops bch_sb_field_ops_ext = { 1156 .validate = bch2_sb_ext_validate, 1157 .to_text = bch2_sb_ext_to_text, 1158 }; 1159 1160 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = { 1161 #define x(f, nr) \ 1162 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f, 1163 BCH_SB_FIELDS() 1164 #undef x 1165 }; 1166 1167 static const struct bch_sb_field_ops bch2_sb_field_null_ops; 1168 1169 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type) 1170 { 1171 return likely(type < ARRAY_SIZE(bch2_sb_field_ops)) 1172 ? bch2_sb_field_ops[type] 1173 : &bch2_sb_field_null_ops; 1174 } 1175 1176 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f, 1177 struct printbuf *err) 1178 { 1179 unsigned type = le32_to_cpu(f->type); 1180 struct printbuf field_err = PRINTBUF; 1181 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type); 1182 int ret; 1183 1184 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0; 1185 if (ret) { 1186 prt_printf(err, "Invalid superblock section %s: %s", 1187 bch2_sb_fields[type], field_err.buf); 1188 prt_newline(err); 1189 bch2_sb_field_to_text(err, sb, f); 1190 } 1191 1192 printbuf_exit(&field_err); 1193 return ret; 1194 } 1195 1196 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb, 1197 struct bch_sb_field *f) 1198 { 1199 unsigned type = le32_to_cpu(f->type); 1200 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type); 1201 1202 if (!out->nr_tabstops) 1203 printbuf_tabstop_push(out, 32); 1204 1205 if (type < BCH_SB_FIELD_NR) 1206 prt_printf(out, "%s", bch2_sb_fields[type]); 1207 else 1208 prt_printf(out, "(unknown field %u)", type); 1209 1210 prt_printf(out, " (size %zu):", vstruct_bytes(f)); 1211 prt_newline(out); 1212 1213 if (ops->to_text) { 1214 printbuf_indent_add(out, 2); 1215 ops->to_text(out, sb, f); 1216 printbuf_indent_sub(out, 2); 1217 } 1218 } 1219 1220 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l) 1221 { 1222 unsigned i; 1223 1224 prt_printf(out, "Type: %u", l->layout_type); 1225 prt_newline(out); 1226 1227 prt_str(out, "Superblock max size: "); 1228 prt_units_u64(out, 512 << l->sb_max_size_bits); 1229 prt_newline(out); 1230 1231 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks); 1232 prt_newline(out); 1233 1234 prt_str(out, "Offsets: "); 1235 for (i = 0; i < l->nr_superblocks; i++) { 1236 if (i) 1237 prt_str(out, ", "); 1238 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i])); 1239 } 1240 prt_newline(out); 1241 } 1242 1243 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb, 1244 bool print_layout, unsigned fields) 1245 { 1246 struct bch_sb_field *f; 1247 u64 fields_have = 0; 1248 unsigned nr_devices = 0; 1249 1250 if (!out->nr_tabstops) 1251 printbuf_tabstop_push(out, 44); 1252 1253 for (int i = 0; i < sb->nr_devices; i++) 1254 nr_devices += bch2_dev_exists(sb, i); 1255 1256 prt_printf(out, "External UUID:"); 1257 prt_tab(out); 1258 pr_uuid(out, sb->user_uuid.b); 1259 prt_newline(out); 1260 1261 prt_printf(out, "Internal UUID:"); 1262 prt_tab(out); 1263 pr_uuid(out, sb->uuid.b); 1264 prt_newline(out); 1265 1266 prt_str(out, "Device index:"); 1267 prt_tab(out); 1268 prt_printf(out, "%u", sb->dev_idx); 1269 prt_newline(out); 1270 1271 prt_str(out, "Label:"); 1272 prt_tab(out); 1273 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label); 1274 prt_newline(out); 1275 1276 prt_str(out, "Version:"); 1277 prt_tab(out); 1278 bch2_version_to_text(out, le16_to_cpu(sb->version)); 1279 prt_newline(out); 1280 1281 prt_str(out, "Version upgrade complete:"); 1282 prt_tab(out); 1283 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb)); 1284 prt_newline(out); 1285 1286 prt_printf(out, "Oldest version on disk:"); 1287 prt_tab(out); 1288 bch2_version_to_text(out, le16_to_cpu(sb->version_min)); 1289 prt_newline(out); 1290 1291 prt_printf(out, "Created:"); 1292 prt_tab(out); 1293 if (sb->time_base_lo) 1294 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC)); 1295 else 1296 prt_printf(out, "(not set)"); 1297 prt_newline(out); 1298 1299 prt_printf(out, "Sequence number:"); 1300 prt_tab(out); 1301 prt_printf(out, "%llu", le64_to_cpu(sb->seq)); 1302 prt_newline(out); 1303 1304 prt_printf(out, "Superblock size:"); 1305 prt_tab(out); 1306 prt_printf(out, "%zu", vstruct_bytes(sb)); 1307 prt_newline(out); 1308 1309 prt_printf(out, "Clean:"); 1310 prt_tab(out); 1311 prt_printf(out, "%llu", BCH_SB_CLEAN(sb)); 1312 prt_newline(out); 1313 1314 prt_printf(out, "Devices:"); 1315 prt_tab(out); 1316 prt_printf(out, "%u", nr_devices); 1317 prt_newline(out); 1318 1319 prt_printf(out, "Sections:"); 1320 vstruct_for_each(sb, f) 1321 fields_have |= 1 << le32_to_cpu(f->type); 1322 prt_tab(out); 1323 prt_bitflags(out, bch2_sb_fields, fields_have); 1324 prt_newline(out); 1325 1326 prt_printf(out, "Features:"); 1327 prt_tab(out); 1328 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0])); 1329 prt_newline(out); 1330 1331 prt_printf(out, "Compat features:"); 1332 prt_tab(out); 1333 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0])); 1334 prt_newline(out); 1335 1336 prt_newline(out); 1337 prt_printf(out, "Options:"); 1338 prt_newline(out); 1339 printbuf_indent_add(out, 2); 1340 { 1341 enum bch_opt_id id; 1342 1343 for (id = 0; id < bch2_opts_nr; id++) { 1344 const struct bch_option *opt = bch2_opt_table + id; 1345 1346 if (opt->get_sb != BCH2_NO_SB_OPT) { 1347 u64 v = bch2_opt_from_sb(sb, id); 1348 1349 prt_printf(out, "%s:", opt->attr.name); 1350 prt_tab(out); 1351 bch2_opt_to_text(out, NULL, sb, opt, v, 1352 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST); 1353 prt_newline(out); 1354 } 1355 } 1356 } 1357 1358 printbuf_indent_sub(out, 2); 1359 1360 if (print_layout) { 1361 prt_newline(out); 1362 prt_printf(out, "layout:"); 1363 prt_newline(out); 1364 printbuf_indent_add(out, 2); 1365 bch2_sb_layout_to_text(out, &sb->layout); 1366 printbuf_indent_sub(out, 2); 1367 } 1368 1369 vstruct_for_each(sb, f) 1370 if (fields & (1 << le32_to_cpu(f->type))) { 1371 prt_newline(out); 1372 bch2_sb_field_to_text(out, sb, f); 1373 } 1374 } 1375