util.h 15 KB

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