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_whiteout)
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 void bch2_bpos_swab(struct bpos *);
167 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
169 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
171 return cmp_int(l.hi, r.hi) ?:
175 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
176 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
178 static __always_inline int bversion_zero(struct bversion v)
180 return !bversion_cmp(v, ZERO_VERSION);
183 #ifdef CONFIG_BCACHEFS_DEBUG
184 /* statement expressions confusing unlikely()? */
185 #define bkey_packed(_k) \
186 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
187 (_k)->format != KEY_FORMAT_CURRENT; })
189 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
193 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
195 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
197 return (struct bkey_packed *) k;
200 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
202 return (const struct bkey_packed *) k;
205 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
207 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
210 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
212 return bkey_packed(k) ? NULL : (const struct bkey *) k;
215 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
217 return format->bits_per_field[BKEY_FIELD_INODE] +
218 format->bits_per_field[BKEY_FIELD_OFFSET] +
219 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
222 static inline struct bpos bpos_successor(struct bpos p)
232 static inline struct bpos bpos_predecessor(struct bpos p)
242 static inline struct bpos bpos_nosnap_successor(struct bpos p)
253 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
264 static inline u64 bkey_start_offset(const struct bkey *k)
266 return k->p.offset - k->size;
269 static inline struct bpos bkey_start_pos(const struct bkey *k)
271 return (struct bpos) {
273 .offset = bkey_start_offset(k),
274 .snapshot = k->p.snapshot,
280 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
281 const struct bkey_packed *k)
283 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
285 EBUG_ON(k->u64s < ret);
289 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
290 const struct bkey_packed *k)
292 return bkeyp_key_u64s(format, k) * sizeof(u64);
295 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
296 const struct bkey_packed *k)
298 return k->u64s - bkeyp_key_u64s(format, k);
301 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
302 const struct bkey_packed *k)
304 return bkeyp_val_u64s(format, k) * sizeof(u64);
307 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
308 struct bkey_packed *k, unsigned val_u64s)
310 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
313 #define bkeyp_val(_format, _k) \
314 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
316 extern const struct bkey_format bch2_bkey_format_current;
318 bool bch2_bkey_transform(const struct bkey_format *,
319 struct bkey_packed *,
320 const struct bkey_format *,
321 const struct bkey_packed *);
323 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
324 const struct bkey_packed *);
326 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
327 struct bpos __bkey_unpack_pos(const struct bkey_format *,
328 const struct bkey_packed *);
331 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
332 const struct bkey_format *);
334 enum bkey_pack_pos_ret {
336 BKEY_PACK_POS_SMALLER,
340 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
341 const struct btree *);
343 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
344 const struct btree *b)
346 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
349 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
350 const struct bkey_packed *);
351 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
352 const struct bkey_format *);
354 static inline u64 bkey_field_max(const struct bkey_format *f,
355 enum bch_bkey_fields nr)
357 return f->bits_per_field[nr] < 64
358 ? (le64_to_cpu(f->field_offset[nr]) +
359 ~(~0ULL << f->bits_per_field[nr]))
363 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
365 int bch2_compile_bkey_format(const struct bkey_format *, void *);
369 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
370 void *out) { return 0; }
374 static inline void bkey_reassemble(struct bkey_i *dst,
378 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
381 #define bkey_s_null ((struct bkey_s) { .k = NULL })
382 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
384 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
385 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
387 static inline struct bkey_s bkey_to_s(struct bkey *k)
389 return (struct bkey_s) { .k = k, .v = NULL };
392 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
394 return (struct bkey_s_c) { .k = k, .v = NULL };
397 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
399 return (struct bkey_s) { .k = &k->k, .v = &k->v };
402 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
404 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
408 * For a given type of value (e.g. struct bch_extent), generates the types for
409 * bkey + bch_extent - inline, split, split const - and also all the conversion
410 * functions, which also check that the value is of the correct type.
412 * We use anonymous unions for upcasting - e.g. converting from e.g. a
413 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
416 #define x(name, ...) \
417 struct bkey_i_##name { \
422 struct bch_##name v; \
425 struct bkey_s_c_##name { \
428 const struct bkey *k; \
429 const struct bch_##name *v; \
431 struct bkey_s_c s_c; \
435 struct bkey_s_##name { \
439 struct bch_##name *v; \
441 struct bkey_s_c_##name c; \
443 struct bkey_s_c s_c; \
447 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
449 EBUG_ON(k->k.type != KEY_TYPE_##name); \
450 return container_of(&k->k, struct bkey_i_##name, k); \
453 static inline const struct bkey_i_##name * \
454 bkey_i_to_##name##_c(const struct bkey_i *k) \
456 EBUG_ON(k->k.type != KEY_TYPE_##name); \
457 return container_of(&k->k, struct bkey_i_##name, k); \
460 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
462 EBUG_ON(k.k->type != KEY_TYPE_##name); \
463 return (struct bkey_s_##name) { \
465 .v = container_of(k.v, struct bch_##name, v), \
469 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
471 EBUG_ON(k.k->type != KEY_TYPE_##name); \
472 return (struct bkey_s_c_##name) { \
474 .v = container_of(k.v, struct bch_##name, v), \
478 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
480 return (struct bkey_s_##name) { \
486 static inline struct bkey_s_c_##name \
487 name##_i_to_s_c(const struct bkey_i_##name *k) \
489 return (struct bkey_s_c_##name) { \
495 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
497 EBUG_ON(k->k.type != KEY_TYPE_##name); \
498 return (struct bkey_s_##name) { \
500 .v = container_of(&k->v, struct bch_##name, v), \
504 static inline struct bkey_s_c_##name \
505 bkey_i_to_s_c_##name(const struct bkey_i *k) \
507 EBUG_ON(k->k.type != KEY_TYPE_##name); \
508 return (struct bkey_s_c_##name) { \
510 .v = container_of(&k->v, struct bch_##name, v), \
514 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
516 struct bkey_i_##name *k = \
517 container_of(&_k->k, struct bkey_i_##name, k); \
520 memset(&k->v, 0, sizeof(k->v)); \
521 k->k.type = KEY_TYPE_##name; \
522 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
530 /* byte order helpers */
532 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
534 static inline unsigned high_word_offset(const struct bkey_format *f)
536 return f->key_u64s - 1;
539 #define high_bit_offset 0
540 #define nth_word(p, n) ((p) - (n))
542 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
544 static inline unsigned high_word_offset(const struct bkey_format *f)
549 #define high_bit_offset KEY_PACKED_BITS_START
550 #define nth_word(p, n) ((p) + (n))
553 #error edit for your odd byteorder.
556 #define high_word(f, k) ((k)->_data + high_word_offset(f))
557 #define next_word(p) nth_word(p, 1)
558 #define prev_word(p) nth_word(p, -1)
560 #ifdef CONFIG_BCACHEFS_DEBUG
561 void bch2_bkey_pack_test(void);
563 static inline void bch2_bkey_pack_test(void) {}
566 #endif /* _BCACHEFS_BKEY_H */