]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sb-downgrade.c
626eaaea5b01d7923a8fc79d5bda3c48876b3c92
[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 "recovery.h"
10 #include "sb-downgrade.h"
11 #include "sb-errors.h"
12 #include "super-io.h"
13
14 #include <linux/darray.h>
15
16 #define RECOVERY_PASS_ALL_FSCK          BIT_ULL(63)
17
18 /*
19  * Upgrade, downgrade tables - run certain recovery passes, fix certain errors
20  *
21  * x(version, recovery_passes, errors...)
22  */
23 #define UPGRADE_TABLE()                                         \
24         x(backpointers,                                         \
25           RECOVERY_PASS_ALL_FSCK)                               \
26         x(inode_v3,                                             \
27           RECOVERY_PASS_ALL_FSCK)                               \
28         x(unwritten_extents,                                    \
29           RECOVERY_PASS_ALL_FSCK)                               \
30         x(bucket_gens,                                          \
31           BIT_ULL(BCH_RECOVERY_PASS_bucket_gens_init)|          \
32           RECOVERY_PASS_ALL_FSCK)                               \
33         x(lru_v2,                                               \
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)                               \
39         x(snapshot_trees,                                       \
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)                   \
45         x(deleted_inodes,                                       \
46           BIT_ULL(BCH_RECOVERY_PASS_check_inodes),              \
47           BCH_FSCK_ERR_unlinked_inode_not_on_deleted_list)      \
48         x(rebalance_work,                                       \
49           BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance))
50
51 #define DOWNGRADE_TABLE()
52
53 struct upgrade_downgrade_entry {
54         u64             recovery_passes;
55         u16             version;
56         u16             nr_errors;
57         const u16       *errors;
58 };
59
60 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
61 UPGRADE_TABLE()
62 #undef x
63
64 static const struct upgrade_downgrade_entry upgrade_table[] = {
65 #define x(ver, passes, ...) {                                   \
66         .recovery_passes        = passes,                       \
67         .version                = bcachefs_metadata_version_##ver,\
68         .nr_errors              = ARRAY_SIZE(upgrade_##ver##_errors),   \
69         .errors                 = upgrade_##ver##_errors,       \
70 },
71 UPGRADE_TABLE()
72 #undef x
73 };
74
75 void bch2_sb_set_upgrade(struct bch_fs *c,
76                          unsigned old_version,
77                          unsigned new_version)
78 {
79         lockdep_assert_held(&c->sb_lock);
80
81         struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
82
83         for (const struct upgrade_downgrade_entry *i = upgrade_table;
84              i < upgrade_table + ARRAY_SIZE(upgrade_table);
85              i++)
86                 if (i->version > old_version && i->version <= new_version) {
87                         u64 passes = i->recovery_passes;
88
89                         if (passes & RECOVERY_PASS_ALL_FSCK)
90                                 passes |= bch2_fsck_recovery_passes();
91                         passes &= ~RECOVERY_PASS_ALL_FSCK;
92
93                         ext->recovery_passes_required[0] |=
94                                 cpu_to_le64(bch2_recovery_passes_to_stable(passes));
95
96                         for (const u16 *e = i->errors;
97                              e < i->errors + i->nr_errors;
98                              e++) {
99                                 __set_bit(*e, c->sb.errors_silent);
100                                 ext->errors_silent[*e / 64] |= cpu_to_le64(BIT_ULL(*e % 64));
101                         }
102                 }
103 }
104
105 #define x(ver, passes, ...) static const u16 downgrade_ver_##errors[] = { __VA_ARGS__ };
106 DOWNGRADE_TABLE()
107 #undef x
108
109 static const struct upgrade_downgrade_entry downgrade_table[] = {
110 #define x(ver, passes, ...) {                                   \
111         .recovery_passes        = passes,                       \
112         .version                = bcachefs_metadata_version_##ver,\
113         .nr_errors              = ARRAY_SIZE(downgrade_##ver##_errors), \
114         .errors                 = downgrade_##ver##_errors,     \
115 },
116 DOWNGRADE_TABLE()
117 #undef x
118 };
119
120 static inline const struct bch_sb_field_downgrade_entry *
121 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
122 {
123         return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
124 }
125
126 #define for_each_downgrade_entry(_d, _i)                                                \
127         for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries;             \
128              (void *) _i        < vstruct_end(&(_d)->field) &&                          \
129              (void *) &_i->errors[0] < vstruct_end(&(_d)->field);                       \
130              _i = downgrade_entry_next_c(_i))
131
132 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
133                                       struct printbuf *err)
134 {
135         struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
136
137         for_each_downgrade_entry(e, i) {
138                 if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
139                     BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
140                         prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
141                                    BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
142                                    BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
143                         return -BCH_ERR_invalid_sb_downgrade;
144                 }
145         }
146
147         return 0;
148 }
149
150 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
151                                       struct bch_sb_field *f)
152 {
153         struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
154
155         if (out->nr_tabstops <= 1)
156                 printbuf_tabstop_push(out, 16);
157
158         for_each_downgrade_entry(e, i) {
159                 prt_str(out, "version:");
160                 prt_tab(out);
161                 bch2_version_to_text(out, le16_to_cpu(i->version));
162                 prt_newline(out);
163
164                 prt_str(out, "recovery passes:");
165                 prt_tab(out);
166                 prt_bitflags(out, bch2_recovery_passes,
167                              bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
168                 prt_newline(out);
169
170                 prt_str(out, "errors:");
171                 prt_tab(out);
172                 bool first = true;
173                 for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
174                         if (!first)
175                                 prt_char(out, ',');
176                         first = false;
177                         unsigned e = le16_to_cpu(i->errors[j]);
178                         prt_str(out, e < BCH_SB_ERR_MAX ? bch2_sb_error_strs[e] : "(unknown)");
179                 }
180                 prt_newline(out);
181         }
182 }
183
184 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
185         .validate       = bch2_sb_downgrade_validate,
186         .to_text        = bch2_sb_downgrade_to_text,
187 };
188
189 int bch2_sb_downgrade_update(struct bch_fs *c)
190 {
191         darray_char table = {};
192         int ret = 0;
193
194         for (const struct upgrade_downgrade_entry *src = downgrade_table;
195              src < downgrade_table + ARRAY_SIZE(downgrade_table);
196              src++) {
197                 if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
198                         continue;
199
200                 struct bch_sb_field_downgrade_entry *dst;
201                 unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
202
203                 ret = darray_make_room(&table, bytes);
204                 if (ret)
205                         goto out;
206
207                 dst = (void *) &darray_top(table);
208                 dst->version = cpu_to_le16(src->version);
209                 dst->recovery_passes[0] = cpu_to_le64(src->recovery_passes);
210                 dst->recovery_passes[1] = 0;
211                 dst->nr_errors          = cpu_to_le16(src->nr_errors);
212                 for (unsigned i = 0; i < src->nr_errors; i++)
213                         dst->errors[i] = cpu_to_le16(src->errors[i]);
214
215                 table.nr += bytes;
216         }
217
218         struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
219
220         unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
221
222         if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
223                 goto out;
224
225         d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
226         if (!d) {
227                 ret = -BCH_ERR_ENOSPC_sb_downgrade;
228                 goto out;
229         }
230
231         memcpy(d->entries, table.data, table.nr);
232         memset_u64s_tail(d->entries, 0, table.nr);
233 out:
234         darray_exit(&table);
235         return ret;
236 }
237
238 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
239 {
240         struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
241         if (!d)
242                 return;
243
244         struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
245
246         for_each_downgrade_entry(d, i) {
247                 unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
248                 if (new_minor < minor && minor <= old_minor) {
249                         ext->recovery_passes_required[0] |= i->recovery_passes[0];
250                         ext->recovery_passes_required[1] |= i->recovery_passes[1];
251
252                         for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
253                                 unsigned e = le16_to_cpu(i->errors[j]);
254                                 if (e < BCH_SB_ERR_MAX)
255                                         __set_bit(e, c->sb.errors_silent);
256                                 if (e < sizeof(ext->errors_silent) * 8)
257                                         ext->errors_silent[e / 64] |= cpu_to_le64(BIT_ULL(e % 64));
258                         }
259                 }
260         }
261 }