util.h 15 KB

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