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