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)) \
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)
56 #define DOWNGRADE_TABLE()
58 struct upgrade_downgrade_entry {
65 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
69 static const struct upgrade_downgrade_entry upgrade_table[] = {
70 #define x(ver, passes, ...) { \
71 .recovery_passes = passes, \
72 .version = bcachefs_metadata_version_##ver,\
73 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \
74 .errors = upgrade_##ver##_errors, \
80 void bch2_sb_set_upgrade(struct bch_fs *c,
84 lockdep_assert_held(&c->sb_lock);
86 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
88 for (const struct upgrade_downgrade_entry *i = upgrade_table;
89 i < upgrade_table + ARRAY_SIZE(upgrade_table);
91 if (i->version > old_version && i->version <= new_version) {
92 u64 passes = i->recovery_passes;
94 if (passes & RECOVERY_PASS_ALL_FSCK)
95 passes |= bch2_fsck_recovery_passes();
96 passes &= ~RECOVERY_PASS_ALL_FSCK;
98 ext->recovery_passes_required[0] |=
99 cpu_to_le64(bch2_recovery_passes_to_stable(passes));
101 for (const u16 *e = i->errors;
102 e < i->errors + i->nr_errors;
104 __set_bit(*e, c->sb.errors_silent);
105 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64));
110 #define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ };
114 static const struct upgrade_downgrade_entry downgrade_table[] = {
115 #define x(ver, passes, ...) { \
116 .recovery_passes = passes, \
117 .version = bcachefs_metadata_version_##ver,\
118 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \
119 .errors = downgrade_##ver##_errors, \
125 static inline const struct bch_sb_field_downgrade_entry *
126 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
128 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
131 #define for_each_downgrade_entry(_d, _i) \
132 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
133 (void *) _i < vstruct_end(&(_d)->field) && \
134 (void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
135 _i = downgrade_entry_next_c(_i))
137 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
138 struct printbuf *err)
140 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
142 for_each_downgrade_entry(e, i) {
143 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
144 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
145 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
146 BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
147 BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
148 return -BCH_ERR_invalid_sb_downgrade;
155 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
156 struct bch_sb_field *f)
158 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
160 if (out->nr_tabstops <= 1)
161 printbuf_tabstop_push(out, 16);
163 for_each_downgrade_entry(e, i) {
164 prt_str(out, "version:");
166 bch2_version_to_text(out, le16_to_cpu(i->version));
169 prt_str(out, "recovery passes:");
171 prt_bitflags(out, bch2_recovery_passes,
172 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
175 prt_str(out, "errors:");
178 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
182 unsigned e = le16_to_cpu(i->errors[j]);
183 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
189 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
190 .validate = bch2_sb_downgrade_validate,
191 .to_text = bch2_sb_downgrade_to_text,
194 int bch2_sb_downgrade_update(struct bch_fs *c)
196 darray_char table = {};
199 for (const struct upgrade_downgrade_entry *src = downgrade_table;
200 src < downgrade_table + ARRAY_SIZE(downgrade_table);
202 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
205 struct bch_sb_field_downgrade_entry *dst;
206 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
208 ret = darray_make_room(&table, bytes);
212 dst = (void *) &darray_top(table);
213 dst->version = cpu_to_le16(src->version);
214 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
215 dst->recovery_passes[1] = 0;
216 dst->nr_errors = cpu_to_le16(src->nr_errors);
217 for (unsigned i = 0; i < src->nr_errors; i++)
218 dst->errors[i] = cpu_to_le16(src->errors[i]);
223 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
225 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
227 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
230 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
232 ret = -BCH_ERR_ENOSPC_sb_downgrade;
236 memcpy(d->entries, table.data, table.nr);
237 memset_u64s_tail(d->entries, 0, table.nr);
243 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
245 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
249 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
251 for_each_downgrade_entry(d, i) {
252 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
253 if (new_minor < minor && minor <= old_minor) {
254 ext->recovery_passes_required[0] |= i->recovery_passes[0];
255 ext->recovery_passes_required[1] |= i->recovery_passes[1];
257 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
258 unsigned e = le16_to_cpu(i->errors[j]);
259 if (e < BCH_SB_ERR_MAX)
260 __set_bit(e, c->sb.errors_silent);
261 if (e < sizeof(ext->errors_silent) * 8)
262 __set_bit_le64(e, ext->errors_silent);