]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/extents.h
Update bcachefs sources to c9eb15545d bcachefs: Don't call trans_iter_put() on error...
[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 /* bkey_ptrs: generically over any key type that has ptrs */
179
180 struct bkey_ptrs_c {
181         const union bch_extent_entry    *start;
182         const union bch_extent_entry    *end;
183 };
184
185 struct bkey_ptrs {
186         union bch_extent_entry  *start;
187         union bch_extent_entry  *end;
188 };
189
190 static inline struct bkey_ptrs_c bch2_bkey_ptrs_c(struct bkey_s_c k)
191 {
192         switch (k.k->type) {
193         case KEY_TYPE_btree_ptr: {
194                 struct bkey_s_c_btree_ptr e = bkey_s_c_to_btree_ptr(k);
195                 return (struct bkey_ptrs_c) {
196                         to_entry(&e.v->start[0]),
197                         to_entry(extent_entry_last(e))
198                 };
199         }
200         case KEY_TYPE_extent: {
201                 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
202                 return (struct bkey_ptrs_c) {
203                         e.v->start,
204                         extent_entry_last(e)
205                 };
206         }
207         case KEY_TYPE_stripe: {
208                 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
209                 return (struct bkey_ptrs_c) {
210                         to_entry(&s.v->ptrs[0]),
211                         to_entry(&s.v->ptrs[s.v->nr_blocks]),
212                 };
213         }
214         case KEY_TYPE_reflink_v: {
215                 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
216
217                 return (struct bkey_ptrs_c) {
218                         r.v->start,
219                         bkey_val_end(r),
220                 };
221         }
222         default:
223                 return (struct bkey_ptrs_c) { NULL, NULL };
224         }
225 }
226
227 static inline struct bkey_ptrs bch2_bkey_ptrs(struct bkey_s k)
228 {
229         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k.s_c);
230
231         return (struct bkey_ptrs) {
232                 (void *) p.start,
233                 (void *) p.end
234         };
235 }
236
237 #define __bkey_extent_entry_for_each_from(_start, _end, _entry)         \
238         for ((_entry) = (_start);                                       \
239              (_entry) < (_end);                                         \
240              (_entry) = extent_entry_next(_entry))
241
242 #define __bkey_ptr_next(_ptr, _end)                                     \
243 ({                                                                      \
244         typeof(_end) _entry;                                            \
245                                                                         \
246         __bkey_extent_entry_for_each_from(to_entry(_ptr), _end, _entry) \
247                 if (extent_entry_is_ptr(_entry))                        \
248                         break;                                          \
249                                                                         \
250         _entry < (_end) ? entry_to_ptr(_entry) : NULL;                  \
251 })
252
253 #define bkey_extent_entry_for_each_from(_p, _entry, _start)             \
254         __bkey_extent_entry_for_each_from(_start, (_p).end, _entry)
255
256 #define bkey_extent_entry_for_each(_p, _entry)                          \
257         bkey_extent_entry_for_each_from(_p, _entry, _p.start)
258
259 #define __bkey_for_each_ptr(_start, _end, _ptr)                         \
260         for ((_ptr) = (_start);                                         \
261              ((_ptr) = __bkey_ptr_next(_ptr, _end));                    \
262              (_ptr)++)
263
264 #define bkey_ptr_next(_p, _ptr)                                         \
265         __bkey_ptr_next(_ptr, (_p).end)
266
267 #define bkey_for_each_ptr(_p, _ptr)                                     \
268         __bkey_for_each_ptr(&(_p).start->ptr, (_p).end, _ptr)
269
270 #define __bkey_ptr_next_decode(_k, _end, _ptr, _entry)                  \
271 ({                                                                      \
272         __label__ out;                                                  \
273                                                                         \
274         (_ptr).idx      = 0;                                            \
275         (_ptr).has_ec   = false;                                        \
276                                                                         \
277         __bkey_extent_entry_for_each_from(_entry, _end, _entry)         \
278                 switch (extent_entry_type(_entry)) {                    \
279                 case BCH_EXTENT_ENTRY_ptr:                              \
280                         (_ptr).ptr              = _entry->ptr;          \
281                         goto out;                                       \
282                 case BCH_EXTENT_ENTRY_crc32:                            \
283                 case BCH_EXTENT_ENTRY_crc64:                            \
284                 case BCH_EXTENT_ENTRY_crc128:                           \
285                         (_ptr).crc = bch2_extent_crc_unpack(_k,         \
286                                         entry_to_crc(_entry));          \
287                         break;                                          \
288                 case BCH_EXTENT_ENTRY_stripe_ptr:                       \
289                         (_ptr).ec = _entry->stripe_ptr;                 \
290                         (_ptr).has_ec   = true;                         \
291                         break;                                          \
292                 }                                                       \
293 out:                                                                    \
294         _entry < (_end);                                                \
295 })
296
297 #define __bkey_for_each_ptr_decode(_k, _start, _end, _ptr, _entry)      \
298         for ((_ptr).crc = bch2_extent_crc_unpack(_k, NULL),             \
299              (_entry) = _start;                                         \
300              __bkey_ptr_next_decode(_k, _end, _ptr, _entry);            \
301              (_entry) = extent_entry_next(_entry))
302
303 #define bkey_for_each_ptr_decode(_k, _p, _ptr, _entry)                  \
304         __bkey_for_each_ptr_decode(_k, (_p).start, (_p).end,            \
305                                    _ptr, _entry)
306
307 #define bkey_crc_next(_k, _start, _end, _crc, _iter)                    \
308 ({                                                                      \
309         __bkey_extent_entry_for_each_from(_iter, _end, _iter)           \
310                 if (extent_entry_is_crc(_iter)) {                       \
311                         (_crc) = bch2_extent_crc_unpack(_k,             \
312                                                 entry_to_crc(_iter));   \
313                         break;                                          \
314                 }                                                       \
315                                                                         \
316         (_iter) < (_end);                                               \
317 })
318
319 #define __bkey_for_each_crc(_k, _start, _end, _crc, _iter)              \
320         for ((_crc) = bch2_extent_crc_unpack(_k, NULL),                 \
321              (_iter) = (_start);                                        \
322              bkey_crc_next(_k, _start, _end, _crc, _iter);              \
323              (_iter) = extent_entry_next(_iter))
324
325 #define bkey_for_each_crc(_k, _p, _crc, _iter)                          \
326         __bkey_for_each_crc(_k, (_p).start, (_p).end, _crc, _iter)
327
328 /* Iterate over pointers in KEY_TYPE_extent: */
329
330 #define extent_for_each_entry_from(_e, _entry, _start)                  \
331         __bkey_extent_entry_for_each_from(_start,                       \
332                                 extent_entry_last(_e),_entry)
333
334 #define extent_for_each_entry(_e, _entry)                               \
335         extent_for_each_entry_from(_e, _entry, (_e).v->start)
336
337 #define extent_ptr_next(_e, _ptr)                                       \
338         __bkey_ptr_next(_ptr, extent_entry_last(_e))
339
340 #define extent_for_each_ptr(_e, _ptr)                                   \
341         __bkey_for_each_ptr(&(_e).v->start->ptr, extent_entry_last(_e), _ptr)
342
343 #define extent_for_each_ptr_decode(_e, _ptr, _entry)                    \
344         __bkey_for_each_ptr_decode((_e).k, (_e).v->start,               \
345                                    extent_entry_last(_e), _ptr, _entry)
346
347 /* utility code common to all keys with pointers: */
348
349 void bch2_mark_io_failure(struct bch_io_failures *,
350                           struct extent_ptr_decoded *);
351 int bch2_bkey_pick_read_device(struct bch_fs *, struct bkey_s_c,
352                                struct bch_io_failures *,
353                                struct extent_ptr_decoded *);
354
355 /* KEY_TYPE_btree_ptr: */
356
357 const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
358 void bch2_btree_ptr_debugcheck(struct bch_fs *, struct bkey_s_c);
359 void bch2_btree_ptr_to_text(struct printbuf *, struct bch_fs *,
360                             struct bkey_s_c);
361
362 #define bch2_bkey_ops_btree_ptr (struct bkey_ops) {             \
363         .key_invalid    = bch2_btree_ptr_invalid,               \
364         .key_debugcheck = bch2_btree_ptr_debugcheck,            \
365         .val_to_text    = bch2_btree_ptr_to_text,               \
366         .swab           = bch2_ptr_swab,                        \
367 }
368
369 /* KEY_TYPE_extent: */
370
371 const char *bch2_extent_invalid(const struct bch_fs *, struct bkey_s_c);
372 void bch2_extent_debugcheck(struct bch_fs *, struct bkey_s_c);
373 void bch2_extent_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
374 enum merge_result bch2_extent_merge(struct bch_fs *,
375                                     struct bkey_s, struct bkey_s);
376
377 #define bch2_bkey_ops_extent (struct bkey_ops) {                \
378         .key_invalid    = bch2_extent_invalid,                  \
379         .key_debugcheck = bch2_extent_debugcheck,               \
380         .val_to_text    = bch2_extent_to_text,                  \
381         .swab           = bch2_ptr_swab,                        \
382         .key_normalize  = bch2_extent_normalize,                \
383         .key_merge      = bch2_extent_merge,                    \
384 }
385
386 /* KEY_TYPE_reservation: */
387
388 const char *bch2_reservation_invalid(const struct bch_fs *, struct bkey_s_c);
389 void bch2_reservation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
390 enum merge_result bch2_reservation_merge(struct bch_fs *,
391                                          struct bkey_s, struct bkey_s);
392
393 #define bch2_bkey_ops_reservation (struct bkey_ops) {           \
394         .key_invalid    = bch2_reservation_invalid,             \
395         .val_to_text    = bch2_reservation_to_text,             \
396         .key_merge      = bch2_reservation_merge,               \
397 }
398
399 /* Extent checksum entries: */
400
401 bool bch2_can_narrow_extent_crcs(struct bkey_s_c,
402                                  struct bch_extent_crc_unpacked);
403 bool bch2_bkey_narrow_crcs(struct bkey_i *, struct bch_extent_crc_unpacked);
404 void bch2_extent_crc_append(struct bkey_i *,
405                             struct bch_extent_crc_unpacked);
406
407 /* Generic code for keys with pointers: */
408
409 static inline bool bkey_extent_is_direct_data(const struct bkey *k)
410 {
411         switch (k->type) {
412         case KEY_TYPE_btree_ptr:
413         case KEY_TYPE_extent:
414         case KEY_TYPE_reflink_v:
415                 return true;
416         default:
417                 return false;
418         }
419 }
420
421 static inline bool bkey_extent_is_data(const struct bkey *k)
422 {
423         return bkey_extent_is_direct_data(k) ||
424                 k->type == KEY_TYPE_inline_data ||
425                 k->type == KEY_TYPE_reflink_p;
426 }
427
428 /*
429  * Should extent be counted under inode->i_sectors?
430  */
431 static inline bool bkey_extent_is_allocation(const struct bkey *k)
432 {
433         switch (k->type) {
434         case KEY_TYPE_extent:
435         case KEY_TYPE_reservation:
436         case KEY_TYPE_reflink_p:
437         case KEY_TYPE_reflink_v:
438         case KEY_TYPE_inline_data:
439                 return true;
440         default:
441                 return false;
442         }
443 }
444
445 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
446 {
447         struct bch_devs_list ret = (struct bch_devs_list) { 0 };
448         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
449         const struct bch_extent_ptr *ptr;
450
451         bkey_for_each_ptr(p, ptr)
452                 ret.devs[ret.nr++] = ptr->dev;
453
454         return ret;
455 }
456
457 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
458 {
459         struct bch_devs_list ret = (struct bch_devs_list) { 0 };
460         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
461         const struct bch_extent_ptr *ptr;
462
463         bkey_for_each_ptr(p, ptr)
464                 if (!ptr->cached)
465                         ret.devs[ret.nr++] = ptr->dev;
466
467         return ret;
468 }
469
470 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
471 {
472         struct bch_devs_list ret = (struct bch_devs_list) { 0 };
473         struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
474         const struct bch_extent_ptr *ptr;
475
476         bkey_for_each_ptr(p, ptr)
477                 if (ptr->cached)
478                         ret.devs[ret.nr++] = ptr->dev;
479
480         return ret;
481 }
482
483 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c);
484 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c);
485 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c);
486 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c);
487 bool bch2_check_range_allocated(struct bch_fs *, struct bpos, u64, unsigned);
488 unsigned bch2_bkey_durability(struct bch_fs *, struct bkey_s_c);
489
490 void bch2_bkey_mark_replicas_cached(struct bch_fs *, struct bkey_s,
491                                     unsigned, unsigned);
492
493 void bch2_bkey_append_ptr(struct bkey_i *, struct bch_extent_ptr);
494 void bch2_extent_ptr_decoded_append(struct bkey_i *,
495                                     struct extent_ptr_decoded *);
496 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s,
497                                            struct bch_extent_ptr *);
498
499 #define bch2_bkey_drop_ptrs(_k, _ptr, _cond)                            \
500 do {                                                                    \
501         struct bkey_ptrs _ptrs = bch2_bkey_ptrs(_k);                    \
502                                                                         \
503         _ptr = &_ptrs.start->ptr;                                       \
504                                                                         \
505         while ((_ptr = bkey_ptr_next(_ptrs, _ptr))) {                   \
506                 if (_cond) {                                            \
507                         _ptr = (void *) bch2_bkey_drop_ptr(_k, _ptr);   \
508                         _ptrs = bch2_bkey_ptrs(_k);                     \
509                         continue;                                       \
510                 }                                                       \
511                                                                         \
512                 (_ptr)++;                                               \
513         }                                                               \
514 } while (0)
515
516 void bch2_bkey_drop_device(struct bkey_s, unsigned);
517 const struct bch_extent_ptr *bch2_bkey_has_device(struct bkey_s_c, unsigned);
518 bool bch2_bkey_has_target(struct bch_fs *, struct bkey_s_c, unsigned);
519
520 bool bch2_bkey_matches_ptr(struct bch_fs *, struct bkey_s_c,
521                            struct bch_extent_ptr, u64);
522
523 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
524 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
525                             struct bkey_s_c);
526 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c);
527
528 void bch2_ptr_swab(const struct bkey_format *, struct bkey_packed *);
529
530 /* Generic extent code: */
531
532 int bch2_cut_front_s(struct bpos, struct bkey_s);
533 int bch2_cut_back_s(struct bpos, struct bkey_s);
534
535 static inline void bch2_cut_front(struct bpos where, struct bkey_i *k)
536 {
537         bch2_cut_front_s(where, bkey_i_to_s(k));
538 }
539
540 static inline void bch2_cut_back(struct bpos where, struct bkey_i *k)
541 {
542         bch2_cut_back_s(where, bkey_i_to_s(k));
543 }
544
545 /**
546  * bch_key_resize - adjust size of @k
547  *
548  * bkey_start_offset(k) will be preserved, modifies where the extent ends
549  */
550 static inline void bch2_key_resize(struct bkey *k, unsigned new_size)
551 {
552         k->p.offset -= k->size;
553         k->p.offset += new_size;
554         k->size = new_size;
555 }
556
557 /*
558  * In extent_sort_fix_overlapping(), insert_fixup_extent(),
559  * extent_merge_inline() - we're modifying keys in place that are packed. To do
560  * that we have to unpack the key, modify the unpacked key - then this
561  * copies/repacks the unpacked to the original as necessary.
562  */
563 static inline void extent_save(struct btree *b, struct bkey_packed *dst,
564                                struct bkey *src)
565 {
566         struct bkey_format *f = &b->format;
567         struct bkey_i *dst_unpacked;
568
569         if ((dst_unpacked = packed_to_bkey(dst)))
570                 dst_unpacked->k = *src;
571         else
572                 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
573 }
574
575 #endif /* _BCACHEFS_EXTENTS_H */