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