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 /* bkey_ptrs: generically over any key type that has ptrs */
181 const union bch_extent_entry *start;
182 const union bch_extent_entry *end;
186 union bch_extent_entry *start;
187 union bch_extent_entry *end;
190 static inline struct bkey_ptrs_c bch2_bkey_ptrs_c(struct bkey_s_c k)
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))
200 case KEY_TYPE_extent: {
201 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
202 return (struct bkey_ptrs_c) {
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]),
214 case KEY_TYPE_reflink_v: {
215 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
217 return (struct bkey_ptrs_c) {
223 return (struct bkey_ptrs_c) { NULL, NULL };
227 static inline struct bkey_ptrs bch2_bkey_ptrs(struct bkey_s k)
229 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k.s_c);
231 return (struct bkey_ptrs) {
237 #define __bkey_extent_entry_for_each_from(_start, _end, _entry) \
238 for ((_entry) = (_start); \
240 (_entry) = extent_entry_next(_entry))
242 #define __bkey_ptr_next(_ptr, _end) \
244 typeof(_end) _entry; \
246 __bkey_extent_entry_for_each_from(to_entry(_ptr), _end, _entry) \
247 if (extent_entry_is_ptr(_entry)) \
250 _entry < (_end) ? entry_to_ptr(_entry) : NULL; \
253 #define bkey_extent_entry_for_each_from(_p, _entry, _start) \
254 __bkey_extent_entry_for_each_from(_start, (_p).end, _entry)
256 #define bkey_extent_entry_for_each(_p, _entry) \
257 bkey_extent_entry_for_each_from(_p, _entry, _p.start)
259 #define __bkey_for_each_ptr(_start, _end, _ptr) \
260 for ((_ptr) = (_start); \
261 ((_ptr) = __bkey_ptr_next(_ptr, _end)); \
264 #define bkey_ptr_next(_p, _ptr) \
265 __bkey_ptr_next(_ptr, (_p).end)
267 #define bkey_for_each_ptr(_p, _ptr) \
268 __bkey_for_each_ptr(&(_p).start->ptr, (_p).end, _ptr)
270 #define __bkey_ptr_next_decode(_k, _end, _ptr, _entry) \
275 (_ptr).has_ec = false; \
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; \
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)); \
288 case BCH_EXTENT_ENTRY_stripe_ptr: \
289 (_ptr).ec = _entry->stripe_ptr; \
290 (_ptr).has_ec = true; \
297 #define __bkey_for_each_ptr_decode(_k, _start, _end, _ptr, _entry) \
298 for ((_ptr).crc = bch2_extent_crc_unpack(_k, NULL), \
300 __bkey_ptr_next_decode(_k, _end, _ptr, _entry); \
301 (_entry) = extent_entry_next(_entry))
303 #define bkey_for_each_ptr_decode(_k, _p, _ptr, _entry) \
304 __bkey_for_each_ptr_decode(_k, (_p).start, (_p).end, \
307 #define bkey_crc_next(_k, _start, _end, _crc, _iter) \
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)); \
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))
325 #define bkey_for_each_crc(_k, _p, _crc, _iter) \
326 __bkey_for_each_crc(_k, (_p).start, (_p).end, _crc, _iter)
328 /* Iterate over pointers in KEY_TYPE_extent: */
330 #define extent_for_each_entry_from(_e, _entry, _start) \
331 __bkey_extent_entry_for_each_from(_start, \
332 extent_entry_last(_e),_entry)
334 #define extent_for_each_entry(_e, _entry) \
335 extent_for_each_entry_from(_e, _entry, (_e).v->start)
337 #define extent_ptr_next(_e, _ptr) \
338 __bkey_ptr_next(_ptr, extent_entry_last(_e))
340 #define extent_for_each_ptr(_e, _ptr) \
341 __bkey_for_each_ptr(&(_e).v->start->ptr, extent_entry_last(_e), _ptr)
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)
347 /* utility code common to all keys with pointers: */
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 *);
355 /* KEY_TYPE_btree_ptr: */
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 *,
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, \
369 /* KEY_TYPE_extent: */
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);
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, \
386 /* KEY_TYPE_reservation: */
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);
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, \
399 /* Extent checksum entries: */
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);
407 /* Generic code for keys with pointers: */
409 static inline bool bkey_extent_is_direct_data(const struct bkey *k)
412 case KEY_TYPE_btree_ptr:
413 case KEY_TYPE_extent:
414 case KEY_TYPE_reflink_v:
421 static inline bool bkey_extent_is_data(const struct bkey *k)
423 return bkey_extent_is_direct_data(k) ||
424 k->type == KEY_TYPE_inline_data ||
425 k->type == KEY_TYPE_reflink_p;
429 * Should extent be counted under inode->i_sectors?
431 static inline bool bkey_extent_is_allocation(const struct bkey *k)
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:
445 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
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;
451 bkey_for_each_ptr(p, ptr)
452 ret.devs[ret.nr++] = ptr->dev;
457 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
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;
463 bkey_for_each_ptr(p, ptr)
465 ret.devs[ret.nr++] = ptr->dev;
470 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
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;
476 bkey_for_each_ptr(p, ptr)
478 ret.devs[ret.nr++] = ptr->dev;
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);
490 void bch2_bkey_mark_replicas_cached(struct bch_fs *, struct bkey_s,
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 *);
499 #define bch2_bkey_drop_ptrs(_k, _ptr, _cond) \
501 struct bkey_ptrs _ptrs = bch2_bkey_ptrs(_k); \
503 _ptr = &_ptrs.start->ptr; \
505 while ((_ptr = bkey_ptr_next(_ptrs, _ptr))) { \
507 _ptr = (void *) bch2_bkey_drop_ptr(_k, _ptr); \
508 _ptrs = bch2_bkey_ptrs(_k); \
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);
520 bool bch2_bkey_matches_ptr(struct bch_fs *, struct bkey_s_c,
521 struct bch_extent_ptr, u64);
523 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
524 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
526 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c);
528 void bch2_ptr_swab(const struct bkey_format *, struct bkey_packed *);
530 /* Generic extent code: */
532 int bch2_cut_front_s(struct bpos, struct bkey_s);
533 int bch2_cut_back_s(struct bpos, struct bkey_s);
535 static inline void bch2_cut_front(struct bpos where, struct bkey_i *k)
537 bch2_cut_front_s(where, bkey_i_to_s(k));
540 static inline void bch2_cut_back(struct bpos where, struct bkey_i *k)
542 bch2_cut_back_s(where, bkey_i_to_s(k));
546 * bch_key_resize - adjust size of @k
548 * bkey_start_offset(k) will be preserved, modifies where the extent ends
550 static inline void bch2_key_resize(struct bkey *k, unsigned new_size)
552 k->p.offset -= k->size;
553 k->p.offset += new_size;
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.
563 static inline void extent_save(struct btree *b, struct bkey_packed *dst,
566 struct bkey_format *f = &b->format;
567 struct bkey_i *dst_unpacked;
569 if ((dst_unpacked = packed_to_bkey(dst)))
570 dst_unpacked->k = *src;
572 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
575 #endif /* _BCACHEFS_EXTENTS_H */