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