1 #ifndef _BCACHEFS_EXTENTS_H
2 #define _BCACHEFS_EXTENTS_H
6 #include "extents_types.h"
10 struct btree_node_iter;
12 struct btree_insert_entry;
13 struct extent_insert_hook;
17 struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *,
19 struct btree_node_iter *);
20 struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
23 struct btree_node_iter *);
25 extern const struct bkey_ops bch2_bkey_btree_ops;
26 extern const struct bkey_ops bch2_bkey_extent_ops;
28 struct extent_pick_ptr
29 bch2_btree_pick_ptr(struct bch_fs *, const struct btree *,
30 struct bch_devs_mask *avoid);
32 void bch2_extent_pick_ptr(struct bch_fs *, struct bkey_s_c,
33 struct bch_devs_mask *,
34 struct extent_pick_ptr *);
37 bch2_insert_fixup_extent(struct btree_insert *,
38 struct btree_insert_entry *);
40 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
41 void bch2_extent_mark_replicas_cached(struct bch_fs *, struct bkey_s_extent);
43 const struct bch_extent_ptr *
44 bch2_extent_has_device(struct bkey_s_c_extent, unsigned);
45 bool bch2_extent_drop_device(struct bkey_s_extent, unsigned);
47 unsigned bch2_extent_nr_ptrs(struct bkey_s_c_extent);
48 unsigned bch2_extent_nr_dirty_ptrs(struct bkey_s_c);
49 unsigned bch2_extent_nr_good_ptrs(struct bch_fs *, struct bkey_s_c_extent);
50 unsigned bch2_extent_is_compressed(struct bkey_s_c);
52 bool bch2_extent_matches_ptr(struct bch_fs *, struct bkey_s_c_extent,
53 struct bch_extent_ptr, u64);
55 static inline bool bkey_extent_is_data(const struct bkey *k)
59 case BCH_EXTENT_CACHED:
66 static inline bool bkey_extent_is_allocation(const struct bkey *k)
70 case BCH_EXTENT_CACHED:
78 static inline bool bch2_extent_is_fully_allocated(struct bkey_s_c k)
80 return bkey_extent_is_allocation(k.k) &&
81 !bch2_extent_is_compressed(k);
84 static inline bool bkey_extent_is_cached(const struct bkey *k)
86 return k->type == BCH_EXTENT_CACHED;
89 static inline void bkey_extent_set_cached(struct bkey *k, bool cached)
91 EBUG_ON(k->type != BCH_EXTENT &&
92 k->type != BCH_EXTENT_CACHED);
94 k->type = cached ? BCH_EXTENT_CACHED : BCH_EXTENT;
97 static inline unsigned
98 __extent_entry_type(const union bch_extent_entry *e)
100 return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
103 static inline enum bch_extent_entry_type
104 extent_entry_type(const union bch_extent_entry *e)
106 int ret = __ffs(e->type);
108 EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
113 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
115 switch (extent_entry_type(entry)) {
116 case BCH_EXTENT_ENTRY_crc32:
117 return sizeof(struct bch_extent_crc32);
118 case BCH_EXTENT_ENTRY_crc64:
119 return sizeof(struct bch_extent_crc64);
120 case BCH_EXTENT_ENTRY_crc128:
121 return sizeof(struct bch_extent_crc128);
122 case BCH_EXTENT_ENTRY_ptr:
123 return sizeof(struct bch_extent_ptr);
129 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
131 return extent_entry_bytes(entry) / sizeof(u64);
134 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
136 return extent_entry_type(e) == BCH_EXTENT_ENTRY_ptr;
139 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
141 return !extent_entry_is_ptr(e);
144 union bch_extent_crc {
146 struct bch_extent_crc32 crc32;
147 struct bch_extent_crc64 crc64;
148 struct bch_extent_crc128 crc128;
151 /* downcast, preserves const */
152 #define to_entry(_entry) \
154 BUILD_BUG_ON(!type_is(_entry, union bch_extent_crc *) && \
155 !type_is(_entry, struct bch_extent_ptr *)); \
157 __builtin_choose_expr( \
158 (type_is_exact(_entry, const union bch_extent_crc *) || \
159 type_is_exact(_entry, const struct bch_extent_ptr *)), \
160 (const union bch_extent_entry *) (_entry), \
161 (union bch_extent_entry *) (_entry)); \
164 #define __entry_to_crc(_entry) \
165 __builtin_choose_expr( \
166 type_is_exact(_entry, const union bch_extent_entry *), \
167 (const union bch_extent_crc *) (_entry), \
168 (union bch_extent_crc *) (_entry))
170 #define entry_to_crc(_entry) \
172 EBUG_ON((_entry) && !extent_entry_is_crc(_entry)); \
174 __entry_to_crc(_entry); \
177 #define entry_to_ptr(_entry) \
179 EBUG_ON((_entry) && !extent_entry_is_ptr(_entry)); \
181 __builtin_choose_expr( \
182 type_is_exact(_entry, const union bch_extent_entry *), \
183 (const struct bch_extent_ptr *) (_entry), \
184 (struct bch_extent_ptr *) (_entry)); \
187 /* checksum entries: */
189 enum bch_extent_crc_type {
196 static inline enum bch_extent_crc_type
197 __extent_crc_type(const union bch_extent_crc *crc)
200 return BCH_EXTENT_CRC_NONE;
202 switch (extent_entry_type(to_entry(crc))) {
203 case BCH_EXTENT_ENTRY_crc32:
204 return BCH_EXTENT_CRC32;
205 case BCH_EXTENT_ENTRY_crc64:
206 return BCH_EXTENT_CRC64;
207 case BCH_EXTENT_ENTRY_crc128:
208 return BCH_EXTENT_CRC128;
214 #define extent_crc_type(_crc) \
216 BUILD_BUG_ON(!type_is(_crc, struct bch_extent_crc32 *) && \
217 !type_is(_crc, struct bch_extent_crc64 *) && \
218 !type_is(_crc, struct bch_extent_crc128 *) && \
219 !type_is(_crc, union bch_extent_crc *)); \
221 type_is(_crc, struct bch_extent_crc32 *) ? BCH_EXTENT_CRC32 \
222 : type_is(_crc, struct bch_extent_crc64 *) ? BCH_EXTENT_CRC64 \
223 : type_is(_crc, struct bch_extent_crc128 *) ? BCH_EXTENT_CRC128 \
224 : __extent_crc_type((union bch_extent_crc *) _crc); \
227 static inline struct bch_extent_crc_unpacked
228 bch2_extent_crc_unpack(const struct bkey *k, const union bch_extent_crc *crc)
230 #define common_fields(_crc) \
231 .csum_type = _crc.csum_type, \
232 .compression_type = _crc.compression_type, \
233 .compressed_size = _crc._compressed_size + 1, \
234 .uncompressed_size = _crc._uncompressed_size + 1, \
235 .offset = _crc.offset, \
238 switch (extent_crc_type(crc)) {
239 case BCH_EXTENT_CRC_NONE:
240 return (struct bch_extent_crc_unpacked) {
241 .compressed_size = k->size,
242 .uncompressed_size = k->size,
243 .live_size = k->size,
245 case BCH_EXTENT_CRC32:
246 return (struct bch_extent_crc_unpacked) {
247 common_fields(crc->crc32),
248 .csum.lo = (__force __le64) crc->crc32.csum,
250 case BCH_EXTENT_CRC64:
251 return (struct bch_extent_crc_unpacked) {
252 common_fields(crc->crc64),
253 .nonce = crc->crc64.nonce,
254 .csum.lo = (__force __le64) crc->crc64.csum_lo,
255 .csum.hi = (__force __le64) crc->crc64.csum_hi,
257 case BCH_EXTENT_CRC128:
258 return (struct bch_extent_crc_unpacked) {
259 common_fields(crc->crc128),
260 .nonce = crc->crc128.nonce,
261 .csum = crc->crc128.csum,
269 /* Extent entry iteration: */
271 #define extent_entry_next(_entry) \
272 ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
274 #define extent_entry_last(_e) \
275 vstruct_idx((_e).v, bkey_val_u64s((_e).k))
277 /* Iterate over all entries: */
279 #define extent_for_each_entry_from(_e, _entry, _start) \
280 for ((_entry) = _start; \
281 (_entry) < extent_entry_last(_e); \
282 (_entry) = extent_entry_next(_entry))
284 #define extent_for_each_entry(_e, _entry) \
285 extent_for_each_entry_from(_e, _entry, (_e).v->start)
287 /* Iterate over crcs only: */
289 #define __extent_crc_next(_e, _p) \
291 typeof(&(_e).v->start[0]) _entry = _p; \
293 while ((_entry) < extent_entry_last(_e) && \
294 !extent_entry_is_crc(_entry)) \
295 (_entry) = extent_entry_next(_entry); \
297 entry_to_crc(_entry < extent_entry_last(_e) ? _entry : NULL); \
300 #define __extent_for_each_crc(_e, _crc) \
301 for ((_crc) = __extent_crc_next(_e, (_e).v->start); \
303 (_crc) = __extent_crc_next(_e, extent_entry_next(to_entry(_crc))))
305 #define extent_crc_next(_e, _crc, _iter) \
307 extent_for_each_entry_from(_e, _iter, _iter) \
308 if (extent_entry_is_crc(_iter)) { \
309 (_crc) = bch2_extent_crc_unpack((_e).k, entry_to_crc(_iter));\
313 (_iter) < extent_entry_last(_e); \
316 #define extent_for_each_crc(_e, _crc, _iter) \
317 for ((_crc) = bch2_extent_crc_unpack((_e).k, NULL), \
318 (_iter) = (_e).v->start; \
319 extent_crc_next(_e, _crc, _iter); \
320 (_iter) = extent_entry_next(_iter))
322 /* Iterate over pointers, with crcs: */
324 #define extent_ptr_crc_next(_e, _ptr, _crc) \
327 typeof(&(_e).v->start[0]) _entry; \
329 extent_for_each_entry_from(_e, _entry, to_entry(_ptr)) \
330 if (extent_entry_is_crc(_entry)) { \
331 (_crc) = bch2_extent_crc_unpack((_e).k, entry_to_crc(_entry));\
333 _ptr = entry_to_ptr(_entry); \
342 #define extent_for_each_ptr_crc(_e, _ptr, _crc) \
343 for ((_crc) = bch2_extent_crc_unpack((_e).k, NULL), \
344 (_ptr) = &(_e).v->start->ptr; \
345 ((_ptr) = extent_ptr_crc_next(_e, _ptr, _crc)); \
348 /* Iterate over pointers only, and from a given position: */
350 #define extent_ptr_next(_e, _ptr) \
352 struct bch_extent_crc_unpacked _crc; \
354 extent_ptr_crc_next(_e, _ptr, _crc); \
357 #define extent_for_each_ptr(_e, _ptr) \
358 for ((_ptr) = &(_e).v->start->ptr; \
359 ((_ptr) = extent_ptr_next(_e, _ptr)); \
362 #define extent_ptr_prev(_e, _ptr) \
364 typeof(&(_e).v->start->ptr) _p; \
365 typeof(&(_e).v->start->ptr) _prev = NULL; \
367 extent_for_each_ptr(_e, _p) { \
377 * Use this when you'll be dropping pointers as you iterate. Quadratic,
380 #define extent_for_each_ptr_backwards(_e, _ptr) \
381 for ((_ptr) = extent_ptr_prev(_e, NULL); \
383 (_ptr) = extent_ptr_prev(_e, _ptr))
385 void bch2_extent_crc_append(struct bkey_i_extent *,
386 struct bch_extent_crc_unpacked);
388 static inline void __extent_entry_push(struct bkey_i_extent *e)
390 union bch_extent_entry *entry = extent_entry_last(extent_i_to_s(e));
392 EBUG_ON(bkey_val_u64s(&e->k) + extent_entry_u64s(entry) >
393 BKEY_EXTENT_VAL_U64s_MAX);
395 e->k.u64s += extent_entry_u64s(entry);
398 static inline void extent_ptr_append(struct bkey_i_extent *e,
399 struct bch_extent_ptr ptr)
401 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
402 extent_entry_last(extent_i_to_s(e))->ptr = ptr;
403 __extent_entry_push(e);
406 static inline struct bch_devs_list bch2_extent_devs(struct bkey_s_c_extent e)
408 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
409 const struct bch_extent_ptr *ptr;
411 extent_for_each_ptr(e, ptr)
412 ret.devs[ret.nr++] = ptr->dev;
417 static inline struct bch_devs_list bch2_extent_dirty_devs(struct bkey_s_c_extent e)
419 struct bch_devs_list ret = (struct bch_devs_list) { 0 };
420 const struct bch_extent_ptr *ptr;
422 extent_for_each_ptr(e, ptr)
424 ret.devs[ret.nr++] = ptr->dev;
429 static inline struct bch_devs_list bch2_bkey_devs(struct bkey_s_c k)
433 case BCH_EXTENT_CACHED:
434 return bch2_extent_devs(bkey_s_c_to_extent(k));
436 return (struct bch_devs_list) { .nr = 0 };
440 bool bch2_can_narrow_extent_crcs(struct bkey_s_c_extent,
441 struct bch_extent_crc_unpacked);
442 bool bch2_extent_narrow_crcs(struct bkey_i_extent *, struct bch_extent_crc_unpacked);
443 void bch2_extent_drop_redundant_crcs(struct bkey_s_extent);
445 void __bch2_extent_drop_ptr(struct bkey_s_extent, struct bch_extent_ptr *);
446 void bch2_extent_drop_ptr(struct bkey_s_extent, struct bch_extent_ptr *);
448 bool bch2_cut_front(struct bpos, struct bkey_i *);
449 bool bch2_cut_back(struct bpos, struct bkey *);
450 void bch2_key_resize(struct bkey *, unsigned);
452 int bch2_check_range_allocated(struct bch_fs *, struct bpos, u64);
454 #endif /* _BCACHEFS_EXTENTS_H */