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