1 // SPDX-License-Identifier: GPL-2.0
6 #include "disk_groups.h"
10 #include "journal_sb.h"
11 #include "journal_seq_blacklist.h"
16 #include "sb-members.h"
22 #include <linux/backing-dev.h>
23 #include <linux/sort.h>
25 static const struct blk_holder_ops bch2_sb_handle_bdev_ops = {
28 struct bch2_metadata_version {
34 static const struct bch2_metadata_version bch2_metadata_versions[] = {
35 #define x(n, v, _recovery_passes) { \
38 .recovery_passes = _recovery_passes, \
40 BCH_METADATA_VERSIONS()
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;
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))
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) ==
66 v = bch2_metadata_versions[i].version;
71 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
77 for (const struct bch2_metadata_version *i = bch2_metadata_versions;
78 i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions);
80 if (i->version > old_version && i->version <= new_version) {
81 if (i->recovery_passes & RECOVERY_PASS_ALL_FSCK)
82 ret |= bch2_fsck_recovery_passes();
83 ret |= i->recovery_passes;
86 return ret &= ~RECOVERY_PASS_ALL_FSCK;
89 const char * const bch2_sb_fields[] = {
90 #define x(name, nr) #name,
96 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
99 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
100 enum bch_sb_field_type type)
102 struct bch_sb_field *f;
104 /* XXX: need locking around superblock to access optional fields */
106 vstruct_for_each(sb, f)
107 if (le32_to_cpu(f->type) == type)
112 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
113 struct bch_sb_field *f,
116 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
117 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
119 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
124 f = vstruct_last(sb->sb);
125 memset(f, 0, sizeof(u64) * u64s);
126 f->u64s = cpu_to_le32(u64s);
131 src = vstruct_end(f);
134 f->u64s = cpu_to_le32(u64s);
135 dst = vstruct_end(f);
140 memmove(dst, src, vstruct_end(sb->sb) - src);
143 memset(src, 0, dst - src);
146 sb->sb->u64s = cpu_to_le32(sb_u64s);
148 return u64s ? f : NULL;
151 void bch2_sb_field_delete(struct bch_sb_handle *sb,
152 enum bch_sb_field_type type)
154 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
157 __bch2_sb_field_resize(sb, f, 0);
160 /* Superblock realloc/free: */
162 void bch2_free_super(struct bch_sb_handle *sb)
165 if (!IS_ERR_OR_NULL(sb->bdev))
166 blkdev_put(sb->bdev, sb->holder);
170 memset(sb, 0, sizeof(*sb));
173 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
175 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
176 size_t new_buffer_size;
177 struct bch_sb *new_sb;
181 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
183 new_buffer_size = roundup_pow_of_two(new_bytes);
185 if (sb->sb && sb->buffer_size >= new_buffer_size)
188 if (sb->sb && sb->have_layout) {
189 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
191 if (new_bytes > max_bytes) {
192 pr_err("%pg: superblock too big: want %zu but have %llu",
193 sb->bdev, new_bytes, max_bytes);
194 return -BCH_ERR_ENOSPC_sb;
198 if (sb->buffer_size >= new_buffer_size && sb->sb)
201 if (dynamic_fault("bcachefs:add:super_realloc"))
202 return -BCH_ERR_ENOMEM_sb_realloc_injected;
204 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
206 return -BCH_ERR_ENOMEM_sb_buf_realloc;
211 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size);
213 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
215 return -BCH_ERR_ENOMEM_sb_bio_realloc;
217 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
223 sb->buffer_size = new_buffer_size;
228 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
229 enum bch_sb_field_type type,
232 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
233 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
234 ssize_t d = -old_u64s + u64s;
236 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
240 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
244 lockdep_assert_held(&c->sb_lock);
246 /* XXX: we're not checking that offline device have enough space */
248 for_each_online_member(ca, c, i) {
249 struct bch_sb_handle *dev_sb = &ca->disk_sb;
251 if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) {
252 percpu_ref_put(&ca->ref);
258 f = bch2_sb_field_get(sb->sb, type);
259 f = __bch2_sb_field_resize(sb, f, u64s);
261 f->type = cpu_to_le32(type);
265 /* Superblock validate: */
267 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
269 u64 offset, prev_offset, max_sectors;
272 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
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 internal 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;
491 lockdep_assert_held(&c->sb_lock);
493 c->sb.uuid = src->uuid;
494 c->sb.user_uuid = src->user_uuid;
495 c->sb.version = le16_to_cpu(src->version);
496 c->sb.version_min = le16_to_cpu(src->version_min);
497 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src);
498 c->sb.nr_devices = src->nr_devices;
499 c->sb.clean = BCH_SB_CLEAN(src);
500 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
502 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
503 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
505 /* XXX this is wrong, we need a 96 or 128 bit integer type */
506 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
507 c->sb.nsec_per_time_unit);
508 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
510 c->sb.features = le64_to_cpu(src->features[0]);
511 c->sb.compat = le64_to_cpu(src->compat[0]);
513 for_each_member_device(ca, c, i) {
514 struct bch_member m = bch2_sb_member_get(src, i);
515 ca->mi = bch2_mi_to_cpu(&m);
519 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
521 struct bch_sb_field *src_f, *dst_f;
522 struct bch_sb *dst = dst_handle->sb;
525 dst->version = src->version;
526 dst->version_min = src->version_min;
528 dst->uuid = src->uuid;
529 dst->user_uuid = src->user_uuid;
530 memcpy(dst->label, src->label, sizeof(dst->label));
532 dst->block_size = src->block_size;
533 dst->nr_devices = src->nr_devices;
535 dst->time_base_lo = src->time_base_lo;
536 dst->time_base_hi = src->time_base_hi;
537 dst->time_precision = src->time_precision;
539 memcpy(dst->flags, src->flags, sizeof(dst->flags));
540 memcpy(dst->features, src->features, sizeof(dst->features));
541 memcpy(dst->compat, src->compat, sizeof(dst->compat));
543 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
546 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
549 src_f = bch2_sb_field_get(src, i);
550 dst_f = bch2_sb_field_get(dst, i);
552 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
553 (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
555 int ret = bch2_sb_realloc(dst_handle,
556 le32_to_cpu(dst_handle->sb->u64s) + d);
561 dst = dst_handle->sb;
562 dst_f = bch2_sb_field_get(dst, i);
565 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
566 src_f ? le32_to_cpu(src_f->u64s) : 0);
569 memcpy(dst_f, src_f, vstruct_bytes(src_f));
575 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
579 lockdep_assert_held(&c->sb_lock);
581 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
582 __copy_super(&c->disk_sb, src) ?:
583 bch2_sb_replicas_to_cpu_replicas(c) ?:
584 bch2_sb_disk_groups_to_cpu(c);
592 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
594 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
597 /* read superblock: */
599 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
601 struct bch_csum csum;
605 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
606 sb->bio->bi_iter.bi_sector = offset;
607 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
609 ret = submit_bio_wait(sb->bio);
611 prt_printf(err, "IO error: %i", ret);
615 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
616 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
617 prt_printf(err, "Not a bcachefs superblock");
618 return -BCH_ERR_invalid_sb_magic;
621 ret = bch2_sb_compatible(sb->sb, err);
625 bytes = vstruct_bytes(sb->sb);
627 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
628 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
629 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
630 return -BCH_ERR_invalid_sb_too_big;
633 if (bytes > sb->buffer_size) {
634 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
640 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
641 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
642 return -BCH_ERR_invalid_sb_csum_type;
645 /* XXX: verify MACs */
646 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
647 null_nonce(), sb->sb);
649 if (bch2_crc_cmp(csum, sb->sb->csum)) {
650 prt_printf(err, "bad checksum");
651 return -BCH_ERR_invalid_sb_csum;
654 sb->seq = le64_to_cpu(sb->sb->seq);
659 int bch2_read_super(const char *path, struct bch_opts *opts,
660 struct bch_sb_handle *sb)
662 u64 offset = opt_get(*opts, sb);
663 struct bch_sb_layout layout;
664 struct printbuf err = PRINTBUF;
670 memset(sb, 0, sizeof(*sb));
671 sb->mode = BLK_OPEN_READ;
673 sb->holder = kmalloc(1, GFP_KERNEL);
678 if (opt_get(*opts, direct_io) == false)
679 sb->mode |= BLK_OPEN_BUFFERED;
682 if (!opt_get(*opts, noexcl))
683 sb->mode |= BLK_OPEN_EXCL;
685 if (!opt_get(*opts, nochanges))
686 sb->mode |= BLK_OPEN_WRITE;
688 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
689 if (IS_ERR(sb->bdev) &&
690 PTR_ERR(sb->bdev) == -EACCES &&
691 opt_get(*opts, read_only)) {
692 sb->mode &= ~BLK_OPEN_WRITE;
694 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
695 if (!IS_ERR(sb->bdev))
696 opt_set(*opts, nochanges, true);
699 if (IS_ERR(sb->bdev)) {
700 ret = PTR_ERR(sb->bdev);
704 ret = bch2_sb_realloc(sb, 0);
706 prt_printf(&err, "error allocating memory for superblock");
710 if (bch2_fs_init_fault("read_super")) {
711 prt_printf(&err, "dynamic fault");
716 ret = read_one_super(sb, offset, &err);
720 if (opt_defined(*opts, sb))
723 printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
725 printbuf_reset(&err);
728 * Error reading primary superblock - read location of backup
731 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
732 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
734 * use sb buffer to read layout, since sb buffer is page aligned but
737 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
739 ret = submit_bio_wait(sb->bio);
741 prt_printf(&err, "IO error: %i", ret);
745 memcpy(&layout, sb->sb, sizeof(layout));
746 ret = validate_sb_layout(&layout, &err);
750 for (i = layout.sb_offset;
751 i < layout.sb_offset + layout.nr_superblocks; i++) {
752 offset = le64_to_cpu(*i);
754 if (offset == opt_get(*opts, sb))
757 ret = read_one_super(sb, offset, &err);
765 if (le16_to_cpu(sb->sb->block_size) << 9 <
766 bdev_logical_block_size(sb->bdev) &&
767 opt_get(*opts, direct_io)) {
769 opt_set(*opts, direct_io, false);
773 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
774 le16_to_cpu(sb->sb->block_size) << 9,
775 bdev_logical_block_size(sb->bdev));
776 ret = -BCH_ERR_block_size_too_small;
781 sb->have_layout = true;
783 ret = bch2_sb_validate(sb, &err, READ);
785 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
793 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
800 /* write superblock: */
802 static void write_super_endio(struct bio *bio)
804 struct bch_dev *ca = bio->bi_private;
806 /* XXX: return errors directly */
808 if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
809 bch2_blk_status_to_str(bio->bi_status)))
810 ca->sb_write_error = 1;
812 closure_put(&ca->fs->sb_write);
813 percpu_ref_put(&ca->io_ref);
816 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
818 struct bch_sb *sb = ca->disk_sb.sb;
819 struct bio *bio = ca->disk_sb.bio;
821 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
822 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
823 bio->bi_end_io = write_super_endio;
824 bio->bi_private = ca;
825 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
827 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
830 percpu_ref_get(&ca->io_ref);
831 closure_bio_submit(bio, &c->sb_write);
834 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
836 struct bch_sb *sb = ca->disk_sb.sb;
837 struct bio *bio = ca->disk_sb.bio;
839 sb->offset = sb->layout.sb_offset[idx];
841 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
842 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
845 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
846 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
847 bio->bi_end_io = write_super_endio;
848 bio->bi_private = ca;
849 bch2_bio_map(bio, sb,
850 roundup((size_t) vstruct_bytes(sb),
851 bdev_logical_block_size(ca->disk_sb.bdev)));
853 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
856 percpu_ref_get(&ca->io_ref);
857 closure_bio_submit(bio, &c->sb_write);
860 int bch2_write_super(struct bch_fs *c)
862 struct closure *cl = &c->sb_write;
864 struct printbuf err = PRINTBUF;
865 unsigned i, sb = 0, nr_wrote;
866 struct bch_devs_mask sb_written;
867 bool wrote, can_mount_without_written, can_mount_with_written;
868 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
871 trace_and_count(c, write_super, c, _RET_IP_);
873 if (c->opts.very_degraded)
874 degraded_flags |= BCH_FORCE_IF_LOST;
876 lockdep_assert_held(&c->sb_lock);
878 closure_init_stack(cl);
879 memset(&sb_written, 0, sizeof(sb_written));
881 /* Make sure we're using the new magic numbers: */
882 c->disk_sb.sb->magic = BCHFS_MAGIC;
883 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
885 le64_add_cpu(&c->disk_sb.sb->seq, 1);
887 if (test_bit(BCH_FS_ERROR, &c->flags))
888 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
889 if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
890 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
892 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
894 bch2_sb_counters_from_cpu(c);
896 for_each_online_member(ca, c, i)
897 bch2_sb_from_fs(c, ca);
899 for_each_online_member(ca, c, i) {
900 printbuf_reset(&err);
902 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
904 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
905 percpu_ref_put(&ca->io_ref);
910 if (c->opts.nochanges)
914 * Defer writing the superblock until filesystem initialization is
915 * complete - don't write out a partly initialized superblock:
917 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
920 for_each_online_member(ca, c, i) {
921 __set_bit(ca->dev_idx, sb_written.d);
922 ca->sb_write_error = 0;
925 for_each_online_member(ca, c, i)
926 read_back_super(c, ca);
929 for_each_online_member(ca, c, i) {
930 if (ca->sb_write_error)
933 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
934 bch2_fs_fatal_error(c,
935 "Superblock write was silently dropped! (seq %llu expected %llu)",
936 le64_to_cpu(ca->sb_read_scratch->seq),
938 percpu_ref_put(&ca->io_ref);
939 ret = -BCH_ERR_erofs_sb_err;
943 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
944 bch2_fs_fatal_error(c,
945 "Superblock modified by another process (seq %llu expected %llu)",
946 le64_to_cpu(ca->sb_read_scratch->seq),
948 percpu_ref_put(&ca->io_ref);
949 ret = -BCH_ERR_erofs_sb_err;
956 for_each_online_member(ca, c, i)
957 if (!ca->sb_write_error &&
958 sb < ca->disk_sb.sb->layout.nr_superblocks) {
959 write_one_super(c, ca, sb);
966 for_each_online_member(ca, c, i) {
967 if (ca->sb_write_error)
968 __clear_bit(ca->dev_idx, sb_written.d);
970 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
973 nr_wrote = dev_mask_nr(&sb_written);
975 can_mount_with_written =
976 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
978 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
979 sb_written.d[i] = ~sb_written.d[i];
981 can_mount_without_written =
982 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
985 * If we would be able to mount _without_ the devices we successfully
986 * wrote superblocks to, we weren't able to write to enough devices:
988 * Exception: if we can mount without the successes because we haven't
989 * written anything (new filesystem), we continue if we'd be able to
990 * mount with the devices we did successfully write to:
992 if (bch2_fs_fatal_err_on(!nr_wrote ||
993 !can_mount_with_written ||
994 (can_mount_without_written &&
995 !can_mount_with_written), c,
996 "Unable to write superblock to sufficient devices (from %ps)",
1000 /* Make new options visible after they're persistent: */
1002 printbuf_exit(&err);
1006 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1008 mutex_lock(&c->sb_lock);
1009 if (!(c->sb.features & (1ULL << feat))) {
1010 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1012 bch2_write_super(c);
1014 mutex_unlock(&c->sb_lock);
1017 /* Downgrade if superblock is at a higher version than currently supported: */
1018 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1020 lockdep_assert_held(&c->sb_lock);
1023 * Downgrade, if superblock is at a higher version than currently
1026 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1027 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1028 if (c->sb.version > bcachefs_metadata_version_current)
1029 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1030 if (c->sb.version_min > bcachefs_metadata_version_current)
1031 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1032 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1035 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1037 lockdep_assert_held(&c->sb_lock);
1039 c->disk_sb.sb->version = cpu_to_le16(new_version);
1040 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1043 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1045 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1050 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1052 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1054 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1055 ? bch2_sb_field_ops[type]
1056 : &bch2_sb_field_null_ops;
1059 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1060 struct printbuf *err)
1062 unsigned type = le32_to_cpu(f->type);
1063 struct printbuf field_err = PRINTBUF;
1064 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1067 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1069 prt_printf(err, "Invalid superblock section %s: %s",
1070 bch2_sb_fields[type], field_err.buf);
1072 bch2_sb_field_to_text(err, sb, f);
1075 printbuf_exit(&field_err);
1079 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1080 struct bch_sb_field *f)
1082 unsigned type = le32_to_cpu(f->type);
1083 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1085 if (!out->nr_tabstops)
1086 printbuf_tabstop_push(out, 32);
1088 if (type < BCH_SB_FIELD_NR)
1089 prt_printf(out, "%s", bch2_sb_fields[type]);
1091 prt_printf(out, "(unknown field %u)", type);
1093 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1097 printbuf_indent_add(out, 2);
1098 ops->to_text(out, sb, f);
1099 printbuf_indent_sub(out, 2);
1103 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1107 prt_printf(out, "Type: %u", l->layout_type);
1110 prt_str(out, "Superblock max size: ");
1111 prt_units_u64(out, 512 << l->sb_max_size_bits);
1114 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1117 prt_str(out, "Offsets: ");
1118 for (i = 0; i < l->nr_superblocks; i++) {
1121 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1126 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1127 bool print_layout, unsigned fields)
1129 struct bch_sb_field *f;
1130 u64 fields_have = 0;
1131 unsigned nr_devices = 0;
1133 if (!out->nr_tabstops)
1134 printbuf_tabstop_push(out, 44);
1136 for (int i = 0; i < sb->nr_devices; i++)
1137 nr_devices += bch2_dev_exists(sb, i);
1139 prt_printf(out, "External UUID:");
1141 pr_uuid(out, sb->user_uuid.b);
1144 prt_printf(out, "Internal UUID:");
1146 pr_uuid(out, sb->uuid.b);
1149 prt_str(out, "Device index:");
1151 prt_printf(out, "%u", sb->dev_idx);
1154 prt_str(out, "Label:");
1156 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1159 prt_str(out, "Version:");
1161 bch2_version_to_text(out, le16_to_cpu(sb->version));
1164 prt_str(out, "Version upgrade complete:");
1166 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1169 prt_printf(out, "Oldest version on disk:");
1171 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1174 prt_printf(out, "Created:");
1176 if (sb->time_base_lo)
1177 pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1179 prt_printf(out, "(not set)");
1182 prt_printf(out, "Sequence number:");
1184 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1187 prt_printf(out, "Superblock size:");
1189 prt_printf(out, "%zu", vstruct_bytes(sb));
1192 prt_printf(out, "Clean:");
1194 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1197 prt_printf(out, "Devices:");
1199 prt_printf(out, "%u", nr_devices);
1202 prt_printf(out, "Sections:");
1203 vstruct_for_each(sb, f)
1204 fields_have |= 1 << le32_to_cpu(f->type);
1206 prt_bitflags(out, bch2_sb_fields, fields_have);
1209 prt_printf(out, "Features:");
1211 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1214 prt_printf(out, "Compat features:");
1216 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1220 prt_printf(out, "Options:");
1222 printbuf_indent_add(out, 2);
1226 for (id = 0; id < bch2_opts_nr; id++) {
1227 const struct bch_option *opt = bch2_opt_table + id;
1229 if (opt->get_sb != BCH2_NO_SB_OPT) {
1230 u64 v = bch2_opt_from_sb(sb, id);
1232 prt_printf(out, "%s:", opt->attr.name);
1234 bch2_opt_to_text(out, NULL, sb, opt, v,
1235 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1241 printbuf_indent_sub(out, 2);
1245 prt_printf(out, "layout:");
1247 printbuf_indent_add(out, 2);
1248 bch2_sb_layout_to_text(out, &sb->layout);
1249 printbuf_indent_sub(out, 2);
1252 vstruct_for_each(sb, f)
1253 if (fields & (1 << le32_to_cpu(f->type))) {
1255 bch2_sb_field_to_text(out, sb, f);