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