xref: /linux/fs/bcachefs/sb-downgrade.c (revision a3f143c461444c0b56360bbf468615fa814a8372)
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_passes.h"
11 #include "sb-downgrade.h"
12 #include "sb-errors.h"
13 #include "super-io.h"
14 
15 #define RECOVERY_PASS_ALL_FSCK		BIT_ULL(63)
16 
17 /*
18  * Upgrade, downgrade tables - run certain recovery passes, fix certain errors
19  *
20  * x(version, recovery_passes, errors...)
21  */
22 #define UPGRADE_TABLE()						\
23 	x(backpointers,						\
24 	  RECOVERY_PASS_ALL_FSCK)				\
25 	x(inode_v3,						\
26 	  RECOVERY_PASS_ALL_FSCK)				\
27 	x(unwritten_extents,					\
28 	  RECOVERY_PASS_ALL_FSCK)				\
29 	x(bucket_gens,						\
30 	  BIT_ULL(BCH_RECOVERY_PASS_bucket_gens_init)|		\
31 	  RECOVERY_PASS_ALL_FSCK)				\
32 	x(lru_v2,						\
33 	  RECOVERY_PASS_ALL_FSCK)				\
34 	x(fragmentation_lru,					\
35 	  RECOVERY_PASS_ALL_FSCK)				\
36 	x(no_bps_in_alloc_keys,					\
37 	  RECOVERY_PASS_ALL_FSCK)				\
38 	x(snapshot_trees,					\
39 	  RECOVERY_PASS_ALL_FSCK)				\
40 	x(snapshot_skiplists,					\
41 	  BIT_ULL(BCH_RECOVERY_PASS_check_snapshots),		\
42 	  BCH_FSCK_ERR_snapshot_bad_depth,			\
43 	  BCH_FSCK_ERR_snapshot_bad_skiplist)			\
44 	x(deleted_inodes,					\
45 	  BIT_ULL(BCH_RECOVERY_PASS_check_inodes),		\
46 	  BCH_FSCK_ERR_unlinked_inode_not_on_deleted_list)	\
47 	x(rebalance_work,					\
48 	  BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance))	\
49 	x(subvolume_fs_parent,					\
50 	  BIT_ULL(BCH_RECOVERY_PASS_check_dirents),		\
51 	  BCH_FSCK_ERR_subvol_fs_path_parent_wrong)		\
52 	x(btree_subvolume_children,				\
53 	  BIT_ULL(BCH_RECOVERY_PASS_check_subvols),		\
54 	  BCH_FSCK_ERR_subvol_children_not_set)			\
55 	x(mi_btree_bitmap,					\
56 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
57 	  BCH_FSCK_ERR_btree_bitmap_not_marked)			\
58 	x(disk_accounting_v2,					\
59 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
60 	  BCH_FSCK_ERR_bkey_version_in_future,			\
61 	  BCH_FSCK_ERR_dev_usage_buckets_wrong,			\
62 	  BCH_FSCK_ERR_dev_usage_sectors_wrong,			\
63 	  BCH_FSCK_ERR_dev_usage_fragmented_wrong,		\
64 	  BCH_FSCK_ERR_accounting_mismatch)			\
65 	x(disk_accounting_v3,					\
66 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
67 	  BCH_FSCK_ERR_bkey_version_in_future,			\
68 	  BCH_FSCK_ERR_dev_usage_buckets_wrong,			\
69 	  BCH_FSCK_ERR_dev_usage_sectors_wrong,			\
70 	  BCH_FSCK_ERR_dev_usage_fragmented_wrong,		\
71 	  BCH_FSCK_ERR_accounting_mismatch,			\
72 	  BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0,	\
73 	  BCH_FSCK_ERR_accounting_key_replicas_nr_required_bad,	\
74 	  BCH_FSCK_ERR_accounting_key_replicas_devs_unsorted,	\
75 	  BCH_FSCK_ERR_accounting_key_junk_at_end)		\
76 	x(disk_accounting_inum,					\
77 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
78 	  BCH_FSCK_ERR_accounting_mismatch)			\
79 	x(rebalance_work_acct_fix,				\
80 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
81 	  BCH_FSCK_ERR_accounting_mismatch)			\
82 	x(inode_has_child_snapshots,				\
83 	  BIT_ULL(BCH_RECOVERY_PASS_check_inodes),		\
84 	  BCH_FSCK_ERR_inode_has_child_snapshots_wrong)
85 
86 #define DOWNGRADE_TABLE()					\
87 	x(bucket_stripe_sectors,				\
88 	  0)							\
89 	x(disk_accounting_v2,					\
90 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
91 	  BCH_FSCK_ERR_dev_usage_buckets_wrong,			\
92 	  BCH_FSCK_ERR_dev_usage_sectors_wrong,			\
93 	  BCH_FSCK_ERR_dev_usage_fragmented_wrong,		\
94 	  BCH_FSCK_ERR_fs_usage_hidden_wrong,			\
95 	  BCH_FSCK_ERR_fs_usage_btree_wrong,			\
96 	  BCH_FSCK_ERR_fs_usage_data_wrong,			\
97 	  BCH_FSCK_ERR_fs_usage_cached_wrong,			\
98 	  BCH_FSCK_ERR_fs_usage_reserved_wrong,			\
99 	  BCH_FSCK_ERR_fs_usage_nr_inodes_wrong,		\
100 	  BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong,	\
101 	  BCH_FSCK_ERR_fs_usage_replicas_wrong,			\
102 	  BCH_FSCK_ERR_bkey_version_in_future)			\
103 	x(disk_accounting_v3,					\
104 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
105 	  BCH_FSCK_ERR_dev_usage_buckets_wrong,			\
106 	  BCH_FSCK_ERR_dev_usage_sectors_wrong,			\
107 	  BCH_FSCK_ERR_dev_usage_fragmented_wrong,		\
108 	  BCH_FSCK_ERR_fs_usage_hidden_wrong,			\
109 	  BCH_FSCK_ERR_fs_usage_btree_wrong,			\
110 	  BCH_FSCK_ERR_fs_usage_data_wrong,			\
111 	  BCH_FSCK_ERR_fs_usage_cached_wrong,			\
112 	  BCH_FSCK_ERR_fs_usage_reserved_wrong,			\
113 	  BCH_FSCK_ERR_fs_usage_nr_inodes_wrong,		\
114 	  BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong,	\
115 	  BCH_FSCK_ERR_fs_usage_replicas_wrong,			\
116 	  BCH_FSCK_ERR_accounting_replicas_not_marked,		\
117 	  BCH_FSCK_ERR_bkey_version_in_future)			\
118 	x(rebalance_work_acct_fix,				\
119 	  BIT_ULL(BCH_RECOVERY_PASS_check_allocations),		\
120 	  BCH_FSCK_ERR_accounting_mismatch)
121 
122 struct upgrade_downgrade_entry {
123 	u64		recovery_passes;
124 	u16		version;
125 	u16		nr_errors;
126 	const u16	*errors;
127 };
128 
129 #define x(ver, passes, ...) static const u16 upgrade_##ver##_errors[] = { __VA_ARGS__ };
130 UPGRADE_TABLE()
131 #undef x
132 
133 static const struct upgrade_downgrade_entry upgrade_table[] = {
134 #define x(ver, passes, ...) {					\
135 	.recovery_passes	= passes,			\
136 	.version		= bcachefs_metadata_version_##ver,\
137 	.nr_errors		= ARRAY_SIZE(upgrade_##ver##_errors),	\
138 	.errors			= upgrade_##ver##_errors,	\
139 },
140 UPGRADE_TABLE()
141 #undef x
142 };
143 
144 static int have_stripes(struct bch_fs *c)
145 {
146 	return !btree_node_fake(c->btree_roots_known[BTREE_ID_stripes].b);
147 }
148 
149 int bch2_sb_set_upgrade_extra(struct bch_fs *c)
150 {
151 	unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version;
152 	unsigned new_version = c->sb.version;
153 	bool write_sb = false;
154 	int ret = 0;
155 
156 	mutex_lock(&c->sb_lock);
157 	struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
158 
159 	if (old_version <  bcachefs_metadata_version_bucket_stripe_sectors &&
160 	    new_version >= bcachefs_metadata_version_bucket_stripe_sectors &&
161 	    (ret = have_stripes(c) > 0)) {
162 		__set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_allocations, ext->recovery_passes_required);
163 		__set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent);
164 		__set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_sectors_wrong, ext->errors_silent);
165 		write_sb = true;
166 	}
167 
168 	if (write_sb)
169 		bch2_write_super(c);
170 	mutex_unlock(&c->sb_lock);
171 
172 	return ret < 0 ? ret : 0;
173 }
174 
175 void bch2_sb_set_upgrade(struct bch_fs *c,
176 			 unsigned old_version,
177 			 unsigned new_version)
178 {
179 	lockdep_assert_held(&c->sb_lock);
180 
181 	struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
182 
183 	for (const struct upgrade_downgrade_entry *i = upgrade_table;
184 	     i < upgrade_table + ARRAY_SIZE(upgrade_table);
185 	     i++)
186 		if (i->version > old_version && i->version <= new_version) {
187 			u64 passes = i->recovery_passes;
188 
189 			if (passes & RECOVERY_PASS_ALL_FSCK)
190 				passes |= bch2_fsck_recovery_passes();
191 			passes &= ~RECOVERY_PASS_ALL_FSCK;
192 
193 			ext->recovery_passes_required[0] |=
194 				cpu_to_le64(bch2_recovery_passes_to_stable(passes));
195 
196 			for (const u16 *e = i->errors; e < i->errors + i->nr_errors; e++)
197 				__set_bit_le64(*e, ext->errors_silent);
198 		}
199 }
200 
201 #define x(ver, passes, ...) static const u16 downgrade_##ver##_errors[] = { __VA_ARGS__ };
202 DOWNGRADE_TABLE()
203 #undef x
204 
205 static const struct upgrade_downgrade_entry downgrade_table[] = {
206 #define x(ver, passes, ...) {					\
207 	.recovery_passes	= passes,			\
208 	.version		= bcachefs_metadata_version_##ver,\
209 	.nr_errors		= ARRAY_SIZE(downgrade_##ver##_errors),	\
210 	.errors			= downgrade_##ver##_errors,	\
211 },
212 DOWNGRADE_TABLE()
213 #undef x
214 };
215 
216 static int downgrade_table_extra(struct bch_fs *c, darray_char *table)
217 {
218 	struct bch_sb_field_downgrade_entry *dst = (void *) &darray_top(*table);
219 	unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors);
220 	int ret = 0;
221 
222 	unsigned nr_errors = le16_to_cpu(dst->nr_errors);
223 
224 	switch (le16_to_cpu(dst->version)) {
225 	case bcachefs_metadata_version_bucket_stripe_sectors:
226 		if (have_stripes(c)) {
227 			bytes += sizeof(dst->errors[0]) * 2;
228 
229 			ret = darray_make_room(table, bytes);
230 			if (ret)
231 				return ret;
232 
233 			/* open coded __set_bit_le64, as dst is packed and
234 			 * dst->recovery_passes is misaligned */
235 			unsigned b = BCH_RECOVERY_PASS_STABLE_check_allocations;
236 			dst->recovery_passes[b / 64] |= cpu_to_le64(BIT_ULL(b % 64));
237 
238 			dst->errors[nr_errors++] = cpu_to_le16(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong);
239 		}
240 		break;
241 	}
242 
243 	dst->nr_errors = cpu_to_le16(nr_errors);
244 	return ret;
245 }
246 
247 static inline const struct bch_sb_field_downgrade_entry *
248 downgrade_entry_next_c(const struct bch_sb_field_downgrade_entry *e)
249 {
250 	return (void *) &e->errors[le16_to_cpu(e->nr_errors)];
251 }
252 
253 #define for_each_downgrade_entry(_d, _i)						\
254 	for (const struct bch_sb_field_downgrade_entry *_i = (_d)->entries;		\
255 	     (void *) _i	< vstruct_end(&(_d)->field) &&				\
256 	     (void *) &_i->errors[0] <= vstruct_end(&(_d)->field) &&			\
257 	     (void *) downgrade_entry_next_c(_i) <= vstruct_end(&(_d)->field);		\
258 	     _i = downgrade_entry_next_c(_i))
259 
260 static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
261 				      enum bch_validate_flags flags, struct printbuf *err)
262 {
263 	struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
264 
265 	for (const struct bch_sb_field_downgrade_entry *i = e->entries;
266 	     (void *) i	< vstruct_end(&e->field);
267 	     i = downgrade_entry_next_c(i)) {
268 		/*
269 		 * Careful: sb_field_downgrade_entry is only 2 byte aligned, but
270 		 * section sizes are 8 byte aligned - an empty entry spanning
271 		 * the end of the section is allowed (and ignored):
272 		 */
273 		if ((void *) &i->errors[0] > vstruct_end(&e->field))
274 			break;
275 
276 		if (flags & BCH_VALIDATE_write &&
277 		    (void *) downgrade_entry_next_c(i) > vstruct_end(&e->field)) {
278 			prt_printf(err, "downgrade entry overruns end of superblock section");
279 			return -BCH_ERR_invalid_sb_downgrade;
280 		}
281 
282 		if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
283 		    BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
284 			prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",
285 				   BCH_VERSION_MAJOR(le16_to_cpu(i->version)),
286 				   BCH_VERSION_MAJOR(le16_to_cpu(sb->version)));
287 			return -BCH_ERR_invalid_sb_downgrade;
288 		}
289 	}
290 
291 	return 0;
292 }
293 
294 static void bch2_sb_downgrade_to_text(struct printbuf *out, struct bch_sb *sb,
295 				      struct bch_sb_field *f)
296 {
297 	struct bch_sb_field_downgrade *e = field_to_type(f, downgrade);
298 
299 	if (out->nr_tabstops <= 1)
300 		printbuf_tabstop_push(out, 16);
301 
302 	for_each_downgrade_entry(e, i) {
303 		prt_str(out, "version:\t");
304 		bch2_version_to_text(out, le16_to_cpu(i->version));
305 		prt_newline(out);
306 
307 		prt_str(out, "recovery passes:\t");
308 		prt_bitflags(out, bch2_recovery_passes,
309 			     bch2_recovery_passes_from_stable(le64_to_cpu(i->recovery_passes[0])));
310 		prt_newline(out);
311 
312 		prt_str(out, "errors:\t");
313 		bool first = true;
314 		for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
315 			if (!first)
316 				prt_char(out, ',');
317 			first = false;
318 			bch2_sb_error_id_to_text(out, le16_to_cpu(i->errors[j]));
319 		}
320 		prt_newline(out);
321 	}
322 }
323 
324 const struct bch_sb_field_ops bch_sb_field_ops_downgrade = {
325 	.validate	= bch2_sb_downgrade_validate,
326 	.to_text	= bch2_sb_downgrade_to_text,
327 };
328 
329 int bch2_sb_downgrade_update(struct bch_fs *c)
330 {
331 	if (!test_bit(BCH_FS_btree_running, &c->flags))
332 		return 0;
333 
334 	darray_char table = {};
335 	int ret = 0;
336 
337 	for (const struct upgrade_downgrade_entry *src = downgrade_table;
338 	     src < downgrade_table + ARRAY_SIZE(downgrade_table);
339 	     src++) {
340 		if (BCH_VERSION_MAJOR(src->version) != BCH_VERSION_MAJOR(le16_to_cpu(c->disk_sb.sb->version)))
341 			continue;
342 
343 		struct bch_sb_field_downgrade_entry *dst;
344 		unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * src->nr_errors;
345 
346 		ret = darray_make_room(&table, bytes);
347 		if (ret)
348 			goto out;
349 
350 		dst = (void *) &darray_top(table);
351 		dst->version = cpu_to_le16(src->version);
352 		dst->recovery_passes[0]	= cpu_to_le64(bch2_recovery_passes_to_stable(src->recovery_passes));
353 		dst->recovery_passes[1]	= 0;
354 		dst->nr_errors		= cpu_to_le16(src->nr_errors);
355 		for (unsigned i = 0; i < src->nr_errors; i++)
356 			dst->errors[i] = cpu_to_le16(src->errors[i]);
357 
358 		ret = downgrade_table_extra(c, &table);
359 		if (ret)
360 			goto out;
361 
362 		if (!dst->recovery_passes[0] &&
363 		    !dst->recovery_passes[1] &&
364 		    !dst->nr_errors)
365 			continue;
366 
367 		table.nr += sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors);
368 	}
369 
370 	struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
371 
372 	unsigned sb_u64s = DIV_ROUND_UP(sizeof(*d) + table.nr, sizeof(u64));
373 
374 	if (d && le32_to_cpu(d->field.u64s) > sb_u64s)
375 		goto out;
376 
377 	d = bch2_sb_field_resize(&c->disk_sb, downgrade, sb_u64s);
378 	if (!d) {
379 		ret = -BCH_ERR_ENOSPC_sb_downgrade;
380 		goto out;
381 	}
382 
383 	memcpy(d->entries, table.data, table.nr);
384 	memset_u64s_tail(d->entries, 0, table.nr);
385 out:
386 	darray_exit(&table);
387 	return ret;
388 }
389 
390 void bch2_sb_set_downgrade(struct bch_fs *c, unsigned new_minor, unsigned old_minor)
391 {
392 	struct bch_sb_field_downgrade *d = bch2_sb_field_get(c->disk_sb.sb, downgrade);
393 	if (!d)
394 		return;
395 
396 	struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
397 
398 	for_each_downgrade_entry(d, i) {
399 		unsigned minor = BCH_VERSION_MINOR(le16_to_cpu(i->version));
400 		if (new_minor < minor && minor <= old_minor) {
401 			ext->recovery_passes_required[0] |= i->recovery_passes[0];
402 			ext->recovery_passes_required[1] |= i->recovery_passes[1];
403 
404 			for (unsigned j = 0; j < le16_to_cpu(i->nr_errors); j++) {
405 				unsigned e = le16_to_cpu(i->errors[j]);
406 				if (e < BCH_FSCK_ERR_MAX)
407 					__set_bit(e, c->sb.errors_silent);
408 				if (e < sizeof(ext->errors_silent) * 8)
409 					__set_bit_le64(e, ext->errors_silent);
410 			}
411 		}
412 	}
413 }
414