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_bkey_packed_to_binary_text(struct printbuf *,
16 const struct bkey_format *,
17 const struct bkey_packed *);
19 /* bkey with split value, const */
22 const struct bch_val *v;
25 /* bkey with split value */
36 #define bkey_next(_k) vstruct_next(_k)
38 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
40 static inline size_t bkey_val_bytes(const struct bkey *k)
42 return bkey_val_u64s(k) * sizeof(u64);
45 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
47 unsigned u64s = BKEY_U64s + val_u64s;
49 BUG_ON(u64s > U8_MAX);
53 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
55 set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
58 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
60 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
62 #define bkey_whiteout(_k) \
63 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
72 #define bkey_lr_packed(_l, _r) \
73 ((_l)->format + ((_r)->format << 1))
75 #define bkey_copy(_dst, _src) \
77 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
78 !type_is(_dst, struct bkey_packed *)); \
79 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
80 !type_is(_src, struct bkey_packed *)); \
81 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
82 (u64 *) (_dst) < (u64 *) (_src) + \
83 ((struct bkey *) (_src))->u64s); \
85 memcpy_u64s_small((_dst), (_src), \
86 ((struct bkey *) (_src))->u64s); \
91 struct bkey_format_state {
92 u64 field_min[BKEY_NR_FIELDS];
93 u64 field_max[BKEY_NR_FIELDS];
96 void bch2_bkey_format_init(struct bkey_format_state *);
97 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
98 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
99 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
100 const char *bch2_bkey_format_validate(struct bkey_format *);
103 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
104 const struct bkey_packed *,
105 const struct bkey_packed *);
107 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
110 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
111 const struct bkey_packed *,
112 const struct btree *);
115 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
116 const struct bkey_packed *,
117 const struct bpos *);
120 int bch2_bkey_cmp_packed(const struct btree *,
121 const struct bkey_packed *,
122 const struct bkey_packed *);
125 int __bch2_bkey_cmp_left_packed(const struct btree *,
126 const struct bkey_packed *,
127 const struct bpos *);
130 int bkey_cmp_left_packed(const struct btree *b,
131 const struct bkey_packed *l, const struct bpos *r)
133 return __bch2_bkey_cmp_left_packed(b, l, r);
137 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
138 * pass it by by val... as much as I hate c++, const ref would be nice here:
141 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
142 const struct bkey_packed *l,
145 return bkey_cmp_left_packed(b, l, &r);
148 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
150 return cmp_int(l.inode, r.inode) ?:
151 cmp_int(l.offset, r.offset) ?:
152 cmp_int(l.snapshot, r.snapshot);
155 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
157 return cmp_int(l.inode, r.inode) ?:
158 cmp_int(l.offset, r.offset);
161 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
163 return bpos_cmp(l, r) < 0 ? l : r;
166 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
168 return bpos_cmp(l, r) > 0 ? l : r;
171 void bch2_bpos_swab(struct bpos *);
172 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
174 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
176 return cmp_int(l.hi, r.hi) ?:
180 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
181 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
183 static __always_inline int bversion_zero(struct bversion v)
185 return !bversion_cmp(v, ZERO_VERSION);
188 #ifdef CONFIG_BCACHEFS_DEBUG
189 /* statement expressions confusing unlikely()? */
190 #define bkey_packed(_k) \
191 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
192 (_k)->format != KEY_FORMAT_CURRENT; })
194 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
198 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
200 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
202 return (struct bkey_packed *) k;
205 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
207 return (const struct bkey_packed *) k;
210 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
212 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
215 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
217 return bkey_packed(k) ? NULL : (const struct bkey *) k;
220 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
222 return format->bits_per_field[BKEY_FIELD_INODE] +
223 format->bits_per_field[BKEY_FIELD_OFFSET] +
224 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
227 static inline struct bpos bpos_successor(struct bpos p)
237 static inline struct bpos bpos_predecessor(struct bpos p)
247 static inline struct bpos bpos_nosnap_successor(struct bpos p)
258 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
269 static inline u64 bkey_start_offset(const struct bkey *k)
271 return k->p.offset - k->size;
274 static inline struct bpos bkey_start_pos(const struct bkey *k)
276 return (struct bpos) {
278 .offset = bkey_start_offset(k),
279 .snapshot = k->p.snapshot,
285 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
286 const struct bkey_packed *k)
288 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
290 EBUG_ON(k->u64s < ret);
294 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
295 const struct bkey_packed *k)
297 return bkeyp_key_u64s(format, k) * sizeof(u64);
300 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
301 const struct bkey_packed *k)
303 return k->u64s - bkeyp_key_u64s(format, k);
306 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
307 const struct bkey_packed *k)
309 return bkeyp_val_u64s(format, k) * sizeof(u64);
312 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
313 struct bkey_packed *k, unsigned val_u64s)
315 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
318 #define bkeyp_val(_format, _k) \
319 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
321 extern const struct bkey_format bch2_bkey_format_current;
323 bool bch2_bkey_transform(const struct bkey_format *,
324 struct bkey_packed *,
325 const struct bkey_format *,
326 const struct bkey_packed *);
328 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
329 const struct bkey_packed *);
331 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
332 struct bpos __bkey_unpack_pos(const struct bkey_format *,
333 const struct bkey_packed *);
336 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
337 const struct bkey_format *);
339 enum bkey_pack_pos_ret {
341 BKEY_PACK_POS_SMALLER,
345 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
346 const struct btree *);
348 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
349 const struct btree *b)
351 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
354 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
355 const struct bkey_packed *);
356 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
357 const struct bkey_format *);
359 static inline u64 bkey_field_max(const struct bkey_format *f,
360 enum bch_bkey_fields nr)
362 return f->bits_per_field[nr] < 64
363 ? (le64_to_cpu(f->field_offset[nr]) +
364 ~(~0ULL << f->bits_per_field[nr]))
368 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
370 int bch2_compile_bkey_format(const struct bkey_format *, void *);
374 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
375 void *out) { return 0; }
379 static inline void bkey_reassemble(struct bkey_i *dst,
383 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
386 #define bkey_s_null ((struct bkey_s) { .k = NULL })
387 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
389 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
390 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
392 static inline struct bkey_s bkey_to_s(struct bkey *k)
394 return (struct bkey_s) { .k = k, .v = NULL };
397 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
399 return (struct bkey_s_c) { .k = k, .v = NULL };
402 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
404 return (struct bkey_s) { .k = &k->k, .v = &k->v };
407 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
409 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
413 * For a given type of value (e.g. struct bch_extent), generates the types for
414 * bkey + bch_extent - inline, split, split const - and also all the conversion
415 * functions, which also check that the value is of the correct type.
417 * We use anonymous unions for upcasting - e.g. converting from e.g. a
418 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
421 #define x(name, ...) \
422 struct bkey_i_##name { \
427 struct bch_##name v; \
430 struct bkey_s_c_##name { \
433 const struct bkey *k; \
434 const struct bch_##name *v; \
436 struct bkey_s_c s_c; \
440 struct bkey_s_##name { \
444 struct bch_##name *v; \
446 struct bkey_s_c_##name c; \
448 struct bkey_s_c s_c; \
452 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
454 EBUG_ON(k->k.type != KEY_TYPE_##name); \
455 return container_of(&k->k, struct bkey_i_##name, k); \
458 static inline const struct bkey_i_##name * \
459 bkey_i_to_##name##_c(const struct bkey_i *k) \
461 EBUG_ON(k->k.type != KEY_TYPE_##name); \
462 return container_of(&k->k, struct bkey_i_##name, k); \
465 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
467 EBUG_ON(k.k->type != KEY_TYPE_##name); \
468 return (struct bkey_s_##name) { \
470 .v = container_of(k.v, struct bch_##name, v), \
474 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
476 EBUG_ON(k.k->type != KEY_TYPE_##name); \
477 return (struct bkey_s_c_##name) { \
479 .v = container_of(k.v, struct bch_##name, v), \
483 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
485 return (struct bkey_s_##name) { \
491 static inline struct bkey_s_c_##name \
492 name##_i_to_s_c(const struct bkey_i_##name *k) \
494 return (struct bkey_s_c_##name) { \
500 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
502 EBUG_ON(k->k.type != KEY_TYPE_##name); \
503 return (struct bkey_s_##name) { \
505 .v = container_of(&k->v, struct bch_##name, v), \
509 static inline struct bkey_s_c_##name \
510 bkey_i_to_s_c_##name(const struct bkey_i *k) \
512 EBUG_ON(k->k.type != KEY_TYPE_##name); \
513 return (struct bkey_s_c_##name) { \
515 .v = container_of(&k->v, struct bch_##name, v), \
519 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
521 struct bkey_i_##name *k = \
522 container_of(&_k->k, struct bkey_i_##name, k); \
525 memset(&k->v, 0, sizeof(k->v)); \
526 k->k.type = KEY_TYPE_##name; \
527 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
535 /* byte order helpers */
537 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
539 static inline unsigned high_word_offset(const struct bkey_format *f)
541 return f->key_u64s - 1;
544 #define high_bit_offset 0
545 #define nth_word(p, n) ((p) - (n))
547 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
549 static inline unsigned high_word_offset(const struct bkey_format *f)
554 #define high_bit_offset KEY_PACKED_BITS_START
555 #define nth_word(p, n) ((p) + (n))
558 #error edit for your odd byteorder.
561 #define high_word(f, k) ((k)->_data + high_word_offset(f))
562 #define next_word(p) nth_word(p, 1)
563 #define prev_word(p) nth_word(p, -1)
565 #ifdef CONFIG_BCACHEFS_DEBUG
566 void bch2_bkey_pack_test(void);
568 static inline void bch2_bkey_pack_test(void) {}
571 #endif /* _BCACHEFS_BKEY_H */