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_p_next(_k) vstruct_next(_k)
39 static inline struct bkey_i *bkey_next(struct bkey_i *k)
41 return (struct bkey_i *) (k->_data + k->k.u64s);
44 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
46 static inline size_t bkey_val_bytes(const struct bkey *k)
48 return bkey_val_u64s(k) * sizeof(u64);
51 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
53 unsigned u64s = BKEY_U64s + val_u64s;
55 BUG_ON(u64s > U8_MAX);
59 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
61 set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
64 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
66 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
68 #define bkey_whiteout(_k) \
69 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
78 #define bkey_lr_packed(_l, _r) \
79 ((_l)->format + ((_r)->format << 1))
81 #define bkey_copy(_dst, _src) \
83 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
84 !type_is(_dst, struct bkey_packed *)); \
85 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
86 !type_is(_src, struct bkey_packed *)); \
87 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
88 (u64 *) (_dst) < (u64 *) (_src) + \
89 ((struct bkey *) (_src))->u64s); \
91 memcpy_u64s_small((_dst), (_src), \
92 ((struct bkey *) (_src))->u64s); \
98 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
99 const struct bkey_packed *,
100 const struct bkey_packed *);
102 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
105 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
106 const struct bkey_packed *,
107 const struct btree *);
110 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
111 const struct bkey_packed *,
112 const struct bpos *);
115 int bch2_bkey_cmp_packed(const struct btree *,
116 const struct bkey_packed *,
117 const struct bkey_packed *);
120 int __bch2_bkey_cmp_left_packed(const struct btree *,
121 const struct bkey_packed *,
122 const struct bpos *);
125 int bkey_cmp_left_packed(const struct btree *b,
126 const struct bkey_packed *l, const struct bpos *r)
128 return __bch2_bkey_cmp_left_packed(b, l, r);
132 * The compiler generates better code when we pass bpos by ref, but it's often
133 * enough terribly convenient to pass it by val... as much as I hate c++, const
134 * ref would be nice here:
137 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
138 const struct bkey_packed *l,
141 return bkey_cmp_left_packed(b, l, &r);
144 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
146 return !((l.inode ^ r.inode) |
147 (l.offset ^ r.offset) |
148 (l.snapshot ^ r.snapshot));
151 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
153 return l.inode != r.inode ? l.inode < r.inode :
154 l.offset != r.offset ? l.offset < r.offset :
155 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
158 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
160 return l.inode != r.inode ? l.inode < r.inode :
161 l.offset != r.offset ? l.offset < r.offset :
162 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
165 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
167 return bpos_lt(r, l);
170 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
172 return bpos_le(r, l);
175 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
177 return cmp_int(l.inode, r.inode) ?:
178 cmp_int(l.offset, r.offset) ?:
179 cmp_int(l.snapshot, r.snapshot);
182 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
184 return bpos_lt(l, r) ? l : r;
187 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
189 return bpos_gt(l, r) ? l : r;
192 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
194 return !((l.inode ^ r.inode) |
195 (l.offset ^ r.offset));
198 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
200 return l.inode != r.inode
202 : l.offset < r.offset;
205 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
207 return l.inode != r.inode
209 : l.offset <= r.offset;
212 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
214 return bkey_lt(r, l);
217 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
219 return bkey_le(r, l);
222 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
224 return cmp_int(l.inode, r.inode) ?:
225 cmp_int(l.offset, r.offset);
228 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
230 return bkey_lt(l, r) ? l : r;
233 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
235 return bkey_gt(l, r) ? l : r;
238 void bch2_bpos_swab(struct bpos *);
239 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
241 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
243 return cmp_int(l.hi, r.hi) ?:
247 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
248 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
250 static __always_inline int bversion_zero(struct bversion v)
252 return !bversion_cmp(v, ZERO_VERSION);
255 #ifdef CONFIG_BCACHEFS_DEBUG
256 /* statement expressions confusing unlikely()? */
257 #define bkey_packed(_k) \
258 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
259 (_k)->format != KEY_FORMAT_CURRENT; })
261 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
265 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
267 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
269 return (struct bkey_packed *) k;
272 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
274 return (const struct bkey_packed *) k;
277 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
279 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
282 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
284 return bkey_packed(k) ? NULL : (const struct bkey *) k;
287 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
289 return format->bits_per_field[BKEY_FIELD_INODE] +
290 format->bits_per_field[BKEY_FIELD_OFFSET] +
291 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
294 static inline struct bpos bpos_successor(struct bpos p)
304 static inline struct bpos bpos_predecessor(struct bpos p)
314 static inline struct bpos bpos_nosnap_successor(struct bpos p)
325 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
336 static inline u64 bkey_start_offset(const struct bkey *k)
338 return k->p.offset - k->size;
341 static inline struct bpos bkey_start_pos(const struct bkey *k)
343 return (struct bpos) {
345 .offset = bkey_start_offset(k),
346 .snapshot = k->p.snapshot,
352 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
353 const struct bkey_packed *k)
355 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
357 EBUG_ON(k->u64s < ret);
361 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
362 const struct bkey_packed *k)
364 return bkeyp_key_u64s(format, k) * sizeof(u64);
367 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
368 const struct bkey_packed *k)
370 return k->u64s - bkeyp_key_u64s(format, k);
373 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
374 const struct bkey_packed *k)
376 return bkeyp_val_u64s(format, k) * sizeof(u64);
379 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
380 struct bkey_packed *k, unsigned val_u64s)
382 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
385 #define bkeyp_val(_format, _k) \
386 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
388 extern const struct bkey_format bch2_bkey_format_current;
390 bool bch2_bkey_transform(const struct bkey_format *,
391 struct bkey_packed *,
392 const struct bkey_format *,
393 const struct bkey_packed *);
395 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
396 const struct bkey_packed *);
398 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
399 struct bpos __bkey_unpack_pos(const struct bkey_format *,
400 const struct bkey_packed *);
403 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
404 const struct bkey_format *);
406 enum bkey_pack_pos_ret {
408 BKEY_PACK_POS_SMALLER,
412 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
413 const struct btree *);
415 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
416 const struct btree *b)
418 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
421 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
422 const struct bkey_packed *);
423 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
424 const struct bkey_format *);
426 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
429 __bkey_unpack_key_format_checked(const struct btree *b,
431 const struct bkey_packed *src)
433 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
434 compiled_unpack_fn unpack_fn = b->aux_data;
437 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
438 bch2_expensive_debug_checks) {
439 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
441 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
444 *dst = __bch2_bkey_unpack_key(&b->format, src);
448 static inline struct bkey
449 bkey_unpack_key_format_checked(const struct btree *b,
450 const struct bkey_packed *src)
454 __bkey_unpack_key_format_checked(b, &dst, src);
458 static inline void __bkey_unpack_key(const struct btree *b,
460 const struct bkey_packed *src)
462 if (likely(bkey_packed(src)))
463 __bkey_unpack_key_format_checked(b, dst, src);
465 *dst = *packed_to_bkey_c(src);
469 * bkey_unpack_key -- unpack just the key, not the value
471 static inline struct bkey bkey_unpack_key(const struct btree *b,
472 const struct bkey_packed *src)
474 return likely(bkey_packed(src))
475 ? bkey_unpack_key_format_checked(b, src)
476 : *packed_to_bkey_c(src);
479 static inline struct bpos
480 bkey_unpack_pos_format_checked(const struct btree *b,
481 const struct bkey_packed *src)
483 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
484 return bkey_unpack_key_format_checked(b, src).p;
486 return __bkey_unpack_pos(&b->format, src);
490 static inline struct bpos bkey_unpack_pos(const struct btree *b,
491 const struct bkey_packed *src)
493 return likely(bkey_packed(src))
494 ? bkey_unpack_pos_format_checked(b, src)
495 : packed_to_bkey_c(src)->p;
498 /* Disassembled bkeys */
500 static inline struct bkey_s_c bkey_disassemble(struct btree *b,
501 const struct bkey_packed *k,
504 __bkey_unpack_key(b, u, k);
506 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
509 /* non const version: */
510 static inline struct bkey_s __bkey_disassemble(struct btree *b,
511 struct bkey_packed *k,
514 __bkey_unpack_key(b, u, k);
516 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
519 static inline u64 bkey_field_max(const struct bkey_format *f,
520 enum bch_bkey_fields nr)
522 return f->bits_per_field[nr] < 64
523 ? (le64_to_cpu(f->field_offset[nr]) +
524 ~(~0ULL << f->bits_per_field[nr]))
528 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
530 int bch2_compile_bkey_format(const struct bkey_format *, void *);
534 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
535 void *out) { return 0; }
539 static inline void bkey_reassemble(struct bkey_i *dst,
543 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
546 #define bkey_s_null ((struct bkey_s) { .k = NULL })
547 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
549 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
550 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
552 static inline struct bkey_s bkey_to_s(struct bkey *k)
554 return (struct bkey_s) { .k = k, .v = NULL };
557 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
559 return (struct bkey_s_c) { .k = k, .v = NULL };
562 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
564 return (struct bkey_s) { .k = &k->k, .v = &k->v };
567 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
569 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
573 * For a given type of value (e.g. struct bch_extent), generates the types for
574 * bkey + bch_extent - inline, split, split const - and also all the conversion
575 * functions, which also check that the value is of the correct type.
577 * We use anonymous unions for upcasting - e.g. converting from e.g. a
578 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
581 #define x(name, ...) \
582 struct bkey_i_##name { \
587 struct bch_##name v; \
590 struct bkey_s_c_##name { \
593 const struct bkey *k; \
594 const struct bch_##name *v; \
596 struct bkey_s_c s_c; \
600 struct bkey_s_##name { \
604 struct bch_##name *v; \
606 struct bkey_s_c_##name c; \
608 struct bkey_s_c s_c; \
612 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
614 EBUG_ON(k->k.type != KEY_TYPE_##name); \
615 return container_of(&k->k, struct bkey_i_##name, k); \
618 static inline const struct bkey_i_##name * \
619 bkey_i_to_##name##_c(const struct bkey_i *k) \
621 EBUG_ON(k->k.type != KEY_TYPE_##name); \
622 return container_of(&k->k, struct bkey_i_##name, k); \
625 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
627 EBUG_ON(k.k->type != KEY_TYPE_##name); \
628 return (struct bkey_s_##name) { \
630 .v = container_of(k.v, struct bch_##name, v), \
634 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
636 EBUG_ON(k.k->type != KEY_TYPE_##name); \
637 return (struct bkey_s_c_##name) { \
639 .v = container_of(k.v, struct bch_##name, v), \
643 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
645 return (struct bkey_s_##name) { \
651 static inline struct bkey_s_c_##name \
652 name##_i_to_s_c(const struct bkey_i_##name *k) \
654 return (struct bkey_s_c_##name) { \
660 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
662 EBUG_ON(k->k.type != KEY_TYPE_##name); \
663 return (struct bkey_s_##name) { \
665 .v = container_of(&k->v, struct bch_##name, v), \
669 static inline struct bkey_s_c_##name \
670 bkey_i_to_s_c_##name(const struct bkey_i *k) \
672 EBUG_ON(k->k.type != KEY_TYPE_##name); \
673 return (struct bkey_s_c_##name) { \
675 .v = container_of(&k->v, struct bch_##name, v), \
679 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
681 struct bkey_i_##name *k = \
682 container_of(&_k->k, struct bkey_i_##name, k); \
685 memset(&k->v, 0, sizeof(k->v)); \
686 k->k.type = KEY_TYPE_##name; \
687 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
695 /* byte order helpers */
697 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
699 static inline unsigned high_word_offset(const struct bkey_format *f)
701 return f->key_u64s - 1;
704 #define high_bit_offset 0
705 #define nth_word(p, n) ((p) - (n))
707 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
709 static inline unsigned high_word_offset(const struct bkey_format *f)
714 #define high_bit_offset KEY_PACKED_BITS_START
715 #define nth_word(p, n) ((p) + (n))
718 #error edit for your odd byteorder.
721 #define high_word(f, k) ((k)->_data + high_word_offset(f))
722 #define next_word(p) nth_word(p, 1)
723 #define prev_word(p) nth_word(p, -1)
725 #ifdef CONFIG_BCACHEFS_DEBUG
726 void bch2_bkey_pack_test(void);
728 static inline void bch2_bkey_pack_test(void) {}
731 #define bkey_fields() \
732 x(BKEY_FIELD_INODE, p.inode) \
733 x(BKEY_FIELD_OFFSET, p.offset) \
734 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
735 x(BKEY_FIELD_SIZE, size) \
736 x(BKEY_FIELD_VERSION_HI, version.hi) \
737 x(BKEY_FIELD_VERSION_LO, version.lo)
739 struct bkey_format_state {
740 u64 field_min[BKEY_NR_FIELDS];
741 u64 field_max[BKEY_NR_FIELDS];
744 void bch2_bkey_format_init(struct bkey_format_state *);
746 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
748 s->field_min[field] = min(s->field_min[field], v);
749 s->field_max[field] = max(s->field_max[field], v);
753 * Changes @format so that @k can be successfully packed with @format
755 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
757 #define x(id, field) __bkey_format_add(s, id, k->field);
762 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
763 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
764 const char *bch2_bkey_format_validate(struct bkey_format *);
766 #endif /* _BCACHEFS_BKEY_H */