1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/blkdev.h>
7 #include <linux/errno.h>
8 #include <linux/kernel.h>
9 #include <linux/sched/clock.h>
10 #include <linux/llist.h>
11 #include <linux/ratelimit.h>
12 #include <linux/vmalloc.h>
13 #include <linux/workqueue.h>
14 #include <linux/crc64.h>
18 #define PAGE_SECTORS (PAGE_SIZE / 512)
22 #ifdef CONFIG_BCACHE_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)
30 #define EBUG_ON(cond) do { if (cond); } while (0)
31 #define atomic_dec_bug(v) atomic_dec(v)
32 #define atomic_inc_bug(v, i) atomic_inc(v)
36 #define DECLARE_HEAP(type, name) \
42 #define init_heap(heap, _size, gfp) \
46 (heap)->size = (_size); \
47 _bytes = (heap)->size * sizeof(*(heap)->data); \
48 (heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
52 #define free_heap(heap) \
54 kvfree((heap)->data); \
55 (heap)->data = NULL; \
58 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
60 #define heap_sift(h, i, cmp) \
64 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
66 if (_r + 1 < (h)->used && \
67 cmp((h)->data[_r], (h)->data[_r + 1])) \
70 if (cmp((h)->data[_r], (h)->data[_j])) \
72 heap_swap(h, _r, _j); \
76 #define heap_sift_down(h, i, cmp) \
79 size_t p = (i - 1) / 2; \
80 if (cmp((h)->data[i], (h)->data[p])) \
87 #define heap_add(h, d, cmp) \
89 bool _r = !heap_full(h); \
91 size_t _i = (h)->used++; \
94 heap_sift_down(h, _i, cmp); \
95 heap_sift(h, _i, cmp); \
100 #define heap_pop(h, d, cmp) \
102 bool _r = (h)->used; \
104 (d) = (h)->data[0]; \
106 heap_swap(h, 0, (h)->used); \
107 heap_sift(h, 0, cmp); \
112 #define heap_peek(h) ((h)->used ? (h)->data[0] : NULL)
114 #define heap_full(h) ((h)->used == (h)->size)
116 #define DECLARE_FIFO(type, name) \
118 size_t front, back, size, mask; \
122 #define fifo_for_each(c, fifo, iter) \
123 for (iter = (fifo)->front; \
124 c = (fifo)->data[iter], iter != (fifo)->back; \
125 iter = (iter + 1) & (fifo)->mask)
127 #define __init_fifo(fifo, gfp) \
129 size_t _allocated_size, _bytes; \
130 BUG_ON(!(fifo)->size); \
132 _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
133 _bytes = _allocated_size * sizeof(*(fifo)->data); \
135 (fifo)->mask = _allocated_size - 1; \
136 (fifo)->front = (fifo)->back = 0; \
138 (fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
142 #define init_fifo_exact(fifo, _size, gfp) \
144 (fifo)->size = (_size); \
145 __init_fifo(fifo, gfp); \
148 #define init_fifo(fifo, _size, gfp) \
150 (fifo)->size = (_size); \
151 if ((fifo)->size > 4) \
152 (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
153 __init_fifo(fifo, gfp); \
156 #define free_fifo(fifo) \
158 kvfree((fifo)->data); \
159 (fifo)->data = NULL; \
162 #define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
163 #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
165 #define fifo_empty(fifo) (!fifo_used(fifo))
166 #define fifo_full(fifo) (!fifo_free(fifo))
168 #define fifo_front(fifo) ((fifo)->data[(fifo)->front])
169 #define fifo_back(fifo) \
170 ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
172 #define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
174 #define fifo_push_back(fifo, i) \
176 bool _r = !fifo_full((fifo)); \
178 (fifo)->data[(fifo)->back++] = (i); \
179 (fifo)->back &= (fifo)->mask; \
184 #define fifo_pop_front(fifo, i) \
186 bool _r = !fifo_empty((fifo)); \
188 (i) = (fifo)->data[(fifo)->front++]; \
189 (fifo)->front &= (fifo)->mask; \
194 #define fifo_push_front(fifo, i) \
196 bool _r = !fifo_full((fifo)); \
199 (fifo)->front &= (fifo)->mask; \
200 (fifo)->data[(fifo)->front] = (i); \
205 #define fifo_pop_back(fifo, i) \
207 bool _r = !fifo_empty((fifo)); \
210 (fifo)->back &= (fifo)->mask; \
211 (i) = (fifo)->data[(fifo)->back] \
216 #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
217 #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
219 #define fifo_swap(l, r) \
221 swap((l)->front, (r)->front); \
222 swap((l)->back, (r)->back); \
223 swap((l)->size, (r)->size); \
224 swap((l)->mask, (r)->mask); \
225 swap((l)->data, (r)->data); \
228 #define fifo_move(dest, src) \
230 typeof(*((dest)->data)) _t; \
231 while (!fifo_full(dest) && \
233 fifo_push(dest, _t); \
237 * Simple array based allocator - preallocates a number of elements and you can
238 * never allocate more than that, also has no locking.
240 * Handy because if you know you only need a fixed number of elements you don't
241 * have to worry about memory allocation failure, and sometimes a mempool isn't
244 * We treat the free elements as entries in a singly linked list, and the
245 * freelist as a stack - allocating and freeing push and pop off the freelist.
248 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
254 #define array_alloc(array) \
256 typeof((array)->freelist) _ret = (array)->freelist; \
259 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
264 #define array_free(array, ptr) \
266 typeof((array)->freelist) _ptr = ptr; \
268 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
269 (array)->freelist = _ptr; \
272 #define array_allocator_init(array) \
274 typeof((array)->freelist) _i; \
276 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
277 (array)->freelist = NULL; \
279 for (_i = (array)->data; \
280 _i < (array)->data + ARRAY_SIZE((array)->data); \
282 array_free(array, _i); \
285 #define array_freelist_empty(array) ((array)->freelist == NULL)
287 #define ANYSINT_MAX(t) \
288 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
290 int bch_strtoint_h(const char *cp, int *res);
291 int bch_strtouint_h(const char *cp, unsigned int *res);
292 int bch_strtoll_h(const char *cp, long long *res);
293 int bch_strtoull_h(const char *cp, unsigned long long *res);
295 static inline int bch_strtol_h(const char *cp, long *res)
297 #if BITS_PER_LONG == 32
298 return bch_strtoint_h(cp, (int *) res);
300 return bch_strtoll_h(cp, (long long *) res);
304 static inline int bch_strtoul_h(const char *cp, long *res)
306 #if BITS_PER_LONG == 32
307 return bch_strtouint_h(cp, (unsigned int *) res);
309 return bch_strtoull_h(cp, (unsigned long long *) res);
313 #define strtoi_h(cp, res) \
314 (__builtin_types_compatible_p(typeof(*res), int) \
315 ? bch_strtoint_h(cp, (void *) res) \
316 : __builtin_types_compatible_p(typeof(*res), long) \
317 ? bch_strtol_h(cp, (void *) res) \
318 : __builtin_types_compatible_p(typeof(*res), long long) \
319 ? bch_strtoll_h(cp, (void *) res) \
320 : __builtin_types_compatible_p(typeof(*res), unsigned int) \
321 ? bch_strtouint_h(cp, (void *) res) \
322 : __builtin_types_compatible_p(typeof(*res), unsigned long) \
323 ? bch_strtoul_h(cp, (void *) res) \
324 : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
325 ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
327 #define strtoul_safe(cp, var) \
330 int _r = kstrtoul(cp, 10, &_v); \
336 #define strtoul_safe_clamp(cp, var, min, max) \
339 int _r = kstrtoul(cp, 10, &_v); \
341 var = clamp_t(typeof(var), _v, min, max); \
345 #define snprint(buf, size, var) \
346 snprintf(buf, size, \
347 __builtin_types_compatible_p(typeof(var), int) \
349 __builtin_types_compatible_p(typeof(var), unsigned int) \
351 __builtin_types_compatible_p(typeof(var), long) \
353 __builtin_types_compatible_p(typeof(var), unsigned long)\
355 __builtin_types_compatible_p(typeof(var), int64_t) \
357 __builtin_types_compatible_p(typeof(var), uint64_t) \
359 __builtin_types_compatible_p(typeof(var), const char *) \
360 ? "%s\n" : "%i\n", var)
362 ssize_t bch_hprint(char *buf, int64_t v);
364 bool bch_is_zero(const char *p, size_t n);
365 int bch_parse_uuid(const char *s, char *uuid);
370 * all fields are in nanoseconds, averages are ewmas stored left shifted
373 uint64_t max_duration;
374 uint64_t average_duration;
375 uint64_t average_frequency;
379 void bch_time_stats_update(struct time_stats *stats, uint64_t time);
381 static inline unsigned int local_clock_us(void)
383 return local_clock() >> 10;
386 #define NSEC_PER_ns 1L
387 #define NSEC_PER_us NSEC_PER_USEC
388 #define NSEC_PER_ms NSEC_PER_MSEC
389 #define NSEC_PER_sec NSEC_PER_SEC
391 #define __print_time_stat(stats, name, stat, units) \
392 sysfs_print(name ## _ ## stat ## _ ## units, \
393 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
395 #define sysfs_print_time_stats(stats, name, \
399 __print_time_stat(stats, name, \
400 average_frequency, frequency_units); \
401 __print_time_stat(stats, name, \
402 average_duration, duration_units); \
403 sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
404 div_u64((stats)->max_duration, \
405 NSEC_PER_ ## duration_units)); \
407 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
408 ? div_s64(local_clock() - (stats)->last, \
409 NSEC_PER_ ## frequency_units) \
413 #define sysfs_time_stats_attribute(name, \
416 read_attribute(name ## _average_frequency_ ## frequency_units); \
417 read_attribute(name ## _average_duration_ ## duration_units); \
418 read_attribute(name ## _max_duration_ ## duration_units); \
419 read_attribute(name ## _last_ ## frequency_units)
421 #define sysfs_time_stats_attribute_list(name, \
424 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
425 &sysfs_ ## name ## _average_duration_ ## duration_units, \
426 &sysfs_ ## name ## _max_duration_ ## duration_units, \
427 &sysfs_ ## name ## _last_ ## frequency_units,
429 #define ewma_add(ewma, val, weight, factor) \
431 (ewma) *= (weight) - 1; \
432 (ewma) += (val) << factor; \
433 (ewma) /= (weight); \
437 struct bch_ratelimit {
438 /* Next time we want to do some work, in nanoseconds */
442 * Rate at which we want to do work, in units per second
443 * The units here correspond to the units passed to bch_next_delay()
448 static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
450 d->next = local_clock();
453 uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
455 #define __DIV_SAFE(n, d, zero) \
457 typeof(n) _n = (n); \
458 typeof(d) _d = (d); \
459 _d ? _n / _d : zero; \
462 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
464 #define container_of_or_null(ptr, type, member) \
466 typeof(ptr) _ptr = ptr; \
467 _ptr ? container_of(_ptr, type, member) : NULL; \
470 #define RB_INSERT(root, new, member, cmp) \
473 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
479 this = container_of(*n, typeof(*(new)), member); \
480 res = cmp(new, this); \
488 rb_link_node(&(new)->member, parent, n); \
489 rb_insert_color(&(new)->member, root); \
495 #define RB_SEARCH(root, search, member, cmp) \
497 struct rb_node *n = (root)->rb_node; \
498 typeof(&(search)) this, ret = NULL; \
502 this = container_of(n, typeof(search), member); \
503 res = cmp(&(search), this); \
515 #define RB_GREATER(root, search, member, cmp) \
517 struct rb_node *n = (root)->rb_node; \
518 typeof(&(search)) this, ret = NULL; \
522 this = container_of(n, typeof(search), member); \
523 res = cmp(&(search), this); \
533 #define RB_FIRST(root, type, member) \
534 container_of_or_null(rb_first(root), type, member)
536 #define RB_LAST(root, type, member) \
537 container_of_or_null(rb_last(root), type, member)
539 #define RB_NEXT(ptr, member) \
540 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
542 #define RB_PREV(ptr, member) \
543 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
545 static inline uint64_t bch_crc64(const void *p, size_t len)
547 uint64_t crc = 0xffffffffffffffffULL;
549 crc = crc64_be(crc, p, len);
550 return crc ^ 0xffffffffffffffffULL;
553 static inline uint64_t bch_crc64_update(uint64_t crc,
557 crc = crc64_be(crc, p, len);
561 /* Does linear interpolation between powers of two */
562 static inline unsigned int fract_exp_two(unsigned int x,
563 unsigned int fract_bits)
565 unsigned int fract = x & ~(~0 << fract_bits);
569 x += (x * fract) >> fract_bits;
574 void bch_bio_map(struct bio *bio, void *base);
575 int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
577 static inline sector_t bdev_sectors(struct block_device *bdev)
579 return bdev->bd_inode->i_size >> 9;
581 #endif /* _BCACHE_UTIL_H */