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