1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 
20 /**
21  * 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  */
seq_buf_can_fit(struct seq_buf * s,size_t len)28 static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
29 {
30 	return s->len + len <= s->size;
31 }
32 
33 /**
34  * 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  */
seq_buf_print_seq(struct seq_file * m,struct seq_buf * s)40 int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s)
41 {
42 	unsigned int len = seq_buf_used(s);
43 
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  */
seq_buf_vprintf(struct seq_buf * s,const char * fmt,va_list args)57 int 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);
65 		if (s->len + len < s->size) {
66 			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  */
seq_buf_printf(struct seq_buf * s,const char * fmt,...)83 int 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 
95 #ifdef CONFIG_BINARY_PRINTF
96 /**
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  */
seq_buf_bprintf(struct seq_buf * s,const char * fmt,const u32 * binary)113 int 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);
122 		if (s->len + ret < s->size) {
123 			s->len += ret;
124 			return 0;
125 		}
126 	}
127 	seq_buf_set_overflow(s);
128 	return -1;
129 }
130 #endif /* CONFIG_BINARY_PRINTF */
131 
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  */
seq_buf_puts(struct seq_buf * s,const char * str)141 int 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 
147 	/* Add 1 to len for the trailing null byte which must be there */
148 	len += 1;
149 
150 	if (seq_buf_can_fit(s, len)) {
151 		memcpy(s->buffer + s->len, str, len);
152 		/* Don't count the trailing null byte against the capacity */
153 		s->len += len - 1;
154 		return 0;
155 	}
156 	seq_buf_set_overflow(s);
157 	return -1;
158 }
159 
160 /**
161  * seq_buf_putc - sequence printing of simple character
162  * @s: seq_buf descriptor
163  * @c: simple character to record
164  *
165  * Copy a single character into the sequence buffer.
166  *
167  * Returns zero on success, -1 on overflow
168  */
seq_buf_putc(struct seq_buf * s,unsigned char c)169 int seq_buf_putc(struct seq_buf *s, unsigned char c)
170 {
171 	WARN_ON(s->size == 0);
172 
173 	if (seq_buf_can_fit(s, 1)) {
174 		s->buffer[s->len++] = c;
175 		return 0;
176 	}
177 	seq_buf_set_overflow(s);
178 	return -1;
179 }
180 
181 /**
182  * seq_buf_putmem - write raw data into the sequenc buffer
183  * @s: seq_buf descriptor
184  * @mem: The raw memory to copy into the buffer
185  * @len: The length of the raw memory to copy (in bytes)
186  *
187  * There may be cases where raw memory needs to be written into the
188  * buffer and a strcpy() would not work. Using this function allows
189  * for such cases.
190  *
191  * Returns zero on success, -1 on overflow
192  */
seq_buf_putmem(struct seq_buf * s,const void * mem,unsigned int len)193 int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
194 {
195 	WARN_ON(s->size == 0);
196 
197 	if (seq_buf_can_fit(s, len)) {
198 		memcpy(s->buffer + s->len, mem, len);
199 		s->len += len;
200 		return 0;
201 	}
202 	seq_buf_set_overflow(s);
203 	return -1;
204 }
205 
206 #define MAX_MEMHEX_BYTES	8U
207 #define HEX_CHARS		(MAX_MEMHEX_BYTES*2 + 1)
208 
209 /**
210  * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
211  * @s: seq_buf descriptor
212  * @mem: The raw memory to write its hex ASCII representation of
213  * @len: The length of the raw memory to copy (in bytes)
214  *
215  * This is similar to seq_buf_putmem() except instead of just copying the
216  * raw memory into the buffer it writes its ASCII representation of it
217  * in hex characters.
218  *
219  * Returns zero on success, -1 on overflow
220  */
seq_buf_putmem_hex(struct seq_buf * s,const void * mem,unsigned int len)221 int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
222 		       unsigned int len)
223 {
224 	unsigned char hex[HEX_CHARS];
225 	const unsigned char *data = mem;
226 	unsigned int start_len;
227 	int i, j;
228 
229 	WARN_ON(s->size == 0);
230 
231 	BUILD_BUG_ON(MAX_MEMHEX_BYTES * 2 >= HEX_CHARS);
232 
233 	while (len) {
234 		start_len = min(len, MAX_MEMHEX_BYTES);
235 #ifdef __BIG_ENDIAN
236 		for (i = 0, j = 0; i < start_len; i++) {
237 #else
238 		for (i = start_len-1, j = 0; i >= 0; i--) {
239 #endif
240 			hex[j++] = hex_asc_hi(data[i]);
241 			hex[j++] = hex_asc_lo(data[i]);
242 		}
243 		if (WARN_ON_ONCE(j == 0 || j/2 > len))
244 			break;
245 
246 		/* j increments twice per loop */
247 		hex[j++] = ' ';
248 
249 		seq_buf_putmem(s, hex, j);
250 		if (seq_buf_has_overflowed(s))
251 			return -1;
252 
253 		len -= start_len;
254 		data += start_len;
255 	}
256 	return 0;
257 }
258 
259 /**
260  * seq_buf_path - copy a path into the sequence buffer
261  * @s: seq_buf descriptor
262  * @path: path to write into the sequence buffer.
263  * @esc: set of characters to escape in the output
264  *
265  * Write a path name into the sequence buffer.
266  *
267  * Returns the number of written bytes on success, -1 on overflow
268  */
269 int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
270 {
271 	char *buf;
272 	size_t size = seq_buf_get_buf(s, &buf);
273 	int res = -1;
274 
275 	WARN_ON(s->size == 0);
276 
277 	if (size) {
278 		char *p = d_path(path, buf, size);
279 		if (!IS_ERR(p)) {
280 			char *end = mangle_path(buf, p, esc);
281 			if (end)
282 				res = end - buf;
283 		}
284 	}
285 	seq_buf_commit(s, res);
286 
287 	return res;
288 }
289 
290 /**
291  * seq_buf_to_user - copy the squence buffer to user space
292  * @s: seq_buf descriptor
293  * @ubuf: The userspace memory location to copy to
294  * @cnt: The amount to copy
295  *
296  * Copies the sequence buffer into the userspace memory pointed to
297  * by @ubuf. It starts from the last read position (@s->readpos)
298  * and writes up to @cnt characters or till it reaches the end of
299  * the content in the buffer (@s->len), which ever comes first.
300  *
301  * On success, it returns a positive number of the number of bytes
302  * it copied.
303  *
304  * On failure it returns -EBUSY if all of the content in the
305  * sequence has been already read, which includes nothing in the
306  * sequence (@s->len == @s->readpos).
307  *
308  * Returns -EFAULT if the copy to userspace fails.
309  */
310 int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
311 {
312 	int len;
313 	int ret;
314 
315 	if (!cnt)
316 		return 0;
317 
318 	len = seq_buf_used(s);
319 
320 	if (len <= s->readpos)
321 		return -EBUSY;
322 
323 	len -= s->readpos;
324 	if (cnt > len)
325 		cnt = len;
326 	ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
327 	if (ret == cnt)
328 		return -EFAULT;
329 
330 	cnt -= ret;
331 
332 	s->readpos += cnt;
333 	return cnt;
334 }
335