1 // SPDX-License-Identifier: GPL-2.0
6 #include "bkey_methods.h"
13 #define EBUG_ON(cond) BUG_ON(cond)
18 const struct bkey_format bch2_bkey_format_current = BKEY_FORMAT_CURRENT;
20 void bch2_bkey_packed_to_binary_text(struct printbuf *out,
21 const struct bkey_format *f,
22 const struct bkey_packed *k)
24 const u64 *p = high_word(f, k);
25 unsigned word_bits = 64 - high_bit_offset;
26 unsigned nr_key_bits = bkey_format_key_bits(f) + high_bit_offset;
27 u64 v = *p & (~0ULL >> high_bit_offset);
30 prt_str(out, "(empty)");
35 unsigned next_key_bits = nr_key_bits;
37 if (nr_key_bits < 64) {
38 v >>= 64 - nr_key_bits;
44 bch2_prt_u64_binary(out, v, min(word_bits, nr_key_bits));
54 nr_key_bits = next_key_bits;
58 #ifdef CONFIG_BCACHEFS_DEBUG
60 static void bch2_bkey_pack_verify(const struct bkey_packed *packed,
61 const struct bkey *unpacked,
62 const struct bkey_format *format)
66 BUG_ON(bkeyp_val_u64s(format, packed) !=
67 bkey_val_u64s(unpacked));
69 BUG_ON(packed->u64s < bkeyp_key_u64s(format, packed));
71 tmp = __bch2_bkey_unpack_key(format, packed);
73 if (memcmp(&tmp, unpacked, sizeof(struct bkey))) {
74 struct printbuf buf = PRINTBUF;
76 prt_printf(&buf, "keys differ: format u64s %u fields %u %u %u %u %u\n",
78 format->bits_per_field[0],
79 format->bits_per_field[1],
80 format->bits_per_field[2],
81 format->bits_per_field[3],
82 format->bits_per_field[4]);
84 prt_printf(&buf, "compiled unpack: ");
85 bch2_bkey_to_text(&buf, unpacked);
88 prt_printf(&buf, "c unpack: ");
89 bch2_bkey_to_text(&buf, &tmp);
92 prt_printf(&buf, "compiled unpack: ");
93 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
94 (struct bkey_packed *) unpacked);
97 prt_printf(&buf, "c unpack: ");
98 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
99 (struct bkey_packed *) &tmp);
102 panic("%s", buf.buf);
107 static inline void bch2_bkey_pack_verify(const struct bkey_packed *packed,
108 const struct bkey *unpacked,
109 const struct bkey_format *format) {}
113 const struct bkey_format *format;
114 unsigned bits; /* bits remaining in current word */
115 u64 w; /* current word */
116 u64 *p; /* pointer to next word */
120 static struct pack_state pack_state_init(const struct bkey_format *format,
121 struct bkey_packed *k)
123 u64 *p = high_word(format, k);
125 return (struct pack_state) {
127 .bits = 64 - high_bit_offset,
134 static void pack_state_finish(struct pack_state *state,
135 struct bkey_packed *k)
137 EBUG_ON(state->p < k->_data);
138 EBUG_ON(state->p >= k->_data + state->format->key_u64s);
140 *state->p = state->w;
143 struct unpack_state {
144 const struct bkey_format *format;
145 unsigned bits; /* bits remaining in current word */
146 u64 w; /* current word */
147 const u64 *p; /* pointer to next word */
151 static struct unpack_state unpack_state_init(const struct bkey_format *format,
152 const struct bkey_packed *k)
154 const u64 *p = high_word(format, k);
156 return (struct unpack_state) {
158 .bits = 64 - high_bit_offset,
159 .w = *p << high_bit_offset,
165 static u64 get_inc_field(struct unpack_state *state, unsigned field)
167 unsigned bits = state->format->bits_per_field[field];
168 u64 v = 0, offset = le64_to_cpu(state->format->field_offset[field]);
170 if (bits >= state->bits) {
171 v = state->w >> (64 - bits);
174 state->p = next_word(state->p);
175 state->w = *state->p;
179 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
180 v |= (state->w >> 1) >> (63 - bits);
188 static bool set_inc_field(struct pack_state *state, unsigned field, u64 v)
190 unsigned bits = state->format->bits_per_field[field];
191 u64 offset = le64_to_cpu(state->format->field_offset[field]);
201 if (bits > state->bits) {
203 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
204 state->w |= (v >> 1) >> (bits - 1);
206 *state->p = state->w;
207 state->p = next_word(state->p);
213 state->w |= v << state->bits;
219 * Note: does NOT set out->format (we don't know what it should be here!)
221 * Also: doesn't work on extents - it doesn't preserve the invariant that
222 * if k is packed bkey_start_pos(k) will successfully pack
224 static bool bch2_bkey_transform_key(const struct bkey_format *out_f,
225 struct bkey_packed *out,
226 const struct bkey_format *in_f,
227 const struct bkey_packed *in)
229 struct pack_state out_s = pack_state_init(out_f, out);
230 struct unpack_state in_s = unpack_state_init(in_f, in);
236 for (i = 0; i < BKEY_NR_FIELDS; i++)
237 if (!set_inc_field(&out_s, i, get_inc_field(&in_s, i)))
240 /* Can't happen because the val would be too big to unpack: */
241 EBUG_ON(in->u64s - in_f->key_u64s + out_f->key_u64s > U8_MAX);
243 pack_state_finish(&out_s, out);
244 out->u64s = out_f->key_u64s + in->u64s - in_f->key_u64s;
245 out->needs_whiteout = in->needs_whiteout;
246 out->type = in->type;
251 bool bch2_bkey_transform(const struct bkey_format *out_f,
252 struct bkey_packed *out,
253 const struct bkey_format *in_f,
254 const struct bkey_packed *in)
256 if (!bch2_bkey_transform_key(out_f, out, in_f, in))
259 memcpy_u64s((u64 *) out + out_f->key_u64s,
260 (u64 *) in + in_f->key_u64s,
261 (in->u64s - in_f->key_u64s));
265 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *format,
266 const struct bkey_packed *in)
268 struct unpack_state state = unpack_state_init(format, in);
271 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
272 EBUG_ON(in->u64s < format->key_u64s);
273 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
274 EBUG_ON(in->u64s - format->key_u64s + BKEY_U64s > U8_MAX);
276 out.u64s = BKEY_U64s + in->u64s - format->key_u64s;
277 out.format = KEY_FORMAT_CURRENT;
278 out.needs_whiteout = in->needs_whiteout;
282 #define x(id, field) out.field = get_inc_field(&state, id);
289 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
290 struct bpos __bkey_unpack_pos(const struct bkey_format *format,
291 const struct bkey_packed *in)
293 struct unpack_state state = unpack_state_init(format, in);
296 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
297 EBUG_ON(in->u64s < format->key_u64s);
298 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
300 out.inode = get_inc_field(&state, BKEY_FIELD_INODE);
301 out.offset = get_inc_field(&state, BKEY_FIELD_OFFSET);
302 out.snapshot = get_inc_field(&state, BKEY_FIELD_SNAPSHOT);
309 * bch2_bkey_pack_key -- pack just the key, not the value
311 bool bch2_bkey_pack_key(struct bkey_packed *out, const struct bkey *in,
312 const struct bkey_format *format)
314 struct pack_state state = pack_state_init(format, out);
317 EBUG_ON((void *) in == (void *) out);
318 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
319 EBUG_ON(in->format != KEY_FORMAT_CURRENT);
323 #define x(id, field) if (!set_inc_field(&state, id, in->field)) return false;
326 pack_state_finish(&state, out);
327 out->u64s = format->key_u64s + in->u64s - BKEY_U64s;
328 out->format = KEY_FORMAT_LOCAL_BTREE;
329 out->needs_whiteout = in->needs_whiteout;
330 out->type = in->type;
332 bch2_bkey_pack_verify(out, in, format);
337 * bch2_bkey_unpack -- unpack the key and the value
339 void bch2_bkey_unpack(const struct btree *b, struct bkey_i *dst,
340 const struct bkey_packed *src)
342 __bkey_unpack_key(b, &dst->k, src);
345 bkeyp_val(&b->format, src),
346 bkeyp_val_u64s(&b->format, src));
350 * bch2_bkey_pack -- pack the key and the value
352 bool bch2_bkey_pack(struct bkey_packed *out, const struct bkey_i *in,
353 const struct bkey_format *format)
355 struct bkey_packed tmp;
357 if (!bch2_bkey_pack_key(&tmp, &in->k, format))
360 memmove_u64s((u64 *) out + format->key_u64s,
362 bkey_val_u64s(&in->k));
363 memcpy_u64s(out, &tmp, format->key_u64s);
369 static bool set_inc_field_lossy(struct pack_state *state, unsigned field, u64 v)
371 unsigned bits = state->format->bits_per_field[field];
372 u64 offset = le64_to_cpu(state->format->field_offset[field]);
378 if (fls64(v) > bits) {
379 v = ~(~0ULL << bits);
383 if (bits > state->bits) {
385 state->w |= (v >> 1) >> (bits - 1);
387 *state->p = state->w;
388 state->p = next_word(state->p);
394 state->w |= v << state->bits;
399 #ifdef CONFIG_BCACHEFS_DEBUG
400 static bool bkey_packed_successor(struct bkey_packed *out,
401 const struct btree *b,
402 struct bkey_packed k)
404 const struct bkey_format *f = &b->format;
405 unsigned nr_key_bits = b->nr_key_bits;
406 unsigned first_bit, offset;
409 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(f));
416 first_bit = high_bit_offset + nr_key_bits - 1;
417 p = nth_word(high_word(f, out), first_bit >> 6);
418 offset = 63 - (first_bit & 63);
420 while (nr_key_bits) {
421 unsigned bits = min(64 - offset, nr_key_bits);
422 u64 mask = (~0ULL >> (64 - bits)) << offset;
424 if ((*p & mask) != mask) {
425 *p += 1ULL << offset;
426 EBUG_ON(bch2_bkey_cmp_packed(b, out, &k) <= 0);
441 * Returns a packed key that compares <= in
443 * This is used in bset_search_tree(), where we need a packed pos in order to be
444 * able to compare against the keys in the auxiliary search tree - and it's
445 * legal to use a packed pos that isn't equivalent to the original pos,
446 * _provided_ it compares <= to the original pos.
448 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *out,
450 const struct btree *b)
452 const struct bkey_format *f = &b->format;
453 struct pack_state state = pack_state_init(f, out);
455 #ifdef CONFIG_BCACHEFS_DEBUG
456 struct bpos orig = in;
462 * bch2_bkey_pack_key() will write to all of f->key_u64s, minus the 3
463 * byte header, but pack_pos() won't if the len/version fields are big
464 * enough - we need to make sure to zero them out:
466 for (i = 0; i < f->key_u64s; i++)
469 if (unlikely(in.snapshot <
470 le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]))) {
473 return BKEY_PACK_POS_FAIL;
474 in.snapshot = KEY_SNAPSHOT_MAX;
478 if (unlikely(in.offset <
479 le64_to_cpu(f->field_offset[BKEY_FIELD_OFFSET]))) {
481 return BKEY_PACK_POS_FAIL;
482 in.offset = KEY_OFFSET_MAX;
483 in.snapshot = KEY_SNAPSHOT_MAX;
487 if (unlikely(in.inode <
488 le64_to_cpu(f->field_offset[BKEY_FIELD_INODE])))
489 return BKEY_PACK_POS_FAIL;
491 if (unlikely(!set_inc_field_lossy(&state, BKEY_FIELD_INODE, in.inode))) {
492 in.offset = KEY_OFFSET_MAX;
493 in.snapshot = KEY_SNAPSHOT_MAX;
497 if (unlikely(!set_inc_field_lossy(&state, BKEY_FIELD_OFFSET, in.offset))) {
498 in.snapshot = KEY_SNAPSHOT_MAX;
502 if (unlikely(!set_inc_field_lossy(&state, BKEY_FIELD_SNAPSHOT, in.snapshot)))
505 pack_state_finish(&state, out);
506 out->u64s = f->key_u64s;
507 out->format = KEY_FORMAT_LOCAL_BTREE;
508 out->type = KEY_TYPE_deleted;
510 #ifdef CONFIG_BCACHEFS_DEBUG
512 BUG_ON(bkey_cmp_left_packed(b, out, &orig));
514 struct bkey_packed successor;
516 BUG_ON(bkey_cmp_left_packed(b, out, &orig) >= 0);
517 BUG_ON(bkey_packed_successor(&successor, b, *out) &&
518 bkey_cmp_left_packed(b, &successor, &orig) < 0);
522 return exact ? BKEY_PACK_POS_EXACT : BKEY_PACK_POS_SMALLER;
525 void bch2_bkey_format_init(struct bkey_format_state *s)
529 for (i = 0; i < ARRAY_SIZE(s->field_min); i++)
530 s->field_min[i] = U64_MAX;
532 for (i = 0; i < ARRAY_SIZE(s->field_max); i++)
535 /* Make sure we can store a size of 0: */
536 s->field_min[BKEY_FIELD_SIZE] = 0;
539 void bch2_bkey_format_add_pos(struct bkey_format_state *s, struct bpos p)
543 __bkey_format_add(s, field++, p.inode);
544 __bkey_format_add(s, field++, p.offset);
545 __bkey_format_add(s, field++, p.snapshot);
549 * We don't want it to be possible for the packed format to represent fields
550 * bigger than a u64... that will cause confusion and issues (like with
551 * bkey_packed_successor())
553 static void set_format_field(struct bkey_format *f, enum bch_bkey_fields i,
554 unsigned bits, u64 offset)
556 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
557 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
559 bits = min(bits, unpacked_bits);
561 offset = bits == unpacked_bits ? 0 : min(offset, unpacked_max - ((1ULL << bits) - 1));
563 f->bits_per_field[i] = bits;
564 f->field_offset[i] = cpu_to_le64(offset);
567 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s)
569 unsigned i, bits = KEY_PACKED_BITS_START;
570 struct bkey_format ret = {
571 .nr_fields = BKEY_NR_FIELDS,
574 for (i = 0; i < ARRAY_SIZE(s->field_min); i++) {
575 s->field_min[i] = min(s->field_min[i], s->field_max[i]);
577 set_format_field(&ret, i,
578 fls64(s->field_max[i] - s->field_min[i]),
581 bits += ret.bits_per_field[i];
584 /* allow for extent merging: */
585 if (ret.bits_per_field[BKEY_FIELD_SIZE]) {
586 ret.bits_per_field[BKEY_FIELD_SIZE] += 4;
590 ret.key_u64s = DIV_ROUND_UP(bits, 64);
592 /* if we have enough spare bits, round fields up to nearest byte */
593 bits = ret.key_u64s * 64 - bits;
595 for (i = 0; i < ARRAY_SIZE(ret.bits_per_field); i++) {
596 unsigned r = round_up(ret.bits_per_field[i], 8) -
597 ret.bits_per_field[i];
600 set_format_field(&ret, i,
601 ret.bits_per_field[i] + r,
602 le64_to_cpu(ret.field_offset[i]));
607 EBUG_ON(bch2_bkey_format_validate(&ret));
611 const char *bch2_bkey_format_validate(struct bkey_format *f)
613 unsigned i, bits = KEY_PACKED_BITS_START;
615 if (f->nr_fields != BKEY_NR_FIELDS)
616 return "incorrect number of fields";
619 * Verify that the packed format can't represent fields larger than the
622 for (i = 0; i < f->nr_fields; i++) {
623 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
624 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
625 u64 packed_max = f->bits_per_field[i]
626 ? ~((~0ULL << 1) << (f->bits_per_field[i] - 1))
628 u64 field_offset = le64_to_cpu(f->field_offset[i]);
630 if (packed_max + field_offset < packed_max ||
631 packed_max + field_offset > unpacked_max)
632 return "field too large";
634 bits += f->bits_per_field[i];
637 if (f->key_u64s != DIV_ROUND_UP(bits, 64))
638 return "incorrect key_u64s";
644 * Most significant differing bit
645 * Bits are indexed from 0 - return is [0, nr_key_bits)
648 unsigned bch2_bkey_greatest_differing_bit(const struct btree *b,
649 const struct bkey_packed *l_k,
650 const struct bkey_packed *r_k)
652 const u64 *l = high_word(&b->format, l_k);
653 const u64 *r = high_word(&b->format, r_k);
654 unsigned nr_key_bits = b->nr_key_bits;
655 unsigned word_bits = 64 - high_bit_offset;
658 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
660 /* for big endian, skip past header */
661 l_v = *l & (~0ULL >> high_bit_offset);
662 r_v = *r & (~0ULL >> high_bit_offset);
664 while (nr_key_bits) {
665 if (nr_key_bits < word_bits) {
666 l_v >>= word_bits - nr_key_bits;
667 r_v >>= word_bits - nr_key_bits;
670 nr_key_bits -= word_bits;
674 return fls64(l_v ^ r_v) - 1 + nr_key_bits;
689 * Bits are indexed from 0 - return is [0, nr_key_bits)
692 unsigned bch2_bkey_ffs(const struct btree *b, const struct bkey_packed *k)
694 const u64 *p = high_word(&b->format, k);
695 unsigned nr_key_bits = b->nr_key_bits;
696 unsigned ret = 0, offset;
698 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
700 offset = nr_key_bits;
701 while (offset > 64) {
706 offset = 64 - offset;
708 while (nr_key_bits) {
709 unsigned bits = nr_key_bits + offset < 64
713 u64 mask = (~0ULL >> (64 - bits)) << offset;
716 return ret + __ffs64(*p & mask) - offset;
729 #define I(_x) (*(out)++ = (_x))
731 #define I2(i0, i1) (I1(i0), I(i1))
732 #define I3(i0, i1, i2) (I2(i0, i1), I(i2))
733 #define I4(i0, i1, i2, i3) (I3(i0, i1, i2), I(i3))
734 #define I5(i0, i1, i2, i3, i4) (I4(i0, i1, i2, i3), I(i4))
736 static u8 *compile_bkey_field(const struct bkey_format *format, u8 *out,
737 enum bch_bkey_fields field,
738 unsigned dst_offset, unsigned dst_size,
741 unsigned bits = format->bits_per_field[field];
742 u64 offset = le64_to_cpu(format->field_offset[field]);
743 unsigned i, byte, bit_offset, align, shl, shr;
745 if (!bits && !offset) {
756 /* just return offset: */
760 if (offset > S32_MAX) {
761 /* mov [rdi + dst_offset], offset */
762 I3(0xc7, 0x47, dst_offset);
763 memcpy(out, &offset, 4);
766 I3(0xc7, 0x47, dst_offset + 4);
767 memcpy(out, (void *) &offset + 4, 4);
770 /* mov [rdi + dst_offset], offset */
772 I4(0x48, 0xc7, 0x47, dst_offset);
773 memcpy(out, &offset, 4);
778 /* mov [rdi + dst_offset], offset */
779 I3(0xc7, 0x47, dst_offset);
780 memcpy(out, &offset, 4);
790 bit_offset = format->key_u64s * 64;
791 for (i = 0; i <= field; i++)
792 bit_offset -= format->bits_per_field[i];
794 byte = bit_offset / 8;
795 bit_offset -= byte * 8;
799 if (bit_offset == 0 && bits == 8) {
800 /* movzx eax, BYTE PTR [rsi + imm8] */
801 I4(0x0f, 0xb6, 0x46, byte);
802 } else if (bit_offset == 0 && bits == 16) {
803 /* movzx eax, WORD PTR [rsi + imm8] */
804 I4(0x0f, 0xb7, 0x46, byte);
805 } else if (bit_offset + bits <= 32) {
806 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
808 bit_offset += align * 8;
810 BUG_ON(bit_offset + bits > 32);
812 /* mov eax, [rsi + imm8] */
813 I3(0x8b, 0x46, byte);
817 I3(0xc1, 0xe8, bit_offset);
820 if (bit_offset + bits < 32) {
821 unsigned mask = ~0U >> (32 - bits);
825 memcpy(out, &mask, 4);
828 } else if (bit_offset + bits <= 64) {
829 align = min(8 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 7);
831 bit_offset += align * 8;
833 BUG_ON(bit_offset + bits > 64);
835 /* mov rax, [rsi + imm8] */
836 I4(0x48, 0x8b, 0x46, byte);
838 shl = 64 - bit_offset - bits;
839 shr = bit_offset + shl;
843 I4(0x48, 0xc1, 0xe0, shl);
848 I4(0x48, 0xc1, 0xe8, shr);
851 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
853 bit_offset += align * 8;
855 BUG_ON(bit_offset + bits > 96);
857 /* mov rax, [rsi + byte] */
858 I4(0x48, 0x8b, 0x46, byte);
860 /* mov edx, [rsi + byte + 8] */
861 I3(0x8b, 0x56, byte + 8);
863 /* bits from next word: */
864 shr = bit_offset + bits - 64;
865 BUG_ON(shr > bit_offset);
867 /* shr rax, bit_offset */
868 I4(0x48, 0xc1, 0xe8, shr);
871 I4(0x48, 0xc1, 0xe2, 64 - shr);
874 I3(0x48, 0x09, 0xd0);
876 shr = bit_offset - shr;
880 I4(0x48, 0xc1, 0xe8, shr);
885 if (offset > S32_MAX) {
888 memcpy(out, &offset, 8);
891 I3(0x48, 0x01, 0xd0);
892 } else if (offset + (~0ULL >> (64 - bits)) > U32_MAX) {
895 memcpy(out, &offset, 4);
900 memcpy(out, &offset, 4);
906 /* mov [rdi + dst_offset], rax */
907 I4(0x48, 0x89, 0x47, dst_offset);
910 /* mov [rdi + dst_offset], eax */
911 I3(0x89, 0x47, dst_offset);
920 int bch2_compile_bkey_format(const struct bkey_format *format, void *_out)
922 bool eax_zeroed = false;
926 * rdi: dst - unpacked key
927 * rsi: src - packed key
930 /* k->u64s, k->format, k->type */
935 /* add eax, BKEY_U64s - format->key_u64s */
936 I5(0x05, BKEY_U64s - format->key_u64s, KEY_FORMAT_CURRENT, 0, 0);
938 /* and eax, imm32: mask out k->pad: */
939 I5(0x25, 0xff, 0xff, 0xff, 0);
944 #define x(id, field) \
945 out = compile_bkey_field(format, out, id, \
946 offsetof(struct bkey, field), \
947 sizeof(((struct bkey *) NULL)->field), \
955 return (void *) out - _out;
962 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *l,
963 const struct bkey_packed *r,
964 const struct btree *b)
966 return __bch2_bkey_cmp_packed_format_checked_inlined(l, r, b);
970 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *b,
971 const struct bkey_packed *l,
972 const struct bpos *r)
974 return bpos_cmp(bkey_unpack_pos_format_checked(b, l), *r);
978 int bch2_bkey_cmp_packed(const struct btree *b,
979 const struct bkey_packed *l,
980 const struct bkey_packed *r)
982 return bch2_bkey_cmp_packed_inlined(b, l, r);
986 int __bch2_bkey_cmp_left_packed(const struct btree *b,
987 const struct bkey_packed *l,
988 const struct bpos *r)
990 const struct bkey *l_unpacked;
992 return unlikely(l_unpacked = packed_to_bkey_c(l))
993 ? bpos_cmp(l_unpacked->p, *r)
994 : __bch2_bkey_cmp_left_packed_format_checked(b, l, r);
997 void bch2_bpos_swab(struct bpos *p)
1000 u8 *h = ((u8 *) &p[1]) - 1;
1009 void bch2_bkey_swab_key(const struct bkey_format *_f, struct bkey_packed *k)
1011 const struct bkey_format *f = bkey_packed(k) ? _f : &bch2_bkey_format_current;
1012 u8 *l = k->key_start;
1013 u8 *h = (u8 *) (k->_data + f->key_u64s) - 1;
1022 #ifdef CONFIG_BCACHEFS_DEBUG
1023 void bch2_bkey_pack_test(void)
1025 struct bkey t = KEY(4134ULL, 1250629070527416633ULL, 0);
1026 struct bkey_packed p;
1028 struct bkey_format test_format = {
1030 .nr_fields = BKEY_NR_FIELDS,
1038 struct unpack_state in_s =
1039 unpack_state_init(&bch2_bkey_format_current, (void *) &t);
1040 struct pack_state out_s = pack_state_init(&test_format, &p);
1043 for (i = 0; i < out_s.format->nr_fields; i++) {
1044 u64 a, v = get_inc_field(&in_s, i);
1047 #define x(id, field) case id: a = t.field; break;
1055 panic("got %llu actual %llu i %u\n", v, a, i);
1057 if (!set_inc_field(&out_s, i, v))
1058 panic("failed at %u\n", i);
1061 BUG_ON(!bch2_bkey_pack_key(&p, &t, &test_format));