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