1 // SPDX-License-Identifier: GPL-2.0
4 #include "backpointers.h"
6 #include "alloc_background.h"
8 #include "btree_journal_iter.h"
9 #include "btree_update.h"
10 #include "btree_update_interior.h"
17 #include "fs-common.h"
19 #include "journal_io.h"
20 #include "journal_reclaim.h"
21 #include "journal_seq_blacklist.h"
23 #include "logged_ops.h"
26 #include "rebalance.h"
31 #include "subvolume.h"
34 #include <linux/sort.h>
35 #include <linux/stat.h>
37 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
39 static bool btree_id_is_alloc(enum btree_id id)
43 case BTREE_ID_backpointers:
44 case BTREE_ID_need_discard:
45 case BTREE_ID_freespace:
46 case BTREE_ID_bucket_gens:
53 /* for -o reconstruct_alloc: */
54 static void drop_alloc_keys(struct journal_keys *keys)
58 for (src = 0, dst = 0; src < keys->nr; src++)
59 if (!btree_id_is_alloc(keys->d[src].btree_id))
60 keys->d[dst++] = keys->d[src];
66 * Btree node pointers have a field to stack a pointer to the in memory btree
67 * node; we need to zero out this field when reading in btree nodes, or when
68 * reading in keys from the journal:
70 static void zero_out_btree_mem_ptr(struct journal_keys *keys)
72 struct journal_key *i;
74 for (i = keys->d; i < keys->d + keys->nr; i++)
75 if (i->k->k.type == KEY_TYPE_btree_ptr_v2)
76 bkey_i_to_btree_ptr_v2(i->k)->v.mem_ptr = 0;
81 static void replay_now_at(struct journal *j, u64 seq)
83 BUG_ON(seq < j->replay_journal_seq);
85 seq = min(seq, j->replay_journal_seq_end);
87 while (j->replay_journal_seq < seq)
88 bch2_journal_pin_put(j, j->replay_journal_seq++);
91 static int bch2_journal_replay_key(struct btree_trans *trans,
92 struct journal_key *k)
94 struct btree_iter iter;
97 BTREE_ITER_NOT_EXTENTS;
98 unsigned update_flags = BTREE_TRIGGER_NORUN;
104 trans->journal_res.seq = k->journal_seq;
107 * BTREE_UPDATE_KEY_CACHE_RECLAIM disables key cache lookup/update to
108 * keep the key cache coherent with the underlying btree. Nothing
109 * besides the allocator is doing updates yet so we don't need key cache
110 * coherency for non-alloc btrees, and key cache fills for snapshots
111 * btrees use BTREE_ITER_FILTER_SNAPSHOTS, which isn't available until
112 * the snapshots recovery pass runs.
114 if (!k->level && k->btree_id == BTREE_ID_alloc)
115 iter_flags |= BTREE_ITER_CACHED;
117 update_flags |= BTREE_UPDATE_KEY_CACHE_RECLAIM;
119 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p,
120 BTREE_MAX_DEPTH, k->level,
122 ret = bch2_btree_iter_traverse(&iter);
126 /* Must be checked with btree locked: */
130 ret = bch2_trans_update(trans, &iter, k->k, update_flags);
132 bch2_trans_iter_exit(trans, &iter);
136 static int journal_sort_seq_cmp(const void *_l, const void *_r)
138 const struct journal_key *l = *((const struct journal_key **)_l);
139 const struct journal_key *r = *((const struct journal_key **)_r);
141 return cmp_int(l->journal_seq, r->journal_seq);
144 static int bch2_journal_replay(struct bch_fs *c)
146 struct journal_keys *keys = &c->journal_keys;
147 DARRAY(struct journal_key *) keys_sorted = { 0 };
148 struct journal *j = &c->journal;
149 u64 start_seq = c->journal_replay_seq_start;
150 u64 end_seq = c->journal_replay_seq_start;
151 struct btree_trans *trans = bch2_trans_get(c);
155 ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
156 keys->nr, start_seq, end_seq);
161 BUG_ON(!atomic_read(&keys->ref));
164 * First, attempt to replay keys in sorted order. This is more
165 * efficient - better locality of btree access - but some might fail if
166 * that would cause a journal deadlock.
168 for (size_t i = 0; i < keys->nr; i++) {
171 struct journal_key *k = keys->d + i;
173 /* Skip fastpath if we're low on space in the journal */
174 ret = c->journal.watermark ? -1 :
175 commit_do(trans, NULL, NULL,
176 BCH_TRANS_COMMIT_no_enospc|
177 BCH_TRANS_COMMIT_journal_reclaim|
178 (!k->allocated ? BCH_TRANS_COMMIT_no_journal_res : 0),
179 bch2_journal_replay_key(trans, k));
180 BUG_ON(!ret && !k->overwritten);
182 ret = darray_push(&keys_sorted, k);
189 * Now, replay any remaining keys in the order in which they appear in
190 * the journal, unpinning those journal entries as we go:
192 sort(keys_sorted.data, keys_sorted.nr,
193 sizeof(keys_sorted.data[0]),
194 journal_sort_seq_cmp, NULL);
196 darray_for_each(keys_sorted, kp) {
199 struct journal_key *k = *kp;
201 replay_now_at(j, k->journal_seq);
203 ret = commit_do(trans, NULL, NULL,
204 BCH_TRANS_COMMIT_no_enospc|
206 ? BCH_TRANS_COMMIT_no_journal_res|BCH_WATERMARK_reclaim
208 bch2_journal_replay_key(trans, k));
209 bch_err_msg(c, ret, "while replaying key at btree %s level %u:",
210 bch2_btree_id_str(k->btree_id), k->level);
214 BUG_ON(!k->overwritten);
218 * We need to put our btree_trans before calling flush_all_pins(), since
219 * that will use a btree_trans internally
221 bch2_trans_put(trans);
224 if (!c->opts.keep_journal)
225 bch2_journal_keys_put_initial(c);
227 replay_now_at(j, j->replay_journal_seq_end);
228 j->replay_journal_seq = 0;
230 bch2_journal_set_replay_done(j);
233 bch2_journal_log_msg(c, "journal replay finished");
236 bch2_trans_put(trans);
237 darray_exit(&keys_sorted);
242 /* journal replay early: */
244 static int journal_replay_entry_early(struct bch_fs *c,
245 struct jset_entry *entry)
249 switch (entry->type) {
250 case BCH_JSET_ENTRY_btree_root: {
251 struct btree_root *r;
253 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) {
254 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL });
259 r = bch2_btree_id_root(c, entry->btree_id);
262 r->level = entry->level;
263 bkey_copy(&r->key, (struct bkey_i *) entry->start);
271 case BCH_JSET_ENTRY_usage: {
272 struct jset_entry_usage *u =
273 container_of(entry, struct jset_entry_usage, entry);
275 switch (entry->btree_id) {
276 case BCH_FS_USAGE_reserved:
277 if (entry->level < BCH_REPLICAS_MAX)
278 c->usage_base->persistent_reserved[entry->level] =
281 case BCH_FS_USAGE_inodes:
282 c->usage_base->nr_inodes = le64_to_cpu(u->v);
284 case BCH_FS_USAGE_key_version:
285 atomic64_set(&c->key_version,
292 case BCH_JSET_ENTRY_data_usage: {
293 struct jset_entry_data_usage *u =
294 container_of(entry, struct jset_entry_data_usage, entry);
296 ret = bch2_replicas_set_usage(c, &u->r,
300 case BCH_JSET_ENTRY_dev_usage: {
301 struct jset_entry_dev_usage *u =
302 container_of(entry, struct jset_entry_dev_usage, entry);
303 struct bch_dev *ca = bch_dev_bkey_exists(c, le32_to_cpu(u->dev));
304 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
306 for (i = 0; i < min_t(unsigned, nr_types, BCH_DATA_NR); i++) {
307 ca->usage_base->d[i].buckets = le64_to_cpu(u->d[i].buckets);
308 ca->usage_base->d[i].sectors = le64_to_cpu(u->d[i].sectors);
309 ca->usage_base->d[i].fragmented = le64_to_cpu(u->d[i].fragmented);
314 case BCH_JSET_ENTRY_blacklist: {
315 struct jset_entry_blacklist *bl_entry =
316 container_of(entry, struct jset_entry_blacklist, entry);
318 ret = bch2_journal_seq_blacklist_add(c,
319 le64_to_cpu(bl_entry->seq),
320 le64_to_cpu(bl_entry->seq) + 1);
323 case BCH_JSET_ENTRY_blacklist_v2: {
324 struct jset_entry_blacklist_v2 *bl_entry =
325 container_of(entry, struct jset_entry_blacklist_v2, entry);
327 ret = bch2_journal_seq_blacklist_add(c,
328 le64_to_cpu(bl_entry->start),
329 le64_to_cpu(bl_entry->end) + 1);
332 case BCH_JSET_ENTRY_clock: {
333 struct jset_entry_clock *clock =
334 container_of(entry, struct jset_entry_clock, entry);
336 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time));
343 static int journal_replay_early(struct bch_fs *c,
344 struct bch_sb_field_clean *clean)
347 for (struct jset_entry *entry = clean->start;
348 entry != vstruct_end(&clean->field);
349 entry = vstruct_next(entry)) {
350 int ret = journal_replay_entry_early(c, entry);
355 struct genradix_iter iter;
356 struct journal_replay *i, **_i;
358 genradix_for_each(&c->journal_entries, iter, _i) {
364 vstruct_for_each(&i->j, entry) {
365 int ret = journal_replay_entry_early(c, entry);
372 bch2_fs_usage_initialize(c);
377 /* sb clean section: */
379 static int read_btree_roots(struct bch_fs *c)
384 for (i = 0; i < btree_id_nr_alive(c); i++) {
385 struct btree_root *r = bch2_btree_id_root(c, i);
390 if (btree_id_is_alloc(i) &&
391 c->opts.reconstruct_alloc) {
392 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
399 ? FSCK_CAN_IGNORE : 0,
400 btree_root_bkey_invalid,
401 "invalid btree root %s",
402 bch2_btree_id_str(i));
403 if (i == BTREE_ID_alloc)
404 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
407 ret = bch2_btree_root_read(c, i, &r->key, r->level);
410 btree_root_read_error,
411 "error reading btree root %s",
412 bch2_btree_id_str(i));
413 if (btree_id_is_alloc(i))
414 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
419 for (i = 0; i < BTREE_ID_NR; i++) {
420 struct btree_root *r = bch2_btree_id_root(c, i);
425 bch2_btree_root_alloc(c, i);
432 static int bch2_initialize_subvolumes(struct bch_fs *c)
434 struct bkey_i_snapshot_tree root_tree;
435 struct bkey_i_snapshot root_snapshot;
436 struct bkey_i_subvolume root_volume;
439 bkey_snapshot_tree_init(&root_tree.k_i);
440 root_tree.k.p.offset = 1;
441 root_tree.v.master_subvol = cpu_to_le32(1);
442 root_tree.v.root_snapshot = cpu_to_le32(U32_MAX);
444 bkey_snapshot_init(&root_snapshot.k_i);
445 root_snapshot.k.p.offset = U32_MAX;
446 root_snapshot.v.flags = 0;
447 root_snapshot.v.parent = 0;
448 root_snapshot.v.subvol = cpu_to_le32(BCACHEFS_ROOT_SUBVOL);
449 root_snapshot.v.tree = cpu_to_le32(1);
450 SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
452 bkey_subvolume_init(&root_volume.k_i);
453 root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
454 root_volume.v.flags = 0;
455 root_volume.v.snapshot = cpu_to_le32(U32_MAX);
456 root_volume.v.inode = cpu_to_le64(BCACHEFS_ROOT_INO);
458 ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees, &root_tree.k_i, NULL, 0) ?:
459 bch2_btree_insert(c, BTREE_ID_snapshots, &root_snapshot.k_i, NULL, 0) ?:
460 bch2_btree_insert(c, BTREE_ID_subvolumes, &root_volume.k_i, NULL, 0);
465 static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
467 struct btree_iter iter;
469 struct bch_inode_unpacked inode;
472 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
473 SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
478 if (!bkey_is_inode(k.k)) {
479 bch_err(trans->c, "root inode not found");
480 ret = -BCH_ERR_ENOENT_inode;
484 ret = bch2_inode_unpack(k, &inode);
487 inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
489 ret = bch2_inode_write(trans, &iter, &inode);
491 bch2_trans_iter_exit(trans, &iter);
495 /* set bi_subvol on root inode */
497 static int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
499 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,
500 __bch2_fs_upgrade_for_subvolumes(trans));
505 const char * const bch2_recovery_passes[] = {
506 #define x(_fn, _when) #_fn,
507 BCH_RECOVERY_PASSES()
512 static int bch2_check_allocations(struct bch_fs *c)
514 return bch2_gc(c, true, c->opts.norecovery);
517 static int bch2_set_may_go_rw(struct bch_fs *c)
519 struct journal_keys *keys = &c->journal_keys;
522 * After we go RW, the journal keys buffer can't be modified (except for
523 * setting journal_key->overwritten: it will be accessed by multiple
526 move_gap(keys->d, keys->nr, keys->size, keys->gap, keys->nr);
527 keys->gap = keys->nr;
529 set_bit(BCH_FS_may_go_rw, &c->flags);
531 if (keys->nr || c->opts.fsck || !c->sb.clean)
532 return bch2_fs_read_write_early(c);
536 struct recovery_pass_fn {
537 int (*fn)(struct bch_fs *);
541 static struct recovery_pass_fn recovery_pass_fns[] = {
542 #define x(_fn, _when) { .fn = bch2_##_fn, .when = _when },
543 BCH_RECOVERY_PASSES()
547 static void check_version_upgrade(struct bch_fs *c)
549 unsigned latest_compatible = bch2_latest_compatible_version(c->sb.version);
550 unsigned latest_version = bcachefs_metadata_version_current;
551 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version;
552 unsigned new_version = 0;
555 if (old_version < bcachefs_metadata_required_upgrade_below) {
556 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible ||
557 latest_compatible < bcachefs_metadata_required_upgrade_below)
558 new_version = latest_version;
560 new_version = latest_compatible;
562 switch (c->opts.version_upgrade) {
563 case BCH_VERSION_UPGRADE_compatible:
564 new_version = latest_compatible;
566 case BCH_VERSION_UPGRADE_incompatible:
567 new_version = latest_version;
569 case BCH_VERSION_UPGRADE_none:
570 new_version = old_version;
575 if (new_version > old_version) {
576 struct printbuf buf = PRINTBUF;
578 if (old_version < bcachefs_metadata_required_upgrade_below)
579 prt_str(&buf, "Version upgrade required:\n");
581 if (old_version != c->sb.version) {
582 prt_str(&buf, "Version upgrade from ");
583 bch2_version_to_text(&buf, c->sb.version_upgrade_complete);
584 prt_str(&buf, " to ");
585 bch2_version_to_text(&buf, c->sb.version);
586 prt_str(&buf, " incomplete\n");
589 prt_printf(&buf, "Doing %s version upgrade from ",
590 BCH_VERSION_MAJOR(old_version) != BCH_VERSION_MAJOR(new_version)
591 ? "incompatible" : "compatible");
592 bch2_version_to_text(&buf, old_version);
593 prt_str(&buf, " to ");
594 bch2_version_to_text(&buf, new_version);
597 recovery_passes = bch2_upgrade_recovery_passes(c, old_version, new_version);
598 if (recovery_passes) {
599 if ((recovery_passes & RECOVERY_PASS_ALL_FSCK) == RECOVERY_PASS_ALL_FSCK)
600 prt_str(&buf, "fsck required");
602 prt_str(&buf, "running recovery passes: ");
603 prt_bitflags(&buf, bch2_recovery_passes, recovery_passes);
606 c->recovery_passes_explicit |= recovery_passes;
607 c->opts.fix_errors = FSCK_FIX_yes;
610 bch_info(c, "%s", buf.buf);
612 mutex_lock(&c->sb_lock);
613 bch2_sb_upgrade(c, new_version);
614 mutex_unlock(&c->sb_lock);
620 u64 bch2_fsck_recovery_passes(void)
624 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++)
625 if (recovery_pass_fns[i].when & PASS_FSCK)
630 static bool should_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
632 struct recovery_pass_fn *p = recovery_pass_fns + pass;
634 if (c->opts.norecovery && pass > BCH_RECOVERY_PASS_snapshots_read)
636 if (c->recovery_passes_explicit & BIT_ULL(pass))
638 if ((p->when & PASS_FSCK) && c->opts.fsck)
640 if ((p->when & PASS_UNCLEAN) && !c->sb.clean)
642 if (p->when & PASS_ALWAYS)
647 static int bch2_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
649 struct recovery_pass_fn *p = recovery_pass_fns + pass;
652 if (!(p->when & PASS_SILENT))
653 bch2_print(c, KERN_INFO bch2_log_msg(c, "%s..."),
654 bch2_recovery_passes[pass]);
658 if (!(p->when & PASS_SILENT))
659 bch2_print(c, KERN_CONT " done\n");
664 static int bch2_run_recovery_passes(struct bch_fs *c)
668 while (c->curr_recovery_pass < ARRAY_SIZE(recovery_pass_fns)) {
669 if (should_run_recovery_pass(c, c->curr_recovery_pass)) {
670 ret = bch2_run_recovery_pass(c, c->curr_recovery_pass);
671 if (bch2_err_matches(ret, BCH_ERR_restart_recovery))
676 c->recovery_passes_complete |= BIT_ULL(c->curr_recovery_pass);
678 c->curr_recovery_pass++;
679 c->recovery_pass_done = max(c->recovery_pass_done, c->curr_recovery_pass);
685 int bch2_run_online_recovery_passes(struct bch_fs *c)
689 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++) {
690 struct recovery_pass_fn *p = recovery_pass_fns + i;
692 if (!(p->when & PASS_ONLINE))
695 ret = bch2_run_recovery_pass(c, i);
696 if (bch2_err_matches(ret, BCH_ERR_restart_recovery)) {
697 i = c->curr_recovery_pass;
707 int bch2_fs_recovery(struct bch_fs *c)
709 struct bch_sb_field_clean *clean = NULL;
710 struct jset *last_journal_entry = NULL;
711 u64 last_seq = 0, blacklist_seq, journal_seq;
712 bool write_sb = false;
716 clean = bch2_read_superblock_clean(c);
717 ret = PTR_ERR_OR_ZERO(clean);
721 bch_info(c, "recovering from clean shutdown, journal seq %llu",
722 le64_to_cpu(clean->journal_seq));
724 bch_info(c, "recovering from unclean shutdown");
727 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) {
728 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported");
734 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
735 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
740 if (c->opts.fsck || !(c->opts.nochanges && c->opts.norecovery))
741 check_version_upgrade(c);
743 if (c->opts.fsck && c->opts.norecovery) {
744 bch_err(c, "cannot select both norecovery and fsck");
749 ret = bch2_blacklist_table_initialize(c);
751 bch_err(c, "error initializing blacklist table");
755 if (!c->sb.clean || c->opts.fsck || c->opts.keep_journal) {
756 struct genradix_iter iter;
757 struct journal_replay **i;
759 bch_verbose(c, "starting journal read");
760 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
765 * note: cmd_list_journal needs the blacklist table fully up to date so
766 * it can asterisk ignored journal entries:
768 if (c->opts.read_journal_only)
771 genradix_for_each_reverse(&c->journal_entries, iter, i)
772 if (*i && !(*i)->ignore) {
773 last_journal_entry = &(*i)->j;
777 if (mustfix_fsck_err_on(c->sb.clean &&
778 last_journal_entry &&
779 !journal_entry_empty(last_journal_entry), c,
780 clean_but_journal_not_empty,
781 "filesystem marked clean but journal not empty")) {
782 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
783 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
787 if (!last_journal_entry) {
788 fsck_err_on(!c->sb.clean, c,
789 dirty_but_no_journal_entries,
790 "no journal entries found");
794 genradix_for_each_reverse(&c->journal_entries, iter, i)
796 last_journal_entry = &(*i)->j;
797 (*i)->ignore = false;
799 * This was probably a NO_FLUSH entry,
800 * so last_seq was garbage - but we know
801 * we're only using a single journal
802 * entry, set it here:
804 (*i)->j.last_seq = (*i)->j.seq;
809 ret = bch2_journal_keys_sort(c);
813 if (c->sb.clean && last_journal_entry) {
814 ret = bch2_verify_superblock_clean(c, &clean,
822 bch_err(c, "no superblock clean section found");
823 ret = -BCH_ERR_fsck_repair_impossible;
827 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1;
830 c->journal_replay_seq_start = last_seq;
831 c->journal_replay_seq_end = blacklist_seq - 1;
833 if (c->opts.reconstruct_alloc) {
834 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
835 drop_alloc_keys(&c->journal_keys);
838 zero_out_btree_mem_ptr(&c->journal_keys);
840 ret = journal_replay_early(c, clean);
845 * After an unclean shutdown, skip then next few journal sequence
846 * numbers as they may have been referenced by btree writes that
847 * happened before their corresponding journal writes - those btree
848 * writes need to be ignored, by skipping and blacklisting the next few
849 * journal sequence numbers:
854 if (blacklist_seq != journal_seq) {
855 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu",
856 blacklist_seq, journal_seq) ?:
857 bch2_journal_seq_blacklist_add(c,
858 blacklist_seq, journal_seq);
860 bch_err(c, "error creating new journal seq blacklist entry");
865 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
866 journal_seq, last_seq, blacklist_seq - 1) ?:
867 bch2_fs_journal_start(&c->journal, journal_seq);
871 if (c->opts.reconstruct_alloc)
872 bch2_journal_log_msg(c, "dropping alloc info");
875 * Skip past versions that might have possibly been used (as nonces),
876 * but hadn't had their pointers written:
878 if (c->sb.encryption_type && !c->sb.clean)
879 atomic64_add(1 << 16, &c->key_version);
881 ret = read_btree_roots(c);
886 (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) ||
887 BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)))
888 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_topology);
890 ret = bch2_run_recovery_passes(c);
894 /* If we fixed errors, verify that fs is actually clean now: */
895 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
896 test_bit(BCH_FS_errors_fixed, &c->flags) &&
897 !test_bit(BCH_FS_errors_not_fixed, &c->flags) &&
898 !test_bit(BCH_FS_error, &c->flags)) {
899 bch2_flush_fsck_errs(c);
901 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean");
902 clear_bit(BCH_FS_errors_fixed, &c->flags);
904 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
906 ret = bch2_run_recovery_passes(c);
910 if (test_bit(BCH_FS_errors_fixed, &c->flags) ||
911 test_bit(BCH_FS_errors_not_fixed, &c->flags)) {
912 bch_err(c, "Second fsck run was not clean");
913 set_bit(BCH_FS_errors_not_fixed, &c->flags);
916 set_bit(BCH_FS_errors_fixed, &c->flags);
919 if (enabled_qtypes(c)) {
920 bch_verbose(c, "reading quotas");
921 ret = bch2_fs_quota_read(c);
924 bch_verbose(c, "quotas done");
927 mutex_lock(&c->sb_lock);
928 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != c->sb.version) {
929 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, c->sb.version);
933 if (!test_bit(BCH_FS_error, &c->flags)) {
934 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
939 !test_bit(BCH_FS_error, &c->flags) &&
940 !test_bit(BCH_FS_errors_not_fixed, &c->flags)) {
941 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
942 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0);
948 mutex_unlock(&c->sb_lock);
950 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) ||
951 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) {
952 struct bch_move_stats stats;
954 bch2_move_stats_init(&stats, "recovery");
956 struct printbuf buf = PRINTBUF;
957 bch2_version_to_text(&buf, c->sb.version_min);
958 bch_info(c, "scanning for old btree nodes: min_version %s", buf.buf);
961 ret = bch2_fs_read_write_early(c) ?:
962 bch2_scan_old_btree_nodes(c, &stats);
965 bch_info(c, "scanning for old btree nodes done");
968 if (c->journal_seq_blacklist_table &&
969 c->journal_seq_blacklist_table->nr > 128)
970 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
974 set_bit(BCH_FS_fsck_done, &c->flags);
975 bch2_flush_fsck_errs(c);
977 if (!c->opts.keep_journal &&
978 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
979 bch2_journal_keys_put_initial(c);
982 if (!ret && test_bit(BCH_FS_need_delete_dead_snapshots, &c->flags)) {
983 bch2_fs_read_write_early(c);
984 bch2_delete_dead_snapshots_async(c);
991 bch2_fs_emergency_read_only(c);
995 int bch2_fs_initialize(struct bch_fs *c)
997 struct bch_inode_unpacked root_inode, lostfound_inode;
998 struct bkey_inode_buf packed_inode;
999 struct qstr lostfound = QSTR("lost+found");
1002 bch_notice(c, "initializing new filesystem");
1004 mutex_lock(&c->sb_lock);
1005 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1006 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1008 bch2_sb_maybe_downgrade(c);
1010 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) {
1011 bch2_sb_upgrade(c, bcachefs_metadata_version_current);
1012 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1013 bch2_write_super(c);
1015 mutex_unlock(&c->sb_lock);
1017 c->curr_recovery_pass = ARRAY_SIZE(recovery_pass_fns);
1018 set_bit(BCH_FS_may_go_rw, &c->flags);
1019 set_bit(BCH_FS_fsck_done, &c->flags);
1021 for (unsigned i = 0; i < BTREE_ID_NR; i++)
1022 bch2_btree_root_alloc(c, i);
1024 for_each_member_device(c, ca)
1025 bch2_dev_usage_init(ca);
1027 ret = bch2_fs_journal_alloc(c);
1032 * journal_res_get() will crash if called before this has
1033 * set up the journal.pin FIFO and journal.cur pointer:
1035 bch2_fs_journal_start(&c->journal, 1);
1036 bch2_journal_set_replay_done(&c->journal);
1038 ret = bch2_fs_read_write_early(c);
1043 * Write out the superblock and journal buckets, now that we can do
1046 bch_verbose(c, "marking superblocks");
1047 ret = bch2_trans_mark_dev_sbs(c);
1048 bch_err_msg(c, ret, "marking superblocks");
1052 for_each_online_member(c, ca)
1053 ca->new_fs_bucket_idx = 0;
1055 ret = bch2_fs_freespace_init(c);
1059 ret = bch2_initialize_subvolumes(c);
1063 bch_verbose(c, "reading snapshots table");
1064 ret = bch2_snapshots_read(c);
1067 bch_verbose(c, "reading snapshots done");
1069 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL);
1070 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1071 root_inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
1072 bch2_inode_pack(&packed_inode, &root_inode);
1073 packed_inode.inode.k.p.snapshot = U32_MAX;
1075 ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed_inode.inode.k_i, NULL, 0);
1076 bch_err_msg(c, ret, "creating root directory");
1080 bch2_inode_init_early(c, &lostfound_inode);
1082 ret = bch2_trans_do(c, NULL, NULL, 0,
1083 bch2_create_trans(trans,
1084 BCACHEFS_ROOT_SUBVOL_INUM,
1085 &root_inode, &lostfound_inode,
1087 0, 0, S_IFDIR|0700, 0,
1088 NULL, NULL, (subvol_inum) { 0 }, 0));
1089 bch_err_msg(c, ret, "creating lost+found");
1093 c->recovery_pass_done = ARRAY_SIZE(recovery_pass_fns) - 1;
1095 if (enabled_qtypes(c)) {
1096 ret = bch2_fs_quota_read(c);
1101 ret = bch2_journal_flush(&c->journal);
1102 bch_err_msg(c, ret, "writing first journal entry");
1106 mutex_lock(&c->sb_lock);
1107 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1108 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1110 bch2_write_super(c);
1111 mutex_unlock(&c->sb_lock);