]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/extents.h
Update bcachefs sources to 3cd63315a6 bcachefs: Track incompressible data
[bcachefs-tools-debian] / libbcachefs / extents.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_EXTENTS_H
3 #define _BCACHEFS_EXTENTS_H
4
5 #include "bcachefs.h"
6 #include "bkey.h"
7 #include "extents_types.h"
8
9 struct bch_fs;
10 struct btree_trans;
11
12 /* extent entries: */
13
14 #define extent_entry_last(_e)                                           \
15         ((typeof(&(_e).v->start[0])) bkey_val_end(_e))
16
17 #define entry_to_ptr(_entry)                                            \
18 ({                                                                      \
19         EBUG_ON((_entry) && !extent_entry_is_ptr(_entry));              \
20                                                                         \
21         __builtin_choose_expr(                                          \
22                 type_is_exact(_entry, const union bch_extent_entry *),  \
23                 (const struct bch_extent_ptr *) (_entry),               \
24                 (struct bch_extent_ptr *) (_entry));                    \
25 })
26
27 /* downcast, preserves const */
28 #define to_entry(_entry)                                                \
29 ({                                                                      \
30         BUILD_BUG_ON(!type_is(_entry, union bch_extent_crc *) &&        \
31                      !type_is(_entry, struct bch_extent_ptr *) &&       \
32                      !type_is(_entry, struct bch_extent_stripe_ptr *)); \
33                                                                         \
34         __builtin_choose_expr(                                          \
35                 (type_is_exact(_entry, const union bch_extent_crc *) || \
36                  type_is_exact(_entry, const struct bch_extent_ptr *) ||\
37                  type_is_exact(_entry, const struct bch_extent_stripe_ptr *)),\
38                 (const union bch_extent_entry *) (_entry),              \
39                 (union bch_extent_entry *) (_entry));                   \
40 })
41
42 #define extent_entry_next(_entry)                                       \
43         ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
44
45 static inline unsigned
46 __extent_entry_type(const union bch_extent_entry *e)
47 {
48         return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
49 }
50
51 static inline enum bch_extent_entry_type
52 extent_entry_type(const union bch_extent_entry *e)
53 {
54         int ret = __ffs(e->type);
55
56         EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
57
58         return ret;
59 }
60
61 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
62 {
63         switch (extent_entry_type(entry)) {
64 #define x(f, n)                                         \
65         case BCH_EXTENT_ENTRY_##f:                      \
66                 return sizeof(struct bch_extent_##f);
67         BCH_EXTENT_ENTRY_TYPES()
68 #undef x
69         default:
70                 BUG();
71         }
72 }
73
74 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
75 {
76         return extent_entry_bytes(entry) / sizeof(u64);
77 }
78
79 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
80 {
81         switch (extent_entry_type(e)) {
82         case BCH_EXTENT_ENTRY_ptr:
83                 return true;
84         default:
85                 return false;
86         }
87 }
88
89 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
90 {
91         switch (extent_entry_type(e)) {
92         case BCH_EXTENT_ENTRY_crc32:
93         case BCH_EXTENT_ENTRY_crc64:
94         case BCH_EXTENT_ENTRY_crc128:
95                 return true;
96         default:
97                 return false;
98         }
99 }
100
101 union bch_extent_crc {
102         u8                              type;
103         struct bch_extent_crc32         crc32;
104         struct bch_extent_crc64         crc64;
105         struct bch_extent_crc128        crc128;
106 };
107
108 #define __entry_to_crc(_entry)                                          \
109         __builtin_choose_expr(                                          \
110                 type_is_exact(_entry, const union bch_extent_entry *),  \
111                 (const union bch_extent_crc *) (_entry),                \
112                 (union bch_extent_crc *) (_entry))
113
114 #define entry_to_crc(_entry)                                            \
115 ({                                                                      \
116         EBUG_ON((_entry) && !extent_entry_is_crc(_entry));              \
117                                                                         \
118         __entry_to_crc(_entry);                                         \
119 })
120
121 static inline struct bch_extent_crc_unpacked
122 bch2_extent_crc_unpack(const struct bkey *k, const union bch_extent_crc *crc)
123 {
124 #define common_fields(_crc)                                             \
125                 .csum_type              = _crc.csum_type,               \
126                 .compression_type       = _crc.compression_type,        \
127                 .compressed_size        = _crc._compressed_size + 1,    \
128                 .uncompressed_size      = _crc._uncompressed_size + 1,  \
129                 .offset                 = _crc.offset,                  \
130                 .live_size              = k->size
131
132         if (!crc)
133                 return (struct bch_extent_crc_unpacked) {
134                         .compressed_size        = k->size,
135                         .uncompressed_size      = k->size,
136                         .live_size              = k->size,
137                 };
138
139         switch (extent_entry_type(to_entry(crc))) {
140         case BCH_EXTENT_ENTRY_crc32: {
141                 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
142                         common_fields(crc->crc32),
143                 };
144
145                 *((__le32 *) &ret.csum.lo) = crc->crc32.csum;
146
147                 memcpy(&ret.csum.lo, &crc->crc32.csum,
148                        sizeof(crc->crc32.csum));
149
150                 return ret;
151         }
152         case BCH_EXTENT_ENTRY_crc64: {
153                 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
154                         common_fields(crc->crc64),
155                         .nonce                  = crc->crc64.nonce,
156                         .csum.lo                = (__force __le64) crc->crc64.csum_lo,
157                 };
158
159                 *((__le16 *) &ret.csum.hi) = crc->crc64.csum_hi;
160
161                 return ret;
162         }
163         case BCH_EXTENT_ENTRY_crc128: {
164                 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
165                         common_fields(crc->crc128),
166                         .nonce                  = crc->crc128.nonce,
167                         .csum                   = crc->crc128.csum,
168                 };
169
170                 return ret;
171         }
172         default:
173                 BUG();
174         }
175 #undef common_fields
176 }
177
178 static inline bool crc_is_compressed(struct bch_extent_crc_unpacked crc)
179 {
180         return (crc.compression_type != BCH_COMPRESSION_TYPE_none &&
181                 crc.compression_type != BCH_COMPRESSION_TYPE_incompressible);
182 }
183
184 /* bkey_ptrs: generically over any key type that has ptrs */
185
186 struct bkey_ptrs_c {
187         const union bch_extent_entry    *start;
188         const union bch_extent_entry    *end;
189 };
190
191 struct bkey_ptrs {
192         union bch_extent_entry  *start;
193         union bch_extent_entry  *end;
194 };
195
196 static inline struct bkey_ptrs_c bch2_bkey_ptrs_c(struct bkey_s_c k)
197 {
198         switch (k.k->type) {
199         case KEY_TYPE_btree_ptr: {
200                 struct bkey_s_c_btree_ptr e = bkey_s_c_to_btree_ptr(k);
201                 return (struct bkey_ptrs_c) {
202                         to_entry(&e.v->start[0]),
203                         to_entry(extent_entry_last(e))
204                 };
205         }
206         case KEY_TYPE_extent: {
207                 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
208                 return (struct bkey_ptrs_c) {
209                         e.v->start,
210                         extent_entry_last(e)
211                 };
212         }
213         case KEY_TYPE_stripe: {
214                 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
215                 return (struct bkey_ptrs_c) {
216                         to_entry(&s.v->ptrs[0]),
217                         to_entry(&s.v->ptrs[s.v->nr_blocks]),
218                 };
219         }
220         case KEY_TYPE_reflink_v: {
221                 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
222
223                 return (struct bkey_ptrs_c) {
224                         r.v->start,
225                         bkey_val_end(r),
226                 };
227         }
228         default:
229                 return (struct bkey_ptrs_c) { NULL, NULL };
230         }
231 }
232
233 static inline struct bkey_ptrs bch2_bkey_ptrs(struct bkey_s k)
234 {
235         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k.s_c);
236
237         return (struct bkey_ptrs) {
238                 (void *) p.start,
239                 (void *) p.end
240         };
241 }
242
243 #define __bkey_extent_entry_for_each_from(_start, _end, _entry)         \
244         for ((_entry) = (_start);                                       \
245              (_entry) < (_end);                                         \
246              (_entry) = extent_entry_next(_entry))
247
248 #define __bkey_ptr_next(_ptr, _end)                                     \
249 ({                                                                      \
250         typeof(_end) _entry;                                            \
251                                                                         \
252         __bkey_extent_entry_for_each_from(to_entry(_ptr), _end, _entry) \
253                 if (extent_entry_is_ptr(_entry))                        \
254                         break;                                          \
255                                                                         \
256         _entry < (_end) ? entry_to_ptr(_entry) : NULL;                  \
257 })
258
259 #define bkey_extent_entry_for_each_from(_p, _entry, _start)             \
260         __bkey_extent_entry_for_each_from(_start, (_p).end, _entry)
261
262 #define bkey_extent_entry_for_each(_p, _entry)                          \
263         bkey_extent_entry_for_each_from(_p, _entry, _p.start)
264
265 #define __bkey_for_each_ptr(_start, _end, _ptr)                         \
266         for ((_ptr) = (_start);                                         \
267              ((_ptr) = __bkey_ptr_next(_ptr, _end));                    \
268              (_ptr)++)
269
270 #define bkey_ptr_next(_p, _ptr)                                         \
271         __bkey_ptr_next(_ptr, (_p).end)
272
273 #define bkey_for_each_ptr(_p, _ptr)                                     \
274         __bkey_for_each_ptr(&(_p).start->ptr, (_p).end, _ptr)
275
276 #define __bkey_ptr_next_decode(_k, _end, _ptr, _entry)                  \
277 ({                                                                      \
278         __label__ out;                                                  \
279                                                                         \
280         (_ptr).idx      = 0;                                            \
281         (_ptr).has_ec   = false;                                        \
282                                                                         \
283         __bkey_extent_entry_for_each_from(_entry, _end, _entry)         \
284                 switch (extent_entry_type(_entry)) {                    \
285                 case BCH_EXTENT_ENTRY_ptr:                              \
286                         (_ptr).ptr              = _entry->ptr;          \
287                         goto out;                                       \
288                 case BCH_EXTENT_ENTRY_crc32:                            \
289                 case BCH_EXTENT_ENTRY_crc64:                            \
290                 case BCH_EXTENT_ENTRY_crc128:                           \
291                         (_ptr).crc = bch2_extent_crc_unpack(_k,         \
292                                         entry_to_crc(_entry));          \
293                         break;                                          \
294                 case BCH_EXTENT_ENTRY_stripe_ptr:                       \
295                         (_ptr).ec = _entry->stripe_ptr;                 \
296                         (_ptr).has_ec   = true;                         \
297                         break;                                          \
298                 }                                                       \
299 out:                                                                    \
300         _entry < (_end);                                                \
301 })
302
303 #define __bkey_for_each_ptr_decode(_k, _start, _end, _ptr, _entry)      \
304         for ((_ptr).crc = bch2_extent_crc_unpack(_k, NULL),             \
305              (_entry) = _start;                                         \
306              __bkey_ptr_next_decode(_k, _end, _ptr, _entry);            \
307              (_entry) = extent_entry_next(_entry))
308
309 #define bkey_for_each_ptr_decode(_k, _p, _ptr, _entry)                  \
310         __bkey_for_each_ptr_decode(_k, (_p).start, (_p).end,            \
311                                    _ptr, _entry)
312
313 #define bkey_crc_next(_k, _start, _end, _crc, _iter)                    \
314 ({                                                                      \
315         __bkey_extent_entry_for_each_from(_iter, _end, _iter)           \
316                 if (extent_entry_is_crc(_iter)) {                       \
317                         (_crc) = bch2_extent_crc_unpack(_k,             \
318                                                 entry_to_crc(_iter));   \
319                         break;                                          \
320                 }                                                       \
321                                                                         \
322         (_iter) < (_end);                                               \
323 })
324
325 #define __bkey_for_each_crc(_k, _start, _end, _crc, _iter)              \
326         for ((_crc) = bch2_extent_crc_unpack(_k, NULL),                 \
327              (_iter) = (_start);                                        \
328              bkey_crc_next(_k, _start, _end, _crc, _iter);              \
329              (_iter) = extent_entry_next(_iter))
330
331 #define bkey_for_each_crc(_k, _p, _crc, _iter)                          \
332         __bkey_for_each_crc(_k, (_p).start, (_p).end, _crc, _iter)
333
334 /* Iterate over pointers in KEY_TYPE_extent: */
335
336 #define extent_for_each_entry_from(_e, _entry, _start)                  \
337         __bkey_extent_entry_for_each_from(_start,                       \
338                                 extent_entry_last(_e),_entry)
339
340 #define extent_for_each_entry(_e, _entry)                               \
341         extent_for_each_entry_from(_e, _entry, (_e).v->start)
342
343 #define extent_ptr_next(_e, _ptr)                                       \
344         __bkey_ptr_next(_ptr, extent_entry_last(_e))
345
346 #define extent_for_each_ptr(_e, _ptr)                                   \
347         __bkey_for_each_ptr(&(_e).v->start->ptr, extent_entry_last(_e), _ptr)
348
349 #define extent_for_each_ptr_decode(_e, _ptr, _entry)                    \
350         __bkey_for_each_ptr_decode((_e).k, (_e).v->start,               \
351                                    extent_entry_last(_e), _ptr, _entry)
352
353 /* utility code common to all keys with pointers: */
354
355 void bch2_mark_io_failure(struct bch_io_failures *,
356                           struct extent_ptr_decoded *);
357 int bch2_bkey_pick_read_device(struct bch_fs *, struct bkey_s_c,
358                                struct bch_io_failures *,
359                                struct extent_ptr_decoded *);
360
361 /* KEY_TYPE_btree_ptr: */
362
363 const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
364 void bch2_btree_ptr_debugcheck(struct bch_fs *, struct bkey_s_c);
365 void bch2_btree_ptr_to_text(struct printbuf *, struct bch_fs *,
366                             struct bkey_s_c);
367
368 #define bch2_bkey_ops_btree_ptr (struct bkey_ops) {             \
369         .key_invalid    = bch2_btree_ptr_invalid,               \
370         .key_debugcheck = bch2_btree_ptr_debugcheck,            \
371         .val_to_text    = bch2_btree_ptr_to_text,               \
372         .swab           = bch2_ptr_swab,                        \
373 }
374
375 /* KEY_TYPE_extent: */
376
377 const char *bch2_extent_invalid(const struct bch_fs *, struct bkey_s_c);
378 void bch2_extent_debugcheck(struct bch_fs *, struct bkey_s_c);
379 void bch2_extent_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
380 enum merge_result bch2_extent_merge(struct bch_fs *,
381                                     struct bkey_s, struct bkey_s);
382
383 #define bch2_bkey_ops_extent (struct bkey_ops) {                \
384         .key_invalid    = bch2_extent_invalid,                  \
385         .key_debugcheck = bch2_extent_debugcheck,               \
386         .val_to_text    = bch2_extent_to_text,                  \
387         .swab           = bch2_ptr_swab,                        \
388         .key_normalize  = bch2_extent_normalize,                \
389         .key_merge      = bch2_extent_merge,                    \
390 }
391
392 /* KEY_TYPE_reservation: */
393
394 const char *bch2_reservation_invalid(const struct bch_fs *, struct bkey_s_c);
395 void bch2_reservation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
396 enum merge_result bch2_reservation_merge(struct bch_fs *,
397                                          struct bkey_s, struct bkey_s);
398
399 #define bch2_bkey_ops_reservation (struct bkey_ops) {           \
400         .key_invalid    = bch2_reservation_invalid,             \
401         .val_to_text    = bch2_reservation_to_text,             \
402         .key_merge      = bch2_reservation_merge,               \
403 }
404
405 /* Extent checksum entries: */
406
407 bool bch2_can_narrow_extent_crcs(struct bkey_s_c,
408                                  struct bch_extent_crc_unpacked);
409 bool bch2_bkey_narrow_crcs(struct bkey_i *, struct bch_extent_crc_unpacked);
410 void bch2_extent_crc_append(struct bkey_i *,
411                             struct bch_extent_crc_unpacked);
412
413 /* Generic code for keys with pointers: */
414
415 static inline bool bkey_extent_is_direct_data(const struct bkey *k)
416 {
417         switch (k->type) {
418         case KEY_TYPE_btree_ptr:
419         case KEY_TYPE_extent:
420         case KEY_TYPE_reflink_v:
421                 return true;
422         default:
423                 return false;
424         }
425 }
426
427 static inline bool bkey_extent_is_data(const struct bkey *k)
428 {
429         return bkey_extent_is_direct_data(k) ||
430                 k->type == KEY_TYPE_inline_data ||
431                 k->type == KEY_TYPE_reflink_p;
432 }
433
434 /*
435  * Should extent be counted under inode->i_sectors?
436  */
437 static inline bool bkey_extent_is_allocation(const struct bkey *k)
438 {
439         switch (k->type) {
440         case KEY_TYPE_extent:
441         case KEY_TYPE_reservation:
442         case KEY_TYPE_reflink_p:
443         case KEY_TYPE_reflink_v:
444         case KEY_TYPE_inline_data:
445                 return true;
446         default:
447                 return false;
448         }
449 }
450
451 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
452 {
453         struct bch_devs_list ret = (struct bch_devs_list) { 0 };
454         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
455         const struct bch_extent_ptr *ptr;
456
457         bkey_for_each_ptr(p, ptr)
458                 ret.devs[ret.nr++] = ptr->dev;
459
460         return ret;
461 }
462
463 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
464 {
465         struct bch_devs_list ret = (struct bch_devs_list) { 0 };
466         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
467         const struct bch_extent_ptr *ptr;
468
469         bkey_for_each_ptr(p, ptr)
470                 if (!ptr->cached)
471                         ret.devs[ret.nr++] = ptr->dev;
472
473         return ret;
474 }
475
476 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
477 {
478         struct bch_devs_list ret = (struct bch_devs_list) { 0 };
479         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
480         const struct bch_extent_ptr *ptr;
481
482         bkey_for_each_ptr(p, ptr)
483                 if (ptr->cached)
484                         ret.devs[ret.nr++] = ptr->dev;
485
486         return ret;
487 }
488
489 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c);
490 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c);
491 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c);
492 bool bch2_bkey_is_incompressible(struct bkey_s_c);
493 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c);
494 bool bch2_check_range_allocated(struct bch_fs *, struct bpos, u64, unsigned);
495 unsigned bch2_bkey_durability(struct bch_fs *, struct bkey_s_c);
496
497 void bch2_bkey_mark_replicas_cached(struct bch_fs *, struct bkey_s,
498                                     unsigned, unsigned);
499
500 void bch2_bkey_append_ptr(struct bkey_i *, struct bch_extent_ptr);
501 void bch2_extent_ptr_decoded_append(struct bkey_i *,
502                                     struct extent_ptr_decoded *);
503 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s,
504                                            struct bch_extent_ptr *);
505
506 #define bch2_bkey_drop_ptrs(_k, _ptr, _cond)                            \
507 do {                                                                    \
508         struct bkey_ptrs _ptrs = bch2_bkey_ptrs(_k);                    \
509                                                                         \
510         _ptr = &_ptrs.start->ptr;                                       \
511                                                                         \
512         while ((_ptr = bkey_ptr_next(_ptrs, _ptr))) {                   \
513                 if (_cond) {                                            \
514                         _ptr = (void *) bch2_bkey_drop_ptr(_k, _ptr);   \
515                         _ptrs = bch2_bkey_ptrs(_k);                     \
516                         continue;                                       \
517                 }                                                       \
518                                                                         \
519                 (_ptr)++;                                               \
520         }                                                               \
521 } while (0)
522
523 void bch2_bkey_drop_device(struct bkey_s, unsigned);
524 const struct bch_extent_ptr *bch2_bkey_has_device(struct bkey_s_c, unsigned);
525 bool bch2_bkey_has_target(struct bch_fs *, struct bkey_s_c, unsigned);
526
527 bool bch2_bkey_matches_ptr(struct bch_fs *, struct bkey_s_c,
528                            struct bch_extent_ptr, u64);
529
530 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
531 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
532                             struct bkey_s_c);
533 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c);
534
535 void bch2_ptr_swab(const struct bkey_format *, struct bkey_packed *);
536
537 /* Generic extent code: */
538
539 int bch2_cut_front_s(struct bpos, struct bkey_s);
540 int bch2_cut_back_s(struct bpos, struct bkey_s);
541
542 static inline void bch2_cut_front(struct bpos where, struct bkey_i *k)
543 {
544         bch2_cut_front_s(where, bkey_i_to_s(k));
545 }
546
547 static inline void bch2_cut_back(struct bpos where, struct bkey_i *k)
548 {
549         bch2_cut_back_s(where, bkey_i_to_s(k));
550 }
551
552 /**
553  * bch_key_resize - adjust size of @k
554  *
555  * bkey_start_offset(k) will be preserved, modifies where the extent ends
556  */
557 static inline void bch2_key_resize(struct bkey *k, unsigned new_size)
558 {
559         k->p.offset -= k->size;
560         k->p.offset += new_size;
561         k->size = new_size;
562 }
563
564 /*
565  * In extent_sort_fix_overlapping(), insert_fixup_extent(),
566  * extent_merge_inline() - we're modifying keys in place that are packed. To do
567  * that we have to unpack the key, modify the unpacked key - then this
568  * copies/repacks the unpacked to the original as necessary.
569  */
570 static inline void extent_save(struct btree *b, struct bkey_packed *dst,
571                                struct bkey *src)
572 {
573         struct bkey_format *f = &b->format;
574         struct bkey_i *dst_unpacked;
575
576         if ((dst_unpacked = packed_to_bkey(dst)))
577                 dst_unpacked->k = *src;
578         else
579                 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
580 }
581
582 #endif /* _BCACHEFS_EXTENTS_H */