seq_buf.c 11 KB

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