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);
423 for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
424 const struct bch_option *opt = bch2_opt_table + opt_id;
426 if (opt->get_sb != BCH2_NO_SB_OPT) {
427 u64 v = bch2_opt_from_sb(sb, opt_id);
429 prt_printf(out, "Invalid option ");
430 ret = bch2_opt_validate(opt, v, out);
438 /* validate layout */
439 ret = validate_sb_layout(&sb->layout, out);
443 vstruct_for_each(sb, f) {
445 prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
446 le32_to_cpu(f->type));
447 return -BCH_ERR_invalid_sb_field_size;
450 if (vstruct_next(f) > vstruct_last(sb)) {
451 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
452 le32_to_cpu(f->type));
453 return -BCH_ERR_invalid_sb_field_size;
457 /* members must be validated first: */
458 mi = bch2_sb_get_members(sb);
460 prt_printf(out, "Invalid superblock: member info area missing");
461 return -BCH_ERR_invalid_sb_members_missing;
464 ret = bch2_sb_field_validate(sb, &mi->field, out);
468 vstruct_for_each(sb, f) {
469 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
472 ret = bch2_sb_field_validate(sb, f, out);
482 static void bch2_sb_update(struct bch_fs *c)
484 struct bch_sb *src = c->disk_sb.sb;
485 struct bch_sb_field_members *mi = bch2_sb_get_members(src);
489 lockdep_assert_held(&c->sb_lock);
491 c->sb.uuid = src->uuid;
492 c->sb.user_uuid = src->user_uuid;
493 c->sb.version = le16_to_cpu(src->version);
494 c->sb.version_min = le16_to_cpu(src->version_min);
495 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src) ?: c->sb.version;
496 c->sb.nr_devices = src->nr_devices;
497 c->sb.clean = BCH_SB_CLEAN(src);
498 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
500 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
501 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
503 /* XXX this is wrong, we need a 96 or 128 bit integer type */
504 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
505 c->sb.nsec_per_time_unit);
506 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
508 c->sb.features = le64_to_cpu(src->features[0]);
509 c->sb.compat = le64_to_cpu(src->compat[0]);
511 for_each_member_device(ca, c, i)
512 ca->mi = bch2_mi_to_cpu(mi->members + i);
515 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
517 struct bch_sb_field *src_f, *dst_f;
518 struct bch_sb *dst = dst_handle->sb;
521 dst->version = src->version;
522 dst->version_min = src->version_min;
524 dst->uuid = src->uuid;
525 dst->user_uuid = src->user_uuid;
526 memcpy(dst->label, src->label, sizeof(dst->label));
528 dst->block_size = src->block_size;
529 dst->nr_devices = src->nr_devices;
531 dst->time_base_lo = src->time_base_lo;
532 dst->time_base_hi = src->time_base_hi;
533 dst->time_precision = src->time_precision;
535 memcpy(dst->flags, src->flags, sizeof(dst->flags));
536 memcpy(dst->features, src->features, sizeof(dst->features));
537 memcpy(dst->compat, src->compat, sizeof(dst->compat));
539 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
542 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
545 src_f = bch2_sb_field_get(src, i);
546 dst_f = bch2_sb_field_get(dst, i);
548 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
549 (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
551 int ret = bch2_sb_realloc(dst_handle, le32_to_cpu(dst_handle->sb->u64s) + d);
555 dst = dst_handle->sb;
556 dst_f = bch2_sb_field_get(dst, i);
559 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
560 src_f ? le32_to_cpu(src_f->u64s) : 0);
563 memcpy(dst_f, src_f, vstruct_bytes(src_f));
569 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
573 lockdep_assert_held(&c->sb_lock);
575 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
576 __copy_super(&c->disk_sb, src) ?:
577 bch2_sb_replicas_to_cpu_replicas(c) ?:
578 bch2_sb_disk_groups_to_cpu(c);
586 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
588 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
591 /* read superblock: */
593 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
595 struct bch_csum csum;
599 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
600 sb->bio->bi_iter.bi_sector = offset;
601 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
603 ret = submit_bio_wait(sb->bio);
605 prt_printf(err, "IO error: %i", ret);
609 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
610 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
611 prt_printf(err, "Not a bcachefs superblock");
612 return -BCH_ERR_invalid_sb_magic;
615 ret = bch2_sb_compatible(sb->sb, err);
619 bytes = vstruct_bytes(sb->sb);
621 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
622 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
623 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
624 return -BCH_ERR_invalid_sb_too_big;
627 if (bytes > sb->buffer_size) {
628 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
634 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
635 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
636 return -BCH_ERR_invalid_sb_csum_type;
639 /* XXX: verify MACs */
640 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
641 null_nonce(), sb->sb);
643 if (bch2_crc_cmp(csum, sb->sb->csum)) {
644 prt_printf(err, "bad checksum");
645 return -BCH_ERR_invalid_sb_csum;
648 sb->seq = le64_to_cpu(sb->sb->seq);
653 int bch2_read_super(const char *path, struct bch_opts *opts,
654 struct bch_sb_handle *sb)
656 u64 offset = opt_get(*opts, sb);
657 struct bch_sb_layout layout;
658 struct printbuf err = PRINTBUF;
664 memset(sb, 0, sizeof(*sb));
665 sb->mode = FMODE_READ;
669 if (opt_get(*opts, direct_io) == false)
670 sb->mode |= FMODE_BUFFERED;
673 if (!opt_get(*opts, noexcl))
674 sb->mode |= FMODE_EXCL;
676 if (!opt_get(*opts, nochanges))
677 sb->mode |= FMODE_WRITE;
679 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
680 if (IS_ERR(sb->bdev) &&
681 PTR_ERR(sb->bdev) == -EACCES &&
682 opt_get(*opts, read_only)) {
683 sb->mode &= ~FMODE_WRITE;
685 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
686 if (!IS_ERR(sb->bdev))
687 opt_set(*opts, nochanges, true);
690 if (IS_ERR(sb->bdev)) {
691 ret = PTR_ERR(sb->bdev);
695 ret = bch2_sb_realloc(sb, 0);
697 prt_printf(&err, "error allocating memory for superblock");
701 if (bch2_fs_init_fault("read_super")) {
702 prt_printf(&err, "dynamic fault");
707 ret = read_one_super(sb, offset, &err);
711 if (opt_defined(*opts, sb))
714 printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
716 printbuf_reset(&err);
719 * Error reading primary superblock - read location of backup
722 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
723 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
725 * use sb buffer to read layout, since sb buffer is page aligned but
728 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
730 ret = submit_bio_wait(sb->bio);
732 prt_printf(&err, "IO error: %i", ret);
736 memcpy(&layout, sb->sb, sizeof(layout));
737 ret = validate_sb_layout(&layout, &err);
741 for (i = layout.sb_offset;
742 i < layout.sb_offset + layout.nr_superblocks; i++) {
743 offset = le64_to_cpu(*i);
745 if (offset == opt_get(*opts, sb))
748 ret = read_one_super(sb, offset, &err);
756 if (le16_to_cpu(sb->sb->block_size) << 9 <
757 bdev_logical_block_size(sb->bdev) &&
758 opt_get(*opts, direct_io)) {
760 opt_set(*opts, direct_io, false);
764 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
765 le16_to_cpu(sb->sb->block_size) << 9,
766 bdev_logical_block_size(sb->bdev));
767 ret = -BCH_ERR_block_size_too_small;
772 sb->have_layout = true;
774 ret = bch2_sb_validate(sb, &err, READ);
776 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
784 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
791 /* write superblock: */
793 static void write_super_endio(struct bio *bio)
795 struct bch_dev *ca = bio->bi_private;
797 /* XXX: return errors directly */
799 if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
800 bch2_blk_status_to_str(bio->bi_status)))
801 ca->sb_write_error = 1;
803 closure_put(&ca->fs->sb_write);
804 percpu_ref_put(&ca->io_ref);
807 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
809 struct bch_sb *sb = ca->disk_sb.sb;
810 struct bio *bio = ca->disk_sb.bio;
812 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
813 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
814 bio->bi_end_io = write_super_endio;
815 bio->bi_private = ca;
816 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
818 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
821 percpu_ref_get(&ca->io_ref);
822 closure_bio_submit(bio, &c->sb_write);
825 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
827 struct bch_sb *sb = ca->disk_sb.sb;
828 struct bio *bio = ca->disk_sb.bio;
830 sb->offset = sb->layout.sb_offset[idx];
832 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
833 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
836 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
837 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
838 bio->bi_end_io = write_super_endio;
839 bio->bi_private = ca;
840 bch2_bio_map(bio, sb,
841 roundup((size_t) vstruct_bytes(sb),
842 bdev_logical_block_size(ca->disk_sb.bdev)));
844 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
847 percpu_ref_get(&ca->io_ref);
848 closure_bio_submit(bio, &c->sb_write);
851 int bch2_write_super(struct bch_fs *c)
853 struct closure *cl = &c->sb_write;
855 struct printbuf err = PRINTBUF;
856 unsigned i, sb = 0, nr_wrote;
857 struct bch_devs_mask sb_written;
858 bool wrote, can_mount_without_written, can_mount_with_written;
859 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
862 trace_and_count(c, write_super, c, _RET_IP_);
864 if (c->opts.very_degraded)
865 degraded_flags |= BCH_FORCE_IF_LOST;
867 lockdep_assert_held(&c->sb_lock);
869 closure_init_stack(cl);
870 memset(&sb_written, 0, sizeof(sb_written));
872 /* Make sure we're using the new magic numbers: */
873 c->disk_sb.sb->magic = BCHFS_MAGIC;
874 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
876 le64_add_cpu(&c->disk_sb.sb->seq, 1);
878 if (test_bit(BCH_FS_ERROR, &c->flags))
879 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
880 if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
881 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
883 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
885 bch2_sb_counters_from_cpu(c);
887 for_each_online_member(ca, c, i)
888 bch2_sb_from_fs(c, ca);
890 for_each_online_member(ca, c, i) {
891 printbuf_reset(&err);
893 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
895 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
896 percpu_ref_put(&ca->io_ref);
901 if (c->opts.nochanges)
905 * Defer writing the superblock until filesystem initialization is
906 * complete - don't write out a partly initialized superblock:
908 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
911 for_each_online_member(ca, c, i) {
912 __set_bit(ca->dev_idx, sb_written.d);
913 ca->sb_write_error = 0;
916 for_each_online_member(ca, c, i)
917 read_back_super(c, ca);
920 for_each_online_member(ca, c, i) {
921 if (ca->sb_write_error)
924 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
925 bch2_fs_fatal_error(c,
926 "Superblock write was silently dropped! (seq %llu expected %llu)",
927 le64_to_cpu(ca->sb_read_scratch->seq),
929 percpu_ref_put(&ca->io_ref);
930 ret = -BCH_ERR_erofs_sb_err;
934 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
935 bch2_fs_fatal_error(c,
936 "Superblock modified by another process (seq %llu expected %llu)",
937 le64_to_cpu(ca->sb_read_scratch->seq),
939 percpu_ref_put(&ca->io_ref);
940 ret = -BCH_ERR_erofs_sb_err;
947 for_each_online_member(ca, c, i)
948 if (!ca->sb_write_error &&
949 sb < ca->disk_sb.sb->layout.nr_superblocks) {
950 write_one_super(c, ca, sb);
957 for_each_online_member(ca, c, i) {
958 if (ca->sb_write_error)
959 __clear_bit(ca->dev_idx, sb_written.d);
961 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
964 nr_wrote = dev_mask_nr(&sb_written);
966 can_mount_with_written =
967 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
969 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
970 sb_written.d[i] = ~sb_written.d[i];
972 can_mount_without_written =
973 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
976 * If we would be able to mount _without_ the devices we successfully
977 * wrote superblocks to, we weren't able to write to enough devices:
979 * Exception: if we can mount without the successes because we haven't
980 * written anything (new filesystem), we continue if we'd be able to
981 * mount with the devices we did successfully write to:
983 if (bch2_fs_fatal_err_on(!nr_wrote ||
984 !can_mount_with_written ||
985 (can_mount_without_written &&
986 !can_mount_with_written), c,
987 "Unable to write superblock to sufficient devices (from %ps)",
991 /* Make new options visible after they're persistent: */
997 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
999 mutex_lock(&c->sb_lock);
1000 if (!(c->sb.features & (1ULL << feat))) {
1001 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1003 bch2_write_super(c);
1005 mutex_unlock(&c->sb_lock);
1008 /* BCH_SB_FIELD_members: */
1010 static int bch2_sb_members_validate(struct bch_sb *sb,
1011 struct bch_sb_field *f,
1012 struct printbuf *err)
1014 struct bch_sb_field_members *mi = field_to_type(f, members);
1017 if ((void *) (mi->members + sb->nr_devices) >
1018 vstruct_end(&mi->field)) {
1019 prt_printf(err, "too many devices for section size");
1020 return -BCH_ERR_invalid_sb_members;
1023 for (i = 0; i < sb->nr_devices; i++) {
1024 struct bch_member *m = mi->members + i;
1026 if (!bch2_member_exists(m))
1029 if (le64_to_cpu(m->nbuckets) > LONG_MAX) {
1030 prt_printf(err, "device %u: too many buckets (got %llu, max %lu)",
1031 i, le64_to_cpu(m->nbuckets), LONG_MAX);
1032 return -BCH_ERR_invalid_sb_members;
1035 if (le64_to_cpu(m->nbuckets) -
1036 le16_to_cpu(m->first_bucket) < BCH_MIN_NR_NBUCKETS) {
1037 prt_printf(err, "device %u: not enough buckets (got %llu, max %u)",
1038 i, le64_to_cpu(m->nbuckets), BCH_MIN_NR_NBUCKETS);
1039 return -BCH_ERR_invalid_sb_members;
1042 if (le16_to_cpu(m->bucket_size) <
1043 le16_to_cpu(sb->block_size)) {
1044 prt_printf(err, "device %u: bucket size %u smaller than block size %u",
1045 i, le16_to_cpu(m->bucket_size), le16_to_cpu(sb->block_size));
1046 return -BCH_ERR_invalid_sb_members;
1049 if (le16_to_cpu(m->bucket_size) <
1050 BCH_SB_BTREE_NODE_SIZE(sb)) {
1051 prt_printf(err, "device %u: bucket size %u smaller than btree node size %llu",
1052 i, le16_to_cpu(m->bucket_size), BCH_SB_BTREE_NODE_SIZE(sb));
1053 return -BCH_ERR_invalid_sb_members;
1060 static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb,
1061 struct bch_sb_field *f)
1063 struct bch_sb_field_members *mi = field_to_type(f, members);
1064 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
1067 for (i = 0; i < sb->nr_devices; i++) {
1068 struct bch_member *m = mi->members + i;
1069 unsigned data_have = bch2_sb_dev_has_data(sb, i);
1070 u64 bucket_size = le16_to_cpu(m->bucket_size);
1071 u64 device_size = le64_to_cpu(m->nbuckets) * bucket_size;
1073 if (!bch2_member_exists(m))
1076 prt_printf(out, "Device:");
1078 prt_printf(out, "%u", i);
1081 printbuf_indent_add(out, 2);
1083 prt_printf(out, "UUID:");
1085 pr_uuid(out, m->uuid.b);
1088 prt_printf(out, "Size:");
1090 prt_units_u64(out, device_size << 9);
1093 prt_printf(out, "Bucket size:");
1095 prt_units_u64(out, bucket_size << 9);
1098 prt_printf(out, "First bucket:");
1100 prt_printf(out, "%u", le16_to_cpu(m->first_bucket));
1103 prt_printf(out, "Buckets:");
1105 prt_printf(out, "%llu", le64_to_cpu(m->nbuckets));
1108 prt_printf(out, "Last mount:");
1111 pr_time(out, le64_to_cpu(m->last_mount));
1113 prt_printf(out, "(never)");
1116 prt_printf(out, "State:");
1118 prt_printf(out, "%s",
1119 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
1120 ? bch2_member_states[BCH_MEMBER_STATE(m)]
1124 prt_printf(out, "Label:");
1126 if (BCH_MEMBER_GROUP(m)) {
1127 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
1129 if (idx < disk_groups_nr(gi))
1130 prt_printf(out, "%s (%u)",
1131 gi->entries[idx].label, idx);
1133 prt_printf(out, "(bad disk labels section)");
1135 prt_printf(out, "(none)");
1139 prt_printf(out, "Data allowed:");
1141 if (BCH_MEMBER_DATA_ALLOWED(m))
1142 prt_bitflags(out, bch2_data_types, BCH_MEMBER_DATA_ALLOWED(m));
1144 prt_printf(out, "(none)");
1147 prt_printf(out, "Has data:");
1150 prt_bitflags(out, bch2_data_types, data_have);
1152 prt_printf(out, "(none)");
1155 prt_printf(out, "Discard:");
1157 prt_printf(out, "%llu", BCH_MEMBER_DISCARD(m));
1160 prt_printf(out, "Freespace initialized:");
1162 prt_printf(out, "%llu", BCH_MEMBER_FREESPACE_INITIALIZED(m));
1165 printbuf_indent_sub(out, 2);
1169 static const struct bch_sb_field_ops bch_sb_field_ops_members = {
1170 .validate = bch2_sb_members_validate,
1171 .to_text = bch2_sb_members_to_text,
1174 /* BCH_SB_FIELD_crypt: */
1176 static int bch2_sb_crypt_validate(struct bch_sb *sb,
1177 struct bch_sb_field *f,
1178 struct printbuf *err)
1180 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1182 if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
1183 prt_printf(err, "wrong size (got %zu should be %zu)",
1184 vstruct_bytes(&crypt->field), sizeof(*crypt));
1185 return -BCH_ERR_invalid_sb_crypt;
1188 if (BCH_CRYPT_KDF_TYPE(crypt)) {
1189 prt_printf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
1190 return -BCH_ERR_invalid_sb_crypt;
1196 static void bch2_sb_crypt_to_text(struct printbuf *out, struct bch_sb *sb,
1197 struct bch_sb_field *f)
1199 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1201 prt_printf(out, "KFD: %llu", BCH_CRYPT_KDF_TYPE(crypt));
1203 prt_printf(out, "scrypt n: %llu", BCH_KDF_SCRYPT_N(crypt));
1205 prt_printf(out, "scrypt r: %llu", BCH_KDF_SCRYPT_R(crypt));
1207 prt_printf(out, "scrypt p: %llu", BCH_KDF_SCRYPT_P(crypt));
1211 static const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
1212 .validate = bch2_sb_crypt_validate,
1213 .to_text = bch2_sb_crypt_to_text,
1216 /* BCH_SB_FIELD_clean: */
1218 int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *clean, int write)
1220 struct jset_entry *entry;
1223 for (entry = clean->start;
1224 entry < (struct jset_entry *) vstruct_end(&clean->field);
1225 entry = vstruct_next(entry)) {
1226 ret = bch2_journal_entry_validate(c, NULL, entry,
1227 le16_to_cpu(c->disk_sb.sb->version),
1228 BCH_SB_BIG_ENDIAN(c->disk_sb.sb),
1237 /* Downgrade if superblock is at a higher version than currently supported: */
1238 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1240 lockdep_assert_held(&c->sb_lock);
1243 * Downgrade, if superblock is at a higher version than currently
1246 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1247 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1248 if (c->sb.version > bcachefs_metadata_version_current)
1249 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1250 if (c->sb.version_min > bcachefs_metadata_version_current)
1251 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1252 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1255 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1257 lockdep_assert_held(&c->sb_lock);
1259 c->disk_sb.sb->version = cpu_to_le16(new_version);
1260 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1263 int bch2_fs_mark_dirty(struct bch_fs *c)
1268 * Unconditionally write superblock, to verify it hasn't changed before
1272 mutex_lock(&c->sb_lock);
1273 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1275 bch2_sb_maybe_downgrade(c);
1276 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
1278 ret = bch2_write_super(c);
1279 mutex_unlock(&c->sb_lock);
1284 static struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
1286 struct jset_entry *entry = *end;
1287 unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
1289 memset(entry, 0, u64s * sizeof(u64));
1291 * The u64s field counts from the start of data, ignoring the shared
1294 entry->u64s = cpu_to_le16(u64s - 1);
1296 *end = vstruct_next(*end);
1300 void bch2_journal_super_entries_add_common(struct bch_fs *c,
1301 struct jset_entry **end,
1307 percpu_down_read(&c->mark_lock);
1310 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1311 bch2_fs_usage_acc_to_base(c, i);
1313 bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK);
1317 struct jset_entry_usage *u =
1318 container_of(jset_entry_init(end, sizeof(*u)),
1319 struct jset_entry_usage, entry);
1321 u->entry.type = BCH_JSET_ENTRY_usage;
1322 u->entry.btree_id = BCH_FS_USAGE_inodes;
1323 u->v = cpu_to_le64(c->usage_base->nr_inodes);
1327 struct jset_entry_usage *u =
1328 container_of(jset_entry_init(end, sizeof(*u)),
1329 struct jset_entry_usage, entry);
1331 u->entry.type = BCH_JSET_ENTRY_usage;
1332 u->entry.btree_id = BCH_FS_USAGE_key_version;
1333 u->v = cpu_to_le64(atomic64_read(&c->key_version));
1336 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1337 struct jset_entry_usage *u =
1338 container_of(jset_entry_init(end, sizeof(*u)),
1339 struct jset_entry_usage, entry);
1341 u->entry.type = BCH_JSET_ENTRY_usage;
1342 u->entry.btree_id = BCH_FS_USAGE_reserved;
1344 u->v = cpu_to_le64(c->usage_base->persistent_reserved[i]);
1347 for (i = 0; i < c->replicas.nr; i++) {
1348 struct bch_replicas_entry *e =
1349 cpu_replicas_entry(&c->replicas, i);
1350 struct jset_entry_data_usage *u =
1351 container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs),
1352 struct jset_entry_data_usage, entry);
1354 u->entry.type = BCH_JSET_ENTRY_data_usage;
1355 u->v = cpu_to_le64(c->usage_base->replicas[i]);
1356 unsafe_memcpy(&u->r, e, replicas_entry_bytes(e),
1357 "embedded variable length struct");
1360 for_each_member_device(ca, c, dev) {
1361 unsigned b = sizeof(struct jset_entry_dev_usage) +
1362 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR;
1363 struct jset_entry_dev_usage *u =
1364 container_of(jset_entry_init(end, b),
1365 struct jset_entry_dev_usage, entry);
1367 u->entry.type = BCH_JSET_ENTRY_dev_usage;
1368 u->dev = cpu_to_le32(dev);
1369 u->buckets_ec = cpu_to_le64(ca->usage_base->buckets_ec);
1371 for (i = 0; i < BCH_DATA_NR; i++) {
1372 u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets);
1373 u->d[i].sectors = cpu_to_le64(ca->usage_base->d[i].sectors);
1374 u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented);
1378 percpu_up_read(&c->mark_lock);
1380 for (i = 0; i < 2; i++) {
1381 struct jset_entry_clock *clock =
1382 container_of(jset_entry_init(end, sizeof(*clock)),
1383 struct jset_entry_clock, entry);
1385 clock->entry.type = BCH_JSET_ENTRY_clock;
1387 clock->time = cpu_to_le64(atomic64_read(&c->io_clock[i].now));
1391 void bch2_fs_mark_clean(struct bch_fs *c)
1393 struct bch_sb_field_clean *sb_clean;
1394 struct jset_entry *entry;
1398 mutex_lock(&c->sb_lock);
1399 if (BCH_SB_CLEAN(c->disk_sb.sb))
1402 SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
1404 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
1405 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata);
1406 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates));
1407 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled));
1409 u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved;
1411 sb_clean = bch2_sb_resize_clean(&c->disk_sb, u64s);
1413 bch_err(c, "error resizing superblock while setting filesystem clean");
1417 sb_clean->flags = 0;
1418 sb_clean->journal_seq = cpu_to_le64(atomic64_read(&c->journal.seq));
1420 /* Trying to catch outstanding bug: */
1421 BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX);
1423 entry = sb_clean->start;
1424 bch2_journal_super_entries_add_common(c, &entry, 0);
1425 entry = bch2_btree_roots_to_journal_entries(c, entry, entry);
1426 BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
1429 vstruct_end(&sb_clean->field) - (void *) entry);
1432 * this should be in the write path, and we should be validating every
1433 * superblock section:
1435 ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE);
1437 bch_err(c, "error writing marking filesystem clean: validate error");
1441 bch2_write_super(c);
1443 mutex_unlock(&c->sb_lock);
1446 static int bch2_sb_clean_validate(struct bch_sb *sb,
1447 struct bch_sb_field *f,
1448 struct printbuf *err)
1450 struct bch_sb_field_clean *clean = field_to_type(f, clean);
1452 if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
1453 prt_printf(err, "wrong size (got %zu should be %zu)",
1454 vstruct_bytes(&clean->field), sizeof(*clean));
1455 return -BCH_ERR_invalid_sb_clean;
1461 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
1462 struct bch_sb_field *f)
1464 struct bch_sb_field_clean *clean = field_to_type(f, clean);
1465 struct jset_entry *entry;
1467 prt_printf(out, "flags: %x", le32_to_cpu(clean->flags));
1469 prt_printf(out, "journal_seq: %llu", le64_to_cpu(clean->journal_seq));
1472 for (entry = clean->start;
1473 entry != vstruct_end(&clean->field);
1474 entry = vstruct_next(entry)) {
1475 if (entry->type == BCH_JSET_ENTRY_btree_keys &&
1479 bch2_journal_entry_to_text(out, NULL, entry);
1484 static const struct bch_sb_field_ops bch_sb_field_ops_clean = {
1485 .validate = bch2_sb_clean_validate,
1486 .to_text = bch2_sb_clean_to_text,
1489 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1491 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1496 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1498 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1500 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1501 ? bch2_sb_field_ops[type]
1502 : &bch2_sb_field_null_ops;
1505 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1506 struct printbuf *err)
1508 unsigned type = le32_to_cpu(f->type);
1509 struct printbuf field_err = PRINTBUF;
1510 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1513 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1515 prt_printf(err, "Invalid superblock section %s: %s",
1516 bch2_sb_fields[type], field_err.buf);
1518 bch2_sb_field_to_text(err, sb, f);
1521 printbuf_exit(&field_err);
1525 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1526 struct bch_sb_field *f)
1528 unsigned type = le32_to_cpu(f->type);
1529 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1531 if (!out->nr_tabstops)
1532 printbuf_tabstop_push(out, 32);
1534 if (type < BCH_SB_FIELD_NR)
1535 prt_printf(out, "%s", bch2_sb_fields[type]);
1537 prt_printf(out, "(unknown field %u)", type);
1539 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1543 printbuf_indent_add(out, 2);
1544 ops->to_text(out, sb, f);
1545 printbuf_indent_sub(out, 2);
1549 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1553 prt_printf(out, "Type: %u", l->layout_type);
1556 prt_str(out, "Superblock max size: ");
1557 prt_units_u64(out, 512 << l->sb_max_size_bits);
1560 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1563 prt_str(out, "Offsets: ");
1564 for (i = 0; i < l->nr_superblocks; i++) {
1567 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1572 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1573 bool print_layout, unsigned fields)
1575 struct bch_sb_field_members *mi;
1576 struct bch_sb_field *f;
1577 u64 fields_have = 0;
1578 unsigned nr_devices = 0;
1580 if (!out->nr_tabstops)
1581 printbuf_tabstop_push(out, 44);
1583 mi = bch2_sb_get_members(sb);
1585 struct bch_member *m;
1587 for (m = mi->members;
1588 m < mi->members + sb->nr_devices;
1590 nr_devices += bch2_member_exists(m);
1593 prt_printf(out, "External UUID:");
1595 pr_uuid(out, sb->user_uuid.b);
1598 prt_printf(out, "Internal UUID:");
1600 pr_uuid(out, sb->uuid.b);
1603 prt_str(out, "Device index:");
1605 prt_printf(out, "%u", sb->dev_idx);
1608 prt_str(out, "Label:");
1610 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1613 prt_str(out, "Version:");
1615 bch2_version_to_text(out, le16_to_cpu(sb->version));
1618 prt_str(out, "Version upgrade complete:");
1620 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1623 prt_printf(out, "Oldest version on disk:");
1625 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1628 prt_printf(out, "Created:");
1630 if (sb->time_base_lo)
1631 pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1633 prt_printf(out, "(not set)");
1636 prt_printf(out, "Sequence number:");
1638 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1641 prt_printf(out, "Superblock size:");
1643 prt_printf(out, "%zu", vstruct_bytes(sb));
1646 prt_printf(out, "Clean:");
1648 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1651 prt_printf(out, "Devices:");
1653 prt_printf(out, "%u", nr_devices);
1656 prt_printf(out, "Sections:");
1657 vstruct_for_each(sb, f)
1658 fields_have |= 1 << le32_to_cpu(f->type);
1660 prt_bitflags(out, bch2_sb_fields, fields_have);
1663 prt_printf(out, "Features:");
1665 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1668 prt_printf(out, "Compat features:");
1670 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1674 prt_printf(out, "Options:");
1676 printbuf_indent_add(out, 2);
1680 for (id = 0; id < bch2_opts_nr; id++) {
1681 const struct bch_option *opt = bch2_opt_table + id;
1683 if (opt->get_sb != BCH2_NO_SB_OPT) {
1684 u64 v = bch2_opt_from_sb(sb, id);
1686 prt_printf(out, "%s:", opt->attr.name);
1688 bch2_opt_to_text(out, NULL, sb, opt, v,
1689 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1695 printbuf_indent_sub(out, 2);
1699 prt_printf(out, "layout:");
1701 printbuf_indent_add(out, 2);
1702 bch2_sb_layout_to_text(out, &sb->layout);
1703 printbuf_indent_sub(out, 2);
1706 vstruct_for_each(sb, f)
1707 if (fields & (1 << le32_to_cpu(f->type))) {
1709 bch2_sb_field_to_text(out, sb, f);