1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_H
3 #define _BCACHEFS_BKEY_H
6 #include "bcachefs_format.h"
12 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
15 void bch2_to_binary(char *, const u64 *, unsigned);
17 /* bkey with split value, const */
20 const struct bch_val *v;
23 /* bkey with split value */
34 #define bkey_next(_k) vstruct_next(_k)
36 static inline struct bkey_packed *bkey_next_skip_noops(struct bkey_packed *k,
37 struct bkey_packed *end)
41 while (k != end && !k->u64s)
42 k = (void *) ((u64 *) k + 1);
46 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
48 static inline size_t bkey_val_bytes(const struct bkey *k)
50 return bkey_val_u64s(k) * sizeof(u64);
53 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
55 k->u64s = BKEY_U64s + val_u64s;
58 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
60 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
63 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
65 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
67 #define bkey_whiteout(_k) \
68 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_discard)
77 #define bkey_lr_packed(_l, _r) \
78 ((_l)->format + ((_r)->format << 1))
80 #define bkey_copy(_dst, _src) \
82 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
83 !type_is(_dst, struct bkey_packed *)); \
84 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
85 !type_is(_src, struct bkey_packed *)); \
86 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
87 (u64 *) (_dst) < (u64 *) (_src) + \
88 ((struct bkey *) (_src))->u64s); \
90 memcpy_u64s_small((_dst), (_src), \
91 ((struct bkey *) (_src))->u64s); \
96 struct bkey_format_state {
97 u64 field_min[BKEY_NR_FIELDS];
98 u64 field_max[BKEY_NR_FIELDS];
101 void bch2_bkey_format_init(struct bkey_format_state *);
102 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
103 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
104 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
105 const char *bch2_bkey_format_validate(struct bkey_format *);
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 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
143 * pass it by by val... as much as I hate c++, const ref would be nice here:
146 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
147 const struct bkey_packed *l,
150 return bkey_cmp_left_packed(b, l, &r);
154 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
156 if (l.inode != r.inode)
157 return l.inode < r.inode ? -1 : 1;
158 if (l.offset != r.offset)
159 return l.offset < r.offset ? -1 : 1;
160 if (l.snapshot != r.snapshot)
161 return l.snapshot < r.snapshot ? -1 : 1;
165 int bkey_cmp(struct bpos l, struct bpos r);
168 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
170 return bkey_cmp(l, r) < 0 ? l : r;
173 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
175 return bkey_cmp(l, r) > 0 ? l : r;
178 #define sbb(a, b, borrow) \
191 static inline struct bpos bpos_sub(struct bpos a, struct bpos b)
195 sbb(a.snapshot, b.snapshot, borrow);
196 sbb(a.offset, b.offset, borrow);
197 sbb(a.inode, b.inode, borrow);
201 static inline struct bpos bpos_diff(struct bpos l, struct bpos r)
203 if (bkey_cmp(l, r) > 0)
206 return bpos_sub(r, l);
209 void bch2_bpos_swab(struct bpos *);
210 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
212 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
214 return cmp_int(l.hi, r.hi) ?:
218 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
219 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
221 static __always_inline int bversion_zero(struct bversion v)
223 return !bversion_cmp(v, ZERO_VERSION);
226 #ifdef CONFIG_BCACHEFS_DEBUG
227 /* statement expressions confusing unlikely()? */
228 #define bkey_packed(_k) \
229 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
230 (_k)->format != KEY_FORMAT_CURRENT; })
232 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
236 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
238 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
240 return (struct bkey_packed *) k;
243 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
245 return (const struct bkey_packed *) k;
248 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
250 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
253 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
255 return bkey_packed(k) ? NULL : (const struct bkey *) k;
258 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
260 return format->bits_per_field[BKEY_FIELD_INODE] +
261 format->bits_per_field[BKEY_FIELD_OFFSET] +
262 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
265 static inline struct bpos bkey_successor(struct bpos p)
270 BUG_ON(!++ret.inode);
275 static inline struct bpos bkey_predecessor(struct bpos p)
280 BUG_ON(!ret.inode--);
285 static inline u64 bkey_start_offset(const struct bkey *k)
287 return k->p.offset - k->size;
290 static inline struct bpos bkey_start_pos(const struct bkey *k)
292 return (struct bpos) {
294 .offset = bkey_start_offset(k),
295 .snapshot = k->p.snapshot,
301 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
302 const struct bkey_packed *k)
304 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
306 EBUG_ON(k->u64s < ret);
310 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
311 const struct bkey_packed *k)
313 return bkeyp_key_u64s(format, k) * sizeof(u64);
316 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
317 const struct bkey_packed *k)
319 return k->u64s - bkeyp_key_u64s(format, k);
322 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
323 const struct bkey_packed *k)
325 return bkeyp_val_u64s(format, k) * sizeof(u64);
328 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
329 struct bkey_packed *k, unsigned val_u64s)
331 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
334 #define bkeyp_val(_format, _k) \
335 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
337 extern const struct bkey_format bch2_bkey_format_current;
339 bool bch2_bkey_transform(const struct bkey_format *,
340 struct bkey_packed *,
341 const struct bkey_format *,
342 const struct bkey_packed *);
344 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
345 const struct bkey_packed *);
347 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
348 struct bpos __bkey_unpack_pos(const struct bkey_format *,
349 const struct bkey_packed *);
352 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
353 const struct bkey_format *);
355 enum bkey_pack_pos_ret {
357 BKEY_PACK_POS_SMALLER,
361 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
362 const struct btree *);
364 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
365 const struct btree *b)
367 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
370 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
371 const struct bkey_packed *);
372 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
373 const struct bkey_format *);
375 static inline u64 bkey_field_max(const struct bkey_format *f,
376 enum bch_bkey_fields nr)
378 return f->bits_per_field[nr] < 64
379 ? (le64_to_cpu(f->field_offset[nr]) +
380 ~(~0ULL << f->bits_per_field[nr]))
384 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
386 int bch2_compile_bkey_format(const struct bkey_format *, void *);
390 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
391 void *out) { return 0; }
395 static inline void bkey_reassemble(struct bkey_i *dst,
399 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
402 #define bkey_s_null ((struct bkey_s) { .k = NULL })
403 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
405 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
406 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
408 static inline struct bkey_s bkey_to_s(struct bkey *k)
410 return (struct bkey_s) { .k = k, .v = NULL };
413 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
415 return (struct bkey_s_c) { .k = k, .v = NULL };
418 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
420 return (struct bkey_s) { .k = &k->k, .v = &k->v };
423 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
425 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
429 * For a given type of value (e.g. struct bch_extent), generates the types for
430 * bkey + bch_extent - inline, split, split const - and also all the conversion
431 * functions, which also check that the value is of the correct type.
433 * We use anonymous unions for upcasting - e.g. converting from e.g. a
434 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
437 #define x(name, ...) \
438 struct bkey_i_##name { \
443 struct bch_##name v; \
446 struct bkey_s_c_##name { \
449 const struct bkey *k; \
450 const struct bch_##name *v; \
452 struct bkey_s_c s_c; \
456 struct bkey_s_##name { \
460 struct bch_##name *v; \
462 struct bkey_s_c_##name c; \
464 struct bkey_s_c s_c; \
468 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
470 EBUG_ON(k->k.type != KEY_TYPE_##name); \
471 return container_of(&k->k, struct bkey_i_##name, k); \
474 static inline const struct bkey_i_##name * \
475 bkey_i_to_##name##_c(const struct bkey_i *k) \
477 EBUG_ON(k->k.type != KEY_TYPE_##name); \
478 return container_of(&k->k, struct bkey_i_##name, k); \
481 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
483 EBUG_ON(k.k->type != KEY_TYPE_##name); \
484 return (struct bkey_s_##name) { \
486 .v = container_of(k.v, struct bch_##name, v), \
490 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
492 EBUG_ON(k.k->type != KEY_TYPE_##name); \
493 return (struct bkey_s_c_##name) { \
495 .v = container_of(k.v, struct bch_##name, v), \
499 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
501 return (struct bkey_s_##name) { \
507 static inline struct bkey_s_c_##name \
508 name##_i_to_s_c(const struct bkey_i_##name *k) \
510 return (struct bkey_s_c_##name) { \
516 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
518 EBUG_ON(k->k.type != KEY_TYPE_##name); \
519 return (struct bkey_s_##name) { \
521 .v = container_of(&k->v, struct bch_##name, v), \
525 static inline struct bkey_s_c_##name \
526 bkey_i_to_s_c_##name(const struct bkey_i *k) \
528 EBUG_ON(k->k.type != KEY_TYPE_##name); \
529 return (struct bkey_s_c_##name) { \
531 .v = container_of(&k->v, struct bch_##name, v), \
535 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
537 struct bkey_i_##name *k = \
538 container_of(&_k->k, struct bkey_i_##name, k); \
541 memset(&k->v, 0, sizeof(k->v)); \
542 k->k.type = KEY_TYPE_##name; \
543 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
551 /* byte order helpers */
553 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
555 static inline unsigned high_word_offset(const struct bkey_format *f)
557 return f->key_u64s - 1;
560 #define high_bit_offset 0
561 #define nth_word(p, n) ((p) - (n))
563 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
565 static inline unsigned high_word_offset(const struct bkey_format *f)
570 #define high_bit_offset KEY_PACKED_BITS_START
571 #define nth_word(p, n) ((p) + (n))
574 #error edit for your odd byteorder.
577 #define high_word(f, k) ((k)->_data + high_word_offset(f))
578 #define next_word(p) nth_word(p, 1)
579 #define prev_word(p) nth_word(p, -1)
581 #ifdef CONFIG_BCACHEFS_DEBUG
582 void bch2_bkey_pack_test(void);
584 static inline void bch2_bkey_pack_test(void) {}
587 #endif /* _BCACHEFS_BKEY_H */