]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Update bcachefs sources to 3704d0779c bcachefs: Improved human readable integer parsing
[bcachefs-tools-debian] / libbcachefs / util.h
index 7e1729a4b125a954c529b5735ce18e4c918ef997..1fe66fd91ccc724203d29b83d6787c94f9e1b5b6 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _BCACHEFS_UTIL_H
 #define _BCACHEFS_UTIL_H
 
@@ -10,6 +11,7 @@
 #include <linux/sched/clock.h>
 #include <linux/llist.h>
 #include <linux/log2.h>
+#include <linux/printbuf.h>
 #include <linux/percpu.h>
 #include <linux/preempt.h>
 #include <linux/ratelimit.h>
@@ -17,9 +19,6 @@
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
 
-#define PAGE_SECTOR_SHIFT      (PAGE_SHIFT - 9)
-#define PAGE_SECTORS           (1UL << PAGE_SECTOR_SHIFT)
-
 struct closure;
 
 #ifdef CONFIG_BCACHEFS_DEBUG
@@ -36,17 +35,6 @@ struct closure;
 #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)
@@ -98,7 +86,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)
@@ -223,9 +211,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)                           \
@@ -248,31 +238,43 @@ do {                                                                      \
 #define ANYSINT_MAX(t)                                                 \
        ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
 
-struct printbuf {
-       char            *pos;
-       char            *end;
-};
 
-static inline size_t printbuf_remaining(struct printbuf *buf)
+#ifdef __KERNEL__
+static inline void pr_time(struct printbuf *out, u64 time)
 {
-       return buf->end - buf->pos;
+       prt_printf(out, "%llu", time);
 }
+#else
+#include <time.h>
+static inline void pr_time(struct printbuf *out, u64 _time)
+{
+       char time_str[64];
+       time_t time = _time;
+       struct tm *tm = localtime(&time);
+       size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
+       if (!err)
+               prt_printf(out, "(formatting error)");
+       else
+               prt_printf(out, "%s", time_str);
+}
+#endif
 
-#define _PBUF(_buf, _len)                                              \
-       ((struct printbuf) {                                            \
-               .pos    = _buf,                                         \
-               .end    = _buf + _len,                                  \
-       })
-
-#define PBUF(_buf) _PBUF(_buf, sizeof(_buf))
+#ifdef __KERNEL__
+static inline void uuid_unparse_lower(u8 *uuid, char *out)
+{
+       sprintf(out, "%pUb", uuid);
+}
+#else
+#include <uuid/uuid.h>
+#endif
 
-#define pr_buf(_out, ...)                                              \
-do {                                                                   \
-       (_out)->pos += scnprintf((_out)->pos, printbuf_remaining(_out), \
-                                __VA_ARGS__);                          \
-} while (0)
+static inline void pr_uuid(struct printbuf *out, u8 *uuid)
+{
+       char uuid_str[40];
 
-void bch_scnmemcpy(struct printbuf *, const char *, size_t);
+       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 *);
@@ -336,8 +338,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"               \
@@ -347,14 +349,8 @@ 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
@@ -397,7 +393,7 @@ static inline void bch2_time_stats_update(struct 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);
+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 *);
@@ -454,7 +450,7 @@ struct bch_pd_controller {
 
 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);                                      \
@@ -478,7 +474,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)                           \
@@ -510,8 +506,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)
 {
@@ -546,9 +542,19 @@ 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)
 {
@@ -590,6 +596,24 @@ static inline void memmove_u64s_down(void *dst, const void *src,
        __memmove_u64s_down(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)
 {
@@ -627,35 +651,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)
-
 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));
@@ -684,6 +687,31 @@ 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;                                               \
@@ -715,10 +743,7 @@ static inline void percpu_u64_set(u64 __percpu *dst, u64 src)
 
        for_each_possible_cpu(cpu)
                *per_cpu_ptr(dst, cpu) = 0;
-
-       preempt_disable();
-       *this_cpu_ptr(dst) = src;
-       preempt_enable();
+       this_cpu_write(*dst, src);
 }
 
 static inline void acc_u64s(u64 *acc, const u64 *src, unsigned nr)
@@ -738,6 +763,21 @@ static inline void acc_u64s_percpu(u64 *acc, const u64 __percpu *src,
                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);
+}
+
 #endif /* _BCACHEFS_UTIL_H */