Lines Matching +full:out +full:-

1 // SPDX-License-Identifier: GPL-2.0
14 #include "sb-clean.h"
15 #include "sb-counters.h"
16 #include "sb-downgrade.h"
17 #include "sb-errors.h"
18 #include "sb-members.h"
19 #include "super-io.h"
24 #include <linux/backing-dev.h>
44 void bch2_version_to_text(struct printbuf *out, unsigned v) in bch2_version_to_text() argument
54 prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str); in bch2_version_to_text()
87 if (le32_to_cpu(f->type) == type) in bch2_sb_field_get_id()
96 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0; in __bch2_sb_field_resize()
97 unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s; in __bch2_sb_field_resize()
99 BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size); in __bch2_sb_field_resize()
104 f = vstruct_last(sb->sb); in __bch2_sb_field_resize()
106 f->u64s = cpu_to_le32(u64s); in __bch2_sb_field_resize()
107 f->type = 0; in __bch2_sb_field_resize()
114 f->u64s = cpu_to_le32(u64s); in __bch2_sb_field_resize()
120 memmove(dst, src, vstruct_end(sb->sb) - src); in __bch2_sb_field_resize()
123 memset(src, 0, dst - src); in __bch2_sb_field_resize()
126 sb->sb->u64s = cpu_to_le32(sb_u64s); in __bch2_sb_field_resize()
134 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type); in bch2_sb_field_delete()
144 kfree(sb->bio); in bch2_free_super()
145 if (!IS_ERR_OR_NULL(sb->s_bdev_file)) in bch2_free_super()
146 bdev_fput(sb->s_bdev_file); in bch2_free_super()
147 kfree(sb->holder); in bch2_free_super()
148 kfree(sb->sb_name); in bch2_free_super()
150 kfree(sb->sb); in bch2_free_super()
161 if (sb->bdev) in bch2_sb_realloc()
162 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev)); in bch2_sb_realloc()
166 if (sb->sb && sb->buffer_size >= new_buffer_size) in bch2_sb_realloc()
169 if (sb->sb && sb->have_layout) { in bch2_sb_realloc()
170 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits; in bch2_sb_realloc()
175 prt_bdevname(&buf, sb->bdev); in bch2_sb_realloc()
179 return -BCH_ERR_ENOSPC_sb; in bch2_sb_realloc()
183 if (sb->buffer_size >= new_buffer_size && sb->sb) in bch2_sb_realloc()
187 return -BCH_ERR_ENOMEM_sb_realloc_injected; in bch2_sb_realloc()
189 new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO); in bch2_sb_realloc()
191 return -BCH_ERR_ENOMEM_sb_buf_realloc; in bch2_sb_realloc()
193 sb->sb = new_sb; in bch2_sb_realloc()
195 if (sb->have_bio) { in bch2_sb_realloc()
196 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size); in bch2_sb_realloc()
200 return -BCH_ERR_ENOMEM_sb_bio_realloc; in bch2_sb_realloc()
202 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0); in bch2_sb_realloc()
204 kfree(sb->bio); in bch2_sb_realloc()
205 sb->bio = bio; in bch2_sb_realloc()
208 sb->buffer_size = new_buffer_size; in bch2_sb_realloc()
217 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type); in bch2_sb_field_resize_id()
218 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0; in bch2_sb_field_resize_id()
219 ssize_t d = -old_u64s + u64s; in bch2_sb_field_resize_id()
221 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) in bch2_sb_field_resize_id()
224 if (sb->fs_sb) { in bch2_sb_field_resize_id()
227 lockdep_assert_held(&c->sb_lock); in bch2_sb_field_resize_id()
232 struct bch_sb_handle *dev_sb = &ca->disk_sb; in bch2_sb_field_resize_id()
234 if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) { in bch2_sb_field_resize_id()
235 percpu_ref_put(&ca->io_ref); in bch2_sb_field_resize_id()
241 f = bch2_sb_field_get_id(sb->sb, type); in bch2_sb_field_resize_id()
244 f->type = cpu_to_le32(type); in bch2_sb_field_resize_id()
252 struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type); in bch2_sb_field_get_minsize_id()
254 if (!f || le32_to_cpu(f->u64s) < u64s) in bch2_sb_field_get_minsize_id()
261 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out) in validate_sb_layout() argument
268 if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) && in validate_sb_layout()
269 !uuid_equal(&layout->magic, &BCHFS_MAGIC)) { in validate_sb_layout()
270 prt_printf(out, "Not a bcachefs superblock layout"); in validate_sb_layout()
271 return -BCH_ERR_invalid_sb_layout; in validate_sb_layout()
274 if (layout->layout_type != 0) { in validate_sb_layout()
275 prt_printf(out, "Invalid superblock layout type %u", in validate_sb_layout()
276 layout->layout_type); in validate_sb_layout()
277 return -BCH_ERR_invalid_sb_layout_type; in validate_sb_layout()
280 if (!layout->nr_superblocks) { in validate_sb_layout()
281 prt_printf(out, "Invalid superblock layout: no superblocks"); in validate_sb_layout()
282 return -BCH_ERR_invalid_sb_layout_nr_superblocks; in validate_sb_layout()
285 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) { in validate_sb_layout()
286 prt_printf(out, "Invalid superblock layout: too many superblocks"); in validate_sb_layout()
287 return -BCH_ERR_invalid_sb_layout_nr_superblocks; in validate_sb_layout()
290 if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) { in validate_sb_layout()
291 prt_printf(out, "Invalid superblock layout: max_size_bits too high"); in validate_sb_layout()
292 return -BCH_ERR_invalid_sb_layout_sb_max_size_bits; in validate_sb_layout()
295 max_sectors = 1 << layout->sb_max_size_bits; in validate_sb_layout()
297 prev_offset = le64_to_cpu(layout->sb_offset[0]); in validate_sb_layout()
299 for (i = 1; i < layout->nr_superblocks; i++) { in validate_sb_layout()
300 offset = le64_to_cpu(layout->sb_offset[i]); in validate_sb_layout()
303 prt_printf(out, "Invalid superblock layout: superblocks overlap\n" in validate_sb_layout()
305 i - 1, prev_offset + max_sectors, offset); in validate_sb_layout()
306 return -BCH_ERR_invalid_sb_layout_superblocks_overlap; in validate_sb_layout()
314 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out) in bch2_sb_compatible() argument
316 u16 version = le16_to_cpu(sb->version); in bch2_sb_compatible()
317 u16 version_min = le16_to_cpu(sb->version_min); in bch2_sb_compatible()
320 prt_str(out, "Unsupported superblock version "); in bch2_sb_compatible()
321 bch2_version_to_text(out, version); in bch2_sb_compatible()
322 prt_str(out, " (min "); in bch2_sb_compatible()
323 bch2_version_to_text(out, bcachefs_metadata_version_min); in bch2_sb_compatible()
324 prt_str(out, ", max "); in bch2_sb_compatible()
325 bch2_version_to_text(out, bcachefs_metadata_version_current); in bch2_sb_compatible()
326 prt_str(out, ")"); in bch2_sb_compatible()
327 return -BCH_ERR_invalid_sb_version; in bch2_sb_compatible()
331 prt_str(out, "Unsupported superblock version_min "); in bch2_sb_compatible()
332 bch2_version_to_text(out, version_min); in bch2_sb_compatible()
333 prt_str(out, " (min "); in bch2_sb_compatible()
334 bch2_version_to_text(out, bcachefs_metadata_version_min); in bch2_sb_compatible()
335 prt_str(out, ", max "); in bch2_sb_compatible()
336 bch2_version_to_text(out, bcachefs_metadata_version_current); in bch2_sb_compatible()
337 prt_str(out, ")"); in bch2_sb_compatible()
338 return -BCH_ERR_invalid_sb_version; in bch2_sb_compatible()
342 prt_str(out, "Bad minimum version "); in bch2_sb_compatible()
343 bch2_version_to_text(out, version_min); in bch2_sb_compatible()
344 prt_str(out, ", greater than version field "); in bch2_sb_compatible()
345 bch2_version_to_text(out, version); in bch2_sb_compatible()
346 return -BCH_ERR_invalid_sb_version; in bch2_sb_compatible()
353 enum bch_validate_flags flags, struct printbuf *out) in bch2_sb_validate() argument
355 struct bch_sb *sb = disk_sb->sb; in bch2_sb_validate()
361 ret = bch2_sb_compatible(sb, out); in bch2_sb_validate()
365 if (sb->features[1] || in bch2_sb_validate()
366 (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) { in bch2_sb_validate()
367 prt_printf(out, "Filesystem has incompatible features"); in bch2_sb_validate()
368 return -BCH_ERR_invalid_sb_features; in bch2_sb_validate()
371 block_size = le16_to_cpu(sb->block_size); in bch2_sb_validate()
374 prt_printf(out, "Block size too big (got %u, max %u)", in bch2_sb_validate()
376 return -BCH_ERR_invalid_sb_block_size; in bch2_sb_validate()
379 if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) { in bch2_sb_validate()
380 prt_printf(out, "Bad user UUID (got zeroes)"); in bch2_sb_validate()
381 return -BCH_ERR_invalid_sb_uuid; in bch2_sb_validate()
384 if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) { in bch2_sb_validate()
385 prt_printf(out, "Bad internal UUID (got zeroes)"); in bch2_sb_validate()
386 return -BCH_ERR_invalid_sb_uuid; in bch2_sb_validate()
389 if (!sb->nr_devices || in bch2_sb_validate()
390 sb->nr_devices > BCH_SB_MEMBERS_MAX) { in bch2_sb_validate()
391 prt_printf(out, "Bad number of member devices %u (max %u)", in bch2_sb_validate()
392 sb->nr_devices, BCH_SB_MEMBERS_MAX); in bch2_sb_validate()
393 return -BCH_ERR_invalid_sb_too_many_members; in bch2_sb_validate()
396 if (sb->dev_idx >= sb->nr_devices) { in bch2_sb_validate()
397 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)", in bch2_sb_validate()
398 sb->dev_idx, sb->nr_devices); in bch2_sb_validate()
399 return -BCH_ERR_invalid_sb_dev_idx; in bch2_sb_validate()
402 if (!sb->time_precision || in bch2_sb_validate()
403 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) { in bch2_sb_validate()
404 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)", in bch2_sb_validate()
405 le32_to_cpu(sb->time_precision), NSEC_PER_SEC); in bch2_sb_validate()
406 return -BCH_ERR_invalid_sb_time_precision; in bch2_sb_validate()
421 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version)); in bch2_sb_validate()
423 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_disk_accounting_v2 && in bch2_sb_validate()
427 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_disk_accounting_v2) in bch2_sb_validate()
434 if (opt->get_sb != BCH2_NO_SB_OPT) { in bch2_sb_validate()
437 prt_printf(out, "Invalid option "); in bch2_sb_validate()
438 ret = bch2_opt_validate(opt, v, out); in bch2_sb_validate()
442 printbuf_reset(out); in bch2_sb_validate()
447 ret = validate_sb_layout(&sb->layout, out); in bch2_sb_validate()
452 if (!f->u64s) { in bch2_sb_validate()
453 prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)", in bch2_sb_validate()
454 le32_to_cpu(f->type)); in bch2_sb_validate()
455 return -BCH_ERR_invalid_sb_field_size; in bch2_sb_validate()
459 prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)", in bch2_sb_validate()
460 le32_to_cpu(f->type)); in bch2_sb_validate()
461 return -BCH_ERR_invalid_sb_field_size; in bch2_sb_validate()
468 prt_printf(out, "Invalid superblock: member info area missing"); in bch2_sb_validate()
469 return -BCH_ERR_invalid_sb_members_missing; in bch2_sb_validate()
472 ret = bch2_sb_field_validate(sb, &mi->field, flags, out); in bch2_sb_validate()
477 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members_v1) in bch2_sb_validate()
480 ret = bch2_sb_field_validate(sb, f, flags, out); in bch2_sb_validate()
486 bch2_sb_member_get(sb, sb->dev_idx).seq != sb->seq) { in bch2_sb_validate()
487 prt_printf(out, "Invalid superblock: member seq %llu != sb seq %llu", in bch2_sb_validate()
488 le64_to_cpu(bch2_sb_member_get(sb, sb->dev_idx).seq), in bch2_sb_validate()
489 le64_to_cpu(sb->seq)); in bch2_sb_validate()
490 return -BCH_ERR_invalid_sb_members_missing; in bch2_sb_validate()
507 BUG_ON(nr & (BITS_PER_TYPE(long) - 1)); in le_bitvector_to_cpu()
515 struct bch_sb *src = c->disk_sb.sb; in bch2_sb_update()
517 lockdep_assert_held(&c->sb_lock); in bch2_sb_update()
519 c->sb.uuid = src->uuid; in bch2_sb_update()
520 c->sb.user_uuid = src->user_uuid; in bch2_sb_update()
521 c->sb.version = le16_to_cpu(src->version); in bch2_sb_update()
522 c->sb.version_min = le16_to_cpu(src->version_min); in bch2_sb_update()
523 c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src); in bch2_sb_update()
524 c->sb.nr_devices = src->nr_devices; in bch2_sb_update()
525 c->sb.clean = BCH_SB_CLEAN(src); in bch2_sb_update()
526 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src); in bch2_sb_update()
528 c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision); in bch2_sb_update()
529 c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit; in bch2_sb_update()
532 c->sb.time_base_lo = div_u64(le64_to_cpu(src->time_base_lo), in bch2_sb_update()
533 c->sb.nsec_per_time_unit); in bch2_sb_update()
534 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi); in bch2_sb_update()
536 c->sb.features = le64_to_cpu(src->features[0]); in bch2_sb_update()
537 c->sb.compat = le64_to_cpu(src->compat[0]); in bch2_sb_update()
539 memset(c->sb.errors_silent, 0, sizeof(c->sb.errors_silent)); in bch2_sb_update()
543 le_bitvector_to_cpu(c->sb.errors_silent, (void *) ext->errors_silent, in bch2_sb_update()
544 sizeof(c->sb.errors_silent) * 8); in bch2_sb_update()
545 c->sb.btrees_lost_data = le64_to_cpu(ext->btrees_lost_data); in bch2_sb_update()
549 struct bch_member m = bch2_sb_member_get(src, ca->dev_idx); in bch2_sb_update()
550 ca->mi = bch2_mi_to_cpu(&m); in bch2_sb_update()
557 struct bch_sb *dst = dst_handle->sb; in __copy_super()
560 dst->version = src->version; in __copy_super()
561 dst->version_min = src->version_min; in __copy_super()
562 dst->seq = src->seq; in __copy_super()
563 dst->uuid = src->uuid; in __copy_super()
564 dst->user_uuid = src->user_uuid; in __copy_super()
565 memcpy(dst->label, src->label, sizeof(dst->label)); in __copy_super()
567 dst->block_size = src->block_size; in __copy_super()
568 dst->nr_devices = src->nr_devices; in __copy_super()
570 dst->time_base_lo = src->time_base_lo; in __copy_super()
571 dst->time_base_hi = src->time_base_hi; in __copy_super()
572 dst->time_precision = src->time_precision; in __copy_super()
573 dst->write_time = src->write_time; in __copy_super()
575 memcpy(dst->flags, src->flags, sizeof(dst->flags)); in __copy_super()
576 memcpy(dst->features, src->features, sizeof(dst->features)); in __copy_super()
577 memcpy(dst->compat, src->compat, sizeof(dst->compat)); in __copy_super()
588 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) - in __copy_super()
589 (dst_f ? le32_to_cpu(dst_f->u64s) : 0); in __copy_super()
592 le32_to_cpu(dst_handle->sb->u64s) + d); in __copy_super()
597 dst = dst_handle->sb; in __copy_super()
602 src_f ? le32_to_cpu(src_f->u64s) : 0); in __copy_super()
615 lockdep_assert_held(&c->sb_lock); in bch2_sb_to_fs()
617 ret = bch2_sb_realloc(&c->disk_sb, 0) ?: in bch2_sb_to_fs()
618 __copy_super(&c->disk_sb, src) ?: in bch2_sb_to_fs()
630 return __copy_super(&ca->disk_sb, c->disk_sb.sb); in bch2_sb_from_fs()
640 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META); in read_one_super()
641 sb->bio->bi_iter.bi_sector = offset; in read_one_super()
642 bch2_bio_map(sb->bio, sb->sb, sb->buffer_size); in read_one_super()
644 ret = submit_bio_wait(sb->bio); in read_one_super()
650 if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) && in read_one_super()
651 !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) { in read_one_super()
653 pr_uuid(err, sb->sb->magic.b); in read_one_super()
655 return -BCH_ERR_invalid_sb_magic; in read_one_super()
658 ret = bch2_sb_compatible(sb->sb, err); in read_one_super()
662 bytes = vstruct_bytes(sb->sb); in read_one_super()
664 u64 sb_size = 512ULL << min(BCH_SB_LAYOUT_SIZE_BITS_MAX, sb->sb->layout.sb_max_size_bits); in read_one_super()
668 return -BCH_ERR_invalid_sb_too_big; in read_one_super()
671 if (bytes > sb->buffer_size) { in read_one_super()
672 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s)); in read_one_super()
678 enum bch_csum_type csum_type = BCH_SB_CSUM_TYPE(sb->sb); in read_one_super()
680 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb)); in read_one_super()
681 return -BCH_ERR_invalid_sb_csum_type; in read_one_super()
685 struct bch_csum csum = csum_vstruct(NULL, csum_type, null_nonce(), sb->sb); in read_one_super()
686 if (bch2_crc_cmp(csum, sb->sb->csum)) { in read_one_super()
687 bch2_csum_err_msg(err, csum_type, sb->sb->csum, csum); in read_one_super()
688 return -BCH_ERR_invalid_sb_csum; in read_one_super()
691 sb->seq = le64_to_cpu(sb->sb->seq); in read_one_super()
709 sb->mode = BLK_OPEN_READ; in __bch2_read_super()
710 sb->have_bio = true; in __bch2_read_super()
711 sb->holder = kmalloc(1, GFP_KERNEL); in __bch2_read_super()
712 if (!sb->holder) in __bch2_read_super()
713 return -ENOMEM; in __bch2_read_super()
715 sb->sb_name = kstrdup(path, GFP_KERNEL); in __bch2_read_super()
716 if (!sb->sb_name) { in __bch2_read_super()
717 ret = -ENOMEM; in __bch2_read_super()
724 sb->mode |= BLK_OPEN_BUFFERED; in __bch2_read_super()
728 sb->mode |= BLK_OPEN_EXCL; in __bch2_read_super()
731 sb->mode |= BLK_OPEN_WRITE; in __bch2_read_super()
733 sb->s_bdev_file = bdev_file_open_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); in __bch2_read_super()
734 if (IS_ERR(sb->s_bdev_file) && in __bch2_read_super()
735 PTR_ERR(sb->s_bdev_file) == -EACCES && in __bch2_read_super()
737 sb->mode &= ~BLK_OPEN_WRITE; in __bch2_read_super()
739 sb->s_bdev_file = bdev_file_open_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); in __bch2_read_super()
740 if (!IS_ERR(sb->s_bdev_file)) in __bch2_read_super()
744 if (IS_ERR(sb->s_bdev_file)) { in __bch2_read_super()
745 ret = PTR_ERR(sb->s_bdev_file); in __bch2_read_super()
749 sb->bdev = file_bdev(sb->s_bdev_file); in __bch2_read_super()
759 ret = -EFAULT; in __bch2_read_super()
772 if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg) in __bch2_read_super()
781 * Error reading primary superblock - read location of backup in __bch2_read_super()
784 bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META); in __bch2_read_super()
785 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR; in __bch2_read_super()
790 bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout)); in __bch2_read_super()
792 ret = submit_bio_wait(sb->bio); in __bch2_read_super()
798 memcpy(&layout, sb->sb, sizeof(layout)); in __bch2_read_super()
808 ret = -BCH_ERR_invalid; in __bch2_read_super()
820 if (le16_to_cpu(sb->sb->block_size) << 9 < in __bch2_read_super()
821 bdev_logical_block_size(sb->bdev) && in __bch2_read_super()
829 le16_to_cpu(sb->sb->block_size) << 9, in __bch2_read_super()
830 bdev_logical_block_size(sb->bdev)); in __bch2_read_super()
831 ret = -BCH_ERR_block_size_too_small; in __bch2_read_super()
835 sb->have_layout = true; in __bch2_read_super()
843 out: in __bch2_read_super()
851 goto out; in __bch2_read_super()
872 struct bch_dev *ca = bio->bi_private; in write_super_endio()
876 if (bch2_dev_io_err_on(bio->bi_status, ca, in write_super_endio()
882 bch2_blk_status_to_str(bio->bi_status))) in write_super_endio()
883 ca->sb_write_error = 1; in write_super_endio()
885 closure_put(&ca->fs->sb_write); in write_super_endio()
886 percpu_ref_put(&ca->io_ref); in write_super_endio()
891 struct bch_sb *sb = ca->disk_sb.sb; in read_back_super()
892 struct bio *bio = ca->disk_sb.bio; in read_back_super()
894 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META); in read_back_super()
895 bio->bi_iter.bi_sector = le64_to_cpu(sb->layout.sb_offset[0]); in read_back_super()
896 bio->bi_end_io = write_super_endio; in read_back_super()
897 bio->bi_private = ca; in read_back_super()
898 bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE); in read_back_super()
900 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb], in read_back_super()
903 percpu_ref_get(&ca->io_ref); in read_back_super()
904 closure_bio_submit(bio, &c->sb_write); in read_back_super()
909 struct bch_sb *sb = ca->disk_sb.sb; in write_one_super()
910 struct bio *bio = ca->disk_sb.bio; in write_one_super()
912 sb->offset = sb->layout.sb_offset[idx]; in write_one_super()
914 SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false)); in write_one_super()
915 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb), in write_one_super()
918 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META); in write_one_super()
919 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset); in write_one_super()
920 bio->bi_end_io = write_super_endio; in write_one_super()
921 bio->bi_private = ca; in write_one_super()
924 bdev_logical_block_size(ca->disk_sb.bdev))); in write_one_super()
926 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb], in write_one_super()
929 percpu_ref_get(&ca->io_ref); in write_one_super()
930 closure_bio_submit(bio, &c->sb_write); in write_one_super()
935 struct closure *cl = &c->sb_write; in bch2_write_super()
946 if (c->opts.very_degraded) in bch2_write_super()
949 lockdep_assert_held(&c->sb_lock); in bch2_write_super()
957 percpu_ref_put(&ca->io_ref); in bch2_write_super()
958 goto out; in bch2_write_super()
960 percpu_ref_get(&ca->io_ref); in bch2_write_super()
964 c->disk_sb.sb->magic = BCHFS_MAGIC; in bch2_write_super()
965 c->disk_sb.sb->layout.magic = BCHFS_MAGIC; in bch2_write_super()
967 le64_add_cpu(&c->disk_sb.sb->seq, 1); in bch2_write_super()
969 struct bch_sb_field_members_v2 *mi = bch2_sb_field_get(c->disk_sb.sb, members_v2); in bch2_write_super()
971 __bch2_members_v2_get_mut(mi, (*ca)->dev_idx)->seq = c->disk_sb.sb->seq; in bch2_write_super()
972 c->disk_sb.sb->write_time = cpu_to_le64(ktime_get_real_seconds()); in bch2_write_super()
974 if (test_bit(BCH_FS_error, &c->flags)) in bch2_write_super()
975 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1); in bch2_write_super()
976 if (test_bit(BCH_FS_topology_error, &c->flags)) in bch2_write_super()
977 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1); in bch2_write_super()
979 SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN); in bch2_write_super()
983 bch2_sb_members_cpy_v2_v1(&c->disk_sb); in bch2_write_super()
993 ret = bch2_sb_validate(&(*ca)->disk_sb, BCH_VALIDATE_write, &err); in bch2_write_super()
996 goto out; in bch2_write_super()
1000 if (c->opts.nochanges) in bch2_write_super()
1001 goto out; in bch2_write_super()
1005 * complete - don't write out a partly initialized superblock: in bch2_write_super()
1007 if (!BCH_SB_INITIALIZED(c->disk_sb.sb)) in bch2_write_super()
1008 goto out; in bch2_write_super()
1010 if (le16_to_cpu(c->disk_sb.sb->version) > bcachefs_metadata_version_current) { in bch2_write_super()
1013 bch2_version_to_text(&buf, le16_to_cpu(c->disk_sb.sb->version)); in bch2_write_super()
1019 return -BCH_ERR_sb_not_downgraded; in bch2_write_super()
1023 __set_bit((*ca)->dev_idx, sb_written.d); in bch2_write_super()
1024 (*ca)->sb_write_error = 0; in bch2_write_super()
1034 if (ca->sb_write_error) in bch2_write_super()
1037 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) { in bch2_write_super()
1040 prt_bdevname(&buf, ca->disk_sb.bdev); in bch2_write_super()
1043 le64_to_cpu(ca->sb_read_scratch->seq), in bch2_write_super()
1044 ca->disk_sb.seq); in bch2_write_super()
1047 ret = -BCH_ERR_erofs_sb_err; in bch2_write_super()
1050 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) { in bch2_write_super()
1053 prt_bdevname(&buf, ca->disk_sb.bdev); in bch2_write_super()
1056 le64_to_cpu(ca->sb_read_scratch->seq), in bch2_write_super()
1057 ca->disk_sb.seq); in bch2_write_super()
1060 ret = -BCH_ERR_erofs_sb_err; in bch2_write_super()
1065 goto out; in bch2_write_super()
1071 if (!ca->sb_write_error && in bch2_write_super()
1072 sb < ca->disk_sb.sb->layout.nr_superblocks) { in bch2_write_super()
1083 if (ca->sb_write_error) in bch2_write_super()
1084 __clear_bit(ca->dev_idx, sb_written.d); in bch2_write_super()
1086 ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq); in bch2_write_super()
1114 ret = -1; in bch2_write_super()
1115 out: in bch2_write_super()
1119 percpu_ref_put(&(*ca)->io_ref); in bch2_write_super()
1127 mutex_lock(&c->sb_lock); in __bch2_check_set_feature()
1128 if (!(c->sb.features & (1ULL << feat))) { in __bch2_check_set_feature()
1129 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat); in __bch2_check_set_feature()
1133 mutex_unlock(&c->sb_lock); in __bch2_check_set_feature()
1139 bool ret = bcachefs_metadata_version_current < c->sb.version; in bch2_check_version_downgrade()
1141 lockdep_assert_held(&c->sb_lock); in bch2_check_version_downgrade()
1147 * c->sb will be checked before we write the superblock, so update it as in bch2_check_version_downgrade()
1150 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current) in bch2_check_version_downgrade()
1151 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current); in bch2_check_version_downgrade()
1152 if (c->sb.version > bcachefs_metadata_version_current) in bch2_check_version_downgrade()
1153 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current); in bch2_check_version_downgrade()
1154 if (c->sb.version_min > bcachefs_metadata_version_current) in bch2_check_version_downgrade()
1155 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current); in bch2_check_version_downgrade()
1156 c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1); in bch2_check_version_downgrade()
1162 lockdep_assert_held(&c->sb_lock); in bch2_sb_upgrade()
1165 BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version))) in bch2_sb_upgrade()
1166 bch2_sb_field_resize(&c->disk_sb, downgrade, 0); in bch2_sb_upgrade()
1168 c->disk_sb.sb->version = cpu_to_le16(new_version); in bch2_sb_upgrade()
1169 c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL); in bch2_sb_upgrade()
1177 return -BCH_ERR_invalid_sb_ext; in bch2_sb_ext_validate()
1183 static void bch2_sb_ext_to_text(struct printbuf *out, struct bch_sb *sb, in bch2_sb_ext_to_text() argument
1188 prt_printf(out, "Recovery passes required:\t"); in bch2_sb_ext_to_text()
1189 prt_bitflags(out, bch2_recovery_passes, in bch2_sb_ext_to_text()
1190 bch2_recovery_passes_from_stable(le64_to_cpu(e->recovery_passes_required[0]))); in bch2_sb_ext_to_text()
1191 prt_newline(out); in bch2_sb_ext_to_text()
1193 unsigned long *errors_silent = kmalloc(sizeof(e->errors_silent), GFP_KERNEL); in bch2_sb_ext_to_text()
1195 le_bitvector_to_cpu(errors_silent, (void *) e->errors_silent, sizeof(e->errors_silent) * 8); in bch2_sb_ext_to_text()
1197 prt_printf(out, "Errors to silently fix:\t"); in bch2_sb_ext_to_text()
1198 prt_bitflags_vector(out, bch2_sb_error_strs, errors_silent, in bch2_sb_ext_to_text()
1199 min(BCH_FSCK_ERR_MAX, sizeof(e->errors_silent) * 8)); in bch2_sb_ext_to_text()
1200 prt_newline(out); in bch2_sb_ext_to_text()
1205 prt_printf(out, "Btrees with missing data:\t"); in bch2_sb_ext_to_text()
1206 prt_bitflags(out, __bch2_btree_ids, le64_to_cpu(e->btrees_lost_data)); in bch2_sb_ext_to_text()
1207 prt_newline(out); in bch2_sb_ext_to_text()
1234 unsigned type = le32_to_cpu(f->type); in bch2_sb_field_validate()
1239 ret = ops->validate ? ops->validate(sb, f, flags, &field_err) : 0; in bch2_sb_field_validate()
1251 void __bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb, in __bch2_sb_field_to_text() argument
1254 unsigned type = le32_to_cpu(f->type); in __bch2_sb_field_to_text()
1257 if (!out->nr_tabstops) in __bch2_sb_field_to_text()
1258 printbuf_tabstop_push(out, 32); in __bch2_sb_field_to_text()
1260 if (ops->to_text) in __bch2_sb_field_to_text()
1261 ops->to_text(out, sb, f); in __bch2_sb_field_to_text()
1264 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb, in bch2_sb_field_to_text() argument
1267 unsigned type = le32_to_cpu(f->type); in bch2_sb_field_to_text()
1270 prt_printf(out, "%s", bch2_sb_fields[type]); in bch2_sb_field_to_text()
1272 prt_printf(out, "(unknown field %u)", type); in bch2_sb_field_to_text()
1274 prt_printf(out, " (size %zu):", vstruct_bytes(f)); in bch2_sb_field_to_text()
1275 prt_newline(out); in bch2_sb_field_to_text()
1277 __bch2_sb_field_to_text(out, sb, f); in bch2_sb_field_to_text()
1280 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l) in bch2_sb_layout_to_text() argument
1284 prt_printf(out, "Type: %u", l->layout_type); in bch2_sb_layout_to_text()
1285 prt_newline(out); in bch2_sb_layout_to_text()
1287 prt_str(out, "Superblock max size: "); in bch2_sb_layout_to_text()
1288 prt_units_u64(out, 512 << l->sb_max_size_bits); in bch2_sb_layout_to_text()
1289 prt_newline(out); in bch2_sb_layout_to_text()
1291 prt_printf(out, "Nr superblocks: %u", l->nr_superblocks); in bch2_sb_layout_to_text()
1292 prt_newline(out); in bch2_sb_layout_to_text()
1294 prt_str(out, "Offsets: "); in bch2_sb_layout_to_text()
1295 for (i = 0; i < l->nr_superblocks; i++) { in bch2_sb_layout_to_text()
1297 prt_str(out, ", "); in bch2_sb_layout_to_text()
1298 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i])); in bch2_sb_layout_to_text()
1300 prt_newline(out); in bch2_sb_layout_to_text()
1303 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb, in bch2_sb_to_text() argument
1306 if (!out->nr_tabstops) in bch2_sb_to_text()
1307 printbuf_tabstop_push(out, 44); in bch2_sb_to_text()
1309 prt_printf(out, "External UUID:\t"); in bch2_sb_to_text()
1310 pr_uuid(out, sb->user_uuid.b); in bch2_sb_to_text()
1311 prt_newline(out); in bch2_sb_to_text()
1313 prt_printf(out, "Internal UUID:\t"); in bch2_sb_to_text()
1314 pr_uuid(out, sb->uuid.b); in bch2_sb_to_text()
1315 prt_newline(out); in bch2_sb_to_text()
1317 prt_printf(out, "Magic number:\t"); in bch2_sb_to_text()
1318 pr_uuid(out, sb->magic.b); in bch2_sb_to_text()
1319 prt_newline(out); in bch2_sb_to_text()
1321 prt_printf(out, "Device index:\t%u\n", sb->dev_idx); in bch2_sb_to_text()
1323 prt_printf(out, "Label:\t"); in bch2_sb_to_text()
1324 if (!strlen(sb->label)) in bch2_sb_to_text()
1325 prt_printf(out, "(none)"); in bch2_sb_to_text()
1327 prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label); in bch2_sb_to_text()
1328 prt_newline(out); in bch2_sb_to_text()
1330 prt_printf(out, "Version:\t"); in bch2_sb_to_text()
1331 bch2_version_to_text(out, le16_to_cpu(sb->version)); in bch2_sb_to_text()
1332 prt_newline(out); in bch2_sb_to_text()
1334 prt_printf(out, "Version upgrade complete:\t"); in bch2_sb_to_text()
1335 bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb)); in bch2_sb_to_text()
1336 prt_newline(out); in bch2_sb_to_text()
1338 prt_printf(out, "Oldest version on disk:\t"); in bch2_sb_to_text()
1339 bch2_version_to_text(out, le16_to_cpu(sb->version_min)); in bch2_sb_to_text()
1340 prt_newline(out); in bch2_sb_to_text()
1342 prt_printf(out, "Created:\t"); in bch2_sb_to_text()
1343 if (sb->time_base_lo) in bch2_sb_to_text()
1344 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC)); in bch2_sb_to_text()
1346 prt_printf(out, "(not set)"); in bch2_sb_to_text()
1347 prt_newline(out); in bch2_sb_to_text()
1349 prt_printf(out, "Sequence number:\t"); in bch2_sb_to_text()
1350 prt_printf(out, "%llu", le64_to_cpu(sb->seq)); in bch2_sb_to_text()
1351 prt_newline(out); in bch2_sb_to_text()
1353 prt_printf(out, "Time of last write:\t"); in bch2_sb_to_text()
1354 bch2_prt_datetime(out, le64_to_cpu(sb->write_time)); in bch2_sb_to_text()
1355 prt_newline(out); in bch2_sb_to_text()
1357 prt_printf(out, "Superblock size:\t"); in bch2_sb_to_text()
1358 prt_units_u64(out, vstruct_bytes(sb)); in bch2_sb_to_text()
1359 prt_str(out, "/"); in bch2_sb_to_text()
1360 prt_units_u64(out, 512ULL << sb->layout.sb_max_size_bits); in bch2_sb_to_text()
1361 prt_newline(out); in bch2_sb_to_text()
1363 prt_printf(out, "Clean:\t%llu\n", BCH_SB_CLEAN(sb)); in bch2_sb_to_text()
1364 prt_printf(out, "Devices:\t%u\n", bch2_sb_nr_devices(sb)); in bch2_sb_to_text()
1366 prt_printf(out, "Sections:\t"); in bch2_sb_to_text()
1369 fields_have |= 1 << le32_to_cpu(f->type); in bch2_sb_to_text()
1370 prt_bitflags(out, bch2_sb_fields, fields_have); in bch2_sb_to_text()
1371 prt_newline(out); in bch2_sb_to_text()
1373 prt_printf(out, "Features:\t"); in bch2_sb_to_text()
1374 prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0])); in bch2_sb_to_text()
1375 prt_newline(out); in bch2_sb_to_text()
1377 prt_printf(out, "Compat features:\t"); in bch2_sb_to_text()
1378 prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0])); in bch2_sb_to_text()
1379 prt_newline(out); in bch2_sb_to_text()
1381 prt_newline(out); in bch2_sb_to_text()
1382 prt_printf(out, "Options:"); in bch2_sb_to_text()
1383 prt_newline(out); in bch2_sb_to_text()
1384 printbuf_indent_add(out, 2); in bch2_sb_to_text()
1391 if (opt->get_sb != BCH2_NO_SB_OPT) { in bch2_sb_to_text()
1394 prt_printf(out, "%s:\t", opt->attr.name); in bch2_sb_to_text()
1395 bch2_opt_to_text(out, NULL, sb, opt, v, in bch2_sb_to_text()
1397 prt_newline(out); in bch2_sb_to_text()
1402 printbuf_indent_sub(out, 2); in bch2_sb_to_text()
1405 prt_newline(out); in bch2_sb_to_text()
1406 prt_printf(out, "layout:"); in bch2_sb_to_text()
1407 prt_newline(out); in bch2_sb_to_text()
1408 printbuf_indent_add(out, 2); in bch2_sb_to_text()
1409 bch2_sb_layout_to_text(out, &sb->layout); in bch2_sb_to_text()
1410 printbuf_indent_sub(out, 2); in bch2_sb_to_text()
1414 if (fields & (1 << le32_to_cpu(f->type))) { in bch2_sb_to_text()
1415 prt_newline(out); in bch2_sb_to_text()
1416 bch2_sb_field_to_text(out, sb, f); in bch2_sb_to_text()