1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
6 #include "btree_journal_iter.h"
7 #include "btree_node_scan.h"
8 #include "btree_update.h"
9 #include "btree_update_interior.h"
15 #include "fs-common.h"
16 #include "journal_io.h"
17 #include "journal_reclaim.h"
18 #include "journal_seq_blacklist.h"
19 #include "logged_ops.h"
22 #include "rebalance.h"
24 #include "recovery_passes.h"
27 #include "sb-downgrade.h"
31 #include <linux/sort.h>
32 #include <linux/stat.h>
34 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
36 void bch2_btree_lost_data(struct bch_fs *c, enum btree_id btree)
38 u64 b = BIT_ULL(btree);
40 if (!(c->sb.btrees_lost_data & b)) {
41 bch_err(c, "flagging btree %s lost data", bch2_btree_id_str(btree));
43 mutex_lock(&c->sb_lock);
44 bch2_sb_field_get(c->disk_sb.sb, ext)->btrees_lost_data |= cpu_to_le64(b);
46 mutex_unlock(&c->sb_lock);
50 /* for -o reconstruct_alloc: */
51 static void bch2_reconstruct_alloc(struct bch_fs *c)
53 bch2_journal_log_msg(c, "dropping alloc info");
54 bch_info(c, "dropping and reconstructing all alloc info");
56 mutex_lock(&c->sb_lock);
57 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
59 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_allocations, ext->recovery_passes_required);
60 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_alloc_info, ext->recovery_passes_required);
61 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_lrus, ext->recovery_passes_required);
62 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_extents_to_backpointers, ext->recovery_passes_required);
63 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_alloc_to_lru_refs, ext->recovery_passes_required);
65 __set_bit_le64(BCH_FSCK_ERR_ptr_to_missing_alloc_key, ext->errors_silent);
66 __set_bit_le64(BCH_FSCK_ERR_ptr_gen_newer_than_bucket_gen, ext->errors_silent);
67 __set_bit_le64(BCH_FSCK_ERR_stale_dirty_ptr, ext->errors_silent);
68 __set_bit_le64(BCH_FSCK_ERR_alloc_key_data_type_wrong, ext->errors_silent);
69 __set_bit_le64(BCH_FSCK_ERR_alloc_key_gen_wrong, ext->errors_silent);
70 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent);
71 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_wrong, ext->errors_silent);
72 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_redundancy_wrong, ext->errors_silent);
73 __set_bit_le64(BCH_FSCK_ERR_need_discard_key_wrong, ext->errors_silent);
74 __set_bit_le64(BCH_FSCK_ERR_freespace_key_wrong, ext->errors_silent);
75 __set_bit_le64(BCH_FSCK_ERR_bucket_gens_key_wrong, ext->errors_silent);
76 __set_bit_le64(BCH_FSCK_ERR_freespace_hole_missing, ext->errors_silent);
77 __set_bit_le64(BCH_FSCK_ERR_ptr_to_missing_backpointer, ext->errors_silent);
78 __set_bit_le64(BCH_FSCK_ERR_lru_entry_bad, ext->errors_silent);
79 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
82 mutex_unlock(&c->sb_lock);
84 c->recovery_passes_explicit |= bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0]));
87 bch2_shoot_down_journal_keys(c, BTREE_ID_alloc,
88 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX);
89 bch2_shoot_down_journal_keys(c, BTREE_ID_backpointers,
90 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX);
91 bch2_shoot_down_journal_keys(c, BTREE_ID_need_discard,
92 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX);
93 bch2_shoot_down_journal_keys(c, BTREE_ID_freespace,
94 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX);
95 bch2_shoot_down_journal_keys(c, BTREE_ID_bucket_gens,
96 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX);
100 * Btree node pointers have a field to stack a pointer to the in memory btree
101 * node; we need to zero out this field when reading in btree nodes, or when
102 * reading in keys from the journal:
104 static void zero_out_btree_mem_ptr(struct journal_keys *keys)
106 darray_for_each(*keys, i)
107 if (i->k->k.type == KEY_TYPE_btree_ptr_v2)
108 bkey_i_to_btree_ptr_v2(i->k)->v.mem_ptr = 0;
111 /* journal replay: */
113 static void replay_now_at(struct journal *j, u64 seq)
115 BUG_ON(seq < j->replay_journal_seq);
117 seq = min(seq, j->replay_journal_seq_end);
119 while (j->replay_journal_seq < seq)
120 bch2_journal_pin_put(j, j->replay_journal_seq++);
123 static int bch2_journal_replay_key(struct btree_trans *trans,
124 struct journal_key *k)
126 struct btree_iter iter;
127 unsigned iter_flags =
129 BTREE_ITER_NOT_EXTENTS;
130 unsigned update_flags = BTREE_TRIGGER_NORUN;
136 trans->journal_res.seq = k->journal_seq;
139 * BTREE_UPDATE_KEY_CACHE_RECLAIM disables key cache lookup/update to
140 * keep the key cache coherent with the underlying btree. Nothing
141 * besides the allocator is doing updates yet so we don't need key cache
142 * coherency for non-alloc btrees, and key cache fills for snapshots
143 * btrees use BTREE_ITER_FILTER_SNAPSHOTS, which isn't available until
144 * the snapshots recovery pass runs.
146 if (!k->level && k->btree_id == BTREE_ID_alloc)
147 iter_flags |= BTREE_ITER_CACHED;
149 update_flags |= BTREE_UPDATE_KEY_CACHE_RECLAIM;
151 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p,
152 BTREE_MAX_DEPTH, k->level,
154 ret = bch2_btree_iter_traverse(&iter);
158 struct btree_path *path = btree_iter_path(trans, &iter);
159 if (unlikely(!btree_path_node(path, k->level))) {
160 bch2_trans_iter_exit(trans, &iter);
161 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p,
162 BTREE_MAX_DEPTH, 0, iter_flags);
163 ret = bch2_btree_iter_traverse(&iter) ?:
164 bch2_btree_increase_depth(trans, iter.path, 0) ?:
165 -BCH_ERR_transaction_restart_nested;
169 /* Must be checked with btree locked: */
173 ret = bch2_trans_update(trans, &iter, k->k, update_flags);
175 bch2_trans_iter_exit(trans, &iter);
179 static int journal_sort_seq_cmp(const void *_l, const void *_r)
181 const struct journal_key *l = *((const struct journal_key **)_l);
182 const struct journal_key *r = *((const struct journal_key **)_r);
184 return cmp_int(l->journal_seq, r->journal_seq);
187 int bch2_journal_replay(struct bch_fs *c)
189 struct journal_keys *keys = &c->journal_keys;
190 DARRAY(struct journal_key *) keys_sorted = { 0 };
191 struct journal *j = &c->journal;
192 u64 start_seq = c->journal_replay_seq_start;
193 u64 end_seq = c->journal_replay_seq_start;
194 struct btree_trans *trans = bch2_trans_get(c);
195 bool immediate_flush = false;
199 ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
200 keys->nr, start_seq, end_seq);
205 BUG_ON(!atomic_read(&keys->ref));
207 move_gap(keys, keys->nr);
210 * First, attempt to replay keys in sorted order. This is more
211 * efficient - better locality of btree access - but some might fail if
212 * that would cause a journal deadlock.
214 darray_for_each(*keys, k) {
218 * k->allocated means the key wasn't read in from the journal,
219 * rather it was from early repair code
222 immediate_flush = true;
224 /* Skip fastpath if we're low on space in the journal */
225 ret = c->journal.watermark ? -1 :
226 commit_do(trans, NULL, NULL,
227 BCH_TRANS_COMMIT_no_enospc|
228 BCH_TRANS_COMMIT_journal_reclaim|
229 (!k->allocated ? BCH_TRANS_COMMIT_no_journal_res : 0),
230 bch2_journal_replay_key(trans, k));
231 BUG_ON(!ret && !k->overwritten);
233 ret = darray_push(&keys_sorted, k);
240 * Now, replay any remaining keys in the order in which they appear in
241 * the journal, unpinning those journal entries as we go:
243 sort(keys_sorted.data, keys_sorted.nr,
244 sizeof(keys_sorted.data[0]),
245 journal_sort_seq_cmp, NULL);
247 darray_for_each(keys_sorted, kp) {
250 struct journal_key *k = *kp;
253 replay_now_at(j, k->journal_seq);
255 replay_now_at(j, j->replay_journal_seq_end);
257 ret = commit_do(trans, NULL, NULL,
258 BCH_TRANS_COMMIT_no_enospc|
260 ? BCH_TRANS_COMMIT_no_journal_res|BCH_WATERMARK_reclaim
262 bch2_journal_replay_key(trans, k));
263 bch_err_msg(c, ret, "while replaying key at btree %s level %u:",
264 bch2_btree_id_str(k->btree_id), k->level);
268 BUG_ON(!k->overwritten);
272 * We need to put our btree_trans before calling flush_all_pins(), since
273 * that will use a btree_trans internally
275 bch2_trans_put(trans);
278 if (!c->opts.retain_recovery_info &&
279 c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay)
280 bch2_journal_keys_put_initial(c);
282 replay_now_at(j, j->replay_journal_seq_end);
283 j->replay_journal_seq = 0;
285 bch2_journal_set_replay_done(j);
287 /* if we did any repair, flush it immediately */
288 if (immediate_flush) {
289 bch2_journal_flush_all_pins(&c->journal);
290 ret = bch2_journal_meta(&c->journal);
294 bch2_journal_log_msg(c, "journal replay finished");
297 bch2_trans_put(trans);
298 darray_exit(&keys_sorted);
303 /* journal replay early: */
305 static int journal_replay_entry_early(struct bch_fs *c,
306 struct jset_entry *entry)
310 switch (entry->type) {
311 case BCH_JSET_ENTRY_btree_root: {
312 struct btree_root *r;
314 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) {
315 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL });
320 r = bch2_btree_id_root(c, entry->btree_id);
323 r->level = entry->level;
324 bkey_copy(&r->key, (struct bkey_i *) entry->start);
327 r->error = -BCH_ERR_btree_node_read_error;
332 case BCH_JSET_ENTRY_usage: {
333 struct jset_entry_usage *u =
334 container_of(entry, struct jset_entry_usage, entry);
336 switch (entry->btree_id) {
337 case BCH_FS_USAGE_reserved:
338 if (entry->level < BCH_REPLICAS_MAX)
339 c->usage_base->persistent_reserved[entry->level] =
342 case BCH_FS_USAGE_inodes:
343 c->usage_base->b.nr_inodes = le64_to_cpu(u->v);
345 case BCH_FS_USAGE_key_version:
346 atomic64_set(&c->key_version,
353 case BCH_JSET_ENTRY_data_usage: {
354 struct jset_entry_data_usage *u =
355 container_of(entry, struct jset_entry_data_usage, entry);
357 ret = bch2_replicas_set_usage(c, &u->r,
361 case BCH_JSET_ENTRY_dev_usage: {
362 struct jset_entry_dev_usage *u =
363 container_of(entry, struct jset_entry_dev_usage, entry);
364 struct bch_dev *ca = bch_dev_bkey_exists(c, le32_to_cpu(u->dev));
365 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
367 for (i = 0; i < min_t(unsigned, nr_types, BCH_DATA_NR); i++) {
368 ca->usage_base->d[i].buckets = le64_to_cpu(u->d[i].buckets);
369 ca->usage_base->d[i].sectors = le64_to_cpu(u->d[i].sectors);
370 ca->usage_base->d[i].fragmented = le64_to_cpu(u->d[i].fragmented);
375 case BCH_JSET_ENTRY_blacklist: {
376 struct jset_entry_blacklist *bl_entry =
377 container_of(entry, struct jset_entry_blacklist, entry);
379 ret = bch2_journal_seq_blacklist_add(c,
380 le64_to_cpu(bl_entry->seq),
381 le64_to_cpu(bl_entry->seq) + 1);
384 case BCH_JSET_ENTRY_blacklist_v2: {
385 struct jset_entry_blacklist_v2 *bl_entry =
386 container_of(entry, struct jset_entry_blacklist_v2, entry);
388 ret = bch2_journal_seq_blacklist_add(c,
389 le64_to_cpu(bl_entry->start),
390 le64_to_cpu(bl_entry->end) + 1);
393 case BCH_JSET_ENTRY_clock: {
394 struct jset_entry_clock *clock =
395 container_of(entry, struct jset_entry_clock, entry);
397 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time));
404 static int journal_replay_early(struct bch_fs *c,
405 struct bch_sb_field_clean *clean)
408 for (struct jset_entry *entry = clean->start;
409 entry != vstruct_end(&clean->field);
410 entry = vstruct_next(entry)) {
411 int ret = journal_replay_entry_early(c, entry);
416 struct genradix_iter iter;
417 struct journal_replay *i, **_i;
419 genradix_for_each(&c->journal_entries, iter, _i) {
422 if (journal_replay_ignore(i))
425 vstruct_for_each(&i->j, entry) {
426 int ret = journal_replay_entry_early(c, entry);
433 bch2_fs_usage_initialize(c);
438 /* sb clean section: */
440 static int read_btree_roots(struct bch_fs *c)
444 for (unsigned i = 0; i < btree_id_nr_alive(c); i++) {
445 struct btree_root *r = bch2_btree_id_root(c, i);
450 if (btree_id_is_alloc(i) && c->opts.reconstruct_alloc)
453 if (mustfix_fsck_err_on((ret = r->error),
454 c, btree_root_bkey_invalid,
455 "invalid btree root %s",
456 bch2_btree_id_str(i)) ||
457 mustfix_fsck_err_on((ret = r->error = bch2_btree_root_read(c, i, &r->key, r->level)),
458 c, btree_root_read_error,
459 "error reading btree root %s l=%u: %s",
460 bch2_btree_id_str(i), r->level, bch2_err_str(ret))) {
461 if (btree_id_is_alloc(i)) {
462 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_allocations);
463 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_alloc_info);
464 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_lrus);
465 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_extents_to_backpointers);
466 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_alloc_to_lru_refs);
467 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
469 } else if (!(c->recovery_passes_explicit & BIT_ULL(BCH_RECOVERY_PASS_scan_for_btree_nodes))) {
470 bch_info(c, "will run btree node scan");
471 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_scan_for_btree_nodes);
472 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_topology);
476 bch2_btree_lost_data(c, i);
480 for (unsigned i = 0; i < BTREE_ID_NR; i++) {
481 struct btree_root *r = bch2_btree_id_root(c, i);
483 if (!r->b && !r->error) {
486 bch2_btree_root_alloc_fake(c, i, 0);
493 static bool check_version_upgrade(struct bch_fs *c)
495 unsigned latest_version = bcachefs_metadata_version_current;
496 unsigned latest_compatible = min(latest_version,
497 bch2_latest_compatible_version(c->sb.version));
498 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version;
499 unsigned new_version = 0;
501 if (old_version < bcachefs_metadata_required_upgrade_below) {
502 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible ||
503 latest_compatible < bcachefs_metadata_required_upgrade_below)
504 new_version = latest_version;
506 new_version = latest_compatible;
508 switch (c->opts.version_upgrade) {
509 case BCH_VERSION_UPGRADE_compatible:
510 new_version = latest_compatible;
512 case BCH_VERSION_UPGRADE_incompatible:
513 new_version = latest_version;
515 case BCH_VERSION_UPGRADE_none:
516 new_version = min(old_version, latest_version);
521 if (new_version > old_version) {
522 struct printbuf buf = PRINTBUF;
524 if (old_version < bcachefs_metadata_required_upgrade_below)
525 prt_str(&buf, "Version upgrade required:\n");
527 if (old_version != c->sb.version) {
528 prt_str(&buf, "Version upgrade from ");
529 bch2_version_to_text(&buf, c->sb.version_upgrade_complete);
530 prt_str(&buf, " to ");
531 bch2_version_to_text(&buf, c->sb.version);
532 prt_str(&buf, " incomplete\n");
535 prt_printf(&buf, "Doing %s version upgrade from ",
536 BCH_VERSION_MAJOR(old_version) != BCH_VERSION_MAJOR(new_version)
537 ? "incompatible" : "compatible");
538 bch2_version_to_text(&buf, old_version);
539 prt_str(&buf, " to ");
540 bch2_version_to_text(&buf, new_version);
543 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
544 __le64 passes = ext->recovery_passes_required[0];
545 bch2_sb_set_upgrade(c, old_version, new_version);
546 passes = ext->recovery_passes_required[0] & ~passes;
549 prt_str(&buf, " running recovery passes: ");
550 prt_bitflags(&buf, bch2_recovery_passes,
551 bch2_recovery_passes_from_stable(le64_to_cpu(passes)));
554 bch_info(c, "%s", buf.buf);
556 bch2_sb_upgrade(c, new_version);
565 int bch2_fs_recovery(struct bch_fs *c)
567 struct bch_sb_field_clean *clean = NULL;
568 struct jset *last_journal_entry = NULL;
569 u64 last_seq = 0, blacklist_seq, journal_seq;
573 clean = bch2_read_superblock_clean(c);
574 ret = PTR_ERR_OR_ZERO(clean);
578 bch_info(c, "recovering from clean shutdown, journal seq %llu",
579 le64_to_cpu(clean->journal_seq));
581 bch_info(c, "recovering from unclean shutdown");
584 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) {
585 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported");
591 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
592 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
597 if (c->opts.norecovery)
598 c->opts.recovery_pass_last = BCH_RECOVERY_PASS_journal_replay - 1;
600 if (!c->opts.nochanges) {
601 mutex_lock(&c->sb_lock);
602 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
603 bool write_sb = false;
605 if (BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)) {
606 ext->recovery_passes_required[0] |=
607 cpu_to_le64(bch2_recovery_passes_to_stable(BIT_ULL(BCH_RECOVERY_PASS_check_topology)));
611 u64 sb_passes = bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0]));
613 struct printbuf buf = PRINTBUF;
614 prt_str(&buf, "superblock requires following recovery passes to be run:\n ");
615 prt_bitflags(&buf, bch2_recovery_passes, sb_passes);
616 bch_info(c, "%s", buf.buf);
620 if (bch2_check_version_downgrade(c)) {
621 struct printbuf buf = PRINTBUF;
623 prt_str(&buf, "Version downgrade required:");
625 __le64 passes = ext->recovery_passes_required[0];
626 bch2_sb_set_downgrade(c,
627 BCH_VERSION_MINOR(bcachefs_metadata_version_current),
628 BCH_VERSION_MINOR(c->sb.version));
629 passes = ext->recovery_passes_required[0] & ~passes;
631 prt_str(&buf, "\n running recovery passes: ");
632 prt_bitflags(&buf, bch2_recovery_passes,
633 bch2_recovery_passes_from_stable(le64_to_cpu(passes)));
636 bch_info(c, "%s", buf.buf);
641 if (check_version_upgrade(c))
647 c->recovery_passes_explicit |= bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0]));
648 mutex_unlock(&c->sb_lock);
651 if (c->opts.fsck && IS_ENABLED(CONFIG_BCACHEFS_DEBUG))
652 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_topology);
655 set_bit(BCH_FS_fsck_running, &c->flags);
657 ret = bch2_blacklist_table_initialize(c);
659 bch_err(c, "error initializing blacklist table");
663 if (!c->sb.clean || c->opts.fsck || c->opts.retain_recovery_info) {
664 struct genradix_iter iter;
665 struct journal_replay **i;
667 bch_verbose(c, "starting journal read");
668 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
673 * note: cmd_list_journal needs the blacklist table fully up to date so
674 * it can asterisk ignored journal entries:
676 if (c->opts.read_journal_only)
679 genradix_for_each_reverse(&c->journal_entries, iter, i)
680 if (!journal_replay_ignore(*i)) {
681 last_journal_entry = &(*i)->j;
685 if (mustfix_fsck_err_on(c->sb.clean &&
686 last_journal_entry &&
687 !journal_entry_empty(last_journal_entry), c,
688 clean_but_journal_not_empty,
689 "filesystem marked clean but journal not empty")) {
690 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
691 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
695 if (!last_journal_entry) {
696 fsck_err_on(!c->sb.clean, c,
697 dirty_but_no_journal_entries,
698 "no journal entries found");
702 genradix_for_each_reverse(&c->journal_entries, iter, i)
704 last_journal_entry = &(*i)->j;
705 (*i)->ignore_blacklisted = false;
706 (*i)->ignore_not_dirty= false;
708 * This was probably a NO_FLUSH entry,
709 * so last_seq was garbage - but we know
710 * we're only using a single journal
711 * entry, set it here:
713 (*i)->j.last_seq = (*i)->j.seq;
718 ret = bch2_journal_keys_sort(c);
722 if (c->sb.clean && last_journal_entry) {
723 ret = bch2_verify_superblock_clean(c, &clean,
731 bch_err(c, "no superblock clean section found");
732 ret = -BCH_ERR_fsck_repair_impossible;
736 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1;
739 c->journal_replay_seq_start = last_seq;
740 c->journal_replay_seq_end = blacklist_seq - 1;
742 if (c->opts.reconstruct_alloc)
743 bch2_reconstruct_alloc(c);
745 zero_out_btree_mem_ptr(&c->journal_keys);
747 ret = journal_replay_early(c, clean);
752 * After an unclean shutdown, skip then next few journal sequence
753 * numbers as they may have been referenced by btree writes that
754 * happened before their corresponding journal writes - those btree
755 * writes need to be ignored, by skipping and blacklisting the next few
756 * journal sequence numbers:
761 if (blacklist_seq != journal_seq) {
762 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu",
763 blacklist_seq, journal_seq) ?:
764 bch2_journal_seq_blacklist_add(c,
765 blacklist_seq, journal_seq);
767 bch_err_msg(c, ret, "error creating new journal seq blacklist entry");
772 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
773 journal_seq, last_seq, blacklist_seq - 1) ?:
774 bch2_fs_journal_start(&c->journal, journal_seq);
779 * Skip past versions that might have possibly been used (as nonces),
780 * but hadn't had their pointers written:
782 if (c->sb.encryption_type && !c->sb.clean)
783 atomic64_add(1 << 16, &c->key_version);
785 ret = read_btree_roots(c);
789 ret = bch2_run_recovery_passes(c);
793 clear_bit(BCH_FS_fsck_running, &c->flags);
795 /* fsync if we fixed errors */
796 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
797 bch2_journal_flush_all_pins(&c->journal);
798 bch2_journal_meta(&c->journal);
801 /* If we fixed errors, verify that fs is actually clean now: */
802 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
803 test_bit(BCH_FS_errors_fixed, &c->flags) &&
804 !test_bit(BCH_FS_errors_not_fixed, &c->flags) &&
805 !test_bit(BCH_FS_error, &c->flags)) {
806 bch2_flush_fsck_errs(c);
808 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean");
809 clear_bit(BCH_FS_errors_fixed, &c->flags);
811 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
813 ret = bch2_run_recovery_passes(c);
817 if (test_bit(BCH_FS_errors_fixed, &c->flags) ||
818 test_bit(BCH_FS_errors_not_fixed, &c->flags)) {
819 bch_err(c, "Second fsck run was not clean");
820 set_bit(BCH_FS_errors_not_fixed, &c->flags);
823 set_bit(BCH_FS_errors_fixed, &c->flags);
826 if (enabled_qtypes(c)) {
827 bch_verbose(c, "reading quotas");
828 ret = bch2_fs_quota_read(c);
831 bch_verbose(c, "quotas done");
834 mutex_lock(&c->sb_lock);
835 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
836 bool write_sb = false;
838 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != le16_to_cpu(c->disk_sb.sb->version)) {
839 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, le16_to_cpu(c->disk_sb.sb->version));
843 if (!test_bit(BCH_FS_error, &c->flags) &&
844 !(c->disk_sb.sb->compat[0] & cpu_to_le64(1ULL << BCH_COMPAT_alloc_info))) {
845 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
849 if (!test_bit(BCH_FS_error, &c->flags) &&
850 !bch2_is_zero(ext->errors_silent, sizeof(ext->errors_silent))) {
851 memset(ext->errors_silent, 0, sizeof(ext->errors_silent));
856 !test_bit(BCH_FS_error, &c->flags) &&
857 c->recovery_pass_done == BCH_RECOVERY_PASS_NR - 1 &&
858 ext->btrees_lost_data) {
859 ext->btrees_lost_data = 0;
864 !test_bit(BCH_FS_error, &c->flags) &&
865 !test_bit(BCH_FS_errors_not_fixed, &c->flags)) {
866 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
867 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0);
873 mutex_unlock(&c->sb_lock);
875 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) ||
876 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) {
877 struct bch_move_stats stats;
879 bch2_move_stats_init(&stats, "recovery");
881 struct printbuf buf = PRINTBUF;
882 bch2_version_to_text(&buf, c->sb.version_min);
883 bch_info(c, "scanning for old btree nodes: min_version %s", buf.buf);
886 ret = bch2_fs_read_write_early(c) ?:
887 bch2_scan_old_btree_nodes(c, &stats);
890 bch_info(c, "scanning for old btree nodes done");
893 if (c->journal_seq_blacklist_table &&
894 c->journal_seq_blacklist_table->nr > 128)
895 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
899 bch2_flush_fsck_errs(c);
901 if (!c->opts.retain_recovery_info) {
902 bch2_journal_keys_put_initial(c);
903 bch2_find_btree_nodes_exit(&c->found_btree_nodes);
909 test_bit(BCH_FS_need_delete_dead_snapshots, &c->flags) &&
910 !c->opts.nochanges) {
911 bch2_fs_read_write_early(c);
912 bch2_delete_dead_snapshots_async(c);
919 bch2_fs_emergency_read_only(c);
923 int bch2_fs_initialize(struct bch_fs *c)
925 struct bch_inode_unpacked root_inode, lostfound_inode;
926 struct bkey_inode_buf packed_inode;
927 struct qstr lostfound = QSTR("lost+found");
930 bch_notice(c, "initializing new filesystem");
931 set_bit(BCH_FS_new_fs, &c->flags);
933 mutex_lock(&c->sb_lock);
934 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
935 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
937 bch2_check_version_downgrade(c);
939 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) {
940 bch2_sb_upgrade(c, bcachefs_metadata_version_current);
941 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
944 mutex_unlock(&c->sb_lock);
946 c->curr_recovery_pass = BCH_RECOVERY_PASS_NR;
947 set_bit(BCH_FS_may_go_rw, &c->flags);
949 for (unsigned i = 0; i < BTREE_ID_NR; i++)
950 bch2_btree_root_alloc_fake(c, i, 0);
952 for_each_member_device(c, ca)
953 bch2_dev_usage_init(ca);
955 ret = bch2_fs_journal_alloc(c);
960 * journal_res_get() will crash if called before this has
961 * set up the journal.pin FIFO and journal.cur pointer:
963 bch2_fs_journal_start(&c->journal, 1);
964 bch2_journal_set_replay_done(&c->journal);
966 ret = bch2_fs_read_write_early(c);
971 * Write out the superblock and journal buckets, now that we can do
974 bch_verbose(c, "marking superblocks");
975 ret = bch2_trans_mark_dev_sbs(c);
976 bch_err_msg(c, ret, "marking superblocks");
980 for_each_online_member(c, ca)
981 ca->new_fs_bucket_idx = 0;
983 ret = bch2_fs_freespace_init(c);
987 ret = bch2_initialize_subvolumes(c);
991 bch_verbose(c, "reading snapshots table");
992 ret = bch2_snapshots_read(c);
995 bch_verbose(c, "reading snapshots done");
997 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL);
998 root_inode.bi_inum = BCACHEFS_ROOT_INO;
999 root_inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
1000 bch2_inode_pack(&packed_inode, &root_inode);
1001 packed_inode.inode.k.p.snapshot = U32_MAX;
1003 ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed_inode.inode.k_i, NULL, 0);
1004 bch_err_msg(c, ret, "creating root directory");
1008 bch2_inode_init_early(c, &lostfound_inode);
1010 ret = bch2_trans_do(c, NULL, NULL, 0,
1011 bch2_create_trans(trans,
1012 BCACHEFS_ROOT_SUBVOL_INUM,
1013 &root_inode, &lostfound_inode,
1015 0, 0, S_IFDIR|0700, 0,
1016 NULL, NULL, (subvol_inum) { 0 }, 0));
1017 bch_err_msg(c, ret, "creating lost+found");
1021 c->recovery_pass_done = BCH_RECOVERY_PASS_NR - 1;
1023 if (enabled_qtypes(c)) {
1024 ret = bch2_fs_quota_read(c);
1029 ret = bch2_journal_flush(&c->journal);
1030 bch_err_msg(c, ret, "writing first journal entry");
1034 mutex_lock(&c->sb_lock);
1035 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1036 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1038 bch2_write_super(c);
1039 mutex_unlock(&c->sb_lock);