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-errors.h"
17 #include "sb-members.h"
23 #include <linux/backing-dev.h>
24 #include <linux/sort.h>
26 static const struct blk_holder_ops bch2_sb_handle_bdev_ops = {
29 struct bch2_metadata_version {
35 static const struct bch2_metadata_version bch2_metadata_versions[] = {
36 #define x(n, v, _recovery_passes) { \
39 .recovery_passes = _recovery_passes, \
41 BCH_METADATA_VERSIONS()
45 void bch2_version_to_text(struct printbuf *out, unsigned v)
47 const char *str = "(unknown version)";
49 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
50 if (bch2_metadata_versions[i].version == v) {
51 str = bch2_metadata_versions[i].name;
55 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
58 unsigned bch2_latest_compatible_version(unsigned v)
60 if (!BCH_VERSION_MAJOR(v))
63 for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
64 if (bch2_metadata_versions[i].version > v &&
65 BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
67 v = bch2_metadata_versions[i].version;
72 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
78 for (const struct bch2_metadata_version *i = bch2_metadata_versions;
79 i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions);
81 if (i->version > old_version && i->version <= new_version) {
82 if (i->recovery_passes & RECOVERY_PASS_ALL_FSCK)
83 ret |= bch2_fsck_recovery_passes();
84 ret |= i->recovery_passes;
87 return ret &= ~RECOVERY_PASS_ALL_FSCK;
90 const char * const bch2_sb_fields[] = {
91 #define x(name, nr) #name,
97 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
100 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *sb,
101 enum bch_sb_field_type type)
103 /* XXX: need locking around superblock to access optional fields */
105 vstruct_for_each(sb, f)
106 if (le32_to_cpu(f->type) == type)
111 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
112 struct bch_sb_field *f,
115 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
116 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
118 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
123 f = vstruct_last(sb->sb);
124 memset(f, 0, sizeof(u64) * u64s);
125 f->u64s = cpu_to_le32(u64s);
130 src = vstruct_end(f);
133 f->u64s = cpu_to_le32(u64s);
134 dst = vstruct_end(f);
139 memmove(dst, src, vstruct_end(sb->sb) - src);
142 memset(src, 0, dst - src);
145 sb->sb->u64s = cpu_to_le32(sb_u64s);
147 return u64s ? f : NULL;
150 void bch2_sb_field_delete(struct bch_sb_handle *sb,
151 enum bch_sb_field_type type)
153 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
156 __bch2_sb_field_resize(sb, f, 0);
159 /* Superblock realloc/free: */
161 void bch2_free_super(struct bch_sb_handle *sb)
164 if (!IS_ERR_OR_NULL(sb->bdev))
165 blkdev_put(sb->bdev, sb->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_id(struct bch_sb_handle *sb,
229 enum bch_sb_field_type type,
232 struct bch_sb_field *f = bch2_sb_field_get_id(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);
242 lockdep_assert_held(&c->sb_lock);
244 /* XXX: we're not checking that offline device have enough space */
246 for_each_online_member(c, ca) {
247 struct bch_sb_handle *dev_sb = &ca->disk_sb;
249 if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) {
250 percpu_ref_put(&ca->ref);
256 f = bch2_sb_field_get_id(sb->sb, type);
257 f = __bch2_sb_field_resize(sb, f, u64s);
259 f->type = cpu_to_le32(type);
263 /* Superblock validate: */
265 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
267 u64 offset, prev_offset, max_sectors;
270 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
272 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
273 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
274 prt_printf(out, "Not a bcachefs superblock layout");
275 return -BCH_ERR_invalid_sb_layout;
278 if (layout->layout_type != 0) {
279 prt_printf(out, "Invalid superblock layout type %u",
280 layout->layout_type);
281 return -BCH_ERR_invalid_sb_layout_type;
284 if (!layout->nr_superblocks) {
285 prt_printf(out, "Invalid superblock layout: no superblocks");
286 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
289 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
290 prt_printf(out, "Invalid superblock layout: too many superblocks");
291 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
294 max_sectors = 1 << layout->sb_max_size_bits;
296 prev_offset = le64_to_cpu(layout->sb_offset[0]);
298 for (i = 1; i < layout->nr_superblocks; i++) {
299 offset = le64_to_cpu(layout->sb_offset[i]);
301 if (offset < prev_offset + max_sectors) {
302 prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
303 " (sb %u ends at %llu next starts at %llu",
304 i - 1, prev_offset + max_sectors, offset);
305 return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
307 prev_offset = offset;
313 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
315 u16 version = le16_to_cpu(sb->version);
316 u16 version_min = le16_to_cpu(sb->version_min);
318 if (!bch2_version_compatible(version)) {
319 prt_str(out, "Unsupported superblock version ");
320 bch2_version_to_text(out, version);
321 prt_str(out, " (min ");
322 bch2_version_to_text(out, bcachefs_metadata_version_min);
323 prt_str(out, ", max ");
324 bch2_version_to_text(out, bcachefs_metadata_version_current);
326 return -BCH_ERR_invalid_sb_version;
329 if (!bch2_version_compatible(version_min)) {
330 prt_str(out, "Unsupported superblock version_min ");
331 bch2_version_to_text(out, version_min);
332 prt_str(out, " (min ");
333 bch2_version_to_text(out, bcachefs_metadata_version_min);
334 prt_str(out, ", max ");
335 bch2_version_to_text(out, bcachefs_metadata_version_current);
337 return -BCH_ERR_invalid_sb_version;
340 if (version_min > version) {
341 prt_str(out, "Bad minimum version ");
342 bch2_version_to_text(out, version_min);
343 prt_str(out, ", greater than version field ");
344 bch2_version_to_text(out, version);
345 return -BCH_ERR_invalid_sb_version;
351 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
354 struct bch_sb *sb = disk_sb->sb;
355 struct bch_sb_field_members_v1 *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 internal 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_field_get(sb, members_v1);
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_v1)
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;
486 lockdep_assert_held(&c->sb_lock);
488 c->sb.uuid = src->uuid;
489 c->sb.user_uuid = src->user_uuid;
490 c->sb.version = le16_to_cpu(src->version);
491 c->sb.version_min = le16_to_cpu(src->version_min);
492 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src);
493 c->sb.nr_devices = src->nr_devices;
494 c->sb.clean = BCH_SB_CLEAN(src);
495 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
497 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
498 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
500 /* XXX this is wrong, we need a 96 or 128 bit integer type */
501 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
502 c->sb.nsec_per_time_unit);
503 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
505 c->sb.features = le64_to_cpu(src->features[0]);
506 c->sb.compat = le64_to_cpu(src->compat[0]);
508 for_each_member_device(c, ca) {
509 struct bch_member m = bch2_sb_member_get(src, ca->dev_idx);
510 ca->mi = bch2_mi_to_cpu(&m);
514 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
516 struct bch_sb_field *src_f, *dst_f;
517 struct bch_sb *dst = dst_handle->sb;
520 dst->version = src->version;
521 dst->version_min = src->version_min;
523 dst->uuid = src->uuid;
524 dst->user_uuid = src->user_uuid;
525 memcpy(dst->label, src->label, sizeof(dst->label));
527 dst->block_size = src->block_size;
528 dst->nr_devices = src->nr_devices;
530 dst->time_base_lo = src->time_base_lo;
531 dst->time_base_hi = src->time_base_hi;
532 dst->time_precision = src->time_precision;
533 dst->write_time = src->write_time;
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_id(src, i);
546 dst_f = bch2_sb_field_get_id(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_id(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 static int __bch2_read_super(const char *path, struct bch_opts *opts,
656 struct bch_sb_handle *sb, bool ignore_notbchfs_msg)
658 u64 offset = opt_get(*opts, sb);
659 struct bch_sb_layout layout;
660 struct printbuf err = PRINTBUF;
661 struct printbuf err2 = PRINTBUF;
667 memset(sb, 0, sizeof(*sb));
668 sb->mode = BLK_OPEN_READ;
670 sb->holder = kmalloc(1, GFP_KERNEL);
674 sb->sb_name = kstrdup(path, GFP_KERNEL);
679 if (opt_get(*opts, direct_io) == false)
680 sb->mode |= BLK_OPEN_BUFFERED;
683 if (!opt_get(*opts, noexcl))
684 sb->mode |= BLK_OPEN_EXCL;
686 if (!opt_get(*opts, nochanges))
687 sb->mode |= BLK_OPEN_WRITE;
689 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
690 if (IS_ERR(sb->bdev) &&
691 PTR_ERR(sb->bdev) == -EACCES &&
692 opt_get(*opts, read_only)) {
693 sb->mode &= ~BLK_OPEN_WRITE;
695 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
696 if (!IS_ERR(sb->bdev))
697 opt_set(*opts, nochanges, true);
700 if (IS_ERR(sb->bdev)) {
701 ret = PTR_ERR(sb->bdev);
705 ret = bch2_sb_realloc(sb, 0);
707 prt_printf(&err, "error allocating memory for superblock");
711 if (bch2_fs_init_fault("read_super")) {
712 prt_printf(&err, "dynamic fault");
717 ret = read_one_super(sb, offset, &err);
721 if (opt_defined(*opts, sb))
724 prt_printf(&err2, "bcachefs (%s): error reading default superblock: %s\n",
726 if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg)
727 printk(KERN_INFO "%s", err2.buf);
729 printk(KERN_ERR "%s", err2.buf);
731 printbuf_exit(&err2);
732 printbuf_reset(&err);
735 * Error reading primary superblock - read location of backup
738 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
739 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
741 * use sb buffer to read layout, since sb buffer is page aligned but
744 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
746 ret = submit_bio_wait(sb->bio);
748 prt_printf(&err, "IO error: %i", ret);
752 memcpy(&layout, sb->sb, sizeof(layout));
753 ret = validate_sb_layout(&layout, &err);
757 for (i = layout.sb_offset;
758 i < layout.sb_offset + layout.nr_superblocks; i++) {
759 offset = le64_to_cpu(*i);
761 if (offset == opt_get(*opts, sb))
764 ret = read_one_super(sb, offset, &err);
772 if (le16_to_cpu(sb->sb->block_size) << 9 <
773 bdev_logical_block_size(sb->bdev) &&
774 opt_get(*opts, direct_io)) {
776 opt_set(*opts, direct_io, false);
780 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
781 le16_to_cpu(sb->sb->block_size) << 9,
782 bdev_logical_block_size(sb->bdev));
783 ret = -BCH_ERR_block_size_too_small;
788 sb->have_layout = true;
790 ret = bch2_sb_validate(sb, &err, READ);
792 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s\n",
800 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s\n",
807 int bch2_read_super(const char *path, struct bch_opts *opts,
808 struct bch_sb_handle *sb)
810 return __bch2_read_super(path, opts, sb, false);
813 /* provide a silenced version for mount.bcachefs */
815 int bch2_read_super_silent(const char *path, struct bch_opts *opts,
816 struct bch_sb_handle *sb)
818 return __bch2_read_super(path, opts, sb, true);
821 /* write superblock: */
823 static void write_super_endio(struct bio *bio)
825 struct bch_dev *ca = bio->bi_private;
827 /* XXX: return errors directly */
829 if (bch2_dev_io_err_on(bio->bi_status, ca,
831 ? BCH_MEMBER_ERROR_write
832 : BCH_MEMBER_ERROR_read,
833 "superblock %s error: %s",
834 bio_data_dir(bio) ? "write" : "read",
835 bch2_blk_status_to_str(bio->bi_status)))
836 ca->sb_write_error = 1;
838 closure_put(&ca->fs->sb_write);
839 percpu_ref_put(&ca->io_ref);
842 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
844 struct bch_sb *sb = ca->disk_sb.sb;
845 struct bio *bio = ca->disk_sb.bio;
847 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
848 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
849 bio->bi_end_io = write_super_endio;
850 bio->bi_private = ca;
851 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
853 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
856 percpu_ref_get(&ca->io_ref);
857 closure_bio_submit(bio, &c->sb_write);
860 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
862 struct bch_sb *sb = ca->disk_sb.sb;
863 struct bio *bio = ca->disk_sb.bio;
865 sb->offset = sb->layout.sb_offset[idx];
867 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
868 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
871 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
872 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
873 bio->bi_end_io = write_super_endio;
874 bio->bi_private = ca;
875 bch2_bio_map(bio, sb,
876 roundup((size_t) vstruct_bytes(sb),
877 bdev_logical_block_size(ca->disk_sb.bdev)));
879 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
882 percpu_ref_get(&ca->io_ref);
883 closure_bio_submit(bio, &c->sb_write);
886 int bch2_write_super(struct bch_fs *c)
888 struct closure *cl = &c->sb_write;
889 struct printbuf err = PRINTBUF;
890 unsigned sb = 0, nr_wrote;
891 struct bch_devs_mask sb_written;
892 bool wrote, can_mount_without_written, can_mount_with_written;
893 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
896 trace_and_count(c, write_super, c, _RET_IP_);
898 if (c->opts.very_degraded)
899 degraded_flags |= BCH_FORCE_IF_LOST;
901 lockdep_assert_held(&c->sb_lock);
903 closure_init_stack(cl);
904 memset(&sb_written, 0, sizeof(sb_written));
906 /* Make sure we're using the new magic numbers: */
907 c->disk_sb.sb->magic = BCHFS_MAGIC;
908 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
910 le64_add_cpu(&c->disk_sb.sb->seq, 1);
912 struct bch_sb_field_members_v2 *mi = bch2_sb_field_get(c->disk_sb.sb, members_v2);
913 for_each_online_member(c, ca)
914 __bch2_members_v2_get_mut(mi, ca->dev_idx)->seq = c->disk_sb.sb->seq;
915 c->disk_sb.sb->write_time = cpu_to_le64(ktime_get_real_seconds());
917 if (test_bit(BCH_FS_error, &c->flags))
918 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
919 if (test_bit(BCH_FS_topology_error, &c->flags))
920 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
922 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
924 bch2_sb_counters_from_cpu(c);
925 bch2_sb_members_from_cpu(c);
926 bch2_sb_members_cpy_v2_v1(&c->disk_sb);
927 bch2_sb_errors_from_cpu(c);
929 for_each_online_member(c, ca)
930 bch2_sb_from_fs(c, ca);
932 for_each_online_member(c, ca) {
933 printbuf_reset(&err);
935 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
937 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
938 percpu_ref_put(&ca->io_ref);
943 if (c->opts.nochanges)
947 * Defer writing the superblock until filesystem initialization is
948 * complete - don't write out a partly initialized superblock:
950 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
953 if (le16_to_cpu(c->disk_sb.sb->version) > bcachefs_metadata_version_current) {
954 struct printbuf buf = PRINTBUF;
955 prt_printf(&buf, "attempting to write superblock that wasn't version downgraded (");
956 bch2_version_to_text(&buf, le16_to_cpu(c->disk_sb.sb->version));
957 prt_str(&buf, " > ");
958 bch2_version_to_text(&buf, bcachefs_metadata_version_current);
960 bch2_fs_fatal_error(c, "%s", buf.buf);
962 return -BCH_ERR_sb_not_downgraded;
965 for_each_online_member(c, ca) {
966 __set_bit(ca->dev_idx, sb_written.d);
967 ca->sb_write_error = 0;
970 for_each_online_member(c, ca)
971 read_back_super(c, ca);
974 for_each_online_member(c, ca) {
975 if (ca->sb_write_error)
978 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
979 bch2_fs_fatal_error(c,
980 "Superblock write was silently dropped! (seq %llu expected %llu)",
981 le64_to_cpu(ca->sb_read_scratch->seq),
983 percpu_ref_put(&ca->io_ref);
984 ret = -BCH_ERR_erofs_sb_err;
988 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
989 bch2_fs_fatal_error(c,
990 "Superblock modified by another process (seq %llu expected %llu)",
991 le64_to_cpu(ca->sb_read_scratch->seq),
993 percpu_ref_put(&ca->io_ref);
994 ret = -BCH_ERR_erofs_sb_err;
1001 for_each_online_member(c, ca)
1002 if (!ca->sb_write_error &&
1003 sb < ca->disk_sb.sb->layout.nr_superblocks) {
1004 write_one_super(c, ca, sb);
1011 for_each_online_member(c, ca) {
1012 if (ca->sb_write_error)
1013 __clear_bit(ca->dev_idx, sb_written.d);
1015 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
1018 nr_wrote = dev_mask_nr(&sb_written);
1020 can_mount_with_written =
1021 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1023 for (unsigned i = 0; i < ARRAY_SIZE(sb_written.d); i++)
1024 sb_written.d[i] = ~sb_written.d[i];
1026 can_mount_without_written =
1027 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1030 * If we would be able to mount _without_ the devices we successfully
1031 * wrote superblocks to, we weren't able to write to enough devices:
1033 * Exception: if we can mount without the successes because we haven't
1034 * written anything (new filesystem), we continue if we'd be able to
1035 * mount with the devices we did successfully write to:
1037 if (bch2_fs_fatal_err_on(!nr_wrote ||
1038 !can_mount_with_written ||
1039 (can_mount_without_written &&
1040 !can_mount_with_written), c,
1041 "Unable to write superblock to sufficient devices (from %ps)",
1045 /* Make new options visible after they're persistent: */
1047 printbuf_exit(&err);
1051 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1053 mutex_lock(&c->sb_lock);
1054 if (!(c->sb.features & (1ULL << feat))) {
1055 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1057 bch2_write_super(c);
1059 mutex_unlock(&c->sb_lock);
1062 /* Downgrade if superblock is at a higher version than currently supported: */
1063 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1065 lockdep_assert_held(&c->sb_lock);
1068 * Downgrade, if superblock is at a higher version than currently
1071 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1072 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1073 if (c->sb.version > bcachefs_metadata_version_current)
1074 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1075 if (c->sb.version_min > bcachefs_metadata_version_current)
1076 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1077 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1080 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1082 lockdep_assert_held(&c->sb_lock);
1084 c->disk_sb.sb->version = cpu_to_le16(new_version);
1085 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1088 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1090 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1095 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1097 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1099 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1100 ? bch2_sb_field_ops[type]
1101 : &bch2_sb_field_null_ops;
1104 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1105 struct printbuf *err)
1107 unsigned type = le32_to_cpu(f->type);
1108 struct printbuf field_err = PRINTBUF;
1109 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1112 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1114 prt_printf(err, "Invalid superblock section %s: %s",
1115 bch2_sb_fields[type], field_err.buf);
1117 bch2_sb_field_to_text(err, sb, f);
1120 printbuf_exit(&field_err);
1124 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1125 struct bch_sb_field *f)
1127 unsigned type = le32_to_cpu(f->type);
1128 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1130 if (!out->nr_tabstops)
1131 printbuf_tabstop_push(out, 32);
1133 if (type < BCH_SB_FIELD_NR)
1134 prt_printf(out, "%s", bch2_sb_fields[type]);
1136 prt_printf(out, "(unknown field %u)", type);
1138 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1142 printbuf_indent_add(out, 2);
1143 ops->to_text(out, sb, f);
1144 printbuf_indent_sub(out, 2);
1148 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1152 prt_printf(out, "Type: %u", l->layout_type);
1155 prt_str(out, "Superblock max size: ");
1156 prt_units_u64(out, 512 << l->sb_max_size_bits);
1159 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1162 prt_str(out, "Offsets: ");
1163 for (i = 0; i < l->nr_superblocks; i++) {
1166 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1171 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1172 bool print_layout, unsigned fields)
1174 u64 fields_have = 0;
1175 unsigned nr_devices = 0;
1177 if (!out->nr_tabstops)
1178 printbuf_tabstop_push(out, 44);
1180 for (int i = 0; i < sb->nr_devices; i++)
1181 nr_devices += bch2_dev_exists(sb, i);
1183 prt_printf(out, "External UUID:");
1185 pr_uuid(out, sb->user_uuid.b);
1188 prt_printf(out, "Internal UUID:");
1190 pr_uuid(out, sb->uuid.b);
1193 prt_str(out, "Device index:");
1195 prt_printf(out, "%u", sb->dev_idx);
1198 prt_str(out, "Label:");
1200 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1203 prt_str(out, "Version:");
1205 bch2_version_to_text(out, le16_to_cpu(sb->version));
1208 prt_str(out, "Version upgrade complete:");
1210 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1213 prt_printf(out, "Oldest version on disk:");
1215 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1218 prt_printf(out, "Created:");
1220 if (sb->time_base_lo)
1221 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1223 prt_printf(out, "(not set)");
1226 prt_printf(out, "Sequence number:");
1228 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1231 prt_printf(out, "Time of last write:");
1233 bch2_prt_datetime(out, le64_to_cpu(sb->write_time));
1236 prt_printf(out, "Superblock size:");
1238 prt_printf(out, "%zu", vstruct_bytes(sb));
1241 prt_printf(out, "Clean:");
1243 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1246 prt_printf(out, "Devices:");
1248 prt_printf(out, "%u", nr_devices);
1251 prt_printf(out, "Sections:");
1252 vstruct_for_each(sb, f)
1253 fields_have |= 1 << le32_to_cpu(f->type);
1255 prt_bitflags(out, bch2_sb_fields, fields_have);
1258 prt_printf(out, "Features:");
1260 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1263 prt_printf(out, "Compat features:");
1265 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1269 prt_printf(out, "Options:");
1271 printbuf_indent_add(out, 2);
1275 for (id = 0; id < bch2_opts_nr; id++) {
1276 const struct bch_option *opt = bch2_opt_table + id;
1278 if (opt->get_sb != BCH2_NO_SB_OPT) {
1279 u64 v = bch2_opt_from_sb(sb, id);
1281 prt_printf(out, "%s:", opt->attr.name);
1283 bch2_opt_to_text(out, NULL, sb, opt, v,
1284 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1290 printbuf_indent_sub(out, 2);
1294 prt_printf(out, "layout:");
1296 printbuf_indent_add(out, 2);
1297 bch2_sb_layout_to_text(out, &sb->layout);
1298 printbuf_indent_sub(out, 2);
1301 vstruct_for_each(sb, f)
1302 if (fields & (1 << le32_to_cpu(f->type))) {
1304 bch2_sb_field_to_text(out, sb, f);