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