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
10 #include "sb-downgrade.h"
11 #include "sb-errors.h"
14 #include <linux/darray.h>
16 #define RECOVERY_PASS_ALL_FSCK BIT_ULL(63)
19 * Upgrade, downgrade tables - run certain recovery passes, fix certain errors
21 * x(version, recovery_passes, errors...)
23 #define UPGRADE_TABLE() \
25 RECOVERY_PASS_ALL_FSCK) \
27 RECOVERY_PASS_ALL_FSCK) \
28 x(unwritten_extents, \
29 RECOVERY_PASS_ALL_FSCK) \
31 BIT_ULL(BCH_RECOVERY_PASS_bucket_gens_init)| \
32 RECOVERY_PASS_ALL_FSCK) \
34 RECOVERY_PASS_ALL_FSCK) \
35 x(fragmentation_lru, \
36 RECOVERY_PASS_ALL_FSCK) \
37 x(no_bps_in_alloc_keys, \
38 RECOVERY_PASS_ALL_FSCK) \
40 RECOVERY_PASS_ALL_FSCK) \
41 x(snapshot_skiplists, \
42 BIT_ULL(BCH_RECOVERY_PASS_check_snapshots), \
43 BCH_FSCK_ERR_snapshot_bad_depth, \
44 BCH_FSCK_ERR_snapshot_bad_skiplist) \
46 BIT_ULL(BCH_RECOVERY_PASS_check_inodes), \
47 BCH_FSCK_ERR_unlinked_inode_not_on_deleted_list) \
49 BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance)) \
50 x(subvolume_fs_parent, \
51 BIT_ULL(BCH_RECOVERY_PASS_check_dirents), \
52 BCH_FSCK_ERR_subvol_fs_path_parent_wrong) \
53 x(btree_subvolume_children, \
54 BIT_ULL(BCH_RECOVERY_PASS_check_subvols), \
55 BCH_FSCK_ERR_subvol_children_not_set)
57 #define DOWNGRADE_TABLE()
59 struct upgrade_downgrade_entry {
66 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
70 static const struct upgrade_downgrade_entry upgrade_table[] = {
71 #define x(ver, passes, ...) { \
72 .recovery_passes = passes, \
73 .version = bcachefs_metadata_version_##ver,\
74 .nr_errors = ARRAY_SIZE(upgrade_##ver##_errors), \
75 .errors = upgrade_##ver##_errors, \
81 void bch2_sb_set_upgrade(struct bch_fs *c,
85 lockdep_assert_held(&c->sb_lock);
87 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
89 for (const struct upgrade_downgrade_entry *i = upgrade_table;
90 i < upgrade_table + ARRAY_SIZE(upgrade_table);
92 if (i->version > old_version && i->version <= new_version) {
93 u64 passes = i->recovery_passes;
95 if (passes & RECOVERY_PASS_ALL_FSCK)
96 passes |= bch2_fsck_recovery_passes();
97 passes &= ~RECOVERY_PASS_ALL_FSCK;
99 ext->recovery_passes_required[0] |=
100 cpu_to_le64(bch2_recovery_passes_to_stable(passes));
102 for (const u16 *e = i->errors;
103 e < i->errors + i->nr_errors;
105 __set_bit(*e, c->sb.errors_silent);
106 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64));
111 #define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ };
115 static const struct upgrade_downgrade_entry downgrade_table[] = {
116 #define x(ver, passes, ...) { \
117 .recovery_passes = passes, \
118 .version = bcachefs_metadata_version_##ver,\
119 .nr_errors = ARRAY_SIZE(downgrade_##ver##_errors), \
120 .errors = downgrade_##ver##_errors, \
126 static inline const struct bch_sb_field_downgrade_entry *
127 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
129 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
132 #define for_each_downgrade_entry(_d, _i) \
133 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
134 (void *) _i < vstruct_end(&(_d)->field) && \
135 (void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
136 _i = downgrade_entry_next_c(_i))
138 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
139 struct printbuf *err)
141 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
143 for_each_downgrade_entry(e, i) {
144 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
145 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
146 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
147 BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
148 BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
149 return -BCH_ERR_invalid_sb_downgrade;
156 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
157 struct bch_sb_field *f)
159 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
161 if (out->nr_tabstops <= 1)
162 printbuf_tabstop_push(out, 16);
164 for_each_downgrade_entry(e, i) {
165 prt_str(out, "version:");
167 bch2_version_to_text(out, le16_to_cpu(i->version));
170 prt_str(out, "recovery passes:");
172 prt_bitflags(out, bch2_recovery_passes,
173 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
176 prt_str(out, "errors:");
179 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
183 unsigned e = le16_to_cpu(i->errors[j]);
184 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
190 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
191 .validate = bch2_sb_downgrade_validate,
192 .to_text = bch2_sb_downgrade_to_text,
195 int bch2_sb_downgrade_update(struct bch_fs *c)
197 darray_char table = {};
200 for (const struct upgrade_downgrade_entry *src = downgrade_table;
201 src < downgrade_table + ARRAY_SIZE(downgrade_table);
203 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
206 struct bch_sb_field_downgrade_entry *dst;
207 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
209 ret = darray_make_room(&table, bytes);
213 dst = (void *) &darray_top(table);
214 dst->version = cpu_to_le16(src->version);
215 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
216 dst->recovery_passes[1] = 0;
217 dst->nr_errors = cpu_to_le16(src->nr_errors);
218 for (unsigned i = 0; i < src->nr_errors; i++)
219 dst->errors[i] = cpu_to_le16(src->errors[i]);
224 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
226 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
228 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
231 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
233 ret = -BCH_ERR_ENOSPC_sb_downgrade;
237 memcpy(d->entries, table.data, table.nr);
238 memset_u64s_tail(d->entries, 0, table.nr);
244 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
246 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
250 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
252 for_each_downgrade_entry(d, i) {
253 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
254 if (new_minor < minor && minor <= old_minor) {
255 ext->recovery_passes_required[0] |= i->recovery_passes[0];
256 ext->recovery_passes_required[1] |= i->recovery_passes[1];
258 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
259 unsigned e = le16_to_cpu(i->errors[j]);
260 if (e < BCH_SB_ERR_MAX)
261 __set_bit(e, c->sb.errors_silent);
262 if (e < sizeof(ext->errors_silent) * 8)
263 ext->errors_silent[e / 64] |= cpu_to_le64(BIT_ULL(e % 64));