summaryrefslogtreecommitdiff
path: root/fs/bcachefs/util.h
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2022-02-20 05:00:45 -0500
committerKent Overstreet <kent.overstreet@gmail.com>2022-04-17 15:44:22 -0400
commit5594b3cc3260ecfe48e6c1de2719e71c47bee217 (patch)
tree7cf99d5e52933ea3d0f715e783a4ad77b0612f26 /fs/bcachefs/util.h
parent59fe84c2200b7a43c4baf8d89c69aea2bc15c0ad (diff)
bcachefs: Add .to_text() methods for all superblock sections
This patch improves the superblock .to_text() methods and adds methods for all types that were missing them. It also improves printbufs by allowing them to specfiy what units we want to be printing in, and adds new wrapper methods for unifying our kernel and userspace environments. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Diffstat (limited to 'fs/bcachefs/util.h')
-rw-r--r--fs/bcachefs/util.h64
1 files changed, 51 insertions, 13 deletions
diff --git a/fs/bcachefs/util.h b/fs/bcachefs/util.h
index a80d4ec22d95..b43c195381f8 100644
--- a/fs/bcachefs/util.h
+++ b/fs/bcachefs/util.h
@@ -235,10 +235,17 @@ 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;
- unsigned indent;
+ char *pos;
+ char *end;
+ unsigned indent;
+ enum printbuf_units units;
};
static inline size_t printbuf_remaining(struct printbuf *buf)
@@ -272,7 +279,7 @@ static inline void printbuf_indent_pop(struct printbuf *buf, unsigned spaces)
buf->indent -= spaces;
}
-static inline void printbuf_newline(struct printbuf *buf)
+static inline void pr_newline(struct printbuf *buf)
{
unsigned i;
@@ -281,6 +288,46 @@ static inline void printbuf_newline(struct printbuf *buf)
pr_buf(buf, " ");
}
+void bch2_pr_units(struct printbuf *, s64, s64);
+#define pr_units(...) bch2_pr_units(__VA_ARGS__)
+
+#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, "%plU", 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, 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 *);
@@ -766,13 +813,4 @@ 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)
-{
- sprintf(out, "%plU", uuid);
-}
-#else
-#include <uuid/uuid.h>
-#endif
-
#endif /* _BCACHEFS_UTIL_H */