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