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