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