]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey.h
Update bcachefs sources to 9e76e8d98c bcachefs: Fix uninitialized field in hash_check...
[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 #define bkey_packed_typecheck(_k)                                       \
61 ({                                                                      \
62         BUILD_BUG_ON(!type_is(_k, struct bkey *) &&                     \
63                      !type_is(_k, struct bkey_packed *));               \
64         type_is(_k, struct bkey_packed *);                              \
65 })
66
67 enum bkey_lr_packed {
68         BKEY_PACKED_BOTH,
69         BKEY_PACKED_RIGHT,
70         BKEY_PACKED_LEFT,
71         BKEY_PACKED_NONE,
72 };
73
74 #define bkey_lr_packed_typecheck(_l, _r)                                \
75         (!bkey_packed_typecheck(_l) + ((!bkey_packed_typecheck(_r)) << 1))
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 bkey_packed *,
126                            const struct bkey_packed *,
127                            const struct btree *);
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 /*
154  * If @_l or @_r are struct bkey * (not bkey_packed *), uses type information to
155  * skip dispatching on k->format:
156  */
157 #define bkey_cmp_packed(_b, _l, _r)                                     \
158 ({                                                                      \
159         int _cmp;                                                       \
160                                                                         \
161         switch (bkey_lr_packed_typecheck(_l, _r)) {                     \
162         case BKEY_PACKED_NONE:                                          \
163                 _cmp = bkey_cmp(((struct bkey *) (_l))->p,              \
164                                 ((struct bkey *) (_r))->p);             \
165                 break;                                                  \
166         case BKEY_PACKED_LEFT:                                          \
167                 _cmp = bkey_cmp_left_packed((_b),                       \
168                                   (struct bkey_packed *) (_l),          \
169                                   &((struct bkey *) (_r))->p);          \
170                 break;                                                  \
171         case BKEY_PACKED_RIGHT:                                         \
172                 _cmp = -bkey_cmp_left_packed((_b),                      \
173                                   (struct bkey_packed *) (_r),          \
174                                   &((struct bkey *) (_l))->p);          \
175                 break;                                                  \
176         case BKEY_PACKED_BOTH:                                          \
177                 _cmp = __bch2_bkey_cmp_packed((void *) (_l),            \
178                                          (void *) (_r), (_b));          \
179                 break;                                                  \
180         }                                                               \
181         _cmp;                                                           \
182 })
183
184 #if 1
185 static __always_inline int bkey_cmp(struct bpos l, struct bpos r)
186 {
187         if (l.inode != r.inode)
188                 return l.inode < r.inode ? -1 : 1;
189         if (l.offset != r.offset)
190                 return l.offset < r.offset ? -1 : 1;
191         if (l.snapshot != r.snapshot)
192                 return l.snapshot < r.snapshot ? -1 : 1;
193         return 0;
194 }
195 #else
196 int bkey_cmp(struct bpos l, struct bpos r);
197 #endif
198
199 static inline struct bpos bpos_min(struct bpos l, struct bpos r)
200 {
201         return bkey_cmp(l, r) < 0 ? l : r;
202 }
203
204 void bch2_bpos_swab(struct bpos *);
205 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *);
206
207 static __always_inline int bversion_cmp(struct bversion l, struct bversion r)
208 {
209         return  cmp_int(l.hi, r.hi) ?:
210                 cmp_int(l.lo, r.lo);
211 }
212
213 #define ZERO_VERSION    ((struct bversion) { .hi = 0, .lo = 0 })
214 #define MAX_VERSION     ((struct bversion) { .hi = ~0, .lo = ~0ULL })
215
216 static __always_inline int bversion_zero(struct bversion v)
217 {
218         return !bversion_cmp(v, ZERO_VERSION);
219 }
220
221 #ifdef CONFIG_BCACHEFS_DEBUG
222 /* statement expressions confusing unlikely()? */
223 #define bkey_packed(_k)                                                 \
224         ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT);                  \
225          (_k)->format != KEY_FORMAT_CURRENT; })
226 #else
227 #define bkey_packed(_k)         ((_k)->format != KEY_FORMAT_CURRENT)
228 #endif
229
230 /*
231  * It's safe to treat an unpacked bkey as a packed one, but not the reverse
232  */
233 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k)
234 {
235         return (struct bkey_packed *) k;
236 }
237
238 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k)
239 {
240         return (const struct bkey_packed *) k;
241 }
242
243 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k)
244 {
245         return bkey_packed(k) ? NULL : (struct bkey_i *) k;
246 }
247
248 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k)
249 {
250         return bkey_packed(k) ? NULL : (const struct bkey *) k;
251 }
252
253 static inline unsigned bkey_format_key_bits(const struct bkey_format *format)
254 {
255         return format->bits_per_field[BKEY_FIELD_INODE] +
256                 format->bits_per_field[BKEY_FIELD_OFFSET] +
257                 format->bits_per_field[BKEY_FIELD_SNAPSHOT];
258 }
259
260 static inline struct bpos bkey_successor(struct bpos p)
261 {
262         struct bpos ret = p;
263
264         if (!++ret.offset)
265                 BUG_ON(!++ret.inode);
266
267         return ret;
268 }
269
270 static inline struct bpos bkey_predecessor(struct bpos p)
271 {
272         struct bpos ret = p;
273
274         if (!ret.offset--)
275                 BUG_ON(!ret.inode--);
276
277         return ret;
278 }
279
280 static inline u64 bkey_start_offset(const struct bkey *k)
281 {
282         return k->p.offset - k->size;
283 }
284
285 static inline struct bpos bkey_start_pos(const struct bkey *k)
286 {
287         return (struct bpos) {
288                 .inode          = k->p.inode,
289                 .offset         = bkey_start_offset(k),
290                 .snapshot       = k->p.snapshot,
291         };
292 }
293
294 /* Packed helpers */
295
296 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format,
297                                       const struct bkey_packed *k)
298 {
299         unsigned ret = bkey_packed(k) ? format->key_u64s : BKEY_U64s;
300
301         EBUG_ON(k->u64s < ret);
302         return ret;
303 }
304
305 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format,
306                                        const struct bkey_packed *k)
307 {
308         return bkeyp_key_u64s(format, k) * sizeof(u64);
309 }
310
311 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format,
312                                       const struct bkey_packed *k)
313 {
314         return k->u64s - bkeyp_key_u64s(format, k);
315 }
316
317 static inline size_t bkeyp_val_bytes(const struct bkey_format *format,
318                                      const struct bkey_packed *k)
319 {
320         return bkeyp_val_u64s(format, k) * sizeof(u64);
321 }
322
323 static inline void set_bkeyp_val_u64s(const struct bkey_format *format,
324                                       struct bkey_packed *k, unsigned val_u64s)
325 {
326         k->u64s = bkeyp_key_u64s(format, k) + val_u64s;
327 }
328
329 #define bkeyp_val(_format, _k)                                          \
330          ((struct bch_val *) ((_k)->_data + bkeyp_key_u64s(_format, _k)))
331
332 extern const struct bkey_format bch2_bkey_format_current;
333
334 bool bch2_bkey_transform(const struct bkey_format *,
335                          struct bkey_packed *,
336                          const struct bkey_format *,
337                          const struct bkey_packed *);
338
339 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *,
340                                    const struct bkey_packed *);
341
342 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK
343 struct bpos __bkey_unpack_pos(const struct bkey_format *,
344                               const struct bkey_packed *);
345 #endif
346
347 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *,
348                    const struct bkey_format *);
349
350 enum bkey_pack_pos_ret {
351         BKEY_PACK_POS_EXACT,
352         BKEY_PACK_POS_SMALLER,
353         BKEY_PACK_POS_FAIL,
354 };
355
356 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos,
357                                            const struct btree *);
358
359 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in,
360                                  const struct btree *b)
361 {
362         return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT;
363 }
364
365 void bch2_bkey_unpack(const struct btree *, struct bkey_i *,
366                  const struct bkey_packed *);
367 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *,
368                const struct bkey_format *);
369
370 static inline u64 bkey_field_max(const struct bkey_format *f,
371                                  enum bch_bkey_fields nr)
372 {
373         return f->bits_per_field[nr] < 64
374                 ? (le64_to_cpu(f->field_offset[nr]) +
375                    ~(~0ULL << f->bits_per_field[nr]))
376                 : U64_MAX;
377 }
378
379 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK
380
381 int bch2_compile_bkey_format(const struct bkey_format *, void *);
382
383 #else
384
385 static inline int bch2_compile_bkey_format(const struct bkey_format *format,
386                                           void *out) { return 0; }
387
388 #endif
389
390 static inline void bkey_reassemble(struct bkey_i *dst,
391                                    struct bkey_s_c src)
392 {
393         BUG_ON(bkey_packed(src.k));
394         dst->k = *src.k;
395         memcpy_u64s(&dst->v, src.v, bkey_val_u64s(src.k));
396 }
397
398 #define bkey_s_null             ((struct bkey_s)   { .k = NULL })
399 #define bkey_s_c_null           ((struct bkey_s_c) { .k = NULL })
400
401 #define bkey_s_err(err)         ((struct bkey_s)   { .k = ERR_PTR(err) })
402 #define bkey_s_c_err(err)       ((struct bkey_s_c) { .k = ERR_PTR(err) })
403
404 static inline struct bkey_s bkey_to_s(struct bkey *k)
405 {
406         return (struct bkey_s) { .k = k, .v = NULL };
407 }
408
409 static inline struct bkey_s_c bkey_to_s_c(const struct bkey *k)
410 {
411         return (struct bkey_s_c) { .k = k, .v = NULL };
412 }
413
414 static inline struct bkey_s bkey_i_to_s(struct bkey_i *k)
415 {
416         return (struct bkey_s) { .k = &k->k, .v = &k->v };
417 }
418
419 static inline struct bkey_s_c bkey_i_to_s_c(const struct bkey_i *k)
420 {
421         return (struct bkey_s_c) { .k = &k->k, .v = &k->v };
422 }
423
424 /*
425  * For a given type of value (e.g. struct bch_extent), generates the types for
426  * bkey + bch_extent - inline, split, split const - and also all the conversion
427  * functions, which also check that the value is of the correct type.
428  *
429  * We use anonymous unions for upcasting - e.g. converting from e.g. a
430  * bkey_i_extent to a bkey_i - since that's always safe, instead of conversion
431  * functions.
432  */
433 #define BKEY_VAL_ACCESSORS(name)                                        \
434 struct bkey_i_##name {                                                  \
435         union {                                                         \
436                 struct bkey             k;                              \
437                 struct bkey_i           k_i;                            \
438         };                                                              \
439         struct bch_##name               v;                              \
440 };                                                                      \
441                                                                         \
442 struct bkey_s_c_##name {                                                \
443         union {                                                         \
444         struct {                                                        \
445                 const struct bkey       *k;                             \
446                 const struct bch_##name *v;                             \
447         };                                                              \
448         struct bkey_s_c                 s_c;                            \
449         };                                                              \
450 };                                                                      \
451                                                                         \
452 struct bkey_s_##name {                                                  \
453         union {                                                         \
454         struct {                                                        \
455                 struct bkey             *k;                             \
456                 struct bch_##name       *v;                             \
457         };                                                              \
458         struct bkey_s_c_##name          c;                              \
459         struct bkey_s                   s;                              \
460         struct bkey_s_c                 s_c;                            \
461         };                                                              \
462 };                                                                      \
463                                                                         \
464 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k)  \
465 {                                                                       \
466         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
467         return container_of(&k->k, struct bkey_i_##name, k);            \
468 }                                                                       \
469                                                                         \
470 static inline const struct bkey_i_##name *                              \
471 bkey_i_to_##name##_c(const struct bkey_i *k)                            \
472 {                                                                       \
473         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
474         return container_of(&k->k, struct bkey_i_##name, k);            \
475 }                                                                       \
476                                                                         \
477 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k)    \
478 {                                                                       \
479         EBUG_ON(k.k->type != KEY_TYPE_##name);                          \
480         return (struct bkey_s_##name) {                                 \
481                 .k = k.k,                                               \
482                 .v = container_of(k.v, struct bch_##name, v),           \
483         };                                                              \
484 }                                                                       \
485                                                                         \
486 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
487 {                                                                       \
488         EBUG_ON(k.k->type != KEY_TYPE_##name);                          \
489         return (struct bkey_s_c_##name) {                               \
490                 .k = k.k,                                               \
491                 .v = container_of(k.v, struct bch_##name, v),           \
492         };                                                              \
493 }                                                                       \
494                                                                         \
495 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
496 {                                                                       \
497         return (struct bkey_s_##name) {                                 \
498                 .k = &k->k,                                             \
499                 .v = &k->v,                                             \
500         };                                                              \
501 }                                                                       \
502                                                                         \
503 static inline struct bkey_s_c_##name                                    \
504 name##_i_to_s_c(const struct bkey_i_##name *k)                          \
505 {                                                                       \
506         return (struct bkey_s_c_##name) {                               \
507                 .k = &k->k,                                             \
508                 .v = &k->v,                                             \
509         };                                                              \
510 }                                                                       \
511                                                                         \
512 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
513 {                                                                       \
514         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
515         return (struct bkey_s_##name) {                                 \
516                 .k = &k->k,                                             \
517                 .v = container_of(&k->v, struct bch_##name, v),         \
518         };                                                              \
519 }                                                                       \
520                                                                         \
521 static inline struct bkey_s_c_##name                                    \
522 bkey_i_to_s_c_##name(const struct bkey_i *k)                            \
523 {                                                                       \
524         EBUG_ON(k->k.type != KEY_TYPE_##name);                          \
525         return (struct bkey_s_c_##name) {                               \
526                 .k = &k->k,                                             \
527                 .v = container_of(&k->v, struct bch_##name, v),         \
528         };                                                              \
529 }                                                                       \
530                                                                         \
531 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
532 {                                                                       \
533         struct bkey_i_##name *k =                                       \
534                 container_of(&_k->k, struct bkey_i_##name, k);          \
535                                                                         \
536         bkey_init(&k->k);                                               \
537         memset(&k->v, 0, sizeof(k->v));                                 \
538         k->k.type = KEY_TYPE_##name;                                    \
539         set_bkey_val_bytes(&k->k, sizeof(k->v));                        \
540                                                                         \
541         return k;                                                       \
542 }
543
544 BKEY_VAL_ACCESSORS(cookie);
545 BKEY_VAL_ACCESSORS(btree_ptr);
546 BKEY_VAL_ACCESSORS(extent);
547 BKEY_VAL_ACCESSORS(reservation);
548 BKEY_VAL_ACCESSORS(inode);
549 BKEY_VAL_ACCESSORS(inode_generation);
550 BKEY_VAL_ACCESSORS(dirent);
551 BKEY_VAL_ACCESSORS(xattr);
552 BKEY_VAL_ACCESSORS(alloc);
553 BKEY_VAL_ACCESSORS(quota);
554 BKEY_VAL_ACCESSORS(stripe);
555 BKEY_VAL_ACCESSORS(reflink_p);
556 BKEY_VAL_ACCESSORS(reflink_v);
557
558 /* byte order helpers */
559
560 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
561
562 static inline unsigned high_word_offset(const struct bkey_format *f)
563 {
564         return f->key_u64s - 1;
565 }
566
567 #define high_bit_offset         0
568 #define nth_word(p, n)          ((p) - (n))
569
570 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
571
572 static inline unsigned high_word_offset(const struct bkey_format *f)
573 {
574         return 0;
575 }
576
577 #define high_bit_offset         KEY_PACKED_BITS_START
578 #define nth_word(p, n)          ((p) + (n))
579
580 #else
581 #error edit for your odd byteorder.
582 #endif
583
584 #define high_word(f, k)         ((k)->_data + high_word_offset(f))
585 #define next_word(p)            nth_word(p, 1)
586 #define prev_word(p)            nth_word(p, -1)
587
588 #ifdef CONFIG_BCACHEFS_DEBUG
589 void bch2_bkey_pack_test(void);
590 #else
591 static inline void bch2_bkey_pack_test(void) {}
592 #endif
593
594 #endif /* _BCACHEFS_BKEY_H */