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