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