1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_EXTENTS_H
3 #define _BCACHEFS_EXTENTS_H
7 #include "extents_types.h"
11 struct btree_insert_entry;
15 #define extent_entry_last(_e) \
16 ((typeof(&(_e).v->start[0])) bkey_val_end(_e))
18 #define entry_to_ptr(_entry) \
20 EBUG_ON((_entry) && !extent_entry_is_ptr(_entry)); \
22 __builtin_choose_expr( \
23 type_is_exact(_entry, const union bch_extent_entry *), \
24 (const struct bch_extent_ptr *) (_entry), \
25 (struct bch_extent_ptr *) (_entry)); \
28 /* downcast, preserves const */
29 #define to_entry(_entry) \
31 BUILD_BUG_ON(!type_is(_entry, union bch_extent_crc *) && \
32 !type_is(_entry, struct bch_extent_ptr *) && \
33 !type_is(_entry, struct bch_extent_stripe_ptr *)); \
35 __builtin_choose_expr( \
36 (type_is_exact(_entry, const union bch_extent_crc *) || \
37 type_is_exact(_entry, const struct bch_extent_ptr *) ||\
38 type_is_exact(_entry, const struct bch_extent_stripe_ptr *)),\
39 (const union bch_extent_entry *) (_entry), \
40 (union bch_extent_entry *) (_entry)); \
43 #define extent_entry_next(_entry) \
44 ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
46 static inline unsigned
47 __extent_entry_type(const union bch_extent_entry *e)
49 return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
52 static inline enum bch_extent_entry_type
53 extent_entry_type(const union bch_extent_entry *e)
55 int ret = __ffs(e->type);
57 EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
62 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
64 switch (extent_entry_type(entry)) {
66 case BCH_EXTENT_ENTRY_##f: \
67 return sizeof(struct bch_extent_##f);
68 BCH_EXTENT_ENTRY_TYPES()
75 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
77 return extent_entry_bytes(entry) / sizeof(u64);
80 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
82 switch (extent_entry_type(e)) {
83 case BCH_EXTENT_ENTRY_ptr:
90 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
92 switch (extent_entry_type(e)) {
93 case BCH_EXTENT_ENTRY_crc32:
94 case BCH_EXTENT_ENTRY_crc64:
95 case BCH_EXTENT_ENTRY_crc128:
102 union bch_extent_crc {
104 struct bch_extent_crc32 crc32;
105 struct bch_extent_crc64 crc64;
106 struct bch_extent_crc128 crc128;
109 #define __entry_to_crc(_entry) \
110 __builtin_choose_expr( \
111 type_is_exact(_entry, const union bch_extent_entry *), \
112 (const union bch_extent_crc *) (_entry), \
113 (union bch_extent_crc *) (_entry))
115 #define entry_to_crc(_entry) \
117 EBUG_ON((_entry) && !extent_entry_is_crc(_entry)); \
119 __entry_to_crc(_entry); \
122 static inline struct bch_extent_crc_unpacked
123 bch2_extent_crc_unpack(const struct bkey *k, const union bch_extent_crc *crc)
125 #define common_fields(_crc) \
126 .csum_type = _crc.csum_type, \
127 .compression_type = _crc.compression_type, \
128 .compressed_size = _crc._compressed_size + 1, \
129 .uncompressed_size = _crc._uncompressed_size + 1, \
130 .offset = _crc.offset, \
134 return (struct bch_extent_crc_unpacked) {
135 .compressed_size = k->size,
136 .uncompressed_size = k->size,
137 .live_size = k->size,
140 switch (extent_entry_type(to_entry(crc))) {
141 case BCH_EXTENT_ENTRY_crc32: {
142 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
143 common_fields(crc->crc32),
146 *((__le32 *) &ret.csum.lo) = crc->crc32.csum;
148 memcpy(&ret.csum.lo, &crc->crc32.csum,
149 sizeof(crc->crc32.csum));
153 case BCH_EXTENT_ENTRY_crc64: {
154 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
155 common_fields(crc->crc64),
156 .nonce = crc->crc64.nonce,
157 .csum.lo = (__force __le64) crc->crc64.csum_lo,
160 *((__le16 *) &ret.csum.hi) = crc->crc64.csum_hi;
164 case BCH_EXTENT_ENTRY_crc128: {
165 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
166 common_fields(crc->crc128),
167 .nonce = crc->crc128.nonce,
168 .csum = crc->crc128.csum,
179 /* bkey_ptrs: generically over any key type that has ptrs */
182 const union bch_extent_entry *start;
183 const union bch_extent_entry *end;
187 union bch_extent_entry *start;
188 union bch_extent_entry *end;
191 static inline struct bkey_ptrs_c bch2_bkey_ptrs_c(struct bkey_s_c k)
194 case KEY_TYPE_btree_ptr: {
195 struct bkey_s_c_btree_ptr e = bkey_s_c_to_btree_ptr(k);
196 return (struct bkey_ptrs_c) {
197 to_entry(&e.v->start[0]),
198 to_entry(extent_entry_last(e))
201 case KEY_TYPE_extent: {
202 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
203 return (struct bkey_ptrs_c) {
208 case KEY_TYPE_stripe: {
209 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
210 return (struct bkey_ptrs_c) {
211 to_entry(&s.v->ptrs[0]),
212 to_entry(&s.v->ptrs[s.v->nr_blocks]),
215 case KEY_TYPE_reflink_v: {
216 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
218 return (struct bkey_ptrs_c) {
224 return (struct bkey_ptrs_c) { NULL, NULL };
228 static inline struct bkey_ptrs bch2_bkey_ptrs(struct bkey_s k)
230 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k.s_c);
232 return (struct bkey_ptrs) {
238 #define __bkey_extent_entry_for_each_from(_start, _end, _entry) \
239 for ((_entry) = (_start); \
241 (_entry) = extent_entry_next(_entry))
243 #define __bkey_ptr_next(_ptr, _end) \
245 typeof(_end) _entry; \
247 __bkey_extent_entry_for_each_from(to_entry(_ptr), _end, _entry) \
248 if (extent_entry_is_ptr(_entry)) \
251 _entry < (_end) ? entry_to_ptr(_entry) : NULL; \
254 #define bkey_extent_entry_for_each_from(_p, _entry, _start) \
255 __bkey_extent_entry_for_each_from(_start, (_p).end, _entry)
257 #define bkey_extent_entry_for_each(_p, _entry) \
258 bkey_extent_entry_for_each_from(_p, _entry, _p.start)
260 #define __bkey_for_each_ptr(_start, _end, _ptr) \
261 for ((_ptr) = (_start); \
262 ((_ptr) = __bkey_ptr_next(_ptr, _end)); \
265 #define bkey_ptr_next(_p, _ptr) \
266 __bkey_ptr_next(_ptr, (_p).end)
268 #define bkey_for_each_ptr(_p, _ptr) \
269 __bkey_for_each_ptr(&(_p).start->ptr, (_p).end, _ptr)
271 #define __bkey_ptr_next_decode(_k, _end, _ptr, _entry) \
276 (_ptr).has_ec = false; \
278 __bkey_extent_entry_for_each_from(_entry, _end, _entry) \
279 switch (extent_entry_type(_entry)) { \
280 case BCH_EXTENT_ENTRY_ptr: \
281 (_ptr).ptr = _entry->ptr; \
283 case BCH_EXTENT_ENTRY_crc32: \
284 case BCH_EXTENT_ENTRY_crc64: \
285 case BCH_EXTENT_ENTRY_crc128: \
286 (_ptr).crc = bch2_extent_crc_unpack(_k, \
287 entry_to_crc(_entry)); \
289 case BCH_EXTENT_ENTRY_stripe_ptr: \
290 (_ptr).ec = _entry->stripe_ptr; \
291 (_ptr).has_ec = true; \
298 #define __bkey_for_each_ptr_decode(_k, _start, _end, _ptr, _entry) \
299 for ((_ptr).crc = bch2_extent_crc_unpack(_k, NULL), \
301 __bkey_ptr_next_decode(_k, _end, _ptr, _entry); \
302 (_entry) = extent_entry_next(_entry))
304 #define bkey_for_each_ptr_decode(_k, _p, _ptr, _entry) \
305 __bkey_for_each_ptr_decode(_k, (_p).start, (_p).end, \
308 #define bkey_crc_next(_k, _start, _end, _crc, _iter) \
310 __bkey_extent_entry_for_each_from(_iter, _end, _iter) \
311 if (extent_entry_is_crc(_iter)) { \
312 (_crc) = bch2_extent_crc_unpack(_k, \
313 entry_to_crc(_iter)); \
320 #define __bkey_for_each_crc(_k, _start, _end, _crc, _iter) \
321 for ((_crc) = bch2_extent_crc_unpack(_k, NULL), \
322 (_iter) = (_start); \
323 bkey_crc_next(_k, _start, _end, _crc, _iter); \
324 (_iter) = extent_entry_next(_iter))
326 #define bkey_for_each_crc(_k, _p, _crc, _iter) \
327 __bkey_for_each_crc(_k, (_p).start, (_p).end, _crc, _iter)
329 /* Iterate over pointers in KEY_TYPE_extent: */
331 #define extent_for_each_entry_from(_e, _entry, _start) \
332 __bkey_extent_entry_for_each_from(_start, \
333 extent_entry_last(_e),_entry)
335 #define extent_for_each_entry(_e, _entry) \
336 extent_for_each_entry_from(_e, _entry, (_e).v->start)
338 #define extent_ptr_next(_e, _ptr) \
339 __bkey_ptr_next(_ptr, extent_entry_last(_e))
341 #define extent_for_each_ptr(_e, _ptr) \
342 __bkey_for_each_ptr(&(_e).v->start->ptr, extent_entry_last(_e), _ptr)
344 #define extent_for_each_ptr_decode(_e, _ptr, _entry) \
345 __bkey_for_each_ptr_decode((_e).k, (_e).v->start, \
346 extent_entry_last(_e), _ptr, _entry)
348 /* utility code common to all keys with pointers: */
350 void bch2_mark_io_failure(struct bch_io_failures *,
351 struct extent_ptr_decoded *);
352 int bch2_bkey_pick_read_device(struct bch_fs *, struct bkey_s_c,
353 struct bch_io_failures *,
354 struct extent_ptr_decoded *);
356 /* KEY_TYPE_btree_ptr: */
358 const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
359 void bch2_btree_ptr_debugcheck(struct bch_fs *, struct bkey_s_c);
360 void bch2_btree_ptr_to_text(struct printbuf *, struct bch_fs *,
363 #define bch2_bkey_ops_btree_ptr (struct bkey_ops) { \
364 .key_invalid = bch2_btree_ptr_invalid, \
365 .key_debugcheck = bch2_btree_ptr_debugcheck, \
366 .val_to_text = bch2_btree_ptr_to_text, \
367 .swab = bch2_ptr_swab, \
370 /* KEY_TYPE_extent: */
372 const char *bch2_extent_invalid(const struct bch_fs *, struct bkey_s_c);
373 void bch2_extent_debugcheck(struct bch_fs *, struct bkey_s_c);
374 void bch2_extent_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
375 enum merge_result bch2_extent_merge(struct bch_fs *,
376 struct bkey_s, struct bkey_s);
378 #define bch2_bkey_ops_extent (struct bkey_ops) { \
379 .key_invalid = bch2_extent_invalid, \
380 .key_debugcheck = bch2_extent_debugcheck, \
381 .val_to_text = bch2_extent_to_text, \
382 .swab = bch2_ptr_swab, \
383 .key_normalize = bch2_extent_normalize, \
384 .key_merge = bch2_extent_merge, \
387 /* KEY_TYPE_reservation: */
389 const char *bch2_reservation_invalid(const struct bch_fs *, struct bkey_s_c);
390 void bch2_reservation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
391 enum merge_result bch2_reservation_merge(struct bch_fs *,
392 struct bkey_s, struct bkey_s);
394 #define bch2_bkey_ops_reservation (struct bkey_ops) { \
395 .key_invalid = bch2_reservation_invalid, \
396 .val_to_text = bch2_reservation_to_text, \
397 .key_merge = bch2_reservation_merge, \
400 /* Extent checksum entries: */
402 bool bch2_can_narrow_extent_crcs(struct bkey_s_c,
403 struct bch_extent_crc_unpacked);
404 bool bch2_bkey_narrow_crcs(struct bkey_i *, struct bch_extent_crc_unpacked);
405 void bch2_extent_crc_append(struct bkey_i *,
406 struct bch_extent_crc_unpacked);
408 /* Generic code for keys with pointers: */
410 static inline bool bkey_extent_is_direct_data(const struct bkey *k)
413 case KEY_TYPE_btree_ptr:
414 case KEY_TYPE_extent:
415 case KEY_TYPE_reflink_v:
422 static inline bool bkey_extent_is_data(const struct bkey *k)
424 return bkey_extent_is_direct_data(k) ||
425 k->type == KEY_TYPE_inline_data ||
426 k->type == KEY_TYPE_reflink_p;
430 * Should extent be counted under inode->i_sectors?
432 static inline bool bkey_extent_is_allocation(const struct bkey *k)
435 case KEY_TYPE_extent:
436 case KEY_TYPE_reservation:
437 case KEY_TYPE_reflink_p:
438 case KEY_TYPE_reflink_v:
439 case KEY_TYPE_inline_data:
446 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
448 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
449 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
450 const struct bch_extent_ptr *ptr;
452 bkey_for_each_ptr(p, ptr)
453 ret.devs[ret.nr++] = ptr->dev;
458 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
460 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
461 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
462 const struct bch_extent_ptr *ptr;
464 bkey_for_each_ptr(p, ptr)
466 ret.devs[ret.nr++] = ptr->dev;
471 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
473 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
474 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
475 const struct bch_extent_ptr *ptr;
477 bkey_for_each_ptr(p, ptr)
479 ret.devs[ret.nr++] = ptr->dev;
484 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c);
485 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c);
486 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c);
487 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c);
488 bool bch2_check_range_allocated(struct bch_fs *, struct bpos, u64, unsigned);
489 unsigned bch2_bkey_durability(struct bch_fs *, struct bkey_s_c);
491 void bch2_bkey_mark_replicas_cached(struct bch_fs *, struct bkey_s,
494 void bch2_bkey_append_ptr(struct bkey_i *, struct bch_extent_ptr);
495 void bch2_extent_ptr_decoded_append(struct bkey_i *,
496 struct extent_ptr_decoded *);
497 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s,
498 struct bch_extent_ptr *);
500 #define bch2_bkey_drop_ptrs(_k, _ptr, _cond) \
502 struct bkey_ptrs _ptrs = bch2_bkey_ptrs(_k); \
504 _ptr = &_ptrs.start->ptr; \
506 while ((_ptr = bkey_ptr_next(_ptrs, _ptr))) { \
508 _ptr = (void *) bch2_bkey_drop_ptr(_k, _ptr); \
509 _ptrs = bch2_bkey_ptrs(_k); \
517 void bch2_bkey_drop_device(struct bkey_s, unsigned);
518 const struct bch_extent_ptr *bch2_bkey_has_device(struct bkey_s_c, unsigned);
519 bool bch2_bkey_has_target(struct bch_fs *, struct bkey_s_c, unsigned);
521 bool bch2_bkey_matches_ptr(struct bch_fs *, struct bkey_s_c,
522 struct bch_extent_ptr, u64);
524 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
525 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
527 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c);
529 void bch2_ptr_swab(const struct bkey_format *, struct bkey_packed *);
531 /* Generic extent code: */
533 int bch2_cut_front_s(struct bpos, struct bkey_s);
534 int bch2_cut_back_s(struct bpos, struct bkey_s);
536 static inline void bch2_cut_front(struct bpos where, struct bkey_i *k)
538 bch2_cut_front_s(where, bkey_i_to_s(k));
541 static inline void bch2_cut_back(struct bpos where, struct bkey_i *k)
543 bch2_cut_back_s(where, bkey_i_to_s(k));
547 * bch_key_resize - adjust size of @k
549 * bkey_start_offset(k) will be preserved, modifies where the extent ends
551 static inline void bch2_key_resize(struct bkey *k, unsigned new_size)
553 k->p.offset -= k->size;
554 k->p.offset += new_size;
559 * In extent_sort_fix_overlapping(), insert_fixup_extent(),
560 * extent_merge_inline() - we're modifying keys in place that are packed. To do
561 * that we have to unpack the key, modify the unpacked key - then this
562 * copies/repacks the unpacked to the original as necessary.
564 static inline void extent_save(struct btree *b, struct bkey_packed *dst,
567 struct bkey_format *f = &b->format;
568 struct bkey_i *dst_unpacked;
570 if ((dst_unpacked = packed_to_bkey(dst)))
571 dst_unpacked->k = *src;
573 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
576 #endif /* _BCACHEFS_EXTENTS_H */