1 // SPDX-License-Identifier: GPL-2.0
4 * Superblock section that contains a list of recovery passes to run when
5 * downgrading past a given version
11 #include "sb-downgrade.h"
12 #include "sb-errors.h"
15 #define RECOVERY_PASS_ALL_FSCK BIT_ULL(63)
18 * Upgrade, downgrade tables - run certain recovery passes, fix certain errors
20 * x(version, recovery_passes, errors...)
22 #define UPGRADE_TABLE() \
24 RECOVERY_PASS_ALL_FSCK) \
26 RECOVERY_PASS_ALL_FSCK) \
27 x(unwritten_extents, \
28 RECOVERY_PASS_ALL_FSCK) \
30 BIT_ULL(BCH_RECOVERY_PASS_bucket_gens_init)| \
31 RECOVERY_PASS_ALL_FSCK) \
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) \
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) \
45 BIT_ULL(BCH_RECOVERY_PASS_check_inodes), \
46 BCH_FSCK_ERR_unlinked_inode_not_on_deleted_list) \
48 BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance))
50 #define DOWNGRADE_TABLE()
52 struct upgrade_downgrade_entry {
59 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
63 static const struct upgrade_downgrade_entry upgrade_table[] = {
64 #define x(ver, passes, ...) { \
65 .recovery_passes = passes, \
66 .version = bcachefs_metadata_version_##ver,\
67 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \
68 .errors = upgrade_##ver##_errors, \
74 void bch2_sb_set_upgrade(struct bch_fs *c,
78 lockdep_assert_held(&c->sb_lock);
80 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
82 for (const struct upgrade_downgrade_entry *i = upgrade_table;
83 i < upgrade_table + ARRAY_SIZE(upgrade_table);
85 if (i->version > old_version && i->version <= new_version) {
86 u64 passes = i->recovery_passes;
88 if (passes & RECOVERY_PASS_ALL_FSCK)
89 passes |= bch2_fsck_recovery_passes();
90 passes &= ~RECOVERY_PASS_ALL_FSCK;
92 ext->recovery_passes_required[0] |=
93 cpu_to_le64(bch2_recovery_passes_to_stable(passes));
95 for (const u16 *e = i->errors;
96 e < i->errors + i->nr_errors;
98 __set_bit(*e, c->sb.errors_silent);
99 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64));
104 #define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ };
108 static 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, \
119 static inline const struct bch_sb_field_downgrade_entry *
120 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
122 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
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))
131 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
132 struct printbuf *err)
134 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
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;
149 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
150 struct bch_sb_field *f)
152 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
154 if (out->nr_tabstops <= 1)
155 printbuf_tabstop_push(out, 16);
157 for_each_downgrade_entry(e, i) {
158 prt_str(out, "version:");
160 bch2_version_to_text(out, le16_to_cpu(i->version));
163 prt_str(out, "recovery passes:");
165 prt_bitflags(out, bch2_recovery_passes,
166 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
169 prt_str(out, "errors:");
172 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
176 unsigned e = le16_to_cpu(i->errors[j]);
177 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
183 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
184 .validate = bch2_sb_downgrade_validate,
185 .to_text = bch2_sb_downgrade_to_text,
188 int bch2_sb_downgrade_update(struct bch_fs *c)
190 darray_char table = {};
193 for (const struct upgrade_downgrade_entry *src = downgrade_table;
194 src < downgrade_table + ARRAY_SIZE(downgrade_table);
196 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
199 struct bch_sb_field_downgrade_entry *dst;
200 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
202 ret = darray_make_room(&table, bytes);
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]);
217 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
219 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
221 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
224 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
226 ret = -BCH_ERR_ENOSPC_sb_downgrade;
230 memcpy(d->entries, table.data, table.nr);
231 memset_u64s_tail(d->entries, 0, table.nr);
237 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
239 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
243 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
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];
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));