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