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;
534 memcpy(dst->flags, src->flags, sizeof(dst->flags));
535 memcpy(dst->features, src->features, sizeof(dst->features));
536 memcpy(dst->compat, src->compat, sizeof(dst->compat));
538 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
541 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
544 src_f = bch2_sb_field_get_id(src, i);
545 dst_f = bch2_sb_field_get_id(dst, i);
547 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
548 (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
550 int ret = bch2_sb_realloc(dst_handle,
551 le32_to_cpu(dst_handle->sb->u64s) + d);
556 dst = dst_handle->sb;
557 dst_f = bch2_sb_field_get_id(dst, i);
560 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
561 src_f ? le32_to_cpu(src_f->u64s) : 0);
564 memcpy(dst_f, src_f, vstruct_bytes(src_f));
570 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
574 lockdep_assert_held(&c->sb_lock);
576 ret = bch2_sb_realloc(&c->disk_sb, 0) ?:
577 __copy_super(&c->disk_sb, src) ?:
578 bch2_sb_replicas_to_cpu_replicas(c) ?:
579 bch2_sb_disk_groups_to_cpu(c);
587 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
589 return __copy_super(&ca->disk_sb, c->disk_sb.sb);
592 /* read superblock: */
594 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
596 struct bch_csum csum;
600 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
601 sb->bio->bi_iter.bi_sector = offset;
602 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
604 ret = submit_bio_wait(sb->bio);
606 prt_printf(err, "IO error: %i", ret);
610 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
611 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
612 prt_printf(err, "Not a bcachefs superblock");
613 return -BCH_ERR_invalid_sb_magic;
616 ret = bch2_sb_compatible(sb->sb, err);
620 bytes = vstruct_bytes(sb->sb);
622 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
623 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
624 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
625 return -BCH_ERR_invalid_sb_too_big;
628 if (bytes > sb->buffer_size) {
629 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
635 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
636 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
637 return -BCH_ERR_invalid_sb_csum_type;
640 /* XXX: verify MACs */
641 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
642 null_nonce(), sb->sb);
644 if (bch2_crc_cmp(csum, sb->sb->csum)) {
645 prt_printf(err, "bad checksum");
646 return -BCH_ERR_invalid_sb_csum;
649 sb->seq = le64_to_cpu(sb->sb->seq);
654 static int __bch2_read_super(const char *path, struct bch_opts *opts,
655 struct bch_sb_handle *sb, bool ignore_notbchfs_msg)
657 u64 offset = opt_get(*opts, sb);
658 struct bch_sb_layout layout;
659 struct printbuf err = PRINTBUF;
660 struct printbuf err2 = PRINTBUF;
666 memset(sb, 0, sizeof(*sb));
667 sb->mode = BLK_OPEN_READ;
669 sb->holder = kmalloc(1, GFP_KERNEL);
673 sb->sb_name = kstrdup(path, 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 prt_printf(&err2, "bcachefs (%s): error reading default superblock: %s\n",
725 if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg)
726 printk(KERN_INFO "%s", err2.buf);
728 printk(KERN_ERR "%s", err2.buf);
730 printbuf_exit(&err2);
731 printbuf_reset(&err);
734 * Error reading primary superblock - read location of backup
737 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
738 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
740 * use sb buffer to read layout, since sb buffer is page aligned but
743 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
745 ret = submit_bio_wait(sb->bio);
747 prt_printf(&err, "IO error: %i", ret);
751 memcpy(&layout, sb->sb, sizeof(layout));
752 ret = validate_sb_layout(&layout, &err);
756 for (i = layout.sb_offset;
757 i < layout.sb_offset + layout.nr_superblocks; i++) {
758 offset = le64_to_cpu(*i);
760 if (offset == opt_get(*opts, sb))
763 ret = read_one_super(sb, offset, &err);
771 if (le16_to_cpu(sb->sb->block_size) << 9 <
772 bdev_logical_block_size(sb->bdev) &&
773 opt_get(*opts, direct_io)) {
775 opt_set(*opts, direct_io, false);
779 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
780 le16_to_cpu(sb->sb->block_size) << 9,
781 bdev_logical_block_size(sb->bdev));
782 ret = -BCH_ERR_block_size_too_small;
787 sb->have_layout = true;
789 ret = bch2_sb_validate(sb, &err, READ);
791 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s\n",
799 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s\n",
806 int bch2_read_super(const char *path, struct bch_opts *opts,
807 struct bch_sb_handle *sb)
809 return __bch2_read_super(path, opts, sb, false);
812 /* provide a silenced version for mount.bcachefs */
814 int bch2_read_super_silent(const char *path, struct bch_opts *opts,
815 struct bch_sb_handle *sb)
817 return __bch2_read_super(path, opts, sb, true);
820 /* write superblock: */
822 static void write_super_endio(struct bio *bio)
824 struct bch_dev *ca = bio->bi_private;
826 /* XXX: return errors directly */
828 if (bch2_dev_io_err_on(bio->bi_status, ca,
830 ? BCH_MEMBER_ERROR_write
831 : BCH_MEMBER_ERROR_read,
832 "superblock %s error: %s",
833 bio_data_dir(bio) ? "write" : "read",
834 bch2_blk_status_to_str(bio->bi_status)))
835 ca->sb_write_error = 1;
837 closure_put(&ca->fs->sb_write);
838 percpu_ref_put(&ca->io_ref);
841 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
843 struct bch_sb *sb = ca->disk_sb.sb;
844 struct bio *bio = ca->disk_sb.bio;
846 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
847 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
848 bio->bi_end_io = write_super_endio;
849 bio->bi_private = ca;
850 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
852 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
855 percpu_ref_get(&ca->io_ref);
856 closure_bio_submit(bio, &c->sb_write);
859 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
861 struct bch_sb *sb = ca->disk_sb.sb;
862 struct bio *bio = ca->disk_sb.bio;
864 sb->offset = sb->layout.sb_offset[idx];
866 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
867 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
870 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
871 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
872 bio->bi_end_io = write_super_endio;
873 bio->bi_private = ca;
874 bch2_bio_map(bio, sb,
875 roundup((size_t) vstruct_bytes(sb),
876 bdev_logical_block_size(ca->disk_sb.bdev)));
878 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
881 percpu_ref_get(&ca->io_ref);
882 closure_bio_submit(bio, &c->sb_write);
885 int bch2_write_super(struct bch_fs *c)
887 struct closure *cl = &c->sb_write;
888 struct printbuf err = PRINTBUF;
889 unsigned sb = 0, nr_wrote;
890 struct bch_devs_mask sb_written;
891 bool wrote, can_mount_without_written, can_mount_with_written;
892 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
895 trace_and_count(c, write_super, c, _RET_IP_);
897 if (c->opts.very_degraded)
898 degraded_flags |= BCH_FORCE_IF_LOST;
900 lockdep_assert_held(&c->sb_lock);
902 closure_init_stack(cl);
903 memset(&sb_written, 0, sizeof(sb_written));
905 /* Make sure we're using the new magic numbers: */
906 c->disk_sb.sb->magic = BCHFS_MAGIC;
907 c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
909 le64_add_cpu(&c->disk_sb.sb->seq, 1);
911 if (test_bit(BCH_FS_error, &c->flags))
912 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
913 if (test_bit(BCH_FS_topology_error, &c->flags))
914 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
916 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
918 bch2_sb_counters_from_cpu(c);
919 bch2_sb_members_from_cpu(c);
920 bch2_sb_members_cpy_v2_v1(&c->disk_sb);
921 bch2_sb_errors_from_cpu(c);
923 for_each_online_member(c, ca)
924 bch2_sb_from_fs(c, ca);
926 for_each_online_member(c, ca) {
927 printbuf_reset(&err);
929 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
931 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
932 percpu_ref_put(&ca->io_ref);
937 if (c->opts.nochanges)
941 * Defer writing the superblock until filesystem initialization is
942 * complete - don't write out a partly initialized superblock:
944 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
947 for_each_online_member(c, ca) {
948 __set_bit(ca->dev_idx, sb_written.d);
949 ca->sb_write_error = 0;
952 for_each_online_member(c, ca)
953 read_back_super(c, ca);
956 for_each_online_member(c, ca) {
957 if (ca->sb_write_error)
960 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
961 bch2_fs_fatal_error(c,
962 "Superblock write was silently dropped! (seq %llu expected %llu)",
963 le64_to_cpu(ca->sb_read_scratch->seq),
965 percpu_ref_put(&ca->io_ref);
966 ret = -BCH_ERR_erofs_sb_err;
970 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
971 bch2_fs_fatal_error(c,
972 "Superblock modified by another process (seq %llu expected %llu)",
973 le64_to_cpu(ca->sb_read_scratch->seq),
975 percpu_ref_put(&ca->io_ref);
976 ret = -BCH_ERR_erofs_sb_err;
983 for_each_online_member(c, ca)
984 if (!ca->sb_write_error &&
985 sb < ca->disk_sb.sb->layout.nr_superblocks) {
986 write_one_super(c, ca, sb);
993 for_each_online_member(c, ca) {
994 if (ca->sb_write_error)
995 __clear_bit(ca->dev_idx, sb_written.d);
997 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
1000 nr_wrote = dev_mask_nr(&sb_written);
1002 can_mount_with_written =
1003 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1005 for (unsigned i = 0; i < ARRAY_SIZE(sb_written.d); i++)
1006 sb_written.d[i] = ~sb_written.d[i];
1008 can_mount_without_written =
1009 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1012 * If we would be able to mount _without_ the devices we successfully
1013 * wrote superblocks to, we weren't able to write to enough devices:
1015 * Exception: if we can mount without the successes because we haven't
1016 * written anything (new filesystem), we continue if we'd be able to
1017 * mount with the devices we did successfully write to:
1019 if (bch2_fs_fatal_err_on(!nr_wrote ||
1020 !can_mount_with_written ||
1021 (can_mount_without_written &&
1022 !can_mount_with_written), c,
1023 "Unable to write superblock to sufficient devices (from %ps)",
1027 /* Make new options visible after they're persistent: */
1029 printbuf_exit(&err);
1033 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1035 mutex_lock(&c->sb_lock);
1036 if (!(c->sb.features & (1ULL << feat))) {
1037 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1039 bch2_write_super(c);
1041 mutex_unlock(&c->sb_lock);
1044 /* Downgrade if superblock is at a higher version than currently supported: */
1045 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1047 lockdep_assert_held(&c->sb_lock);
1050 * Downgrade, if superblock is at a higher version than currently
1053 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1054 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1055 if (c->sb.version > bcachefs_metadata_version_current)
1056 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1057 if (c->sb.version_min > bcachefs_metadata_version_current)
1058 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1059 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1062 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1064 lockdep_assert_held(&c->sb_lock);
1066 c->disk_sb.sb->version = cpu_to_le16(new_version);
1067 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1070 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1072 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1077 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1079 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1081 return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1082 ? bch2_sb_field_ops[type]
1083 : &bch2_sb_field_null_ops;
1086 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1087 struct printbuf *err)
1089 unsigned type = le32_to_cpu(f->type);
1090 struct printbuf field_err = PRINTBUF;
1091 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1094 ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1096 prt_printf(err, "Invalid superblock section %s: %s",
1097 bch2_sb_fields[type], field_err.buf);
1099 bch2_sb_field_to_text(err, sb, f);
1102 printbuf_exit(&field_err);
1106 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1107 struct bch_sb_field *f)
1109 unsigned type = le32_to_cpu(f->type);
1110 const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1112 if (!out->nr_tabstops)
1113 printbuf_tabstop_push(out, 32);
1115 if (type < BCH_SB_FIELD_NR)
1116 prt_printf(out, "%s", bch2_sb_fields[type]);
1118 prt_printf(out, "(unknown field %u)", type);
1120 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1124 printbuf_indent_add(out, 2);
1125 ops->to_text(out, sb, f);
1126 printbuf_indent_sub(out, 2);
1130 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1134 prt_printf(out, "Type: %u", l->layout_type);
1137 prt_str(out, "Superblock max size: ");
1138 prt_units_u64(out, 512 << l->sb_max_size_bits);
1141 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1144 prt_str(out, "Offsets: ");
1145 for (i = 0; i < l->nr_superblocks; i++) {
1148 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1153 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1154 bool print_layout, unsigned fields)
1156 u64 fields_have = 0;
1157 unsigned nr_devices = 0;
1159 if (!out->nr_tabstops)
1160 printbuf_tabstop_push(out, 44);
1162 for (int i = 0; i < sb->nr_devices; i++)
1163 nr_devices += bch2_dev_exists(sb, i);
1165 prt_printf(out, "External UUID:");
1167 pr_uuid(out, sb->user_uuid.b);
1170 prt_printf(out, "Internal UUID:");
1172 pr_uuid(out, sb->uuid.b);
1175 prt_str(out, "Device index:");
1177 prt_printf(out, "%u", sb->dev_idx);
1180 prt_str(out, "Label:");
1182 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1185 prt_str(out, "Version:");
1187 bch2_version_to_text(out, le16_to_cpu(sb->version));
1190 prt_str(out, "Version upgrade complete:");
1192 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1195 prt_printf(out, "Oldest version on disk:");
1197 bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1200 prt_printf(out, "Created:");
1202 if (sb->time_base_lo)
1203 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1205 prt_printf(out, "(not set)");
1208 prt_printf(out, "Sequence number:");
1210 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1213 prt_printf(out, "Superblock size:");
1215 prt_printf(out, "%zu", vstruct_bytes(sb));
1218 prt_printf(out, "Clean:");
1220 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1223 prt_printf(out, "Devices:");
1225 prt_printf(out, "%u", nr_devices);
1228 prt_printf(out, "Sections:");
1229 vstruct_for_each(sb, f)
1230 fields_have |= 1 << le32_to_cpu(f->type);
1232 prt_bitflags(out, bch2_sb_fields, fields_have);
1235 prt_printf(out, "Features:");
1237 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1240 prt_printf(out, "Compat features:");
1242 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1246 prt_printf(out, "Options:");
1248 printbuf_indent_add(out, 2);
1252 for (id = 0; id < bch2_opts_nr; id++) {
1253 const struct bch_option *opt = bch2_opt_table + id;
1255 if (opt->get_sb != BCH2_NO_SB_OPT) {
1256 u64 v = bch2_opt_from_sb(sb, id);
1258 prt_printf(out, "%s:", opt->attr.name);
1260 bch2_opt_to_text(out, NULL, sb, opt, v,
1261 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1267 printbuf_indent_sub(out, 2);
1271 prt_printf(out, "layout:");
1273 printbuf_indent_add(out, 2);
1274 bch2_sb_layout_to_text(out, &sb->layout);
1275 printbuf_indent_sub(out, 2);
1278 vstruct_for_each(sb, f)
1279 if (fields & (1 << le32_to_cpu(f->type))) {
1281 bch2_sb_field_to_text(out, sb, f);