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 static inline unsigned bkey_val_u64s(const struct bkey *k)
37 return 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 k->u64s = BKEY_U64s + val_u64s;
50 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
52 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
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)
60 #define bkey_packed_typecheck(_k) \
62 BUILD_BUG_ON(!type_is(_k, struct bkey *) && \
63 !type_is(_k, struct bkey_packed *)); \
64 type_is(_k, struct bkey_packed *); \
74 #define bkey_lr_packed_typecheck(_l, _r) \
75 (!bkey_packed_typecheck(_l) + ((!bkey_packed_typecheck(_r)) << 1))
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 __memmove_u64s_down((_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 bkey_packed *,
126 const struct bkey_packed *,
127 const struct btree *);
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 * If @_l or @_r are struct bkey * (not bkey_packed *), uses type information to
155 * skip dispatching on k->format:
157 #define bkey_cmp_packed(_b, _l, _r) \
161 switch (bkey_lr_packed_typecheck(_l, _r)) { \
162 case BKEY_PACKED_NONE: \
163 _cmp = bkey_cmp(((struct bkey *) (_l))->p, \
164 ((struct bkey *) (_r))->p); \
166 case BKEY_PACKED_LEFT: \
167 _cmp = bkey_cmp_left_packed((_b), \
168 (struct bkey_packed *) (_l), \
169 &((struct bkey *) (_r))->p); \
171 case BKEY_PACKED_RIGHT: \
172 _cmp = -bkey_cmp_left_packed((_b), \
173 (struct bkey_packed *) (_r), \
174 &((struct bkey *) (_l))->p); \
176 case BKEY_PACKED_BOTH: \
177 _cmp = __bch2_bkey_cmp_packed((void *) (_l), \
178 (void *) (_r), (_b)); \
185 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
187 if (l.inode != r.inode)
188 return l.inode < r.inode ? -1 : 1;
189 if (l.offset != r.offset)
190 return l.offset < r.offset ? -1 : 1;
191 if (l.snapshot != r.snapshot)
192 return l.snapshot < r.snapshot ? -1 : 1;
196 int bkey_cmp(struct bpos l, struct bpos r);
199 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
201 return bkey_cmp(l, r) < 0 ? l : r;
204 void bch2_bpos_swab(struct bpos *);
205 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
207 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
209 return (l.hi > r.hi) - (l.hi < r.hi) ?:
210 (l.lo > r.lo) - (l.lo < r.lo);
213 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
214 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
216 static __always_inline int bversion_zero(struct bversion v)
218 return !bversion_cmp(v, ZERO_VERSION);
221 #ifdef CONFIG_BCACHEFS_DEBUG
222 /* statement expressions confusing unlikely()? */
223 #define bkey_packed(_k) \
224 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
225 (_k)->format != KEY_FORMAT_CURRENT; })
227 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
231 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
233 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
235 return (struct bkey_packed *) k;
238 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
240 return (const struct bkey_packed *) k;
243 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
245 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
248 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
250 return bkey_packed(k) ? NULL : (const struct bkey *) k;
253 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
255 return format->bits_per_field[BKEY_FIELD_INODE] +
256 format->bits_per_field[BKEY_FIELD_OFFSET] +
257 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
260 static inline struct bpos bkey_successor(struct bpos p)
265 BUG_ON(!++ret.inode);
270 static inline struct bpos bkey_predecessor(struct bpos p)
275 BUG_ON(!ret.inode--);
280 static inline u64 bkey_start_offset(const struct bkey *k)
282 return k->p.offset - k->size;
285 static inline struct bpos bkey_start_pos(const struct bkey *k)
287 return (struct bpos) {
289 .offset = bkey_start_offset(k),
290 .snapshot = k->p.snapshot,
296 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
297 const struct bkey_packed *k)
299 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
301 EBUG_ON(k->u64s < ret);
305 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
306 const struct bkey_packed *k)
308 return bkeyp_key_u64s(format, k) * sizeof(u64);
311 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
312 const struct bkey_packed *k)
314 return k->u64s - bkeyp_key_u64s(format, k);
317 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
318 const struct bkey_packed *k)
320 return bkeyp_val_u64s(format, k) * sizeof(u64);
323 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
324 struct bkey_packed *k, unsigned val_u64s)
326 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
329 #define bkeyp_val(_format, _k) \
330 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
332 extern const struct bkey_format bch2_bkey_format_current;
334 bool bch2_bkey_transform(const struct bkey_format *,
335 struct bkey_packed *,
336 const struct bkey_format *,
337 const struct bkey_packed *);
339 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
340 const struct bkey_packed *);
342 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
343 struct bpos __bkey_unpack_pos(const struct bkey_format *,
344 const struct bkey_packed *);
347 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
348 const struct bkey_format *);
350 enum bkey_pack_pos_ret {
352 BKEY_PACK_POS_SMALLER,
356 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
357 const struct btree *);
359 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
360 const struct btree *b)
362 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
365 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
366 const struct bkey_packed *);
367 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
368 const struct bkey_format *);
370 static inline u64 bkey_field_max(const struct bkey_format *f,
371 enum bch_bkey_fields nr)
373 return f->bits_per_field[nr] < 64
374 ? (le64_to_cpu(f->field_offset[nr]) +
375 ~(~0ULL << f->bits_per_field[nr]))
379 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
381 int bch2_compile_bkey_format(const struct bkey_format *, void *);
385 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
386 void *out) { return 0; }
390 static inline void bkey_reassemble(struct bkey_i *dst,
393 BUG_ON(bkey_packed(src.k));
395 memcpy_u64s(&dst->v, src.v, bkey_val_u64s(src.k));
398 #define bkey_s_null ((struct bkey_s) { .k = NULL })
399 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
401 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
402 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
404 static inline struct bkey_s bkey_to_s(struct bkey *k)
406 return (struct bkey_s) { .k = k, .v = NULL };
409 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
411 return (struct bkey_s_c) { .k = k, .v = NULL };
414 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
416 return (struct bkey_s) { .k = &k->k, .v = &k->v };
419 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
421 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
425 * For a given type of value (e.g. struct bch_extent), generates the types for
426 * bkey + bch_extent - inline, split, split const - and also all the conversion
427 * functions, which also check that the value is of the correct type.
429 * We use anonymous unions for upcasting - e.g. converting from e.g. a
430 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
433 #define __BKEY_VAL_ACCESSORS(name, nr, _assert) \
434 struct bkey_s_c_##name { \
437 const struct bkey *k; \
438 const struct bch_##name *v; \
440 struct bkey_s_c s_c; \
444 struct bkey_s_##name { \
448 struct bch_##name *v; \
450 struct bkey_s_c_##name c; \
452 struct bkey_s_c s_c; \
456 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
458 _assert(k->k.type, nr); \
459 return container_of(&k->k, struct bkey_i_##name, k); \
462 static inline const struct bkey_i_##name * \
463 bkey_i_to_##name##_c(const struct bkey_i *k) \
465 _assert(k->k.type, nr); \
466 return container_of(&k->k, struct bkey_i_##name, k); \
469 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
471 _assert(k.k->type, nr); \
472 return (struct bkey_s_##name) { \
474 .v = container_of(k.v, struct bch_##name, v), \
478 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
480 _assert(k.k->type, nr); \
481 return (struct bkey_s_c_##name) { \
483 .v = container_of(k.v, struct bch_##name, v), \
487 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
489 return (struct bkey_s_##name) { \
495 static inline struct bkey_s_c_##name \
496 name##_i_to_s_c(const struct bkey_i_##name *k) \
498 return (struct bkey_s_c_##name) { \
504 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
506 _assert(k->k.type, nr); \
507 return (struct bkey_s_##name) { \
509 .v = container_of(&k->v, struct bch_##name, v), \
513 static inline struct bkey_s_c_##name \
514 bkey_i_to_s_c_##name(const struct bkey_i *k) \
516 _assert(k->k.type, nr); \
517 return (struct bkey_s_c_##name) { \
519 .v = container_of(&k->v, struct bch_##name, v), \
523 static inline struct bch_##name * \
524 bkey_p_##name##_val(const struct bkey_format *f, \
525 struct bkey_packed *k) \
527 return container_of(bkeyp_val(f, k), struct bch_##name, v); \
530 static inline const struct bch_##name * \
531 bkey_p_c_##name##_val(const struct bkey_format *f, \
532 const struct bkey_packed *k) \
534 return container_of(bkeyp_val(f, k), struct bch_##name, v); \
537 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
539 struct bkey_i_##name *k = \
540 container_of(&_k->k, struct bkey_i_##name, k); \
543 memset(&k->v, 0, sizeof(k->v)); \
545 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
550 #define __BKEY_VAL_ASSERT(_type, _nr) EBUG_ON(_type != _nr)
552 #define BKEY_VAL_ACCESSORS(name, _nr) \
553 static inline void __bch_##name##_assert(u8 type, u8 nr) \
555 EBUG_ON(type != _nr); \
558 __BKEY_VAL_ACCESSORS(name, _nr, __bch_##name##_assert)
560 BKEY_VAL_ACCESSORS(cookie, KEY_TYPE_COOKIE);
562 static inline void __bch2_extent_assert(u8 type, u8 nr)
564 EBUG_ON(type != BCH_EXTENT && type != BCH_EXTENT_CACHED);
567 __BKEY_VAL_ACCESSORS(extent, BCH_EXTENT, __bch2_extent_assert);
568 BKEY_VAL_ACCESSORS(reservation, BCH_RESERVATION);
570 BKEY_VAL_ACCESSORS(inode, BCH_INODE_FS);
571 BKEY_VAL_ACCESSORS(inode_blockdev, BCH_INODE_BLOCKDEV);
572 BKEY_VAL_ACCESSORS(inode_generation, BCH_INODE_GENERATION);
574 BKEY_VAL_ACCESSORS(dirent, BCH_DIRENT);
576 BKEY_VAL_ACCESSORS(xattr, BCH_XATTR);
578 BKEY_VAL_ACCESSORS(alloc, BCH_ALLOC);
580 BKEY_VAL_ACCESSORS(quota, BCH_QUOTA);
582 /* byte order helpers */
584 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
586 static inline unsigned high_word_offset(const struct bkey_format *f)
588 return f->key_u64s - 1;
591 #define high_bit_offset 0
592 #define nth_word(p, n) ((p) - (n))
594 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
596 static inline unsigned high_word_offset(const struct bkey_format *f)
601 #define high_bit_offset KEY_PACKED_BITS_START
602 #define nth_word(p, n) ((p) + (n))
605 #error edit for your odd byteorder.
608 #define high_word(f, k) ((k)->_data + high_word_offset(f))
609 #define next_word(p) nth_word(p, 1)
610 #define prev_word(p) nth_word(p, -1)
612 #ifdef CONFIG_BCACHEFS_DEBUG
613 void bch2_bkey_pack_test(void);
615 static inline void bch2_bkey_pack_test(void) {}
618 #endif /* _BCACHEFS_BKEY_H */