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