Linux 6.13-rc4
[linux.git] / fs / bcachefs / super-io.c
blob7c71594f6a8bd369100a32539ec4f263d2c4ed50
1 // SPDX-License-Identifier: GPL-2.0
3 #include "bcachefs.h"
4 #include "checksum.h"
5 #include "disk_groups.h"
6 #include "ec.h"
7 #include "error.h"
8 #include "journal.h"
9 #include "journal_sb.h"
10 #include "journal_seq_blacklist.h"
11 #include "recovery_passes.h"
12 #include "replicas.h"
13 #include "quota.h"
14 #include "sb-clean.h"
15 #include "sb-counters.h"
16 #include "sb-downgrade.h"
17 #include "sb-errors.h"
18 #include "sb-members.h"
19 #include "super-io.h"
20 #include "super.h"
21 #include "trace.h"
22 #include "vstructs.h"
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 {
31 u16 version;
32 const char *name;
35 static const struct bch2_metadata_version bch2_metadata_versions[] = {
36 #define x(n, v) { \
37 .version = v, \
38 .name = #n, \
40 BCH_METADATA_VERSIONS()
41 #undef x
44 void bch2_version_to_text(struct printbuf *out, unsigned v)
46 const char *str = "(unknown version)";
48 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
49 if (bch2_metadata_versions[i].version == v) {
50 str = bch2_metadata_versions[i].name;
51 break;
54 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
57 unsigned bch2_latest_compatible_version(unsigned v)
59 if (!BCH_VERSION_MAJOR(v))
60 return v;
62 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
63 if (bch2_metadata_versions[i].version > v &&
64 BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
65 BCH_VERSION_MAJOR(v))
66 v = bch2_metadata_versions[i].version;
68 return v;
71 const char * const bch2_sb_fields[] = {
72 #define x(name, nr) #name,
73 BCH_SB_FIELDS()
74 #undef x
75 NULL
78 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
79 enum bch_validate_flags, struct printbuf *);
81 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *sb,
82 enum bch_sb_field_type type)
84 /* XXX: need locking around superblock to access optional fields */
86 vstruct_for_each(sb, f)
87 if (le32_to_cpu(f->type) == type)
88 return f;
89 return NULL;
92 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
93 struct bch_sb_field *f,
94 unsigned u64s)
96 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
97 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
99 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
101 if (!f && !u64s) {
102 /* nothing to do: */
103 } else if (!f) {
104 f = vstruct_last(sb->sb);
105 memset(f, 0, sizeof(u64) * u64s);
106 f->u64s = cpu_to_le32(u64s);
107 f->type = 0;
108 } else {
109 void *src, *dst;
111 src = vstruct_end(f);
113 if (u64s) {
114 f->u64s = cpu_to_le32(u64s);
115 dst = vstruct_end(f);
116 } else {
117 dst = f;
120 memmove(dst, src, vstruct_end(sb->sb) - src);
122 if (dst > src)
123 memset(src, 0, dst - src);
126 sb->sb->u64s = cpu_to_le32(sb_u64s);
128 return u64s ? f : NULL;
131 void bch2_sb_field_delete(struct bch_sb_handle *sb,
132 enum bch_sb_field_type type)
134 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
136 if (f)
137 __bch2_sb_field_resize(sb, f, 0);
140 /* Superblock realloc/free: */
142 void bch2_free_super(struct bch_sb_handle *sb)
144 kfree(sb->bio);
145 if (!IS_ERR_OR_NULL(sb->s_bdev_file))
146 bdev_fput(sb->s_bdev_file);
147 kfree(sb->holder);
148 kfree(sb->sb_name);
150 kfree(sb->sb);
151 memset(sb, 0, sizeof(*sb));
154 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
156 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
157 size_t new_buffer_size;
158 struct bch_sb *new_sb;
159 struct bio *bio;
161 if (sb->bdev)
162 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
164 new_buffer_size = roundup_pow_of_two(new_bytes);
166 if (sb->sb && sb->buffer_size >= new_buffer_size)
167 return 0;
169 if (sb->sb && sb->have_layout) {
170 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
172 if (new_bytes > max_bytes) {
173 struct printbuf buf = PRINTBUF;
175 prt_bdevname(&buf, sb->bdev);
176 prt_printf(&buf, ": superblock too big: want %zu but have %llu", new_bytes, max_bytes);
177 pr_err("%s", buf.buf);
178 printbuf_exit(&buf);
179 return -BCH_ERR_ENOSPC_sb;
183 if (sb->buffer_size >= new_buffer_size && sb->sb)
184 return 0;
186 if (dynamic_fault("bcachefs:add:super_realloc"))
187 return -BCH_ERR_ENOMEM_sb_realloc_injected;
189 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
190 if (!new_sb)
191 return -BCH_ERR_ENOMEM_sb_buf_realloc;
193 sb->sb = new_sb;
195 if (sb->have_bio) {
196 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size);
198 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
199 if (!bio)
200 return -BCH_ERR_ENOMEM_sb_bio_realloc;
202 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
204 kfree(sb->bio);
205 sb->bio = bio;
208 sb->buffer_size = new_buffer_size;
210 return 0;
213 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *sb,
214 enum bch_sb_field_type type,
215 unsigned u64s)
217 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
218 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
219 ssize_t d = -old_u64s + u64s;
221 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
222 return NULL;
224 if (sb->fs_sb) {
225 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
227 lockdep_assert_held(&c->sb_lock);
229 /* XXX: we're not checking that offline device have enough space */
231 for_each_online_member(c, ca) {
232 struct bch_sb_handle *dev_sb = &ca->disk_sb;
234 if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) {
235 percpu_ref_put(&ca->io_ref);
236 return NULL;
241 f = bch2_sb_field_get_id(sb->sb, type);
242 f = __bch2_sb_field_resize(sb, f, u64s);
243 if (f)
244 f->type = cpu_to_le32(type);
245 return f;
248 struct bch_sb_field *bch2_sb_field_get_minsize_id(struct bch_sb_handle *sb,
249 enum bch_sb_field_type type,
250 unsigned u64s)
252 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
254 if (!f || le32_to_cpu(f->u64s) < u64s)
255 f = bch2_sb_field_resize_id(sb, type, u64s);
256 return f;
259 /* Superblock validate: */
261 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
263 u64 offset, prev_offset, max_sectors;
264 unsigned i;
266 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
268 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
269 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
270 prt_printf(out, "Not a bcachefs superblock layout");
271 return -BCH_ERR_invalid_sb_layout;
274 if (layout->layout_type != 0) {
275 prt_printf(out, "Invalid superblock layout type %u",
276 layout->layout_type);
277 return -BCH_ERR_invalid_sb_layout_type;
280 if (!layout->nr_superblocks) {
281 prt_printf(out, "Invalid superblock layout: no superblocks");
282 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
285 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
286 prt_printf(out, "Invalid superblock layout: too many superblocks");
287 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
290 if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) {
291 prt_printf(out, "Invalid superblock layout: max_size_bits too high");
292 return -BCH_ERR_invalid_sb_layout_sb_max_size_bits;
295 max_sectors = 1 << layout->sb_max_size_bits;
297 prev_offset = le64_to_cpu(layout->sb_offset[0]);
299 for (i = 1; i < layout->nr_superblocks; i++) {
300 offset = le64_to_cpu(layout->sb_offset[i]);
302 if (offset < prev_offset + max_sectors) {
303 prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
304 " (sb %u ends at %llu next starts at %llu",
305 i - 1, prev_offset + max_sectors, offset);
306 return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
308 prev_offset = offset;
311 return 0;
314 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
316 u16 version = le16_to_cpu(sb->version);
317 u16 version_min = le16_to_cpu(sb->version_min);
319 if (!bch2_version_compatible(version)) {
320 prt_str(out, "Unsupported superblock version ");
321 bch2_version_to_text(out, version);
322 prt_str(out, " (min ");
323 bch2_version_to_text(out, bcachefs_metadata_version_min);
324 prt_str(out, ", max ");
325 bch2_version_to_text(out, bcachefs_metadata_version_current);
326 prt_str(out, ")");
327 return -BCH_ERR_invalid_sb_version;
330 if (!bch2_version_compatible(version_min)) {
331 prt_str(out, "Unsupported superblock version_min ");
332 bch2_version_to_text(out, version_min);
333 prt_str(out, " (min ");
334 bch2_version_to_text(out, bcachefs_metadata_version_min);
335 prt_str(out, ", max ");
336 bch2_version_to_text(out, bcachefs_metadata_version_current);
337 prt_str(out, ")");
338 return -BCH_ERR_invalid_sb_version;
341 if (version_min > version) {
342 prt_str(out, "Bad minimum version ");
343 bch2_version_to_text(out, version_min);
344 prt_str(out, ", greater than version field ");
345 bch2_version_to_text(out, version);
346 return -BCH_ERR_invalid_sb_version;
349 return 0;
352 static int bch2_sb_validate(struct bch_sb_handle *disk_sb,
353 enum bch_validate_flags flags, struct printbuf *out)
355 struct bch_sb *sb = disk_sb->sb;
356 struct bch_sb_field_members_v1 *mi;
357 enum bch_opt_id opt_id;
358 u16 block_size;
359 int ret;
361 ret = bch2_sb_compatible(sb, out);
362 if (ret)
363 return ret;
365 if (sb->features[1] ||
366 (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
367 prt_printf(out, "Filesystem has incompatible features");
368 return -BCH_ERR_invalid_sb_features;
371 block_size = le16_to_cpu(sb->block_size);
373 if (block_size > PAGE_SECTORS) {
374 prt_printf(out, "Block size too big (got %u, max %u)",
375 block_size, PAGE_SECTORS);
376 return -BCH_ERR_invalid_sb_block_size;
379 if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
380 prt_printf(out, "Bad user UUID (got zeroes)");
381 return -BCH_ERR_invalid_sb_uuid;
384 if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
385 prt_printf(out, "Bad internal UUID (got zeroes)");
386 return -BCH_ERR_invalid_sb_uuid;
389 if (!sb->nr_devices ||
390 sb->nr_devices > BCH_SB_MEMBERS_MAX) {
391 prt_printf(out, "Bad number of member devices %u (max %u)",
392 sb->nr_devices, BCH_SB_MEMBERS_MAX);
393 return -BCH_ERR_invalid_sb_too_many_members;
396 if (sb->dev_idx >= sb->nr_devices) {
397 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
398 sb->dev_idx, sb->nr_devices);
399 return -BCH_ERR_invalid_sb_dev_idx;
402 if (!sb->time_precision ||
403 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
404 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
405 le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
406 return -BCH_ERR_invalid_sb_time_precision;
409 if (!flags) {
411 * Been seeing a bug where these are getting inexplicably
412 * zeroed, so we're now validating them, but we have to be
413 * careful not to preven people's filesystems from mounting:
415 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
416 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
417 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
418 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
420 if (!BCH_SB_VERSION_UPGRADE_COMPLETE(sb))
421 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version));
423 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_disk_accounting_v2 &&
424 !BCH_SB_ALLOCATOR_STUCK_TIMEOUT(sb))
425 SET_BCH_SB_ALLOCATOR_STUCK_TIMEOUT(sb, 30);
427 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_disk_accounting_v2)
428 SET_BCH_SB_PROMOTE_WHOLE_EXTENTS(sb, true);
431 for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
432 const struct bch_option *opt = bch2_opt_table + opt_id;
434 if (opt->get_sb != BCH2_NO_SB_OPT) {
435 u64 v = bch2_opt_from_sb(sb, opt_id);
437 prt_printf(out, "Invalid option ");
438 ret = bch2_opt_validate(opt, v, out);
439 if (ret)
440 return ret;
442 printbuf_reset(out);
446 /* validate layout */
447 ret = validate_sb_layout(&sb->layout, out);
448 if (ret)
449 return ret;
451 vstruct_for_each(sb, f) {
452 if (!f->u64s) {
453 prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
454 le32_to_cpu(f->type));
455 return -BCH_ERR_invalid_sb_field_size;
458 if (vstruct_next(f) > vstruct_last(sb)) {
459 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
460 le32_to_cpu(f->type));
461 return -BCH_ERR_invalid_sb_field_size;
465 /* members must be validated first: */
466 mi = bch2_sb_field_get(sb, members_v1);
467 if (!mi) {
468 prt_printf(out, "Invalid superblock: member info area missing");
469 return -BCH_ERR_invalid_sb_members_missing;
472 ret = bch2_sb_field_validate(sb, &mi->field, flags, out);
473 if (ret)
474 return ret;
476 vstruct_for_each(sb, f) {
477 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members_v1)
478 continue;
480 ret = bch2_sb_field_validate(sb, f, flags, out);
481 if (ret)
482 return ret;
485 if ((flags & BCH_VALIDATE_write) &&
486 bch2_sb_member_get(sb, sb->dev_idx).seq != sb->seq) {
487 prt_printf(out, "Invalid superblock: member seq %llu != sb seq %llu",
488 le64_to_cpu(bch2_sb_member_get(sb, sb->dev_idx).seq),
489 le64_to_cpu(sb->seq));
490 return -BCH_ERR_invalid_sb_members_missing;
493 return 0;
496 /* device open: */
498 static unsigned long le_ulong_to_cpu(unsigned long v)
500 return sizeof(unsigned long) == 8
501 ? le64_to_cpu(v)
502 : le32_to_cpu(v);
505 static void le_bitvector_to_cpu(unsigned long *dst, unsigned long *src, unsigned nr)
507 BUG_ON(nr & (BITS_PER_TYPE(long) - 1));
509 for (unsigned i = 0; i < BITS_TO_LONGS(nr); i++)
510 dst[i] = le_ulong_to_cpu(src[i]);
513 static void bch2_sb_update(struct bch_fs *c)
515 struct bch_sb *src = c->disk_sb.sb;
517 lockdep_assert_held(&c->sb_lock);
519 c->sb.uuid = src->uuid;
520 c->sb.user_uuid = src->user_uuid;
521 c->sb.version = le16_to_cpu(src->version);
522 c->sb.version_min = le16_to_cpu(src->version_min);
523 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src);
524 c->sb.nr_devices = src->nr_devices;
525 c->sb.clean = BCH_SB_CLEAN(src);
526 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
528 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
529 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
531 /* XXX this is wrong, we need a 96 or 128 bit integer type */
532 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
533 c->sb.nsec_per_time_unit);
534 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
536 c->sb.features = le64_to_cpu(src->features[0]);
537 c->sb.compat = le64_to_cpu(src->compat[0]);
539 memset(c->sb.errors_silent, 0, sizeof(c->sb.errors_silent));
541 struct bch_sb_field_ext *ext = bch2_sb_field_get(src, ext);
542 if (ext) {
543 le_bitvector_to_cpu(c->sb.errors_silent, (void *) ext->errors_silent,
544 sizeof(c->sb.errors_silent) * 8);
545 c->sb.btrees_lost_data = le64_to_cpu(ext->btrees_lost_data);
548 for_each_member_device(c, ca) {
549 struct bch_member m = bch2_sb_member_get(src, ca->dev_idx);
550 ca->mi = bch2_mi_to_cpu(&m);
554 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
556 struct bch_sb_field *src_f, *dst_f;
557 struct bch_sb *dst = dst_handle->sb;
558 unsigned i;
560 dst->version = src->version;
561 dst->version_min = src->version_min;
562 dst->seq = src->seq;
563 dst->uuid = src->uuid;
564 dst->user_uuid = src->user_uuid;
565 memcpy(dst->label, src->label, sizeof(dst->label));
567 dst->block_size = src->block_size;
568 dst->nr_devices = src->nr_devices;
570 dst->time_base_lo = src->time_base_lo;
571 dst->time_base_hi = src->time_base_hi;
572 dst->time_precision = src->time_precision;
573 dst->write_time = src->write_time;
575 memcpy(dst->flags, src->flags, sizeof(dst->flags));
576 memcpy(dst->features, src->features, sizeof(dst->features));
577 memcpy(dst->compat, src->compat, sizeof(dst->compat));
579 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
580 int d;
582 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
583 continue;
585 src_f = bch2_sb_field_get_id(src, i);
586 dst_f = bch2_sb_field_get_id(dst, i);
588 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
589 (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
590 if (d > 0) {
591 int ret = bch2_sb_realloc(dst_handle,
592 le32_to_cpu(dst_handle->sb->u64s) + d);
594 if (ret)
595 return ret;
597 dst = dst_handle->sb;
598 dst_f = bch2_sb_field_get_id(dst, i);
601 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
602 src_f ? le32_to_cpu(src_f->u64s) : 0);
604 if (src_f)
605 memcpy(dst_f, src_f, vstruct_bytes(src_f));
608 return 0;
611 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
613 int ret;
615 lockdep_assert_held(&c->sb_lock);
617 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
618 __copy_super(&c->disk_sb, src) ?:
619 bch2_sb_replicas_to_cpu_replicas(c) ?:
620 bch2_sb_disk_groups_to_cpu(c);
621 if (ret)
622 return ret;
624 bch2_sb_update(c);
625 return 0;
628 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
630 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
633 /* read superblock: */
635 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
637 size_t bytes;
638 int ret;
639 reread:
640 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
641 sb->bio->bi_iter.bi_sector = offset;
642 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
644 ret = submit_bio_wait(sb->bio);
645 if (ret) {
646 prt_printf(err, "IO error: %i", ret);
647 return ret;
650 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
651 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
652 prt_str(err, "Not a bcachefs superblock (got magic ");
653 pr_uuid(err, sb->sb->magic.b);
654 prt_str(err, ")");
655 return -BCH_ERR_invalid_sb_magic;
658 ret = bch2_sb_compatible(sb->sb, err);
659 if (ret)
660 return ret;
662 bytes = vstruct_bytes(sb->sb);
664 u64 sb_size = 512ULL << min(BCH_SB_LAYOUT_SIZE_BITS_MAX, sb->sb->layout.sb_max_size_bits);
665 if (bytes > sb_size) {
666 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %llu)",
667 bytes, sb_size);
668 return -BCH_ERR_invalid_sb_too_big;
671 if (bytes > sb->buffer_size) {
672 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
673 if (ret)
674 return ret;
675 goto reread;
678 enum bch_csum_type csum_type = BCH_SB_CSUM_TYPE(sb->sb);
679 if (csum_type >= BCH_CSUM_NR) {
680 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
681 return -BCH_ERR_invalid_sb_csum_type;
684 /* XXX: verify MACs */
685 struct bch_csum csum = csum_vstruct(NULL, csum_type, null_nonce(), sb->sb);
686 if (bch2_crc_cmp(csum, sb->sb->csum)) {
687 bch2_csum_err_msg(err, csum_type, sb->sb->csum, csum);
688 return -BCH_ERR_invalid_sb_csum;
691 sb->seq = le64_to_cpu(sb->sb->seq);
693 return 0;
696 static int __bch2_read_super(const char *path, struct bch_opts *opts,
697 struct bch_sb_handle *sb, bool ignore_notbchfs_msg)
699 u64 offset = opt_get(*opts, sb);
700 struct bch_sb_layout layout;
701 struct printbuf err = PRINTBUF;
702 struct printbuf err2 = PRINTBUF;
703 __le64 *i;
704 int ret;
705 #ifndef __KERNEL__
706 retry:
707 #endif
708 memset(sb, 0, sizeof(*sb));
709 sb->mode = BLK_OPEN_READ;
710 sb->have_bio = true;
711 sb->holder = kmalloc(1, GFP_KERNEL);
712 if (!sb->holder)
713 return -ENOMEM;
715 sb->sb_name = kstrdup(path, GFP_KERNEL);
716 if (!sb->sb_name) {
717 ret = -ENOMEM;
718 prt_printf(&err, "error allocating memory for sb_name");
719 goto err;
722 #ifndef __KERNEL__
723 if (opt_get(*opts, direct_io) == false)
724 sb->mode |= BLK_OPEN_BUFFERED;
725 #endif
727 if (!opt_get(*opts, noexcl))
728 sb->mode |= BLK_OPEN_EXCL;
730 if (!opt_get(*opts, nochanges))
731 sb->mode |= BLK_OPEN_WRITE;
733 sb->s_bdev_file = bdev_file_open_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
734 if (IS_ERR(sb->s_bdev_file) &&
735 PTR_ERR(sb->s_bdev_file) == -EACCES &&
736 opt_get(*opts, read_only)) {
737 sb->mode &= ~BLK_OPEN_WRITE;
739 sb->s_bdev_file = bdev_file_open_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
740 if (!IS_ERR(sb->s_bdev_file))
741 opt_set(*opts, nochanges, true);
744 if (IS_ERR(sb->s_bdev_file)) {
745 ret = PTR_ERR(sb->s_bdev_file);
746 prt_printf(&err, "error opening %s: %s", path, bch2_err_str(ret));
747 goto err;
749 sb->bdev = file_bdev(sb->s_bdev_file);
751 ret = bch2_sb_realloc(sb, 0);
752 if (ret) {
753 prt_printf(&err, "error allocating memory for superblock");
754 goto err;
757 if (bch2_fs_init_fault("read_super")) {
758 prt_printf(&err, "dynamic fault");
759 ret = -EFAULT;
760 goto err;
763 ret = read_one_super(sb, offset, &err);
764 if (!ret)
765 goto got_super;
767 if (opt_defined(*opts, sb))
768 goto err;
770 prt_printf(&err2, "bcachefs (%s): error reading default superblock: %s\n",
771 path, err.buf);
772 if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg)
773 bch2_print_opts(opts, KERN_INFO "%s", err2.buf);
774 else
775 bch2_print_opts(opts, KERN_ERR "%s", err2.buf);
777 printbuf_exit(&err2);
778 printbuf_reset(&err);
781 * Error reading primary superblock - read location of backup
782 * superblocks:
784 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
785 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
787 * use sb buffer to read layout, since sb buffer is page aligned but
788 * layout won't be:
790 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
792 ret = submit_bio_wait(sb->bio);
793 if (ret) {
794 prt_printf(&err, "IO error: %i", ret);
795 goto err;
798 memcpy(&layout, sb->sb, sizeof(layout));
799 ret = validate_sb_layout(&layout, &err);
800 if (ret)
801 goto err;
803 for (i = layout.sb_offset;
804 i < layout.sb_offset + layout.nr_superblocks; i++) {
805 offset = le64_to_cpu(*i);
807 if (offset == opt_get(*opts, sb)) {
808 ret = -BCH_ERR_invalid;
809 continue;
812 ret = read_one_super(sb, offset, &err);
813 if (!ret)
814 goto got_super;
817 goto err;
819 got_super:
820 if (le16_to_cpu(sb->sb->block_size) << 9 <
821 bdev_logical_block_size(sb->bdev) &&
822 opt_get(*opts, direct_io)) {
823 #ifndef __KERNEL__
824 opt_set(*opts, direct_io, false);
825 bch2_free_super(sb);
826 goto retry;
827 #endif
828 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
829 le16_to_cpu(sb->sb->block_size) << 9,
830 bdev_logical_block_size(sb->bdev));
831 ret = -BCH_ERR_block_size_too_small;
832 goto err;
835 sb->have_layout = true;
837 ret = bch2_sb_validate(sb, 0, &err);
838 if (ret) {
839 bch2_print_opts(opts, KERN_ERR "bcachefs (%s): error validating superblock: %s\n",
840 path, err.buf);
841 goto err_no_print;
843 out:
844 printbuf_exit(&err);
845 return ret;
846 err:
847 bch2_print_opts(opts, KERN_ERR "bcachefs (%s): error reading superblock: %s\n",
848 path, err.buf);
849 err_no_print:
850 bch2_free_super(sb);
851 goto out;
854 int bch2_read_super(const char *path, struct bch_opts *opts,
855 struct bch_sb_handle *sb)
857 return __bch2_read_super(path, opts, sb, false);
860 /* provide a silenced version for mount.bcachefs */
862 int bch2_read_super_silent(const char *path, struct bch_opts *opts,
863 struct bch_sb_handle *sb)
865 return __bch2_read_super(path, opts, sb, true);
868 /* write superblock: */
870 static void write_super_endio(struct bio *bio)
872 struct bch_dev *ca = bio->bi_private;
874 /* XXX: return errors directly */
876 if (bch2_dev_io_err_on(bio->bi_status, ca,
877 bio_data_dir(bio)
878 ? BCH_MEMBER_ERROR_write
879 : BCH_MEMBER_ERROR_read,
880 "superblock %s error: %s",
881 bio_data_dir(bio) ? "write" : "read",
882 bch2_blk_status_to_str(bio->bi_status)))
883 ca->sb_write_error = 1;
885 closure_put(&ca->fs->sb_write);
886 percpu_ref_put(&ca->io_ref);
889 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
891 struct bch_sb *sb = ca->disk_sb.sb;
892 struct bio *bio = ca->disk_sb.bio;
894 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
895 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
896 bio->bi_end_io = write_super_endio;
897 bio->bi_private = ca;
898 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
900 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
901 bio_sectors(bio));
903 percpu_ref_get(&ca->io_ref);
904 closure_bio_submit(bio, &c->sb_write);
907 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
909 struct bch_sb *sb = ca->disk_sb.sb;
910 struct bio *bio = ca->disk_sb.bio;
912 sb->offset = sb->layout.sb_offset[idx];
914 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
915 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
916 null_nonce(), sb);
918 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
919 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
920 bio->bi_end_io = write_super_endio;
921 bio->bi_private = ca;
922 bch2_bio_map(bio, sb,
923 roundup((size_t) vstruct_bytes(sb),
924 bdev_logical_block_size(ca->disk_sb.bdev)));
926 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
927 bio_sectors(bio));
929 percpu_ref_get(&ca->io_ref);
930 closure_bio_submit(bio, &c->sb_write);
933 int bch2_write_super(struct bch_fs *c)
935 struct closure *cl = &c->sb_write;
936 struct printbuf err = PRINTBUF;
937 unsigned sb = 0, nr_wrote;
938 struct bch_devs_mask sb_written;
939 bool wrote, can_mount_without_written, can_mount_with_written;
940 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
941 DARRAY(struct bch_dev *) online_devices = {};
942 int ret = 0;
944 trace_and_count(c, write_super, c, _RET_IP_);
946 if (c->opts.very_degraded)
947 degraded_flags |= BCH_FORCE_IF_LOST;
949 lockdep_assert_held(&c->sb_lock);
951 closure_init_stack(cl);
952 memset(&sb_written, 0, sizeof(sb_written));
954 for_each_online_member(c, ca) {
955 ret = darray_push(&online_devices, ca);
956 if (bch2_fs_fatal_err_on(ret, c, "%s: error allocating online devices", __func__)) {
957 percpu_ref_put(&ca->io_ref);
958 goto out;
960 percpu_ref_get(&ca->io_ref);
963 /* Make sure we're using the new magic numbers: */
964 c->disk_sb.sb->magic = BCHFS_MAGIC;
965 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
967 le64_add_cpu(&c->disk_sb.sb->seq, 1);
969 struct bch_sb_field_members_v2 *mi = bch2_sb_field_get(c->disk_sb.sb, members_v2);
970 darray_for_each(online_devices, ca)
971 __bch2_members_v2_get_mut(mi, (*ca)->dev_idx)->seq = c->disk_sb.sb->seq;
972 c->disk_sb.sb->write_time = cpu_to_le64(ktime_get_real_seconds());
974 if (test_bit(BCH_FS_error, &c->flags))
975 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
976 if (test_bit(BCH_FS_topology_error, &c->flags))
977 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
979 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
981 bch2_sb_counters_from_cpu(c);
982 bch2_sb_members_from_cpu(c);
983 bch2_sb_members_cpy_v2_v1(&c->disk_sb);
984 bch2_sb_errors_from_cpu(c);
985 bch2_sb_downgrade_update(c);
987 darray_for_each(online_devices, ca)
988 bch2_sb_from_fs(c, (*ca));
990 darray_for_each(online_devices, ca) {
991 printbuf_reset(&err);
993 ret = bch2_sb_validate(&(*ca)->disk_sb, BCH_VALIDATE_write, &err);
994 if (ret) {
995 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
996 goto out;
1000 if (c->opts.nochanges)
1001 goto out;
1004 * Defer writing the superblock until filesystem initialization is
1005 * complete - don't write out a partly initialized superblock:
1007 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
1008 goto out;
1010 if (le16_to_cpu(c->disk_sb.sb->version) > bcachefs_metadata_version_current) {
1011 struct printbuf buf = PRINTBUF;
1012 prt_printf(&buf, "attempting to write superblock that wasn't version downgraded (");
1013 bch2_version_to_text(&buf, le16_to_cpu(c->disk_sb.sb->version));
1014 prt_str(&buf, " > ");
1015 bch2_version_to_text(&buf, bcachefs_metadata_version_current);
1016 prt_str(&buf, ")");
1017 bch2_fs_fatal_error(c, ": %s", buf.buf);
1018 printbuf_exit(&buf);
1019 return -BCH_ERR_sb_not_downgraded;
1022 darray_for_each(online_devices, ca) {
1023 __set_bit((*ca)->dev_idx, sb_written.d);
1024 (*ca)->sb_write_error = 0;
1027 darray_for_each(online_devices, ca)
1028 read_back_super(c, *ca);
1029 closure_sync(cl);
1031 darray_for_each(online_devices, cap) {
1032 struct bch_dev *ca = *cap;
1034 if (ca->sb_write_error)
1035 continue;
1037 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
1038 struct printbuf buf = PRINTBUF;
1039 prt_char(&buf, ' ');
1040 prt_bdevname(&buf, ca->disk_sb.bdev);
1041 prt_printf(&buf,
1042 ": Superblock write was silently dropped! (seq %llu expected %llu)",
1043 le64_to_cpu(ca->sb_read_scratch->seq),
1044 ca->disk_sb.seq);
1045 bch2_fs_fatal_error(c, "%s", buf.buf);
1046 printbuf_exit(&buf);
1047 ret = -BCH_ERR_erofs_sb_err;
1050 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
1051 struct printbuf buf = PRINTBUF;
1052 prt_char(&buf, ' ');
1053 prt_bdevname(&buf, ca->disk_sb.bdev);
1054 prt_printf(&buf,
1055 ": Superblock modified by another process (seq %llu expected %llu)",
1056 le64_to_cpu(ca->sb_read_scratch->seq),
1057 ca->disk_sb.seq);
1058 bch2_fs_fatal_error(c, "%s", buf.buf);
1059 printbuf_exit(&buf);
1060 ret = -BCH_ERR_erofs_sb_err;
1064 if (ret)
1065 goto out;
1067 do {
1068 wrote = false;
1069 darray_for_each(online_devices, cap) {
1070 struct bch_dev *ca = *cap;
1071 if (!ca->sb_write_error &&
1072 sb < ca->disk_sb.sb->layout.nr_superblocks) {
1073 write_one_super(c, ca, sb);
1074 wrote = true;
1077 closure_sync(cl);
1078 sb++;
1079 } while (wrote);
1081 darray_for_each(online_devices, cap) {
1082 struct bch_dev *ca = *cap;
1083 if (ca->sb_write_error)
1084 __clear_bit(ca->dev_idx, sb_written.d);
1085 else
1086 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
1089 nr_wrote = dev_mask_nr(&sb_written);
1091 can_mount_with_written =
1092 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1094 for (unsigned i = 0; i < ARRAY_SIZE(sb_written.d); i++)
1095 sb_written.d[i] = ~sb_written.d[i];
1097 can_mount_without_written =
1098 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1101 * If we would be able to mount _without_ the devices we successfully
1102 * wrote superblocks to, we weren't able to write to enough devices:
1104 * Exception: if we can mount without the successes because we haven't
1105 * written anything (new filesystem), we continue if we'd be able to
1106 * mount with the devices we did successfully write to:
1108 if (bch2_fs_fatal_err_on(!nr_wrote ||
1109 !can_mount_with_written ||
1110 (can_mount_without_written &&
1111 !can_mount_with_written), c,
1112 ": Unable to write superblock to sufficient devices (from %ps)",
1113 (void *) _RET_IP_))
1114 ret = -1;
1115 out:
1116 /* Make new options visible after they're persistent: */
1117 bch2_sb_update(c);
1118 darray_for_each(online_devices, ca)
1119 percpu_ref_put(&(*ca)->io_ref);
1120 darray_exit(&online_devices);
1121 printbuf_exit(&err);
1122 return ret;
1125 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1127 mutex_lock(&c->sb_lock);
1128 if (!(c->sb.features & (1ULL << feat))) {
1129 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1131 bch2_write_super(c);
1133 mutex_unlock(&c->sb_lock);
1136 /* Downgrade if superblock is at a higher version than currently supported: */
1137 bool bch2_check_version_downgrade(struct bch_fs *c)
1139 bool ret = bcachefs_metadata_version_current < c->sb.version;
1141 lockdep_assert_held(&c->sb_lock);
1144 * Downgrade, if superblock is at a higher version than currently
1145 * supported:
1147 * c->sb will be checked before we write the superblock, so update it as
1148 * well:
1150 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1151 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1152 if (c->sb.version > bcachefs_metadata_version_current)
1153 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1154 if (c->sb.version_min > bcachefs_metadata_version_current)
1155 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1156 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1157 return ret;
1160 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1162 lockdep_assert_held(&c->sb_lock);
1164 if (BCH_VERSION_MAJOR(new_version) >
1165 BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
1166 bch2_sb_field_resize(&c->disk_sb, downgrade, 0);
1168 c->disk_sb.sb->version = cpu_to_le16(new_version);
1169 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1172 static int bch2_sb_ext_validate(struct bch_sb *sb, struct bch_sb_field *f,
1173 enum bch_validate_flags flags, struct printbuf *err)
1175 if (vstruct_bytes(f) < 88) {
1176 prt_printf(err, "field too small (%zu < %u)", vstruct_bytes(f), 88);
1177 return -BCH_ERR_invalid_sb_ext;
1180 return 0;
1183 static void bch2_sb_ext_to_text(struct printbuf *out, struct bch_sb *sb,
1184 struct bch_sb_field *f)
1186 struct bch_sb_field_ext *e = field_to_type(f, ext);
1188 prt_printf(out, "Recovery passes required:\t");
1189 prt_bitflags(out, bch2_recovery_passes,
1190 bch2_recovery_passes_from_stable(le64_to_cpu(e->recovery_passes_required[0])));
1191 prt_newline(out);
1193 unsigned long *errors_silent = kmalloc(sizeof(e->errors_silent), GFP_KERNEL);
1194 if (errors_silent) {
1195 le_bitvector_to_cpu(errors_silent, (void *) e->errors_silent, sizeof(e->errors_silent) * 8);
1197 prt_printf(out, "Errors to silently fix:\t");
1198 prt_bitflags_vector(out, bch2_sb_error_strs, errors_silent,
1199 min(BCH_FSCK_ERR_MAX, sizeof(e->errors_silent) * 8));
1200 prt_newline(out);
1202 kfree(errors_silent);
1205 prt_printf(out, "Btrees with missing data:\t");
1206 prt_bitflags(out, __bch2_btree_ids, le64_to_cpu(e->btrees_lost_data));
1207 prt_newline(out);
1210 static const struct bch_sb_field_ops bch_sb_field_ops_ext = {
1211 .validate = bch2_sb_ext_validate,
1212 .to_text = bch2_sb_ext_to_text,
1215 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1216 #define x(f, nr) \
1217 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1218 BCH_SB_FIELDS()
1219 #undef x
1222 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1224 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1226 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1227 ? bch2_sb_field_ops[type]
1228 : &bch2_sb_field_null_ops;
1231 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1232 enum bch_validate_flags flags, struct printbuf *err)
1234 unsigned type = le32_to_cpu(f->type);
1235 struct printbuf field_err = PRINTBUF;
1236 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1237 int ret;
1239 ret = ops->validate ? ops->validate(sb, f, flags, &field_err) : 0;
1240 if (ret) {
1241 prt_printf(err, "Invalid superblock section %s: %s",
1242 bch2_sb_fields[type], field_err.buf);
1243 prt_newline(err);
1244 bch2_sb_field_to_text(err, sb, f);
1247 printbuf_exit(&field_err);
1248 return ret;
1251 void __bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1252 struct bch_sb_field *f)
1254 unsigned type = le32_to_cpu(f->type);
1255 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1257 if (!out->nr_tabstops)
1258 printbuf_tabstop_push(out, 32);
1260 if (ops->to_text)
1261 ops->to_text(out, sb, f);
1264 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1265 struct bch_sb_field *f)
1267 unsigned type = le32_to_cpu(f->type);
1269 if (type < BCH_SB_FIELD_NR)
1270 prt_printf(out, "%s", bch2_sb_fields[type]);
1271 else
1272 prt_printf(out, "(unknown field %u)", type);
1274 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1275 prt_newline(out);
1277 __bch2_sb_field_to_text(out, sb, f);
1280 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1282 unsigned i;
1284 prt_printf(out, "Type: %u", l->layout_type);
1285 prt_newline(out);
1287 prt_str(out, "Superblock max size: ");
1288 prt_units_u64(out, 512 << l->sb_max_size_bits);
1289 prt_newline(out);
1291 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1292 prt_newline(out);
1294 prt_str(out, "Offsets: ");
1295 for (i = 0; i < l->nr_superblocks; i++) {
1296 if (i)
1297 prt_str(out, ", ");
1298 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1300 prt_newline(out);
1303 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1304 bool print_layout, unsigned fields)
1306 if (!out->nr_tabstops)
1307 printbuf_tabstop_push(out, 44);
1309 prt_printf(out, "External UUID:\t");
1310 pr_uuid(out, sb->user_uuid.b);
1311 prt_newline(out);
1313 prt_printf(out, "Internal UUID:\t");
1314 pr_uuid(out, sb->uuid.b);
1315 prt_newline(out);
1317 prt_printf(out, "Magic number:\t");
1318 pr_uuid(out, sb->magic.b);
1319 prt_newline(out);
1321 prt_printf(out, "Device index:\t%u\n", sb->dev_idx);
1323 prt_printf(out, "Label:\t");
1324 if (!strlen(sb->label))
1325 prt_printf(out, "(none)");
1326 else
1327 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1328 prt_newline(out);
1330 prt_printf(out, "Version:\t");
1331 bch2_version_to_text(out, le16_to_cpu(sb->version));
1332 prt_newline(out);
1334 prt_printf(out, "Version upgrade complete:\t");
1335 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1336 prt_newline(out);
1338 prt_printf(out, "Oldest version on disk:\t");
1339 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1340 prt_newline(out);
1342 prt_printf(out, "Created:\t");
1343 if (sb->time_base_lo)
1344 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1345 else
1346 prt_printf(out, "(not set)");
1347 prt_newline(out);
1349 prt_printf(out, "Sequence number:\t");
1350 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1351 prt_newline(out);
1353 prt_printf(out, "Time of last write:\t");
1354 bch2_prt_datetime(out, le64_to_cpu(sb->write_time));
1355 prt_newline(out);
1357 prt_printf(out, "Superblock size:\t");
1358 prt_units_u64(out, vstruct_bytes(sb));
1359 prt_str(out, "/");
1360 prt_units_u64(out, 512ULL << sb->layout.sb_max_size_bits);
1361 prt_newline(out);
1363 prt_printf(out, "Clean:\t%llu\n", BCH_SB_CLEAN(sb));
1364 prt_printf(out, "Devices:\t%u\n", bch2_sb_nr_devices(sb));
1366 prt_printf(out, "Sections:\t");
1367 u64 fields_have = 0;
1368 vstruct_for_each(sb, f)
1369 fields_have |= 1 << le32_to_cpu(f->type);
1370 prt_bitflags(out, bch2_sb_fields, fields_have);
1371 prt_newline(out);
1373 prt_printf(out, "Features:\t");
1374 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1375 prt_newline(out);
1377 prt_printf(out, "Compat features:\t");
1378 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1379 prt_newline(out);
1381 prt_newline(out);
1382 prt_printf(out, "Options:");
1383 prt_newline(out);
1384 printbuf_indent_add(out, 2);
1386 enum bch_opt_id id;
1388 for (id = 0; id < bch2_opts_nr; id++) {
1389 const struct bch_option *opt = bch2_opt_table + id;
1391 if (opt->get_sb != BCH2_NO_SB_OPT) {
1392 u64 v = bch2_opt_from_sb(sb, id);
1394 prt_printf(out, "%s:\t", opt->attr.name);
1395 bch2_opt_to_text(out, NULL, sb, opt, v,
1396 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1397 prt_newline(out);
1402 printbuf_indent_sub(out, 2);
1404 if (print_layout) {
1405 prt_newline(out);
1406 prt_printf(out, "layout:");
1407 prt_newline(out);
1408 printbuf_indent_add(out, 2);
1409 bch2_sb_layout_to_text(out, &sb->layout);
1410 printbuf_indent_sub(out, 2);
1413 vstruct_for_each(sb, f)
1414 if (fields & (1 << le32_to_cpu(f->type))) {
1415 prt_newline(out);
1416 bch2_sb_field_to_text(out, sb, f);