1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_update_interior.h"
7 #include "disk_groups.h"
12 #include "journal_io.h"
13 #include "journal_sb.h"
14 #include "journal_seq_blacklist.h"
22 #include <linux/backing-dev.h>
23 #include <linux/pretty-printers.h>
24 #include <linux/sort.h>
26 #include <trace/events/bcachefs.h>
28 const char * const bch2_sb_fields[] = {
29 #define x(name, nr) #name,
35 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
38 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
39 enum bch_sb_field_type type)
41 struct bch_sb_field *f;
43 /* XXX: need locking around superblock to access optional fields */
45 vstruct_for_each(sb, f)
46 if (le32_to_cpu(f->type) == type)
51 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
52 struct bch_sb_field *f,
55 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
56 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
58 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
63 f = vstruct_last(sb->sb);
64 memset(f, 0, sizeof(u64) * u64s);
65 f->u64s = cpu_to_le32(u64s);
73 f->u64s = cpu_to_le32(u64s);
79 memmove(dst, src, vstruct_end(sb->sb) - src);
82 memset(src, 0, dst - src);
85 sb->sb->u64s = cpu_to_le32(sb_u64s);
87 return u64s ? f : NULL;
90 void bch2_sb_field_delete(struct bch_sb_handle *sb,
91 enum bch_sb_field_type type)
93 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
96 __bch2_sb_field_resize(sb, f, 0);
99 /* Superblock realloc/free: */
101 void bch2_free_super(struct bch_sb_handle *sb)
105 if (!IS_ERR_OR_NULL(sb->bdev))
106 blkdev_put(sb->bdev, sb->mode);
109 memset(sb, 0, sizeof(*sb));
112 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
114 size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
115 size_t new_buffer_size;
116 struct bch_sb *new_sb;
120 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
122 new_buffer_size = roundup_pow_of_two(new_bytes);
124 if (sb->sb && sb->buffer_size >= new_buffer_size)
127 if (sb->have_layout) {
128 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
130 if (new_bytes > max_bytes) {
131 pr_err("%pg: superblock too big: want %zu but have %llu",
132 sb->bdev, new_bytes, max_bytes);
133 return -BCH_ERR_ENOSPC_sb;
137 if (sb->buffer_size >= new_buffer_size && sb->sb)
140 if (dynamic_fault("bcachefs:add:super_realloc"))
144 unsigned nr_bvecs = DIV_ROUND_UP(new_buffer_size, PAGE_SIZE);
146 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
150 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
157 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
162 sb->buffer_size = new_buffer_size;
167 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
168 enum bch_sb_field_type type,
171 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
172 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
173 ssize_t d = -old_u64s + u64s;
175 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
179 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
183 lockdep_assert_held(&c->sb_lock);
185 /* XXX: we're not checking that offline device have enough space */
187 for_each_online_member(ca, c, i) {
188 struct bch_sb_handle *sb = &ca->disk_sb;
190 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
191 percpu_ref_put(&ca->ref);
197 f = bch2_sb_field_get(sb->sb, type);
198 f = __bch2_sb_field_resize(sb, f, u64s);
200 f->type = cpu_to_le32(type);
204 /* Superblock validate: */
206 static inline void __bch2_sb_layout_size_assert(void)
208 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
211 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
213 u64 offset, prev_offset, max_sectors;
216 if (uuid_le_cmp(layout->magic, BCACHE_MAGIC)) {
217 prt_printf(out, "Not a bcachefs superblock layout");
221 if (layout->layout_type != 0) {
222 prt_printf(out, "Invalid superblock layout type %u",
223 layout->layout_type);
227 if (!layout->nr_superblocks) {
228 prt_printf(out, "Invalid superblock layout: no superblocks");
232 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
233 prt_printf(out, "Invalid superblock layout: too many superblocks");
237 max_sectors = 1 << layout->sb_max_size_bits;
239 prev_offset = le64_to_cpu(layout->sb_offset[0]);
241 for (i = 1; i < layout->nr_superblocks; i++) {
242 offset = le64_to_cpu(layout->sb_offset[i]);
244 if (offset < prev_offset + max_sectors) {
245 prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
246 " (sb %u ends at %llu next starts at %llu",
247 i - 1, prev_offset + max_sectors, offset);
250 prev_offset = offset;
256 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
259 struct bch_sb *sb = disk_sb->sb;
260 struct bch_sb_field *f;
261 struct bch_sb_field_members *mi;
262 enum bch_opt_id opt_id;
263 u32 version, version_min;
267 version = le16_to_cpu(sb->version);
268 version_min = version >= bcachefs_metadata_version_bkey_renumber
269 ? le16_to_cpu(sb->version_min)
272 if (version >= bcachefs_metadata_version_max) {
273 prt_printf(out, "Unsupported superblock version %u (min %u, max %u)",
274 version, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
278 if (version_min < bcachefs_metadata_version_min) {
279 prt_printf(out, "Unsupported superblock version %u (min %u, max %u)",
280 version_min, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
284 if (version_min > version) {
285 prt_printf(out, "Bad minimum version %u, greater than version field %u",
286 version_min, version);
290 if (sb->features[1] ||
291 (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
292 prt_printf(out, "Filesystem has incompatible features");
296 block_size = le16_to_cpu(sb->block_size);
298 if (block_size > PAGE_SECTORS) {
299 prt_printf(out, "Block size too big (got %u, max %u)",
300 block_size, PAGE_SECTORS);
304 if (bch2_is_zero(sb->user_uuid.b, sizeof(uuid_le))) {
305 prt_printf(out, "Bad user UUID (got zeroes)");
309 if (bch2_is_zero(sb->uuid.b, sizeof(uuid_le))) {
310 prt_printf(out, "Bad intenal UUID (got zeroes)");
314 if (!sb->nr_devices ||
315 sb->nr_devices > BCH_SB_MEMBERS_MAX) {
316 prt_printf(out, "Bad number of member devices %u (max %u)",
317 sb->nr_devices, BCH_SB_MEMBERS_MAX);
321 if (sb->dev_idx >= sb->nr_devices) {
322 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
323 sb->dev_idx, sb->nr_devices);
327 if (!sb->time_precision ||
328 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
329 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
330 le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
336 * Been seeing a bug where these are getting inexplicably
337 * zeroed, so we'r now validating them, but we have to be
338 * careful not to preven people's filesystems from mounting:
340 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
341 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
342 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
343 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
346 for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
347 const struct bch_option *opt = bch2_opt_table + opt_id;
349 if (opt->get_sb != BCH2_NO_SB_OPT) {
350 u64 v = bch2_opt_from_sb(sb, opt_id);
352 prt_printf(out, "Invalid option ");
353 ret = bch2_opt_validate(opt, v, out);
361 /* validate layout */
362 ret = validate_sb_layout(&sb->layout, out);
366 vstruct_for_each(sb, f) {
368 prt_printf(out, "Invalid superblock: optional with size 0 (type %u)",
369 le32_to_cpu(f->type));
373 if (vstruct_next(f) > vstruct_last(sb)) {
374 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
375 le32_to_cpu(f->type));
380 /* members must be validated first: */
381 mi = bch2_sb_get_members(sb);
383 prt_printf(out, "Invalid superblock: member info area missing");
387 ret = bch2_sb_field_validate(sb, &mi->field, out);
391 vstruct_for_each(sb, f) {
392 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
395 ret = bch2_sb_field_validate(sb, f, out);
405 static void bch2_sb_update(struct bch_fs *c)
407 struct bch_sb *src = c->disk_sb.sb;
408 struct bch_sb_field_members *mi = bch2_sb_get_members(src);
412 lockdep_assert_held(&c->sb_lock);
414 c->sb.uuid = src->uuid;
415 c->sb.user_uuid = src->user_uuid;
416 c->sb.version = le16_to_cpu(src->version);
417 c->sb.version_min = le16_to_cpu(src->version_min);
418 c->sb.nr_devices = src->nr_devices;
419 c->sb.clean = BCH_SB_CLEAN(src);
420 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
422 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
423 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
425 /* XXX this is wrong, we need a 96 or 128 bit integer type */
426 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo),
427 c->sb.nsec_per_time_unit);
428 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
430 c->sb.features = le64_to_cpu(src->features[0]);
431 c->sb.compat = le64_to_cpu(src->compat[0]);
433 for_each_member_device(ca, c, i)
434 ca->mi = bch2_mi_to_cpu(mi->members + i);
437 static void __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
439 struct bch_sb_field *src_f, *dst_f;
440 struct bch_sb *dst = dst_handle->sb;
443 dst->version = src->version;
444 dst->version_min = src->version_min;
446 dst->uuid = src->uuid;
447 dst->user_uuid = src->user_uuid;
448 memcpy(dst->label, src->label, sizeof(dst->label));
450 dst->block_size = src->block_size;
451 dst->nr_devices = src->nr_devices;
453 dst->time_base_lo = src->time_base_lo;
454 dst->time_base_hi = src->time_base_hi;
455 dst->time_precision = src->time_precision;
457 memcpy(dst->flags, src->flags, sizeof(dst->flags));
458 memcpy(dst->features, src->features, sizeof(dst->features));
459 memcpy(dst->compat, src->compat, sizeof(dst->compat));
461 for (i = 0; i < BCH_SB_FIELD_NR; i++) {
462 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
465 src_f = bch2_sb_field_get(src, i);
466 dst_f = bch2_sb_field_get(dst, i);
467 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
468 src_f ? le32_to_cpu(src_f->u64s) : 0);
471 memcpy(dst_f, src_f, vstruct_bytes(src_f));
475 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
477 struct bch_sb_field_journal *journal_buckets =
478 bch2_sb_get_journal(src);
479 unsigned journal_u64s = journal_buckets
480 ? le32_to_cpu(journal_buckets->field.u64s)
484 lockdep_assert_held(&c->sb_lock);
486 ret = bch2_sb_realloc(&c->disk_sb,
487 le32_to_cpu(src->u64s) - journal_u64s);
491 __copy_super(&c->disk_sb, src);
493 ret = bch2_sb_replicas_to_cpu_replicas(c);
497 ret = bch2_sb_disk_groups_to_cpu(c);
505 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
507 struct bch_sb *src = c->disk_sb.sb, *dst = ca->disk_sb.sb;
508 struct bch_sb_field_journal *journal_buckets =
509 bch2_sb_get_journal(dst);
510 unsigned journal_u64s = journal_buckets
511 ? le32_to_cpu(journal_buckets->field.u64s)
513 unsigned u64s = le32_to_cpu(src->u64s) + journal_u64s;
516 ret = bch2_sb_realloc(&ca->disk_sb, u64s);
520 __copy_super(&ca->disk_sb, src);
524 /* read superblock: */
526 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
528 struct bch_csum csum;
529 u32 version, version_min;
533 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
534 sb->bio->bi_iter.bi_sector = offset;
535 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
537 ret = submit_bio_wait(sb->bio);
539 prt_printf(err, "IO error: %i", ret);
543 if (uuid_le_cmp(sb->sb->magic, BCACHE_MAGIC)) {
544 prt_printf(err, "Not a bcachefs superblock");
548 version = le16_to_cpu(sb->sb->version);
549 version_min = version >= bcachefs_metadata_version_bkey_renumber
550 ? le16_to_cpu(sb->sb->version_min)
553 if (version >= bcachefs_metadata_version_max) {
554 prt_printf(err, "Unsupported superblock version %u (min %u, max %u)",
555 version, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
559 if (version_min < bcachefs_metadata_version_min) {
560 prt_printf(err, "Unsupported superblock version %u (min %u, max %u)",
561 version_min, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
565 bytes = vstruct_bytes(sb->sb);
567 if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
568 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
569 bytes, 512UL << sb->sb->layout.sb_max_size_bits);
573 if (bytes > sb->buffer_size) {
574 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s)))
579 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
580 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
584 /* XXX: verify MACs */
585 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
586 null_nonce(), sb->sb);
588 if (bch2_crc_cmp(csum, sb->sb->csum)) {
589 prt_printf(err, "bad checksum");
593 sb->seq = le64_to_cpu(sb->sb->seq);
598 int bch2_read_super(const char *path, struct bch_opts *opts,
599 struct bch_sb_handle *sb)
601 u64 offset = opt_get(*opts, sb);
602 struct bch_sb_layout layout;
603 struct printbuf err = PRINTBUF;
607 pr_verbose_init(*opts, "");
609 memset(sb, 0, sizeof(*sb));
610 sb->mode = FMODE_READ;
613 if (!opt_get(*opts, noexcl))
614 sb->mode |= FMODE_EXCL;
616 if (!opt_get(*opts, nochanges))
617 sb->mode |= FMODE_WRITE;
619 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
620 if (IS_ERR(sb->bdev) &&
621 PTR_ERR(sb->bdev) == -EACCES &&
622 opt_get(*opts, read_only)) {
623 sb->mode &= ~FMODE_WRITE;
625 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
626 if (!IS_ERR(sb->bdev))
627 opt_set(*opts, nochanges, true);
630 if (IS_ERR(sb->bdev)) {
631 ret = PTR_ERR(sb->bdev);
635 ret = bch2_sb_realloc(sb, 0);
637 prt_printf(&err, "error allocating memory for superblock");
641 if (bch2_fs_init_fault("read_super")) {
642 prt_printf(&err, "dynamic fault");
647 ret = read_one_super(sb, offset, &err);
651 if (opt_defined(*opts, sb))
654 printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
656 printbuf_reset(&err);
659 * Error reading primary superblock - read location of backup
662 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
663 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
665 * use sb buffer to read layout, since sb buffer is page aligned but
668 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
670 ret = submit_bio_wait(sb->bio);
672 prt_printf(&err, "IO error: %i", ret);
676 memcpy(&layout, sb->sb, sizeof(layout));
677 ret = validate_sb_layout(&layout, &err);
681 for (i = layout.sb_offset;
682 i < layout.sb_offset + layout.nr_superblocks; i++) {
683 offset = le64_to_cpu(*i);
685 if (offset == opt_get(*opts, sb))
688 ret = read_one_super(sb, offset, &err);
696 if (le16_to_cpu(sb->sb->block_size) << 9 <
697 bdev_logical_block_size(sb->bdev)) {
698 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
699 le16_to_cpu(sb->sb->block_size) << 9,
700 bdev_logical_block_size(sb->bdev));
706 sb->have_layout = true;
708 ret = bch2_sb_validate(sb, &err, READ);
710 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
715 pr_verbose_init(*opts, "ret %i", ret);
719 printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
726 /* write superblock: */
728 static void write_super_endio(struct bio *bio)
730 struct bch_dev *ca = bio->bi_private;
732 /* XXX: return errors directly */
734 if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
735 bch2_blk_status_to_str(bio->bi_status)))
736 ca->sb_write_error = 1;
738 closure_put(&ca->fs->sb_write);
739 percpu_ref_put(&ca->io_ref);
742 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
744 struct bch_sb *sb = ca->disk_sb.sb;
745 struct bio *bio = ca->disk_sb.bio;
747 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
748 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]);
749 bio->bi_end_io = write_super_endio;
750 bio->bi_private = ca;
751 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
753 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
756 percpu_ref_get(&ca->io_ref);
757 closure_bio_submit(bio, &c->sb_write);
760 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
762 struct bch_sb *sb = ca->disk_sb.sb;
763 struct bio *bio = ca->disk_sb.bio;
765 sb->offset = sb->layout.sb_offset[idx];
767 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
768 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
771 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
772 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
773 bio->bi_end_io = write_super_endio;
774 bio->bi_private = ca;
775 bch2_bio_map(bio, sb,
776 roundup((size_t) vstruct_bytes(sb),
777 bdev_logical_block_size(ca->disk_sb.bdev)));
779 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
782 percpu_ref_get(&ca->io_ref);
783 closure_bio_submit(bio, &c->sb_write);
786 int bch2_write_super(struct bch_fs *c)
788 struct closure *cl = &c->sb_write;
790 struct printbuf err = PRINTBUF;
791 unsigned i, sb = 0, nr_wrote;
792 struct bch_devs_mask sb_written;
793 bool wrote, can_mount_without_written, can_mount_with_written;
794 unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
797 trace_and_count(c, write_super, c, _RET_IP_);
799 if (c->opts.very_degraded)
800 degraded_flags |= BCH_FORCE_IF_LOST;
802 lockdep_assert_held(&c->sb_lock);
804 closure_init_stack(cl);
805 memset(&sb_written, 0, sizeof(sb_written));
807 le64_add_cpu(&c->disk_sb.sb->seq, 1);
809 if (test_bit(BCH_FS_ERROR, &c->flags))
810 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
811 if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
812 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
814 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
816 bch2_sb_counters_from_cpu(c);
818 for_each_online_member(ca, c, i)
819 bch2_sb_from_fs(c, ca);
821 for_each_online_member(ca, c, i) {
822 printbuf_reset(&err);
824 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
826 bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
827 percpu_ref_put(&ca->io_ref);
832 if (c->opts.nochanges)
836 * Defer writing the superblock until filesystem initialization is
837 * complete - don't write out a partly initialized superblock:
839 if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
842 for_each_online_member(ca, c, i) {
843 __set_bit(ca->dev_idx, sb_written.d);
844 ca->sb_write_error = 0;
847 for_each_online_member(ca, c, i)
848 read_back_super(c, ca);
851 for_each_online_member(ca, c, i) {
852 if (ca->sb_write_error)
855 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
856 bch2_fs_fatal_error(c,
857 "Superblock write was silently dropped! (seq %llu expected %llu)",
858 le64_to_cpu(ca->sb_read_scratch->seq),
860 percpu_ref_put(&ca->io_ref);
865 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
866 bch2_fs_fatal_error(c,
867 "Superblock modified by another process (seq %llu expected %llu)",
868 le64_to_cpu(ca->sb_read_scratch->seq),
870 percpu_ref_put(&ca->io_ref);
878 for_each_online_member(ca, c, i)
879 if (!ca->sb_write_error &&
880 sb < ca->disk_sb.sb->layout.nr_superblocks) {
881 write_one_super(c, ca, sb);
888 for_each_online_member(ca, c, i) {
889 if (ca->sb_write_error)
890 __clear_bit(ca->dev_idx, sb_written.d);
892 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
895 nr_wrote = dev_mask_nr(&sb_written);
897 can_mount_with_written =
898 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
900 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
901 sb_written.d[i] = ~sb_written.d[i];
903 can_mount_without_written =
904 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
907 * If we would be able to mount _without_ the devices we successfully
908 * wrote superblocks to, we weren't able to write to enough devices:
910 * Exception: if we can mount without the successes because we haven't
911 * written anything (new filesystem), we continue if we'd be able to
912 * mount with the devices we did successfully write to:
914 if (bch2_fs_fatal_err_on(!nr_wrote ||
915 !can_mount_with_written ||
916 (can_mount_without_written &&
917 !can_mount_with_written), c,
918 "Unable to write superblock to sufficient devices (from %ps)",
922 /* Make new options visible after they're persistent: */
928 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
930 mutex_lock(&c->sb_lock);
931 if (!(c->sb.features & (1ULL << feat))) {
932 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
936 mutex_unlock(&c->sb_lock);
939 /* BCH_SB_FIELD_members: */
941 static int bch2_sb_members_validate(struct bch_sb *sb,
942 struct bch_sb_field *f,
943 struct printbuf *err)
945 struct bch_sb_field_members *mi = field_to_type(f, members);
948 if ((void *) (mi->members + sb->nr_devices) >
949 vstruct_end(&mi->field)) {
950 prt_printf(err, "too many devices for section size");
954 for (i = 0; i < sb->nr_devices; i++) {
955 struct bch_member *m = mi->members + i;
957 if (!bch2_member_exists(m))
960 if (le64_to_cpu(m->nbuckets) > LONG_MAX) {
961 prt_printf(err, "device %u: too many buckets (got %llu, max %lu)",
962 i, le64_to_cpu(m->nbuckets), LONG_MAX);
966 if (le64_to_cpu(m->nbuckets) -
967 le16_to_cpu(m->first_bucket) < BCH_MIN_NR_NBUCKETS) {
968 prt_printf(err, "device %u: not enough buckets (got %llu, max %u)",
969 i, le64_to_cpu(m->nbuckets), BCH_MIN_NR_NBUCKETS);
973 if (le16_to_cpu(m->bucket_size) <
974 le16_to_cpu(sb->block_size)) {
975 prt_printf(err, "device %u: bucket size %u smaller than block size %u",
976 i, le16_to_cpu(m->bucket_size), le16_to_cpu(sb->block_size));
980 if (le16_to_cpu(m->bucket_size) <
981 BCH_SB_BTREE_NODE_SIZE(sb)) {
982 prt_printf(err, "device %u: bucket size %u smaller than btree node size %llu",
983 i, le16_to_cpu(m->bucket_size), BCH_SB_BTREE_NODE_SIZE(sb));
991 static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb,
992 struct bch_sb_field *f)
994 struct bch_sb_field_members *mi = field_to_type(f, members);
995 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
998 for (i = 0; i < sb->nr_devices; i++) {
999 struct bch_member *m = mi->members + i;
1000 unsigned data_have = bch2_sb_dev_has_data(sb, i);
1001 u64 bucket_size = le16_to_cpu(m->bucket_size);
1002 u64 device_size = le64_to_cpu(m->nbuckets) * bucket_size;
1004 if (!bch2_member_exists(m))
1007 prt_printf(out, "Device:");
1009 prt_printf(out, "%u", i);
1012 printbuf_indent_add(out, 2);
1014 prt_printf(out, "UUID:");
1016 pr_uuid(out, m->uuid.b);
1019 prt_printf(out, "Size:");
1021 prt_units_u64(out, device_size << 9);
1024 prt_printf(out, "Bucket size:");
1026 prt_units_u64(out, bucket_size << 9);
1029 prt_printf(out, "First bucket:");
1031 prt_printf(out, "%u", le16_to_cpu(m->first_bucket));
1034 prt_printf(out, "Buckets:");
1036 prt_printf(out, "%llu", le64_to_cpu(m->nbuckets));
1039 prt_printf(out, "Last mount:");
1042 pr_time(out, le64_to_cpu(m->last_mount));
1044 prt_printf(out, "(never)");
1047 prt_printf(out, "State:");
1049 prt_printf(out, "%s",
1050 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
1051 ? bch2_member_states[BCH_MEMBER_STATE(m)]
1055 prt_printf(out, "Label:");
1057 if (BCH_MEMBER_GROUP(m)) {
1058 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
1060 if (idx < disk_groups_nr(gi))
1061 prt_printf(out, "%s (%u)",
1062 gi->entries[idx].label, idx);
1064 prt_printf(out, "(bad disk labels section)");
1066 prt_printf(out, "(none)");
1070 prt_printf(out, "Data allowed:");
1072 if (BCH_MEMBER_DATA_ALLOWED(m))
1073 prt_bitflags(out, bch2_data_types, BCH_MEMBER_DATA_ALLOWED(m));
1075 prt_printf(out, "(none)");
1078 prt_printf(out, "Has data:");
1081 prt_bitflags(out, bch2_data_types, data_have);
1083 prt_printf(out, "(none)");
1086 prt_printf(out, "Discard:");
1088 prt_printf(out, "%llu", BCH_MEMBER_DISCARD(m));
1091 prt_printf(out, "Freespace initialized:");
1093 prt_printf(out, "%llu", BCH_MEMBER_FREESPACE_INITIALIZED(m));
1096 printbuf_indent_sub(out, 2);
1100 static const struct bch_sb_field_ops bch_sb_field_ops_members = {
1101 .validate = bch2_sb_members_validate,
1102 .to_text = bch2_sb_members_to_text,
1105 /* BCH_SB_FIELD_crypt: */
1107 static int bch2_sb_crypt_validate(struct bch_sb *sb,
1108 struct bch_sb_field *f,
1109 struct printbuf *err)
1111 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1113 if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
1114 prt_printf(err, "wrong size (got %zu should be %zu)",
1115 vstruct_bytes(&crypt->field), sizeof(*crypt));
1119 if (BCH_CRYPT_KDF_TYPE(crypt)) {
1120 prt_printf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
1127 static void bch2_sb_crypt_to_text(struct printbuf *out, struct bch_sb *sb,
1128 struct bch_sb_field *f)
1130 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1132 prt_printf(out, "KFD: %llu", BCH_CRYPT_KDF_TYPE(crypt));
1134 prt_printf(out, "scrypt n: %llu", BCH_KDF_SCRYPT_N(crypt));
1136 prt_printf(out, "scrypt r: %llu", BCH_KDF_SCRYPT_R(crypt));
1138 prt_printf(out, "scrypt p: %llu", BCH_KDF_SCRYPT_P(crypt));
1142 static const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
1143 .validate = bch2_sb_crypt_validate,
1144 .to_text = bch2_sb_crypt_to_text,
1147 /* BCH_SB_FIELD_clean: */
1149 int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *clean, int write)
1151 struct jset_entry *entry;
1154 for (entry = clean->start;
1155 entry < (struct jset_entry *) vstruct_end(&clean->field);
1156 entry = vstruct_next(entry)) {
1157 ret = bch2_journal_entry_validate(c, NULL, entry,
1158 le16_to_cpu(c->disk_sb.sb->version),
1159 BCH_SB_BIG_ENDIAN(c->disk_sb.sb),
1168 int bch2_fs_mark_dirty(struct bch_fs *c)
1173 * Unconditionally write superblock, to verify it hasn't changed before
1177 mutex_lock(&c->sb_lock);
1178 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1179 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
1180 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1181 ret = bch2_write_super(c);
1182 mutex_unlock(&c->sb_lock);
1187 static struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
1189 struct jset_entry *entry = *end;
1190 unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
1192 memset(entry, 0, u64s * sizeof(u64));
1194 * The u64s field counts from the start of data, ignoring the shared
1197 entry->u64s = cpu_to_le16(u64s - 1);
1199 *end = vstruct_next(*end);
1203 void bch2_journal_super_entries_add_common(struct bch_fs *c,
1204 struct jset_entry **end,
1210 percpu_down_read(&c->mark_lock);
1213 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1214 bch2_fs_usage_acc_to_base(c, i);
1216 bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK);
1220 struct jset_entry_usage *u =
1221 container_of(jset_entry_init(end, sizeof(*u)),
1222 struct jset_entry_usage, entry);
1224 u->entry.type = BCH_JSET_ENTRY_usage;
1225 u->entry.btree_id = BCH_FS_USAGE_inodes;
1226 u->v = cpu_to_le64(c->usage_base->nr_inodes);
1230 struct jset_entry_usage *u =
1231 container_of(jset_entry_init(end, sizeof(*u)),
1232 struct jset_entry_usage, entry);
1234 u->entry.type = BCH_JSET_ENTRY_usage;
1235 u->entry.btree_id = BCH_FS_USAGE_key_version;
1236 u->v = cpu_to_le64(atomic64_read(&c->key_version));
1239 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1240 struct jset_entry_usage *u =
1241 container_of(jset_entry_init(end, sizeof(*u)),
1242 struct jset_entry_usage, entry);
1244 u->entry.type = BCH_JSET_ENTRY_usage;
1245 u->entry.btree_id = BCH_FS_USAGE_reserved;
1247 u->v = cpu_to_le64(c->usage_base->persistent_reserved[i]);
1250 for (i = 0; i < c->replicas.nr; i++) {
1251 struct bch_replicas_entry *e =
1252 cpu_replicas_entry(&c->replicas, i);
1253 struct jset_entry_data_usage *u =
1254 container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs),
1255 struct jset_entry_data_usage, entry);
1257 u->entry.type = BCH_JSET_ENTRY_data_usage;
1258 u->v = cpu_to_le64(c->usage_base->replicas[i]);
1259 memcpy(&u->r, e, replicas_entry_bytes(e));
1262 for_each_member_device(ca, c, dev) {
1263 unsigned b = sizeof(struct jset_entry_dev_usage) +
1264 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR;
1265 struct jset_entry_dev_usage *u =
1266 container_of(jset_entry_init(end, b),
1267 struct jset_entry_dev_usage, entry);
1269 u->entry.type = BCH_JSET_ENTRY_dev_usage;
1270 u->dev = cpu_to_le32(dev);
1271 u->buckets_ec = cpu_to_le64(ca->usage_base->buckets_ec);
1273 for (i = 0; i < BCH_DATA_NR; i++) {
1274 u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets);
1275 u->d[i].sectors = cpu_to_le64(ca->usage_base->d[i].sectors);
1276 u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented);
1280 percpu_up_read(&c->mark_lock);
1282 for (i = 0; i < 2; i++) {
1283 struct jset_entry_clock *clock =
1284 container_of(jset_entry_init(end, sizeof(*clock)),
1285 struct jset_entry_clock, entry);
1287 clock->entry.type = BCH_JSET_ENTRY_clock;
1289 clock->time = cpu_to_le64(atomic64_read(&c->io_clock[i].now));
1293 void bch2_fs_mark_clean(struct bch_fs *c)
1295 struct bch_sb_field_clean *sb_clean;
1296 struct jset_entry *entry;
1300 mutex_lock(&c->sb_lock);
1301 if (BCH_SB_CLEAN(c->disk_sb.sb))
1304 SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
1306 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
1307 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata);
1308 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates));
1309 c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled));
1311 u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved;
1313 sb_clean = bch2_sb_resize_clean(&c->disk_sb, u64s);
1315 bch_err(c, "error resizing superblock while setting filesystem clean");
1319 sb_clean->flags = 0;
1320 sb_clean->journal_seq = cpu_to_le64(atomic64_read(&c->journal.seq));
1322 /* Trying to catch outstanding bug: */
1323 BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX);
1325 entry = sb_clean->start;
1326 bch2_journal_super_entries_add_common(c, &entry, 0);
1327 entry = bch2_btree_roots_to_journal_entries(c, entry, entry);
1328 BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
1331 vstruct_end(&sb_clean->field) - (void *) entry);
1334 * this should be in the write path, and we should be validating every
1335 * superblock section:
1337 ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE);
1339 bch_err(c, "error writing marking filesystem clean: validate error");
1343 bch2_write_super(c);
1345 mutex_unlock(&c->sb_lock);
1348 static int bch2_sb_clean_validate(struct bch_sb *sb,
1349 struct bch_sb_field *f,
1350 struct printbuf *err)
1352 struct bch_sb_field_clean *clean = field_to_type(f, clean);
1354 if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
1355 prt_printf(err, "wrong size (got %zu should be %zu)",
1356 vstruct_bytes(&clean->field), sizeof(*clean));
1363 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
1364 struct bch_sb_field *f)
1366 struct bch_sb_field_clean *clean = field_to_type(f, clean);
1367 struct jset_entry *entry;
1369 prt_printf(out, "flags: %x", le32_to_cpu(clean->flags));
1371 prt_printf(out, "journal_seq: %llu", le64_to_cpu(clean->journal_seq));
1374 for (entry = clean->start;
1375 entry != vstruct_end(&clean->field);
1376 entry = vstruct_next(entry)) {
1377 if (entry->type == BCH_JSET_ENTRY_btree_keys &&
1381 bch2_journal_entry_to_text(out, NULL, entry);
1386 static const struct bch_sb_field_ops bch_sb_field_ops_clean = {
1387 .validate = bch2_sb_clean_validate,
1388 .to_text = bch2_sb_clean_to_text,
1391 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1393 [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1398 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1399 struct printbuf *err)
1401 unsigned type = le32_to_cpu(f->type);
1402 struct printbuf field_err = PRINTBUF;
1405 if (type >= BCH_SB_FIELD_NR)
1408 ret = bch2_sb_field_ops[type]->validate(sb, f, &field_err);
1410 prt_printf(err, "Invalid superblock section %s: %s",
1411 bch2_sb_fields[type],
1414 bch2_sb_field_to_text(err, sb, f);
1417 printbuf_exit(&field_err);
1421 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1422 struct bch_sb_field *f)
1424 unsigned type = le32_to_cpu(f->type);
1425 const struct bch_sb_field_ops *ops = type < BCH_SB_FIELD_NR
1426 ? bch2_sb_field_ops[type] : NULL;
1428 if (!out->nr_tabstops)
1429 printbuf_tabstop_push(out, 32);
1432 prt_printf(out, "%s", bch2_sb_fields[type]);
1434 prt_printf(out, "(unknown field %u)", type);
1436 prt_printf(out, " (size %zu):", vstruct_bytes(f));
1439 if (ops && ops->to_text) {
1440 printbuf_indent_add(out, 2);
1441 bch2_sb_field_ops[type]->to_text(out, sb, f);
1442 printbuf_indent_sub(out, 2);
1446 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1450 prt_printf(out, "Type: %u", l->layout_type);
1453 prt_str(out, "Superblock max size: ");
1454 prt_units_u64(out, 512 << l->sb_max_size_bits);
1457 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks);
1460 prt_str(out, "Offsets: ");
1461 for (i = 0; i < l->nr_superblocks; i++) {
1464 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1469 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1470 bool print_layout, unsigned fields)
1472 struct bch_sb_field_members *mi;
1473 struct bch_sb_field *f;
1474 u64 fields_have = 0;
1475 unsigned nr_devices = 0;
1477 if (!out->nr_tabstops)
1478 printbuf_tabstop_push(out, 44);
1480 mi = bch2_sb_get_members(sb);
1482 struct bch_member *m;
1484 for (m = mi->members;
1485 m < mi->members + sb->nr_devices;
1487 nr_devices += bch2_member_exists(m);
1490 prt_printf(out, "External UUID:");
1492 pr_uuid(out, sb->user_uuid.b);
1495 prt_printf(out, "Internal UUID:");
1497 pr_uuid(out, sb->uuid.b);
1500 prt_str(out, "Device index:");
1502 prt_printf(out, "%u", sb->dev_idx);
1505 prt_str(out, "Label:");
1507 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1510 prt_str(out, "Version:");
1512 prt_printf(out, "%s", bch2_metadata_versions[le16_to_cpu(sb->version)]);
1515 prt_printf(out, "Oldest version on disk:");
1517 prt_printf(out, "%s", bch2_metadata_versions[le16_to_cpu(sb->version_min)]);
1520 prt_printf(out, "Created:");
1522 if (sb->time_base_lo)
1523 pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1525 prt_printf(out, "(not set)");
1528 prt_printf(out, "Sequence number:");
1530 prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1533 prt_printf(out, "Superblock size:");
1535 prt_printf(out, "%zu", vstruct_bytes(sb));
1538 prt_printf(out, "Clean:");
1540 prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1543 prt_printf(out, "Devices:");
1545 prt_printf(out, "%u", nr_devices);
1548 prt_printf(out, "Sections:");
1549 vstruct_for_each(sb, f)
1550 fields_have |= 1 << le32_to_cpu(f->type);
1552 prt_bitflags(out, bch2_sb_fields, fields_have);
1555 prt_printf(out, "Features:");
1557 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1560 prt_printf(out, "Compat features:");
1562 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1566 prt_printf(out, "Options:");
1568 printbuf_indent_add(out, 2);
1572 for (id = 0; id < bch2_opts_nr; id++) {
1573 const struct bch_option *opt = bch2_opt_table + id;
1575 if (opt->get_sb != BCH2_NO_SB_OPT) {
1576 u64 v = bch2_opt_from_sb(sb, id);
1578 prt_printf(out, "%s:", opt->attr.name);
1580 bch2_opt_to_text(out, NULL, sb, opt, v,
1581 OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1587 printbuf_indent_sub(out, 2);
1591 prt_printf(out, "layout:");
1593 printbuf_indent_add(out, 2);
1594 bch2_sb_layout_to_text(out, &sb->layout);
1595 printbuf_indent_sub(out, 2);
1598 vstruct_for_each(sb, f)
1599 if (fields & (1 << le32_to_cpu(f->type))) {
1601 bch2_sb_field_to_text(out, sb, f);