1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_H
3 #define _BCACHEFS_BKEY_H
4
5 #include <linux/bug.h>
6 #include "bcachefs_format.h"
7 #include "bkey_types.h"
8 #include "btree_types.h"
9 #include "util.h"
10 #include "vstructs.h"
11
12 #if 0
13
14 /*
15 * compiled unpack functions are disabled, pending a new interface for
16 * dynamically allocating executable memory:
17 */
18
19 #ifdef CONFIG_X86_64
20 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
21 #endif
22 #endif
23
24 void bch2_bkey_packed_to_binary_text(struct printbuf *,
25 const struct bkey_format *,
26 const struct bkey_packed *);
27
28 enum bkey_lr_packed {
29 BKEY_PACKED_BOTH,
30 BKEY_PACKED_RIGHT,
31 BKEY_PACKED_LEFT,
32 BKEY_PACKED_NONE,
33 };
34
35 #define bkey_lr_packed(_l, _r) \
36 ((_l)->format + ((_r)->format << 1))
37
bkey_p_copy(struct bkey_packed * dst,const struct bkey_packed * src)38 static inline void bkey_p_copy(struct bkey_packed *dst, const struct bkey_packed *src)
39 {
40 memcpy_u64s_small(dst, src, src->u64s);
41 }
42
bkey_copy(struct bkey_i * dst,const struct bkey_i * src)43 static inline void bkey_copy(struct bkey_i *dst, const struct bkey_i *src)
44 {
45 memcpy_u64s_small(dst, src, src->k.u64s);
46 }
47
48 struct btree;
49
50 __pure
51 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
52 const struct bkey_packed *,
53 const struct bkey_packed *);
54 __pure
55 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
56
57 __pure
58 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
59 const struct bkey_packed *,
60 const struct btree *);
61
62 __pure
63 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
64 const struct bkey_packed *,
65 const struct bpos *);
66
67 __pure
68 int bch2_bkey_cmp_packed(const struct btree *,
69 const struct bkey_packed *,
70 const struct bkey_packed *);
71
72 __pure
73 int __bch2_bkey_cmp_left_packed(const struct btree *,
74 const struct bkey_packed *,
75 const struct bpos *);
76
77 static inline __pure
bkey_cmp_left_packed(const struct btree * b,const struct bkey_packed * l,const struct bpos * r)78 int bkey_cmp_left_packed(const struct btree *b,
79 const struct bkey_packed *l, const struct bpos *r)
80 {
81 return __bch2_bkey_cmp_left_packed(b, l, r);
82 }
83
84 /*
85 * The compiler generates better code when we pass bpos by ref, but it's often
86 * enough terribly convenient to pass it by val... as much as I hate c++, const
87 * ref would be nice here:
88 */
89 __pure __flatten
bkey_cmp_left_packed_byval(const struct btree * b,const struct bkey_packed * l,struct bpos r)90 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
91 const struct bkey_packed *l,
92 struct bpos r)
93 {
94 return bkey_cmp_left_packed(b, l, &r);
95 }
96
bpos_eq(struct bpos l,struct bpos r)97 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
98 {
99 return !((l.inode ^ r.inode) |
100 (l.offset ^ r.offset) |
101 (l.snapshot ^ r.snapshot));
102 }
103
bpos_lt(struct bpos l,struct bpos r)104 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
105 {
106 return l.inode != r.inode ? l.inode < r.inode :
107 l.offset != r.offset ? l.offset < r.offset :
108 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
109 }
110
bpos_le(struct bpos l,struct bpos r)111 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
112 {
113 return l.inode != r.inode ? l.inode < r.inode :
114 l.offset != r.offset ? l.offset < r.offset :
115 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
116 }
117
bpos_gt(struct bpos l,struct bpos r)118 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
119 {
120 return bpos_lt(r, l);
121 }
122
bpos_ge(struct bpos l,struct bpos r)123 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
124 {
125 return bpos_le(r, l);
126 }
127
bpos_cmp(struct bpos l,struct bpos r)128 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
129 {
130 return cmp_int(l.inode, r.inode) ?:
131 cmp_int(l.offset, r.offset) ?:
132 cmp_int(l.snapshot, r.snapshot);
133 }
134
bpos_min(struct bpos l,struct bpos r)135 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
136 {
137 return bpos_lt(l, r) ? l : r;
138 }
139
bpos_max(struct bpos l,struct bpos r)140 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
141 {
142 return bpos_gt(l, r) ? l : r;
143 }
144
bkey_eq(struct bpos l,struct bpos r)145 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
146 {
147 return !((l.inode ^ r.inode) |
148 (l.offset ^ r.offset));
149 }
150
bkey_lt(struct bpos l,struct bpos r)151 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
152 {
153 return l.inode != r.inode
154 ? l.inode < r.inode
155 : l.offset < r.offset;
156 }
157
bkey_le(struct bpos l,struct bpos r)158 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
159 {
160 return l.inode != r.inode
161 ? l.inode < r.inode
162 : l.offset <= r.offset;
163 }
164
bkey_gt(struct bpos l,struct bpos r)165 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
166 {
167 return bkey_lt(r, l);
168 }
169
bkey_ge(struct bpos l,struct bpos r)170 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
171 {
172 return bkey_le(r, l);
173 }
174
bkey_cmp(struct bpos l,struct bpos r)175 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
176 {
177 return cmp_int(l.inode, r.inode) ?:
178 cmp_int(l.offset, r.offset);
179 }
180
bkey_min(struct bpos l,struct bpos r)181 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
182 {
183 return bkey_lt(l, r) ? l : r;
184 }
185
bkey_max(struct bpos l,struct bpos r)186 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
187 {
188 return bkey_gt(l, r) ? l : r;
189 }
190
bkey_and_val_eq(struct bkey_s_c l,struct bkey_s_c r)191 static inline bool bkey_and_val_eq(struct bkey_s_c l, struct bkey_s_c r)
192 {
193 return bpos_eq(l.k->p, r.k->p) &&
194 l.k->size == r.k->size &&
195 bkey_bytes(l.k) == bkey_bytes(r.k) &&
196 !memcmp(l.v, r.v, bkey_val_bytes(l.k));
197 }
198
199 void bch2_bpos_swab(struct bpos *);
200 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
201
bversion_cmp(struct bversion l,struct bversion r)202 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
203 {
204 return cmp_int(l.hi, r.hi) ?:
205 cmp_int(l.lo, r.lo);
206 }
207
208 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
209 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
210
bversion_zero(struct bversion v)211 static __always_inline bool bversion_zero(struct bversion v)
212 {
213 return bversion_cmp(v, ZERO_VERSION) == 0;
214 }
215
216 #ifdef CONFIG_BCACHEFS_DEBUG
217 /* statement expressions confusing unlikely()? */
218 #define bkey_packed(_k) \
219 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
220 (_k)->format != KEY_FORMAT_CURRENT; })
221 #else
222 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
223 #endif
224
225 /*
226 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
227 */
bkey_to_packed(struct bkey_i * k)228 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
229 {
230 return (struct bkey_packed *) k;
231 }
232
bkey_to_packed_c(const struct bkey_i * k)233 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
234 {
235 return (const struct bkey_packed *) k;
236 }
237
packed_to_bkey(struct bkey_packed * k)238 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
239 {
240 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
241 }
242
packed_to_bkey_c(const struct bkey_packed * k)243 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
244 {
245 return bkey_packed(k) ? NULL : (const struct bkey *) k;
246 }
247
bkey_format_key_bits(const struct bkey_format * format)248 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
249 {
250 return format->bits_per_field[BKEY_FIELD_INODE] +
251 format->bits_per_field[BKEY_FIELD_OFFSET] +
252 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
253 }
254
bpos_successor(struct bpos p)255 static inline struct bpos bpos_successor(struct bpos p)
256 {
257 if (!++p.snapshot &&
258 !++p.offset &&
259 !++p.inode)
260 BUG();
261
262 return p;
263 }
264
bpos_predecessor(struct bpos p)265 static inline struct bpos bpos_predecessor(struct bpos p)
266 {
267 if (!p.snapshot-- &&
268 !p.offset-- &&
269 !p.inode--)
270 BUG();
271
272 return p;
273 }
274
bpos_nosnap_successor(struct bpos p)275 static inline struct bpos bpos_nosnap_successor(struct bpos p)
276 {
277 p.snapshot = 0;
278
279 if (!++p.offset &&
280 !++p.inode)
281 BUG();
282
283 return p;
284 }
285
bpos_nosnap_predecessor(struct bpos p)286 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
287 {
288 p.snapshot = 0;
289
290 if (!p.offset-- &&
291 !p.inode--)
292 BUG();
293
294 return p;
295 }
296
bkey_start_offset(const struct bkey * k)297 static inline u64 bkey_start_offset(const struct bkey *k)
298 {
299 return k->p.offset - k->size;
300 }
301
bkey_start_pos(const struct bkey * k)302 static inline struct bpos bkey_start_pos(const struct bkey *k)
303 {
304 return (struct bpos) {
305 .inode = k->p.inode,
306 .offset = bkey_start_offset(k),
307 .snapshot = k->p.snapshot,
308 };
309 }
310
311 /* Packed helpers */
312
bkeyp_key_u64s(const struct bkey_format * format,const struct bkey_packed * k)313 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
314 const struct bkey_packed *k)
315 {
316 return bkey_packed(k) ? format->key_u64s : BKEY_U64s;
317 }
318
bkeyp_u64s_valid(const struct bkey_format * f,const struct bkey_packed * k)319 static inline bool bkeyp_u64s_valid(const struct bkey_format *f,
320 const struct bkey_packed *k)
321 {
322 return ((unsigned) k->u64s - bkeyp_key_u64s(f, k) <= U8_MAX - BKEY_U64s);
323 }
324
bkeyp_key_bytes(const struct bkey_format * format,const struct bkey_packed * k)325 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
326 const struct bkey_packed *k)
327 {
328 return bkeyp_key_u64s(format, k) * sizeof(u64);
329 }
330
bkeyp_val_u64s(const struct bkey_format * format,const struct bkey_packed * k)331 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
332 const struct bkey_packed *k)
333 {
334 return k->u64s - bkeyp_key_u64s(format, k);
335 }
336
bkeyp_val_bytes(const struct bkey_format * format,const struct bkey_packed * k)337 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
338 const struct bkey_packed *k)
339 {
340 return bkeyp_val_u64s(format, k) * sizeof(u64);
341 }
342
set_bkeyp_val_u64s(const struct bkey_format * format,struct bkey_packed * k,unsigned val_u64s)343 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
344 struct bkey_packed *k, unsigned val_u64s)
345 {
346 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
347 }
348
349 #define bkeyp_val(_format, _k) \
350 ((struct bch_val *) ((u64 *) (_k)->_data + bkeyp_key_u64s(_format, _k)))
351
352 extern const struct bkey_format bch2_bkey_format_current;
353
354 bool bch2_bkey_transform(const struct bkey_format *,
355 struct bkey_packed *,
356 const struct bkey_format *,
357 const struct bkey_packed *);
358
359 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
360 const struct bkey_packed *);
361
362 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
363 struct bpos __bkey_unpack_pos(const struct bkey_format *,
364 const struct bkey_packed *);
365 #endif
366
367 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
368 const struct bkey_format *);
369
370 enum bkey_pack_pos_ret {
371 BKEY_PACK_POS_EXACT,
372 BKEY_PACK_POS_SMALLER,
373 BKEY_PACK_POS_FAIL,
374 };
375
376 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
377 const struct btree *);
378
bkey_pack_pos(struct bkey_packed * out,struct bpos in,const struct btree * b)379 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
380 const struct btree *b)
381 {
382 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
383 }
384
385 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
386 const struct bkey_packed *);
387 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
388 const struct bkey_format *);
389
390 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
391
392 static inline void
__bkey_unpack_key_format_checked(const struct btree * b,struct bkey * dst,const struct bkey_packed * src)393 __bkey_unpack_key_format_checked(const struct btree *b,
394 struct bkey *dst,
395 const struct bkey_packed *src)
396 {
397 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
398 compiled_unpack_fn unpack_fn = b->aux_data;
399 unpack_fn(dst, src);
400
401 if (static_branch_unlikely(&bch2_debug_check_bkey_unpack)) {
402 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
403
404 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
405 }
406 } else {
407 *dst = __bch2_bkey_unpack_key(&b->format, src);
408 }
409 }
410
411 static inline struct bkey
bkey_unpack_key_format_checked(const struct btree * b,const struct bkey_packed * src)412 bkey_unpack_key_format_checked(const struct btree *b,
413 const struct bkey_packed *src)
414 {
415 struct bkey dst;
416
417 __bkey_unpack_key_format_checked(b, &dst, src);
418 return dst;
419 }
420
__bkey_unpack_key(const struct btree * b,struct bkey * dst,const struct bkey_packed * src)421 static inline void __bkey_unpack_key(const struct btree *b,
422 struct bkey *dst,
423 const struct bkey_packed *src)
424 {
425 if (likely(bkey_packed(src)))
426 __bkey_unpack_key_format_checked(b, dst, src);
427 else
428 *dst = *packed_to_bkey_c(src);
429 }
430
431 /**
432 * bkey_unpack_key -- unpack just the key, not the value
433 */
bkey_unpack_key(const struct btree * b,const struct bkey_packed * src)434 static inline struct bkey bkey_unpack_key(const struct btree *b,
435 const struct bkey_packed *src)
436 {
437 return likely(bkey_packed(src))
438 ? bkey_unpack_key_format_checked(b, src)
439 : *packed_to_bkey_c(src);
440 }
441
442 static inline struct bpos
bkey_unpack_pos_format_checked(const struct btree * b,const struct bkey_packed * src)443 bkey_unpack_pos_format_checked(const struct btree *b,
444 const struct bkey_packed *src)
445 {
446 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
447 return bkey_unpack_key_format_checked(b, src).p;
448 #else
449 return __bkey_unpack_pos(&b->format, src);
450 #endif
451 }
452
bkey_unpack_pos(const struct btree * b,const struct bkey_packed * src)453 static inline struct bpos bkey_unpack_pos(const struct btree *b,
454 const struct bkey_packed *src)
455 {
456 return likely(bkey_packed(src))
457 ? bkey_unpack_pos_format_checked(b, src)
458 : packed_to_bkey_c(src)->p;
459 }
460
461 /* Disassembled bkeys */
462
bkey_disassemble(const struct btree * b,const struct bkey_packed * k,struct bkey * u)463 static inline struct bkey_s_c bkey_disassemble(const struct btree *b,
464 const struct bkey_packed *k,
465 struct bkey *u)
466 {
467 __bkey_unpack_key(b, u, k);
468
469 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
470 }
471
472 /* non const version: */
__bkey_disassemble(const struct btree * b,struct bkey_packed * k,struct bkey * u)473 static inline struct bkey_s __bkey_disassemble(const struct btree *b,
474 struct bkey_packed *k,
475 struct bkey *u)
476 {
477 __bkey_unpack_key(b, u, k);
478
479 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
480 }
481
bkey_field_max(const struct bkey_format * f,enum bch_bkey_fields nr)482 static inline u64 bkey_field_max(const struct bkey_format *f,
483 enum bch_bkey_fields nr)
484 {
485 return f->bits_per_field[nr] < 64
486 ? (le64_to_cpu(f->field_offset[nr]) +
487 ~(~0ULL << f->bits_per_field[nr]))
488 : U64_MAX;
489 }
490
491 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
492
493 int bch2_compile_bkey_format(const struct bkey_format *, void *);
494
495 #else
496
bch2_compile_bkey_format(const struct bkey_format * format,void * out)497 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
498 void *out) { return 0; }
499
500 #endif
501
bkey_reassemble(struct bkey_i * dst,struct bkey_s_c src)502 static inline void bkey_reassemble(struct bkey_i *dst,
503 struct bkey_s_c src)
504 {
505 dst->k = *src.k;
506 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
507 }
508
509 /* byte order helpers */
510
511 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
512
high_word_offset(const struct bkey_format * f)513 static inline unsigned high_word_offset(const struct bkey_format *f)
514 {
515 return f->key_u64s - 1;
516 }
517
518 #define high_bit_offset 0
519 #define nth_word(p, n) ((p) - (n))
520
521 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
522
high_word_offset(const struct bkey_format * f)523 static inline unsigned high_word_offset(const struct bkey_format *f)
524 {
525 return 0;
526 }
527
528 #define high_bit_offset KEY_PACKED_BITS_START
529 #define nth_word(p, n) ((p) + (n))
530
531 #else
532 #error edit for your odd byteorder.
533 #endif
534
535 #define high_word(f, k) ((u64 *) (k)->_data + high_word_offset(f))
536 #define next_word(p) nth_word(p, 1)
537 #define prev_word(p) nth_word(p, -1)
538
539 #ifdef CONFIG_BCACHEFS_DEBUG
540 void bch2_bkey_pack_test(void);
541 #else
bch2_bkey_pack_test(void)542 static inline void bch2_bkey_pack_test(void) {}
543 #endif
544
545 #define bkey_fields() \
546 x(BKEY_FIELD_INODE, p.inode) \
547 x(BKEY_FIELD_OFFSET, p.offset) \
548 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
549 x(BKEY_FIELD_SIZE, size) \
550 x(BKEY_FIELD_VERSION_HI, bversion.hi) \
551 x(BKEY_FIELD_VERSION_LO, bversion.lo)
552
553 struct bkey_format_state {
554 u64 field_min[BKEY_NR_FIELDS];
555 u64 field_max[BKEY_NR_FIELDS];
556 };
557
558 void bch2_bkey_format_init(struct bkey_format_state *);
559
__bkey_format_add(struct bkey_format_state * s,unsigned field,u64 v)560 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
561 {
562 s->field_min[field] = min(s->field_min[field], v);
563 s->field_max[field] = max(s->field_max[field], v);
564 }
565
566 /*
567 * Changes @format so that @k can be successfully packed with @format
568 */
bch2_bkey_format_add_key(struct bkey_format_state * s,const struct bkey * k)569 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
570 {
571 #define x(id, field) __bkey_format_add(s, id, k->field);
572 bkey_fields()
573 #undef x
574 }
575
576 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
577 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
578
bch2_bkey_format_field_overflows(struct bkey_format * f,unsigned i)579 static inline bool bch2_bkey_format_field_overflows(struct bkey_format *f, unsigned i)
580 {
581 unsigned f_bits = f->bits_per_field[i];
582 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
583 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
584 u64 field_offset = le64_to_cpu(f->field_offset[i]);
585
586 if (f_bits > unpacked_bits)
587 return true;
588
589 if ((f_bits == unpacked_bits) && field_offset)
590 return true;
591
592 u64 f_mask = f_bits
593 ? ~((~0ULL << (f_bits - 1)) << 1)
594 : 0;
595
596 if (((field_offset + f_mask) & unpacked_mask) < field_offset)
597 return true;
598 return false;
599 }
600
601 int bch2_bkey_format_invalid(struct bch_fs *, struct bkey_format *,
602 enum bch_validate_flags, struct printbuf *);
603 void bch2_bkey_format_to_text(struct printbuf *, const struct bkey_format *);
604
605 #endif /* _BCACHEFS_BKEY_H */
606