]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Update bcachefs sources to 2a6125decb43 bcachefs: bch_sb_field_downgrade
[bcachefs-tools-debian] / libbcachefs / util.h
index 5b98ece3cf9beb084e2da1bb5c4db27db76c4d9a..4290e0a53b7563a4e6d912ce17083a947b6a3f4b 100644 (file)
@@ -17,7 +17,8 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
-#include <linux/mean_and_variance.h>
+
+#include "mean_and_variance.h"
 
 #include "darray.h"
 
@@ -60,13 +61,12 @@ static inline void vpfree(void *p, size_t size)
                free_pages((unsigned long) p, get_order(size));
 }
 
-static inline void *vpmalloc_noprof(size_t size, gfp_t gfp_mask)
+static inline void *vpmalloc(size_t size, gfp_t gfp_mask)
 {
-       return (void *) get_free_pages_noprof(gfp_mask|__GFP_NOWARN,
-                                             get_order(size)) ?:
-               __vmalloc_noprof(size, gfp_mask);
+       return (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
+                                        get_order(size)) ?:
+               __vmalloc(size, gfp_mask);
 }
-#define vpmalloc(_size, _gfp)  alloc_hooks(vpmalloc_noprof(_size, _gfp))
 
 static inline void kvpfree(void *p, size_t size)
 {
@@ -76,13 +76,12 @@ static inline void kvpfree(void *p, size_t size)
                vpfree(p, size);
 }
 
-static inline void *kvpmalloc_noprof(size_t size, gfp_t gfp_mask)
+static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
 {
        return size < PAGE_SIZE
-               ? kmalloc_noprof(size, gfp_mask)
-               : vpmalloc_noprof(size, gfp_mask);
+               ? kmalloc(size, gfp_mask)
+               : vpmalloc(size, gfp_mask);
 }
-#define kvpmalloc(_size, _gfp) alloc_hooks(kvpmalloc_noprof(_size, _gfp))
 
 int mempool_init_kvpmalloc_pool(mempool_t *, int, size_t);
 
@@ -244,28 +243,10 @@ do {                                                                      \
 #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);
-
-#ifdef __KERNEL__
-static inline void pr_time(struct printbuf *out, u64 time)
-{
-       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
+void bch2_prt_datetime(struct printbuf *, time64_t);
 
 #ifdef __KERNEL__
 static inline void uuid_unparse_lower(u8 *uuid, char *out)
@@ -393,8 +374,9 @@ struct bch2_time_stat_buffer {
 struct bch2_time_stats {
        spinlock_t      lock;
        /* all fields are in nanoseconds */
-       u64             max_duration;
        u64             min_duration;
+       u64             max_duration;
+       u64             total_duration;
        u64             max_freq;
        u64             min_freq;
        u64             last_event;
@@ -409,15 +391,39 @@ struct bch2_time_stats {
 
 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
 void __bch2_time_stats_update(struct bch2_time_stats *stats, u64, u64);
-#else
-static inline void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end) {}
-#endif
 
 static inline void bch2_time_stats_update(struct bch2_time_stats *stats, u64 start)
 {
        __bch2_time_stats_update(stats, start, local_clock());
 }
 
+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 bch2_time_stats *);
@@ -534,9 +540,7 @@ static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
 }
 
 void bch2_bio_map(struct bio *bio, void *base, size_t);
-int bch2_bio_alloc_pages_noprof(struct bio *, size_t, gfp_t);
-#define bch2_bio_alloc_pages(_bio, _size, _gfp)                                \
-       alloc_hooks(bch2_bio_alloc_pages_noprof(_bio, _size, _gfp))
+int bch2_bio_alloc_pages(struct bio *, size_t, gfp_t);
 
 static inline sector_t bdev_sectors(struct block_device *bdev)
 {
@@ -779,12 +783,12 @@ static inline void __move_gap(void *array, size_t element_size,
 
 #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;                        \
@@ -845,6 +849,21 @@ 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 */