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)
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 memcpy_u64s_small((_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 btree *,
126 const struct bkey_packed *,
127 const struct bkey_packed *);
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 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
156 if (l.inode != r.inode)
157 return l.inode < r.inode ? -1 : 1;
158 if (l.offset != r.offset)
159 return l.offset < r.offset ? -1 : 1;
160 if (l.snapshot != r.snapshot)
161 return l.snapshot < r.snapshot ? -1 : 1;
165 int bkey_cmp(struct bpos l, struct bpos r);
168 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
170 return bkey_cmp(l, r) < 0 ? l : r;
173 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
175 return bkey_cmp(l, r) > 0 ? l : r;
178 void bch2_bpos_swab(struct bpos *);
179 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
181 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
183 return cmp_int(l.hi, r.hi) ?:
187 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
188 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
190 static __always_inline int bversion_zero(struct bversion v)
192 return !bversion_cmp(v, ZERO_VERSION);
195 #ifdef CONFIG_BCACHEFS_DEBUG
196 /* statement expressions confusing unlikely()? */
197 #define bkey_packed(_k) \
198 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
199 (_k)->format != KEY_FORMAT_CURRENT; })
201 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
205 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
207 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
209 return (struct bkey_packed *) k;
212 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
214 return (const struct bkey_packed *) k;
217 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
219 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
222 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
224 return bkey_packed(k) ? NULL : (const struct bkey *) k;
227 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
229 return format->bits_per_field[BKEY_FIELD_INODE] +
230 format->bits_per_field[BKEY_FIELD_OFFSET] +
231 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
234 static inline struct bpos bkey_successor(struct bpos p)
239 BUG_ON(!++ret.inode);
244 static inline struct bpos bkey_predecessor(struct bpos p)
249 BUG_ON(!ret.inode--);
254 static inline u64 bkey_start_offset(const struct bkey *k)
256 return k->p.offset - k->size;
259 static inline struct bpos bkey_start_pos(const struct bkey *k)
261 return (struct bpos) {
263 .offset = bkey_start_offset(k),
264 .snapshot = k->p.snapshot,
270 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
271 const struct bkey_packed *k)
273 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
275 EBUG_ON(k->u64s < ret);
279 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
280 const struct bkey_packed *k)
282 return bkeyp_key_u64s(format, k) * sizeof(u64);
285 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
286 const struct bkey_packed *k)
288 return k->u64s - bkeyp_key_u64s(format, k);
291 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
292 const struct bkey_packed *k)
294 return bkeyp_val_u64s(format, k) * sizeof(u64);
297 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
298 struct bkey_packed *k, unsigned val_u64s)
300 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
303 #define bkeyp_val(_format, _k) \
304 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
306 extern const struct bkey_format bch2_bkey_format_current;
308 bool bch2_bkey_transform(const struct bkey_format *,
309 struct bkey_packed *,
310 const struct bkey_format *,
311 const struct bkey_packed *);
313 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
314 const struct bkey_packed *);
316 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
317 struct bpos __bkey_unpack_pos(const struct bkey_format *,
318 const struct bkey_packed *);
321 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
322 const struct bkey_format *);
324 enum bkey_pack_pos_ret {
326 BKEY_PACK_POS_SMALLER,
330 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
331 const struct btree *);
333 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
334 const struct btree *b)
336 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
339 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
340 const struct bkey_packed *);
341 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
342 const struct bkey_format *);
344 static inline u64 bkey_field_max(const struct bkey_format *f,
345 enum bch_bkey_fields nr)
347 return f->bits_per_field[nr] < 64
348 ? (le64_to_cpu(f->field_offset[nr]) +
349 ~(~0ULL << f->bits_per_field[nr]))
353 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
355 int bch2_compile_bkey_format(const struct bkey_format *, void *);
359 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
360 void *out) { return 0; }
364 static inline void bkey_reassemble(struct bkey_i *dst,
368 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
371 #define bkey_s_null ((struct bkey_s) { .k = NULL })
372 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
374 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
375 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
377 static inline struct bkey_s bkey_to_s(struct bkey *k)
379 return (struct bkey_s) { .k = k, .v = NULL };
382 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
384 return (struct bkey_s_c) { .k = k, .v = NULL };
387 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
389 return (struct bkey_s) { .k = &k->k, .v = &k->v };
392 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
394 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
398 * For a given type of value (e.g. struct bch_extent), generates the types for
399 * bkey + bch_extent - inline, split, split const - and also all the conversion
400 * functions, which also check that the value is of the correct type.
402 * We use anonymous unions for upcasting - e.g. converting from e.g. a
403 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
406 #define BKEY_VAL_ACCESSORS(name) \
407 struct bkey_i_##name { \
412 struct bch_##name v; \
415 struct bkey_s_c_##name { \
418 const struct bkey *k; \
419 const struct bch_##name *v; \
421 struct bkey_s_c s_c; \
425 struct bkey_s_##name { \
429 struct bch_##name *v; \
431 struct bkey_s_c_##name c; \
433 struct bkey_s_c s_c; \
437 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
439 EBUG_ON(k->k.type != KEY_TYPE_##name); \
440 return container_of(&k->k, struct bkey_i_##name, k); \
443 static inline const struct bkey_i_##name * \
444 bkey_i_to_##name##_c(const struct bkey_i *k) \
446 EBUG_ON(k->k.type != KEY_TYPE_##name); \
447 return container_of(&k->k, struct bkey_i_##name, k); \
450 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
452 EBUG_ON(k.k->type != KEY_TYPE_##name); \
453 return (struct bkey_s_##name) { \
455 .v = container_of(k.v, struct bch_##name, v), \
459 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
461 EBUG_ON(k.k->type != KEY_TYPE_##name); \
462 return (struct bkey_s_c_##name) { \
464 .v = container_of(k.v, struct bch_##name, v), \
468 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
470 return (struct bkey_s_##name) { \
476 static inline struct bkey_s_c_##name \
477 name##_i_to_s_c(const struct bkey_i_##name *k) \
479 return (struct bkey_s_c_##name) { \
485 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
487 EBUG_ON(k->k.type != KEY_TYPE_##name); \
488 return (struct bkey_s_##name) { \
490 .v = container_of(&k->v, struct bch_##name, v), \
494 static inline struct bkey_s_c_##name \
495 bkey_i_to_s_c_##name(const struct bkey_i *k) \
497 EBUG_ON(k->k.type != KEY_TYPE_##name); \
498 return (struct bkey_s_c_##name) { \
500 .v = container_of(&k->v, struct bch_##name, v), \
504 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
506 struct bkey_i_##name *k = \
507 container_of(&_k->k, struct bkey_i_##name, k); \
510 memset(&k->v, 0, sizeof(k->v)); \
511 k->k.type = KEY_TYPE_##name; \
512 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
517 BKEY_VAL_ACCESSORS(cookie);
518 BKEY_VAL_ACCESSORS(btree_ptr);
519 BKEY_VAL_ACCESSORS(extent);
520 BKEY_VAL_ACCESSORS(reservation);
521 BKEY_VAL_ACCESSORS(inode);
522 BKEY_VAL_ACCESSORS(inode_generation);
523 BKEY_VAL_ACCESSORS(dirent);
524 BKEY_VAL_ACCESSORS(xattr);
525 BKEY_VAL_ACCESSORS(alloc);
526 BKEY_VAL_ACCESSORS(quota);
527 BKEY_VAL_ACCESSORS(stripe);
528 BKEY_VAL_ACCESSORS(reflink_p);
529 BKEY_VAL_ACCESSORS(reflink_v);
530 BKEY_VAL_ACCESSORS(inline_data);
531 BKEY_VAL_ACCESSORS(btree_ptr_v2);
532 BKEY_VAL_ACCESSORS(indirect_inline_data);
533 BKEY_VAL_ACCESSORS(alloc_v2);
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 */