1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_UTIL_H 3 #define _BCACHEFS_UTIL_H 4 5 #include <linux/bio.h> 6 #include <linux/blkdev.h> 7 #include <linux/closure.h> 8 #include <linux/errno.h> 9 #include <linux/freezer.h> 10 #include <linux/kernel.h> 11 #include <linux/sched/clock.h> 12 #include <linux/llist.h> 13 #include <linux/log2.h> 14 #include <linux/percpu.h> 15 #include <linux/preempt.h> 16 #include <linux/ratelimit.h> 17 #include <linux/slab.h> 18 #include <linux/vmalloc.h> 19 #include <linux/workqueue.h> 20 21 #include "mean_and_variance.h" 22 23 #include "darray.h" 24 #include "time_stats.h" 25 26 struct closure; 27 28 #ifdef CONFIG_BCACHEFS_DEBUG 29 #define EBUG_ON(cond) BUG_ON(cond) 30 #else 31 #define EBUG_ON(cond) 32 #endif 33 34 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 35 #define CPU_BIG_ENDIAN 0 36 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 37 #define CPU_BIG_ENDIAN 1 38 #endif 39 40 /* type hackery */ 41 42 #define type_is_exact(_val, _type) \ 43 __builtin_types_compatible_p(typeof(_val), _type) 44 45 #define type_is(_val, _type) \ 46 (__builtin_types_compatible_p(typeof(_val), _type) || \ 47 __builtin_types_compatible_p(typeof(_val), const _type)) 48 49 /* Userspace doesn't align allocations as nicely as the kernel allocators: */ 50 static inline size_t buf_pages(void *p, size_t len) 51 { 52 return DIV_ROUND_UP(len + 53 ((unsigned long) p & (PAGE_SIZE - 1)), 54 PAGE_SIZE); 55 } 56 57 #define HEAP(type) \ 58 struct { \ 59 size_t size, used; \ 60 type *data; \ 61 } 62 63 #define DECLARE_HEAP(type, name) HEAP(type) name 64 65 #define init_heap(heap, _size, gfp) \ 66 ({ \ 67 (heap)->used = 0; \ 68 (heap)->size = (_size); \ 69 (heap)->data = kvmalloc((heap)->size * sizeof((heap)->data[0]),\ 70 (gfp)); \ 71 }) 72 73 #define free_heap(heap) \ 74 do { \ 75 kvfree((heap)->data); \ 76 (heap)->data = NULL; \ 77 } while (0) 78 79 #define heap_set_backpointer(h, i, _fn) \ 80 do { \ 81 void (*fn)(typeof(h), size_t) = _fn; \ 82 if (fn) \ 83 fn(h, i); \ 84 } while (0) 85 86 #define heap_swap(h, i, j, set_backpointer) \ 87 do { \ 88 swap((h)->data[i], (h)->data[j]); \ 89 heap_set_backpointer(h, i, set_backpointer); \ 90 heap_set_backpointer(h, j, set_backpointer); \ 91 } while (0) 92 93 #define heap_peek(h) \ 94 ({ \ 95 EBUG_ON(!(h)->used); \ 96 (h)->data[0]; \ 97 }) 98 99 #define heap_full(h) ((h)->used == (h)->size) 100 101 #define heap_sift_down(h, i, cmp, set_backpointer) \ 102 do { \ 103 size_t _c, _j = i; \ 104 \ 105 for (; _j * 2 + 1 < (h)->used; _j = _c) { \ 106 _c = _j * 2 + 1; \ 107 if (_c + 1 < (h)->used && \ 108 cmp(h, (h)->data[_c], (h)->data[_c + 1]) >= 0) \ 109 _c++; \ 110 \ 111 if (cmp(h, (h)->data[_c], (h)->data[_j]) >= 0) \ 112 break; \ 113 heap_swap(h, _c, _j, set_backpointer); \ 114 } \ 115 } while (0) 116 117 #define heap_sift_up(h, i, cmp, set_backpointer) \ 118 do { \ 119 while (i) { \ 120 size_t p = (i - 1) / 2; \ 121 if (cmp(h, (h)->data[i], (h)->data[p]) >= 0) \ 122 break; \ 123 heap_swap(h, i, p, set_backpointer); \ 124 i = p; \ 125 } \ 126 } while (0) 127 128 #define __heap_add(h, d, cmp, set_backpointer) \ 129 ({ \ 130 size_t _i = (h)->used++; \ 131 (h)->data[_i] = d; \ 132 heap_set_backpointer(h, _i, set_backpointer); \ 133 \ 134 heap_sift_up(h, _i, cmp, set_backpointer); \ 135 _i; \ 136 }) 137 138 #define heap_add(h, d, cmp, set_backpointer) \ 139 ({ \ 140 bool _r = !heap_full(h); \ 141 if (_r) \ 142 __heap_add(h, d, cmp, set_backpointer); \ 143 _r; \ 144 }) 145 146 #define heap_add_or_replace(h, new, cmp, set_backpointer) \ 147 do { \ 148 if (!heap_add(h, new, cmp, set_backpointer) && \ 149 cmp(h, new, heap_peek(h)) >= 0) { \ 150 (h)->data[0] = new; \ 151 heap_set_backpointer(h, 0, set_backpointer); \ 152 heap_sift_down(h, 0, cmp, set_backpointer); \ 153 } \ 154 } while (0) 155 156 #define heap_del(h, i, cmp, set_backpointer) \ 157 do { \ 158 size_t _i = (i); \ 159 \ 160 BUG_ON(_i >= (h)->used); \ 161 (h)->used--; \ 162 if ((_i) < (h)->used) { \ 163 heap_swap(h, _i, (h)->used, set_backpointer); \ 164 heap_sift_up(h, _i, cmp, set_backpointer); \ 165 heap_sift_down(h, _i, cmp, set_backpointer); \ 166 } \ 167 } while (0) 168 169 #define heap_pop(h, d, cmp, set_backpointer) \ 170 ({ \ 171 bool _r = (h)->used; \ 172 if (_r) { \ 173 (d) = (h)->data[0]; \ 174 heap_del(h, 0, cmp, set_backpointer); \ 175 } \ 176 _r; \ 177 }) 178 179 #define heap_resort(heap, cmp, set_backpointer) \ 180 do { \ 181 ssize_t _i; \ 182 for (_i = (ssize_t) (heap)->used / 2 - 1; _i >= 0; --_i) \ 183 heap_sift_down(heap, _i, cmp, set_backpointer); \ 184 } while (0) 185 186 #define ANYSINT_MAX(t) \ 187 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1) 188 189 #include "printbuf.h" 190 191 #define prt_vprintf(_out, ...) bch2_prt_vprintf(_out, __VA_ARGS__) 192 #define prt_printf(_out, ...) bch2_prt_printf(_out, __VA_ARGS__) 193 #define printbuf_str(_buf) bch2_printbuf_str(_buf) 194 #define printbuf_exit(_buf) bch2_printbuf_exit(_buf) 195 196 #define printbuf_tabstops_reset(_buf) bch2_printbuf_tabstops_reset(_buf) 197 #define printbuf_tabstop_pop(_buf) bch2_printbuf_tabstop_pop(_buf) 198 #define printbuf_tabstop_push(_buf, _n) bch2_printbuf_tabstop_push(_buf, _n) 199 200 #define printbuf_indent_add(_out, _n) bch2_printbuf_indent_add(_out, _n) 201 #define printbuf_indent_sub(_out, _n) bch2_printbuf_indent_sub(_out, _n) 202 203 #define prt_newline(_out) bch2_prt_newline(_out) 204 #define prt_tab(_out) bch2_prt_tab(_out) 205 #define prt_tab_rjust(_out) bch2_prt_tab_rjust(_out) 206 207 #define prt_bytes_indented(...) bch2_prt_bytes_indented(__VA_ARGS__) 208 #define prt_u64(_out, _v) prt_printf(_out, "%llu", (u64) (_v)) 209 #define prt_human_readable_u64(...) bch2_prt_human_readable_u64(__VA_ARGS__) 210 #define prt_human_readable_s64(...) bch2_prt_human_readable_s64(__VA_ARGS__) 211 #define prt_units_u64(...) bch2_prt_units_u64(__VA_ARGS__) 212 #define prt_units_s64(...) bch2_prt_units_s64(__VA_ARGS__) 213 #define prt_string_option(...) bch2_prt_string_option(__VA_ARGS__) 214 #define prt_bitflags(...) bch2_prt_bitflags(__VA_ARGS__) 215 #define prt_bitflags_vector(...) bch2_prt_bitflags_vector(__VA_ARGS__) 216 217 void bch2_pr_time_units(struct printbuf *, u64); 218 void bch2_prt_datetime(struct printbuf *, time64_t); 219 220 #ifdef __KERNEL__ 221 static inline void uuid_unparse_lower(u8 *uuid, char *out) 222 { 223 sprintf(out, "%pUb", uuid); 224 } 225 #else 226 #include <uuid/uuid.h> 227 #endif 228 229 static inline void pr_uuid(struct printbuf *out, u8 *uuid) 230 { 231 char uuid_str[40]; 232 233 uuid_unparse_lower(uuid, uuid_str); 234 prt_printf(out, "%s", uuid_str); 235 } 236 237 int bch2_strtoint_h(const char *, int *); 238 int bch2_strtouint_h(const char *, unsigned int *); 239 int bch2_strtoll_h(const char *, long long *); 240 int bch2_strtoull_h(const char *, unsigned long long *); 241 int bch2_strtou64_h(const char *, u64 *); 242 243 static inline int bch2_strtol_h(const char *cp, long *res) 244 { 245 #if BITS_PER_LONG == 32 246 return bch2_strtoint_h(cp, (int *) res); 247 #else 248 return bch2_strtoll_h(cp, (long long *) res); 249 #endif 250 } 251 252 static inline int bch2_strtoul_h(const char *cp, long *res) 253 { 254 #if BITS_PER_LONG == 32 255 return bch2_strtouint_h(cp, (unsigned int *) res); 256 #else 257 return bch2_strtoull_h(cp, (unsigned long long *) res); 258 #endif 259 } 260 261 #define strtoi_h(cp, res) \ 262 ( type_is(*res, int) ? bch2_strtoint_h(cp, (void *) res)\ 263 : type_is(*res, long) ? bch2_strtol_h(cp, (void *) res)\ 264 : type_is(*res, long long) ? bch2_strtoll_h(cp, (void *) res)\ 265 : type_is(*res, unsigned) ? bch2_strtouint_h(cp, (void *) res)\ 266 : type_is(*res, unsigned long) ? bch2_strtoul_h(cp, (void *) res)\ 267 : type_is(*res, unsigned long long) ? bch2_strtoull_h(cp, (void *) res)\ 268 : -EINVAL) 269 270 #define strtoul_safe(cp, var) \ 271 ({ \ 272 unsigned long _v; \ 273 int _r = kstrtoul(cp, 10, &_v); \ 274 if (!_r) \ 275 var = _v; \ 276 _r; \ 277 }) 278 279 #define strtoul_safe_clamp(cp, var, min, max) \ 280 ({ \ 281 unsigned long _v; \ 282 int _r = kstrtoul(cp, 10, &_v); \ 283 if (!_r) \ 284 var = clamp_t(typeof(var), _v, min, max); \ 285 _r; \ 286 }) 287 288 #define strtoul_safe_restrict(cp, var, min, max) \ 289 ({ \ 290 unsigned long _v; \ 291 int _r = kstrtoul(cp, 10, &_v); \ 292 if (!_r && _v >= min && _v <= max) \ 293 var = _v; \ 294 else \ 295 _r = -EINVAL; \ 296 _r; \ 297 }) 298 299 #define snprint(out, var) \ 300 prt_printf(out, \ 301 type_is(var, int) ? "%i\n" \ 302 : type_is(var, unsigned) ? "%u\n" \ 303 : type_is(var, long) ? "%li\n" \ 304 : type_is(var, unsigned long) ? "%lu\n" \ 305 : type_is(var, s64) ? "%lli\n" \ 306 : type_is(var, u64) ? "%llu\n" \ 307 : type_is(var, char *) ? "%s\n" \ 308 : "%i\n", var) 309 310 bool bch2_is_zero(const void *, size_t); 311 312 u64 bch2_read_flag_list(char *, const char * const[]); 313 314 void bch2_prt_u64_base2_nbits(struct printbuf *, u64, unsigned); 315 void bch2_prt_u64_base2(struct printbuf *, u64); 316 317 void bch2_print_string_as_lines(const char *prefix, const char *lines); 318 void bch2_print_string_as_lines_nonblocking(const char *prefix, const char *lines); 319 320 typedef DARRAY(unsigned long) bch_stacktrace; 321 int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *, unsigned, gfp_t); 322 void bch2_prt_backtrace(struct printbuf *, bch_stacktrace *); 323 int bch2_prt_task_backtrace(struct printbuf *, struct task_struct *, unsigned, gfp_t); 324 325 static inline void prt_bdevname(struct printbuf *out, struct block_device *bdev) 326 { 327 #ifdef __KERNEL__ 328 prt_printf(out, "%pg", bdev); 329 #else 330 prt_str(out, bdev->name); 331 #endif 332 } 333 334 void bch2_time_stats_to_text(struct printbuf *, struct bch2_time_stats *); 335 336 #define ewma_add(ewma, val, weight) \ 337 ({ \ 338 typeof(ewma) _ewma = (ewma); \ 339 typeof(weight) _weight = (weight); \ 340 \ 341 (((_ewma << _weight) - _ewma) + (val)) >> _weight; \ 342 }) 343 344 struct bch_ratelimit { 345 /* Next time we want to do some work, in nanoseconds */ 346 u64 next; 347 348 /* 349 * Rate at which we want to do work, in units per nanosecond 350 * The units here correspond to the units passed to 351 * bch2_ratelimit_increment() 352 */ 353 unsigned rate; 354 }; 355 356 static inline void bch2_ratelimit_reset(struct bch_ratelimit *d) 357 { 358 d->next = local_clock(); 359 } 360 361 u64 bch2_ratelimit_delay(struct bch_ratelimit *); 362 void bch2_ratelimit_increment(struct bch_ratelimit *, u64); 363 364 struct bch_pd_controller { 365 struct bch_ratelimit rate; 366 unsigned long last_update; 367 368 s64 last_actual; 369 s64 smoothed_derivative; 370 371 unsigned p_term_inverse; 372 unsigned d_smooth; 373 unsigned d_term; 374 375 /* for exporting to sysfs (no effect on behavior) */ 376 s64 last_derivative; 377 s64 last_proportional; 378 s64 last_change; 379 s64 last_target; 380 381 /* 382 * If true, the rate will not increase if bch2_ratelimit_delay() 383 * is not being called often enough. 384 */ 385 bool backpressure; 386 }; 387 388 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int); 389 void bch2_pd_controller_init(struct bch_pd_controller *); 390 void bch2_pd_controller_debug_to_text(struct printbuf *, struct bch_pd_controller *); 391 392 #define sysfs_pd_controller_attribute(name) \ 393 rw_attribute(name##_rate); \ 394 rw_attribute(name##_rate_bytes); \ 395 rw_attribute(name##_rate_d_term); \ 396 rw_attribute(name##_rate_p_term_inverse); \ 397 read_attribute(name##_rate_debug) 398 399 #define sysfs_pd_controller_files(name) \ 400 &sysfs_##name##_rate, \ 401 &sysfs_##name##_rate_bytes, \ 402 &sysfs_##name##_rate_d_term, \ 403 &sysfs_##name##_rate_p_term_inverse, \ 404 &sysfs_##name##_rate_debug 405 406 #define sysfs_pd_controller_show(name, var) \ 407 do { \ 408 sysfs_hprint(name##_rate, (var)->rate.rate); \ 409 sysfs_print(name##_rate_bytes, (var)->rate.rate); \ 410 sysfs_print(name##_rate_d_term, (var)->d_term); \ 411 sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \ 412 \ 413 if (attr == &sysfs_##name##_rate_debug) \ 414 bch2_pd_controller_debug_to_text(out, var); \ 415 } while (0) 416 417 #define sysfs_pd_controller_store(name, var) \ 418 do { \ 419 sysfs_strtoul_clamp(name##_rate, \ 420 (var)->rate.rate, 1, UINT_MAX); \ 421 sysfs_strtoul_clamp(name##_rate_bytes, \ 422 (var)->rate.rate, 1, UINT_MAX); \ 423 sysfs_strtoul(name##_rate_d_term, (var)->d_term); \ 424 sysfs_strtoul_clamp(name##_rate_p_term_inverse, \ 425 (var)->p_term_inverse, 1, INT_MAX); \ 426 } while (0) 427 428 #define container_of_or_null(ptr, type, member) \ 429 ({ \ 430 typeof(ptr) _ptr = ptr; \ 431 _ptr ? container_of(_ptr, type, member) : NULL; \ 432 }) 433 434 /* Does linear interpolation between powers of two */ 435 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits) 436 { 437 unsigned fract = x & ~(~0 << fract_bits); 438 439 x >>= fract_bits; 440 x = 1 << x; 441 x += (x * fract) >> fract_bits; 442 443 return x; 444 } 445 446 void bch2_bio_map(struct bio *bio, void *base, size_t); 447 int bch2_bio_alloc_pages(struct bio *, size_t, gfp_t); 448 449 #define closure_bio_submit(bio, cl) \ 450 do { \ 451 closure_get(cl); \ 452 submit_bio(bio); \ 453 } while (0) 454 455 #define kthread_wait(cond) \ 456 ({ \ 457 int _ret = 0; \ 458 \ 459 while (1) { \ 460 set_current_state(TASK_INTERRUPTIBLE); \ 461 if (kthread_should_stop()) { \ 462 _ret = -1; \ 463 break; \ 464 } \ 465 \ 466 if (cond) \ 467 break; \ 468 \ 469 schedule(); \ 470 } \ 471 set_current_state(TASK_RUNNING); \ 472 _ret; \ 473 }) 474 475 #define kthread_wait_freezable(cond) \ 476 ({ \ 477 int _ret = 0; \ 478 while (1) { \ 479 set_current_state(TASK_INTERRUPTIBLE); \ 480 if (kthread_should_stop()) { \ 481 _ret = -1; \ 482 break; \ 483 } \ 484 \ 485 if (cond) \ 486 break; \ 487 \ 488 schedule(); \ 489 try_to_freeze(); \ 490 } \ 491 set_current_state(TASK_RUNNING); \ 492 _ret; \ 493 }) 494 495 size_t bch2_rand_range(size_t); 496 497 void memcpy_to_bio(struct bio *, struct bvec_iter, const void *); 498 void memcpy_from_bio(void *, struct bio *, struct bvec_iter); 499 500 static inline void memcpy_u64s_small(void *dst, const void *src, 501 unsigned u64s) 502 { 503 u64 *d = dst; 504 const u64 *s = src; 505 506 while (u64s--) 507 *d++ = *s++; 508 } 509 510 static inline void __memcpy_u64s(void *dst, const void *src, 511 unsigned u64s) 512 { 513 #ifdef CONFIG_X86_64 514 long d0, d1, d2; 515 516 asm volatile("rep ; movsq" 517 : "=&c" (d0), "=&D" (d1), "=&S" (d2) 518 : "0" (u64s), "1" (dst), "2" (src) 519 : "memory"); 520 #else 521 u64 *d = dst; 522 const u64 *s = src; 523 524 while (u64s--) 525 *d++ = *s++; 526 #endif 527 } 528 529 static inline void memcpy_u64s(void *dst, const void *src, 530 unsigned u64s) 531 { 532 EBUG_ON(!(dst >= src + u64s * sizeof(u64) || 533 dst + u64s * sizeof(u64) <= src)); 534 535 __memcpy_u64s(dst, src, u64s); 536 } 537 538 static inline void __memmove_u64s_down(void *dst, const void *src, 539 unsigned u64s) 540 { 541 __memcpy_u64s(dst, src, u64s); 542 } 543 544 static inline void memmove_u64s_down(void *dst, const void *src, 545 unsigned u64s) 546 { 547 EBUG_ON(dst > src); 548 549 __memmove_u64s_down(dst, src, u64s); 550 } 551 552 static inline void __memmove_u64s_down_small(void *dst, const void *src, 553 unsigned u64s) 554 { 555 memcpy_u64s_small(dst, src, u64s); 556 } 557 558 static inline void memmove_u64s_down_small(void *dst, const void *src, 559 unsigned u64s) 560 { 561 EBUG_ON(dst > src); 562 563 __memmove_u64s_down_small(dst, src, u64s); 564 } 565 566 static inline void __memmove_u64s_up_small(void *_dst, const void *_src, 567 unsigned u64s) 568 { 569 u64 *dst = (u64 *) _dst + u64s; 570 u64 *src = (u64 *) _src + u64s; 571 572 while (u64s--) 573 *--dst = *--src; 574 } 575 576 static inline void memmove_u64s_up_small(void *dst, const void *src, 577 unsigned u64s) 578 { 579 EBUG_ON(dst < src); 580 581 __memmove_u64s_up_small(dst, src, u64s); 582 } 583 584 static inline void __memmove_u64s_up(void *_dst, const void *_src, 585 unsigned u64s) 586 { 587 u64 *dst = (u64 *) _dst + u64s - 1; 588 u64 *src = (u64 *) _src + u64s - 1; 589 590 #ifdef CONFIG_X86_64 591 long d0, d1, d2; 592 593 asm volatile("std ;\n" 594 "rep ; movsq\n" 595 "cld ;\n" 596 : "=&c" (d0), "=&D" (d1), "=&S" (d2) 597 : "0" (u64s), "1" (dst), "2" (src) 598 : "memory"); 599 #else 600 while (u64s--) 601 *dst-- = *src--; 602 #endif 603 } 604 605 static inline void memmove_u64s_up(void *dst, const void *src, 606 unsigned u64s) 607 { 608 EBUG_ON(dst < src); 609 610 __memmove_u64s_up(dst, src, u64s); 611 } 612 613 static inline void memmove_u64s(void *dst, const void *src, 614 unsigned u64s) 615 { 616 if (dst < src) 617 __memmove_u64s_down(dst, src, u64s); 618 else 619 __memmove_u64s_up(dst, src, u64s); 620 } 621 622 /* Set the last few bytes up to a u64 boundary given an offset into a buffer. */ 623 static inline void memset_u64s_tail(void *s, int c, unsigned bytes) 624 { 625 unsigned rem = round_up(bytes, sizeof(u64)) - bytes; 626 627 memset(s + bytes, c, rem); 628 } 629 630 /* just the memmove, doesn't update @_nr */ 631 #define __array_insert_item(_array, _nr, _pos) \ 632 memmove(&(_array)[(_pos) + 1], \ 633 &(_array)[(_pos)], \ 634 sizeof((_array)[0]) * ((_nr) - (_pos))) 635 636 #define array_insert_item(_array, _nr, _pos, _new_item) \ 637 do { \ 638 __array_insert_item(_array, _nr, _pos); \ 639 (_nr)++; \ 640 (_array)[(_pos)] = (_new_item); \ 641 } while (0) 642 643 #define array_remove_items(_array, _nr, _pos, _nr_to_remove) \ 644 do { \ 645 (_nr) -= (_nr_to_remove); \ 646 memmove(&(_array)[(_pos)], \ 647 &(_array)[(_pos) + (_nr_to_remove)], \ 648 sizeof((_array)[0]) * ((_nr) - (_pos))); \ 649 } while (0) 650 651 #define array_remove_item(_array, _nr, _pos) \ 652 array_remove_items(_array, _nr, _pos, 1) 653 654 static inline void __move_gap(void *array, size_t element_size, 655 size_t nr, size_t size, 656 size_t old_gap, size_t new_gap) 657 { 658 size_t gap_end = old_gap + size - nr; 659 660 if (new_gap < old_gap) { 661 size_t move = old_gap - new_gap; 662 663 memmove(array + element_size * (gap_end - move), 664 array + element_size * (old_gap - move), 665 element_size * move); 666 } else if (new_gap > old_gap) { 667 size_t move = new_gap - old_gap; 668 669 memmove(array + element_size * old_gap, 670 array + element_size * gap_end, 671 element_size * move); 672 } 673 } 674 675 /* Move the gap in a gap buffer: */ 676 #define move_gap(_d, _new_gap) \ 677 do { \ 678 BUG_ON(_new_gap > (_d)->nr); \ 679 BUG_ON((_d)->gap > (_d)->nr); \ 680 \ 681 __move_gap((_d)->data, sizeof((_d)->data[0]), \ 682 (_d)->nr, (_d)->size, (_d)->gap, _new_gap); \ 683 (_d)->gap = _new_gap; \ 684 } while (0) 685 686 #define bubble_sort(_base, _nr, _cmp) \ 687 do { \ 688 ssize_t _i, _last; \ 689 bool _swapped = true; \ 690 \ 691 for (_last= (ssize_t) (_nr) - 1; _last > 0 && _swapped; --_last) {\ 692 _swapped = false; \ 693 for (_i = 0; _i < _last; _i++) \ 694 if (_cmp((_base)[_i], (_base)[_i + 1]) > 0) { \ 695 swap((_base)[_i], (_base)[_i + 1]); \ 696 _swapped = true; \ 697 } \ 698 } \ 699 } while (0) 700 701 static inline u64 percpu_u64_get(u64 __percpu *src) 702 { 703 u64 ret = 0; 704 int cpu; 705 706 for_each_possible_cpu(cpu) 707 ret += *per_cpu_ptr(src, cpu); 708 return ret; 709 } 710 711 static inline void percpu_u64_set(u64 __percpu *dst, u64 src) 712 { 713 int cpu; 714 715 for_each_possible_cpu(cpu) 716 *per_cpu_ptr(dst, cpu) = 0; 717 this_cpu_write(*dst, src); 718 } 719 720 static inline void acc_u64s(u64 *acc, const u64 *src, unsigned nr) 721 { 722 unsigned i; 723 724 for (i = 0; i < nr; i++) 725 acc[i] += src[i]; 726 } 727 728 static inline void acc_u64s_percpu(u64 *acc, const u64 __percpu *src, 729 unsigned nr) 730 { 731 int cpu; 732 733 for_each_possible_cpu(cpu) 734 acc_u64s(acc, per_cpu_ptr(src, cpu), nr); 735 } 736 737 static inline void percpu_memset(void __percpu *p, int c, size_t bytes) 738 { 739 int cpu; 740 741 for_each_possible_cpu(cpu) 742 memset(per_cpu_ptr(p, cpu), c, bytes); 743 } 744 745 u64 *bch2_acc_percpu_u64s(u64 __percpu *, unsigned); 746 747 #define cmp_int(l, r) ((l > r) - (l < r)) 748 749 static inline int u8_cmp(u8 l, u8 r) 750 { 751 return cmp_int(l, r); 752 } 753 754 static inline int cmp_le32(__le32 l, __le32 r) 755 { 756 return cmp_int(le32_to_cpu(l), le32_to_cpu(r)); 757 } 758 759 #include <linux/uuid.h> 760 761 #define QSTR(n) { { { .len = strlen(n) } }, .name = n } 762 763 static inline bool qstr_eq(const struct qstr l, const struct qstr r) 764 { 765 return l.len == r.len && !memcmp(l.name, r.name, l.len); 766 } 767 768 void bch2_darray_str_exit(darray_str *); 769 int bch2_split_devs(const char *, darray_str *); 770 771 #ifdef __KERNEL__ 772 773 __must_check 774 static inline int copy_to_user_errcode(void __user *to, const void *from, unsigned long n) 775 { 776 return copy_to_user(to, from, n) ? -EFAULT : 0; 777 } 778 779 __must_check 780 static inline int copy_from_user_errcode(void *to, const void __user *from, unsigned long n) 781 { 782 return copy_from_user(to, from, n) ? -EFAULT : 0; 783 } 784 785 #endif 786 787 static inline void mod_bit(long nr, volatile unsigned long *addr, bool v) 788 { 789 if (v) 790 set_bit(nr, addr); 791 else 792 clear_bit(nr, addr); 793 } 794 795 static inline void __set_bit_le64(size_t bit, __le64 *addr) 796 { 797 addr[bit / 64] |= cpu_to_le64(BIT_ULL(bit % 64)); 798 } 799 800 static inline void __clear_bit_le64(size_t bit, __le64 *addr) 801 { 802 addr[bit / 64] &= ~cpu_to_le64(BIT_ULL(bit % 64)); 803 } 804 805 static inline bool test_bit_le64(size_t bit, __le64 *addr) 806 { 807 return (addr[bit / 64] & cpu_to_le64(BIT_ULL(bit % 64))) != 0; 808 } 809 810 #endif /* _BCACHEFS_UTIL_H */ 811