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 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
21 const struct bkey_packed *);
23 void bch2_bkey_packed_to_binary_text(struct printbuf *out,
24 const struct bkey_format *f,
25 const struct bkey_packed *k)
27 const u64 *p = high_word(f, k);
28 unsigned word_bits = 64 - high_bit_offset;
29 unsigned nr_key_bits = bkey_format_key_bits(f) + high_bit_offset;
30 u64 v = *p & (~0ULL >> high_bit_offset);
33 prt_str(out, "(empty)");
38 unsigned next_key_bits = nr_key_bits;
40 if (nr_key_bits < 64) {
41 v >>= 64 - nr_key_bits;
47 bch2_prt_u64_binary(out, v, min(word_bits, nr_key_bits));
57 nr_key_bits = next_key_bits;
61 #ifdef CONFIG_BCACHEFS_DEBUG
63 static void bch2_bkey_pack_verify(const struct bkey_packed *packed,
64 const struct bkey *unpacked,
65 const struct bkey_format *format)
69 BUG_ON(bkeyp_val_u64s(format, packed) !=
70 bkey_val_u64s(unpacked));
72 BUG_ON(packed->u64s < bkeyp_key_u64s(format, packed));
74 tmp = __bch2_bkey_unpack_key(format, packed);
76 if (memcmp(&tmp, unpacked, sizeof(struct bkey))) {
77 struct printbuf buf = PRINTBUF;
79 prt_printf(&buf, "keys differ: format u64s %u fields %u %u %u %u %u\n",
81 format->bits_per_field[0],
82 format->bits_per_field[1],
83 format->bits_per_field[2],
84 format->bits_per_field[3],
85 format->bits_per_field[4]);
87 prt_printf(&buf, "compiled unpack: ");
88 bch2_bkey_to_text(&buf, unpacked);
91 prt_printf(&buf, "c unpack: ");
92 bch2_bkey_to_text(&buf, &tmp);
95 prt_printf(&buf, "compiled unpack: ");
96 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
97 (struct bkey_packed *) unpacked);
100 prt_printf(&buf, "c unpack: ");
101 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
102 (struct bkey_packed *) &tmp);
105 panic("%s", buf.buf);
110 static inline void bch2_bkey_pack_verify(const struct bkey_packed *packed,
111 const struct bkey *unpacked,
112 const struct bkey_format *format) {}
116 const struct bkey_format *format;
117 unsigned bits; /* bits remaining in current word */
118 u64 w; /* current word */
119 u64 *p; /* pointer to next word */
123 static struct pack_state pack_state_init(const struct bkey_format *format,
124 struct bkey_packed *k)
126 u64 *p = high_word(format, k);
128 return (struct pack_state) {
130 .bits = 64 - high_bit_offset,
137 static void pack_state_finish(struct pack_state *state,
138 struct bkey_packed *k)
140 EBUG_ON(state->p < k->_data);
141 EBUG_ON(state->p >= k->_data + state->format->key_u64s);
143 *state->p = state->w;
146 struct unpack_state {
147 const struct bkey_format *format;
148 unsigned bits; /* bits remaining in current word */
149 u64 w; /* current word */
150 const u64 *p; /* pointer to next word */
154 static struct unpack_state unpack_state_init(const struct bkey_format *format,
155 const struct bkey_packed *k)
157 const u64 *p = high_word(format, k);
159 return (struct unpack_state) {
161 .bits = 64 - high_bit_offset,
162 .w = *p << high_bit_offset,
168 static u64 get_inc_field(struct unpack_state *state, unsigned field)
170 unsigned bits = state->format->bits_per_field[field];
171 u64 v = 0, offset = le64_to_cpu(state->format->field_offset[field]);
173 if (bits >= state->bits) {
174 v = state->w >> (64 - bits);
177 state->p = next_word(state->p);
178 state->w = *state->p;
182 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
183 v |= (state->w >> 1) >> (63 - bits);
191 static bool set_inc_field(struct pack_state *state, unsigned field, u64 v)
193 unsigned bits = state->format->bits_per_field[field];
194 u64 offset = le64_to_cpu(state->format->field_offset[field]);
204 if (bits > state->bits) {
206 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
207 state->w |= (v >> 1) >> (bits - 1);
209 *state->p = state->w;
210 state->p = next_word(state->p);
216 state->w |= v << state->bits;
222 * Note: does NOT set out->format (we don't know what it should be here!)
224 * Also: doesn't work on extents - it doesn't preserve the invariant that
225 * if k is packed bkey_start_pos(k) will successfully pack
227 static bool bch2_bkey_transform_key(const struct bkey_format *out_f,
228 struct bkey_packed *out,
229 const struct bkey_format *in_f,
230 const struct bkey_packed *in)
232 struct pack_state out_s = pack_state_init(out_f, out);
233 struct unpack_state in_s = unpack_state_init(in_f, in);
239 for (i = 0; i < BKEY_NR_FIELDS; i++)
240 if (!set_inc_field(&out_s, i, get_inc_field(&in_s, i)))
243 /* Can't happen because the val would be too big to unpack: */
244 EBUG_ON(in->u64s - in_f->key_u64s + out_f->key_u64s > U8_MAX);
246 pack_state_finish(&out_s, out);
247 out->u64s = out_f->key_u64s + in->u64s - in_f->key_u64s;
248 out->needs_whiteout = in->needs_whiteout;
249 out->type = in->type;
254 bool bch2_bkey_transform(const struct bkey_format *out_f,
255 struct bkey_packed *out,
256 const struct bkey_format *in_f,
257 const struct bkey_packed *in)
259 if (!bch2_bkey_transform_key(out_f, out, in_f, in))
262 memcpy_u64s((u64 *) out + out_f->key_u64s,
263 (u64 *) in + in_f->key_u64s,
264 (in->u64s - in_f->key_u64s));
268 #define bkey_fields() \
269 x(BKEY_FIELD_INODE, p.inode) \
270 x(BKEY_FIELD_OFFSET, p.offset) \
271 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
272 x(BKEY_FIELD_SIZE, size) \
273 x(BKEY_FIELD_VERSION_HI, version.hi) \
274 x(BKEY_FIELD_VERSION_LO, version.lo)
276 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *format,
277 const struct bkey_packed *in)
279 struct unpack_state state = unpack_state_init(format, in);
282 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
283 EBUG_ON(in->u64s < format->key_u64s);
284 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
285 EBUG_ON(in->u64s - format->key_u64s + BKEY_U64s > U8_MAX);
287 out.u64s = BKEY_U64s + in->u64s - format->key_u64s;
288 out.format = KEY_FORMAT_CURRENT;
289 out.needs_whiteout = in->needs_whiteout;
293 #define x(id, field) out.field = get_inc_field(&state, id);
300 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
301 struct bpos __bkey_unpack_pos(const struct bkey_format *format,
302 const struct bkey_packed *in)
304 struct unpack_state state = unpack_state_init(format, in);
307 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
308 EBUG_ON(in->u64s < format->key_u64s);
309 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
311 out.inode = get_inc_field(&state, BKEY_FIELD_INODE);
312 out.offset = get_inc_field(&state, BKEY_FIELD_OFFSET);
313 out.snapshot = get_inc_field(&state, BKEY_FIELD_SNAPSHOT);
320 * bch2_bkey_pack_key -- pack just the key, not the value
322 bool bch2_bkey_pack_key(struct bkey_packed *out, const struct bkey *in,
323 const struct bkey_format *format)
325 struct pack_state state = pack_state_init(format, out);
328 EBUG_ON((void *) in == (void *) out);
329 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
330 EBUG_ON(in->format != KEY_FORMAT_CURRENT);
334 #define x(id, field) if (!set_inc_field(&state, id, in->field)) return false;
339 * Extents - we have to guarantee that if an extent is packed, a trimmed
340 * version will also pack:
342 if (bkey_start_offset(in) <
343 le64_to_cpu(format->field_offset[BKEY_FIELD_OFFSET]))
346 pack_state_finish(&state, out);
347 out->u64s = format->key_u64s + in->u64s - BKEY_U64s;
348 out->format = KEY_FORMAT_LOCAL_BTREE;
349 out->needs_whiteout = in->needs_whiteout;
350 out->type = in->type;
352 bch2_bkey_pack_verify(out, in, format);
357 * bch2_bkey_unpack -- unpack the key and the value
359 void bch2_bkey_unpack(const struct btree *b, struct bkey_i *dst,
360 const struct bkey_packed *src)
362 __bkey_unpack_key(b, &dst->k, src);
365 bkeyp_val(&b->format, src),
366 bkeyp_val_u64s(&b->format, src));
370 * bch2_bkey_pack -- pack the key and the value
372 bool bch2_bkey_pack(struct bkey_packed *out, const struct bkey_i *in,
373 const struct bkey_format *format)
375 struct bkey_packed tmp;
377 if (!bch2_bkey_pack_key(&tmp, &in->k, format))
380 memmove_u64s((u64 *) out + format->key_u64s,
382 bkey_val_u64s(&in->k));
383 memcpy_u64s(out, &tmp, format->key_u64s);
389 static bool set_inc_field_lossy(struct pack_state *state, unsigned field, u64 v)
391 unsigned bits = state->format->bits_per_field[field];
392 u64 offset = le64_to_cpu(state->format->field_offset[field]);
398 if (fls64(v) > bits) {
399 v = ~(~0ULL << bits);
403 if (bits > state->bits) {
405 state->w |= (v >> 1) >> (bits - 1);
407 *state->p = state->w;
408 state->p = next_word(state->p);
414 state->w |= v << state->bits;
419 #ifdef CONFIG_BCACHEFS_DEBUG
420 static bool bkey_packed_successor(struct bkey_packed *out,
421 const struct btree *b,
422 struct bkey_packed k)
424 const struct bkey_format *f = &b->format;
425 unsigned nr_key_bits = b->nr_key_bits;
426 unsigned first_bit, offset;
429 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(f));
436 first_bit = high_bit_offset + nr_key_bits - 1;
437 p = nth_word(high_word(f, out), first_bit >> 6);
438 offset = 63 - (first_bit & 63);
440 while (nr_key_bits) {
441 unsigned bits = min(64 - offset, nr_key_bits);
442 u64 mask = (~0ULL >> (64 - bits)) << offset;
444 if ((*p & mask) != mask) {
445 *p += 1ULL << offset;
446 EBUG_ON(bch2_bkey_cmp_packed(b, out, &k) <= 0);
461 * Returns a packed key that compares <= in
463 * This is used in bset_search_tree(), where we need a packed pos in order to be
464 * able to compare against the keys in the auxiliary search tree - and it's
465 * legal to use a packed pos that isn't equivalent to the original pos,
466 * _provided_ it compares <= to the original pos.
468 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *out,
470 const struct btree *b)
472 const struct bkey_format *f = &b->format;
473 struct pack_state state = pack_state_init(f, out);
475 #ifdef CONFIG_BCACHEFS_DEBUG
476 struct bpos orig = in;
482 * bch2_bkey_pack_key() will write to all of f->key_u64s, minus the 3
483 * byte header, but pack_pos() won't if the len/version fields are big
484 * enough - we need to make sure to zero them out:
486 for (i = 0; i < f->key_u64s; i++)
489 if (unlikely(in.snapshot <
490 le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]))) {
493 return BKEY_PACK_POS_FAIL;
494 in.snapshot = KEY_SNAPSHOT_MAX;
498 if (unlikely(in.offset <
499 le64_to_cpu(f->field_offset[BKEY_FIELD_OFFSET]))) {
501 return BKEY_PACK_POS_FAIL;
502 in.offset = KEY_OFFSET_MAX;
503 in.snapshot = KEY_SNAPSHOT_MAX;
507 if (unlikely(in.inode <
508 le64_to_cpu(f->field_offset[BKEY_FIELD_INODE])))
509 return BKEY_PACK_POS_FAIL;
511 if (!set_inc_field_lossy(&state, BKEY_FIELD_INODE, in.inode)) {
512 in.offset = KEY_OFFSET_MAX;
513 in.snapshot = KEY_SNAPSHOT_MAX;
517 if (!set_inc_field_lossy(&state, BKEY_FIELD_OFFSET, in.offset)) {
518 in.snapshot = KEY_SNAPSHOT_MAX;
522 if (!set_inc_field_lossy(&state, BKEY_FIELD_SNAPSHOT, in.snapshot))
525 pack_state_finish(&state, out);
526 out->u64s = f->key_u64s;
527 out->format = KEY_FORMAT_LOCAL_BTREE;
528 out->type = KEY_TYPE_deleted;
530 #ifdef CONFIG_BCACHEFS_DEBUG
532 BUG_ON(bkey_cmp_left_packed(b, out, &orig));
534 struct bkey_packed successor;
536 BUG_ON(bkey_cmp_left_packed(b, out, &orig) >= 0);
537 BUG_ON(bkey_packed_successor(&successor, b, *out) &&
538 bkey_cmp_left_packed(b, &successor, &orig) < 0);
542 return exact ? BKEY_PACK_POS_EXACT : BKEY_PACK_POS_SMALLER;
545 void bch2_bkey_format_init(struct bkey_format_state *s)
549 for (i = 0; i < ARRAY_SIZE(s->field_min); i++)
550 s->field_min[i] = U64_MAX;
552 for (i = 0; i < ARRAY_SIZE(s->field_max); i++)
555 /* Make sure we can store a size of 0: */
556 s->field_min[BKEY_FIELD_SIZE] = 0;
559 static void __bkey_format_add(struct bkey_format_state *s,
560 unsigned field, u64 v)
562 s->field_min[field] = min(s->field_min[field], v);
563 s->field_max[field] = max(s->field_max[field], v);
567 * Changes @format so that @k can be successfully packed with @format
569 void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
571 #define x(id, field) __bkey_format_add(s, id, k->field);
574 __bkey_format_add(s, BKEY_FIELD_OFFSET, bkey_start_offset(k));
577 void bch2_bkey_format_add_pos(struct bkey_format_state *s, struct bpos p)
581 __bkey_format_add(s, field++, p.inode);
582 __bkey_format_add(s, field++, p.offset);
583 __bkey_format_add(s, field++, p.snapshot);
587 * We don't want it to be possible for the packed format to represent fields
588 * bigger than a u64... that will cause confusion and issues (like with
589 * bkey_packed_successor())
591 static void set_format_field(struct bkey_format *f, enum bch_bkey_fields i,
592 unsigned bits, u64 offset)
594 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
595 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
597 bits = min(bits, unpacked_bits);
599 offset = bits == unpacked_bits ? 0 : min(offset, unpacked_max - ((1ULL << bits) - 1));
601 f->bits_per_field[i] = bits;
602 f->field_offset[i] = cpu_to_le64(offset);
605 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s)
607 unsigned i, bits = KEY_PACKED_BITS_START;
608 struct bkey_format ret = {
609 .nr_fields = BKEY_NR_FIELDS,
612 for (i = 0; i < ARRAY_SIZE(s->field_min); i++) {
613 s->field_min[i] = min(s->field_min[i], s->field_max[i]);
615 set_format_field(&ret, i,
616 fls64(s->field_max[i] - s->field_min[i]),
619 bits += ret.bits_per_field[i];
622 /* allow for extent merging: */
623 if (ret.bits_per_field[BKEY_FIELD_SIZE]) {
624 ret.bits_per_field[BKEY_FIELD_SIZE] += 4;
628 ret.key_u64s = DIV_ROUND_UP(bits, 64);
630 /* if we have enough spare bits, round fields up to nearest byte */
631 bits = ret.key_u64s * 64 - bits;
633 for (i = 0; i < ARRAY_SIZE(ret.bits_per_field); i++) {
634 unsigned r = round_up(ret.bits_per_field[i], 8) -
635 ret.bits_per_field[i];
638 set_format_field(&ret, i,
639 ret.bits_per_field[i] + r,
640 le64_to_cpu(ret.field_offset[i]));
645 EBUG_ON(bch2_bkey_format_validate(&ret));
649 const char *bch2_bkey_format_validate(struct bkey_format *f)
651 unsigned i, bits = KEY_PACKED_BITS_START;
653 if (f->nr_fields != BKEY_NR_FIELDS)
654 return "incorrect number of fields";
657 * Verify that the packed format can't represent fields larger than the
660 for (i = 0; i < f->nr_fields; i++) {
661 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
662 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
663 u64 packed_max = f->bits_per_field[i]
664 ? ~((~0ULL << 1) << (f->bits_per_field[i] - 1))
666 u64 field_offset = le64_to_cpu(f->field_offset[i]);
668 if (packed_max + field_offset < packed_max ||
669 packed_max + field_offset > unpacked_max)
670 return "field too large";
672 bits += f->bits_per_field[i];
675 if (f->key_u64s != DIV_ROUND_UP(bits, 64))
676 return "incorrect key_u64s";
682 * Most significant differing bit
683 * Bits are indexed from 0 - return is [0, nr_key_bits)
686 unsigned bch2_bkey_greatest_differing_bit(const struct btree *b,
687 const struct bkey_packed *l_k,
688 const struct bkey_packed *r_k)
690 const u64 *l = high_word(&b->format, l_k);
691 const u64 *r = high_word(&b->format, r_k);
692 unsigned nr_key_bits = b->nr_key_bits;
693 unsigned word_bits = 64 - high_bit_offset;
696 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
698 /* for big endian, skip past header */
699 l_v = *l & (~0ULL >> high_bit_offset);
700 r_v = *r & (~0ULL >> high_bit_offset);
702 while (nr_key_bits) {
703 if (nr_key_bits < word_bits) {
704 l_v >>= word_bits - nr_key_bits;
705 r_v >>= word_bits - nr_key_bits;
708 nr_key_bits -= word_bits;
712 return fls64(l_v ^ r_v) - 1 + nr_key_bits;
727 * Bits are indexed from 0 - return is [0, nr_key_bits)
730 unsigned bch2_bkey_ffs(const struct btree *b, const struct bkey_packed *k)
732 const u64 *p = high_word(&b->format, k);
733 unsigned nr_key_bits = b->nr_key_bits;
734 unsigned ret = 0, offset;
736 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
738 offset = nr_key_bits;
739 while (offset > 64) {
744 offset = 64 - offset;
746 while (nr_key_bits) {
747 unsigned bits = nr_key_bits + offset < 64
751 u64 mask = (~0ULL >> (64 - bits)) << offset;
754 return ret + __ffs64(*p & mask) - offset;
767 #define I(_x) (*(out)++ = (_x))
769 #define I2(i0, i1) (I1(i0), I(i1))
770 #define I3(i0, i1, i2) (I2(i0, i1), I(i2))
771 #define I4(i0, i1, i2, i3) (I3(i0, i1, i2), I(i3))
772 #define I5(i0, i1, i2, i3, i4) (I4(i0, i1, i2, i3), I(i4))
774 static u8 *compile_bkey_field(const struct bkey_format *format, u8 *out,
775 enum bch_bkey_fields field,
776 unsigned dst_offset, unsigned dst_size,
779 unsigned bits = format->bits_per_field[field];
780 u64 offset = le64_to_cpu(format->field_offset[field]);
781 unsigned i, byte, bit_offset, align, shl, shr;
783 if (!bits && !offset) {
794 /* just return offset: */
798 if (offset > S32_MAX) {
799 /* mov [rdi + dst_offset], offset */
800 I3(0xc7, 0x47, dst_offset);
801 memcpy(out, &offset, 4);
804 I3(0xc7, 0x47, dst_offset + 4);
805 memcpy(out, (void *) &offset + 4, 4);
808 /* mov [rdi + dst_offset], offset */
810 I4(0x48, 0xc7, 0x47, dst_offset);
811 memcpy(out, &offset, 4);
816 /* mov [rdi + dst_offset], offset */
817 I3(0xc7, 0x47, dst_offset);
818 memcpy(out, &offset, 4);
828 bit_offset = format->key_u64s * 64;
829 for (i = 0; i <= field; i++)
830 bit_offset -= format->bits_per_field[i];
832 byte = bit_offset / 8;
833 bit_offset -= byte * 8;
837 if (bit_offset == 0 && bits == 8) {
838 /* movzx eax, BYTE PTR [rsi + imm8] */
839 I4(0x0f, 0xb6, 0x46, byte);
840 } else if (bit_offset == 0 && bits == 16) {
841 /* movzx eax, WORD PTR [rsi + imm8] */
842 I4(0x0f, 0xb7, 0x46, byte);
843 } else if (bit_offset + bits <= 32) {
844 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
846 bit_offset += align * 8;
848 BUG_ON(bit_offset + bits > 32);
850 /* mov eax, [rsi + imm8] */
851 I3(0x8b, 0x46, byte);
855 I3(0xc1, 0xe8, bit_offset);
858 if (bit_offset + bits < 32) {
859 unsigned mask = ~0U >> (32 - bits);
863 memcpy(out, &mask, 4);
866 } else if (bit_offset + bits <= 64) {
867 align = min(8 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 7);
869 bit_offset += align * 8;
871 BUG_ON(bit_offset + bits > 64);
873 /* mov rax, [rsi + imm8] */
874 I4(0x48, 0x8b, 0x46, byte);
876 shl = 64 - bit_offset - bits;
877 shr = bit_offset + shl;
881 I4(0x48, 0xc1, 0xe0, shl);
886 I4(0x48, 0xc1, 0xe8, shr);
889 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
891 bit_offset += align * 8;
893 BUG_ON(bit_offset + bits > 96);
895 /* mov rax, [rsi + byte] */
896 I4(0x48, 0x8b, 0x46, byte);
898 /* mov edx, [rsi + byte + 8] */
899 I3(0x8b, 0x56, byte + 8);
901 /* bits from next word: */
902 shr = bit_offset + bits - 64;
903 BUG_ON(shr > bit_offset);
905 /* shr rax, bit_offset */
906 I4(0x48, 0xc1, 0xe8, shr);
909 I4(0x48, 0xc1, 0xe2, 64 - shr);
912 I3(0x48, 0x09, 0xd0);
914 shr = bit_offset - shr;
918 I4(0x48, 0xc1, 0xe8, shr);
923 if (offset > S32_MAX) {
926 memcpy(out, &offset, 8);
929 I3(0x48, 0x01, 0xd0);
930 } else if (offset + (~0ULL >> (64 - bits)) > U32_MAX) {
933 memcpy(out, &offset, 4);
938 memcpy(out, &offset, 4);
944 /* mov [rdi + dst_offset], rax */
945 I4(0x48, 0x89, 0x47, dst_offset);
948 /* mov [rdi + dst_offset], eax */
949 I3(0x89, 0x47, dst_offset);
958 int bch2_compile_bkey_format(const struct bkey_format *format, void *_out)
960 bool eax_zeroed = false;
964 * rdi: dst - unpacked key
965 * rsi: src - packed key
968 /* k->u64s, k->format, k->type */
973 /* add eax, BKEY_U64s - format->key_u64s */
974 I5(0x05, BKEY_U64s - format->key_u64s, KEY_FORMAT_CURRENT, 0, 0);
976 /* and eax, imm32: mask out k->pad: */
977 I5(0x25, 0xff, 0xff, 0xff, 0);
982 #define x(id, field) \
983 out = compile_bkey_field(format, out, id, \
984 offsetof(struct bkey, field), \
985 sizeof(((struct bkey *) NULL)->field), \
993 return (void *) out - _out;
1000 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *l,
1001 const struct bkey_packed *r,
1002 const struct btree *b)
1004 return __bch2_bkey_cmp_packed_format_checked_inlined(l, r, b);
1008 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *b,
1009 const struct bkey_packed *l,
1010 const struct bpos *r)
1012 return bpos_cmp(bkey_unpack_pos_format_checked(b, l), *r);
1016 int bch2_bkey_cmp_packed(const struct btree *b,
1017 const struct bkey_packed *l,
1018 const struct bkey_packed *r)
1020 return bch2_bkey_cmp_packed_inlined(b, l, r);
1024 int __bch2_bkey_cmp_left_packed(const struct btree *b,
1025 const struct bkey_packed *l,
1026 const struct bpos *r)
1028 const struct bkey *l_unpacked;
1030 return unlikely(l_unpacked = packed_to_bkey_c(l))
1031 ? bpos_cmp(l_unpacked->p, *r)
1032 : __bch2_bkey_cmp_left_packed_format_checked(b, l, r);
1035 void bch2_bpos_swab(struct bpos *p)
1038 u8 *h = ((u8 *) &p[1]) - 1;
1047 void bch2_bkey_swab_key(const struct bkey_format *_f, struct bkey_packed *k)
1049 const struct bkey_format *f = bkey_packed(k) ? _f : &bch2_bkey_format_current;
1050 u8 *l = k->key_start;
1051 u8 *h = (u8 *) (k->_data + f->key_u64s) - 1;
1060 #ifdef CONFIG_BCACHEFS_DEBUG
1061 void bch2_bkey_pack_test(void)
1063 struct bkey t = KEY(4134ULL, 1250629070527416633ULL, 0);
1064 struct bkey_packed p;
1066 struct bkey_format test_format = {
1068 .nr_fields = BKEY_NR_FIELDS,
1076 struct unpack_state in_s =
1077 unpack_state_init(&bch2_bkey_format_current, (void *) &t);
1078 struct pack_state out_s = pack_state_init(&test_format, &p);
1081 for (i = 0; i < out_s.format->nr_fields; i++) {
1082 u64 a, v = get_inc_field(&in_s, i);
1085 #define x(id, field) case id: a = t.field; break;
1093 panic("got %llu actual %llu i %u\n", v, a, i);
1095 if (!set_inc_field(&out_s, i, v))
1096 panic("failed at %u\n", i);
1099 BUG_ON(!bch2_bkey_pack_key(&p, &t, &test_format));