diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2022-03-22 00:16:31 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2022-05-20 13:54:48 -0400 |
commit | f3cd24b05dfac667f183c0d12eac8890edb95f62 (patch) | |
tree | 8831dff58cc40359a085532071ed5df11bfa9c33 | |
parent | a136245957317195bb1aa87040d5ceba05a916b4 (diff) |
bcachefs: Convert to lib/printbuf.cprintbuf_v2
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
-rw-r--r-- | fs/bcachefs/btree_iter.c | 4 | ||||
-rw-r--r-- | fs/bcachefs/btree_update_leaf.c | 2 | ||||
-rw-r--r-- | fs/bcachefs/checksum.c | 12 | ||||
-rw-r--r-- | fs/bcachefs/debug.c | 7 | ||||
-rw-r--r-- | fs/bcachefs/io.c | 2 | ||||
-rw-r--r-- | fs/bcachefs/journal.c | 8 | ||||
-rw-r--r-- | fs/bcachefs/journal_io.c | 2 | ||||
-rw-r--r-- | fs/bcachefs/opts.c | 6 | ||||
-rw-r--r-- | fs/bcachefs/rebalance.c | 10 | ||||
-rw-r--r-- | fs/bcachefs/super-io.c | 38 | ||||
-rw-r--r-- | fs/bcachefs/super.c | 12 | ||||
-rw-r--r-- | fs/bcachefs/sysfs.c | 21 | ||||
-rw-r--r-- | fs/bcachefs/tests.c | 4 | ||||
-rw-r--r-- | fs/bcachefs/util.c | 141 | ||||
-rw-r--r-- | fs/bcachefs/util.h | 121 |
15 files changed, 75 insertions, 315 deletions
diff --git a/fs/bcachefs/btree_iter.c b/fs/bcachefs/btree_iter.c index e7541af9518f..7a0633397e10 100644 --- a/fs/bcachefs/btree_iter.c +++ b/fs/bcachefs/btree_iter.c @@ -1844,7 +1844,7 @@ void bch2_trans_updates_to_text(struct printbuf *buf, struct btree_trans *trans) pr_buf(buf, "transaction updates for %s journal seq %llu", trans->fn, trans->journal_res.seq); pr_newline(buf); - pr_indent_push(buf, 2); + pr_indent_add(buf, 2); trans_for_each_update(trans, i) { struct bkey_s_c old = { &i->old_k, i->old_v }; @@ -1863,7 +1863,7 @@ void bch2_trans_updates_to_text(struct printbuf *buf, struct btree_trans *trans) pr_newline(buf); } - pr_indent_pop(buf, 2); + pr_indent_sub(buf, 2); } noinline __cold diff --git a/fs/bcachefs/btree_update_leaf.c b/fs/bcachefs/btree_update_leaf.c index 58bb687a3a8f..c0dfafe432bb 100644 --- a/fs/bcachefs/btree_update_leaf.c +++ b/fs/bcachefs/btree_update_leaf.c @@ -868,7 +868,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, pr_buf(&buf, "invalid bkey on insert from %s -> %ps", trans->fn, (void *) i->ip_allocated); pr_newline(&buf); - pr_indent_push(&buf, 2); + pr_indent_add(&buf, 2); bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k)); pr_newline(&buf); diff --git a/fs/bcachefs/checksum.c b/fs/bcachefs/checksum.c index 50157b4013a5..8dac6389ff4e 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..db9f54420072 100644 --- a/fs/bcachefs/debug.c +++ b/fs/bcachefs/debug.c @@ -24,6 +24,7 @@ #include <linux/console.h> #include <linux/debugfs.h> #include <linux/module.h> +#include <linux/pretty-printers.h> #include <linux/random.h> #include <linux/seq_file.h> @@ -433,14 +434,14 @@ static void bch2_cached_btree_node_to_text(struct printbuf *out, struct bch_fs * b->c.level); pr_newline(out); - pr_indent_push(out, 2); + pr_indent_add(out, 2); bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(&b->key)); pr_newline(out); 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: "); @@ -473,7 +474,7 @@ static void bch2_cached_btree_node_to_text(struct printbuf *out, struct bch_fs * pr_buf(out, "%llu", b->writes[1].journal.seq); pr_newline(out); - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); } static ssize_t bch2_cached_btree_nodes_read(struct file *file, char __user *buf, diff --git a/fs/bcachefs/io.c b/fs/bcachefs/io.c index 1ad4c7d77812..5e17fb134bcf 100644 --- a/fs/bcachefs/io.c +++ b/fs/bcachefs/io.c @@ -1987,7 +1987,7 @@ static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans, BTREE_ITER_CACHED); pr_buf(&buf, "Attempting to read from stale dirty pointer:"); - pr_indent_push(&buf, 2); + pr_indent_add(&buf, 2); pr_newline(&buf); bch2_bkey_val_to_text(&buf, c, k); diff --git a/fs/bcachefs/journal.c b/fs/bcachefs/journal.c index 4c5b67599007..5c276fa1c907 100644 --- a/fs/bcachefs/journal.c +++ b/fs/bcachefs/journal.c @@ -1301,7 +1301,7 @@ void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j) pr_tab(out); pr_buf(out, "%llu", seq); pr_newline(out); - pr_indent_push(out, 2); + pr_indent_add(out, 2); pr_buf(out, "refcount:"); pr_tab(out); @@ -1318,7 +1318,7 @@ void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j) pr_buf(out, "%li jiffies", j->buf[i].expires - jiffies); pr_newline(out); - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); } pr_buf(out, @@ -1390,7 +1390,7 @@ bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 pr_buf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count)); pr_newline(out); - pr_indent_push(out, 2); + pr_indent_add(out, 2); list_for_each_entry(pin, &pin_list->list, list) { pr_buf(out, "\t%px %ps", pin, pin->flush); @@ -1412,7 +1412,7 @@ bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 pr_newline(out); } - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); --out->atomic; spin_unlock(&j->lock); diff --git a/fs/bcachefs/journal_io.c b/fs/bcachefs/journal_io.c index e537a578c443..62326881ba2b 100644 --- a/fs/bcachefs/journal_io.c +++ b/fs/bcachefs/journal_io.c @@ -265,7 +265,7 @@ static int journal_validate_key(struct bch_fs *c, const char *where, (u64 *) k - entry->_data, le16_to_cpu(entry->u64s)); pr_newline(&buf); - pr_indent_push(&buf, 2); + pr_indent_add(&buf, 2); bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k)); pr_newline(&buf); diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c index 385451ef865e..d282b85b7159 100644 --- a/fs/bcachefs/opts.c +++ b/fs/bcachefs/opts.c @@ -9,6 +9,8 @@ #include "super-io.h" #include "util.h" +#include <linux/pretty-printers.h> + #define x(t, n) [n] = #t, const char * const bch2_metadata_versions[] = { @@ -316,13 +318,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..b268c1d7bcb9 100644 --- a/fs/bcachefs/super-io.c +++ b/fs/bcachefs/super-io.c @@ -20,6 +20,7 @@ #include "counters.h" #include <linux/backing-dev.h> +#include <linux/pretty-printers.h> #include <linux/sort.h> #include <trace/events/bcachefs.h> @@ -1015,7 +1016,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, pr_buf(out, "%u", i); pr_newline(out); - pr_indent_push(out, 2); + pr_indent_add(out, 2); pr_buf(out, "UUID:"); pr_tab(out); @@ -1024,12 +1025,12 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, pr_buf(out, "Size:"); pr_tab(out); - pr_units(out, device_size, device_size << 9); + pr_units_u64(out, device_size << 9); pr_newline(out); pr_buf(out, "Bucket size:"); pr_tab(out); - pr_units(out, bucket_size, bucket_size << 9); + pr_units_u64(out, bucket_size << 9); pr_newline(out); pr_buf(out, "First bucket:"); @@ -1076,8 +1077,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 +1085,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); @@ -1100,7 +1100,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, pr_buf(out, "%llu", BCH_MEMBER_FREESPACE_INITIALIZED(m)); pr_newline(out); - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); } } @@ -1444,9 +1444,9 @@ void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb, pr_newline(out); if (ops && ops->to_text) { - pr_indent_push(out, 2); + pr_indent_add(out, 2); bch2_sb_field_ops[type]->to_text(out, sb, f); - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); } } @@ -1458,9 +1458,7 @@ void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l) pr_newline(out); pr_buf(out, "Superblock max size: "); - pr_units(out, - 1 << l->sb_max_size_bits, - 512 << l->sb_max_size_bits); + pr_units_u64(out, 512 << l->sb_max_size_bits); pr_newline(out); pr_buf(out, "Nr superblocks: %u", l->nr_superblocks); @@ -1558,25 +1556,23 @@ 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); pr_buf(out, "Options:"); pr_newline(out); - pr_indent_push(out, 2); + pr_indent_add(out, 2); { enum bch_opt_id id; @@ -1595,15 +1591,15 @@ void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb, } } - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); if (print_layout) { pr_newline(out); pr_buf(out, "layout:"); pr_newline(out); - pr_indent_push(out, 2); + pr_indent_add(out, 2); bch2_sb_layout_to_text(out, &sb->layout); - pr_indent_pop(out, 2); + pr_indent_sub(out, 2); } vstruct_for_each(sb, f) diff --git a/fs/bcachefs/super.c b/fs/bcachefs/super.c index bdac2b727233..b5f4611e9633 100644 --- a/fs/bcachefs/super.c +++ b/fs/bcachefs/super.c @@ -54,6 +54,7 @@ #include <linux/idr.h> #include <linux/module.h> #include <linux/percpu.h> +#include <linux/pretty-printers.h> #include <linux/random.h> #include <linux/sysfs.h> #include <crypto/hash.h> @@ -596,6 +597,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 +701,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 +1486,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..f977e7673624 100644 --- a/fs/bcachefs/sysfs.c +++ b/fs/bcachefs/sysfs.c @@ -34,6 +34,7 @@ #include "tests.h" #include <linux/blkdev.h> +#include <linux/pretty-printers.h> #include <linux/sort.h> #include <linux/sched/clock.h> @@ -99,7 +100,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 +341,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 +567,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 +844,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..0705e7d823ee 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,120 +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) @@ -462,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 |