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 #define bkey_fields() \
266 x(BKEY_FIELD_INODE, p.inode) \
267 x(BKEY_FIELD_OFFSET, p.offset) \
268 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
269 x(BKEY_FIELD_SIZE, size) \
270 x(BKEY_FIELD_VERSION_HI, version.hi) \
271 x(BKEY_FIELD_VERSION_LO, version.lo)
273 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *format,
274 const struct bkey_packed *in)
276 struct unpack_state state = unpack_state_init(format, in);
279 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
280 EBUG_ON(in->u64s < format->key_u64s);
281 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
282 EBUG_ON(in->u64s - format->key_u64s + BKEY_U64s > U8_MAX);
284 out.u64s = BKEY_U64s + in->u64s - format->key_u64s;
285 out.format = KEY_FORMAT_CURRENT;
286 out.needs_whiteout = in->needs_whiteout;
290 #define x(id, field) out.field = get_inc_field(&state, id);
297 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
298 struct bpos __bkey_unpack_pos(const struct bkey_format *format,
299 const struct bkey_packed *in)
301 struct unpack_state state = unpack_state_init(format, in);
304 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
305 EBUG_ON(in->u64s < format->key_u64s);
306 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
308 out.inode = get_inc_field(&state, BKEY_FIELD_INODE);
309 out.offset = get_inc_field(&state, BKEY_FIELD_OFFSET);
310 out.snapshot = get_inc_field(&state, BKEY_FIELD_SNAPSHOT);
317 * bch2_bkey_pack_key -- pack just the key, not the value
319 bool bch2_bkey_pack_key(struct bkey_packed *out, const struct bkey *in,
320 const struct bkey_format *format)
322 struct pack_state state = pack_state_init(format, out);
325 EBUG_ON((void *) in == (void *) out);
326 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
327 EBUG_ON(in->format != KEY_FORMAT_CURRENT);
331 #define x(id, field) if (!set_inc_field(&state, id, in->field)) return false;
336 * Extents - we have to guarantee that if an extent is packed, a trimmed
337 * version will also pack:
339 if (bkey_start_offset(in) <
340 le64_to_cpu(format->field_offset[BKEY_FIELD_OFFSET]))
343 pack_state_finish(&state, out);
344 out->u64s = format->key_u64s + in->u64s - BKEY_U64s;
345 out->format = KEY_FORMAT_LOCAL_BTREE;
346 out->needs_whiteout = in->needs_whiteout;
347 out->type = in->type;
349 bch2_bkey_pack_verify(out, in, format);
354 * bch2_bkey_unpack -- unpack the key and the value
356 void bch2_bkey_unpack(const struct btree *b, struct bkey_i *dst,
357 const struct bkey_packed *src)
359 __bkey_unpack_key(b, &dst->k, src);
362 bkeyp_val(&b->format, src),
363 bkeyp_val_u64s(&b->format, src));
367 * bch2_bkey_pack -- pack the key and the value
369 bool bch2_bkey_pack(struct bkey_packed *out, const struct bkey_i *in,
370 const struct bkey_format *format)
372 struct bkey_packed tmp;
374 if (!bch2_bkey_pack_key(&tmp, &in->k, format))
377 memmove_u64s((u64 *) out + format->key_u64s,
379 bkey_val_u64s(&in->k));
380 memcpy_u64s(out, &tmp, format->key_u64s);
386 static bool set_inc_field_lossy(struct pack_state *state, unsigned field, u64 v)
388 unsigned bits = state->format->bits_per_field[field];
389 u64 offset = le64_to_cpu(state->format->field_offset[field]);
395 if (fls64(v) > bits) {
396 v = ~(~0ULL << bits);
400 if (bits > state->bits) {
402 state->w |= (v >> 1) >> (bits - 1);
404 *state->p = state->w;
405 state->p = next_word(state->p);
411 state->w |= v << state->bits;
416 #ifdef CONFIG_BCACHEFS_DEBUG
417 static bool bkey_packed_successor(struct bkey_packed *out,
418 const struct btree *b,
419 struct bkey_packed k)
421 const struct bkey_format *f = &b->format;
422 unsigned nr_key_bits = b->nr_key_bits;
423 unsigned first_bit, offset;
426 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(f));
433 first_bit = high_bit_offset + nr_key_bits - 1;
434 p = nth_word(high_word(f, out), first_bit >> 6);
435 offset = 63 - (first_bit & 63);
437 while (nr_key_bits) {
438 unsigned bits = min(64 - offset, nr_key_bits);
439 u64 mask = (~0ULL >> (64 - bits)) << offset;
441 if ((*p & mask) != mask) {
442 *p += 1ULL << offset;
443 EBUG_ON(bch2_bkey_cmp_packed(b, out, &k) <= 0);
458 * Returns a packed key that compares <= in
460 * This is used in bset_search_tree(), where we need a packed pos in order to be
461 * able to compare against the keys in the auxiliary search tree - and it's
462 * legal to use a packed pos that isn't equivalent to the original pos,
463 * _provided_ it compares <= to the original pos.
465 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *out,
467 const struct btree *b)
469 const struct bkey_format *f = &b->format;
470 struct pack_state state = pack_state_init(f, out);
472 #ifdef CONFIG_BCACHEFS_DEBUG
473 struct bpos orig = in;
479 * bch2_bkey_pack_key() will write to all of f->key_u64s, minus the 3
480 * byte header, but pack_pos() won't if the len/version fields are big
481 * enough - we need to make sure to zero them out:
483 for (i = 0; i < f->key_u64s; i++)
486 if (unlikely(in.snapshot <
487 le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]))) {
490 return BKEY_PACK_POS_FAIL;
491 in.snapshot = KEY_SNAPSHOT_MAX;
495 if (unlikely(in.offset <
496 le64_to_cpu(f->field_offset[BKEY_FIELD_OFFSET]))) {
498 return BKEY_PACK_POS_FAIL;
499 in.offset = KEY_OFFSET_MAX;
500 in.snapshot = KEY_SNAPSHOT_MAX;
504 if (unlikely(in.inode <
505 le64_to_cpu(f->field_offset[BKEY_FIELD_INODE])))
506 return BKEY_PACK_POS_FAIL;
508 if (!set_inc_field_lossy(&state, BKEY_FIELD_INODE, in.inode)) {
509 in.offset = KEY_OFFSET_MAX;
510 in.snapshot = KEY_SNAPSHOT_MAX;
514 if (!set_inc_field_lossy(&state, BKEY_FIELD_OFFSET, in.offset)) {
515 in.snapshot = KEY_SNAPSHOT_MAX;
519 if (!set_inc_field_lossy(&state, BKEY_FIELD_SNAPSHOT, in.snapshot))
522 pack_state_finish(&state, out);
523 out->u64s = f->key_u64s;
524 out->format = KEY_FORMAT_LOCAL_BTREE;
525 out->type = KEY_TYPE_deleted;
527 #ifdef CONFIG_BCACHEFS_DEBUG
529 BUG_ON(bkey_cmp_left_packed(b, out, &orig));
531 struct bkey_packed successor;
533 BUG_ON(bkey_cmp_left_packed(b, out, &orig) >= 0);
534 BUG_ON(bkey_packed_successor(&successor, b, *out) &&
535 bkey_cmp_left_packed(b, &successor, &orig) < 0);
539 return exact ? BKEY_PACK_POS_EXACT : BKEY_PACK_POS_SMALLER;
542 void bch2_bkey_format_init(struct bkey_format_state *s)
546 for (i = 0; i < ARRAY_SIZE(s->field_min); i++)
547 s->field_min[i] = U64_MAX;
549 for (i = 0; i < ARRAY_SIZE(s->field_max); i++)
552 /* Make sure we can store a size of 0: */
553 s->field_min[BKEY_FIELD_SIZE] = 0;
556 static void __bkey_format_add(struct bkey_format_state *s,
557 unsigned field, u64 v)
559 s->field_min[field] = min(s->field_min[field], v);
560 s->field_max[field] = max(s->field_max[field], v);
564 * Changes @format so that @k can be successfully packed with @format
566 void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
568 #define x(id, field) __bkey_format_add(s, id, k->field);
571 __bkey_format_add(s, BKEY_FIELD_OFFSET, bkey_start_offset(k));
574 void bch2_bkey_format_add_pos(struct bkey_format_state *s, struct bpos p)
578 __bkey_format_add(s, field++, p.inode);
579 __bkey_format_add(s, field++, p.offset);
580 __bkey_format_add(s, field++, p.snapshot);
584 * We don't want it to be possible for the packed format to represent fields
585 * bigger than a u64... that will cause confusion and issues (like with
586 * bkey_packed_successor())
588 static void set_format_field(struct bkey_format *f, enum bch_bkey_fields i,
589 unsigned bits, u64 offset)
591 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
592 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
594 bits = min(bits, unpacked_bits);
596 offset = bits == unpacked_bits ? 0 : min(offset, unpacked_max - ((1ULL << bits) - 1));
598 f->bits_per_field[i] = bits;
599 f->field_offset[i] = cpu_to_le64(offset);
602 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s)
604 unsigned i, bits = KEY_PACKED_BITS_START;
605 struct bkey_format ret = {
606 .nr_fields = BKEY_NR_FIELDS,
609 for (i = 0; i < ARRAY_SIZE(s->field_min); i++) {
610 s->field_min[i] = min(s->field_min[i], s->field_max[i]);
612 set_format_field(&ret, i,
613 fls64(s->field_max[i] - s->field_min[i]),
616 bits += ret.bits_per_field[i];
619 /* allow for extent merging: */
620 if (ret.bits_per_field[BKEY_FIELD_SIZE]) {
621 ret.bits_per_field[BKEY_FIELD_SIZE] += 4;
625 ret.key_u64s = DIV_ROUND_UP(bits, 64);
627 /* if we have enough spare bits, round fields up to nearest byte */
628 bits = ret.key_u64s * 64 - bits;
630 for (i = 0; i < ARRAY_SIZE(ret.bits_per_field); i++) {
631 unsigned r = round_up(ret.bits_per_field[i], 8) -
632 ret.bits_per_field[i];
635 set_format_field(&ret, i,
636 ret.bits_per_field[i] + r,
637 le64_to_cpu(ret.field_offset[i]));
642 EBUG_ON(bch2_bkey_format_validate(&ret));
646 const char *bch2_bkey_format_validate(struct bkey_format *f)
648 unsigned i, bits = KEY_PACKED_BITS_START;
650 if (f->nr_fields != BKEY_NR_FIELDS)
651 return "incorrect number of fields";
654 * Verify that the packed format can't represent fields larger than the
657 for (i = 0; i < f->nr_fields; i++) {
658 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
659 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
660 u64 packed_max = f->bits_per_field[i]
661 ? ~((~0ULL << 1) << (f->bits_per_field[i] - 1))
663 u64 field_offset = le64_to_cpu(f->field_offset[i]);
665 if (packed_max + field_offset < packed_max ||
666 packed_max + field_offset > unpacked_max)
667 return "field too large";
669 bits += f->bits_per_field[i];
672 if (f->key_u64s != DIV_ROUND_UP(bits, 64))
673 return "incorrect key_u64s";
679 * Most significant differing bit
680 * Bits are indexed from 0 - return is [0, nr_key_bits)
683 unsigned bch2_bkey_greatest_differing_bit(const struct btree *b,
684 const struct bkey_packed *l_k,
685 const struct bkey_packed *r_k)
687 const u64 *l = high_word(&b->format, l_k);
688 const u64 *r = high_word(&b->format, r_k);
689 unsigned nr_key_bits = b->nr_key_bits;
690 unsigned word_bits = 64 - high_bit_offset;
693 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
695 /* for big endian, skip past header */
696 l_v = *l & (~0ULL >> high_bit_offset);
697 r_v = *r & (~0ULL >> high_bit_offset);
699 while (nr_key_bits) {
700 if (nr_key_bits < word_bits) {
701 l_v >>= word_bits - nr_key_bits;
702 r_v >>= word_bits - nr_key_bits;
705 nr_key_bits -= word_bits;
709 return fls64(l_v ^ r_v) - 1 + nr_key_bits;
724 * Bits are indexed from 0 - return is [0, nr_key_bits)
727 unsigned bch2_bkey_ffs(const struct btree *b, const struct bkey_packed *k)
729 const u64 *p = high_word(&b->format, k);
730 unsigned nr_key_bits = b->nr_key_bits;
731 unsigned ret = 0, offset;
733 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
735 offset = nr_key_bits;
736 while (offset > 64) {
741 offset = 64 - offset;
743 while (nr_key_bits) {
744 unsigned bits = nr_key_bits + offset < 64
748 u64 mask = (~0ULL >> (64 - bits)) << offset;
751 return ret + __ffs64(*p & mask) - offset;
764 #define I(_x) (*(out)++ = (_x))
766 #define I2(i0, i1) (I1(i0), I(i1))
767 #define I3(i0, i1, i2) (I2(i0, i1), I(i2))
768 #define I4(i0, i1, i2, i3) (I3(i0, i1, i2), I(i3))
769 #define I5(i0, i1, i2, i3, i4) (I4(i0, i1, i2, i3), I(i4))
771 static u8 *compile_bkey_field(const struct bkey_format *format, u8 *out,
772 enum bch_bkey_fields field,
773 unsigned dst_offset, unsigned dst_size,
776 unsigned bits = format->bits_per_field[field];
777 u64 offset = le64_to_cpu(format->field_offset[field]);
778 unsigned i, byte, bit_offset, align, shl, shr;
780 if (!bits && !offset) {
791 /* just return offset: */
795 if (offset > S32_MAX) {
796 /* mov [rdi + dst_offset], offset */
797 I3(0xc7, 0x47, dst_offset);
798 memcpy(out, &offset, 4);
801 I3(0xc7, 0x47, dst_offset + 4);
802 memcpy(out, (void *) &offset + 4, 4);
805 /* mov [rdi + dst_offset], offset */
807 I4(0x48, 0xc7, 0x47, dst_offset);
808 memcpy(out, &offset, 4);
813 /* mov [rdi + dst_offset], offset */
814 I3(0xc7, 0x47, dst_offset);
815 memcpy(out, &offset, 4);
825 bit_offset = format->key_u64s * 64;
826 for (i = 0; i <= field; i++)
827 bit_offset -= format->bits_per_field[i];
829 byte = bit_offset / 8;
830 bit_offset -= byte * 8;
834 if (bit_offset == 0 && bits == 8) {
835 /* movzx eax, BYTE PTR [rsi + imm8] */
836 I4(0x0f, 0xb6, 0x46, byte);
837 } else if (bit_offset == 0 && bits == 16) {
838 /* movzx eax, WORD PTR [rsi + imm8] */
839 I4(0x0f, 0xb7, 0x46, byte);
840 } else if (bit_offset + bits <= 32) {
841 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
843 bit_offset += align * 8;
845 BUG_ON(bit_offset + bits > 32);
847 /* mov eax, [rsi + imm8] */
848 I3(0x8b, 0x46, byte);
852 I3(0xc1, 0xe8, bit_offset);
855 if (bit_offset + bits < 32) {
856 unsigned mask = ~0U >> (32 - bits);
860 memcpy(out, &mask, 4);
863 } else if (bit_offset + bits <= 64) {
864 align = min(8 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 7);
866 bit_offset += align * 8;
868 BUG_ON(bit_offset + bits > 64);
870 /* mov rax, [rsi + imm8] */
871 I4(0x48, 0x8b, 0x46, byte);
873 shl = 64 - bit_offset - bits;
874 shr = bit_offset + shl;
878 I4(0x48, 0xc1, 0xe0, shl);
883 I4(0x48, 0xc1, 0xe8, shr);
886 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
888 bit_offset += align * 8;
890 BUG_ON(bit_offset + bits > 96);
892 /* mov rax, [rsi + byte] */
893 I4(0x48, 0x8b, 0x46, byte);
895 /* mov edx, [rsi + byte + 8] */
896 I3(0x8b, 0x56, byte + 8);
898 /* bits from next word: */
899 shr = bit_offset + bits - 64;
900 BUG_ON(shr > bit_offset);
902 /* shr rax, bit_offset */
903 I4(0x48, 0xc1, 0xe8, shr);
906 I4(0x48, 0xc1, 0xe2, 64 - shr);
909 I3(0x48, 0x09, 0xd0);
911 shr = bit_offset - shr;
915 I4(0x48, 0xc1, 0xe8, shr);
920 if (offset > S32_MAX) {
923 memcpy(out, &offset, 8);
926 I3(0x48, 0x01, 0xd0);
927 } else if (offset + (~0ULL >> (64 - bits)) > U32_MAX) {
930 memcpy(out, &offset, 4);
935 memcpy(out, &offset, 4);
941 /* mov [rdi + dst_offset], rax */
942 I4(0x48, 0x89, 0x47, dst_offset);
945 /* mov [rdi + dst_offset], eax */
946 I3(0x89, 0x47, dst_offset);
955 int bch2_compile_bkey_format(const struct bkey_format *format, void *_out)
957 bool eax_zeroed = false;
961 * rdi: dst - unpacked key
962 * rsi: src - packed key
965 /* k->u64s, k->format, k->type */
970 /* add eax, BKEY_U64s - format->key_u64s */
971 I5(0x05, BKEY_U64s - format->key_u64s, KEY_FORMAT_CURRENT, 0, 0);
973 /* and eax, imm32: mask out k->pad: */
974 I5(0x25, 0xff, 0xff, 0xff, 0);
979 #define x(id, field) \
980 out = compile_bkey_field(format, out, id, \
981 offsetof(struct bkey, field), \
982 sizeof(((struct bkey *) NULL)->field), \
990 return (void *) out - _out;
997 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *l,
998 const struct bkey_packed *r,
999 const struct btree *b)
1001 return __bch2_bkey_cmp_packed_format_checked_inlined(l, r, b);
1005 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *b,
1006 const struct bkey_packed *l,
1007 const struct bpos *r)
1009 return bpos_cmp(bkey_unpack_pos_format_checked(b, l), *r);
1013 int bch2_bkey_cmp_packed(const struct btree *b,
1014 const struct bkey_packed *l,
1015 const struct bkey_packed *r)
1017 return bch2_bkey_cmp_packed_inlined(b, l, r);
1021 int __bch2_bkey_cmp_left_packed(const struct btree *b,
1022 const struct bkey_packed *l,
1023 const struct bpos *r)
1025 const struct bkey *l_unpacked;
1027 return unlikely(l_unpacked = packed_to_bkey_c(l))
1028 ? bpos_cmp(l_unpacked->p, *r)
1029 : __bch2_bkey_cmp_left_packed_format_checked(b, l, r);
1032 void bch2_bpos_swab(struct bpos *p)
1035 u8 *h = ((u8 *) &p[1]) - 1;
1044 void bch2_bkey_swab_key(const struct bkey_format *_f, struct bkey_packed *k)
1046 const struct bkey_format *f = bkey_packed(k) ? _f : &bch2_bkey_format_current;
1047 u8 *l = k->key_start;
1048 u8 *h = (u8 *) (k->_data + f->key_u64s) - 1;
1057 #ifdef CONFIG_BCACHEFS_DEBUG
1058 void bch2_bkey_pack_test(void)
1060 struct bkey t = KEY(4134ULL, 1250629070527416633ULL, 0);
1061 struct bkey_packed p;
1063 struct bkey_format test_format = {
1065 .nr_fields = BKEY_NR_FIELDS,
1073 struct unpack_state in_s =
1074 unpack_state_init(&bch2_bkey_format_current, (void *) &t);
1075 struct pack_state out_s = pack_state_init(&test_format, &p);
1078 for (i = 0; i < out_s.format->nr_fields; i++) {
1079 u64 a, v = get_inc_field(&in_s, i);
1082 #define x(id, field) case id: a = t.field; break;
1090 panic("got %llu actual %llu i %u\n", v, a, i);
1092 if (!set_inc_field(&out_s, i, v))
1093 panic("failed at %u\n", i);
1096 BUG_ON(!bch2_bkey_pack_key(&p, &t, &test_format));