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