]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / libbcachefs / util.h
index 433ba9c14dcbdc7f45bc4b68535fd4db81ea5188..c75fc31915d3936d8c0a26949915534aac482b3a 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _BCACHEFS_UTIL_H
 #define _BCACHEFS_UTIL_H
 
 #include <linux/sched/clock.h>
 #include <linux/llist.h>
 #include <linux/log2.h>
+#include <linux/percpu.h>
+#include <linux/preempt.h>
 #include <linux/ratelimit.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
 
-#define PAGE_SECTOR_SHIFT      (PAGE_SHIFT - 9)
-#define PAGE_SECTORS           (1UL << PAGE_SECTOR_SHIFT)
+#include "mean_and_variance.h"
+
+#include "darray.h"
 
 struct closure;
 
 #ifdef CONFIG_BCACHEFS_DEBUG
-
 #define EBUG_ON(cond)          BUG_ON(cond)
-#define atomic_dec_bug(v)      BUG_ON(atomic_dec_return(v) < 0)
-#define atomic_inc_bug(v, i)   BUG_ON(atomic_inc_return(v) <= i)
-#define atomic_sub_bug(i, v)   BUG_ON(atomic_sub_return(i, v) < 0)
-#define atomic_add_bug(i, v)   BUG_ON(atomic_add_return(i, v) < 0)
-#define atomic_long_dec_bug(v)         BUG_ON(atomic_long_dec_return(v) < 0)
-#define atomic_long_sub_bug(i, v)      BUG_ON(atomic_long_sub_return(i, v) < 0)
-#define atomic64_dec_bug(v)    BUG_ON(atomic64_dec_return(v) < 0)
-#define atomic64_inc_bug(v, i) BUG_ON(atomic64_inc_return(v) <= i)
-#define atomic64_sub_bug(i, v) BUG_ON(atomic64_sub_return(i, v) < 0)
-#define atomic64_add_bug(i, v) BUG_ON(atomic64_add_return(i, v) < 0)
-
-#define memcpy(dst, src, len)                                          \
-({                                                                     \
-       void *_dst = (dst);                                             \
-       const void *_src = (src);                                       \
-       size_t _len = (len);                                            \
-                                                                       \
-       BUG_ON(!((void *) (_dst) >= (void *) (_src) + (_len) ||         \
-                (void *) (_dst) + (_len) <= (void *) (_src)));         \
-       memcpy(_dst, _src, _len);                                       \
-})
-
-#else /* DEBUG */
-
-#define EBUG_ON(cond)
-#define atomic_dec_bug(v)      atomic_dec(v)
-#define atomic_inc_bug(v, i)   atomic_inc(v)
-#define atomic_sub_bug(i, v)   atomic_sub(i, v)
-#define atomic_add_bug(i, v)   atomic_add(i, v)
-#define atomic_long_dec_bug(v)         atomic_long_dec(v)
-#define atomic_long_sub_bug(i, v)      atomic_long_sub(i, v)
-#define atomic64_dec_bug(v)    atomic64_dec(v)
-#define atomic64_inc_bug(v, i) atomic64_inc(v)
-#define atomic64_sub_bug(i, v) atomic64_sub(i, v)
-#define atomic64_add_bug(i, v) atomic64_add(i, v)
-
-#endif
-
-#ifndef __CHECKER__
-#define __flatten __attribute__((flatten))
 #else
-/* sparse doesn't know about attribute((flatten)) */
-#define __flatten
+#define EBUG_ON(cond)
 #endif
 
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@@ -103,7 +65,7 @@ static inline void *vpmalloc(size_t size, gfp_t gfp_mask)
 {
        return (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
                                         get_order(size)) ?:
-               __vmalloc(size, gfp_mask, PAGE_KERNEL);
+               __vmalloc(size, gfp_mask);
 }
 
 static inline void kvpfree(void *p, size_t size)
@@ -228,9 +190,11 @@ do {                                                                       \
                                                                        \
        BUG_ON(_i >= (h)->used);                                        \
        (h)->used--;                                                    \
-       heap_swap(h, _i, (h)->used, set_backpointer);                   \
-       heap_sift_up(h, _i, cmp, set_backpointer);                      \
-       heap_sift_down(h, _i, cmp, set_backpointer);                    \
+       if ((_i) < (h)->used) {                                         \
+               heap_swap(h, _i, (h)->used, set_backpointer);           \
+               heap_sift_up(h, _i, cmp, set_backpointer);              \
+               heap_sift_down(h, _i, cmp, set_backpointer);            \
+       }                                                               \
 } while (0)
 
 #define heap_pop(h, d, cmp, set_backpointer)                           \
@@ -253,10 +217,59 @@ do {                                                                      \
 #define ANYSINT_MAX(t)                                                 \
        ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
 
+#include "printbuf.h"
+
+#define prt_vprintf(_out, ...)         bch2_prt_vprintf(_out, __VA_ARGS__)
+#define prt_printf(_out, ...)          bch2_prt_printf(_out, __VA_ARGS__)
+#define printbuf_str(_buf)             bch2_printbuf_str(_buf)
+#define printbuf_exit(_buf)            bch2_printbuf_exit(_buf)
+
+#define printbuf_tabstops_reset(_buf)  bch2_printbuf_tabstops_reset(_buf)
+#define printbuf_tabstop_pop(_buf)     bch2_printbuf_tabstop_pop(_buf)
+#define printbuf_tabstop_push(_buf, _n)        bch2_printbuf_tabstop_push(_buf, _n)
+
+#define printbuf_indent_add(_out, _n)  bch2_printbuf_indent_add(_out, _n)
+#define printbuf_indent_sub(_out, _n)  bch2_printbuf_indent_sub(_out, _n)
+
+#define prt_newline(_out)              bch2_prt_newline(_out)
+#define prt_tab(_out)                  bch2_prt_tab(_out)
+#define prt_tab_rjust(_out)            bch2_prt_tab_rjust(_out)
+
+#define prt_bytes_indented(...)                bch2_prt_bytes_indented(__VA_ARGS__)
+#define prt_u64(_out, _v)              prt_printf(_out, "%llu", (u64) (_v))
+#define prt_human_readable_u64(...)    bch2_prt_human_readable_u64(__VA_ARGS__)
+#define prt_human_readable_s64(...)    bch2_prt_human_readable_s64(__VA_ARGS__)
+#define prt_units_u64(...)             bch2_prt_units_u64(__VA_ARGS__)
+#define prt_units_s64(...)             bch2_prt_units_s64(__VA_ARGS__)
+#define prt_string_option(...)         bch2_prt_string_option(__VA_ARGS__)
+#define prt_bitflags(...)              bch2_prt_bitflags(__VA_ARGS__)
+#define prt_bitflags_vector(...)       bch2_prt_bitflags_vector(__VA_ARGS__)
+
+void bch2_pr_time_units(struct printbuf *, u64);
+void bch2_prt_datetime(struct printbuf *, time64_t);
+
+#ifdef __KERNEL__
+static inline void uuid_unparse_lower(u8 *uuid, char *out)
+{
+       sprintf(out, "%pUb", uuid);
+}
+#else
+#include <uuid/uuid.h>
+#endif
+
+static inline void pr_uuid(struct printbuf *out, u8 *uuid)
+{
+       char uuid_str[40];
+
+       uuid_unparse_lower(uuid, uuid_str);
+       prt_printf(out, "%s", uuid_str);
+}
+
 int bch2_strtoint_h(const char *, int *);
 int bch2_strtouint_h(const char *, unsigned int *);
 int bch2_strtoll_h(const char *, long long *);
 int bch2_strtoull_h(const char *, unsigned long long *);
+int bch2_strtou64_h(const char *, u64 *);
 
 static inline int bch2_strtol_h(const char *cp, long *res)
 {
@@ -314,8 +327,8 @@ static inline int bch2_strtoul_h(const char *cp, long *res)
        _r;                                                             \
 })
 
-#define snprint(buf, size, var)                                                \
-       snprintf(buf, size,                                             \
+#define snprint(out, var)                                              \
+       prt_printf(out,                                                 \
                   type_is(var, int)            ? "%i\n"                \
                 : type_is(var, unsigned)       ? "%u\n"                \
                 : type_is(var, long)           ? "%li\n"               \
@@ -325,59 +338,105 @@ static inline int bch2_strtoul_h(const char *cp, long *res)
                 : type_is(var, char *)         ? "%s\n"                \
                 : "%i\n", var)
 
-ssize_t bch2_hprint(char *buf, s64 v);
-
 bool bch2_is_zero(const void *, size_t);
 
-ssize_t bch2_scnprint_string_list(char *, size_t, const char * const[], size_t);
-
-ssize_t bch2_scnprint_flag_list(char *, size_t, const char * const[], u64);
 u64 bch2_read_flag_list(char *, const char * const[]);
 
+void bch2_prt_u64_binary(struct printbuf *, u64, unsigned);
+
+void bch2_print_string_as_lines(const char *prefix, const char *lines);
+
+typedef DARRAY(unsigned long) bch_stacktrace;
+int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *, unsigned);
+void bch2_prt_backtrace(struct printbuf *, bch_stacktrace *);
+int bch2_prt_task_backtrace(struct printbuf *, struct task_struct *, unsigned);
+
+static inline void prt_bdevname(struct printbuf *out, struct block_device *bdev)
+{
+#ifdef __KERNEL__
+       prt_printf(out, "%pg", bdev);
+#else
+       prt_str(out, bdev->name);
+#endif
+}
+
 #define NR_QUANTILES   15
 #define QUANTILE_IDX(i)        inorder_to_eytzinger0(i, NR_QUANTILES)
 #define QUANTILE_FIRST eytzinger0_first(NR_QUANTILES)
 #define QUANTILE_LAST  eytzinger0_last(NR_QUANTILES)
 
-struct quantiles {
-       struct quantile_entry {
+struct bch2_quantiles {
+       struct bch2_quantile_entry {
                u64     m;
                u64     step;
        }               entries[NR_QUANTILES];
 };
 
-struct time_stat_buffer {
+struct bch2_time_stat_buffer {
        unsigned        nr;
-       struct time_stat_buffer_entry {
+       struct bch2_time_stat_buffer_entry {
                u64     start;
                u64     end;
        }               entries[32];
 };
 
-struct time_stats {
+struct bch2_time_stats {
        spinlock_t      lock;
-       u64             count;
        /* all fields are in nanoseconds */
-       u64             average_duration;
-       u64             average_frequency;
+       u64             min_duration;
        u64             max_duration;
+       u64             total_duration;
+       u64             max_freq;
+       u64             min_freq;
        u64             last_event;
-       struct quantiles quantiles;
+       struct bch2_quantiles quantiles;
 
-       struct time_stat_buffer __percpu *buffer;
+       struct mean_and_variance          duration_stats;
+       struct mean_and_variance_weighted duration_stats_weighted;
+       struct mean_and_variance          freq_stats;
+       struct mean_and_variance_weighted freq_stats_weighted;
+       struct bch2_time_stat_buffer __percpu *buffer;
 };
 
-void __bch2_time_stats_update(struct time_stats *stats, u64, u64);
+#ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
+void __bch2_time_stats_update(struct bch2_time_stats *stats, u64, u64);
 
-static inline void bch2_time_stats_update(struct time_stats *stats, u64 start)
+static inline void bch2_time_stats_update(struct bch2_time_stats *stats, u64 start)
 {
        __bch2_time_stats_update(stats, start, local_clock());
 }
 
-size_t bch2_time_stats_print(struct time_stats *, char *, size_t);
+static inline bool track_event_change(struct bch2_time_stats *stats,
+                                     u64 *start, bool v)
+{
+       if (v != !!*start) {
+               if (!v) {
+                       bch2_time_stats_update(stats, *start);
+                       *start = 0;
+               } else {
+                       *start = local_clock() ?: 1;
+                       return true;
+               }
+       }
+
+       return false;
+}
+#else
+static inline void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end) {}
+static inline void bch2_time_stats_update(struct bch2_time_stats *stats, u64 start) {}
+static inline bool track_event_change(struct bch2_time_stats *stats,
+                                     u64 *start, bool v)
+{
+       bool ret = v && !*start;
+       *start = v;
+       return ret;
+}
+#endif
+
+void bch2_time_stats_to_text(struct printbuf *, struct bch2_time_stats *);
 
-void bch2_time_stats_exit(struct time_stats *);
-void bch2_time_stats_init(struct time_stats *);
+void bch2_time_stats_exit(struct bch2_time_stats *);
+void bch2_time_stats_init(struct bch2_time_stats *);
 
 #define ewma_add(ewma, val, weight)                                    \
 ({                                                                     \
@@ -424,14 +483,16 @@ struct bch_pd_controller {
        s64                     last_change;
        s64                     last_target;
 
-       /* If true, the rate will not increase if bch2_ratelimit_delay()
-        * is not being called often enough. */
+       /*
+        * If true, the rate will not increase if bch2_ratelimit_delay()
+        * is not being called often enough.
+        */
        bool                    backpressure;
 };
 
 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int);
 void bch2_pd_controller_init(struct bch_pd_controller *);
-size_t bch2_pd_controller_print_debug(struct bch_pd_controller *, char *);
+void bch2_pd_controller_debug_to_text(struct printbuf *, struct bch_pd_controller *);
 
 #define sysfs_pd_controller_attribute(name)                            \
        rw_attribute(name##_rate);                                      \
@@ -455,7 +516,7 @@ do {                                                                        \
        sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \
                                                                        \
        if (attr == &sysfs_##name##_rate_debug)                         \
-               return bch2_pd_controller_print_debug(var, buf);                \
+               bch2_pd_controller_debug_to_text(out, var);             \
 } while (0)
 
 #define sysfs_pd_controller_store(name, var)                           \
@@ -469,96 +530,12 @@ do {                                                                      \
                            (var)->p_term_inverse, 1, INT_MAX);         \
 } while (0)
 
-#define __DIV_SAFE(n, d, zero)                                         \
-({                                                                     \
-       typeof(n) _n = (n);                                             \
-       typeof(d) _d = (d);                                             \
-       _d ? _n / _d : zero;                                            \
-})
-
-#define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
-
 #define container_of_or_null(ptr, type, member)                                \
 ({                                                                     \
        typeof(ptr) _ptr = ptr;                                         \
        _ptr ? container_of(_ptr, type, member) : NULL;                 \
 })
 
-#define RB_INSERT(root, new, member, cmp)                              \
-({                                                                     \
-       __label__ dup;                                                  \
-       struct rb_node **n = &(root)->rb_node, *parent = NULL;          \
-       typeof(new) this;                                               \
-       int res, ret = -1;                                              \
-                                                                       \
-       while (*n) {                                                    \
-               parent = *n;                                            \
-               this = container_of(*n, typeof(*(new)), member);        \
-               res = cmp(new, this);                                   \
-               if (!res)                                               \
-                       goto dup;                                       \
-               n = res < 0                                             \
-                       ? &(*n)->rb_left                                \
-                       : &(*n)->rb_right;                              \
-       }                                                               \
-                                                                       \
-       rb_link_node(&(new)->member, parent, n);                        \
-       rb_insert_color(&(new)->member, root);                          \
-       ret = 0;                                                        \
-dup:                                                                   \
-       ret;                                                            \
-})
-
-#define RB_SEARCH(root, search, member, cmp)                           \
-({                                                                     \
-       struct rb_node *n = (root)->rb_node;                            \
-       typeof(&(search)) this, ret = NULL;                             \
-       int res;                                                        \
-                                                                       \
-       while (n) {                                                     \
-               this = container_of(n, typeof(search), member);         \
-               res = cmp(&(search), this);                             \
-               if (!res) {                                             \
-                       ret = this;                                     \
-                       break;                                          \
-               }                                                       \
-               n = res < 0                                             \
-                       ? n->rb_left                                    \
-                       : n->rb_right;                                  \
-       }                                                               \
-       ret;                                                            \
-})
-
-#define RB_GREATER(root, search, member, cmp)                          \
-({                                                                     \
-       struct rb_node *n = (root)->rb_node;                            \
-       typeof(&(search)) this, ret = NULL;                             \
-       int res;                                                        \
-                                                                       \
-       while (n) {                                                     \
-               this = container_of(n, typeof(search), member);         \
-               res = cmp(&(search), this);                             \
-               if (res < 0) {                                          \
-                       ret = this;                                     \
-                       n = n->rb_left;                                 \
-               } else                                                  \
-                       n = n->rb_right;                                \
-       }                                                               \
-       ret;                                                            \
-})
-
-#define RB_FIRST(root, type, member)                                   \
-       container_of_or_null(rb_first(root), type, member)
-
-#define RB_LAST(root, type, member)                                    \
-       container_of_or_null(rb_last(root), type, member)
-
-#define RB_NEXT(ptr, member)                                           \
-       container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
-
-#define RB_PREV(ptr, member)                                           \
-       container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
-
 /* Does linear interpolation between powers of two */
 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
 {
@@ -571,8 +548,8 @@ static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
        return x;
 }
 
-void bch2_bio_map(struct bio *bio, void *base);
-int bch2_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
+void bch2_bio_map(struct bio *bio, void *base, size_t);
+int bch2_bio_alloc_pages(struct bio *, size_t, gfp_t);
 
 static inline sector_t bdev_sectors(struct block_device *bdev)
 {
@@ -585,6 +562,26 @@ do {                                                                       \
        submit_bio(bio);                                                \
 } while (0)
 
+#define kthread_wait(cond)                                             \
+({                                                                     \
+       int _ret = 0;                                                   \
+                                                                       \
+       while (1) {                                                     \
+               set_current_state(TASK_INTERRUPTIBLE);                  \
+               if (kthread_should_stop()) {                            \
+                       _ret = -1;                                      \
+                       break;                                          \
+               }                                                       \
+                                                                       \
+               if (cond)                                               \
+                       break;                                          \
+                                                                       \
+               schedule();                                             \
+       }                                                               \
+       set_current_state(TASK_RUNNING);                                \
+       _ret;                                                           \
+})
+
 #define kthread_wait_freezable(cond)                                   \
 ({                                                                     \
        int _ret = 0;                                                   \
@@ -607,14 +604,25 @@ do {                                                                      \
 
 size_t bch2_rand_range(size_t);
 
-void memcpy_to_bio(struct bio *, struct bvec_iter, void *);
+void memcpy_to_bio(struct bio *, struct bvec_iter, const void *);
 void memcpy_from_bio(void *, struct bio *, struct bvec_iter);
 
+static inline void memcpy_u64s_small(void *dst, const void *src,
+                                    unsigned u64s)
+{
+       u64 *d = dst;
+       const u64 *s = src;
+
+       while (u64s--)
+               *d++ = *s++;
+}
+
 static inline void __memcpy_u64s(void *dst, const void *src,
                                 unsigned u64s)
 {
 #ifdef CONFIG_X86_64
        long d0, d1, d2;
+
        asm volatile("rep ; movsq"
                     : "=&c" (d0), "=&D" (d1), "=&S" (d2)
                     : "0" (u64s), "1" (dst), "2" (src)
@@ -651,6 +659,38 @@ static inline void memmove_u64s_down(void *dst, const void *src,
        __memmove_u64s_down(dst, src, u64s);
 }
 
+static inline void __memmove_u64s_down_small(void *dst, const void *src,
+                                      unsigned u64s)
+{
+       memcpy_u64s_small(dst, src, u64s);
+}
+
+static inline void memmove_u64s_down_small(void *dst, const void *src,
+                                    unsigned u64s)
+{
+       EBUG_ON(dst > src);
+
+       __memmove_u64s_down_small(dst, src, u64s);
+}
+
+static inline void __memmove_u64s_up_small(void *_dst, const void *_src,
+                                          unsigned u64s)
+{
+       u64 *dst = (u64 *) _dst + u64s;
+       u64 *src = (u64 *) _src + u64s;
+
+       while (u64s--)
+               *--dst = *--src;
+}
+
+static inline void memmove_u64s_up_small(void *dst, const void *src,
+                                        unsigned u64s)
+{
+       EBUG_ON(dst < src);
+
+       __memmove_u64s_up_small(dst, src, u64s);
+}
+
 static inline void __memmove_u64s_up(void *_dst, const void *_src,
                                     unsigned u64s)
 {
@@ -659,6 +699,7 @@ static inline void __memmove_u64s_up(void *_dst, const void *_src,
 
 #ifdef CONFIG_X86_64
        long d0, d1, d2;
+
        asm volatile("std ;\n"
                     "rep ; movsq\n"
                     "cld ;\n"
@@ -688,37 +729,14 @@ static inline void memmove_u64s(void *dst, const void *src,
                __memmove_u64s_up(dst, src, u64s);
 }
 
-static inline struct bio_vec next_contig_bvec(struct bio *bio,
-                                             struct bvec_iter *iter)
+/* Set the last few bytes up to a u64 boundary given an offset into a buffer. */
+static inline void memset_u64s_tail(void *s, int c, unsigned bytes)
 {
-       struct bio_vec bv = bio_iter_iovec(bio, *iter);
+       unsigned rem = round_up(bytes, sizeof(u64)) - bytes;
 
-       bio_advance_iter(bio, iter, bv.bv_len);
-#ifndef CONFIG_HIGHMEM
-       while (iter->bi_size) {
-               struct bio_vec next = bio_iter_iovec(bio, *iter);
-
-               if (page_address(bv.bv_page) + bv.bv_offset + bv.bv_len !=
-                   page_address(next.bv_page) + next.bv_offset)
-                       break;
-
-               bv.bv_len += next.bv_len;
-               bio_advance_iter(bio, iter, next.bv_len);
-       }
-#endif
-       return bv;
+       memset(s + bytes, c, rem);
 }
 
-#define __bio_for_each_contig_segment(bv, bio, iter, start)            \
-       for (iter = (start);                                            \
-            (iter).bi_size &&                                          \
-               ((bv = next_contig_bvec((bio), &(iter))), 1);)
-
-#define bio_for_each_contig_segment(bv, bio, iter)                     \
-       __bio_for_each_contig_segment(bv, bio, iter, (bio)->bi_iter)
-
-size_t bch_scnmemcpy(char *, size_t, const char *, size_t);
-
 void sort_cmp_size(void *base, size_t num, size_t size,
          int (*cmp_func)(const void *, const void *, size_t),
          void (*swap_func)(void *, void *, size_t));
@@ -747,14 +765,39 @@ do {                                                                      \
 #define array_remove_item(_array, _nr, _pos)                           \
        array_remove_items(_array, _nr, _pos, 1)
 
+static inline void __move_gap(void *array, size_t element_size,
+                             size_t nr, size_t size,
+                             size_t old_gap, size_t new_gap)
+{
+       size_t gap_end = old_gap + size - nr;
+
+       if (new_gap < old_gap) {
+               size_t move = old_gap - new_gap;
+
+               memmove(array + element_size * (gap_end - move),
+                       array + element_size * (old_gap - move),
+                               element_size * move);
+       } else if (new_gap > old_gap) {
+               size_t move = new_gap - old_gap;
+
+               memmove(array + element_size * old_gap,
+                       array + element_size * gap_end,
+                               element_size * move);
+       }
+}
+
+/* Move the gap in a gap buffer: */
+#define move_gap(_array, _nr, _size, _old_gap, _new_gap)       \
+       __move_gap(_array, sizeof(_array[0]), _nr, _size, _old_gap, _new_gap)
+
 #define bubble_sort(_base, _nr, _cmp)                                  \
 do {                                                                   \
-       ssize_t _i, _end;                                               \
+       ssize_t _i, _last;                                              \
        bool _swapped = true;                                           \
                                                                        \
-       for (_end = (ssize_t) (_nr) - 1; _end > 0 && _swapped; --_end) {\
+       for (_last= (ssize_t) (_nr) - 1; _last > 0 && _swapped; --_last) {\
                _swapped = false;                                       \
-               for (_i = 0; _i < _end; _i++)                           \
+               for (_i = 0; _i < _last; _i++)                          \
                        if (_cmp((_base)[_i], (_base)[_i + 1]) > 0) {   \
                                swap((_base)[_i], (_base)[_i + 1]);     \
                                _swapped = true;                        \
@@ -762,4 +805,74 @@ do {                                                                       \
        }                                                               \
 } while (0)
 
+static inline u64 percpu_u64_get(u64 __percpu *src)
+{
+       u64 ret = 0;
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               ret += *per_cpu_ptr(src, cpu);
+       return ret;
+}
+
+static inline void percpu_u64_set(u64 __percpu *dst, u64 src)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               *per_cpu_ptr(dst, cpu) = 0;
+       this_cpu_write(*dst, src);
+}
+
+static inline void acc_u64s(u64 *acc, const u64 *src, unsigned nr)
+{
+       unsigned i;
+
+       for (i = 0; i < nr; i++)
+               acc[i] += src[i];
+}
+
+static inline void acc_u64s_percpu(u64 *acc, const u64 __percpu *src,
+                                  unsigned nr)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               acc_u64s(acc, per_cpu_ptr(src, cpu), nr);
+}
+
+static inline void percpu_memset(void __percpu *p, int c, size_t bytes)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               memset(per_cpu_ptr(p, cpu), c, bytes);
+}
+
+u64 *bch2_acc_percpu_u64s(u64 __percpu *, unsigned);
+
+#define cmp_int(l, r)          ((l > r) - (l < r))
+
+static inline int u8_cmp(u8 l, u8 r)
+{
+       return cmp_int(l, r);
+}
+
+static inline int cmp_le32(__le32 l, __le32 r)
+{
+       return cmp_int(le32_to_cpu(l), le32_to_cpu(r));
+}
+
+#include <linux/uuid.h>
+
+#define QSTR(n) { { { .len = strlen(n) } }, .name = n }
+
+static inline bool qstr_eq(const struct qstr l, const struct qstr r)
+{
+       return l.len == r.len && !memcmp(l.name, r.name, l.len);
+}
+
+void bch2_darray_str_exit(darray_str *);
+int bch2_split_devs(const char *, darray_str *);
+
 #endif /* _BCACHEFS_UTIL_H */