]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Update bcachefs sources to ab3b6e7dd6 bcachefs: Fix pr_buf() calls
[bcachefs-tools-debian] / libbcachefs / util.h
index f48c6380684f67ec5f6052507c07906148ee66f6..086d941f23e59648829f087a4c7565db979bd0ba 100644 (file)
@@ -18,9 +18,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
@@ -37,17 +34,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)
@@ -224,9 +210,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)                           \
@@ -249,31 +237,157 @@ do {                                                                     \
 #define ANYSINT_MAX(t)                                                 \
        ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
 
+enum printbuf_units {
+       PRINTBUF_UNITS_RAW,
+       PRINTBUF_UNITS_BYTES,
+       PRINTBUF_UNITS_HUMAN_READABLE,
+};
+
 struct printbuf {
-       char            *pos;
-       char            *end;
+       char                    *buf;
+       unsigned                size;
+       unsigned                pos;
+       unsigned                last_newline;
+       unsigned                last_field;
+       unsigned                indent;
+       enum printbuf_units     units:8;
+       u8                      atomic;
+       bool                    allocation_failure:1;
+       u8                      tabstop;
+       u8                      tabstops[4];
 };
 
+#define PRINTBUF ((struct printbuf) { NULL })
+
+static inline void printbuf_exit(struct printbuf *buf)
+{
+       kfree(buf->buf);
+       buf->buf = ERR_PTR(-EINTR); /* poison value */
+}
+
+static inline void printbuf_reset(struct printbuf *buf)
+{
+       buf->pos                = 0;
+       buf->last_newline       = 0;
+       buf->last_field         = 0;
+       buf->indent             = 0;
+       buf->tabstop            = 0;
+}
+
 static inline size_t printbuf_remaining(struct printbuf *buf)
 {
-       return buf->end - buf->pos;
+       return buf->size - buf->pos;
 }
 
-#define _PBUF(_buf, _len)                                              \
-       ((struct printbuf) {                                            \
-               .pos    = _buf,                                         \
-               .end    = _buf + _len,                                  \
-       })
+static inline size_t printbuf_linelen(struct printbuf *buf)
+{
+       return buf->pos - buf->last_newline;
+}
 
-#define PBUF(_buf) _PBUF(_buf, sizeof(_buf))
+void bch2_pr_buf(struct printbuf *out, const char *fmt, ...)
+       __attribute__ ((format (printf, 2, 3)));
 
-#define pr_buf(_out, ...)                                              \
-do {                                                                   \
-       (_out)->pos += scnprintf((_out)->pos, printbuf_remaining(_out), \
-                                __VA_ARGS__);                          \
-} while (0)
+#define pr_buf(_out, ...) bch2_pr_buf(_out, __VA_ARGS__)
+
+static inline void pr_char(struct printbuf *out, char c)
+{
+       bch2_pr_buf(out, "%c", c);
+}
+
+static inline void pr_indent_push(struct printbuf *buf, unsigned spaces)
+{
+       buf->indent += spaces;
+       while (spaces--)
+               pr_char(buf, ' ');
+}
+
+static inline void pr_indent_pop(struct printbuf *buf, unsigned spaces)
+{
+       if (buf->last_newline + buf->indent == buf->pos) {
+               buf->pos -= spaces;
+               buf->buf[buf->pos] = 0;
+       }
+       buf->indent -= spaces;
+}
+
+static inline void pr_newline(struct printbuf *buf)
+{
+       unsigned i;
+
+       pr_char(buf, '\n');
+
+       buf->last_newline       = buf->pos;
+
+       for (i = 0; i < buf->indent; i++)
+               pr_char(buf, ' ');
 
-void bch_scnmemcpy(struct printbuf *, const char *, size_t);
+       buf->last_field         = buf->pos;
+       buf->tabstop = 0;
+}
+
+static inline void pr_tab(struct printbuf *buf)
+{
+       BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops));
+
+       while (printbuf_remaining(buf) > 1 &&
+              printbuf_linelen(buf) < buf->tabstops[buf->tabstop])
+               pr_char(buf, ' ');
+
+       buf->last_field = buf->pos;
+       buf->tabstop++;
+}
+
+void bch2_pr_tab_rjust(struct printbuf *);
+
+static inline void pr_tab_rjust(struct printbuf *buf)
+{
+       bch2_pr_tab_rjust(buf);
+}
+
+void bch2_pr_units(struct printbuf *, s64, s64);
+#define pr_units(...) bch2_pr_units(__VA_ARGS__)
+
+static inline void pr_sectors(struct printbuf *out, u64 v)
+{
+       bch2_pr_units(out, v, v << 9);
+}
+
+#ifdef __KERNEL__
+static inline void pr_time(struct printbuf *out, u64 time)
+{
+       pr_buf(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)
+               pr_buf(out, "(formatting error)");
+       else
+               pr_buf(out, "%s", time_str);
+}
+#endif
+
+#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);
+       pr_buf(out, "%s", uuid_str);
+}
 
 int bch2_strtoint_h(const char *, int *);
 int bch2_strtouint_h(const char *, unsigned int *);
@@ -337,8 +451,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)                                              \
+       pr_buf(out,                                                     \
                   type_is(var, int)            ? "%i\n"                \
                 : type_is(var, unsigned)       ? "%u\n"                \
                 : type_is(var, long)           ? "%li\n"               \
@@ -455,7 +569,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);                                      \
@@ -479,7 +593,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)                           \
@@ -723,10 +837,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)
@@ -758,4 +869,9 @@ 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 */