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