xref: /linux/fs/bcachefs/bkey.h (revision b8e85e6f3a09fc56b0ff574887798962ef8a8f80)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_H
3 #define _BCACHEFS_BKEY_H
4 
5 #include <linux/bug.h>
6 #include "bcachefs_format.h"
7 
8 #include "btree_types.h"
9 #include "util.h"
10 #include "vstructs.h"
11 
12 enum bkey_invalid_flags {
13 	BKEY_INVALID_WRITE		= (1U << 0),
14 	BKEY_INVALID_COMMIT		= (1U << 1),
15 	BKEY_INVALID_JOURNAL		= (1U << 2),
16 };
17 
18 #if 0
19 
20 /*
21  * compiled unpack functions are disabled, pending a new interface for
22  * dynamically allocating executable memory:
23  */
24 
25 #ifdef CONFIG_X86_64
26 #define HAVE_BCACHEFS_COMPILED_UNPACK	1
27 #endif
28 #endif
29 
30 void bch2_bkey_packed_to_binary_text(struct printbuf *,
31 				     const struct bkey_format *,
32 				     const struct bkey_packed *);
33 
34 /* bkey with split value, const */
35 struct bkey_s_c {
36 	const struct bkey	*k;
37 	const struct bch_val	*v;
38 };
39 
40 /* bkey with split value */
41 struct bkey_s {
42 	union {
43 	struct {
44 		struct bkey	*k;
45 		struct bch_val	*v;
46 	};
47 	struct bkey_s_c		s_c;
48 	};
49 };
50 
51 #define bkey_p_next(_k)		vstruct_next(_k)
52 
53 static inline struct bkey_i *bkey_next(struct bkey_i *k)
54 {
55 	return (struct bkey_i *) ((u64 *) k->_data + k->k.u64s);
56 }
57 
58 #define bkey_val_u64s(_k)	((_k)->u64s - BKEY_U64s)
59 
60 static inline size_t bkey_val_bytes(const struct bkey *k)
61 {
62 	return bkey_val_u64s(k) * sizeof(u64);
63 }
64 
65 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
66 {
67 	unsigned u64s = BKEY_U64s + val_u64s;
68 
69 	BUG_ON(u64s > U8_MAX);
70 	k->u64s = u64s;
71 }
72 
73 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
74 {
75 	set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
76 }
77 
78 #define bkey_val_end(_k)	((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
79 
80 #define bkey_deleted(_k)	((_k)->type == KEY_TYPE_deleted)
81 
82 #define bkey_whiteout(_k)				\
83 	((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
84 
85 enum bkey_lr_packed {
86 	BKEY_PACKED_BOTH,
87 	BKEY_PACKED_RIGHT,
88 	BKEY_PACKED_LEFT,
89 	BKEY_PACKED_NONE,
90 };
91 
92 #define bkey_lr_packed(_l, _r)						\
93 	((_l)->format + ((_r)->format << 1))
94 
95 static inline void bkey_p_copy(struct bkey_packed *dst, const struct bkey_packed *src)
96 {
97 	memcpy_u64s_small(dst, src, src->u64s);
98 }
99 
100 static inline void bkey_copy(struct bkey_i *dst, const struct bkey_i *src)
101 {
102 	memcpy_u64s_small(dst, src, src->k.u64s);
103 }
104 
105 struct btree;
106 
107 __pure
108 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
109 					  const struct bkey_packed *,
110 					  const struct bkey_packed *);
111 __pure
112 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
113 
114 __pure
115 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
116 				     const struct bkey_packed *,
117 				     const struct btree *);
118 
119 __pure
120 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
121 					  const struct bkey_packed *,
122 					  const struct bpos *);
123 
124 __pure
125 int bch2_bkey_cmp_packed(const struct btree *,
126 			 const struct bkey_packed *,
127 			 const struct bkey_packed *);
128 
129 __pure
130 int __bch2_bkey_cmp_left_packed(const struct btree *,
131 				const struct bkey_packed *,
132 				const struct bpos *);
133 
134 static inline __pure
135 int bkey_cmp_left_packed(const struct btree *b,
136 			 const struct bkey_packed *l, const struct bpos *r)
137 {
138 	return __bch2_bkey_cmp_left_packed(b, l, r);
139 }
140 
141 /*
142  * The compiler generates better code when we pass bpos by ref, but it's often
143  * enough terribly convenient to pass it by val... as much as I hate c++, const
144  * ref would be nice here:
145  */
146 __pure __flatten
147 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
148 					     const struct bkey_packed *l,
149 					     struct bpos r)
150 {
151 	return bkey_cmp_left_packed(b, l, &r);
152 }
153 
154 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
155 {
156 	return  !((l.inode	^ r.inode) |
157 		  (l.offset	^ r.offset) |
158 		  (l.snapshot	^ r.snapshot));
159 }
160 
161 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
162 {
163 	return  l.inode	!= r.inode ? l.inode < r.inode :
164 		l.offset != r.offset ? l.offset < r.offset :
165 		l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
166 }
167 
168 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
169 {
170 	return  l.inode	!= r.inode ? l.inode < r.inode :
171 		l.offset != r.offset ? l.offset < r.offset :
172 		l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
173 }
174 
175 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
176 {
177 	return bpos_lt(r, l);
178 }
179 
180 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
181 {
182 	return bpos_le(r, l);
183 }
184 
185 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
186 {
187 	return  cmp_int(l.inode,    r.inode) ?:
188 		cmp_int(l.offset,   r.offset) ?:
189 		cmp_int(l.snapshot, r.snapshot);
190 }
191 
192 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
193 {
194 	return bpos_lt(l, r) ? l : r;
195 }
196 
197 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
198 {
199 	return bpos_gt(l, r) ? l : r;
200 }
201 
202 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
203 {
204 	return  !((l.inode	^ r.inode) |
205 		  (l.offset	^ r.offset));
206 }
207 
208 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
209 {
210 	return  l.inode	!= r.inode
211 		? l.inode < r.inode
212 		: l.offset < r.offset;
213 }
214 
215 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
216 {
217 	return  l.inode	!= r.inode
218 		? l.inode < r.inode
219 		: l.offset <= r.offset;
220 }
221 
222 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
223 {
224 	return bkey_lt(r, l);
225 }
226 
227 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
228 {
229 	return bkey_le(r, l);
230 }
231 
232 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
233 {
234 	return  cmp_int(l.inode,    r.inode) ?:
235 		cmp_int(l.offset,   r.offset);
236 }
237 
238 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
239 {
240 	return bkey_lt(l, r) ? l : r;
241 }
242 
243 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
244 {
245 	return bkey_gt(l, r) ? l : r;
246 }
247 
248 void bch2_bpos_swab(struct bpos *);
249 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
250 
251 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
252 {
253 	return  cmp_int(l.hi, r.hi) ?:
254 		cmp_int(l.lo, r.lo);
255 }
256 
257 #define ZERO_VERSION	((struct bversion) { .hi = 0, .lo = 0 })
258 #define MAX_VERSION	((struct bversion) { .hi = ~0, .lo = ~0ULL })
259 
260 static __always_inline int bversion_zero(struct bversion v)
261 {
262 	return !bversion_cmp(v, ZERO_VERSION);
263 }
264 
265 #ifdef CONFIG_BCACHEFS_DEBUG
266 /* statement expressions confusing unlikely()? */
267 #define bkey_packed(_k)							\
268 	({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT);			\
269 	 (_k)->format != KEY_FORMAT_CURRENT; })
270 #else
271 #define bkey_packed(_k)		((_k)->format != KEY_FORMAT_CURRENT)
272 #endif
273 
274 /*
275  * It's safe to treat an unpacked bkey as a packed one, but not the reverse
276  */
277 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
278 {
279 	return (struct bkey_packed *) k;
280 }
281 
282 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
283 {
284 	return (const struct bkey_packed *) k;
285 }
286 
287 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
288 {
289 	return bkey_packed(k) ? NULL : (struct bkey_i *) k;
290 }
291 
292 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
293 {
294 	return bkey_packed(k) ? NULL : (const struct bkey *) k;
295 }
296 
297 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
298 {
299 	return format->bits_per_field[BKEY_FIELD_INODE] +
300 		format->bits_per_field[BKEY_FIELD_OFFSET] +
301 		format->bits_per_field[BKEY_FIELD_SNAPSHOT];
302 }
303 
304 static inline struct bpos bpos_successor(struct bpos p)
305 {
306 	if (!++p.snapshot &&
307 	    !++p.offset &&
308 	    !++p.inode)
309 		BUG();
310 
311 	return p;
312 }
313 
314 static inline struct bpos bpos_predecessor(struct bpos p)
315 {
316 	if (!p.snapshot-- &&
317 	    !p.offset-- &&
318 	    !p.inode--)
319 		BUG();
320 
321 	return p;
322 }
323 
324 static inline struct bpos bpos_nosnap_successor(struct bpos p)
325 {
326 	p.snapshot = 0;
327 
328 	if (!++p.offset &&
329 	    !++p.inode)
330 		BUG();
331 
332 	return p;
333 }
334 
335 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
336 {
337 	p.snapshot = 0;
338 
339 	if (!p.offset-- &&
340 	    !p.inode--)
341 		BUG();
342 
343 	return p;
344 }
345 
346 static inline u64 bkey_start_offset(const struct bkey *k)
347 {
348 	return k->p.offset - k->size;
349 }
350 
351 static inline struct bpos bkey_start_pos(const struct bkey *k)
352 {
353 	return (struct bpos) {
354 		.inode		= k->p.inode,
355 		.offset		= bkey_start_offset(k),
356 		.snapshot	= k->p.snapshot,
357 	};
358 }
359 
360 /* Packed helpers */
361 
362 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
363 				      const struct bkey_packed *k)
364 {
365 	unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
366 
367 	EBUG_ON(k->u64s < ret);
368 	return ret;
369 }
370 
371 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
372 				       const struct bkey_packed *k)
373 {
374 	return bkeyp_key_u64s(format, k) * sizeof(u64);
375 }
376 
377 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
378 				      const struct bkey_packed *k)
379 {
380 	return k->u64s - bkeyp_key_u64s(format, k);
381 }
382 
383 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
384 				     const struct bkey_packed *k)
385 {
386 	return bkeyp_val_u64s(format, k) * sizeof(u64);
387 }
388 
389 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
390 				      struct bkey_packed *k, unsigned val_u64s)
391 {
392 	k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
393 }
394 
395 #define bkeyp_val(_format, _k)						\
396 	 ((struct bch_val *) ((u64 *) (_k)->_data + bkeyp_key_u64s(_format, _k)))
397 
398 extern const struct bkey_format bch2_bkey_format_current;
399 
400 bool bch2_bkey_transform(const struct bkey_format *,
401 			 struct bkey_packed *,
402 			 const struct bkey_format *,
403 			 const struct bkey_packed *);
404 
405 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
406 				   const struct bkey_packed *);
407 
408 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
409 struct bpos __bkey_unpack_pos(const struct bkey_format *,
410 			      const struct bkey_packed *);
411 #endif
412 
413 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
414 		   const struct bkey_format *);
415 
416 enum bkey_pack_pos_ret {
417 	BKEY_PACK_POS_EXACT,
418 	BKEY_PACK_POS_SMALLER,
419 	BKEY_PACK_POS_FAIL,
420 };
421 
422 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
423 					   const struct btree *);
424 
425 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
426 				 const struct btree *b)
427 {
428 	return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
429 }
430 
431 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
432 		 const struct bkey_packed *);
433 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
434 	       const struct bkey_format *);
435 
436 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
437 
438 static inline void
439 __bkey_unpack_key_format_checked(const struct btree *b,
440 			       struct bkey *dst,
441 			       const struct bkey_packed *src)
442 {
443 	if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
444 		compiled_unpack_fn unpack_fn = b->aux_data;
445 		unpack_fn(dst, src);
446 
447 		if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
448 		    bch2_expensive_debug_checks) {
449 			struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
450 
451 			BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
452 		}
453 	} else {
454 		*dst = __bch2_bkey_unpack_key(&b->format, src);
455 	}
456 }
457 
458 static inline struct bkey
459 bkey_unpack_key_format_checked(const struct btree *b,
460 			       const struct bkey_packed *src)
461 {
462 	struct bkey dst;
463 
464 	__bkey_unpack_key_format_checked(b, &dst, src);
465 	return dst;
466 }
467 
468 static inline void __bkey_unpack_key(const struct btree *b,
469 				     struct bkey *dst,
470 				     const struct bkey_packed *src)
471 {
472 	if (likely(bkey_packed(src)))
473 		__bkey_unpack_key_format_checked(b, dst, src);
474 	else
475 		*dst = *packed_to_bkey_c(src);
476 }
477 
478 /**
479  * bkey_unpack_key -- unpack just the key, not the value
480  */
481 static inline struct bkey bkey_unpack_key(const struct btree *b,
482 					  const struct bkey_packed *src)
483 {
484 	return likely(bkey_packed(src))
485 		? bkey_unpack_key_format_checked(b, src)
486 		: *packed_to_bkey_c(src);
487 }
488 
489 static inline struct bpos
490 bkey_unpack_pos_format_checked(const struct btree *b,
491 			       const struct bkey_packed *src)
492 {
493 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
494 	return bkey_unpack_key_format_checked(b, src).p;
495 #else
496 	return __bkey_unpack_pos(&b->format, src);
497 #endif
498 }
499 
500 static inline struct bpos bkey_unpack_pos(const struct btree *b,
501 					  const struct bkey_packed *src)
502 {
503 	return likely(bkey_packed(src))
504 		? bkey_unpack_pos_format_checked(b, src)
505 		: packed_to_bkey_c(src)->p;
506 }
507 
508 /* Disassembled bkeys */
509 
510 static inline struct bkey_s_c bkey_disassemble(const struct btree *b,
511 					       const struct bkey_packed *k,
512 					       struct bkey *u)
513 {
514 	__bkey_unpack_key(b, u, k);
515 
516 	return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
517 }
518 
519 /* non const version: */
520 static inline struct bkey_s __bkey_disassemble(const struct btree *b,
521 					       struct bkey_packed *k,
522 					       struct bkey *u)
523 {
524 	__bkey_unpack_key(b, u, k);
525 
526 	return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
527 }
528 
529 static inline u64 bkey_field_max(const struct bkey_format *f,
530 				 enum bch_bkey_fields nr)
531 {
532 	return f->bits_per_field[nr] < 64
533 		? (le64_to_cpu(f->field_offset[nr]) +
534 		   ~(~0ULL << f->bits_per_field[nr]))
535 		: U64_MAX;
536 }
537 
538 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
539 
540 int bch2_compile_bkey_format(const struct bkey_format *, void *);
541 
542 #else
543 
544 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
545 					  void *out) { return 0; }
546 
547 #endif
548 
549 static inline void bkey_reassemble(struct bkey_i *dst,
550 				   struct bkey_s_c src)
551 {
552 	dst->k = *src.k;
553 	memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
554 }
555 
556 #define bkey_s_null		((struct bkey_s)   { .k = NULL })
557 #define bkey_s_c_null		((struct bkey_s_c) { .k = NULL })
558 
559 #define bkey_s_err(err)		((struct bkey_s)   { .k = ERR_PTR(err) })
560 #define bkey_s_c_err(err)	((struct bkey_s_c) { .k = ERR_PTR(err) })
561 
562 static inline struct bkey_s bkey_to_s(struct bkey *k)
563 {
564 	return (struct bkey_s) { .k = k, .v = NULL };
565 }
566 
567 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
568 {
569 	return (struct bkey_s_c) { .k = k, .v = NULL };
570 }
571 
572 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
573 {
574 	return (struct bkey_s) { .k = &k->k, .v = &k->v };
575 }
576 
577 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
578 {
579 	return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
580 }
581 
582 /*
583  * For a given type of value (e.g. struct bch_extent), generates the types for
584  * bkey + bch_extent - inline, split, split const - and also all the conversion
585  * functions, which also check that the value is of the correct type.
586  *
587  * We use anonymous unions for upcasting - e.g. converting from e.g. a
588  * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
589  * functions.
590  */
591 #define x(name, ...)					\
592 struct bkey_i_##name {							\
593 	union {								\
594 		struct bkey		k;				\
595 		struct bkey_i		k_i;				\
596 	};								\
597 	struct bch_##name		v;				\
598 };									\
599 									\
600 struct bkey_s_c_##name {						\
601 	union {								\
602 	struct {							\
603 		const struct bkey	*k;				\
604 		const struct bch_##name	*v;				\
605 	};								\
606 	struct bkey_s_c			s_c;				\
607 	};								\
608 };									\
609 									\
610 struct bkey_s_##name {							\
611 	union {								\
612 	struct {							\
613 		struct bkey		*k;				\
614 		struct bch_##name	*v;				\
615 	};								\
616 	struct bkey_s_c_##name		c;				\
617 	struct bkey_s			s;				\
618 	struct bkey_s_c			s_c;				\
619 	};								\
620 };									\
621 									\
622 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k)	\
623 {									\
624 	EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name);	\
625 	return container_of(&k->k, struct bkey_i_##name, k);		\
626 }									\
627 									\
628 static inline const struct bkey_i_##name *				\
629 bkey_i_to_##name##_c(const struct bkey_i *k)				\
630 {									\
631 	EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name);	\
632 	return container_of(&k->k, struct bkey_i_##name, k);		\
633 }									\
634 									\
635 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k)	\
636 {									\
637 	EBUG_ON(!IS_ERR_OR_NULL(k.k) && k.k->type != KEY_TYPE_##name);	\
638 	return (struct bkey_s_##name) {					\
639 		.k = k.k,						\
640 		.v = container_of(k.v, struct bch_##name, v),		\
641 	};								\
642 }									\
643 									\
644 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
645 {									\
646 	EBUG_ON(!IS_ERR_OR_NULL(k.k) && k.k->type != KEY_TYPE_##name);	\
647 	return (struct bkey_s_c_##name) {				\
648 		.k = k.k,						\
649 		.v = container_of(k.v, struct bch_##name, v),		\
650 	};								\
651 }									\
652 									\
653 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
654 {									\
655 	return (struct bkey_s_##name) {					\
656 		.k = &k->k,						\
657 		.v = &k->v,						\
658 	};								\
659 }									\
660 									\
661 static inline struct bkey_s_c_##name					\
662 name##_i_to_s_c(const struct bkey_i_##name *k)				\
663 {									\
664 	return (struct bkey_s_c_##name) {				\
665 		.k = &k->k,						\
666 		.v = &k->v,						\
667 	};								\
668 }									\
669 									\
670 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k)	\
671 {									\
672 	EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name);	\
673 	return (struct bkey_s_##name) {					\
674 		.k = &k->k,						\
675 		.v = container_of(&k->v, struct bch_##name, v),		\
676 	};								\
677 }									\
678 									\
679 static inline struct bkey_s_c_##name					\
680 bkey_i_to_s_c_##name(const struct bkey_i *k)				\
681 {									\
682 	EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name);	\
683 	return (struct bkey_s_c_##name) {				\
684 		.k = &k->k,						\
685 		.v = container_of(&k->v, struct bch_##name, v),		\
686 	};								\
687 }									\
688 									\
689 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
690 {									\
691 	struct bkey_i_##name *k =					\
692 		container_of(&_k->k, struct bkey_i_##name, k);		\
693 									\
694 	bkey_init(&k->k);						\
695 	memset(&k->v, 0, sizeof(k->v));					\
696 	k->k.type = KEY_TYPE_##name;					\
697 	set_bkey_val_bytes(&k->k, sizeof(k->v));			\
698 									\
699 	return k;							\
700 }
701 
702 BCH_BKEY_TYPES();
703 #undef x
704 
705 /* byte order helpers */
706 
707 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
708 
709 static inline unsigned high_word_offset(const struct bkey_format *f)
710 {
711 	return f->key_u64s - 1;
712 }
713 
714 #define high_bit_offset		0
715 #define nth_word(p, n)		((p) - (n))
716 
717 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
718 
719 static inline unsigned high_word_offset(const struct bkey_format *f)
720 {
721 	return 0;
722 }
723 
724 #define high_bit_offset		KEY_PACKED_BITS_START
725 #define nth_word(p, n)		((p) + (n))
726 
727 #else
728 #error edit for your odd byteorder.
729 #endif
730 
731 #define high_word(f, k)		((u64 *) (k)->_data + high_word_offset(f))
732 #define next_word(p)		nth_word(p, 1)
733 #define prev_word(p)		nth_word(p, -1)
734 
735 #ifdef CONFIG_BCACHEFS_DEBUG
736 void bch2_bkey_pack_test(void);
737 #else
738 static inline void bch2_bkey_pack_test(void) {}
739 #endif
740 
741 #define bkey_fields()							\
742 	x(BKEY_FIELD_INODE,		p.inode)			\
743 	x(BKEY_FIELD_OFFSET,		p.offset)			\
744 	x(BKEY_FIELD_SNAPSHOT,		p.snapshot)			\
745 	x(BKEY_FIELD_SIZE,		size)				\
746 	x(BKEY_FIELD_VERSION_HI,	version.hi)			\
747 	x(BKEY_FIELD_VERSION_LO,	version.lo)
748 
749 struct bkey_format_state {
750 	u64 field_min[BKEY_NR_FIELDS];
751 	u64 field_max[BKEY_NR_FIELDS];
752 };
753 
754 void bch2_bkey_format_init(struct bkey_format_state *);
755 
756 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
757 {
758 	s->field_min[field] = min(s->field_min[field], v);
759 	s->field_max[field] = max(s->field_max[field], v);
760 }
761 
762 /*
763  * Changes @format so that @k can be successfully packed with @format
764  */
765 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
766 {
767 #define x(id, field) __bkey_format_add(s, id, k->field);
768 	bkey_fields()
769 #undef x
770 }
771 
772 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
773 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
774 int bch2_bkey_format_invalid(struct bch_fs *, struct bkey_format *,
775 			     enum bkey_invalid_flags, struct printbuf *);
776 void bch2_bkey_format_to_text(struct printbuf *, const struct bkey_format *);
777 
778 #endif /* _BCACHEFS_BKEY_H */
779