1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_update_interior.h"
8 #include "disk_groups.h"
13 #include "journal_io.h"
14 #include "journal_sb.h"
15 #include "journal_seq_blacklist.h"
24 #include <linux/backing-dev.h>
25 #include <linux/sort.h>
27 struct bch2_metadata_version {
33 static const struct bch2_metadata_version bch2_metadata_versions[] = {
34 #define x(n, v, _recovery_passes) { \
37 .recovery_passes = _recovery_passes, \
39 BCH_METADATA_VERSIONS()
43 void bch2_version_to_text(struct printbuf *out, unsigned v)
45 const char *str = "(unknown version)";
47 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
48 if (bch2_metadata_versions[i].version == v) {
49 str = bch2_metadata_versions[i].name;
53 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
56 unsigned bch2_latest_compatible_version(unsigned v)
58 if (!BCH_VERSION_MAJOR(v))
61 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
62 if (bch2_metadata_versions[i].version > v &&
63 BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
65 v = bch2_metadata_versions[i].version;
70 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
76 for (const struct bch2_metadata_version *i = bch2_metadata_versions;
77 i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions);
79 if (i->version > old_version && i->version <= new_version) {
80 if (i->recovery_passes & RECOVERY_PASS_ALL_FSCK)
81 ret |= bch2_fsck_recovery_passes();
82 ret |= i->recovery_passes;
85 return ret &= ~RECOVERY_PASS_ALL_FSCK;
88 const char * const bch2_sb_fields[] = {
89 #define x(name, nr) #name,
95 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
98 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
99 enum bch_sb_field_type type)
101 struct bch_sb_field *f;
103 /* XXX: need locking around superblock to access optional fields */
105 vstruct_for_each(sb, f)
106 if (le32_to_cpu(f->type) == type)
111 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
112 struct bch_sb_field *f,
115 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
116 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
118 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
123 f = vstruct_last(sb->sb);
124 memset(f, 0, sizeof(u64) * u64s);
125 f->u64s = cpu_to_le32(u64s);
130 src = vstruct_end(f);
133 f->u64s = cpu_to_le32(u64s);
134 dst = vstruct_end(f);
139 memmove(dst, src, vstruct_end(sb->sb) - src);
142 memset(src, 0, dst - src);
145 sb->sb->u64s = cpu_to_le32(sb_u64s);
147 return u64s ? f : NULL;
150 void bch2_sb_field_delete(struct bch_sb_handle *sb,
151 enum bch_sb_field_type type)
153 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
156 __bch2_sb_field_resize(sb, f, 0);
159 /* Superblock realloc/free: */
161 void bch2_free_super(struct bch_sb_handle *sb)
164 if (!IS_ERR_OR_NULL(sb->bdev))
165 blkdev_put(sb->bdev, sb->mode);
168 memset(sb, 0, sizeof(*sb));
171 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
173 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
174 size_t new_buffer_size;
175 struct bch_sb *new_sb;
179 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
181 new_buffer_size = roundup_pow_of_two(new_bytes);
183 if (sb->sb && sb->buffer_size >= new_buffer_size)
186 if (sb->have_layout) {
187 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
189 if (new_bytes > max_bytes) {
190 pr_err("%pg: superblock too big: want %zu but have %llu",
191 sb->bdev, new_bytes, max_bytes);
192 return -BCH_ERR_ENOSPC_sb;
196 if (sb->buffer_size >= new_buffer_size && sb->sb)
199 if (dynamic_fault("bcachefs:add:super_realloc"))
200 return -BCH_ERR_ENOMEM_sb_realloc_injected;
203 unsigned nr_bvecs = DIV_ROUND_UP(new_buffer_size, PAGE_SIZE);
205 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
207 return -BCH_ERR_ENOMEM_sb_bio_realloc;
209 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
215 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
217 return -BCH_ERR_ENOMEM_sb_buf_realloc;
220 sb->buffer_size = new_buffer_size;
225 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
226 enum bch_sb_field_type type,
229 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
230 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
231 ssize_t d = -old_u64s + u64s;
233 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
237 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
241 lockdep_assert_held(&c->sb_lock);
243 /* XXX: we're not checking that offline device have enough space */
245 for_each_online_member(ca, c, i) {
246 struct bch_sb_handle *sb = &ca->disk_sb;
248 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
249 percpu_ref_put(&ca->ref);
255 f = bch2_sb_field_get(sb->sb, type);
256 f = __bch2_sb_field_resize(sb, f, u64s);
258 f->type = cpu_to_le32(type);
262 /* Superblock validate: */
264 static inline void __bch2_sb_layout_size_assert(void)
266 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
269 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
271 u64 offset, prev_offset, max_sectors;
274 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
275 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
276 prt_printf(out, "Not a bcachefs superblock layout");
277 return -BCH_ERR_invalid_sb_layout;
280 if (layout->layout_type != 0) {
281 prt_printf(out, "Invalid superblock layout type %u",
282 layout->layout_type);
283 return -BCH_ERR_invalid_sb_layout_type;
286 if (!layout->nr_superblocks) {
287 prt_printf(out, "Invalid superblock layout: no superblocks");
288 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
291 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
292 prt_printf(out, "Invalid superblock layout: too many superblocks");
293 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
296 max_sectors = 1 << layout->sb_max_size_bits;
298 prev_offset = le64_to_cpu(layout->sb_offset[0]);
300 for (i = 1; i < layout->nr_superblocks; i++) {
301 offset = le64_to_cpu(layout->sb_offset[i]);
303 if (offset < prev_offset + max_sectors) {
304 prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
305 " (sb %u ends at %llu next starts at %llu",
306 i - 1, prev_offset + max_sectors, offset);
307 return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
309 prev_offset = offset;
315 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
317 u16 version = le16_to_cpu(sb->version);
318 u16 version_min = le16_to_cpu(sb->version_min);
320 if (!bch2_version_compatible(version)) {
321 prt_str(out, "Unsupported superblock version ");
322 bch2_version_to_text(out, version);
323 prt_str(out, " (min ");
324 bch2_version_to_text(out, bcachefs_metadata_version_min);
325 prt_str(out, ", max ");
326 bch2_version_to_text(out, bcachefs_metadata_version_current);
328 return -BCH_ERR_invalid_sb_version;
331 if (!bch2_version_compatible(version_min)) {
332 prt_str(out, "Unsupported superblock version_min ");
333 bch2_version_to_text(out, version_min);
334 prt_str(out, " (min ");
335 bch2_version_to_text(out, bcachefs_metadata_version_min);
336 prt_str(out, ", max ");
337 bch2_version_to_text(out, bcachefs_metadata_version_current);
339 return -BCH_ERR_invalid_sb_version;
342 if (version_min > version) {
343 prt_str(out, "Bad minimum version ");
344 bch2_version_to_text(out, version_min);
345 prt_str(out, ", greater than version field ");
346 bch2_version_to_text(out, version);
347 return -BCH_ERR_invalid_sb_version;
353 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
356 struct bch_sb *sb = disk_sb->sb;
357 struct bch_sb_field *f;
358 struct bch_sb_field_members *mi;
359 enum bch_opt_id opt_id;
363 ret = bch2_sb_compatible(sb, out);
367 if (sb->features[1] ||
368 (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
369 prt_printf(out, "Filesystem has incompatible features");
370 return -BCH_ERR_invalid_sb_features;
373 block_size = le16_to_cpu(sb->block_size);
375 if (block_size > PAGE_SECTORS) {
376 prt_printf(out, "Block size too big (got %u, max %u)",
377 block_size, PAGE_SECTORS);
378 return -BCH_ERR_invalid_sb_block_size;
381 if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
382 prt_printf(out, "Bad user UUID (got zeroes)");
383 return -BCH_ERR_invalid_sb_uuid;
386 if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
387 prt_printf(out, "Bad intenal UUID (got zeroes)");
388 return -BCH_ERR_invalid_sb_uuid;
391 if (!sb->nr_devices ||
392 sb->nr_devices > BCH_SB_MEMBERS_MAX) {
393 prt_printf(out, "Bad number of member devices %u (max %u)",
394 sb->nr_devices, BCH_SB_MEMBERS_MAX);
395 return -BCH_ERR_invalid_sb_too_many_members;
398 if (sb->dev_idx >= sb->nr_devices) {
399 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
400 sb->dev_idx, sb->nr_devices);
401 return -BCH_ERR_invalid_sb_dev_idx;
404 if (!sb->time_precision ||
405 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
406 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
407 le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
408 return -BCH_ERR_invalid_sb_time_precision;
413 * Been seeing a bug where these are getting inexplicably
414 * zeroed, so we're now validating them, but we have to be
415 * careful not to preven people's filesystems from mounting:
417 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
418 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
419 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
420 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
422 if (!BCH_SB_VERSION_UPGRADE_COMPLETE(sb))
423 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version));
426 for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
427 const struct bch_option *opt = bch2_opt_table + opt_id;
429 if (opt->get_sb != BCH2_NO_SB_OPT) {
430 u64 v = bch2_opt_from_sb(sb, opt_id);
432 prt_printf(out, "Invalid option ");
433 ret = bch2_opt_validate(opt, v, out);
441 /* validate layout */
442 ret = validate_sb_layout(&sb->layout, out);
446 vstruct_for_each(sb, f) {
448 prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
449 le32_to_cpu(f->type));
450 return -BCH_ERR_invalid_sb_field_size;
453 if (vstruct_next(f) > vstruct_last(sb)) {
454 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
455 le32_to_cpu(f->type));
456 return -BCH_ERR_invalid_sb_field_size;
460 /* members must be validated first: */
461 mi = bch2_sb_get_members(sb);
463 prt_printf(out, "Invalid superblock: member info area missing");
464 return -BCH_ERR_invalid_sb_members_missing;
467 ret = bch2_sb_field_validate(sb, &mi->field, out);
471 vstruct_for_each(sb, f) {
472 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
475 ret = bch2_sb_field_validate(sb, f, out);
485 static void bch2_sb_update(struct bch_fs *c)
487 struct bch_sb *src = c->disk_sb.sb;
488 struct bch_sb_field_members *mi = bch2_sb_get_members(src);
492 lockdep_assert_held(&c->sb_lock);
494 c->sb.uuid = src->uuid;
495 c->sb.user_uuid = src->user_uuid;
496 c->sb.version = le16_to_cpu(src->version);
497 c->sb.version_min = le16_to_cpu(src->version_min);
498 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src);
499 c->sb.nr_devices = src->nr_devices;
500 c->sb.clean = BCH_SB_CLEAN(src);
501 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
503 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
504 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
506 /* XXX this is wrong, we need a 96 or 128 bit integer type */
507 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
508 c->sb.nsec_per_time_unit);
509 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
511 c->sb.features = le64_to_cpu(src->features[0]);
512 c->sb.compat = le64_to_cpu(src->compat[0]);
514 for_each_member_device(ca, c, i)
515 ca->mi = bch2_mi_to_cpu(mi->members + i);
518 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
520 struct bch_sb_field *src_f, *dst_f;
521 struct bch_sb *dst = dst_handle->sb;
524 dst->version = src->version;
525 dst->version_min = src->version_min;
527 dst->uuid = src->uuid;
528 dst->user_uuid = src->user_uuid;
529 memcpy(dst->label, src->label, sizeof(dst->label));
531 dst->block_size = src->block_size;
532 dst->nr_devices = src->nr_devices;
534 dst->time_base_lo = src->time_base_lo;
535 dst->time_base_hi = src->time_base_hi;
536 dst->time_precision = src->time_precision;
538 memcpy(dst->flags, src->flags, sizeof(dst->flags));
539 memcpy(dst->features, src->features, sizeof(dst->features));
540 memcpy(dst->compat, src->compat, sizeof(dst->compat));
542 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
545 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
548 src_f = bch2_sb_field_get(src, i);
549 dst_f = bch2_sb_field_get(dst, i);
551 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
552 (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
554 int ret = bch2_sb_realloc(dst_handle, le32_to_cpu(dst_handle->sb->u64s) + d);
558 dst = dst_handle->sb;
559 dst_f = bch2_sb_field_get(dst, i);
562 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
563 src_f ? le32_to_cpu(src_f->u64s) : 0);
566 memcpy(dst_f, src_f, vstruct_bytes(src_f));
572 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
576 lockdep_assert_held(&c->sb_lock);
578 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
579 __copy_super(&c->disk_sb, src) ?:
580 bch2_sb_replicas_to_cpu_replicas(c) ?:
581 bch2_sb_disk_groups_to_cpu(c);
589 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
591 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
594 /* read superblock: */
596 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
598 struct bch_csum csum;
602 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
603 sb->bio->bi_iter.bi_sector = offset;
604 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
606 ret = submit_bio_wait(sb->bio);
608 prt_printf(err, "IO error: %i", ret);
612 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
613 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
614 prt_printf(err, "Not a bcachefs superblock");
615 return -BCH_ERR_invalid_sb_magic;
618 ret = bch2_sb_compatible(sb->sb, err);
622 bytes = vstruct_bytes(sb->sb);
624 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
625 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
626 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
627 return -BCH_ERR_invalid_sb_too_big;
630 if (bytes > sb->buffer_size) {
631 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
637 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
638 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
639 return -BCH_ERR_invalid_sb_csum_type;
642 /* XXX: verify MACs */
643 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
644 null_nonce(), sb->sb);
646 if (bch2_crc_cmp(csum, sb->sb->csum)) {
647 prt_printf(err, "bad checksum");
648 return -BCH_ERR_invalid_sb_csum;
651 sb->seq = le64_to_cpu(sb->sb->seq);
656 int bch2_read_super(const char *path, struct bch_opts *opts,
657 struct bch_sb_handle *sb)
659 u64 offset = opt_get(*opts, sb);
660 struct bch_sb_layout layout;
661 struct printbuf err = PRINTBUF;
667 memset(sb, 0, sizeof(*sb));
668 sb->mode = FMODE_READ;
672 if (opt_get(*opts, direct_io) == false)
673 sb->mode |= FMODE_BUFFERED;
676 if (!opt_get(*opts, noexcl))
677 sb->mode |= FMODE_EXCL;
679 if (!opt_get(*opts, nochanges))
680 sb->mode |= FMODE_WRITE;
682 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
683 if (IS_ERR(sb->bdev) &&
684 PTR_ERR(sb->bdev) == -EACCES &&
685 opt_get(*opts, read_only)) {
686 sb->mode &= ~FMODE_WRITE;
688 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
689 if (!IS_ERR(sb->bdev))
690 opt_set(*opts, nochanges, true);
693 if (IS_ERR(sb->bdev)) {
694 ret = PTR_ERR(sb->bdev);
698 ret = bch2_sb_realloc(sb, 0);
700 prt_printf(&err, "error allocating memory for superblock");
704 if (bch2_fs_init_fault("read_super")) {
705 prt_printf(&err, "dynamic fault");
710 ret = read_one_super(sb, offset, &err);
714 if (opt_defined(*opts, sb))
717 printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
719 printbuf_reset(&err);
722 * Error reading primary superblock - read location of backup
725 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
726 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
728 * use sb buffer to read layout, since sb buffer is page aligned but
731 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
733 ret = submit_bio_wait(sb->bio);
735 prt_printf(&err, "IO error: %i", ret);
739 memcpy(&layout, sb->sb, sizeof(layout));
740 ret = validate_sb_layout(&layout, &err);
744 for (i = layout.sb_offset;
745 i < layout.sb_offset + layout.nr_superblocks; i++) {
746 offset = le64_to_cpu(*i);
748 if (offset == opt_get(*opts, sb))
751 ret = read_one_super(sb, offset, &err);
759 if (le16_to_cpu(sb->sb->block_size) << 9 <
760 bdev_logical_block_size(sb->bdev) &&
761 opt_get(*opts, direct_io)) {
763 opt_set(*opts, direct_io, false);
767 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
768 le16_to_cpu(sb->sb->block_size) << 9,
769 bdev_logical_block_size(sb->bdev));
770 ret = -BCH_ERR_block_size_too_small;
775 sb->have_layout = true;
777 ret = bch2_sb_validate(sb, &err, READ);
779 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
787 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
794 /* write superblock: */
796 static void write_super_endio(struct bio *bio)
798 struct bch_dev *ca = bio->bi_private;
800 /* XXX: return errors directly */
802 if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
803 bch2_blk_status_to_str(bio->bi_status)))
804 ca->sb_write_error = 1;
806 closure_put(&ca->fs->sb_write);
807 percpu_ref_put(&ca->io_ref);
810 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
812 struct bch_sb *sb = ca->disk_sb.sb;
813 struct bio *bio = ca->disk_sb.bio;
815 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
816 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
817 bio->bi_end_io = write_super_endio;
818 bio->bi_private = ca;
819 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
821 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
824 percpu_ref_get(&ca->io_ref);
825 closure_bio_submit(bio, &c->sb_write);
828 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
830 struct bch_sb *sb = ca->disk_sb.sb;
831 struct bio *bio = ca->disk_sb.bio;
833 sb->offset = sb->layout.sb_offset[idx];
835 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
836 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
839 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
840 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
841 bio->bi_end_io = write_super_endio;
842 bio->bi_private = ca;
843 bch2_bio_map(bio, sb,
844 roundup((size_t) vstruct_bytes(sb),
845 bdev_logical_block_size(ca->disk_sb.bdev)));
847 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
850 percpu_ref_get(&ca->io_ref);
851 closure_bio_submit(bio, &c->sb_write);
854 int bch2_write_super(struct bch_fs *c)
856 struct closure *cl = &c->sb_write;
858 struct printbuf err = PRINTBUF;
859 unsigned i, sb = 0, nr_wrote;
860 struct bch_devs_mask sb_written;
861 bool wrote, can_mount_without_written, can_mount_with_written;
862 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
865 trace_and_count(c, write_super, c, _RET_IP_);
867 if (c->opts.very_degraded)
868 degraded_flags |= BCH_FORCE_IF_LOST;
870 lockdep_assert_held(&c->sb_lock);
872 closure_init_stack(cl);
873 memset(&sb_written, 0, sizeof(sb_written));
875 /* Make sure we're using the new magic numbers: */
876 c->disk_sb.sb->magic = BCHFS_MAGIC;
877 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
879 le64_add_cpu(&c->disk_sb.sb->seq, 1);
881 if (test_bit(BCH_FS_ERROR, &c->flags))
882 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
883 if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
884 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
886 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
888 bch2_sb_counters_from_cpu(c);
890 for_each_online_member(ca, c, i)
891 bch2_sb_from_fs(c, ca);
893 for_each_online_member(ca, c, i) {
894 printbuf_reset(&err);
896 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
898 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
899 percpu_ref_put(&ca->io_ref);
904 if (c->opts.nochanges)
908 * Defer writing the superblock until filesystem initialization is
909 * complete - don't write out a partly initialized superblock:
911 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
914 for_each_online_member(ca, c, i) {
915 __set_bit(ca->dev_idx, sb_written.d);
916 ca->sb_write_error = 0;
919 for_each_online_member(ca, c, i)
920 read_back_super(c, ca);
923 for_each_online_member(ca, c, i) {
924 if (ca->sb_write_error)
927 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
928 bch2_fs_fatal_error(c,
929 "Superblock write was silently dropped! (seq %llu expected %llu)",
930 le64_to_cpu(ca->sb_read_scratch->seq),
932 percpu_ref_put(&ca->io_ref);
933 ret = -BCH_ERR_erofs_sb_err;
937 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
938 bch2_fs_fatal_error(c,
939 "Superblock modified by another process (seq %llu expected %llu)",
940 le64_to_cpu(ca->sb_read_scratch->seq),
942 percpu_ref_put(&ca->io_ref);
943 ret = -BCH_ERR_erofs_sb_err;
950 for_each_online_member(ca, c, i)
951 if (!ca->sb_write_error &&
952 sb < ca->disk_sb.sb->layout.nr_superblocks) {
953 write_one_super(c, ca, sb);
960 for_each_online_member(ca, c, i) {
961 if (ca->sb_write_error)
962 __clear_bit(ca->dev_idx, sb_written.d);
964 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
967 nr_wrote = dev_mask_nr(&sb_written);
969 can_mount_with_written =
970 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
972 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
973 sb_written.d[i] = ~sb_written.d[i];
975 can_mount_without_written =
976 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
979 * If we would be able to mount _without_ the devices we successfully
980 * wrote superblocks to, we weren't able to write to enough devices:
982 * Exception: if we can mount without the successes because we haven't
983 * written anything (new filesystem), we continue if we'd be able to
984 * mount with the devices we did successfully write to:
986 if (bch2_fs_fatal_err_on(!nr_wrote ||
987 !can_mount_with_written ||
988 (can_mount_without_written &&
989 !can_mount_with_written), c,
990 "Unable to write superblock to sufficient devices (from %ps)",
994 /* Make new options visible after they're persistent: */
1000 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1002 mutex_lock(&c->sb_lock);
1003 if (!(c->sb.features & (1ULL << feat))) {
1004 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1006 bch2_write_super(c);
1008 mutex_unlock(&c->sb_lock);
1011 /* BCH_SB_FIELD_members: */
1013 static int bch2_sb_members_validate(struct bch_sb *sb,
1014 struct bch_sb_field *f,
1015 struct printbuf *err)
1017 struct bch_sb_field_members *mi = field_to_type(f, members);
1020 if ((void *) (mi->members + sb->nr_devices) >
1021 vstruct_end(&mi->field)) {
1022 prt_printf(err, "too many devices for section size");
1023 return -BCH_ERR_invalid_sb_members;
1026 for (i = 0; i < sb->nr_devices; i++) {
1027 struct bch_member *m = mi->members + i;
1029 if (!bch2_member_exists(m))
1032 if (le64_to_cpu(m->nbuckets) > LONG_MAX) {
1033 prt_printf(err, "device %u: too many buckets (got %llu, max %lu)",
1034 i, le64_to_cpu(m->nbuckets), LONG_MAX);
1035 return -BCH_ERR_invalid_sb_members;
1038 if (le64_to_cpu(m->nbuckets) -
1039 le16_to_cpu(m->first_bucket) < BCH_MIN_NR_NBUCKETS) {
1040 prt_printf(err, "device %u: not enough buckets (got %llu, max %u)",
1041 i, le64_to_cpu(m->nbuckets), BCH_MIN_NR_NBUCKETS);
1042 return -BCH_ERR_invalid_sb_members;
1045 if (le16_to_cpu(m->bucket_size) <
1046 le16_to_cpu(sb->block_size)) {
1047 prt_printf(err, "device %u: bucket size %u smaller than block size %u",
1048 i, le16_to_cpu(m->bucket_size), le16_to_cpu(sb->block_size));
1049 return -BCH_ERR_invalid_sb_members;
1052 if (le16_to_cpu(m->bucket_size) <
1053 BCH_SB_BTREE_NODE_SIZE(sb)) {
1054 prt_printf(err, "device %u: bucket size %u smaller than btree node size %llu",
1055 i, le16_to_cpu(m->bucket_size), BCH_SB_BTREE_NODE_SIZE(sb));
1056 return -BCH_ERR_invalid_sb_members;
1063 static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb,
1064 struct bch_sb_field *f)
1066 struct bch_sb_field_members *mi = field_to_type(f, members);
1067 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
1070 for (i = 0; i < sb->nr_devices; i++) {
1071 struct bch_member *m = mi->members + i;
1072 unsigned data_have = bch2_sb_dev_has_data(sb, i);
1073 u64 bucket_size = le16_to_cpu(m->bucket_size);
1074 u64 device_size = le64_to_cpu(m->nbuckets) * bucket_size;
1076 if (!bch2_member_exists(m))
1079 prt_printf(out, "Device:");
1081 prt_printf(out, "%u", i);
1084 printbuf_indent_add(out, 2);
1086 prt_printf(out, "UUID:");
1088 pr_uuid(out, m->uuid.b);
1091 prt_printf(out, "Size:");
1093 prt_units_u64(out, device_size << 9);
1096 prt_printf(out, "Bucket size:");
1098 prt_units_u64(out, bucket_size << 9);
1101 prt_printf(out, "First bucket:");
1103 prt_printf(out, "%u", le16_to_cpu(m->first_bucket));
1106 prt_printf(out, "Buckets:");
1108 prt_printf(out, "%llu", le64_to_cpu(m->nbuckets));
1111 prt_printf(out, "Last mount:");
1114 pr_time(out, le64_to_cpu(m->last_mount));
1116 prt_printf(out, "(never)");
1119 prt_printf(out, "State:");
1121 prt_printf(out, "%s",
1122 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
1123 ? bch2_member_states[BCH_MEMBER_STATE(m)]
1127 prt_printf(out, "Label:");
1129 if (BCH_MEMBER_GROUP(m)) {
1130 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
1132 if (idx < disk_groups_nr(gi))
1133 prt_printf(out, "%s (%u)",
1134 gi->entries[idx].label, idx);
1136 prt_printf(out, "(bad disk labels section)");
1138 prt_printf(out, "(none)");
1142 prt_printf(out, "Data allowed:");
1144 if (BCH_MEMBER_DATA_ALLOWED(m))
1145 prt_bitflags(out, bch2_data_types, BCH_MEMBER_DATA_ALLOWED(m));
1147 prt_printf(out, "(none)");
1150 prt_printf(out, "Has data:");
1153 prt_bitflags(out, bch2_data_types, data_have);
1155 prt_printf(out, "(none)");
1158 prt_printf(out, "Discard:");
1160 prt_printf(out, "%llu", BCH_MEMBER_DISCARD(m));
1163 prt_printf(out, "Freespace initialized:");
1165 prt_printf(out, "%llu", BCH_MEMBER_FREESPACE_INITIALIZED(m));
1168 printbuf_indent_sub(out, 2);
1172 static const struct bch_sb_field_ops bch_sb_field_ops_members = {
1173 .validate = bch2_sb_members_validate,
1174 .to_text = bch2_sb_members_to_text,
1177 /* BCH_SB_FIELD_crypt: */
1179 static int bch2_sb_crypt_validate(struct bch_sb *sb,
1180 struct bch_sb_field *f,
1181 struct printbuf *err)
1183 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1185 if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
1186 prt_printf(err, "wrong size (got %zu should be %zu)",
1187 vstruct_bytes(&crypt->field), sizeof(*crypt));
1188 return -BCH_ERR_invalid_sb_crypt;
1191 if (BCH_CRYPT_KDF_TYPE(crypt)) {
1192 prt_printf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
1193 return -BCH_ERR_invalid_sb_crypt;
1199 static void bch2_sb_crypt_to_text(struct printbuf *out, struct bch_sb *sb,
1200 struct bch_sb_field *f)
1202 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1204 prt_printf(out, "KFD: %llu", BCH_CRYPT_KDF_TYPE(crypt));
1206 prt_printf(out, "scrypt n: %llu", BCH_KDF_SCRYPT_N(crypt));
1208 prt_printf(out, "scrypt r: %llu", BCH_KDF_SCRYPT_R(crypt));
1210 prt_printf(out, "scrypt p: %llu", BCH_KDF_SCRYPT_P(crypt));
1214 static const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
1215 .validate = bch2_sb_crypt_validate,
1216 .to_text = bch2_sb_crypt_to_text,
1219 /* BCH_SB_FIELD_clean: */
1221 int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *clean, int write)
1223 struct jset_entry *entry;
1226 for (entry = clean->start;
1227 entry < (struct jset_entry *) vstruct_end(&clean->field);
1228 entry = vstruct_next(entry)) {
1229 ret = bch2_journal_entry_validate(c, NULL, entry,
1230 le16_to_cpu(c->disk_sb.sb->version),
1231 BCH_SB_BIG_ENDIAN(c->disk_sb.sb),
1240 /* Downgrade if superblock is at a higher version than currently supported: */
1241 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1243 lockdep_assert_held(&c->sb_lock);
1246 * Downgrade, if superblock is at a higher version than currently
1249 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1250 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1251 if (c->sb.version > bcachefs_metadata_version_current)
1252 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1253 if (c->sb.version_min > bcachefs_metadata_version_current)
1254 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1255 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1258 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1260 lockdep_assert_held(&c->sb_lock);
1262 c->disk_sb.sb->version = cpu_to_le16(new_version);
1263 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1266 int bch2_fs_mark_dirty(struct bch_fs *c)
1271 * Unconditionally write superblock, to verify it hasn't changed before
1275 mutex_lock(&c->sb_lock);
1276 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1278 bch2_sb_maybe_downgrade(c);
1279 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
1281 ret = bch2_write_super(c);
1282 mutex_unlock(&c->sb_lock);
1287 static struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
1289 struct jset_entry *entry = *end;
1290 unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
1292 memset(entry, 0, u64s * sizeof(u64));
1294 * The u64s field counts from the start of data, ignoring the shared
1297 entry->u64s = cpu_to_le16(u64s - 1);
1299 *end = vstruct_next(*end);
1303 void bch2_journal_super_entries_add_common(struct bch_fs *c,
1304 struct jset_entry **end,
1310 percpu_down_read(&c->mark_lock);
1313 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1314 bch2_fs_usage_acc_to_base(c, i);
1316 bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK);
1320 struct jset_entry_usage *u =
1321 container_of(jset_entry_init(end, sizeof(*u)),
1322 struct jset_entry_usage, entry);
1324 u->entry.type = BCH_JSET_ENTRY_usage;
1325 u->entry.btree_id = BCH_FS_USAGE_inodes;
1326 u->v = cpu_to_le64(c->usage_base->nr_inodes);
1330 struct jset_entry_usage *u =
1331 container_of(jset_entry_init(end, sizeof(*u)),
1332 struct jset_entry_usage, entry);
1334 u->entry.type = BCH_JSET_ENTRY_usage;
1335 u->entry.btree_id = BCH_FS_USAGE_key_version;
1336 u->v = cpu_to_le64(atomic64_read(&c->key_version));
1339 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1340 struct jset_entry_usage *u =
1341 container_of(jset_entry_init(end, sizeof(*u)),
1342 struct jset_entry_usage, entry);
1344 u->entry.type = BCH_JSET_ENTRY_usage;
1345 u->entry.btree_id = BCH_FS_USAGE_reserved;
1347 u->v = cpu_to_le64(c->usage_base->persistent_reserved[i]);
1350 for (i = 0; i < c->replicas.nr; i++) {
1351 struct bch_replicas_entry *e =
1352 cpu_replicas_entry(&c->replicas, i);
1353 struct jset_entry_data_usage *u =
1354 container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs),
1355 struct jset_entry_data_usage, entry);
1357 u->entry.type = BCH_JSET_ENTRY_data_usage;
1358 u->v = cpu_to_le64(c->usage_base->replicas[i]);
1359 unsafe_memcpy(&u->r, e, replicas_entry_bytes(e),
1360 "embedded variable length struct");
1363 for_each_member_device(ca, c, dev) {
1364 unsigned b = sizeof(struct jset_entry_dev_usage) +
1365 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR;
1366 struct jset_entry_dev_usage *u =
1367 container_of(jset_entry_init(end, b),
1368 struct jset_entry_dev_usage, entry);
1370 u->entry.type = BCH_JSET_ENTRY_dev_usage;
1371 u->dev = cpu_to_le32(dev);
1372 u->buckets_ec = cpu_to_le64(ca->usage_base->buckets_ec);
1374 for (i = 0; i < BCH_DATA_NR; i++) {
1375 u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets);
1376 u->d[i].sectors = cpu_to_le64(ca->usage_base->d[i].sectors);
1377 u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented);
1381 percpu_up_read(&c->mark_lock);
1383 for (i = 0; i < 2; i++) {
1384 struct jset_entry_clock *clock =
1385 container_of(jset_entry_init(end, sizeof(*clock)),
1386 struct jset_entry_clock, entry);
1388 clock->entry.type = BCH_JSET_ENTRY_clock;
1390 clock->time = cpu_to_le64(atomic64_read(&c->io_clock[i].now));
1394 void bch2_fs_mark_clean(struct bch_fs *c)
1396 struct bch_sb_field_clean *sb_clean;
1397 struct jset_entry *entry;
1401 mutex_lock(&c->sb_lock);
1402 if (BCH_SB_CLEAN(c->disk_sb.sb))
1405 SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
1407 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
1408 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata);
1409 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates));
1410 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled));
1412 u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved;
1414 sb_clean = bch2_sb_resize_clean(&c->disk_sb, u64s);
1416 bch_err(c, "error resizing superblock while setting filesystem clean");
1420 sb_clean->flags = 0;
1421 sb_clean->journal_seq = cpu_to_le64(atomic64_read(&c->journal.seq));
1423 /* Trying to catch outstanding bug: */
1424 BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX);
1426 entry = sb_clean->start;
1427 bch2_journal_super_entries_add_common(c, &entry, 0);
1428 entry = bch2_btree_roots_to_journal_entries(c, entry, entry);
1429 BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
1432 vstruct_end(&sb_clean->field) - (void *) entry);
1435 * this should be in the write path, and we should be validating every
1436 * superblock section:
1438 ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE);
1440 bch_err(c, "error writing marking filesystem clean: validate error");
1444 bch2_write_super(c);
1446 mutex_unlock(&c->sb_lock);
1449 static int bch2_sb_clean_validate(struct bch_sb *sb,
1450 struct bch_sb_field *f,
1451 struct printbuf *err)
1453 struct bch_sb_field_clean *clean = field_to_type(f, clean);
1455 if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
1456 prt_printf(err, "wrong size (got %zu should be %zu)",
1457 vstruct_bytes(&clean->field), sizeof(*clean));
1458 return -BCH_ERR_invalid_sb_clean;
1464 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
1465 struct bch_sb_field *f)
1467 struct bch_sb_field_clean *clean = field_to_type(f, clean);
1468 struct jset_entry *entry;
1470 prt_printf(out, "flags: %x", le32_to_cpu(clean->flags));
1472 prt_printf(out, "journal_seq: %llu", le64_to_cpu(clean->journal_seq));
1475 for (entry = clean->start;
1476 entry != vstruct_end(&clean->field);
1477 entry = vstruct_next(entry)) {
1478 if (entry->type == BCH_JSET_ENTRY_btree_keys &&
1482 bch2_journal_entry_to_text(out, NULL, entry);
1487 static const struct bch_sb_field_ops bch_sb_field_ops_clean = {
1488 .validate = bch2_sb_clean_validate,
1489 .to_text = bch2_sb_clean_to_text,
1492 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1494 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1499 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1501 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1503 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1504 ? bch2_sb_field_ops[type]
1505 : &bch2_sb_field_null_ops;
1508 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1509 struct printbuf *err)
1511 unsigned type = le32_to_cpu(f->type);
1512 struct printbuf field_err = PRINTBUF;
1513 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1516 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1518 prt_printf(err, "Invalid superblock section %s: %s",
1519 bch2_sb_fields[type], field_err.buf);
1521 bch2_sb_field_to_text(err, sb, f);
1524 printbuf_exit(&field_err);
1528 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1529 struct bch_sb_field *f)
1531 unsigned type = le32_to_cpu(f->type);
1532 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1534 if (!out->nr_tabstops)
1535 printbuf_tabstop_push(out, 32);
1537 if (type < BCH_SB_FIELD_NR)
1538 prt_printf(out, "%s", bch2_sb_fields[type]);
1540 prt_printf(out, "(unknown field %u)", type);
1542 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1546 printbuf_indent_add(out, 2);
1547 ops->to_text(out, sb, f);
1548 printbuf_indent_sub(out, 2);
1552 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1556 prt_printf(out, "Type: %u", l->layout_type);
1559 prt_str(out, "Superblock max size: ");
1560 prt_units_u64(out, 512 << l->sb_max_size_bits);
1563 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1566 prt_str(out, "Offsets: ");
1567 for (i = 0; i < l->nr_superblocks; i++) {
1570 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1575 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1576 bool print_layout, unsigned fields)
1578 struct bch_sb_field_members *mi;
1579 struct bch_sb_field *f;
1580 u64 fields_have = 0;
1581 unsigned nr_devices = 0;
1583 if (!out->nr_tabstops)
1584 printbuf_tabstop_push(out, 44);
1586 mi = bch2_sb_get_members(sb);
1588 struct bch_member *m;
1590 for (m = mi->members;
1591 m < mi->members + sb->nr_devices;
1593 nr_devices += bch2_member_exists(m);
1596 prt_printf(out, "External UUID:");
1598 pr_uuid(out, sb->user_uuid.b);
1601 prt_printf(out, "Internal UUID:");
1603 pr_uuid(out, sb->uuid.b);
1606 prt_str(out, "Device index:");
1608 prt_printf(out, "%u", sb->dev_idx);
1611 prt_str(out, "Label:");
1613 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1616 prt_str(out, "Version:");
1618 bch2_version_to_text(out, le16_to_cpu(sb->version));
1621 prt_str(out, "Version upgrade complete:");
1623 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1626 prt_printf(out, "Oldest version on disk:");
1628 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1631 prt_printf(out, "Created:");
1633 if (sb->time_base_lo)
1634 pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1636 prt_printf(out, "(not set)");
1639 prt_printf(out, "Sequence number:");
1641 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1644 prt_printf(out, "Superblock size:");
1646 prt_printf(out, "%zu", vstruct_bytes(sb));
1649 prt_printf(out, "Clean:");
1651 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1654 prt_printf(out, "Devices:");
1656 prt_printf(out, "%u", nr_devices);
1659 prt_printf(out, "Sections:");
1660 vstruct_for_each(sb, f)
1661 fields_have |= 1 << le32_to_cpu(f->type);
1663 prt_bitflags(out, bch2_sb_fields, fields_have);
1666 prt_printf(out, "Features:");
1668 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1671 prt_printf(out, "Compat features:");
1673 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1677 prt_printf(out, "Options:");
1679 printbuf_indent_add(out, 2);
1683 for (id = 0; id < bch2_opts_nr; id++) {
1684 const struct bch_option *opt = bch2_opt_table + id;
1686 if (opt->get_sb != BCH2_NO_SB_OPT) {
1687 u64 v = bch2_opt_from_sb(sb, id);
1689 prt_printf(out, "%s:", opt->attr.name);
1691 bch2_opt_to_text(out, NULL, sb, opt, v,
1692 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1698 printbuf_indent_sub(out, 2);
1702 prt_printf(out, "layout:");
1704 printbuf_indent_add(out, 2);
1705 bch2_sb_layout_to_text(out, &sb->layout);
1706 printbuf_indent_sub(out, 2);
1709 vstruct_for_each(sb, f)
1710 if (fields & (1 << le32_to_cpu(f->type))) {
1712 bch2_sb_field_to_text(out, sb, f);