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 #include <trace/events/bcache.h>
20 #ifdef CONFIG_BCACHE_EDEBUG
22 #define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0)
23 #define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i)
27 #define atomic_dec_bug(v) atomic_dec(v)
28 #define atomic_inc_bug(v, i) atomic_inc(v)
32 #define BITMASK(name, type, field, offset, size) \
33 static inline uint64_t name(const type *k) \
34 { return (k->field >> offset) & ~(((uint64_t) ~0) << size); } \
36 static inline void SET_##name(type *k, uint64_t v) \
38 k->field &= ~(~((uint64_t) ~0 << size) << offset); \
39 k->field |= v << offset; \
42 #define DECLARE_HEAP(type, name) \
48 #define init_heap(heap, _size, gfp) \
52 (heap)->size = (_size); \
53 _bytes = (heap)->size * sizeof(*(heap)->data); \
54 (heap)->data = NULL; \
55 if (_bytes < KMALLOC_MAX_SIZE) \
56 (heap)->data = kmalloc(_bytes, (gfp)); \
57 if ((!(heap)->data) && ((gfp) & GFP_KERNEL)) \
58 (heap)->data = vmalloc(_bytes); \
62 #define free_heap(heap) \
64 if (is_vmalloc_addr((heap)->data)) \
65 vfree((heap)->data); \
67 kfree((heap)->data); \
68 (heap)->data = NULL; \
71 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
73 #define heap_sift(h, i, cmp) \
77 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
79 if (_r + 1 < (h)->used && \
80 cmp((h)->data[_r], (h)->data[_r + 1])) \
83 if (cmp((h)->data[_r], (h)->data[_j])) \
85 heap_swap(h, _r, _j); \
89 #define heap_sift_down(h, i, cmp) \
92 size_t p = (i - 1) / 2; \
93 if (cmp((h)->data[i], (h)->data[p])) \
100 #define heap_add(h, d, cmp) \
102 bool _r = !heap_full(h); \
104 size_t _i = (h)->used++; \
107 heap_sift_down(h, _i, cmp); \
108 heap_sift(h, _i, cmp); \
113 #define heap_pop(h, d, cmp) \
115 bool _r = (h)->used; \
117 (d) = (h)->data[0]; \
119 heap_swap(h, 0, (h)->used); \
120 heap_sift(h, 0, cmp); \
125 #define heap_peek(h) ((h)->size ? (h)->data[0] : NULL)
127 #define heap_full(h) ((h)->used == (h)->size)
129 #define DECLARE_FIFO(type, name) \
131 size_t front, back, size, mask; \
135 #define fifo_for_each(c, fifo, iter) \
136 for (iter = (fifo)->front; \
137 c = (fifo)->data[iter], iter != (fifo)->back; \
138 iter = (iter + 1) & (fifo)->mask)
140 #define __init_fifo(fifo, gfp) \
142 size_t _allocated_size, _bytes; \
143 BUG_ON(!(fifo)->size); \
145 _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
146 _bytes = _allocated_size * sizeof(*(fifo)->data); \
148 (fifo)->mask = _allocated_size - 1; \
149 (fifo)->front = (fifo)->back = 0; \
150 (fifo)->data = NULL; \
152 if (_bytes < KMALLOC_MAX_SIZE) \
153 (fifo)->data = kmalloc(_bytes, (gfp)); \
154 if ((!(fifo)->data) && ((gfp) & GFP_KERNEL)) \
155 (fifo)->data = vmalloc(_bytes); \
159 #define init_fifo_exact(fifo, _size, gfp) \
161 (fifo)->size = (_size); \
162 __init_fifo(fifo, gfp); \
165 #define init_fifo(fifo, _size, gfp) \
167 (fifo)->size = (_size); \
168 if ((fifo)->size > 4) \
169 (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
170 __init_fifo(fifo, gfp); \
173 #define free_fifo(fifo) \
175 if (is_vmalloc_addr((fifo)->data)) \
176 vfree((fifo)->data); \
178 kfree((fifo)->data); \
179 (fifo)->data = NULL; \
182 #define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
183 #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
185 #define fifo_empty(fifo) (!fifo_used(fifo))
186 #define fifo_full(fifo) (!fifo_free(fifo))
188 #define fifo_front(fifo) ((fifo)->data[(fifo)->front])
189 #define fifo_back(fifo) \
190 ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
192 #define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
194 #define fifo_push_back(fifo, i) \
196 bool _r = !fifo_full((fifo)); \
198 (fifo)->data[(fifo)->back++] = (i); \
199 (fifo)->back &= (fifo)->mask; \
204 #define fifo_pop_front(fifo, i) \
206 bool _r = !fifo_empty((fifo)); \
208 (i) = (fifo)->data[(fifo)->front++]; \
209 (fifo)->front &= (fifo)->mask; \
214 #define fifo_push_front(fifo, i) \
216 bool _r = !fifo_full((fifo)); \
219 (fifo)->front &= (fifo)->mask; \
220 (fifo)->data[(fifo)->front] = (i); \
225 #define fifo_pop_back(fifo, i) \
227 bool _r = !fifo_empty((fifo)); \
230 (fifo)->back &= (fifo)->mask; \
231 (i) = (fifo)->data[(fifo)->back] \
236 #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
237 #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
239 #define fifo_swap(l, r) \
241 swap((l)->front, (r)->front); \
242 swap((l)->back, (r)->back); \
243 swap((l)->size, (r)->size); \
244 swap((l)->mask, (r)->mask); \
245 swap((l)->data, (r)->data); \
248 #define fifo_move(dest, src) \
250 typeof(*((dest)->data)) _t; \
251 while (!fifo_full(dest) && \
253 fifo_push(dest, _t); \
257 * Simple array based allocator - preallocates a number of elements and you can
258 * never allocate more than that, also has no locking.
260 * Handy because if you know you only need a fixed number of elements you don't
261 * have to worry about memory allocation failure, and sometimes a mempool isn't
264 * We treat the free elements as entries in a singly linked list, and the
265 * freelist as a stack - allocating and freeing push and pop off the freelist.
268 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
274 #define array_alloc(array) \
276 typeof((array)->freelist) _ret = (array)->freelist; \
279 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
284 #define array_free(array, ptr) \
286 typeof((array)->freelist) _ptr = ptr; \
288 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
289 (array)->freelist = _ptr; \
292 #define array_allocator_init(array) \
294 typeof((array)->freelist) _i; \
296 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
297 (array)->freelist = NULL; \
299 for (_i = (array)->data; \
300 _i < (array)->data + ARRAY_SIZE((array)->data); \
302 array_free(array, _i); \
305 #define array_freelist_empty(array) ((array)->freelist == NULL)
307 #define ANYSINT_MAX(t) \
308 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
310 int bch_strtoint_h(const char *, int *);
311 int bch_strtouint_h(const char *, unsigned int *);
312 int bch_strtoll_h(const char *, long long *);
313 int bch_strtoull_h(const char *, unsigned long long *);
315 static inline int bch_strtol_h(const char *cp, long *res)
317 #if BITS_PER_LONG == 32
318 return bch_strtoint_h(cp, (int *) res);
320 return bch_strtoll_h(cp, (long long *) res);
324 static inline int bch_strtoul_h(const char *cp, long *res)
326 #if BITS_PER_LONG == 32
327 return bch_strtouint_h(cp, (unsigned int *) res);
329 return bch_strtoull_h(cp, (unsigned long long *) res);
333 #define strtoi_h(cp, res) \
334 (__builtin_types_compatible_p(typeof(*res), int) \
335 ? bch_strtoint_h(cp, (void *) res) \
336 : __builtin_types_compatible_p(typeof(*res), long) \
337 ? bch_strtol_h(cp, (void *) res) \
338 : __builtin_types_compatible_p(typeof(*res), long long) \
339 ? bch_strtoll_h(cp, (void *) res) \
340 : __builtin_types_compatible_p(typeof(*res), unsigned int) \
341 ? bch_strtouint_h(cp, (void *) res) \
342 : __builtin_types_compatible_p(typeof(*res), unsigned long) \
343 ? bch_strtoul_h(cp, (void *) res) \
344 : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
345 ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
347 #define strtoul_safe(cp, var) \
350 int _r = kstrtoul(cp, 10, &_v); \
356 #define strtoul_safe_clamp(cp, var, min, max) \
359 int _r = kstrtoul(cp, 10, &_v); \
361 var = clamp_t(typeof(var), _v, min, max); \
365 #define snprint(buf, size, var) \
366 snprintf(buf, size, \
367 __builtin_types_compatible_p(typeof(var), int) \
369 __builtin_types_compatible_p(typeof(var), unsigned) \
371 __builtin_types_compatible_p(typeof(var), long) \
373 __builtin_types_compatible_p(typeof(var), unsigned long)\
375 __builtin_types_compatible_p(typeof(var), int64_t) \
377 __builtin_types_compatible_p(typeof(var), uint64_t) \
379 __builtin_types_compatible_p(typeof(var), const char *) \
380 ? "%s\n" : "%i\n", var)
382 ssize_t bch_hprint(char *buf, int64_t v);
384 bool bch_is_zero(const char *p, size_t n);
385 int bch_parse_uuid(const char *s, char *uuid);
387 ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
390 ssize_t bch_read_string_list(const char *buf, const char * const list[]);
394 * all fields are in nanoseconds, averages are ewmas stored left shifted
397 uint64_t max_duration;
398 uint64_t average_duration;
399 uint64_t average_frequency;
403 void bch_time_stats_update(struct time_stats *stats, uint64_t time);
405 #define NSEC_PER_ns 1L
406 #define NSEC_PER_us NSEC_PER_USEC
407 #define NSEC_PER_ms NSEC_PER_MSEC
408 #define NSEC_PER_sec NSEC_PER_SEC
410 #define __print_time_stat(stats, name, stat, units) \
411 sysfs_print(name ## _ ## stat ## _ ## units, \
412 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
414 #define sysfs_print_time_stats(stats, name, \
418 __print_time_stat(stats, name, \
419 average_frequency, frequency_units); \
420 __print_time_stat(stats, name, \
421 average_duration, duration_units); \
422 __print_time_stat(stats, name, \
423 max_duration, duration_units); \
425 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
426 ? div_s64(local_clock() - (stats)->last, \
427 NSEC_PER_ ## frequency_units) \
431 #define sysfs_time_stats_attribute(name, \
434 read_attribute(name ## _average_frequency_ ## frequency_units); \
435 read_attribute(name ## _average_duration_ ## duration_units); \
436 read_attribute(name ## _max_duration_ ## duration_units); \
437 read_attribute(name ## _last_ ## frequency_units)
439 #define sysfs_time_stats_attribute_list(name, \
442 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
443 &sysfs_ ## name ## _average_duration_ ## duration_units, \
444 &sysfs_ ## name ## _max_duration_ ## duration_units, \
445 &sysfs_ ## name ## _last_ ## frequency_units,
447 #define ewma_add(ewma, val, weight, factor) \
449 (ewma) *= (weight) - 1; \
450 (ewma) += (val) << factor; \
451 (ewma) /= (weight); \
460 static inline void ratelimit_reset(struct ratelimit *d)
462 d->next = local_clock();
465 unsigned bch_next_delay(struct ratelimit *d, uint64_t done);
467 #define __DIV_SAFE(n, d, zero) \
469 typeof(n) _n = (n); \
470 typeof(d) _d = (d); \
471 _d ? _n / _d : zero; \
474 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
476 #define container_of_or_null(ptr, type, member) \
478 typeof(ptr) _ptr = ptr; \
479 _ptr ? container_of(_ptr, type, member) : NULL; \
482 #define RB_INSERT(root, new, member, cmp) \
485 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
491 this = container_of(*n, typeof(*(new)), member); \
492 res = cmp(new, this); \
500 rb_link_node(&(new)->member, parent, n); \
501 rb_insert_color(&(new)->member, root); \
507 #define RB_SEARCH(root, search, member, cmp) \
509 struct rb_node *n = (root)->rb_node; \
510 typeof(&(search)) this, ret = NULL; \
514 this = container_of(n, typeof(search), member); \
515 res = cmp(&(search), this); \
527 #define RB_GREATER(root, search, member, cmp) \
529 struct rb_node *n = (root)->rb_node; \
530 typeof(&(search)) this, ret = NULL; \
534 this = container_of(n, typeof(search), member); \
535 res = cmp(&(search), this); \
545 #define RB_FIRST(root, type, member) \
546 container_of_or_null(rb_first(root), type, member)
548 #define RB_LAST(root, type, member) \
549 container_of_or_null(rb_last(root), type, member)
551 #define RB_NEXT(ptr, member) \
552 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
554 #define RB_PREV(ptr, member) \
555 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
557 /* Does linear interpolation between powers of two */
558 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
560 unsigned fract = x & ~(~0 << fract_bits);
564 x += (x * fract) >> fract_bits;
569 #define bio_end(bio) ((bio)->bi_sector + bio_sectors(bio))
571 void bch_bio_map(struct bio *bio, void *base);
573 int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp);
575 static inline sector_t bdev_sectors(struct block_device *bdev)
577 return bdev->bd_inode->i_size >> 9;
580 #define closure_bio_submit(bio, cl, dev) \
583 bch_generic_make_request(bio, &(dev)->bio_split_hook); \
586 uint64_t bch_crc64_update(uint64_t, const void *, size_t);
587 uint64_t bch_crc64(const void *, size_t);
589 #endif /* _BCACHE_UTIL_H */