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 static inline struct bkey_packed *bkey_next_skip_noops(struct bkey_packed *k,
37 struct bkey_packed *end)
41 while (k != end && !k->u64s)
42 k = (void *) ((u64 *) k + 1);
46 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
48 static inline size_t bkey_val_bytes(const struct bkey *k)
50 return bkey_val_u64s(k) * sizeof(u64);
53 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
55 k->u64s = BKEY_U64s + val_u64s;
58 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
60 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
63 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
65 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_deleted)
67 #define bkey_whiteout(_k) \
68 ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_discard)
70 #define bkey_packed_typecheck(_k) \
72 BUILD_BUG_ON(!type_is(_k, struct bkey *) && \
73 !type_is(_k, struct bkey_packed *)); \
74 type_is(_k, struct bkey_packed *); \
84 #define bkey_lr_packed_typecheck(_l, _r) \
85 (!bkey_packed_typecheck(_l) + ((!bkey_packed_typecheck(_r)) << 1))
87 #define bkey_lr_packed(_l, _r) \
88 ((_l)->format + ((_r)->format << 1))
90 #define bkey_copy(_dst, _src) \
92 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
93 !type_is(_dst, struct bkey_packed *)); \
94 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
95 !type_is(_src, struct bkey_packed *)); \
96 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
97 (u64 *) (_dst) < (u64 *) (_src) + \
98 ((struct bkey *) (_src))->u64s); \
100 memcpy_u64s_small((_dst), (_src), \
101 ((struct bkey *) (_src))->u64s); \
106 struct bkey_format_state {
107 u64 field_min[BKEY_NR_FIELDS];
108 u64 field_max[BKEY_NR_FIELDS];
111 void bch2_bkey_format_init(struct bkey_format_state *);
112 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
113 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
114 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
115 const char *bch2_bkey_format_validate(struct bkey_format *);
118 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
119 const struct bkey_packed *,
120 const struct bkey_packed *);
122 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
125 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
126 const struct bkey_packed *,
127 const struct btree *);
130 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
131 const struct bkey_packed *,
132 const struct bpos *);
135 int __bch2_bkey_cmp_packed(const struct bkey_packed *,
136 const struct bkey_packed *,
137 const struct btree *);
140 int __bch2_bkey_cmp_left_packed(const struct btree *,
141 const struct bkey_packed *,
142 const struct bpos *);
145 int bkey_cmp_left_packed(const struct btree *b,
146 const struct bkey_packed *l, const struct bpos *r)
148 return __bch2_bkey_cmp_left_packed(b, l, r);
152 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
153 * pass it by by val... as much as I hate c++, const ref would be nice here:
156 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
157 const struct bkey_packed *l,
160 return bkey_cmp_left_packed(b, l, &r);
164 * If @_l or @_r are struct bkey * (not bkey_packed *), uses type information to
165 * skip dispatching on k->format:
167 #define bkey_cmp_packed(_b, _l, _r) \
171 switch (bkey_lr_packed_typecheck(_l, _r)) { \
172 case BKEY_PACKED_NONE: \
173 _cmp = bkey_cmp(((struct bkey *) (_l))->p, \
174 ((struct bkey *) (_r))->p); \
176 case BKEY_PACKED_LEFT: \
177 _cmp = bkey_cmp_left_packed((_b), \
178 (struct bkey_packed *) (_l), \
179 &((struct bkey *) (_r))->p); \
181 case BKEY_PACKED_RIGHT: \
182 _cmp = -bkey_cmp_left_packed((_b), \
183 (struct bkey_packed *) (_r), \
184 &((struct bkey *) (_l))->p); \
186 case BKEY_PACKED_BOTH: \
187 _cmp = __bch2_bkey_cmp_packed((void *) (_l), \
188 (void *) (_r), (_b)); \
195 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
197 if (l.inode != r.inode)
198 return l.inode < r.inode ? -1 : 1;
199 if (l.offset != r.offset)
200 return l.offset < r.offset ? -1 : 1;
201 if (l.snapshot != r.snapshot)
202 return l.snapshot < r.snapshot ? -1 : 1;
206 int bkey_cmp(struct bpos l, struct bpos r);
209 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
211 return bkey_cmp(l, r) < 0 ? l : r;
214 void bch2_bpos_swab(struct bpos *);
215 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
217 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
219 return cmp_int(l.hi, r.hi) ?:
223 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
224 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
226 static __always_inline int bversion_zero(struct bversion v)
228 return !bversion_cmp(v, ZERO_VERSION);
231 #ifdef CONFIG_BCACHEFS_DEBUG
232 /* statement expressions confusing unlikely()? */
233 #define bkey_packed(_k) \
234 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
235 (_k)->format != KEY_FORMAT_CURRENT; })
237 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
241 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
243 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
245 return (struct bkey_packed *) k;
248 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
250 return (const struct bkey_packed *) k;
253 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
255 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
258 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
260 return bkey_packed(k) ? NULL : (const struct bkey *) k;
263 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
265 return format->bits_per_field[BKEY_FIELD_INODE] +
266 format->bits_per_field[BKEY_FIELD_OFFSET] +
267 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
270 static inline struct bpos bkey_successor(struct bpos p)
275 BUG_ON(!++ret.inode);
280 static inline struct bpos bkey_predecessor(struct bpos p)
285 BUG_ON(!ret.inode--);
290 static inline u64 bkey_start_offset(const struct bkey *k)
292 return k->p.offset - k->size;
295 static inline struct bpos bkey_start_pos(const struct bkey *k)
297 return (struct bpos) {
299 .offset = bkey_start_offset(k),
300 .snapshot = k->p.snapshot,
306 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
307 const struct bkey_packed *k)
309 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
311 EBUG_ON(k->u64s < ret);
315 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
316 const struct bkey_packed *k)
318 return bkeyp_key_u64s(format, k) * sizeof(u64);
321 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
322 const struct bkey_packed *k)
324 return k->u64s - bkeyp_key_u64s(format, k);
327 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
328 const struct bkey_packed *k)
330 return bkeyp_val_u64s(format, k) * sizeof(u64);
333 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
334 struct bkey_packed *k, unsigned val_u64s)
336 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
339 #define bkeyp_val(_format, _k) \
340 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
342 extern const struct bkey_format bch2_bkey_format_current;
344 bool bch2_bkey_transform(const struct bkey_format *,
345 struct bkey_packed *,
346 const struct bkey_format *,
347 const struct bkey_packed *);
349 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
350 const struct bkey_packed *);
352 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
353 struct bpos __bkey_unpack_pos(const struct bkey_format *,
354 const struct bkey_packed *);
357 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
358 const struct bkey_format *);
360 enum bkey_pack_pos_ret {
362 BKEY_PACK_POS_SMALLER,
366 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
367 const struct btree *);
369 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
370 const struct btree *b)
372 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
375 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
376 const struct bkey_packed *);
377 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
378 const struct bkey_format *);
380 static inline u64 bkey_field_max(const struct bkey_format *f,
381 enum bch_bkey_fields nr)
383 return f->bits_per_field[nr] < 64
384 ? (le64_to_cpu(f->field_offset[nr]) +
385 ~(~0ULL << f->bits_per_field[nr]))
389 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
391 int bch2_compile_bkey_format(const struct bkey_format *, void *);
395 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
396 void *out) { return 0; }
400 static inline void bkey_reassemble(struct bkey_i *dst,
403 BUG_ON(bkey_packed(src.k));
405 memcpy_u64s(&dst->v, src.v, bkey_val_u64s(src.k));
408 #define bkey_s_null ((struct bkey_s) { .k = NULL })
409 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
411 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
412 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
414 static inline struct bkey_s bkey_to_s(struct bkey *k)
416 return (struct bkey_s) { .k = k, .v = NULL };
419 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
421 return (struct bkey_s_c) { .k = k, .v = NULL };
424 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
426 return (struct bkey_s) { .k = &k->k, .v = &k->v };
429 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
431 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
435 * For a given type of value (e.g. struct bch_extent), generates the types for
436 * bkey + bch_extent - inline, split, split const - and also all the conversion
437 * functions, which also check that the value is of the correct type.
439 * We use anonymous unions for upcasting - e.g. converting from e.g. a
440 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
443 #define BKEY_VAL_ACCESSORS(name) \
444 struct bkey_i_##name { \
449 struct bch_##name v; \
452 struct bkey_s_c_##name { \
455 const struct bkey *k; \
456 const struct bch_##name *v; \
458 struct bkey_s_c s_c; \
462 struct bkey_s_##name { \
466 struct bch_##name *v; \
468 struct bkey_s_c_##name c; \
470 struct bkey_s_c s_c; \
474 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
476 EBUG_ON(k->k.type != KEY_TYPE_##name); \
477 return container_of(&k->k, struct bkey_i_##name, k); \
480 static inline const struct bkey_i_##name * \
481 bkey_i_to_##name##_c(const struct bkey_i *k) \
483 EBUG_ON(k->k.type != KEY_TYPE_##name); \
484 return container_of(&k->k, struct bkey_i_##name, k); \
487 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
489 EBUG_ON(k.k->type != KEY_TYPE_##name); \
490 return (struct bkey_s_##name) { \
492 .v = container_of(k.v, struct bch_##name, v), \
496 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
498 EBUG_ON(k.k->type != KEY_TYPE_##name); \
499 return (struct bkey_s_c_##name) { \
501 .v = container_of(k.v, struct bch_##name, v), \
505 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
507 return (struct bkey_s_##name) { \
513 static inline struct bkey_s_c_##name \
514 name##_i_to_s_c(const struct bkey_i_##name *k) \
516 return (struct bkey_s_c_##name) { \
522 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
524 EBUG_ON(k->k.type != KEY_TYPE_##name); \
525 return (struct bkey_s_##name) { \
527 .v = container_of(&k->v, struct bch_##name, v), \
531 static inline struct bkey_s_c_##name \
532 bkey_i_to_s_c_##name(const struct bkey_i *k) \
534 EBUG_ON(k->k.type != KEY_TYPE_##name); \
535 return (struct bkey_s_c_##name) { \
537 .v = container_of(&k->v, struct bch_##name, v), \
541 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
543 struct bkey_i_##name *k = \
544 container_of(&_k->k, struct bkey_i_##name, k); \
547 memset(&k->v, 0, sizeof(k->v)); \
548 k->k.type = KEY_TYPE_##name; \
549 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
554 BKEY_VAL_ACCESSORS(cookie);
555 BKEY_VAL_ACCESSORS(btree_ptr);
556 BKEY_VAL_ACCESSORS(extent);
557 BKEY_VAL_ACCESSORS(reservation);
558 BKEY_VAL_ACCESSORS(inode);
559 BKEY_VAL_ACCESSORS(inode_generation);
560 BKEY_VAL_ACCESSORS(dirent);
561 BKEY_VAL_ACCESSORS(xattr);
562 BKEY_VAL_ACCESSORS(alloc);
563 BKEY_VAL_ACCESSORS(quota);
564 BKEY_VAL_ACCESSORS(stripe);
565 BKEY_VAL_ACCESSORS(reflink_p);
566 BKEY_VAL_ACCESSORS(reflink_v);
567 BKEY_VAL_ACCESSORS(inline_data);
569 /* byte order helpers */
571 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
573 static inline unsigned high_word_offset(const struct bkey_format *f)
575 return f->key_u64s - 1;
578 #define high_bit_offset 0
579 #define nth_word(p, n) ((p) - (n))
581 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
583 static inline unsigned high_word_offset(const struct bkey_format *f)
588 #define high_bit_offset KEY_PACKED_BITS_START
589 #define nth_word(p, n) ((p) + (n))
592 #error edit for your odd byteorder.
595 #define high_word(f, k) ((k)->_data + high_word_offset(f))
596 #define next_word(p) nth_word(p, 1)
597 #define prev_word(p) nth_word(p, -1)
599 #ifdef CONFIG_BCACHEFS_DEBUG
600 void bch2_bkey_pack_test(void);
602 static inline void bch2_bkey_pack_test(void) {}
605 #endif /* _BCACHEFS_BKEY_H */