1 // SPDX-License-Identifier: GPL-2.0
6 #include "disk_groups.h"
11 #include "journal_sb.h"
12 #include "journal_seq_blacklist.h"
17 #include "sb-members.h"
23 #include <linux/backing-dev.h>
24 #include <linux/sort.h>
26 struct bch2_metadata_version {
32 static const struct bch2_metadata_version bch2_metadata_versions[] = {
33 #define x(n, v, _recovery_passes) { \
36 .recovery_passes = _recovery_passes, \
38 BCH_METADATA_VERSIONS()
42 void bch2_version_to_text(struct printbuf *out, unsigned v)
44 const char *str = "(unknown version)";
46 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
47 if (bch2_metadata_versions[i].version == v) {
48 str = bch2_metadata_versions[i].name;
52 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
55 unsigned bch2_latest_compatible_version(unsigned v)
57 if (!BCH_VERSION_MAJOR(v))
60 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
61 if (bch2_metadata_versions[i].version > v &&
62 BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
64 v = bch2_metadata_versions[i].version;
69 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
75 for (const struct bch2_metadata_version *i = bch2_metadata_versions;
76 i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions);
78 if (i->version > old_version && i->version <= new_version) {
79 if (i->recovery_passes & RECOVERY_PASS_ALL_FSCK)
80 ret |= bch2_fsck_recovery_passes();
81 ret |= i->recovery_passes;
84 return ret &= ~RECOVERY_PASS_ALL_FSCK;
87 const char * const bch2_sb_fields[] = {
88 #define x(name, nr) #name,
94 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
97 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
98 enum bch_sb_field_type type)
100 struct bch_sb_field *f;
102 /* XXX: need locking around superblock to access optional fields */
104 vstruct_for_each(sb, f)
105 if (le32_to_cpu(f->type) == type)
110 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
111 struct bch_sb_field *f,
114 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
115 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
117 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
122 f = vstruct_last(sb->sb);
123 memset(f, 0, sizeof(u64) * u64s);
124 f->u64s = cpu_to_le32(u64s);
129 src = vstruct_end(f);
132 f->u64s = cpu_to_le32(u64s);
133 dst = vstruct_end(f);
138 memmove(dst, src, vstruct_end(sb->sb) - src);
141 memset(src, 0, dst - src);
144 sb->sb->u64s = cpu_to_le32(sb_u64s);
146 return u64s ? f : NULL;
149 void bch2_sb_field_delete(struct bch_sb_handle *sb,
150 enum bch_sb_field_type type)
152 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
155 __bch2_sb_field_resize(sb, f, 0);
158 /* Superblock realloc/free: */
160 void bch2_free_super(struct bch_sb_handle *sb)
163 if (!IS_ERR_OR_NULL(sb->bdev))
164 blkdev_put(sb->bdev, sb->mode);
167 memset(sb, 0, sizeof(*sb));
170 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
172 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
173 size_t new_buffer_size;
174 struct bch_sb *new_sb;
178 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
180 new_buffer_size = roundup_pow_of_two(new_bytes);
182 if (sb->sb && sb->buffer_size >= new_buffer_size)
185 if (sb->have_layout) {
186 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
188 if (new_bytes > max_bytes) {
189 pr_err("%pg: superblock too big: want %zu but have %llu",
190 sb->bdev, new_bytes, max_bytes);
191 return -BCH_ERR_ENOSPC_sb;
195 if (sb->buffer_size >= new_buffer_size && sb->sb)
198 if (dynamic_fault("bcachefs:add:super_realloc"))
199 return -BCH_ERR_ENOMEM_sb_realloc_injected;
201 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
203 return -BCH_ERR_ENOMEM_sb_buf_realloc;
208 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size);
210 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
212 return -BCH_ERR_ENOMEM_sb_bio_realloc;
214 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
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 int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
266 u64 offset, prev_offset, max_sectors;
269 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
271 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
272 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
273 prt_printf(out, "Not a bcachefs superblock layout");
274 return -BCH_ERR_invalid_sb_layout;
277 if (layout->layout_type != 0) {
278 prt_printf(out, "Invalid superblock layout type %u",
279 layout->layout_type);
280 return -BCH_ERR_invalid_sb_layout_type;
283 if (!layout->nr_superblocks) {
284 prt_printf(out, "Invalid superblock layout: no superblocks");
285 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
288 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
289 prt_printf(out, "Invalid superblock layout: too many superblocks");
290 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
293 max_sectors = 1 << layout->sb_max_size_bits;
295 prev_offset = le64_to_cpu(layout->sb_offset[0]);
297 for (i = 1; i < layout->nr_superblocks; i++) {
298 offset = le64_to_cpu(layout->sb_offset[i]);
300 if (offset < prev_offset + max_sectors) {
301 prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
302 " (sb %u ends at %llu next starts at %llu",
303 i - 1, prev_offset + max_sectors, offset);
304 return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
306 prev_offset = offset;
312 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
314 u16 version = le16_to_cpu(sb->version);
315 u16 version_min = le16_to_cpu(sb->version_min);
317 if (!bch2_version_compatible(version)) {
318 prt_str(out, "Unsupported superblock version ");
319 bch2_version_to_text(out, version);
320 prt_str(out, " (min ");
321 bch2_version_to_text(out, bcachefs_metadata_version_min);
322 prt_str(out, ", max ");
323 bch2_version_to_text(out, bcachefs_metadata_version_current);
325 return -BCH_ERR_invalid_sb_version;
328 if (!bch2_version_compatible(version_min)) {
329 prt_str(out, "Unsupported superblock version_min ");
330 bch2_version_to_text(out, version_min);
331 prt_str(out, " (min ");
332 bch2_version_to_text(out, bcachefs_metadata_version_min);
333 prt_str(out, ", max ");
334 bch2_version_to_text(out, bcachefs_metadata_version_current);
336 return -BCH_ERR_invalid_sb_version;
339 if (version_min > version) {
340 prt_str(out, "Bad minimum version ");
341 bch2_version_to_text(out, version_min);
342 prt_str(out, ", greater than version field ");
343 bch2_version_to_text(out, version);
344 return -BCH_ERR_invalid_sb_version;
350 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
353 struct bch_sb *sb = disk_sb->sb;
354 struct bch_sb_field *f;
355 struct bch_sb_field_members *mi;
356 enum bch_opt_id opt_id;
360 ret = bch2_sb_compatible(sb, out);
364 if (sb->features[1] ||
365 (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
366 prt_printf(out, "Filesystem has incompatible features");
367 return -BCH_ERR_invalid_sb_features;
370 block_size = le16_to_cpu(sb->block_size);
372 if (block_size > PAGE_SECTORS) {
373 prt_printf(out, "Block size too big (got %u, max %u)",
374 block_size, PAGE_SECTORS);
375 return -BCH_ERR_invalid_sb_block_size;
378 if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
379 prt_printf(out, "Bad user UUID (got zeroes)");
380 return -BCH_ERR_invalid_sb_uuid;
383 if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
384 prt_printf(out, "Bad intenal UUID (got zeroes)");
385 return -BCH_ERR_invalid_sb_uuid;
388 if (!sb->nr_devices ||
389 sb->nr_devices > BCH_SB_MEMBERS_MAX) {
390 prt_printf(out, "Bad number of member devices %u (max %u)",
391 sb->nr_devices, BCH_SB_MEMBERS_MAX);
392 return -BCH_ERR_invalid_sb_too_many_members;
395 if (sb->dev_idx >= sb->nr_devices) {
396 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
397 sb->dev_idx, sb->nr_devices);
398 return -BCH_ERR_invalid_sb_dev_idx;
401 if (!sb->time_precision ||
402 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
403 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
404 le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
405 return -BCH_ERR_invalid_sb_time_precision;
410 * Been seeing a bug where these are getting inexplicably
411 * zeroed, so we're now validating them, but we have to be
412 * careful not to preven people's filesystems from mounting:
414 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
415 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
416 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
417 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
419 if (!BCH_SB_VERSION_UPGRADE_COMPLETE(sb))
420 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version));
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);
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,
552 le32_to_cpu(dst_handle->sb->u64s) + d);
557 dst = dst_handle->sb;
558 dst_f = bch2_sb_field_get(dst, i);
561 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
562 src_f ? le32_to_cpu(src_f->u64s) : 0);
565 memcpy(dst_f, src_f, vstruct_bytes(src_f));
571 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
575 lockdep_assert_held(&c->sb_lock);
577 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
578 __copy_super(&c->disk_sb, src) ?:
579 bch2_sb_replicas_to_cpu_replicas(c) ?:
580 bch2_sb_disk_groups_to_cpu(c);
588 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
590 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
593 /* read superblock: */
595 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
597 struct bch_csum csum;
601 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
602 sb->bio->bi_iter.bi_sector = offset;
603 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
605 ret = submit_bio_wait(sb->bio);
607 prt_printf(err, "IO error: %i", ret);
611 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
612 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
613 prt_printf(err, "Not a bcachefs superblock");
614 return -BCH_ERR_invalid_sb_magic;
617 ret = bch2_sb_compatible(sb->sb, err);
621 bytes = vstruct_bytes(sb->sb);
623 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
624 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
625 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
626 return -BCH_ERR_invalid_sb_too_big;
629 if (bytes > sb->buffer_size) {
630 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
636 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
637 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
638 return -BCH_ERR_invalid_sb_csum_type;
641 /* XXX: verify MACs */
642 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
643 null_nonce(), sb->sb);
645 if (bch2_crc_cmp(csum, sb->sb->csum)) {
646 prt_printf(err, "bad checksum");
647 return -BCH_ERR_invalid_sb_csum;
650 sb->seq = le64_to_cpu(sb->sb->seq);
655 int bch2_read_super(const char *path, struct bch_opts *opts,
656 struct bch_sb_handle *sb)
658 u64 offset = opt_get(*opts, sb);
659 struct bch_sb_layout layout;
660 struct printbuf err = PRINTBUF;
666 memset(sb, 0, sizeof(*sb));
667 sb->mode = FMODE_READ;
671 if (opt_get(*opts, direct_io) == false)
672 sb->mode |= FMODE_BUFFERED;
675 if (!opt_get(*opts, noexcl))
676 sb->mode |= FMODE_EXCL;
678 if (!opt_get(*opts, nochanges))
679 sb->mode |= FMODE_WRITE;
681 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
682 if (IS_ERR(sb->bdev) &&
683 PTR_ERR(sb->bdev) == -EACCES &&
684 opt_get(*opts, read_only)) {
685 sb->mode &= ~FMODE_WRITE;
687 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
688 if (!IS_ERR(sb->bdev))
689 opt_set(*opts, nochanges, true);
692 if (IS_ERR(sb->bdev)) {
693 ret = PTR_ERR(sb->bdev);
697 ret = bch2_sb_realloc(sb, 0);
699 prt_printf(&err, "error allocating memory for superblock");
703 if (bch2_fs_init_fault("read_super")) {
704 prt_printf(&err, "dynamic fault");
709 ret = read_one_super(sb, offset, &err);
713 if (opt_defined(*opts, sb))
716 printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
718 printbuf_reset(&err);
721 * Error reading primary superblock - read location of backup
724 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
725 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
727 * use sb buffer to read layout, since sb buffer is page aligned but
730 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
732 ret = submit_bio_wait(sb->bio);
734 prt_printf(&err, "IO error: %i", ret);
738 memcpy(&layout, sb->sb, sizeof(layout));
739 ret = validate_sb_layout(&layout, &err);
743 for (i = layout.sb_offset;
744 i < layout.sb_offset + layout.nr_superblocks; i++) {
745 offset = le64_to_cpu(*i);
747 if (offset == opt_get(*opts, sb))
750 ret = read_one_super(sb, offset, &err);
758 if (le16_to_cpu(sb->sb->block_size) << 9 <
759 bdev_logical_block_size(sb->bdev) &&
760 opt_get(*opts, direct_io)) {
762 opt_set(*opts, direct_io, false);
766 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
767 le16_to_cpu(sb->sb->block_size) << 9,
768 bdev_logical_block_size(sb->bdev));
769 ret = -BCH_ERR_block_size_too_small;
774 sb->have_layout = true;
776 ret = bch2_sb_validate(sb, &err, READ);
778 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
786 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
793 /* write superblock: */
795 static void write_super_endio(struct bio *bio)
797 struct bch_dev *ca = bio->bi_private;
799 /* XXX: return errors directly */
801 if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
802 bch2_blk_status_to_str(bio->bi_status)))
803 ca->sb_write_error = 1;
805 closure_put(&ca->fs->sb_write);
806 percpu_ref_put(&ca->io_ref);
809 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
811 struct bch_sb *sb = ca->disk_sb.sb;
812 struct bio *bio = ca->disk_sb.bio;
814 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
815 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
816 bio->bi_end_io = write_super_endio;
817 bio->bi_private = ca;
818 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
820 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
823 percpu_ref_get(&ca->io_ref);
824 closure_bio_submit(bio, &c->sb_write);
827 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
829 struct bch_sb *sb = ca->disk_sb.sb;
830 struct bio *bio = ca->disk_sb.bio;
832 sb->offset = sb->layout.sb_offset[idx];
834 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
835 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
838 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
839 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
840 bio->bi_end_io = write_super_endio;
841 bio->bi_private = ca;
842 bch2_bio_map(bio, sb,
843 roundup((size_t) vstruct_bytes(sb),
844 bdev_logical_block_size(ca->disk_sb.bdev)));
846 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
849 percpu_ref_get(&ca->io_ref);
850 closure_bio_submit(bio, &c->sb_write);
853 int bch2_write_super(struct bch_fs *c)
855 struct closure *cl = &c->sb_write;
857 struct printbuf err = PRINTBUF;
858 unsigned i, sb = 0, nr_wrote;
859 struct bch_devs_mask sb_written;
860 bool wrote, can_mount_without_written, can_mount_with_written;
861 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
864 trace_and_count(c, write_super, c, _RET_IP_);
866 if (c->opts.very_degraded)
867 degraded_flags |= BCH_FORCE_IF_LOST;
869 lockdep_assert_held(&c->sb_lock);
871 closure_init_stack(cl);
872 memset(&sb_written, 0, sizeof(sb_written));
874 /* Make sure we're using the new magic numbers: */
875 c->disk_sb.sb->magic = BCHFS_MAGIC;
876 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
878 le64_add_cpu(&c->disk_sb.sb->seq, 1);
880 if (test_bit(BCH_FS_ERROR, &c->flags))
881 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
882 if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
883 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
885 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
887 bch2_sb_counters_from_cpu(c);
889 for_each_online_member(ca, c, i)
890 bch2_sb_from_fs(c, ca);
892 for_each_online_member(ca, c, i) {
893 printbuf_reset(&err);
895 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
897 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
898 percpu_ref_put(&ca->io_ref);
903 if (c->opts.nochanges)
907 * Defer writing the superblock until filesystem initialization is
908 * complete - don't write out a partly initialized superblock:
910 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
913 for_each_online_member(ca, c, i) {
914 __set_bit(ca->dev_idx, sb_written.d);
915 ca->sb_write_error = 0;
918 for_each_online_member(ca, c, i)
919 read_back_super(c, ca);
922 for_each_online_member(ca, c, i) {
923 if (ca->sb_write_error)
926 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
927 bch2_fs_fatal_error(c,
928 "Superblock write was silently dropped! (seq %llu expected %llu)",
929 le64_to_cpu(ca->sb_read_scratch->seq),
931 percpu_ref_put(&ca->io_ref);
932 ret = -BCH_ERR_erofs_sb_err;
936 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
937 bch2_fs_fatal_error(c,
938 "Superblock modified by another process (seq %llu expected %llu)",
939 le64_to_cpu(ca->sb_read_scratch->seq),
941 percpu_ref_put(&ca->io_ref);
942 ret = -BCH_ERR_erofs_sb_err;
949 for_each_online_member(ca, c, i)
950 if (!ca->sb_write_error &&
951 sb < ca->disk_sb.sb->layout.nr_superblocks) {
952 write_one_super(c, ca, sb);
959 for_each_online_member(ca, c, i) {
960 if (ca->sb_write_error)
961 __clear_bit(ca->dev_idx, sb_written.d);
963 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
966 nr_wrote = dev_mask_nr(&sb_written);
968 can_mount_with_written =
969 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
971 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
972 sb_written.d[i] = ~sb_written.d[i];
974 can_mount_without_written =
975 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
978 * If we would be able to mount _without_ the devices we successfully
979 * wrote superblocks to, we weren't able to write to enough devices:
981 * Exception: if we can mount without the successes because we haven't
982 * written anything (new filesystem), we continue if we'd be able to
983 * mount with the devices we did successfully write to:
985 if (bch2_fs_fatal_err_on(!nr_wrote ||
986 !can_mount_with_written ||
987 (can_mount_without_written &&
988 !can_mount_with_written), c,
989 "Unable to write superblock to sufficient devices (from %ps)",
993 /* Make new options visible after they're persistent: */
999 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1001 mutex_lock(&c->sb_lock);
1002 if (!(c->sb.features & (1ULL << feat))) {
1003 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1005 bch2_write_super(c);
1007 mutex_unlock(&c->sb_lock);
1010 /* Downgrade if superblock is at a higher version than currently supported: */
1011 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1013 lockdep_assert_held(&c->sb_lock);
1016 * Downgrade, if superblock is at a higher version than currently
1019 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1020 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1021 if (c->sb.version > bcachefs_metadata_version_current)
1022 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1023 if (c->sb.version_min > bcachefs_metadata_version_current)
1024 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1025 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1028 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1030 lockdep_assert_held(&c->sb_lock);
1032 c->disk_sb.sb->version = cpu_to_le16(new_version);
1033 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1036 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1038 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1043 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1045 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1047 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1048 ? bch2_sb_field_ops[type]
1049 : &bch2_sb_field_null_ops;
1052 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1053 struct printbuf *err)
1055 unsigned type = le32_to_cpu(f->type);
1056 struct printbuf field_err = PRINTBUF;
1057 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1060 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1062 prt_printf(err, "Invalid superblock section %s: %s",
1063 bch2_sb_fields[type], field_err.buf);
1065 bch2_sb_field_to_text(err, sb, f);
1068 printbuf_exit(&field_err);
1072 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1073 struct bch_sb_field *f)
1075 unsigned type = le32_to_cpu(f->type);
1076 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1078 if (!out->nr_tabstops)
1079 printbuf_tabstop_push(out, 32);
1081 if (type < BCH_SB_FIELD_NR)
1082 prt_printf(out, "%s", bch2_sb_fields[type]);
1084 prt_printf(out, "(unknown field %u)", type);
1086 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1090 printbuf_indent_add(out, 2);
1091 ops->to_text(out, sb, f);
1092 printbuf_indent_sub(out, 2);
1096 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1100 prt_printf(out, "Type: %u", l->layout_type);
1103 prt_str(out, "Superblock max size: ");
1104 prt_units_u64(out, 512 << l->sb_max_size_bits);
1107 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1110 prt_str(out, "Offsets: ");
1111 for (i = 0; i < l->nr_superblocks; i++) {
1114 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1119 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1120 bool print_layout, unsigned fields)
1122 struct bch_sb_field_members *mi;
1123 struct bch_sb_field *f;
1124 u64 fields_have = 0;
1125 unsigned nr_devices = 0;
1127 if (!out->nr_tabstops)
1128 printbuf_tabstop_push(out, 44);
1130 mi = bch2_sb_get_members(sb);
1132 struct bch_member *m;
1134 for (m = mi->members;
1135 m < mi->members + sb->nr_devices;
1137 nr_devices += bch2_member_exists(m);
1140 prt_printf(out, "External UUID:");
1142 pr_uuid(out, sb->user_uuid.b);
1145 prt_printf(out, "Internal UUID:");
1147 pr_uuid(out, sb->uuid.b);
1150 prt_str(out, "Device index:");
1152 prt_printf(out, "%u", sb->dev_idx);
1155 prt_str(out, "Label:");
1157 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1160 prt_str(out, "Version:");
1162 bch2_version_to_text(out, le16_to_cpu(sb->version));
1165 prt_str(out, "Version upgrade complete:");
1167 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1170 prt_printf(out, "Oldest version on disk:");
1172 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1175 prt_printf(out, "Created:");
1177 if (sb->time_base_lo)
1178 pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1180 prt_printf(out, "(not set)");
1183 prt_printf(out, "Sequence number:");
1185 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1188 prt_printf(out, "Superblock size:");
1190 prt_printf(out, "%zu", vstruct_bytes(sb));
1193 prt_printf(out, "Clean:");
1195 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1198 prt_printf(out, "Devices:");
1200 prt_printf(out, "%u", nr_devices);
1203 prt_printf(out, "Sections:");
1204 vstruct_for_each(sb, f)
1205 fields_have |= 1 << le32_to_cpu(f->type);
1207 prt_bitflags(out, bch2_sb_fields, fields_have);
1210 prt_printf(out, "Features:");
1212 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1215 prt_printf(out, "Compat features:");
1217 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1221 prt_printf(out, "Options:");
1223 printbuf_indent_add(out, 2);
1227 for (id = 0; id < bch2_opts_nr; id++) {
1228 const struct bch_option *opt = bch2_opt_table + id;
1230 if (opt->get_sb != BCH2_NO_SB_OPT) {
1231 u64 v = bch2_opt_from_sb(sb, id);
1233 prt_printf(out, "%s:", opt->attr.name);
1235 bch2_opt_to_text(out, NULL, sb, opt, v,
1236 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1242 printbuf_indent_sub(out, 2);
1246 prt_printf(out, "layout:");
1248 printbuf_indent_add(out, 2);
1249 bch2_sb_layout_to_text(out, &sb->layout);
1250 printbuf_indent_sub(out, 2);
1253 vstruct_for_each(sb, f)
1254 if (fields & (1 << le32_to_cpu(f->type))) {
1256 bch2_sb_field_to_text(out, sb, f);