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