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