5 #include <linux/blkdev.h>
6 #include <linux/closure.h>
7 #include <linux/errno.h>
8 #include <linux/blkdev.h>
9 #include <linux/freezer.h>
10 #include <linux/kernel.h>
11 #include <linux/llist.h>
12 #include <linux/ratelimit.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/workqueue.h>
17 #define PAGE_SECTOR_SHIFT (PAGE_SHIFT - 9)
18 #define PAGE_SECTORS (1UL << PAGE_SECTOR_SHIFT)
22 #ifdef CONFIG_BCACHEFS_DEBUG
24 #define EBUG_ON(cond) BUG_ON(cond)
25 #define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0)
26 #define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i)
27 #define atomic_sub_bug(i, v) BUG_ON(atomic_sub_return(i, v) < 0)
28 #define atomic_add_bug(i, v) BUG_ON(atomic_add_return(i, v) < 0)
29 #define atomic_long_dec_bug(v) BUG_ON(atomic_long_dec_return(v) < 0)
30 #define atomic_long_sub_bug(i, v) BUG_ON(atomic_long_sub_return(i, v) < 0)
31 #define atomic64_dec_bug(v) BUG_ON(atomic64_dec_return(v) < 0)
32 #define atomic64_inc_bug(v, i) BUG_ON(atomic64_inc_return(v) <= i)
33 #define atomic64_sub_bug(i, v) BUG_ON(atomic64_sub_return(i, v) < 0)
34 #define atomic64_add_bug(i, v) BUG_ON(atomic64_add_return(i, v) < 0)
36 #define memcpy(_dst, _src, _len) \
38 BUG_ON(!((void *) (_dst) >= (void *) (_src) + (_len) || \
39 (void *) (_dst) + (_len) <= (void *) (_src))); \
40 memcpy(_dst, _src, _len); \
46 #define atomic_dec_bug(v) atomic_dec(v)
47 #define atomic_inc_bug(v, i) atomic_inc(v)
48 #define atomic_sub_bug(i, v) atomic_sub(i, v)
49 #define atomic_add_bug(i, v) atomic_add(i, v)
50 #define atomic_long_dec_bug(v) atomic_long_dec(v)
51 #define atomic_long_sub_bug(i, v) atomic_long_sub(i, v)
52 #define atomic64_dec_bug(v) atomic64_dec(v)
53 #define atomic64_inc_bug(v, i) atomic64_inc(v)
54 #define atomic64_sub_bug(i, v) atomic64_sub(i, v)
55 #define atomic64_add_bug(i, v) atomic64_add(i, v)
60 #define __flatten __attribute__((flatten))
62 /* sparse doesn't know about attribute((flatten)) */
66 #ifdef __LITTLE_ENDIAN
67 #define CPU_BIG_ENDIAN 0
69 #define CPU_BIG_ENDIAN 1
74 #define type_is_exact(_val, _type) \
75 __builtin_types_compatible_p(typeof(_val), _type)
77 #define type_is(_val, _type) \
78 (__builtin_types_compatible_p(typeof(_val), _type) || \
79 __builtin_types_compatible_p(typeof(_val), const _type))
81 static inline void *kvmalloc(size_t bytes, gfp_t gfp)
83 if (bytes <= PAGE_SIZE ||
85 return kmalloc(bytes, gfp);
87 return ((bytes <= KMALLOC_MAX_SIZE)
88 ? kmalloc(bytes, gfp|__GFP_NOWARN)
93 #define DECLARE_HEAP(type, name) \
99 #define init_heap(heap, _size, gfp) \
103 (heap)->size = (_size); \
104 _bytes = (heap)->size * sizeof(*(heap)->data); \
105 (heap)->data = kvmalloc(_bytes, (gfp)); \
109 #define free_heap(heap) \
111 kvfree((heap)->data); \
112 (heap)->data = NULL; \
115 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
117 #define heap_sift(h, i, cmp) \
121 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
123 if (_r + 1 < (h)->used && \
124 cmp((h)->data[_r], (h)->data[_r + 1])) \
127 if (cmp((h)->data[_r], (h)->data[_j])) \
129 heap_swap(h, _r, _j); \
133 #define heap_sift_down(h, i, cmp) \
136 size_t p = (i - 1) / 2; \
137 if (cmp((h)->data[i], (h)->data[p])) \
139 heap_swap(h, i, p); \
144 #define heap_add(h, d, cmp) \
146 bool _r = !heap_full(h); \
148 size_t _i = (h)->used++; \
151 heap_sift_down(h, _i, cmp); \
152 heap_sift(h, _i, cmp); \
157 #define heap_del(h, i, cmp) \
161 BUG_ON(_i >= (h)->used); \
163 heap_swap(h, _i, (h)->used); \
164 heap_sift_down(h, _i, cmp); \
165 heap_sift(h, _i, cmp); \
168 #define heap_pop(h, d, cmp) \
170 bool _r = (h)->used; \
172 (d) = (h)->data[0]; \
173 heap_del(h, 0, cmp); \
178 #define heap_peek(h) \
180 EBUG_ON(!(h)->used); \
184 #define heap_full(h) ((h)->used == (h)->size)
186 #define heap_resort(heap, cmp) \
189 for (_i = (ssize_t) (heap)->used / 2 - 1; _i >= 0; --_i) \
190 heap_sift(heap, _i, cmp); \
194 * Simple array based allocator - preallocates a number of elements and you can
195 * never allocate more than that, also has no locking.
197 * Handy because if you know you only need a fixed number of elements you don't
198 * have to worry about memory allocation failure, and sometimes a mempool isn't
201 * We treat the free elements as entries in a singly linked list, and the
202 * freelist as a stack - allocating and freeing push and pop off the freelist.
205 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
211 #define array_alloc(array) \
213 typeof((array)->freelist) _ret = (array)->freelist; \
216 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
221 #define array_free(array, ptr) \
223 typeof((array)->freelist) _ptr = ptr; \
225 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
226 (array)->freelist = _ptr; \
229 #define array_allocator_init(array) \
231 typeof((array)->freelist) _i; \
233 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
234 (array)->freelist = NULL; \
236 for (_i = (array)->data; \
237 _i < (array)->data + ARRAY_SIZE((array)->data); \
239 array_free(array, _i); \
242 #define array_freelist_empty(array) ((array)->freelist == NULL)
244 #define ANYSINT_MAX(t) \
245 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
247 int bch2_strtoint_h(const char *, int *);
248 int bch2_strtouint_h(const char *, unsigned int *);
249 int bch2_strtoll_h(const char *, long long *);
250 int bch2_strtoull_h(const char *, unsigned long long *);
252 static inline int bch2_strtol_h(const char *cp, long *res)
254 #if BITS_PER_LONG == 32
255 return bch2_strtoint_h(cp, (int *) res);
257 return bch2_strtoll_h(cp, (long long *) res);
261 static inline int bch2_strtoul_h(const char *cp, long *res)
263 #if BITS_PER_LONG == 32
264 return bch2_strtouint_h(cp, (unsigned int *) res);
266 return bch2_strtoull_h(cp, (unsigned long long *) res);
270 #define strtoi_h(cp, res) \
271 ( type_is(*res, int) ? bch2_strtoint_h(cp, (void *) res)\
272 : type_is(*res, long) ? bch2_strtol_h(cp, (void *) res)\
273 : type_is(*res, long long) ? bch2_strtoll_h(cp, (void *) res)\
274 : type_is(*res, unsigned) ? bch2_strtouint_h(cp, (void *) res)\
275 : type_is(*res, unsigned long) ? bch2_strtoul_h(cp, (void *) res)\
276 : type_is(*res, unsigned long long) ? bch2_strtoull_h(cp, (void *) res)\
279 #define strtoul_safe(cp, var) \
282 int _r = kstrtoul(cp, 10, &_v); \
288 #define strtoul_safe_clamp(cp, var, min, max) \
291 int _r = kstrtoul(cp, 10, &_v); \
293 var = clamp_t(typeof(var), _v, min, max); \
297 #define strtoul_safe_restrict(cp, var, min, max) \
300 int _r = kstrtoul(cp, 10, &_v); \
301 if (!_r && _v >= min && _v <= max) \
308 #define snprint(buf, size, var) \
309 snprintf(buf, size, \
310 type_is(var, int) ? "%i\n" \
311 : type_is(var, unsigned) ? "%u\n" \
312 : type_is(var, long) ? "%li\n" \
313 : type_is(var, unsigned long) ? "%lu\n" \
314 : type_is(var, s64) ? "%lli\n" \
315 : type_is(var, u64) ? "%llu\n" \
316 : type_is(var, char *) ? "%s\n" \
319 ssize_t bch2_hprint(char *buf, s64 v);
321 bool bch2_is_zero(const void *, size_t);
323 ssize_t bch2_snprint_string_list(char *buf, size_t size, const char * const list[],
326 ssize_t bch2_read_string_list(const char *buf, const char * const list[]);
332 * all fields are in nanoseconds, averages are ewmas stored left shifted
337 u64 average_duration;
338 u64 average_frequency;
342 void bch2_time_stats_clear(struct time_stats *stats);
343 void __bch2_time_stats_update(struct time_stats *stats, u64 time);
344 void bch2_time_stats_update(struct time_stats *stats, u64 time);
346 static inline unsigned local_clock_us(void)
348 return local_clock() >> 10;
351 #define NSEC_PER_ns 1L
352 #define NSEC_PER_us NSEC_PER_USEC
353 #define NSEC_PER_ms NSEC_PER_MSEC
354 #define NSEC_PER_sec NSEC_PER_SEC
356 #define __print_time_stat(stats, name, stat, units) \
357 sysfs_print(name ## _ ## stat ## _ ## units, \
358 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
360 #define sysfs_print_time_stats(stats, name, \
364 __print_time_stat(stats, name, \
365 average_frequency, frequency_units); \
366 __print_time_stat(stats, name, \
367 average_duration, duration_units); \
368 sysfs_print(name ## _ ##count, (stats)->count); \
369 sysfs_print(name ## _ ##last_duration ## _ ## duration_units, \
370 div_u64((stats)->last_duration, \
371 NSEC_PER_ ## duration_units)); \
372 sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
373 div_u64((stats)->max_duration, \
374 NSEC_PER_ ## duration_units)); \
376 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
377 ? div_s64(local_clock() - (stats)->last, \
378 NSEC_PER_ ## frequency_units) \
382 #define sysfs_clear_time_stats(stats, name) \
384 if (attr == &sysfs_ ## name ## _clear) \
385 bch2_time_stats_clear(stats); \
388 #define sysfs_time_stats_attribute(name, \
391 write_attribute(name ## _clear); \
392 read_attribute(name ## _count); \
393 read_attribute(name ## _average_frequency_ ## frequency_units); \
394 read_attribute(name ## _average_duration_ ## duration_units); \
395 read_attribute(name ## _last_duration_ ## duration_units); \
396 read_attribute(name ## _max_duration_ ## duration_units); \
397 read_attribute(name ## _last_ ## frequency_units)
399 #define sysfs_time_stats_attribute_list(name, \
402 &sysfs_ ## name ## _clear, \
403 &sysfs_ ## name ## _count, \
404 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
405 &sysfs_ ## name ## _average_duration_ ## duration_units, \
406 &sysfs_ ## name ## _last_duration_ ## duration_units, \
407 &sysfs_ ## name ## _max_duration_ ## duration_units, \
408 &sysfs_ ## name ## _last_ ## frequency_units,
410 #define ewma_add(ewma, val, weight) \
412 typeof(ewma) _ewma = (ewma); \
413 typeof(weight) _weight = (weight); \
415 (((_ewma << _weight) - _ewma) + (val)) >> _weight; \
418 struct bch_ratelimit {
419 /* Next time we want to do some work, in nanoseconds */
423 * Rate at which we want to do work, in units per nanosecond
424 * The units here correspond to the units passed to
425 * bch2_ratelimit_increment()
430 static inline void bch2_ratelimit_reset(struct bch_ratelimit *d)
432 d->next = local_clock();
435 u64 bch2_ratelimit_delay(struct bch_ratelimit *);
436 void bch2_ratelimit_increment(struct bch_ratelimit *, u64);
437 int bch2_ratelimit_wait_freezable_stoppable(struct bch_ratelimit *);
439 struct bch_pd_controller {
440 struct bch_ratelimit rate;
441 unsigned long last_update;
444 s64 smoothed_derivative;
446 unsigned p_term_inverse;
450 /* for exporting to sysfs (no effect on behavior) */
452 s64 last_proportional;
456 /* If true, the rate will not increase if bch2_ratelimit_delay()
457 * is not being called often enough. */
461 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int);
462 void bch2_pd_controller_init(struct bch_pd_controller *);
463 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *, char *);
465 #define sysfs_pd_controller_attribute(name) \
466 rw_attribute(name##_rate); \
467 rw_attribute(name##_rate_bytes); \
468 rw_attribute(name##_rate_d_term); \
469 rw_attribute(name##_rate_p_term_inverse); \
470 read_attribute(name##_rate_debug)
472 #define sysfs_pd_controller_files(name) \
473 &sysfs_##name##_rate, \
474 &sysfs_##name##_rate_bytes, \
475 &sysfs_##name##_rate_d_term, \
476 &sysfs_##name##_rate_p_term_inverse, \
477 &sysfs_##name##_rate_debug
479 #define sysfs_pd_controller_show(name, var) \
481 sysfs_hprint(name##_rate, (var)->rate.rate); \
482 sysfs_print(name##_rate_bytes, (var)->rate.rate); \
483 sysfs_print(name##_rate_d_term, (var)->d_term); \
484 sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \
486 if (attr == &sysfs_##name##_rate_debug) \
487 return bch2_pd_controller_print_debug(var, buf); \
490 #define sysfs_pd_controller_store(name, var) \
492 sysfs_strtoul_clamp(name##_rate, \
493 (var)->rate.rate, 1, UINT_MAX); \
494 sysfs_strtoul_clamp(name##_rate_bytes, \
495 (var)->rate.rate, 1, UINT_MAX); \
496 sysfs_strtoul(name##_rate_d_term, (var)->d_term); \
497 sysfs_strtoul_clamp(name##_rate_p_term_inverse, \
498 (var)->p_term_inverse, 1, INT_MAX); \
501 #define __DIV_SAFE(n, d, zero) \
503 typeof(n) _n = (n); \
504 typeof(d) _d = (d); \
505 _d ? _n / _d : zero; \
508 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
510 #define container_of_or_null(ptr, type, member) \
512 typeof(ptr) _ptr = ptr; \
513 _ptr ? container_of(_ptr, type, member) : NULL; \
516 #define RB_INSERT(root, new, member, cmp) \
519 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
525 this = container_of(*n, typeof(*(new)), member); \
526 res = cmp(new, this); \
534 rb_link_node(&(new)->member, parent, n); \
535 rb_insert_color(&(new)->member, root); \
541 #define RB_SEARCH(root, search, member, cmp) \
543 struct rb_node *n = (root)->rb_node; \
544 typeof(&(search)) this, ret = NULL; \
548 this = container_of(n, typeof(search), member); \
549 res = cmp(&(search), this); \
561 #define RB_GREATER(root, search, member, cmp) \
563 struct rb_node *n = (root)->rb_node; \
564 typeof(&(search)) this, ret = NULL; \
568 this = container_of(n, typeof(search), member); \
569 res = cmp(&(search), this); \
579 #define RB_FIRST(root, type, member) \
580 container_of_or_null(rb_first(root), type, member)
582 #define RB_LAST(root, type, member) \
583 container_of_or_null(rb_last(root), type, member)
585 #define RB_NEXT(ptr, member) \
586 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
588 #define RB_PREV(ptr, member) \
589 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
591 /* Does linear interpolation between powers of two */
592 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
594 unsigned fract = x & ~(~0 << fract_bits);
598 x += (x * fract) >> fract_bits;
603 void bch2_bio_map(struct bio *bio, void *base);
605 static inline sector_t bdev_sectors(struct block_device *bdev)
607 return bdev->bd_inode->i_size >> 9;
610 #define closure_bio_submit(bio, cl) \
616 #define kthread_wait_freezable(cond) \
620 set_current_state(TASK_INTERRUPTIBLE); \
621 if (kthread_should_stop()) { \
632 set_current_state(TASK_RUNNING); \
636 size_t bch2_rand_range(size_t);
638 void memcpy_to_bio(struct bio *, struct bvec_iter, void *);
639 void memcpy_from_bio(void *, struct bio *, struct bvec_iter);
641 static inline void __memcpy_u64s(void *dst, const void *src,
646 asm volatile("rep ; movsq"
647 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
648 : "0" (u64s), "1" (dst), "2" (src)
659 static inline void memcpy_u64s(void *dst, const void *src,
662 EBUG_ON(!(dst >= src + u64s * sizeof(u64) ||
663 dst + u64s * sizeof(u64) <= src));
665 __memcpy_u64s(dst, src, u64s);
668 static inline void __memmove_u64s_down(void *dst, const void *src,
671 __memcpy_u64s(dst, src, u64s);
674 static inline void memmove_u64s_down(void *dst, const void *src,
679 __memmove_u64s_down(dst, src, u64s);
682 static inline void __memmove_u64s_up(void *_dst, const void *_src,
685 u64 *dst = (u64 *) _dst + u64s - 1;
686 u64 *src = (u64 *) _src + u64s - 1;
690 asm volatile("std ;\n"
693 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
694 : "0" (u64s), "1" (dst), "2" (src)
702 static inline void memmove_u64s_up(void *dst, const void *src,
707 __memmove_u64s_up(dst, src, u64s);
710 static inline void memmove_u64s(void *dst, const void *src,
714 __memmove_u64s_down(dst, src, u64s);
716 __memmove_u64s_up(dst, src, u64s);
719 static inline struct bio_vec next_contig_bvec(struct bio *bio,
720 struct bvec_iter *iter)
722 struct bio_vec bv = bio_iter_iovec(bio, *iter);
724 bio_advance_iter(bio, iter, bv.bv_len);
725 #ifndef CONFIG_HIGHMEM
726 while (iter->bi_size) {
727 struct bio_vec next = bio_iter_iovec(bio, *iter);
729 if (page_address(bv.bv_page) + bv.bv_offset + bv.bv_len !=
730 page_address(next.bv_page) + next.bv_offset)
733 bv.bv_len += next.bv_len;
734 bio_advance_iter(bio, iter, next.bv_len);
740 #define __bio_for_each_contig_segment(bv, bio, iter, start) \
741 for (iter = (start); \
743 ((bv = next_contig_bvec((bio), &(iter))), 1);)
745 #define bio_for_each_contig_segment(bv, bio, iter) \
746 __bio_for_each_contig_segment(bv, bio, iter, (bio)->bi_iter)
748 #endif /* _BCACHE_UTIL_H */