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"
15 * compiled unpack functions are disabled, pending a new interface for
16 * dynamically allocating executable memory:
20 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
24 void bch2_bkey_packed_to_binary_text(struct printbuf *,
25 const struct bkey_format *,
26 const struct bkey_packed *);
28 /* bkey with split value, const */
31 const struct bch_val *v;
34 /* bkey with split value */
45 #define bkey_p_next(_k) vstruct_next(_k)
47 static inline struct bkey_i *bkey_next(struct bkey_i *k)
49 return (struct bkey_i *) (k->_data + k->k.u64s);
52 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
54 static inline size_t bkey_val_bytes(const struct bkey *k)
56 return bkey_val_u64s(k) * sizeof(u64);
59 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
61 unsigned u64s = BKEY_U64s + val_u64s;
63 BUG_ON(u64s > U8_MAX);
67 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
69 set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
72 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
74 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
76 #define bkey_whiteout(_k) \
77 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
86 #define bkey_lr_packed(_l, _r) \
87 ((_l)->format + ((_r)->format << 1))
89 #define bkey_copy(_dst, _src) \
91 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
92 !type_is(_dst, struct bkey_packed *)); \
93 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
94 !type_is(_src, struct bkey_packed *)); \
95 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
96 (u64 *) (_dst) < (u64 *) (_src) + \
97 ((struct bkey *) (_src))->u64s); \
99 memcpy_u64s_small((_dst), (_src), \
100 ((struct bkey *) (_src))->u64s); \
106 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
107 const struct bkey_packed *,
108 const struct bkey_packed *);
110 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
113 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
114 const struct bkey_packed *,
115 const struct btree *);
118 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
119 const struct bkey_packed *,
120 const struct bpos *);
123 int bch2_bkey_cmp_packed(const struct btree *,
124 const struct bkey_packed *,
125 const struct bkey_packed *);
128 int __bch2_bkey_cmp_left_packed(const struct btree *,
129 const struct bkey_packed *,
130 const struct bpos *);
133 int bkey_cmp_left_packed(const struct btree *b,
134 const struct bkey_packed *l, const struct bpos *r)
136 return __bch2_bkey_cmp_left_packed(b, l, r);
140 * The compiler generates better code when we pass bpos by ref, but it's often
141 * enough terribly convenient to pass it by val... as much as I hate c++, const
142 * ref would be nice here:
145 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
146 const struct bkey_packed *l,
149 return bkey_cmp_left_packed(b, l, &r);
152 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
154 return !((l.inode ^ r.inode) |
155 (l.offset ^ r.offset) |
156 (l.snapshot ^ r.snapshot));
159 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
161 return l.inode != r.inode ? l.inode < r.inode :
162 l.offset != r.offset ? l.offset < r.offset :
163 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
166 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
168 return l.inode != r.inode ? l.inode < r.inode :
169 l.offset != r.offset ? l.offset < r.offset :
170 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
173 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
175 return bpos_lt(r, l);
178 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
180 return bpos_le(r, l);
183 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
185 return cmp_int(l.inode, r.inode) ?:
186 cmp_int(l.offset, r.offset) ?:
187 cmp_int(l.snapshot, r.snapshot);
190 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
192 return bpos_lt(l, r) ? l : r;
195 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
197 return bpos_gt(l, r) ? l : r;
200 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
202 return !((l.inode ^ r.inode) |
203 (l.offset ^ r.offset));
206 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
208 return l.inode != r.inode
210 : l.offset < r.offset;
213 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
215 return l.inode != r.inode
217 : l.offset <= r.offset;
220 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
222 return bkey_lt(r, l);
225 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
227 return bkey_le(r, l);
230 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
232 return cmp_int(l.inode, r.inode) ?:
233 cmp_int(l.offset, r.offset);
236 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
238 return bkey_lt(l, r) ? l : r;
241 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
243 return bkey_gt(l, r) ? l : r;
246 void bch2_bpos_swab(struct bpos *);
247 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
249 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
251 return cmp_int(l.hi, r.hi) ?:
255 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
256 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
258 static __always_inline int bversion_zero(struct bversion v)
260 return !bversion_cmp(v, ZERO_VERSION);
263 #ifdef CONFIG_BCACHEFS_DEBUG
264 /* statement expressions confusing unlikely()? */
265 #define bkey_packed(_k) \
266 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
267 (_k)->format != KEY_FORMAT_CURRENT; })
269 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
273 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
275 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
277 return (struct bkey_packed *) k;
280 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
282 return (const struct bkey_packed *) k;
285 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
287 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
290 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
292 return bkey_packed(k) ? NULL : (const struct bkey *) k;
295 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
297 return format->bits_per_field[BKEY_FIELD_INODE] +
298 format->bits_per_field[BKEY_FIELD_OFFSET] +
299 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
302 static inline struct bpos bpos_successor(struct bpos p)
312 static inline struct bpos bpos_predecessor(struct bpos p)
322 static inline struct bpos bpos_nosnap_successor(struct bpos p)
333 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
344 static inline u64 bkey_start_offset(const struct bkey *k)
346 return k->p.offset - k->size;
349 static inline struct bpos bkey_start_pos(const struct bkey *k)
351 return (struct bpos) {
353 .offset = bkey_start_offset(k),
354 .snapshot = k->p.snapshot,
360 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
361 const struct bkey_packed *k)
363 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
365 EBUG_ON(k->u64s < ret);
369 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
370 const struct bkey_packed *k)
372 return bkeyp_key_u64s(format, k) * sizeof(u64);
375 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
376 const struct bkey_packed *k)
378 return k->u64s - bkeyp_key_u64s(format, k);
381 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
382 const struct bkey_packed *k)
384 return bkeyp_val_u64s(format, k) * sizeof(u64);
387 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
388 struct bkey_packed *k, unsigned val_u64s)
390 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
393 #define bkeyp_val(_format, _k) \
394 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
396 extern const struct bkey_format bch2_bkey_format_current;
398 bool bch2_bkey_transform(const struct bkey_format *,
399 struct bkey_packed *,
400 const struct bkey_format *,
401 const struct bkey_packed *);
403 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
404 const struct bkey_packed *);
406 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
407 struct bpos __bkey_unpack_pos(const struct bkey_format *,
408 const struct bkey_packed *);
411 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
412 const struct bkey_format *);
414 enum bkey_pack_pos_ret {
416 BKEY_PACK_POS_SMALLER,
420 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
421 const struct btree *);
423 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
424 const struct btree *b)
426 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
429 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
430 const struct bkey_packed *);
431 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
432 const struct bkey_format *);
434 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
437 __bkey_unpack_key_format_checked(const struct btree *b,
439 const struct bkey_packed *src)
441 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
442 compiled_unpack_fn unpack_fn = b->aux_data;
445 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
446 bch2_expensive_debug_checks) {
447 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
449 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
452 *dst = __bch2_bkey_unpack_key(&b->format, src);
456 static inline struct bkey
457 bkey_unpack_key_format_checked(const struct btree *b,
458 const struct bkey_packed *src)
462 __bkey_unpack_key_format_checked(b, &dst, src);
466 static inline void __bkey_unpack_key(const struct btree *b,
468 const struct bkey_packed *src)
470 if (likely(bkey_packed(src)))
471 __bkey_unpack_key_format_checked(b, dst, src);
473 *dst = *packed_to_bkey_c(src);
477 * bkey_unpack_key -- unpack just the key, not the value
479 static inline struct bkey bkey_unpack_key(const struct btree *b,
480 const struct bkey_packed *src)
482 return likely(bkey_packed(src))
483 ? bkey_unpack_key_format_checked(b, src)
484 : *packed_to_bkey_c(src);
487 static inline struct bpos
488 bkey_unpack_pos_format_checked(const struct btree *b,
489 const struct bkey_packed *src)
491 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
492 return bkey_unpack_key_format_checked(b, src).p;
494 return __bkey_unpack_pos(&b->format, src);
498 static inline struct bpos bkey_unpack_pos(const struct btree *b,
499 const struct bkey_packed *src)
501 return likely(bkey_packed(src))
502 ? bkey_unpack_pos_format_checked(b, src)
503 : packed_to_bkey_c(src)->p;
506 /* Disassembled bkeys */
508 static inline struct bkey_s_c bkey_disassemble(const struct btree *b,
509 const struct bkey_packed *k,
512 __bkey_unpack_key(b, u, k);
514 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
517 /* non const version: */
518 static inline struct bkey_s __bkey_disassemble(const struct btree *b,
519 struct bkey_packed *k,
522 __bkey_unpack_key(b, u, k);
524 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
527 static inline u64 bkey_field_max(const struct bkey_format *f,
528 enum bch_bkey_fields nr)
530 return f->bits_per_field[nr] < 64
531 ? (le64_to_cpu(f->field_offset[nr]) +
532 ~(~0ULL << f->bits_per_field[nr]))
536 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
538 int bch2_compile_bkey_format(const struct bkey_format *, void *);
542 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
543 void *out) { return 0; }
547 static inline void bkey_reassemble(struct bkey_i *dst,
551 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
554 #define bkey_s_null ((struct bkey_s) { .k = NULL })
555 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
557 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
558 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
560 static inline struct bkey_s bkey_to_s(struct bkey *k)
562 return (struct bkey_s) { .k = k, .v = NULL };
565 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
567 return (struct bkey_s_c) { .k = k, .v = NULL };
570 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
572 return (struct bkey_s) { .k = &k->k, .v = &k->v };
575 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
577 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
581 * For a given type of value (e.g. struct bch_extent), generates the types for
582 * bkey + bch_extent - inline, split, split const - and also all the conversion
583 * functions, which also check that the value is of the correct type.
585 * We use anonymous unions for upcasting - e.g. converting from e.g. a
586 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
589 #define x(name, ...) \
590 struct bkey_i_##name { \
595 struct bch_##name v; \
598 struct bkey_s_c_##name { \
601 const struct bkey *k; \
602 const struct bch_##name *v; \
604 struct bkey_s_c s_c; \
608 struct bkey_s_##name { \
612 struct bch_##name *v; \
614 struct bkey_s_c_##name c; \
616 struct bkey_s_c s_c; \
620 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
622 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
623 return container_of(&k->k, struct bkey_i_##name, k); \
626 static inline const struct bkey_i_##name * \
627 bkey_i_to_##name##_c(const struct bkey_i *k) \
629 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
630 return container_of(&k->k, struct bkey_i_##name, k); \
633 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
635 EBUG_ON(!IS_ERR_OR_NULL(k.k) && k.k->type != KEY_TYPE_##name); \
636 return (struct bkey_s_##name) { \
638 .v = container_of(k.v, struct bch_##name, v), \
642 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
644 EBUG_ON(!IS_ERR_OR_NULL(k.k) && k.k->type != KEY_TYPE_##name); \
645 return (struct bkey_s_c_##name) { \
647 .v = container_of(k.v, struct bch_##name, v), \
651 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
653 return (struct bkey_s_##name) { \
659 static inline struct bkey_s_c_##name \
660 name##_i_to_s_c(const struct bkey_i_##name *k) \
662 return (struct bkey_s_c_##name) { \
668 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
670 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
671 return (struct bkey_s_##name) { \
673 .v = container_of(&k->v, struct bch_##name, v), \
677 static inline struct bkey_s_c_##name \
678 bkey_i_to_s_c_##name(const struct bkey_i *k) \
680 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
681 return (struct bkey_s_c_##name) { \
683 .v = container_of(&k->v, struct bch_##name, v), \
687 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
689 struct bkey_i_##name *k = \
690 container_of(&_k->k, struct bkey_i_##name, k); \
693 memset(&k->v, 0, sizeof(k->v)); \
694 k->k.type = KEY_TYPE_##name; \
695 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
703 /* byte order helpers */
705 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
707 static inline unsigned high_word_offset(const struct bkey_format *f)
709 return f->key_u64s - 1;
712 #define high_bit_offset 0
713 #define nth_word(p, n) ((p) - (n))
715 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
717 static inline unsigned high_word_offset(const struct bkey_format *f)
722 #define high_bit_offset KEY_PACKED_BITS_START
723 #define nth_word(p, n) ((p) + (n))
726 #error edit for your odd byteorder.
729 #define high_word(f, k) ((k)->_data + high_word_offset(f))
730 #define next_word(p) nth_word(p, 1)
731 #define prev_word(p) nth_word(p, -1)
733 #ifdef CONFIG_BCACHEFS_DEBUG
734 void bch2_bkey_pack_test(void);
736 static inline void bch2_bkey_pack_test(void) {}
739 #define bkey_fields() \
740 x(BKEY_FIELD_INODE, p.inode) \
741 x(BKEY_FIELD_OFFSET, p.offset) \
742 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
743 x(BKEY_FIELD_SIZE, size) \
744 x(BKEY_FIELD_VERSION_HI, version.hi) \
745 x(BKEY_FIELD_VERSION_LO, version.lo)
747 struct bkey_format_state {
748 u64 field_min[BKEY_NR_FIELDS];
749 u64 field_max[BKEY_NR_FIELDS];
752 void bch2_bkey_format_init(struct bkey_format_state *);
754 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
756 s->field_min[field] = min(s->field_min[field], v);
757 s->field_max[field] = max(s->field_max[field], v);
761 * Changes @format so that @k can be successfully packed with @format
763 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
765 #define x(id, field) __bkey_format_add(s, id, k->field);
770 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
771 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
772 const char *bch2_bkey_format_validate(struct bkey_format *);
774 #endif /* _BCACHEFS_BKEY_H */