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_val_end(_k) vstruct_idx((_k).v, bkey_val_u64s((_k).k))
57 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
59 #define bkey_whiteout(_k) \
60 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_discard)
62 #define bkey_packed_typecheck(_k) \
64 BUILD_BUG_ON(!type_is(_k, struct bkey *) && \
65 !type_is(_k, struct bkey_packed *)); \
66 type_is(_k, struct bkey_packed *); \
76 #define bkey_lr_packed_typecheck(_l, _r) \
77 (!bkey_packed_typecheck(_l) + ((!bkey_packed_typecheck(_r)) << 1))
79 #define bkey_lr_packed(_l, _r) \
80 ((_l)->format + ((_r)->format << 1))
82 #define bkey_copy(_dst, _src) \
84 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
85 !type_is(_dst, struct bkey_packed *)); \
86 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
87 !type_is(_src, struct bkey_packed *)); \
88 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
89 (u64 *) (_dst) < (u64 *) (_src) + \
90 ((struct bkey *) (_src))->u64s); \
92 __memmove_u64s_down((_dst), (_src), \
93 ((struct bkey *) (_src))->u64s); \
98 struct bkey_format_state {
99 u64 field_min[BKEY_NR_FIELDS];
100 u64 field_max[BKEY_NR_FIELDS];
103 void bch2_bkey_format_init(struct bkey_format_state *);
104 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
105 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
106 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
107 const char *bch2_bkey_format_validate(struct bkey_format *);
110 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
111 const struct bkey_packed *,
112 const struct bkey_packed *);
114 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
117 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
118 const struct bkey_packed *,
119 const struct btree *);
122 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
123 const struct bkey_packed *,
124 const struct bpos *);
127 int __bch2_bkey_cmp_packed(const struct bkey_packed *,
128 const struct bkey_packed *,
129 const struct btree *);
132 int __bch2_bkey_cmp_left_packed(const struct btree *,
133 const struct bkey_packed *,
134 const struct bpos *);
137 int bkey_cmp_left_packed(const struct btree *b,
138 const struct bkey_packed *l, const struct bpos *r)
140 return __bch2_bkey_cmp_left_packed(b, l, r);
144 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
145 * pass it by by val... as much as I hate c++, const ref would be nice here:
148 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
149 const struct bkey_packed *l,
152 return bkey_cmp_left_packed(b, l, &r);
156 * If @_l or @_r are struct bkey * (not bkey_packed *), uses type information to
157 * skip dispatching on k->format:
159 #define bkey_cmp_packed(_b, _l, _r) \
163 switch (bkey_lr_packed_typecheck(_l, _r)) { \
164 case BKEY_PACKED_NONE: \
165 _cmp = bkey_cmp(((struct bkey *) (_l))->p, \
166 ((struct bkey *) (_r))->p); \
168 case BKEY_PACKED_LEFT: \
169 _cmp = bkey_cmp_left_packed((_b), \
170 (struct bkey_packed *) (_l), \
171 &((struct bkey *) (_r))->p); \
173 case BKEY_PACKED_RIGHT: \
174 _cmp = -bkey_cmp_left_packed((_b), \
175 (struct bkey_packed *) (_r), \
176 &((struct bkey *) (_l))->p); \
178 case BKEY_PACKED_BOTH: \
179 _cmp = __bch2_bkey_cmp_packed((void *) (_l), \
180 (void *) (_r), (_b)); \
187 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
189 if (l.inode != r.inode)
190 return l.inode < r.inode ? -1 : 1;
191 if (l.offset != r.offset)
192 return l.offset < r.offset ? -1 : 1;
193 if (l.snapshot != r.snapshot)
194 return l.snapshot < r.snapshot ? -1 : 1;
198 int bkey_cmp(struct bpos l, struct bpos r);
201 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
203 return bkey_cmp(l, r) < 0 ? l : r;
206 void bch2_bpos_swab(struct bpos *);
207 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
209 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
211 return (l.hi > r.hi) - (l.hi < r.hi) ?:
212 (l.lo > r.lo) - (l.lo < r.lo);
215 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
216 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
218 static __always_inline int bversion_zero(struct bversion v)
220 return !bversion_cmp(v, ZERO_VERSION);
223 #ifdef CONFIG_BCACHEFS_DEBUG
224 /* statement expressions confusing unlikely()? */
225 #define bkey_packed(_k) \
226 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
227 (_k)->format != KEY_FORMAT_CURRENT; })
229 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
233 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
235 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
237 return (struct bkey_packed *) k;
240 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
242 return (const struct bkey_packed *) k;
245 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
247 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
250 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
252 return bkey_packed(k) ? NULL : (const struct bkey *) k;
255 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
257 return format->bits_per_field[BKEY_FIELD_INODE] +
258 format->bits_per_field[BKEY_FIELD_OFFSET] +
259 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
262 static inline struct bpos bkey_successor(struct bpos p)
267 BUG_ON(!++ret.inode);
272 static inline struct bpos bkey_predecessor(struct bpos p)
277 BUG_ON(!ret.inode--);
282 static inline u64 bkey_start_offset(const struct bkey *k)
284 return k->p.offset - k->size;
287 static inline struct bpos bkey_start_pos(const struct bkey *k)
289 return (struct bpos) {
291 .offset = bkey_start_offset(k),
292 .snapshot = k->p.snapshot,
298 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
299 const struct bkey_packed *k)
301 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
303 EBUG_ON(k->u64s < ret);
307 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
308 const struct bkey_packed *k)
310 return bkeyp_key_u64s(format, k) * sizeof(u64);
313 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
314 const struct bkey_packed *k)
316 return k->u64s - bkeyp_key_u64s(format, k);
319 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
320 const struct bkey_packed *k)
322 return bkeyp_val_u64s(format, k) * sizeof(u64);
325 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
326 struct bkey_packed *k, unsigned val_u64s)
328 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
331 #define bkeyp_val(_format, _k) \
332 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
334 extern const struct bkey_format bch2_bkey_format_current;
336 bool bch2_bkey_transform(const struct bkey_format *,
337 struct bkey_packed *,
338 const struct bkey_format *,
339 const struct bkey_packed *);
341 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
342 const struct bkey_packed *);
344 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
345 struct bpos __bkey_unpack_pos(const struct bkey_format *,
346 const struct bkey_packed *);
349 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
350 const struct bkey_format *);
352 enum bkey_pack_pos_ret {
354 BKEY_PACK_POS_SMALLER,
358 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
359 const struct btree *);
361 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
362 const struct btree *b)
364 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
367 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
368 const struct bkey_packed *);
369 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
370 const struct bkey_format *);
372 static inline u64 bkey_field_max(const struct bkey_format *f,
373 enum bch_bkey_fields nr)
375 return f->bits_per_field[nr] < 64
376 ? (le64_to_cpu(f->field_offset[nr]) +
377 ~(~0ULL << f->bits_per_field[nr]))
381 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
383 int bch2_compile_bkey_format(const struct bkey_format *, void *);
387 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
388 void *out) { return 0; }
392 static inline void bkey_reassemble(struct bkey_i *dst,
395 BUG_ON(bkey_packed(src.k));
397 memcpy_u64s(&dst->v, src.v, bkey_val_u64s(src.k));
400 #define bkey_s_null ((struct bkey_s) { .k = NULL })
401 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
403 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
404 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
406 static inline struct bkey_s bkey_to_s(struct bkey *k)
408 return (struct bkey_s) { .k = k, .v = NULL };
411 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
413 return (struct bkey_s_c) { .k = k, .v = NULL };
416 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
418 return (struct bkey_s) { .k = &k->k, .v = &k->v };
421 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
423 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
427 * For a given type of value (e.g. struct bch_extent), generates the types for
428 * bkey + bch_extent - inline, split, split const - and also all the conversion
429 * functions, which also check that the value is of the correct type.
431 * We use anonymous unions for upcasting - e.g. converting from e.g. a
432 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
435 #define BKEY_VAL_ACCESSORS(name) \
436 struct bkey_i_##name { \
441 struct bch_##name v; \
444 struct bkey_s_c_##name { \
447 const struct bkey *k; \
448 const struct bch_##name *v; \
450 struct bkey_s_c s_c; \
454 struct bkey_s_##name { \
458 struct bch_##name *v; \
460 struct bkey_s_c_##name c; \
462 struct bkey_s_c s_c; \
466 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
468 EBUG_ON(k->k.type != KEY_TYPE_##name); \
469 return container_of(&k->k, struct bkey_i_##name, k); \
472 static inline const struct bkey_i_##name * \
473 bkey_i_to_##name##_c(const struct bkey_i *k) \
475 EBUG_ON(k->k.type != KEY_TYPE_##name); \
476 return container_of(&k->k, struct bkey_i_##name, k); \
479 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
481 EBUG_ON(k.k->type != KEY_TYPE_##name); \
482 return (struct bkey_s_##name) { \
484 .v = container_of(k.v, struct bch_##name, v), \
488 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
490 EBUG_ON(k.k->type != KEY_TYPE_##name); \
491 return (struct bkey_s_c_##name) { \
493 .v = container_of(k.v, struct bch_##name, v), \
497 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
499 return (struct bkey_s_##name) { \
505 static inline struct bkey_s_c_##name \
506 name##_i_to_s_c(const struct bkey_i_##name *k) \
508 return (struct bkey_s_c_##name) { \
514 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
516 EBUG_ON(k->k.type != KEY_TYPE_##name); \
517 return (struct bkey_s_##name) { \
519 .v = container_of(&k->v, struct bch_##name, v), \
523 static inline struct bkey_s_c_##name \
524 bkey_i_to_s_c_##name(const struct bkey_i *k) \
526 EBUG_ON(k->k.type != KEY_TYPE_##name); \
527 return (struct bkey_s_c_##name) { \
529 .v = container_of(&k->v, struct bch_##name, v), \
533 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
535 struct bkey_i_##name *k = \
536 container_of(&_k->k, struct bkey_i_##name, k); \
539 memset(&k->v, 0, sizeof(k->v)); \
540 k->k.type = KEY_TYPE_##name; \
541 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
546 BKEY_VAL_ACCESSORS(cookie);
547 BKEY_VAL_ACCESSORS(btree_ptr);
548 BKEY_VAL_ACCESSORS(extent);
549 BKEY_VAL_ACCESSORS(reservation);
550 BKEY_VAL_ACCESSORS(inode);
551 BKEY_VAL_ACCESSORS(inode_generation);
552 BKEY_VAL_ACCESSORS(dirent);
553 BKEY_VAL_ACCESSORS(xattr);
554 BKEY_VAL_ACCESSORS(alloc);
555 BKEY_VAL_ACCESSORS(quota);
556 BKEY_VAL_ACCESSORS(stripe);
558 /* byte order helpers */
560 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
562 static inline unsigned high_word_offset(const struct bkey_format *f)
564 return f->key_u64s - 1;
567 #define high_bit_offset 0
568 #define nth_word(p, n) ((p) - (n))
570 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
572 static inline unsigned high_word_offset(const struct bkey_format *f)
577 #define high_bit_offset KEY_PACKED_BITS_START
578 #define nth_word(p, n) ((p) + (n))
581 #error edit for your odd byteorder.
584 #define high_word(f, k) ((k)->_data + high_word_offset(f))
585 #define next_word(p) nth_word(p, 1)
586 #define prev_word(p) nth_word(p, -1)
588 #ifdef CONFIG_BCACHEFS_DEBUG
589 void bch2_bkey_pack_test(void);
591 static inline void bch2_bkey_pack_test(void) {}
594 #endif /* _BCACHEFS_BKEY_H */