util.h 15 KB

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