5 #include <linux/blkdev.h>
6 #include <linux/errno.h>
7 #include <linux/kernel.h>
8 #include <linux/llist.h>
9 #include <linux/ratelimit.h>
10 #include <linux/vmalloc.h>
11 #include <linux/workqueue.h>
15 #define PAGE_SECTORS (PAGE_SIZE / 512)
19 #ifdef CONFIG_BCACHE_DEBUG
21 #define EBUG_ON(cond) BUG_ON(cond)
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 EBUG_ON(cond) do { if (cond); } while (0)
28 #define atomic_dec_bug(v) atomic_dec(v)
29 #define atomic_inc_bug(v, i) atomic_inc(v)
33 #define DECLARE_HEAP(type, name) \
39 #define init_heap(heap, _size, gfp) \
43 (heap)->size = (_size); \
44 _bytes = (heap)->size * sizeof(*(heap)->data); \
45 (heap)->data = NULL; \
46 if (_bytes < KMALLOC_MAX_SIZE) \
47 (heap)->data = kmalloc(_bytes, (gfp)); \
48 if ((!(heap)->data) && ((gfp) & GFP_KERNEL)) \
49 (heap)->data = vmalloc(_bytes); \
53 #define free_heap(heap) \
55 if (is_vmalloc_addr((heap)->data)) \
56 vfree((heap)->data); \
58 kfree((heap)->data); \
59 (heap)->data = NULL; \
62 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
64 #define heap_sift(h, i, cmp) \
68 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
70 if (_r + 1 < (h)->used && \
71 cmp((h)->data[_r], (h)->data[_r + 1])) \
74 if (cmp((h)->data[_r], (h)->data[_j])) \
76 heap_swap(h, _r, _j); \
80 #define heap_sift_down(h, i, cmp) \
83 size_t p = (i - 1) / 2; \
84 if (cmp((h)->data[i], (h)->data[p])) \
91 #define heap_add(h, d, cmp) \
93 bool _r = !heap_full(h); \
95 size_t _i = (h)->used++; \
98 heap_sift_down(h, _i, cmp); \
99 heap_sift(h, _i, cmp); \
104 #define heap_pop(h, d, cmp) \
106 bool _r = (h)->used; \
108 (d) = (h)->data[0]; \
110 heap_swap(h, 0, (h)->used); \
111 heap_sift(h, 0, cmp); \
116 #define heap_peek(h) ((h)->used ? (h)->data[0] : NULL)
118 #define heap_full(h) ((h)->used == (h)->size)
120 #define DECLARE_FIFO(type, name) \
122 size_t front, back, size, mask; \
126 #define fifo_for_each(c, fifo, iter) \
127 for (iter = (fifo)->front; \
128 c = (fifo)->data[iter], iter != (fifo)->back; \
129 iter = (iter + 1) & (fifo)->mask)
131 #define __init_fifo(fifo, gfp) \
133 size_t _allocated_size, _bytes; \
134 BUG_ON(!(fifo)->size); \
136 _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
137 _bytes = _allocated_size * sizeof(*(fifo)->data); \
139 (fifo)->mask = _allocated_size - 1; \
140 (fifo)->front = (fifo)->back = 0; \
141 (fifo)->data = NULL; \
143 if (_bytes < KMALLOC_MAX_SIZE) \
144 (fifo)->data = kmalloc(_bytes, (gfp)); \
145 if ((!(fifo)->data) && ((gfp) & GFP_KERNEL)) \
146 (fifo)->data = vmalloc(_bytes); \
150 #define init_fifo_exact(fifo, _size, gfp) \
152 (fifo)->size = (_size); \
153 __init_fifo(fifo, gfp); \
156 #define init_fifo(fifo, _size, gfp) \
158 (fifo)->size = (_size); \
159 if ((fifo)->size > 4) \
160 (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
161 __init_fifo(fifo, gfp); \
164 #define free_fifo(fifo) \
166 if (is_vmalloc_addr((fifo)->data)) \
167 vfree((fifo)->data); \
169 kfree((fifo)->data); \
170 (fifo)->data = NULL; \
173 #define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
174 #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
176 #define fifo_empty(fifo) (!fifo_used(fifo))
177 #define fifo_full(fifo) (!fifo_free(fifo))
179 #define fifo_front(fifo) ((fifo)->data[(fifo)->front])
180 #define fifo_back(fifo) \
181 ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
183 #define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
185 #define fifo_push_back(fifo, i) \
187 bool _r = !fifo_full((fifo)); \
189 (fifo)->data[(fifo)->back++] = (i); \
190 (fifo)->back &= (fifo)->mask; \
195 #define fifo_pop_front(fifo, i) \
197 bool _r = !fifo_empty((fifo)); \
199 (i) = (fifo)->data[(fifo)->front++]; \
200 (fifo)->front &= (fifo)->mask; \
205 #define fifo_push_front(fifo, i) \
207 bool _r = !fifo_full((fifo)); \
210 (fifo)->front &= (fifo)->mask; \
211 (fifo)->data[(fifo)->front] = (i); \
216 #define fifo_pop_back(fifo, i) \
218 bool _r = !fifo_empty((fifo)); \
221 (fifo)->back &= (fifo)->mask; \
222 (i) = (fifo)->data[(fifo)->back] \
227 #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
228 #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
230 #define fifo_swap(l, r) \
232 swap((l)->front, (r)->front); \
233 swap((l)->back, (r)->back); \
234 swap((l)->size, (r)->size); \
235 swap((l)->mask, (r)->mask); \
236 swap((l)->data, (r)->data); \
239 #define fifo_move(dest, src) \
241 typeof(*((dest)->data)) _t; \
242 while (!fifo_full(dest) && \
244 fifo_push(dest, _t); \
248 * Simple array based allocator - preallocates a number of elements and you can
249 * never allocate more than that, also has no locking.
251 * Handy because if you know you only need a fixed number of elements you don't
252 * have to worry about memory allocation failure, and sometimes a mempool isn't
255 * We treat the free elements as entries in a singly linked list, and the
256 * freelist as a stack - allocating and freeing push and pop off the freelist.
259 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
265 #define array_alloc(array) \
267 typeof((array)->freelist) _ret = (array)->freelist; \
270 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
275 #define array_free(array, ptr) \
277 typeof((array)->freelist) _ptr = ptr; \
279 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
280 (array)->freelist = _ptr; \
283 #define array_allocator_init(array) \
285 typeof((array)->freelist) _i; \
287 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
288 (array)->freelist = NULL; \
290 for (_i = (array)->data; \
291 _i < (array)->data + ARRAY_SIZE((array)->data); \
293 array_free(array, _i); \
296 #define array_freelist_empty(array) ((array)->freelist == NULL)
298 #define ANYSINT_MAX(t) \
299 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
301 int bch_strtoint_h(const char *, int *);
302 int bch_strtouint_h(const char *, unsigned int *);
303 int bch_strtoll_h(const char *, long long *);
304 int bch_strtoull_h(const char *, unsigned long long *);
306 static inline int bch_strtol_h(const char *cp
, long *res
)
308 #if BITS_PER_LONG == 32
309 return bch_strtoint_h(cp
, (int *) res
);
311 return bch_strtoll_h(cp
, (long long *) res
);
315 static inline int bch_strtoul_h(const char *cp
, long *res
)
317 #if BITS_PER_LONG == 32
318 return bch_strtouint_h(cp
, (unsigned int *) res
);
320 return bch_strtoull_h(cp
, (unsigned long long *) res
);
324 #define strtoi_h(cp, res) \
325 (__builtin_types_compatible_p(typeof(*res), int) \
326 ? bch_strtoint_h(cp, (void *) res) \
327 : __builtin_types_compatible_p(typeof(*res), long) \
328 ? bch_strtol_h(cp, (void *) res) \
329 : __builtin_types_compatible_p(typeof(*res), long long) \
330 ? bch_strtoll_h(cp, (void *) res) \
331 : __builtin_types_compatible_p(typeof(*res), unsigned int) \
332 ? bch_strtouint_h(cp, (void *) res) \
333 : __builtin_types_compatible_p(typeof(*res), unsigned long) \
334 ? bch_strtoul_h(cp, (void *) res) \
335 : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
336 ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
338 #define strtoul_safe(cp, var) \
341 int _r = kstrtoul(cp, 10, &_v); \
347 #define strtoul_safe_clamp(cp, var, min, max) \
350 int _r = kstrtoul(cp, 10, &_v); \
352 var = clamp_t(typeof(var), _v, min, max); \
356 #define snprint(buf, size, var) \
357 snprintf(buf, size, \
358 __builtin_types_compatible_p(typeof(var), int) \
360 __builtin_types_compatible_p(typeof(var), unsigned) \
362 __builtin_types_compatible_p(typeof(var), long) \
364 __builtin_types_compatible_p(typeof(var), unsigned long)\
366 __builtin_types_compatible_p(typeof(var), int64_t) \
368 __builtin_types_compatible_p(typeof(var), uint64_t) \
370 __builtin_types_compatible_p(typeof(var), const char *) \
371 ? "%s\n" : "%i\n", var)
373 ssize_t
bch_hprint(char *buf
, int64_t v
);
375 bool bch_is_zero(const char *p
, size_t n
);
376 int bch_parse_uuid(const char *s
, char *uuid
);
378 ssize_t
bch_snprint_string_list(char *buf
, size_t size
, const char * const list
[],
381 ssize_t
bch_read_string_list(const char *buf
, const char * const list
[]);
386 * all fields are in nanoseconds, averages are ewmas stored left shifted
389 uint64_t max_duration
;
390 uint64_t average_duration
;
391 uint64_t average_frequency
;
395 void bch_time_stats_update(struct time_stats
*stats
, uint64_t time
);
397 static inline unsigned local_clock_us(void)
399 return local_clock() >> 10;
402 #define NSEC_PER_ns 1L
403 #define NSEC_PER_us NSEC_PER_USEC
404 #define NSEC_PER_ms NSEC_PER_MSEC
405 #define NSEC_PER_sec NSEC_PER_SEC
407 #define __print_time_stat(stats, name, stat, units) \
408 sysfs_print(name ## _ ## stat ## _ ## units, \
409 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
411 #define sysfs_print_time_stats(stats, name, \
415 __print_time_stat(stats, name, \
416 average_frequency, frequency_units); \
417 __print_time_stat(stats, name, \
418 average_duration, duration_units); \
419 sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
420 div_u64((stats)->max_duration, NSEC_PER_ ## duration_units));\
422 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
423 ? div_s64(local_clock() - (stats)->last, \
424 NSEC_PER_ ## frequency_units) \
428 #define sysfs_time_stats_attribute(name, \
431 read_attribute(name ## _average_frequency_ ## frequency_units); \
432 read_attribute(name ## _average_duration_ ## duration_units); \
433 read_attribute(name ## _max_duration_ ## duration_units); \
434 read_attribute(name ## _last_ ## frequency_units)
436 #define sysfs_time_stats_attribute_list(name, \
439 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
440 &sysfs_ ## name ## _average_duration_ ## duration_units, \
441 &sysfs_ ## name ## _max_duration_ ## duration_units, \
442 &sysfs_ ## name ## _last_ ## frequency_units,
444 #define ewma_add(ewma, val, weight, factor) \
446 (ewma) *= (weight) - 1; \
447 (ewma) += (val) << factor; \
448 (ewma) /= (weight); \
452 struct bch_ratelimit
{
453 /* Next time we want to do some work, in nanoseconds */
457 * Rate at which we want to do work, in units per nanosecond
458 * The units here correspond to the units passed to bch_next_delay()
463 static inline void bch_ratelimit_reset(struct bch_ratelimit
*d
)
465 d
->next
= local_clock();
468 uint64_t bch_next_delay(struct bch_ratelimit
*d
, uint64_t done
);
470 #define __DIV_SAFE(n, d, zero) \
472 typeof(n) _n = (n); \
473 typeof(d) _d = (d); \
474 _d ? _n / _d : zero; \
477 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
479 #define container_of_or_null(ptr, type, member) \
481 typeof(ptr) _ptr = ptr; \
482 _ptr ? container_of(_ptr, type, member) : NULL; \
485 #define RB_INSERT(root, new, member, cmp) \
488 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
494 this = container_of(*n, typeof(*(new)), member); \
495 res = cmp(new, this); \
503 rb_link_node(&(new)->member, parent, n); \
504 rb_insert_color(&(new)->member, root); \
510 #define RB_SEARCH(root, search, member, cmp) \
512 struct rb_node *n = (root)->rb_node; \
513 typeof(&(search)) this, ret = NULL; \
517 this = container_of(n, typeof(search), member); \
518 res = cmp(&(search), this); \
530 #define RB_GREATER(root, search, member, cmp) \
532 struct rb_node *n = (root)->rb_node; \
533 typeof(&(search)) this, ret = NULL; \
537 this = container_of(n, typeof(search), member); \
538 res = cmp(&(search), this); \
548 #define RB_FIRST(root, type, member) \
549 container_of_or_null(rb_first(root), type, member)
551 #define RB_LAST(root, type, member) \
552 container_of_or_null(rb_last(root), type, member)
554 #define RB_NEXT(ptr, member) \
555 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
557 #define RB_PREV(ptr, member) \
558 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
560 /* Does linear interpolation between powers of two */
561 static inline unsigned fract_exp_two(unsigned x
, unsigned fract_bits
)
563 unsigned fract
= x
& ~(~0 << fract_bits
);
567 x
+= (x
* fract
) >> fract_bits
;
572 void bch_bio_map(struct bio
*bio
, void *base
);
574 static inline sector_t
bdev_sectors(struct block_device
*bdev
)
576 return bdev
->bd_inode
->i_size
>> 9;
579 #define closure_bio_submit(bio, cl, dev) \
582 bch_generic_make_request(bio, &(dev)->bio_split_hook); \
585 uint64_t bch_crc64_update(uint64_t, const void *, size_t);
586 uint64_t bch_crc64(const void *, size_t);
588 #endif /* _BCACHE_UTIL_H */