Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 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 | */ |
Andy Shevchenko | 8a566f9 | 2024-02-15 16:22:55 +0200 | [diff] [blame] | 16 | |
| 17 | #include <linux/bug.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/export.h> |
| 20 | #include <linux/hex.h> |
| 21 | #include <linux/minmax.h> |
| 22 | #include <linux/printk.h> |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 23 | #include <linux/seq_buf.h> |
Andy Shevchenko | 8a566f9 | 2024-02-15 16:22:55 +0200 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
| 25 | #include <linux/sprintf.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/uaccess.h> |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 29 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 30 | /** |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 31 | * seq_buf_can_fit - can the new data fit in the current buffer? |
| 32 | * @s: the seq_buf descriptor |
| 33 | * @len: The length to see if it can fit in the current buffer |
| 34 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 35 | * Returns: true if there's enough unused space in the seq_buf buffer |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 36 | * to fit the amount of new data according to @len. |
| 37 | */ |
| 38 | static bool seq_buf_can_fit(struct seq_buf *s, size_t len) |
| 39 | { |
Steven Rostedt (Red Hat) | 8cd709a | 2014-10-29 15:26:09 -0400 | [diff] [blame] | 40 | return s->len + len <= s->size; |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 44 | * seq_buf_print_seq - move the contents of seq_buf into a seq_file |
| 45 | * @m: the seq_file descriptor that is the destination |
| 46 | * @s: the seq_buf descriptor that is the source. |
| 47 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 48 | * Returns: zero on success, non-zero otherwise. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 49 | */ |
| 50 | int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s) |
| 51 | { |
Steven Rostedt (Red Hat) | eeab981 | 2014-11-06 16:38:28 -0500 | [diff] [blame] | 52 | unsigned int len = seq_buf_used(s); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 53 | |
| 54 | return seq_write(m, s->buffer, len); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * seq_buf_vprintf - sequence printing of information. |
| 59 | * @s: seq_buf descriptor |
| 60 | * @fmt: printf format string |
| 61 | * @args: va_list of arguments from a printf() type function |
| 62 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 63 | * Writes a vnprintf() format into the sequence buffer. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 64 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 65 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 66 | */ |
| 67 | int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args) |
| 68 | { |
| 69 | int len; |
| 70 | |
| 71 | WARN_ON(s->size == 0); |
| 72 | |
| 73 | if (s->len < s->size) { |
| 74 | len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args); |
Steven Rostedt (Red Hat) | 4a8fe4e | 2015-03-04 09:56:02 -0500 | [diff] [blame] | 75 | if (s->len + len < s->size) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 76 | s->len += len; |
| 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | seq_buf_set_overflow(s); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * seq_buf_printf - sequence printing of information |
| 86 | * @s: seq_buf descriptor |
| 87 | * @fmt: printf format string |
| 88 | * |
| 89 | * Writes a printf() format into the sequence buffer. |
| 90 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 91 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 92 | */ |
| 93 | int seq_buf_printf(struct seq_buf *s, const char *fmt, ...) |
| 94 | { |
| 95 | va_list ap; |
| 96 | int ret; |
| 97 | |
| 98 | va_start(ap, fmt); |
| 99 | ret = seq_buf_vprintf(s, fmt, ap); |
| 100 | va_end(ap); |
| 101 | |
| 102 | return ret; |
| 103 | } |
Vaibhav Jain | 97c02c7 | 2020-06-15 18:14:03 +0530 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(seq_buf_printf); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 105 | |
Sergey Senozhatsky | 96928d9 | 2023-04-15 19:01:10 +0900 | [diff] [blame] | 106 | /** |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 107 | * seq_buf_do_printk - printk() seq_buf line by line |
Sergey Senozhatsky | 96928d9 | 2023-04-15 19:01:10 +0900 | [diff] [blame] | 108 | * @s: seq_buf descriptor |
| 109 | * @lvl: printk level |
| 110 | * |
| 111 | * printk()-s a multi-line sequential buffer line by line. The function |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 112 | * makes sure that the buffer in @s is NUL-terminated and safe to read |
Sergey Senozhatsky | 96928d9 | 2023-04-15 19:01:10 +0900 | [diff] [blame] | 113 | * as a string. |
| 114 | */ |
| 115 | void seq_buf_do_printk(struct seq_buf *s, const char *lvl) |
| 116 | { |
| 117 | const char *start, *lf; |
| 118 | |
| 119 | if (s->size == 0 || s->len == 0) |
| 120 | return; |
| 121 | |
Kees Cook | dcc4e57 | 2023-10-27 08:56:38 -0700 | [diff] [blame] | 122 | start = seq_buf_str(s); |
Sergey Senozhatsky | 96928d9 | 2023-04-15 19:01:10 +0900 | [diff] [blame] | 123 | while ((lf = strchr(start, '\n'))) { |
| 124 | int len = lf - start + 1; |
| 125 | |
| 126 | printk("%s%.*s", lvl, len, start); |
| 127 | start = ++lf; |
| 128 | } |
| 129 | |
| 130 | /* No trailing LF */ |
| 131 | if (start < s->buffer + s->len) |
| 132 | printk("%s%s\n", lvl, start); |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(seq_buf_do_printk); |
| 135 | |
Steven Rostedt (Red Hat) | 2448913 | 2014-11-03 18:53:50 -0500 | [diff] [blame] | 136 | #ifdef CONFIG_BINARY_PRINTF |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 137 | /** |
| 138 | * seq_buf_bprintf - Write the printf string from binary arguments |
| 139 | * @s: seq_buf descriptor |
| 140 | * @fmt: The format string for the @binary arguments |
| 141 | * @binary: The binary arguments for @fmt. |
| 142 | * |
| 143 | * When recording in a fast path, a printf may be recorded with just |
| 144 | * saving the format and the arguments as they were passed to the |
| 145 | * function, instead of wasting cycles converting the arguments into |
| 146 | * ASCII characters. Instead, the arguments are saved in a 32 bit |
| 147 | * word array that is defined by the format string constraints. |
| 148 | * |
| 149 | * This function will take the format and the binary array and finish |
| 150 | * the conversion into the ASCII string within the buffer. |
| 151 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 152 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 153 | */ |
| 154 | int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary) |
| 155 | { |
| 156 | unsigned int len = seq_buf_buffer_left(s); |
| 157 | int ret; |
| 158 | |
| 159 | WARN_ON(s->size == 0); |
| 160 | |
| 161 | if (s->len < s->size) { |
| 162 | ret = bstr_printf(s->buffer + s->len, len, fmt, binary); |
Steven Rostedt (Red Hat) | 4d4eb4d | 2015-03-04 23:30:45 -0500 | [diff] [blame] | 163 | if (s->len + ret < s->size) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 164 | s->len += ret; |
| 165 | return 0; |
| 166 | } |
| 167 | } |
| 168 | seq_buf_set_overflow(s); |
| 169 | return -1; |
| 170 | } |
Steven Rostedt (Red Hat) | 2448913 | 2014-11-03 18:53:50 -0500 | [diff] [blame] | 171 | #endif /* CONFIG_BINARY_PRINTF */ |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * seq_buf_puts - sequence printing of simple string |
| 175 | * @s: seq_buf descriptor |
| 176 | * @str: simple string to record |
| 177 | * |
| 178 | * Copy a simple string into the sequence buffer. |
| 179 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 180 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 181 | */ |
| 182 | int seq_buf_puts(struct seq_buf *s, const char *str) |
| 183 | { |
Michael Ellerman | 29924e5 | 2018-10-19 15:21:09 +1100 | [diff] [blame] | 184 | size_t len = strlen(str); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 185 | |
| 186 | WARN_ON(s->size == 0); |
| 187 | |
Michael Ellerman | 0464ed2 | 2018-10-19 15:21:08 +1100 | [diff] [blame] | 188 | /* Add 1 to len for the trailing null byte which must be there */ |
| 189 | len += 1; |
| 190 | |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 191 | if (seq_buf_can_fit(s, len)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 192 | memcpy(s->buffer + s->len, str, len); |
Michael Ellerman | 0464ed2 | 2018-10-19 15:21:08 +1100 | [diff] [blame] | 193 | /* Don't count the trailing null byte against the capacity */ |
| 194 | s->len += len - 1; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | seq_buf_set_overflow(s); |
| 198 | return -1; |
| 199 | } |
Christophe JAILLET | 70a9aff | 2023-11-01 18:59:06 +0100 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(seq_buf_puts); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * seq_buf_putc - sequence printing of simple character |
| 204 | * @s: seq_buf descriptor |
| 205 | * @c: simple character to record |
| 206 | * |
| 207 | * Copy a single character into the sequence buffer. |
| 208 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 209 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 210 | */ |
| 211 | int seq_buf_putc(struct seq_buf *s, unsigned char c) |
| 212 | { |
| 213 | WARN_ON(s->size == 0); |
| 214 | |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 215 | if (seq_buf_can_fit(s, 1)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 216 | s->buffer[s->len++] = c; |
| 217 | return 0; |
| 218 | } |
| 219 | seq_buf_set_overflow(s); |
| 220 | return -1; |
| 221 | } |
Christophe JAILLET | 685b38c | 2023-11-01 18:59:05 +0100 | [diff] [blame] | 222 | EXPORT_SYMBOL_GPL(seq_buf_putc); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 223 | |
| 224 | /** |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 225 | * seq_buf_putmem - write raw data into the sequence buffer |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 226 | * @s: seq_buf descriptor |
| 227 | * @mem: The raw memory to copy into the buffer |
| 228 | * @len: The length of the raw memory to copy (in bytes) |
| 229 | * |
| 230 | * There may be cases where raw memory needs to be written into the |
| 231 | * buffer and a strcpy() would not work. Using this function allows |
| 232 | * for such cases. |
| 233 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 234 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 235 | */ |
| 236 | int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len) |
| 237 | { |
| 238 | WARN_ON(s->size == 0); |
| 239 | |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 240 | if (seq_buf_can_fit(s, len)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 241 | memcpy(s->buffer + s->len, mem, len); |
| 242 | s->len += len; |
| 243 | return 0; |
| 244 | } |
| 245 | seq_buf_set_overflow(s); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | #define MAX_MEMHEX_BYTES 8U |
| 250 | #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1) |
| 251 | |
| 252 | /** |
| 253 | * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex |
| 254 | * @s: seq_buf descriptor |
| 255 | * @mem: The raw memory to write its hex ASCII representation of |
| 256 | * @len: The length of the raw memory to copy (in bytes) |
| 257 | * |
| 258 | * This is similar to seq_buf_putmem() except instead of just copying the |
| 259 | * raw memory into the buffer it writes its ASCII representation of it |
| 260 | * in hex characters. |
| 261 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 262 | * Returns: zero on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 263 | */ |
| 264 | int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, |
| 265 | unsigned int len) |
| 266 | { |
| 267 | unsigned char hex[HEX_CHARS]; |
| 268 | const unsigned char *data = mem; |
| 269 | unsigned int start_len; |
| 270 | int i, j; |
| 271 | |
| 272 | WARN_ON(s->size == 0); |
| 273 | |
Yun Zhou | d3b1603 | 2021-06-26 11:21:55 +0800 | [diff] [blame] | 274 | BUILD_BUG_ON(MAX_MEMHEX_BYTES * 2 >= HEX_CHARS); |
| 275 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 276 | while (len) { |
Yun Zhou | d3b1603 | 2021-06-26 11:21:55 +0800 | [diff] [blame] | 277 | start_len = min(len, MAX_MEMHEX_BYTES); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 278 | #ifdef __BIG_ENDIAN |
| 279 | for (i = 0, j = 0; i < start_len; i++) { |
| 280 | #else |
| 281 | for (i = start_len-1, j = 0; i >= 0; i--) { |
| 282 | #endif |
| 283 | hex[j++] = hex_asc_hi(data[i]); |
| 284 | hex[j++] = hex_asc_lo(data[i]); |
| 285 | } |
| 286 | if (WARN_ON_ONCE(j == 0 || j/2 > len)) |
| 287 | break; |
| 288 | |
| 289 | /* j increments twice per loop */ |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 290 | hex[j++] = ' '; |
| 291 | |
| 292 | seq_buf_putmem(s, hex, j); |
| 293 | if (seq_buf_has_overflowed(s)) |
| 294 | return -1; |
Yun Zhou | 6a2cbc5 | 2021-06-26 11:21:56 +0800 | [diff] [blame] | 295 | |
| 296 | len -= start_len; |
| 297 | data += start_len; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * seq_buf_path - copy a path into the sequence buffer |
| 304 | * @s: seq_buf descriptor |
| 305 | * @path: path to write into the sequence buffer. |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 306 | * @esc: set of characters to escape in the output |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 307 | * |
| 308 | * Write a path name into the sequence buffer. |
| 309 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 310 | * Returns: the number of written bytes on success, -1 on overflow. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 311 | */ |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 312 | int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc) |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 313 | { |
Steven Rostedt (Red Hat) | 01cb06a | 2014-10-29 17:30:50 -0400 | [diff] [blame] | 314 | char *buf; |
| 315 | size_t size = seq_buf_get_buf(s, &buf); |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 316 | int res = -1; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 317 | |
| 318 | WARN_ON(s->size == 0); |
| 319 | |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 320 | if (size) { |
| 321 | char *p = d_path(path, buf, size); |
| 322 | if (!IS_ERR(p)) { |
| 323 | char *end = mangle_path(buf, p, esc); |
| 324 | if (end) |
| 325 | res = end - buf; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 326 | } |
| 327 | } |
Steven Rostedt (Red Hat) | 01cb06a | 2014-10-29 17:30:50 -0400 | [diff] [blame] | 328 | seq_buf_commit(s, res); |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 329 | |
| 330 | return res; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
Zhen Lei | 9dbbc3b | 2021-07-07 18:07:31 -0700 | [diff] [blame] | 334 | * seq_buf_to_user - copy the sequence buffer to user space |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 335 | * @s: seq_buf descriptor |
| 336 | * @ubuf: The userspace memory location to copy to |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 337 | * @start: The first byte in the buffer to copy |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 338 | * @cnt: The amount to copy |
| 339 | * |
| 340 | * Copies the sequence buffer into the userspace memory pointed to |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 341 | * by @ubuf. It starts from @start and writes up to @cnt characters |
| 342 | * or until it reaches the end of the content in the buffer (@s->len), |
| 343 | * whichever comes first. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 344 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 345 | * Returns: |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 346 | * On success, it returns a positive number of the number of bytes |
| 347 | * it copied. |
| 348 | * |
| 349 | * On failure it returns -EBUSY if all of the content in the |
| 350 | * sequence has been already read, which includes nothing in the |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 351 | * sequence (@s->len == @start). |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 352 | * |
| 353 | * Returns -EFAULT if the copy to userspace fails. |
| 354 | */ |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 355 | int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, size_t start, int cnt) |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 356 | { |
| 357 | int len; |
| 358 | int ret; |
| 359 | |
| 360 | if (!cnt) |
| 361 | return 0; |
| 362 | |
Jerry Snitselaar | ff078d8 | 2015-11-16 12:57:28 -0700 | [diff] [blame] | 363 | len = seq_buf_used(s); |
| 364 | |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 365 | if (len <= start) |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 366 | return -EBUSY; |
| 367 | |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 368 | len -= start; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 369 | if (cnt > len) |
| 370 | cnt = len; |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 371 | ret = copy_to_user(ubuf, s->buffer + start, cnt); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 372 | if (ret == cnt) |
| 373 | return -EFAULT; |
| 374 | |
Matthew Wilcox (Oracle) | d0ed46b | 2023-10-20 04:35:45 +0100 | [diff] [blame] | 375 | return cnt - ret; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 376 | } |
Piotr Maziarz | 353cade | 2019-11-07 13:45:37 +0100 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * seq_buf_hex_dump - print formatted hex dump into the sequence buffer |
| 380 | * @s: seq_buf descriptor |
| 381 | * @prefix_str: string to prefix each line with; |
| 382 | * caller supplies trailing spaces for alignment if desired |
| 383 | * @prefix_type: controls whether prefix of an offset, address, or none |
| 384 | * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE) |
| 385 | * @rowsize: number of bytes to print per line; must be 16 or 32 |
| 386 | * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) |
| 387 | * @buf: data blob to dump |
| 388 | * @len: number of bytes in the @buf |
| 389 | * @ascii: include ASCII after the hex output |
| 390 | * |
| 391 | * Function is an analogue of print_hex_dump() and thus has similar interface. |
| 392 | * |
| 393 | * linebuf size is maximal length for one line. |
| 394 | * 32 * 3 - maximum bytes per line, each printed into 2 chars + 1 for |
| 395 | * separating space |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 396 | * 2 - spaces separating hex dump and ASCII representation |
| 397 | * 32 - ASCII representation |
Piotr Maziarz | 353cade | 2019-11-07 13:45:37 +0100 | [diff] [blame] | 398 | * 1 - terminating '\0' |
| 399 | * |
Andy Shevchenko | 6efe4d18 | 2024-02-15 17:25:06 +0200 | [diff] [blame] | 400 | * Returns: zero on success, -1 on overflow. |
Piotr Maziarz | 353cade | 2019-11-07 13:45:37 +0100 | [diff] [blame] | 401 | */ |
| 402 | int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, int prefix_type, |
| 403 | int rowsize, int groupsize, |
| 404 | const void *buf, size_t len, bool ascii) |
| 405 | { |
| 406 | const u8 *ptr = buf; |
| 407 | int i, linelen, remaining = len; |
| 408 | unsigned char linebuf[32 * 3 + 2 + 32 + 1]; |
| 409 | int ret; |
| 410 | |
| 411 | if (rowsize != 16 && rowsize != 32) |
| 412 | rowsize = 16; |
| 413 | |
| 414 | for (i = 0; i < len; i += rowsize) { |
| 415 | linelen = min(remaining, rowsize); |
| 416 | remaining -= rowsize; |
| 417 | |
| 418 | hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, |
| 419 | linebuf, sizeof(linebuf), ascii); |
| 420 | |
| 421 | switch (prefix_type) { |
| 422 | case DUMP_PREFIX_ADDRESS: |
| 423 | ret = seq_buf_printf(s, "%s%p: %s\n", |
| 424 | prefix_str, ptr + i, linebuf); |
| 425 | break; |
| 426 | case DUMP_PREFIX_OFFSET: |
| 427 | ret = seq_buf_printf(s, "%s%.8x: %s\n", |
| 428 | prefix_str, i, linebuf); |
| 429 | break; |
| 430 | default: |
| 431 | ret = seq_buf_printf(s, "%s%s\n", prefix_str, linebuf); |
| 432 | break; |
| 433 | } |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | } |
| 437 | return 0; |
| 438 | } |