1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_H
3 #define _BCACHEFS_BKEY_H
6 #include "bcachefs_format.h"
8 #include "btree_types.h"
13 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
16 void bch2_bkey_packed_to_binary_text(struct printbuf *,
17 const struct bkey_format *,
18 const struct bkey_packed *);
20 /* bkey with split value, const */
23 const struct bch_val *v;
26 /* bkey with split value */
37 #define bkey_next(_k) vstruct_next(_k)
39 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
41 static inline size_t bkey_val_bytes(const struct bkey *k)
43 return bkey_val_u64s(k) * sizeof(u64);
46 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
48 unsigned u64s = BKEY_U64s + val_u64s;
50 BUG_ON(u64s > U8_MAX);
54 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
56 set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
59 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
61 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
63 #define bkey_whiteout(_k) \
64 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
73 #define bkey_lr_packed(_l, _r) \
74 ((_l)->format + ((_r)->format << 1))
76 #define bkey_copy(_dst, _src) \
78 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
79 !type_is(_dst, struct bkey_packed *)); \
80 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
81 !type_is(_src, struct bkey_packed *)); \
82 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
83 (u64 *) (_dst) < (u64 *) (_src) + \
84 ((struct bkey *) (_src))->u64s); \
86 memcpy_u64s_small((_dst), (_src), \
87 ((struct bkey *) (_src))->u64s); \
92 struct bkey_format_state {
93 u64 field_min[BKEY_NR_FIELDS];
94 u64 field_max[BKEY_NR_FIELDS];
97 void bch2_bkey_format_init(struct bkey_format_state *);
98 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
99 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
100 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
101 const char *bch2_bkey_format_validate(struct bkey_format *);
104 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
105 const struct bkey_packed *,
106 const struct bkey_packed *);
108 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
111 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
112 const struct bkey_packed *,
113 const struct btree *);
116 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
117 const struct bkey_packed *,
118 const struct bpos *);
121 int bch2_bkey_cmp_packed(const struct btree *,
122 const struct bkey_packed *,
123 const struct bkey_packed *);
126 int __bch2_bkey_cmp_left_packed(const struct btree *,
127 const struct bkey_packed *,
128 const struct bpos *);
131 int bkey_cmp_left_packed(const struct btree *b,
132 const struct bkey_packed *l, const struct bpos *r)
134 return __bch2_bkey_cmp_left_packed(b, l, r);
138 * The compiler generates better code when we pass bpos by ref, but it's often
139 * enough terribly convenient to pass it by val... as much as I hate c++, const
140 * ref would be nice here:
143 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
144 const struct bkey_packed *l,
147 return bkey_cmp_left_packed(b, l, &r);
150 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
152 return cmp_int(l.inode, r.inode) ?:
153 cmp_int(l.offset, r.offset) ?:
154 cmp_int(l.snapshot, r.snapshot);
157 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
159 return cmp_int(l.inode, r.inode) ?:
160 cmp_int(l.offset, r.offset);
163 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
165 return bpos_cmp(l, r) < 0 ? l : r;
168 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
170 return bpos_cmp(l, r) > 0 ? l : r;
173 void bch2_bpos_swab(struct bpos *);
174 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
176 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
178 return cmp_int(l.hi, r.hi) ?:
182 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
183 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
185 static __always_inline int bversion_zero(struct bversion v)
187 return !bversion_cmp(v, ZERO_VERSION);
190 #ifdef CONFIG_BCACHEFS_DEBUG
191 /* statement expressions confusing unlikely()? */
192 #define bkey_packed(_k) \
193 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
194 (_k)->format != KEY_FORMAT_CURRENT; })
196 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
200 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
202 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
204 return (struct bkey_packed *) k;
207 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
209 return (const struct bkey_packed *) k;
212 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
214 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
217 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
219 return bkey_packed(k) ? NULL : (const struct bkey *) k;
222 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
224 return format->bits_per_field[BKEY_FIELD_INODE] +
225 format->bits_per_field[BKEY_FIELD_OFFSET] +
226 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
229 static inline struct bpos bpos_successor(struct bpos p)
239 static inline struct bpos bpos_predecessor(struct bpos p)
249 static inline struct bpos bpos_nosnap_successor(struct bpos p)
260 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
271 static inline u64 bkey_start_offset(const struct bkey *k)
273 return k->p.offset - k->size;
276 static inline struct bpos bkey_start_pos(const struct bkey *k)
278 return (struct bpos) {
280 .offset = bkey_start_offset(k),
281 .snapshot = k->p.snapshot,
287 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
288 const struct bkey_packed *k)
290 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
292 EBUG_ON(k->u64s < ret);
296 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
297 const struct bkey_packed *k)
299 return bkeyp_key_u64s(format, k) * sizeof(u64);
302 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
303 const struct bkey_packed *k)
305 return k->u64s - bkeyp_key_u64s(format, k);
308 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
309 const struct bkey_packed *k)
311 return bkeyp_val_u64s(format, k) * sizeof(u64);
314 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
315 struct bkey_packed *k, unsigned val_u64s)
317 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
320 #define bkeyp_val(_format, _k) \
321 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
323 extern const struct bkey_format bch2_bkey_format_current;
325 bool bch2_bkey_transform(const struct bkey_format *,
326 struct bkey_packed *,
327 const struct bkey_format *,
328 const struct bkey_packed *);
330 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
331 const struct bkey_packed *);
333 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
334 struct bpos __bkey_unpack_pos(const struct bkey_format *,
335 const struct bkey_packed *);
338 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
339 const struct bkey_format *);
341 enum bkey_pack_pos_ret {
343 BKEY_PACK_POS_SMALLER,
347 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
348 const struct btree *);
350 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
351 const struct btree *b)
353 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
356 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
357 const struct bkey_packed *);
358 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
359 const struct bkey_format *);
361 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
364 __bkey_unpack_key_format_checked(const struct btree *b,
366 const struct bkey_packed *src)
368 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
369 compiled_unpack_fn unpack_fn = b->aux_data;
372 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
373 bch2_expensive_debug_checks) {
374 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
376 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
379 *dst = __bch2_bkey_unpack_key(&b->format, src);
383 static inline struct bkey
384 bkey_unpack_key_format_checked(const struct btree *b,
385 const struct bkey_packed *src)
389 __bkey_unpack_key_format_checked(b, &dst, src);
393 static inline void __bkey_unpack_key(const struct btree *b,
395 const struct bkey_packed *src)
397 if (likely(bkey_packed(src)))
398 __bkey_unpack_key_format_checked(b, dst, src);
400 *dst = *packed_to_bkey_c(src);
404 * bkey_unpack_key -- unpack just the key, not the value
406 static inline struct bkey bkey_unpack_key(const struct btree *b,
407 const struct bkey_packed *src)
409 return likely(bkey_packed(src))
410 ? bkey_unpack_key_format_checked(b, src)
411 : *packed_to_bkey_c(src);
414 static inline struct bpos
415 bkey_unpack_pos_format_checked(const struct btree *b,
416 const struct bkey_packed *src)
418 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
419 return bkey_unpack_key_format_checked(b, src).p;
421 return __bkey_unpack_pos(&b->format, src);
425 static inline struct bpos bkey_unpack_pos(const struct btree *b,
426 const struct bkey_packed *src)
428 return likely(bkey_packed(src))
429 ? bkey_unpack_pos_format_checked(b, src)
430 : packed_to_bkey_c(src)->p;
433 /* Disassembled bkeys */
435 static inline struct bkey_s_c bkey_disassemble(struct btree *b,
436 const struct bkey_packed *k,
439 __bkey_unpack_key(b, u, k);
441 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
444 /* non const version: */
445 static inline struct bkey_s __bkey_disassemble(struct btree *b,
446 struct bkey_packed *k,
449 __bkey_unpack_key(b, u, k);
451 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
454 static inline u64 bkey_field_max(const struct bkey_format *f,
455 enum bch_bkey_fields nr)
457 return f->bits_per_field[nr] < 64
458 ? (le64_to_cpu(f->field_offset[nr]) +
459 ~(~0ULL << f->bits_per_field[nr]))
463 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
465 int bch2_compile_bkey_format(const struct bkey_format *, void *);
469 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
470 void *out) { return 0; }
474 static inline void bkey_reassemble(struct bkey_i *dst,
478 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
481 #define bkey_s_null ((struct bkey_s) { .k = NULL })
482 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
484 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
485 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
487 static inline struct bkey_s bkey_to_s(struct bkey *k)
489 return (struct bkey_s) { .k = k, .v = NULL };
492 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
494 return (struct bkey_s_c) { .k = k, .v = NULL };
497 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
499 return (struct bkey_s) { .k = &k->k, .v = &k->v };
502 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
504 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
508 * For a given type of value (e.g. struct bch_extent), generates the types for
509 * bkey + bch_extent - inline, split, split const - and also all the conversion
510 * functions, which also check that the value is of the correct type.
512 * We use anonymous unions for upcasting - e.g. converting from e.g. a
513 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
516 #define x(name, ...) \
517 struct bkey_i_##name { \
522 struct bch_##name v; \
525 struct bkey_s_c_##name { \
528 const struct bkey *k; \
529 const struct bch_##name *v; \
531 struct bkey_s_c s_c; \
535 struct bkey_s_##name { \
539 struct bch_##name *v; \
541 struct bkey_s_c_##name c; \
543 struct bkey_s_c s_c; \
547 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
549 EBUG_ON(k->k.type != KEY_TYPE_##name); \
550 return container_of(&k->k, struct bkey_i_##name, k); \
553 static inline const struct bkey_i_##name * \
554 bkey_i_to_##name##_c(const struct bkey_i *k) \
556 EBUG_ON(k->k.type != KEY_TYPE_##name); \
557 return container_of(&k->k, struct bkey_i_##name, k); \
560 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
562 EBUG_ON(k.k->type != KEY_TYPE_##name); \
563 return (struct bkey_s_##name) { \
565 .v = container_of(k.v, struct bch_##name, v), \
569 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
571 EBUG_ON(k.k->type != KEY_TYPE_##name); \
572 return (struct bkey_s_c_##name) { \
574 .v = container_of(k.v, struct bch_##name, v), \
578 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
580 return (struct bkey_s_##name) { \
586 static inline struct bkey_s_c_##name \
587 name##_i_to_s_c(const struct bkey_i_##name *k) \
589 return (struct bkey_s_c_##name) { \
595 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
597 EBUG_ON(k->k.type != KEY_TYPE_##name); \
598 return (struct bkey_s_##name) { \
600 .v = container_of(&k->v, struct bch_##name, v), \
604 static inline struct bkey_s_c_##name \
605 bkey_i_to_s_c_##name(const struct bkey_i *k) \
607 EBUG_ON(k->k.type != KEY_TYPE_##name); \
608 return (struct bkey_s_c_##name) { \
610 .v = container_of(&k->v, struct bch_##name, v), \
614 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
616 struct bkey_i_##name *k = \
617 container_of(&_k->k, struct bkey_i_##name, k); \
620 memset(&k->v, 0, sizeof(k->v)); \
621 k->k.type = KEY_TYPE_##name; \
622 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
630 /* byte order helpers */
632 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
634 static inline unsigned high_word_offset(const struct bkey_format *f)
636 return f->key_u64s - 1;
639 #define high_bit_offset 0
640 #define nth_word(p, n) ((p) - (n))
642 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
644 static inline unsigned high_word_offset(const struct bkey_format *f)
649 #define high_bit_offset KEY_PACKED_BITS_START
650 #define nth_word(p, n) ((p) + (n))
653 #error edit for your odd byteorder.
656 #define high_word(f, k) ((k)->_data + high_word_offset(f))
657 #define next_word(p) nth_word(p, 1)
658 #define prev_word(p) nth_word(p, -1)
660 #ifdef CONFIG_BCACHEFS_DEBUG
661 void bch2_bkey_pack_test(void);
663 static inline void bch2_bkey_pack_test(void) {}
666 #endif /* _BCACHEFS_BKEY_H */