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