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 static inline unsigned
44 __extent_entry_type(const union bch_extent_entry *e)
46 return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
49 static inline enum bch_extent_entry_type
50 extent_entry_type(const union bch_extent_entry *e)
52 int ret = __ffs(e->type);
54 EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
59 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
61 switch (extent_entry_type(entry)) {
63 case BCH_EXTENT_ENTRY_##f: \
64 return sizeof(struct bch_extent_##f);
65 BCH_EXTENT_ENTRY_TYPES()
72 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
74 return extent_entry_bytes(entry) / sizeof(u64);
77 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
79 switch (extent_entry_type(e)) {
80 case BCH_EXTENT_ENTRY_ptr:
87 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
89 switch (extent_entry_type(e)) {
90 case BCH_EXTENT_ENTRY_crc32:
91 case BCH_EXTENT_ENTRY_crc64:
92 case BCH_EXTENT_ENTRY_crc128:
99 union bch_extent_crc {
101 struct bch_extent_crc32 crc32;
102 struct bch_extent_crc64 crc64;
103 struct bch_extent_crc128 crc128;
106 #define __entry_to_crc(_entry) \
107 __builtin_choose_expr( \
108 type_is_exact(_entry, const union bch_extent_entry *), \
109 (const union bch_extent_crc *) (_entry), \
110 (union bch_extent_crc *) (_entry))
112 #define entry_to_crc(_entry) \
114 EBUG_ON((_entry) && !extent_entry_is_crc(_entry)); \
116 __entry_to_crc(_entry); \
119 static inline struct bch_extent_crc_unpacked
120 bch2_extent_crc_unpack(const struct bkey *k, const union bch_extent_crc *crc)
122 #define common_fields(_crc) \
123 .csum_type = _crc.csum_type, \
124 .compression_type = _crc.compression_type, \
125 .compressed_size = _crc._compressed_size + 1, \
126 .uncompressed_size = _crc._uncompressed_size + 1, \
127 .offset = _crc.offset, \
131 return (struct bch_extent_crc_unpacked) {
132 .compressed_size = k->size,
133 .uncompressed_size = k->size,
134 .live_size = k->size,
137 switch (extent_entry_type(to_entry(crc))) {
138 case BCH_EXTENT_ENTRY_crc32: {
139 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
140 common_fields(crc->crc32),
143 *((__le32 *) &ret.csum.lo) = crc->crc32.csum;
145 memcpy(&ret.csum.lo, &crc->crc32.csum,
146 sizeof(crc->crc32.csum));
150 case BCH_EXTENT_ENTRY_crc64: {
151 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
152 common_fields(crc->crc64),
153 .nonce = crc->crc64.nonce,
154 .csum.lo = (__force __le64) crc->crc64.csum_lo,
157 *((__le16 *) &ret.csum.hi) = crc->crc64.csum_hi;
161 case BCH_EXTENT_ENTRY_crc128: {
162 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
163 common_fields(crc->crc128),
164 .nonce = crc->crc128.nonce,
165 .csum = crc->crc128.csum,
176 /* bkey_ptrs: generically over any key type that has ptrs */
179 const union bch_extent_entry *start;
180 const union bch_extent_entry *end;
184 union bch_extent_entry *start;
185 union bch_extent_entry *end;
188 /* iterate over bkey ptrs */
190 #define extent_entry_next(_entry) \
191 ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
193 #define __bkey_extent_entry_for_each_from(_start, _end, _entry) \
194 for ((_entry) = (_start); \
196 (_entry) = extent_entry_next(_entry))
198 #define __bkey_ptr_next(_ptr, _end) \
200 typeof(_end) _entry; \
202 __bkey_extent_entry_for_each_from(to_entry(_ptr), _end, _entry) \
203 if (extent_entry_is_ptr(_entry)) \
206 _entry < (_end) ? entry_to_ptr(_entry) : NULL; \
209 #define bkey_extent_entry_for_each_from(_p, _entry, _start) \
210 __bkey_extent_entry_for_each_from(_start, (_p).end, _entry)
212 #define bkey_extent_entry_for_each(_p, _entry) \
213 bkey_extent_entry_for_each_from(_p, _entry, _p.start)
215 #define __bkey_for_each_ptr(_start, _end, _ptr) \
216 for ((_ptr) = (_start); \
217 ((_ptr) = __bkey_ptr_next(_ptr, _end)); \
220 #define bkey_ptr_next(_p, _ptr) \
221 __bkey_ptr_next(_ptr, (_p).end)
223 #define bkey_for_each_ptr(_p, _ptr) \
224 __bkey_for_each_ptr(&(_p).start->ptr, (_p).end, _ptr)
226 #define __bkey_ptr_next_decode(_k, _end, _ptr, _entry) \
233 __bkey_extent_entry_for_each_from(_entry, _end, _entry) \
234 switch (extent_entry_type(_entry)) { \
235 case BCH_EXTENT_ENTRY_ptr: \
236 (_ptr).ptr = _entry->ptr; \
238 case BCH_EXTENT_ENTRY_crc32: \
239 case BCH_EXTENT_ENTRY_crc64: \
240 case BCH_EXTENT_ENTRY_crc128: \
241 (_ptr).crc = bch2_extent_crc_unpack(_k, \
242 entry_to_crc(_entry)); \
244 case BCH_EXTENT_ENTRY_stripe_ptr: \
245 (_ptr).ec[(_ptr).ec_nr++] = _entry->stripe_ptr; \
252 #define __bkey_for_each_ptr_decode(_k, _start, _end, _ptr, _entry) \
253 for ((_ptr).crc = bch2_extent_crc_unpack(_k, NULL), \
255 __bkey_ptr_next_decode(_k, _end, _ptr, _entry); \
256 (_entry) = extent_entry_next(_entry))
258 #define bkey_for_each_ptr_decode(_k, _p, _ptr, _entry) \
259 __bkey_for_each_ptr_decode(_k, (_p).start, (_p).end, \
262 #define bkey_crc_next(_k, _start, _end, _crc, _iter) \
264 __bkey_extent_entry_for_each_from(_iter, _end, _iter) \
265 if (extent_entry_is_crc(_iter)) { \
266 (_crc) = bch2_extent_crc_unpack(_k, \
267 entry_to_crc(_iter)); \
274 #define __bkey_for_each_crc(_k, _start, _end, _crc, _iter) \
275 for ((_crc) = bch2_extent_crc_unpack(_k, NULL), \
276 (_iter) = (_start); \
277 bkey_crc_next(_k, _start, _end, _crc, _iter); \
278 (_iter) = extent_entry_next(_iter))
280 #define bkey_for_each_crc(_k, _p, _crc, _iter) \
281 __bkey_for_each_crc(_k, (_p).start, (_p).end, _crc, _iter)
283 /* utility code common to all keys with pointers: */
285 static inline struct bkey_ptrs_c bch2_bkey_ptrs_c(struct bkey_s_c k)
288 case KEY_TYPE_btree_ptr: {
289 struct bkey_s_c_btree_ptr e = bkey_s_c_to_btree_ptr(k);
290 return (struct bkey_ptrs_c) {
291 to_entry(&e.v->start[0]),
292 to_entry(extent_entry_last(e))
295 case KEY_TYPE_extent: {
296 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
297 return (struct bkey_ptrs_c) {
302 case KEY_TYPE_stripe: {
303 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
304 return (struct bkey_ptrs_c) {
305 to_entry(&s.v->ptrs[0]),
306 to_entry(&s.v->ptrs[s.v->nr_blocks]),
309 case KEY_TYPE_reflink_v: {
310 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
312 return (struct bkey_ptrs_c) {
318 return (struct bkey_ptrs_c) { NULL, NULL };
322 static inline struct bkey_ptrs bch2_bkey_ptrs(struct bkey_s k)
324 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k.s_c);
326 return (struct bkey_ptrs) {
332 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
334 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
335 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
336 const struct bch_extent_ptr *ptr;
338 bkey_for_each_ptr(p, ptr)
339 ret.devs[ret.nr++] = ptr->dev;
344 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
346 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
347 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
348 const struct bch_extent_ptr *ptr;
350 bkey_for_each_ptr(p, ptr)
352 ret.devs[ret.nr++] = ptr->dev;
357 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
359 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
360 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
361 const struct bch_extent_ptr *ptr;
363 bkey_for_each_ptr(p, ptr)
365 ret.devs[ret.nr++] = ptr->dev;
370 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c);
371 unsigned bch2_bkey_nr_dirty_ptrs(struct bkey_s_c);
372 unsigned bch2_bkey_durability(struct bch_fs *, struct bkey_s_c);
374 void bch2_mark_io_failure(struct bch_io_failures *,
375 struct extent_ptr_decoded *);
376 int bch2_bkey_pick_read_device(struct bch_fs *, struct bkey_s_c,
377 struct bch_io_failures *,
378 struct extent_ptr_decoded *);
380 void bch2_bkey_append_ptr(struct bkey_i *, struct bch_extent_ptr);
381 void bch2_bkey_drop_device(struct bkey_s, unsigned);
382 const struct bch_extent_ptr *bch2_bkey_has_device(struct bkey_s_c, unsigned);
383 bool bch2_bkey_has_target(struct bch_fs *, struct bkey_s_c, unsigned);
385 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
387 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c);
391 const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
392 void bch2_btree_ptr_debugcheck(struct bch_fs *, struct btree *,
394 void bch2_btree_ptr_to_text(struct printbuf *, struct bch_fs *,
396 void bch2_ptr_swab(const struct bkey_format *, struct bkey_packed *);
398 #define bch2_bkey_ops_btree_ptr (struct bkey_ops) { \
399 .key_invalid = bch2_btree_ptr_invalid, \
400 .key_debugcheck = bch2_btree_ptr_debugcheck, \
401 .val_to_text = bch2_btree_ptr_to_text, \
402 .swab = bch2_ptr_swab, \
407 const char *bch2_extent_invalid(const struct bch_fs *, struct bkey_s_c);
408 void bch2_extent_debugcheck(struct bch_fs *, struct btree *, struct bkey_s_c);
409 void bch2_extent_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
410 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
411 enum merge_result bch2_extent_merge(struct bch_fs *,
412 struct bkey_s, struct bkey_s);
414 #define bch2_bkey_ops_extent (struct bkey_ops) { \
415 .key_invalid = bch2_extent_invalid, \
416 .key_debugcheck = bch2_extent_debugcheck, \
417 .val_to_text = bch2_extent_to_text, \
418 .swab = bch2_ptr_swab, \
419 .key_normalize = bch2_extent_normalize, \
420 .key_merge = bch2_extent_merge, \
423 /* bch_reservation: */
425 const char *bch2_reservation_invalid(const struct bch_fs *, struct bkey_s_c);
426 void bch2_reservation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
427 enum merge_result bch2_reservation_merge(struct bch_fs *,
428 struct bkey_s, struct bkey_s);
430 #define bch2_bkey_ops_reservation (struct bkey_ops) { \
431 .key_invalid = bch2_reservation_invalid, \
432 .val_to_text = bch2_reservation_to_text, \
433 .key_merge = bch2_reservation_merge, \
436 int bch2_extent_atomic_end(struct btree_trans *, struct btree_iter *,
437 struct bkey_i *, struct bpos *);
438 int bch2_extent_trim_atomic(struct bkey_i *, struct btree_iter *);
439 int bch2_extent_is_atomic(struct bkey_i *, struct btree_iter *);
441 enum btree_insert_ret
442 bch2_extent_can_insert(struct btree_trans *, struct btree_insert_entry *,
444 void bch2_insert_fixup_extent(struct btree_trans *,
445 struct btree_insert_entry *);
447 void bch2_bkey_mark_replicas_cached(struct bch_fs *, struct bkey_s,
450 const struct bch_extent_ptr *
451 bch2_extent_has_device(struct bkey_s_c_extent, unsigned);
453 unsigned bch2_extent_is_compressed(struct bkey_s_c);
455 bool bch2_bkey_matches_ptr(struct bch_fs *, struct bkey_s_c,
456 struct bch_extent_ptr, u64);
458 static inline bool bkey_extent_is_data(const struct bkey *k)
461 case KEY_TYPE_btree_ptr:
462 case KEY_TYPE_extent:
463 case KEY_TYPE_reflink_p:
464 case KEY_TYPE_reflink_v:
472 * Should extent be counted under inode->i_sectors?
474 static inline bool bkey_extent_is_allocation(const struct bkey *k)
477 case KEY_TYPE_extent:
478 case KEY_TYPE_reservation:
479 case KEY_TYPE_reflink_p:
480 case KEY_TYPE_reflink_v:
487 /* Extent entry iteration: */
489 #define extent_for_each_entry_from(_e, _entry, _start) \
490 __bkey_extent_entry_for_each_from(_start, \
491 extent_entry_last(_e),_entry)
493 #define extent_for_each_entry(_e, _entry) \
494 extent_for_each_entry_from(_e, _entry, (_e).v->start)
496 #define extent_ptr_next(_e, _ptr) \
497 __bkey_ptr_next(_ptr, extent_entry_last(_e))
499 #define extent_for_each_ptr(_e, _ptr) \
500 __bkey_for_each_ptr(&(_e).v->start->ptr, extent_entry_last(_e), _ptr)
502 #define extent_for_each_ptr_decode(_e, _ptr, _entry) \
503 __bkey_for_each_ptr_decode((_e).k, (_e).v->start, \
504 extent_entry_last(_e), _ptr, _entry)
506 void bch2_extent_crc_append(struct bkey_i *,
507 struct bch_extent_crc_unpacked);
508 void bch2_extent_ptr_decoded_append(struct bkey_i *,
509 struct extent_ptr_decoded *);
511 bool bch2_can_narrow_extent_crcs(struct bkey_s_c,
512 struct bch_extent_crc_unpacked);
513 bool bch2_bkey_narrow_crcs(struct bkey_i *, struct bch_extent_crc_unpacked);
515 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s,
516 struct bch_extent_ptr *);
518 #define bch2_bkey_drop_ptrs(_k, _ptr, _cond) \
520 struct bkey_ptrs _ptrs = bch2_bkey_ptrs(_k); \
522 _ptr = &_ptrs.start->ptr; \
524 while ((_ptr = bkey_ptr_next(_ptrs, _ptr))) { \
526 _ptr = (void *) bch2_bkey_drop_ptr(_k, _ptr); \
527 _ptrs = bch2_bkey_ptrs(_k); \
535 void __bch2_cut_front(struct bpos, struct bkey_s);
537 static inline void bch2_cut_front(struct bpos where, struct bkey_i *k)
539 __bch2_cut_front(where, bkey_i_to_s(k));
542 bool bch2_cut_back(struct bpos, struct bkey *);
543 void bch2_key_resize(struct bkey *, unsigned);
546 * In extent_sort_fix_overlapping(), insert_fixup_extent(),
547 * extent_merge_inline() - we're modifying keys in place that are packed. To do
548 * that we have to unpack the key, modify the unpacked key - then this
549 * copies/repacks the unpacked to the original as necessary.
551 static inline void extent_save(struct btree *b, struct bkey_packed *dst,
554 struct bkey_format *f = &b->format;
555 struct bkey_i *dst_unpacked;
557 if ((dst_unpacked = packed_to_bkey(dst)))
558 dst_unpacked->k = *src;
560 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
563 bool bch2_check_range_allocated(struct bch_fs *, struct bpos, u64, unsigned);
564 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c);
566 #endif /* _BCACHEFS_EXTENTS_H */