1 #ifndef _BCACHEFS_BKEY_H
2 #define _BCACHEFS_BKEY_H
5 #include "bcachefs_format.h"
10 void bch2_to_binary(char *, const u64 *, unsigned);
11 int bch2_bkey_to_text(char *, size_t, const struct bkey *);
13 #define BKEY_PADDED(key) __BKEY_PADDED(key, BKEY_EXTENT_VAL_U64s_MAX)
15 /* bkey with split value, const */
18 const struct bch_val *v;
21 /* bkey with split value */
32 #define bkey_next(_k) vstruct_next(_k)
34 static inline unsigned bkey_val_u64s(const struct bkey *k)
36 return k->u64s - BKEY_U64s;
39 static inline size_t bkey_val_bytes(const struct bkey *k)
41 return bkey_val_u64s(k) * sizeof(u64);
44 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
46 k->u64s = BKEY_U64s + val_u64s;
49 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
51 k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
55 * Mark a key as deleted without changing the size of the value (i.e. modifying
56 * keys in the btree in place)
58 static inline void __set_bkey_deleted(struct bkey *k)
60 k->type = KEY_TYPE_DELETED;
63 static inline void set_bkey_deleted(struct bkey *k)
65 __set_bkey_deleted(k);
66 set_bkey_val_u64s(k, 0);
69 #define bkey_deleted(_k) ((_k)->type == KEY_TYPE_DELETED)
71 #define bkey_whiteout(_k) \
72 ((_k)->type == KEY_TYPE_DELETED || (_k)->type == KEY_TYPE_DISCARD)
74 #define bkey_packed_typecheck(_k) \
76 BUILD_BUG_ON(!type_is(_k, struct bkey *) && \
77 !type_is(_k, struct bkey_packed *)); \
78 type_is(_k, struct bkey_packed *); \
88 #define bkey_lr_packed_typecheck(_l, _r) \
89 (!bkey_packed_typecheck(_l) + ((!bkey_packed_typecheck(_r)) << 1))
91 #define bkey_lr_packed(_l, _r) \
92 ((_l)->format + ((_r)->format << 1))
94 #define bkey_copy(_dst, _src) \
96 BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) && \
97 !type_is(_dst, struct bkey_packed *)); \
98 BUILD_BUG_ON(!type_is(_src, struct bkey_i *) && \
99 !type_is(_src, struct bkey_packed *)); \
100 EBUG_ON((u64 *) (_dst) > (u64 *) (_src) && \
101 (u64 *) (_dst) < (u64 *) (_src) + \
102 ((struct bkey *) (_src))->u64s); \
104 __memmove_u64s_down((_dst), (_src), \
105 ((struct bkey *) (_src))->u64s); \
110 struct bkey_format_state {
111 u64 field_min[BKEY_NR_FIELDS];
112 u64 field_max[BKEY_NR_FIELDS];
115 void bch2_bkey_format_init(struct bkey_format_state *);
116 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
117 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
118 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
119 const char *bch2_bkey_format_validate(struct bkey_format *);
122 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
123 const struct bkey_packed *,
124 const struct bkey_packed *);
126 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
129 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
130 const struct bkey_packed *,
131 const struct btree *);
134 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
135 const struct bkey_packed *,
136 const struct bpos *);
139 int __bch2_bkey_cmp_packed(const struct bkey_packed *,
140 const struct bkey_packed *,
141 const struct btree *);
144 int __bch2_bkey_cmp_left_packed(const struct btree *,
145 const struct bkey_packed *,
146 const struct bpos *);
149 int bkey_cmp_left_packed(const struct btree *b,
150 const struct bkey_packed *l, const struct bpos *r)
152 return __bch2_bkey_cmp_left_packed(b, l, r);
156 * we prefer to pass bpos by ref, but it's often enough terribly convenient to
157 * pass it by by val... as much as I hate c++, const ref would be nice here:
160 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
161 const struct bkey_packed *l,
164 return bkey_cmp_left_packed(b, l, &r);
168 * If @_l or @_r are struct bkey * (not bkey_packed *), uses type information to
169 * skip dispatching on k->format:
171 #define bkey_cmp_packed(_b, _l, _r) \
175 switch (bkey_lr_packed_typecheck(_l, _r)) { \
176 case BKEY_PACKED_NONE: \
177 _cmp = bkey_cmp(((struct bkey *) (_l))->p, \
178 ((struct bkey *) (_r))->p); \
180 case BKEY_PACKED_LEFT: \
181 _cmp = bkey_cmp_left_packed((_b), \
182 (struct bkey_packed *) (_l), \
183 &((struct bkey *) (_r))->p); \
185 case BKEY_PACKED_RIGHT: \
186 _cmp = -bkey_cmp_left_packed((_b), \
187 (struct bkey_packed *) (_r), \
188 &((struct bkey *) (_l))->p); \
190 case BKEY_PACKED_BOTH: \
191 _cmp = __bch2_bkey_cmp_packed((void *) (_l), \
192 (void *) (_r), (_b)); \
199 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
201 if (l.inode != r.inode)
202 return l.inode < r.inode ? -1 : 1;
203 if (l.offset != r.offset)
204 return l.offset < r.offset ? -1 : 1;
205 if (l.snapshot != r.snapshot)
206 return l.snapshot < r.snapshot ? -1 : 1;
210 int bkey_cmp(struct bpos l, struct bpos r);
213 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
215 return bkey_cmp(l, r) < 0 ? l : r;
218 void bch2_bpos_swab(struct bpos *);
219 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
221 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
224 return l.hi < r.hi ? -1 : 1;
226 return l.lo < r.lo ? -1 : 1;
230 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
232 static __always_inline int bversion_zero(struct bversion v)
234 return !bversion_cmp(v, ZERO_VERSION);
237 #ifdef CONFIG_BCACHEFS_DEBUG
238 /* statement expressions confusing unlikely()? */
239 #define bkey_packed(_k) \
240 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
241 (_k)->format != KEY_FORMAT_CURRENT; })
243 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
247 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
249 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
251 return (struct bkey_packed *) k;
254 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
256 return (const struct bkey_packed *) k;
259 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
261 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
264 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
266 return bkey_packed(k) ? NULL : (const struct bkey *) k;
269 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
271 return format->bits_per_field[BKEY_FIELD_INODE] +
272 format->bits_per_field[BKEY_FIELD_OFFSET] +
273 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
276 static inline struct bpos bkey_successor(struct bpos p)
281 BUG_ON(!++ret.inode);
286 static inline u64 bkey_start_offset(const struct bkey *k)
288 return k->p.offset - k->size;
291 static inline struct bpos bkey_start_pos(const struct bkey *k)
293 return (struct bpos) {
295 .offset = bkey_start_offset(k),
296 .snapshot = k->p.snapshot,
302 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
303 const struct bkey_packed *k)
305 unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
307 EBUG_ON(k->u64s < ret);
311 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
312 const struct bkey_packed *k)
314 return bkeyp_key_u64s(format, k) * sizeof(u64);
317 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
318 const struct bkey_packed *k)
320 return k->u64s - bkeyp_key_u64s(format, k);
323 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
324 const struct bkey_packed *k)
326 return bkeyp_val_u64s(format, k) * sizeof(u64);
329 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
330 struct bkey_packed *k, unsigned val_u64s)
332 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
335 #define bkeyp_val(_format, _k) \
336 ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
338 extern const struct bkey_format bch2_bkey_format_current;
340 bool bch2_bkey_transform(const struct bkey_format *,
341 struct bkey_packed *,
342 const struct bkey_format *,
343 const struct bkey_packed *);
345 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
346 const struct bkey_packed *);
348 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
349 struct bpos __bkey_unpack_pos(const struct bkey_format *,
350 const struct bkey_packed *);
353 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
354 const struct bkey_format *);
356 enum bkey_pack_pos_ret {
358 BKEY_PACK_POS_SMALLER,
362 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
363 const struct btree *);
365 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
366 const struct btree *b)
368 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
371 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
372 const struct bkey_packed *);
373 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
374 const struct bkey_format *);
376 static inline u64 bkey_field_max(const struct bkey_format *f,
377 enum bch_bkey_fields nr)
379 return f->bits_per_field[nr] < 64
380 ? f->field_offset[nr] + ~(~0ULL << f->bits_per_field[nr])
385 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
387 int bch2_compile_bkey_format(const struct bkey_format *, void *);
391 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
392 void *out) { return 0; }
396 static inline void bkey_reassemble(struct bkey_i *dst,
399 BUG_ON(bkey_packed(src.k));
401 memcpy_u64s(&dst->v, src.v, bkey_val_u64s(src.k));
404 #define bkey_s_null ((struct bkey_s) { .k = NULL })
405 #define bkey_s_c_null ((struct bkey_s_c) { .k = NULL })
407 #define bkey_s_err(err) ((struct bkey_s) { .k = ERR_PTR(err) })
408 #define bkey_s_c_err(err) ((struct bkey_s_c) { .k = ERR_PTR(err) })
410 static inline struct bkey_s bkey_to_s(struct bkey *k)
412 return (struct bkey_s) { .k = k, .v = NULL };
415 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
417 return (struct bkey_s_c) { .k = k, .v = NULL };
420 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
422 return (struct bkey_s) { .k = &k->k, .v = &k->v };
425 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
427 return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
431 * For a given type of value (e.g. struct bch_extent), generates the types for
432 * bkey + bch_extent - inline, split, split const - and also all the conversion
433 * functions, which also check that the value is of the correct type.
435 * We use anonymous unions for upcasting - e.g. converting from e.g. a
436 * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
439 #define __BKEY_VAL_ACCESSORS(name, nr, _assert) \
440 struct bkey_s_c_##name { \
443 const struct bkey *k; \
444 const struct bch_##name *v; \
446 struct bkey_s_c s_c; \
450 struct bkey_s_##name { \
454 struct bch_##name *v; \
456 struct bkey_s_c_##name c; \
458 struct bkey_s_c s_c; \
462 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k) \
464 _assert(k->k.type, nr); \
465 return container_of(&k->k, struct bkey_i_##name, k); \
468 static inline const struct bkey_i_##name * \
469 bkey_i_to_##name##_c(const struct bkey_i *k) \
471 _assert(k->k.type, nr); \
472 return container_of(&k->k, struct bkey_i_##name, k); \
475 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k) \
477 _assert(k.k->type, nr); \
478 return (struct bkey_s_##name) { \
480 .v = container_of(k.v, struct bch_##name, v), \
484 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
486 _assert(k.k->type, nr); \
487 return (struct bkey_s_c_##name) { \
489 .v = container_of(k.v, struct bch_##name, v), \
493 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
495 return (struct bkey_s_##name) { \
501 static inline struct bkey_s_c_##name \
502 name##_i_to_s_c(const struct bkey_i_##name *k) \
504 return (struct bkey_s_c_##name) { \
510 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
512 _assert(k->k.type, nr); \
513 return (struct bkey_s_##name) { \
515 .v = container_of(&k->v, struct bch_##name, v), \
519 static inline struct bkey_s_c_##name \
520 bkey_i_to_s_c_##name(const struct bkey_i *k) \
522 _assert(k->k.type, nr); \
523 return (struct bkey_s_c_##name) { \
525 .v = container_of(&k->v, struct bch_##name, v), \
529 static inline struct bch_##name * \
530 bkey_p_##name##_val(const struct bkey_format *f, \
531 struct bkey_packed *k) \
533 return container_of(bkeyp_val(f, k), struct bch_##name, v); \
536 static inline const struct bch_##name * \
537 bkey_p_c_##name##_val(const struct bkey_format *f, \
538 const struct bkey_packed *k) \
540 return container_of(bkeyp_val(f, k), struct bch_##name, v); \
543 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
545 struct bkey_i_##name *k = \
546 container_of(&_k->k, struct bkey_i_##name, k); \
549 memset(&k->v, 0, sizeof(k->v)); \
551 set_bkey_val_bytes(&k->k, sizeof(k->v)); \
556 #define __BKEY_VAL_ASSERT(_type, _nr) EBUG_ON(_type != _nr)
558 #define BKEY_VAL_ACCESSORS(name, _nr) \
559 static inline void __bch_##name##_assert(u8 type, u8 nr) \
561 EBUG_ON(type != _nr); \
564 __BKEY_VAL_ACCESSORS(name, _nr, __bch_##name##_assert)
566 BKEY_VAL_ACCESSORS(cookie, KEY_TYPE_COOKIE);
568 static inline void __bch2_extent_assert(u8 type, u8 nr)
570 EBUG_ON(type != BCH_EXTENT && type != BCH_EXTENT_CACHED);
573 __BKEY_VAL_ACCESSORS(extent, BCH_EXTENT, __bch2_extent_assert);
574 BKEY_VAL_ACCESSORS(reservation, BCH_RESERVATION);
576 BKEY_VAL_ACCESSORS(inode, BCH_INODE_FS);
577 BKEY_VAL_ACCESSORS(inode_blockdev, BCH_INODE_BLOCKDEV);
578 BKEY_VAL_ACCESSORS(inode_generation, BCH_INODE_GENERATION);
580 BKEY_VAL_ACCESSORS(dirent, BCH_DIRENT);
582 BKEY_VAL_ACCESSORS(xattr, BCH_XATTR);
584 BKEY_VAL_ACCESSORS(alloc, BCH_ALLOC);
586 /* byte order helpers */
588 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
589 #error edit for your odd byteorder.
592 #ifdef __LITTLE_ENDIAN
594 #define high_bit_offset 0
595 #define __high_word(u64s, k) ((k)->_data + (u64s) - 1)
596 #define nth_word(p, n) ((p) - (n))
600 #define high_bit_offset KEY_PACKED_BITS_START
601 #define __high_word(u64s, k) ((k)->_data)
602 #define nth_word(p, n) ((p) + (n))
606 #define high_word(format, k) __high_word((format)->key_u64s, k)
607 #define next_word(p) nth_word(p, 1)
608 #define prev_word(p) nth_word(p, -1)
610 #ifdef CONFIG_BCACHEFS_DEBUG
611 void bch2_bkey_pack_test(void);
613 static inline void bch2_bkey_pack_test(void) {}
616 #endif /* _BCACHEFS_BKEY_H */