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"
17 * When dowgrading past certain versions, we need to run certain recovery passes
18 * and fix certain errors:
20 * x(version, recovery_passes, errors...)
23 #define DOWNGRADE_TABLE()
25 struct downgrade_entry {
32 #define x(ver, passes, ...) static const u16 ver_##errors[] = { __VA_ARGS__ };
36 static const struct downgrade_entry downgrade_table[] = {
37 #define x(ver, passes, ...) { \
38 .recovery_passes = passes, \
39 .version = bcachefs_metadata_version_##ver,\
40 .nr_errors = ARRAY_SIZE(ver_##errors), \
41 .errors = ver_##errors, \
47 static inline const struct bch_sb_field_downgrade_entry *
48 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
50 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
53 #define for_each_downgrade_entry(_d, _i) \
54 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
55 (void *) _i < vstruct_end(&(_d)->field) && \
56 (void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
57 _i = downgrade_entry_next_c(_i))
59 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
62 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
64 for_each_downgrade_entry(e, i) {
65 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
66 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
67 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
68 BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
69 BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
70 return -BCH_ERR_invalid_sb_downgrade;
77 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
78 struct bch_sb_field *f)
80 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
82 if (out->nr_tabstops <= 1)
83 printbuf_tabstop_push(out, 16);
85 for_each_downgrade_entry(e, i) {
86 prt_str(out, "version:");
88 bch2_version_to_text(out, le16_to_cpu(i->version));
91 prt_str(out, "recovery passes:");
93 prt_bitflags(out, bch2_recovery_passes,
94 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
97 prt_str(out, "errors:");
100 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
104 unsigned e = le16_to_cpu(i->errors[j]);
105 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
111 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
112 .validate = bch2_sb_downgrade_validate,
113 .to_text = bch2_sb_downgrade_to_text,
116 int bch2_sb_downgrade_update(struct bch_fs *c)
118 darray_char table = {};
121 for (const struct downgrade_entry *src = downgrade_table;
122 src < downgrade_table + ARRAY_SIZE(downgrade_table);
124 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
127 struct bch_sb_field_downgrade_entry *dst;
128 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
130 ret = darray_make_room(&table, bytes);
134 dst = (void *) &darray_top(table);
135 dst->version = cpu_to_le16(src->version);
136 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
137 dst->recovery_passes[1] = 0;
138 dst->nr_errors = cpu_to_le16(src->nr_errors);
139 for (unsigned i = 0; i < src->nr_errors; i++)
140 dst->errors[i] = cpu_to_le16(src->errors[i]);
145 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
147 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
149 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
152 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
154 ret = -BCH_ERR_ENOSPC_sb_downgrade;
158 memcpy(d->entries, table.data, table.nr);
159 memset_u64s_tail(d->entries, 0, table.nr);
165 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
167 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
171 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
173 for_each_downgrade_entry(d, i) {
174 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
175 if (new_minor < minor && minor <= old_minor) {
176 ext->recovery_passes_required[0] |= i->recovery_passes[0];
177 ext->recovery_passes_required[1] |= i->recovery_passes[1];
179 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
180 unsigned e = le16_to_cpu(i->errors[j]);
181 if (e < BCH_SB_ERR_MAX)
182 __set_bit(e, c->sb.errors_silent);
183 if (e < sizeof(ext->errors_silent) * 8)
184 ext->errors_silent[e / 64] |= cpu_to_le64(BIT_ULL(e % 64));