Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _BCACHEFS_BKEY_H
3#define _BCACHEFS_BKEY_H
4
5#include <linux/bug.h>
6#include "bcachefs_format.h"
7#include "bkey_types.h"
8#include "btree_types.h"
9#include "util.h"
10#include "vstructs.h"
11
12#if 0
13
14/*
15 * compiled unpack functions are disabled, pending a new interface for
16 * dynamically allocating executable memory:
17 */
18
19#ifdef CONFIG_X86_64
20#define HAVE_BCACHEFS_COMPILED_UNPACK 1
21#endif
22#endif
23
24void bch2_bkey_packed_to_binary_text(struct printbuf *,
25 const struct bkey_format *,
26 const struct bkey_packed *);
27
28enum bkey_lr_packed {
29 BKEY_PACKED_BOTH,
30 BKEY_PACKED_RIGHT,
31 BKEY_PACKED_LEFT,
32 BKEY_PACKED_NONE,
33};
34
35#define bkey_lr_packed(_l, _r) \
36 ((_l)->format + ((_r)->format << 1))
37
38static inline void bkey_p_copy(struct bkey_packed *dst, const struct bkey_packed *src)
39{
40 memcpy_u64s_small(dst, src, src->u64s);
41}
42
43static inline void bkey_copy(struct bkey_i *dst, const struct bkey_i *src)
44{
45 memcpy_u64s_small(dst, src, src->k.u64s);
46}
47
48struct btree;
49
50__pure
51unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
52 const struct bkey_packed *,
53 const struct bkey_packed *);
54__pure
55unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
56
57__pure
58int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
59 const struct bkey_packed *,
60 const struct btree *);
61
62__pure
63int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
64 const struct bkey_packed *,
65 const struct bpos *);
66
67__pure
68int bch2_bkey_cmp_packed(const struct btree *,
69 const struct bkey_packed *,
70 const struct bkey_packed *);
71
72__pure
73int __bch2_bkey_cmp_left_packed(const struct btree *,
74 const struct bkey_packed *,
75 const struct bpos *);
76
77static inline __pure
78int bkey_cmp_left_packed(const struct btree *b,
79 const struct bkey_packed *l, const struct bpos *r)
80{
81 return __bch2_bkey_cmp_left_packed(b, l, r);
82}
83
84/*
85 * The compiler generates better code when we pass bpos by ref, but it's often
86 * enough terribly convenient to pass it by val... as much as I hate c++, const
87 * ref would be nice here:
88 */
89__pure __flatten
90static inline int bkey_cmp_left_packed_byval(const struct btree *b,
91 const struct bkey_packed *l,
92 struct bpos r)
93{
94 return bkey_cmp_left_packed(b, l, &r);
95}
96
97static __always_inline bool bpos_eq(struct bpos l, struct bpos r)
98{
99 return !((l.inode ^ r.inode) |
100 (l.offset ^ r.offset) |
101 (l.snapshot ^ r.snapshot));
102}
103
104static __always_inline bool bpos_lt(struct bpos l, struct bpos r)
105{
106 return l.inode != r.inode ? l.inode < r.inode :
107 l.offset != r.offset ? l.offset < r.offset :
108 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false;
109}
110
111static __always_inline bool bpos_le(struct bpos l, struct bpos r)
112{
113 return l.inode != r.inode ? l.inode < r.inode :
114 l.offset != r.offset ? l.offset < r.offset :
115 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true;
116}
117
118static __always_inline bool bpos_gt(struct bpos l, struct bpos r)
119{
120 return bpos_lt(r, l);
121}
122
123static __always_inline bool bpos_ge(struct bpos l, struct bpos r)
124{
125 return bpos_le(r, l);
126}
127
128static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
129{
130 return cmp_int(l.inode, r.inode) ?:
131 cmp_int(l.offset, r.offset) ?:
132 cmp_int(l.snapshot, r.snapshot);
133}
134
135static inline struct bpos bpos_min(struct bpos l, struct bpos r)
136{
137 return bpos_lt(l, r) ? l : r;
138}
139
140static inline struct bpos bpos_max(struct bpos l, struct bpos r)
141{
142 return bpos_gt(l, r) ? l : r;
143}
144
145static __always_inline bool bkey_eq(struct bpos l, struct bpos r)
146{
147 return !((l.inode ^ r.inode) |
148 (l.offset ^ r.offset));
149}
150
151static __always_inline bool bkey_lt(struct bpos l, struct bpos r)
152{
153 return l.inode != r.inode
154 ? l.inode < r.inode
155 : l.offset < r.offset;
156}
157
158static __always_inline bool bkey_le(struct bpos l, struct bpos r)
159{
160 return l.inode != r.inode
161 ? l.inode < r.inode
162 : l.offset <= r.offset;
163}
164
165static __always_inline bool bkey_gt(struct bpos l, struct bpos r)
166{
167 return bkey_lt(r, l);
168}
169
170static __always_inline bool bkey_ge(struct bpos l, struct bpos r)
171{
172 return bkey_le(r, l);
173}
174
175static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
176{
177 return cmp_int(l.inode, r.inode) ?:
178 cmp_int(l.offset, r.offset);
179}
180
181static inline struct bpos bkey_min(struct bpos l, struct bpos r)
182{
183 return bkey_lt(l, r) ? l : r;
184}
185
186static inline struct bpos bkey_max(struct bpos l, struct bpos r)
187{
188 return bkey_gt(l, r) ? l : r;
189}
190
191static inline bool bkey_and_val_eq(struct bkey_s_c l, struct bkey_s_c r)
192{
193 return bpos_eq(l.k->p, r.k->p) &&
194 bkey_bytes(l.k) == bkey_bytes(r.k) &&
195 !memcmp(l.v, r.v, bkey_val_bytes(l.k));
196}
197
198void bch2_bpos_swab(struct bpos *);
199void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
200
201static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
202{
203 return cmp_int(l.hi, r.hi) ?:
204 cmp_int(l.lo, r.lo);
205}
206
207#define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 })
208#define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL })
209
210static __always_inline bool bversion_zero(struct bversion v)
211{
212 return bversion_cmp(v, ZERO_VERSION) == 0;
213}
214
215#ifdef CONFIG_BCACHEFS_DEBUG
216/* statement expressions confusing unlikely()? */
217#define bkey_packed(_k) \
218 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \
219 (_k)->format != KEY_FORMAT_CURRENT; })
220#else
221#define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT)
222#endif
223
224/*
225 * It's safe to treat an unpacked bkey as a packed one, but not the reverse
226 */
227static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
228{
229 return (struct bkey_packed *) k;
230}
231
232static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
233{
234 return (const struct bkey_packed *) k;
235}
236
237static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
238{
239 return bkey_packed(k) ? NULL : (struct bkey_i *) k;
240}
241
242static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
243{
244 return bkey_packed(k) ? NULL : (const struct bkey *) k;
245}
246
247static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
248{
249 return format->bits_per_field[BKEY_FIELD_INODE] +
250 format->bits_per_field[BKEY_FIELD_OFFSET] +
251 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
252}
253
254static inline struct bpos bpos_successor(struct bpos p)
255{
256 if (!++p.snapshot &&
257 !++p.offset &&
258 !++p.inode)
259 BUG();
260
261 return p;
262}
263
264static inline struct bpos bpos_predecessor(struct bpos p)
265{
266 if (!p.snapshot-- &&
267 !p.offset-- &&
268 !p.inode--)
269 BUG();
270
271 return p;
272}
273
274static inline struct bpos bpos_nosnap_successor(struct bpos p)
275{
276 p.snapshot = 0;
277
278 if (!++p.offset &&
279 !++p.inode)
280 BUG();
281
282 return p;
283}
284
285static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
286{
287 p.snapshot = 0;
288
289 if (!p.offset-- &&
290 !p.inode--)
291 BUG();
292
293 return p;
294}
295
296static inline u64 bkey_start_offset(const struct bkey *k)
297{
298 return k->p.offset - k->size;
299}
300
301static inline struct bpos bkey_start_pos(const struct bkey *k)
302{
303 return (struct bpos) {
304 .inode = k->p.inode,
305 .offset = bkey_start_offset(k),
306 .snapshot = k->p.snapshot,
307 };
308}
309
310/* Packed helpers */
311
312static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
313 const struct bkey_packed *k)
314{
315 return bkey_packed(k) ? format->key_u64s : BKEY_U64s;
316}
317
318static inline bool bkeyp_u64s_valid(const struct bkey_format *f,
319 const struct bkey_packed *k)
320{
321 return ((unsigned) k->u64s - bkeyp_key_u64s(f, k) <= U8_MAX - BKEY_U64s);
322}
323
324static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
325 const struct bkey_packed *k)
326{
327 return bkeyp_key_u64s(format, k) * sizeof(u64);
328}
329
330static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
331 const struct bkey_packed *k)
332{
333 return k->u64s - bkeyp_key_u64s(format, k);
334}
335
336static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
337 const struct bkey_packed *k)
338{
339 return bkeyp_val_u64s(format, k) * sizeof(u64);
340}
341
342static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
343 struct bkey_packed *k, unsigned val_u64s)
344{
345 k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
346}
347
348#define bkeyp_val(_format, _k) \
349 ((struct bch_val *) ((u64 *) (_k)->_data + bkeyp_key_u64s(_format, _k)))
350
351extern const struct bkey_format bch2_bkey_format_current;
352
353bool bch2_bkey_transform(const struct bkey_format *,
354 struct bkey_packed *,
355 const struct bkey_format *,
356 const struct bkey_packed *);
357
358struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
359 const struct bkey_packed *);
360
361#ifndef HAVE_BCACHEFS_COMPILED_UNPACK
362struct bpos __bkey_unpack_pos(const struct bkey_format *,
363 const struct bkey_packed *);
364#endif
365
366bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
367 const struct bkey_format *);
368
369enum bkey_pack_pos_ret {
370 BKEY_PACK_POS_EXACT,
371 BKEY_PACK_POS_SMALLER,
372 BKEY_PACK_POS_FAIL,
373};
374
375enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
376 const struct btree *);
377
378static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
379 const struct btree *b)
380{
381 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
382}
383
384void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
385 const struct bkey_packed *);
386bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
387 const struct bkey_format *);
388
389typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
390
391static inline void
392__bkey_unpack_key_format_checked(const struct btree *b,
393 struct bkey *dst,
394 const struct bkey_packed *src)
395{
396 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
397 compiled_unpack_fn unpack_fn = b->aux_data;
398 unpack_fn(dst, src);
399
400 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
401 bch2_expensive_debug_checks) {
402 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
403
404 BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
405 }
406 } else {
407 *dst = __bch2_bkey_unpack_key(&b->format, src);
408 }
409}
410
411static inline struct bkey
412bkey_unpack_key_format_checked(const struct btree *b,
413 const struct bkey_packed *src)
414{
415 struct bkey dst;
416
417 __bkey_unpack_key_format_checked(b, &dst, src);
418 return dst;
419}
420
421static inline void __bkey_unpack_key(const struct btree *b,
422 struct bkey *dst,
423 const struct bkey_packed *src)
424{
425 if (likely(bkey_packed(src)))
426 __bkey_unpack_key_format_checked(b, dst, src);
427 else
428 *dst = *packed_to_bkey_c(src);
429}
430
431/**
432 * bkey_unpack_key -- unpack just the key, not the value
433 */
434static inline struct bkey bkey_unpack_key(const struct btree *b,
435 const struct bkey_packed *src)
436{
437 return likely(bkey_packed(src))
438 ? bkey_unpack_key_format_checked(b, src)
439 : *packed_to_bkey_c(src);
440}
441
442static inline struct bpos
443bkey_unpack_pos_format_checked(const struct btree *b,
444 const struct bkey_packed *src)
445{
446#ifdef HAVE_BCACHEFS_COMPILED_UNPACK
447 return bkey_unpack_key_format_checked(b, src).p;
448#else
449 return __bkey_unpack_pos(&b->format, src);
450#endif
451}
452
453static inline struct bpos bkey_unpack_pos(const struct btree *b,
454 const struct bkey_packed *src)
455{
456 return likely(bkey_packed(src))
457 ? bkey_unpack_pos_format_checked(b, src)
458 : packed_to_bkey_c(src)->p;
459}
460
461/* Disassembled bkeys */
462
463static inline struct bkey_s_c bkey_disassemble(const struct btree *b,
464 const struct bkey_packed *k,
465 struct bkey *u)
466{
467 __bkey_unpack_key(b, u, k);
468
469 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
470}
471
472/* non const version: */
473static inline struct bkey_s __bkey_disassemble(const struct btree *b,
474 struct bkey_packed *k,
475 struct bkey *u)
476{
477 __bkey_unpack_key(b, u, k);
478
479 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
480}
481
482static inline u64 bkey_field_max(const struct bkey_format *f,
483 enum bch_bkey_fields nr)
484{
485 return f->bits_per_field[nr] < 64
486 ? (le64_to_cpu(f->field_offset[nr]) +
487 ~(~0ULL << f->bits_per_field[nr]))
488 : U64_MAX;
489}
490
491#ifdef HAVE_BCACHEFS_COMPILED_UNPACK
492
493int bch2_compile_bkey_format(const struct bkey_format *, void *);
494
495#else
496
497static inline int bch2_compile_bkey_format(const struct bkey_format *format,
498 void *out) { return 0; }
499
500#endif
501
502static inline void bkey_reassemble(struct bkey_i *dst,
503 struct bkey_s_c src)
504{
505 dst->k = *src.k;
506 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
507}
508
509/* byte order helpers */
510
511#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
512
513static inline unsigned high_word_offset(const struct bkey_format *f)
514{
515 return f->key_u64s - 1;
516}
517
518#define high_bit_offset 0
519#define nth_word(p, n) ((p) - (n))
520
521#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
522
523static inline unsigned high_word_offset(const struct bkey_format *f)
524{
525 return 0;
526}
527
528#define high_bit_offset KEY_PACKED_BITS_START
529#define nth_word(p, n) ((p) + (n))
530
531#else
532#error edit for your odd byteorder.
533#endif
534
535#define high_word(f, k) ((u64 *) (k)->_data + high_word_offset(f))
536#define next_word(p) nth_word(p, 1)
537#define prev_word(p) nth_word(p, -1)
538
539#ifdef CONFIG_BCACHEFS_DEBUG
540void bch2_bkey_pack_test(void);
541#else
542static inline void bch2_bkey_pack_test(void) {}
543#endif
544
545#define bkey_fields() \
546 x(BKEY_FIELD_INODE, p.inode) \
547 x(BKEY_FIELD_OFFSET, p.offset) \
548 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \
549 x(BKEY_FIELD_SIZE, size) \
550 x(BKEY_FIELD_VERSION_HI, bversion.hi) \
551 x(BKEY_FIELD_VERSION_LO, bversion.lo)
552
553struct bkey_format_state {
554 u64 field_min[BKEY_NR_FIELDS];
555 u64 field_max[BKEY_NR_FIELDS];
556};
557
558void bch2_bkey_format_init(struct bkey_format_state *);
559
560static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v)
561{
562 s->field_min[field] = min(s->field_min[field], v);
563 s->field_max[field] = max(s->field_max[field], v);
564}
565
566/*
567 * Changes @format so that @k can be successfully packed with @format
568 */
569static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k)
570{
571#define x(id, field) __bkey_format_add(s, id, k->field);
572 bkey_fields()
573#undef x
574}
575
576void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
577struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
578
579static inline bool bch2_bkey_format_field_overflows(struct bkey_format *f, unsigned i)
580{
581 unsigned f_bits = f->bits_per_field[i];
582 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
583 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
584 u64 field_offset = le64_to_cpu(f->field_offset[i]);
585
586 if (f_bits > unpacked_bits)
587 return true;
588
589 if ((f_bits == unpacked_bits) && field_offset)
590 return true;
591
592 u64 f_mask = f_bits
593 ? ~((~0ULL << (f_bits - 1)) << 1)
594 : 0;
595
596 if (((field_offset + f_mask) & unpacked_mask) < field_offset)
597 return true;
598 return false;
599}
600
601int bch2_bkey_format_invalid(struct bch_fs *, struct bkey_format *,
602 enum bch_validate_flags, struct printbuf *);
603void bch2_bkey_format_to_text(struct printbuf *, const struct bkey_format *);
604
605#endif /* _BCACHEFS_BKEY_H */