Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1 | /* |
| 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) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 19 | /** |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 20 | * 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 | */ |
| 27 | static bool seq_buf_can_fit(struct seq_buf *s, size_t len) |
| 28 | { |
Steven Rostedt (Red Hat) | 8cd709a | 2014-10-29 15:26:09 -0400 | [diff] [blame^] | 29 | return s->len + len <= s->size; |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /** |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 33 | * 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 | */ |
| 39 | int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s) |
| 40 | { |
Steven Rostedt (Red Hat) | eeab981 | 2014-11-06 16:38:28 -0500 | [diff] [blame] | 41 | unsigned int len = seq_buf_used(s); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 42 | |
| 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 | */ |
| 56 | int 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) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 64 | if (seq_buf_can_fit(s, len)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 65 | 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 | */ |
| 82 | int 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 | */ |
| 104 | int 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 | /* |
Steven Rostedt (Red Hat) | 8cd709a | 2014-10-29 15:26:09 -0400 | [diff] [blame^] | 113 | * Note, because bitmap_scnprintf() only returns the number of bytes |
| 114 | * written and not the number that would be written, we use the last |
| 115 | * byte of the buffer to let us know if we overflowed. There's a small |
| 116 | * chance that the bitmap could have fit exactly inside the buffer, but |
| 117 | * it's not that critical if that does happen. |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 118 | */ |
| 119 | if (len > 1) { |
| 120 | ret = bitmap_scnprintf(s->buffer + s->len, len, maskp, nmaskbits); |
| 121 | if (ret < len) { |
| 122 | s->len += ret; |
| 123 | return 0; |
| 124 | } |
| 125 | } |
| 126 | seq_buf_set_overflow(s); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * seq_buf_bprintf - Write the printf string from binary arguments |
| 132 | * @s: seq_buf descriptor |
| 133 | * @fmt: The format string for the @binary arguments |
| 134 | * @binary: The binary arguments for @fmt. |
| 135 | * |
| 136 | * When recording in a fast path, a printf may be recorded with just |
| 137 | * saving the format and the arguments as they were passed to the |
| 138 | * function, instead of wasting cycles converting the arguments into |
| 139 | * ASCII characters. Instead, the arguments are saved in a 32 bit |
| 140 | * word array that is defined by the format string constraints. |
| 141 | * |
| 142 | * This function will take the format and the binary array and finish |
| 143 | * the conversion into the ASCII string within the buffer. |
| 144 | * |
| 145 | * Returns zero on success, -1 on overflow. |
| 146 | */ |
| 147 | int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary) |
| 148 | { |
| 149 | unsigned int len = seq_buf_buffer_left(s); |
| 150 | int ret; |
| 151 | |
| 152 | WARN_ON(s->size == 0); |
| 153 | |
| 154 | if (s->len < s->size) { |
| 155 | ret = bstr_printf(s->buffer + s->len, len, fmt, binary); |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 156 | if (seq_buf_can_fit(s, ret)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 157 | s->len += ret; |
| 158 | return 0; |
| 159 | } |
| 160 | } |
| 161 | seq_buf_set_overflow(s); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * seq_buf_puts - sequence printing of simple string |
| 167 | * @s: seq_buf descriptor |
| 168 | * @str: simple string to record |
| 169 | * |
| 170 | * Copy a simple string into the sequence buffer. |
| 171 | * |
| 172 | * Returns zero on success, -1 on overflow |
| 173 | */ |
| 174 | int seq_buf_puts(struct seq_buf *s, const char *str) |
| 175 | { |
| 176 | unsigned int len = strlen(str); |
| 177 | |
| 178 | WARN_ON(s->size == 0); |
| 179 | |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 180 | if (seq_buf_can_fit(s, len)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 181 | memcpy(s->buffer + s->len, str, len); |
| 182 | s->len += len; |
| 183 | return 0; |
| 184 | } |
| 185 | seq_buf_set_overflow(s); |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * seq_buf_putc - sequence printing of simple character |
| 191 | * @s: seq_buf descriptor |
| 192 | * @c: simple character to record |
| 193 | * |
| 194 | * Copy a single character into the sequence buffer. |
| 195 | * |
| 196 | * Returns zero on success, -1 on overflow |
| 197 | */ |
| 198 | int seq_buf_putc(struct seq_buf *s, unsigned char c) |
| 199 | { |
| 200 | WARN_ON(s->size == 0); |
| 201 | |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 202 | if (seq_buf_can_fit(s, 1)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 203 | s->buffer[s->len++] = c; |
| 204 | return 0; |
| 205 | } |
| 206 | seq_buf_set_overflow(s); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * seq_buf_putmem - write raw data into the sequenc buffer |
| 212 | * @s: seq_buf descriptor |
| 213 | * @mem: The raw memory to copy into the buffer |
| 214 | * @len: The length of the raw memory to copy (in bytes) |
| 215 | * |
| 216 | * There may be cases where raw memory needs to be written into the |
| 217 | * buffer and a strcpy() would not work. Using this function allows |
| 218 | * for such cases. |
| 219 | * |
| 220 | * Returns zero on success, -1 on overflow |
| 221 | */ |
| 222 | int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len) |
| 223 | { |
| 224 | WARN_ON(s->size == 0); |
| 225 | |
Steven Rostedt (Red Hat) | 9b77215 | 2014-11-14 16:18:14 -0500 | [diff] [blame] | 226 | if (seq_buf_can_fit(s, len)) { |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 227 | memcpy(s->buffer + s->len, mem, len); |
| 228 | s->len += len; |
| 229 | return 0; |
| 230 | } |
| 231 | seq_buf_set_overflow(s); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | #define MAX_MEMHEX_BYTES 8U |
| 236 | #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1) |
| 237 | |
| 238 | /** |
| 239 | * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex |
| 240 | * @s: seq_buf descriptor |
| 241 | * @mem: The raw memory to write its hex ASCII representation of |
| 242 | * @len: The length of the raw memory to copy (in bytes) |
| 243 | * |
| 244 | * This is similar to seq_buf_putmem() except instead of just copying the |
| 245 | * raw memory into the buffer it writes its ASCII representation of it |
| 246 | * in hex characters. |
| 247 | * |
| 248 | * Returns zero on success, -1 on overflow |
| 249 | */ |
| 250 | int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, |
| 251 | unsigned int len) |
| 252 | { |
| 253 | unsigned char hex[HEX_CHARS]; |
| 254 | const unsigned char *data = mem; |
| 255 | unsigned int start_len; |
| 256 | int i, j; |
| 257 | |
| 258 | WARN_ON(s->size == 0); |
| 259 | |
| 260 | while (len) { |
| 261 | start_len = min(len, HEX_CHARS - 1); |
| 262 | #ifdef __BIG_ENDIAN |
| 263 | for (i = 0, j = 0; i < start_len; i++) { |
| 264 | #else |
| 265 | for (i = start_len-1, j = 0; i >= 0; i--) { |
| 266 | #endif |
| 267 | hex[j++] = hex_asc_hi(data[i]); |
| 268 | hex[j++] = hex_asc_lo(data[i]); |
| 269 | } |
| 270 | if (WARN_ON_ONCE(j == 0 || j/2 > len)) |
| 271 | break; |
| 272 | |
| 273 | /* j increments twice per loop */ |
| 274 | len -= j / 2; |
| 275 | hex[j++] = ' '; |
| 276 | |
| 277 | seq_buf_putmem(s, hex, j); |
| 278 | if (seq_buf_has_overflowed(s)) |
| 279 | return -1; |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * seq_buf_path - copy a path into the sequence buffer |
| 286 | * @s: seq_buf descriptor |
| 287 | * @path: path to write into the sequence buffer. |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 288 | * @esc: set of characters to escape in the output |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 289 | * |
| 290 | * Write a path name into the sequence buffer. |
| 291 | * |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 292 | * 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] | 293 | */ |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 294 | 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] | 295 | { |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 296 | char *buf = s->buffer + s->len; |
| 297 | size_t size = seq_buf_buffer_left(s); |
| 298 | int res = -1; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 299 | |
| 300 | WARN_ON(s->size == 0); |
| 301 | |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 302 | if (size) { |
| 303 | char *p = d_path(path, buf, size); |
| 304 | if (!IS_ERR(p)) { |
| 305 | char *end = mangle_path(buf, p, esc); |
| 306 | if (end) |
| 307 | res = end - buf; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 308 | } |
| 309 | } |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 310 | if (res > 0) |
| 311 | s->len += res; |
| 312 | |
| 313 | return res; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * seq_buf_to_user - copy the squence buffer to user space |
| 318 | * @s: seq_buf descriptor |
| 319 | * @ubuf: The userspace memory location to copy to |
| 320 | * @cnt: The amount to copy |
| 321 | * |
| 322 | * Copies the sequence buffer into the userspace memory pointed to |
| 323 | * by @ubuf. It starts from the last read position (@s->readpos) |
| 324 | * and writes up to @cnt characters or till it reaches the end of |
| 325 | * the content in the buffer (@s->len), which ever comes first. |
| 326 | * |
| 327 | * On success, it returns a positive number of the number of bytes |
| 328 | * it copied. |
| 329 | * |
| 330 | * On failure it returns -EBUSY if all of the content in the |
| 331 | * sequence has been already read, which includes nothing in the |
| 332 | * sequence (@s->len == @s->readpos). |
| 333 | * |
| 334 | * Returns -EFAULT if the copy to userspace fails. |
| 335 | */ |
| 336 | int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt) |
| 337 | { |
| 338 | int len; |
| 339 | int ret; |
| 340 | |
| 341 | if (!cnt) |
| 342 | return 0; |
| 343 | |
| 344 | if (s->len <= s->readpos) |
| 345 | return -EBUSY; |
| 346 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 347 | len = seq_buf_used(s) - s->readpos; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 348 | if (cnt > len) |
| 349 | cnt = len; |
| 350 | ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); |
| 351 | if (ret == cnt) |
| 352 | return -EFAULT; |
| 353 | |
| 354 | cnt -= ret; |
| 355 | |
| 356 | s->readpos += cnt; |
| 357 | return cnt; |
| 358 | } |