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