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"
28 #include "subvolume.h"
31 #include <linux/sort.h>
32 #include <linux/stat.h>
34 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
36 /* for -o reconstruct_alloc: */
37 static void drop_alloc_keys(struct journal_keys *keys)
41 for (src = 0, dst = 0; src < keys->nr; src++)
42 if (keys->d[src].btree_id != BTREE_ID_alloc)
43 keys->d[dst++] = keys->d[src];
49 * Btree node pointers have a field to stack a pointer to the in memory btree
50 * node; we need to zero out this field when reading in btree nodes, or when
51 * reading in keys from the journal:
53 static void zero_out_btree_mem_ptr(struct journal_keys *keys)
55 struct journal_key *i;
57 for (i = keys->d; i < keys->d + keys->nr; i++)
58 if (i->k->k.type == KEY_TYPE_btree_ptr_v2)
59 bkey_i_to_btree_ptr_v2(i->k)->v.mem_ptr = 0;
64 static void replay_now_at(struct journal *j, u64 seq)
66 BUG_ON(seq < j->replay_journal_seq);
68 seq = min(seq, j->replay_journal_seq_end);
70 while (j->replay_journal_seq < seq)
71 bch2_journal_pin_put(j, j->replay_journal_seq++);
74 static int bch2_journal_replay_key(struct btree_trans *trans,
75 struct journal_key *k)
77 struct btree_iter iter;
80 BTREE_ITER_NOT_EXTENTS;
81 unsigned update_flags = BTREE_TRIGGER_NORUN;
85 * BTREE_UPDATE_KEY_CACHE_RECLAIM disables key cache lookup/update to
86 * keep the key cache coherent with the underlying btree. Nothing
87 * besides the allocator is doing updates yet so we don't need key cache
88 * coherency for non-alloc btrees, and key cache fills for snapshots
89 * btrees use BTREE_ITER_FILTER_SNAPSHOTS, which isn't available until
90 * the snapshots recovery pass runs.
92 if (!k->level && k->btree_id == BTREE_ID_alloc)
93 iter_flags |= BTREE_ITER_CACHED;
95 update_flags |= BTREE_UPDATE_KEY_CACHE_RECLAIM;
97 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p,
98 BTREE_MAX_DEPTH, k->level,
100 ret = bch2_btree_iter_traverse(&iter);
104 /* Must be checked with btree locked: */
108 ret = bch2_trans_update(trans, &iter, k->k, update_flags);
110 bch2_trans_iter_exit(trans, &iter);
114 static int journal_sort_seq_cmp(const void *_l, const void *_r)
116 const struct journal_key *l = *((const struct journal_key **)_l);
117 const struct journal_key *r = *((const struct journal_key **)_r);
119 return cmp_int(l->journal_seq, r->journal_seq);
122 static int bch2_journal_replay(struct bch_fs *c)
124 struct journal_keys *keys = &c->journal_keys;
125 struct journal_key **keys_sorted, *k;
126 struct journal *j = &c->journal;
127 u64 start_seq = c->journal_replay_seq_start;
128 u64 end_seq = c->journal_replay_seq_start;
132 move_gap(keys->d, keys->nr, keys->size, keys->gap, keys->nr);
133 keys->gap = keys->nr;
135 keys_sorted = kvmalloc_array(sizeof(*keys_sorted), keys->nr, GFP_KERNEL);
137 return -BCH_ERR_ENOMEM_journal_replay;
139 for (i = 0; i < keys->nr; i++)
140 keys_sorted[i] = &keys->d[i];
142 sort(keys_sorted, keys->nr,
143 sizeof(keys_sorted[0]),
144 journal_sort_seq_cmp, NULL);
147 ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
148 keys->nr, start_seq, end_seq);
153 for (i = 0; i < keys->nr; i++) {
158 replay_now_at(j, k->journal_seq);
160 ret = bch2_trans_do(c, NULL, NULL,
161 BTREE_INSERT_LAZY_RW|
164 ? BTREE_INSERT_JOURNAL_REPLAY|BCH_WATERMARK_reclaim
166 bch2_journal_replay_key(&trans, k));
168 bch_err(c, "journal replay: error while replaying key at btree %s level %u: %s",
169 bch2_btree_ids[k->btree_id], k->level, bch2_err_str(ret));
174 replay_now_at(j, j->replay_journal_seq_end);
175 j->replay_journal_seq = 0;
177 bch2_journal_set_replay_done(j);
178 bch2_journal_flush_all_pins(j);
179 ret = bch2_journal_error(j);
181 if (keys->nr && !ret)
182 bch2_journal_log_msg(c, "journal replay finished");
191 /* journal replay early: */
193 static int journal_replay_entry_early(struct bch_fs *c,
194 struct jset_entry *entry)
198 switch (entry->type) {
199 case BCH_JSET_ENTRY_btree_root: {
200 struct btree_root *r;
202 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) {
203 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL });
208 r = bch2_btree_id_root(c, entry->btree_id);
211 r->level = entry->level;
212 bkey_copy(&r->key, &entry->start[0]);
220 case BCH_JSET_ENTRY_usage: {
221 struct jset_entry_usage *u =
222 container_of(entry, struct jset_entry_usage, entry);
224 switch (entry->btree_id) {
225 case BCH_FS_USAGE_reserved:
226 if (entry->level < BCH_REPLICAS_MAX)
227 c->usage_base->persistent_reserved[entry->level] =
230 case BCH_FS_USAGE_inodes:
231 c->usage_base->nr_inodes = le64_to_cpu(u->v);
233 case BCH_FS_USAGE_key_version:
234 atomic64_set(&c->key_version,
241 case BCH_JSET_ENTRY_data_usage: {
242 struct jset_entry_data_usage *u =
243 container_of(entry, struct jset_entry_data_usage, entry);
245 ret = bch2_replicas_set_usage(c, &u->r,
249 case BCH_JSET_ENTRY_dev_usage: {
250 struct jset_entry_dev_usage *u =
251 container_of(entry, struct jset_entry_dev_usage, entry);
252 struct bch_dev *ca = bch_dev_bkey_exists(c, le32_to_cpu(u->dev));
253 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
255 ca->usage_base->buckets_ec = le64_to_cpu(u->buckets_ec);
257 for (i = 0; i < min_t(unsigned, nr_types, BCH_DATA_NR); i++) {
258 ca->usage_base->d[i].buckets = le64_to_cpu(u->d[i].buckets);
259 ca->usage_base->d[i].sectors = le64_to_cpu(u->d[i].sectors);
260 ca->usage_base->d[i].fragmented = le64_to_cpu(u->d[i].fragmented);
265 case BCH_JSET_ENTRY_blacklist: {
266 struct jset_entry_blacklist *bl_entry =
267 container_of(entry, struct jset_entry_blacklist, entry);
269 ret = bch2_journal_seq_blacklist_add(c,
270 le64_to_cpu(bl_entry->seq),
271 le64_to_cpu(bl_entry->seq) + 1);
274 case BCH_JSET_ENTRY_blacklist_v2: {
275 struct jset_entry_blacklist_v2 *bl_entry =
276 container_of(entry, struct jset_entry_blacklist_v2, entry);
278 ret = bch2_journal_seq_blacklist_add(c,
279 le64_to_cpu(bl_entry->start),
280 le64_to_cpu(bl_entry->end) + 1);
283 case BCH_JSET_ENTRY_clock: {
284 struct jset_entry_clock *clock =
285 container_of(entry, struct jset_entry_clock, entry);
287 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time));
294 static int journal_replay_early(struct bch_fs *c,
295 struct bch_sb_field_clean *clean)
297 struct jset_entry *entry;
301 for (entry = clean->start;
302 entry != vstruct_end(&clean->field);
303 entry = vstruct_next(entry)) {
304 ret = journal_replay_entry_early(c, entry);
309 struct genradix_iter iter;
310 struct journal_replay *i, **_i;
312 genradix_for_each(&c->journal_entries, iter, _i) {
318 vstruct_for_each(&i->j, entry) {
319 ret = journal_replay_entry_early(c, entry);
326 bch2_fs_usage_initialize(c);
331 /* sb clean section: */
333 static bool btree_id_is_alloc(enum btree_id id)
337 case BTREE_ID_backpointers:
338 case BTREE_ID_need_discard:
339 case BTREE_ID_freespace:
340 case BTREE_ID_bucket_gens:
347 static int read_btree_roots(struct bch_fs *c)
352 for (i = 0; i < btree_id_nr_alive(c); i++) {
353 struct btree_root *r = bch2_btree_id_root(c, i);
358 if (btree_id_is_alloc(i) &&
359 c->opts.reconstruct_alloc) {
360 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
365 __fsck_err(c, btree_id_is_alloc(i)
366 ? FSCK_CAN_IGNORE : 0,
367 "invalid btree root %s",
369 if (i == BTREE_ID_alloc)
370 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
373 ret = bch2_btree_root_read(c, i, &r->key, r->level);
377 ? FSCK_CAN_IGNORE : 0,
378 "error reading btree root %s",
380 if (btree_id_is_alloc(i))
381 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
385 for (i = 0; i < BTREE_ID_NR; i++) {
386 struct btree_root *r = bch2_btree_id_root(c, i);
391 bch2_btree_root_alloc(c, i);
398 static int bch2_initialize_subvolumes(struct bch_fs *c)
400 struct bkey_i_snapshot_tree root_tree;
401 struct bkey_i_snapshot root_snapshot;
402 struct bkey_i_subvolume root_volume;
405 bkey_snapshot_tree_init(&root_tree.k_i);
406 root_tree.k.p.offset = 1;
407 root_tree.v.master_subvol = cpu_to_le32(1);
408 root_tree.v.root_snapshot = cpu_to_le32(U32_MAX);
410 bkey_snapshot_init(&root_snapshot.k_i);
411 root_snapshot.k.p.offset = U32_MAX;
412 root_snapshot.v.flags = 0;
413 root_snapshot.v.parent = 0;
414 root_snapshot.v.subvol = cpu_to_le32(BCACHEFS_ROOT_SUBVOL);
415 root_snapshot.v.tree = cpu_to_le32(1);
416 SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
418 bkey_subvolume_init(&root_volume.k_i);
419 root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
420 root_volume.v.flags = 0;
421 root_volume.v.snapshot = cpu_to_le32(U32_MAX);
422 root_volume.v.inode = cpu_to_le64(BCACHEFS_ROOT_INO);
424 ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees,
427 bch2_btree_insert(c, BTREE_ID_snapshots,
430 bch2_btree_insert(c, BTREE_ID_subvolumes,
438 static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
440 struct btree_iter iter;
442 struct bch_inode_unpacked inode;
445 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
446 SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
451 if (!bkey_is_inode(k.k)) {
452 bch_err(trans->c, "root inode not found");
453 ret = -BCH_ERR_ENOENT_inode;
457 ret = bch2_inode_unpack(k, &inode);
460 inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
462 ret = bch2_inode_write(trans, &iter, &inode);
464 bch2_trans_iter_exit(trans, &iter);
468 /* set bi_subvol on root inode */
470 static int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
472 int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
473 __bch2_fs_upgrade_for_subvolumes(&trans));
479 const char * const bch2_recovery_passes[] = {
480 #define x(_fn, _when) #_fn,
481 BCH_RECOVERY_PASSES()
486 static int bch2_check_allocations(struct bch_fs *c)
488 return bch2_gc(c, true, c->opts.norecovery);
491 static int bch2_set_may_go_rw(struct bch_fs *c)
493 set_bit(BCH_FS_MAY_GO_RW, &c->flags);
497 struct recovery_pass_fn {
498 int (*fn)(struct bch_fs *);
502 static struct recovery_pass_fn recovery_pass_fns[] = {
503 #define x(_fn, _when) { .fn = bch2_##_fn, .when = _when },
504 BCH_RECOVERY_PASSES()
508 static void check_version_upgrade(struct bch_fs *c)
510 unsigned latest_compatible = bch2_version_compatible(c->sb.version);
511 unsigned latest_version = bcachefs_metadata_version_current;
512 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version;
513 unsigned new_version = 0;
516 if (old_version < bcachefs_metadata_required_upgrade_below) {
517 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible ||
518 latest_compatible < bcachefs_metadata_required_upgrade_below)
519 new_version = latest_version;
521 new_version = latest_compatible;
523 switch (c->opts.version_upgrade) {
524 case BCH_VERSION_UPGRADE_compatible:
525 new_version = latest_compatible;
527 case BCH_VERSION_UPGRADE_incompatible:
528 new_version = latest_version;
530 case BCH_VERSION_UPGRADE_none:
531 new_version = old_version;
536 if (new_version > old_version) {
537 struct printbuf buf = PRINTBUF;
539 if (old_version < bcachefs_metadata_required_upgrade_below)
540 prt_str(&buf, "Version upgrade required:\n");
542 if (old_version != c->sb.version) {
543 prt_str(&buf, "Version upgrade from ");
544 bch2_version_to_text(&buf, c->sb.version_upgrade_complete);
545 prt_str(&buf, " to ");
546 bch2_version_to_text(&buf, c->sb.version);
547 prt_str(&buf, " incomplete\n");
550 prt_printf(&buf, "Doing %s version upgrade from ",
551 BCH_VERSION_MAJOR(old_version) != BCH_VERSION_MAJOR(new_version)
552 ? "incompatible" : "compatible");
553 bch2_version_to_text(&buf, old_version);
554 prt_str(&buf, " to ");
555 bch2_version_to_text(&buf, new_version);
558 recovery_passes = bch2_upgrade_recovery_passes(c, old_version, new_version);
559 if (recovery_passes) {
560 if ((recovery_passes & RECOVERY_PASS_ALL_FSCK) == RECOVERY_PASS_ALL_FSCK)
561 prt_str(&buf, "fsck required");
563 prt_str(&buf, "running recovery passses: ");
564 prt_bitflags(&buf, bch2_recovery_passes, recovery_passes);
567 c->recovery_passes_explicit |= recovery_passes;
568 c->opts.fix_errors = FSCK_FIX_yes;
571 bch_info(c, "%s", buf.buf);
573 mutex_lock(&c->sb_lock);
574 bch2_sb_upgrade(c, new_version);
575 mutex_unlock(&c->sb_lock);
581 u64 bch2_fsck_recovery_passes(void)
585 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++)
586 if (recovery_pass_fns[i].when & PASS_FSCK)
591 static bool should_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
593 struct recovery_pass_fn *p = recovery_pass_fns + c->curr_recovery_pass;
595 if (c->opts.norecovery && pass > BCH_RECOVERY_PASS_snapshots_read)
597 if (c->recovery_passes_explicit & BIT_ULL(pass))
599 if ((p->when & PASS_FSCK) && c->opts.fsck)
601 if ((p->when & PASS_UNCLEAN) && !c->sb.clean)
603 if (p->when & PASS_ALWAYS)
608 static int bch2_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
612 c->curr_recovery_pass = pass;
614 if (should_run_recovery_pass(c, pass)) {
615 struct recovery_pass_fn *p = recovery_pass_fns + pass;
617 if (!(p->when & PASS_SILENT))
618 printk(KERN_INFO bch2_log_msg(c, "%s..."),
619 bch2_recovery_passes[pass]);
623 if (!(p->when & PASS_SILENT))
624 printk(KERN_CONT " done\n");
626 c->recovery_passes_complete |= BIT_ULL(pass);
632 static int bch2_run_recovery_passes(struct bch_fs *c)
636 while (c->curr_recovery_pass < ARRAY_SIZE(recovery_pass_fns)) {
637 ret = bch2_run_recovery_pass(c, c->curr_recovery_pass);
638 if (bch2_err_matches(ret, BCH_ERR_restart_recovery))
642 c->curr_recovery_pass++;
648 int bch2_fs_recovery(struct bch_fs *c)
650 struct bch_sb_field_clean *clean = NULL;
651 struct jset *last_journal_entry = NULL;
652 u64 last_seq, blacklist_seq, journal_seq;
653 bool write_sb = false;
657 clean = bch2_read_superblock_clean(c);
658 ret = PTR_ERR_OR_ZERO(clean);
662 bch_info(c, "recovering from clean shutdown, journal seq %llu",
663 le64_to_cpu(clean->journal_seq));
665 bch_info(c, "recovering from unclean shutdown");
668 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) {
669 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported");
675 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
676 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
681 if (c->opts.fsck || !(c->opts.nochanges && c->opts.norecovery))
682 check_version_upgrade(c);
684 if (c->opts.fsck && c->opts.norecovery) {
685 bch_err(c, "cannot select both norecovery and fsck");
690 ret = bch2_blacklist_table_initialize(c);
692 bch_err(c, "error initializing blacklist table");
696 if (!c->sb.clean || c->opts.fsck || c->opts.keep_journal) {
697 struct genradix_iter iter;
698 struct journal_replay **i;
700 bch_verbose(c, "starting journal read");
701 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
706 * note: cmd_list_journal needs the blacklist table fully up to date so
707 * it can asterisk ignored journal entries:
709 if (c->opts.read_journal_only)
712 genradix_for_each_reverse(&c->journal_entries, iter, i)
713 if (*i && !(*i)->ignore) {
714 last_journal_entry = &(*i)->j;
718 if (mustfix_fsck_err_on(c->sb.clean &&
719 last_journal_entry &&
720 !journal_entry_empty(last_journal_entry), c,
721 "filesystem marked clean but journal not empty")) {
722 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
723 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
727 if (!last_journal_entry) {
728 fsck_err_on(!c->sb.clean, c, "no journal entries found");
732 genradix_for_each_reverse(&c->journal_entries, iter, i)
734 last_journal_entry = &(*i)->j;
735 (*i)->ignore = false;
740 ret = bch2_journal_keys_sort(c);
744 if (c->sb.clean && last_journal_entry) {
745 ret = bch2_verify_superblock_clean(c, &clean,
753 bch_err(c, "no superblock clean section found");
754 ret = -BCH_ERR_fsck_repair_impossible;
758 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1;
761 c->journal_replay_seq_start = last_seq;
762 c->journal_replay_seq_end = blacklist_seq - 1;;
764 if (c->opts.reconstruct_alloc) {
765 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
766 drop_alloc_keys(&c->journal_keys);
769 zero_out_btree_mem_ptr(&c->journal_keys);
771 ret = journal_replay_early(c, clean);
776 * After an unclean shutdown, skip then next few journal sequence
777 * numbers as they may have been referenced by btree writes that
778 * happened before their corresponding journal writes - those btree
779 * writes need to be ignored, by skipping and blacklisting the next few
780 * journal sequence numbers:
785 if (blacklist_seq != journal_seq) {
786 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu",
787 blacklist_seq, journal_seq) ?:
788 bch2_journal_seq_blacklist_add(c,
789 blacklist_seq, journal_seq);
791 bch_err(c, "error creating new journal seq blacklist entry");
796 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
797 journal_seq, last_seq, blacklist_seq - 1) ?:
798 bch2_fs_journal_start(&c->journal, journal_seq);
802 if (c->opts.reconstruct_alloc)
803 bch2_journal_log_msg(c, "dropping alloc info");
806 * Skip past versions that might have possibly been used (as nonces),
807 * but hadn't had their pointers written:
809 if (c->sb.encryption_type && !c->sb.clean)
810 atomic64_add(1 << 16, &c->key_version);
812 ret = read_btree_roots(c);
817 (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) ||
818 BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)))
819 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_topology);
821 ret = bch2_run_recovery_passes(c);
825 /* If we fixed errors, verify that fs is actually clean now: */
826 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
827 test_bit(BCH_FS_ERRORS_FIXED, &c->flags) &&
828 !test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags) &&
829 !test_bit(BCH_FS_ERROR, &c->flags)) {
830 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean");
831 clear_bit(BCH_FS_ERRORS_FIXED, &c->flags);
833 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
835 ret = bch2_run_recovery_passes(c);
839 if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags) ||
840 test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
841 bch_err(c, "Second fsck run was not clean");
842 set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
845 set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
848 if (enabled_qtypes(c)) {
849 bch_verbose(c, "reading quotas");
850 ret = bch2_fs_quota_read(c);
853 bch_verbose(c, "quotas done");
856 mutex_lock(&c->sb_lock);
857 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != c->sb.version) {
858 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, c->sb.version);
862 if (!test_bit(BCH_FS_ERROR, &c->flags)) {
863 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
868 !test_bit(BCH_FS_ERROR, &c->flags) &&
869 !test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
870 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
871 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0);
877 mutex_unlock(&c->sb_lock);
879 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) ||
880 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) {
881 struct bch_move_stats stats;
883 bch2_move_stats_init(&stats, "recovery");
885 bch_info(c, "scanning for old btree nodes");
886 ret = bch2_fs_read_write(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 set_bit(BCH_FS_FSCK_DONE, &c->flags);
900 bch2_flush_fsck_errs(c);
902 if (!c->opts.keep_journal &&
903 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags)) {
904 bch2_journal_keys_free(&c->journal_keys);
905 bch2_journal_entries_free(c);
909 if (!ret && test_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags)) {
910 bch2_fs_read_write_early(c);
911 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");
932 bch_notice(c, "initializing new filesystem");
934 mutex_lock(&c->sb_lock);
935 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
936 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
938 bch2_sb_maybe_downgrade(c);
940 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) {
941 bch2_sb_upgrade(c, bcachefs_metadata_version_current);
942 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
945 mutex_unlock(&c->sb_lock);
947 c->curr_recovery_pass = ARRAY_SIZE(recovery_pass_fns);
948 set_bit(BCH_FS_MAY_GO_RW, &c->flags);
949 set_bit(BCH_FS_FSCK_DONE, &c->flags);
951 for (i = 0; i < BTREE_ID_NR; i++)
952 bch2_btree_root_alloc(c, i);
954 for_each_online_member(ca, c, i)
955 bch2_dev_usage_init(ca);
957 for_each_online_member(ca, c, i) {
958 ret = bch2_dev_journal_alloc(ca);
960 percpu_ref_put(&ca->io_ref);
966 * journal_res_get() will crash if called before this has
967 * set up the journal.pin FIFO and journal.cur pointer:
969 bch2_fs_journal_start(&c->journal, 1);
970 bch2_journal_set_replay_done(&c->journal);
972 ret = bch2_fs_read_write_early(c);
977 * Write out the superblock and journal buckets, now that we can do
980 bch_verbose(c, "marking superblocks");
981 for_each_member_device(ca, c, i) {
982 ret = bch2_trans_mark_dev_sb(c, ca);
984 percpu_ref_put(&ca->ref);
988 ca->new_fs_bucket_idx = 0;
991 ret = bch2_fs_freespace_init(c);
995 ret = bch2_initialize_subvolumes(c);
999 bch_verbose(c, "reading snapshots table");
1000 ret = bch2_snapshots_read(c);
1003 bch_verbose(c, "reading snapshots done");
1005 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL);
1006 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1007 root_inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
1008 bch2_inode_pack(&packed_inode, &root_inode);
1009 packed_inode.inode.k.p.snapshot = U32_MAX;
1011 ret = bch2_btree_insert(c, BTREE_ID_inodes,
1012 &packed_inode.inode.k_i,
1015 bch_err_msg(c, ret, "creating root directory");
1019 bch2_inode_init_early(c, &lostfound_inode);
1021 ret = bch2_trans_do(c, NULL, NULL, 0,
1022 bch2_create_trans(&trans,
1023 BCACHEFS_ROOT_SUBVOL_INUM,
1024 &root_inode, &lostfound_inode,
1026 0, 0, S_IFDIR|0700, 0,
1027 NULL, NULL, (subvol_inum) { 0 }, 0));
1029 bch_err_msg(c, ret, "creating lost+found");
1033 if (enabled_qtypes(c)) {
1034 ret = bch2_fs_quota_read(c);
1039 ret = bch2_journal_flush(&c->journal);
1041 bch_err_msg(c, ret, "writing first journal entry");
1045 mutex_lock(&c->sb_lock);
1046 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1047 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1049 bch2_write_super(c);
1050 mutex_unlock(&c->sb_lock);
1054 bch_err_fn(ca, ret);