util.h 15 KB

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