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