]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / libbcachefs / util.h
index e55407dc53249d1a93106cd9e4b37c9df74b523e..1b3aced8d83caf63f867553d87ab091fab807a54 100644 (file)
@@ -5,49 +5,28 @@
 #include <linux/bio.h>
 #include <linux/blkdev.h>
 #include <linux/closure.h>
+#include <linux/darray.h>
 #include <linux/errno.h>
 #include <linux/freezer.h>
 #include <linux/kernel.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/sched/clock.h>
 #include <linux/slab.h>
+#include <linux/time_stats.h>
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
+#include <linux/mean_and_variance.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)
-
-#else /* DEBUG */
-
+#else
 #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
 
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@@ -73,38 +52,6 @@ static inline size_t buf_pages(void *p, size_t len)
                            PAGE_SIZE);
 }
 
-static inline void vpfree(void *p, size_t size)
-{
-       if (is_vmalloc_addr(p))
-               vfree(p);
-       else
-               free_pages((unsigned long) p, get_order(size));
-}
-
-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);
-}
-
-static inline void kvpfree(void *p, size_t size)
-{
-       if (size < PAGE_SIZE)
-               kfree(p);
-       else
-               vpfree(p, size);
-}
-
-static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
-{
-       return size < PAGE_SIZE
-               ? kmalloc(size, gfp_mask)
-               : vpmalloc(size, gfp_mask);
-}
-
-int mempool_init_kvpmalloc_pool(mempool_t *, int, size_t);
-
 #define HEAP(type)                                                     \
 struct {                                                               \
        size_t size, used;                                              \
@@ -117,13 +64,13 @@ struct {                                                           \
 ({                                                                     \
        (heap)->used = 0;                                               \
        (heap)->size = (_size);                                         \
-       (heap)->data = kvpmalloc((heap)->size * sizeof((heap)->data[0]),\
+       (heap)->data = kvmalloc((heap)->size * sizeof((heap)->data[0]),\
                                 (gfp));                                \
 })
 
 #define free_heap(heap)                                                        \
 do {                                                                   \
-       kvpfree((heap)->data, (heap)->size * sizeof((heap)->data[0]));  \
+       kvfree((heap)->data);                                           \
        (heap)->data = NULL;                                            \
 } while (0)
 
@@ -210,9 +157,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)                           \
@@ -235,54 +184,54 @@ do {                                                                      \
 #define ANYSINT_MAX(t)                                                 \
        ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
 
-struct printbuf {
-       char            *pos;
-       char            *end;
-       unsigned        indent;
-};
+#include "printbuf.h"
 
-static inline size_t printbuf_remaining(struct printbuf *buf)
-{
-       return buf->end - buf->pos;
-}
+#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 _PBUF(_buf, _len)                                              \
-       ((struct printbuf) {                                            \
-               .pos    = _buf,                                         \
-               .end    = _buf + _len,                                  \
-       })
+#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 PBUF(_buf) _PBUF(_buf, sizeof(_buf))
+#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 pr_buf(_out, ...)                                              \
-do {                                                                   \
-       (_out)->pos += scnprintf((_out)->pos, printbuf_remaining(_out), \
-                                __VA_ARGS__);                          \
-} while (0)
+#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)
 
-static inline void printbuf_indent_push(struct printbuf *buf, unsigned spaces)
-{
-       buf->indent += spaces;
-       while (spaces--)
-               pr_buf(buf, " ");
-}
+#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);
 
-static inline void printbuf_indent_pop(struct printbuf *buf, unsigned spaces)
+#ifdef __KERNEL__
+static inline void uuid_unparse_lower(u8 *uuid, char *out)
 {
-       buf->indent -= spaces;
+       sprintf(out, "%pUb", uuid);
 }
+#else
+#include <uuid/uuid.h>
+#endif
 
-static inline void printbuf_newline(struct printbuf *buf)
+static inline void pr_uuid(struct printbuf *out, u8 *uuid)
 {
-       unsigned i;
+       char uuid_str[40];
 
-       pr_buf(buf, "\n");
-       for (i = 0; i < buf->indent; i++)
-               pr_buf(buf, " ");
+       uuid_unparse_lower(uuid, uuid_str);
+       prt_printf(out, "%s", uuid_str);
 }
 
-void bch_scnmemcpy(struct printbuf *, const char *, size_t);
-
 int bch2_strtoint_h(const char *, int *);
 int bch2_strtouint_h(const char *, unsigned int *);
 int bch2_strtoll_h(const char *, long long *);
@@ -345,8 +294,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"               \
@@ -356,61 +305,31 @@ static inline int bch2_strtoul_h(const char *cp, long *res)
                 : type_is(var, char *)         ? "%s\n"                \
                 : "%i\n", var)
 
-void bch2_hprint(struct printbuf *, s64);
-
 bool bch2_is_zero(const void *, size_t);
 
-void bch2_string_opt_to_text(struct printbuf *,
-                            const char * const [], size_t);
-
-void bch2_flags_to_text(struct printbuf *, const char * const[], u64);
 u64 bch2_read_flag_list(char *, const char * const[]);
 
-#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 {
-               u64     m;
-               u64     step;
-       }               entries[NR_QUANTILES];
-};
-
-struct time_stat_buffer {
-       unsigned        nr;
-       struct time_stat_buffer_entry {
-               u64     start;
-               u64     end;
-       }               entries[32];
-};
+void bch2_prt_u64_base2_nbits(struct printbuf *, u64, unsigned);
+void bch2_prt_u64_base2(struct printbuf *, u64);
 
-struct time_stats {
-       spinlock_t      lock;
-       u64             count;
-       /* all fields are in nanoseconds */
-       u64             average_duration;
-       u64             average_frequency;
-       u64             max_duration;
-       u64             last_event;
-       struct quantiles quantiles;
-
-       struct time_stat_buffer __percpu *buffer;
-};
+void bch2_print_string_as_lines(const char *prefix, const char *lines);
 
-void __bch2_time_stats_update(struct time_stats *stats, u64, u64);
+typedef DARRAY(unsigned long) bch_stacktrace;
+int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *, unsigned, gfp_t);
+void bch2_prt_backtrace(struct printbuf *, bch_stacktrace *);
+int bch2_prt_task_backtrace(struct printbuf *, struct task_struct *, unsigned, gfp_t);
 
-static inline void bch2_time_stats_update(struct time_stats *stats, u64 start)
+static inline void prt_bdevname(struct printbuf *out, struct block_device *bdev)
 {
-       __bch2_time_stats_update(stats, start, local_clock());
+#ifdef __KERNEL__
+       prt_printf(out, "%pg", bdev);
+#else
+       prt_str(out, bdev->name);
+#endif
 }
 
 void bch2_time_stats_to_text(struct printbuf *, struct time_stats *);
 
-void bch2_time_stats_exit(struct time_stats *);
-void bch2_time_stats_init(struct time_stats *);
-
 #define ewma_add(ewma, val, weight)                                    \
 ({                                                                     \
        typeof(ewma) _ewma = (ewma);                                    \
@@ -456,14 +375,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);                                      \
@@ -487,7 +408,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)                           \
@@ -533,6 +454,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;                                                   \
@@ -573,6 +514,7 @@ static inline void __memcpy_u64s(void *dst, const void *src,
 {
 #ifdef CONFIG_X86_64
        long d0, d1, d2;
+
        asm volatile("rep ; movsq"
                     : "=&c" (d0), "=&D" (d1), "=&S" (d2)
                     : "0" (u64s), "1" (dst), "2" (src)
@@ -609,6 +551,20 @@ 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)
 {
@@ -635,6 +591,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"
@@ -672,42 +629,39 @@ static inline void memset_u64s_tail(void *s, int c, unsigned bytes)
        memset(s + bytes, c, rem);
 }
 
-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));
+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;
 
-/* just the memmove, doesn't update @_nr */
-#define __array_insert_item(_array, _nr, _pos)                         \
-       memmove(&(_array)[(_pos) + 1],                                  \
-               &(_array)[(_pos)],                                      \
-               sizeof((_array)[0]) * ((_nr) - (_pos)))
+       if (new_gap < old_gap) {
+               size_t move = old_gap - new_gap;
 
-#define array_insert_item(_array, _nr, _pos, _new_item)                        \
-do {                                                                   \
-       __array_insert_item(_array, _nr, _pos);                         \
-       (_nr)++;                                                        \
-       (_array)[(_pos)] = (_new_item);                                 \
-} while (0)
+               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;
 
-#define array_remove_items(_array, _nr, _pos, _nr_to_remove)           \
-do {                                                                   \
-       (_nr) -= (_nr_to_remove);                                       \
-       memmove(&(_array)[(_pos)],                                      \
-               &(_array)[(_pos) + (_nr_to_remove)],                    \
-               sizeof((_array)[0]) * ((_nr) - (_pos)));                \
-} while (0)
+               memmove(array + element_size * old_gap,
+                       array + element_size * gap_end,
+                               element_size * move);
+       }
+}
 
-#define array_remove_item(_array, _nr, _pos)                           \
-       array_remove_items(_array, _nr, _pos, 1)
+/* 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;                        \
@@ -768,13 +722,21 @@ static inline int u8_cmp(u8 l, u8 r)
        return cmp_int(l, r);
 }
 
-#ifdef __KERNEL__
-static inline void uuid_unparse_lower(u8 *uuid, char *out)
+static inline int cmp_le32(__le32 l, __le32 r)
 {
-       sprintf(out, "%plU", uuid);
+       return cmp_int(le32_to_cpu(l), le32_to_cpu(r));
 }
-#else
-#include <uuid/uuid.h>
-#endif
+
+#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 */