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