util.h 15 KB

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