bcache: Pull on disk data structures out into a separate header
[deliverable/linux.git] / drivers / md / bcache / util.h
CommitLineData
cafe5635
KO
1
2#ifndef _BCACHE_UTIL_H
3#define _BCACHE_UTIL_H
4
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>
11
12#include "closure.h"
13
14#define PAGE_SECTORS (PAGE_SIZE / 512)
15
16struct closure;
17
cafe5635
KO
18#ifdef CONFIG_BCACHE_EDEBUG
19
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)
22
23#else /* EDEBUG */
24
25#define atomic_dec_bug(v) atomic_dec(v)
26#define atomic_inc_bug(v, i) atomic_inc(v)
27
28#endif
29
cafe5635
KO
30#define DECLARE_HEAP(type, name) \
31 struct { \
32 size_t size, used; \
33 type *data; \
34 } name
35
36#define init_heap(heap, _size, gfp) \
37({ \
38 size_t _bytes; \
39 (heap)->used = 0; \
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); \
47 (heap)->data; \
48})
49
50#define free_heap(heap) \
51do { \
52 if (is_vmalloc_addr((heap)->data)) \
53 vfree((heap)->data); \
54 else \
55 kfree((heap)->data); \
56 (heap)->data = NULL; \
57} while (0)
58
59#define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
60
61#define heap_sift(h, i, cmp) \
62do { \
63 size_t _r, _j = i; \
64 \
65 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
66 _r = _j * 2 + 1; \
67 if (_r + 1 < (h)->used && \
68 cmp((h)->data[_r], (h)->data[_r + 1])) \
69 _r++; \
70 \
71 if (cmp((h)->data[_r], (h)->data[_j])) \
72 break; \
73 heap_swap(h, _r, _j); \
74 } \
75} while (0)
76
77#define heap_sift_down(h, i, cmp) \
78do { \
79 while (i) { \
80 size_t p = (i - 1) / 2; \
81 if (cmp((h)->data[i], (h)->data[p])) \
82 break; \
83 heap_swap(h, i, p); \
84 i = p; \
85 } \
86} while (0)
87
88#define heap_add(h, d, cmp) \
89({ \
90 bool _r = !heap_full(h); \
91 if (_r) { \
92 size_t _i = (h)->used++; \
93 (h)->data[_i] = d; \
94 \
95 heap_sift_down(h, _i, cmp); \
96 heap_sift(h, _i, cmp); \
97 } \
98 _r; \
99})
100
101#define heap_pop(h, d, cmp) \
102({ \
103 bool _r = (h)->used; \
104 if (_r) { \
105 (d) = (h)->data[0]; \
106 (h)->used--; \
107 heap_swap(h, 0, (h)->used); \
108 heap_sift(h, 0, cmp); \
109 } \
110 _r; \
111})
112
113#define heap_peek(h) ((h)->size ? (h)->data[0] : NULL)
114
115#define heap_full(h) ((h)->used == (h)->size)
116
117#define DECLARE_FIFO(type, name) \
118 struct { \
119 size_t front, back, size, mask; \
120 type *data; \
121 } name
122
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)
127
128#define __init_fifo(fifo, gfp) \
129({ \
130 size_t _allocated_size, _bytes; \
131 BUG_ON(!(fifo)->size); \
132 \
133 _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
134 _bytes = _allocated_size * sizeof(*(fifo)->data); \
135 \
136 (fifo)->mask = _allocated_size - 1; \
137 (fifo)->front = (fifo)->back = 0; \
138 (fifo)->data = NULL; \
139 \
140 if (_bytes < KMALLOC_MAX_SIZE) \
141 (fifo)->data = kmalloc(_bytes, (gfp)); \
142 if ((!(fifo)->data) && ((gfp) & GFP_KERNEL)) \
143 (fifo)->data = vmalloc(_bytes); \
144 (fifo)->data; \
145})
146
147#define init_fifo_exact(fifo, _size, gfp) \
148({ \
149 (fifo)->size = (_size); \
150 __init_fifo(fifo, gfp); \
151})
152
153#define init_fifo(fifo, _size, gfp) \
154({ \
155 (fifo)->size = (_size); \
156 if ((fifo)->size > 4) \
157 (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
158 __init_fifo(fifo, gfp); \
159})
160
161#define free_fifo(fifo) \
162do { \
163 if (is_vmalloc_addr((fifo)->data)) \
164 vfree((fifo)->data); \
165 else \
166 kfree((fifo)->data); \
167 (fifo)->data = NULL; \
168} while (0)
169
170#define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
171#define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
172
173#define fifo_empty(fifo) (!fifo_used(fifo))
174#define fifo_full(fifo) (!fifo_free(fifo))
175
176#define fifo_front(fifo) ((fifo)->data[(fifo)->front])
177#define fifo_back(fifo) \
178 ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
179
180#define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
181
182#define fifo_push_back(fifo, i) \
183({ \
184 bool _r = !fifo_full((fifo)); \
185 if (_r) { \
186 (fifo)->data[(fifo)->back++] = (i); \
187 (fifo)->back &= (fifo)->mask; \
188 } \
189 _r; \
190})
191
192#define fifo_pop_front(fifo, i) \
193({ \
194 bool _r = !fifo_empty((fifo)); \
195 if (_r) { \
196 (i) = (fifo)->data[(fifo)->front++]; \
197 (fifo)->front &= (fifo)->mask; \
198 } \
199 _r; \
200})
201
202#define fifo_push_front(fifo, i) \
203({ \
204 bool _r = !fifo_full((fifo)); \
205 if (_r) { \
206 --(fifo)->front; \
207 (fifo)->front &= (fifo)->mask; \
208 (fifo)->data[(fifo)->front] = (i); \
209 } \
210 _r; \
211})
212
213#define fifo_pop_back(fifo, i) \
214({ \
215 bool _r = !fifo_empty((fifo)); \
216 if (_r) { \
217 --(fifo)->back; \
218 (fifo)->back &= (fifo)->mask; \
219 (i) = (fifo)->data[(fifo)->back] \
220 } \
221 _r; \
222})
223
224#define fifo_push(fifo, i) fifo_push_back(fifo, (i))
225#define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
226
227#define fifo_swap(l, r) \
228do { \
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); \
234} while (0)
235
236#define fifo_move(dest, src) \
237do { \
238 typeof(*((dest)->data)) _t; \
239 while (!fifo_full(dest) && \
240 fifo_pop(src, _t)) \
241 fifo_push(dest, _t); \
242} while (0)
243
244/*
245 * Simple array based allocator - preallocates a number of elements and you can
246 * never allocate more than that, also has no locking.
247 *
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
250 * what you want.
251 *
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.
254 */
255
256#define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
257 struct { \
258 type *freelist; \
259 type data[size]; \
260 } name
261
262#define array_alloc(array) \
263({ \
264 typeof((array)->freelist) _ret = (array)->freelist; \
265 \
266 if (_ret) \
267 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
268 \
269 _ret; \
270})
271
272#define array_free(array, ptr) \
273do { \
274 typeof((array)->freelist) _ptr = ptr; \
275 \
276 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
277 (array)->freelist = _ptr; \
278} while (0)
279
280#define array_allocator_init(array) \
281do { \
282 typeof((array)->freelist) _i; \
283 \
284 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
285 (array)->freelist = NULL; \
286 \
287 for (_i = (array)->data; \
288 _i < (array)->data + ARRAY_SIZE((array)->data); \
289 _i++) \
290 array_free(array, _i); \
291} while (0)
292
293#define array_freelist_empty(array) ((array)->freelist == NULL)
294
295#define ANYSINT_MAX(t) \
296 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
297
169ef1cf
KO
298int bch_strtoint_h(const char *, int *);
299int bch_strtouint_h(const char *, unsigned int *);
300int bch_strtoll_h(const char *, long long *);
301int bch_strtoull_h(const char *, unsigned long long *);
cafe5635 302
169ef1cf 303static inline int bch_strtol_h(const char *cp, long *res)
cafe5635
KO
304{
305#if BITS_PER_LONG == 32
169ef1cf 306 return bch_strtoint_h(cp, (int *) res);
cafe5635 307#else
169ef1cf 308 return bch_strtoll_h(cp, (long long *) res);
cafe5635
KO
309#endif
310}
311
169ef1cf 312static inline int bch_strtoul_h(const char *cp, long *res)
cafe5635
KO
313{
314#if BITS_PER_LONG == 32
169ef1cf 315 return bch_strtouint_h(cp, (unsigned int *) res);
cafe5635 316#else
169ef1cf 317 return bch_strtoull_h(cp, (unsigned long long *) res);
cafe5635
KO
318#endif
319}
320
321#define strtoi_h(cp, res) \
322 (__builtin_types_compatible_p(typeof(*res), int) \
169ef1cf 323 ? bch_strtoint_h(cp, (void *) res) \
cafe5635 324 : __builtin_types_compatible_p(typeof(*res), long) \
169ef1cf 325 ? bch_strtol_h(cp, (void *) res) \
cafe5635 326 : __builtin_types_compatible_p(typeof(*res), long long) \
169ef1cf 327 ? bch_strtoll_h(cp, (void *) res) \
cafe5635 328 : __builtin_types_compatible_p(typeof(*res), unsigned int) \
169ef1cf 329 ? bch_strtouint_h(cp, (void *) res) \
cafe5635 330 : __builtin_types_compatible_p(typeof(*res), unsigned long) \
169ef1cf 331 ? bch_strtoul_h(cp, (void *) res) \
cafe5635 332 : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
169ef1cf 333 ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
cafe5635
KO
334
335#define strtoul_safe(cp, var) \
336({ \
337 unsigned long _v; \
338 int _r = kstrtoul(cp, 10, &_v); \
339 if (!_r) \
340 var = _v; \
341 _r; \
342})
343
344#define strtoul_safe_clamp(cp, var, min, max) \
345({ \
346 unsigned long _v; \
347 int _r = kstrtoul(cp, 10, &_v); \
348 if (!_r) \
349 var = clamp_t(typeof(var), _v, min, max); \
350 _r; \
351})
352
353#define snprint(buf, size, var) \
354 snprintf(buf, size, \
355 __builtin_types_compatible_p(typeof(var), int) \
356 ? "%i\n" : \
357 __builtin_types_compatible_p(typeof(var), unsigned) \
358 ? "%u\n" : \
359 __builtin_types_compatible_p(typeof(var), long) \
360 ? "%li\n" : \
361 __builtin_types_compatible_p(typeof(var), unsigned long)\
362 ? "%lu\n" : \
363 __builtin_types_compatible_p(typeof(var), int64_t) \
364 ? "%lli\n" : \
365 __builtin_types_compatible_p(typeof(var), uint64_t) \
366 ? "%llu\n" : \
367 __builtin_types_compatible_p(typeof(var), const char *) \
368 ? "%s\n" : "%i\n", var)
369
169ef1cf 370ssize_t bch_hprint(char *buf, int64_t v);
cafe5635 371
169ef1cf
KO
372bool bch_is_zero(const char *p, size_t n);
373int bch_parse_uuid(const char *s, char *uuid);
cafe5635 374
169ef1cf 375ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
cafe5635
KO
376 size_t selected);
377
169ef1cf 378ssize_t bch_read_string_list(const char *buf, const char * const list[]);
cafe5635
KO
379
380struct time_stats {
381 /*
382 * all fields are in nanoseconds, averages are ewmas stored left shifted
383 * by 8
384 */
385 uint64_t max_duration;
386 uint64_t average_duration;
387 uint64_t average_frequency;
388 uint64_t last;
389};
390
169ef1cf 391void bch_time_stats_update(struct time_stats *stats, uint64_t time);
cafe5635
KO
392
393#define NSEC_PER_ns 1L
394#define NSEC_PER_us NSEC_PER_USEC
395#define NSEC_PER_ms NSEC_PER_MSEC
396#define NSEC_PER_sec NSEC_PER_SEC
397
398#define __print_time_stat(stats, name, stat, units) \
399 sysfs_print(name ## _ ## stat ## _ ## units, \
400 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
401
402#define sysfs_print_time_stats(stats, name, \
403 frequency_units, \
404 duration_units) \
405do { \
406 __print_time_stat(stats, name, \
407 average_frequency, frequency_units); \
408 __print_time_stat(stats, name, \
409 average_duration, duration_units); \
410 __print_time_stat(stats, name, \
411 max_duration, duration_units); \
412 \
413 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
414 ? div_s64(local_clock() - (stats)->last, \
415 NSEC_PER_ ## frequency_units) \
416 : -1LL); \
417} while (0)
418
419#define sysfs_time_stats_attribute(name, \
420 frequency_units, \
421 duration_units) \
422read_attribute(name ## _average_frequency_ ## frequency_units); \
423read_attribute(name ## _average_duration_ ## duration_units); \
424read_attribute(name ## _max_duration_ ## duration_units); \
425read_attribute(name ## _last_ ## frequency_units)
426
427#define sysfs_time_stats_attribute_list(name, \
428 frequency_units, \
429 duration_units) \
430&sysfs_ ## name ## _average_frequency_ ## frequency_units, \
431&sysfs_ ## name ## _average_duration_ ## duration_units, \
432&sysfs_ ## name ## _max_duration_ ## duration_units, \
433&sysfs_ ## name ## _last_ ## frequency_units,
434
435#define ewma_add(ewma, val, weight, factor) \
436({ \
437 (ewma) *= (weight) - 1; \
438 (ewma) += (val) << factor; \
439 (ewma) /= (weight); \
440 (ewma) >> factor; \
441})
442
c2a4f318
KO
443struct bch_ratelimit {
444 /* Next time we want to do some work, in nanoseconds */
cafe5635 445 uint64_t next;
c2a4f318
KO
446
447 /*
448 * Rate at which we want to do work, in units per nanosecond
449 * The units here correspond to the units passed to bch_next_delay()
450 */
cafe5635
KO
451 unsigned rate;
452};
453
c2a4f318 454static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
cafe5635
KO
455{
456 d->next = local_clock();
457}
458
c2a4f318 459uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
cafe5635
KO
460
461#define __DIV_SAFE(n, d, zero) \
462({ \
463 typeof(n) _n = (n); \
464 typeof(d) _d = (d); \
465 _d ? _n / _d : zero; \
466})
467
468#define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
469
470#define container_of_or_null(ptr, type, member) \
471({ \
472 typeof(ptr) _ptr = ptr; \
473 _ptr ? container_of(_ptr, type, member) : NULL; \
474})
475
476#define RB_INSERT(root, new, member, cmp) \
477({ \
478 __label__ dup; \
479 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
480 typeof(new) this; \
481 int res, ret = -1; \
482 \
483 while (*n) { \
484 parent = *n; \
485 this = container_of(*n, typeof(*(new)), member); \
486 res = cmp(new, this); \
487 if (!res) \
488 goto dup; \
489 n = res < 0 \
490 ? &(*n)->rb_left \
491 : &(*n)->rb_right; \
492 } \
493 \
494 rb_link_node(&(new)->member, parent, n); \
495 rb_insert_color(&(new)->member, root); \
496 ret = 0; \
497dup: \
498 ret; \
499})
500
501#define RB_SEARCH(root, search, member, cmp) \
502({ \
503 struct rb_node *n = (root)->rb_node; \
504 typeof(&(search)) this, ret = NULL; \
505 int res; \
506 \
507 while (n) { \
508 this = container_of(n, typeof(search), member); \
509 res = cmp(&(search), this); \
510 if (!res) { \
511 ret = this; \
512 break; \
513 } \
514 n = res < 0 \
515 ? n->rb_left \
516 : n->rb_right; \
517 } \
518 ret; \
519})
520
521#define RB_GREATER(root, search, member, cmp) \
522({ \
523 struct rb_node *n = (root)->rb_node; \
524 typeof(&(search)) this, ret = NULL; \
525 int res; \
526 \
527 while (n) { \
528 this = container_of(n, typeof(search), member); \
529 res = cmp(&(search), this); \
530 if (res < 0) { \
531 ret = this; \
532 n = n->rb_left; \
533 } else \
534 n = n->rb_right; \
535 } \
536 ret; \
537})
538
539#define RB_FIRST(root, type, member) \
540 container_of_or_null(rb_first(root), type, member)
541
542#define RB_LAST(root, type, member) \
543 container_of_or_null(rb_last(root), type, member)
544
545#define RB_NEXT(ptr, member) \
546 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
547
548#define RB_PREV(ptr, member) \
549 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
550
551/* Does linear interpolation between powers of two */
552static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
553{
554 unsigned fract = x & ~(~0 << fract_bits);
555
556 x >>= fract_bits;
557 x = 1 << x;
558 x += (x * fract) >> fract_bits;
559
560 return x;
561}
562
169ef1cf 563void bch_bio_map(struct bio *bio, void *base);
cafe5635 564
cafe5635
KO
565static inline sector_t bdev_sectors(struct block_device *bdev)
566{
567 return bdev->bd_inode->i_size >> 9;
568}
569
570#define closure_bio_submit(bio, cl, dev) \
571do { \
572 closure_get(cl); \
573 bch_generic_make_request(bio, &(dev)->bio_split_hook); \
574} while (0)
575
169ef1cf
KO
576uint64_t bch_crc64_update(uint64_t, const void *, size_t);
577uint64_t bch_crc64(const void *, size_t);
cafe5635
KO
578
579#endif /* _BCACHE_UTIL_H */
This page took 0.076989 seconds and 5 git commands to generate.