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 #define bkey_val_u64s(_k) ((_k)->u64s - BKEY_U64s)
38 static inline size_t bkey_val_bytes(const struct bkey *k)
40 return bkey_val_u64s(k) * sizeof(u64);
43 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
45 k->u64s = BKEY_U64s + val_u64s;
48 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
50 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
53 #define bkey_val_end(_k) ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
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 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 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 cmp_int(l.hi, r.hi) ?:
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) \
434 struct bkey_i_##name { \
439 struct bch_##name v; \
442 struct bkey_s_c_##name { \
445 const struct bkey *k; \
446 const struct bch_##name *v; \
448 struct bkey_s_c s_c; \
452 struct bkey_s_##name { \
456 struct bch_##name *v; \
458 struct bkey_s_c_##name c; \
460 struct bkey_s_c s_c; \
464 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
466 EBUG_ON(k->k.type != KEY_TYPE_##name); \
467 return container_of(&k->k, struct bkey_i_##name, k); \
470 static inline const struct bkey_i_##name * \
471 bkey_i_to_##name##_c(const struct bkey_i *k) \
473 EBUG_ON(k->k.type != KEY_TYPE_##name); \
474 return container_of(&k->k, struct bkey_i_##name, k); \
477 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
479 EBUG_ON(k.k->type != KEY_TYPE_##name); \
480 return (struct bkey_s_##name) { \
482 .v = container_of(k.v, struct bch_##name, v), \
486 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
488 EBUG_ON(k.k->type != KEY_TYPE_##name); \
489 return (struct bkey_s_c_##name) { \
491 .v = container_of(k.v, struct bch_##name, v), \
495 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
497 return (struct bkey_s_##name) { \
503 static inline struct bkey_s_c_##name \
504 name##_i_to_s_c(const struct bkey_i_##name *k) \
506 return (struct bkey_s_c_##name) { \
512 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
514 EBUG_ON(k->k.type != KEY_TYPE_##name); \
515 return (struct bkey_s_##name) { \
517 .v = container_of(&k->v, struct bch_##name, v), \
521 static inline struct bkey_s_c_##name \
522 bkey_i_to_s_c_##name(const struct bkey_i *k) \
524 EBUG_ON(k->k.type != KEY_TYPE_##name); \
525 return (struct bkey_s_c_##name) { \
527 .v = container_of(&k->v, struct bch_##name, v), \
531 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
533 struct bkey_i_##name *k = \
534 container_of(&_k->k, struct bkey_i_##name, k); \
537 memset(&k->v, 0, sizeof(k->v)); \
538 k->k.type = KEY_TYPE_##name; \
539 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
544 BKEY_VAL_ACCESSORS(cookie);
545 BKEY_VAL_ACCESSORS(btree_ptr);
546 BKEY_VAL_ACCESSORS(extent);
547 BKEY_VAL_ACCESSORS(reservation);
548 BKEY_VAL_ACCESSORS(inode);
549 BKEY_VAL_ACCESSORS(inode_generation);
550 BKEY_VAL_ACCESSORS(dirent);
551 BKEY_VAL_ACCESSORS(xattr);
552 BKEY_VAL_ACCESSORS(alloc);
553 BKEY_VAL_ACCESSORS(quota);
554 BKEY_VAL_ACCESSORS(stripe);
555 BKEY_VAL_ACCESSORS(reflink_p);
556 BKEY_VAL_ACCESSORS(reflink_v);
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 */