seq_buf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. if (seq_buf_can_fit(s, len)) {
  132. memcpy(s->buffer + s->len, str, len);
  133. s->len += len;
  134. return 0;
  135. }
  136. seq_buf_set_overflow(s);
  137. return -1;
  138. }
  139. /**
  140. * seq_buf_putc - sequence printing of simple character
  141. * @s: seq_buf descriptor
  142. * @c: simple character to record
  143. *
  144. * Copy a single character into the sequence buffer.
  145. *
  146. * Returns zero on success, -1 on overflow
  147. */
  148. int seq_buf_putc(struct seq_buf *s, unsigned char c)
  149. {
  150. WARN_ON(s->size == 0);
  151. if (seq_buf_can_fit(s, 1)) {
  152. s->buffer[s->len++] = c;
  153. return 0;
  154. }
  155. seq_buf_set_overflow(s);
  156. return -1;
  157. }
  158. /**
  159. * seq_buf_putmem - write raw data into the sequenc buffer
  160. * @s: seq_buf descriptor
  161. * @mem: The raw memory to copy into the buffer
  162. * @len: The length of the raw memory to copy (in bytes)
  163. *
  164. * There may be cases where raw memory needs to be written into the
  165. * buffer and a strcpy() would not work. Using this function allows
  166. * for such cases.
  167. *
  168. * Returns zero on success, -1 on overflow
  169. */
  170. int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
  171. {
  172. WARN_ON(s->size == 0);
  173. if (seq_buf_can_fit(s, len)) {
  174. memcpy(s->buffer + s->len, mem, len);
  175. s->len += len;
  176. return 0;
  177. }
  178. seq_buf_set_overflow(s);
  179. return -1;
  180. }
  181. #define MAX_MEMHEX_BYTES 8U
  182. #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
  183. /**
  184. * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
  185. * @s: seq_buf descriptor
  186. * @mem: The raw memory to write its hex ASCII representation of
  187. * @len: The length of the raw memory to copy (in bytes)
  188. *
  189. * This is similar to seq_buf_putmem() except instead of just copying the
  190. * raw memory into the buffer it writes its ASCII representation of it
  191. * in hex characters.
  192. *
  193. * Returns zero on success, -1 on overflow
  194. */
  195. int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
  196. unsigned int len)
  197. {
  198. unsigned char hex[HEX_CHARS];
  199. const unsigned char *data = mem;
  200. unsigned int start_len;
  201. int i, j;
  202. WARN_ON(s->size == 0);
  203. while (len) {
  204. start_len = min(len, HEX_CHARS - 1);
  205. #ifdef __BIG_ENDIAN
  206. for (i = 0, j = 0; i < start_len; i++) {
  207. #else
  208. for (i = start_len-1, j = 0; i >= 0; i--) {
  209. #endif
  210. hex[j++] = hex_asc_hi(data[i]);
  211. hex[j++] = hex_asc_lo(data[i]);
  212. }
  213. if (WARN_ON_ONCE(j == 0 || j/2 > len))
  214. break;
  215. /* j increments twice per loop */
  216. len -= j / 2;
  217. hex[j++] = ' ';
  218. seq_buf_putmem(s, hex, j);
  219. if (seq_buf_has_overflowed(s))
  220. return -1;
  221. }
  222. return 0;
  223. }
  224. /**
  225. * seq_buf_path - copy a path into the sequence buffer
  226. * @s: seq_buf descriptor
  227. * @path: path to write into the sequence buffer.
  228. * @esc: set of characters to escape in the output
  229. *
  230. * Write a path name into the sequence buffer.
  231. *
  232. * Returns the number of written bytes on success, -1 on overflow
  233. */
  234. int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
  235. {
  236. char *buf;
  237. size_t size = seq_buf_get_buf(s, &buf);
  238. int res = -1;
  239. WARN_ON(s->size == 0);
  240. if (size) {
  241. char *p = d_path(path, buf, size);
  242. if (!IS_ERR(p)) {
  243. char *end = mangle_path(buf, p, esc);
  244. if (end)
  245. res = end - buf;
  246. }
  247. }
  248. seq_buf_commit(s, res);
  249. return res;
  250. }
  251. /**
  252. * seq_buf_to_user - copy the squence buffer to user space
  253. * @s: seq_buf descriptor
  254. * @ubuf: The userspace memory location to copy to
  255. * @cnt: The amount to copy
  256. *
  257. * Copies the sequence buffer into the userspace memory pointed to
  258. * by @ubuf. It starts from the last read position (@s->readpos)
  259. * and writes up to @cnt characters or till it reaches the end of
  260. * the content in the buffer (@s->len), which ever comes first.
  261. *
  262. * On success, it returns a positive number of the number of bytes
  263. * it copied.
  264. *
  265. * On failure it returns -EBUSY if all of the content in the
  266. * sequence has been already read, which includes nothing in the
  267. * sequence (@s->len == @s->readpos).
  268. *
  269. * Returns -EFAULT if the copy to userspace fails.
  270. */
  271. int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
  272. {
  273. int len;
  274. int ret;
  275. if (!cnt)
  276. return 0;
  277. len = seq_buf_used(s);
  278. if (len <= s->readpos)
  279. return -EBUSY;
  280. len -= s->readpos;
  281. if (cnt > len)
  282. cnt = len;
  283. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  284. if (ret == cnt)
  285. return -EFAULT;
  286. cnt -= ret;
  287. s->readpos += cnt;
  288. return cnt;
  289. }