Merge tag 'x86-asm-2024-01-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[linux-2.6-microblaze.git] / fs / bcachefs / sb-downgrade.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Superblock section that contains a list of recovery passes to run when
5  * downgrading past a given version
6  */
7
8 #include "bcachefs.h"
9 #include "darray.h"
10 #include "recovery.h"
11 #include "sb-downgrade.h"
12 #include "sb-errors.h"
13 #include "super-io.h"
14
15 /*
16  * Downgrade table:
17  * When dowgrading past certain versions, we need to run certain recovery passes
18  * and fix certain errors:
19  *
20  * x(version, recovery_passes, errors...)
21  */
22
23 #define DOWNGRADE_TABLE()
24
25 struct downgrade_entry {
26         u64             recovery_passes;
27         u16             version;
28         u16             nr_errors;
29         const u16       *errors;
30 };
31
32 #define x(ver, passes, ...) static const u16 ver_##errors[] = { __VA_ARGS__ };
33 DOWNGRADE_TABLE()
34 #undef x
35
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,                 \
42 },
43 DOWNGRADE_TABLE()
44 #undef x
45 };
46
47 static inline const struct bch_sb_field_downgrade_entry *
48 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
49 {
50         return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
51 }
52
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))
58
59 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
60                                       struct printbuf *err)
61 {
62         struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
63
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;
71                 }
72         }
73
74         return 0;
75 }
76
77 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
78                                       struct bch_sb_field *f)
79 {
80         struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
81
82         if (out->nr_tabstops <= 1)
83                 printbuf_tabstop_push(out, 16);
84
85         for_each_downgrade_entry(e, i) {
86                 prt_str(out, "version:");
87                 prt_tab(out);
88                 bch2_version_to_text(out, le16_to_cpu(i->version));
89                 prt_newline(out);
90
91                 prt_str(out, "recovery passes:");
92                 prt_tab(out);
93                 prt_bitflags(out, bch2_recovery_passes,
94                              bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
95                 prt_newline(out);
96
97                 prt_str(out, "errors:");
98                 prt_tab(out);
99                 bool first = true;
100                 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
101                         if (!first)
102                                 prt_char(out, ',');
103                         first = false;
104                         unsigned e = le16_to_cpu(i->errors[j]);
105                         prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
106                 }
107                 prt_newline(out);
108         }
109 }
110
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,
114 };
115
116 int bch2_sb_downgrade_update(struct bch_fs *c)
117 {
118         darray_char table = {};
119         int ret = 0;
120
121         for (const struct downgrade_entry *src = downgrade_table;
122              src < downgrade_table + ARRAY_SIZE(downgrade_table);
123              src++) {
124                 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
125                         continue;
126
127                 struct bch_sb_field_downgrade_entry *dst;
128                 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
129
130                 ret = darray_make_room(&table, bytes);
131                 if (ret)
132                         goto out;
133
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]);
141
142                 table.nr += bytes;
143         }
144
145         struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
146
147         unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
148
149         if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
150                 goto out;
151
152         d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
153         if (!d) {
154                 ret = -BCH_ERR_ENOSPC_sb_downgrade;
155                 goto out;
156         }
157
158         memcpy(d->entries, table.data, table.nr);
159         memset_u64s_tail(d->entries, 0, table.nr);
160 out:
161         darray_exit(&table);
162         return ret;
163 }
164
165 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
166 {
167         struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
168         if (!d)
169                 return;
170
171         struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
172
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];
178
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));
185                         }
186                 }
187         }
188 }