1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
5 #include "backpointers.h"
7 #include "btree_node_scan.h"
13 #include "logged_ops.h"
14 #include "rebalance.h"
16 #include "recovery_passes.h"
18 #include "subvolume.h"
22 const char * const bch2_recovery_passes[] = {
23 #define x(_fn, ...) #_fn,
29 static int bch2_check_allocations(struct bch_fs *c)
31 return bch2_gc(c, true, false);
34 static int bch2_set_may_go_rw(struct bch_fs *c)
36 struct journal_keys *keys = &c->journal_keys;
39 * After we go RW, the journal keys buffer can't be modified (except for
40 * setting journal_key->overwritten: it will be accessed by multiple
43 move_gap(keys, keys->nr);
45 set_bit(BCH_FS_may_go_rw, &c->flags);
47 if (keys->nr || c->opts.fsck || !c->sb.clean || c->recovery_passes_explicit)
48 return bch2_fs_read_write_early(c);
52 struct recovery_pass_fn {
53 int (*fn)(struct bch_fs *);
57 static struct recovery_pass_fn recovery_pass_fns[] = {
58 #define x(_fn, _id, _when) { .fn = bch2_##_fn, .when = _when },
63 static const u8 passes_to_stable_map[] = {
64 #define x(n, id, ...) [BCH_RECOVERY_PASS_##n] = BCH_RECOVERY_PASS_STABLE_##n,
69 static enum bch_recovery_pass_stable bch2_recovery_pass_to_stable(enum bch_recovery_pass pass)
71 return passes_to_stable_map[pass];
74 u64 bch2_recovery_passes_to_stable(u64 v)
77 for (unsigned i = 0; i < ARRAY_SIZE(passes_to_stable_map); i++)
79 ret |= BIT_ULL(passes_to_stable_map[i]);
83 u64 bch2_recovery_passes_from_stable(u64 v)
85 static const u8 map[] = {
86 #define x(n, id, ...) [BCH_RECOVERY_PASS_STABLE_##n] = BCH_RECOVERY_PASS_##n,
92 for (unsigned i = 0; i < ARRAY_SIZE(map); i++)
94 ret |= BIT_ULL(map[i]);
99 * For when we need to rewind recovery passes and run a pass we skipped:
101 int bch2_run_explicit_recovery_pass(struct bch_fs *c,
102 enum bch_recovery_pass pass)
104 if (c->recovery_passes_explicit & BIT_ULL(pass))
107 bch_info(c, "running explicit recovery pass %s (%u), currently at %s (%u)",
108 bch2_recovery_passes[pass], pass,
109 bch2_recovery_passes[c->curr_recovery_pass], c->curr_recovery_pass);
111 c->recovery_passes_explicit |= BIT_ULL(pass);
113 if (c->curr_recovery_pass >= pass) {
114 c->curr_recovery_pass = pass;
115 c->recovery_passes_complete &= (1ULL << pass) >> 1;
116 return -BCH_ERR_restart_recovery;
122 int bch2_run_explicit_recovery_pass_persistent(struct bch_fs *c,
123 enum bch_recovery_pass pass)
125 enum bch_recovery_pass_stable s = bch2_recovery_pass_to_stable(pass);
127 mutex_lock(&c->sb_lock);
128 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
130 if (!test_bit_le64(s, ext->recovery_passes_required)) {
131 __set_bit_le64(s, ext->recovery_passes_required);
134 mutex_unlock(&c->sb_lock);
136 return bch2_run_explicit_recovery_pass(c, pass);
139 static void bch2_clear_recovery_pass_required(struct bch_fs *c,
140 enum bch_recovery_pass pass)
142 enum bch_recovery_pass_stable s = bch2_recovery_pass_to_stable(pass);
144 mutex_lock(&c->sb_lock);
145 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
147 if (test_bit_le64(s, ext->recovery_passes_required)) {
148 __clear_bit_le64(s, ext->recovery_passes_required);
151 mutex_unlock(&c->sb_lock);
154 u64 bch2_fsck_recovery_passes(void)
158 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++)
159 if (recovery_pass_fns[i].when & PASS_FSCK)
164 static bool should_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
166 struct recovery_pass_fn *p = recovery_pass_fns + pass;
168 if (c->recovery_passes_explicit & BIT_ULL(pass))
170 if ((p->when & PASS_FSCK) && c->opts.fsck)
172 if ((p->when & PASS_UNCLEAN) && !c->sb.clean)
174 if (p->when & PASS_ALWAYS)
179 static int bch2_run_recovery_pass(struct bch_fs *c, enum bch_recovery_pass pass)
181 struct recovery_pass_fn *p = recovery_pass_fns + pass;
184 if (!(p->when & PASS_SILENT))
185 bch2_print(c, KERN_INFO bch2_log_msg(c, "%s..."),
186 bch2_recovery_passes[pass]);
190 if (!(p->when & PASS_SILENT))
191 bch2_print(c, KERN_CONT " done\n");
196 int bch2_run_online_recovery_passes(struct bch_fs *c)
200 for (unsigned i = 0; i < ARRAY_SIZE(recovery_pass_fns); i++) {
201 struct recovery_pass_fn *p = recovery_pass_fns + i;
203 if (!(p->when & PASS_ONLINE))
206 ret = bch2_run_recovery_pass(c, i);
207 if (bch2_err_matches(ret, BCH_ERR_restart_recovery)) {
208 i = c->curr_recovery_pass;
218 int bch2_run_recovery_passes(struct bch_fs *c)
222 while (c->curr_recovery_pass < ARRAY_SIZE(recovery_pass_fns)) {
223 if (c->opts.recovery_pass_last &&
224 c->curr_recovery_pass > c->opts.recovery_pass_last)
227 if (should_run_recovery_pass(c, c->curr_recovery_pass)) {
228 unsigned pass = c->curr_recovery_pass;
230 ret = bch2_run_recovery_pass(c, c->curr_recovery_pass);
231 if (bch2_err_matches(ret, BCH_ERR_restart_recovery) ||
232 (ret && c->curr_recovery_pass < pass))
237 c->recovery_passes_complete |= BIT_ULL(c->curr_recovery_pass);
240 c->recovery_pass_done = max(c->recovery_pass_done, c->curr_recovery_pass);
242 if (!test_bit(BCH_FS_error, &c->flags))
243 bch2_clear_recovery_pass_required(c, c->curr_recovery_pass);
245 c->curr_recovery_pass++;