xref: /linux/fs/bcachefs/printbuf.h (revision 5e2cb28dd7e182dfa641550dfa225913509ad45d)
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /* Copyright (C) 2022 Kent Overstreet */
3 
4 #ifndef _BCACHEFS_PRINTBUF_H
5 #define _BCACHEFS_PRINTBUF_H
6 
7 /*
8  * Printbufs: Simple strings for printing to, with optional heap allocation
9  *
10  * This code has provisions for use in userspace, to aid in making other code
11  * portable between kernelspace and userspace.
12  *
13  * Basic example:
14  *   struct printbuf buf = PRINTBUF;
15  *
16  *   prt_printf(&buf, "foo=");
17  *   foo_to_text(&buf, foo);
18  *   printk("%s", buf.buf);
19  *   printbuf_exit(&buf);
20  *
21  * Or
22  *   struct printbuf buf = PRINTBUF_EXTERN(char_buf, char_buf_size)
23  *
24  * We can now write pretty printers instead of writing code that dumps
25  * everything to the kernel log buffer, and then those pretty-printers can be
26  * used by other code that outputs to kernel log, sysfs, debugfs, etc.
27  *
28  * Memory allocation: Outputing to a printbuf may allocate memory. This
29  * allocation is done with GFP_KERNEL, by default: use the newer
30  * memalloc_*_(save|restore) functions as needed.
31  *
32  * Since no equivalent yet exists for GFP_ATOMIC/GFP_NOWAIT, memory allocations
33  * will be done with GFP_NOWAIT if printbuf->atomic is nonzero.
34  *
35  * It's allowed to grab the output buffer and free it later with kfree() instead
36  * of using printbuf_exit(), if the user just needs a heap allocated string at
37  * the end.
38  *
39  * Memory allocation failures: We don't return errors directly, because on
40  * memory allocation failure we usually don't want to bail out and unwind - we
41  * want to print what we've got, on a best-effort basis. But code that does want
42  * to return -ENOMEM may check printbuf.allocation_failure.
43  *
44  * Indenting, tabstops:
45  *
46  * To aid is writing multi-line pretty printers spread across multiple
47  * functions, printbufs track the current indent level.
48  *
49  * printbuf_indent_push() and printbuf_indent_pop() increase and decrease the current indent
50  * level, respectively.
51  *
52  * To use tabstops, set printbuf->tabstops[]; they are in units of spaces, from
53  * start of line. Once set, prt_tab() will output spaces up to the next tabstop.
54  * prt_tab_rjust() will also advance the current line of text up to the next
55  * tabstop, but it does so by shifting text since the previous tabstop up to the
56  * next tabstop - right justifying it.
57  *
58  * Make sure you use prt_newline() instead of \n in the format string for indent
59  * level and tabstops to work corretly.
60  *
61  * Output units: printbuf->units exists to tell pretty-printers how to output
62  * numbers: a raw value (e.g. directly from a superblock field), as bytes, or as
63  * human readable bytes. prt_units() obeys it.
64  */
65 
66 #include <linux/kernel.h>
67 #include <linux/string.h>
68 
69 enum printbuf_si {
70 	PRINTBUF_UNITS_2,	/* use binary powers of 2^10 */
71 	PRINTBUF_UNITS_10,	/* use powers of 10^3 (standard SI) */
72 };
73 
74 #define PRINTBUF_INLINE_TABSTOPS	6
75 
76 struct printbuf {
77 	char			*buf;
78 	unsigned		size;
79 	unsigned		pos;
80 	unsigned		last_newline;
81 	unsigned		last_field;
82 	unsigned		indent;
83 	/*
84 	 * If nonzero, allocations will be done with GFP_ATOMIC:
85 	 */
86 	u8			atomic;
87 	bool			allocation_failure:1;
88 	bool			heap_allocated:1;
89 	enum printbuf_si	si_units:1;
90 	bool			human_readable_units:1;
91 	bool			has_indent_or_tabstops:1;
92 	bool			suppress_indent_tabstop_handling:1;
93 	u8			nr_tabstops;
94 
95 	/*
96 	 * Do not modify directly: use printbuf_tabstop_add(),
97 	 * printbuf_tabstop_get()
98 	 */
99 	u8			cur_tabstop;
100 	u8			_tabstops[PRINTBUF_INLINE_TABSTOPS];
101 };
102 
103 int bch2_printbuf_make_room(struct printbuf *, unsigned);
104 __printf(2, 3) void bch2_prt_printf(struct printbuf *out, const char *fmt, ...);
105 __printf(2, 0) void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list);
106 const char *bch2_printbuf_str(const struct printbuf *);
107 void bch2_printbuf_exit(struct printbuf *);
108 
109 void bch2_printbuf_tabstops_reset(struct printbuf *);
110 void bch2_printbuf_tabstop_pop(struct printbuf *);
111 int bch2_printbuf_tabstop_push(struct printbuf *, unsigned);
112 
113 void bch2_printbuf_indent_add(struct printbuf *, unsigned);
114 void bch2_printbuf_indent_sub(struct printbuf *, unsigned);
115 
116 void bch2_prt_newline(struct printbuf *);
117 void bch2_prt_tab(struct printbuf *);
118 void bch2_prt_tab_rjust(struct printbuf *);
119 
120 void bch2_prt_bytes_indented(struct printbuf *, const char *, unsigned);
121 void bch2_prt_human_readable_u64(struct printbuf *, u64);
122 void bch2_prt_human_readable_s64(struct printbuf *, s64);
123 void bch2_prt_units_u64(struct printbuf *, u64);
124 void bch2_prt_units_s64(struct printbuf *, s64);
125 void bch2_prt_string_option(struct printbuf *, const char * const[], size_t);
126 void bch2_prt_bitflags(struct printbuf *, const char * const[], u64);
127 
128 /* Initializer for a heap allocated printbuf: */
129 #define PRINTBUF ((struct printbuf) { .heap_allocated = true })
130 
131 /* Initializer a printbuf that points to an external buffer: */
132 #define PRINTBUF_EXTERN(_buf, _size)			\
133 ((struct printbuf) {					\
134 	.buf	= _buf,					\
135 	.size	= _size,				\
136 })
137 
138 /*
139  * Returns size remaining of output buffer:
140  */
141 static inline unsigned printbuf_remaining_size(struct printbuf *out)
142 {
143 	return out->pos < out->size ? out->size - out->pos : 0;
144 }
145 
146 /*
147  * Returns number of characters we can print to the output buffer - i.e.
148  * excluding the terminating nul:
149  */
150 static inline unsigned printbuf_remaining(struct printbuf *out)
151 {
152 	return out->pos < out->size ? out->size - out->pos - 1 : 0;
153 }
154 
155 static inline unsigned printbuf_written(struct printbuf *out)
156 {
157 	return out->size ? min(out->pos, out->size - 1) : 0;
158 }
159 
160 /*
161  * Returns true if output was truncated:
162  */
163 static inline bool printbuf_overflowed(struct printbuf *out)
164 {
165 	return out->pos >= out->size;
166 }
167 
168 static inline void printbuf_nul_terminate(struct printbuf *out)
169 {
170 	bch2_printbuf_make_room(out, 1);
171 
172 	if (out->pos < out->size)
173 		out->buf[out->pos] = 0;
174 	else if (out->size)
175 		out->buf[out->size - 1] = 0;
176 }
177 
178 /* Doesn't call bch2_printbuf_make_room(), doesn't nul terminate: */
179 static inline void __prt_char_reserved(struct printbuf *out, char c)
180 {
181 	if (printbuf_remaining(out))
182 		out->buf[out->pos] = c;
183 	out->pos++;
184 }
185 
186 /* Doesn't nul terminate: */
187 static inline void __prt_char(struct printbuf *out, char c)
188 {
189 	bch2_printbuf_make_room(out, 1);
190 	__prt_char_reserved(out, c);
191 }
192 
193 static inline void prt_char(struct printbuf *out, char c)
194 {
195 	__prt_char(out, c);
196 	printbuf_nul_terminate(out);
197 }
198 
199 static inline void __prt_chars_reserved(struct printbuf *out, char c, unsigned n)
200 {
201 	unsigned i, can_print = min(n, printbuf_remaining(out));
202 
203 	for (i = 0; i < can_print; i++)
204 		out->buf[out->pos++] = c;
205 	out->pos += n - can_print;
206 }
207 
208 static inline void prt_chars(struct printbuf *out, char c, unsigned n)
209 {
210 	bch2_printbuf_make_room(out, n);
211 	__prt_chars_reserved(out, c, n);
212 	printbuf_nul_terminate(out);
213 }
214 
215 static inline void prt_bytes(struct printbuf *out, const void *b, unsigned n)
216 {
217 	unsigned i, can_print;
218 
219 	bch2_printbuf_make_room(out, n);
220 
221 	can_print = min(n, printbuf_remaining(out));
222 
223 	for (i = 0; i < can_print; i++)
224 		out->buf[out->pos++] = ((char *) b)[i];
225 	out->pos += n - can_print;
226 
227 	printbuf_nul_terminate(out);
228 }
229 
230 static inline void prt_str(struct printbuf *out, const char *str)
231 {
232 	prt_bytes(out, str, strlen(str));
233 }
234 
235 static inline void prt_str_indented(struct printbuf *out, const char *str)
236 {
237 	bch2_prt_bytes_indented(out, str, strlen(str));
238 }
239 
240 static inline void prt_hex_byte(struct printbuf *out, u8 byte)
241 {
242 	bch2_printbuf_make_room(out, 2);
243 	__prt_char_reserved(out, hex_asc_hi(byte));
244 	__prt_char_reserved(out, hex_asc_lo(byte));
245 	printbuf_nul_terminate(out);
246 }
247 
248 static inline void prt_hex_byte_upper(struct printbuf *out, u8 byte)
249 {
250 	bch2_printbuf_make_room(out, 2);
251 	__prt_char_reserved(out, hex_asc_upper_hi(byte));
252 	__prt_char_reserved(out, hex_asc_upper_lo(byte));
253 	printbuf_nul_terminate(out);
254 }
255 
256 /**
257  * printbuf_reset - re-use a printbuf without freeing and re-initializing it:
258  */
259 static inline void printbuf_reset(struct printbuf *buf)
260 {
261 	buf->pos		= 0;
262 	buf->allocation_failure	= 0;
263 	buf->indent		= 0;
264 	buf->nr_tabstops	= 0;
265 	buf->cur_tabstop	= 0;
266 }
267 
268 /**
269  * printbuf_atomic_inc - mark as entering an atomic section
270  */
271 static inline void printbuf_atomic_inc(struct printbuf *buf)
272 {
273 	buf->atomic++;
274 }
275 
276 /**
277  * printbuf_atomic_inc - mark as leaving an atomic section
278  */
279 static inline void printbuf_atomic_dec(struct printbuf *buf)
280 {
281 	buf->atomic--;
282 }
283 
284 #endif /* _BCACHEFS_PRINTBUF_H */
285