1 // SPDX-License-Identifier: LGPL-2.1+
2 /* Copyright (C) 2022 Kent Overstreet */
5 #include <linux/math64.h>
6 #include <linux/printbuf.h>
7 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
14 #define EXPORT_SYMBOL(x)
18 static inline size_t printbuf_linelen(struct printbuf *buf)
20 return buf->pos - buf->last_newline;
23 int printbuf_make_room(struct printbuf *out, unsigned extra)
28 if (!out->heap_allocated)
31 /* Reserved space for terminating nul: */
34 if (out->pos + extra < out->size)
37 new_size = roundup_pow_of_two(out->size + extra);
38 buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT);
41 out->allocation_failure = true;
49 EXPORT_SYMBOL(printbuf_make_room);
52 * printbuf_str - returns printbuf's buf as a C string, guaranteed to be null
55 const char *printbuf_str(const struct printbuf *buf)
58 * If we've written to a printbuf then it's guaranteed to be a null
59 * terminated string - but if we haven't, then we might not have
60 * allocated a buffer at all:
66 EXPORT_SYMBOL(printbuf_str);
69 * printbuf_exit - exit a printbuf, freeing memory it owns and poisoning it
70 * against accidental use.
72 void printbuf_exit(struct printbuf *buf)
74 if (buf->heap_allocated) {
76 buf->buf = ERR_PTR(-EINTR); /* poison value */
79 EXPORT_SYMBOL(printbuf_exit);
81 void prt_newline(struct printbuf *buf)
85 printbuf_make_room(buf, 1 + buf->indent);
87 __prt_char(buf, '\n');
89 buf->last_newline = buf->pos;
91 for (i = 0; i < buf->indent; i++)
94 printbuf_nul_terminate(buf);
96 buf->last_field = buf->pos;
99 EXPORT_SYMBOL(prt_newline);
102 * printbuf_indent_add - add to the current indent level
104 * @buf: printbuf to control
105 * @spaces: number of spaces to add to the current indent level
107 * Subsequent lines, and the current line if the output position is at the start
108 * of the current line, will be indented by @spaces more spaces.
110 void printbuf_indent_add(struct printbuf *buf, unsigned spaces)
112 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
115 buf->indent += spaces;
119 EXPORT_SYMBOL(printbuf_indent_add);
122 * printbuf_indent_sub - subtract from the current indent level
124 * @buf: printbuf to control
125 * @spaces: number of spaces to subtract from the current indent level
127 * Subsequent lines, and the current line if the output position is at the start
128 * of the current line, will be indented by @spaces less spaces.
130 void printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
132 if (WARN_ON_ONCE(spaces > buf->indent))
133 spaces = buf->indent;
135 if (buf->last_newline + buf->indent == buf->pos) {
137 printbuf_nul_terminate(buf);
139 buf->indent -= spaces;
141 EXPORT_SYMBOL(printbuf_indent_sub);
144 * prt_tab - Advance printbuf to the next tabstop
146 * @buf: printbuf to control
148 * Advance output to the next tabstop by printing spaces.
150 void prt_tab(struct printbuf *out)
152 int spaces = max_t(int, 0, out->tabstops[out->tabstop] - printbuf_linelen(out));
154 BUG_ON(out->tabstop > ARRAY_SIZE(out->tabstops));
156 prt_chars(out, ' ', spaces);
158 out->last_field = out->pos;
161 EXPORT_SYMBOL(prt_tab);
164 * prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
167 * @buf: printbuf to control
169 * Advance output to the next tabstop by inserting spaces immediately after the
170 * previous tabstop, right justifying previously outputted text.
172 void prt_tab_rjust(struct printbuf *buf)
174 BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops));
176 if (printbuf_linelen(buf) < buf->tabstops[buf->tabstop]) {
177 unsigned move = buf->pos - buf->last_field;
178 unsigned shift = buf->tabstops[buf->tabstop] -
179 printbuf_linelen(buf);
181 printbuf_make_room(buf, shift);
183 if (buf->last_field + shift < buf->size)
184 memmove(buf->buf + buf->last_field + shift,
185 buf->buf + buf->last_field,
186 min(move, buf->size - 1 - buf->last_field - shift));
188 if (buf->last_field < buf->size)
189 memset(buf->buf + buf->last_field, ' ',
190 min(shift, buf->size - buf->last_field));
193 printbuf_nul_terminate(buf);
196 buf->last_field = buf->pos;
199 EXPORT_SYMBOL(prt_tab_rjust);
201 enum string_size_units {
202 STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
203 STRING_UNITS_2, /* use binary powers of 2^10 */
205 static int string_get_size(u64 size, u64 blk_size,
206 const enum string_size_units units,
209 static const char *const units_10[] = {
210 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
212 static const char *const units_2[] = {
213 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
215 static const char *const *const units_str[] = {
216 [STRING_UNITS_10] = units_10,
217 [STRING_UNITS_2] = units_2,
219 static const unsigned int divisor[] = {
220 [STRING_UNITS_10] = 1000,
221 [STRING_UNITS_2] = 1024,
223 static const unsigned int rounding[] = { 500, 50, 5 };
225 u32 remainder = 0, sf_cap;
236 /* This is Napier's algorithm. Reduce the original block size to
238 * coefficient * divisor[units]^i
240 * we do the reduction so both coefficients are just under 32 bits so
241 * that multiplying them together won't overflow 64 bits and we keep
242 * as much precision as possible in the numbers.
244 * Note: it's safe to throw away the remainders here because all the
245 * precision is in the coefficients.
247 while (blk_size >> 32) {
248 do_div(blk_size, divisor[units]);
253 do_div(size, divisor[units]);
257 /* now perform the actual multiplication keeping i as the sum of the
261 /* and logarithmically reduce it until it's just under the divisor */
262 while (size >= divisor[units]) {
263 remainder = do_div(size, divisor[units]);
267 /* work out in j how many digits of precision we need from the
270 for (j = 0; sf_cap*10 < 1000; j++)
273 if (units == STRING_UNITS_2) {
274 /* express the remainder as a decimal. It's currently the
275 * numerator of a fraction whose denominator is
276 * divisor[units], which is 1 << 10 for STRING_UNITS_2 */
281 /* add a 5 to the digit below what will be printed to ensure
282 * an arithmetical round up and carry it through to size */
283 remainder += rounding[j];
284 if (remainder >= 1000) {
290 snprintf(tmp, sizeof(tmp), ".%03u", remainder);
295 if (i >= ARRAY_SIZE(units_2))
298 unit = units_str[units][i];
300 return snprintf(buf, len, "%u%s %s", (u32)size, tmp, unit);
304 * prt_human_readable_u64 - Print out a u64 in human readable units
306 * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units
308 void prt_human_readable_u64(struct printbuf *buf, u64 v)
310 printbuf_make_room(buf, 10);
311 buf->pos += string_get_size(v, 1, !buf->si_units,
313 printbuf_remaining_size(buf));
315 EXPORT_SYMBOL(prt_human_readable_u64);
318 * prt_human_readable_s64 - Print out a s64 in human readable units
320 * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units
322 void prt_human_readable_s64(struct printbuf *buf, s64 v)
326 prt_human_readable_u64(buf, abs(v));
328 EXPORT_SYMBOL(prt_human_readable_s64);
331 * prt_units_u64 - Print out a u64 according to printbuf unit options
333 * Units are either raw (default), or human reabable units (controlled via
334 * @buf->human_readable_units)
336 void prt_units_u64(struct printbuf *out, u64 v)
338 if (out->human_readable_units)
339 prt_human_readable_u64(out, v);
341 prt_printf(out, "%llu", v);
343 EXPORT_SYMBOL(prt_units_u64);
346 * prt_units_s64 - Print out a s64 according to printbuf unit options
348 * Units are either raw (default), or human reabable units (controlled via
349 * @buf->human_readable_units)
351 void prt_units_s64(struct printbuf *out, s64 v)
355 prt_units_u64(out, abs(v));
357 EXPORT_SYMBOL(prt_units_s64);