]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sb-downgrade.c
Update bcachefs sources to 2a6125decb43 bcachefs: bch_sb_field_downgrade
[bcachefs-tools-debian] / libbcachefs / 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         x(disk_accounting_v2,                                   \
25           BIT_ULL(BCH_RECOVERY_PASS_check_alloc_info),          \
26           BCH_FSCK_ERR_dev_usage_buckets_wrong)
27
28 struct downgrade_entry {
29         u64             recovery_passes;
30         u16             version;
31         u16             nr_errors;
32         const u16       *errors;
33 };
34
35 #define x(ver, passes, ...) static const u16 ver_##errors[] = { __VA_ARGS__ };
36 DOWNGRADE_TABLE()
37 #undef x
38
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,                 \
45 },
46 DOWNGRADE_TABLE()
47 #undef x
48 };
49
50 static inline const struct bch_sb_field_downgrade_entry *
51 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
52 {
53         return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
54 }
55
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))
61
62 static inline unsigned bch2_sb_field_downgrade_u64s(unsigned nr)
63 {
64         return (sizeof(struct bch_sb_field_downgrade) +
65                 sizeof(struct bch_sb_field_downgrade_entry) * nr) / sizeof(u64);
66 }
67
68 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
69                                       struct printbuf *err)
70 {
71         struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
72
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;
80                 }
81         }
82
83         return 0;
84 }
85
86 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
87                                       struct bch_sb_field *f)
88 {
89         struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
90
91         if (out->nr_tabstops <= 1)
92                 printbuf_tabstop_push(out, 16);
93
94         for_each_downgrade_entry(e, i) {
95                 prt_str(out, "version:");
96                 prt_tab(out);
97                 bch2_version_to_text(out, le16_to_cpu(i->version));
98                 prt_newline(out);
99
100                 prt_str(out, "recovery passes:");
101                 prt_tab(out);
102                 prt_bitflags(out, bch2_recovery_passes,
103                              bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
104                 prt_newline(out);
105
106                 prt_str(out, "errors:");
107                 prt_tab(out);
108                 bool first = true;
109                 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
110                         if (!first)
111                                 prt_char(out, ',');
112                         first = false;
113                         unsigned e = le16_to_cpu(i->errors[j]);
114                         prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
115                 }
116                 prt_newline(out);
117         }
118 }
119
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,
123 };
124
125 int bch2_sb_downgrade_update(struct bch_fs *c)
126 {
127         darray_char table = {};
128         int ret = 0;
129
130         for (const struct downgrade_entry *src = downgrade_table;
131              src < downgrade_table + ARRAY_SIZE(downgrade_table);
132              src++) {
133                 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
134                         continue;
135
136                 struct bch_sb_field_downgrade_entry *dst;
137                 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
138
139                 ret = darray_make_room(&table, bytes);
140                 if (ret)
141                         goto out;
142
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]);
150
151                 table.nr += bytes;
152         }
153
154         struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
155
156         unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
157
158         if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
159                 goto out;
160
161         d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
162         if (!d) {
163                 ret = -BCH_ERR_ENOSPC_sb_downgrade;
164                 goto out;
165         }
166
167         memcpy(d->entries, table.data, table.nr);
168         memset_u64s_tail(d->entries, 0, table.nr);
169 out:
170         darray_exit(&table);
171         return ret;
172 }
173
174 int bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
175 {
176         struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
177         if (!d)
178                 return 0;
179
180         struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
181
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];
187
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));
194                         }
195                 }
196         }
197
198         return 0;
199 }