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