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