1 // SPDX-License-Identifier: GPL-2.0
6 #include "disk_groups.h"
10 #include "journal_sb.h"
11 #include "journal_seq_blacklist.h"
16 #include "sb-downgrade.h"
17 #include "sb-errors.h"
18 #include "sb-members.h"
24 #include <linux/backing-dev.h>
25 #include <linux/sort.h>
27 static const struct blk_holder_ops bch2_sb_handle_bdev_ops = {
30 struct bch2_metadata_version {
36 static const struct bch2_metadata_version bch2_metadata_versions[] = {
37 #define x(n, v, _recovery_passes) { \
40 .recovery_passes = _recovery_passes, \
42 BCH_METADATA_VERSIONS()
46 void bch2_version_to_text(struct printbuf *out, unsigned v)
48 const char *str = "(unknown version)";
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;
56 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
59 unsigned bch2_latest_compatible_version(unsigned v)
61 if (!BCH_VERSION_MAJOR(v))
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) ==
68 v = bch2_metadata_versions[i].version;
73 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
79 for (const struct bch2_metadata_version *i = bch2_metadata_versions;
80 i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions);
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;
88 return ret &= ~RECOVERY_PASS_ALL_FSCK;
91 const char * const bch2_sb_fields[] = {
92 #define x(name, nr) #name,
98 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
101 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *sb,
102 enum bch_sb_field_type type)
104 /* XXX: need locking around superblock to access optional fields */
106 vstruct_for_each(sb, f)
107 if (le32_to_cpu(f->type) == type)
112 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
113 struct bch_sb_field *f,
116 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
117 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
119 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
124 f = vstruct_last(sb->sb);
125 memset(f, 0, sizeof(u64) * u64s);
126 f->u64s = cpu_to_le32(u64s);
131 src = vstruct_end(f);
134 f->u64s = cpu_to_le32(u64s);
135 dst = vstruct_end(f);
140 memmove(dst, src, vstruct_end(sb->sb) - src);
143 memset(src, 0, dst - src);
146 sb->sb->u64s = cpu_to_le32(sb_u64s);
148 return u64s ? f : NULL;
151 void bch2_sb_field_delete(struct bch_sb_handle *sb,
152 enum bch_sb_field_type type)
154 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
157 __bch2_sb_field_resize(sb, f, 0);
160 /* Superblock realloc/free: */
162 void bch2_free_super(struct bch_sb_handle *sb)
165 if (!IS_ERR_OR_NULL(sb->bdev))
166 blkdev_put(sb->bdev, sb->holder);
171 memset(sb, 0, sizeof(*sb));
174 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
176 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
177 size_t new_buffer_size;
178 struct bch_sb *new_sb;
182 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
184 new_buffer_size = roundup_pow_of_two(new_bytes);
186 if (sb->sb && sb->buffer_size >= new_buffer_size)
189 if (sb->sb && sb->have_layout) {
190 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
192 if (new_bytes > max_bytes) {
193 pr_err("%pg: superblock too big: want %zu but have %llu",
194 sb->bdev, new_bytes, max_bytes);
195 return -BCH_ERR_ENOSPC_sb;
199 if (sb->buffer_size >= new_buffer_size && sb->sb)
202 if (dynamic_fault("bcachefs:add:super_realloc"))
203 return -BCH_ERR_ENOMEM_sb_realloc_injected;
205 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
207 return -BCH_ERR_ENOMEM_sb_buf_realloc;
212 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size);
214 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
216 return -BCH_ERR_ENOMEM_sb_bio_realloc;
218 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
224 sb->buffer_size = new_buffer_size;
229 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *sb,
230 enum bch_sb_field_type type,
233 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
234 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
235 ssize_t d = -old_u64s + u64s;
237 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
241 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
243 lockdep_assert_held(&c->sb_lock);
245 /* XXX: we're not checking that offline device have enough space */
247 for_each_online_member(c, ca) {
248 struct bch_sb_handle *dev_sb = &ca->disk_sb;
250 if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) {
251 percpu_ref_put(&ca->ref);
257 f = bch2_sb_field_get_id(sb->sb, type);
258 f = __bch2_sb_field_resize(sb, f, u64s);
260 f->type = cpu_to_le32(type);
264 struct bch_sb_field *bch2_sb_field_get_minsize_id(struct bch_sb_handle *sb,
265 enum bch_sb_field_type type,
268 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
270 if (!f || le32_to_cpu(f->u64s) < u64s)
271 f = bch2_sb_field_resize_id(sb, type, u64s);
275 /* Superblock validate: */
277 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
279 u64 offset, prev_offset, max_sectors;
282 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
284 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
285 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
286 prt_printf(out, "Not a bcachefs superblock layout");
287 return -BCH_ERR_invalid_sb_layout;
290 if (layout->layout_type != 0) {
291 prt_printf(out, "Invalid superblock layout type %u",
292 layout->layout_type);
293 return -BCH_ERR_invalid_sb_layout_type;
296 if (!layout->nr_superblocks) {
297 prt_printf(out, "Invalid superblock layout: no superblocks");
298 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
301 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
302 prt_printf(out, "Invalid superblock layout: too many superblocks");
303 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
306 max_sectors = 1 << layout->sb_max_size_bits;
308 prev_offset = le64_to_cpu(layout->sb_offset[0]);
310 for (i = 1; i < layout->nr_superblocks; i++) {
311 offset = le64_to_cpu(layout->sb_offset[i]);
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;
319 prev_offset = offset;
325 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
327 u16 version = le16_to_cpu(sb->version);
328 u16 version_min = le16_to_cpu(sb->version_min);
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);
338 return -BCH_ERR_invalid_sb_version;
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);
349 return -BCH_ERR_invalid_sb_version;
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;
363 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
366 struct bch_sb *sb = disk_sb->sb;
367 struct bch_sb_field_members_v1 *mi;
368 enum bch_opt_id opt_id;
372 ret = bch2_sb_compatible(sb, out);
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;
382 block_size = le16_to_cpu(sb->block_size);
384 if (block_size > PAGE_SECTORS) {
385 prt_printf(out, "Block size too big (got %u, max %u)",
386 block_size, PAGE_SECTORS);
387 return -BCH_ERR_invalid_sb_block_size;
390 if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
391 prt_printf(out, "Bad user UUID (got zeroes)");
392 return -BCH_ERR_invalid_sb_uuid;
395 if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
396 prt_printf(out, "Bad internal UUID (got zeroes)");
397 return -BCH_ERR_invalid_sb_uuid;
400 if (!sb->nr_devices ||
401 sb->nr_devices > BCH_SB_MEMBERS_MAX) {
402 prt_printf(out, "Bad number of member devices %u (max %u)",
403 sb->nr_devices, BCH_SB_MEMBERS_MAX);
404 return -BCH_ERR_invalid_sb_too_many_members;
407 if (sb->dev_idx >= sb->nr_devices) {
408 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
409 sb->dev_idx, sb->nr_devices);
410 return -BCH_ERR_invalid_sb_dev_idx;
413 if (!sb->time_precision ||
414 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
415 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
416 le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
417 return -BCH_ERR_invalid_sb_time_precision;
422 * Been seeing a bug where these are getting inexplicably
423 * zeroed, so we're now validating them, but we have to be
424 * careful not to preven people's filesystems from mounting:
426 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
427 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
428 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
429 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
431 if (!BCH_SB_VERSION_UPGRADE_COMPLETE(sb))
432 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version));
435 for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
436 const struct bch_option *opt = bch2_opt_table + opt_id;
438 if (opt->get_sb != BCH2_NO_SB_OPT) {
439 u64 v = bch2_opt_from_sb(sb, opt_id);
441 prt_printf(out, "Invalid option ");
442 ret = bch2_opt_validate(opt, v, out);
450 /* validate layout */
451 ret = validate_sb_layout(&sb->layout, out);
455 vstruct_for_each(sb, f) {
457 prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
458 le32_to_cpu(f->type));
459 return -BCH_ERR_invalid_sb_field_size;
462 if (vstruct_next(f) > vstruct_last(sb)) {
463 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
464 le32_to_cpu(f->type));
465 return -BCH_ERR_invalid_sb_field_size;
469 /* members must be validated first: */
470 mi = bch2_sb_field_get(sb, members_v1);
472 prt_printf(out, "Invalid superblock: member info area missing");
473 return -BCH_ERR_invalid_sb_members_missing;
476 ret = bch2_sb_field_validate(sb, &mi->field, out);
480 vstruct_for_each(sb, f) {
481 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members_v1)
484 ret = bch2_sb_field_validate(sb, f, out);
494 static unsigned long le_ulong_to_cpu(unsigned long v)
496 return sizeof(unsigned long) == 8
501 static void le_bitvector_to_cpu(unsigned long *dst, unsigned long *src, unsigned nr)
503 BUG_ON(nr & (BITS_PER_TYPE(long) - 1));
505 for (unsigned i = 0; i < BITS_TO_LONGS(nr); i++)
506 dst[i] = le_ulong_to_cpu(src[i]);
509 static void bch2_sb_update(struct bch_fs *c)
511 struct bch_sb *src = c->disk_sb.sb;
513 lockdep_assert_held(&c->sb_lock);
515 c->sb.uuid = src->uuid;
516 c->sb.user_uuid = src->user_uuid;
517 c->sb.version = le16_to_cpu(src->version);
518 c->sb.version_min = le16_to_cpu(src->version_min);
519 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src);
520 c->sb.nr_devices = src->nr_devices;
521 c->sb.clean = BCH_SB_CLEAN(src);
522 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
524 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
525 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
527 /* XXX this is wrong, we need a 96 or 128 bit integer type */
528 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
529 c->sb.nsec_per_time_unit);
530 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
532 c->sb.features = le64_to_cpu(src->features[0]);
533 c->sb.compat = le64_to_cpu(src->compat[0]);
535 memset(c->sb.errors_silent, 0, sizeof(c->sb.errors_silent));
537 struct bch_sb_field_ext *ext = bch2_sb_field_get(src, ext);
539 le_bitvector_to_cpu(c->sb.errors_silent, (void *) ext->errors_silent,
540 sizeof(c->sb.errors_silent) * 8);
542 for_each_member_device(c, ca) {
543 struct bch_member m = bch2_sb_member_get(src, ca->dev_idx);
544 ca->mi = bch2_mi_to_cpu(&m);
548 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
550 struct bch_sb_field *src_f, *dst_f;
551 struct bch_sb *dst = dst_handle->sb;
554 dst->version = src->version;
555 dst->version_min = src->version_min;
557 dst->uuid = src->uuid;
558 dst->user_uuid = src->user_uuid;
559 memcpy(dst->label, src->label, sizeof(dst->label));
561 dst->block_size = src->block_size;
562 dst->nr_devices = src->nr_devices;
564 dst->time_base_lo = src->time_base_lo;
565 dst->time_base_hi = src->time_base_hi;
566 dst->time_precision = src->time_precision;
567 dst->write_time = src->write_time;
569 memcpy(dst->flags, src->flags, sizeof(dst->flags));
570 memcpy(dst->features, src->features, sizeof(dst->features));
571 memcpy(dst->compat, src->compat, sizeof(dst->compat));
573 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
576 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
579 src_f = bch2_sb_field_get_id(src, i);
580 dst_f = bch2_sb_field_get_id(dst, i);
582 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
583 (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
585 int ret = bch2_sb_realloc(dst_handle,
586 le32_to_cpu(dst_handle->sb->u64s) + d);
591 dst = dst_handle->sb;
592 dst_f = bch2_sb_field_get_id(dst, i);
595 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
596 src_f ? le32_to_cpu(src_f->u64s) : 0);
599 memcpy(dst_f, src_f, vstruct_bytes(src_f));
605 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
609 lockdep_assert_held(&c->sb_lock);
611 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
612 __copy_super(&c->disk_sb, src) ?:
613 bch2_sb_replicas_to_cpu_replicas(c) ?:
614 bch2_sb_disk_groups_to_cpu(c);
622 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
624 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
627 /* read superblock: */
629 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
631 struct bch_csum csum;
635 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
636 sb->bio->bi_iter.bi_sector = offset;
637 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
639 ret = submit_bio_wait(sb->bio);
641 prt_printf(err, "IO error: %i", ret);
645 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
646 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
647 prt_printf(err, "Not a bcachefs superblock");
648 return -BCH_ERR_invalid_sb_magic;
651 ret = bch2_sb_compatible(sb->sb, err);
655 bytes = vstruct_bytes(sb->sb);
657 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
658 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
659 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
660 return -BCH_ERR_invalid_sb_too_big;
663 if (bytes > sb->buffer_size) {
664 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
670 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
671 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
672 return -BCH_ERR_invalid_sb_csum_type;
675 /* XXX: verify MACs */
676 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
677 null_nonce(), sb->sb);
679 if (bch2_crc_cmp(csum, sb->sb->csum)) {
680 prt_printf(err, "bad checksum");
681 return -BCH_ERR_invalid_sb_csum;
684 sb->seq = le64_to_cpu(sb->sb->seq);
689 static int __bch2_read_super(const char *path, struct bch_opts *opts,
690 struct bch_sb_handle *sb, bool ignore_notbchfs_msg)
692 u64 offset = opt_get(*opts, sb);
693 struct bch_sb_layout layout;
694 struct printbuf err = PRINTBUF;
695 struct printbuf err2 = PRINTBUF;
701 memset(sb, 0, sizeof(*sb));
702 sb->mode = BLK_OPEN_READ;
704 sb->holder = kmalloc(1, GFP_KERNEL);
708 sb->sb_name = kstrdup(path, GFP_KERNEL);
713 if (opt_get(*opts, direct_io) == false)
714 sb->mode |= BLK_OPEN_BUFFERED;
717 if (!opt_get(*opts, noexcl))
718 sb->mode |= BLK_OPEN_EXCL;
720 if (!opt_get(*opts, nochanges))
721 sb->mode |= BLK_OPEN_WRITE;
723 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
724 if (IS_ERR(sb->bdev) &&
725 PTR_ERR(sb->bdev) == -EACCES &&
726 opt_get(*opts, read_only)) {
727 sb->mode &= ~BLK_OPEN_WRITE;
729 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
730 if (!IS_ERR(sb->bdev))
731 opt_set(*opts, nochanges, true);
734 if (IS_ERR(sb->bdev)) {
735 ret = PTR_ERR(sb->bdev);
739 ret = bch2_sb_realloc(sb, 0);
741 prt_printf(&err, "error allocating memory for superblock");
745 if (bch2_fs_init_fault("read_super")) {
746 prt_printf(&err, "dynamic fault");
751 ret = read_one_super(sb, offset, &err);
755 if (opt_defined(*opts, sb))
758 prt_printf(&err2, "bcachefs (%s): error reading default superblock: %s\n",
760 if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg)
761 printk(KERN_INFO "%s", err2.buf);
763 printk(KERN_ERR "%s", err2.buf);
765 printbuf_exit(&err2);
766 printbuf_reset(&err);
769 * Error reading primary superblock - read location of backup
772 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
773 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
775 * use sb buffer to read layout, since sb buffer is page aligned but
778 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
780 ret = submit_bio_wait(sb->bio);
782 prt_printf(&err, "IO error: %i", ret);
786 memcpy(&layout, sb->sb, sizeof(layout));
787 ret = validate_sb_layout(&layout, &err);
791 for (i = layout.sb_offset;
792 i < layout.sb_offset + layout.nr_superblocks; i++) {
793 offset = le64_to_cpu(*i);
795 if (offset == opt_get(*opts, sb))
798 ret = read_one_super(sb, offset, &err);
806 if (le16_to_cpu(sb->sb->block_size) << 9 <
807 bdev_logical_block_size(sb->bdev) &&
808 opt_get(*opts, direct_io)) {
810 opt_set(*opts, direct_io, false);
814 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
815 le16_to_cpu(sb->sb->block_size) << 9,
816 bdev_logical_block_size(sb->bdev));
817 ret = -BCH_ERR_block_size_too_small;
822 sb->have_layout = true;
824 ret = bch2_sb_validate(sb, &err, READ);
826 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s\n",
834 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s\n",
841 int bch2_read_super(const char *path, struct bch_opts *opts,
842 struct bch_sb_handle *sb)
844 return __bch2_read_super(path, opts, sb, false);
847 /* provide a silenced version for mount.bcachefs */
849 int bch2_read_super_silent(const char *path, struct bch_opts *opts,
850 struct bch_sb_handle *sb)
852 return __bch2_read_super(path, opts, sb, true);
855 /* write superblock: */
857 static void write_super_endio(struct bio *bio)
859 struct bch_dev *ca = bio->bi_private;
861 /* XXX: return errors directly */
863 if (bch2_dev_io_err_on(bio->bi_status, ca,
865 ? BCH_MEMBER_ERROR_write
866 : BCH_MEMBER_ERROR_read,
867 "superblock %s error: %s",
868 bio_data_dir(bio) ? "write" : "read",
869 bch2_blk_status_to_str(bio->bi_status)))
870 ca->sb_write_error = 1;
872 closure_put(&ca->fs->sb_write);
873 percpu_ref_put(&ca->io_ref);
876 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
878 struct bch_sb *sb = ca->disk_sb.sb;
879 struct bio *bio = ca->disk_sb.bio;
881 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
882 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
883 bio->bi_end_io = write_super_endio;
884 bio->bi_private = ca;
885 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
887 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
890 percpu_ref_get(&ca->io_ref);
891 closure_bio_submit(bio, &c->sb_write);
894 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
896 struct bch_sb *sb = ca->disk_sb.sb;
897 struct bio *bio = ca->disk_sb.bio;
899 sb->offset = sb->layout.sb_offset[idx];
901 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
902 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
905 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
906 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
907 bio->bi_end_io = write_super_endio;
908 bio->bi_private = ca;
909 bch2_bio_map(bio, sb,
910 roundup((size_t) vstruct_bytes(sb),
911 bdev_logical_block_size(ca->disk_sb.bdev)));
913 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
916 percpu_ref_get(&ca->io_ref);
917 closure_bio_submit(bio, &c->sb_write);
920 int bch2_write_super(struct bch_fs *c)
922 struct closure *cl = &c->sb_write;
923 struct printbuf err = PRINTBUF;
924 unsigned sb = 0, nr_wrote;
925 struct bch_devs_mask sb_written;
926 bool wrote, can_mount_without_written, can_mount_with_written;
927 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
930 trace_and_count(c, write_super, c, _RET_IP_);
932 if (c->opts.very_degraded)
933 degraded_flags |= BCH_FORCE_IF_LOST;
935 lockdep_assert_held(&c->sb_lock);
937 closure_init_stack(cl);
938 memset(&sb_written, 0, sizeof(sb_written));
940 /* Make sure we're using the new magic numbers: */
941 c->disk_sb.sb->magic = BCHFS_MAGIC;
942 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
944 le64_add_cpu(&c->disk_sb.sb->seq, 1);
946 struct bch_sb_field_members_v2 *mi = bch2_sb_field_get(c->disk_sb.sb, members_v2);
947 for_each_online_member(c, ca)
948 __bch2_members_v2_get_mut(mi, ca->dev_idx)->seq = c->disk_sb.sb->seq;
949 c->disk_sb.sb->write_time = cpu_to_le64(ktime_get_real_seconds());
951 if (test_bit(BCH_FS_error, &c->flags))
952 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
953 if (test_bit(BCH_FS_topology_error, &c->flags))
954 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
956 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
958 bch2_sb_counters_from_cpu(c);
959 bch2_sb_members_from_cpu(c);
960 bch2_sb_members_cpy_v2_v1(&c->disk_sb);
961 bch2_sb_errors_from_cpu(c);
962 bch2_sb_downgrade_update(c);
964 for_each_online_member(c, ca)
965 bch2_sb_from_fs(c, ca);
967 for_each_online_member(c, ca) {
968 printbuf_reset(&err);
970 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
972 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
973 percpu_ref_put(&ca->io_ref);
978 if (c->opts.nochanges)
982 * Defer writing the superblock until filesystem initialization is
983 * complete - don't write out a partly initialized superblock:
985 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
988 if (le16_to_cpu(c->disk_sb.sb->version) > bcachefs_metadata_version_current) {
989 struct printbuf buf = PRINTBUF;
990 prt_printf(&buf, "attempting to write superblock that wasn't version downgraded (");
991 bch2_version_to_text(&buf, le16_to_cpu(c->disk_sb.sb->version));
992 prt_str(&buf, " > ");
993 bch2_version_to_text(&buf, bcachefs_metadata_version_current);
995 bch2_fs_fatal_error(c, "%s", buf.buf);
997 return -BCH_ERR_sb_not_downgraded;
1000 for_each_online_member(c, ca) {
1001 __set_bit(ca->dev_idx, sb_written.d);
1002 ca->sb_write_error = 0;
1005 for_each_online_member(c, ca)
1006 read_back_super(c, ca);
1009 for_each_online_member(c, ca) {
1010 if (ca->sb_write_error)
1013 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
1014 bch2_fs_fatal_error(c,
1015 "Superblock write was silently dropped! (seq %llu expected %llu)",
1016 le64_to_cpu(ca->sb_read_scratch->seq),
1018 percpu_ref_put(&ca->io_ref);
1019 ret = -BCH_ERR_erofs_sb_err;
1023 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
1024 bch2_fs_fatal_error(c,
1025 "Superblock modified by another process (seq %llu expected %llu)",
1026 le64_to_cpu(ca->sb_read_scratch->seq),
1028 percpu_ref_put(&ca->io_ref);
1029 ret = -BCH_ERR_erofs_sb_err;
1036 for_each_online_member(c, ca)
1037 if (!ca->sb_write_error &&
1038 sb < ca->disk_sb.sb->layout.nr_superblocks) {
1039 write_one_super(c, ca, sb);
1046 for_each_online_member(c, ca) {
1047 if (ca->sb_write_error)
1048 __clear_bit(ca->dev_idx, sb_written.d);
1050 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
1053 nr_wrote = dev_mask_nr(&sb_written);
1055 can_mount_with_written =
1056 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1058 for (unsigned i = 0; i < ARRAY_SIZE(sb_written.d); i++)
1059 sb_written.d[i] = ~sb_written.d[i];
1061 can_mount_without_written =
1062 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1065 * If we would be able to mount _without_ the devices we successfully
1066 * wrote superblocks to, we weren't able to write to enough devices:
1068 * Exception: if we can mount without the successes because we haven't
1069 * written anything (new filesystem), we continue if we'd be able to
1070 * mount with the devices we did successfully write to:
1072 if (bch2_fs_fatal_err_on(!nr_wrote ||
1073 !can_mount_with_written ||
1074 (can_mount_without_written &&
1075 !can_mount_with_written), c,
1076 "Unable to write superblock to sufficient devices (from %ps)",
1080 /* Make new options visible after they're persistent: */
1082 printbuf_exit(&err);
1086 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1088 mutex_lock(&c->sb_lock);
1089 if (!(c->sb.features & (1ULL << feat))) {
1090 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1092 bch2_write_super(c);
1094 mutex_unlock(&c->sb_lock);
1097 /* Downgrade if superblock is at a higher version than currently supported: */
1098 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1100 lockdep_assert_held(&c->sb_lock);
1103 * Downgrade, if superblock is at a higher version than currently
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 if (c->sb.version > bcachefs_metadata_version_current)
1109 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1110 if (c->sb.version_min > bcachefs_metadata_version_current)
1111 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1112 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1115 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1117 lockdep_assert_held(&c->sb_lock);
1119 if (BCH_VERSION_MAJOR(new_version) >
1120 BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
1121 bch2_sb_field_resize(&c->disk_sb, downgrade, 0);
1123 c->disk_sb.sb->version = cpu_to_le16(new_version);
1124 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1127 static int bch2_sb_ext_validate(struct bch_sb *sb, struct bch_sb_field *f,
1128 struct printbuf *err)
1130 if (vstruct_bytes(f) < 88) {
1131 prt_printf(err, "field too small (%zu < %u)", vstruct_bytes(f), 88);
1132 return -BCH_ERR_invalid_sb_ext;
1138 static void bch2_sb_ext_to_text(struct printbuf *out, struct bch_sb *sb,
1139 struct bch_sb_field *f)
1141 struct bch_sb_field_ext *e = field_to_type(f, ext);
1143 prt_printf(out, "Recovery passes required:");
1145 prt_bitflags(out, bch2_recovery_passes,
1146 bch2_recovery_passes_from_stable(le64_to_cpu(e->recovery_passes_required[0])));
1149 unsigned long *errors_silent = kmalloc(sizeof(e->errors_silent), GFP_KERNEL);
1150 if (errors_silent) {
1151 le_bitvector_to_cpu(errors_silent, (void *) e->errors_silent, sizeof(e->errors_silent) * 8);
1153 prt_printf(out, "Errors to silently fix:");
1155 prt_bitflags_vector(out, bch2_sb_error_strs, errors_silent, sizeof(e->errors_silent) * 8);
1158 kfree(errors_silent);
1162 static const struct bch_sb_field_ops bch_sb_field_ops_ext = {
1163 .validate = bch2_sb_ext_validate,
1164 .to_text = bch2_sb_ext_to_text,
1167 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1169 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1174 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1176 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1178 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1179 ? bch2_sb_field_ops[type]
1180 : &bch2_sb_field_null_ops;
1183 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1184 struct printbuf *err)
1186 unsigned type = le32_to_cpu(f->type);
1187 struct printbuf field_err = PRINTBUF;
1188 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1191 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1193 prt_printf(err, "Invalid superblock section %s: %s",
1194 bch2_sb_fields[type], field_err.buf);
1196 bch2_sb_field_to_text(err, sb, f);
1199 printbuf_exit(&field_err);
1203 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1204 struct bch_sb_field *f)
1206 unsigned type = le32_to_cpu(f->type);
1207 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1209 if (!out->nr_tabstops)
1210 printbuf_tabstop_push(out, 32);
1212 if (type < BCH_SB_FIELD_NR)
1213 prt_printf(out, "%s", bch2_sb_fields[type]);
1215 prt_printf(out, "(unknown field %u)", type);
1217 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1221 printbuf_indent_add(out, 2);
1222 ops->to_text(out, sb, f);
1223 printbuf_indent_sub(out, 2);
1227 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1231 prt_printf(out, "Type: %u", l->layout_type);
1234 prt_str(out, "Superblock max size: ");
1235 prt_units_u64(out, 512 << l->sb_max_size_bits);
1238 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1241 prt_str(out, "Offsets: ");
1242 for (i = 0; i < l->nr_superblocks; i++) {
1245 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1250 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1251 bool print_layout, unsigned fields)
1253 u64 fields_have = 0;
1254 unsigned nr_devices = 0;
1256 if (!out->nr_tabstops)
1257 printbuf_tabstop_push(out, 44);
1259 for (int i = 0; i < sb->nr_devices; i++)
1260 nr_devices += bch2_dev_exists(sb, i);
1262 prt_printf(out, "External UUID:");
1264 pr_uuid(out, sb->user_uuid.b);
1267 prt_printf(out, "Internal UUID:");
1269 pr_uuid(out, sb->uuid.b);
1272 prt_str(out, "Device index:");
1274 prt_printf(out, "%u", sb->dev_idx);
1277 prt_str(out, "Label:");
1279 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1282 prt_str(out, "Version:");
1284 bch2_version_to_text(out, le16_to_cpu(sb->version));
1287 prt_str(out, "Version upgrade complete:");
1289 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1292 prt_printf(out, "Oldest version on disk:");
1294 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1297 prt_printf(out, "Created:");
1299 if (sb->time_base_lo)
1300 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1302 prt_printf(out, "(not set)");
1305 prt_printf(out, "Sequence number:");
1307 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1310 prt_printf(out, "Time of last write:");
1312 bch2_prt_datetime(out, le64_to_cpu(sb->write_time));
1315 prt_printf(out, "Superblock size:");
1317 prt_printf(out, "%zu", vstruct_bytes(sb));
1320 prt_printf(out, "Clean:");
1322 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1325 prt_printf(out, "Devices:");
1327 prt_printf(out, "%u", nr_devices);
1330 prt_printf(out, "Sections:");
1331 vstruct_for_each(sb, f)
1332 fields_have |= 1 << le32_to_cpu(f->type);
1334 prt_bitflags(out, bch2_sb_fields, fields_have);
1337 prt_printf(out, "Features:");
1339 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1342 prt_printf(out, "Compat features:");
1344 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1348 prt_printf(out, "Options:");
1350 printbuf_indent_add(out, 2);
1354 for (id = 0; id < bch2_opts_nr; id++) {
1355 const struct bch_option *opt = bch2_opt_table + id;
1357 if (opt->get_sb != BCH2_NO_SB_OPT) {
1358 u64 v = bch2_opt_from_sb(sb, id);
1360 prt_printf(out, "%s:", opt->attr.name);
1362 bch2_opt_to_text(out, NULL, sb, opt, v,
1363 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1369 printbuf_indent_sub(out, 2);
1373 prt_printf(out, "layout:");
1375 printbuf_indent_add(out, 2);
1376 bch2_sb_layout_to_text(out, &sb->layout);
1377 printbuf_indent_sub(out, 2);
1380 vstruct_for_each(sb, f)
1381 if (fields & (1 << le32_to_cpu(f->type))) {
1383 bch2_sb_field_to_text(out, sb, f);