1 // SPDX-License-Identifier: GPL-2.0
5 #include "bkey_methods.h"
12 #define EBUG_ON(cond) BUG_ON(cond)
17 const struct bkey_format bch2_bkey_format_current = BKEY_FORMAT_CURRENT;
19 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
20 const struct bkey_packed *);
22 void bch2_bkey_packed_to_binary_text(struct printbuf *out,
23 const struct bkey_format *f,
24 const struct bkey_packed *k)
26 const u64 *p = high_word(f, k);
27 unsigned word_bits = 64 - high_bit_offset;
28 unsigned nr_key_bits = bkey_format_key_bits(f) + high_bit_offset;
29 u64 v = *p & (~0ULL >> high_bit_offset);
32 prt_str(out, "(empty)");
37 unsigned next_key_bits = nr_key_bits;
39 if (nr_key_bits < 64) {
40 v >>= 64 - nr_key_bits;
46 bch2_prt_u64_binary(out, v, min(word_bits, nr_key_bits));
56 nr_key_bits = next_key_bits;
60 #ifdef CONFIG_BCACHEFS_DEBUG
62 static void bch2_bkey_pack_verify(const struct bkey_packed *packed,
63 const struct bkey *unpacked,
64 const struct bkey_format *format)
68 BUG_ON(bkeyp_val_u64s(format, packed) !=
69 bkey_val_u64s(unpacked));
71 BUG_ON(packed->u64s < bkeyp_key_u64s(format, packed));
73 tmp = __bch2_bkey_unpack_key(format, packed);
75 if (memcmp(&tmp, unpacked, sizeof(struct bkey))) {
76 struct printbuf buf = PRINTBUF;
78 prt_printf(&buf, "keys differ: format u64s %u fields %u %u %u %u %u\n",
80 format->bits_per_field[0],
81 format->bits_per_field[1],
82 format->bits_per_field[2],
83 format->bits_per_field[3],
84 format->bits_per_field[4]);
86 prt_printf(&buf, "compiled unpack: ");
87 bch2_bkey_to_text(&buf, unpacked);
90 prt_printf(&buf, "c unpack: ");
91 bch2_bkey_to_text(&buf, &tmp);
94 prt_printf(&buf, "compiled unpack: ");
95 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
96 (struct bkey_packed *) unpacked);
99 prt_printf(&buf, "c unpack: ");
100 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
101 (struct bkey_packed *) &tmp);
104 panic("%s", buf.buf);
109 static inline void bch2_bkey_pack_verify(const struct bkey_packed *packed,
110 const struct bkey *unpacked,
111 const struct bkey_format *format) {}
115 const struct bkey_format *format;
116 unsigned bits; /* bits remaining in current word */
117 u64 w; /* current word */
118 u64 *p; /* pointer to next word */
122 static struct pack_state pack_state_init(const struct bkey_format *format,
123 struct bkey_packed *k)
125 u64 *p = high_word(format, k);
127 return (struct pack_state) {
129 .bits = 64 - high_bit_offset,
136 static void pack_state_finish(struct pack_state *state,
137 struct bkey_packed *k)
139 EBUG_ON(state->p < k->_data);
140 EBUG_ON(state->p >= k->_data + state->format->key_u64s);
142 *state->p = state->w;
145 struct unpack_state {
146 const struct bkey_format *format;
147 unsigned bits; /* bits remaining in current word */
148 u64 w; /* current word */
149 const u64 *p; /* pointer to next word */
153 static struct unpack_state unpack_state_init(const struct bkey_format *format,
154 const struct bkey_packed *k)
156 const u64 *p = high_word(format, k);
158 return (struct unpack_state) {
160 .bits = 64 - high_bit_offset,
161 .w = *p << high_bit_offset,
167 static u64 get_inc_field(struct unpack_state *state, unsigned field)
169 unsigned bits = state->format->bits_per_field[field];
170 u64 v = 0, offset = le64_to_cpu(state->format->field_offset[field]);
172 if (bits >= state->bits) {
173 v = state->w >> (64 - bits);
176 state->p = next_word(state->p);
177 state->w = *state->p;
181 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
182 v |= (state->w >> 1) >> (63 - bits);
190 static bool set_inc_field(struct pack_state *state, unsigned field, u64 v)
192 unsigned bits = state->format->bits_per_field[field];
193 u64 offset = le64_to_cpu(state->format->field_offset[field]);
203 if (bits > state->bits) {
205 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
206 state->w |= (v >> 1) >> (bits - 1);
208 *state->p = state->w;
209 state->p = next_word(state->p);
215 state->w |= v << state->bits;
221 * Note: does NOT set out->format (we don't know what it should be here!)
223 * Also: doesn't work on extents - it doesn't preserve the invariant that
224 * if k is packed bkey_start_pos(k) will successfully pack
226 static bool bch2_bkey_transform_key(const struct bkey_format *out_f,
227 struct bkey_packed *out,
228 const struct bkey_format *in_f,
229 const struct bkey_packed *in)
231 struct pack_state out_s = pack_state_init(out_f, out);
232 struct unpack_state in_s = unpack_state_init(in_f, in);
238 for (i = 0; i < BKEY_NR_FIELDS; i++)
239 if (!set_inc_field(&out_s, i, get_inc_field(&in_s, i)))
242 /* Can't happen because the val would be too big to unpack: */
243 EBUG_ON(in->u64s - in_f->key_u64s + out_f->key_u64s > U8_MAX);
245 pack_state_finish(&out_s, out);
246 out->u64s = out_f->key_u64s + in->u64s - in_f->key_u64s;
247 out->needs_whiteout = in->needs_whiteout;
248 out->type = in->type;
253 bool bch2_bkey_transform(const struct bkey_format *out_f,
254 struct bkey_packed *out,
255 const struct bkey_format *in_f,
256 const struct bkey_packed *in)
258 if (!bch2_bkey_transform_key(out_f, out, in_f, in))
261 memcpy_u64s((u64 *) out + out_f->key_u64s,
262 (u64 *) in + in_f->key_u64s,
263 (in->u64s - in_f->key_u64s));
267 #define bkey_fields() \
268 x(BKEY_FIELD_INODE, p.inode) \
269 x(BKEY_FIELD_OFFSET, p.offset) \
270 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
271 x(BKEY_FIELD_SIZE, size) \
272 x(BKEY_FIELD_VERSION_HI, version.hi) \
273 x(BKEY_FIELD_VERSION_LO, version.lo)
275 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *format,
276 const struct bkey_packed *in)
278 struct unpack_state state = unpack_state_init(format, in);
281 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
282 EBUG_ON(in->u64s < format->key_u64s);
283 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
284 EBUG_ON(in->u64s - format->key_u64s + BKEY_U64s > U8_MAX);
286 out.u64s = BKEY_U64s + in->u64s - format->key_u64s;
287 out.format = KEY_FORMAT_CURRENT;
288 out.needs_whiteout = in->needs_whiteout;
292 #define x(id, field) out.field = get_inc_field(&state, id);
299 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
300 struct bpos __bkey_unpack_pos(const struct bkey_format *format,
301 const struct bkey_packed *in)
303 struct unpack_state state = unpack_state_init(format, in);
306 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
307 EBUG_ON(in->u64s < format->key_u64s);
308 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
310 out.inode = get_inc_field(&state, BKEY_FIELD_INODE);
311 out.offset = get_inc_field(&state, BKEY_FIELD_OFFSET);
312 out.snapshot = get_inc_field(&state, BKEY_FIELD_SNAPSHOT);
319 * bch2_bkey_pack_key -- pack just the key, not the value
321 bool bch2_bkey_pack_key(struct bkey_packed *out, const struct bkey *in,
322 const struct bkey_format *format)
324 struct pack_state state = pack_state_init(format, out);
327 EBUG_ON((void *) in == (void *) out);
328 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
329 EBUG_ON(in->format != KEY_FORMAT_CURRENT);
333 #define x(id, field) if (!set_inc_field(&state, id, in->field)) return false;
338 * Extents - we have to guarantee that if an extent is packed, a trimmed
339 * version will also pack:
341 if (bkey_start_offset(in) <
342 le64_to_cpu(format->field_offset[BKEY_FIELD_OFFSET]))
345 pack_state_finish(&state, out);
346 out->u64s = format->key_u64s + in->u64s - BKEY_U64s;
347 out->format = KEY_FORMAT_LOCAL_BTREE;
348 out->needs_whiteout = in->needs_whiteout;
349 out->type = in->type;
351 bch2_bkey_pack_verify(out, in, format);
356 * bch2_bkey_unpack -- unpack the key and the value
358 void bch2_bkey_unpack(const struct btree *b, struct bkey_i *dst,
359 const struct bkey_packed *src)
361 __bkey_unpack_key(b, &dst->k, src);
364 bkeyp_val(&b->format, src),
365 bkeyp_val_u64s(&b->format, src));
369 * bch2_bkey_pack -- pack the key and the value
371 bool bch2_bkey_pack(struct bkey_packed *out, const struct bkey_i *in,
372 const struct bkey_format *format)
374 struct bkey_packed tmp;
376 if (!bch2_bkey_pack_key(&tmp, &in->k, format))
379 memmove_u64s((u64 *) out + format->key_u64s,
381 bkey_val_u64s(&in->k));
382 memcpy_u64s(out, &tmp, format->key_u64s);
388 static bool set_inc_field_lossy(struct pack_state *state, unsigned field, u64 v)
390 unsigned bits = state->format->bits_per_field[field];
391 u64 offset = le64_to_cpu(state->format->field_offset[field]);
397 if (fls64(v) > bits) {
398 v = ~(~0ULL << bits);
402 if (bits > state->bits) {
404 state->w |= (v >> 1) >> (bits - 1);
406 *state->p = state->w;
407 state->p = next_word(state->p);
413 state->w |= v << state->bits;
418 #ifdef CONFIG_BCACHEFS_DEBUG
419 static bool bkey_packed_successor(struct bkey_packed *out,
420 const struct btree *b,
421 struct bkey_packed k)
423 const struct bkey_format *f = &b->format;
424 unsigned nr_key_bits = b->nr_key_bits;
425 unsigned first_bit, offset;
428 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(f));
435 first_bit = high_bit_offset + nr_key_bits - 1;
436 p = nth_word(high_word(f, out), first_bit >> 6);
437 offset = 63 - (first_bit & 63);
439 while (nr_key_bits) {
440 unsigned bits = min(64 - offset, nr_key_bits);
441 u64 mask = (~0ULL >> (64 - bits)) << offset;
443 if ((*p & mask) != mask) {
444 *p += 1ULL << offset;
445 EBUG_ON(bch2_bkey_cmp_packed(b, out, &k) <= 0);
460 * Returns a packed key that compares <= in
462 * This is used in bset_search_tree(), where we need a packed pos in order to be
463 * able to compare against the keys in the auxiliary search tree - and it's
464 * legal to use a packed pos that isn't equivalent to the original pos,
465 * _provided_ it compares <= to the original pos.
467 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *out,
469 const struct btree *b)
471 const struct bkey_format *f = &b->format;
472 struct pack_state state = pack_state_init(f, out);
474 #ifdef CONFIG_BCACHEFS_DEBUG
475 struct bpos orig = in;
481 * bch2_bkey_pack_key() will write to all of f->key_u64s, minus the 3
482 * byte header, but pack_pos() won't if the len/version fields are big
483 * enough - we need to make sure to zero them out:
485 for (i = 0; i < f->key_u64s; i++)
488 if (unlikely(in.snapshot <
489 le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]))) {
492 return BKEY_PACK_POS_FAIL;
493 in.snapshot = KEY_SNAPSHOT_MAX;
497 if (unlikely(in.offset <
498 le64_to_cpu(f->field_offset[BKEY_FIELD_OFFSET]))) {
500 return BKEY_PACK_POS_FAIL;
501 in.offset = KEY_OFFSET_MAX;
502 in.snapshot = KEY_SNAPSHOT_MAX;
506 if (unlikely(in.inode <
507 le64_to_cpu(f->field_offset[BKEY_FIELD_INODE])))
508 return BKEY_PACK_POS_FAIL;
510 if (!set_inc_field_lossy(&state, BKEY_FIELD_INODE, in.inode)) {
511 in.offset = KEY_OFFSET_MAX;
512 in.snapshot = KEY_SNAPSHOT_MAX;
516 if (!set_inc_field_lossy(&state, BKEY_FIELD_OFFSET, in.offset)) {
517 in.snapshot = KEY_SNAPSHOT_MAX;
521 if (!set_inc_field_lossy(&state, BKEY_FIELD_SNAPSHOT, in.snapshot))
524 pack_state_finish(&state, out);
525 out->u64s = f->key_u64s;
526 out->format = KEY_FORMAT_LOCAL_BTREE;
527 out->type = KEY_TYPE_deleted;
529 #ifdef CONFIG_BCACHEFS_DEBUG
531 BUG_ON(bkey_cmp_left_packed(b, out, &orig));
533 struct bkey_packed successor;
535 BUG_ON(bkey_cmp_left_packed(b, out, &orig) >= 0);
536 BUG_ON(bkey_packed_successor(&successor, b, *out) &&
537 bkey_cmp_left_packed(b, &successor, &orig) < 0);
541 return exact ? BKEY_PACK_POS_EXACT : BKEY_PACK_POS_SMALLER;
544 void bch2_bkey_format_init(struct bkey_format_state *s)
548 for (i = 0; i < ARRAY_SIZE(s->field_min); i++)
549 s->field_min[i] = U64_MAX;
551 for (i = 0; i < ARRAY_SIZE(s->field_max); i++)
554 /* Make sure we can store a size of 0: */
555 s->field_min[BKEY_FIELD_SIZE] = 0;
558 static void __bkey_format_add(struct bkey_format_state *s,
559 unsigned field, u64 v)
561 s->field_min[field] = min(s->field_min[field], v);
562 s->field_max[field] = max(s->field_max[field], v);
566 * Changes @format so that @k can be successfully packed with @format
568 void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
570 #define x(id, field) __bkey_format_add(s, id, k->field);
573 __bkey_format_add(s, BKEY_FIELD_OFFSET, bkey_start_offset(k));
576 void bch2_bkey_format_add_pos(struct bkey_format_state *s, struct bpos p)
580 __bkey_format_add(s, field++, p.inode);
581 __bkey_format_add(s, field++, p.offset);
582 __bkey_format_add(s, field++, p.snapshot);
586 * We don't want it to be possible for the packed format to represent fields
587 * bigger than a u64... that will cause confusion and issues (like with
588 * bkey_packed_successor())
590 static void set_format_field(struct bkey_format *f, enum bch_bkey_fields i,
591 unsigned bits, u64 offset)
593 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
594 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
596 bits = min(bits, unpacked_bits);
598 offset = bits == unpacked_bits ? 0 : min(offset, unpacked_max - ((1ULL << bits) - 1));
600 f->bits_per_field[i] = bits;
601 f->field_offset[i] = cpu_to_le64(offset);
604 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s)
606 unsigned i, bits = KEY_PACKED_BITS_START;
607 struct bkey_format ret = {
608 .nr_fields = BKEY_NR_FIELDS,
611 for (i = 0; i < ARRAY_SIZE(s->field_min); i++) {
612 s->field_min[i] = min(s->field_min[i], s->field_max[i]);
614 set_format_field(&ret, i,
615 fls64(s->field_max[i] - s->field_min[i]),
618 bits += ret.bits_per_field[i];
621 /* allow for extent merging: */
622 if (ret.bits_per_field[BKEY_FIELD_SIZE]) {
623 ret.bits_per_field[BKEY_FIELD_SIZE] += 4;
627 ret.key_u64s = DIV_ROUND_UP(bits, 64);
629 /* if we have enough spare bits, round fields up to nearest byte */
630 bits = ret.key_u64s * 64 - bits;
632 for (i = 0; i < ARRAY_SIZE(ret.bits_per_field); i++) {
633 unsigned r = round_up(ret.bits_per_field[i], 8) -
634 ret.bits_per_field[i];
637 set_format_field(&ret, i,
638 ret.bits_per_field[i] + r,
639 le64_to_cpu(ret.field_offset[i]));
644 EBUG_ON(bch2_bkey_format_validate(&ret));
648 const char *bch2_bkey_format_validate(struct bkey_format *f)
650 unsigned i, bits = KEY_PACKED_BITS_START;
652 if (f->nr_fields != BKEY_NR_FIELDS)
653 return "incorrect number of fields";
656 * Verify that the packed format can't represent fields larger than the
659 for (i = 0; i < f->nr_fields; i++) {
660 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
661 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
662 u64 packed_max = f->bits_per_field[i]
663 ? ~((~0ULL << 1) << (f->bits_per_field[i] - 1))
665 u64 field_offset = le64_to_cpu(f->field_offset[i]);
667 if (packed_max + field_offset < packed_max ||
668 packed_max + field_offset > unpacked_max)
669 return "field too large";
671 bits += f->bits_per_field[i];
674 if (f->key_u64s != DIV_ROUND_UP(bits, 64))
675 return "incorrect key_u64s";
681 * Most significant differing bit
682 * Bits are indexed from 0 - return is [0, nr_key_bits)
685 unsigned bch2_bkey_greatest_differing_bit(const struct btree *b,
686 const struct bkey_packed *l_k,
687 const struct bkey_packed *r_k)
689 const u64 *l = high_word(&b->format, l_k);
690 const u64 *r = high_word(&b->format, r_k);
691 unsigned nr_key_bits = b->nr_key_bits;
692 unsigned word_bits = 64 - high_bit_offset;
695 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
697 /* for big endian, skip past header */
698 l_v = *l & (~0ULL >> high_bit_offset);
699 r_v = *r & (~0ULL >> high_bit_offset);
701 while (nr_key_bits) {
702 if (nr_key_bits < word_bits) {
703 l_v >>= word_bits - nr_key_bits;
704 r_v >>= word_bits - nr_key_bits;
707 nr_key_bits -= word_bits;
711 return fls64(l_v ^ r_v) - 1 + nr_key_bits;
726 * Bits are indexed from 0 - return is [0, nr_key_bits)
729 unsigned bch2_bkey_ffs(const struct btree *b, const struct bkey_packed *k)
731 const u64 *p = high_word(&b->format, k);
732 unsigned nr_key_bits = b->nr_key_bits;
733 unsigned ret = 0, offset;
735 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
737 offset = nr_key_bits;
738 while (offset > 64) {
743 offset = 64 - offset;
745 while (nr_key_bits) {
746 unsigned bits = nr_key_bits + offset < 64
750 u64 mask = (~0ULL >> (64 - bits)) << offset;
753 return ret + __ffs64(*p & mask) - offset;
766 static inline int __bkey_cmp_bits(const u64 *l, const u64 *r,
767 unsigned nr_key_bits)
772 /* we shouldn't need asm for this, but gcc is being retarded: */
774 asm(".intel_syntax noprefix;"
786 "lea rdi, [rdi - 8];"
787 "lea rsi, [rsi - 8];"
802 ".att_syntax prefix;"
803 : "=&D" (d0), "=&S" (d1), "=&d" (d2), "=&c" (d3), "=&a" (cmp)
804 : "0" (l), "1" (r), "3" (nr_key_bits)
805 : "r8", "r9", "cc", "memory");
810 #define I(_x) (*(out)++ = (_x))
812 #define I2(i0, i1) (I1(i0), I(i1))
813 #define I3(i0, i1, i2) (I2(i0, i1), I(i2))
814 #define I4(i0, i1, i2, i3) (I3(i0, i1, i2), I(i3))
815 #define I5(i0, i1, i2, i3, i4) (I4(i0, i1, i2, i3), I(i4))
817 static u8 *compile_bkey_field(const struct bkey_format *format, u8 *out,
818 enum bch_bkey_fields field,
819 unsigned dst_offset, unsigned dst_size,
822 unsigned bits = format->bits_per_field[field];
823 u64 offset = le64_to_cpu(format->field_offset[field]);
824 unsigned i, byte, bit_offset, align, shl, shr;
826 if (!bits && !offset) {
837 /* just return offset: */
841 if (offset > S32_MAX) {
842 /* mov [rdi + dst_offset], offset */
843 I3(0xc7, 0x47, dst_offset);
844 memcpy(out, &offset, 4);
847 I3(0xc7, 0x47, dst_offset + 4);
848 memcpy(out, (void *) &offset + 4, 4);
851 /* mov [rdi + dst_offset], offset */
853 I4(0x48, 0xc7, 0x47, dst_offset);
854 memcpy(out, &offset, 4);
859 /* mov [rdi + dst_offset], offset */
860 I3(0xc7, 0x47, dst_offset);
861 memcpy(out, &offset, 4);
871 bit_offset = format->key_u64s * 64;
872 for (i = 0; i <= field; i++)
873 bit_offset -= format->bits_per_field[i];
875 byte = bit_offset / 8;
876 bit_offset -= byte * 8;
880 if (bit_offset == 0 && bits == 8) {
881 /* movzx eax, BYTE PTR [rsi + imm8] */
882 I4(0x0f, 0xb6, 0x46, byte);
883 } else if (bit_offset == 0 && bits == 16) {
884 /* movzx eax, WORD PTR [rsi + imm8] */
885 I4(0x0f, 0xb7, 0x46, byte);
886 } else if (bit_offset + bits <= 32) {
887 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
889 bit_offset += align * 8;
891 BUG_ON(bit_offset + bits > 32);
893 /* mov eax, [rsi + imm8] */
894 I3(0x8b, 0x46, byte);
898 I3(0xc1, 0xe8, bit_offset);
901 if (bit_offset + bits < 32) {
902 unsigned mask = ~0U >> (32 - bits);
906 memcpy(out, &mask, 4);
909 } else if (bit_offset + bits <= 64) {
910 align = min(8 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 7);
912 bit_offset += align * 8;
914 BUG_ON(bit_offset + bits > 64);
916 /* mov rax, [rsi + imm8] */
917 I4(0x48, 0x8b, 0x46, byte);
919 shl = 64 - bit_offset - bits;
920 shr = bit_offset + shl;
924 I4(0x48, 0xc1, 0xe0, shl);
929 I4(0x48, 0xc1, 0xe8, shr);
932 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
934 bit_offset += align * 8;
936 BUG_ON(bit_offset + bits > 96);
938 /* mov rax, [rsi + byte] */
939 I4(0x48, 0x8b, 0x46, byte);
941 /* mov edx, [rsi + byte + 8] */
942 I3(0x8b, 0x56, byte + 8);
944 /* bits from next word: */
945 shr = bit_offset + bits - 64;
946 BUG_ON(shr > bit_offset);
948 /* shr rax, bit_offset */
949 I4(0x48, 0xc1, 0xe8, shr);
952 I4(0x48, 0xc1, 0xe2, 64 - shr);
955 I3(0x48, 0x09, 0xd0);
957 shr = bit_offset - shr;
961 I4(0x48, 0xc1, 0xe8, shr);
966 if (offset > S32_MAX) {
969 memcpy(out, &offset, 8);
972 I3(0x48, 0x01, 0xd0);
973 } else if (offset + (~0ULL >> (64 - bits)) > U32_MAX) {
976 memcpy(out, &offset, 4);
981 memcpy(out, &offset, 4);
987 /* mov [rdi + dst_offset], rax */
988 I4(0x48, 0x89, 0x47, dst_offset);
991 /* mov [rdi + dst_offset], eax */
992 I3(0x89, 0x47, dst_offset);
1001 int bch2_compile_bkey_format(const struct bkey_format *format, void *_out)
1003 bool eax_zeroed = false;
1007 * rdi: dst - unpacked key
1008 * rsi: src - packed key
1011 /* k->u64s, k->format, k->type */
1013 /* mov eax, [rsi] */
1016 /* add eax, BKEY_U64s - format->key_u64s */
1017 I5(0x05, BKEY_U64s - format->key_u64s, KEY_FORMAT_CURRENT, 0, 0);
1019 /* and eax, imm32: mask out k->pad: */
1020 I5(0x25, 0xff, 0xff, 0xff, 0);
1022 /* mov [rdi], eax */
1025 #define x(id, field) \
1026 out = compile_bkey_field(format, out, id, \
1027 offsetof(struct bkey, field), \
1028 sizeof(((struct bkey *) NULL)->field), \
1036 return (void *) out - _out;
1040 static inline int __bkey_cmp_bits(const u64 *l, const u64 *r,
1041 unsigned nr_key_bits)
1048 /* for big endian, skip past header */
1049 nr_key_bits += high_bit_offset;
1050 l_v = *l & (~0ULL >> high_bit_offset);
1051 r_v = *r & (~0ULL >> high_bit_offset);
1054 if (nr_key_bits < 64) {
1055 l_v >>= 64 - nr_key_bits;
1056 r_v >>= 64 - nr_key_bits;
1062 if (!nr_key_bits || l_v != r_v)
1072 return cmp_int(l_v, r_v);
1077 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *l,
1078 const struct bkey_packed *r,
1079 const struct btree *b)
1081 const struct bkey_format *f = &b->format;
1084 EBUG_ON(!bkey_packed(l) || !bkey_packed(r));
1085 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(f));
1087 ret = __bkey_cmp_bits(high_word(f, l),
1091 EBUG_ON(ret != bpos_cmp(bkey_unpack_pos(b, l),
1092 bkey_unpack_pos(b, r)));
1097 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *b,
1098 const struct bkey_packed *l,
1099 const struct bpos *r)
1101 return bpos_cmp(bkey_unpack_pos_format_checked(b, l), *r);
1105 int bch2_bkey_cmp_packed(const struct btree *b,
1106 const struct bkey_packed *l,
1107 const struct bkey_packed *r)
1109 struct bkey unpacked;
1111 if (likely(bkey_packed(l) && bkey_packed(r)))
1112 return __bch2_bkey_cmp_packed_format_checked(l, r, b);
1114 if (bkey_packed(l)) {
1115 __bkey_unpack_key_format_checked(b, &unpacked, l);
1116 l = (void*) &unpacked;
1117 } else if (bkey_packed(r)) {
1118 __bkey_unpack_key_format_checked(b, &unpacked, r);
1119 r = (void*) &unpacked;
1122 return bpos_cmp(((struct bkey *) l)->p, ((struct bkey *) r)->p);
1126 int __bch2_bkey_cmp_left_packed(const struct btree *b,
1127 const struct bkey_packed *l,
1128 const struct bpos *r)
1130 const struct bkey *l_unpacked;
1132 return unlikely(l_unpacked = packed_to_bkey_c(l))
1133 ? bpos_cmp(l_unpacked->p, *r)
1134 : __bch2_bkey_cmp_left_packed_format_checked(b, l, r);
1137 void bch2_bpos_swab(struct bpos *p)
1140 u8 *h = ((u8 *) &p[1]) - 1;
1149 void bch2_bkey_swab_key(const struct bkey_format *_f, struct bkey_packed *k)
1151 const struct bkey_format *f = bkey_packed(k) ? _f : &bch2_bkey_format_current;
1152 u8 *l = k->key_start;
1153 u8 *h = (u8 *) (k->_data + f->key_u64s) - 1;
1162 #ifdef CONFIG_BCACHEFS_DEBUG
1163 void bch2_bkey_pack_test(void)
1165 struct bkey t = KEY(4134ULL, 1250629070527416633ULL, 0);
1166 struct bkey_packed p;
1168 struct bkey_format test_format = {
1170 .nr_fields = BKEY_NR_FIELDS,
1178 struct unpack_state in_s =
1179 unpack_state_init(&bch2_bkey_format_current, (void *) &t);
1180 struct pack_state out_s = pack_state_init(&test_format, &p);
1183 for (i = 0; i < out_s.format->nr_fields; i++) {
1184 u64 a, v = get_inc_field(&in_s, i);
1187 #define x(id, field) case id: a = t.field; break;
1195 panic("got %llu actual %llu i %u\n", v, a, i);
1197 if (!set_inc_field(&out_s, i, v))
1198 panic("failed at %u\n", i);
1201 BUG_ON(!bch2_bkey_pack_key(&p, &t, &test_format));