1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_H
3 #define _BCACHEFS_BKEY_H
6 #include "bcachefs_format.h"
7 #include "bkey_types.h"
8 #include "btree_types.h"
12 enum bkey_invalid_flags {
13 BKEY_INVALID_WRITE = (1U << 0),
14 BKEY_INVALID_COMMIT = (1U << 1),
15 BKEY_INVALID_JOURNAL = (1U << 2),
21 * compiled unpack functions are disabled, pending a new interface for
22 * dynamically allocating executable memory:
26 #define HAVE_BCACHEFS_COMPILED_UNPACK 1
30 void bch2_bkey_packed_to_binary_text(struct printbuf *,
31 const struct bkey_format *,
32 const struct bkey_packed *);
41 #define bkey_lr_packed(_l, _r) \
42 ((_l)->format + ((_r)->format << 1))
44 static inline void bkey_p_copy(struct bkey_packed *dst, const struct bkey_packed *src)
46 memcpy_u64s_small(dst, src, src->u64s);
49 static inline void bkey_copy(struct bkey_i *dst, const struct bkey_i *src)
51 memcpy_u64s_small(dst, src, src->k.u64s);
57 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
58 const struct bkey_packed *,
59 const struct bkey_packed *);
61 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
64 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
65 const struct bkey_packed *,
66 const struct btree *);
69 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
70 const struct bkey_packed *,
74 int bch2_bkey_cmp_packed(const struct btree *,
75 const struct bkey_packed *,
76 const struct bkey_packed *);
79 int __bch2_bkey_cmp_left_packed(const struct btree *,
80 const struct bkey_packed *,
84 int bkey_cmp_left_packed(const struct btree *b,
85 const struct bkey_packed *l, const struct bpos *r)
87 return __bch2_bkey_cmp_left_packed(b, l, r);
91 * The compiler generates better code when we pass bpos by ref, but it's often
92 * enough terribly convenient to pass it by val... as much as I hate c++, const
93 * ref would be nice here:
96 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
97 const struct bkey_packed *l,
100 return bkey_cmp_left_packed(b, l, &r);
103 static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
105 return !((l.inode ^ r.inode) |
106 (l.offset ^ r.offset) |
107 (l.snapshot ^ r.snapshot));
110 static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
112 return l.inode != r.inode ? l.inode < r.inode :
113 l.offset != r.offset ? l.offset < r.offset :
114 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
117 static __always_inline bool bpos_le(struct bpos l, struct bpos r)
119 return l.inode != r.inode ? l.inode < r.inode :
120 l.offset != r.offset ? l.offset < r.offset :
121 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
124 static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
126 return bpos_lt(r, l);
129 static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
131 return bpos_le(r, l);
134 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
136 return cmp_int(l.inode, r.inode) ?:
137 cmp_int(l.offset, r.offset) ?:
138 cmp_int(l.snapshot, r.snapshot);
141 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
143 return bpos_lt(l, r) ? l : r;
146 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
148 return bpos_gt(l, r) ? l : r;
151 static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
153 return !((l.inode ^ r.inode) |
154 (l.offset ^ r.offset));
157 static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
159 return l.inode != r.inode
161 : l.offset < r.offset;
164 static __always_inline bool bkey_le(struct bpos l, struct bpos r)
166 return l.inode != r.inode
168 : l.offset <= r.offset;
171 static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
173 return bkey_lt(r, l);
176 static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
178 return bkey_le(r, l);
181 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
183 return cmp_int(l.inode, r.inode) ?:
184 cmp_int(l.offset, r.offset);
187 static inline struct bpos bkey_min(struct bpos l, struct bpos r)
189 return bkey_lt(l, r) ? l : r;
192 static inline struct bpos bkey_max(struct bpos l, struct bpos r)
194 return bkey_gt(l, r) ? l : r;
197 void bch2_bpos_swab(struct bpos *);
198 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
200 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
202 return cmp_int(l.hi, r.hi) ?:
206 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
207 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
209 static __always_inline int bversion_zero(struct bversion v)
211 return !bversion_cmp(v, ZERO_VERSION);
214 #ifdef CONFIG_BCACHEFS_DEBUG
215 /* statement expressions confusing unlikely()? */
216 #define bkey_packed(_k) \
217 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
218 (_k)->format != KEY_FORMAT_CURRENT; })
220 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
224 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
226 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
228 return (struct bkey_packed *) k;
231 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
233 return (const struct bkey_packed *) k;
236 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
238 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
241 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
243 return bkey_packed(k) ? NULL : (const struct bkey *) k;
246 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
248 return format->bits_per_field[BKEY_FIELD_INODE] +
249 format->bits_per_field[BKEY_FIELD_OFFSET] +
250 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
253 static inline struct bpos bpos_successor(struct bpos p)
263 static inline struct bpos bpos_predecessor(struct bpos p)
273 static inline struct bpos bpos_nosnap_successor(struct bpos p)
284 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
295 static inline u64 bkey_start_offset(const struct bkey *k)
297 return k->p.offset - k->size;
300 static inline struct bpos bkey_start_pos(const struct bkey *k)
302 return (struct bpos) {
304 .offset = bkey_start_offset(k),
305 .snapshot = k->p.snapshot,
311 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
312 const struct bkey_packed *k)
314 return bkey_packed(k) ? format->key_u64s : BKEY_U64s;
317 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
318 const struct bkey_packed *k)
320 return bkeyp_key_u64s(format, k) * sizeof(u64);
323 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
324 const struct bkey_packed *k)
326 return k->u64s - bkeyp_key_u64s(format, k);
329 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
330 const struct bkey_packed *k)
332 return bkeyp_val_u64s(format, k) * sizeof(u64);
335 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
336 struct bkey_packed *k, unsigned val_u64s)
338 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
341 #define bkeyp_val(_format, _k) \
342 ((struct bch_val *) ((u64 *) (_k)->_data + bkeyp_key_u64s(_format, _k)))
344 extern const struct bkey_format bch2_bkey_format_current;
346 bool bch2_bkey_transform(const struct bkey_format *,
347 struct bkey_packed *,
348 const struct bkey_format *,
349 const struct bkey_packed *);
351 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
352 const struct bkey_packed *);
354 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
355 struct bpos __bkey_unpack_pos(const struct bkey_format *,
356 const struct bkey_packed *);
359 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
360 const struct bkey_format *);
362 enum bkey_pack_pos_ret {
364 BKEY_PACK_POS_SMALLER,
368 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
369 const struct btree *);
371 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
372 const struct btree *b)
374 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
377 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
378 const struct bkey_packed *);
379 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
380 const struct bkey_format *);
382 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
385 __bkey_unpack_key_format_checked(const struct btree *b,
387 const struct bkey_packed *src)
389 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
390 compiled_unpack_fn unpack_fn = b->aux_data;
393 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
394 bch2_expensive_debug_checks) {
395 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
397 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
400 *dst = __bch2_bkey_unpack_key(&b->format, src);
404 static inline struct bkey
405 bkey_unpack_key_format_checked(const struct btree *b,
406 const struct bkey_packed *src)
410 __bkey_unpack_key_format_checked(b, &dst, src);
414 static inline void __bkey_unpack_key(const struct btree *b,
416 const struct bkey_packed *src)
418 if (likely(bkey_packed(src)))
419 __bkey_unpack_key_format_checked(b, dst, src);
421 *dst = *packed_to_bkey_c(src);
425 * bkey_unpack_key -- unpack just the key, not the value
427 static inline struct bkey bkey_unpack_key(const struct btree *b,
428 const struct bkey_packed *src)
430 return likely(bkey_packed(src))
431 ? bkey_unpack_key_format_checked(b, src)
432 : *packed_to_bkey_c(src);
435 static inline struct bpos
436 bkey_unpack_pos_format_checked(const struct btree *b,
437 const struct bkey_packed *src)
439 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
440 return bkey_unpack_key_format_checked(b, src).p;
442 return __bkey_unpack_pos(&b->format, src);
446 static inline struct bpos bkey_unpack_pos(const struct btree *b,
447 const struct bkey_packed *src)
449 return likely(bkey_packed(src))
450 ? bkey_unpack_pos_format_checked(b, src)
451 : packed_to_bkey_c(src)->p;
454 /* Disassembled bkeys */
456 static inline struct bkey_s_c bkey_disassemble(const struct btree *b,
457 const struct bkey_packed *k,
460 __bkey_unpack_key(b, u, k);
462 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
465 /* non const version: */
466 static inline struct bkey_s __bkey_disassemble(const struct btree *b,
467 struct bkey_packed *k,
470 __bkey_unpack_key(b, u, k);
472 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
475 static inline u64 bkey_field_max(const struct bkey_format *f,
476 enum bch_bkey_fields nr)
478 return f->bits_per_field[nr] < 64
479 ? (le64_to_cpu(f->field_offset[nr]) +
480 ~(~0ULL << f->bits_per_field[nr]))
484 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
486 int bch2_compile_bkey_format(const struct bkey_format *, void *);
490 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
491 void *out) { return 0; }
495 static inline void bkey_reassemble(struct bkey_i *dst,
499 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
502 /* byte order helpers */
504 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
506 static inline unsigned high_word_offset(const struct bkey_format *f)
508 return f->key_u64s - 1;
511 #define high_bit_offset 0
512 #define nth_word(p, n) ((p) - (n))
514 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
516 static inline unsigned high_word_offset(const struct bkey_format *f)
521 #define high_bit_offset KEY_PACKED_BITS_START
522 #define nth_word(p, n) ((p) + (n))
525 #error edit for your odd byteorder.
528 #define high_word(f, k) ((u64 *) (k)->_data + high_word_offset(f))
529 #define next_word(p) nth_word(p, 1)
530 #define prev_word(p) nth_word(p, -1)
532 #ifdef CONFIG_BCACHEFS_DEBUG
533 void bch2_bkey_pack_test(void);
535 static inline void bch2_bkey_pack_test(void) {}
538 #define bkey_fields() \
539 x(BKEY_FIELD_INODE, p.inode) \
540 x(BKEY_FIELD_OFFSET, p.offset) \
541 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
542 x(BKEY_FIELD_SIZE, size) \
543 x(BKEY_FIELD_VERSION_HI, version.hi) \
544 x(BKEY_FIELD_VERSION_LO, version.lo)
546 struct bkey_format_state {
547 u64 field_min[BKEY_NR_FIELDS];
548 u64 field_max[BKEY_NR_FIELDS];
551 void bch2_bkey_format_init(struct bkey_format_state *);
553 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
555 s->field_min[field] = min(s->field_min[field], v);
556 s->field_max[field] = max(s->field_max[field], v);
560 * Changes @format so that @k can be successfully packed with @format
562 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
564 #define x(id, field) __bkey_format_add(s, id, k->field);
569 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
570 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
571 int bch2_bkey_format_invalid(struct bch_fs *, struct bkey_format *,
572 enum bkey_invalid_flags, struct printbuf *);
573 void bch2_bkey_format_to_text(struct printbuf *, const struct bkey_format *);
575 #endif /* _BCACHEFS_BKEY_H */