1// 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_passes.h"
11#include "sb-downgrade.h"
12#include "sb-errors.h"
13#include "super-io.h"
14
15#define RECOVERY_PASS_ALL_FSCK BIT_ULL(63)
16
17/*
18 * Upgrade, downgrade tables - run certain recovery passes, fix certain errors
19 *
20 * x(version, recovery_passes, errors...)
21 */
22#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, \
41 BIT_ULL(BCH_RECOVERY_PASS_check_snapshots), \
42 BCH_FSCK_ERR_snapshot_bad_depth, \
43 BCH_FSCK_ERR_snapshot_bad_skiplist) \
44 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)) \
49 x(subvolume_fs_parent, \
50 BIT_ULL(BCH_RECOVERY_PASS_check_dirents), \
51 BCH_FSCK_ERR_subvol_fs_path_parent_wrong) \
52 x(btree_subvolume_children, \
53 BIT_ULL(BCH_RECOVERY_PASS_check_subvols), \
54 BCH_FSCK_ERR_subvol_children_not_set) \
55 x(mi_btree_bitmap, \
56 BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \
57 BCH_FSCK_ERR_btree_bitmap_not_marked)
58
59#define DOWNGRADE_TABLE()
60
61struct upgrade_downgrade_entry {
62 u64 recovery_passes;
63 u16 version;
64 u16 nr_errors;
65 const u16 *errors;
66};
67
68#define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
69UPGRADE_TABLE()
70#undef x
71
72static const struct upgrade_downgrade_entry upgrade_table[] = {
73#define x(ver, passes, ...) { \
74 .recovery_passes = passes, \
75 .version = bcachefs_metadata_version_##ver,\
76 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \
77 .errors = upgrade_##ver##_errors, \
78},
79UPGRADE_TABLE()
80#undef x
81};
82
83void bch2_sb_set_upgrade(struct bch_fs *c,
84 unsigned old_version,
85 unsigned new_version)
86{
87 lockdep_assert_held(&c->sb_lock);
88
89 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
90
91 for (const struct upgrade_downgrade_entry *i = upgrade_table;
92 i < upgrade_table + ARRAY_SIZE(upgrade_table);
93 i++)
94 if (i->version > old_version && i->version <= new_version) {
95 u64 passes = i->recovery_passes;
96
97 if (passes & RECOVERY_PASS_ALL_FSCK)
98 passes |= bch2_fsck_recovery_passes();
99 passes &= ~RECOVERY_PASS_ALL_FSCK;
100
101 ext->recovery_passes_required[0] |=
102 cpu_to_le64(bch2_recovery_passes_to_stable(passes));
103
104 for (const u16 *e = i->errors;
105 e < i->errors + i->nr_errors;
106 e++) {
107 __set_bit(*e, c->sb.errors_silent);
108 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64));
109 }
110 }
111}
112
113#define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ };
114DOWNGRADE_TABLE()
115#undef x
116
117static const struct upgrade_downgrade_entry downgrade_table[] = {
118#define x(ver, passes, ...) { \
119 .recovery_passes = passes, \
120 .version = bcachefs_metadata_version_##ver,\
121 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \
122 .errors = downgrade_##ver##_errors, \
123},
124DOWNGRADE_TABLE()
125#undef x
126};
127
128static inline const struct bch_sb_field_downgrade_entry *
129downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
130{
131 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
132}
133
134#define for_each_downgrade_entry(_d, _i) \
135 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
136 (void *) _i < vstruct_end(&(_d)->field) && \
137 (void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
138 _i = downgrade_entry_next_c(_i))
139
140static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
141 struct printbuf *err)
142{
143 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
144
145 for_each_downgrade_entry(e, i) {
146 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
147 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
148 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
149 BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
150 BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
151 return -BCH_ERR_invalid_sb_downgrade;
152 }
153 }
154
155 return 0;
156}
157
158static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
159 struct bch_sb_field *f)
160{
161 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
162
163 if (out->nr_tabstops <= 1)
164 printbuf_tabstop_push(out, 16);
165
166 for_each_downgrade_entry(e, i) {
167 prt_str(out, str: "version:");
168 prt_tab(out);
169 bch2_version_to_text(out, le16_to_cpu(i->version));
170 prt_newline(out);
171
172 prt_str(out, str: "recovery passes:");
173 prt_tab(out);
174 prt_bitflags(out, bch2_recovery_passes,
175 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
176 prt_newline(out);
177
178 prt_str(out, str: "errors:");
179 prt_tab(out);
180 bool first = true;
181 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
182 if (!first)
183 prt_char(out, c: ',');
184 first = false;
185 unsigned e = le16_to_cpu(i->errors[j]);
186 prt_str(out, str: e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
187 }
188 prt_newline(out);
189 }
190}
191
192const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
193 .validate = bch2_sb_downgrade_validate,
194 .to_text = bch2_sb_downgrade_to_text,
195};
196
197int bch2_sb_downgrade_update(struct bch_fs *c)
198{
199 darray_char table = {};
200 int ret = 0;
201
202 for (const struct upgrade_downgrade_entry *src = downgrade_table;
203 src < downgrade_table + ARRAY_SIZE(downgrade_table);
204 src++) {
205 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
206 continue;
207
208 struct bch_sb_field_downgrade_entry *dst;
209 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
210
211 ret = darray_make_room(&table, bytes);
212 if (ret)
213 goto out;
214
215 dst = (void *) &darray_top(table);
216 dst->version = cpu_to_le16(src->version);
217 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
218 dst->recovery_passes[1] = 0;
219 dst->nr_errors = cpu_to_le16(src->nr_errors);
220 for (unsigned i = 0; i < src->nr_errors; i++)
221 dst->errors[i] = cpu_to_le16(src->errors[i]);
222
223 table.nr += bytes;
224 }
225
226 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
227
228 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
229
230 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
231 goto out;
232
233 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
234 if (!d) {
235 ret = -BCH_ERR_ENOSPC_sb_downgrade;
236 goto out;
237 }
238
239 memcpy(d->entries, table.data, table.nr);
240 memset_u64s_tail(s: d->entries, c: 0, bytes: table.nr);
241out:
242 darray_exit(&table);
243 return ret;
244}
245
246void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
247{
248 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
249 if (!d)
250 return;
251
252 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
253
254 for_each_downgrade_entry(d, i) {
255 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
256 if (new_minor < minor && minor <= old_minor) {
257 ext->recovery_passes_required[0] |= i->recovery_passes[0];
258 ext->recovery_passes_required[1] |= i->recovery_passes[1];
259
260 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
261 unsigned e = le16_to_cpu(i->errors[j]);
262 if (e < BCH_SB_ERR_MAX)
263 __set_bit(e, c->sb.errors_silent);
264 if (e < sizeof(ext->errors_silent) * 8)
265 __set_bit_le64(bit: e, addr: ext->errors_silent);
266 }
267 }
268 }
269}
270

source code of linux/fs/bcachefs/sb-downgrade.c