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);
163 * First, attempt to replay keys in sorted order. This is more
164 * efficient, but some might fail if that would cause a journal
167 for (size_t i = 0; i < keys->nr; i++) {
170 struct journal_key *k = keys->d + i;
172 ret = commit_do(trans, NULL, NULL,
173 BCH_TRANS_COMMIT_no_enospc|
174 BCH_TRANS_COMMIT_journal_reclaim|
175 (!k->allocated ? BCH_TRANS_COMMIT_no_journal_res : 0),
176 bch2_journal_replay_key(trans, k));
177 BUG_ON(!ret && !k->overwritten);
179 ret = darray_push(&keys_sorted, k);
186 * Now, replay any remaining keys in the order in which they appear in
187 * the journal, unpinning those journal entries as we go:
189 sort(keys_sorted.data, keys_sorted.nr,
190 sizeof(keys_sorted.data[0]),
191 journal_sort_seq_cmp, NULL);
193 darray_for_each(keys_sorted, kp) {
196 struct journal_key *k = *kp;
198 replay_now_at(j, k->journal_seq);
200 ret = commit_do(trans, NULL, NULL,
201 BCH_TRANS_COMMIT_no_enospc|
203 ? BCH_TRANS_COMMIT_no_journal_res|BCH_WATERMARK_reclaim
205 bch2_journal_replay_key(trans, k));
206 bch_err_msg(c, ret, "while replaying key at btree %s level %u:",
207 bch2_btree_id_str(k->btree_id), k->level);
211 BUG_ON(!k->overwritten);
214 bch2_trans_put(trans);
217 replay_now_at(j, j->replay_journal_seq_end);
218 j->replay_journal_seq = 0;
220 bch2_journal_set_replay_done(j);
221 bch2_journal_flush_all_pins(j);
222 ret = bch2_journal_error(j);
224 if (keys->nr && !ret)
225 bch2_journal_log_msg(c, "journal replay finished");
228 bch2_trans_put(trans);
229 darray_exit(&keys_sorted);
234 /* journal replay early: */
236 static int journal_replay_entry_early(struct bch_fs *c,
237 struct jset_entry *entry)
241 switch (entry->type) {
242 case BCH_JSET_ENTRY_btree_root: {
243 struct btree_root *r;
245 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) {
246 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL });
251 r = bch2_btree_id_root(c, entry->btree_id);
254 r->level = entry->level;
255 bkey_copy(&r->key, (struct bkey_i *) entry->start);
263 case BCH_JSET_ENTRY_usage: {
264 struct jset_entry_usage *u =
265 container_of(entry, struct jset_entry_usage, entry);
267 switch (entry->btree_id) {
268 case BCH_FS_USAGE_reserved:
269 if (entry->level < BCH_REPLICAS_MAX)
270 c->usage_base->persistent_reserved[entry->level] =
273 case BCH_FS_USAGE_inodes:
274 c->usage_base->nr_inodes = le64_to_cpu(u->v);
276 case BCH_FS_USAGE_key_version:
277 atomic64_set(&c->key_version,
284 case BCH_JSET_ENTRY_data_usage: {
285 struct jset_entry_data_usage *u =
286 container_of(entry, struct jset_entry_data_usage, entry);
288 ret = bch2_replicas_set_usage(c, &u->r,
292 case BCH_JSET_ENTRY_dev_usage: {
293 struct jset_entry_dev_usage *u =
294 container_of(entry, struct jset_entry_dev_usage, entry);
295 struct bch_dev *ca = bch_dev_bkey_exists(c, le32_to_cpu(u->dev));
296 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
298 ca->usage_base->buckets_ec = le64_to_cpu(u->buckets_ec);
300 for (i = 0; i < min_t(unsigned, nr_types, BCH_DATA_NR); i++) {
301 ca->usage_base->d[i].buckets = le64_to_cpu(u->d[i].buckets);
302 ca->usage_base->d[i].sectors = le64_to_cpu(u->d[i].sectors);
303 ca->usage_base->d[i].fragmented = le64_to_cpu(u->d[i].fragmented);
308 case BCH_JSET_ENTRY_blacklist: {
309 struct jset_entry_blacklist *bl_entry =
310 container_of(entry, struct jset_entry_blacklist, entry);
312 ret = bch2_journal_seq_blacklist_add(c,
313 le64_to_cpu(bl_entry->seq),
314 le64_to_cpu(bl_entry->seq) + 1);
317 case BCH_JSET_ENTRY_blacklist_v2: {
318 struct jset_entry_blacklist_v2 *bl_entry =
319 container_of(entry, struct jset_entry_blacklist_v2, entry);
321 ret = bch2_journal_seq_blacklist_add(c,
322 le64_to_cpu(bl_entry->start),
323 le64_to_cpu(bl_entry->end) + 1);
326 case BCH_JSET_ENTRY_clock: {
327 struct jset_entry_clock *clock =
328 container_of(entry, struct jset_entry_clock, entry);
330 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time));
337 static int journal_replay_early(struct bch_fs *c,
338 struct bch_sb_field_clean *clean)
340 struct jset_entry *entry;
344 for (entry = clean->start;
345 entry != vstruct_end(&clean->field);
346 entry = vstruct_next(entry)) {
347 ret = journal_replay_entry_early(c, entry);
352 struct genradix_iter iter;
353 struct journal_replay *i, **_i;
355 genradix_for_each(&c->journal_entries, iter, _i) {
361 vstruct_for_each(&i->j, entry) {
362 ret = journal_replay_entry_early(c, entry);
369 bch2_fs_usage_initialize(c);
374 /* sb clean section: */
376 static int read_btree_roots(struct bch_fs *c)
381 for (i = 0; i < btree_id_nr_alive(c); i++) {
382 struct btree_root *r = bch2_btree_id_root(c, i);
387 if (btree_id_is_alloc(i) &&
388 c->opts.reconstruct_alloc) {
389 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
396 ? FSCK_CAN_IGNORE : 0,
397 btree_root_bkey_invalid,
398 "invalid btree root %s",
399 bch2_btree_id_str(i));
400 if (i == BTREE_ID_alloc)
401 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
404 ret = bch2_btree_root_read(c, i, &r->key, r->level);
407 btree_root_read_error,
408 "error reading btree root %s",
409 bch2_btree_id_str(i));
410 if (btree_id_is_alloc(i))
411 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
416 for (i = 0; i < BTREE_ID_NR; i++) {
417 struct btree_root *r = bch2_btree_id_root(c, i);
422 bch2_btree_root_alloc(c, i);
429 static int bch2_initialize_subvolumes(struct bch_fs *c)
431 struct bkey_i_snapshot_tree root_tree;
432 struct bkey_i_snapshot root_snapshot;
433 struct bkey_i_subvolume root_volume;
436 bkey_snapshot_tree_init(&root_tree.k_i);
437 root_tree.k.p.offset = 1;
438 root_tree.v.master_subvol = cpu_to_le32(1);
439 root_tree.v.root_snapshot = cpu_to_le32(U32_MAX);
441 bkey_snapshot_init(&root_snapshot.k_i);
442 root_snapshot.k.p.offset = U32_MAX;
443 root_snapshot.v.flags = 0;
444 root_snapshot.v.parent = 0;
445 root_snapshot.v.subvol = cpu_to_le32(BCACHEFS_ROOT_SUBVOL);
446 root_snapshot.v.tree = cpu_to_le32(1);
447 SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
449 bkey_subvolume_init(&root_volume.k_i);
450 root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
451 root_volume.v.flags = 0;
452 root_volume.v.snapshot = cpu_to_le32(U32_MAX);
453 root_volume.v.inode = cpu_to_le64(BCACHEFS_ROOT_INO);
455 ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees, &root_tree.k_i, NULL, 0) ?:
456 bch2_btree_insert(c, BTREE_ID_snapshots, &root_snapshot.k_i, NULL, 0) ?:
457 bch2_btree_insert(c, BTREE_ID_subvolumes, &root_volume.k_i, NULL, 0);
463 static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
465 struct btree_iter iter;
467 struct bch_inode_unpacked inode;
470 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
471 SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
476 if (!bkey_is_inode(k.k)) {
477 bch_err(trans->c, "root inode not found");
478 ret = -BCH_ERR_ENOENT_inode;
482 ret = bch2_inode_unpack(k, &inode);
485 inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
487 ret = bch2_inode_write(trans, &iter, &inode);
489 bch2_trans_iter_exit(trans, &iter);
493 /* set bi_subvol on root inode */
495 static int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
497 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,
498 __bch2_fs_upgrade_for_subvolumes(trans));
504 const char * const bch2_recovery_passes[] = {
505 #define x(_fn, _when) #_fn,
506 BCH_RECOVERY_PASSES()
511 static int bch2_check_allocations(struct bch_fs *c)
513 return bch2_gc(c, true, c->opts.norecovery);
516 static int bch2_set_may_go_rw(struct bch_fs *c)
518 struct journal_keys *keys = &c->journal_keys;
521 * After we go RW, the journal keys buffer can't be modified (except for
522 * setting journal_key->overwritten: it will be accessed by multiple
525 move_gap(keys->d, keys->nr, keys->size, keys->gap, keys->nr);
526 keys->gap = keys->nr;
528 set_bit(BCH_FS_MAY_GO_RW, &c->flags);
530 return bch2_fs_read_write_early(c);
534 struct recovery_pass_fn {
535 int (*fn)(struct bch_fs *);
539 static struct recovery_pass_fn recovery_pass_fns[] = {
540 #define x(_fn, _when) { .fn = bch2_##_fn, .when = _when },
541 BCH_RECOVERY_PASSES()
545 static void check_version_upgrade(struct bch_fs *c)
547 unsigned latest_compatible = bch2_latest_compatible_version(c->sb.version);
548 unsigned latest_version = bcachefs_metadata_version_current;
549 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version;
550 unsigned new_version = 0;
553 if (old_version < bcachefs_metadata_required_upgrade_below) {
554 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible ||
555 latest_compatible < bcachefs_metadata_required_upgrade_below)
556 new_version = latest_version;
558 new_version = latest_compatible;
560 switch (c->opts.version_upgrade) {
561 case BCH_VERSION_UPGRADE_compatible:
562 new_version = latest_compatible;
564 case BCH_VERSION_UPGRADE_incompatible:
565 new_version = latest_version;
567 case BCH_VERSION_UPGRADE_none:
568 new_version = old_version;
573 if (new_version > old_version) {
574 struct printbuf buf = PRINTBUF;
576 if (old_version < bcachefs_metadata_required_upgrade_below)
577 prt_str(&buf, "Version upgrade required:\n");
579 if (old_version != c->sb.version) {
580 prt_str(&buf, "Version upgrade from ");
581 bch2_version_to_text(&buf, c->sb.version_upgrade_complete);
582 prt_str(&buf, " to ");
583 bch2_version_to_text(&buf, c->sb.version);
584 prt_str(&buf, " incomplete\n");
587 prt_printf(&buf, "Doing %s version upgrade from ",
588 BCH_VERSION_MAJOR(old_version) != BCH_VERSION_MAJOR(new_version)
589 ? "incompatible" : "compatible");
590 bch2_version_to_text(&buf, old_version);
591 prt_str(&buf, " to ");
592 bch2_version_to_text(&buf, new_version);
595 recovery_passes = bch2_upgrade_recovery_passes(c, old_version, new_version);
596 if (recovery_passes) {
597 if ((recovery_passes & RECOVERY_PASS_ALL_FSCK) == RECOVERY_PASS_ALL_FSCK)
598 prt_str(&buf, "fsck required");
600 prt_str(&buf, "running recovery passes: ");
601 prt_bitflags(&buf, bch2_recovery_passes, recovery_passes);
604 c->recovery_passes_explicit |= recovery_passes;
605 c->opts.fix_errors = FSCK_FIX_yes;
608 bch_info(c, "%s", buf.buf);
610 mutex_lock(&c->sb_lock);
611 bch2_sb_upgrade(c, new_version);
612 mutex_unlock(&c->sb_lock);
618 u64 bch2_fsck_recovery_passes(void)
622 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++)
623 if (recovery_pass_fns[i].when & PASS_FSCK)
628 static bool should_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
630 struct recovery_pass_fn *p = recovery_pass_fns + c->curr_recovery_pass;
632 if (c->opts.norecovery && pass > BCH_RECOVERY_PASS_snapshots_read)
634 if (c->recovery_passes_explicit & BIT_ULL(pass))
636 if ((p->when & PASS_FSCK) && c->opts.fsck)
638 if ((p->when & PASS_UNCLEAN) && !c->sb.clean)
640 if (p->when & PASS_ALWAYS)
645 static int bch2_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
649 c->curr_recovery_pass = pass;
651 if (should_run_recovery_pass(c, pass)) {
652 struct recovery_pass_fn *p = recovery_pass_fns + pass;
654 if (!(p->when & PASS_SILENT))
655 printk(KERN_INFO bch2_log_msg(c, "%s..."),
656 bch2_recovery_passes[pass]);
660 if (!(p->when & PASS_SILENT))
661 printk(KERN_CONT " done\n");
663 c->recovery_passes_complete |= BIT_ULL(pass);
669 static int bch2_run_recovery_passes(struct bch_fs *c)
673 while (c->curr_recovery_pass < ARRAY_SIZE(recovery_pass_fns)) {
674 ret = bch2_run_recovery_pass(c, c->curr_recovery_pass);
675 if (bch2_err_matches(ret, BCH_ERR_restart_recovery))
679 c->curr_recovery_pass++;
685 int bch2_fs_recovery(struct bch_fs *c)
687 struct bch_sb_field_clean *clean = NULL;
688 struct jset *last_journal_entry = NULL;
689 u64 last_seq = 0, blacklist_seq, journal_seq;
690 bool write_sb = false;
694 clean = bch2_read_superblock_clean(c);
695 ret = PTR_ERR_OR_ZERO(clean);
699 bch_info(c, "recovering from clean shutdown, journal seq %llu",
700 le64_to_cpu(clean->journal_seq));
702 bch_info(c, "recovering from unclean shutdown");
705 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) {
706 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported");
712 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
713 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
718 if (c->opts.fsck || !(c->opts.nochanges && c->opts.norecovery))
719 check_version_upgrade(c);
721 if (c->opts.fsck && c->opts.norecovery) {
722 bch_err(c, "cannot select both norecovery and fsck");
727 ret = bch2_blacklist_table_initialize(c);
729 bch_err(c, "error initializing blacklist table");
733 if (!c->sb.clean || c->opts.fsck || c->opts.keep_journal) {
734 struct genradix_iter iter;
735 struct journal_replay **i;
737 bch_verbose(c, "starting journal read");
738 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
743 * note: cmd_list_journal needs the blacklist table fully up to date so
744 * it can asterisk ignored journal entries:
746 if (c->opts.read_journal_only)
749 genradix_for_each_reverse(&c->journal_entries, iter, i)
750 if (*i && !(*i)->ignore) {
751 last_journal_entry = &(*i)->j;
755 if (mustfix_fsck_err_on(c->sb.clean &&
756 last_journal_entry &&
757 !journal_entry_empty(last_journal_entry), c,
758 clean_but_journal_not_empty,
759 "filesystem marked clean but journal not empty")) {
760 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
761 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
765 if (!last_journal_entry) {
766 fsck_err_on(!c->sb.clean, c,
767 dirty_but_no_journal_entries,
768 "no journal entries found");
772 genradix_for_each_reverse(&c->journal_entries, iter, i)
774 last_journal_entry = &(*i)->j;
775 (*i)->ignore = false;
777 * This was probably a NO_FLUSH entry,
778 * so last_seq was garbage - but we know
779 * we're only using a single journal
780 * entry, set it here:
782 (*i)->j.last_seq = (*i)->j.seq;
787 ret = bch2_journal_keys_sort(c);
791 if (c->sb.clean && last_journal_entry) {
792 ret = bch2_verify_superblock_clean(c, &clean,
800 bch_err(c, "no superblock clean section found");
801 ret = -BCH_ERR_fsck_repair_impossible;
805 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1;
808 c->journal_replay_seq_start = last_seq;
809 c->journal_replay_seq_end = blacklist_seq - 1;
811 if (c->opts.reconstruct_alloc) {
812 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
813 drop_alloc_keys(&c->journal_keys);
816 zero_out_btree_mem_ptr(&c->journal_keys);
818 ret = journal_replay_early(c, clean);
823 * After an unclean shutdown, skip then next few journal sequence
824 * numbers as they may have been referenced by btree writes that
825 * happened before their corresponding journal writes - those btree
826 * writes need to be ignored, by skipping and blacklisting the next few
827 * journal sequence numbers:
832 if (blacklist_seq != journal_seq) {
833 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu",
834 blacklist_seq, journal_seq) ?:
835 bch2_journal_seq_blacklist_add(c,
836 blacklist_seq, journal_seq);
838 bch_err(c, "error creating new journal seq blacklist entry");
843 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
844 journal_seq, last_seq, blacklist_seq - 1) ?:
845 bch2_fs_journal_start(&c->journal, journal_seq);
849 if (c->opts.reconstruct_alloc)
850 bch2_journal_log_msg(c, "dropping alloc info");
853 * Skip past versions that might have possibly been used (as nonces),
854 * but hadn't had their pointers written:
856 if (c->sb.encryption_type && !c->sb.clean)
857 atomic64_add(1 << 16, &c->key_version);
859 ret = read_btree_roots(c);
864 (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) ||
865 BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)))
866 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_check_topology);
868 ret = bch2_run_recovery_passes(c);
872 /* If we fixed errors, verify that fs is actually clean now: */
873 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
874 test_bit(BCH_FS_ERRORS_FIXED, &c->flags) &&
875 !test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags) &&
876 !test_bit(BCH_FS_ERROR, &c->flags)) {
877 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean");
878 clear_bit(BCH_FS_ERRORS_FIXED, &c->flags);
880 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
882 ret = bch2_run_recovery_passes(c);
886 if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags) ||
887 test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
888 bch_err(c, "Second fsck run was not clean");
889 set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
892 set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
895 if (enabled_qtypes(c)) {
896 bch_verbose(c, "reading quotas");
897 ret = bch2_fs_quota_read(c);
900 bch_verbose(c, "quotas done");
903 mutex_lock(&c->sb_lock);
904 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != c->sb.version) {
905 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, c->sb.version);
909 if (!test_bit(BCH_FS_ERROR, &c->flags)) {
910 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
915 !test_bit(BCH_FS_ERROR, &c->flags) &&
916 !test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
917 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
918 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0);
924 mutex_unlock(&c->sb_lock);
926 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) ||
927 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) {
928 struct bch_move_stats stats;
930 bch2_move_stats_init(&stats, "recovery");
932 bch_info(c, "scanning for old btree nodes");
933 ret = bch2_fs_read_write(c) ?:
934 bch2_scan_old_btree_nodes(c, &stats);
937 bch_info(c, "scanning for old btree nodes done");
940 if (c->journal_seq_blacklist_table &&
941 c->journal_seq_blacklist_table->nr > 128)
942 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
946 set_bit(BCH_FS_FSCK_DONE, &c->flags);
947 bch2_flush_fsck_errs(c);
949 if (!c->opts.keep_journal &&
950 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags)) {
951 bch2_journal_keys_free(&c->journal_keys);
952 bch2_journal_entries_free(c);
956 if (!ret && test_bit(BCH_FS_NEED_DELETE_DEAD_SNAPSHOTS, &c->flags)) {
957 bch2_fs_read_write_early(c);
958 bch2_delete_dead_snapshots_async(c);
966 bch2_fs_emergency_read_only(c);
970 int bch2_fs_initialize(struct bch_fs *c)
972 struct bch_inode_unpacked root_inode, lostfound_inode;
973 struct bkey_inode_buf packed_inode;
974 struct qstr lostfound = QSTR("lost+found");
979 bch_notice(c, "initializing new filesystem");
981 mutex_lock(&c->sb_lock);
982 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
983 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
985 bch2_sb_maybe_downgrade(c);
987 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) {
988 bch2_sb_upgrade(c, bcachefs_metadata_version_current);
989 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
992 mutex_unlock(&c->sb_lock);
994 c->curr_recovery_pass = ARRAY_SIZE(recovery_pass_fns);
995 set_bit(BCH_FS_MAY_GO_RW, &c->flags);
996 set_bit(BCH_FS_FSCK_DONE, &c->flags);
998 for (i = 0; i < BTREE_ID_NR; i++)
999 bch2_btree_root_alloc(c, i);
1001 for_each_member_device(ca, c, i)
1002 bch2_dev_usage_init(ca);
1004 ret = bch2_fs_journal_alloc(c);
1009 * journal_res_get() will crash if called before this has
1010 * set up the journal.pin FIFO and journal.cur pointer:
1012 bch2_fs_journal_start(&c->journal, 1);
1013 bch2_journal_set_replay_done(&c->journal);
1015 ret = bch2_fs_read_write_early(c);
1020 * Write out the superblock and journal buckets, now that we can do
1023 bch_verbose(c, "marking superblocks");
1024 ret = bch2_trans_mark_dev_sbs(c);
1025 bch_err_msg(c, ret, "marking superblocks");
1029 for_each_online_member(ca, c, i)
1030 ca->new_fs_bucket_idx = 0;
1032 ret = bch2_fs_freespace_init(c);
1036 ret = bch2_initialize_subvolumes(c);
1040 bch_verbose(c, "reading snapshots table");
1041 ret = bch2_snapshots_read(c);
1044 bch_verbose(c, "reading snapshots done");
1046 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL);
1047 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1048 root_inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
1049 bch2_inode_pack(&packed_inode, &root_inode);
1050 packed_inode.inode.k.p.snapshot = U32_MAX;
1052 ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed_inode.inode.k_i, NULL, 0);
1054 bch_err_msg(c, ret, "creating root directory");
1058 bch2_inode_init_early(c, &lostfound_inode);
1060 ret = bch2_trans_do(c, NULL, NULL, 0,
1061 bch2_create_trans(trans,
1062 BCACHEFS_ROOT_SUBVOL_INUM,
1063 &root_inode, &lostfound_inode,
1065 0, 0, S_IFDIR|0700, 0,
1066 NULL, NULL, (subvol_inum) { 0 }, 0));
1068 bch_err_msg(c, ret, "creating lost+found");
1072 if (enabled_qtypes(c)) {
1073 ret = bch2_fs_quota_read(c);
1078 ret = bch2_journal_flush(&c->journal);
1080 bch_err_msg(c, ret, "writing first journal entry");
1084 mutex_lock(&c->sb_lock);
1085 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1086 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1088 bch2_write_super(c);
1089 mutex_unlock(&c->sb_lock);
1093 bch_err_fn(ca, ret);