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