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