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 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
38 static inline size_t bkey_val_bytes(const struct bkey *k)
40 return bkey_val_u64s(k) * sizeof(u64);
43 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
45 k->u64s = BKEY_U64s + val_u64s;
48 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
50 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
53 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
55 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
57 #define bkey_whiteout(_k) \
58 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_discard)
67 #define bkey_lr_packed(_l, _r) \
68 ((_l)->format + ((_r)->format << 1))
70 #define bkey_copy(_dst, _src) \
72 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
73 !type_is(_dst, struct bkey_packed *)); \
74 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
75 !type_is(_src, struct bkey_packed *)); \
76 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
77 (u64 *) (_dst) < (u64 *) (_src) + \
78 ((struct bkey *) (_src))->u64s); \
80 memcpy_u64s_small((_dst), (_src), \
81 ((struct bkey *) (_src))->u64s); \
86 struct bkey_format_state {
87 u64 field_min[BKEY_NR_FIELDS];
88 u64 field_max[BKEY_NR_FIELDS];
91 void bch2_bkey_format_init(struct bkey_format_state *);
92 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
93 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
94 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
95 const char *bch2_bkey_format_validate(struct bkey_format *);
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 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
133 * pass it by by val... as much as I hate c++, const ref would be nice here:
136 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
137 const struct bkey_packed *l,
140 return bkey_cmp_left_packed(b, l, &r);
143 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
145 return cmp_int(l.inode, r.inode) ?:
146 cmp_int(l.offset, r.offset) ?:
147 cmp_int(l.snapshot, r.snapshot);
150 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
152 return cmp_int(l.inode, r.inode) ?:
153 cmp_int(l.offset, r.offset);
156 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
158 return bpos_cmp(l, r) < 0 ? l : r;
161 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
163 return bpos_cmp(l, r) > 0 ? l : r;
166 #define sbb(a, b, borrow) \
179 static inline struct bpos bpos_sub(struct bpos a, struct bpos b)
183 sbb(a.snapshot, b.snapshot, borrow);
184 sbb(a.offset, b.offset, borrow);
185 sbb(a.inode, b.inode, borrow);
189 static inline struct bpos bpos_diff(struct bpos l, struct bpos r)
191 if (bpos_cmp(l, r) > 0)
194 return bpos_sub(r, l);
197 void bch2_bpos_swab(struct bpos *);
198 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
200 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
202 return cmp_int(l.hi, r.hi) ?:
206 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
207 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
209 static __always_inline int bversion_zero(struct bversion v)
211 return !bversion_cmp(v, ZERO_VERSION);
214 #ifdef CONFIG_BCACHEFS_DEBUG
215 /* statement expressions confusing unlikely()? */
216 #define bkey_packed(_k) \
217 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
218 (_k)->format != KEY_FORMAT_CURRENT; })
220 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
224 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
226 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
228 return (struct bkey_packed *) k;
231 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
233 return (const struct bkey_packed *) k;
236 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
238 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
241 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
243 return bkey_packed(k) ? NULL : (const struct bkey *) k;
246 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
248 return format->bits_per_field[BKEY_FIELD_INODE] +
249 format->bits_per_field[BKEY_FIELD_OFFSET] +
250 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
253 static inline struct bpos bpos_successor(struct bpos p)
263 static inline struct bpos bpos_predecessor(struct bpos p)
273 static inline struct bpos bpos_nosnap_successor(struct bpos p)
284 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
295 static inline u64 bkey_start_offset(const struct bkey *k)
297 return k->p.offset - k->size;
300 static inline struct bpos bkey_start_pos(const struct bkey *k)
302 return (struct bpos) {
304 .offset = bkey_start_offset(k),
305 .snapshot = k->p.snapshot,
311 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
312 const struct bkey_packed *k)
314 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
316 EBUG_ON(k->u64s < ret);
320 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
321 const struct bkey_packed *k)
323 return bkeyp_key_u64s(format, k) * sizeof(u64);
326 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
327 const struct bkey_packed *k)
329 return k->u64s - bkeyp_key_u64s(format, k);
332 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
333 const struct bkey_packed *k)
335 return bkeyp_val_u64s(format, k) * sizeof(u64);
338 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
339 struct bkey_packed *k, unsigned val_u64s)
341 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
344 #define bkeyp_val(_format, _k) \
345 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
347 extern const struct bkey_format bch2_bkey_format_current;
349 bool bch2_bkey_transform(const struct bkey_format *,
350 struct bkey_packed *,
351 const struct bkey_format *,
352 const struct bkey_packed *);
354 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
355 const struct bkey_packed *);
357 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
358 struct bpos __bkey_unpack_pos(const struct bkey_format *,
359 const struct bkey_packed *);
362 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
363 const struct bkey_format *);
365 enum bkey_pack_pos_ret {
367 BKEY_PACK_POS_SMALLER,
371 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
372 const struct btree *);
374 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
375 const struct btree *b)
377 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
380 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
381 const struct bkey_packed *);
382 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
383 const struct bkey_format *);
385 static inline u64 bkey_field_max(const struct bkey_format *f,
386 enum bch_bkey_fields nr)
388 return f->bits_per_field[nr] < 64
389 ? (le64_to_cpu(f->field_offset[nr]) +
390 ~(~0ULL << f->bits_per_field[nr]))
394 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
396 int bch2_compile_bkey_format(const struct bkey_format *, void *);
400 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
401 void *out) { return 0; }
405 static inline void bkey_reassemble(struct bkey_i *dst,
409 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
412 #define bkey_s_null ((struct bkey_s) { .k = NULL })
413 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
415 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
416 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
418 static inline struct bkey_s bkey_to_s(struct bkey *k)
420 return (struct bkey_s) { .k = k, .v = NULL };
423 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
425 return (struct bkey_s_c) { .k = k, .v = NULL };
428 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
430 return (struct bkey_s) { .k = &k->k, .v = &k->v };
433 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
435 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
439 * For a given type of value (e.g. struct bch_extent), generates the types for
440 * bkey + bch_extent - inline, split, split const - and also all the conversion
441 * functions, which also check that the value is of the correct type.
443 * We use anonymous unions for upcasting - e.g. converting from e.g. a
444 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
447 #define x(name, ...) \
448 struct bkey_i_##name { \
453 struct bch_##name v; \
456 struct bkey_s_c_##name { \
459 const struct bkey *k; \
460 const struct bch_##name *v; \
462 struct bkey_s_c s_c; \
466 struct bkey_s_##name { \
470 struct bch_##name *v; \
472 struct bkey_s_c_##name c; \
474 struct bkey_s_c s_c; \
478 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
480 EBUG_ON(k->k.type != KEY_TYPE_##name); \
481 return container_of(&k->k, struct bkey_i_##name, k); \
484 static inline const struct bkey_i_##name * \
485 bkey_i_to_##name##_c(const struct bkey_i *k) \
487 EBUG_ON(k->k.type != KEY_TYPE_##name); \
488 return container_of(&k->k, struct bkey_i_##name, k); \
491 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
493 EBUG_ON(k.k->type != KEY_TYPE_##name); \
494 return (struct bkey_s_##name) { \
496 .v = container_of(k.v, struct bch_##name, v), \
500 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
502 EBUG_ON(k.k->type != KEY_TYPE_##name); \
503 return (struct bkey_s_c_##name) { \
505 .v = container_of(k.v, struct bch_##name, v), \
509 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
511 return (struct bkey_s_##name) { \
517 static inline struct bkey_s_c_##name \
518 name##_i_to_s_c(const struct bkey_i_##name *k) \
520 return (struct bkey_s_c_##name) { \
526 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
528 EBUG_ON(k->k.type != KEY_TYPE_##name); \
529 return (struct bkey_s_##name) { \
531 .v = container_of(&k->v, struct bch_##name, v), \
535 static inline struct bkey_s_c_##name \
536 bkey_i_to_s_c_##name(const struct bkey_i *k) \
538 EBUG_ON(k->k.type != KEY_TYPE_##name); \
539 return (struct bkey_s_c_##name) { \
541 .v = container_of(&k->v, struct bch_##name, v), \
545 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
547 struct bkey_i_##name *k = \
548 container_of(&_k->k, struct bkey_i_##name, k); \
551 memset(&k->v, 0, sizeof(k->v)); \
552 k->k.type = KEY_TYPE_##name; \
553 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
561 /* byte order helpers */
563 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
565 static inline unsigned high_word_offset(const struct bkey_format *f)
567 return f->key_u64s - 1;
570 #define high_bit_offset 0
571 #define nth_word(p, n) ((p) - (n))
573 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
575 static inline unsigned high_word_offset(const struct bkey_format *f)
580 #define high_bit_offset KEY_PACKED_BITS_START
581 #define nth_word(p, n) ((p) + (n))
584 #error edit for your odd byteorder.
587 #define high_word(f, k) ((k)->_data + high_word_offset(f))
588 #define next_word(p) nth_word(p, 1)
589 #define prev_word(p) nth_word(p, -1)
591 #ifdef CONFIG_BCACHEFS_DEBUG
592 void bch2_bkey_pack_test(void);
594 static inline void bch2_bkey_pack_test(void) {}
597 #endif /* _BCACHEFS_BKEY_H */