]>
Commit | Line | Data |
---|---|---|
3a161d99 SRRH |
1 | /* |
2 | * seq_buf.c | |
3 | * | |
4 | * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> | |
5 | * | |
6 | * The seq_buf is a handy tool that allows you to pass a descriptor around | |
7 | * to a buffer that other functions can write to. It is similar to the | |
8 | * seq_file functionality but has some differences. | |
9 | * | |
10 | * To use it, the seq_buf must be initialized with seq_buf_init(). | |
11 | * This will set up the counters within the descriptor. You can call | |
12 | * seq_buf_init() more than once to reset the seq_buf to start | |
13 | * from scratch. | |
14 | */ | |
15 | #include <linux/uaccess.h> | |
16 | #include <linux/seq_file.h> | |
17 | #include <linux/seq_buf.h> | |
18 | ||
19 | /* How much buffer is written? */ | |
20 | #define SEQ_BUF_USED(s) min((s)->len, (s)->size - 1) | |
21 | ||
22 | /** | |
23 | * seq_buf_print_seq - move the contents of seq_buf into a seq_file | |
24 | * @m: the seq_file descriptor that is the destination | |
25 | * @s: the seq_buf descriptor that is the source. | |
26 | * | |
27 | * Returns zero on success, non zero otherwise | |
28 | */ | |
29 | int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s) | |
30 | { | |
31 | unsigned int len = SEQ_BUF_USED(s); | |
32 | ||
33 | return seq_write(m, s->buffer, len); | |
34 | } | |
35 | ||
36 | /** | |
37 | * seq_buf_vprintf - sequence printing of information. | |
38 | * @s: seq_buf descriptor | |
39 | * @fmt: printf format string | |
40 | * @args: va_list of arguments from a printf() type function | |
41 | * | |
42 | * Writes a vnprintf() format into the sequencce buffer. | |
43 | * | |
44 | * Returns zero on success, -1 on overflow. | |
45 | */ | |
46 | int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args) | |
47 | { | |
48 | int len; | |
49 | ||
50 | WARN_ON(s->size == 0); | |
51 | ||
52 | if (s->len < s->size) { | |
53 | len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args); | |
54 | if (s->len + len < s->size) { | |
55 | s->len += len; | |
56 | return 0; | |
57 | } | |
58 | } | |
59 | seq_buf_set_overflow(s); | |
60 | return -1; | |
61 | } | |
62 | ||
63 | /** | |
64 | * seq_buf_printf - sequence printing of information | |
65 | * @s: seq_buf descriptor | |
66 | * @fmt: printf format string | |
67 | * | |
68 | * Writes a printf() format into the sequence buffer. | |
69 | * | |
70 | * Returns zero on success, -1 on overflow. | |
71 | */ | |
72 | int seq_buf_printf(struct seq_buf *s, const char *fmt, ...) | |
73 | { | |
74 | va_list ap; | |
75 | int ret; | |
76 | ||
77 | va_start(ap, fmt); | |
78 | ret = seq_buf_vprintf(s, fmt, ap); | |
79 | va_end(ap); | |
80 | ||
81 | return ret; | |
82 | } | |
83 | ||
84 | /** | |
85 | * seq_buf_bitmask - write a bitmask array in its ASCII representation | |
86 | * @s: seq_buf descriptor | |
87 | * @maskp: points to an array of unsigned longs that represent a bitmask | |
88 | * @nmaskbits: The number of bits that are valid in @maskp | |
89 | * | |
90 | * Writes a ASCII representation of a bitmask string into @s. | |
91 | * | |
92 | * Returns zero on success, -1 on overflow. | |
93 | */ | |
94 | int seq_buf_bitmask(struct seq_buf *s, const unsigned long *maskp, | |
95 | int nmaskbits) | |
96 | { | |
97 | unsigned int len = seq_buf_buffer_left(s); | |
98 | int ret; | |
99 | ||
100 | WARN_ON(s->size == 0); | |
101 | ||
102 | /* | |
103 | * The last byte of the buffer is used to determine if we | |
104 | * overflowed or not. | |
105 | */ | |
106 | if (len > 1) { | |
107 | ret = bitmap_scnprintf(s->buffer + s->len, len, maskp, nmaskbits); | |
108 | if (ret < len) { | |
109 | s->len += ret; | |
110 | return 0; | |
111 | } | |
112 | } | |
113 | seq_buf_set_overflow(s); | |
114 | return -1; | |
115 | } | |
116 | ||
117 | /** | |
118 | * seq_buf_bprintf - Write the printf string from binary arguments | |
119 | * @s: seq_buf descriptor | |
120 | * @fmt: The format string for the @binary arguments | |
121 | * @binary: The binary arguments for @fmt. | |
122 | * | |
123 | * When recording in a fast path, a printf may be recorded with just | |
124 | * saving the format and the arguments as they were passed to the | |
125 | * function, instead of wasting cycles converting the arguments into | |
126 | * ASCII characters. Instead, the arguments are saved in a 32 bit | |
127 | * word array that is defined by the format string constraints. | |
128 | * | |
129 | * This function will take the format and the binary array and finish | |
130 | * the conversion into the ASCII string within the buffer. | |
131 | * | |
132 | * Returns zero on success, -1 on overflow. | |
133 | */ | |
134 | int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary) | |
135 | { | |
136 | unsigned int len = seq_buf_buffer_left(s); | |
137 | int ret; | |
138 | ||
139 | WARN_ON(s->size == 0); | |
140 | ||
141 | if (s->len < s->size) { | |
142 | ret = bstr_printf(s->buffer + s->len, len, fmt, binary); | |
143 | if (s->len + ret < s->size) { | |
144 | s->len += ret; | |
145 | return 0; | |
146 | } | |
147 | } | |
148 | seq_buf_set_overflow(s); | |
149 | return -1; | |
150 | } | |
151 | ||
152 | /** | |
153 | * seq_buf_puts - sequence printing of simple string | |
154 | * @s: seq_buf descriptor | |
155 | * @str: simple string to record | |
156 | * | |
157 | * Copy a simple string into the sequence buffer. | |
158 | * | |
159 | * Returns zero on success, -1 on overflow | |
160 | */ | |
161 | int seq_buf_puts(struct seq_buf *s, const char *str) | |
162 | { | |
163 | unsigned int len = strlen(str); | |
164 | ||
165 | WARN_ON(s->size == 0); | |
166 | ||
167 | if (s->len + len < s->size) { | |
168 | memcpy(s->buffer + s->len, str, len); | |
169 | s->len += len; | |
170 | return 0; | |
171 | } | |
172 | seq_buf_set_overflow(s); | |
173 | return -1; | |
174 | } | |
175 | ||
176 | /** | |
177 | * seq_buf_putc - sequence printing of simple character | |
178 | * @s: seq_buf descriptor | |
179 | * @c: simple character to record | |
180 | * | |
181 | * Copy a single character into the sequence buffer. | |
182 | * | |
183 | * Returns zero on success, -1 on overflow | |
184 | */ | |
185 | int seq_buf_putc(struct seq_buf *s, unsigned char c) | |
186 | { | |
187 | WARN_ON(s->size == 0); | |
188 | ||
189 | if (s->len + 1 < s->size) { | |
190 | s->buffer[s->len++] = c; | |
191 | return 0; | |
192 | } | |
193 | seq_buf_set_overflow(s); | |
194 | return -1; | |
195 | } | |
196 | ||
197 | /** | |
198 | * seq_buf_putmem - write raw data into the sequenc buffer | |
199 | * @s: seq_buf descriptor | |
200 | * @mem: The raw memory to copy into the buffer | |
201 | * @len: The length of the raw memory to copy (in bytes) | |
202 | * | |
203 | * There may be cases where raw memory needs to be written into the | |
204 | * buffer and a strcpy() would not work. Using this function allows | |
205 | * for such cases. | |
206 | * | |
207 | * Returns zero on success, -1 on overflow | |
208 | */ | |
209 | int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len) | |
210 | { | |
211 | WARN_ON(s->size == 0); | |
212 | ||
213 | if (s->len + len < s->size) { | |
214 | memcpy(s->buffer + s->len, mem, len); | |
215 | s->len += len; | |
216 | return 0; | |
217 | } | |
218 | seq_buf_set_overflow(s); | |
219 | return -1; | |
220 | } | |
221 | ||
222 | #define MAX_MEMHEX_BYTES 8U | |
223 | #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1) | |
224 | ||
225 | /** | |
226 | * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex | |
227 | * @s: seq_buf descriptor | |
228 | * @mem: The raw memory to write its hex ASCII representation of | |
229 | * @len: The length of the raw memory to copy (in bytes) | |
230 | * | |
231 | * This is similar to seq_buf_putmem() except instead of just copying the | |
232 | * raw memory into the buffer it writes its ASCII representation of it | |
233 | * in hex characters. | |
234 | * | |
235 | * Returns zero on success, -1 on overflow | |
236 | */ | |
237 | int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, | |
238 | unsigned int len) | |
239 | { | |
240 | unsigned char hex[HEX_CHARS]; | |
241 | const unsigned char *data = mem; | |
242 | unsigned int start_len; | |
243 | int i, j; | |
244 | ||
245 | WARN_ON(s->size == 0); | |
246 | ||
247 | while (len) { | |
248 | start_len = min(len, HEX_CHARS - 1); | |
249 | #ifdef __BIG_ENDIAN | |
250 | for (i = 0, j = 0; i < start_len; i++) { | |
251 | #else | |
252 | for (i = start_len-1, j = 0; i >= 0; i--) { | |
253 | #endif | |
254 | hex[j++] = hex_asc_hi(data[i]); | |
255 | hex[j++] = hex_asc_lo(data[i]); | |
256 | } | |
257 | if (WARN_ON_ONCE(j == 0 || j/2 > len)) | |
258 | break; | |
259 | ||
260 | /* j increments twice per loop */ | |
261 | len -= j / 2; | |
262 | hex[j++] = ' '; | |
263 | ||
264 | seq_buf_putmem(s, hex, j); | |
265 | if (seq_buf_has_overflowed(s)) | |
266 | return -1; | |
267 | } | |
268 | return 0; | |
269 | } | |
270 | ||
271 | /** | |
272 | * seq_buf_path - copy a path into the sequence buffer | |
273 | * @s: seq_buf descriptor | |
274 | * @path: path to write into the sequence buffer. | |
275 | * | |
276 | * Write a path name into the sequence buffer. | |
277 | * | |
278 | * Returns zero on success, -1 on overflow | |
279 | */ | |
280 | int seq_buf_path(struct seq_buf *s, const struct path *path) | |
281 | { | |
282 | unsigned int len = seq_buf_buffer_left(s); | |
283 | unsigned char *p; | |
284 | ||
285 | WARN_ON(s->size == 0); | |
286 | ||
287 | p = d_path(path, s->buffer + s->len, len); | |
288 | if (!IS_ERR(p)) { | |
289 | p = mangle_path(s->buffer + s->len, p, "\n"); | |
290 | if (p) { | |
291 | s->len = p - s->buffer; | |
292 | return 0; | |
293 | } | |
294 | } | |
295 | seq_buf_set_overflow(s); | |
296 | return -1; | |
297 | } | |
298 | ||
299 | /** | |
300 | * seq_buf_to_user - copy the squence buffer to user space | |
301 | * @s: seq_buf descriptor | |
302 | * @ubuf: The userspace memory location to copy to | |
303 | * @cnt: The amount to copy | |
304 | * | |
305 | * Copies the sequence buffer into the userspace memory pointed to | |
306 | * by @ubuf. It starts from the last read position (@s->readpos) | |
307 | * and writes up to @cnt characters or till it reaches the end of | |
308 | * the content in the buffer (@s->len), which ever comes first. | |
309 | * | |
310 | * On success, it returns a positive number of the number of bytes | |
311 | * it copied. | |
312 | * | |
313 | * On failure it returns -EBUSY if all of the content in the | |
314 | * sequence has been already read, which includes nothing in the | |
315 | * sequence (@s->len == @s->readpos). | |
316 | * | |
317 | * Returns -EFAULT if the copy to userspace fails. | |
318 | */ | |
319 | int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt) | |
320 | { | |
321 | int len; | |
322 | int ret; | |
323 | ||
324 | if (!cnt) | |
325 | return 0; | |
326 | ||
327 | if (s->len <= s->readpos) | |
328 | return -EBUSY; | |
329 | ||
330 | len = s->len - s->readpos; | |
331 | if (cnt > len) | |
332 | cnt = len; | |
333 | ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); | |
334 | if (ret == cnt) | |
335 | return -EFAULT; | |
336 | ||
337 | cnt -= ret; | |
338 | ||
339 | s->readpos += cnt; | |
340 | return cnt; | |
341 | } |