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