blob: 5954f9fb6675a7bf577bef1db9555e7ba9627383 [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{
Steven Rostedt (Red Hat)8cd709a2014-10-29 15:26:09 -040029 return s->len + len <= s->size;
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -050030}
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)4a8fe4e2015-03-04 09:56:02 -050064 if (s->len + len < s->size) {
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
Steven Rostedt (Red Hat)24489132014-11-03 18:53:50 -050094#ifdef CONFIG_BINARY_PRINTF
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040095/**
96 * seq_buf_bprintf - Write the printf string from binary arguments
97 * @s: seq_buf descriptor
98 * @fmt: The format string for the @binary arguments
99 * @binary: The binary arguments for @fmt.
100 *
101 * When recording in a fast path, a printf may be recorded with just
102 * saving the format and the arguments as they were passed to the
103 * function, instead of wasting cycles converting the arguments into
104 * ASCII characters. Instead, the arguments are saved in a 32 bit
105 * word array that is defined by the format string constraints.
106 *
107 * This function will take the format and the binary array and finish
108 * the conversion into the ASCII string within the buffer.
109 *
110 * Returns zero on success, -1 on overflow.
111 */
112int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
113{
114 unsigned int len = seq_buf_buffer_left(s);
115 int ret;
116
117 WARN_ON(s->size == 0);
118
119 if (s->len < s->size) {
120 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
Steven Rostedt (Red Hat)4d4eb4d2015-03-04 23:30:45 -0500121 if (s->len + ret < s->size) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400122 s->len += ret;
123 return 0;
124 }
125 }
126 seq_buf_set_overflow(s);
127 return -1;
128}
Steven Rostedt (Red Hat)24489132014-11-03 18:53:50 -0500129#endif /* CONFIG_BINARY_PRINTF */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400130
131/**
132 * seq_buf_puts - sequence printing of simple string
133 * @s: seq_buf descriptor
134 * @str: simple string to record
135 *
136 * Copy a simple string into the sequence buffer.
137 *
138 * Returns zero on success, -1 on overflow
139 */
140int seq_buf_puts(struct seq_buf *s, const char *str)
141{
142 unsigned int len = strlen(str);
143
144 WARN_ON(s->size == 0);
145
Michael Ellermancb56da642018-10-19 15:21:08 +1100146 /* Add 1 to len for the trailing null byte which must be there */
147 len += 1;
148
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -0500149 if (seq_buf_can_fit(s, len)) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400150 memcpy(s->buffer + s->len, str, len);
Michael Ellermancb56da642018-10-19 15:21:08 +1100151 /* Don't count the trailing null byte against the capacity */
152 s->len += len - 1;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400153 return 0;
154 }
155 seq_buf_set_overflow(s);
156 return -1;
157}
158
159/**
160 * seq_buf_putc - sequence printing of simple character
161 * @s: seq_buf descriptor
162 * @c: simple character to record
163 *
164 * Copy a single character into the sequence buffer.
165 *
166 * Returns zero on success, -1 on overflow
167 */
168int seq_buf_putc(struct seq_buf *s, unsigned char c)
169{
170 WARN_ON(s->size == 0);
171
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -0500172 if (seq_buf_can_fit(s, 1)) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400173 s->buffer[s->len++] = c;
174 return 0;
175 }
176 seq_buf_set_overflow(s);
177 return -1;
178}
179
180/**
181 * seq_buf_putmem - write raw data into the sequenc buffer
182 * @s: seq_buf descriptor
183 * @mem: The raw memory to copy into the buffer
184 * @len: The length of the raw memory to copy (in bytes)
185 *
186 * There may be cases where raw memory needs to be written into the
187 * buffer and a strcpy() would not work. Using this function allows
188 * for such cases.
189 *
190 * Returns zero on success, -1 on overflow
191 */
192int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
193{
194 WARN_ON(s->size == 0);
195
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -0500196 if (seq_buf_can_fit(s, len)) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400197 memcpy(s->buffer + s->len, mem, len);
198 s->len += len;
199 return 0;
200 }
201 seq_buf_set_overflow(s);
202 return -1;
203}
204
205#define MAX_MEMHEX_BYTES 8U
206#define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
207
208/**
209 * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
210 * @s: seq_buf descriptor
211 * @mem: The raw memory to write its hex ASCII representation of
212 * @len: The length of the raw memory to copy (in bytes)
213 *
214 * This is similar to seq_buf_putmem() except instead of just copying the
215 * raw memory into the buffer it writes its ASCII representation of it
216 * in hex characters.
217 *
218 * Returns zero on success, -1 on overflow
219 */
220int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
221 unsigned int len)
222{
223 unsigned char hex[HEX_CHARS];
224 const unsigned char *data = mem;
225 unsigned int start_len;
226 int i, j;
227
228 WARN_ON(s->size == 0);
229
230 while (len) {
231 start_len = min(len, HEX_CHARS - 1);
232#ifdef __BIG_ENDIAN
233 for (i = 0, j = 0; i < start_len; i++) {
234#else
235 for (i = start_len-1, j = 0; i >= 0; i--) {
236#endif
237 hex[j++] = hex_asc_hi(data[i]);
238 hex[j++] = hex_asc_lo(data[i]);
239 }
240 if (WARN_ON_ONCE(j == 0 || j/2 > len))
241 break;
242
243 /* j increments twice per loop */
244 len -= j / 2;
245 hex[j++] = ' ';
246
247 seq_buf_putmem(s, hex, j);
248 if (seq_buf_has_overflowed(s))
249 return -1;
250 }
251 return 0;
252}
253
254/**
255 * seq_buf_path - copy a path into the sequence buffer
256 * @s: seq_buf descriptor
257 * @path: path to write into the sequence buffer.
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400258 * @esc: set of characters to escape in the output
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400259 *
260 * Write a path name into the sequence buffer.
261 *
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400262 * Returns the number of written bytes on success, -1 on overflow
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400263 */
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400264int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400265{
Steven Rostedt (Red Hat)01cb06a2014-10-29 17:30:50 -0400266 char *buf;
267 size_t size = seq_buf_get_buf(s, &buf);
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400268 int res = -1;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400269
270 WARN_ON(s->size == 0);
271
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400272 if (size) {
273 char *p = d_path(path, buf, size);
274 if (!IS_ERR(p)) {
275 char *end = mangle_path(buf, p, esc);
276 if (end)
277 res = end - buf;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400278 }
279 }
Steven Rostedt (Red Hat)01cb06a2014-10-29 17:30:50 -0400280 seq_buf_commit(s, res);
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400281
282 return res;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400283}
284
285/**
286 * seq_buf_to_user - copy the squence buffer to user space
287 * @s: seq_buf descriptor
288 * @ubuf: The userspace memory location to copy to
289 * @cnt: The amount to copy
290 *
291 * Copies the sequence buffer into the userspace memory pointed to
292 * by @ubuf. It starts from the last read position (@s->readpos)
293 * and writes up to @cnt characters or till it reaches the end of
294 * the content in the buffer (@s->len), which ever comes first.
295 *
296 * On success, it returns a positive number of the number of bytes
297 * it copied.
298 *
299 * On failure it returns -EBUSY if all of the content in the
300 * sequence has been already read, which includes nothing in the
301 * sequence (@s->len == @s->readpos).
302 *
303 * Returns -EFAULT if the copy to userspace fails.
304 */
305int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
306{
307 int len;
308 int ret;
309
310 if (!cnt)
311 return 0;
312
Jerry Snitselaarff078d82015-11-16 12:57:28 -0700313 len = seq_buf_used(s);
314
315 if (len <= s->readpos)
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400316 return -EBUSY;
317
Jerry Snitselaarff078d82015-11-16 12:57:28 -0700318 len -= s->readpos;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400319 if (cnt > len)
320 cnt = len;
321 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
322 if (ret == cnt)
323 return -EFAULT;
324
325 cnt -= ret;
326
327 s->readpos += cnt;
328 return cnt;
329}