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