seq_buf.c 10.0 KB

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