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