diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2022-03-22 00:16:31 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2022-04-22 19:31:27 -0400 |
commit | 8c69628d9527a1fed31289649ef53e732bfdd5e3 (patch) | |
tree | a5e6a56537b8406e22eede8b1406ff4fee9dde11 | |
parent | ede8fb590f16cab304dc17ca6a4bca1160410f04 (diff) |
bcachefs: Convert to lib/printbuf.c
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
-rw-r--r-- | fs/bcachefs/checksum.c | 12 | ||||
-rw-r--r-- | fs/bcachefs/debug.c | 2 | ||||
-rw-r--r-- | fs/bcachefs/opts.c | 4 | ||||
-rw-r--r-- | fs/bcachefs/rebalance.c | 10 | ||||
-rw-r--r-- | fs/bcachefs/super-io.c | 13 | ||||
-rw-r--r-- | fs/bcachefs/super.c | 11 | ||||
-rw-r--r-- | fs/bcachefs/sysfs.c | 20 | ||||
-rw-r--r-- | fs/bcachefs/tests.c | 4 | ||||
-rw-r--r-- | fs/bcachefs/util.c | 141 | ||||
-rw-r--r-- | fs/bcachefs/util.h | 159 | ||||
-rw-r--r-- | lib/printbuf.c | 2 |
11 files changed, 48 insertions, 330 deletions
diff --git a/fs/bcachefs/checksum.c b/fs/bcachefs/checksum.c index 425582f60d7a..58afc49b2473 100644 --- a/fs/bcachefs/checksum.c +++ b/fs/bcachefs/checksum.c @@ -462,13 +462,15 @@ static int __bch2_request_key(char *key_description, struct bch_key *key) int bch2_request_key(struct bch_sb *sb, struct bch_key *key) { - char key_description[60]; - char uuid[40]; + struct printbuf key_description = PRINTBUF; + int ret; - uuid_unparse_lower(sb->user_uuid.b, uuid); - sprintf(key_description, "bcachefs:%s", uuid); + pr_buf(&key_description, "bcachefs:"); + pr_uuid(&key_description, sb->user_uuid.b); - return __bch2_request_key(key_description, key); + ret = __bch2_request_key(key_description.buf, key); + printbuf_exit(&key_description); + return ret; } int bch2_decrypt_sb_key(struct bch_fs *c, diff --git a/fs/bcachefs/debug.c b/fs/bcachefs/debug.c index 3b869be18506..882b11ff45f5 100644 --- a/fs/bcachefs/debug.c +++ b/fs/bcachefs/debug.c @@ -440,7 +440,7 @@ static void bch2_cached_btree_node_to_text(struct printbuf *out, struct bch_fs * pr_buf(out, "flags: "); pr_tab(out); - bch2_flags_to_text(out, bch2_btree_node_flags, b->flags); + pr_bitflags(out, bch2_btree_node_flags, b->flags); pr_newline(out); pr_buf(out, "pcpu read locks: "); diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c index 385451ef865e..a55aa8c51346 100644 --- a/fs/bcachefs/opts.c +++ b/fs/bcachefs/opts.c @@ -316,13 +316,13 @@ void bch2_opt_to_text(struct printbuf *out, case BCH_OPT_BOOL: case BCH_OPT_UINT: if (opt->flags & OPT_HUMAN_READABLE) - bch2_hprint(out, v); + pr_human_readable_u64(out, v); else pr_buf(out, "%lli", v); break; case BCH_OPT_STR: if (flags & OPT_SHOW_FULL_LIST) - bch2_string_opt_to_text(out, opt->choices, v); + pr_string_option(out, opt->choices, v); else pr_buf(out, "%s", opt->choices[v]); break; diff --git a/fs/bcachefs/rebalance.c b/fs/bcachefs/rebalance.c index d914892f5339..e557c6f6163e 100644 --- a/fs/bcachefs/rebalance.c +++ b/fs/bcachefs/rebalance.c @@ -263,17 +263,17 @@ void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c) pr_buf(out, "fullest_dev (%i):", w.dev_most_full_idx); pr_tab(out); - bch2_hprint(out, w.dev_most_full_work << 9); + pr_human_readable_u64(out, w.dev_most_full_work << 9); pr_buf(out, "/"); - bch2_hprint(out, w.dev_most_full_capacity << 9); + pr_human_readable_u64(out, w.dev_most_full_capacity << 9); pr_newline(out); pr_buf(out, "total work:"); pr_tab(out); - bch2_hprint(out, w.total_work << 9); + pr_human_readable_u64(out, w.total_work << 9); pr_buf(out, "/"); - bch2_hprint(out, c->capacity << 9); + pr_human_readable_u64(out, c->capacity << 9); pr_newline(out); pr_buf(out, "rate:"); @@ -288,7 +288,7 @@ void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c) case REBALANCE_THROTTLED: pr_buf(out, "throttled for %lu sec or ", (r->throttled_until_cputime - jiffies) / HZ); - bch2_hprint(out, + pr_human_readable_u64(out, (r->throttled_until_iotime - atomic64_read(&c->io_clock[WRITE].now)) << 9); pr_buf(out, " io"); diff --git a/fs/bcachefs/super-io.c b/fs/bcachefs/super-io.c index a2b789b4ac68..fd93f59dc489 100644 --- a/fs/bcachefs/super-io.c +++ b/fs/bcachefs/super-io.c @@ -1076,8 +1076,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, pr_buf(out, "Data allowed:"); pr_tab(out); if (BCH_MEMBER_DATA_ALLOWED(m)) - bch2_flags_to_text(out, bch2_data_types, - BCH_MEMBER_DATA_ALLOWED(m)); + pr_bitflags(out, bch2_data_types, BCH_MEMBER_DATA_ALLOWED(m)); else pr_buf(out, "(none)"); pr_newline(out); @@ -1085,7 +1084,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, pr_buf(out, "Has data:"); pr_tab(out); if (data_have) - bch2_flags_to_text(out, bch2_data_types, data_have); + pr_bitflags(out, bch2_data_types, data_have); else pr_buf(out, "(none)"); pr_newline(out); @@ -1558,19 +1557,17 @@ void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb, vstruct_for_each(sb, f) fields_have |= 1 << le32_to_cpu(f->type); pr_tab(out); - bch2_flags_to_text(out, bch2_sb_fields, fields_have); + pr_bitflags(out, bch2_sb_fields, fields_have); pr_newline(out); pr_buf(out, "Features:"); pr_tab(out); - bch2_flags_to_text(out, bch2_sb_features, - le64_to_cpu(sb->features[0])); + pr_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0])); pr_newline(out); pr_buf(out, "Compat features:"); pr_tab(out); - bch2_flags_to_text(out, bch2_sb_compat, - le64_to_cpu(sb->compat[0])); + pr_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0])); pr_newline(out); pr_newline(out); diff --git a/fs/bcachefs/super.c b/fs/bcachefs/super.c index bdac2b727233..c2951e139e64 100644 --- a/fs/bcachefs/super.c +++ b/fs/bcachefs/super.c @@ -596,6 +596,7 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) { struct bch_sb_field_members *mi; struct bch_fs *c; + struct printbuf name = PRINTBUF; unsigned i, iter_size; int ret = 0; @@ -699,7 +700,13 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) if (ret) goto err; - uuid_unparse_lower(c->sb.user_uuid.b, c->name); + pr_uuid(&name, c->sb.user_uuid.b); + strlcpy(c->name, name.buf, sizeof(c->name)); + printbuf_exit(&name); + + ret = name.allocation_failure ? -ENOMEM : 0; + if (ret) + goto err; /* Compat: */ if (sb->version <= bcachefs_metadata_version_inode_v2 && @@ -1478,7 +1485,7 @@ int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) if (data) { struct printbuf data_has = PRINTBUF; - bch2_flags_to_text(&data_has, bch2_data_types, data); + pr_bitflags(&data_has, bch2_data_types, data); bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); printbuf_exit(&data_has); ret = -EBUSY; diff --git a/fs/bcachefs/sysfs.c b/fs/bcachefs/sysfs.c index 77e2ec73319e..87741a78b9e6 100644 --- a/fs/bcachefs/sysfs.c +++ b/fs/bcachefs/sysfs.c @@ -99,7 +99,7 @@ do { \ #define sysfs_hprint(file, val) \ do { \ if (attr == &sysfs_ ## file) \ - bch2_hprint(out, val); \ + pr_human_readable_s64(out, val); \ } while (0) #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var)) @@ -340,22 +340,22 @@ static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c pr_buf(out, "uncompressed:\n"); pr_buf(out, " nr extents: %llu\n", nr_uncompressed_extents); pr_buf(out, " size: "); - bch2_hprint(out, uncompressed_sectors << 9); + pr_human_readable_u64(out, uncompressed_sectors << 9); pr_buf(out, "\n"); pr_buf(out, "compressed:\n"); pr_buf(out, " nr extents: %llu\n", nr_compressed_extents); pr_buf(out, " compressed size: "); - bch2_hprint(out, compressed_sectors_compressed << 9); + pr_human_readable_u64(out, compressed_sectors_compressed << 9); pr_buf(out, "\n"); pr_buf(out, " uncompressed size: "); - bch2_hprint(out, compressed_sectors_uncompressed << 9); + pr_human_readable_u64(out, compressed_sectors_uncompressed << 9); pr_buf(out, "\n"); pr_buf(out, "incompressible:\n"); pr_buf(out, " nr extents: %llu\n", nr_incompressible_extents); pr_buf(out, " size: "); - bch2_hprint(out, incompressible_sectors << 9); + pr_human_readable_u64(out, incompressible_sectors << 9); pr_buf(out, "\n"); return 0; } @@ -566,12 +566,12 @@ SHOW(bch2_fs_counters) counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\ pr_buf(out, "since mount:"); \ pr_tab(out); \ - bch2_hprint(out, counter_since_mount << 9); \ + pr_human_readable_u64(out, counter_since_mount << 9); \ pr_newline(out); \ \ pr_buf(out, "since filesystem creation:"); \ pr_tab(out); \ - bch2_hprint(out, counter << 9); \ + pr_human_readable_u64(out, counter << 9); \ pr_newline(out); \ } BCH_PERSISTENT_COUNTERS() @@ -843,14 +843,12 @@ SHOW(bch2_dev) } if (attr == &sysfs_has_data) { - bch2_flags_to_text(out, bch2_data_types, - bch2_dev_has_data(c, ca)); + pr_bitflags(out, bch2_data_types, bch2_dev_has_data(c, ca)); pr_char(out, '\n'); } if (attr == &sysfs_state_rw) { - bch2_string_opt_to_text(out, bch2_member_states, - ca->mi.state); + pr_string_option(out, bch2_member_states, ca->mi.state); pr_char(out, '\n'); } diff --git a/fs/bcachefs/tests.c b/fs/bcachefs/tests.c index 4369bfc55a94..3c9bdbe60f81 100644 --- a/fs/bcachefs/tests.c +++ b/fs/bcachefs/tests.c @@ -932,8 +932,8 @@ int bch2_btree_perf_test(struct bch_fs *c, const char *testname, time = j.finish - j.start; scnprintf(name_buf, sizeof(name_buf), "%s:", testname); - bch2_hprint(&nr_buf, nr); - bch2_hprint(&per_sec_buf, div64_u64(nr * NSEC_PER_SEC, time)); + pr_human_readable_u64(&nr_buf, nr); + pr_human_readable_u64(&per_sec_buf, div64_u64(nr * NSEC_PER_SEC, time)); printk(KERN_INFO "%-12s %s with %u threads in %5llu sec, %5llu nsec per iter, %5s per sec\n", name_buf, nr_buf.buf, nr_threads, div_u64(time, NSEC_PER_SEC), diff --git a/fs/bcachefs/util.c b/fs/bcachefs/util.c index 37fc20413764..1198f5f8daf3 100644 --- a/fs/bcachefs/util.c +++ b/fs/bcachefs/util.c @@ -99,135 +99,6 @@ STRTO_H(strtoll, long long) STRTO_H(strtoull, unsigned long long) STRTO_H(strtou64, u64) -static int bch2_printbuf_realloc(struct printbuf *out, unsigned extra) -{ - unsigned new_size; - char *buf; - - if (out->pos + extra + 1 < out->size) - return 0; - - new_size = roundup_pow_of_two(out->size + extra); - buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_ATOMIC); - - if (!buf) { - out->allocation_failure = true; - return -ENOMEM; - } - - out->buf = buf; - out->size = new_size; - return 0; -} - -void bch2_pr_buf(struct printbuf *out, const char *fmt, ...) -{ - va_list args; - int len; - - do { - va_start(args, fmt); - len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args); - va_end(args); - } while (len + 1 >= printbuf_remaining(out) && - !bch2_printbuf_realloc(out, len + 1)); - - len = min_t(size_t, len, - printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0); - out->pos += len; -} - -void bch2_pr_tab_rjust(struct printbuf *buf) -{ - BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops)); - - if (printbuf_linelen(buf) < buf->tabstops[buf->tabstop]) { - unsigned move = buf->pos - buf->last_field; - unsigned shift = buf->tabstops[buf->tabstop] - - printbuf_linelen(buf); - - bch2_printbuf_realloc(buf, shift); - - if (buf->last_field + shift + 1 < buf->size) { - move = min(move, buf->size - 1 - buf->last_field - shift); - - memmove(buf->buf + buf->last_field + shift, - buf->buf + buf->last_field, - move); - memset(buf->buf + buf->last_field, ' ', shift); - buf->pos += shift; - buf->buf[buf->pos] = 0; - } - } - - buf->last_field = buf->pos; - buf->tabstop++; -} - -void bch2_hprint(struct printbuf *buf, s64 v) -{ - int u, t = 0; - - for (u = 0; v >= 1024 || v <= -1024; u++) { - t = v & ~(~0U << 10); - v >>= 10; - } - - pr_buf(buf, "%lli", v); - - /* - * 103 is magic: t is in the range [-1023, 1023] and we want - * to turn it into [-9, 9] - */ - if (u && t && v < 100 && v > -100) - pr_buf(buf, ".%i", t / 103); - if (u) - pr_char(buf, si_units[u]); -} - -void bch2_pr_units(struct printbuf *out, s64 raw, s64 bytes) -{ - switch (out->units) { - case PRINTBUF_UNITS_RAW: - pr_buf(out, "%llu", raw); - break; - case PRINTBUF_UNITS_BYTES: - pr_buf(out, "%llu", bytes); - break; - case PRINTBUF_UNITS_HUMAN_READABLE: - bch2_hprint(out, bytes); - break; - } -} - -void bch2_string_opt_to_text(struct printbuf *out, - const char * const list[], - size_t selected) -{ - size_t i; - - for (i = 0; list[i]; i++) - pr_buf(out, i == selected ? "[%s] " : "%s ", list[i]); -} - -void bch2_flags_to_text(struct printbuf *out, - const char * const list[], u64 flags) -{ - unsigned bit, nr = 0; - bool first = true; - - while (list[nr]) - nr++; - - while (flags && (bit = __ffs(flags)) < nr) { - if (!first) - pr_buf(out, ","); - first = false; - pr_buf(out, "%s", list[bit]); - flags ^= 1 << bit; - } -} - u64 bch2_read_flag_list(char *opt, const char * const list[]) { u64 ret = 0; @@ -550,32 +421,32 @@ void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_contro pr_buf(out, "rate:"); pr_tab(out); - bch2_hprint(out, pd->rate.rate); + pr_human_readable_s64(out, pd->rate.rate); pr_newline(out); pr_buf(out, "target:"); pr_tab(out); - bch2_hprint(out, pd->last_target); + pr_human_readable_u64(out, pd->last_target); pr_newline(out); pr_buf(out, "actual:"); pr_tab(out); - bch2_hprint(out, pd->last_actual); + pr_human_readable_u64(out, pd->last_actual); pr_newline(out); pr_buf(out, "proportional:"); pr_tab(out); - bch2_hprint(out, pd->last_proportional); + pr_human_readable_s64(out, pd->last_proportional); pr_newline(out); pr_buf(out, "derivative:"); pr_tab(out); - bch2_hprint(out, pd->last_derivative); + pr_human_readable_s64(out, pd->last_derivative); pr_newline(out); pr_buf(out, "change:"); pr_tab(out); - bch2_hprint(out, pd->last_change); + pr_human_readable_s64(out, pd->last_change); pr_newline(out); pr_buf(out, "next io:"); diff --git a/fs/bcachefs/util.h b/fs/bcachefs/util.h index 98f70a5cc5d0..73130c1e2c99 100644 --- a/fs/bcachefs/util.h +++ b/fs/bcachefs/util.h @@ -11,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> @@ -237,158 +238,6 @@ 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 *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->size - buf->pos; -} - -static inline size_t printbuf_linelen(struct printbuf *buf) -{ - return buf->pos - buf->last_newline; -} - -void bch2_pr_buf(struct printbuf *out, const char *fmt, ...) - __attribute__ ((format (printf, 2, 3))); - -#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, ' '); - - 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 *); int bch2_strtoll_h(const char *, long long *); @@ -462,14 +311,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 diff --git a/lib/printbuf.c b/lib/printbuf.c index e0dfa82cdad6..4312e83f209b 100644 --- a/lib/printbuf.c +++ b/lib/printbuf.c @@ -221,7 +221,7 @@ void pr_uuid(struct printbuf *out, u8 *uuid) char uuid_str[40]; uuid_unparse_lower(uuid, uuid_str); - pr_buf(out, uuid_str); + pr_buf(out, "%s", uuid_str); } #endif |