xref: /linux-6.15/fs/bcachefs/bkey_methods.c (revision b65db750)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "backpointers.h"
5 #include "bkey_methods.h"
6 #include "btree_cache.h"
7 #include "btree_types.h"
8 #include "alloc_background.h"
9 #include "dirent.h"
10 #include "ec.h"
11 #include "error.h"
12 #include "extents.h"
13 #include "inode.h"
14 #include "io_misc.h"
15 #include "lru.h"
16 #include "quota.h"
17 #include "reflink.h"
18 #include "snapshot.h"
19 #include "subvolume.h"
20 #include "xattr.h"
21 
22 const char * const bch2_bkey_types[] = {
23 #define x(name, nr) #name,
24 	BCH_BKEY_TYPES()
25 #undef x
26 	NULL
27 };
28 
29 static int deleted_key_invalid(struct bch_fs *c, struct bkey_s_c k,
30 			       enum bkey_invalid_flags flags, struct printbuf *err)
31 {
32 	return 0;
33 }
34 
35 #define bch2_bkey_ops_deleted ((struct bkey_ops) {	\
36 	.key_invalid = deleted_key_invalid,		\
37 })
38 
39 #define bch2_bkey_ops_whiteout ((struct bkey_ops) {	\
40 	.key_invalid = deleted_key_invalid,		\
41 })
42 
43 static int empty_val_key_invalid(struct bch_fs *c, struct bkey_s_c k,
44 				 enum bkey_invalid_flags flags, struct printbuf *err)
45 {
46 	int ret = 0;
47 
48 	bkey_fsck_err_on(bkey_val_bytes(k.k), c, err,
49 			 bkey_val_size_nonzero,
50 			 "incorrect value size (%zu != 0)",
51 			 bkey_val_bytes(k.k));
52 fsck_err:
53 	return ret;
54 }
55 
56 #define bch2_bkey_ops_error ((struct bkey_ops) {	\
57 	.key_invalid = empty_val_key_invalid,		\
58 })
59 
60 static int key_type_cookie_invalid(struct bch_fs *c, struct bkey_s_c k,
61 				   enum bkey_invalid_flags flags, struct printbuf *err)
62 {
63 	return 0;
64 }
65 
66 #define bch2_bkey_ops_cookie ((struct bkey_ops) {	\
67 	.key_invalid	= key_type_cookie_invalid,	\
68 	.min_val_size	= 8,				\
69 })
70 
71 #define bch2_bkey_ops_hash_whiteout ((struct bkey_ops) {\
72 	.key_invalid = empty_val_key_invalid,		\
73 })
74 
75 static int key_type_inline_data_invalid(struct bch_fs *c, struct bkey_s_c k,
76 					enum bkey_invalid_flags flags, struct printbuf *err)
77 {
78 	return 0;
79 }
80 
81 static void key_type_inline_data_to_text(struct printbuf *out, struct bch_fs *c,
82 					 struct bkey_s_c k)
83 {
84 	struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k);
85 	unsigned datalen = bkey_inline_data_bytes(k.k);
86 
87 	prt_printf(out, "datalen %u: %*phN",
88 	       datalen, min(datalen, 32U), d.v->data);
89 }
90 
91 #define bch2_bkey_ops_inline_data ((struct bkey_ops) {	\
92 	.key_invalid	= key_type_inline_data_invalid,	\
93 	.val_to_text	= key_type_inline_data_to_text,	\
94 })
95 
96 static bool key_type_set_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
97 {
98 	bch2_key_resize(l.k, l.k->size + r.k->size);
99 	return true;
100 }
101 
102 #define bch2_bkey_ops_set ((struct bkey_ops) {		\
103 	.key_invalid	= empty_val_key_invalid,	\
104 	.key_merge	= key_type_set_merge,		\
105 })
106 
107 const struct bkey_ops bch2_bkey_ops[] = {
108 #define x(name, nr) [KEY_TYPE_##name]	= bch2_bkey_ops_##name,
109 	BCH_BKEY_TYPES()
110 #undef x
111 };
112 
113 const struct bkey_ops bch2_bkey_null_ops = {
114 };
115 
116 int bch2_bkey_val_invalid(struct bch_fs *c, struct bkey_s_c k,
117 			  enum bkey_invalid_flags flags,
118 			  struct printbuf *err)
119 {
120 	const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
121 	int ret = 0;
122 
123 	bkey_fsck_err_on(bkey_val_bytes(k.k) < ops->min_val_size, c, err,
124 			 bkey_val_size_too_small,
125 			 "bad val size (%zu < %u)",
126 			 bkey_val_bytes(k.k), ops->min_val_size);
127 
128 	if (!ops->key_invalid)
129 		return 0;
130 
131 	ret = ops->key_invalid(c, k, flags, err);
132 fsck_err:
133 	return ret;
134 }
135 
136 static u64 bch2_key_types_allowed[] = {
137 	[BKEY_TYPE_btree] =
138 		BIT_ULL(KEY_TYPE_deleted)|
139 		BIT_ULL(KEY_TYPE_btree_ptr)|
140 		BIT_ULL(KEY_TYPE_btree_ptr_v2),
141 #define x(name, nr, flags, keys)	[BKEY_TYPE_##name] = BIT_ULL(KEY_TYPE_deleted)|keys,
142 	BCH_BTREE_IDS()
143 #undef x
144 };
145 
146 const char *bch2_btree_node_type_str(enum btree_node_type type)
147 {
148 	return type == BKEY_TYPE_btree ? "internal btree node" : bch2_btree_id_str(type - 1);
149 }
150 
151 int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
152 			enum btree_node_type type,
153 			enum bkey_invalid_flags flags,
154 			struct printbuf *err)
155 {
156 	int ret = 0;
157 
158 	bkey_fsck_err_on(k.k->u64s < BKEY_U64s, c, err,
159 			 bkey_u64s_too_small,
160 			 "u64s too small (%u < %zu)", k.k->u64s, BKEY_U64s);
161 
162 	if (type >= BKEY_TYPE_NR)
163 		return 0;
164 
165 	bkey_fsck_err_on((flags & BKEY_INVALID_COMMIT) &&
166 			 !(bch2_key_types_allowed[type] & BIT_ULL(k.k->type)), c, err,
167 			 bkey_invalid_type_for_btree,
168 			 "invalid key type for btree %s (%s)",
169 			 bch2_btree_node_type_str(type), bch2_bkey_types[k.k->type]);
170 
171 	if (btree_node_type_is_extents(type) && !bkey_whiteout(k.k)) {
172 		bkey_fsck_err_on(k.k->size == 0, c, err,
173 				 bkey_extent_size_zero,
174 				 "size == 0");
175 
176 		bkey_fsck_err_on(k.k->size > k.k->p.offset, c, err,
177 				 bkey_extent_size_greater_than_offset,
178 				 "size greater than offset (%u > %llu)",
179 				 k.k->size, k.k->p.offset);
180 	} else {
181 		bkey_fsck_err_on(k.k->size, c, err,
182 				 bkey_size_nonzero,
183 				 "size != 0");
184 	}
185 
186 	if (type != BKEY_TYPE_btree) {
187 		enum btree_id btree = type - 1;
188 
189 		bkey_fsck_err_on(!btree_type_has_snapshots(btree) &&
190 				 k.k->p.snapshot, c, err,
191 				 bkey_snapshot_nonzero,
192 				 "nonzero snapshot");
193 
194 		bkey_fsck_err_on(btree_type_has_snapshots(btree) &&
195 				 !k.k->p.snapshot, c, err,
196 				 bkey_snapshot_zero,
197 				 "snapshot == 0");
198 
199 		bkey_fsck_err_on(bkey_eq(k.k->p, POS_MAX), c, err,
200 				 bkey_at_pos_max,
201 				 "key at POS_MAX");
202 	}
203 fsck_err:
204 	return ret;
205 }
206 
207 int bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
208 		      enum btree_node_type type,
209 		      enum bkey_invalid_flags flags,
210 		      struct printbuf *err)
211 {
212 	return __bch2_bkey_invalid(c, k, type, flags, err) ?:
213 		bch2_bkey_val_invalid(c, k, flags, err);
214 }
215 
216 int bch2_bkey_in_btree_node(struct bch_fs *c, struct btree *b,
217 			    struct bkey_s_c k, struct printbuf *err)
218 {
219 	int ret = 0;
220 
221 	bkey_fsck_err_on(bpos_lt(k.k->p, b->data->min_key), c, err,
222 			 bkey_before_start_of_btree_node,
223 			 "key before start of btree node");
224 
225 	bkey_fsck_err_on(bpos_gt(k.k->p, b->data->max_key), c, err,
226 			 bkey_after_end_of_btree_node,
227 			 "key past end of btree node");
228 fsck_err:
229 	return ret;
230 }
231 
232 void bch2_bpos_to_text(struct printbuf *out, struct bpos pos)
233 {
234 	if (bpos_eq(pos, POS_MIN))
235 		prt_printf(out, "POS_MIN");
236 	else if (bpos_eq(pos, POS_MAX))
237 		prt_printf(out, "POS_MAX");
238 	else if (bpos_eq(pos, SPOS_MAX))
239 		prt_printf(out, "SPOS_MAX");
240 	else {
241 		if (pos.inode == U64_MAX)
242 			prt_printf(out, "U64_MAX");
243 		else
244 			prt_printf(out, "%llu", pos.inode);
245 		prt_printf(out, ":");
246 		if (pos.offset == U64_MAX)
247 			prt_printf(out, "U64_MAX");
248 		else
249 			prt_printf(out, "%llu", pos.offset);
250 		prt_printf(out, ":");
251 		if (pos.snapshot == U32_MAX)
252 			prt_printf(out, "U32_MAX");
253 		else
254 			prt_printf(out, "%u", pos.snapshot);
255 	}
256 }
257 
258 void bch2_bkey_to_text(struct printbuf *out, const struct bkey *k)
259 {
260 	if (k) {
261 		prt_printf(out, "u64s %u type ", k->u64s);
262 
263 		if (k->type < KEY_TYPE_MAX)
264 			prt_printf(out, "%s ", bch2_bkey_types[k->type]);
265 		else
266 			prt_printf(out, "%u ", k->type);
267 
268 		bch2_bpos_to_text(out, k->p);
269 
270 		prt_printf(out, " len %u ver %llu", k->size, k->version.lo);
271 	} else {
272 		prt_printf(out, "(null)");
273 	}
274 }
275 
276 void bch2_val_to_text(struct printbuf *out, struct bch_fs *c,
277 		      struct bkey_s_c k)
278 {
279 	const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
280 
281 	if (likely(ops->val_to_text))
282 		ops->val_to_text(out, c, k);
283 }
284 
285 void bch2_bkey_val_to_text(struct printbuf *out, struct bch_fs *c,
286 			   struct bkey_s_c k)
287 {
288 	bch2_bkey_to_text(out, k.k);
289 
290 	if (bkey_val_bytes(k.k)) {
291 		prt_printf(out, ": ");
292 		bch2_val_to_text(out, c, k);
293 	}
294 }
295 
296 void bch2_bkey_swab_val(struct bkey_s k)
297 {
298 	const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
299 
300 	if (ops->swab)
301 		ops->swab(k);
302 }
303 
304 bool bch2_bkey_normalize(struct bch_fs *c, struct bkey_s k)
305 {
306 	const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
307 
308 	return ops->key_normalize
309 		? ops->key_normalize(c, k)
310 		: false;
311 }
312 
313 bool bch2_bkey_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
314 {
315 	const struct bkey_ops *ops = bch2_bkey_type_ops(l.k->type);
316 
317 	return ops->key_merge &&
318 		bch2_bkey_maybe_mergable(l.k, r.k) &&
319 		(u64) l.k->size + r.k->size <= KEY_SIZE_MAX &&
320 		!bch2_key_merging_disabled &&
321 		ops->key_merge(c, l, r);
322 }
323 
324 static const struct old_bkey_type {
325 	u8		btree_node_type;
326 	u8		old;
327 	u8		new;
328 } bkey_renumber_table[] = {
329 	{BKEY_TYPE_btree,	128, KEY_TYPE_btree_ptr		},
330 	{BKEY_TYPE_extents,	128, KEY_TYPE_extent		},
331 	{BKEY_TYPE_extents,	129, KEY_TYPE_extent		},
332 	{BKEY_TYPE_extents,	130, KEY_TYPE_reservation	},
333 	{BKEY_TYPE_inodes,	128, KEY_TYPE_inode		},
334 	{BKEY_TYPE_inodes,	130, KEY_TYPE_inode_generation	},
335 	{BKEY_TYPE_dirents,	128, KEY_TYPE_dirent		},
336 	{BKEY_TYPE_dirents,	129, KEY_TYPE_hash_whiteout	},
337 	{BKEY_TYPE_xattrs,	128, KEY_TYPE_xattr		},
338 	{BKEY_TYPE_xattrs,	129, KEY_TYPE_hash_whiteout	},
339 	{BKEY_TYPE_alloc,	128, KEY_TYPE_alloc		},
340 	{BKEY_TYPE_quotas,	128, KEY_TYPE_quota		},
341 };
342 
343 void bch2_bkey_renumber(enum btree_node_type btree_node_type,
344 			struct bkey_packed *k,
345 			int write)
346 {
347 	const struct old_bkey_type *i;
348 
349 	for (i = bkey_renumber_table;
350 	     i < bkey_renumber_table + ARRAY_SIZE(bkey_renumber_table);
351 	     i++)
352 		if (btree_node_type == i->btree_node_type &&
353 		    k->type == (write ? i->new : i->old)) {
354 			k->type = write ? i->old : i->new;
355 			break;
356 		}
357 }
358 
359 void __bch2_bkey_compat(unsigned level, enum btree_id btree_id,
360 			unsigned version, unsigned big_endian,
361 			int write,
362 			struct bkey_format *f,
363 			struct bkey_packed *k)
364 {
365 	const struct bkey_ops *ops;
366 	struct bkey uk;
367 	unsigned nr_compat = 5;
368 	int i;
369 
370 	/*
371 	 * Do these operations in reverse order in the write path:
372 	 */
373 
374 	for (i = 0; i < nr_compat; i++)
375 	switch (!write ? i : nr_compat - 1 - i) {
376 	case 0:
377 		if (big_endian != CPU_BIG_ENDIAN)
378 			bch2_bkey_swab_key(f, k);
379 		break;
380 	case 1:
381 		if (version < bcachefs_metadata_version_bkey_renumber)
382 			bch2_bkey_renumber(__btree_node_type(level, btree_id), k, write);
383 		break;
384 	case 2:
385 		if (version < bcachefs_metadata_version_inode_btree_change &&
386 		    btree_id == BTREE_ID_inodes) {
387 			if (!bkey_packed(k)) {
388 				struct bkey_i *u = packed_to_bkey(k);
389 
390 				swap(u->k.p.inode, u->k.p.offset);
391 			} else if (f->bits_per_field[BKEY_FIELD_INODE] &&
392 				   f->bits_per_field[BKEY_FIELD_OFFSET]) {
393 				struct bkey_format tmp = *f, *in = f, *out = &tmp;
394 
395 				swap(tmp.bits_per_field[BKEY_FIELD_INODE],
396 				     tmp.bits_per_field[BKEY_FIELD_OFFSET]);
397 				swap(tmp.field_offset[BKEY_FIELD_INODE],
398 				     tmp.field_offset[BKEY_FIELD_OFFSET]);
399 
400 				if (!write)
401 					swap(in, out);
402 
403 				uk = __bch2_bkey_unpack_key(in, k);
404 				swap(uk.p.inode, uk.p.offset);
405 				BUG_ON(!bch2_bkey_pack_key(k, &uk, out));
406 			}
407 		}
408 		break;
409 	case 3:
410 		if (version < bcachefs_metadata_version_snapshot &&
411 		    (level || btree_type_has_snapshots(btree_id))) {
412 			struct bkey_i *u = packed_to_bkey(k);
413 
414 			if (u) {
415 				u->k.p.snapshot = write
416 					? 0 : U32_MAX;
417 			} else {
418 				u64 min_packed = le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]);
419 				u64 max_packed = min_packed +
420 					~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]);
421 
422 				uk = __bch2_bkey_unpack_key(f, k);
423 				uk.p.snapshot = write
424 					? min_packed : min_t(u64, U32_MAX, max_packed);
425 
426 				BUG_ON(!bch2_bkey_pack_key(k, &uk, f));
427 			}
428 		}
429 
430 		break;
431 	case 4: {
432 		struct bkey_s u;
433 
434 		if (!bkey_packed(k)) {
435 			u = bkey_i_to_s(packed_to_bkey(k));
436 		} else {
437 			uk = __bch2_bkey_unpack_key(f, k);
438 			u.k = &uk;
439 			u.v = bkeyp_val(f, k);
440 		}
441 
442 		if (big_endian != CPU_BIG_ENDIAN)
443 			bch2_bkey_swab_val(u);
444 
445 		ops = bch2_bkey_type_ops(k->type);
446 
447 		if (ops->compat)
448 			ops->compat(btree_id, version, big_endian, write, u);
449 		break;
450 	}
451 	default:
452 		BUG();
453 	}
454 }
455