1 #ifndef _BCACHEFS_BKEY_H
2 #define _BCACHEFS_BKEY_H
5 #include "bcachefs_format.h"
11 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
14 void bch2_to_binary(char *, const u64 *, unsigned);
16 /* bkey with split value, const */
19 const struct bch_val *v;
22 /* bkey with split value */
33 #define bkey_next(_k) vstruct_next(_k)
35 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
37 static inline size_t bkey_val_bytes(const struct bkey *k)
39 return bkey_val_u64s(k) * sizeof(u64);
42 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
44 k->u64s = BKEY_U64s + val_u64s;
47 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
49 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
52 #define bkey_val_end(_k) vstruct_idx((_k).v, bkey_val_u64s((_k).k))
54 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
56 #define bkey_whiteout(_k) \
57 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_discard)
59 #define bkey_packed_typecheck(_k) \
61 BUILD_BUG_ON(!type_is(_k, struct bkey *) && \
62 !type_is(_k, struct bkey_packed *)); \
63 type_is(_k, struct bkey_packed *); \
73 #define bkey_lr_packed_typecheck(_l, _r) \
74 (!bkey_packed_typecheck(_l) + ((!bkey_packed_typecheck(_r)) << 1))
76 #define bkey_lr_packed(_l, _r) \
77 ((_l)->format + ((_r)->format << 1))
79 #define bkey_copy(_dst, _src) \
81 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
82 !type_is(_dst, struct bkey_packed *)); \
83 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
84 !type_is(_src, struct bkey_packed *)); \
85 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
86 (u64 *) (_dst) < (u64 *) (_src) + \
87 ((struct bkey *) (_src))->u64s); \
89 __memmove_u64s_down((_dst), (_src), \
90 ((struct bkey *) (_src))->u64s); \
95 struct bkey_format_state {
96 u64 field_min[BKEY_NR_FIELDS];
97 u64 field_max[BKEY_NR_FIELDS];
100 void bch2_bkey_format_init(struct bkey_format_state *);
101 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
102 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
103 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
104 const char *bch2_bkey_format_validate(struct bkey_format *);
107 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
108 const struct bkey_packed *,
109 const struct bkey_packed *);
111 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
114 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
115 const struct bkey_packed *,
116 const struct btree *);
119 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
120 const struct bkey_packed *,
121 const struct bpos *);
124 int __bch2_bkey_cmp_packed(const struct bkey_packed *,
125 const struct bkey_packed *,
126 const struct btree *);
129 int __bch2_bkey_cmp_left_packed(const struct btree *,
130 const struct bkey_packed *,
131 const struct bpos *);
134 int bkey_cmp_left_packed(const struct btree *b,
135 const struct bkey_packed *l, const struct bpos *r)
137 return __bch2_bkey_cmp_left_packed(b, l, r);
141 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
142 * pass it by by val... as much as I hate c++, const ref would be nice here:
145 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
146 const struct bkey_packed *l,
149 return bkey_cmp_left_packed(b, l, &r);
153 * If @_l or @_r are struct bkey * (not bkey_packed *), uses type information to
154 * skip dispatching on k->format:
156 #define bkey_cmp_packed(_b, _l, _r) \
160 switch (bkey_lr_packed_typecheck(_l, _r)) { \
161 case BKEY_PACKED_NONE: \
162 _cmp = bkey_cmp(((struct bkey *) (_l))->p, \
163 ((struct bkey *) (_r))->p); \
165 case BKEY_PACKED_LEFT: \
166 _cmp = bkey_cmp_left_packed((_b), \
167 (struct bkey_packed *) (_l), \
168 &((struct bkey *) (_r))->p); \
170 case BKEY_PACKED_RIGHT: \
171 _cmp = -bkey_cmp_left_packed((_b), \
172 (struct bkey_packed *) (_r), \
173 &((struct bkey *) (_l))->p); \
175 case BKEY_PACKED_BOTH: \
176 _cmp = __bch2_bkey_cmp_packed((void *) (_l), \
177 (void *) (_r), (_b)); \
184 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
186 if (l.inode != r.inode)
187 return l.inode < r.inode ? -1 : 1;
188 if (l.offset != r.offset)
189 return l.offset < r.offset ? -1 : 1;
190 if (l.snapshot != r.snapshot)
191 return l.snapshot < r.snapshot ? -1 : 1;
195 int bkey_cmp(struct bpos l, struct bpos r);
198 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
200 return bkey_cmp(l, r) < 0 ? l : r;
203 void bch2_bpos_swab(struct bpos *);
204 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
206 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
208 return cmp_int(l.hi, r.hi) ?:
212 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
213 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
215 static __always_inline int bversion_zero(struct bversion v)
217 return !bversion_cmp(v, ZERO_VERSION);
220 #ifdef CONFIG_BCACHEFS_DEBUG
221 /* statement expressions confusing unlikely()? */
222 #define bkey_packed(_k) \
223 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
224 (_k)->format != KEY_FORMAT_CURRENT; })
226 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
230 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
232 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
234 return (struct bkey_packed *) k;
237 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
239 return (const struct bkey_packed *) k;
242 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
244 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
247 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
249 return bkey_packed(k) ? NULL : (const struct bkey *) k;
252 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
254 return format->bits_per_field[BKEY_FIELD_INODE] +
255 format->bits_per_field[BKEY_FIELD_OFFSET] +
256 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
259 static inline struct bpos bkey_successor(struct bpos p)
264 BUG_ON(!++ret.inode);
269 static inline struct bpos bkey_predecessor(struct bpos p)
274 BUG_ON(!ret.inode--);
279 static inline u64 bkey_start_offset(const struct bkey *k)
281 return k->p.offset - k->size;
284 static inline struct bpos bkey_start_pos(const struct bkey *k)
286 return (struct bpos) {
288 .offset = bkey_start_offset(k),
289 .snapshot = k->p.snapshot,
295 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
296 const struct bkey_packed *k)
298 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
300 EBUG_ON(k->u64s < ret);
304 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
305 const struct bkey_packed *k)
307 return bkeyp_key_u64s(format, k) * sizeof(u64);
310 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
311 const struct bkey_packed *k)
313 return k->u64s - bkeyp_key_u64s(format, k);
316 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
317 const struct bkey_packed *k)
319 return bkeyp_val_u64s(format, k) * sizeof(u64);
322 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
323 struct bkey_packed *k, unsigned val_u64s)
325 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
328 #define bkeyp_val(_format, _k) \
329 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
331 extern const struct bkey_format bch2_bkey_format_current;
333 bool bch2_bkey_transform(const struct bkey_format *,
334 struct bkey_packed *,
335 const struct bkey_format *,
336 const struct bkey_packed *);
338 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
339 const struct bkey_packed *);
341 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
342 struct bpos __bkey_unpack_pos(const struct bkey_format *,
343 const struct bkey_packed *);
346 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
347 const struct bkey_format *);
349 enum bkey_pack_pos_ret {
351 BKEY_PACK_POS_SMALLER,
355 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
356 const struct btree *);
358 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
359 const struct btree *b)
361 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
364 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
365 const struct bkey_packed *);
366 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
367 const struct bkey_format *);
369 static inline u64 bkey_field_max(const struct bkey_format *f,
370 enum bch_bkey_fields nr)
372 return f->bits_per_field[nr] < 64
373 ? (le64_to_cpu(f->field_offset[nr]) +
374 ~(~0ULL << f->bits_per_field[nr]))
378 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
380 int bch2_compile_bkey_format(const struct bkey_format *, void *);
384 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
385 void *out) { return 0; }
389 static inline void bkey_reassemble(struct bkey_i *dst,
392 BUG_ON(bkey_packed(src.k));
394 memcpy_u64s(&dst->v, src.v, bkey_val_u64s(src.k));
397 #define bkey_s_null ((struct bkey_s) { .k = NULL })
398 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
400 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
401 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
403 static inline struct bkey_s bkey_to_s(struct bkey *k)
405 return (struct bkey_s) { .k = k, .v = NULL };
408 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
410 return (struct bkey_s_c) { .k = k, .v = NULL };
413 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
415 return (struct bkey_s) { .k = &k->k, .v = &k->v };
418 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
420 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
424 * For a given type of value (e.g. struct bch_extent), generates the types for
425 * bkey + bch_extent - inline, split, split const - and also all the conversion
426 * functions, which also check that the value is of the correct type.
428 * We use anonymous unions for upcasting - e.g. converting from e.g. a
429 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
432 #define BKEY_VAL_ACCESSORS(name) \
433 struct bkey_i_##name { \
438 struct bch_##name v; \
441 struct bkey_s_c_##name { \
444 const struct bkey *k; \
445 const struct bch_##name *v; \
447 struct bkey_s_c s_c; \
451 struct bkey_s_##name { \
455 struct bch_##name *v; \
457 struct bkey_s_c_##name c; \
459 struct bkey_s_c s_c; \
463 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
465 EBUG_ON(k->k.type != KEY_TYPE_##name); \
466 return container_of(&k->k, struct bkey_i_##name, k); \
469 static inline const struct bkey_i_##name * \
470 bkey_i_to_##name##_c(const struct bkey_i *k) \
472 EBUG_ON(k->k.type != KEY_TYPE_##name); \
473 return container_of(&k->k, struct bkey_i_##name, k); \
476 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
478 EBUG_ON(k.k->type != KEY_TYPE_##name); \
479 return (struct bkey_s_##name) { \
481 .v = container_of(k.v, struct bch_##name, v), \
485 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
487 EBUG_ON(k.k->type != KEY_TYPE_##name); \
488 return (struct bkey_s_c_##name) { \
490 .v = container_of(k.v, struct bch_##name, v), \
494 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
496 return (struct bkey_s_##name) { \
502 static inline struct bkey_s_c_##name \
503 name##_i_to_s_c(const struct bkey_i_##name *k) \
505 return (struct bkey_s_c_##name) { \
511 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
513 EBUG_ON(k->k.type != KEY_TYPE_##name); \
514 return (struct bkey_s_##name) { \
516 .v = container_of(&k->v, struct bch_##name, v), \
520 static inline struct bkey_s_c_##name \
521 bkey_i_to_s_c_##name(const struct bkey_i *k) \
523 EBUG_ON(k->k.type != KEY_TYPE_##name); \
524 return (struct bkey_s_c_##name) { \
526 .v = container_of(&k->v, struct bch_##name, v), \
530 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
532 struct bkey_i_##name *k = \
533 container_of(&_k->k, struct bkey_i_##name, k); \
536 memset(&k->v, 0, sizeof(k->v)); \
537 k->k.type = KEY_TYPE_##name; \
538 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
543 BKEY_VAL_ACCESSORS(cookie);
544 BKEY_VAL_ACCESSORS(btree_ptr);
545 BKEY_VAL_ACCESSORS(extent);
546 BKEY_VAL_ACCESSORS(reservation);
547 BKEY_VAL_ACCESSORS(inode);
548 BKEY_VAL_ACCESSORS(inode_generation);
549 BKEY_VAL_ACCESSORS(dirent);
550 BKEY_VAL_ACCESSORS(xattr);
551 BKEY_VAL_ACCESSORS(alloc);
552 BKEY_VAL_ACCESSORS(quota);
553 BKEY_VAL_ACCESSORS(stripe);
555 /* byte order helpers */
557 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
559 static inline unsigned high_word_offset(const struct bkey_format *f)
561 return f->key_u64s - 1;
564 #define high_bit_offset 0
565 #define nth_word(p, n) ((p) - (n))
567 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
569 static inline unsigned high_word_offset(const struct bkey_format *f)
574 #define high_bit_offset KEY_PACKED_BITS_START
575 #define nth_word(p, n) ((p) + (n))
578 #error edit for your odd byteorder.
581 #define high_word(f, k) ((k)->_data + high_word_offset(f))
582 #define next_word(p) nth_word(p, 1)
583 #define prev_word(p) nth_word(p, -1)
585 #ifdef CONFIG_BCACHEFS_DEBUG
586 void bch2_bkey_pack_test(void);
588 static inline void bch2_bkey_pack_test(void) {}
591 #endif /* _BCACHEFS_BKEY_H */