5 #include <linux/errno.h>
6 #include <linux/kernel.h>
7 #include <linux/llist.h>
8 #include <linux/ratelimit.h>
9 #include <linux/vmalloc.h>
10 #include <linux/workqueue.h>
14 #define PAGE_SECTORS (PAGE_SIZE / 512)
18 #ifdef CONFIG_BCACHE_DEBUG
20 #define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0)
21 #define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i)
25 #define atomic_dec_bug(v) atomic_dec(v)
26 #define atomic_inc_bug(v, i) atomic_inc(v)
30 #define DECLARE_HEAP(type, name) \
36 #define init_heap(heap, _size, gfp) \
40 (heap)->size = (_size); \
41 _bytes = (heap)->size * sizeof(*(heap)->data); \
42 (heap)->data = NULL; \
43 if (_bytes < KMALLOC_MAX_SIZE) \
44 (heap)->data = kmalloc(_bytes, (gfp)); \
45 if ((!(heap)->data) && ((gfp) & GFP_KERNEL)) \
46 (heap)->data = vmalloc(_bytes); \
50 #define free_heap(heap) \
52 if (is_vmalloc_addr((heap)->data)) \
53 vfree((heap)->data); \
55 kfree((heap)->data); \
56 (heap)->data = NULL; \
59 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
61 #define heap_sift(h, i, cmp) \
65 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
67 if (_r + 1 < (h)->used && \
68 cmp((h)->data[_r], (h)->data[_r + 1])) \
71 if (cmp((h)->data[_r], (h)->data[_j])) \
73 heap_swap(h, _r, _j); \
77 #define heap_sift_down(h, i, cmp) \
80 size_t p = (i - 1) / 2; \
81 if (cmp((h)->data[i], (h)->data[p])) \
88 #define heap_add(h, d, cmp) \
90 bool _r = !heap_full(h); \
92 size_t _i = (h)->used++; \
95 heap_sift_down(h, _i, cmp); \
96 heap_sift(h, _i, cmp); \
101 #define heap_pop(h, d, cmp) \
103 bool _r = (h)->used; \
105 (d) = (h)->data[0]; \
107 heap_swap(h, 0, (h)->used); \
108 heap_sift(h, 0, cmp); \
113 #define heap_peek(h) ((h)->used ? (h)->data[0] : NULL)
115 #define heap_full(h) ((h)->used == (h)->size)
117 #define DECLARE_FIFO(type, name) \
119 size_t front, back, size, mask; \
123 #define fifo_for_each(c, fifo, iter) \
124 for (iter = (fifo)->front; \
125 c = (fifo)->data[iter], iter != (fifo)->back; \
126 iter = (iter + 1) & (fifo)->mask)
128 #define __init_fifo(fifo, gfp) \
130 size_t _allocated_size, _bytes; \
131 BUG_ON(!(fifo)->size); \
133 _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
134 _bytes = _allocated_size * sizeof(*(fifo)->data); \
136 (fifo)->mask = _allocated_size - 1; \
137 (fifo)->front = (fifo)->back = 0; \
138 (fifo)->data = NULL; \
140 if (_bytes < KMALLOC_MAX_SIZE) \
141 (fifo)->data = kmalloc(_bytes, (gfp)); \
142 if ((!(fifo)->data) && ((gfp) & GFP_KERNEL)) \
143 (fifo)->data = vmalloc(_bytes); \
147 #define init_fifo_exact(fifo, _size, gfp) \
149 (fifo)->size = (_size); \
150 __init_fifo(fifo, gfp); \
153 #define init_fifo(fifo, _size, gfp) \
155 (fifo)->size = (_size); \
156 if ((fifo)->size > 4) \
157 (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
158 __init_fifo(fifo, gfp); \
161 #define free_fifo(fifo) \
163 if (is_vmalloc_addr((fifo)->data)) \
164 vfree((fifo)->data); \
166 kfree((fifo)->data); \
167 (fifo)->data = NULL; \
170 #define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
171 #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
173 #define fifo_empty(fifo) (!fifo_used(fifo))
174 #define fifo_full(fifo) (!fifo_free(fifo))
176 #define fifo_front(fifo) ((fifo)->data[(fifo)->front])
177 #define fifo_back(fifo) \
178 ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
180 #define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
182 #define fifo_push_back(fifo, i) \
184 bool _r = !fifo_full((fifo)); \
186 (fifo)->data[(fifo)->back++] = (i); \
187 (fifo)->back &= (fifo)->mask; \
192 #define fifo_pop_front(fifo, i) \
194 bool _r = !fifo_empty((fifo)); \
196 (i) = (fifo)->data[(fifo)->front++]; \
197 (fifo)->front &= (fifo)->mask; \
202 #define fifo_push_front(fifo, i) \
204 bool _r = !fifo_full((fifo)); \
207 (fifo)->front &= (fifo)->mask; \
208 (fifo)->data[(fifo)->front] = (i); \
213 #define fifo_pop_back(fifo, i) \
215 bool _r = !fifo_empty((fifo)); \
218 (fifo)->back &= (fifo)->mask; \
219 (i) = (fifo)->data[(fifo)->back] \
224 #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
225 #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
227 #define fifo_swap(l, r) \
229 swap((l)->front, (r)->front); \
230 swap((l)->back, (r)->back); \
231 swap((l)->size, (r)->size); \
232 swap((l)->mask, (r)->mask); \
233 swap((l)->data, (r)->data); \
236 #define fifo_move(dest, src) \
238 typeof(*((dest)->data)) _t; \
239 while (!fifo_full(dest) && \
241 fifo_push(dest, _t); \
245 * Simple array based allocator - preallocates a number of elements and you can
246 * never allocate more than that, also has no locking.
248 * Handy because if you know you only need a fixed number of elements you don't
249 * have to worry about memory allocation failure, and sometimes a mempool isn't
252 * We treat the free elements as entries in a singly linked list, and the
253 * freelist as a stack - allocating and freeing push and pop off the freelist.
256 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
262 #define array_alloc(array) \
264 typeof((array)->freelist) _ret = (array)->freelist; \
267 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
272 #define array_free(array, ptr) \
274 typeof((array)->freelist) _ptr = ptr; \
276 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
277 (array)->freelist = _ptr; \
280 #define array_allocator_init(array) \
282 typeof((array)->freelist) _i; \
284 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
285 (array)->freelist = NULL; \
287 for (_i = (array)->data; \
288 _i < (array)->data + ARRAY_SIZE((array)->data); \
290 array_free(array, _i); \
293 #define array_freelist_empty(array) ((array)->freelist == NULL)
295 #define ANYSINT_MAX(t) \
296 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
298 int bch_strtoint_h(const char *, int *);
299 int bch_strtouint_h(const char *, unsigned int *);
300 int bch_strtoll_h(const char *, long long *);
301 int bch_strtoull_h(const char *, unsigned long long *);
303 static inline int bch_strtol_h(const char *cp, long *res)
305 #if BITS_PER_LONG == 32
306 return bch_strtoint_h(cp, (int *) res);
308 return bch_strtoll_h(cp, (long long *) res);
312 static inline int bch_strtoul_h(const char *cp, long *res)
314 #if BITS_PER_LONG == 32
315 return bch_strtouint_h(cp, (unsigned int *) res);
317 return bch_strtoull_h(cp, (unsigned long long *) res);
321 #define strtoi_h(cp, res) \
322 (__builtin_types_compatible_p(typeof(*res), int) \
323 ? bch_strtoint_h(cp, (void *) res) \
324 : __builtin_types_compatible_p(typeof(*res), long) \
325 ? bch_strtol_h(cp, (void *) res) \
326 : __builtin_types_compatible_p(typeof(*res), long long) \
327 ? bch_strtoll_h(cp, (void *) res) \
328 : __builtin_types_compatible_p(typeof(*res), unsigned int) \
329 ? bch_strtouint_h(cp, (void *) res) \
330 : __builtin_types_compatible_p(typeof(*res), unsigned long) \
331 ? bch_strtoul_h(cp, (void *) res) \
332 : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
333 ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
335 #define strtoul_safe(cp, var) \
338 int _r = kstrtoul(cp, 10, &_v); \
344 #define strtoul_safe_clamp(cp, var, min, max) \
347 int _r = kstrtoul(cp, 10, &_v); \
349 var = clamp_t(typeof(var), _v, min, max); \
353 #define snprint(buf, size, var) \
354 snprintf(buf, size, \
355 __builtin_types_compatible_p(typeof(var), int) \
357 __builtin_types_compatible_p(typeof(var), unsigned) \
359 __builtin_types_compatible_p(typeof(var), long) \
361 __builtin_types_compatible_p(typeof(var), unsigned long)\
363 __builtin_types_compatible_p(typeof(var), int64_t) \
365 __builtin_types_compatible_p(typeof(var), uint64_t) \
367 __builtin_types_compatible_p(typeof(var), const char *) \
368 ? "%s\n" : "%i\n", var)
370 ssize_t bch_hprint(char *buf, int64_t v);
372 bool bch_is_zero(const char *p, size_t n);
373 int bch_parse_uuid(const char *s, char *uuid);
375 ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
378 ssize_t bch_read_string_list(const char *buf, const char * const list[]);
383 * all fields are in nanoseconds, averages are ewmas stored left shifted
386 uint64_t max_duration;
387 uint64_t average_duration;
388 uint64_t average_frequency;
392 void bch_time_stats_update(struct time_stats *stats, uint64_t time);
394 #define NSEC_PER_ns 1L
395 #define NSEC_PER_us NSEC_PER_USEC
396 #define NSEC_PER_ms NSEC_PER_MSEC
397 #define NSEC_PER_sec NSEC_PER_SEC
399 #define __print_time_stat(stats, name, stat, units) \
400 sysfs_print(name ## _ ## stat ## _ ## units, \
401 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
403 #define sysfs_print_time_stats(stats, name, \
407 __print_time_stat(stats, name, \
408 average_frequency, frequency_units); \
409 __print_time_stat(stats, name, \
410 average_duration, duration_units); \
411 __print_time_stat(stats, name, \
412 max_duration, duration_units); \
414 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
415 ? div_s64(local_clock() - (stats)->last, \
416 NSEC_PER_ ## frequency_units) \
420 #define sysfs_time_stats_attribute(name, \
423 read_attribute(name ## _average_frequency_ ## frequency_units); \
424 read_attribute(name ## _average_duration_ ## duration_units); \
425 read_attribute(name ## _max_duration_ ## duration_units); \
426 read_attribute(name ## _last_ ## frequency_units)
428 #define sysfs_time_stats_attribute_list(name, \
431 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
432 &sysfs_ ## name ## _average_duration_ ## duration_units, \
433 &sysfs_ ## name ## _max_duration_ ## duration_units, \
434 &sysfs_ ## name ## _last_ ## frequency_units,
436 #define ewma_add(ewma, val, weight, factor) \
438 (ewma) *= (weight) - 1; \
439 (ewma) += (val) << factor; \
440 (ewma) /= (weight); \
444 struct bch_ratelimit {
445 /* Next time we want to do some work, in nanoseconds */
449 * Rate at which we want to do work, in units per nanosecond
450 * The units here correspond to the units passed to bch_next_delay()
455 static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
457 d->next = local_clock();
460 uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
462 #define __DIV_SAFE(n, d, zero) \
464 typeof(n) _n = (n); \
465 typeof(d) _d = (d); \
466 _d ? _n / _d : zero; \
469 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
471 #define container_of_or_null(ptr, type, member) \
473 typeof(ptr) _ptr = ptr; \
474 _ptr ? container_of(_ptr, type, member) : NULL; \
477 #define RB_INSERT(root, new, member, cmp) \
480 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
486 this = container_of(*n, typeof(*(new)), member); \
487 res = cmp(new, this); \
495 rb_link_node(&(new)->member, parent, n); \
496 rb_insert_color(&(new)->member, root); \
502 #define RB_SEARCH(root, search, member, cmp) \
504 struct rb_node *n = (root)->rb_node; \
505 typeof(&(search)) this, ret = NULL; \
509 this = container_of(n, typeof(search), member); \
510 res = cmp(&(search), this); \
522 #define RB_GREATER(root, search, member, cmp) \
524 struct rb_node *n = (root)->rb_node; \
525 typeof(&(search)) this, ret = NULL; \
529 this = container_of(n, typeof(search), member); \
530 res = cmp(&(search), this); \
540 #define RB_FIRST(root, type, member) \
541 container_of_or_null(rb_first(root), type, member)
543 #define RB_LAST(root, type, member) \
544 container_of_or_null(rb_last(root), type, member)
546 #define RB_NEXT(ptr, member) \
547 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
549 #define RB_PREV(ptr, member) \
550 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
552 /* Does linear interpolation between powers of two */
553 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
555 unsigned fract = x & ~(~0 << fract_bits);
559 x += (x * fract) >> fract_bits;
564 void bch_bio_map(struct bio *bio, void *base);
566 static inline sector_t bdev_sectors(struct block_device *bdev)
568 return bdev->bd_inode->i_size >> 9;
571 #define closure_bio_submit(bio, cl, dev) \
574 bch_generic_make_request(bio, &(dev)->bio_split_hook); \
577 uint64_t bch_crc64_update(uint64_t, const void *, size_t);
578 uint64_t bch_crc64(const void *, size_t);
580 #endif /* _BCACHE_UTIL_H */