seq_buf.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifdef CONFIG_BINARY_PRINTF
  86. /**
  87. * seq_buf_bprintf - Write the printf string from binary arguments
  88. * @s: seq_buf descriptor
  89. * @fmt: The format string for the @binary arguments
  90. * @binary: The binary arguments for @fmt.
  91. *
  92. * When recording in a fast path, a printf may be recorded with just
  93. * saving the format and the arguments as they were passed to the
  94. * function, instead of wasting cycles converting the arguments into
  95. * ASCII characters. Instead, the arguments are saved in a 32 bit
  96. * word array that is defined by the format string constraints.
  97. *
  98. * This function will take the format and the binary array and finish
  99. * the conversion into the ASCII string within the buffer.
  100. *
  101. * Returns zero on success, -1 on overflow.
  102. */
  103. int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
  104. {
  105. unsigned int len = seq_buf_buffer_left(s);
  106. int ret;
  107. WARN_ON(s->size == 0);
  108. if (s->len < s->size) {
  109. ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
  110. if (s->len + ret < s->size) {
  111. s->len += ret;
  112. return 0;
  113. }
  114. }
  115. seq_buf_set_overflow(s);
  116. return -1;
  117. }
  118. #endif /* CONFIG_BINARY_PRINTF */
  119. /**
  120. * seq_buf_puts - sequence printing of simple string
  121. * @s: seq_buf descriptor
  122. * @str: simple string to record
  123. *
  124. * Copy a simple string into the sequence buffer.
  125. *
  126. * Returns zero on success, -1 on overflow
  127. */
  128. int seq_buf_puts(struct seq_buf *s, const char *str)
  129. {
  130. unsigned int len = strlen(str);
  131. WARN_ON(s->size == 0);
  132. /* Add 1 to len for the trailing null byte which must be there */
  133. len += 1;
  134. if (seq_buf_can_fit(s, len)) {
  135. memcpy(s->buffer + s->len, str, len);
  136. /* Don't count the trailing null byte against the capacity */
  137. s->len += len - 1;
  138. return 0;
  139. }
  140. seq_buf_set_overflow(s);
  141. return -1;
  142. }
  143. /**
  144. * seq_buf_putc - sequence printing of simple character
  145. * @s: seq_buf descriptor
  146. * @c: simple character to record
  147. *
  148. * Copy a single character into the sequence buffer.
  149. *
  150. * Returns zero on success, -1 on overflow
  151. */
  152. int seq_buf_putc(struct seq_buf *s, unsigned char c)
  153. {
  154. WARN_ON(s->size == 0);
  155. if (seq_buf_can_fit(s, 1)) {
  156. s->buffer[s->len++] = c;
  157. return 0;
  158. }
  159. seq_buf_set_overflow(s);
  160. return -1;
  161. }
  162. /**
  163. * seq_buf_putmem - write raw data into the sequenc buffer
  164. * @s: seq_buf descriptor
  165. * @mem: The raw memory to copy into the buffer
  166. * @len: The length of the raw memory to copy (in bytes)
  167. *
  168. * There may be cases where raw memory needs to be written into the
  169. * buffer and a strcpy() would not work. Using this function allows
  170. * for such cases.
  171. *
  172. * Returns zero on success, -1 on overflow
  173. */
  174. int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
  175. {
  176. WARN_ON(s->size == 0);
  177. if (seq_buf_can_fit(s, len)) {
  178. memcpy(s->buffer + s->len, mem, len);
  179. s->len += len;
  180. return 0;
  181. }
  182. seq_buf_set_overflow(s);
  183. return -1;
  184. }
  185. #define MAX_MEMHEX_BYTES 8U
  186. #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
  187. /**
  188. * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
  189. * @s: seq_buf descriptor
  190. * @mem: The raw memory to write its hex ASCII representation of
  191. * @len: The length of the raw memory to copy (in bytes)
  192. *
  193. * This is similar to seq_buf_putmem() except instead of just copying the
  194. * raw memory into the buffer it writes its ASCII representation of it
  195. * in hex characters.
  196. *
  197. * Returns zero on success, -1 on overflow
  198. */
  199. int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
  200. unsigned int len)
  201. {
  202. unsigned char hex[HEX_CHARS];
  203. const unsigned char *data = mem;
  204. unsigned int start_len;
  205. int i, j;
  206. WARN_ON(s->size == 0);
  207. while (len) {
  208. start_len = min(len, HEX_CHARS - 1);
  209. #ifdef __BIG_ENDIAN
  210. for (i = 0, j = 0; i < start_len; i++) {
  211. #else
  212. for (i = start_len-1, j = 0; i >= 0; i--) {
  213. #endif
  214. hex[j++] = hex_asc_hi(data[i]);
  215. hex[j++] = hex_asc_lo(data[i]);
  216. }
  217. if (WARN_ON_ONCE(j == 0 || j/2 > len))
  218. break;
  219. /* j increments twice per loop */
  220. len -= j / 2;
  221. hex[j++] = ' ';
  222. seq_buf_putmem(s, hex, j);
  223. if (seq_buf_has_overflowed(s))
  224. return -1;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * seq_buf_path - copy a path into the sequence buffer
  230. * @s: seq_buf descriptor
  231. * @path: path to write into the sequence buffer.
  232. * @esc: set of characters to escape in the output
  233. *
  234. * Write a path name into the sequence buffer.
  235. *
  236. * Returns the number of written bytes on success, -1 on overflow
  237. */
  238. int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
  239. {
  240. char *buf;
  241. size_t size = seq_buf_get_buf(s, &buf);
  242. int res = -1;
  243. WARN_ON(s->size == 0);
  244. if (size) {
  245. char *p = d_path(path, buf, size);
  246. if (!IS_ERR(p)) {
  247. char *end = mangle_path(buf, p, esc);
  248. if (end)
  249. res = end - buf;
  250. }
  251. }
  252. seq_buf_commit(s, res);
  253. return res;
  254. }
  255. /**
  256. * seq_buf_to_user - copy the squence buffer to user space
  257. * @s: seq_buf descriptor
  258. * @ubuf: The userspace memory location to copy to
  259. * @cnt: The amount to copy
  260. *
  261. * Copies the sequence buffer into the userspace memory pointed to
  262. * by @ubuf. It starts from the last read position (@s->readpos)
  263. * and writes up to @cnt characters or till it reaches the end of
  264. * the content in the buffer (@s->len), which ever comes first.
  265. *
  266. * On success, it returns a positive number of the number of bytes
  267. * it copied.
  268. *
  269. * On failure it returns -EBUSY if all of the content in the
  270. * sequence has been already read, which includes nothing in the
  271. * sequence (@s->len == @s->readpos).
  272. *
  273. * Returns -EFAULT if the copy to userspace fails.
  274. */
  275. int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
  276. {
  277. int len;
  278. int ret;
  279. if (!cnt)
  280. return 0;
  281. len = seq_buf_used(s);
  282. if (len <= s->readpos)
  283. return -EBUSY;
  284. len -= s->readpos;
  285. if (cnt > len)
  286. cnt = len;
  287. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  288. if (ret == cnt)
  289. return -EFAULT;
  290. cnt -= ret;
  291. s->readpos += cnt;
  292. return cnt;
  293. }