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() \
24 x(disk_accounting_v2, \
25 BIT_ULL(BCH_RECOVERY_PASS_check_alloc_info), \
26 BCH_FSCK_ERR_dev_usage_buckets_wrong)
28 struct downgrade_entry {
35 #define x(ver, passes, ...) static const u16 ver_##errors[] = { __VA_ARGS__ };
39 static const struct downgrade_entry downgrade_table[] = {
40 #define x(ver, passes, ...) { \
41 .recovery_passes = passes, \
42 .version = bcachefs_metadata_version_##ver,\
43 .nr_errors = ARRAY_SIZE(ver_##errors), \
44 .errors = ver_##errors, \
50 static inline const struct bch_sb_field_downgrade_entry *
51 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
53 return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
56 #define for_each_downgrade_entry(_d, _i) \
57 for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries; \
58 (void *) _i < vstruct_end(&(_d)->field) && \
59 (void *) &_i->errors[0] < vstruct_end(&(_d)->field); \
60 _i = downgrade_entry_next_c(_i))
62 static inline unsigned bch2_sb_field_downgrade_u64s(unsigned nr)
64 return (sizeof(struct bch_sb_field_downgrade) +
65 sizeof(struct bch_sb_field_downgrade_entry) * nr) / sizeof(u64);
68 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
71 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
73 for_each_downgrade_entry(e, i) {
74 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
75 BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
76 prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
77 BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
78 BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
79 return -BCH_ERR_invalid_sb_downgrade;
86 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
87 struct bch_sb_field *f)
89 struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
91 if (out->nr_tabstops <= 1)
92 printbuf_tabstop_push(out, 16);
94 for_each_downgrade_entry(e, i) {
95 prt_str(out, "version:");
97 bch2_version_to_text(out, le16_to_cpu(i->version));
100 prt_str(out, "recovery passes:");
102 prt_bitflags(out, bch2_recovery_passes,
103 bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
106 prt_str(out, "errors:");
109 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
113 unsigned e = le16_to_cpu(i->errors[j]);
114 prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
120 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
121 .validate = bch2_sb_downgrade_validate,
122 .to_text = bch2_sb_downgrade_to_text,
125 int bch2_sb_downgrade_update(struct bch_fs *c)
127 darray_char table = {};
130 for (const struct downgrade_entry *src = downgrade_table;
131 src < downgrade_table + ARRAY_SIZE(downgrade_table);
133 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
136 struct bch_sb_field_downgrade_entry *dst;
137 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
139 ret = darray_make_room(&table, bytes);
143 dst = (void *) &darray_top(table);
144 dst->version = cpu_to_le16(src->version);
145 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
146 dst->recovery_passes[1] = 0;
147 dst->nr_errors = cpu_to_le16(src->nr_errors);
148 for (unsigned i = 0; i < src->nr_errors; i++)
149 dst->errors[i] = cpu_to_le16(src->errors[i]);
154 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
156 unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
158 if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
161 d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
163 ret = -BCH_ERR_ENOSPC_sb_downgrade;
167 memcpy(d->entries, table.data, table.nr);
168 memset_u64s_tail(d->entries, 0, table.nr);
174 int bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
176 struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
180 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
182 for_each_downgrade_entry(d, i) {
183 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
184 if (new_minor < minor && minor <= old_minor) {
185 ext->recovery_passes_required[0] |= i->recovery_passes[0];
186 ext->recovery_passes_required[1] |= i->recovery_passes[1];
188 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
189 unsigned e = le16_to_cpu(i->errors[j]);
190 if (e < BCH_SB_ERR_MAX)
191 __set_bit(e, c->sb.errors_silent);
192 if (e < sizeof(ext->errors_silent) * 8)
193 ext->errors_silent[e / 64] |= cpu_to_le64(BIT_ULL(e % 64));