Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * seq_buf.c
0004  *
0005  * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
0006  *
0007  * The seq_buf is a handy tool that allows you to pass a descriptor around
0008  * to a buffer that other functions can write to. It is similar to the
0009  * seq_file functionality but has some differences.
0010  *
0011  * To use it, the seq_buf must be initialized with seq_buf_init().
0012  * This will set up the counters within the descriptor. You can call
0013  * seq_buf_init() more than once to reset the seq_buf to start
0014  * from scratch.
0015  */
0016 #include <linux/uaccess.h>
0017 #include <linux/seq_file.h>
0018 #include <linux/seq_buf.h>
0019 
0020 /**
0021  * seq_buf_can_fit - can the new data fit in the current buffer?
0022  * @s: the seq_buf descriptor
0023  * @len: The length to see if it can fit in the current buffer
0024  *
0025  * Returns true if there's enough unused space in the seq_buf buffer
0026  * to fit the amount of new data according to @len.
0027  */
0028 static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
0029 {
0030     return s->len + len <= s->size;
0031 }
0032 
0033 /**
0034  * seq_buf_print_seq - move the contents of seq_buf into a seq_file
0035  * @m: the seq_file descriptor that is the destination
0036  * @s: the seq_buf descriptor that is the source.
0037  *
0038  * Returns zero on success, non zero otherwise
0039  */
0040 int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s)
0041 {
0042     unsigned int len = seq_buf_used(s);
0043 
0044     return seq_write(m, s->buffer, len);
0045 }
0046 
0047 /**
0048  * seq_buf_vprintf - sequence printing of information.
0049  * @s: seq_buf descriptor
0050  * @fmt: printf format string
0051  * @args: va_list of arguments from a printf() type function
0052  *
0053  * Writes a vnprintf() format into the sequencce buffer.
0054  *
0055  * Returns zero on success, -1 on overflow.
0056  */
0057 int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
0058 {
0059     int len;
0060 
0061     WARN_ON(s->size == 0);
0062 
0063     if (s->len < s->size) {
0064         len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
0065         if (s->len + len < s->size) {
0066             s->len += len;
0067             return 0;
0068         }
0069     }
0070     seq_buf_set_overflow(s);
0071     return -1;
0072 }
0073 
0074 /**
0075  * seq_buf_printf - sequence printing of information
0076  * @s: seq_buf descriptor
0077  * @fmt: printf format string
0078  *
0079  * Writes a printf() format into the sequence buffer.
0080  *
0081  * Returns zero on success, -1 on overflow.
0082  */
0083 int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
0084 {
0085     va_list ap;
0086     int ret;
0087 
0088     va_start(ap, fmt);
0089     ret = seq_buf_vprintf(s, fmt, ap);
0090     va_end(ap);
0091 
0092     return ret;
0093 }
0094 EXPORT_SYMBOL_GPL(seq_buf_printf);
0095 
0096 #ifdef CONFIG_BINARY_PRINTF
0097 /**
0098  * seq_buf_bprintf - Write the printf string from binary arguments
0099  * @s: seq_buf descriptor
0100  * @fmt: The format string for the @binary arguments
0101  * @binary: The binary arguments for @fmt.
0102  *
0103  * When recording in a fast path, a printf may be recorded with just
0104  * saving the format and the arguments as they were passed to the
0105  * function, instead of wasting cycles converting the arguments into
0106  * ASCII characters. Instead, the arguments are saved in a 32 bit
0107  * word array that is defined by the format string constraints.
0108  *
0109  * This function will take the format and the binary array and finish
0110  * the conversion into the ASCII string within the buffer.
0111  *
0112  * Returns zero on success, -1 on overflow.
0113  */
0114 int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
0115 {
0116     unsigned int len = seq_buf_buffer_left(s);
0117     int ret;
0118 
0119     WARN_ON(s->size == 0);
0120 
0121     if (s->len < s->size) {
0122         ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
0123         if (s->len + ret < s->size) {
0124             s->len += ret;
0125             return 0;
0126         }
0127     }
0128     seq_buf_set_overflow(s);
0129     return -1;
0130 }
0131 #endif /* CONFIG_BINARY_PRINTF */
0132 
0133 /**
0134  * seq_buf_puts - sequence printing of simple string
0135  * @s: seq_buf descriptor
0136  * @str: simple string to record
0137  *
0138  * Copy a simple string into the sequence buffer.
0139  *
0140  * Returns zero on success, -1 on overflow
0141  */
0142 int seq_buf_puts(struct seq_buf *s, const char *str)
0143 {
0144     size_t len = strlen(str);
0145 
0146     WARN_ON(s->size == 0);
0147 
0148     /* Add 1 to len for the trailing null byte which must be there */
0149     len += 1;
0150 
0151     if (seq_buf_can_fit(s, len)) {
0152         memcpy(s->buffer + s->len, str, len);
0153         /* Don't count the trailing null byte against the capacity */
0154         s->len += len - 1;
0155         return 0;
0156     }
0157     seq_buf_set_overflow(s);
0158     return -1;
0159 }
0160 
0161 /**
0162  * seq_buf_putc - sequence printing of simple character
0163  * @s: seq_buf descriptor
0164  * @c: simple character to record
0165  *
0166  * Copy a single character into the sequence buffer.
0167  *
0168  * Returns zero on success, -1 on overflow
0169  */
0170 int seq_buf_putc(struct seq_buf *s, unsigned char c)
0171 {
0172     WARN_ON(s->size == 0);
0173 
0174     if (seq_buf_can_fit(s, 1)) {
0175         s->buffer[s->len++] = c;
0176         return 0;
0177     }
0178     seq_buf_set_overflow(s);
0179     return -1;
0180 }
0181 
0182 /**
0183  * seq_buf_putmem - write raw data into the sequenc buffer
0184  * @s: seq_buf descriptor
0185  * @mem: The raw memory to copy into the buffer
0186  * @len: The length of the raw memory to copy (in bytes)
0187  *
0188  * There may be cases where raw memory needs to be written into the
0189  * buffer and a strcpy() would not work. Using this function allows
0190  * for such cases.
0191  *
0192  * Returns zero on success, -1 on overflow
0193  */
0194 int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
0195 {
0196     WARN_ON(s->size == 0);
0197 
0198     if (seq_buf_can_fit(s, len)) {
0199         memcpy(s->buffer + s->len, mem, len);
0200         s->len += len;
0201         return 0;
0202     }
0203     seq_buf_set_overflow(s);
0204     return -1;
0205 }
0206 
0207 #define MAX_MEMHEX_BYTES    8U
0208 #define HEX_CHARS       (MAX_MEMHEX_BYTES*2 + 1)
0209 
0210 /**
0211  * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
0212  * @s: seq_buf descriptor
0213  * @mem: The raw memory to write its hex ASCII representation of
0214  * @len: The length of the raw memory to copy (in bytes)
0215  *
0216  * This is similar to seq_buf_putmem() except instead of just copying the
0217  * raw memory into the buffer it writes its ASCII representation of it
0218  * in hex characters.
0219  *
0220  * Returns zero on success, -1 on overflow
0221  */
0222 int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
0223                unsigned int len)
0224 {
0225     unsigned char hex[HEX_CHARS];
0226     const unsigned char *data = mem;
0227     unsigned int start_len;
0228     int i, j;
0229 
0230     WARN_ON(s->size == 0);
0231 
0232     BUILD_BUG_ON(MAX_MEMHEX_BYTES * 2 >= HEX_CHARS);
0233 
0234     while (len) {
0235         start_len = min(len, MAX_MEMHEX_BYTES);
0236 #ifdef __BIG_ENDIAN
0237         for (i = 0, j = 0; i < start_len; i++) {
0238 #else
0239         for (i = start_len-1, j = 0; i >= 0; i--) {
0240 #endif
0241             hex[j++] = hex_asc_hi(data[i]);
0242             hex[j++] = hex_asc_lo(data[i]);
0243         }
0244         if (WARN_ON_ONCE(j == 0 || j/2 > len))
0245             break;
0246 
0247         /* j increments twice per loop */
0248         hex[j++] = ' ';
0249 
0250         seq_buf_putmem(s, hex, j);
0251         if (seq_buf_has_overflowed(s))
0252             return -1;
0253 
0254         len -= start_len;
0255         data += start_len;
0256     }
0257     return 0;
0258 }
0259 
0260 /**
0261  * seq_buf_path - copy a path into the sequence buffer
0262  * @s: seq_buf descriptor
0263  * @path: path to write into the sequence buffer.
0264  * @esc: set of characters to escape in the output
0265  *
0266  * Write a path name into the sequence buffer.
0267  *
0268  * Returns the number of written bytes on success, -1 on overflow
0269  */
0270 int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
0271 {
0272     char *buf;
0273     size_t size = seq_buf_get_buf(s, &buf);
0274     int res = -1;
0275 
0276     WARN_ON(s->size == 0);
0277 
0278     if (size) {
0279         char *p = d_path(path, buf, size);
0280         if (!IS_ERR(p)) {
0281             char *end = mangle_path(buf, p, esc);
0282             if (end)
0283                 res = end - buf;
0284         }
0285     }
0286     seq_buf_commit(s, res);
0287 
0288     return res;
0289 }
0290 
0291 /**
0292  * seq_buf_to_user - copy the sequence buffer to user space
0293  * @s: seq_buf descriptor
0294  * @ubuf: The userspace memory location to copy to
0295  * @cnt: The amount to copy
0296  *
0297  * Copies the sequence buffer into the userspace memory pointed to
0298  * by @ubuf. It starts from the last read position (@s->readpos)
0299  * and writes up to @cnt characters or till it reaches the end of
0300  * the content in the buffer (@s->len), which ever comes first.
0301  *
0302  * On success, it returns a positive number of the number of bytes
0303  * it copied.
0304  *
0305  * On failure it returns -EBUSY if all of the content in the
0306  * sequence has been already read, which includes nothing in the
0307  * sequence (@s->len == @s->readpos).
0308  *
0309  * Returns -EFAULT if the copy to userspace fails.
0310  */
0311 int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
0312 {
0313     int len;
0314     int ret;
0315 
0316     if (!cnt)
0317         return 0;
0318 
0319     len = seq_buf_used(s);
0320 
0321     if (len <= s->readpos)
0322         return -EBUSY;
0323 
0324     len -= s->readpos;
0325     if (cnt > len)
0326         cnt = len;
0327     ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
0328     if (ret == cnt)
0329         return -EFAULT;
0330 
0331     cnt -= ret;
0332 
0333     s->readpos += cnt;
0334     return cnt;
0335 }
0336 
0337 /**
0338  * seq_buf_hex_dump - print formatted hex dump into the sequence buffer
0339  * @s: seq_buf descriptor
0340  * @prefix_str: string to prefix each line with;
0341  *  caller supplies trailing spaces for alignment if desired
0342  * @prefix_type: controls whether prefix of an offset, address, or none
0343  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
0344  * @rowsize: number of bytes to print per line; must be 16 or 32
0345  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
0346  * @buf: data blob to dump
0347  * @len: number of bytes in the @buf
0348  * @ascii: include ASCII after the hex output
0349  *
0350  * Function is an analogue of print_hex_dump() and thus has similar interface.
0351  *
0352  * linebuf size is maximal length for one line.
0353  * 32 * 3 - maximum bytes per line, each printed into 2 chars + 1 for
0354  *  separating space
0355  * 2 - spaces separating hex dump and ascii representation
0356  * 32 - ascii representation
0357  * 1 - terminating '\0'
0358  *
0359  * Returns zero on success, -1 on overflow
0360  */
0361 int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, int prefix_type,
0362              int rowsize, int groupsize,
0363              const void *buf, size_t len, bool ascii)
0364 {
0365     const u8 *ptr = buf;
0366     int i, linelen, remaining = len;
0367     unsigned char linebuf[32 * 3 + 2 + 32 + 1];
0368     int ret;
0369 
0370     if (rowsize != 16 && rowsize != 32)
0371         rowsize = 16;
0372 
0373     for (i = 0; i < len; i += rowsize) {
0374         linelen = min(remaining, rowsize);
0375         remaining -= rowsize;
0376 
0377         hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
0378                    linebuf, sizeof(linebuf), ascii);
0379 
0380         switch (prefix_type) {
0381         case DUMP_PREFIX_ADDRESS:
0382             ret = seq_buf_printf(s, "%s%p: %s\n",
0383                    prefix_str, ptr + i, linebuf);
0384             break;
0385         case DUMP_PREFIX_OFFSET:
0386             ret = seq_buf_printf(s, "%s%.8x: %s\n",
0387                          prefix_str, i, linebuf);
0388             break;
0389         default:
0390             ret = seq_buf_printf(s, "%s%s\n", prefix_str, linebuf);
0391             break;
0392         }
0393         if (ret)
0394             return ret;
0395     }
0396     return 0;
0397 }