Lines Matching +full:- +full:c
1 // SPDX-License-Identifier: GPL-2.0
28 #include "sb-clean.h"
29 #include "sb-downgrade.h"
31 #include "super-io.h"
37 int bch2_btree_lost_data(struct bch_fs *c, enum btree_id btree) in bch2_btree_lost_data() argument
42 mutex_lock(&c->sb_lock); in bch2_btree_lost_data()
43 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); in bch2_btree_lost_data()
45 if (!(c->sb.btrees_lost_data & b)) { in bch2_btree_lost_data()
48 bch_err(c, "flagging btree %s lost data", buf.buf); in bch2_btree_lost_data()
50 ext->btrees_lost_data |= cpu_to_le64(b); in bch2_btree_lost_data()
54 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_topology) ?: re… in bch2_btree_lost_data()
57 __set_bit_le64(BCH_FSCK_ERR_accounting_mismatch, ext->errors_silent); in bch2_btree_lost_data()
58 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_allocations) ?:… in bch2_btree_lost_data()
65 ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_lrus) ?: ret; in bch2_btree_lost_data()
66 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_backpointers_to… in bch2_btree_lost_data()
71 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_alloc_info) ?: … in bch2_btree_lost_data()
73 __set_bit_le64(BCH_FSCK_ERR_alloc_key_data_type_wrong, ext->errors_silent); in bch2_btree_lost_data()
74 __set_bit_le64(BCH_FSCK_ERR_alloc_key_gen_wrong, ext->errors_silent); in bch2_btree_lost_data()
75 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent); in bch2_btree_lost_data()
76 __set_bit_le64(BCH_FSCK_ERR_alloc_key_cached_sectors_wrong, ext->errors_silent); in bch2_btree_lost_data()
77 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_wrong, ext->errors_silent); in bch2_btree_lost_data()
78 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_redundancy_wrong, ext->errors_silent); in bch2_btree_lost_data()
81 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_btree_backpoint… in bch2_btree_lost_data()
82 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_extents_to_back… in bch2_btree_lost_data()
85 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_alloc_info) ?: … in bch2_btree_lost_data()
88 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_alloc_info) ?: … in bch2_btree_lost_data()
91 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_alloc_info) ?: … in bch2_btree_lost_data()
94 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_alloc_info) ?: … in bch2_btree_lost_data()
97 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_check_allocations) ?:… in bch2_btree_lost_data()
100 …ret = bch2_run_explicit_recovery_pass_persistent_locked(c, BCH_RECOVERY_PASS_scan_for_btree_nodes)… in bch2_btree_lost_data()
104 bch2_write_super(c); in bch2_btree_lost_data()
105 mutex_unlock(&c->sb_lock); in bch2_btree_lost_data()
110 static void kill_btree(struct bch_fs *c, enum btree_id btree) in kill_btree() argument
112 bch2_btree_id_root(c, btree)->alive = false; in kill_btree()
113 bch2_shoot_down_journal_keys(c, btree, 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX); in kill_btree()
116 /* for -o reconstruct_alloc: */
117 static void bch2_reconstruct_alloc(struct bch_fs *c) in bch2_reconstruct_alloc() argument
119 bch2_journal_log_msg(c, "dropping alloc info"); in bch2_reconstruct_alloc()
120 bch_info(c, "dropping and reconstructing all alloc info"); in bch2_reconstruct_alloc()
122 mutex_lock(&c->sb_lock); in bch2_reconstruct_alloc()
123 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); in bch2_reconstruct_alloc()
125 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_allocations, ext->recovery_passes_required); in bch2_reconstruct_alloc()
126 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_alloc_info, ext->recovery_passes_required); in bch2_reconstruct_alloc()
127 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_lrus, ext->recovery_passes_required); in bch2_reconstruct_alloc()
128 …__set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_extents_to_backpointers, ext->recovery_passes_requir… in bch2_reconstruct_alloc()
129 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_alloc_to_lru_refs, ext->recovery_passes_required); in bch2_reconstruct_alloc()
131 __set_bit_le64(BCH_FSCK_ERR_ptr_to_missing_alloc_key, ext->errors_silent); in bch2_reconstruct_alloc()
132 __set_bit_le64(BCH_FSCK_ERR_ptr_gen_newer_than_bucket_gen, ext->errors_silent); in bch2_reconstruct_alloc()
133 __set_bit_le64(BCH_FSCK_ERR_stale_dirty_ptr, ext->errors_silent); in bch2_reconstruct_alloc()
135 __set_bit_le64(BCH_FSCK_ERR_dev_usage_buckets_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
136 __set_bit_le64(BCH_FSCK_ERR_dev_usage_sectors_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
137 __set_bit_le64(BCH_FSCK_ERR_dev_usage_fragmented_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
139 __set_bit_le64(BCH_FSCK_ERR_fs_usage_btree_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
140 __set_bit_le64(BCH_FSCK_ERR_fs_usage_cached_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
141 __set_bit_le64(BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
142 __set_bit_le64(BCH_FSCK_ERR_fs_usage_replicas_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
144 __set_bit_le64(BCH_FSCK_ERR_alloc_key_to_missing_lru_entry, ext->errors_silent); in bch2_reconstruct_alloc()
146 __set_bit_le64(BCH_FSCK_ERR_alloc_key_data_type_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
147 __set_bit_le64(BCH_FSCK_ERR_alloc_key_gen_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
148 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
149 __set_bit_le64(BCH_FSCK_ERR_alloc_key_cached_sectors_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
150 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
151 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_redundancy_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
152 __set_bit_le64(BCH_FSCK_ERR_need_discard_key_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
153 __set_bit_le64(BCH_FSCK_ERR_freespace_key_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
154 __set_bit_le64(BCH_FSCK_ERR_bucket_gens_key_wrong, ext->errors_silent); in bch2_reconstruct_alloc()
155 __set_bit_le64(BCH_FSCK_ERR_freespace_hole_missing, ext->errors_silent); in bch2_reconstruct_alloc()
156 __set_bit_le64(BCH_FSCK_ERR_ptr_to_missing_backpointer, ext->errors_silent); in bch2_reconstruct_alloc()
157 __set_bit_le64(BCH_FSCK_ERR_lru_entry_bad, ext->errors_silent); in bch2_reconstruct_alloc()
158 __set_bit_le64(BCH_FSCK_ERR_accounting_mismatch, ext->errors_silent); in bch2_reconstruct_alloc()
159 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info); in bch2_reconstruct_alloc()
161 …c->opts.recovery_passes |= bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_requi… in bch2_reconstruct_alloc()
163 bch2_write_super(c); in bch2_reconstruct_alloc()
164 mutex_unlock(&c->sb_lock); in bch2_reconstruct_alloc()
166 for (unsigned i = 0; i < btree_id_nr_alive(c); i++) in bch2_reconstruct_alloc()
168 kill_btree(c, i); in bch2_reconstruct_alloc()
179 if (i->k->k.type == KEY_TYPE_btree_ptr_v2) in zero_out_btree_mem_ptr()
180 bkey_i_to_btree_ptr_v2(i->k)->v.mem_ptr = 0; in zero_out_btree_mem_ptr()
187 BUG_ON(seq < j->replay_journal_seq); in replay_now_at()
189 seq = min(seq, j->replay_journal_seq_end); in replay_now_at()
191 while (j->replay_journal_seq < seq) in replay_now_at()
192 bch2_journal_pin_put(j, j->replay_journal_seq++); in replay_now_at()
199 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p, in bch2_journal_replay_accounting_key()
200 BTREE_MAX_DEPTH, k->level, in bch2_journal_replay_accounting_key()
210 if (bversion_cmp(old.k->bversion, k->k->k.bversion) >= 0) { in bch2_journal_replay_accounting_key()
215 struct bkey_i *new = k->k; in bch2_journal_replay_accounting_key()
216 if (old.k->type == KEY_TYPE_accounting) { in bch2_journal_replay_accounting_key()
217 new = bch2_bkey_make_mut_noupdate(trans, bkey_i_to_s_c(k->k)); in bch2_journal_replay_accounting_key()
226 trans->journal_res.seq = k->journal_seq; in bch2_journal_replay_accounting_key()
244 if (k->overwritten) in bch2_journal_replay_key()
247 trans->journal_res.seq = k->journal_seq; in bch2_journal_replay_key()
253 * coherency for non-alloc btrees, and key cache fills for snapshots in bch2_journal_replay_key()
257 if (!k->level && k->btree_id == BTREE_ID_alloc) in bch2_journal_replay_key()
262 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p, in bch2_journal_replay_key()
263 BTREE_MAX_DEPTH, k->level, in bch2_journal_replay_key()
270 if (unlikely(!btree_path_node(path, k->level))) { in bch2_journal_replay_key()
272 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p, in bch2_journal_replay_key()
276 -BCH_ERR_transaction_restart_nested; in bch2_journal_replay_key()
281 if (k->overwritten) in bch2_journal_replay_key()
284 if (k->k->k.type == KEY_TYPE_accounting) { in bch2_journal_replay_key()
285 ret = bch2_trans_update_buffered(trans, BTREE_ID_accounting, k->k); in bch2_journal_replay_key()
289 ret = bch2_trans_update(trans, &iter, k->k, update_flags); in bch2_journal_replay_key()
306 return cmp_int(l->journal_seq - 1, r->journal_seq - 1); in journal_sort_seq_cmp()
309 int bch2_journal_replay(struct bch_fs *c) in bch2_journal_replay() argument
311 struct journal_keys *keys = &c->journal_keys; in bch2_journal_replay()
313 struct journal *j = &c->journal; in bch2_journal_replay()
314 u64 start_seq = c->journal_replay_seq_start; in bch2_journal_replay()
315 u64 end_seq = c->journal_replay_seq_start; in bch2_journal_replay()
320 if (keys->nr) { in bch2_journal_replay()
321 ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)", in bch2_journal_replay()
322 keys->nr, start_seq, end_seq); in bch2_journal_replay()
327 BUG_ON(!atomic_read(&keys->ref)); in bch2_journal_replay()
329 move_gap(keys, keys->nr); in bch2_journal_replay()
330 trans = bch2_trans_get(c); in bch2_journal_replay()
337 if (!(k->k->k.type == KEY_TYPE_accounting && !k->allocated)) in bch2_journal_replay()
349 if (bch2_fs_fatal_err_on(ret, c, "error replaying accounting; %s", bch2_err_str(ret))) in bch2_journal_replay()
352 k->overwritten = true; in bch2_journal_replay()
355 set_bit(BCH_FS_accounting_replay_done, &c->flags); in bch2_journal_replay()
359 * efficient - better locality of btree access - but some might fail if in bch2_journal_replay()
366 * k->allocated means the key wasn't read in from the journal, in bch2_journal_replay()
369 if (k->allocated) in bch2_journal_replay()
373 ret = c->journal.watermark ? -1 : in bch2_journal_replay()
378 (!k->allocated ? BCH_TRANS_COMMIT_no_journal_res : 0), in bch2_journal_replay()
380 BUG_ON(!ret && !k->overwritten && k->k->k.type != KEY_TYPE_accounting); in bch2_journal_replay()
402 if (k->journal_seq) in bch2_journal_replay()
403 replay_now_at(j, k->journal_seq); in bch2_journal_replay()
405 replay_now_at(j, j->replay_journal_seq_end); in bch2_journal_replay()
410 (!k->allocated in bch2_journal_replay()
416 bch2_btree_id_level_to_text(&buf, k->btree_id, k->level); in bch2_journal_replay()
417 bch_err_msg(c, ret, "while replaying key at %s:", buf.buf); in bch2_journal_replay()
422 BUG_ON(k->btree_id != BTREE_ID_accounting && !k->overwritten); in bch2_journal_replay()
432 if (!c->opts.retain_recovery_info && in bch2_journal_replay()
433 c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay) in bch2_journal_replay()
434 bch2_journal_keys_put_initial(c); in bch2_journal_replay()
436 replay_now_at(j, j->replay_journal_seq_end); in bch2_journal_replay()
437 j->replay_journal_seq = 0; in bch2_journal_replay()
443 bch2_journal_flush_all_pins(&c->journal); in bch2_journal_replay()
444 ret = bch2_journal_meta(&c->journal); in bch2_journal_replay()
447 if (keys->nr) in bch2_journal_replay()
448 bch2_journal_log_msg(c, "journal replay finished"); in bch2_journal_replay()
453 bch_err_fn(c, ret); in bch2_journal_replay()
459 static int journal_replay_entry_early(struct bch_fs *c, in journal_replay_entry_early() argument
464 switch (entry->type) { in journal_replay_entry_early()
467 if (unlikely(!entry->u64s)) in journal_replay_entry_early()
470 if (fsck_err_on(entry->btree_id >= BTREE_ID_NR_MAX, in journal_replay_entry_early()
471 c, invalid_btree_id, in journal_replay_entry_early()
473 entry->btree_id, BTREE_ID_NR_MAX)) in journal_replay_entry_early()
476 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) { in journal_replay_entry_early()
477 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL }); in journal_replay_entry_early()
482 struct btree_root *r = bch2_btree_id_root(c, entry->btree_id); in journal_replay_entry_early()
484 r->level = entry->level; in journal_replay_entry_early()
485 bkey_copy(&r->key, (struct bkey_i *) entry->start); in journal_replay_entry_early()
486 r->error = 0; in journal_replay_entry_early()
487 r->alive = true; in journal_replay_entry_early()
494 switch (entry->btree_id) { in journal_replay_entry_early()
496 atomic64_set(&c->key_version, le64_to_cpu(u->v)); in journal_replay_entry_early()
505 ret = bch2_journal_seq_blacklist_add(c, in journal_replay_entry_early()
506 le64_to_cpu(bl_entry->seq), in journal_replay_entry_early()
507 le64_to_cpu(bl_entry->seq) + 1); in journal_replay_entry_early()
514 ret = bch2_journal_seq_blacklist_add(c, in journal_replay_entry_early()
515 le64_to_cpu(bl_entry->start), in journal_replay_entry_early()
516 le64_to_cpu(bl_entry->end) + 1); in journal_replay_entry_early()
523 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time)); in journal_replay_entry_early()
530 static int journal_replay_early(struct bch_fs *c, in journal_replay_early() argument
534 for (struct jset_entry *entry = clean->start; in journal_replay_early()
535 entry != vstruct_end(&clean->field); in journal_replay_early()
537 int ret = journal_replay_entry_early(c, entry); in journal_replay_early()
545 genradix_for_each(&c->journal_entries, iter, _i) { in journal_replay_early()
551 vstruct_for_each(&i->j, entry) { in journal_replay_early()
552 int ret = journal_replay_entry_early(c, entry); in journal_replay_early()
564 static int read_btree_roots(struct bch_fs *c) in read_btree_roots() argument
569 for (unsigned i = 0; i < btree_id_nr_alive(c); i++) { in read_btree_roots()
570 struct btree_root *r = bch2_btree_id_root(c, i); in read_btree_roots()
572 if (!r->alive) in read_btree_roots()
576 bch2_btree_id_level_to_text(&buf, i, r->level); in read_btree_roots()
578 if (mustfix_fsck_err_on((ret = r->error), in read_btree_roots()
579 c, btree_root_bkey_invalid, in read_btree_roots()
582 mustfix_fsck_err_on((ret = r->error = bch2_btree_root_read(c, i, &r->key, r->level)), in read_btree_roots()
583 c, btree_root_read_error, in read_btree_roots()
587 r->error = 0; in read_btree_roots()
589 ret = bch2_btree_lost_data(c, i); in read_btree_roots()
595 struct btree_root *r = bch2_btree_id_root(c, i); in read_btree_roots()
597 if (!r->b && !r->error) { in read_btree_roots()
598 r->alive = false; in read_btree_roots()
599 r->level = 0; in read_btree_roots()
600 bch2_btree_root_alloc_fake(c, i, 0); in read_btree_roots()
608 static bool check_version_upgrade(struct bch_fs *c) in check_version_upgrade() argument
612 bch2_latest_compatible_version(c->sb.version)); in check_version_upgrade()
613 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version; in check_version_upgrade()
618 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible || in check_version_upgrade()
624 switch (c->opts.version_upgrade) { in check_version_upgrade()
643 if (old_version != c->sb.version) { in check_version_upgrade()
645 bch2_version_to_text(&buf, c->sb.version_upgrade_complete); in check_version_upgrade()
647 bch2_version_to_text(&buf, c->sb.version); in check_version_upgrade()
659 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); in check_version_upgrade()
660 __le64 passes = ext->recovery_passes_required[0]; in check_version_upgrade()
661 bch2_sb_set_upgrade(c, old_version, new_version); in check_version_upgrade()
662 passes = ext->recovery_passes_required[0] & ~passes; in check_version_upgrade()
670 bch_info(c, "%s", buf.buf); in check_version_upgrade()
676 if (new_version > c->sb.version_incompat && in check_version_upgrade()
677 c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible) { in check_version_upgrade()
683 bch2_version_to_text(&buf, c->sb.version_incompat_allowed); in check_version_upgrade()
686 bch_info(c, "%s", buf.buf); in check_version_upgrade()
693 bch2_sb_upgrade(c, new_version, in check_version_upgrade()
694 c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible); in check_version_upgrade()
699 int bch2_fs_recovery(struct bch_fs *c) in bch2_fs_recovery() argument
706 if (c->sb.clean) { in bch2_fs_recovery()
707 clean = bch2_read_superblock_clean(c); in bch2_fs_recovery()
712 bch_info(c, "recovering from clean shutdown, journal seq %llu", in bch2_fs_recovery()
713 le64_to_cpu(clean->journal_seq)); in bch2_fs_recovery()
715 bch_info(c, "recovering from unclean shutdown"); in bch2_fs_recovery()
718 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) { in bch2_fs_recovery()
719 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported"); in bch2_fs_recovery()
720 ret = -EINVAL; in bch2_fs_recovery()
724 if (!c->sb.clean && in bch2_fs_recovery()
725 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) { in bch2_fs_recovery()
726 …bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fi… in bch2_fs_recovery()
727 ret = -EINVAL; in bch2_fs_recovery()
731 if (c->opts.norecovery) { in bch2_fs_recovery()
732 c->opts.recovery_pass_last = c->opts.recovery_pass_last in bch2_fs_recovery()
733 ? min(c->opts.recovery_pass_last, BCH_RECOVERY_PASS_snapshots_read) in bch2_fs_recovery()
735 c->opts.nochanges = true; in bch2_fs_recovery()
736 c->opts.read_only = true; in bch2_fs_recovery()
739 mutex_lock(&c->sb_lock); in bch2_fs_recovery()
740 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); in bch2_fs_recovery()
743 if (BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)) { in bch2_fs_recovery()
744 ext->recovery_passes_required[0] |= in bch2_fs_recovery()
749 u64 sb_passes = bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0])); in bch2_fs_recovery()
754 bch_info(c, "%s", buf.buf); in bch2_fs_recovery()
758 if (bch2_check_version_downgrade(c)) { in bch2_fs_recovery()
763 __le64 passes = ext->recovery_passes_required[0]; in bch2_fs_recovery()
764 bch2_sb_set_downgrade(c, in bch2_fs_recovery()
766 BCH_VERSION_MINOR(c->sb.version)); in bch2_fs_recovery()
767 passes = ext->recovery_passes_required[0] & ~passes; in bch2_fs_recovery()
774 bch_info(c, "%s", buf.buf); in bch2_fs_recovery()
779 if (check_version_upgrade(c)) in bch2_fs_recovery()
782 …c->opts.recovery_passes |= bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_requi… in bch2_fs_recovery()
784 if (c->sb.version_upgrade_complete < bcachefs_metadata_version_autofix_errors) { in bch2_fs_recovery()
785 SET_BCH_SB_ERROR_ACTION(c->disk_sb.sb, BCH_ON_ERROR_fix_safe); in bch2_fs_recovery()
790 bch2_write_super(c); in bch2_fs_recovery()
791 mutex_unlock(&c->sb_lock); in bch2_fs_recovery()
793 if (c->opts.fsck) in bch2_fs_recovery()
794 set_bit(BCH_FS_fsck_running, &c->flags); in bch2_fs_recovery()
795 if (c->sb.clean) in bch2_fs_recovery()
796 set_bit(BCH_FS_clean_recovery, &c->flags); in bch2_fs_recovery()
797 set_bit(BCH_FS_recovery_running, &c->flags); in bch2_fs_recovery()
799 ret = bch2_blacklist_table_initialize(c); in bch2_fs_recovery()
801 bch_err(c, "error initializing blacklist table"); in bch2_fs_recovery()
805 bch2_journal_pos_from_member_info_resume(c); in bch2_fs_recovery()
807 if (!c->sb.clean || c->opts.retain_recovery_info) { in bch2_fs_recovery()
811 bch_verbose(c, "starting journal read"); in bch2_fs_recovery()
812 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq); in bch2_fs_recovery()
820 if (c->opts.read_journal_only) in bch2_fs_recovery()
823 genradix_for_each_reverse(&c->journal_entries, iter, i) in bch2_fs_recovery()
825 last_journal_entry = &(*i)->j; in bch2_fs_recovery()
829 if (mustfix_fsck_err_on(c->sb.clean && in bch2_fs_recovery()
831 !journal_entry_empty(last_journal_entry), c, in bch2_fs_recovery()
834 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info); in bch2_fs_recovery()
835 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); in bch2_fs_recovery()
836 c->sb.clean = false; in bch2_fs_recovery()
840 fsck_err_on(!c->sb.clean, c, in bch2_fs_recovery()
846 genradix_for_each_reverse(&c->journal_entries, iter, i) in bch2_fs_recovery()
848 last_journal_entry = &(*i)->j; in bch2_fs_recovery()
849 (*i)->ignore_blacklisted = false; in bch2_fs_recovery()
850 (*i)->ignore_not_dirty= false; in bch2_fs_recovery()
853 * so last_seq was garbage - but we know in bch2_fs_recovery()
857 (*i)->j.last_seq = (*i)->j.seq; in bch2_fs_recovery()
862 ret = bch2_journal_keys_sort(c); in bch2_fs_recovery()
866 if (c->sb.clean && last_journal_entry) { in bch2_fs_recovery()
867 ret = bch2_verify_superblock_clean(c, &clean, in bch2_fs_recovery()
875 bch_err(c, "no superblock clean section found"); in bch2_fs_recovery()
876 ret = -BCH_ERR_fsck_repair_impossible; in bch2_fs_recovery()
880 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1; in bch2_fs_recovery()
883 c->journal_replay_seq_start = last_seq; in bch2_fs_recovery()
884 c->journal_replay_seq_end = blacklist_seq - 1; in bch2_fs_recovery()
886 zero_out_btree_mem_ptr(&c->journal_keys); in bch2_fs_recovery()
888 ret = journal_replay_early(c, clean); in bch2_fs_recovery()
892 if (c->opts.reconstruct_alloc) in bch2_fs_recovery()
893 bch2_reconstruct_alloc(c); in bch2_fs_recovery()
898 * happened before their corresponding journal writes - those btree in bch2_fs_recovery()
902 if (!c->sb.clean) in bch2_fs_recovery()
906 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu", in bch2_fs_recovery()
908 bch2_journal_seq_blacklist_add(c, in bch2_fs_recovery()
911 bch_err_msg(c, ret, "error creating new journal seq blacklist entry"); in bch2_fs_recovery()
916 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu", in bch2_fs_recovery()
917 journal_seq, last_seq, blacklist_seq - 1) ?: in bch2_fs_recovery()
918 bch2_fs_journal_start(&c->journal, journal_seq); in bch2_fs_recovery()
926 if (c->sb.encryption_type && !c->sb.clean) in bch2_fs_recovery()
927 atomic64_add(1 << 16, &c->key_version); in bch2_fs_recovery()
929 ret = read_btree_roots(c); in bch2_fs_recovery()
933 set_bit(BCH_FS_btree_running, &c->flags); in bch2_fs_recovery()
935 ret = bch2_sb_set_upgrade_extra(c); in bch2_fs_recovery()
937 ret = bch2_run_recovery_passes(c); in bch2_fs_recovery()
947 set_bit(BCH_FS_may_go_rw, &c->flags); in bch2_fs_recovery()
948 clear_bit(BCH_FS_fsck_running, &c->flags); in bch2_fs_recovery()
949 clear_bit(BCH_FS_recovery_running, &c->flags); in bch2_fs_recovery()
952 set_bit(BCH_FS_accounting_replay_done, &c->flags); in bch2_fs_recovery()
954 bch2_async_btree_node_rewrites_flush(c); in bch2_fs_recovery()
957 if (test_bit(BCH_FS_errors_fixed, &c->flags)) { in bch2_fs_recovery()
958 bch2_journal_flush_all_pins(&c->journal); in bch2_fs_recovery()
959 bch2_journal_meta(&c->journal); in bch2_fs_recovery()
964 test_bit(BCH_FS_errors_fixed, &c->flags) && in bch2_fs_recovery()
965 !test_bit(BCH_FS_errors_not_fixed, &c->flags) && in bch2_fs_recovery()
966 !test_bit(BCH_FS_error, &c->flags)) { in bch2_fs_recovery()
967 bch2_flush_fsck_errs(c); in bch2_fs_recovery()
969 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean"); in bch2_fs_recovery()
970 clear_bit(BCH_FS_errors_fixed, &c->flags); in bch2_fs_recovery()
972 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info; in bch2_fs_recovery()
974 ret = bch2_run_recovery_passes(c); in bch2_fs_recovery()
978 if (test_bit(BCH_FS_errors_fixed, &c->flags) || in bch2_fs_recovery()
979 test_bit(BCH_FS_errors_not_fixed, &c->flags)) { in bch2_fs_recovery()
980 bch_err(c, "Second fsck run was not clean"); in bch2_fs_recovery()
981 set_bit(BCH_FS_errors_not_fixed, &c->flags); in bch2_fs_recovery()
984 set_bit(BCH_FS_errors_fixed, &c->flags); in bch2_fs_recovery()
987 if (enabled_qtypes(c)) { in bch2_fs_recovery()
988 bch_verbose(c, "reading quotas"); in bch2_fs_recovery()
989 ret = bch2_fs_quota_read(c); in bch2_fs_recovery()
992 bch_verbose(c, "quotas done"); in bch2_fs_recovery()
995 mutex_lock(&c->sb_lock); in bch2_fs_recovery()
996 ext = bch2_sb_field_get(c->disk_sb.sb, ext); in bch2_fs_recovery()
999 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != le16_to_cpu(c->disk_sb.sb->version)) { in bch2_fs_recovery()
1000 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, le16_to_cpu(c->disk_sb.sb->version)); in bch2_fs_recovery()
1004 if (!test_bit(BCH_FS_error, &c->flags) && in bch2_fs_recovery()
1005 !(c->disk_sb.sb->compat[0] & cpu_to_le64(1ULL << BCH_COMPAT_alloc_info))) { in bch2_fs_recovery()
1006 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info); in bch2_fs_recovery()
1010 if (!test_bit(BCH_FS_error, &c->flags) && in bch2_fs_recovery()
1011 !bch2_is_zero(ext->errors_silent, sizeof(ext->errors_silent))) { in bch2_fs_recovery()
1012 memset(ext->errors_silent, 0, sizeof(ext->errors_silent)); in bch2_fs_recovery()
1016 if (c->opts.fsck && in bch2_fs_recovery()
1017 !test_bit(BCH_FS_error, &c->flags) && in bch2_fs_recovery()
1018 c->recovery_pass_done == BCH_RECOVERY_PASS_NR - 1 && in bch2_fs_recovery()
1019 ext->btrees_lost_data) { in bch2_fs_recovery()
1020 ext->btrees_lost_data = 0; in bch2_fs_recovery()
1024 if (c->opts.fsck && in bch2_fs_recovery()
1025 !test_bit(BCH_FS_error, &c->flags) && in bch2_fs_recovery()
1026 !test_bit(BCH_FS_errors_not_fixed, &c->flags)) { in bch2_fs_recovery()
1027 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0); in bch2_fs_recovery()
1028 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0); in bch2_fs_recovery()
1032 if (bch2_blacklist_entries_gc(c)) in bch2_fs_recovery()
1036 bch2_write_super(c); in bch2_fs_recovery()
1037 mutex_unlock(&c->sb_lock); in bch2_fs_recovery()
1039 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) || in bch2_fs_recovery()
1040 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) { in bch2_fs_recovery()
1046 bch2_version_to_text(&buf, c->sb.version_min); in bch2_fs_recovery()
1047 bch_info(c, "scanning for old btree nodes: min_version %s", buf.buf); in bch2_fs_recovery()
1050 ret = bch2_fs_read_write_early(c) ?: in bch2_fs_recovery()
1051 bch2_scan_old_btree_nodes(c, &stats); in bch2_fs_recovery()
1054 bch_info(c, "scanning for old btree nodes done"); in bch2_fs_recovery()
1059 bch2_flush_fsck_errs(c); in bch2_fs_recovery()
1061 if (!c->opts.retain_recovery_info) { in bch2_fs_recovery()
1062 bch2_journal_keys_put_initial(c); in bch2_fs_recovery()
1063 bch2_find_btree_nodes_exit(&c->found_btree_nodes); in bch2_fs_recovery()
1069 test_bit(BCH_FS_need_delete_dead_snapshots, &c->flags) && in bch2_fs_recovery()
1070 !c->opts.nochanges) { in bch2_fs_recovery()
1071 bch2_fs_read_write_early(c); in bch2_fs_recovery()
1072 bch2_delete_dead_snapshots_async(c); in bch2_fs_recovery()
1075 bch_err_fn(c, ret); in bch2_fs_recovery()
1079 bch2_fs_emergency_read_only(c); in bch2_fs_recovery()
1083 int bch2_fs_initialize(struct bch_fs *c) in bch2_fs_initialize() argument
1091 bch_notice(c, "initializing new filesystem"); in bch2_fs_initialize()
1092 set_bit(BCH_FS_new_fs, &c->flags); in bch2_fs_initialize()
1094 mutex_lock(&c->sb_lock); in bch2_fs_initialize()
1095 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done); in bch2_fs_initialize()
1096 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done); in bch2_fs_initialize()
1098 bch2_check_version_downgrade(c); in bch2_fs_initialize()
1100 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) { in bch2_fs_initialize()
1101 bch2_sb_upgrade(c, bcachefs_metadata_version_current, false); in bch2_fs_initialize()
1102 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current); in bch2_fs_initialize()
1103 bch2_write_super(c); in bch2_fs_initialize()
1106 for_each_member_device(c, ca) { in bch2_fs_initialize()
1107 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in bch2_fs_initialize()
1109 ca->mi = bch2_mi_to_cpu(m); in bch2_fs_initialize()
1112 bch2_write_super(c); in bch2_fs_initialize()
1113 mutex_unlock(&c->sb_lock); in bch2_fs_initialize()
1115 set_bit(BCH_FS_btree_running, &c->flags); in bch2_fs_initialize()
1116 set_bit(BCH_FS_may_go_rw, &c->flags); in bch2_fs_initialize()
1119 bch2_btree_root_alloc_fake(c, i, 0); in bch2_fs_initialize()
1121 ret = bch2_fs_journal_alloc(c); in bch2_fs_initialize()
1129 ret = bch2_fs_journal_start(&c->journal, 1); in bch2_fs_initialize()
1133 ret = bch2_fs_read_write_early(c); in bch2_fs_initialize()
1137 set_bit(BCH_FS_accounting_replay_done, &c->flags); in bch2_fs_initialize()
1138 bch2_journal_set_replay_done(&c->journal); in bch2_fs_initialize()
1140 for_each_member_device(c, ca) { in bch2_fs_initialize()
1152 bch_verbose(c, "marking superblocks"); in bch2_fs_initialize()
1153 ret = bch2_trans_mark_dev_sbs(c); in bch2_fs_initialize()
1154 bch_err_msg(c, ret, "marking superblocks"); in bch2_fs_initialize()
1158 ret = bch2_fs_freespace_init(c); in bch2_fs_initialize()
1162 ret = bch2_initialize_subvolumes(c); in bch2_fs_initialize()
1166 bch_verbose(c, "reading snapshots table"); in bch2_fs_initialize()
1167 ret = bch2_snapshots_read(c); in bch2_fs_initialize()
1170 bch_verbose(c, "reading snapshots done"); in bch2_fs_initialize()
1172 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL); in bch2_fs_initialize()
1178 ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed_inode.inode.k_i, NULL, 0, 0); in bch2_fs_initialize()
1179 bch_err_msg(c, ret, "creating root directory"); in bch2_fs_initialize()
1183 bch2_inode_init_early(c, &lostfound_inode); in bch2_fs_initialize()
1185 ret = bch2_trans_commit_do(c, NULL, NULL, 0, in bch2_fs_initialize()
1192 bch_err_msg(c, ret, "creating lost+found"); in bch2_fs_initialize()
1196 c->recovery_pass_done = BCH_RECOVERY_PASS_NR - 1; in bch2_fs_initialize()
1198 bch2_copygc_wakeup(c); in bch2_fs_initialize()
1199 bch2_rebalance_wakeup(c); in bch2_fs_initialize()
1201 if (enabled_qtypes(c)) { in bch2_fs_initialize()
1202 ret = bch2_fs_quota_read(c); in bch2_fs_initialize()
1207 ret = bch2_journal_flush(&c->journal); in bch2_fs_initialize()
1208 bch_err_msg(c, ret, "writing first journal entry"); in bch2_fs_initialize()
1212 mutex_lock(&c->sb_lock); in bch2_fs_initialize()
1213 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true); in bch2_fs_initialize()
1214 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); in bch2_fs_initialize()
1216 bch2_write_super(c); in bch2_fs_initialize()
1217 mutex_unlock(&c->sb_lock); in bch2_fs_initialize()
1219 c->curr_recovery_pass = BCH_RECOVERY_PASS_NR; in bch2_fs_initialize()
1222 bch_err_fn(c, ret); in bch2_fs_initialize()