1 #ifndef _BCACHEFS_EXTENTS_H
2 #define _BCACHEFS_EXTENTS_H
6 #include "extents_types.h"
10 struct btree_node_iter;
11 struct btree_node_iter_large;
13 struct btree_insert_entry;
14 struct extent_insert_hook;
18 const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
19 void bch2_btree_ptr_debugcheck(struct bch_fs *, struct btree *,
21 void bch2_btree_ptr_to_text(struct bch_fs *, char *, size_t, struct bkey_s_c);
22 void bch2_ptr_swab(const struct bkey_format *, struct bkey_packed *);
24 #define bch2_bkey_btree_ops (struct bkey_ops) { \
25 .key_invalid = bch2_btree_ptr_invalid, \
26 .key_debugcheck = bch2_btree_ptr_debugcheck, \
27 .val_to_text = bch2_btree_ptr_to_text, \
28 .swab = bch2_ptr_swab, \
31 const char *bch2_extent_invalid(const struct bch_fs *, struct bkey_s_c);
32 void bch2_extent_debugcheck(struct bch_fs *, struct btree *, struct bkey_s_c);
33 void bch2_extent_to_text(struct bch_fs *, char *, size_t, struct bkey_s_c);
34 bool bch2_ptr_normalize(struct bch_fs *, struct btree *, struct bkey_s);
35 enum merge_result bch2_extent_merge(struct bch_fs *, struct btree *,
36 struct bkey_i *, struct bkey_i *);
38 #define bch2_bkey_extent_ops (struct bkey_ops) { \
39 .key_invalid = bch2_extent_invalid, \
40 .key_debugcheck = bch2_extent_debugcheck, \
41 .val_to_text = bch2_extent_to_text, \
42 .swab = bch2_ptr_swab, \
43 .key_normalize = bch2_ptr_normalize, \
44 .key_merge = bch2_extent_merge, \
48 struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *,
50 struct btree_node_iter_large *);
51 struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
54 struct btree_node_iter_large *);
56 int bch2_btree_pick_ptr(struct bch_fs *, const struct btree *,
57 struct bch_devs_mask *avoid,
58 struct extent_pick_ptr *);
60 int bch2_extent_pick_ptr(struct bch_fs *, struct bkey_s_c,
61 struct bch_devs_mask *,
62 struct extent_pick_ptr *);
65 bch2_insert_fixup_extent(struct btree_insert *,
66 struct btree_insert_entry *);
68 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
69 void bch2_extent_mark_replicas_cached(struct bch_fs *, struct bkey_s_extent,
72 const struct bch_extent_ptr *
73 bch2_extent_has_device(struct bkey_s_c_extent, unsigned);
74 bool bch2_extent_drop_device(struct bkey_s_extent, unsigned);
75 const struct bch_extent_ptr *
76 bch2_extent_has_group(struct bch_fs *, struct bkey_s_c_extent, unsigned);
77 const struct bch_extent_ptr *
78 bch2_extent_has_target(struct bch_fs *, struct bkey_s_c_extent, unsigned);
80 unsigned bch2_extent_nr_ptrs(struct bkey_s_c_extent);
81 unsigned bch2_extent_nr_dirty_ptrs(struct bkey_s_c);
82 unsigned bch2_extent_is_compressed(struct bkey_s_c);
84 unsigned bch2_extent_ptr_durability(struct bch_fs *,
85 const struct bch_extent_ptr *);
86 unsigned bch2_extent_durability(struct bch_fs *, struct bkey_s_c_extent);
88 bool bch2_extent_matches_ptr(struct bch_fs *, struct bkey_s_c_extent,
89 struct bch_extent_ptr, u64);
91 static inline bool bkey_extent_is_data(const struct bkey *k)
95 case BCH_EXTENT_CACHED:
102 static inline bool bkey_extent_is_allocation(const struct bkey *k)
106 case BCH_EXTENT_CACHED:
107 case BCH_RESERVATION:
114 static inline bool bch2_extent_is_fully_allocated(struct bkey_s_c k)
116 return bkey_extent_is_allocation(k.k) &&
117 !bch2_extent_is_compressed(k);
120 static inline bool bkey_extent_is_cached(const struct bkey *k)
122 return k->type == BCH_EXTENT_CACHED;
125 static inline void bkey_extent_set_cached(struct bkey *k, bool cached)
127 EBUG_ON(k->type != BCH_EXTENT &&
128 k->type != BCH_EXTENT_CACHED);
130 k->type = cached ? BCH_EXTENT_CACHED : BCH_EXTENT;
133 static inline unsigned
134 __extent_entry_type(const union bch_extent_entry *e)
136 return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
139 static inline enum bch_extent_entry_type
140 extent_entry_type(const union bch_extent_entry *e)
142 int ret = __ffs(e->type);
144 EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
149 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
151 switch (extent_entry_type(entry)) {
152 case BCH_EXTENT_ENTRY_crc32:
153 return sizeof(struct bch_extent_crc32);
154 case BCH_EXTENT_ENTRY_crc64:
155 return sizeof(struct bch_extent_crc64);
156 case BCH_EXTENT_ENTRY_crc128:
157 return sizeof(struct bch_extent_crc128);
158 case BCH_EXTENT_ENTRY_ptr:
159 return sizeof(struct bch_extent_ptr);
165 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
167 return extent_entry_bytes(entry) / sizeof(u64);
170 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
172 return extent_entry_type(e) == BCH_EXTENT_ENTRY_ptr;
175 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
177 return !extent_entry_is_ptr(e);
180 union bch_extent_crc {
182 struct bch_extent_crc32 crc32;
183 struct bch_extent_crc64 crc64;
184 struct bch_extent_crc128 crc128;
187 /* downcast, preserves const */
188 #define to_entry(_entry) \
190 BUILD_BUG_ON(!type_is(_entry, union bch_extent_crc *) && \
191 !type_is(_entry, struct bch_extent_ptr *)); \
193 __builtin_choose_expr( \
194 (type_is_exact(_entry, const union bch_extent_crc *) || \
195 type_is_exact(_entry, const struct bch_extent_ptr *)), \
196 (const union bch_extent_entry *) (_entry), \
197 (union bch_extent_entry *) (_entry)); \
200 #define __entry_to_crc(_entry) \
201 __builtin_choose_expr( \
202 type_is_exact(_entry, const union bch_extent_entry *), \
203 (const union bch_extent_crc *) (_entry), \
204 (union bch_extent_crc *) (_entry))
206 #define entry_to_crc(_entry) \
208 EBUG_ON((_entry) && !extent_entry_is_crc(_entry)); \
210 __entry_to_crc(_entry); \
213 #define entry_to_ptr(_entry) \
215 EBUG_ON((_entry) && !extent_entry_is_ptr(_entry)); \
217 __builtin_choose_expr( \
218 type_is_exact(_entry, const union bch_extent_entry *), \
219 (const struct bch_extent_ptr *) (_entry), \
220 (struct bch_extent_ptr *) (_entry)); \
223 /* checksum entries: */
225 enum bch_extent_crc_type {
232 static inline enum bch_extent_crc_type
233 __extent_crc_type(const union bch_extent_crc *crc)
236 return BCH_EXTENT_CRC_NONE;
238 switch (extent_entry_type(to_entry(crc))) {
239 case BCH_EXTENT_ENTRY_crc32:
240 return BCH_EXTENT_CRC32;
241 case BCH_EXTENT_ENTRY_crc64:
242 return BCH_EXTENT_CRC64;
243 case BCH_EXTENT_ENTRY_crc128:
244 return BCH_EXTENT_CRC128;
250 #define extent_crc_type(_crc) \
252 BUILD_BUG_ON(!type_is(_crc, struct bch_extent_crc32 *) && \
253 !type_is(_crc, struct bch_extent_crc64 *) && \
254 !type_is(_crc, struct bch_extent_crc128 *) && \
255 !type_is(_crc, union bch_extent_crc *)); \
257 type_is(_crc, struct bch_extent_crc32 *) ? BCH_EXTENT_CRC32 \
258 : type_is(_crc, struct bch_extent_crc64 *) ? BCH_EXTENT_CRC64 \
259 : type_is(_crc, struct bch_extent_crc128 *) ? BCH_EXTENT_CRC128 \
260 : __extent_crc_type((union bch_extent_crc *) _crc); \
263 static inline struct bch_extent_crc_unpacked
264 bch2_extent_crc_unpack(const struct bkey *k, const union bch_extent_crc *crc)
266 #define common_fields(_crc) \
267 .csum_type = _crc.csum_type, \
268 .compression_type = _crc.compression_type, \
269 .compressed_size = _crc._compressed_size + 1, \
270 .uncompressed_size = _crc._uncompressed_size + 1, \
271 .offset = _crc.offset, \
274 switch (extent_crc_type(crc)) {
275 case BCH_EXTENT_CRC_NONE:
276 return (struct bch_extent_crc_unpacked) {
277 .compressed_size = k->size,
278 .uncompressed_size = k->size,
279 .live_size = k->size,
281 case BCH_EXTENT_CRC32: {
282 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
283 common_fields(crc->crc32),
286 *((__le32 *) &ret.csum.lo) = crc->crc32.csum;
288 memcpy(&ret.csum.lo, &crc->crc32.csum,
289 sizeof(crc->crc32.csum));
293 case BCH_EXTENT_CRC64: {
294 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
295 common_fields(crc->crc64),
296 .nonce = crc->crc64.nonce,
297 .csum.lo = (__force __le64) crc->crc64.csum_lo,
300 *((__le16 *) &ret.csum.hi) = crc->crc64.csum_hi;
304 case BCH_EXTENT_CRC128: {
305 struct bch_extent_crc_unpacked ret = (struct bch_extent_crc_unpacked) {
306 common_fields(crc->crc128),
307 .nonce = crc->crc128.nonce,
308 .csum = crc->crc128.csum,
319 /* Extent entry iteration: */
321 #define extent_entry_next(_entry) \
322 ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
324 #define extent_entry_last(_e) \
325 vstruct_idx((_e).v, bkey_val_u64s((_e).k))
327 /* Iterate over all entries: */
329 #define extent_for_each_entry_from(_e, _entry, _start) \
330 for ((_entry) = _start; \
331 (_entry) < extent_entry_last(_e); \
332 (_entry) = extent_entry_next(_entry))
334 #define extent_for_each_entry(_e, _entry) \
335 extent_for_each_entry_from(_e, _entry, (_e).v->start)
337 /* Iterate over crcs only: */
339 #define __extent_crc_next(_e, _p) \
341 typeof(&(_e).v->start[0]) _entry = _p; \
343 while ((_entry) < extent_entry_last(_e) && \
344 !extent_entry_is_crc(_entry)) \
345 (_entry) = extent_entry_next(_entry); \
347 entry_to_crc(_entry < extent_entry_last(_e) ? _entry : NULL); \
350 #define __extent_for_each_crc(_e, _crc) \
351 for ((_crc) = __extent_crc_next(_e, (_e).v->start); \
353 (_crc) = __extent_crc_next(_e, extent_entry_next(to_entry(_crc))))
355 #define extent_crc_next(_e, _crc, _iter) \
357 extent_for_each_entry_from(_e, _iter, _iter) \
358 if (extent_entry_is_crc(_iter)) { \
359 (_crc) = bch2_extent_crc_unpack((_e).k, entry_to_crc(_iter));\
363 (_iter) < extent_entry_last(_e); \
366 #define extent_for_each_crc(_e, _crc, _iter) \
367 for ((_crc) = bch2_extent_crc_unpack((_e).k, NULL), \
368 (_iter) = (_e).v->start; \
369 extent_crc_next(_e, _crc, _iter); \
370 (_iter) = extent_entry_next(_iter))
372 /* Iterate over pointers, with crcs: */
374 #define extent_ptr_crc_next(_e, _ptr, _crc) \
377 typeof(&(_e).v->start[0]) _entry; \
379 extent_for_each_entry_from(_e, _entry, to_entry(_ptr)) \
380 if (extent_entry_is_crc(_entry)) { \
381 (_crc) = bch2_extent_crc_unpack((_e).k, entry_to_crc(_entry));\
383 _ptr = entry_to_ptr(_entry); \
392 #define extent_for_each_ptr_crc(_e, _ptr, _crc) \
393 for ((_crc) = bch2_extent_crc_unpack((_e).k, NULL), \
394 (_ptr) = &(_e).v->start->ptr; \
395 ((_ptr) = extent_ptr_crc_next(_e, _ptr, _crc)); \
398 /* Iterate over pointers only, and from a given position: */
400 #define extent_ptr_next(_e, _ptr) \
402 struct bch_extent_crc_unpacked _crc; \
404 extent_ptr_crc_next(_e, _ptr, _crc); \
407 #define extent_for_each_ptr(_e, _ptr) \
408 for ((_ptr) = &(_e).v->start->ptr; \
409 ((_ptr) = extent_ptr_next(_e, _ptr)); \
412 #define extent_ptr_prev(_e, _ptr) \
414 typeof(&(_e).v->start->ptr) _p; \
415 typeof(&(_e).v->start->ptr) _prev = NULL; \
417 extent_for_each_ptr(_e, _p) { \
427 * Use this when you'll be dropping pointers as you iterate. Quadratic,
430 #define extent_for_each_ptr_backwards(_e, _ptr) \
431 for ((_ptr) = extent_ptr_prev(_e, NULL); \
433 (_ptr) = extent_ptr_prev(_e, _ptr))
435 void bch2_extent_crc_append(struct bkey_i_extent *,
436 struct bch_extent_crc_unpacked);
438 static inline void __extent_entry_push(struct bkey_i_extent *e)
440 union bch_extent_entry *entry = extent_entry_last(extent_i_to_s(e));
442 EBUG_ON(bkey_val_u64s(&e->k) + extent_entry_u64s(entry) >
443 BKEY_EXTENT_VAL_U64s_MAX);
445 e->k.u64s += extent_entry_u64s(entry);
448 static inline void extent_ptr_append(struct bkey_i_extent *e,
449 struct bch_extent_ptr ptr)
451 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
452 extent_entry_last(extent_i_to_s(e))->ptr = ptr;
453 __extent_entry_push(e);
456 static inline struct bch_devs_list bch2_extent_devs(struct bkey_s_c_extent e)
458 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
459 const struct bch_extent_ptr *ptr;
461 extent_for_each_ptr(e, ptr)
462 ret.devs[ret.nr++] = ptr->dev;
467 static inline struct bch_devs_list bch2_extent_dirty_devs(struct bkey_s_c_extent e)
469 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
470 const struct bch_extent_ptr *ptr;
472 extent_for_each_ptr(e, ptr)
474 ret.devs[ret.nr++] = ptr->dev;
479 static inline struct bch_devs_list bch2_extent_cached_devs(struct bkey_s_c_extent e)
481 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
482 const struct bch_extent_ptr *ptr;
484 extent_for_each_ptr(e, ptr)
486 ret.devs[ret.nr++] = ptr->dev;
491 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
495 case BCH_EXTENT_CACHED:
496 return bch2_extent_devs(bkey_s_c_to_extent(k));
498 return (struct bch_devs_list) { .nr = 0 };
502 static inline struct bch_devs_list bch2_bkey_dirty_devs(struct bkey_s_c k)
506 case BCH_EXTENT_CACHED:
507 return bch2_extent_dirty_devs(bkey_s_c_to_extent(k));
509 return (struct bch_devs_list) { .nr = 0 };
513 static inline struct bch_devs_list bch2_bkey_cached_devs(struct bkey_s_c k)
517 case BCH_EXTENT_CACHED:
518 return bch2_extent_cached_devs(bkey_s_c_to_extent(k));
520 return (struct bch_devs_list) { .nr = 0 };
524 bool bch2_can_narrow_extent_crcs(struct bkey_s_c_extent,
525 struct bch_extent_crc_unpacked);
526 bool bch2_extent_narrow_crcs(struct bkey_i_extent *, struct bch_extent_crc_unpacked);
527 void bch2_extent_drop_redundant_crcs(struct bkey_s_extent);
529 void __bch2_extent_drop_ptr(struct bkey_s_extent, struct bch_extent_ptr *);
530 void bch2_extent_drop_ptr(struct bkey_s_extent, struct bch_extent_ptr *);
532 bool bch2_cut_front(struct bpos, struct bkey_i *);
533 bool bch2_cut_back(struct bpos, struct bkey *);
534 void bch2_key_resize(struct bkey *, unsigned);
536 int bch2_check_range_allocated(struct bch_fs *, struct bpos, u64);
538 #endif /* _BCACHEFS_EXTENTS_H */