1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_EXTENTS_H
3 #define _BCACHEFS_EXTENTS_H
7 #include "extents_types.h"
14 #define extent_entry_last(_e) \
15 ((typeof(&(_e).v->start[0])) bkey_val_end(_e))
17 #define entry_to_ptr(_entry) \
19 EBUG_ON((_entry) && !extent_entry_is_ptr(_entry)); \
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)); \
27 /* downcast, preserves const */
28 #define to_entry(_entry) \
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 *)); \
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)); \
42 #define extent_entry_next(_entry) \
43 ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
45 static inline unsigned
46 __extent_entry_type(const union bch_extent_entry *e)
48 return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
51 static inline enum bch_extent_entry_type
52 extent_entry_type(const union bch_extent_entry *e)
54 int ret = __ffs(e->type);
56 EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
61 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
63 switch (extent_entry_type(entry)) {
65 case BCH_EXTENT_ENTRY_##f: \
66 return sizeof(struct bch_extent_##f);
67 BCH_EXTENT_ENTRY_TYPES()
74 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
76 return extent_entry_bytes(entry) / sizeof(u64);
79 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
81 switch (extent_entry_type(e)) {
82 case BCH_EXTENT_ENTRY_ptr:
89 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
91 switch (extent_entry_type(e)) {
92 case BCH_EXTENT_ENTRY_crc32:
93 case BCH_EXTENT_ENTRY_crc64:
94 case BCH_EXTENT_ENTRY_crc128:
101 union bch_extent_crc {
103 struct bch_extent_crc32 crc32;
104 struct bch_extent_crc64 crc64;
105 struct bch_extent_crc128 crc128;
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))
114 #define entry_to_crc(_entry) \
116 EBUG_ON((_entry) && !extent_entry_is_crc(_entry)); \
118 __entry_to_crc(_entry); \
121 static inline struct bch_extent_crc_unpacked
122 bch2_extent_crc_unpack(const struct bkey *k, const union bch_extent_crc *crc)
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, \
133 return (struct bch_extent_crc_unpacked) {
134 .compressed_size = k->size,
135 .uncompressed_size = k->size,
136 .live_size = k->size,
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),
145 *((__le32 *) &ret.csum.lo) = crc->crc32.csum;
147 memcpy(&ret.csum.lo, &crc->crc32.csum,
148 sizeof(crc->crc32.csum));
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,
159 *((__le16 *) &ret.csum.hi) = crc->crc64.csum_hi;
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,
178 static inline bool crc_is_compressed(struct bch_extent_crc_unpacked crc)
180 return (crc.compression_type != BCH_COMPRESSION_TYPE_none &&
181 crc.compression_type != BCH_COMPRESSION_TYPE_incompressible);
184 /* bkey_ptrs: generically over any key type that has ptrs */
187 const union bch_extent_entry *start;
188 const union bch_extent_entry *end;
192 union bch_extent_entry *start;
193 union bch_extent_entry *end;
196 static inline struct bkey_ptrs_c bch2_bkey_ptrs_c(struct bkey_s_c k)
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))
206 case KEY_TYPE_extent: {
207 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
208 return (struct bkey_ptrs_c) {
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]),
220 case KEY_TYPE_reflink_v: {
221 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
223 return (struct bkey_ptrs_c) {
228 case KEY_TYPE_btree_ptr_v2: {
229 struct bkey_s_c_btree_ptr_v2 e = bkey_s_c_to_btree_ptr_v2(k);
230 return (struct bkey_ptrs_c) {
231 to_entry(&e.v->start[0]),
232 to_entry(extent_entry_last(e))
236 return (struct bkey_ptrs_c) { NULL, NULL };
240 static inline struct bkey_ptrs bch2_bkey_ptrs(struct bkey_s k)
242 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k.s_c);
244 return (struct bkey_ptrs) {
250 #define __bkey_extent_entry_for_each_from(_start, _end, _entry) \
251 for ((_entry) = (_start); \
253 (_entry) = extent_entry_next(_entry))
255 #define __bkey_ptr_next(_ptr, _end) \
257 typeof(_end) _entry; \
259 __bkey_extent_entry_for_each_from(to_entry(_ptr), _end, _entry) \
260 if (extent_entry_is_ptr(_entry)) \
263 _entry < (_end) ? entry_to_ptr(_entry) : NULL; \
266 #define bkey_extent_entry_for_each_from(_p, _entry, _start) \
267 __bkey_extent_entry_for_each_from(_start, (_p).end, _entry)
269 #define bkey_extent_entry_for_each(_p, _entry) \
270 bkey_extent_entry_for_each_from(_p, _entry, _p.start)
272 #define __bkey_for_each_ptr(_start, _end, _ptr) \
273 for ((_ptr) = (_start); \
274 ((_ptr) = __bkey_ptr_next(_ptr, _end)); \
277 #define bkey_ptr_next(_p, _ptr) \
278 __bkey_ptr_next(_ptr, (_p).end)
280 #define bkey_for_each_ptr(_p, _ptr) \
281 __bkey_for_each_ptr(&(_p).start->ptr, (_p).end, _ptr)
283 #define __bkey_ptr_next_decode(_k, _end, _ptr, _entry) \
288 (_ptr).has_ec = false; \
290 __bkey_extent_entry_for_each_from(_entry, _end, _entry) \
291 switch (extent_entry_type(_entry)) { \
292 case BCH_EXTENT_ENTRY_ptr: \
293 (_ptr).ptr = _entry->ptr; \
295 case BCH_EXTENT_ENTRY_crc32: \
296 case BCH_EXTENT_ENTRY_crc64: \
297 case BCH_EXTENT_ENTRY_crc128: \
298 (_ptr).crc = bch2_extent_crc_unpack(_k, \
299 entry_to_crc(_entry)); \
301 case BCH_EXTENT_ENTRY_stripe_ptr: \
302 (_ptr).ec = _entry->stripe_ptr; \
303 (_ptr).has_ec = true; \
310 #define __bkey_for_each_ptr_decode(_k, _start, _end, _ptr, _entry) \
311 for ((_ptr).crc = bch2_extent_crc_unpack(_k, NULL), \
313 __bkey_ptr_next_decode(_k, _end, _ptr, _entry); \
314 (_entry) = extent_entry_next(_entry))
316 #define bkey_for_each_ptr_decode(_k, _p, _ptr, _entry) \
317 __bkey_for_each_ptr_decode(_k, (_p).start, (_p).end, \
320 #define bkey_crc_next(_k, _start, _end, _crc, _iter) \
322 __bkey_extent_entry_for_each_from(_iter, _end, _iter) \
323 if (extent_entry_is_crc(_iter)) { \
324 (_crc) = bch2_extent_crc_unpack(_k, \
325 entry_to_crc(_iter)); \
332 #define __bkey_for_each_crc(_k, _start, _end, _crc, _iter) \
333 for ((_crc) = bch2_extent_crc_unpack(_k, NULL), \
334 (_iter) = (_start); \
335 bkey_crc_next(_k, _start, _end, _crc, _iter); \
336 (_iter) = extent_entry_next(_iter))
338 #define bkey_for_each_crc(_k, _p, _crc, _iter) \
339 __bkey_for_each_crc(_k, (_p).start, (_p).end, _crc, _iter)
341 /* Iterate over pointers in KEY_TYPE_extent: */
343 #define extent_for_each_entry_from(_e, _entry, _start) \
344 __bkey_extent_entry_for_each_from(_start, \
345 extent_entry_last(_e),_entry)
347 #define extent_for_each_entry(_e, _entry) \
348 extent_for_each_entry_from(_e, _entry, (_e).v->start)
350 #define extent_ptr_next(_e, _ptr) \
351 __bkey_ptr_next(_ptr, extent_entry_last(_e))
353 #define extent_for_each_ptr(_e, _ptr) \
354 __bkey_for_each_ptr(&(_e).v->start->ptr, extent_entry_last(_e), _ptr)
356 #define extent_for_each_ptr_decode(_e, _ptr, _entry) \
357 __bkey_for_each_ptr_decode((_e).k, (_e).v->start, \
358 extent_entry_last(_e), _ptr, _entry)
360 /* utility code common to all keys with pointers: */
362 void bch2_mark_io_failure(struct bch_io_failures *,
363 struct extent_ptr_decoded *);
364 int bch2_bkey_pick_read_device(struct bch_fs *, struct bkey_s_c,
365 struct bch_io_failures *,
366 struct extent_ptr_decoded *);
368 /* KEY_TYPE_btree_ptr: */
370 const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
371 void bch2_btree_ptr_to_text(struct printbuf *, struct bch_fs *,
374 const char *bch2_btree_ptr_v2_invalid(const struct bch_fs *, struct bkey_s_c);
375 void bch2_btree_ptr_v2_to_text(struct printbuf *, struct bch_fs *,
377 void bch2_btree_ptr_v2_compat(enum btree_id, unsigned, unsigned,
380 #define bch2_bkey_ops_btree_ptr (struct bkey_ops) { \
381 .key_invalid = bch2_btree_ptr_invalid, \
382 .val_to_text = bch2_btree_ptr_to_text, \
383 .swab = bch2_ptr_swab, \
386 #define bch2_bkey_ops_btree_ptr_v2 (struct bkey_ops) { \
387 .key_invalid = bch2_btree_ptr_v2_invalid, \
388 .val_to_text = bch2_btree_ptr_v2_to_text, \
389 .swab = bch2_ptr_swab, \
390 .compat = bch2_btree_ptr_v2_compat, \
393 /* KEY_TYPE_extent: */
395 const char *bch2_extent_invalid(const struct bch_fs *, struct bkey_s_c);
396 void bch2_extent_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
397 enum merge_result bch2_extent_merge(struct bch_fs *,
398 struct bkey_s, struct bkey_s);
400 #define bch2_bkey_ops_extent (struct bkey_ops) { \
401 .key_invalid = bch2_extent_invalid, \
402 .val_to_text = bch2_extent_to_text, \
403 .swab = bch2_ptr_swab, \
404 .key_normalize = bch2_extent_normalize, \
405 .key_merge = bch2_extent_merge, \
408 /* KEY_TYPE_reservation: */
410 const char *bch2_reservation_invalid(const struct bch_fs *, struct bkey_s_c);
411 void bch2_reservation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
412 enum merge_result bch2_reservation_merge(struct bch_fs *,
413 struct bkey_s, struct bkey_s);
415 #define bch2_bkey_ops_reservation (struct bkey_ops) { \
416 .key_invalid = bch2_reservation_invalid, \
417 .val_to_text = bch2_reservation_to_text, \
418 .key_merge = bch2_reservation_merge, \
421 /* Extent checksum entries: */
423 bool bch2_can_narrow_extent_crcs(struct bkey_s_c,
424 struct bch_extent_crc_unpacked);
425 bool bch2_bkey_narrow_crcs(struct bkey_i *, struct bch_extent_crc_unpacked);
426 void bch2_extent_crc_append(struct bkey_i *,
427 struct bch_extent_crc_unpacked);
429 /* Generic code for keys with pointers: */
431 static inline bool bkey_extent_is_direct_data(const struct bkey *k)
434 case KEY_TYPE_btree_ptr:
435 case KEY_TYPE_btree_ptr_v2:
436 case KEY_TYPE_extent:
437 case KEY_TYPE_reflink_v:
444 static inline bool bkey_extent_is_inline_data(const struct bkey *k)
446 return k->type == KEY_TYPE_inline_data ||
447 k->type == KEY_TYPE_indirect_inline_data;
450 static inline unsigned bkey_inline_data_offset(const struct bkey *k)
453 case KEY_TYPE_inline_data:
454 return sizeof(struct bch_inline_data);
455 case KEY_TYPE_indirect_inline_data:
456 return sizeof(struct bch_indirect_inline_data);
462 static inline unsigned bkey_inline_data_bytes(const struct bkey *k)
464 return bkey_val_bytes(k) - bkey_inline_data_offset(k);
467 #define bkey_inline_data_p(_k) (((void *) (_k).v) + bkey_inline_data_offset((_k).k))
469 static inline bool bkey_extent_is_data(const struct bkey *k)
471 return bkey_extent_is_direct_data(k) ||
472 bkey_extent_is_inline_data(k) ||
473 k->type == KEY_TYPE_reflink_p;
477 * Should extent be counted under inode->i_sectors?
479 static inline bool bkey_extent_is_allocation(const struct bkey *k)
482 case KEY_TYPE_extent:
483 case KEY_TYPE_reservation:
484 case KEY_TYPE_reflink_p:
485 case KEY_TYPE_reflink_v:
486 case KEY_TYPE_inline_data:
487 case KEY_TYPE_indirect_inline_data:
494 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
496 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
497 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
498 const struct bch_extent_ptr *ptr;
500 bkey_for_each_ptr(p, ptr)
501 ret.devs[ret.nr++] = ptr->dev;
506 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
508 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
509 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
510 const struct bch_extent_ptr *ptr;
512 bkey_for_each_ptr(p, ptr)
514 ret.devs[ret.nr++] = ptr->dev;
519 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
521 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
522 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
523 const struct bch_extent_ptr *ptr;
525 bkey_for_each_ptr(p, ptr)
527 ret.devs[ret.nr++] = ptr->dev;
532 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c);
533 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c);
534 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c);
535 bool bch2_bkey_is_incompressible(struct bkey_s_c);
536 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c);
537 bool bch2_check_range_allocated(struct bch_fs *, struct bpos, u64, unsigned, bool);
539 unsigned bch2_bkey_replicas(struct bch_fs *, struct bkey_s_c);
540 unsigned bch2_bkey_durability(struct bch_fs *, struct bkey_s_c);
542 void bch2_bkey_mark_replicas_cached(struct bch_fs *, struct bkey_s,
545 void bch2_bkey_extent_entry_drop(struct bkey_i *, union bch_extent_entry *);
546 void bch2_bkey_append_ptr(struct bkey_i *, struct bch_extent_ptr);
547 void bch2_extent_ptr_decoded_append(struct bkey_i *,
548 struct extent_ptr_decoded *);
549 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s,
550 struct bch_extent_ptr *);
552 #define bch2_bkey_drop_ptrs(_k, _ptr, _cond) \
554 struct bkey_ptrs _ptrs = bch2_bkey_ptrs(_k); \
556 _ptr = &_ptrs.start->ptr; \
558 while ((_ptr = bkey_ptr_next(_ptrs, _ptr))) { \
560 _ptr = (void *) bch2_bkey_drop_ptr(_k, _ptr); \
561 _ptrs = bch2_bkey_ptrs(_k); \
569 void bch2_bkey_drop_device(struct bkey_s, unsigned);
570 const struct bch_extent_ptr *bch2_bkey_has_device(struct bkey_s_c, unsigned);
571 bool bch2_bkey_has_target(struct bch_fs *, struct bkey_s_c, unsigned);
573 bool bch2_bkey_matches_ptr(struct bch_fs *, struct bkey_s_c,
574 struct bch_extent_ptr, u64);
576 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
577 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
579 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c);
581 void bch2_ptr_swab(struct bkey_s);
583 /* Generic extent code: */
585 enum bch_extent_overlap {
586 BCH_EXTENT_OVERLAP_ALL = 0,
587 BCH_EXTENT_OVERLAP_BACK = 1,
588 BCH_EXTENT_OVERLAP_FRONT = 2,
589 BCH_EXTENT_OVERLAP_MIDDLE = 3,
592 /* Returns how k overlaps with m */
593 static inline enum bch_extent_overlap bch2_extent_overlap(const struct bkey *k,
594 const struct bkey *m)
596 int cmp1 = bkey_cmp(k->p, m->p) < 0;
597 int cmp2 = bkey_cmp(bkey_start_pos(k),
598 bkey_start_pos(m)) > 0;
600 return (cmp1 << 1) + cmp2;
603 int bch2_cut_front_s(struct bpos, struct bkey_s);
604 int bch2_cut_back_s(struct bpos, struct bkey_s);
606 static inline void bch2_cut_front(struct bpos where, struct bkey_i *k)
608 bch2_cut_front_s(where, bkey_i_to_s(k));
611 static inline void bch2_cut_back(struct bpos where, struct bkey_i *k)
613 bch2_cut_back_s(where, bkey_i_to_s(k));
617 * bch_key_resize - adjust size of @k
619 * bkey_start_offset(k) will be preserved, modifies where the extent ends
621 static inline void bch2_key_resize(struct bkey *k, unsigned new_size)
623 k->p.offset -= k->size;
624 k->p.offset += new_size;
629 * In extent_sort_fix_overlapping(), insert_fixup_extent(),
630 * extent_merge_inline() - we're modifying keys in place that are packed. To do
631 * that we have to unpack the key, modify the unpacked key - then this
632 * copies/repacks the unpacked to the original as necessary.
634 static inline void extent_save(struct btree *b, struct bkey_packed *dst,
637 struct bkey_format *f = &b->format;
638 struct bkey_i *dst_unpacked;
640 if ((dst_unpacked = packed_to_bkey(dst)))
641 dst_unpacked->k = *src;
643 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
646 #endif /* _BCACHEFS_EXTENTS_H */