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"
12 enum bkey_invalid_flags {
13 BKEY_INVALID_WRITE = (1U << 0),
14 BKEY_INVALID_COMMIT = (1U << 1),
15 BKEY_INVALID_JOURNAL = (1U << 2),
21 * compiled unpack functions are disabled, pending a new interface for
22 * dynamically allocating executable memory:
26 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
30 void bch2_bkey_packed_to_binary_text(struct printbuf *,
31 const struct bkey_format *,
32 const struct bkey_packed *);
34 /* bkey with split value, const */
37 const struct bch_val *v;
40 /* bkey with split value */
51 #define bkey_p_next(_k) vstruct_next(_k)
53 static inline struct bkey_i *bkey_next(struct bkey_i *k)
55 return (struct bkey_i *) ((u64 *) k->_data + k->k.u64s);
58 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
60 static inline size_t bkey_val_bytes(const struct bkey *k)
62 return bkey_val_u64s(k) * sizeof(u64);
65 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
67 unsigned u64s = BKEY_U64s + val_u64s;
69 BUG_ON(u64s > U8_MAX);
73 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
75 set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
78 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
80 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
82 #define bkey_whiteout(_k) \
83 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
92 #define bkey_lr_packed(_l, _r) \
93 ((_l)->format + ((_r)->format << 1))
95 static inline void bkey_p_copy(struct bkey_packed *dst, const struct bkey_packed *src)
97 memcpy_u64s_small(dst, src, src->u64s);
100 static inline void bkey_copy(struct bkey_i *dst, const struct bkey_i *src)
102 memcpy_u64s_small(dst, src, src->k.u64s);
108 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
109 const struct bkey_packed *,
110 const struct bkey_packed *);
112 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
115 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
116 const struct bkey_packed *,
117 const struct btree *);
120 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
121 const struct bkey_packed *,
122 const struct bpos *);
125 int bch2_bkey_cmp_packed(const struct btree *,
126 const struct bkey_packed *,
127 const struct bkey_packed *);
130 int __bch2_bkey_cmp_left_packed(const struct btree *,
131 const struct bkey_packed *,
132 const struct bpos *);
135 int bkey_cmp_left_packed(const struct btree *b,
136 const struct bkey_packed *l, const struct bpos *r)
138 return __bch2_bkey_cmp_left_packed(b, l, r);
142 * The compiler generates better code when we pass bpos by ref, but it's often
143 * enough terribly convenient to pass it by val... as much as I hate c++, const
144 * ref would be nice here:
147 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
148 const struct bkey_packed *l,
151 return bkey_cmp_left_packed(b, l, &r);
154 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
156 return !((l.inode ^ r.inode) |
157 (l.offset ^ r.offset) |
158 (l.snapshot ^ r.snapshot));
161 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
163 return l.inode != r.inode ? l.inode < r.inode :
164 l.offset != r.offset ? l.offset < r.offset :
165 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
168 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
170 return l.inode != r.inode ? l.inode < r.inode :
171 l.offset != r.offset ? l.offset < r.offset :
172 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
175 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
177 return bpos_lt(r, l);
180 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
182 return bpos_le(r, l);
185 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
187 return cmp_int(l.inode, r.inode) ?:
188 cmp_int(l.offset, r.offset) ?:
189 cmp_int(l.snapshot, r.snapshot);
192 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
194 return bpos_lt(l, r) ? l : r;
197 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
199 return bpos_gt(l, r) ? l : r;
202 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
204 return !((l.inode ^ r.inode) |
205 (l.offset ^ r.offset));
208 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
210 return l.inode != r.inode
212 : l.offset < r.offset;
215 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
217 return l.inode != r.inode
219 : l.offset <= r.offset;
222 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
224 return bkey_lt(r, l);
227 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
229 return bkey_le(r, l);
232 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
234 return cmp_int(l.inode, r.inode) ?:
235 cmp_int(l.offset, r.offset);
238 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
240 return bkey_lt(l, r) ? l : r;
243 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
245 return bkey_gt(l, r) ? l : r;
248 void bch2_bpos_swab(struct bpos *);
249 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
251 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
253 return cmp_int(l.hi, r.hi) ?:
257 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
258 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
260 static __always_inline int bversion_zero(struct bversion v)
262 return !bversion_cmp(v, ZERO_VERSION);
265 #ifdef CONFIG_BCACHEFS_DEBUG
266 /* statement expressions confusing unlikely()? */
267 #define bkey_packed(_k) \
268 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
269 (_k)->format != KEY_FORMAT_CURRENT; })
271 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
275 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
277 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
279 return (struct bkey_packed *) k;
282 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
284 return (const struct bkey_packed *) k;
287 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
289 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
292 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
294 return bkey_packed(k) ? NULL : (const struct bkey *) k;
297 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
299 return format->bits_per_field[BKEY_FIELD_INODE] +
300 format->bits_per_field[BKEY_FIELD_OFFSET] +
301 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
304 static inline struct bpos bpos_successor(struct bpos p)
314 static inline struct bpos bpos_predecessor(struct bpos p)
324 static inline struct bpos bpos_nosnap_successor(struct bpos p)
335 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
346 static inline u64 bkey_start_offset(const struct bkey *k)
348 return k->p.offset - k->size;
351 static inline struct bpos bkey_start_pos(const struct bkey *k)
353 return (struct bpos) {
355 .offset = bkey_start_offset(k),
356 .snapshot = k->p.snapshot,
362 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
363 const struct bkey_packed *k)
365 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
367 EBUG_ON(k->u64s < ret);
371 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
372 const struct bkey_packed *k)
374 return bkeyp_key_u64s(format, k) * sizeof(u64);
377 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
378 const struct bkey_packed *k)
380 return k->u64s - bkeyp_key_u64s(format, k);
383 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
384 const struct bkey_packed *k)
386 return bkeyp_val_u64s(format, k) * sizeof(u64);
389 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
390 struct bkey_packed *k, unsigned val_u64s)
392 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
395 #define bkeyp_val(_format, _k) \
396 ((struct bch_val *) ((u64 *) (_k)->_data + bkeyp_key_u64s(_format, _k)))
398 extern const struct bkey_format bch2_bkey_format_current;
400 bool bch2_bkey_transform(const struct bkey_format *,
401 struct bkey_packed *,
402 const struct bkey_format *,
403 const struct bkey_packed *);
405 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
406 const struct bkey_packed *);
408 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
409 struct bpos __bkey_unpack_pos(const struct bkey_format *,
410 const struct bkey_packed *);
413 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
414 const struct bkey_format *);
416 enum bkey_pack_pos_ret {
418 BKEY_PACK_POS_SMALLER,
422 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
423 const struct btree *);
425 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
426 const struct btree *b)
428 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
431 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
432 const struct bkey_packed *);
433 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
434 const struct bkey_format *);
436 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
439 __bkey_unpack_key_format_checked(const struct btree *b,
441 const struct bkey_packed *src)
443 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
444 compiled_unpack_fn unpack_fn = b->aux_data;
447 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
448 bch2_expensive_debug_checks) {
449 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
451 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
454 *dst = __bch2_bkey_unpack_key(&b->format, src);
458 static inline struct bkey
459 bkey_unpack_key_format_checked(const struct btree *b,
460 const struct bkey_packed *src)
464 __bkey_unpack_key_format_checked(b, &dst, src);
468 static inline void __bkey_unpack_key(const struct btree *b,
470 const struct bkey_packed *src)
472 if (likely(bkey_packed(src)))
473 __bkey_unpack_key_format_checked(b, dst, src);
475 *dst = *packed_to_bkey_c(src);
479 * bkey_unpack_key -- unpack just the key, not the value
481 static inline struct bkey bkey_unpack_key(const struct btree *b,
482 const struct bkey_packed *src)
484 return likely(bkey_packed(src))
485 ? bkey_unpack_key_format_checked(b, src)
486 : *packed_to_bkey_c(src);
489 static inline struct bpos
490 bkey_unpack_pos_format_checked(const struct btree *b,
491 const struct bkey_packed *src)
493 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
494 return bkey_unpack_key_format_checked(b, src).p;
496 return __bkey_unpack_pos(&b->format, src);
500 static inline struct bpos bkey_unpack_pos(const struct btree *b,
501 const struct bkey_packed *src)
503 return likely(bkey_packed(src))
504 ? bkey_unpack_pos_format_checked(b, src)
505 : packed_to_bkey_c(src)->p;
508 /* Disassembled bkeys */
510 static inline struct bkey_s_c bkey_disassemble(const struct btree *b,
511 const struct bkey_packed *k,
514 __bkey_unpack_key(b, u, k);
516 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
519 /* non const version: */
520 static inline struct bkey_s __bkey_disassemble(const struct btree *b,
521 struct bkey_packed *k,
524 __bkey_unpack_key(b, u, k);
526 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
529 static inline u64 bkey_field_max(const struct bkey_format *f,
530 enum bch_bkey_fields nr)
532 return f->bits_per_field[nr] < 64
533 ? (le64_to_cpu(f->field_offset[nr]) +
534 ~(~0ULL << f->bits_per_field[nr]))
538 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
540 int bch2_compile_bkey_format(const struct bkey_format *, void *);
544 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
545 void *out) { return 0; }
549 static inline void bkey_reassemble(struct bkey_i *dst,
553 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
556 #define bkey_s_null ((struct bkey_s) { .k = NULL })
557 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
559 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
560 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
562 static inline struct bkey_s bkey_to_s(struct bkey *k)
564 return (struct bkey_s) { .k = k, .v = NULL };
567 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
569 return (struct bkey_s_c) { .k = k, .v = NULL };
572 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
574 return (struct bkey_s) { .k = &k->k, .v = &k->v };
577 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
579 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
583 * For a given type of value (e.g. struct bch_extent), generates the types for
584 * bkey + bch_extent - inline, split, split const - and also all the conversion
585 * functions, which also check that the value is of the correct type.
587 * We use anonymous unions for upcasting - e.g. converting from e.g. a
588 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
591 #define x(name, ...) \
592 struct bkey_i_##name { \
597 struct bch_##name v; \
600 struct bkey_s_c_##name { \
603 const struct bkey *k; \
604 const struct bch_##name *v; \
606 struct bkey_s_c s_c; \
610 struct bkey_s_##name { \
614 struct bch_##name *v; \
616 struct bkey_s_c_##name c; \
618 struct bkey_s_c s_c; \
622 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
624 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
625 return container_of(&k->k, struct bkey_i_##name, k); \
628 static inline const struct bkey_i_##name * \
629 bkey_i_to_##name##_c(const struct bkey_i *k) \
631 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
632 return container_of(&k->k, struct bkey_i_##name, k); \
635 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
637 EBUG_ON(!IS_ERR_OR_NULL(k.k) && k.k->type != KEY_TYPE_##name); \
638 return (struct bkey_s_##name) { \
640 .v = container_of(k.v, struct bch_##name, v), \
644 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
646 EBUG_ON(!IS_ERR_OR_NULL(k.k) && k.k->type != KEY_TYPE_##name); \
647 return (struct bkey_s_c_##name) { \
649 .v = container_of(k.v, struct bch_##name, v), \
653 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
655 return (struct bkey_s_##name) { \
661 static inline struct bkey_s_c_##name \
662 name##_i_to_s_c(const struct bkey_i_##name *k) \
664 return (struct bkey_s_c_##name) { \
670 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
672 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
673 return (struct bkey_s_##name) { \
675 .v = container_of(&k->v, struct bch_##name, v), \
679 static inline struct bkey_s_c_##name \
680 bkey_i_to_s_c_##name(const struct bkey_i *k) \
682 EBUG_ON(!IS_ERR_OR_NULL(k) && k->k.type != KEY_TYPE_##name); \
683 return (struct bkey_s_c_##name) { \
685 .v = container_of(&k->v, struct bch_##name, v), \
689 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
691 struct bkey_i_##name *k = \
692 container_of(&_k->k, struct bkey_i_##name, k); \
695 memset(&k->v, 0, sizeof(k->v)); \
696 k->k.type = KEY_TYPE_##name; \
697 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
705 /* byte order helpers */
707 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
709 static inline unsigned high_word_offset(const struct bkey_format *f)
711 return f->key_u64s - 1;
714 #define high_bit_offset 0
715 #define nth_word(p, n) ((p) - (n))
717 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
719 static inline unsigned high_word_offset(const struct bkey_format *f)
724 #define high_bit_offset KEY_PACKED_BITS_START
725 #define nth_word(p, n) ((p) + (n))
728 #error edit for your odd byteorder.
731 #define high_word(f, k) ((u64 *) (k)->_data + high_word_offset(f))
732 #define next_word(p) nth_word(p, 1)
733 #define prev_word(p) nth_word(p, -1)
735 #ifdef CONFIG_BCACHEFS_DEBUG
736 void bch2_bkey_pack_test(void);
738 static inline void bch2_bkey_pack_test(void) {}
741 #define bkey_fields() \
742 x(BKEY_FIELD_INODE, p.inode) \
743 x(BKEY_FIELD_OFFSET, p.offset) \
744 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
745 x(BKEY_FIELD_SIZE, size) \
746 x(BKEY_FIELD_VERSION_HI, version.hi) \
747 x(BKEY_FIELD_VERSION_LO, version.lo)
749 struct bkey_format_state {
750 u64 field_min[BKEY_NR_FIELDS];
751 u64 field_max[BKEY_NR_FIELDS];
754 void bch2_bkey_format_init(struct bkey_format_state *);
756 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
758 s->field_min[field] = min(s->field_min[field], v);
759 s->field_max[field] = max(s->field_max[field], v);
763 * Changes @format so that @k can be successfully packed with @format
765 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
767 #define x(id, field) __bkey_format_add(s, id, k->field);
772 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
773 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
774 int bch2_bkey_format_invalid(struct bch_fs *, struct bkey_format *,
775 enum bkey_invalid_flags, struct printbuf *);
776 void bch2_bkey_format_to_text(struct printbuf *, const struct bkey_format *);
778 #endif /* _BCACHEFS_BKEY_H */