util.h 15 KB

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