]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/extents.h
Update bcachefs sources to 6a25f7a00d bcachefs: fix ioctl code
[bcachefs-tools-debian] / libbcachefs / extents.h
1 #ifndef _BCACHE_EXTENTS_H
2 #define _BCACHE_EXTENTS_H
3
4 #include "bcachefs.h"
5 #include "bkey.h"
6 #include "io_types.h"
7
8 struct bch_fs;
9 struct journal_res;
10 struct btree_node_iter;
11 struct btree_insert;
12 struct btree_insert_entry;
13 struct extent_insert_hook;
14 struct bch_devs_mask;
15 union bch_extent_crc;
16
17 struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *,
18                                                   struct btree *,
19                                                   struct btree_node_iter *);
20 struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
21                                                      struct bset *,
22                                                      struct btree *,
23                                                      struct btree_node_iter *);
24
25 extern const struct bkey_ops bch2_bkey_btree_ops;
26 extern const struct bkey_ops bch2_bkey_extent_ops;
27
28 void bch2_get_read_device(struct bch_fs *,
29                           const struct bkey *,
30                           const struct bch_extent_ptr *,
31                           const union bch_extent_crc *,
32                           struct bch_devs_mask *,
33                           struct extent_pick_ptr *);
34 struct extent_pick_ptr
35 bch2_btree_pick_ptr(struct bch_fs *, const struct btree *);
36
37 void bch2_extent_pick_ptr(struct bch_fs *, struct bkey_s_c,
38                           struct bch_devs_mask *,
39                           struct extent_pick_ptr *);
40
41 enum btree_insert_ret
42 bch2_insert_fixup_extent(struct btree_insert *,
43                         struct btree_insert_entry *);
44
45 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
46 void bch2_extent_mark_replicas_cached(struct bch_fs *,
47                                      struct bkey_s_extent, unsigned);
48
49 unsigned bch2_extent_nr_ptrs(struct bkey_s_c_extent);
50 unsigned bch2_extent_nr_dirty_ptrs(struct bkey_s_c);
51
52 static inline bool bkey_extent_is_data(const struct bkey *k)
53 {
54         switch (k->type) {
55         case BCH_EXTENT:
56         case BCH_EXTENT_CACHED:
57                 return true;
58         default:
59                 return false;
60         }
61 }
62
63 static inline bool bkey_extent_is_allocation(const struct bkey *k)
64 {
65         switch (k->type) {
66         case BCH_EXTENT:
67         case BCH_EXTENT_CACHED:
68         case BCH_RESERVATION:
69                 return true;
70         default:
71                 return false;
72         }
73 }
74
75 static inline bool bkey_extent_is_cached(const struct bkey *k)
76 {
77         return k->type == BCH_EXTENT_CACHED;
78 }
79
80 static inline void bkey_extent_set_cached(struct bkey *k, bool cached)
81 {
82         EBUG_ON(k->type != BCH_EXTENT &&
83                 k->type != BCH_EXTENT_CACHED);
84
85         k->type = cached ? BCH_EXTENT_CACHED : BCH_EXTENT;
86 }
87
88 static inline unsigned
89 __extent_entry_type(const union bch_extent_entry *e)
90 {
91         return e->type ? __ffs(e->type) : BCH_EXTENT_ENTRY_MAX;
92 }
93
94 static inline enum bch_extent_entry_type
95 extent_entry_type(const union bch_extent_entry *e)
96 {
97         int ret = __ffs(e->type);
98
99         EBUG_ON(ret < 0 || ret >= BCH_EXTENT_ENTRY_MAX);
100
101         return ret;
102 }
103
104 static inline size_t extent_entry_bytes(const union bch_extent_entry *entry)
105 {
106         switch (extent_entry_type(entry)) {
107         case BCH_EXTENT_ENTRY_crc32:
108                 return sizeof(struct bch_extent_crc32);
109         case BCH_EXTENT_ENTRY_crc64:
110                 return sizeof(struct bch_extent_crc64);
111         case BCH_EXTENT_ENTRY_crc128:
112                 return sizeof(struct bch_extent_crc128);
113         case BCH_EXTENT_ENTRY_ptr:
114                 return sizeof(struct bch_extent_ptr);
115         default:
116                 BUG();
117         }
118 }
119
120 static inline size_t extent_entry_u64s(const union bch_extent_entry *entry)
121 {
122         return extent_entry_bytes(entry) / sizeof(u64);
123 }
124
125 static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
126 {
127         return extent_entry_type(e) == BCH_EXTENT_ENTRY_ptr;
128 }
129
130 static inline bool extent_entry_is_crc(const union bch_extent_entry *e)
131 {
132         return !extent_entry_is_ptr(e);
133 }
134
135 union bch_extent_crc {
136         u8                              type;
137         struct bch_extent_crc32         crc32;
138         struct bch_extent_crc64         crc64;
139         struct bch_extent_crc128        crc128;
140 };
141
142 /* downcast, preserves const */
143 #define to_entry(_entry)                                                \
144 ({                                                                      \
145         BUILD_BUG_ON(!type_is(_entry, union bch_extent_crc *) &&        \
146                      !type_is(_entry, struct bch_extent_ptr *));        \
147                                                                         \
148         __builtin_choose_expr(                                          \
149                 (type_is_exact(_entry, const union bch_extent_crc *) || \
150                  type_is_exact(_entry, const struct bch_extent_ptr *)), \
151                 (const union bch_extent_entry *) (_entry),              \
152                 (union bch_extent_entry *) (_entry));                   \
153 })
154
155 #define __entry_to_crc(_entry)                                          \
156         __builtin_choose_expr(                                          \
157                 type_is_exact(_entry, const union bch_extent_entry *),  \
158                 (const union bch_extent_crc *) (_entry),                \
159                 (union bch_extent_crc *) (_entry))
160
161 #define entry_to_crc(_entry)                                            \
162 ({                                                                      \
163         EBUG_ON((_entry) && !extent_entry_is_crc(_entry));              \
164                                                                         \
165         __entry_to_crc(_entry);                                         \
166 })
167
168 #define entry_to_ptr(_entry)                                            \
169 ({                                                                      \
170         EBUG_ON((_entry) && !extent_entry_is_ptr(_entry));              \
171                                                                         \
172         __builtin_choose_expr(                                          \
173                 type_is_exact(_entry, const union bch_extent_entry *),  \
174                 (const struct bch_extent_ptr *) (_entry),               \
175                 (struct bch_extent_ptr *) (_entry));                    \
176 })
177
178 enum bch_extent_crc_type {
179         BCH_EXTENT_CRC_NONE,
180         BCH_EXTENT_CRC32,
181         BCH_EXTENT_CRC64,
182         BCH_EXTENT_CRC128,
183 };
184
185 static inline enum bch_extent_crc_type
186 __extent_crc_type(const union bch_extent_crc *crc)
187 {
188         if (!crc)
189                 return BCH_EXTENT_CRC_NONE;
190
191         switch (extent_entry_type(to_entry(crc))) {
192         case BCH_EXTENT_ENTRY_crc32:
193                 return BCH_EXTENT_CRC32;
194         case BCH_EXTENT_ENTRY_crc64:
195                 return BCH_EXTENT_CRC64;
196         case BCH_EXTENT_ENTRY_crc128:
197                 return BCH_EXTENT_CRC128;
198         default:
199                 BUG();
200         }
201 }
202
203 #define extent_crc_type(_crc)                                           \
204 ({                                                                      \
205         BUILD_BUG_ON(!type_is(_crc, struct bch_extent_crc32 *) &&       \
206                      !type_is(_crc, struct bch_extent_crc64 *) &&       \
207                      !type_is(_crc, struct bch_extent_crc128 *) &&      \
208                      !type_is(_crc, union bch_extent_crc *));           \
209                                                                         \
210           type_is(_crc, struct bch_extent_crc32 *)  ? BCH_EXTENT_CRC32  \
211         : type_is(_crc, struct bch_extent_crc64 *)  ? BCH_EXTENT_CRC64  \
212         : type_is(_crc, struct bch_extent_crc128 *) ? BCH_EXTENT_CRC128 \
213         : __extent_crc_type((union bch_extent_crc *) _crc);             \
214 })
215
216 #define extent_entry_next(_entry)                                       \
217         ((typeof(_entry)) ((void *) (_entry) + extent_entry_bytes(_entry)))
218
219 #define extent_entry_last(_e)                                           \
220         vstruct_idx((_e).v, bkey_val_u64s((_e).k))
221
222 /* Iterate over all entries: */
223
224 #define extent_for_each_entry_from(_e, _entry, _start)                  \
225         for ((_entry) = _start;                                         \
226              (_entry) < extent_entry_last(_e);                          \
227              (_entry) = extent_entry_next(_entry))
228
229 #define extent_for_each_entry(_e, _entry)                               \
230         extent_for_each_entry_from(_e, _entry, (_e).v->start)
231
232 /* Iterate over crcs only: */
233
234 #define extent_crc_next(_e, _p)                                         \
235 ({                                                                      \
236         typeof(&(_e).v->start[0]) _entry = _p;                          \
237                                                                         \
238         while ((_entry) < extent_entry_last(_e) &&                      \
239                !extent_entry_is_crc(_entry))                            \
240                 (_entry) = extent_entry_next(_entry);                   \
241                                                                         \
242         entry_to_crc(_entry < extent_entry_last(_e) ? _entry : NULL);   \
243 })
244
245 #define extent_for_each_crc(_e, _crc)                                   \
246         for ((_crc) = extent_crc_next(_e, (_e).v->start);               \
247              (_crc);                                                    \
248              (_crc) = extent_crc_next(_e, extent_entry_next(to_entry(_crc))))
249
250 /* Iterate over pointers, with crcs: */
251
252 #define extent_ptr_crc_next_filter(_e, _crc, _ptr, _filter)             \
253 ({                                                                      \
254         __label__ out;                                                  \
255         typeof(&(_e).v->start[0]) _entry;                               \
256                                                                         \
257         extent_for_each_entry_from(_e, _entry, to_entry(_ptr))          \
258                 if (extent_entry_is_crc(_entry)) {                      \
259                         (_crc) = entry_to_crc(_entry);                  \
260                 } else {                                                \
261                         _ptr = entry_to_ptr(_entry);                    \
262                         if (_filter)                                    \
263                                 goto out;                               \
264                 }                                                       \
265                                                                         \
266         _ptr = NULL;                                                    \
267 out:                                                                    \
268         _ptr;                                                           \
269 })
270
271 #define extent_for_each_ptr_crc_filter(_e, _ptr, _crc, _filter)         \
272         for ((_crc) = NULL,                                             \
273              (_ptr) = &(_e).v->start->ptr;                              \
274              ((_ptr) = extent_ptr_crc_next_filter(_e, _crc, _ptr, _filter));\
275              (_ptr)++)
276
277 #define extent_for_each_ptr_crc(_e, _ptr, _crc)                         \
278         extent_for_each_ptr_crc_filter(_e, _ptr, _crc, true)
279
280 /* Iterate over pointers only, and from a given position: */
281
282 #define extent_ptr_next_filter(_e, _ptr, _filter)                       \
283 ({                                                                      \
284         typeof(__entry_to_crc(&(_e).v->start[0])) _crc;                 \
285                                                                         \
286         extent_ptr_crc_next_filter(_e, _crc, _ptr, _filter);            \
287 })
288
289 #define extent_ptr_next(_e, _ptr)                                       \
290         extent_ptr_next_filter(_e, _ptr, true)
291
292 #define extent_for_each_ptr_filter(_e, _ptr, _filter)                   \
293         for ((_ptr) = &(_e).v->start->ptr;                              \
294              ((_ptr) = extent_ptr_next_filter(_e, _ptr, _filter));      \
295              (_ptr)++)
296
297 #define extent_for_each_ptr(_e, _ptr)                                   \
298         extent_for_each_ptr_filter(_e, _ptr, true)
299
300 #define extent_ptr_prev(_e, _ptr)                                       \
301 ({                                                                      \
302         typeof(&(_e).v->start->ptr) _p;                                 \
303         typeof(&(_e).v->start->ptr) _prev = NULL;                       \
304                                                                         \
305         extent_for_each_ptr(_e, _p) {                                   \
306                 if (_p == (_ptr))                                       \
307                         break;                                          \
308                 _prev = _p;                                             \
309         }                                                               \
310                                                                         \
311         _prev;                                                          \
312 })
313
314 /*
315  * Use this when you'll be dropping pointers as you iterate. Quadratic,
316  * unfortunately:
317  */
318 #define extent_for_each_ptr_backwards(_e, _ptr)                         \
319         for ((_ptr) = extent_ptr_prev(_e, NULL);                        \
320              (_ptr);                                                    \
321              (_ptr) = extent_ptr_prev(_e, _ptr))
322
323 void bch2_extent_crc_append(struct bkey_i_extent *, unsigned, unsigned,
324                            unsigned, unsigned, struct bch_csum, unsigned);
325
326 static inline void __extent_entry_push(struct bkey_i_extent *e)
327 {
328         union bch_extent_entry *entry = extent_entry_last(extent_i_to_s(e));
329
330         EBUG_ON(bkey_val_u64s(&e->k) + extent_entry_u64s(entry) >
331                 BKEY_EXTENT_VAL_U64s_MAX);
332
333         e->k.u64s += extent_entry_u64s(entry);
334 }
335
336 static inline void extent_ptr_append(struct bkey_i_extent *e,
337                                      struct bch_extent_ptr ptr)
338 {
339         ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
340         extent_entry_last(extent_i_to_s(e))->ptr = ptr;
341         __extent_entry_push(e);
342 }
343
344 static inline struct bch_extent_crc128 crc_to_128(const struct bkey *k,
345                                                   const union bch_extent_crc *crc)
346 {
347         EBUG_ON(!k->size);
348
349         switch (extent_crc_type(crc)) {
350         case BCH_EXTENT_CRC_NONE:
351                 return (struct bch_extent_crc128) {
352                         ._compressed_size       = k->size - 1,
353                         ._uncompressed_size     = k->size - 1,
354                 };
355         case BCH_EXTENT_CRC32:
356                 return (struct bch_extent_crc128) {
357                         .type                   = 1 << BCH_EXTENT_ENTRY_crc128,
358                         ._compressed_size       = crc->crc32._compressed_size,
359                         ._uncompressed_size     = crc->crc32._uncompressed_size,
360                         .offset                 = crc->crc32.offset,
361                         .csum_type              = crc->crc32.csum_type,
362                         .compression_type       = crc->crc32.compression_type,
363                         .csum.lo                = crc->crc32.csum,
364                 };
365         case BCH_EXTENT_CRC64:
366                 return (struct bch_extent_crc128) {
367                         .type                   = 1 << BCH_EXTENT_ENTRY_crc128,
368                         ._compressed_size       = crc->crc64._compressed_size,
369                         ._uncompressed_size     = crc->crc64._uncompressed_size,
370                         .offset                 = crc->crc64.offset,
371                         .nonce                  = crc->crc64.nonce,
372                         .csum_type              = crc->crc64.csum_type,
373                         .compression_type       = crc->crc64.compression_type,
374                         .csum.lo                = crc->crc64.csum_lo,
375                         .csum.hi                = crc->crc64.csum_hi,
376                 };
377         case BCH_EXTENT_CRC128:
378                 return crc->crc128;
379         default:
380                 BUG();
381         }
382 }
383
384 #define crc_compressed_size(_k, _crc)                                   \
385 ({                                                                      \
386         unsigned _size = 0;                                             \
387                                                                         \
388         switch (extent_crc_type(_crc)) {                                \
389         case BCH_EXTENT_CRC_NONE:                                       \
390                 _size = ((const struct bkey *) (_k))->size;             \
391                 break;                                                  \
392         case BCH_EXTENT_CRC32:                                          \
393                 _size = ((struct bch_extent_crc32 *) _crc)              \
394                         ->_compressed_size + 1;                         \
395                 break;                                                  \
396         case BCH_EXTENT_CRC64:                                          \
397                 _size = ((struct bch_extent_crc64 *) _crc)              \
398                         ->_compressed_size + 1;                         \
399                 break;                                                  \
400         case BCH_EXTENT_CRC128:                                         \
401                 _size = ((struct bch_extent_crc128 *) _crc)             \
402                         ->_compressed_size + 1;                         \
403                 break;                                                  \
404         }                                                               \
405         _size;                                                          \
406 })
407
408 #define crc_uncompressed_size(_k, _crc)                                 \
409 ({                                                                      \
410         unsigned _size = 0;                                             \
411                                                                         \
412         switch (extent_crc_type(_crc)) {                                \
413         case BCH_EXTENT_CRC_NONE:                                       \
414                 _size = ((const struct bkey *) (_k))->size;             \
415                 break;                                                  \
416         case BCH_EXTENT_CRC32:                                          \
417                 _size = ((struct bch_extent_crc32 *) _crc)              \
418                         ->_uncompressed_size + 1;                       \
419                 break;                                                  \
420         case BCH_EXTENT_CRC64:                                          \
421                 _size = ((struct bch_extent_crc64 *) _crc)              \
422                         ->_uncompressed_size + 1;                       \
423                 break;                                                  \
424         case BCH_EXTENT_CRC128:                                         \
425                 _size = ((struct bch_extent_crc128 *) _crc)             \
426                         ->_uncompressed_size + 1;                       \
427                 break;                                                  \
428         }                                                               \
429         _size;                                                          \
430 })
431
432 static inline unsigned crc_offset(const union bch_extent_crc *crc)
433 {
434         switch (extent_crc_type(crc)) {
435         case BCH_EXTENT_CRC_NONE:
436                 return 0;
437         case BCH_EXTENT_CRC32:
438                 return crc->crc32.offset;
439         case BCH_EXTENT_CRC64:
440                 return crc->crc64.offset;
441         case BCH_EXTENT_CRC128:
442                 return crc->crc128.offset;
443         default:
444                 BUG();
445         }
446 }
447
448 static inline unsigned crc_nonce(const union bch_extent_crc *crc)
449 {
450         switch (extent_crc_type(crc)) {
451         case BCH_EXTENT_CRC_NONE:
452         case BCH_EXTENT_CRC32:
453                 return 0;
454         case BCH_EXTENT_CRC64:
455                 return crc->crc64.nonce;
456         case BCH_EXTENT_CRC128:
457                 return crc->crc128.nonce;
458         default:
459                 BUG();
460         }
461 }
462
463 static inline unsigned crc_csum_type(const union bch_extent_crc *crc)
464 {
465         switch (extent_crc_type(crc)) {
466         case BCH_EXTENT_CRC_NONE:
467                 return 0;
468         case BCH_EXTENT_CRC32:
469                 return crc->crc32.csum_type;
470         case BCH_EXTENT_CRC64:
471                 return crc->crc64.csum_type;
472         case BCH_EXTENT_CRC128:
473                 return crc->crc128.csum_type;
474         default:
475                 BUG();
476         }
477 }
478
479 static inline unsigned crc_compression_type(const union bch_extent_crc *crc)
480 {
481         switch (extent_crc_type(crc)) {
482         case BCH_EXTENT_CRC_NONE:
483                 return 0;
484         case BCH_EXTENT_CRC32:
485                 return crc->crc32.compression_type;
486         case BCH_EXTENT_CRC64:
487                 return crc->crc64.compression_type;
488         case BCH_EXTENT_CRC128:
489                 return crc->crc128.compression_type;
490         default:
491                 BUG();
492         }
493 }
494
495 static inline struct bch_csum crc_csum(const union bch_extent_crc *crc)
496 {
497         switch (extent_crc_type(crc)) {
498         case BCH_EXTENT_CRC_NONE:
499                 return (struct bch_csum) { 0 };
500         case BCH_EXTENT_CRC32:
501                 return (struct bch_csum) { .lo = crc->crc32.csum };
502         case BCH_EXTENT_CRC64:
503                 return (struct bch_csum) {
504                         .lo = crc->crc64.csum_lo,
505                         .hi = crc->crc64.csum_hi,
506                 };
507         case BCH_EXTENT_CRC128:
508                 return crc->crc128.csum;
509         default:
510                 BUG();
511         }
512 }
513
514 static inline unsigned bkey_extent_is_compressed(struct bkey_s_c k)
515 {
516         struct bkey_s_c_extent e;
517         const struct bch_extent_ptr *ptr;
518         const union bch_extent_crc *crc;
519         unsigned ret = 0;
520
521         switch (k.k->type) {
522         case BCH_EXTENT:
523         case BCH_EXTENT_CACHED:
524                 e = bkey_s_c_to_extent(k);
525
526                 extent_for_each_ptr_crc(e, ptr, crc)
527                         if (!ptr->cached &&
528                             crc_compression_type(crc) != BCH_COMPRESSION_NONE &&
529                             crc_compressed_size(e.k, crc) < k.k->size)
530                                 ret = max_t(unsigned, ret,
531                                             crc_compressed_size(e.k, crc));
532         }
533
534         return ret;
535 }
536
537 static inline unsigned extent_current_nonce(struct bkey_s_c_extent e)
538 {
539         const union bch_extent_crc *crc;
540
541         extent_for_each_crc(e, crc)
542                 if (bch2_csum_type_is_encryption(crc_csum_type(crc)))
543                         return crc_offset(crc) + crc_nonce(crc);
544
545         return 0;
546 }
547
548 void bch2_extent_narrow_crcs(struct bkey_s_extent);
549 void bch2_extent_drop_redundant_crcs(struct bkey_s_extent);
550
551 void __bch2_extent_drop_ptr(struct bkey_s_extent, struct bch_extent_ptr *);
552 void bch2_extent_drop_ptr(struct bkey_s_extent, struct bch_extent_ptr *);
553 void bch2_extent_drop_ptr_idx(struct bkey_s_extent, unsigned);
554
555 const struct bch_extent_ptr *
556 bch2_extent_has_device(struct bkey_s_c_extent, unsigned);
557 struct bch_extent_ptr *
558 bch2_extent_find_ptr(struct bch_fs *, struct bkey_s_extent,
559                      struct bch_extent_ptr);
560 struct bch_extent_ptr *
561 bch2_extent_find_matching_ptr(struct bch_fs *, struct bkey_s_extent,
562                               struct bkey_s_c_extent);
563
564 bool bch2_cut_front(struct bpos, struct bkey_i *);
565 bool bch2_cut_back(struct bpos, struct bkey *);
566 void bch2_key_resize(struct bkey *, unsigned);
567
568 #endif /* _BCACHE_EXTENTS_H */