]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey.h
19b59ffe0a98fbde8828feb9be1d76641a13e9ab
[bcachefs-tools-debian] / libbcachefs / bkey.h
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
8 #include "btree_types.h"
9 #include "util.h"
10 #include "vstructs.h"
11
12 #ifdef CONFIG_X86_64
13 #define HAVE_BCACHEFS_COMPILED_UNPACK   1
14 #endif
15
16 void bch2_bkey_packed_to_binary_text(struct printbuf *,
17                                      const struct bkey_format *,
18                                      const struct bkey_packed *);
19
20 /* bkey with split value, const */
21 struct bkey_s_c {
22         const struct bkey       *k;
23         const struct bch_val    *v;
24 };
25
26 /* bkey with split value */
27 struct bkey_s {
28         union {
29         struct {
30                 struct bkey     *k;
31                 struct bch_val  *v;
32         };
33         struct bkey_s_c         s_c;
34         };
35 };
36
37 #define bkey_next(_k)           vstruct_next(_k)
38
39 #define bkey_val_u64s(_k)       ((_k)->u64s - BKEY_U64s)
40
41 static inline size_t bkey_val_bytes(const struct bkey *k)
42 {
43         return bkey_val_u64s(k) * sizeof(u64);
44 }
45
46 static inline void set_bkey_val_u64s(struct bkey *k, unsigned val_u64s)
47 {
48         unsigned u64s = BKEY_U64s + val_u64s;
49
50         BUG_ON(u64s > U8_MAX);
51         k->u64s = u64s;
52 }
53
54 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
55 {
56         set_bkey_val_u64s(k, DIV_ROUND_UP(bytes, sizeof(u64)));
57 }
58
59 #define bkey_val_end(_k)        ((void *) (((u64 *) (_k).v) + bkey_val_u64s((_k).k)))
60
61 #define bkey_deleted(_k)        ((_k)->type == KEY_TYPE_deleted)
62
63 #define bkey_whiteout(_k)                               \
64         ((_k)->type == KEY_TYPE_deleted || (_k)->type == KEY_TYPE_whiteout)
65
66 enum bkey_lr_packed {
67         BKEY_PACKED_BOTH,
68         BKEY_PACKED_RIGHT,
69         BKEY_PACKED_LEFT,
70         BKEY_PACKED_NONE,
71 };
72
73 #define bkey_lr_packed(_l, _r)                                          \
74         ((_l)->format + ((_r)->format << 1))
75
76 #define bkey_copy(_dst, _src)                                   \
77 do {                                                            \
78         BUILD_BUG_ON(!type_is(_dst, struct bkey_i *) &&         \
79                      !type_is(_dst, struct bkey_packed *));     \
80         BUILD_BUG_ON(!type_is(_src, struct bkey_i *) &&         \
81                      !type_is(_src, struct bkey_packed *));     \
82         EBUG_ON((u64 *) (_dst) > (u64 *) (_src) &&              \
83                 (u64 *) (_dst) < (u64 *) (_src) +               \
84                 ((struct bkey *) (_src))->u64s);                \
85                                                                 \
86         memcpy_u64s_small((_dst), (_src),                       \
87                           ((struct bkey *) (_src))->u64s);      \
88 } while (0)
89
90 struct btree;
91
92 struct bkey_format_state {
93         u64 field_min[BKEY_NR_FIELDS];
94         u64 field_max[BKEY_NR_FIELDS];
95 };
96
97 void bch2_bkey_format_init(struct bkey_format_state *);
98 void bch2_bkey_format_add_key(struct bkey_format_state *, const struct bkey *);
99 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos);
100 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *);
101 const char *bch2_bkey_format_validate(struct bkey_format *);
102
103 __pure
104 unsigned bch2_bkey_greatest_differing_bit(const struct btree *,
105                                           const struct bkey_packed *,
106                                           const struct bkey_packed *);
107 __pure
108 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *);
109
110 __pure
111 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *,
112                                      const struct bkey_packed *,
113                                      const struct btree *);
114
115 __pure
116 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *,
117                                           const struct bkey_packed *,
118                                           const struct bpos *);
119
120 __pure
121 int bch2_bkey_cmp_packed(const struct btree *,
122                          const struct bkey_packed *,
123                          const struct bkey_packed *);
124
125 __pure
126 int __bch2_bkey_cmp_left_packed(const struct btree *,
127                                 const struct bkey_packed *,
128                                 const struct bpos *);
129
130 static inline __pure
131 int bkey_cmp_left_packed(const struct btree *b,
132                          const struct bkey_packed *l, const struct bpos *r)
133 {
134         return __bch2_bkey_cmp_left_packed(b, l, r);
135 }
136
137 /*
138  * The compiler generates better code when we pass bpos by ref, but it's often
139  * enough terribly convenient to pass it by val... as much as I hate c++, const
140  * ref would be nice here:
141  */
142 __pure __flatten
143 static inline int bkey_cmp_left_packed_byval(const struct btree *b,
144                                              const struct bkey_packed *l,
145                                              struct bpos r)
146 {
147         return bkey_cmp_left_packed(b, l, &r);
148 }
149
150 static __always_inline int bpos_cmp(struct bpos l, struct bpos r)
151 {
152         return  cmp_int(l.inode,    r.inode) ?:
153                 cmp_int(l.offset,   r.offset) ?:
154                 cmp_int(l.snapshot, r.snapshot);
155 }
156
157 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
158 {
159         return  cmp_int(l.inode,    r.inode) ?:
160                 cmp_int(l.offset,   r.offset);
161 }
162
163 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
164 {
165         return bpos_cmp(l, r) < 0 ? l : r;
166 }
167
168 static inline struct bpos bpos_max(struct bpos l, struct bpos r)
169 {
170         return bpos_cmp(l, r) > 0 ? l : r;
171 }
172
173 void bch2_bpos_swab(struct bpos *);
174 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
175
176 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
177 {
178         return  cmp_int(l.hi, r.hi) ?:
179                 cmp_int(l.lo, r.lo);
180 }
181
182 #define ZERO_VERSION    ((struct bversion) { .hi = 0, .lo = 0 })
183 #define MAX_VERSION     ((struct bversion) { .hi = ~0, .lo = ~0ULL })
184
185 static __always_inline int bversion_zero(struct bversion v)
186 {
187         return !bversion_cmp(v, ZERO_VERSION);
188 }
189
190 #ifdef CONFIG_BCACHEFS_DEBUG
191 /* statement expressions confusing unlikely()? */
192 #define bkey_packed(_k)                                                 \
193         ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT);                  \
194          (_k)->format != KEY_FORMAT_CURRENT; })
195 #else
196 #define bkey_packed(_k)         ((_k)->format != KEY_FORMAT_CURRENT)
197 #endif
198
199 /*
200  * It's safe to treat an unpacked bkey as a packed one, but not the reverse
201  */
202 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
203 {
204         return (struct bkey_packed *) k;
205 }
206
207 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
208 {
209         return (const struct bkey_packed *) k;
210 }
211
212 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
213 {
214         return bkey_packed(k) ? NULL : (struct bkey_i *) k;
215 }
216
217 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
218 {
219         return bkey_packed(k) ? NULL : (const struct bkey *) k;
220 }
221
222 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
223 {
224         return format->bits_per_field[BKEY_FIELD_INODE] +
225                 format->bits_per_field[BKEY_FIELD_OFFSET] +
226                 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
227 }
228
229 static inline struct bpos bpos_successor(struct bpos p)
230 {
231         if (!++p.snapshot &&
232             !++p.offset &&
233             !++p.inode)
234                 BUG();
235
236         return p;
237 }
238
239 static inline struct bpos bpos_predecessor(struct bpos p)
240 {
241         if (!p.snapshot-- &&
242             !p.offset-- &&
243             !p.inode--)
244                 BUG();
245
246         return p;
247 }
248
249 static inline struct bpos bpos_nosnap_successor(struct bpos p)
250 {
251         p.snapshot = 0;
252
253         if (!++p.offset &&
254             !++p.inode)
255                 BUG();
256
257         return p;
258 }
259
260 static inline struct bpos bpos_nosnap_predecessor(struct bpos p)
261 {
262         p.snapshot = 0;
263
264         if (!p.offset-- &&
265             !p.inode--)
266                 BUG();
267
268         return p;
269 }
270
271 static inline u64 bkey_start_offset(const struct bkey *k)
272 {
273         return k->p.offset - k->size;
274 }
275
276 static inline struct bpos bkey_start_pos(const struct bkey *k)
277 {
278         return (struct bpos) {
279                 .inode          = k->p.inode,
280                 .offset         = bkey_start_offset(k),
281                 .snapshot       = k->p.snapshot,
282         };
283 }
284
285 /* Packed helpers */
286
287 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
288                                       const struct bkey_packed *k)
289 {
290         unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
291
292         EBUG_ON(k->u64s < ret);
293         return ret;
294 }
295
296 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
297                                        const struct bkey_packed *k)
298 {
299         return bkeyp_key_u64s(format, k) * sizeof(u64);
300 }
301
302 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
303                                       const struct bkey_packed *k)
304 {
305         return k->u64s - bkeyp_key_u64s(format, k);
306 }
307
308 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
309                                      const struct bkey_packed *k)
310 {
311         return bkeyp_val_u64s(format, k) * sizeof(u64);
312 }
313
314 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
315                                       struct bkey_packed *k, unsigned val_u64s)
316 {
317         k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
318 }
319
320 #define bkeyp_val(_format, _k)                                          \
321          ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
322
323 extern const struct bkey_format bch2_bkey_format_current;
324
325 bool bch2_bkey_transform(const struct bkey_format *,
326                          struct bkey_packed *,
327                          const struct bkey_format *,
328                          const struct bkey_packed *);
329
330 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
331                                    const struct bkey_packed *);
332
333 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
334 struct bpos __bkey_unpack_pos(const struct bkey_format *,
335                               const struct bkey_packed *);
336 #endif
337
338 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
339                    const struct bkey_format *);
340
341 enum bkey_pack_pos_ret {
342         BKEY_PACK_POS_EXACT,
343         BKEY_PACK_POS_SMALLER,
344         BKEY_PACK_POS_FAIL,
345 };
346
347 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
348                                            const struct btree *);
349
350 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
351                                  const struct btree *b)
352 {
353         return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
354 }
355
356 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
357                  const struct bkey_packed *);
358 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
359                const struct bkey_format *);
360
361 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *);
362
363 static inline void
364 __bkey_unpack_key_format_checked(const struct btree *b,
365                                struct bkey *dst,
366                                const struct bkey_packed *src)
367 {
368         if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) {
369                 compiled_unpack_fn unpack_fn = b->aux_data;
370                 unpack_fn(dst, src);
371
372                 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
373                     bch2_expensive_debug_checks) {
374                         struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src);
375
376                         BUG_ON(memcmp(dst, &dst2, sizeof(*dst)));
377                 }
378         } else {
379                 *dst = __bch2_bkey_unpack_key(&b->format, src);
380         }
381 }
382
383 static inline struct bkey
384 bkey_unpack_key_format_checked(const struct btree *b,
385                                const struct bkey_packed *src)
386 {
387         struct bkey dst;
388
389         __bkey_unpack_key_format_checked(b, &dst, src);
390         return dst;
391 }
392
393 static inline void __bkey_unpack_key(const struct btree *b,
394                                      struct bkey *dst,
395                                      const struct bkey_packed *src)
396 {
397         if (likely(bkey_packed(src)))
398                 __bkey_unpack_key_format_checked(b, dst, src);
399         else
400                 *dst = *packed_to_bkey_c(src);
401 }
402
403 /**
404  * bkey_unpack_key -- unpack just the key, not the value
405  */
406 static inline struct bkey bkey_unpack_key(const struct btree *b,
407                                           const struct bkey_packed *src)
408 {
409         return likely(bkey_packed(src))
410                 ? bkey_unpack_key_format_checked(b, src)
411                 : *packed_to_bkey_c(src);
412 }
413
414 static inline struct bpos
415 bkey_unpack_pos_format_checked(const struct btree *b,
416                                const struct bkey_packed *src)
417 {
418 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
419         return bkey_unpack_key_format_checked(b, src).p;
420 #else
421         return __bkey_unpack_pos(&b->format, src);
422 #endif
423 }
424
425 static inline struct bpos bkey_unpack_pos(const struct btree *b,
426                                           const struct bkey_packed *src)
427 {
428         return likely(bkey_packed(src))
429                 ? bkey_unpack_pos_format_checked(b, src)
430                 : packed_to_bkey_c(src)->p;
431 }
432
433 /* Disassembled bkeys */
434
435 static inline struct bkey_s_c bkey_disassemble(struct btree *b,
436                                                const struct bkey_packed *k,
437                                                struct bkey *u)
438 {
439         __bkey_unpack_key(b, u, k);
440
441         return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), };
442 }
443
444 /* non const version: */
445 static inline struct bkey_s __bkey_disassemble(struct btree *b,
446                                                struct bkey_packed *k,
447                                                struct bkey *u)
448 {
449         __bkey_unpack_key(b, u, k);
450
451         return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), };
452 }
453
454 static inline u64 bkey_field_max(const struct bkey_format *f,
455                                  enum bch_bkey_fields nr)
456 {
457         return f->bits_per_field[nr] < 64
458                 ? (le64_to_cpu(f->field_offset[nr]) +
459                    ~(~0ULL << f->bits_per_field[nr]))
460                 : U64_MAX;
461 }
462
463 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
464
465 int bch2_compile_bkey_format(const struct bkey_format *, void *);
466
467 #else
468
469 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
470                                           void *out) { return 0; }
471
472 #endif
473
474 static inline void bkey_reassemble(struct bkey_i *dst,
475                                    struct bkey_s_c src)
476 {
477         dst->k = *src.k;
478         memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k));
479 }
480
481 #define bkey_s_null             ((struct bkey_s)   { .k = NULL })
482 #define bkey_s_c_null           ((struct bkey_s_c) { .k = NULL })
483
484 #define bkey_s_err(err)         ((struct bkey_s)   { .k = ERR_PTR(err) })
485 #define bkey_s_c_err(err)       ((struct bkey_s_c) { .k = ERR_PTR(err) })
486
487 static inline struct bkey_s bkey_to_s(struct bkey *k)
488 {
489         return (struct bkey_s) { .k = k, .v = NULL };
490 }
491
492 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
493 {
494         return (struct bkey_s_c) { .k = k, .v = NULL };
495 }
496
497 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
498 {
499         return (struct bkey_s) { .k = &k->k, .v = &k->v };
500 }
501
502 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
503 {
504         return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
505 }
506
507 /*
508  * For a given type of value (e.g. struct bch_extent), generates the types for
509  * bkey + bch_extent - inline, split, split const - and also all the conversion
510  * functions, which also check that the value is of the correct type.
511  *
512  * We use anonymous unions for upcasting - e.g. converting from e.g. a
513  * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
514  * functions.
515  */
516 #define x(name, ...)                                    \
517 struct bkey_i_##name {                                                  \
518         union {                                                         \
519                 struct bkey             k;                              \
520                 struct bkey_i           k_i;                            \
521         };                                                              \
522         struct bch_##name               v;                              \
523 };                                                                      \
524                                                                         \
525 struct bkey_s_c_##name {                                                \
526         union {                                                         \
527         struct {                                                        \
528                 const struct bkey       *k;                             \
529                 const struct bch_##name *v;                             \
530         };                                                              \
531         struct bkey_s_c                 s_c;                            \
532         };                                                              \
533 };                                                                      \
534                                                                         \
535 struct bkey_s_##name {                                                  \
536         union {                                                         \
537         struct {                                                        \
538                 struct bkey             *k;                             \
539                 struct bch_##name       *v;                             \
540         };                                                              \
541         struct bkey_s_c_##name          c;                              \
542         struct bkey_s                   s;                              \
543         struct bkey_s_c                 s_c;                            \
544         };                                                              \
545 };                                                                      \
546                                                                         \
547 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k)  \
548 {                                                                       \
549         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
550         return container_of(&k->k, struct bkey_i_##name, k);            \
551 }                                                                       \
552                                                                         \
553 static inline const struct bkey_i_##name *                              \
554 bkey_i_to_##name##_c(const struct bkey_i *k)                            \
555 {                                                                       \
556         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
557         return container_of(&k->k, struct bkey_i_##name, k);            \
558 }                                                                       \
559                                                                         \
560 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k)    \
561 {                                                                       \
562         EBUG_ON(k.k->type != KEY_TYPE_##name);                          \
563         return (struct bkey_s_##name) {                                 \
564                 .k = k.k,                                               \
565                 .v = container_of(k.v, struct bch_##name, v),           \
566         };                                                              \
567 }                                                                       \
568                                                                         \
569 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
570 {                                                                       \
571         EBUG_ON(k.k->type != KEY_TYPE_##name);                          \
572         return (struct bkey_s_c_##name) {                               \
573                 .k = k.k,                                               \
574                 .v = container_of(k.v, struct bch_##name, v),           \
575         };                                                              \
576 }                                                                       \
577                                                                         \
578 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
579 {                                                                       \
580         return (struct bkey_s_##name) {                                 \
581                 .k = &k->k,                                             \
582                 .v = &k->v,                                             \
583         };                                                              \
584 }                                                                       \
585                                                                         \
586 static inline struct bkey_s_c_##name                                    \
587 name##_i_to_s_c(const struct bkey_i_##name *k)                          \
588 {                                                                       \
589         return (struct bkey_s_c_##name) {                               \
590                 .k = &k->k,                                             \
591                 .v = &k->v,                                             \
592         };                                                              \
593 }                                                                       \
594                                                                         \
595 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
596 {                                                                       \
597         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
598         return (struct bkey_s_##name) {                                 \
599                 .k = &k->k,                                             \
600                 .v = container_of(&k->v, struct bch_##name, v),         \
601         };                                                              \
602 }                                                                       \
603                                                                         \
604 static inline struct bkey_s_c_##name                                    \
605 bkey_i_to_s_c_##name(const struct bkey_i *k)                            \
606 {                                                                       \
607         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
608         return (struct bkey_s_c_##name) {                               \
609                 .k = &k->k,                                             \
610                 .v = container_of(&k->v, struct bch_##name, v),         \
611         };                                                              \
612 }                                                                       \
613                                                                         \
614 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
615 {                                                                       \
616         struct bkey_i_##name *k =                                       \
617                 container_of(&_k->k, struct bkey_i_##name, k);          \
618                                                                         \
619         bkey_init(&k->k);                                               \
620         memset(&k->v, 0, sizeof(k->v));                                 \
621         k->k.type = KEY_TYPE_##name;                                    \
622         set_bkey_val_bytes(&k->k, sizeof(k->v));                        \
623                                                                         \
624         return k;                                                       \
625 }
626
627 BCH_BKEY_TYPES();
628 #undef x
629
630 /* byte order helpers */
631
632 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
633
634 static inline unsigned high_word_offset(const struct bkey_format *f)
635 {
636         return f->key_u64s - 1;
637 }
638
639 #define high_bit_offset         0
640 #define nth_word(p, n)          ((p) - (n))
641
642 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
643
644 static inline unsigned high_word_offset(const struct bkey_format *f)
645 {
646         return 0;
647 }
648
649 #define high_bit_offset         KEY_PACKED_BITS_START
650 #define nth_word(p, n)          ((p) + (n))
651
652 #else
653 #error edit for your odd byteorder.
654 #endif
655
656 #define high_word(f, k)         ((k)->_data + high_word_offset(f))
657 #define next_word(p)            nth_word(p, 1)
658 #define prev_word(p)            nth_word(p, -1)
659
660 #ifdef CONFIG_BCACHEFS_DEBUG
661 void bch2_bkey_pack_test(void);
662 #else
663 static inline void bch2_bkey_pack_test(void) {}
664 #endif
665
666 #endif /* _BCACHEFS_BKEY_H */