blob: 441dcb1bf160e917d531d1a5ea955cf0238f0844 [file] [log] [blame]
Kent Overstreet84f16382023-12-29 15:25:07 -05001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Superblock section that contains a list of recovery passes to run when
5 * downgrading past a given version
6 */
7
8#include "bcachefs.h"
9#include "darray.h"
10#include "recovery.h"
11#include "sb-downgrade.h"
12#include "sb-errors.h"
13#include "super-io.h"
14
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050015#define RECOVERY_PASS_ALL_FSCK BIT_ULL(63)
16
Kent Overstreet84f16382023-12-29 15:25:07 -050017/*
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050018 * Upgrade, downgrade tables - run certain recovery passes, fix certain errors
Kent Overstreet84f16382023-12-29 15:25:07 -050019 *
20 * x(version, recovery_passes, errors...)
21 */
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050022#define UPGRADE_TABLE() \
23 x(backpointers, \
24 RECOVERY_PASS_ALL_FSCK) \
25 x(inode_v3, \
26 RECOVERY_PASS_ALL_FSCK) \
27 x(unwritten_extents, \
28 RECOVERY_PASS_ALL_FSCK) \
29 x(bucket_gens, \
30 BIT_ULL(BCH_RECOVERY_PASS_bucket_gens_init)| \
31 RECOVERY_PASS_ALL_FSCK) \
32 x(lru_v2, \
33 RECOVERY_PASS_ALL_FSCK) \
34 x(fragmentation_lru, \
35 RECOVERY_PASS_ALL_FSCK) \
36 x(no_bps_in_alloc_keys, \
37 RECOVERY_PASS_ALL_FSCK) \
38 x(snapshot_trees, \
39 RECOVERY_PASS_ALL_FSCK) \
40 x(snapshot_skiplists, \
Kent Overstreet074cbcd2024-01-03 23:29:02 -050041 BIT_ULL(BCH_RECOVERY_PASS_check_snapshots), \
42 BCH_FSCK_ERR_snapshot_bad_depth, \
43 BCH_FSCK_ERR_snapshot_bad_skiplist) \
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050044 x(deleted_inodes, \
45 BIT_ULL(BCH_RECOVERY_PASS_check_inodes), \
46 BCH_FSCK_ERR_unlinked_inode_not_on_deleted_list) \
47 x(rebalance_work, \
48 BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance))
Kent Overstreet84f16382023-12-29 15:25:07 -050049
50#define DOWNGRADE_TABLE()
51
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050052struct upgrade_downgrade_entry {
Kent Overstreet84f16382023-12-29 15:25:07 -050053 u64 recovery_passes;
54 u16 version;
55 u16 nr_errors;
56 const u16 *errors;
57};
58
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050059#define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
60UPGRADE_TABLE()
Kent Overstreet84f16382023-12-29 15:25:07 -050061#undef x
62
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050063static const struct upgrade_downgrade_entry upgrade_table[] = {
Kent Overstreet84f16382023-12-29 15:25:07 -050064#define x(ver, passes, ...) { \
65 .recovery_passes = passes, \
66 .version = bcachefs_metadata_version_##ver,\
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -050067 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \
68 .errors = upgrade_##ver##_errors, \
69},
70UPGRADE_TABLE()
71#undef x
72};
73
74void bch2_sb_set_upgrade(struct bch_fs *c,
75 unsigned old_version,
76 unsigned new_version)
77{
78 lockdep_assert_held(&c->sb_lock);
79
80 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
81
82 for (const struct upgrade_downgrade_entry *i = upgrade_table;
83 i < upgrade_table + ARRAY_SIZE(upgrade_table);
84 i++)
85 if (i->version > old_version && i->version <= new_version) {
86 u64 passes = i->recovery_passes;
87
88 if (passes & RECOVERY_PASS_ALL_FSCK)
89 passes |= bch2_fsck_recovery_passes();
90 passes &= ~RECOVERY_PASS_ALL_FSCK;
91
92 ext->recovery_passes_required[0] |=
93 cpu_to_le64(bch2_recovery_passes_to_stable(passes));
94
95 for (const u16 *e = i->errors;
96 e < i->errors + i->nr_errors;
97 e++) {
98 __set_bit(*e, c->sb.errors_silent);
99 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64));
100 }
101 }
102}
103
104#define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ };
105DOWNGRADE_TABLE()
106#undef x
107
108static const struct upgrade_downgrade_entry downgrade_table[] = {
109#define x(ver, passes, ...) { \
110 .recovery_passes = passes, \
111 .version = bcachefs_metadata_version_##ver,\
112 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \
113 .errors = downgrade_##ver##_errors, \
Kent Overstreet84f16382023-12-29 15:25:07 -0500114},
115DOWNGRADE_TABLE()
116#undef x
117};
118
119static inline const struct bch_sb_field_downgrade_entry *
120downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
121{
122 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
123}
124
125#define for_each_downgrade_entry(_d, _i) \
126 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
127 (void *) _i < vstruct_end(&(_d)->field) && \
128 (void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
129 _i = downgrade_entry_next_c(_i))
130
131static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
132 struct printbuf *err)
133{
134 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
135
136 for_each_downgrade_entry(e, i) {
137 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
138 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
139 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
140 BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
141 BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
142 return -BCH_ERR_invalid_sb_downgrade;
143 }
144 }
145
146 return 0;
147}
148
149static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
150 struct bch_sb_field *f)
151{
152 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
153
154 if (out->nr_tabstops <= 1)
155 printbuf_tabstop_push(out, 16);
156
157 for_each_downgrade_entry(e, i) {
158 prt_str(out, "version:");
159 prt_tab(out);
160 bch2_version_to_text(out, le16_to_cpu(i->version));
161 prt_newline(out);
162
163 prt_str(out, "recovery passes:");
164 prt_tab(out);
165 prt_bitflags(out, bch2_recovery_passes,
166 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
167 prt_newline(out);
168
169 prt_str(out, "errors:");
170 prt_tab(out);
171 bool first = true;
172 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
173 if (!first)
174 prt_char(out, ',');
175 first = false;
176 unsigned e = le16_to_cpu(i->errors[j]);
177 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
178 }
179 prt_newline(out);
180 }
181}
182
183const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
184 .validate = bch2_sb_downgrade_validate,
185 .to_text = bch2_sb_downgrade_to_text,
186};
187
188int bch2_sb_downgrade_update(struct bch_fs *c)
189{
190 darray_char table = {};
191 int ret = 0;
192
Kent Overstreet15eaaa4c2024-01-03 21:01:37 -0500193 for (const struct upgrade_downgrade_entry *src = downgrade_table;
Kent Overstreet84f16382023-12-29 15:25:07 -0500194 src < downgrade_table + ARRAY_SIZE(downgrade_table);
195 src++) {
196 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
197 continue;
198
199 struct bch_sb_field_downgrade_entry *dst;
200 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
201
202 ret = darray_make_room(&table, bytes);
203 if (ret)
204 goto out;
205
206 dst = (void *) &darray_top(table);
207 dst->version = cpu_to_le16(src->version);
208 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
209 dst->recovery_passes[1] = 0;
210 dst->nr_errors = cpu_to_le16(src->nr_errors);
211 for (unsigned i = 0; i < src->nr_errors; i++)
212 dst->errors[i] = cpu_to_le16(src->errors[i]);
213
214 table.nr += bytes;
215 }
216
217 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
218
219 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
220
221 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
222 goto out;
223
224 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
225 if (!d) {
226 ret = -BCH_ERR_ENOSPC_sb_downgrade;
227 goto out;
228 }
229
230 memcpy(d->entries, table.data, table.nr);
231 memset_u64s_tail(d->entries, 0, table.nr);
232out:
233 darray_exit(&table);
234 return ret;
235}
236
237void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
238{
239 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
240 if (!d)
241 return;
242
243 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
244
245 for_each_downgrade_entry(d, i) {
246 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
247 if (new_minor < minor && minor <= old_minor) {
248 ext->recovery_passes_required[0] |= i->recovery_passes[0];
249 ext->recovery_passes_required[1] |= i->recovery_passes[1];
250
251 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
252 unsigned e = le16_to_cpu(i->errors[j]);
253 if (e < BCH_SB_ERR_MAX)
254 __set_bit(e, c->sb.errors_silent);
255 if (e < sizeof(ext->errors_silent) * 8)
256 ext->errors_silent[e / 64] |= cpu_to_le64(BIT_ULL(e % 64));
257 }
258 }
259 }
260}