blob: 11f2ae0f90996ac0463ae712785c0135e3576fb8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04002/*
3 * seq_buf.c
4 *
5 * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 *
7 * The seq_buf is a handy tool that allows you to pass a descriptor around
8 * to a buffer that other functions can write to. It is similar to the
9 * seq_file functionality but has some differences.
10 *
11 * To use it, the seq_buf must be initialized with seq_buf_init().
12 * This will set up the counters within the descriptor. You can call
13 * seq_buf_init() more than once to reset the seq_buf to start
14 * from scratch.
15 */
16#include <linux/uaccess.h>
17#include <linux/seq_file.h>
18#include <linux/seq_buf.h>
19
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040020/**
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -050021 * seq_buf_can_fit - can the new data fit in the current buffer?
22 * @s: the seq_buf descriptor
23 * @len: The length to see if it can fit in the current buffer
24 *
25 * Returns true if there's enough unused space in the seq_buf buffer
26 * to fit the amount of new data according to @len.
27 */
28static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
29{
Steven Rostedt (Red Hat)8cd709a2014-10-29 15:26:09 -040030 return s->len + len <= s->size;
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -050031}
32
33/**
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040034 * seq_buf_print_seq - move the contents of seq_buf into a seq_file
35 * @m: the seq_file descriptor that is the destination
36 * @s: the seq_buf descriptor that is the source.
37 *
38 * Returns zero on success, non zero otherwise
39 */
40int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s)
41{
Steven Rostedt (Red Hat)eeab9812014-11-06 16:38:28 -050042 unsigned int len = seq_buf_used(s);
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040043
44 return seq_write(m, s->buffer, len);
45}
46
47/**
48 * seq_buf_vprintf - sequence printing of information.
49 * @s: seq_buf descriptor
50 * @fmt: printf format string
51 * @args: va_list of arguments from a printf() type function
52 *
53 * Writes a vnprintf() format into the sequencce buffer.
54 *
55 * Returns zero on success, -1 on overflow.
56 */
57int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
58{
59 int len;
60
61 WARN_ON(s->size == 0);
62
63 if (s->len < s->size) {
64 len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
Steven Rostedt (Red Hat)4a8fe4e2015-03-04 09:56:02 -050065 if (s->len + len < s->size) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040066 s->len += len;
67 return 0;
68 }
69 }
70 seq_buf_set_overflow(s);
71 return -1;
72}
73
74/**
75 * seq_buf_printf - sequence printing of information
76 * @s: seq_buf descriptor
77 * @fmt: printf format string
78 *
79 * Writes a printf() format into the sequence buffer.
80 *
81 * Returns zero on success, -1 on overflow.
82 */
83int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
84{
85 va_list ap;
86 int ret;
87
88 va_start(ap, fmt);
89 ret = seq_buf_vprintf(s, fmt, ap);
90 va_end(ap);
91
92 return ret;
93}
94
Steven Rostedt (Red Hat)24489132014-11-03 18:53:50 -050095#ifdef CONFIG_BINARY_PRINTF
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040096/**
97 * seq_buf_bprintf - Write the printf string from binary arguments
98 * @s: seq_buf descriptor
99 * @fmt: The format string for the @binary arguments
100 * @binary: The binary arguments for @fmt.
101 *
102 * When recording in a fast path, a printf may be recorded with just
103 * saving the format and the arguments as they were passed to the
104 * function, instead of wasting cycles converting the arguments into
105 * ASCII characters. Instead, the arguments are saved in a 32 bit
106 * word array that is defined by the format string constraints.
107 *
108 * This function will take the format and the binary array and finish
109 * the conversion into the ASCII string within the buffer.
110 *
111 * Returns zero on success, -1 on overflow.
112 */
113int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
114{
115 unsigned int len = seq_buf_buffer_left(s);
116 int ret;
117
118 WARN_ON(s->size == 0);
119
120 if (s->len < s->size) {
121 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
Steven Rostedt (Red Hat)4d4eb4d2015-03-04 23:30:45 -0500122 if (s->len + ret < s->size) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400123 s->len += ret;
124 return 0;
125 }
126 }
127 seq_buf_set_overflow(s);
128 return -1;
129}
Steven Rostedt (Red Hat)24489132014-11-03 18:53:50 -0500130#endif /* CONFIG_BINARY_PRINTF */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400131
132/**
133 * seq_buf_puts - sequence printing of simple string
134 * @s: seq_buf descriptor
135 * @str: simple string to record
136 *
137 * Copy a simple string into the sequence buffer.
138 *
139 * Returns zero on success, -1 on overflow
140 */
141int seq_buf_puts(struct seq_buf *s, const char *str)
142{
143 unsigned int len = strlen(str);
144
145 WARN_ON(s->size == 0);
146
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -0500147 if (seq_buf_can_fit(s, len)) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400148 memcpy(s->buffer + s->len, str, len);
149 s->len += len;
150 return 0;
151 }
152 seq_buf_set_overflow(s);
153 return -1;
154}
155
156/**
157 * seq_buf_putc - sequence printing of simple character
158 * @s: seq_buf descriptor
159 * @c: simple character to record
160 *
161 * Copy a single character into the sequence buffer.
162 *
163 * Returns zero on success, -1 on overflow
164 */
165int seq_buf_putc(struct seq_buf *s, unsigned char c)
166{
167 WARN_ON(s->size == 0);
168
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -0500169 if (seq_buf_can_fit(s, 1)) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400170 s->buffer[s->len++] = c;
171 return 0;
172 }
173 seq_buf_set_overflow(s);
174 return -1;
175}
176
177/**
178 * seq_buf_putmem - write raw data into the sequenc buffer
179 * @s: seq_buf descriptor
180 * @mem: The raw memory to copy into the buffer
181 * @len: The length of the raw memory to copy (in bytes)
182 *
183 * There may be cases where raw memory needs to be written into the
184 * buffer and a strcpy() would not work. Using this function allows
185 * for such cases.
186 *
187 * Returns zero on success, -1 on overflow
188 */
189int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
190{
191 WARN_ON(s->size == 0);
192
Steven Rostedt (Red Hat)9b772152014-11-14 16:18:14 -0500193 if (seq_buf_can_fit(s, len)) {
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400194 memcpy(s->buffer + s->len, mem, len);
195 s->len += len;
196 return 0;
197 }
198 seq_buf_set_overflow(s);
199 return -1;
200}
201
202#define MAX_MEMHEX_BYTES 8U
203#define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
204
205/**
206 * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
207 * @s: seq_buf descriptor
208 * @mem: The raw memory to write its hex ASCII representation of
209 * @len: The length of the raw memory to copy (in bytes)
210 *
211 * This is similar to seq_buf_putmem() except instead of just copying the
212 * raw memory into the buffer it writes its ASCII representation of it
213 * in hex characters.
214 *
215 * Returns zero on success, -1 on overflow
216 */
217int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
218 unsigned int len)
219{
220 unsigned char hex[HEX_CHARS];
221 const unsigned char *data = mem;
222 unsigned int start_len;
223 int i, j;
224
225 WARN_ON(s->size == 0);
226
227 while (len) {
228 start_len = min(len, HEX_CHARS - 1);
229#ifdef __BIG_ENDIAN
230 for (i = 0, j = 0; i < start_len; i++) {
231#else
232 for (i = start_len-1, j = 0; i >= 0; i--) {
233#endif
234 hex[j++] = hex_asc_hi(data[i]);
235 hex[j++] = hex_asc_lo(data[i]);
236 }
237 if (WARN_ON_ONCE(j == 0 || j/2 > len))
238 break;
239
240 /* j increments twice per loop */
241 len -= j / 2;
242 hex[j++] = ' ';
243
244 seq_buf_putmem(s, hex, j);
245 if (seq_buf_has_overflowed(s))
246 return -1;
247 }
248 return 0;
249}
250
251/**
252 * seq_buf_path - copy a path into the sequence buffer
253 * @s: seq_buf descriptor
254 * @path: path to write into the sequence buffer.
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400255 * @esc: set of characters to escape in the output
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400256 *
257 * Write a path name into the sequence buffer.
258 *
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400259 * Returns the number of written bytes on success, -1 on overflow
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400260 */
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400261int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400262{
Steven Rostedt (Red Hat)01cb06a2014-10-29 17:30:50 -0400263 char *buf;
264 size_t size = seq_buf_get_buf(s, &buf);
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400265 int res = -1;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400266
267 WARN_ON(s->size == 0);
268
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400269 if (size) {
270 char *p = d_path(path, buf, size);
271 if (!IS_ERR(p)) {
272 char *end = mangle_path(buf, p, esc);
273 if (end)
274 res = end - buf;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400275 }
276 }
Steven Rostedt (Red Hat)01cb06a2014-10-29 17:30:50 -0400277 seq_buf_commit(s, res);
Steven Rostedt (Red Hat)dd231802014-10-29 13:48:37 -0400278
279 return res;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400280}
281
282/**
283 * seq_buf_to_user - copy the squence buffer to user space
284 * @s: seq_buf descriptor
285 * @ubuf: The userspace memory location to copy to
286 * @cnt: The amount to copy
287 *
288 * Copies the sequence buffer into the userspace memory pointed to
289 * by @ubuf. It starts from the last read position (@s->readpos)
290 * and writes up to @cnt characters or till it reaches the end of
291 * the content in the buffer (@s->len), which ever comes first.
292 *
293 * On success, it returns a positive number of the number of bytes
294 * it copied.
295 *
296 * On failure it returns -EBUSY if all of the content in the
297 * sequence has been already read, which includes nothing in the
298 * sequence (@s->len == @s->readpos).
299 *
300 * Returns -EFAULT if the copy to userspace fails.
301 */
302int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
303{
304 int len;
305 int ret;
306
307 if (!cnt)
308 return 0;
309
Jerry Snitselaarff078d82015-11-16 12:57:28 -0700310 len = seq_buf_used(s);
311
312 if (len <= s->readpos)
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400313 return -EBUSY;
314
Jerry Snitselaarff078d82015-11-16 12:57:28 -0700315 len -= s->readpos;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -0400316 if (cnt > len)
317 cnt = len;
318 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
319 if (ret == cnt)
320 return -EFAULT;
321
322 cnt -= ret;
323
324 s->readpos += cnt;
325 return cnt;
326}