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_key **kp;
149 struct journal *j = &c->journal;
150 u64 start_seq = c->journal_replay_seq_start;
151 u64 end_seq = c->journal_replay_seq_start;
152 struct btree_trans *trans = bch2_trans_get(c);
156 ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
157 keys->nr, start_seq, end_seq);
162 BUG_ON(!atomic_read(&keys->ref));
165 * First, attempt to replay keys in sorted order. This is more
166 * efficient - better locality of btree access - but some might fail if
167 * that would cause a journal deadlock.
169 for (size_t i = 0; i < keys->nr; i++) {
172 struct journal_key *k = keys->d + i;
174 ret = commit_do(trans, NULL, NULL,
175 BCH_TRANS_COMMIT_no_enospc|
176 BCH_TRANS_COMMIT_journal_reclaim|
177 (!k->allocated ? BCH_TRANS_COMMIT_no_journal_res : 0),
178 bch2_journal_replay_key(trans, k));
179 BUG_ON(!ret && !k->overwritten);
181 ret = darray_push(&keys_sorted, k);
188 * Now, replay any remaining keys in the order in which they appear in
189 * the journal, unpinning those journal entries as we go:
191 sort(keys_sorted.data, keys_sorted.nr,
192 sizeof(keys_sorted.data[0]),
193 journal_sort_seq_cmp, NULL);
195 darray_for_each(keys_sorted, kp) {
198 struct journal_key *k = *kp;
200 replay_now_at(j, k->journal_seq);
202 ret = commit_do(trans, NULL, NULL,
203 BCH_TRANS_COMMIT_no_enospc|
205 ? BCH_TRANS_COMMIT_no_journal_res|BCH_WATERMARK_reclaim
207 bch2_journal_replay_key(trans, k));
208 bch_err_msg(c, ret, "while replaying key at btree %s level %u:",
209 bch2_btree_id_str(k->btree_id), k->level);
213 BUG_ON(!k->overwritten);
217 * We need to put our btree_trans before calling flush_all_pins(), since
218 * that will use a btree_trans internally
220 bch2_trans_put(trans);
223 if (!c->opts.keep_journal)
224 bch2_journal_keys_put_initial(c);
226 replay_now_at(j, j->replay_journal_seq_end);
227 j->replay_journal_seq = 0;
229 bch2_journal_set_replay_done(j);
232 bch2_journal_log_msg(c, "journal replay finished");
235 bch2_trans_put(trans);
236 darray_exit(&keys_sorted);
241 /* journal replay early: */
243 static int journal_replay_entry_early(struct bch_fs *c,
244 struct jset_entry *entry)
248 switch (entry->type) {
249 case BCH_JSET_ENTRY_btree_root: {
250 struct btree_root *r;
252 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) {
253 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL });
258 r = bch2_btree_id_root(c, entry->btree_id);
261 r->level = entry->level;
262 bkey_copy(&r->key, (struct bkey_i *) entry->start);
270 case BCH_JSET_ENTRY_usage: {
271 struct jset_entry_usage *u =
272 container_of(entry, struct jset_entry_usage, entry);
274 switch (entry->btree_id) {
275 case BCH_FS_USAGE_reserved:
276 if (entry->level < BCH_REPLICAS_MAX)
277 c->usage_base->persistent_reserved[entry->level] =
280 case BCH_FS_USAGE_inodes:
281 c->usage_base->nr_inodes = le64_to_cpu(u->v);
283 case BCH_FS_USAGE_key_version:
284 atomic64_set(&c->key_version,
291 case BCH_JSET_ENTRY_data_usage: {
292 struct jset_entry_data_usage *u =
293 container_of(entry, struct jset_entry_data_usage, entry);
295 ret = bch2_replicas_set_usage(c, &u->r,
299 case BCH_JSET_ENTRY_dev_usage: {
300 struct jset_entry_dev_usage *u =
301 container_of(entry, struct jset_entry_dev_usage, entry);
302 struct bch_dev *ca = bch_dev_bkey_exists(c, le32_to_cpu(u->dev));
303 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
305 ca->usage_base->buckets_ec = le64_to_cpu(u->buckets_ec);
307 for (i = 0; i < min_t(unsigned, nr_types, BCH_DATA_NR); i++) {
308 ca->usage_base->d[i].buckets = le64_to_cpu(u->d[i].buckets);
309 ca->usage_base->d[i].sectors = le64_to_cpu(u->d[i].sectors);
310 ca->usage_base->d[i].fragmented = le64_to_cpu(u->d[i].fragmented);
315 case BCH_JSET_ENTRY_blacklist: {
316 struct jset_entry_blacklist *bl_entry =
317 container_of(entry, struct jset_entry_blacklist, entry);
319 ret = bch2_journal_seq_blacklist_add(c,
320 le64_to_cpu(bl_entry->seq),
321 le64_to_cpu(bl_entry->seq) + 1);
324 case BCH_JSET_ENTRY_blacklist_v2: {
325 struct jset_entry_blacklist_v2 *bl_entry =
326 container_of(entry, struct jset_entry_blacklist_v2, entry);
328 ret = bch2_journal_seq_blacklist_add(c,
329 le64_to_cpu(bl_entry->start),
330 le64_to_cpu(bl_entry->end) + 1);
333 case BCH_JSET_ENTRY_clock: {
334 struct jset_entry_clock *clock =
335 container_of(entry, struct jset_entry_clock, entry);
337 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time));
344 static int journal_replay_early(struct bch_fs *c,
345 struct bch_sb_field_clean *clean)
347 struct jset_entry *entry;
351 for (entry = clean->start;
352 entry != vstruct_end(&clean->field);
353 entry = vstruct_next(entry)) {
354 ret = journal_replay_entry_early(c, entry);
359 struct genradix_iter iter;
360 struct journal_replay *i, **_i;
362 genradix_for_each(&c->journal_entries, iter, _i) {
368 vstruct_for_each(&i->j, entry) {
369 ret = journal_replay_entry_early(c, entry);
376 bch2_fs_usage_initialize(c);
381 /* sb clean section: */
383 static int read_btree_roots(struct bch_fs *c)
388 for (i = 0; i < btree_id_nr_alive(c); i++) {
389 struct btree_root *r = bch2_btree_id_root(c, i);
394 if (btree_id_is_alloc(i) &&
395 c->opts.reconstruct_alloc) {
396 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
403 ? FSCK_CAN_IGNORE : 0,
404 btree_root_bkey_invalid,
405 "invalid btree root %s",
406 bch2_btree_id_str(i));
407 if (i == BTREE_ID_alloc)
408 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
411 ret = bch2_btree_root_read(c, i, &r->key, r->level);
414 btree_root_read_error,
415 "error reading btree root %s",
416 bch2_btree_id_str(i));
417 if (btree_id_is_alloc(i))
418 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
423 for (i = 0; i < BTREE_ID_NR; i++) {
424 struct btree_root *r = bch2_btree_id_root(c, i);
429 bch2_btree_root_alloc(c, i);
436 static int bch2_initialize_subvolumes(struct bch_fs *c)
438 struct bkey_i_snapshot_tree root_tree;
439 struct bkey_i_snapshot root_snapshot;
440 struct bkey_i_subvolume root_volume;
443 bkey_snapshot_tree_init(&root_tree.k_i);
444 root_tree.k.p.offset = 1;
445 root_tree.v.master_subvol = cpu_to_le32(1);
446 root_tree.v.root_snapshot = cpu_to_le32(U32_MAX);
448 bkey_snapshot_init(&root_snapshot.k_i);
449 root_snapshot.k.p.offset = U32_MAX;
450 root_snapshot.v.flags = 0;
451 root_snapshot.v.parent = 0;
452 root_snapshot.v.subvol = cpu_to_le32(BCACHEFS_ROOT_SUBVOL);
453 root_snapshot.v.tree = cpu_to_le32(1);
454 SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
456 bkey_subvolume_init(&root_volume.k_i);
457 root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
458 root_volume.v.flags = 0;
459 root_volume.v.snapshot = cpu_to_le32(U32_MAX);
460 root_volume.v.inode = cpu_to_le64(BCACHEFS_ROOT_INO);
462 ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees, &root_tree.k_i, NULL, 0) ?:
463 bch2_btree_insert(c, BTREE_ID_snapshots, &root_snapshot.k_i, NULL, 0) ?:
464 bch2_btree_insert(c, BTREE_ID_subvolumes, &root_volume.k_i, NULL, 0);
470 static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
472 struct btree_iter iter;
474 struct bch_inode_unpacked inode;
477 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
478 SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
483 if (!bkey_is_inode(k.k)) {
484 bch_err(trans->c, "root inode not found");
485 ret = -BCH_ERR_ENOENT_inode;
489 ret = bch2_inode_unpack(k, &inode);
492 inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
494 ret = bch2_inode_write(trans, &iter, &inode);
496 bch2_trans_iter_exit(trans, &iter);
500 /* set bi_subvol on root inode */
502 static int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
504 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,
505 __bch2_fs_upgrade_for_subvolumes(trans));
511 const char * const bch2_recovery_passes[] = {
512 #define x(_fn, _when) #_fn,
513 BCH_RECOVERY_PASSES()
518 static int bch2_check_allocations(struct bch_fs *c)
520 return bch2_gc(c, true, c->opts.norecovery);
523 static int bch2_set_may_go_rw(struct bch_fs *c)
525 struct journal_keys *keys = &c->journal_keys;
528 * After we go RW, the journal keys buffer can't be modified (except for
529 * setting journal_key->overwritten: it will be accessed by multiple
532 move_gap(keys->d, keys->nr, keys->size, keys->gap, keys->nr);
533 keys->gap = keys->nr;
535 set_bit(BCH_FS_MAY_GO_RW, &c->flags);
537 return bch2_fs_read_write_early(c);
541 struct recovery_pass_fn {
542 int (*fn)(struct bch_fs *);
546 static struct recovery_pass_fn recovery_pass_fns[] = {
547 #define x(_fn, _when) { .fn = bch2_##_fn, .when = _when },
548 BCH_RECOVERY_PASSES()
552 static void check_version_upgrade(struct bch_fs *c)
554 unsigned latest_compatible = bch2_latest_compatible_version(c->sb.version);
555 unsigned latest_version = bcachefs_metadata_version_current;
556 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version;
557 unsigned new_version = 0;
560 if (old_version < bcachefs_metadata_required_upgrade_below) {
561 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible ||
562 latest_compatible < bcachefs_metadata_required_upgrade_below)
563 new_version = latest_version;
565 new_version = latest_compatible;
567 switch (c->opts.version_upgrade) {
568 case BCH_VERSION_UPGRADE_compatible:
569 new_version = latest_compatible;
571 case BCH_VERSION_UPGRADE_incompatible:
572 new_version = latest_version;
574 case BCH_VERSION_UPGRADE_none:
575 new_version = old_version;
580 if (new_version > old_version) {
581 struct printbuf buf = PRINTBUF;
583 if (old_version < bcachefs_metadata_required_upgrade_below)
584 prt_str(&buf, "Version upgrade required:\n");
586 if (old_version != c->sb.version) {
587 prt_str(&buf, "Version upgrade from ");
588 bch2_version_to_text(&buf, c->sb.version_upgrade_complete);
589 prt_str(&buf, " to ");
590 bch2_version_to_text(&buf, c->sb.version);
591 prt_str(&buf, " incomplete\n");
594 prt_printf(&buf, "Doing %s version upgrade from ",
595 BCH_VERSION_MAJOR(old_version) != BCH_VERSION_MAJOR(new_version)
596 ? "incompatible" : "compatible");
597 bch2_version_to_text(&buf, old_version);
598 prt_str(&buf, " to ");
599 bch2_version_to_text(&buf, new_version);
602 recovery_passes = bch2_upgrade_recovery_passes(c, old_version, new_version);
603 if (recovery_passes) {
604 if ((recovery_passes & RECOVERY_PASS_ALL_FSCK) == RECOVERY_PASS_ALL_FSCK)
605 prt_str(&buf, "fsck required");
607 prt_str(&buf, "running recovery passes: ");
608 prt_bitflags(&buf, bch2_recovery_passes, recovery_passes);
611 c->recovery_passes_explicit |= recovery_passes;
612 c->opts.fix_errors = FSCK_FIX_yes;
615 bch_info(c, "%s", buf.buf);
617 mutex_lock(&c->sb_lock);
618 bch2_sb_upgrade(c, new_version);
619 mutex_unlock(&c->sb_lock);
625 u64 bch2_fsck_recovery_passes(void)
629 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++)
630 if (recovery_pass_fns[i].when & PASS_FSCK)
635 static bool should_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
637 struct recovery_pass_fn *p = recovery_pass_fns + c->curr_recovery_pass;
639 if (c->opts.norecovery && pass > BCH_RECOVERY_PASS_snapshots_read)
641 if (c->recovery_passes_explicit & BIT_ULL(pass))
643 if ((p->when & PASS_FSCK) && c->opts.fsck)
645 if ((p->when & PASS_UNCLEAN) && !c->sb.clean)
647 if (p->when & PASS_ALWAYS)
652 static int bch2_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
656 c->curr_recovery_pass = pass;
658 if (should_run_recovery_pass(c, pass)) {
659 struct recovery_pass_fn *p = recovery_pass_fns + pass;
661 if (!(p->when & PASS_SILENT))
662 printk(KERN_INFO bch2_log_msg(c, "%s..."),
663 bch2_recovery_passes[pass]);
667 if (!(p->when & PASS_SILENT))
668 printk(KERN_CONT " done\n");
670 c->recovery_passes_complete |= BIT_ULL(pass);
676 static int bch2_run_recovery_passes(struct bch_fs *c)
680 while (c->curr_recovery_pass < ARRAY_SIZE(recovery_pass_fns)) {
681 ret = bch2_run_recovery_pass(c, c->curr_recovery_pass);
682 if (bch2_err_matches(ret, BCH_ERR_restart_recovery))
686 c->curr_recovery_pass++;
692 int bch2_fs_recovery(struct bch_fs *c)
694 struct bch_sb_field_clean *clean = NULL;
695 struct jset *last_journal_entry = NULL;
696 u64 last_seq = 0, blacklist_seq, journal_seq;
697 bool write_sb = false;
701 clean = bch2_read_superblock_clean(c);
702 ret = PTR_ERR_OR_ZERO(clean);
706 bch_info(c, "recovering from clean shutdown, journal seq %llu",
707 le64_to_cpu(clean->journal_seq));
709 bch_info(c, "recovering from unclean shutdown");
712 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) {
713 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported");
719 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
720 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
725 if (c->opts.fsck || !(c->opts.nochanges && c->opts.norecovery))
726 check_version_upgrade(c);
728 if (c->opts.fsck && c->opts.norecovery) {
729 bch_err(c, "cannot select both norecovery and fsck");
734 ret = bch2_blacklist_table_initialize(c);
736 bch_err(c, "error initializing blacklist table");
740 if (!c->sb.clean || c->opts.fsck || c->opts.keep_journal) {
741 struct genradix_iter iter;
742 struct journal_replay **i;
744 bch_verbose(c, "starting journal read");
745 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
750 * note: cmd_list_journal needs the blacklist table fully up to date so
751 * it can asterisk ignored journal entries:
753 if (c->opts.read_journal_only)
756 genradix_for_each_reverse(&c->journal_entries, iter, i)
757 if (*i && !(*i)->ignore) {
758 last_journal_entry = &(*i)->j;
762 if (mustfix_fsck_err_on(c->sb.clean &&
763 last_journal_entry &&
764 !journal_entry_empty(last_journal_entry), c,
765 clean_but_journal_not_empty,
766 "filesystem marked clean but journal not empty")) {
767 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
768 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
772 if (!last_journal_entry) {
773 fsck_err_on(!c->sb.clean, c,
774 dirty_but_no_journal_entries,
775 "no journal entries found");
779 genradix_for_each_reverse(&c->journal_entries, iter, i)
781 last_journal_entry = &(*i)->j;
782 (*i)->ignore = false;
784 * This was probably a NO_FLUSH entry,
785 * so last_seq was garbage - but we know
786 * we're only using a single journal
787 * entry, set it here:
789 (*i)->j.last_seq = (*i)->j.seq;
794 ret = bch2_journal_keys_sort(c);
798 if (c->sb.clean && last_journal_entry) {
799 ret = bch2_verify_superblock_clean(c, &clean,
807 bch_err(c, "no superblock clean section found");
808 ret = -BCH_ERR_fsck_repair_impossible;
812 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1;
815 c->journal_replay_seq_start = last_seq;
816 c->journal_replay_seq_end = blacklist_seq - 1;
818 if (c->opts.reconstruct_alloc) {
819 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
820 drop_alloc_keys(&c->journal_keys);
823 zero_out_btree_mem_ptr(&c->journal_keys);
825 ret = journal_replay_early(c, clean);
830 * After an unclean shutdown, skip then next few journal sequence
831 * numbers as they may have been referenced by btree writes that
832 * happened before their corresponding journal writes - those btree
833 * writes need to be ignored, by skipping and blacklisting the next few
834 * journal sequence numbers:
839 if (blacklist_seq != journal_seq) {
840 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu",
841 blacklist_seq, journal_seq) ?:
842 bch2_journal_seq_blacklist_add(c,
843 blacklist_seq, journal_seq);
845 bch_err(c, "error creating new journal seq blacklist entry");
850 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
851 journal_seq, last_seq, blacklist_seq - 1) ?:
852 bch2_fs_journal_start(&c->journal, journal_seq);
856 if (c->opts.reconstruct_alloc)
857 bch2_journal_log_msg(c, "dropping alloc info");
860 * Skip past versions that might have possibly been used (as nonces),
861 * but hadn't had their pointers written:
863 if (c->sb.encryption_type && !c->sb.clean)
864 atomic64_add(1 << 16, &c->key_version);
866 ret = read_btree_roots(c);
871 (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) ||
872 BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)))
873 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_topology);
875 ret = bch2_run_recovery_passes(c);
879 /* If we fixed errors, verify that fs is actually clean now: */
880 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
881 test_bit(BCH_FS_ERRORS_FIXED, &c->flags) &&
882 !test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags) &&
883 !test_bit(BCH_FS_ERROR, &c->flags)) {
884 bch2_flush_fsck_errs(c);
886 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean");
887 clear_bit(BCH_FS_ERRORS_FIXED, &c->flags);
889 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
891 ret = bch2_run_recovery_passes(c);
895 if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags) ||
896 test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
897 bch_err(c, "Second fsck run was not clean");
898 set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
901 set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
904 if (enabled_qtypes(c)) {
905 bch_verbose(c, "reading quotas");
906 ret = bch2_fs_quota_read(c);
909 bch_verbose(c, "quotas done");
912 mutex_lock(&c->sb_lock);
913 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != c->sb.version) {
914 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, c->sb.version);
918 if (!test_bit(BCH_FS_ERROR, &c->flags)) {
919 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
924 !test_bit(BCH_FS_ERROR, &c->flags) &&
925 !test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
926 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
927 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0);
933 mutex_unlock(&c->sb_lock);
935 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) ||
936 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) {
937 struct bch_move_stats stats;
939 bch2_move_stats_init(&stats, "recovery");
941 struct printbuf buf = PRINTBUF;
942 bch2_version_to_text(&buf, c->sb.version_min);
943 bch_info(c, "scanning for old btree nodes: min_version %s", buf.buf);
946 ret = bch2_fs_read_write_early(c) ?:
947 bch2_scan_old_btree_nodes(c, &stats);
950 bch_info(c, "scanning for old btree nodes done");
953 if (c->journal_seq_blacklist_table &&
954 c->journal_seq_blacklist_table->nr > 128)
955 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
959 set_bit(BCH_FS_FSCK_DONE, &c->flags);
960 bch2_flush_fsck_errs(c);
962 if (!c->opts.keep_journal &&
963 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
964 bch2_journal_keys_put_initial(c);
967 if (!ret && test_bit(BCH_FS_NEED_DELETE_DEAD_SNAPSHOTS, &c->flags)) {
968 bch2_fs_read_write_early(c);
969 bch2_delete_dead_snapshots_async(c);
977 bch2_fs_emergency_read_only(c);
981 int bch2_fs_initialize(struct bch_fs *c)
983 struct bch_inode_unpacked root_inode, lostfound_inode;
984 struct bkey_inode_buf packed_inode;
985 struct qstr lostfound = QSTR("lost+found");
990 bch_notice(c, "initializing new filesystem");
992 mutex_lock(&c->sb_lock);
993 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
994 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
996 bch2_sb_maybe_downgrade(c);
998 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) {
999 bch2_sb_upgrade(c, bcachefs_metadata_version_current);
1000 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1001 bch2_write_super(c);
1003 mutex_unlock(&c->sb_lock);
1005 c->curr_recovery_pass = ARRAY_SIZE(recovery_pass_fns);
1006 set_bit(BCH_FS_MAY_GO_RW, &c->flags);
1007 set_bit(BCH_FS_FSCK_DONE, &c->flags);
1009 for (i = 0; i < BTREE_ID_NR; i++)
1010 bch2_btree_root_alloc(c, i);
1012 for_each_member_device(ca, c, i)
1013 bch2_dev_usage_init(ca);
1015 ret = bch2_fs_journal_alloc(c);
1020 * journal_res_get() will crash if called before this has
1021 * set up the journal.pin FIFO and journal.cur pointer:
1023 bch2_fs_journal_start(&c->journal, 1);
1024 bch2_journal_set_replay_done(&c->journal);
1026 ret = bch2_fs_read_write_early(c);
1031 * Write out the superblock and journal buckets, now that we can do
1034 bch_verbose(c, "marking superblocks");
1035 ret = bch2_trans_mark_dev_sbs(c);
1036 bch_err_msg(c, ret, "marking superblocks");
1040 for_each_online_member(ca, c, i)
1041 ca->new_fs_bucket_idx = 0;
1043 ret = bch2_fs_freespace_init(c);
1047 ret = bch2_initialize_subvolumes(c);
1051 bch_verbose(c, "reading snapshots table");
1052 ret = bch2_snapshots_read(c);
1055 bch_verbose(c, "reading snapshots done");
1057 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL);
1058 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1059 root_inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
1060 bch2_inode_pack(&packed_inode, &root_inode);
1061 packed_inode.inode.k.p.snapshot = U32_MAX;
1063 ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed_inode.inode.k_i, NULL, 0);
1065 bch_err_msg(c, ret, "creating root directory");
1069 bch2_inode_init_early(c, &lostfound_inode);
1071 ret = bch2_trans_do(c, NULL, NULL, 0,
1072 bch2_create_trans(trans,
1073 BCACHEFS_ROOT_SUBVOL_INUM,
1074 &root_inode, &lostfound_inode,
1076 0, 0, S_IFDIR|0700, 0,
1077 NULL, NULL, (subvol_inum) { 0 }, 0));
1079 bch_err_msg(c, ret, "creating lost+found");
1083 if (enabled_qtypes(c)) {
1084 ret = bch2_fs_quota_read(c);
1089 ret = bch2_journal_flush(&c->journal);
1091 bch_err_msg(c, ret, "writing first journal entry");
1095 mutex_lock(&c->sb_lock);
1096 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1097 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1099 bch2_write_super(c);
1100 mutex_unlock(&c->sb_lock);
1104 bch_err_fn(ca, ret);