]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/util.h
Update bcachefs sources to 09a5465430 bcachefs: Don't need to walk inodes on clean...
[bcachefs-tools-debian] / libbcachefs / util.h
1 #ifndef _BCACHEFS_UTIL_H
2 #define _BCACHEFS_UTIL_H
3
4 #include <linux/bio.h>
5 #include <linux/blkdev.h>
6 #include <linux/closure.h>
7 #include <linux/errno.h>
8 #include <linux/freezer.h>
9 #include <linux/kernel.h>
10 #include <linux/sched/clock.h>
11 #include <linux/llist.h>
12 #include <linux/log2.h>
13 #include <linux/percpu.h>
14 #include <linux/preempt.h>
15 #include <linux/ratelimit.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/workqueue.h>
19
20 #define PAGE_SECTOR_SHIFT       (PAGE_SHIFT - 9)
21 #define PAGE_SECTORS            (1UL << PAGE_SECTOR_SHIFT)
22
23 struct closure;
24
25 #ifdef CONFIG_BCACHEFS_DEBUG
26
27 #define EBUG_ON(cond)           BUG_ON(cond)
28 #define atomic_dec_bug(v)       BUG_ON(atomic_dec_return(v) < 0)
29 #define atomic_inc_bug(v, i)    BUG_ON(atomic_inc_return(v) <= i)
30 #define atomic_sub_bug(i, v)    BUG_ON(atomic_sub_return(i, v) < 0)
31 #define atomic_add_bug(i, v)    BUG_ON(atomic_add_return(i, v) < 0)
32 #define atomic_long_dec_bug(v)          BUG_ON(atomic_long_dec_return(v) < 0)
33 #define atomic_long_sub_bug(i, v)       BUG_ON(atomic_long_sub_return(i, v) < 0)
34 #define atomic64_dec_bug(v)     BUG_ON(atomic64_dec_return(v) < 0)
35 #define atomic64_inc_bug(v, i)  BUG_ON(atomic64_inc_return(v) <= i)
36 #define atomic64_sub_bug(i, v)  BUG_ON(atomic64_sub_return(i, v) < 0)
37 #define atomic64_add_bug(i, v)  BUG_ON(atomic64_add_return(i, v) < 0)
38
39 #define memcpy(dst, src, len)                                           \
40 ({                                                                      \
41         void *_dst = (dst);                                             \
42         const void *_src = (src);                                       \
43         size_t _len = (len);                                            \
44                                                                         \
45         BUG_ON(!((void *) (_dst) >= (void *) (_src) + (_len) ||         \
46                  (void *) (_dst) + (_len) <= (void *) (_src)));         \
47         memcpy(_dst, _src, _len);                                       \
48 })
49
50 #else /* DEBUG */
51
52 #define EBUG_ON(cond)
53 #define atomic_dec_bug(v)       atomic_dec(v)
54 #define atomic_inc_bug(v, i)    atomic_inc(v)
55 #define atomic_sub_bug(i, v)    atomic_sub(i, v)
56 #define atomic_add_bug(i, v)    atomic_add(i, v)
57 #define atomic_long_dec_bug(v)          atomic_long_dec(v)
58 #define atomic_long_sub_bug(i, v)       atomic_long_sub(i, v)
59 #define atomic64_dec_bug(v)     atomic64_dec(v)
60 #define atomic64_inc_bug(v, i)  atomic64_inc(v)
61 #define atomic64_sub_bug(i, v)  atomic64_sub(i, v)
62 #define atomic64_add_bug(i, v)  atomic64_add(i, v)
63
64 #endif
65
66 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
67 #define CPU_BIG_ENDIAN          0
68 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
69 #define CPU_BIG_ENDIAN          1
70 #endif
71
72 /* type hackery */
73
74 #define type_is_exact(_val, _type)                                      \
75         __builtin_types_compatible_p(typeof(_val), _type)
76
77 #define type_is(_val, _type)                                            \
78         (__builtin_types_compatible_p(typeof(_val), _type) ||           \
79          __builtin_types_compatible_p(typeof(_val), const _type))
80
81 /* Userspace doesn't align allocations as nicely as the kernel allocators: */
82 static inline size_t buf_pages(void *p, size_t len)
83 {
84         return DIV_ROUND_UP(len +
85                             ((unsigned long) p & (PAGE_SIZE - 1)),
86                             PAGE_SIZE);
87 }
88
89 static inline void vpfree(void *p, size_t size)
90 {
91         if (is_vmalloc_addr(p))
92                 vfree(p);
93         else
94                 free_pages((unsigned long) p, get_order(size));
95 }
96
97 static inline void *vpmalloc(size_t size, gfp_t gfp_mask)
98 {
99         return (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
100                                          get_order(size)) ?:
101                 __vmalloc(size, gfp_mask, PAGE_KERNEL);
102 }
103
104 static inline void kvpfree(void *p, size_t size)
105 {
106         if (size < PAGE_SIZE)
107                 kfree(p);
108         else
109                 vpfree(p, size);
110 }
111
112 static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
113 {
114         return size < PAGE_SIZE
115                 ? kmalloc(size, gfp_mask)
116                 : vpmalloc(size, gfp_mask);
117 }
118
119 int mempool_init_kvpmalloc_pool(mempool_t *, int, size_t);
120
121 #define HEAP(type)                                                      \
122 struct {                                                                \
123         size_t size, used;                                              \
124         type *data;                                                     \
125 }
126
127 #define DECLARE_HEAP(type, name) HEAP(type) name
128
129 #define init_heap(heap, _size, gfp)                                     \
130 ({                                                                      \
131         (heap)->used = 0;                                               \
132         (heap)->size = (_size);                                         \
133         (heap)->data = kvpmalloc((heap)->size * sizeof((heap)->data[0]),\
134                                  (gfp));                                \
135 })
136
137 #define free_heap(heap)                                                 \
138 do {                                                                    \
139         kvpfree((heap)->data, (heap)->size * sizeof((heap)->data[0]));  \
140         (heap)->data = NULL;                                            \
141 } while (0)
142
143 #define heap_set_backpointer(h, i, _fn)                                 \
144 do {                                                                    \
145         void (*fn)(typeof(h), size_t) = _fn;                            \
146         if (fn)                                                         \
147                 fn(h, i);                                               \
148 } while (0)
149
150 #define heap_swap(h, i, j, set_backpointer)                             \
151 do {                                                                    \
152         swap((h)->data[i], (h)->data[j]);                               \
153         heap_set_backpointer(h, i, set_backpointer);                    \
154         heap_set_backpointer(h, j, set_backpointer);                    \
155 } while (0)
156
157 #define heap_peek(h)                                                    \
158 ({                                                                      \
159         EBUG_ON(!(h)->used);                                            \
160         (h)->data[0];                                                   \
161 })
162
163 #define heap_full(h)    ((h)->used == (h)->size)
164
165 #define heap_sift_down(h, i, cmp, set_backpointer)                      \
166 do {                                                                    \
167         size_t _c, _j = i;                                              \
168                                                                         \
169         for (; _j * 2 + 1 < (h)->used; _j = _c) {                       \
170                 _c = _j * 2 + 1;                                        \
171                 if (_c + 1 < (h)->used &&                               \
172                     cmp(h, (h)->data[_c], (h)->data[_c + 1]) >= 0)      \
173                         _c++;                                           \
174                                                                         \
175                 if (cmp(h, (h)->data[_c], (h)->data[_j]) >= 0)          \
176                         break;                                          \
177                 heap_swap(h, _c, _j, set_backpointer);                  \
178         }                                                               \
179 } while (0)
180
181 #define heap_sift_up(h, i, cmp, set_backpointer)                        \
182 do {                                                                    \
183         while (i) {                                                     \
184                 size_t p = (i - 1) / 2;                                 \
185                 if (cmp(h, (h)->data[i], (h)->data[p]) >= 0)            \
186                         break;                                          \
187                 heap_swap(h, i, p, set_backpointer);                    \
188                 i = p;                                                  \
189         }                                                               \
190 } while (0)
191
192 #define __heap_add(h, d, cmp, set_backpointer)                          \
193 ({                                                                      \
194         size_t _i = (h)->used++;                                        \
195         (h)->data[_i] = d;                                              \
196         heap_set_backpointer(h, _i, set_backpointer);                   \
197                                                                         \
198         heap_sift_up(h, _i, cmp, set_backpointer);                      \
199         _i;                                                             \
200 })
201
202 #define heap_add(h, d, cmp, set_backpointer)                            \
203 ({                                                                      \
204         bool _r = !heap_full(h);                                        \
205         if (_r)                                                         \
206                 __heap_add(h, d, cmp, set_backpointer);                 \
207         _r;                                                             \
208 })
209
210 #define heap_add_or_replace(h, new, cmp, set_backpointer)               \
211 do {                                                                    \
212         if (!heap_add(h, new, cmp, set_backpointer) &&                  \
213             cmp(h, new, heap_peek(h)) >= 0) {                           \
214                 (h)->data[0] = new;                                     \
215                 heap_set_backpointer(h, 0, set_backpointer);            \
216                 heap_sift_down(h, 0, cmp, set_backpointer);             \
217         }                                                               \
218 } while (0)
219
220 #define heap_del(h, i, cmp, set_backpointer)                            \
221 do {                                                                    \
222         size_t _i = (i);                                                \
223                                                                         \
224         BUG_ON(_i >= (h)->used);                                        \
225         (h)->used--;                                                    \
226         heap_swap(h, _i, (h)->used, set_backpointer);                   \
227         heap_sift_up(h, _i, cmp, set_backpointer);                      \
228         heap_sift_down(h, _i, cmp, set_backpointer);                    \
229 } while (0)
230
231 #define heap_pop(h, d, cmp, set_backpointer)                            \
232 ({                                                                      \
233         bool _r = (h)->used;                                            \
234         if (_r) {                                                       \
235                 (d) = (h)->data[0];                                     \
236                 heap_del(h, 0, cmp, set_backpointer);                   \
237         }                                                               \
238         _r;                                                             \
239 })
240
241 #define heap_resort(heap, cmp, set_backpointer)                         \
242 do {                                                                    \
243         ssize_t _i;                                                     \
244         for (_i = (ssize_t) (heap)->used / 2 -  1; _i >= 0; --_i)       \
245                 heap_sift_down(heap, _i, cmp, set_backpointer);         \
246 } while (0)
247
248 #define ANYSINT_MAX(t)                                                  \
249         ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
250
251 struct printbuf {
252         char            *pos;
253         char            *end;
254 };
255
256 static inline size_t printbuf_remaining(struct printbuf *buf)
257 {
258         return buf->end - buf->pos;
259 }
260
261 #define _PBUF(_buf, _len)                                               \
262         ((struct printbuf) {                                            \
263                 .pos    = _buf,                                         \
264                 .end    = _buf + _len,                                  \
265         })
266
267 #define PBUF(_buf) _PBUF(_buf, sizeof(_buf))
268
269 #define pr_buf(_out, ...)                                               \
270 do {                                                                    \
271         (_out)->pos += scnprintf((_out)->pos, printbuf_remaining(_out), \
272                                  __VA_ARGS__);                          \
273 } while (0)
274
275 void bch_scnmemcpy(struct printbuf *, const char *, size_t);
276
277 int bch2_strtoint_h(const char *, int *);
278 int bch2_strtouint_h(const char *, unsigned int *);
279 int bch2_strtoll_h(const char *, long long *);
280 int bch2_strtoull_h(const char *, unsigned long long *);
281 int bch2_strtou64_h(const char *, u64 *);
282
283 static inline int bch2_strtol_h(const char *cp, long *res)
284 {
285 #if BITS_PER_LONG == 32
286         return bch2_strtoint_h(cp, (int *) res);
287 #else
288         return bch2_strtoll_h(cp, (long long *) res);
289 #endif
290 }
291
292 static inline int bch2_strtoul_h(const char *cp, long *res)
293 {
294 #if BITS_PER_LONG == 32
295         return bch2_strtouint_h(cp, (unsigned int *) res);
296 #else
297         return bch2_strtoull_h(cp, (unsigned long long *) res);
298 #endif
299 }
300
301 #define strtoi_h(cp, res)                                               \
302         ( type_is(*res, int)            ? bch2_strtoint_h(cp, (void *) res)\
303         : type_is(*res, long)           ? bch2_strtol_h(cp, (void *) res)\
304         : type_is(*res, long long)      ? bch2_strtoll_h(cp, (void *) res)\
305         : type_is(*res, unsigned)       ? bch2_strtouint_h(cp, (void *) res)\
306         : type_is(*res, unsigned long)  ? bch2_strtoul_h(cp, (void *) res)\
307         : type_is(*res, unsigned long long) ? bch2_strtoull_h(cp, (void *) res)\
308         : -EINVAL)
309
310 #define strtoul_safe(cp, var)                                           \
311 ({                                                                      \
312         unsigned long _v;                                               \
313         int _r = kstrtoul(cp, 10, &_v);                                 \
314         if (!_r)                                                        \
315                 var = _v;                                               \
316         _r;                                                             \
317 })
318
319 #define strtoul_safe_clamp(cp, var, min, max)                           \
320 ({                                                                      \
321         unsigned long _v;                                               \
322         int _r = kstrtoul(cp, 10, &_v);                                 \
323         if (!_r)                                                        \
324                 var = clamp_t(typeof(var), _v, min, max);               \
325         _r;                                                             \
326 })
327
328 #define strtoul_safe_restrict(cp, var, min, max)                        \
329 ({                                                                      \
330         unsigned long _v;                                               \
331         int _r = kstrtoul(cp, 10, &_v);                                 \
332         if (!_r && _v >= min && _v <= max)                              \
333                 var = _v;                                               \
334         else                                                            \
335                 _r = -EINVAL;                                           \
336         _r;                                                             \
337 })
338
339 #define snprint(buf, size, var)                                         \
340         snprintf(buf, size,                                             \
341                    type_is(var, int)            ? "%i\n"                \
342                  : type_is(var, unsigned)       ? "%u\n"                \
343                  : type_is(var, long)           ? "%li\n"               \
344                  : type_is(var, unsigned long)  ? "%lu\n"               \
345                  : type_is(var, s64)            ? "%lli\n"              \
346                  : type_is(var, u64)            ? "%llu\n"              \
347                  : type_is(var, char *)         ? "%s\n"                \
348                  : "%i\n", var)
349
350 void bch2_hprint(struct printbuf *, s64);
351
352 bool bch2_is_zero(const void *, size_t);
353
354 void bch2_string_opt_to_text(struct printbuf *,
355                              const char * const [], size_t);
356
357 void bch2_flags_to_text(struct printbuf *, const char * const[], u64);
358 u64 bch2_read_flag_list(char *, const char * const[]);
359
360 #define NR_QUANTILES    15
361 #define QUANTILE_IDX(i) inorder_to_eytzinger0(i, NR_QUANTILES)
362 #define QUANTILE_FIRST  eytzinger0_first(NR_QUANTILES)
363 #define QUANTILE_LAST   eytzinger0_last(NR_QUANTILES)
364
365 struct quantiles {
366         struct quantile_entry {
367                 u64     m;
368                 u64     step;
369         }               entries[NR_QUANTILES];
370 };
371
372 struct time_stat_buffer {
373         unsigned        nr;
374         struct time_stat_buffer_entry {
375                 u64     start;
376                 u64     end;
377         }               entries[32];
378 };
379
380 struct time_stats {
381         spinlock_t      lock;
382         u64             count;
383         /* all fields are in nanoseconds */
384         u64             average_duration;
385         u64             average_frequency;
386         u64             max_duration;
387         u64             last_event;
388         struct quantiles quantiles;
389
390         struct time_stat_buffer __percpu *buffer;
391 };
392
393 void __bch2_time_stats_update(struct time_stats *stats, u64, u64);
394
395 static inline void bch2_time_stats_update(struct time_stats *stats, u64 start)
396 {
397         __bch2_time_stats_update(stats, start, local_clock());
398 }
399
400 size_t bch2_time_stats_print(struct time_stats *, char *, size_t);
401
402 void bch2_time_stats_exit(struct time_stats *);
403 void bch2_time_stats_init(struct time_stats *);
404
405 #define ewma_add(ewma, val, weight)                                     \
406 ({                                                                      \
407         typeof(ewma) _ewma = (ewma);                                    \
408         typeof(weight) _weight = (weight);                              \
409                                                                         \
410         (((_ewma << _weight) - _ewma) + (val)) >> _weight;              \
411 })
412
413 struct bch_ratelimit {
414         /* Next time we want to do some work, in nanoseconds */
415         u64                     next;
416
417         /*
418          * Rate at which we want to do work, in units per nanosecond
419          * The units here correspond to the units passed to
420          * bch2_ratelimit_increment()
421          */
422         unsigned                rate;
423 };
424
425 static inline void bch2_ratelimit_reset(struct bch_ratelimit *d)
426 {
427         d->next = local_clock();
428 }
429
430 u64 bch2_ratelimit_delay(struct bch_ratelimit *);
431 void bch2_ratelimit_increment(struct bch_ratelimit *, u64);
432
433 struct bch_pd_controller {
434         struct bch_ratelimit    rate;
435         unsigned long           last_update;
436
437         s64                     last_actual;
438         s64                     smoothed_derivative;
439
440         unsigned                p_term_inverse;
441         unsigned                d_smooth;
442         unsigned                d_term;
443
444         /* for exporting to sysfs (no effect on behavior) */
445         s64                     last_derivative;
446         s64                     last_proportional;
447         s64                     last_change;
448         s64                     last_target;
449
450         /* If true, the rate will not increase if bch2_ratelimit_delay()
451          * is not being called often enough. */
452         bool                    backpressure;
453 };
454
455 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int);
456 void bch2_pd_controller_init(struct bch_pd_controller *);
457 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *, char *);
458
459 #define sysfs_pd_controller_attribute(name)                             \
460         rw_attribute(name##_rate);                                      \
461         rw_attribute(name##_rate_bytes);                                \
462         rw_attribute(name##_rate_d_term);                               \
463         rw_attribute(name##_rate_p_term_inverse);                       \
464         read_attribute(name##_rate_debug)
465
466 #define sysfs_pd_controller_files(name)                                 \
467         &sysfs_##name##_rate,                                           \
468         &sysfs_##name##_rate_bytes,                                     \
469         &sysfs_##name##_rate_d_term,                                    \
470         &sysfs_##name##_rate_p_term_inverse,                            \
471         &sysfs_##name##_rate_debug
472
473 #define sysfs_pd_controller_show(name, var)                             \
474 do {                                                                    \
475         sysfs_hprint(name##_rate,               (var)->rate.rate);      \
476         sysfs_print(name##_rate_bytes,          (var)->rate.rate);      \
477         sysfs_print(name##_rate_d_term,         (var)->d_term);         \
478         sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \
479                                                                         \
480         if (attr == &sysfs_##name##_rate_debug)                         \
481                 return bch2_pd_controller_print_debug(var, buf);                \
482 } while (0)
483
484 #define sysfs_pd_controller_store(name, var)                            \
485 do {                                                                    \
486         sysfs_strtoul_clamp(name##_rate,                                \
487                             (var)->rate.rate, 1, UINT_MAX);             \
488         sysfs_strtoul_clamp(name##_rate_bytes,                          \
489                             (var)->rate.rate, 1, UINT_MAX);             \
490         sysfs_strtoul(name##_rate_d_term,       (var)->d_term);         \
491         sysfs_strtoul_clamp(name##_rate_p_term_inverse,                 \
492                             (var)->p_term_inverse, 1, INT_MAX);         \
493 } while (0)
494
495 #define container_of_or_null(ptr, type, member)                         \
496 ({                                                                      \
497         typeof(ptr) _ptr = ptr;                                         \
498         _ptr ? container_of(_ptr, type, member) : NULL;                 \
499 })
500
501 /* Does linear interpolation between powers of two */
502 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
503 {
504         unsigned fract = x & ~(~0 << fract_bits);
505
506         x >>= fract_bits;
507         x   = 1 << x;
508         x  += (x * fract) >> fract_bits;
509
510         return x;
511 }
512
513 void bch2_bio_map(struct bio *bio, void *base);
514 int bch2_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
515
516 static inline sector_t bdev_sectors(struct block_device *bdev)
517 {
518         return bdev->bd_inode->i_size >> 9;
519 }
520
521 #define closure_bio_submit(bio, cl)                                     \
522 do {                                                                    \
523         closure_get(cl);                                                \
524         submit_bio(bio);                                                \
525 } while (0)
526
527 #define kthread_wait_freezable(cond)                                    \
528 ({                                                                      \
529         int _ret = 0;                                                   \
530         while (1) {                                                     \
531                 set_current_state(TASK_INTERRUPTIBLE);                  \
532                 if (kthread_should_stop()) {                            \
533                         _ret = -1;                                      \
534                         break;                                          \
535                 }                                                       \
536                                                                         \
537                 if (cond)                                               \
538                         break;                                          \
539                                                                         \
540                 schedule();                                             \
541                 try_to_freeze();                                        \
542         }                                                               \
543         set_current_state(TASK_RUNNING);                                \
544         _ret;                                                           \
545 })
546
547 size_t bch2_rand_range(size_t);
548
549 void memcpy_to_bio(struct bio *, struct bvec_iter, void *);
550 void memcpy_from_bio(void *, struct bio *, struct bvec_iter);
551
552 static inline void __memcpy_u64s(void *dst, const void *src,
553                                  unsigned u64s)
554 {
555 #ifdef CONFIG_X86_64
556         long d0, d1, d2;
557         asm volatile("rep ; movsq"
558                      : "=&c" (d0), "=&D" (d1), "=&S" (d2)
559                      : "0" (u64s), "1" (dst), "2" (src)
560                      : "memory");
561 #else
562         u64 *d = dst;
563         const u64 *s = src;
564
565         while (u64s--)
566                 *d++ = *s++;
567 #endif
568 }
569
570 static inline void memcpy_u64s(void *dst, const void *src,
571                                unsigned u64s)
572 {
573         EBUG_ON(!(dst >= src + u64s * sizeof(u64) ||
574                  dst + u64s * sizeof(u64) <= src));
575
576         __memcpy_u64s(dst, src, u64s);
577 }
578
579 static inline void __memmove_u64s_down(void *dst, const void *src,
580                                        unsigned u64s)
581 {
582         __memcpy_u64s(dst, src, u64s);
583 }
584
585 static inline void memmove_u64s_down(void *dst, const void *src,
586                                      unsigned u64s)
587 {
588         EBUG_ON(dst > src);
589
590         __memmove_u64s_down(dst, src, u64s);
591 }
592
593 static inline void __memmove_u64s_up(void *_dst, const void *_src,
594                                      unsigned u64s)
595 {
596         u64 *dst = (u64 *) _dst + u64s - 1;
597         u64 *src = (u64 *) _src + u64s - 1;
598
599 #ifdef CONFIG_X86_64
600         long d0, d1, d2;
601         asm volatile("std ;\n"
602                      "rep ; movsq\n"
603                      "cld ;\n"
604                      : "=&c" (d0), "=&D" (d1), "=&S" (d2)
605                      : "0" (u64s), "1" (dst), "2" (src)
606                      : "memory");
607 #else
608         while (u64s--)
609                 *dst-- = *src--;
610 #endif
611 }
612
613 static inline void memmove_u64s_up(void *dst, const void *src,
614                                    unsigned u64s)
615 {
616         EBUG_ON(dst < src);
617
618         __memmove_u64s_up(dst, src, u64s);
619 }
620
621 static inline void memmove_u64s(void *dst, const void *src,
622                                 unsigned u64s)
623 {
624         if (dst < src)
625                 __memmove_u64s_down(dst, src, u64s);
626         else
627                 __memmove_u64s_up(dst, src, u64s);
628 }
629
630 static inline struct bio_vec next_contig_bvec(struct bio *bio,
631                                               struct bvec_iter *iter)
632 {
633         struct bio_vec bv = bio_iter_iovec(bio, *iter);
634
635         bio_advance_iter(bio, iter, bv.bv_len);
636 #ifndef CONFIG_HIGHMEM
637         while (iter->bi_size) {
638                 struct bio_vec next = bio_iter_iovec(bio, *iter);
639
640                 if (page_address(bv.bv_page) + bv.bv_offset + bv.bv_len !=
641                     page_address(next.bv_page) + next.bv_offset)
642                         break;
643
644                 bv.bv_len += next.bv_len;
645                 bio_advance_iter(bio, iter, next.bv_len);
646         }
647 #endif
648         return bv;
649 }
650
651 #define __bio_for_each_contig_segment(bv, bio, iter, start)             \
652         for (iter = (start);                                            \
653              (iter).bi_size &&                                          \
654                 ((bv = next_contig_bvec((bio), &(iter))), 1);)
655
656 #define bio_for_each_contig_segment(bv, bio, iter)                      \
657         __bio_for_each_contig_segment(bv, bio, iter, (bio)->bi_iter)
658
659 void sort_cmp_size(void *base, size_t num, size_t size,
660           int (*cmp_func)(const void *, const void *, size_t),
661           void (*swap_func)(void *, void *, size_t));
662
663 /* just the memmove, doesn't update @_nr */
664 #define __array_insert_item(_array, _nr, _pos)                          \
665         memmove(&(_array)[(_pos) + 1],                                  \
666                 &(_array)[(_pos)],                                      \
667                 sizeof((_array)[0]) * ((_nr) - (_pos)))
668
669 #define array_insert_item(_array, _nr, _pos, _new_item)                 \
670 do {                                                                    \
671         __array_insert_item(_array, _nr, _pos);                         \
672         (_nr)++;                                                        \
673         (_array)[(_pos)] = (_new_item);                                 \
674 } while (0)
675
676 #define array_remove_items(_array, _nr, _pos, _nr_to_remove)            \
677 do {                                                                    \
678         (_nr) -= (_nr_to_remove);                                       \
679         memmove(&(_array)[(_pos)],                                      \
680                 &(_array)[(_pos) + (_nr_to_remove)],                    \
681                 sizeof((_array)[0]) * ((_nr) - (_pos)));                \
682 } while (0)
683
684 #define array_remove_item(_array, _nr, _pos)                            \
685         array_remove_items(_array, _nr, _pos, 1)
686
687 #define bubble_sort(_base, _nr, _cmp)                                   \
688 do {                                                                    \
689         ssize_t _i, _end;                                               \
690         bool _swapped = true;                                           \
691                                                                         \
692         for (_end = (ssize_t) (_nr) - 1; _end > 0 && _swapped; --_end) {\
693                 _swapped = false;                                       \
694                 for (_i = 0; _i < _end; _i++)                           \
695                         if (_cmp((_base)[_i], (_base)[_i + 1]) > 0) {   \
696                                 swap((_base)[_i], (_base)[_i + 1]);     \
697                                 _swapped = true;                        \
698                         }                                               \
699         }                                                               \
700 } while (0)
701
702 static inline u64 percpu_u64_get(u64 __percpu *src)
703 {
704         u64 ret = 0;
705         int cpu;
706
707         for_each_possible_cpu(cpu)
708                 ret += *per_cpu_ptr(src, cpu);
709         return ret;
710 }
711
712 static inline void percpu_u64_set(u64 __percpu *dst, u64 src)
713 {
714         int cpu;
715
716         for_each_possible_cpu(cpu)
717                 *per_cpu_ptr(dst, cpu) = 0;
718
719         preempt_disable();
720         *this_cpu_ptr(dst) = src;
721         preempt_enable();
722 }
723
724 static inline void acc_u64s(u64 *acc, const u64 *src, unsigned nr)
725 {
726         unsigned i;
727
728         for (i = 0; i < nr; i++)
729                 acc[i] += src[i];
730 }
731
732 static inline void acc_u64s_percpu(u64 *acc, const u64 __percpu *src,
733                                    unsigned nr)
734 {
735         int cpu;
736
737         for_each_possible_cpu(cpu)
738                 acc_u64s(acc, per_cpu_ptr(src, cpu), nr);
739 }
740
741 u64 *bch2_acc_percpu_u64s(u64 __percpu *, unsigned);
742
743 #endif /* _BCACHEFS_UTIL_H */