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); \
93 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
94 const struct bkey_packed *,
95 const struct bkey_packed *);
97 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
100 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
101 const struct bkey_packed *,
102 const struct btree *);
105 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
106 const struct bkey_packed *,
107 const struct bpos *);
110 int bch2_bkey_cmp_packed(const struct btree *,
111 const struct bkey_packed *,
112 const struct bkey_packed *);
115 int __bch2_bkey_cmp_left_packed(const struct btree *,
116 const struct bkey_packed *,
117 const struct bpos *);
120 int bkey_cmp_left_packed(const struct btree *b,
121 const struct bkey_packed *l, const struct bpos *r)
123 return __bch2_bkey_cmp_left_packed(b, l, r);
127 * The compiler generates better code when we pass bpos by ref, but it's often
128 * enough terribly convenient to pass it by val... as much as I hate c++, const
129 * ref would be nice here:
132 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
133 const struct bkey_packed *l,
136 return bkey_cmp_left_packed(b, l, &r);
139 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
141 return !((l.inode ^ r.inode) |
142 (l.offset ^ r.offset) |
143 (l.snapshot ^ r.snapshot));
146 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
148 return l.inode != r.inode ? l.inode < r.inode :
149 l.offset != r.offset ? l.offset < r.offset :
150 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
153 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
155 return l.inode != r.inode ? l.inode < r.inode :
156 l.offset != r.offset ? l.offset < r.offset :
157 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
160 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
162 return bpos_lt(r, l);
165 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
167 return bpos_le(r, l);
170 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
172 return cmp_int(l.inode, r.inode) ?:
173 cmp_int(l.offset, r.offset) ?:
174 cmp_int(l.snapshot, r.snapshot);
177 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
179 return bpos_lt(l, r) ? l : r;
182 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
184 return bpos_gt(l, r) ? l : r;
187 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
189 return !((l.inode ^ r.inode) |
190 (l.offset ^ r.offset));
193 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
195 return l.inode != r.inode
197 : l.offset < r.offset;
200 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
202 return l.inode != r.inode
204 : l.offset <= r.offset;
207 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
209 return bkey_lt(r, l);
212 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
214 return bkey_le(r, l);
217 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
219 return cmp_int(l.inode, r.inode) ?:
220 cmp_int(l.offset, r.offset);
223 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
225 return bkey_lt(l, r) ? l : r;
228 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
230 return bkey_gt(l, r) ? l : r;
233 void bch2_bpos_swab(struct bpos *);
234 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
236 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
238 return cmp_int(l.hi, r.hi) ?:
242 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
243 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
245 static __always_inline int bversion_zero(struct bversion v)
247 return !bversion_cmp(v, ZERO_VERSION);
250 #ifdef CONFIG_BCACHEFS_DEBUG
251 /* statement expressions confusing unlikely()? */
252 #define bkey_packed(_k) \
253 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
254 (_k)->format != KEY_FORMAT_CURRENT; })
256 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
260 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
262 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
264 return (struct bkey_packed *) k;
267 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
269 return (const struct bkey_packed *) k;
272 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
274 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
277 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
279 return bkey_packed(k) ? NULL : (const struct bkey *) k;
282 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
284 return format->bits_per_field[BKEY_FIELD_INODE] +
285 format->bits_per_field[BKEY_FIELD_OFFSET] +
286 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
289 static inline struct bpos bpos_successor(struct bpos p)
299 static inline struct bpos bpos_predecessor(struct bpos p)
309 static inline struct bpos bpos_nosnap_successor(struct bpos p)
320 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
331 static inline u64 bkey_start_offset(const struct bkey *k)
333 return k->p.offset - k->size;
336 static inline struct bpos bkey_start_pos(const struct bkey *k)
338 return (struct bpos) {
340 .offset = bkey_start_offset(k),
341 .snapshot = k->p.snapshot,
347 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
348 const struct bkey_packed *k)
350 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
352 EBUG_ON(k->u64s < ret);
356 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
357 const struct bkey_packed *k)
359 return bkeyp_key_u64s(format, k) * sizeof(u64);
362 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
363 const struct bkey_packed *k)
365 return k->u64s - bkeyp_key_u64s(format, k);
368 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
369 const struct bkey_packed *k)
371 return bkeyp_val_u64s(format, k) * sizeof(u64);
374 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
375 struct bkey_packed *k, unsigned val_u64s)
377 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
380 #define bkeyp_val(_format, _k) \
381 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
383 extern const struct bkey_format bch2_bkey_format_current;
385 bool bch2_bkey_transform(const struct bkey_format *,
386 struct bkey_packed *,
387 const struct bkey_format *,
388 const struct bkey_packed *);
390 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
391 const struct bkey_packed *);
393 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
394 struct bpos __bkey_unpack_pos(const struct bkey_format *,
395 const struct bkey_packed *);
398 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
399 const struct bkey_format *);
401 enum bkey_pack_pos_ret {
403 BKEY_PACK_POS_SMALLER,
407 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
408 const struct btree *);
410 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
411 const struct btree *b)
413 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
416 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
417 const struct bkey_packed *);
418 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
419 const struct bkey_format *);
421 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
424 __bkey_unpack_key_format_checked(const struct btree *b,
426 const struct bkey_packed *src)
428 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
429 compiled_unpack_fn unpack_fn = b->aux_data;
432 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
433 bch2_expensive_debug_checks) {
434 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
436 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
439 *dst = __bch2_bkey_unpack_key(&b->format, src);
443 static inline struct bkey
444 bkey_unpack_key_format_checked(const struct btree *b,
445 const struct bkey_packed *src)
449 __bkey_unpack_key_format_checked(b, &dst, src);
453 static inline void __bkey_unpack_key(const struct btree *b,
455 const struct bkey_packed *src)
457 if (likely(bkey_packed(src)))
458 __bkey_unpack_key_format_checked(b, dst, src);
460 *dst = *packed_to_bkey_c(src);
464 * bkey_unpack_key -- unpack just the key, not the value
466 static inline struct bkey bkey_unpack_key(const struct btree *b,
467 const struct bkey_packed *src)
469 return likely(bkey_packed(src))
470 ? bkey_unpack_key_format_checked(b, src)
471 : *packed_to_bkey_c(src);
474 static inline struct bpos
475 bkey_unpack_pos_format_checked(const struct btree *b,
476 const struct bkey_packed *src)
478 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
479 return bkey_unpack_key_format_checked(b, src).p;
481 return __bkey_unpack_pos(&b->format, src);
485 static inline struct bpos bkey_unpack_pos(const struct btree *b,
486 const struct bkey_packed *src)
488 return likely(bkey_packed(src))
489 ? bkey_unpack_pos_format_checked(b, src)
490 : packed_to_bkey_c(src)->p;
493 /* Disassembled bkeys */
495 static inline struct bkey_s_c bkey_disassemble(struct btree *b,
496 const struct bkey_packed *k,
499 __bkey_unpack_key(b, u, k);
501 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
504 /* non const version: */
505 static inline struct bkey_s __bkey_disassemble(struct btree *b,
506 struct bkey_packed *k,
509 __bkey_unpack_key(b, u, k);
511 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
514 static inline u64 bkey_field_max(const struct bkey_format *f,
515 enum bch_bkey_fields nr)
517 return f->bits_per_field[nr] < 64
518 ? (le64_to_cpu(f->field_offset[nr]) +
519 ~(~0ULL << f->bits_per_field[nr]))
523 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
525 int bch2_compile_bkey_format(const struct bkey_format *, void *);
529 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
530 void *out) { return 0; }
534 static inline void bkey_reassemble(struct bkey_i *dst,
538 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
541 #define bkey_s_null ((struct bkey_s) { .k = NULL })
542 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
544 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
545 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
547 static inline struct bkey_s bkey_to_s(struct bkey *k)
549 return (struct bkey_s) { .k = k, .v = NULL };
552 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
554 return (struct bkey_s_c) { .k = k, .v = NULL };
557 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
559 return (struct bkey_s) { .k = &k->k, .v = &k->v };
562 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
564 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
568 * For a given type of value (e.g. struct bch_extent), generates the types for
569 * bkey + bch_extent - inline, split, split const - and also all the conversion
570 * functions, which also check that the value is of the correct type.
572 * We use anonymous unions for upcasting - e.g. converting from e.g. a
573 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
576 #define x(name, ...) \
577 struct bkey_i_##name { \
582 struct bch_##name v; \
585 struct bkey_s_c_##name { \
588 const struct bkey *k; \
589 const struct bch_##name *v; \
591 struct bkey_s_c s_c; \
595 struct bkey_s_##name { \
599 struct bch_##name *v; \
601 struct bkey_s_c_##name c; \
603 struct bkey_s_c s_c; \
607 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
609 EBUG_ON(k->k.type != KEY_TYPE_##name); \
610 return container_of(&k->k, struct bkey_i_##name, k); \
613 static inline const struct bkey_i_##name * \
614 bkey_i_to_##name##_c(const struct bkey_i *k) \
616 EBUG_ON(k->k.type != KEY_TYPE_##name); \
617 return container_of(&k->k, struct bkey_i_##name, k); \
620 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
622 EBUG_ON(k.k->type != KEY_TYPE_##name); \
623 return (struct bkey_s_##name) { \
625 .v = container_of(k.v, struct bch_##name, v), \
629 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
631 EBUG_ON(k.k->type != KEY_TYPE_##name); \
632 return (struct bkey_s_c_##name) { \
634 .v = container_of(k.v, struct bch_##name, v), \
638 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
640 return (struct bkey_s_##name) { \
646 static inline struct bkey_s_c_##name \
647 name##_i_to_s_c(const struct bkey_i_##name *k) \
649 return (struct bkey_s_c_##name) { \
655 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
657 EBUG_ON(k->k.type != KEY_TYPE_##name); \
658 return (struct bkey_s_##name) { \
660 .v = container_of(&k->v, struct bch_##name, v), \
664 static inline struct bkey_s_c_##name \
665 bkey_i_to_s_c_##name(const struct bkey_i *k) \
667 EBUG_ON(k->k.type != KEY_TYPE_##name); \
668 return (struct bkey_s_c_##name) { \
670 .v = container_of(&k->v, struct bch_##name, v), \
674 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
676 struct bkey_i_##name *k = \
677 container_of(&_k->k, struct bkey_i_##name, k); \
680 memset(&k->v, 0, sizeof(k->v)); \
681 k->k.type = KEY_TYPE_##name; \
682 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
690 /* byte order helpers */
692 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
694 static inline unsigned high_word_offset(const struct bkey_format *f)
696 return f->key_u64s - 1;
699 #define high_bit_offset 0
700 #define nth_word(p, n) ((p) - (n))
702 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
704 static inline unsigned high_word_offset(const struct bkey_format *f)
709 #define high_bit_offset KEY_PACKED_BITS_START
710 #define nth_word(p, n) ((p) + (n))
713 #error edit for your odd byteorder.
716 #define high_word(f, k) ((k)->_data + high_word_offset(f))
717 #define next_word(p) nth_word(p, 1)
718 #define prev_word(p) nth_word(p, -1)
720 #ifdef CONFIG_BCACHEFS_DEBUG
721 void bch2_bkey_pack_test(void);
723 static inline void bch2_bkey_pack_test(void) {}
726 #define bkey_fields() \
727 x(BKEY_FIELD_INODE, p.inode) \
728 x(BKEY_FIELD_OFFSET, p.offset) \
729 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
730 x(BKEY_FIELD_SIZE, size) \
731 x(BKEY_FIELD_VERSION_HI, version.hi) \
732 x(BKEY_FIELD_VERSION_LO, version.lo)
734 struct bkey_format_state {
735 u64 field_min[BKEY_NR_FIELDS];
736 u64 field_max[BKEY_NR_FIELDS];
739 void bch2_bkey_format_init(struct bkey_format_state *);
741 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
743 s->field_min[field] = min(s->field_min[field], v);
744 s->field_max[field] = max(s->field_max[field], v);
748 * Changes @format so that @k can be successfully packed with @format
750 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
752 #define x(id, field) __bkey_format_add(s, id, k->field);
757 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
758 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
759 const char *bch2_bkey_format_validate(struct bkey_format *);
761 #endif /* _BCACHEFS_BKEY_H */