]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey.h
Update bcachefs sources to da7fefde29 bcachefs: shim for userspace raid library
[bcachefs-tools-debian] / libbcachefs / bkey.h
1 #ifndef _BCACHEFS_BKEY_H
2 #define _BCACHEFS_BKEY_H
3
4 #include <linux/bug.h>
5 #include "bcachefs_format.h"
6
7 #include "util.h"
8 #include "vstructs.h"
9
10 #ifdef CONFIG_X86_64
11 #define HAVE_BCACHEFS_COMPILED_UNPACK   1
12 #endif
13
14 void bch2_to_binary(char *, const u64 *, unsigned);
15
16 /* bkey with split value, const */
17 struct bkey_s_c {
18         const struct bkey       *k;
19         const struct bch_val    *v;
20 };
21
22 /* bkey with split value */
23 struct bkey_s {
24         union {
25         struct {
26                 struct bkey     *k;
27                 struct bch_val  *v;
28         };
29         struct bkey_s_c         s_c;
30         };
31 };
32
33 #define bkey_next(_k)           vstruct_next(_k)
34
35 static inline unsigned bkey_val_u64s(const struct bkey *k)
36 {
37         return k->u64s - BKEY_U64s;
38 }
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         k->u64s = BKEY_U64s + val_u64s;
48 }
49
50 static inline void set_bkey_val_bytes(struct bkey *k, unsigned bytes)
51 {
52         k->u64s = BKEY_U64s + DIV_ROUND_UP(bytes, sizeof(u64));
53 }
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         __memmove_u64s_down((_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  (l.hi > r.hi) - (l.hi < r.hi) ?:
210                 (l.lo > r.lo) - (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, nr, _assert)                         \
434 struct bkey_s_c_##name {                                                \
435         union {                                                         \
436         struct {                                                        \
437                 const struct bkey       *k;                             \
438                 const struct bch_##name *v;                             \
439         };                                                              \
440         struct bkey_s_c                 s_c;                            \
441         };                                                              \
442 };                                                                      \
443                                                                         \
444 struct bkey_s_##name {                                                  \
445         union {                                                         \
446         struct {                                                        \
447                 struct bkey             *k;                             \
448                 struct bch_##name       *v;                             \
449         };                                                              \
450         struct bkey_s_c_##name          c;                              \
451         struct bkey_s                   s;                              \
452         struct bkey_s_c                 s_c;                            \
453         };                                                              \
454 };                                                                      \
455                                                                         \
456 static inline struct bkey_i_##name *bkey_i_to_##name(struct bkey_i *k)  \
457 {                                                                       \
458         _assert(k->k.type, nr);                                         \
459         return container_of(&k->k, struct bkey_i_##name, k);            \
460 }                                                                       \
461                                                                         \
462 static inline const struct bkey_i_##name *                              \
463 bkey_i_to_##name##_c(const struct bkey_i *k)                            \
464 {                                                                       \
465         _assert(k->k.type, nr);                                         \
466         return container_of(&k->k, struct bkey_i_##name, k);            \
467 }                                                                       \
468                                                                         \
469 static inline struct bkey_s_##name bkey_s_to_##name(struct bkey_s k)    \
470 {                                                                       \
471         _assert(k.k->type, nr);                                         \
472         return (struct bkey_s_##name) {                                 \
473                 .k = k.k,                                               \
474                 .v = container_of(k.v, struct bch_##name, v),           \
475         };                                                              \
476 }                                                                       \
477                                                                         \
478 static inline struct bkey_s_c_##name bkey_s_c_to_##name(struct bkey_s_c k)\
479 {                                                                       \
480         _assert(k.k->type, nr);                                         \
481         return (struct bkey_s_c_##name) {                               \
482                 .k = k.k,                                               \
483                 .v = container_of(k.v, struct bch_##name, v),           \
484         };                                                              \
485 }                                                                       \
486                                                                         \
487 static inline struct bkey_s_##name name##_i_to_s(struct bkey_i_##name *k)\
488 {                                                                       \
489         return (struct bkey_s_##name) {                                 \
490                 .k = &k->k,                                             \
491                 .v = &k->v,                                             \
492         };                                                              \
493 }                                                                       \
494                                                                         \
495 static inline struct bkey_s_c_##name                                    \
496 name##_i_to_s_c(const struct bkey_i_##name *k)                          \
497 {                                                                       \
498         return (struct bkey_s_c_##name) {                               \
499                 .k = &k->k,                                             \
500                 .v = &k->v,                                             \
501         };                                                              \
502 }                                                                       \
503                                                                         \
504 static inline struct bkey_s_##name bkey_i_to_s_##name(struct bkey_i *k) \
505 {                                                                       \
506         _assert(k->k.type, nr);                                         \
507         return (struct bkey_s_##name) {                                 \
508                 .k = &k->k,                                             \
509                 .v = container_of(&k->v, struct bch_##name, v),         \
510         };                                                              \
511 }                                                                       \
512                                                                         \
513 static inline struct bkey_s_c_##name                                    \
514 bkey_i_to_s_c_##name(const struct bkey_i *k)                            \
515 {                                                                       \
516         _assert(k->k.type, nr);                                         \
517         return (struct bkey_s_c_##name) {                               \
518                 .k = &k->k,                                             \
519                 .v = container_of(&k->v, struct bch_##name, v),         \
520         };                                                              \
521 }                                                                       \
522                                                                         \
523 static inline struct bch_##name *                                       \
524 bkey_p_##name##_val(const struct bkey_format *f,                        \
525                     struct bkey_packed *k)                              \
526 {                                                                       \
527         return container_of(bkeyp_val(f, k), struct bch_##name, v);     \
528 }                                                                       \
529                                                                         \
530 static inline const struct bch_##name *                                 \
531 bkey_p_c_##name##_val(const struct bkey_format *f,                      \
532                       const struct bkey_packed *k)                      \
533 {                                                                       \
534         return container_of(bkeyp_val(f, k), struct bch_##name, v);     \
535 }                                                                       \
536                                                                         \
537 static inline struct bkey_i_##name *bkey_##name##_init(struct bkey_i *_k)\
538 {                                                                       \
539         struct bkey_i_##name *k =                                       \
540                 container_of(&_k->k, struct bkey_i_##name, k);          \
541                                                                         \
542         bkey_init(&k->k);                                               \
543         memset(&k->v, 0, sizeof(k->v));                                 \
544         k->k.type = nr;                                                 \
545         set_bkey_val_bytes(&k->k, sizeof(k->v));                        \
546                                                                         \
547         return k;                                                       \
548 }
549
550 #define __BKEY_VAL_ASSERT(_type, _nr)   EBUG_ON(_type != _nr)
551
552 #define BKEY_VAL_ACCESSORS(name, _nr)                                   \
553         static inline void __bch_##name##_assert(u8 type, u8 nr)        \
554         {                                                               \
555                 EBUG_ON(type != _nr);                                   \
556         }                                                               \
557                                                                         \
558         __BKEY_VAL_ACCESSORS(name, _nr, __bch_##name##_assert)
559
560 BKEY_VAL_ACCESSORS(cookie,              KEY_TYPE_COOKIE);
561
562 static inline void __bch2_extent_assert(u8 type, u8 nr)
563 {
564         EBUG_ON(type != BCH_EXTENT && type != BCH_EXTENT_CACHED);
565 }
566
567 __BKEY_VAL_ACCESSORS(extent,            BCH_EXTENT, __bch2_extent_assert);
568 BKEY_VAL_ACCESSORS(reservation,         BCH_RESERVATION);
569
570 BKEY_VAL_ACCESSORS(inode,               BCH_INODE_FS);
571 BKEY_VAL_ACCESSORS(inode_blockdev,      BCH_INODE_BLOCKDEV);
572 BKEY_VAL_ACCESSORS(inode_generation,    BCH_INODE_GENERATION);
573
574 BKEY_VAL_ACCESSORS(dirent,              BCH_DIRENT);
575
576 BKEY_VAL_ACCESSORS(xattr,               BCH_XATTR);
577
578 BKEY_VAL_ACCESSORS(alloc,               BCH_ALLOC);
579
580 BKEY_VAL_ACCESSORS(quota,               BCH_QUOTA);
581
582 BKEY_VAL_ACCESSORS(stripe,              BCH_STRIPE);
583
584 /* byte order helpers */
585
586 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
587
588 static inline unsigned high_word_offset(const struct bkey_format *f)
589 {
590         return f->key_u64s - 1;
591 }
592
593 #define high_bit_offset         0
594 #define nth_word(p, n)          ((p) - (n))
595
596 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
597
598 static inline unsigned high_word_offset(const struct bkey_format *f)
599 {
600         return 0;
601 }
602
603 #define high_bit_offset         KEY_PACKED_BITS_START
604 #define nth_word(p, n)          ((p) + (n))
605
606 #else
607 #error edit for your odd byteorder.
608 #endif
609
610 #define high_word(f, k)         ((k)->_data + high_word_offset(f))
611 #define next_word(p)            nth_word(p, 1)
612 #define prev_word(p)            nth_word(p, -1)
613
614 #ifdef CONFIG_BCACHEFS_DEBUG
615 void bch2_bkey_pack_test(void);
616 #else
617 static inline void bch2_bkey_pack_test(void) {}
618 #endif
619
620 #endif /* _BCACHEFS_BKEY_H */