seq_buf.c 8.0 KB

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