1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey.h"
5 #include "bkey_cmp.h"
6 #include "bkey_methods.h"
7 #include "bset.h"
8 #include "util.h"
9
10 const struct bkey_format bch2_bkey_format_current = BKEY_FORMAT_CURRENT;
11
bch2_bkey_packed_to_binary_text(struct printbuf * out,const struct bkey_format * f,const struct bkey_packed * k)12 void bch2_bkey_packed_to_binary_text(struct printbuf *out,
13 const struct bkey_format *f,
14 const struct bkey_packed *k)
15 {
16 const u64 *p = high_word(f, k);
17 unsigned word_bits = 64 - high_bit_offset;
18 unsigned nr_key_bits = bkey_format_key_bits(f) + high_bit_offset;
19 u64 v = *p & (~0ULL >> high_bit_offset);
20
21 if (!nr_key_bits) {
22 prt_str(out, "(empty)");
23 return;
24 }
25
26 while (1) {
27 unsigned next_key_bits = nr_key_bits;
28
29 if (nr_key_bits < 64) {
30 v >>= 64 - nr_key_bits;
31 next_key_bits = 0;
32 } else {
33 next_key_bits -= 64;
34 }
35
36 bch2_prt_u64_base2_nbits(out, v, min(word_bits, nr_key_bits));
37
38 if (!next_key_bits)
39 break;
40
41 prt_char(out, ' ');
42
43 p = next_word(p);
44 v = *p;
45 word_bits = 64;
46 nr_key_bits = next_key_bits;
47 }
48 }
49
__bch2_bkey_pack_verify(const struct bkey_packed * packed,const struct bkey * unpacked,const struct bkey_format * format)50 static void __bch2_bkey_pack_verify(const struct bkey_packed *packed,
51 const struct bkey *unpacked,
52 const struct bkey_format *format)
53 {
54 struct bkey tmp;
55
56 BUG_ON(bkeyp_val_u64s(format, packed) !=
57 bkey_val_u64s(unpacked));
58
59 BUG_ON(packed->u64s < bkeyp_key_u64s(format, packed));
60
61 tmp = __bch2_bkey_unpack_key(format, packed);
62
63 if (memcmp(&tmp, unpacked, sizeof(struct bkey))) {
64 struct printbuf buf = PRINTBUF;
65
66 prt_printf(&buf, "keys differ: format u64s %u fields %u %u %u %u %u\n",
67 format->key_u64s,
68 format->bits_per_field[0],
69 format->bits_per_field[1],
70 format->bits_per_field[2],
71 format->bits_per_field[3],
72 format->bits_per_field[4]);
73
74 prt_printf(&buf, "compiled unpack: ");
75 bch2_bkey_to_text(&buf, unpacked);
76 prt_newline(&buf);
77
78 prt_printf(&buf, "c unpack: ");
79 bch2_bkey_to_text(&buf, &tmp);
80 prt_newline(&buf);
81
82 prt_printf(&buf, "compiled unpack: ");
83 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
84 (struct bkey_packed *) unpacked);
85 prt_newline(&buf);
86
87 prt_printf(&buf, "c unpack: ");
88 bch2_bkey_packed_to_binary_text(&buf, &bch2_bkey_format_current,
89 (struct bkey_packed *) &tmp);
90 prt_newline(&buf);
91
92 panic("%s", buf.buf);
93 }
94 }
95
bch2_bkey_pack_verify(const struct bkey_packed * packed,const struct bkey * unpacked,const struct bkey_format * format)96 static inline void bch2_bkey_pack_verify(const struct bkey_packed *packed,
97 const struct bkey *unpacked,
98 const struct bkey_format *format)
99 {
100 if (static_branch_unlikely(&bch2_debug_check_bkey_unpack))
101 __bch2_bkey_pack_verify(packed, unpacked, format);
102 }
103
104 struct pack_state {
105 const struct bkey_format *format;
106 unsigned bits; /* bits remaining in current word */
107 u64 w; /* current word */
108 u64 *p; /* pointer to next word */
109 };
110
111 __always_inline
pack_state_init(const struct bkey_format * format,struct bkey_packed * k)112 static struct pack_state pack_state_init(const struct bkey_format *format,
113 struct bkey_packed *k)
114 {
115 u64 *p = high_word(format, k);
116
117 return (struct pack_state) {
118 .format = format,
119 .bits = 64 - high_bit_offset,
120 .w = 0,
121 .p = p,
122 };
123 }
124
125 __always_inline
pack_state_finish(struct pack_state * state,struct bkey_packed * k)126 static void pack_state_finish(struct pack_state *state,
127 struct bkey_packed *k)
128 {
129 EBUG_ON(state->p < k->_data);
130 EBUG_ON(state->p >= (u64 *) k->_data + state->format->key_u64s);
131
132 *state->p = state->w;
133 }
134
135 struct unpack_state {
136 const struct bkey_format *format;
137 unsigned bits; /* bits remaining in current word */
138 u64 w; /* current word */
139 const u64 *p; /* pointer to next word */
140 };
141
142 __always_inline
unpack_state_init(const struct bkey_format * format,const struct bkey_packed * k)143 static struct unpack_state unpack_state_init(const struct bkey_format *format,
144 const struct bkey_packed *k)
145 {
146 const u64 *p = high_word(format, k);
147
148 return (struct unpack_state) {
149 .format = format,
150 .bits = 64 - high_bit_offset,
151 .w = *p << high_bit_offset,
152 .p = p,
153 };
154 }
155
156 __always_inline
get_inc_field(struct unpack_state * state,unsigned field)157 static u64 get_inc_field(struct unpack_state *state, unsigned field)
158 {
159 unsigned bits = state->format->bits_per_field[field];
160 u64 v = 0, offset = le64_to_cpu(state->format->field_offset[field]);
161
162 if (bits >= state->bits) {
163 v = state->w >> (64 - bits);
164 bits -= state->bits;
165
166 state->p = next_word(state->p);
167 state->w = *state->p;
168 state->bits = 64;
169 }
170
171 /* avoid shift by 64 if bits is 0 - bits is never 64 here: */
172 v |= (state->w >> 1) >> (63 - bits);
173 state->w <<= bits;
174 state->bits -= bits;
175
176 return v + offset;
177 }
178
179 __always_inline
__set_inc_field(struct pack_state * state,unsigned field,u64 v)180 static void __set_inc_field(struct pack_state *state, unsigned field, u64 v)
181 {
182 unsigned bits = state->format->bits_per_field[field];
183
184 if (bits) {
185 if (bits > state->bits) {
186 bits -= state->bits;
187 /* avoid shift by 64 if bits is 64 - bits is never 0 here: */
188 state->w |= (v >> 1) >> (bits - 1);
189
190 *state->p = state->w;
191 state->p = next_word(state->p);
192 state->w = 0;
193 state->bits = 64;
194 }
195
196 state->bits -= bits;
197 state->w |= v << state->bits;
198 }
199 }
200
201 __always_inline
set_inc_field(struct pack_state * state,unsigned field,u64 v)202 static bool set_inc_field(struct pack_state *state, unsigned field, u64 v)
203 {
204 unsigned bits = state->format->bits_per_field[field];
205 u64 offset = le64_to_cpu(state->format->field_offset[field]);
206
207 if (v < offset)
208 return false;
209
210 v -= offset;
211
212 if (fls64(v) > bits)
213 return false;
214
215 __set_inc_field(state, field, v);
216 return true;
217 }
218
219 /*
220 * Note: does NOT set out->format (we don't know what it should be here!)
221 *
222 * Also: doesn't work on extents - it doesn't preserve the invariant that
223 * if k is packed bkey_start_pos(k) will successfully pack
224 */
bch2_bkey_transform_key(const struct bkey_format * out_f,struct bkey_packed * out,const struct bkey_format * in_f,const struct bkey_packed * in)225 static bool bch2_bkey_transform_key(const struct bkey_format *out_f,
226 struct bkey_packed *out,
227 const struct bkey_format *in_f,
228 const struct bkey_packed *in)
229 {
230 struct pack_state out_s = pack_state_init(out_f, out);
231 struct unpack_state in_s = unpack_state_init(in_f, in);
232 u64 *w = out->_data;
233 unsigned i;
234
235 *w = 0;
236
237 for (i = 0; i < BKEY_NR_FIELDS; i++)
238 if (!set_inc_field(&out_s, i, get_inc_field(&in_s, i)))
239 return false;
240
241 /* Can't happen because the val would be too big to unpack: */
242 EBUG_ON(in->u64s - in_f->key_u64s + out_f->key_u64s > U8_MAX);
243
244 pack_state_finish(&out_s, out);
245 out->u64s = out_f->key_u64s + in->u64s - in_f->key_u64s;
246 out->needs_whiteout = in->needs_whiteout;
247 out->type = in->type;
248
249 return true;
250 }
251
bch2_bkey_transform(const struct bkey_format * out_f,struct bkey_packed * out,const struct bkey_format * in_f,const struct bkey_packed * in)252 bool bch2_bkey_transform(const struct bkey_format *out_f,
253 struct bkey_packed *out,
254 const struct bkey_format *in_f,
255 const struct bkey_packed *in)
256 {
257 if (!bch2_bkey_transform_key(out_f, out, in_f, in))
258 return false;
259
260 memcpy_u64s((u64 *) out + out_f->key_u64s,
261 (u64 *) in + in_f->key_u64s,
262 (in->u64s - in_f->key_u64s));
263 return true;
264 }
265
__bch2_bkey_unpack_key(const struct bkey_format * format,const struct bkey_packed * in)266 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *format,
267 const struct bkey_packed *in)
268 {
269 struct unpack_state state = unpack_state_init(format, in);
270 struct bkey out;
271
272 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
273 EBUG_ON(in->u64s < format->key_u64s);
274 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
275 EBUG_ON(in->u64s - format->key_u64s + BKEY_U64s > U8_MAX);
276
277 out.u64s = BKEY_U64s + in->u64s - format->key_u64s;
278 out.format = KEY_FORMAT_CURRENT;
279 out.needs_whiteout = in->needs_whiteout;
280 out.type = in->type;
281 out.pad[0] = 0;
282
283 #define x(id, field) out.field = get_inc_field(&state, id);
284 bkey_fields()
285 #undef x
286
287 return out;
288 }
289
290 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
__bkey_unpack_pos(const struct bkey_format * format,const struct bkey_packed * in)291 struct bpos __bkey_unpack_pos(const struct bkey_format *format,
292 const struct bkey_packed *in)
293 {
294 struct unpack_state state = unpack_state_init(format, in);
295 struct bpos out;
296
297 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
298 EBUG_ON(in->u64s < format->key_u64s);
299 EBUG_ON(in->format != KEY_FORMAT_LOCAL_BTREE);
300
301 out.inode = get_inc_field(&state, BKEY_FIELD_INODE);
302 out.offset = get_inc_field(&state, BKEY_FIELD_OFFSET);
303 out.snapshot = get_inc_field(&state, BKEY_FIELD_SNAPSHOT);
304
305 return out;
306 }
307 #endif
308
309 /**
310 * bch2_bkey_pack_key -- pack just the key, not the value
311 * @out: packed result
312 * @in: key to pack
313 * @format: format of packed result
314 *
315 * Returns: true on success, false on failure
316 */
bch2_bkey_pack_key(struct bkey_packed * out,const struct bkey * in,const struct bkey_format * format)317 bool bch2_bkey_pack_key(struct bkey_packed *out, const struct bkey *in,
318 const struct bkey_format *format)
319 {
320 struct pack_state state = pack_state_init(format, out);
321 u64 *w = out->_data;
322
323 EBUG_ON((void *) in == (void *) out);
324 EBUG_ON(format->nr_fields != BKEY_NR_FIELDS);
325 EBUG_ON(in->format != KEY_FORMAT_CURRENT);
326
327 *w = 0;
328
329 #define x(id, field) if (!set_inc_field(&state, id, in->field)) return false;
330 bkey_fields()
331 #undef x
332 pack_state_finish(&state, out);
333 out->u64s = format->key_u64s + in->u64s - BKEY_U64s;
334 out->format = KEY_FORMAT_LOCAL_BTREE;
335 out->needs_whiteout = in->needs_whiteout;
336 out->type = in->type;
337
338 bch2_bkey_pack_verify(out, in, format);
339 return true;
340 }
341
342 /**
343 * bch2_bkey_unpack -- unpack the key and the value
344 * @b: btree node of @src key (for packed format)
345 * @dst: unpacked result
346 * @src: packed input
347 */
bch2_bkey_unpack(const struct btree * b,struct bkey_i * dst,const struct bkey_packed * src)348 void bch2_bkey_unpack(const struct btree *b, struct bkey_i *dst,
349 const struct bkey_packed *src)
350 {
351 __bkey_unpack_key(b, &dst->k, src);
352
353 memcpy_u64s(&dst->v,
354 bkeyp_val(&b->format, src),
355 bkeyp_val_u64s(&b->format, src));
356 }
357
358 /**
359 * bch2_bkey_pack -- pack the key and the value
360 * @dst: packed result
361 * @src: unpacked input
362 * @format: format of packed result
363 *
364 * Returns: true on success, false on failure
365 */
bch2_bkey_pack(struct bkey_packed * dst,const struct bkey_i * src,const struct bkey_format * format)366 bool bch2_bkey_pack(struct bkey_packed *dst, const struct bkey_i *src,
367 const struct bkey_format *format)
368 {
369 struct bkey_packed tmp;
370
371 if (!bch2_bkey_pack_key(&tmp, &src->k, format))
372 return false;
373
374 memmove_u64s((u64 *) dst + format->key_u64s,
375 &src->v,
376 bkey_val_u64s(&src->k));
377 memcpy_u64s_small(dst, &tmp, format->key_u64s);
378
379 return true;
380 }
381
382 __always_inline
set_inc_field_lossy(struct pack_state * state,unsigned field,u64 v)383 static bool set_inc_field_lossy(struct pack_state *state, unsigned field, u64 v)
384 {
385 unsigned bits = state->format->bits_per_field[field];
386 u64 offset = le64_to_cpu(state->format->field_offset[field]);
387 bool ret = true;
388
389 EBUG_ON(v < offset);
390 v -= offset;
391
392 if (fls64(v) > bits) {
393 v = ~(~0ULL << bits);
394 ret = false;
395 }
396
397 __set_inc_field(state, field, v);
398 return ret;
399 }
400
bkey_packed_successor(struct bkey_packed * out,const struct btree * b,struct bkey_packed k)401 static bool bkey_packed_successor(struct bkey_packed *out,
402 const struct btree *b,
403 struct bkey_packed k)
404 {
405 const struct bkey_format *f = &b->format;
406 unsigned nr_key_bits = b->nr_key_bits;
407 unsigned first_bit, offset;
408 u64 *p;
409
410 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(f));
411
412 if (!nr_key_bits)
413 return false;
414
415 *out = k;
416
417 first_bit = high_bit_offset + nr_key_bits - 1;
418 p = nth_word(high_word(f, out), first_bit >> 6);
419 offset = 63 - (first_bit & 63);
420
421 while (nr_key_bits) {
422 unsigned bits = min(64 - offset, nr_key_bits);
423 u64 mask = (~0ULL >> (64 - bits)) << offset;
424
425 if ((*p & mask) != mask) {
426 *p += 1ULL << offset;
427 EBUG_ON(bch2_bkey_cmp_packed(b, out, &k) <= 0);
428 return true;
429 }
430
431 *p &= ~mask;
432 p = prev_word(p);
433 nr_key_bits -= bits;
434 offset = 0;
435 }
436
437 return false;
438 }
439
bkey_format_has_too_big_fields(const struct bkey_format * f)440 static bool bkey_format_has_too_big_fields(const struct bkey_format *f)
441 {
442 for (unsigned i = 0; i < f->nr_fields; i++) {
443 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
444 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
445 u64 packed_max = f->bits_per_field[i]
446 ? ~((~0ULL << 1) << (f->bits_per_field[i] - 1))
447 : 0;
448 u64 field_offset = le64_to_cpu(f->field_offset[i]);
449
450 if (packed_max + field_offset < packed_max ||
451 packed_max + field_offset > unpacked_max)
452 return true;
453 }
454
455 return false;
456 }
457
458 /*
459 * Returns a packed key that compares <= in
460 *
461 * This is used in bset_search_tree(), where we need a packed pos in order to be
462 * able to compare against the keys in the auxiliary search tree - and it's
463 * legal to use a packed pos that isn't equivalent to the original pos,
464 * _provided_ it compares <= to the original pos.
465 */
bch2_bkey_pack_pos_lossy(struct bkey_packed * out,struct bpos in,const struct btree * b)466 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *out,
467 struct bpos in,
468 const struct btree *b)
469 {
470 const struct bkey_format *f = &b->format;
471 struct pack_state state = pack_state_init(f, out);
472 u64 *w = out->_data;
473 struct bpos orig = in;
474 bool exact = true;
475 unsigned i;
476
477 /*
478 * bch2_bkey_pack_key() will write to all of f->key_u64s, minus the 3
479 * byte header, but pack_pos() won't if the len/version fields are big
480 * enough - we need to make sure to zero them out:
481 */
482 for (i = 0; i < f->key_u64s; i++)
483 w[i] = 0;
484
485 if (unlikely(in.snapshot <
486 le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]))) {
487 if (!in.offset-- &&
488 !in.inode--)
489 return BKEY_PACK_POS_FAIL;
490 in.snapshot = KEY_SNAPSHOT_MAX;
491 exact = false;
492 }
493
494 if (unlikely(in.offset <
495 le64_to_cpu(f->field_offset[BKEY_FIELD_OFFSET]))) {
496 if (!in.inode--)
497 return BKEY_PACK_POS_FAIL;
498 in.offset = KEY_OFFSET_MAX;
499 in.snapshot = KEY_SNAPSHOT_MAX;
500 exact = false;
501 }
502
503 if (unlikely(in.inode <
504 le64_to_cpu(f->field_offset[BKEY_FIELD_INODE])))
505 return BKEY_PACK_POS_FAIL;
506
507 if (unlikely(!set_inc_field_lossy(&state, BKEY_FIELD_INODE, in.inode))) {
508 in.offset = KEY_OFFSET_MAX;
509 in.snapshot = KEY_SNAPSHOT_MAX;
510 exact = false;
511 }
512
513 if (unlikely(!set_inc_field_lossy(&state, BKEY_FIELD_OFFSET, in.offset))) {
514 in.snapshot = KEY_SNAPSHOT_MAX;
515 exact = false;
516 }
517
518 if (unlikely(!set_inc_field_lossy(&state, BKEY_FIELD_SNAPSHOT, in.snapshot)))
519 exact = false;
520
521 pack_state_finish(&state, out);
522 out->u64s = f->key_u64s;
523 out->format = KEY_FORMAT_LOCAL_BTREE;
524 out->type = KEY_TYPE_deleted;
525
526 if (static_branch_unlikely(&bch2_debug_check_bkey_unpack)) {
527 if (exact) {
528 BUG_ON(bkey_cmp_left_packed(b, out, &orig));
529 } else {
530 struct bkey_packed successor;
531
532 BUG_ON(bkey_cmp_left_packed(b, out, &orig) >= 0);
533 BUG_ON(bkey_packed_successor(&successor, b, *out) &&
534 bkey_cmp_left_packed(b, &successor, &orig) < 0 &&
535 !bkey_format_has_too_big_fields(f));
536 }
537 }
538
539 return exact ? BKEY_PACK_POS_EXACT : BKEY_PACK_POS_SMALLER;
540 }
541
bch2_bkey_format_init(struct bkey_format_state * s)542 void bch2_bkey_format_init(struct bkey_format_state *s)
543 {
544 unsigned i;
545
546 for (i = 0; i < ARRAY_SIZE(s->field_min); i++)
547 s->field_min[i] = U64_MAX;
548
549 for (i = 0; i < ARRAY_SIZE(s->field_max); i++)
550 s->field_max[i] = 0;
551
552 /* Make sure we can store a size of 0: */
553 s->field_min[BKEY_FIELD_SIZE] = 0;
554 }
555
bch2_bkey_format_add_pos(struct bkey_format_state * s,struct bpos p)556 void bch2_bkey_format_add_pos(struct bkey_format_state *s, struct bpos p)
557 {
558 unsigned field = 0;
559
560 __bkey_format_add(s, field++, p.inode);
561 __bkey_format_add(s, field++, p.offset);
562 __bkey_format_add(s, field++, p.snapshot);
563 }
564
565 /*
566 * We don't want it to be possible for the packed format to represent fields
567 * bigger than a u64... that will cause confusion and issues (like with
568 * bkey_packed_successor())
569 */
set_format_field(struct bkey_format * f,enum bch_bkey_fields i,unsigned bits,u64 offset)570 static void set_format_field(struct bkey_format *f, enum bch_bkey_fields i,
571 unsigned bits, u64 offset)
572 {
573 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
574 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
575
576 bits = min(bits, unpacked_bits);
577
578 offset = bits == unpacked_bits ? 0 : min(offset, unpacked_max - ((1ULL << bits) - 1));
579
580 f->bits_per_field[i] = bits;
581 f->field_offset[i] = cpu_to_le64(offset);
582 }
583
bch2_bkey_format_done(struct bkey_format_state * s)584 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s)
585 {
586 unsigned i, bits = KEY_PACKED_BITS_START;
587 struct bkey_format ret = {
588 .nr_fields = BKEY_NR_FIELDS,
589 };
590
591 for (i = 0; i < ARRAY_SIZE(s->field_min); i++) {
592 s->field_min[i] = min(s->field_min[i], s->field_max[i]);
593
594 set_format_field(&ret, i,
595 fls64(s->field_max[i] - s->field_min[i]),
596 s->field_min[i]);
597
598 bits += ret.bits_per_field[i];
599 }
600
601 /* allow for extent merging: */
602 if (ret.bits_per_field[BKEY_FIELD_SIZE]) {
603 unsigned b = min(4U, 32U - ret.bits_per_field[BKEY_FIELD_SIZE]);
604
605 ret.bits_per_field[BKEY_FIELD_SIZE] += b;
606 bits += b;
607 }
608
609 ret.key_u64s = DIV_ROUND_UP(bits, 64);
610
611 /* if we have enough spare bits, round fields up to nearest byte */
612 bits = ret.key_u64s * 64 - bits;
613
614 for (i = 0; i < ARRAY_SIZE(ret.bits_per_field); i++) {
615 unsigned r = round_up(ret.bits_per_field[i], 8) -
616 ret.bits_per_field[i];
617
618 if (r <= bits) {
619 set_format_field(&ret, i,
620 ret.bits_per_field[i] + r,
621 le64_to_cpu(ret.field_offset[i]));
622 bits -= r;
623 }
624 }
625
626 if (static_branch_unlikely(&bch2_debug_check_bkey_unpack)) {
627 struct printbuf buf = PRINTBUF;
628
629 BUG_ON(bch2_bkey_format_invalid(NULL, &ret, 0, &buf));
630 printbuf_exit(&buf);
631 }
632
633 return ret;
634 }
635
bch2_bkey_format_invalid(struct bch_fs * c,struct bkey_format * f,enum bch_validate_flags flags,struct printbuf * err)636 int bch2_bkey_format_invalid(struct bch_fs *c,
637 struct bkey_format *f,
638 enum bch_validate_flags flags,
639 struct printbuf *err)
640 {
641 unsigned bits = KEY_PACKED_BITS_START;
642
643 if (f->nr_fields != BKEY_NR_FIELDS) {
644 prt_printf(err, "incorrect number of fields: got %u, should be %u",
645 f->nr_fields, BKEY_NR_FIELDS);
646 return -BCH_ERR_invalid;
647 }
648
649 /*
650 * Verify that the packed format can't represent fields larger than the
651 * unpacked format:
652 */
653 for (unsigned i = 0; i < f->nr_fields; i++) {
654 if (bch2_bkey_format_field_overflows(f, i)) {
655 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
656 u64 unpacked_max = ~((~0ULL << 1) << (unpacked_bits - 1));
657 unsigned packed_bits = min(64, f->bits_per_field[i]);
658 u64 packed_max = packed_bits
659 ? ~((~0ULL << 1) << (packed_bits - 1))
660 : 0;
661
662 prt_printf(err, "field %u too large: %llu + %llu > %llu",
663 i, packed_max, le64_to_cpu(f->field_offset[i]), unpacked_max);
664 return -BCH_ERR_invalid;
665 }
666
667 bits += f->bits_per_field[i];
668 }
669
670 if (f->key_u64s != DIV_ROUND_UP(bits, 64)) {
671 prt_printf(err, "incorrect key_u64s: got %u, should be %u",
672 f->key_u64s, DIV_ROUND_UP(bits, 64));
673 return -BCH_ERR_invalid;
674 }
675
676 return 0;
677 }
678
bch2_bkey_format_to_text(struct printbuf * out,const struct bkey_format * f)679 void bch2_bkey_format_to_text(struct printbuf *out, const struct bkey_format *f)
680 {
681 prt_printf(out, "u64s %u fields ", f->key_u64s);
682
683 for (unsigned i = 0; i < ARRAY_SIZE(f->bits_per_field); i++) {
684 if (i)
685 prt_str(out, ", ");
686 prt_printf(out, "%u:%llu",
687 f->bits_per_field[i],
688 le64_to_cpu(f->field_offset[i]));
689 }
690 }
691
692 /*
693 * Most significant differing bit
694 * Bits are indexed from 0 - return is [0, nr_key_bits)
695 */
696 __pure
bch2_bkey_greatest_differing_bit(const struct btree * b,const struct bkey_packed * l_k,const struct bkey_packed * r_k)697 unsigned bch2_bkey_greatest_differing_bit(const struct btree *b,
698 const struct bkey_packed *l_k,
699 const struct bkey_packed *r_k)
700 {
701 const u64 *l = high_word(&b->format, l_k);
702 const u64 *r = high_word(&b->format, r_k);
703 unsigned nr_key_bits = b->nr_key_bits;
704 unsigned word_bits = 64 - high_bit_offset;
705 u64 l_v, r_v;
706
707 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
708
709 /* for big endian, skip past header */
710 l_v = *l & (~0ULL >> high_bit_offset);
711 r_v = *r & (~0ULL >> high_bit_offset);
712
713 while (nr_key_bits) {
714 if (nr_key_bits < word_bits) {
715 l_v >>= word_bits - nr_key_bits;
716 r_v >>= word_bits - nr_key_bits;
717 nr_key_bits = 0;
718 } else {
719 nr_key_bits -= word_bits;
720 }
721
722 if (l_v != r_v)
723 return fls64(l_v ^ r_v) - 1 + nr_key_bits;
724
725 l = next_word(l);
726 r = next_word(r);
727
728 l_v = *l;
729 r_v = *r;
730 word_bits = 64;
731 }
732
733 return 0;
734 }
735
736 /*
737 * First set bit
738 * Bits are indexed from 0 - return is [0, nr_key_bits)
739 */
740 __pure
bch2_bkey_ffs(const struct btree * b,const struct bkey_packed * k)741 unsigned bch2_bkey_ffs(const struct btree *b, const struct bkey_packed *k)
742 {
743 const u64 *p = high_word(&b->format, k);
744 unsigned nr_key_bits = b->nr_key_bits;
745 unsigned ret = 0, offset;
746
747 EBUG_ON(b->nr_key_bits != bkey_format_key_bits(&b->format));
748
749 offset = nr_key_bits;
750 while (offset > 64) {
751 p = next_word(p);
752 offset -= 64;
753 }
754
755 offset = 64 - offset;
756
757 while (nr_key_bits) {
758 unsigned bits = nr_key_bits + offset < 64
759 ? nr_key_bits
760 : 64 - offset;
761
762 u64 mask = (~0ULL >> (64 - bits)) << offset;
763
764 if (*p & mask)
765 return ret + __ffs64(*p & mask) - offset;
766
767 p = prev_word(p);
768 nr_key_bits -= bits;
769 ret += bits;
770 offset = 0;
771 }
772
773 return 0;
774 }
775
776 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
777
778 #define I(_x) (*(out)++ = (_x))
779 #define I1(i0) I(i0)
780 #define I2(i0, i1) (I1(i0), I(i1))
781 #define I3(i0, i1, i2) (I2(i0, i1), I(i2))
782 #define I4(i0, i1, i2, i3) (I3(i0, i1, i2), I(i3))
783 #define I5(i0, i1, i2, i3, i4) (I4(i0, i1, i2, i3), I(i4))
784
compile_bkey_field(const struct bkey_format * format,u8 * out,enum bch_bkey_fields field,unsigned dst_offset,unsigned dst_size,bool * eax_zeroed)785 static u8 *compile_bkey_field(const struct bkey_format *format, u8 *out,
786 enum bch_bkey_fields field,
787 unsigned dst_offset, unsigned dst_size,
788 bool *eax_zeroed)
789 {
790 unsigned bits = format->bits_per_field[field];
791 u64 offset = le64_to_cpu(format->field_offset[field]);
792 unsigned i, byte, bit_offset, align, shl, shr;
793
794 if (!bits && !offset) {
795 if (!*eax_zeroed) {
796 /* xor eax, eax */
797 I2(0x31, 0xc0);
798 }
799
800 *eax_zeroed = true;
801 goto set_field;
802 }
803
804 if (!bits) {
805 /* just return offset: */
806
807 switch (dst_size) {
808 case 8:
809 if (offset > S32_MAX) {
810 /* mov [rdi + dst_offset], offset */
811 I3(0xc7, 0x47, dst_offset);
812 memcpy(out, &offset, 4);
813 out += 4;
814
815 I3(0xc7, 0x47, dst_offset + 4);
816 memcpy(out, (void *) &offset + 4, 4);
817 out += 4;
818 } else {
819 /* mov [rdi + dst_offset], offset */
820 /* sign extended */
821 I4(0x48, 0xc7, 0x47, dst_offset);
822 memcpy(out, &offset, 4);
823 out += 4;
824 }
825 break;
826 case 4:
827 /* mov [rdi + dst_offset], offset */
828 I3(0xc7, 0x47, dst_offset);
829 memcpy(out, &offset, 4);
830 out += 4;
831 break;
832 default:
833 BUG();
834 }
835
836 return out;
837 }
838
839 bit_offset = format->key_u64s * 64;
840 for (i = 0; i <= field; i++)
841 bit_offset -= format->bits_per_field[i];
842
843 byte = bit_offset / 8;
844 bit_offset -= byte * 8;
845
846 *eax_zeroed = false;
847
848 if (bit_offset == 0 && bits == 8) {
849 /* movzx eax, BYTE PTR [rsi + imm8] */
850 I4(0x0f, 0xb6, 0x46, byte);
851 } else if (bit_offset == 0 && bits == 16) {
852 /* movzx eax, WORD PTR [rsi + imm8] */
853 I4(0x0f, 0xb7, 0x46, byte);
854 } else if (bit_offset + bits <= 32) {
855 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
856 byte -= align;
857 bit_offset += align * 8;
858
859 BUG_ON(bit_offset + bits > 32);
860
861 /* mov eax, [rsi + imm8] */
862 I3(0x8b, 0x46, byte);
863
864 if (bit_offset) {
865 /* shr eax, imm8 */
866 I3(0xc1, 0xe8, bit_offset);
867 }
868
869 if (bit_offset + bits < 32) {
870 unsigned mask = ~0U >> (32 - bits);
871
872 /* and eax, imm32 */
873 I1(0x25);
874 memcpy(out, &mask, 4);
875 out += 4;
876 }
877 } else if (bit_offset + bits <= 64) {
878 align = min(8 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 7);
879 byte -= align;
880 bit_offset += align * 8;
881
882 BUG_ON(bit_offset + bits > 64);
883
884 /* mov rax, [rsi + imm8] */
885 I4(0x48, 0x8b, 0x46, byte);
886
887 shl = 64 - bit_offset - bits;
888 shr = bit_offset + shl;
889
890 if (shl) {
891 /* shl rax, imm8 */
892 I4(0x48, 0xc1, 0xe0, shl);
893 }
894
895 if (shr) {
896 /* shr rax, imm8 */
897 I4(0x48, 0xc1, 0xe8, shr);
898 }
899 } else {
900 align = min(4 - DIV_ROUND_UP(bit_offset + bits, 8), byte & 3);
901 byte -= align;
902 bit_offset += align * 8;
903
904 BUG_ON(bit_offset + bits > 96);
905
906 /* mov rax, [rsi + byte] */
907 I4(0x48, 0x8b, 0x46, byte);
908
909 /* mov edx, [rsi + byte + 8] */
910 I3(0x8b, 0x56, byte + 8);
911
912 /* bits from next word: */
913 shr = bit_offset + bits - 64;
914 BUG_ON(shr > bit_offset);
915
916 /* shr rax, bit_offset */
917 I4(0x48, 0xc1, 0xe8, shr);
918
919 /* shl rdx, imm8 */
920 I4(0x48, 0xc1, 0xe2, 64 - shr);
921
922 /* or rax, rdx */
923 I3(0x48, 0x09, 0xd0);
924
925 shr = bit_offset - shr;
926
927 if (shr) {
928 /* shr rax, imm8 */
929 I4(0x48, 0xc1, 0xe8, shr);
930 }
931 }
932
933 /* rax += offset: */
934 if (offset > S32_MAX) {
935 /* mov rdx, imm64 */
936 I2(0x48, 0xba);
937 memcpy(out, &offset, 8);
938 out += 8;
939 /* add %rdx, %rax */
940 I3(0x48, 0x01, 0xd0);
941 } else if (offset + (~0ULL >> (64 - bits)) > U32_MAX) {
942 /* add rax, imm32 */
943 I2(0x48, 0x05);
944 memcpy(out, &offset, 4);
945 out += 4;
946 } else if (offset) {
947 /* add eax, imm32 */
948 I1(0x05);
949 memcpy(out, &offset, 4);
950 out += 4;
951 }
952 set_field:
953 switch (dst_size) {
954 case 8:
955 /* mov [rdi + dst_offset], rax */
956 I4(0x48, 0x89, 0x47, dst_offset);
957 break;
958 case 4:
959 /* mov [rdi + dst_offset], eax */
960 I3(0x89, 0x47, dst_offset);
961 break;
962 default:
963 BUG();
964 }
965
966 return out;
967 }
968
bch2_compile_bkey_format(const struct bkey_format * format,void * _out)969 int bch2_compile_bkey_format(const struct bkey_format *format, void *_out)
970 {
971 bool eax_zeroed = false;
972 u8 *out = _out;
973
974 /*
975 * rdi: dst - unpacked key
976 * rsi: src - packed key
977 */
978
979 /* k->u64s, k->format, k->type */
980
981 /* mov eax, [rsi] */
982 I2(0x8b, 0x06);
983
984 /* add eax, BKEY_U64s - format->key_u64s */
985 I5(0x05, BKEY_U64s - format->key_u64s, KEY_FORMAT_CURRENT, 0, 0);
986
987 /* and eax, imm32: mask out k->pad: */
988 I5(0x25, 0xff, 0xff, 0xff, 0);
989
990 /* mov [rdi], eax */
991 I2(0x89, 0x07);
992
993 #define x(id, field) \
994 out = compile_bkey_field(format, out, id, \
995 offsetof(struct bkey, field), \
996 sizeof(((struct bkey *) NULL)->field), \
997 &eax_zeroed);
998 bkey_fields()
999 #undef x
1000
1001 /* retq */
1002 I1(0xc3);
1003
1004 return (void *) out - _out;
1005 }
1006
1007 #else
1008 #endif
1009
1010 __pure
__bch2_bkey_cmp_packed_format_checked(const struct bkey_packed * l,const struct bkey_packed * r,const struct btree * b)1011 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *l,
1012 const struct bkey_packed *r,
1013 const struct btree *b)
1014 {
1015 return __bch2_bkey_cmp_packed_format_checked_inlined(l, r, b);
1016 }
1017
1018 __pure __flatten
__bch2_bkey_cmp_left_packed_format_checked(const struct btree * b,const struct bkey_packed * l,const struct bpos * r)1019 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *b,
1020 const struct bkey_packed *l,
1021 const struct bpos *r)
1022 {
1023 return bpos_cmp(bkey_unpack_pos_format_checked(b, l), *r);
1024 }
1025
1026 __pure __flatten
bch2_bkey_cmp_packed(const struct btree * b,const struct bkey_packed * l,const struct bkey_packed * r)1027 int bch2_bkey_cmp_packed(const struct btree *b,
1028 const struct bkey_packed *l,
1029 const struct bkey_packed *r)
1030 {
1031 return bch2_bkey_cmp_packed_inlined(b, l, r);
1032 }
1033
1034 __pure __flatten
__bch2_bkey_cmp_left_packed(const struct btree * b,const struct bkey_packed * l,const struct bpos * r)1035 int __bch2_bkey_cmp_left_packed(const struct btree *b,
1036 const struct bkey_packed *l,
1037 const struct bpos *r)
1038 {
1039 const struct bkey *l_unpacked;
1040
1041 return unlikely(l_unpacked = packed_to_bkey_c(l))
1042 ? bpos_cmp(l_unpacked->p, *r)
1043 : __bch2_bkey_cmp_left_packed_format_checked(b, l, r);
1044 }
1045
bch2_bpos_swab(struct bpos * p)1046 void bch2_bpos_swab(struct bpos *p)
1047 {
1048 u8 *l = (u8 *) p;
1049 u8 *h = ((u8 *) &p[1]) - 1;
1050
1051 while (l < h) {
1052 swap(*l, *h);
1053 l++;
1054 --h;
1055 }
1056 }
1057
bch2_bkey_swab_key(const struct bkey_format * _f,struct bkey_packed * k)1058 void bch2_bkey_swab_key(const struct bkey_format *_f, struct bkey_packed *k)
1059 {
1060 const struct bkey_format *f = bkey_packed(k) ? _f : &bch2_bkey_format_current;
1061 u8 *l = k->key_start;
1062 u8 *h = (u8 *) ((u64 *) k->_data + f->key_u64s) - 1;
1063
1064 while (l < h) {
1065 swap(*l, *h);
1066 l++;
1067 --h;
1068 }
1069 }
1070
1071 #ifdef CONFIG_BCACHEFS_DEBUG
bch2_bkey_pack_test(void)1072 void bch2_bkey_pack_test(void)
1073 {
1074 struct bkey t = KEY(4134ULL, 1250629070527416633ULL, 0);
1075 struct bkey_packed p;
1076
1077 struct bkey_format test_format = {
1078 .key_u64s = 3,
1079 .nr_fields = BKEY_NR_FIELDS,
1080 .bits_per_field = {
1081 13,
1082 64,
1083 32,
1084 },
1085 };
1086
1087 struct unpack_state in_s =
1088 unpack_state_init(&bch2_bkey_format_current, (void *) &t);
1089 struct pack_state out_s = pack_state_init(&test_format, &p);
1090 unsigned i;
1091
1092 for (i = 0; i < out_s.format->nr_fields; i++) {
1093 u64 a, v = get_inc_field(&in_s, i);
1094
1095 switch (i) {
1096 #define x(id, field) case id: a = t.field; break;
1097 bkey_fields()
1098 #undef x
1099 default:
1100 BUG();
1101 }
1102
1103 if (a != v)
1104 panic("got %llu actual %llu i %u\n", v, a, i);
1105
1106 if (!set_inc_field(&out_s, i, v))
1107 panic("failed at %u\n", i);
1108 }
1109
1110 BUG_ON(!bch2_bkey_pack_key(&p, &t, &test_format));
1111 }
1112 #endif
1113