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