diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2023-07-07 02:42:28 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-10-22 17:10:06 -0400 |
commit | 067d228bb0c40542620398ef1d79f00f47c05cbb (patch) | |
tree | 41402dddcd6aa5921eec83a4917c584d3a920e19 /fs/bcachefs/recovery.c | |
parent | 78328fec704e316b36142a9a13af8665cd46da47 (diff) |
bcachefs: Enumerate recovery passes
Recovery and fsck have many different passes/jobs to do, which always
run in the same order - but not all of them run all the time. Some are
for fsck, some for unclean shutdown, some for version upgrades.
This adds some new structure: a defined list of recovery passes that we
can run in a loop, as well as consolidating the log messages.
The main benefit is consolidating the "should run this recovery pass"
logic, as well as cleaning up the "this recovery pass has finished"
state; instead of having a bunch of ad-hoc state bits in c->flags, we've
now got c->curr_recovery_pass.
By consolidating the "should run this recovery pass" logic, in the
future on disk format upgrades will be able to say "upgrading to this
version requires x passes to run", instead of forcing all of fsck to
run.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs/bcachefs/recovery.c')
-rw-r--r-- | fs/bcachefs/recovery.c | 227 |
1 files changed, 87 insertions, 140 deletions
diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c index 1499efc9d2a0..3b9120bd3603 100644 --- a/fs/bcachefs/recovery.c +++ b/fs/bcachefs/recovery.c @@ -1028,7 +1028,7 @@ fsck_err: return ret; } -static int bch2_fs_initialize_subvolumes(struct bch_fs *c) +static int bch2_initialize_subvolumes(struct bch_fs *c) { struct bkey_i_snapshot_tree root_tree; struct bkey_i_snapshot root_snapshot; @@ -1139,6 +1139,88 @@ static void check_version_upgrade(struct bch_fs *c) } } +static int bch2_check_allocations(struct bch_fs *c) +{ + return bch2_gc(c, true, c->opts.norecovery); +} + +static int bch2_set_may_go_rw(struct bch_fs *c) +{ + set_bit(BCH_FS_MAY_GO_RW, &c->flags); + return 0; +} + +struct recovery_pass_fn { + int (*fn)(struct bch_fs *); + const char *name; + unsigned when; +}; + +static struct recovery_pass_fn recovery_passes[] = { +#define x(_fn, _when) { .fn = bch2_##_fn, .name = #_fn, .when = _when }, + BCH_RECOVERY_PASSES() +#undef x +}; + +static bool should_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass) +{ + struct recovery_pass_fn *p = recovery_passes + c->curr_recovery_pass; + + if (c->opts.norecovery && pass > BCH_RECOVERY_PASS_snapshots_read) + return false; + if ((p->when & PASS_FSCK) && c->opts.fsck) + return true; + if ((p->when & PASS_UNCLEAN) && !c->sb.clean) + return true; + if (p->when & PASS_ALWAYS) + return true; + if (p->when >= PASS_UPGRADE(0) && + bch2_version_upgrading_to(c, p->when >> 4)) + return true; + return false; +} + +static int bch2_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass) +{ + int ret; + + c->curr_recovery_pass = pass; + + if (should_run_recovery_pass(c, pass)) { + struct recovery_pass_fn *p = recovery_passes + pass; + + if (!(p->when & PASS_SILENT)) + printk(KERN_INFO bch2_log_msg(c, "%s..."), p->name); + ret = p->fn(c); + if (ret) + return ret; + if (!(p->when & PASS_SILENT)) + printk(KERN_CONT " done\n"); + } + + return 0; +} + +static int bch2_run_recovery_passes(struct bch_fs *c) +{ + int ret = 0; +again: + while (c->curr_recovery_pass < ARRAY_SIZE(recovery_passes)) { + ret = bch2_run_recovery_pass(c, c->curr_recovery_pass); + if (ret) + break; + c->curr_recovery_pass++; + } + + if (bch2_err_matches(ret, BCH_ERR_need_snapshot_cleanup)) { + set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags); + c->curr_recovery_pass = BCH_RECOVERY_PASS_delete_dead_snapshots; + goto again; + } + + return ret; +} + int bch2_fs_recovery(struct bch_fs *c) { struct bch_sb_field_clean *clean = NULL; @@ -1313,141 +1395,9 @@ use_clean: if (ret) goto err; - bch_verbose(c, "starting alloc read"); - ret = bch2_alloc_read(c); - if (ret) - goto err; - bch_verbose(c, "alloc read done"); - - bch_verbose(c, "starting stripes_read"); - ret = bch2_stripes_read(c); + ret = bch2_run_recovery_passes(c); if (ret) goto err; - bch_verbose(c, "stripes_read done"); - - if (c->sb.version < bcachefs_metadata_version_snapshot_2) { - ret = bch2_fs_initialize_subvolumes(c); - if (ret) - goto err; - } - - bch_verbose(c, "reading snapshots table"); - ret = bch2_fs_snapshots_start(c); - if (ret) - goto err; - bch_verbose(c, "reading snapshots done"); - - if (c->opts.fsck) { - bool metadata_only = c->opts.norecovery; - - bch_info(c, "checking allocations"); - ret = bch2_gc(c, true, metadata_only); - if (ret) - goto err; - bch_verbose(c, "done checking allocations"); - - set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags); - - set_bit(BCH_FS_MAY_GO_RW, &c->flags); - - bch_info(c, "starting journal replay, %zu keys", c->journal_keys.nr); - ret = bch2_journal_replay(c); - if (ret) - goto err; - if (c->opts.verbose || !c->sb.clean) - bch_info(c, "journal replay done"); - - bch_info(c, "checking need_discard and freespace btrees"); - ret = bch2_check_alloc_info(c); - if (ret) - goto err; - bch_verbose(c, "done checking need_discard and freespace btrees"); - - set_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags); - - bch_info(c, "checking lrus"); - ret = bch2_check_lrus(c); - if (ret) - goto err; - bch_verbose(c, "done checking lrus"); - set_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags); - - bch_info(c, "checking backpointers to alloc keys"); - ret = bch2_check_btree_backpointers(c); - if (ret) - goto err; - bch_verbose(c, "done checking backpointers to alloc keys"); - - bch_info(c, "checking backpointers to extents"); - ret = bch2_check_backpointers_to_extents(c); - if (ret) - goto err; - bch_verbose(c, "done checking backpointers to extents"); - - bch_info(c, "checking extents to backpointers"); - ret = bch2_check_extents_to_backpointers(c); - if (ret) - goto err; - bch_verbose(c, "done checking extents to backpointers"); - set_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags); - - bch_info(c, "checking alloc to lru refs"); - ret = bch2_check_alloc_to_lru_refs(c); - if (ret) - goto err; - bch_verbose(c, "done checking alloc to lru refs"); - set_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags); - } else { - set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags); - set_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags); - set_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags); - set_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags); - set_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags); - set_bit(BCH_FS_FSCK_DONE, &c->flags); - - if (c->opts.norecovery) - goto out; - - set_bit(BCH_FS_MAY_GO_RW, &c->flags); - - bch_verbose(c, "starting journal replay, %zu keys", c->journal_keys.nr); - ret = bch2_journal_replay(c); - if (ret) - goto err; - if (c->opts.verbose || !c->sb.clean) - bch_info(c, "journal replay done"); - } - - ret = bch2_fs_freespace_init(c); - if (ret) - goto err; - - if (bch2_version_upgrading_to(c, bcachefs_metadata_version_bucket_gens)) { - bch_info(c, "initializing bucket_gens"); - ret = bch2_bucket_gens_init(c); - if (ret) - goto err; - bch_verbose(c, "bucket_gens init done"); - } - - if (bch2_version_upgrading_to(c, bcachefs_metadata_version_snapshot_2)) { - ret = bch2_fs_upgrade_for_subvolumes(c); - if (ret) - goto err; - } - - if (c->opts.fsck) { - ret = bch2_fsck_full(c); - if (ret) - goto err; - bch_verbose(c, "fsck done"); - } else if (!c->sb.clean) { - bch_verbose(c, "checking for deleted inodes"); - ret = bch2_fsck_walk_inodes_only(c); - if (ret) - goto err; - bch_verbose(c, "check inodes done"); - } if (enabled_qtypes(c)) { bch_verbose(c, "reading quotas"); @@ -1548,10 +1498,7 @@ int bch2_fs_initialize(struct bch_fs *c) } mutex_unlock(&c->sb_lock); - set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags); - set_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags); - set_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags); - set_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags); + c->curr_recovery_pass = ARRAY_SIZE(recovery_passes); set_bit(BCH_FS_MAY_GO_RW, &c->flags); set_bit(BCH_FS_FSCK_DONE, &c->flags); @@ -1599,12 +1546,12 @@ int bch2_fs_initialize(struct bch_fs *c) if (ret) goto err; - ret = bch2_fs_initialize_subvolumes(c); + ret = bch2_initialize_subvolumes(c); if (ret) goto err; bch_verbose(c, "reading snapshots table"); - ret = bch2_fs_snapshots_start(c); + ret = bch2_snapshots_read(c); if (ret) goto err; bch_verbose(c, "reading snapshots done"); |