xref: /qemu/qemu-io-cmds.c (revision 4cc70e93)
1797ac58cSKevin Wolf /*
2797ac58cSKevin Wolf  * Command line utility to exercise the QEMU I/O path.
3797ac58cSKevin Wolf  *
4797ac58cSKevin Wolf  * Copyright (C) 2009 Red Hat, Inc.
5797ac58cSKevin Wolf  * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6797ac58cSKevin Wolf  *
7797ac58cSKevin Wolf  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8797ac58cSKevin Wolf  * See the COPYING file in the top-level directory.
9797ac58cSKevin Wolf  */
10797ac58cSKevin Wolf 
113d21994fSKevin Wolf #include "qemu-io.h"
12797ac58cSKevin Wolf #include "block/block_int.h"
13a8d8ecb7SMax Reitz #include "block/qapi.h"
146a1751b7SAlex Bligh #include "qemu/main-loop.h"
15797ac58cSKevin Wolf 
16797ac58cSKevin Wolf #define CMD_NOFILE_OK   0x01
17797ac58cSKevin Wolf 
18797ac58cSKevin Wolf int qemuio_misalign;
19797ac58cSKevin Wolf 
20c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab;
21c2cdf5c5SKevin Wolf static int ncmds;
22c2cdf5c5SKevin Wolf 
23c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b)
24c2cdf5c5SKevin Wolf {
25c2cdf5c5SKevin Wolf     return strcmp(((const cmdinfo_t *)a)->name,
26c2cdf5c5SKevin Wolf                   ((const cmdinfo_t *)b)->name);
27c2cdf5c5SKevin Wolf }
28c2cdf5c5SKevin Wolf 
29c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci)
30c2cdf5c5SKevin Wolf {
31c2cdf5c5SKevin Wolf     cmdtab = g_realloc(cmdtab, ++ncmds * sizeof(*cmdtab));
32c2cdf5c5SKevin Wolf     cmdtab[ncmds - 1] = *ci;
33c2cdf5c5SKevin Wolf     qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
34c2cdf5c5SKevin Wolf }
35c2cdf5c5SKevin Wolf 
36c2cdf5c5SKevin Wolf int qemuio_command_usage(const cmdinfo_t *ci)
37c2cdf5c5SKevin Wolf {
38c2cdf5c5SKevin Wolf     printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
39c2cdf5c5SKevin Wolf     return 0;
40c2cdf5c5SKevin Wolf }
41c2cdf5c5SKevin Wolf 
42c2cdf5c5SKevin Wolf static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
43c2cdf5c5SKevin Wolf {
44c2cdf5c5SKevin Wolf     if (ct->flags & CMD_FLAG_GLOBAL) {
45c2cdf5c5SKevin Wolf         return 1;
46c2cdf5c5SKevin Wolf     }
47c2cdf5c5SKevin Wolf     if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
48c2cdf5c5SKevin Wolf         fprintf(stderr, "no file open, try 'help open'\n");
49c2cdf5c5SKevin Wolf         return 0;
50c2cdf5c5SKevin Wolf     }
51c2cdf5c5SKevin Wolf     return 1;
52c2cdf5c5SKevin Wolf }
53c2cdf5c5SKevin Wolf 
543d21994fSKevin Wolf static int command(BlockDriverState *bs, const cmdinfo_t *ct, int argc,
553d21994fSKevin Wolf                    char **argv)
56c2cdf5c5SKevin Wolf {
57c2cdf5c5SKevin Wolf     char *cmd = argv[0];
58c2cdf5c5SKevin Wolf 
593d21994fSKevin Wolf     if (!init_check_command(bs, ct)) {
60c2cdf5c5SKevin Wolf         return 0;
61c2cdf5c5SKevin Wolf     }
62c2cdf5c5SKevin Wolf 
63c2cdf5c5SKevin Wolf     if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
64c2cdf5c5SKevin Wolf         if (ct->argmax == -1) {
65c2cdf5c5SKevin Wolf             fprintf(stderr,
66c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected at least %d arguments\n",
67c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
68c2cdf5c5SKevin Wolf         } else if (ct->argmin == ct->argmax) {
69c2cdf5c5SKevin Wolf             fprintf(stderr,
70c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected %d arguments\n",
71c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
72c2cdf5c5SKevin Wolf         } else {
73c2cdf5c5SKevin Wolf             fprintf(stderr,
74c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected between %d and %d arguments\n",
75c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin, ct->argmax);
76c2cdf5c5SKevin Wolf         }
77c2cdf5c5SKevin Wolf         return 0;
78c2cdf5c5SKevin Wolf     }
79c2cdf5c5SKevin Wolf     optind = 0;
803d21994fSKevin Wolf     return ct->cfunc(bs, argc, argv);
81c2cdf5c5SKevin Wolf }
82c2cdf5c5SKevin Wolf 
83c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd)
84c2cdf5c5SKevin Wolf {
85c2cdf5c5SKevin Wolf     cmdinfo_t *ct;
86c2cdf5c5SKevin Wolf 
87c2cdf5c5SKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
88c2cdf5c5SKevin Wolf         if (strcmp(ct->name, cmd) == 0 ||
89c2cdf5c5SKevin Wolf             (ct->altname && strcmp(ct->altname, cmd) == 0))
90c2cdf5c5SKevin Wolf         {
91c2cdf5c5SKevin Wolf             return (const cmdinfo_t *)ct;
92c2cdf5c5SKevin Wolf         }
93c2cdf5c5SKevin Wolf     }
94c2cdf5c5SKevin Wolf     return NULL;
95c2cdf5c5SKevin Wolf }
96c2cdf5c5SKevin Wolf 
97c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count)
98c2cdf5c5SKevin Wolf {
99c2cdf5c5SKevin Wolf     int c = 0;
100c2cdf5c5SKevin Wolf     char *p;
101c2cdf5c5SKevin Wolf     char **rval = g_malloc0(sizeof(char *));
102c2cdf5c5SKevin Wolf     char **tmp;
103c2cdf5c5SKevin Wolf 
104c2cdf5c5SKevin Wolf     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
105c2cdf5c5SKevin Wolf         if (!*p) {
106c2cdf5c5SKevin Wolf             continue;
107c2cdf5c5SKevin Wolf         }
108c2cdf5c5SKevin Wolf         c++;
109c2cdf5c5SKevin Wolf         tmp = g_realloc(rval, sizeof(*rval) * (c + 1));
110c2cdf5c5SKevin Wolf         if (!tmp) {
111c2cdf5c5SKevin Wolf             g_free(rval);
112c2cdf5c5SKevin Wolf             rval = NULL;
113c2cdf5c5SKevin Wolf             c = 0;
114c2cdf5c5SKevin Wolf             break;
115c2cdf5c5SKevin Wolf         } else {
116c2cdf5c5SKevin Wolf             rval = tmp;
117c2cdf5c5SKevin Wolf         }
118c2cdf5c5SKevin Wolf         rval[c - 1] = p;
119c2cdf5c5SKevin Wolf         rval[c] = NULL;
120c2cdf5c5SKevin Wolf     }
121c2cdf5c5SKevin Wolf     *count = c;
122c2cdf5c5SKevin Wolf     return rval;
123c2cdf5c5SKevin Wolf }
124c2cdf5c5SKevin Wolf 
125797ac58cSKevin Wolf static int64_t cvtnum(const char *s)
126797ac58cSKevin Wolf {
127797ac58cSKevin Wolf     char *end;
128797ac58cSKevin Wolf     return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
129797ac58cSKevin Wolf }
130797ac58cSKevin Wolf 
1310b613881SKevin Wolf #define EXABYTES(x)     ((long long)(x) << 60)
1320b613881SKevin Wolf #define PETABYTES(x)    ((long long)(x) << 50)
1330b613881SKevin Wolf #define TERABYTES(x)    ((long long)(x) << 40)
1340b613881SKevin Wolf #define GIGABYTES(x)    ((long long)(x) << 30)
1350b613881SKevin Wolf #define MEGABYTES(x)    ((long long)(x) << 20)
1360b613881SKevin Wolf #define KILOBYTES(x)    ((long long)(x) << 10)
1370b613881SKevin Wolf 
1380b613881SKevin Wolf #define TO_EXABYTES(x)  ((x) / EXABYTES(1))
1390b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1))
1400b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1))
1410b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
1420b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
1430b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
1440b613881SKevin Wolf 
1450b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size)
1460b613881SKevin Wolf {
1470b613881SKevin Wolf     char *trim;
1480b613881SKevin Wolf     const char *suffix;
1490b613881SKevin Wolf 
1500b613881SKevin Wolf     if (value >= EXABYTES(1)) {
1510b613881SKevin Wolf         suffix = " EiB";
1520b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
1530b613881SKevin Wolf     } else if (value >= PETABYTES(1)) {
1540b613881SKevin Wolf         suffix = " PiB";
1550b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
1560b613881SKevin Wolf     } else if (value >= TERABYTES(1)) {
1570b613881SKevin Wolf         suffix = " TiB";
1580b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
1590b613881SKevin Wolf     } else if (value >= GIGABYTES(1)) {
1600b613881SKevin Wolf         suffix = " GiB";
1610b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
1620b613881SKevin Wolf     } else if (value >= MEGABYTES(1)) {
1630b613881SKevin Wolf         suffix = " MiB";
1640b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
1650b613881SKevin Wolf     } else if (value >= KILOBYTES(1)) {
1660b613881SKevin Wolf         suffix = " KiB";
1670b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
1680b613881SKevin Wolf     } else {
1690b613881SKevin Wolf         suffix = " bytes";
1700b613881SKevin Wolf         snprintf(str, size - 6, "%f", value);
1710b613881SKevin Wolf     }
1720b613881SKevin Wolf 
1730b613881SKevin Wolf     trim = strstr(str, ".000");
1740b613881SKevin Wolf     if (trim) {
1750b613881SKevin Wolf         strcpy(trim, suffix);
1760b613881SKevin Wolf     } else {
1770b613881SKevin Wolf         strcat(str, suffix);
1780b613881SKevin Wolf     }
1790b613881SKevin Wolf }
1800b613881SKevin Wolf 
1810b613881SKevin Wolf 
1820b613881SKevin Wolf 
1830b613881SKevin Wolf static struct timeval tsub(struct timeval t1, struct timeval t2)
1840b613881SKevin Wolf {
1850b613881SKevin Wolf     t1.tv_usec -= t2.tv_usec;
1860b613881SKevin Wolf     if (t1.tv_usec < 0) {
1870b613881SKevin Wolf         t1.tv_usec += 1000000;
1880b613881SKevin Wolf         t1.tv_sec--;
1890b613881SKevin Wolf     }
1900b613881SKevin Wolf     t1.tv_sec -= t2.tv_sec;
1910b613881SKevin Wolf     return t1;
1920b613881SKevin Wolf }
1930b613881SKevin Wolf 
1940b613881SKevin Wolf static double tdiv(double value, struct timeval tv)
1950b613881SKevin Wolf {
1960b613881SKevin Wolf     return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
1970b613881SKevin Wolf }
1980b613881SKevin Wolf 
1990b613881SKevin Wolf #define HOURS(sec)      ((sec) / (60 * 60))
2000b613881SKevin Wolf #define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
2010b613881SKevin Wolf #define SECONDS(sec)    ((sec) % 60)
2020b613881SKevin Wolf 
2030b613881SKevin Wolf enum {
2040b613881SKevin Wolf     DEFAULT_TIME        = 0x0,
2050b613881SKevin Wolf     TERSE_FIXED_TIME    = 0x1,
2060b613881SKevin Wolf     VERBOSE_FIXED_TIME  = 0x2,
2070b613881SKevin Wolf };
2080b613881SKevin Wolf 
2090b613881SKevin Wolf static void timestr(struct timeval *tv, char *ts, size_t size, int format)
2100b613881SKevin Wolf {
2110b613881SKevin Wolf     double usec = (double)tv->tv_usec / 1000000.0;
2120b613881SKevin Wolf 
2130b613881SKevin Wolf     if (format & TERSE_FIXED_TIME) {
2140b613881SKevin Wolf         if (!HOURS(tv->tv_sec)) {
2150b613881SKevin Wolf             snprintf(ts, size, "%u:%02u.%02u",
2160b613881SKevin Wolf                     (unsigned int) MINUTES(tv->tv_sec),
2170b613881SKevin Wolf                     (unsigned int) SECONDS(tv->tv_sec),
2180b613881SKevin Wolf                     (unsigned int) (usec * 100));
2190b613881SKevin Wolf             return;
2200b613881SKevin Wolf         }
2210b613881SKevin Wolf         format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
2220b613881SKevin Wolf     }
2230b613881SKevin Wolf 
2240b613881SKevin Wolf     if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
2250b613881SKevin Wolf         snprintf(ts, size, "%u:%02u:%02u.%02u",
2260b613881SKevin Wolf                 (unsigned int) HOURS(tv->tv_sec),
2270b613881SKevin Wolf                 (unsigned int) MINUTES(tv->tv_sec),
2280b613881SKevin Wolf                 (unsigned int) SECONDS(tv->tv_sec),
2290b613881SKevin Wolf                 (unsigned int) (usec * 100));
2300b613881SKevin Wolf     } else {
2310b613881SKevin Wolf         snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
2320b613881SKevin Wolf     }
2330b613881SKevin Wolf }
2340b613881SKevin Wolf 
235797ac58cSKevin Wolf /*
236797ac58cSKevin Wolf  * Parse the pattern argument to various sub-commands.
237797ac58cSKevin Wolf  *
238797ac58cSKevin Wolf  * Because the pattern is used as an argument to memset it must evaluate
239797ac58cSKevin Wolf  * to an unsigned integer that fits into a single byte.
240797ac58cSKevin Wolf  */
241797ac58cSKevin Wolf static int parse_pattern(const char *arg)
242797ac58cSKevin Wolf {
243797ac58cSKevin Wolf     char *endptr = NULL;
244797ac58cSKevin Wolf     long pattern;
245797ac58cSKevin Wolf 
246797ac58cSKevin Wolf     pattern = strtol(arg, &endptr, 0);
247797ac58cSKevin Wolf     if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
248797ac58cSKevin Wolf         printf("%s is not a valid pattern byte\n", arg);
249797ac58cSKevin Wolf         return -1;
250797ac58cSKevin Wolf     }
251797ac58cSKevin Wolf 
252797ac58cSKevin Wolf     return pattern;
253797ac58cSKevin Wolf }
254797ac58cSKevin Wolf 
255797ac58cSKevin Wolf /*
256797ac58cSKevin Wolf  * Memory allocation helpers.
257797ac58cSKevin Wolf  *
258797ac58cSKevin Wolf  * Make sure memory is aligned by default, or purposefully misaligned if
259797ac58cSKevin Wolf  * that is specified on the command line.
260797ac58cSKevin Wolf  */
261797ac58cSKevin Wolf 
262797ac58cSKevin Wolf #define MISALIGN_OFFSET     16
263797ac58cSKevin Wolf static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
264797ac58cSKevin Wolf {
265797ac58cSKevin Wolf     void *buf;
266797ac58cSKevin Wolf 
267797ac58cSKevin Wolf     if (qemuio_misalign) {
268797ac58cSKevin Wolf         len += MISALIGN_OFFSET;
269797ac58cSKevin Wolf     }
270797ac58cSKevin Wolf     buf = qemu_blockalign(bs, len);
271797ac58cSKevin Wolf     memset(buf, pattern, len);
272797ac58cSKevin Wolf     if (qemuio_misalign) {
273797ac58cSKevin Wolf         buf += MISALIGN_OFFSET;
274797ac58cSKevin Wolf     }
275797ac58cSKevin Wolf     return buf;
276797ac58cSKevin Wolf }
277797ac58cSKevin Wolf 
278797ac58cSKevin Wolf static void qemu_io_free(void *p)
279797ac58cSKevin Wolf {
280797ac58cSKevin Wolf     if (qemuio_misalign) {
281797ac58cSKevin Wolf         p -= MISALIGN_OFFSET;
282797ac58cSKevin Wolf     }
283797ac58cSKevin Wolf     qemu_vfree(p);
284797ac58cSKevin Wolf }
285797ac58cSKevin Wolf 
286797ac58cSKevin Wolf static void dump_buffer(const void *buffer, int64_t offset, int len)
287797ac58cSKevin Wolf {
288797ac58cSKevin Wolf     int i, j;
289797ac58cSKevin Wolf     const uint8_t *p;
290797ac58cSKevin Wolf 
291797ac58cSKevin Wolf     for (i = 0, p = buffer; i < len; i += 16) {
292797ac58cSKevin Wolf         const uint8_t *s = p;
293797ac58cSKevin Wolf 
294797ac58cSKevin Wolf         printf("%08" PRIx64 ":  ", offset + i);
295797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, p++) {
296797ac58cSKevin Wolf             printf("%02x ", *p);
297797ac58cSKevin Wolf         }
298797ac58cSKevin Wolf         printf(" ");
299797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, s++) {
300797ac58cSKevin Wolf             if (isalnum(*s)) {
301797ac58cSKevin Wolf                 printf("%c", *s);
302797ac58cSKevin Wolf             } else {
303797ac58cSKevin Wolf                 printf(".");
304797ac58cSKevin Wolf             }
305797ac58cSKevin Wolf         }
306797ac58cSKevin Wolf         printf("\n");
307797ac58cSKevin Wolf     }
308797ac58cSKevin Wolf }
309797ac58cSKevin Wolf 
310797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset,
311797ac58cSKevin Wolf                          int count, int total, int cnt, int Cflag)
312797ac58cSKevin Wolf {
313797ac58cSKevin Wolf     char s1[64], s2[64], ts[64];
314797ac58cSKevin Wolf 
315797ac58cSKevin Wolf     timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
316797ac58cSKevin Wolf     if (!Cflag) {
317797ac58cSKevin Wolf         cvtstr((double)total, s1, sizeof(s1));
318797ac58cSKevin Wolf         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
319797ac58cSKevin Wolf         printf("%s %d/%d bytes at offset %" PRId64 "\n",
320797ac58cSKevin Wolf                op, total, count, offset);
321797ac58cSKevin Wolf         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
322797ac58cSKevin Wolf                s1, cnt, ts, s2, tdiv((double)cnt, *t));
323797ac58cSKevin Wolf     } else {/* bytes,ops,time,bytes/sec,ops/sec */
324797ac58cSKevin Wolf         printf("%d,%d,%s,%.3f,%.3f\n",
325797ac58cSKevin Wolf             total, cnt, ts,
326797ac58cSKevin Wolf             tdiv((double)total, *t),
327797ac58cSKevin Wolf             tdiv((double)cnt, *t));
328797ac58cSKevin Wolf     }
329797ac58cSKevin Wolf }
330797ac58cSKevin Wolf 
331797ac58cSKevin Wolf /*
332797ac58cSKevin Wolf  * Parse multiple length statements for vectored I/O, and construct an I/O
333797ac58cSKevin Wolf  * vector matching it.
334797ac58cSKevin Wolf  */
335797ac58cSKevin Wolf static void *
336797ac58cSKevin Wolf create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
337797ac58cSKevin Wolf              int pattern)
338797ac58cSKevin Wolf {
339797ac58cSKevin Wolf     size_t *sizes = g_new0(size_t, nr_iov);
340797ac58cSKevin Wolf     size_t count = 0;
341797ac58cSKevin Wolf     void *buf = NULL;
342797ac58cSKevin Wolf     void *p;
343797ac58cSKevin Wolf     int i;
344797ac58cSKevin Wolf 
345797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
346797ac58cSKevin Wolf         char *arg = argv[i];
347797ac58cSKevin Wolf         int64_t len;
348797ac58cSKevin Wolf 
349797ac58cSKevin Wolf         len = cvtnum(arg);
350797ac58cSKevin Wolf         if (len < 0) {
351797ac58cSKevin Wolf             printf("non-numeric length argument -- %s\n", arg);
352797ac58cSKevin Wolf             goto fail;
353797ac58cSKevin Wolf         }
354797ac58cSKevin Wolf 
355797ac58cSKevin Wolf         /* should be SIZE_T_MAX, but that doesn't exist */
356797ac58cSKevin Wolf         if (len > INT_MAX) {
357797ac58cSKevin Wolf             printf("too large length argument -- %s\n", arg);
358797ac58cSKevin Wolf             goto fail;
359797ac58cSKevin Wolf         }
360797ac58cSKevin Wolf 
361797ac58cSKevin Wolf         if (len & 0x1ff) {
362797ac58cSKevin Wolf             printf("length argument %" PRId64
363797ac58cSKevin Wolf                    " is not sector aligned\n", len);
364797ac58cSKevin Wolf             goto fail;
365797ac58cSKevin Wolf         }
366797ac58cSKevin Wolf 
367797ac58cSKevin Wolf         sizes[i] = len;
368797ac58cSKevin Wolf         count += len;
369797ac58cSKevin Wolf     }
370797ac58cSKevin Wolf 
371797ac58cSKevin Wolf     qemu_iovec_init(qiov, nr_iov);
372797ac58cSKevin Wolf 
373797ac58cSKevin Wolf     buf = p = qemu_io_alloc(bs, count, pattern);
374797ac58cSKevin Wolf 
375797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
376797ac58cSKevin Wolf         qemu_iovec_add(qiov, p, sizes[i]);
377797ac58cSKevin Wolf         p += sizes[i];
378797ac58cSKevin Wolf     }
379797ac58cSKevin Wolf 
380797ac58cSKevin Wolf fail:
381797ac58cSKevin Wolf     g_free(sizes);
382797ac58cSKevin Wolf     return buf;
383797ac58cSKevin Wolf }
384797ac58cSKevin Wolf 
385797ac58cSKevin Wolf static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
386797ac58cSKevin Wolf                    int *total)
387797ac58cSKevin Wolf {
388797ac58cSKevin Wolf     int ret;
389797ac58cSKevin Wolf 
390797ac58cSKevin Wolf     ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
391797ac58cSKevin Wolf     if (ret < 0) {
392797ac58cSKevin Wolf         return ret;
393797ac58cSKevin Wolf     }
394797ac58cSKevin Wolf     *total = count;
395797ac58cSKevin Wolf     return 1;
396797ac58cSKevin Wolf }
397797ac58cSKevin Wolf 
398797ac58cSKevin Wolf static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
399797ac58cSKevin Wolf                     int *total)
400797ac58cSKevin Wolf {
401797ac58cSKevin Wolf     int ret;
402797ac58cSKevin Wolf 
403797ac58cSKevin Wolf     ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
404797ac58cSKevin Wolf     if (ret < 0) {
405797ac58cSKevin Wolf         return ret;
406797ac58cSKevin Wolf     }
407797ac58cSKevin Wolf     *total = count;
408797ac58cSKevin Wolf     return 1;
409797ac58cSKevin Wolf }
410797ac58cSKevin Wolf 
411797ac58cSKevin Wolf static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
412797ac58cSKevin Wolf                     int *total)
413797ac58cSKevin Wolf {
414797ac58cSKevin Wolf     *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
415797ac58cSKevin Wolf     if (*total < 0) {
416797ac58cSKevin Wolf         return *total;
417797ac58cSKevin Wolf     }
418797ac58cSKevin Wolf     return 1;
419797ac58cSKevin Wolf }
420797ac58cSKevin Wolf 
421797ac58cSKevin Wolf static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
422797ac58cSKevin Wolf                      int *total)
423797ac58cSKevin Wolf {
424797ac58cSKevin Wolf     *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
425797ac58cSKevin Wolf     if (*total < 0) {
426797ac58cSKevin Wolf         return *total;
427797ac58cSKevin Wolf     }
428797ac58cSKevin Wolf     return 1;
429797ac58cSKevin Wolf }
430797ac58cSKevin Wolf 
431797ac58cSKevin Wolf typedef struct {
432797ac58cSKevin Wolf     BlockDriverState *bs;
433797ac58cSKevin Wolf     int64_t offset;
434797ac58cSKevin Wolf     int count;
435797ac58cSKevin Wolf     int *total;
436797ac58cSKevin Wolf     int ret;
437797ac58cSKevin Wolf     bool done;
438797ac58cSKevin Wolf } CoWriteZeroes;
439797ac58cSKevin Wolf 
440797ac58cSKevin Wolf static void coroutine_fn co_write_zeroes_entry(void *opaque)
441797ac58cSKevin Wolf {
442797ac58cSKevin Wolf     CoWriteZeroes *data = opaque;
443797ac58cSKevin Wolf 
444797ac58cSKevin Wolf     data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
445aa7bfbffSPeter Lieven                                      data->count / BDRV_SECTOR_SIZE, 0);
446797ac58cSKevin Wolf     data->done = true;
447797ac58cSKevin Wolf     if (data->ret < 0) {
448797ac58cSKevin Wolf         *data->total = data->ret;
449797ac58cSKevin Wolf         return;
450797ac58cSKevin Wolf     }
451797ac58cSKevin Wolf 
452797ac58cSKevin Wolf     *data->total = data->count;
453797ac58cSKevin Wolf }
454797ac58cSKevin Wolf 
455797ac58cSKevin Wolf static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
456797ac58cSKevin Wolf                               int *total)
457797ac58cSKevin Wolf {
458797ac58cSKevin Wolf     Coroutine *co;
459797ac58cSKevin Wolf     CoWriteZeroes data = {
460797ac58cSKevin Wolf         .bs     = bs,
461797ac58cSKevin Wolf         .offset = offset,
462797ac58cSKevin Wolf         .count  = count,
463797ac58cSKevin Wolf         .total  = total,
464797ac58cSKevin Wolf         .done   = false,
465797ac58cSKevin Wolf     };
466797ac58cSKevin Wolf 
467797ac58cSKevin Wolf     co = qemu_coroutine_create(co_write_zeroes_entry);
468797ac58cSKevin Wolf     qemu_coroutine_enter(co, &data);
469797ac58cSKevin Wolf     while (!data.done) {
470797ac58cSKevin Wolf         qemu_aio_wait();
471797ac58cSKevin Wolf     }
472797ac58cSKevin Wolf     if (data.ret < 0) {
473797ac58cSKevin Wolf         return data.ret;
474797ac58cSKevin Wolf     } else {
475797ac58cSKevin Wolf         return 1;
476797ac58cSKevin Wolf     }
477797ac58cSKevin Wolf }
478797ac58cSKevin Wolf 
479797ac58cSKevin Wolf static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
480797ac58cSKevin Wolf                                int count, int *total)
481797ac58cSKevin Wolf {
482797ac58cSKevin Wolf     int ret;
483797ac58cSKevin Wolf 
484797ac58cSKevin Wolf     ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
485797ac58cSKevin Wolf     if (ret < 0) {
486797ac58cSKevin Wolf         return ret;
487797ac58cSKevin Wolf     }
488797ac58cSKevin Wolf     *total = count;
489797ac58cSKevin Wolf     return 1;
490797ac58cSKevin Wolf }
491797ac58cSKevin Wolf 
492797ac58cSKevin Wolf static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
493797ac58cSKevin Wolf                            int count, int *total)
494797ac58cSKevin Wolf {
495797ac58cSKevin Wolf     *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
496797ac58cSKevin Wolf     if (*total < 0) {
497797ac58cSKevin Wolf         return *total;
498797ac58cSKevin Wolf     }
499797ac58cSKevin Wolf     return 1;
500797ac58cSKevin Wolf }
501797ac58cSKevin Wolf 
502797ac58cSKevin Wolf static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
503797ac58cSKevin Wolf                            int count, int *total)
504797ac58cSKevin Wolf {
505797ac58cSKevin Wolf     *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
506797ac58cSKevin Wolf     if (*total < 0) {
507797ac58cSKevin Wolf         return *total;
508797ac58cSKevin Wolf     }
509797ac58cSKevin Wolf     return 1;
510797ac58cSKevin Wolf }
511797ac58cSKevin Wolf 
512797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff
513797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret)
514797ac58cSKevin Wolf {
515797ac58cSKevin Wolf     *(int *)opaque = ret;
516797ac58cSKevin Wolf }
517797ac58cSKevin Wolf 
518797ac58cSKevin Wolf static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
519797ac58cSKevin Wolf                         int64_t offset, int *total)
520797ac58cSKevin Wolf {
521797ac58cSKevin Wolf     int async_ret = NOT_DONE;
522797ac58cSKevin Wolf 
523797ac58cSKevin Wolf     bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
524797ac58cSKevin Wolf                    aio_rw_done, &async_ret);
525797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
526797ac58cSKevin Wolf         main_loop_wait(false);
527797ac58cSKevin Wolf     }
528797ac58cSKevin Wolf 
529797ac58cSKevin Wolf     *total = qiov->size;
530797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
531797ac58cSKevin Wolf }
532797ac58cSKevin Wolf 
533797ac58cSKevin Wolf static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
534797ac58cSKevin Wolf                          int64_t offset, int *total)
535797ac58cSKevin Wolf {
536797ac58cSKevin Wolf     int async_ret = NOT_DONE;
537797ac58cSKevin Wolf 
538797ac58cSKevin Wolf     bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
539797ac58cSKevin Wolf                     aio_rw_done, &async_ret);
540797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
541797ac58cSKevin Wolf         main_loop_wait(false);
542797ac58cSKevin Wolf     }
543797ac58cSKevin Wolf 
544797ac58cSKevin Wolf     *total = qiov->size;
545797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
546797ac58cSKevin Wolf }
547797ac58cSKevin Wolf 
548797ac58cSKevin Wolf struct multiwrite_async_ret {
549797ac58cSKevin Wolf     int num_done;
550797ac58cSKevin Wolf     int error;
551797ac58cSKevin Wolf };
552797ac58cSKevin Wolf 
553797ac58cSKevin Wolf static void multiwrite_cb(void *opaque, int ret)
554797ac58cSKevin Wolf {
555797ac58cSKevin Wolf     struct multiwrite_async_ret *async_ret = opaque;
556797ac58cSKevin Wolf 
557797ac58cSKevin Wolf     async_ret->num_done++;
558797ac58cSKevin Wolf     if (ret < 0) {
559797ac58cSKevin Wolf         async_ret->error = ret;
560797ac58cSKevin Wolf     }
561797ac58cSKevin Wolf }
562797ac58cSKevin Wolf 
563797ac58cSKevin Wolf static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
564797ac58cSKevin Wolf                              int num_reqs, int *total)
565797ac58cSKevin Wolf {
566797ac58cSKevin Wolf     int i, ret;
567797ac58cSKevin Wolf     struct multiwrite_async_ret async_ret = {
568797ac58cSKevin Wolf         .num_done = 0,
569797ac58cSKevin Wolf         .error = 0,
570797ac58cSKevin Wolf     };
571797ac58cSKevin Wolf 
572797ac58cSKevin Wolf     *total = 0;
573797ac58cSKevin Wolf     for (i = 0; i < num_reqs; i++) {
574797ac58cSKevin Wolf         reqs[i].cb = multiwrite_cb;
575797ac58cSKevin Wolf         reqs[i].opaque = &async_ret;
576797ac58cSKevin Wolf         *total += reqs[i].qiov->size;
577797ac58cSKevin Wolf     }
578797ac58cSKevin Wolf 
579797ac58cSKevin Wolf     ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
580797ac58cSKevin Wolf     if (ret < 0) {
581797ac58cSKevin Wolf         return ret;
582797ac58cSKevin Wolf     }
583797ac58cSKevin Wolf 
584797ac58cSKevin Wolf     while (async_ret.num_done < num_reqs) {
585797ac58cSKevin Wolf         main_loop_wait(false);
586797ac58cSKevin Wolf     }
587797ac58cSKevin Wolf 
588797ac58cSKevin Wolf     return async_ret.error < 0 ? async_ret.error : 1;
589797ac58cSKevin Wolf }
590797ac58cSKevin Wolf 
591797ac58cSKevin Wolf static void read_help(void)
592797ac58cSKevin Wolf {
593797ac58cSKevin Wolf     printf(
594797ac58cSKevin Wolf "\n"
595797ac58cSKevin Wolf " reads a range of bytes from the given offset\n"
596797ac58cSKevin Wolf "\n"
597797ac58cSKevin Wolf " Example:\n"
598797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
599797ac58cSKevin Wolf "\n"
600797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
601797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
602797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n"
603797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
604797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n"
605797ac58cSKevin Wolf " -p, -- use bdrv_pread to read the file\n"
606797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
607797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
608797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n"
609797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
610797ac58cSKevin Wolf "\n");
611797ac58cSKevin Wolf }
612797ac58cSKevin Wolf 
613797ac58cSKevin Wolf static int read_f(BlockDriverState *bs, int argc, char **argv);
614797ac58cSKevin Wolf 
615797ac58cSKevin Wolf static const cmdinfo_t read_cmd = {
616797ac58cSKevin Wolf     .name       = "read",
617797ac58cSKevin Wolf     .altname    = "r",
618797ac58cSKevin Wolf     .cfunc      = read_f,
619797ac58cSKevin Wolf     .argmin     = 2,
620797ac58cSKevin Wolf     .argmax     = -1,
621797ac58cSKevin Wolf     .args       = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
622797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
623797ac58cSKevin Wolf     .help       = read_help,
624797ac58cSKevin Wolf };
625797ac58cSKevin Wolf 
626797ac58cSKevin Wolf static int read_f(BlockDriverState *bs, int argc, char **argv)
627797ac58cSKevin Wolf {
628797ac58cSKevin Wolf     struct timeval t1, t2;
629797ac58cSKevin Wolf     int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
630797ac58cSKevin Wolf     int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
631797ac58cSKevin Wolf     int c, cnt;
632797ac58cSKevin Wolf     char *buf;
633797ac58cSKevin Wolf     int64_t offset;
634797ac58cSKevin Wolf     int count;
635797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
636797ac58cSKevin Wolf     int total = 0;
637797ac58cSKevin Wolf     int pattern = 0, pattern_offset = 0, pattern_count = 0;
638797ac58cSKevin Wolf 
639797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
640797ac58cSKevin Wolf         switch (c) {
641797ac58cSKevin Wolf         case 'b':
642797ac58cSKevin Wolf             bflag = 1;
643797ac58cSKevin Wolf             break;
644797ac58cSKevin Wolf         case 'C':
645797ac58cSKevin Wolf             Cflag = 1;
646797ac58cSKevin Wolf             break;
647797ac58cSKevin Wolf         case 'l':
648797ac58cSKevin Wolf             lflag = 1;
649797ac58cSKevin Wolf             pattern_count = cvtnum(optarg);
650797ac58cSKevin Wolf             if (pattern_count < 0) {
651797ac58cSKevin Wolf                 printf("non-numeric length argument -- %s\n", optarg);
652797ac58cSKevin Wolf                 return 0;
653797ac58cSKevin Wolf             }
654797ac58cSKevin Wolf             break;
655797ac58cSKevin Wolf         case 'p':
656797ac58cSKevin Wolf             pflag = 1;
657797ac58cSKevin Wolf             break;
658797ac58cSKevin Wolf         case 'P':
659797ac58cSKevin Wolf             Pflag = 1;
660797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
661797ac58cSKevin Wolf             if (pattern < 0) {
662797ac58cSKevin Wolf                 return 0;
663797ac58cSKevin Wolf             }
664797ac58cSKevin Wolf             break;
665797ac58cSKevin Wolf         case 'q':
666797ac58cSKevin Wolf             qflag = 1;
667797ac58cSKevin Wolf             break;
668797ac58cSKevin Wolf         case 's':
669797ac58cSKevin Wolf             sflag = 1;
670797ac58cSKevin Wolf             pattern_offset = cvtnum(optarg);
671797ac58cSKevin Wolf             if (pattern_offset < 0) {
672797ac58cSKevin Wolf                 printf("non-numeric length argument -- %s\n", optarg);
673797ac58cSKevin Wolf                 return 0;
674797ac58cSKevin Wolf             }
675797ac58cSKevin Wolf             break;
676797ac58cSKevin Wolf         case 'v':
677797ac58cSKevin Wolf             vflag = 1;
678797ac58cSKevin Wolf             break;
679797ac58cSKevin Wolf         default:
680c2cdf5c5SKevin Wolf             return qemuio_command_usage(&read_cmd);
681797ac58cSKevin Wolf         }
682797ac58cSKevin Wolf     }
683797ac58cSKevin Wolf 
684797ac58cSKevin Wolf     if (optind != argc - 2) {
685c2cdf5c5SKevin Wolf         return qemuio_command_usage(&read_cmd);
686797ac58cSKevin Wolf     }
687797ac58cSKevin Wolf 
688797ac58cSKevin Wolf     if (bflag && pflag) {
689797ac58cSKevin Wolf         printf("-b and -p cannot be specified at the same time\n");
690797ac58cSKevin Wolf         return 0;
691797ac58cSKevin Wolf     }
692797ac58cSKevin Wolf 
693797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
694797ac58cSKevin Wolf     if (offset < 0) {
695797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
696797ac58cSKevin Wolf         return 0;
697797ac58cSKevin Wolf     }
698797ac58cSKevin Wolf 
699797ac58cSKevin Wolf     optind++;
700797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
701797ac58cSKevin Wolf     if (count < 0) {
702797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
703797ac58cSKevin Wolf         return 0;
704797ac58cSKevin Wolf     }
705797ac58cSKevin Wolf 
706797ac58cSKevin Wolf     if (!Pflag && (lflag || sflag)) {
707c2cdf5c5SKevin Wolf         return qemuio_command_usage(&read_cmd);
708797ac58cSKevin Wolf     }
709797ac58cSKevin Wolf 
710797ac58cSKevin Wolf     if (!lflag) {
711797ac58cSKevin Wolf         pattern_count = count - pattern_offset;
712797ac58cSKevin Wolf     }
713797ac58cSKevin Wolf 
714797ac58cSKevin Wolf     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
715797ac58cSKevin Wolf         printf("pattern verification range exceeds end of read data\n");
716797ac58cSKevin Wolf         return 0;
717797ac58cSKevin Wolf     }
718797ac58cSKevin Wolf 
719797ac58cSKevin Wolf     if (!pflag) {
720797ac58cSKevin Wolf         if (offset & 0x1ff) {
721797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
722797ac58cSKevin Wolf                    offset);
723797ac58cSKevin Wolf             return 0;
724797ac58cSKevin Wolf         }
725797ac58cSKevin Wolf         if (count & 0x1ff) {
726797ac58cSKevin Wolf             printf("count %d is not sector aligned\n",
727797ac58cSKevin Wolf                    count);
728797ac58cSKevin Wolf             return 0;
729797ac58cSKevin Wolf         }
730797ac58cSKevin Wolf     }
731797ac58cSKevin Wolf 
732797ac58cSKevin Wolf     buf = qemu_io_alloc(bs, count, 0xab);
733797ac58cSKevin Wolf 
734797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
735797ac58cSKevin Wolf     if (pflag) {
736797ac58cSKevin Wolf         cnt = do_pread(bs, buf, offset, count, &total);
737797ac58cSKevin Wolf     } else if (bflag) {
738797ac58cSKevin Wolf         cnt = do_load_vmstate(bs, buf, offset, count, &total);
739797ac58cSKevin Wolf     } else {
740797ac58cSKevin Wolf         cnt = do_read(bs, buf, offset, count, &total);
741797ac58cSKevin Wolf     }
742797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
743797ac58cSKevin Wolf 
744797ac58cSKevin Wolf     if (cnt < 0) {
745797ac58cSKevin Wolf         printf("read failed: %s\n", strerror(-cnt));
746797ac58cSKevin Wolf         goto out;
747797ac58cSKevin Wolf     }
748797ac58cSKevin Wolf 
749797ac58cSKevin Wolf     if (Pflag) {
750797ac58cSKevin Wolf         void *cmp_buf = g_malloc(pattern_count);
751797ac58cSKevin Wolf         memset(cmp_buf, pattern, pattern_count);
752797ac58cSKevin Wolf         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
753797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
754797ac58cSKevin Wolf                    PRId64 ", %d bytes\n",
755797ac58cSKevin Wolf                    offset + pattern_offset, pattern_count);
756797ac58cSKevin Wolf         }
757797ac58cSKevin Wolf         g_free(cmp_buf);
758797ac58cSKevin Wolf     }
759797ac58cSKevin Wolf 
760797ac58cSKevin Wolf     if (qflag) {
761797ac58cSKevin Wolf         goto out;
762797ac58cSKevin Wolf     }
763797ac58cSKevin Wolf 
764797ac58cSKevin Wolf     if (vflag) {
765797ac58cSKevin Wolf         dump_buffer(buf, offset, count);
766797ac58cSKevin Wolf     }
767797ac58cSKevin Wolf 
768797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
769797ac58cSKevin Wolf     t2 = tsub(t2, t1);
770797ac58cSKevin Wolf     print_report("read", &t2, offset, count, total, cnt, Cflag);
771797ac58cSKevin Wolf 
772797ac58cSKevin Wolf out:
773797ac58cSKevin Wolf     qemu_io_free(buf);
774797ac58cSKevin Wolf 
775797ac58cSKevin Wolf     return 0;
776797ac58cSKevin Wolf }
777797ac58cSKevin Wolf 
778797ac58cSKevin Wolf static void readv_help(void)
779797ac58cSKevin Wolf {
780797ac58cSKevin Wolf     printf(
781797ac58cSKevin Wolf "\n"
782797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n"
783797ac58cSKevin Wolf "\n"
784797ac58cSKevin Wolf " Example:\n"
785797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
786797ac58cSKevin Wolf "\n"
787797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
788797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
789797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n"
790797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
791797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
792797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
793797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
794797ac58cSKevin Wolf "\n");
795797ac58cSKevin Wolf }
796797ac58cSKevin Wolf 
797797ac58cSKevin Wolf static int readv_f(BlockDriverState *bs, int argc, char **argv);
798797ac58cSKevin Wolf 
799797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = {
800797ac58cSKevin Wolf     .name       = "readv",
801797ac58cSKevin Wolf     .cfunc      = readv_f,
802797ac58cSKevin Wolf     .argmin     = 2,
803797ac58cSKevin Wolf     .argmax     = -1,
804797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern ] off len [len..]",
805797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
806797ac58cSKevin Wolf     .help       = readv_help,
807797ac58cSKevin Wolf };
808797ac58cSKevin Wolf 
809797ac58cSKevin Wolf static int readv_f(BlockDriverState *bs, int argc, char **argv)
810797ac58cSKevin Wolf {
811797ac58cSKevin Wolf     struct timeval t1, t2;
812797ac58cSKevin Wolf     int Cflag = 0, qflag = 0, vflag = 0;
813797ac58cSKevin Wolf     int c, cnt;
814797ac58cSKevin Wolf     char *buf;
815797ac58cSKevin Wolf     int64_t offset;
816797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
817797ac58cSKevin Wolf     int total = 0;
818797ac58cSKevin Wolf     int nr_iov;
819797ac58cSKevin Wolf     QEMUIOVector qiov;
820797ac58cSKevin Wolf     int pattern = 0;
821797ac58cSKevin Wolf     int Pflag = 0;
822797ac58cSKevin Wolf 
823797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
824797ac58cSKevin Wolf         switch (c) {
825797ac58cSKevin Wolf         case 'C':
826797ac58cSKevin Wolf             Cflag = 1;
827797ac58cSKevin Wolf             break;
828797ac58cSKevin Wolf         case 'P':
829797ac58cSKevin Wolf             Pflag = 1;
830797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
831797ac58cSKevin Wolf             if (pattern < 0) {
832797ac58cSKevin Wolf                 return 0;
833797ac58cSKevin Wolf             }
834797ac58cSKevin Wolf             break;
835797ac58cSKevin Wolf         case 'q':
836797ac58cSKevin Wolf             qflag = 1;
837797ac58cSKevin Wolf             break;
838797ac58cSKevin Wolf         case 'v':
839797ac58cSKevin Wolf             vflag = 1;
840797ac58cSKevin Wolf             break;
841797ac58cSKevin Wolf         default:
842c2cdf5c5SKevin Wolf             return qemuio_command_usage(&readv_cmd);
843797ac58cSKevin Wolf         }
844797ac58cSKevin Wolf     }
845797ac58cSKevin Wolf 
846797ac58cSKevin Wolf     if (optind > argc - 2) {
847c2cdf5c5SKevin Wolf         return qemuio_command_usage(&readv_cmd);
848797ac58cSKevin Wolf     }
849797ac58cSKevin Wolf 
850797ac58cSKevin Wolf 
851797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
852797ac58cSKevin Wolf     if (offset < 0) {
853797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
854797ac58cSKevin Wolf         return 0;
855797ac58cSKevin Wolf     }
856797ac58cSKevin Wolf     optind++;
857797ac58cSKevin Wolf 
858797ac58cSKevin Wolf     if (offset & 0x1ff) {
859797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
860797ac58cSKevin Wolf                offset);
861797ac58cSKevin Wolf         return 0;
862797ac58cSKevin Wolf     }
863797ac58cSKevin Wolf 
864797ac58cSKevin Wolf     nr_iov = argc - optind;
865797ac58cSKevin Wolf     buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
866797ac58cSKevin Wolf     if (buf == NULL) {
867797ac58cSKevin Wolf         return 0;
868797ac58cSKevin Wolf     }
869797ac58cSKevin Wolf 
870797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
871797ac58cSKevin Wolf     cnt = do_aio_readv(bs, &qiov, offset, &total);
872797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
873797ac58cSKevin Wolf 
874797ac58cSKevin Wolf     if (cnt < 0) {
875797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-cnt));
876797ac58cSKevin Wolf         goto out;
877797ac58cSKevin Wolf     }
878797ac58cSKevin Wolf 
879797ac58cSKevin Wolf     if (Pflag) {
880797ac58cSKevin Wolf         void *cmp_buf = g_malloc(qiov.size);
881797ac58cSKevin Wolf         memset(cmp_buf, pattern, qiov.size);
882797ac58cSKevin Wolf         if (memcmp(buf, cmp_buf, qiov.size)) {
883797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
884797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", offset, qiov.size);
885797ac58cSKevin Wolf         }
886797ac58cSKevin Wolf         g_free(cmp_buf);
887797ac58cSKevin Wolf     }
888797ac58cSKevin Wolf 
889797ac58cSKevin Wolf     if (qflag) {
890797ac58cSKevin Wolf         goto out;
891797ac58cSKevin Wolf     }
892797ac58cSKevin Wolf 
893797ac58cSKevin Wolf     if (vflag) {
894797ac58cSKevin Wolf         dump_buffer(buf, offset, qiov.size);
895797ac58cSKevin Wolf     }
896797ac58cSKevin Wolf 
897797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
898797ac58cSKevin Wolf     t2 = tsub(t2, t1);
899797ac58cSKevin Wolf     print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
900797ac58cSKevin Wolf 
901797ac58cSKevin Wolf out:
902797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
903797ac58cSKevin Wolf     qemu_io_free(buf);
904797ac58cSKevin Wolf     return 0;
905797ac58cSKevin Wolf }
906797ac58cSKevin Wolf 
907797ac58cSKevin Wolf static void write_help(void)
908797ac58cSKevin Wolf {
909797ac58cSKevin Wolf     printf(
910797ac58cSKevin Wolf "\n"
911797ac58cSKevin Wolf " writes a range of bytes from the given offset\n"
912797ac58cSKevin Wolf "\n"
913797ac58cSKevin Wolf " Example:\n"
914797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
915797ac58cSKevin Wolf "\n"
916797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
917797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
918797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n"
919797ac58cSKevin Wolf " -c, -- write compressed data with bdrv_write_compressed\n"
920797ac58cSKevin Wolf " -p, -- use bdrv_pwrite to write the file\n"
921797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
922797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
923797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
924797ac58cSKevin Wolf " -z, -- write zeroes using bdrv_co_write_zeroes\n"
925797ac58cSKevin Wolf "\n");
926797ac58cSKevin Wolf }
927797ac58cSKevin Wolf 
928797ac58cSKevin Wolf static int write_f(BlockDriverState *bs, int argc, char **argv);
929797ac58cSKevin Wolf 
930797ac58cSKevin Wolf static const cmdinfo_t write_cmd = {
931797ac58cSKevin Wolf     .name       = "write",
932797ac58cSKevin Wolf     .altname    = "w",
933797ac58cSKevin Wolf     .cfunc      = write_f,
934797ac58cSKevin Wolf     .argmin     = 2,
935797ac58cSKevin Wolf     .argmax     = -1,
936797ac58cSKevin Wolf     .args       = "[-bcCpqz] [-P pattern ] off len",
937797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
938797ac58cSKevin Wolf     .help       = write_help,
939797ac58cSKevin Wolf };
940797ac58cSKevin Wolf 
941797ac58cSKevin Wolf static int write_f(BlockDriverState *bs, int argc, char **argv)
942797ac58cSKevin Wolf {
943797ac58cSKevin Wolf     struct timeval t1, t2;
944797ac58cSKevin Wolf     int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
945797ac58cSKevin Wolf     int cflag = 0;
946797ac58cSKevin Wolf     int c, cnt;
947797ac58cSKevin Wolf     char *buf = NULL;
948797ac58cSKevin Wolf     int64_t offset;
949797ac58cSKevin Wolf     int count;
950797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
951797ac58cSKevin Wolf     int total = 0;
952797ac58cSKevin Wolf     int pattern = 0xcd;
953797ac58cSKevin Wolf 
954797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
955797ac58cSKevin Wolf         switch (c) {
956797ac58cSKevin Wolf         case 'b':
957797ac58cSKevin Wolf             bflag = 1;
958797ac58cSKevin Wolf             break;
959797ac58cSKevin Wolf         case 'c':
960797ac58cSKevin Wolf             cflag = 1;
961797ac58cSKevin Wolf             break;
962797ac58cSKevin Wolf         case 'C':
963797ac58cSKevin Wolf             Cflag = 1;
964797ac58cSKevin Wolf             break;
965797ac58cSKevin Wolf         case 'p':
966797ac58cSKevin Wolf             pflag = 1;
967797ac58cSKevin Wolf             break;
968797ac58cSKevin Wolf         case 'P':
969797ac58cSKevin Wolf             Pflag = 1;
970797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
971797ac58cSKevin Wolf             if (pattern < 0) {
972797ac58cSKevin Wolf                 return 0;
973797ac58cSKevin Wolf             }
974797ac58cSKevin Wolf             break;
975797ac58cSKevin Wolf         case 'q':
976797ac58cSKevin Wolf             qflag = 1;
977797ac58cSKevin Wolf             break;
978797ac58cSKevin Wolf         case 'z':
979797ac58cSKevin Wolf             zflag = 1;
980797ac58cSKevin Wolf             break;
981797ac58cSKevin Wolf         default:
982c2cdf5c5SKevin Wolf             return qemuio_command_usage(&write_cmd);
983797ac58cSKevin Wolf         }
984797ac58cSKevin Wolf     }
985797ac58cSKevin Wolf 
986797ac58cSKevin Wolf     if (optind != argc - 2) {
987c2cdf5c5SKevin Wolf         return qemuio_command_usage(&write_cmd);
988797ac58cSKevin Wolf     }
989797ac58cSKevin Wolf 
990797ac58cSKevin Wolf     if (bflag + pflag + zflag > 1) {
991797ac58cSKevin Wolf         printf("-b, -p, or -z cannot be specified at the same time\n");
992797ac58cSKevin Wolf         return 0;
993797ac58cSKevin Wolf     }
994797ac58cSKevin Wolf 
995797ac58cSKevin Wolf     if (zflag && Pflag) {
996797ac58cSKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
997797ac58cSKevin Wolf         return 0;
998797ac58cSKevin Wolf     }
999797ac58cSKevin Wolf 
1000797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1001797ac58cSKevin Wolf     if (offset < 0) {
1002797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1003797ac58cSKevin Wolf         return 0;
1004797ac58cSKevin Wolf     }
1005797ac58cSKevin Wolf 
1006797ac58cSKevin Wolf     optind++;
1007797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1008797ac58cSKevin Wolf     if (count < 0) {
1009797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1010797ac58cSKevin Wolf         return 0;
1011797ac58cSKevin Wolf     }
1012797ac58cSKevin Wolf 
1013797ac58cSKevin Wolf     if (!pflag) {
1014797ac58cSKevin Wolf         if (offset & 0x1ff) {
1015797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
1016797ac58cSKevin Wolf                    offset);
1017797ac58cSKevin Wolf             return 0;
1018797ac58cSKevin Wolf         }
1019797ac58cSKevin Wolf 
1020797ac58cSKevin Wolf         if (count & 0x1ff) {
1021797ac58cSKevin Wolf             printf("count %d is not sector aligned\n",
1022797ac58cSKevin Wolf                    count);
1023797ac58cSKevin Wolf             return 0;
1024797ac58cSKevin Wolf         }
1025797ac58cSKevin Wolf     }
1026797ac58cSKevin Wolf 
1027797ac58cSKevin Wolf     if (!zflag) {
1028797ac58cSKevin Wolf         buf = qemu_io_alloc(bs, count, pattern);
1029797ac58cSKevin Wolf     }
1030797ac58cSKevin Wolf 
1031797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1032797ac58cSKevin Wolf     if (pflag) {
1033797ac58cSKevin Wolf         cnt = do_pwrite(bs, buf, offset, count, &total);
1034797ac58cSKevin Wolf     } else if (bflag) {
1035797ac58cSKevin Wolf         cnt = do_save_vmstate(bs, buf, offset, count, &total);
1036797ac58cSKevin Wolf     } else if (zflag) {
1037797ac58cSKevin Wolf         cnt = do_co_write_zeroes(bs, offset, count, &total);
1038797ac58cSKevin Wolf     } else if (cflag) {
1039797ac58cSKevin Wolf         cnt = do_write_compressed(bs, buf, offset, count, &total);
1040797ac58cSKevin Wolf     } else {
1041797ac58cSKevin Wolf         cnt = do_write(bs, buf, offset, count, &total);
1042797ac58cSKevin Wolf     }
1043797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1044797ac58cSKevin Wolf 
1045797ac58cSKevin Wolf     if (cnt < 0) {
1046797ac58cSKevin Wolf         printf("write failed: %s\n", strerror(-cnt));
1047797ac58cSKevin Wolf         goto out;
1048797ac58cSKevin Wolf     }
1049797ac58cSKevin Wolf 
1050797ac58cSKevin Wolf     if (qflag) {
1051797ac58cSKevin Wolf         goto out;
1052797ac58cSKevin Wolf     }
1053797ac58cSKevin Wolf 
1054797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1055797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1056797ac58cSKevin Wolf     print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1057797ac58cSKevin Wolf 
1058797ac58cSKevin Wolf out:
1059797ac58cSKevin Wolf     if (!zflag) {
1060797ac58cSKevin Wolf         qemu_io_free(buf);
1061797ac58cSKevin Wolf     }
1062797ac58cSKevin Wolf 
1063797ac58cSKevin Wolf     return 0;
1064797ac58cSKevin Wolf }
1065797ac58cSKevin Wolf 
1066797ac58cSKevin Wolf static void
1067797ac58cSKevin Wolf writev_help(void)
1068797ac58cSKevin Wolf {
1069797ac58cSKevin Wolf     printf(
1070797ac58cSKevin Wolf "\n"
1071797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n"
1072797ac58cSKevin Wolf "\n"
1073797ac58cSKevin Wolf " Example:\n"
1074797ac58cSKevin Wolf " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1075797ac58cSKevin Wolf "\n"
1076797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1077797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1078797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1079797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1080797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1081797ac58cSKevin Wolf "\n");
1082797ac58cSKevin Wolf }
1083797ac58cSKevin Wolf 
1084797ac58cSKevin Wolf static int writev_f(BlockDriverState *bs, int argc, char **argv);
1085797ac58cSKevin Wolf 
1086797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = {
1087797ac58cSKevin Wolf     .name       = "writev",
1088797ac58cSKevin Wolf     .cfunc      = writev_f,
1089797ac58cSKevin Wolf     .argmin     = 2,
1090797ac58cSKevin Wolf     .argmax     = -1,
1091797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..]",
1092797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
1093797ac58cSKevin Wolf     .help       = writev_help,
1094797ac58cSKevin Wolf };
1095797ac58cSKevin Wolf 
1096797ac58cSKevin Wolf static int writev_f(BlockDriverState *bs, int argc, char **argv)
1097797ac58cSKevin Wolf {
1098797ac58cSKevin Wolf     struct timeval t1, t2;
1099797ac58cSKevin Wolf     int Cflag = 0, qflag = 0;
1100797ac58cSKevin Wolf     int c, cnt;
1101797ac58cSKevin Wolf     char *buf;
1102797ac58cSKevin Wolf     int64_t offset;
1103797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1104797ac58cSKevin Wolf     int total = 0;
1105797ac58cSKevin Wolf     int nr_iov;
1106797ac58cSKevin Wolf     int pattern = 0xcd;
1107797ac58cSKevin Wolf     QEMUIOVector qiov;
1108797ac58cSKevin Wolf 
1109797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1110797ac58cSKevin Wolf         switch (c) {
1111797ac58cSKevin Wolf         case 'C':
1112797ac58cSKevin Wolf             Cflag = 1;
1113797ac58cSKevin Wolf             break;
1114797ac58cSKevin Wolf         case 'q':
1115797ac58cSKevin Wolf             qflag = 1;
1116797ac58cSKevin Wolf             break;
1117797ac58cSKevin Wolf         case 'P':
1118797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1119797ac58cSKevin Wolf             if (pattern < 0) {
1120797ac58cSKevin Wolf                 return 0;
1121797ac58cSKevin Wolf             }
1122797ac58cSKevin Wolf             break;
1123797ac58cSKevin Wolf         default:
1124c2cdf5c5SKevin Wolf             return qemuio_command_usage(&writev_cmd);
1125797ac58cSKevin Wolf         }
1126797ac58cSKevin Wolf     }
1127797ac58cSKevin Wolf 
1128797ac58cSKevin Wolf     if (optind > argc - 2) {
1129c2cdf5c5SKevin Wolf         return qemuio_command_usage(&writev_cmd);
1130797ac58cSKevin Wolf     }
1131797ac58cSKevin Wolf 
1132797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1133797ac58cSKevin Wolf     if (offset < 0) {
1134797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1135797ac58cSKevin Wolf         return 0;
1136797ac58cSKevin Wolf     }
1137797ac58cSKevin Wolf     optind++;
1138797ac58cSKevin Wolf 
1139797ac58cSKevin Wolf     if (offset & 0x1ff) {
1140797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1141797ac58cSKevin Wolf                offset);
1142797ac58cSKevin Wolf         return 0;
1143797ac58cSKevin Wolf     }
1144797ac58cSKevin Wolf 
1145797ac58cSKevin Wolf     nr_iov = argc - optind;
1146797ac58cSKevin Wolf     buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
1147797ac58cSKevin Wolf     if (buf == NULL) {
1148797ac58cSKevin Wolf         return 0;
1149797ac58cSKevin Wolf     }
1150797ac58cSKevin Wolf 
1151797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1152797ac58cSKevin Wolf     cnt = do_aio_writev(bs, &qiov, offset, &total);
1153797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1154797ac58cSKevin Wolf 
1155797ac58cSKevin Wolf     if (cnt < 0) {
1156797ac58cSKevin Wolf         printf("writev failed: %s\n", strerror(-cnt));
1157797ac58cSKevin Wolf         goto out;
1158797ac58cSKevin Wolf     }
1159797ac58cSKevin Wolf 
1160797ac58cSKevin Wolf     if (qflag) {
1161797ac58cSKevin Wolf         goto out;
1162797ac58cSKevin Wolf     }
1163797ac58cSKevin Wolf 
1164797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1165797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1166797ac58cSKevin Wolf     print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1167797ac58cSKevin Wolf out:
1168797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
1169797ac58cSKevin Wolf     qemu_io_free(buf);
1170797ac58cSKevin Wolf     return 0;
1171797ac58cSKevin Wolf }
1172797ac58cSKevin Wolf 
1173797ac58cSKevin Wolf static void multiwrite_help(void)
1174797ac58cSKevin Wolf {
1175797ac58cSKevin Wolf     printf(
1176797ac58cSKevin Wolf "\n"
1177797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers,\n"
1178797ac58cSKevin Wolf " in a batch of requests that may be merged by qemu\n"
1179797ac58cSKevin Wolf "\n"
1180797ac58cSKevin Wolf " Example:\n"
1181797ac58cSKevin Wolf " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1182797ac58cSKevin Wolf "  writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1183797ac58cSKevin Wolf "\n"
1184797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1185797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1186797ac58cSKevin Wolf " by one for each request contained in the multiwrite command.\n"
1187797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1188797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1189797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1190797ac58cSKevin Wolf "\n");
1191797ac58cSKevin Wolf }
1192797ac58cSKevin Wolf 
1193797ac58cSKevin Wolf static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
1194797ac58cSKevin Wolf 
1195797ac58cSKevin Wolf static const cmdinfo_t multiwrite_cmd = {
1196797ac58cSKevin Wolf     .name       = "multiwrite",
1197797ac58cSKevin Wolf     .cfunc      = multiwrite_f,
1198797ac58cSKevin Wolf     .argmin     = 2,
1199797ac58cSKevin Wolf     .argmax     = -1,
1200797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1201797ac58cSKevin Wolf     .oneline    = "issues multiple write requests at once",
1202797ac58cSKevin Wolf     .help       = multiwrite_help,
1203797ac58cSKevin Wolf };
1204797ac58cSKevin Wolf 
1205797ac58cSKevin Wolf static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
1206797ac58cSKevin Wolf {
1207797ac58cSKevin Wolf     struct timeval t1, t2;
1208797ac58cSKevin Wolf     int Cflag = 0, qflag = 0;
1209797ac58cSKevin Wolf     int c, cnt;
1210797ac58cSKevin Wolf     char **buf;
1211797ac58cSKevin Wolf     int64_t offset, first_offset = 0;
1212797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1213797ac58cSKevin Wolf     int total = 0;
1214797ac58cSKevin Wolf     int nr_iov;
1215797ac58cSKevin Wolf     int nr_reqs;
1216797ac58cSKevin Wolf     int pattern = 0xcd;
1217797ac58cSKevin Wolf     QEMUIOVector *qiovs;
1218797ac58cSKevin Wolf     int i;
1219797ac58cSKevin Wolf     BlockRequest *reqs;
1220797ac58cSKevin Wolf 
1221797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1222797ac58cSKevin Wolf         switch (c) {
1223797ac58cSKevin Wolf         case 'C':
1224797ac58cSKevin Wolf             Cflag = 1;
1225797ac58cSKevin Wolf             break;
1226797ac58cSKevin Wolf         case 'q':
1227797ac58cSKevin Wolf             qflag = 1;
1228797ac58cSKevin Wolf             break;
1229797ac58cSKevin Wolf         case 'P':
1230797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1231797ac58cSKevin Wolf             if (pattern < 0) {
1232797ac58cSKevin Wolf                 return 0;
1233797ac58cSKevin Wolf             }
1234797ac58cSKevin Wolf             break;
1235797ac58cSKevin Wolf         default:
1236c2cdf5c5SKevin Wolf             return qemuio_command_usage(&writev_cmd);
1237797ac58cSKevin Wolf         }
1238797ac58cSKevin Wolf     }
1239797ac58cSKevin Wolf 
1240797ac58cSKevin Wolf     if (optind > argc - 2) {
1241c2cdf5c5SKevin Wolf         return qemuio_command_usage(&writev_cmd);
1242797ac58cSKevin Wolf     }
1243797ac58cSKevin Wolf 
1244797ac58cSKevin Wolf     nr_reqs = 1;
1245797ac58cSKevin Wolf     for (i = optind; i < argc; i++) {
1246797ac58cSKevin Wolf         if (!strcmp(argv[i], ";")) {
1247797ac58cSKevin Wolf             nr_reqs++;
1248797ac58cSKevin Wolf         }
1249797ac58cSKevin Wolf     }
1250797ac58cSKevin Wolf 
1251797ac58cSKevin Wolf     reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1252797ac58cSKevin Wolf     buf = g_malloc0(nr_reqs * sizeof(*buf));
1253797ac58cSKevin Wolf     qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
1254797ac58cSKevin Wolf 
1255797ac58cSKevin Wolf     for (i = 0; i < nr_reqs && optind < argc; i++) {
1256797ac58cSKevin Wolf         int j;
1257797ac58cSKevin Wolf 
1258797ac58cSKevin Wolf         /* Read the offset of the request */
1259797ac58cSKevin Wolf         offset = cvtnum(argv[optind]);
1260797ac58cSKevin Wolf         if (offset < 0) {
1261797ac58cSKevin Wolf             printf("non-numeric offset argument -- %s\n", argv[optind]);
1262797ac58cSKevin Wolf             goto out;
1263797ac58cSKevin Wolf         }
1264797ac58cSKevin Wolf         optind++;
1265797ac58cSKevin Wolf 
1266797ac58cSKevin Wolf         if (offset & 0x1ff) {
1267797ac58cSKevin Wolf             printf("offset %lld is not sector aligned\n",
1268797ac58cSKevin Wolf                    (long long)offset);
1269797ac58cSKevin Wolf             goto out;
1270797ac58cSKevin Wolf         }
1271797ac58cSKevin Wolf 
1272797ac58cSKevin Wolf         if (i == 0) {
1273797ac58cSKevin Wolf             first_offset = offset;
1274797ac58cSKevin Wolf         }
1275797ac58cSKevin Wolf 
1276797ac58cSKevin Wolf         /* Read lengths for qiov entries */
1277797ac58cSKevin Wolf         for (j = optind; j < argc; j++) {
1278797ac58cSKevin Wolf             if (!strcmp(argv[j], ";")) {
1279797ac58cSKevin Wolf                 break;
1280797ac58cSKevin Wolf             }
1281797ac58cSKevin Wolf         }
1282797ac58cSKevin Wolf 
1283797ac58cSKevin Wolf         nr_iov = j - optind;
1284797ac58cSKevin Wolf 
1285797ac58cSKevin Wolf         /* Build request */
1286797ac58cSKevin Wolf         buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
1287797ac58cSKevin Wolf         if (buf[i] == NULL) {
1288797ac58cSKevin Wolf             goto out;
1289797ac58cSKevin Wolf         }
1290797ac58cSKevin Wolf 
1291797ac58cSKevin Wolf         reqs[i].qiov = &qiovs[i];
1292797ac58cSKevin Wolf         reqs[i].sector = offset >> 9;
1293797ac58cSKevin Wolf         reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1294797ac58cSKevin Wolf 
1295797ac58cSKevin Wolf         optind = j + 1;
1296797ac58cSKevin Wolf 
1297797ac58cSKevin Wolf         pattern++;
1298797ac58cSKevin Wolf     }
1299797ac58cSKevin Wolf 
1300797ac58cSKevin Wolf     /* If there were empty requests at the end, ignore them */
1301797ac58cSKevin Wolf     nr_reqs = i;
1302797ac58cSKevin Wolf 
1303797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1304797ac58cSKevin Wolf     cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
1305797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1306797ac58cSKevin Wolf 
1307797ac58cSKevin Wolf     if (cnt < 0) {
1308797ac58cSKevin Wolf         printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1309797ac58cSKevin Wolf         goto out;
1310797ac58cSKevin Wolf     }
1311797ac58cSKevin Wolf 
1312797ac58cSKevin Wolf     if (qflag) {
1313797ac58cSKevin Wolf         goto out;
1314797ac58cSKevin Wolf     }
1315797ac58cSKevin Wolf 
1316797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1317797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1318797ac58cSKevin Wolf     print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1319797ac58cSKevin Wolf out:
1320797ac58cSKevin Wolf     for (i = 0; i < nr_reqs; i++) {
1321797ac58cSKevin Wolf         qemu_io_free(buf[i]);
1322797ac58cSKevin Wolf         if (reqs[i].qiov != NULL) {
1323797ac58cSKevin Wolf             qemu_iovec_destroy(&qiovs[i]);
1324797ac58cSKevin Wolf         }
1325797ac58cSKevin Wolf     }
1326797ac58cSKevin Wolf     g_free(buf);
1327797ac58cSKevin Wolf     g_free(reqs);
1328797ac58cSKevin Wolf     g_free(qiovs);
1329797ac58cSKevin Wolf     return 0;
1330797ac58cSKevin Wolf }
1331797ac58cSKevin Wolf 
1332797ac58cSKevin Wolf struct aio_ctx {
1333797ac58cSKevin Wolf     QEMUIOVector qiov;
1334797ac58cSKevin Wolf     int64_t offset;
1335797ac58cSKevin Wolf     char *buf;
1336797ac58cSKevin Wolf     int qflag;
1337797ac58cSKevin Wolf     int vflag;
1338797ac58cSKevin Wolf     int Cflag;
1339797ac58cSKevin Wolf     int Pflag;
1340797ac58cSKevin Wolf     int pattern;
1341797ac58cSKevin Wolf     struct timeval t1;
1342797ac58cSKevin Wolf };
1343797ac58cSKevin Wolf 
1344797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret)
1345797ac58cSKevin Wolf {
1346797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1347797ac58cSKevin Wolf     struct timeval t2;
1348797ac58cSKevin Wolf 
1349797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1350797ac58cSKevin Wolf 
1351797ac58cSKevin Wolf 
1352797ac58cSKevin Wolf     if (ret < 0) {
1353797ac58cSKevin Wolf         printf("aio_write failed: %s\n", strerror(-ret));
1354797ac58cSKevin Wolf         goto out;
1355797ac58cSKevin Wolf     }
1356797ac58cSKevin Wolf 
1357797ac58cSKevin Wolf     if (ctx->qflag) {
1358797ac58cSKevin Wolf         goto out;
1359797ac58cSKevin Wolf     }
1360797ac58cSKevin Wolf 
1361797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1362797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1363797ac58cSKevin Wolf     print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1364797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1365797ac58cSKevin Wolf out:
1366797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1367797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1368797ac58cSKevin Wolf     g_free(ctx);
1369797ac58cSKevin Wolf }
1370797ac58cSKevin Wolf 
1371797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret)
1372797ac58cSKevin Wolf {
1373797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1374797ac58cSKevin Wolf     struct timeval t2;
1375797ac58cSKevin Wolf 
1376797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1377797ac58cSKevin Wolf 
1378797ac58cSKevin Wolf     if (ret < 0) {
1379797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-ret));
1380797ac58cSKevin Wolf         goto out;
1381797ac58cSKevin Wolf     }
1382797ac58cSKevin Wolf 
1383797ac58cSKevin Wolf     if (ctx->Pflag) {
1384797ac58cSKevin Wolf         void *cmp_buf = g_malloc(ctx->qiov.size);
1385797ac58cSKevin Wolf 
1386797ac58cSKevin Wolf         memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1387797ac58cSKevin Wolf         if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1388797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
1389797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1390797ac58cSKevin Wolf         }
1391797ac58cSKevin Wolf         g_free(cmp_buf);
1392797ac58cSKevin Wolf     }
1393797ac58cSKevin Wolf 
1394797ac58cSKevin Wolf     if (ctx->qflag) {
1395797ac58cSKevin Wolf         goto out;
1396797ac58cSKevin Wolf     }
1397797ac58cSKevin Wolf 
1398797ac58cSKevin Wolf     if (ctx->vflag) {
1399797ac58cSKevin Wolf         dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1400797ac58cSKevin Wolf     }
1401797ac58cSKevin Wolf 
1402797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1403797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1404797ac58cSKevin Wolf     print_report("read", &t2, ctx->offset, ctx->qiov.size,
1405797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1406797ac58cSKevin Wolf out:
1407797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1408797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1409797ac58cSKevin Wolf     g_free(ctx);
1410797ac58cSKevin Wolf }
1411797ac58cSKevin Wolf 
1412797ac58cSKevin Wolf static void aio_read_help(void)
1413797ac58cSKevin Wolf {
1414797ac58cSKevin Wolf     printf(
1415797ac58cSKevin Wolf "\n"
1416797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n"
1417797ac58cSKevin Wolf "\n"
1418797ac58cSKevin Wolf " Example:\n"
1419797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1420797ac58cSKevin Wolf "\n"
1421797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
1422797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
1423797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n"
1424797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1425797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1426797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
1427797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
1428797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1429797ac58cSKevin Wolf "\n");
1430797ac58cSKevin Wolf }
1431797ac58cSKevin Wolf 
1432797ac58cSKevin Wolf static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
1433797ac58cSKevin Wolf 
1434797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = {
1435797ac58cSKevin Wolf     .name       = "aio_read",
1436797ac58cSKevin Wolf     .cfunc      = aio_read_f,
1437797ac58cSKevin Wolf     .argmin     = 2,
1438797ac58cSKevin Wolf     .argmax     = -1,
1439797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern ] off len [len..]",
1440797ac58cSKevin Wolf     .oneline    = "asynchronously reads a number of bytes",
1441797ac58cSKevin Wolf     .help       = aio_read_help,
1442797ac58cSKevin Wolf };
1443797ac58cSKevin Wolf 
1444797ac58cSKevin Wolf static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
1445797ac58cSKevin Wolf {
1446797ac58cSKevin Wolf     int nr_iov, c;
1447797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1448797ac58cSKevin Wolf 
1449797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1450797ac58cSKevin Wolf         switch (c) {
1451797ac58cSKevin Wolf         case 'C':
1452797ac58cSKevin Wolf             ctx->Cflag = 1;
1453797ac58cSKevin Wolf             break;
1454797ac58cSKevin Wolf         case 'P':
1455797ac58cSKevin Wolf             ctx->Pflag = 1;
1456797ac58cSKevin Wolf             ctx->pattern = parse_pattern(optarg);
1457797ac58cSKevin Wolf             if (ctx->pattern < 0) {
1458797ac58cSKevin Wolf                 g_free(ctx);
1459797ac58cSKevin Wolf                 return 0;
1460797ac58cSKevin Wolf             }
1461797ac58cSKevin Wolf             break;
1462797ac58cSKevin Wolf         case 'q':
1463797ac58cSKevin Wolf             ctx->qflag = 1;
1464797ac58cSKevin Wolf             break;
1465797ac58cSKevin Wolf         case 'v':
1466797ac58cSKevin Wolf             ctx->vflag = 1;
1467797ac58cSKevin Wolf             break;
1468797ac58cSKevin Wolf         default:
1469797ac58cSKevin Wolf             g_free(ctx);
1470c2cdf5c5SKevin Wolf             return qemuio_command_usage(&aio_read_cmd);
1471797ac58cSKevin Wolf         }
1472797ac58cSKevin Wolf     }
1473797ac58cSKevin Wolf 
1474797ac58cSKevin Wolf     if (optind > argc - 2) {
1475797ac58cSKevin Wolf         g_free(ctx);
1476c2cdf5c5SKevin Wolf         return qemuio_command_usage(&aio_read_cmd);
1477797ac58cSKevin Wolf     }
1478797ac58cSKevin Wolf 
1479797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1480797ac58cSKevin Wolf     if (ctx->offset < 0) {
1481797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1482797ac58cSKevin Wolf         g_free(ctx);
1483797ac58cSKevin Wolf         return 0;
1484797ac58cSKevin Wolf     }
1485797ac58cSKevin Wolf     optind++;
1486797ac58cSKevin Wolf 
1487797ac58cSKevin Wolf     if (ctx->offset & 0x1ff) {
1488797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1489797ac58cSKevin Wolf                ctx->offset);
1490797ac58cSKevin Wolf         g_free(ctx);
1491797ac58cSKevin Wolf         return 0;
1492797ac58cSKevin Wolf     }
1493797ac58cSKevin Wolf 
1494797ac58cSKevin Wolf     nr_iov = argc - optind;
1495797ac58cSKevin Wolf     ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1496797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1497797ac58cSKevin Wolf         g_free(ctx);
1498797ac58cSKevin Wolf         return 0;
1499797ac58cSKevin Wolf     }
1500797ac58cSKevin Wolf 
1501797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
1502797ac58cSKevin Wolf     bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1503797ac58cSKevin Wolf                    ctx->qiov.size >> 9, aio_read_done, ctx);
1504797ac58cSKevin Wolf     return 0;
1505797ac58cSKevin Wolf }
1506797ac58cSKevin Wolf 
1507797ac58cSKevin Wolf static void aio_write_help(void)
1508797ac58cSKevin Wolf {
1509797ac58cSKevin Wolf     printf(
1510797ac58cSKevin Wolf "\n"
1511797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n"
1512797ac58cSKevin Wolf " from multiple buffers\n"
1513797ac58cSKevin Wolf "\n"
1514797ac58cSKevin Wolf " Example:\n"
1515797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1516797ac58cSKevin Wolf "\n"
1517797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1518797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1519797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n"
1520797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1521797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1522797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1523797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1524797ac58cSKevin Wolf "\n");
1525797ac58cSKevin Wolf }
1526797ac58cSKevin Wolf 
1527797ac58cSKevin Wolf static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
1528797ac58cSKevin Wolf 
1529797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = {
1530797ac58cSKevin Wolf     .name       = "aio_write",
1531797ac58cSKevin Wolf     .cfunc      = aio_write_f,
1532797ac58cSKevin Wolf     .argmin     = 2,
1533797ac58cSKevin Wolf     .argmax     = -1,
1534797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..]",
1535797ac58cSKevin Wolf     .oneline    = "asynchronously writes a number of bytes",
1536797ac58cSKevin Wolf     .help       = aio_write_help,
1537797ac58cSKevin Wolf };
1538797ac58cSKevin Wolf 
1539797ac58cSKevin Wolf static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
1540797ac58cSKevin Wolf {
1541797ac58cSKevin Wolf     int nr_iov, c;
1542797ac58cSKevin Wolf     int pattern = 0xcd;
1543797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1544797ac58cSKevin Wolf 
1545797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1546797ac58cSKevin Wolf         switch (c) {
1547797ac58cSKevin Wolf         case 'C':
1548797ac58cSKevin Wolf             ctx->Cflag = 1;
1549797ac58cSKevin Wolf             break;
1550797ac58cSKevin Wolf         case 'q':
1551797ac58cSKevin Wolf             ctx->qflag = 1;
1552797ac58cSKevin Wolf             break;
1553797ac58cSKevin Wolf         case 'P':
1554797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1555797ac58cSKevin Wolf             if (pattern < 0) {
1556797ac58cSKevin Wolf                 g_free(ctx);
1557797ac58cSKevin Wolf                 return 0;
1558797ac58cSKevin Wolf             }
1559797ac58cSKevin Wolf             break;
1560797ac58cSKevin Wolf         default:
1561797ac58cSKevin Wolf             g_free(ctx);
1562c2cdf5c5SKevin Wolf             return qemuio_command_usage(&aio_write_cmd);
1563797ac58cSKevin Wolf         }
1564797ac58cSKevin Wolf     }
1565797ac58cSKevin Wolf 
1566797ac58cSKevin Wolf     if (optind > argc - 2) {
1567797ac58cSKevin Wolf         g_free(ctx);
1568c2cdf5c5SKevin Wolf         return qemuio_command_usage(&aio_write_cmd);
1569797ac58cSKevin Wolf     }
1570797ac58cSKevin Wolf 
1571797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1572797ac58cSKevin Wolf     if (ctx->offset < 0) {
1573797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1574797ac58cSKevin Wolf         g_free(ctx);
1575797ac58cSKevin Wolf         return 0;
1576797ac58cSKevin Wolf     }
1577797ac58cSKevin Wolf     optind++;
1578797ac58cSKevin Wolf 
1579797ac58cSKevin Wolf     if (ctx->offset & 0x1ff) {
1580797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1581797ac58cSKevin Wolf                ctx->offset);
1582797ac58cSKevin Wolf         g_free(ctx);
1583797ac58cSKevin Wolf         return 0;
1584797ac58cSKevin Wolf     }
1585797ac58cSKevin Wolf 
1586797ac58cSKevin Wolf     nr_iov = argc - optind;
1587797ac58cSKevin Wolf     ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
1588797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1589797ac58cSKevin Wolf         g_free(ctx);
1590797ac58cSKevin Wolf         return 0;
1591797ac58cSKevin Wolf     }
1592797ac58cSKevin Wolf 
1593797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
1594797ac58cSKevin Wolf     bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1595797ac58cSKevin Wolf                     ctx->qiov.size >> 9, aio_write_done, ctx);
1596797ac58cSKevin Wolf     return 0;
1597797ac58cSKevin Wolf }
1598797ac58cSKevin Wolf 
1599797ac58cSKevin Wolf static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
1600797ac58cSKevin Wolf {
1601797ac58cSKevin Wolf     bdrv_drain_all();
1602797ac58cSKevin Wolf     return 0;
1603797ac58cSKevin Wolf }
1604797ac58cSKevin Wolf 
1605797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = {
1606797ac58cSKevin Wolf     .name       = "aio_flush",
1607797ac58cSKevin Wolf     .cfunc      = aio_flush_f,
1608797ac58cSKevin Wolf     .oneline    = "completes all outstanding aio requests"
1609797ac58cSKevin Wolf };
1610797ac58cSKevin Wolf 
1611797ac58cSKevin Wolf static int flush_f(BlockDriverState *bs, int argc, char **argv)
1612797ac58cSKevin Wolf {
1613797ac58cSKevin Wolf     bdrv_flush(bs);
1614797ac58cSKevin Wolf     return 0;
1615797ac58cSKevin Wolf }
1616797ac58cSKevin Wolf 
1617797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = {
1618797ac58cSKevin Wolf     .name       = "flush",
1619797ac58cSKevin Wolf     .altname    = "f",
1620797ac58cSKevin Wolf     .cfunc      = flush_f,
1621797ac58cSKevin Wolf     .oneline    = "flush all in-core file state to disk",
1622797ac58cSKevin Wolf };
1623797ac58cSKevin Wolf 
1624797ac58cSKevin Wolf static int truncate_f(BlockDriverState *bs, int argc, char **argv)
1625797ac58cSKevin Wolf {
1626797ac58cSKevin Wolf     int64_t offset;
1627797ac58cSKevin Wolf     int ret;
1628797ac58cSKevin Wolf 
1629797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1630797ac58cSKevin Wolf     if (offset < 0) {
1631797ac58cSKevin Wolf         printf("non-numeric truncate argument -- %s\n", argv[1]);
1632797ac58cSKevin Wolf         return 0;
1633797ac58cSKevin Wolf     }
1634797ac58cSKevin Wolf 
1635797ac58cSKevin Wolf     ret = bdrv_truncate(bs, offset);
1636797ac58cSKevin Wolf     if (ret < 0) {
1637797ac58cSKevin Wolf         printf("truncate: %s\n", strerror(-ret));
1638797ac58cSKevin Wolf         return 0;
1639797ac58cSKevin Wolf     }
1640797ac58cSKevin Wolf 
1641797ac58cSKevin Wolf     return 0;
1642797ac58cSKevin Wolf }
1643797ac58cSKevin Wolf 
1644797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = {
1645797ac58cSKevin Wolf     .name       = "truncate",
1646797ac58cSKevin Wolf     .altname    = "t",
1647797ac58cSKevin Wolf     .cfunc      = truncate_f,
1648797ac58cSKevin Wolf     .argmin     = 1,
1649797ac58cSKevin Wolf     .argmax     = 1,
1650797ac58cSKevin Wolf     .args       = "off",
1651797ac58cSKevin Wolf     .oneline    = "truncates the current file at the given offset",
1652797ac58cSKevin Wolf };
1653797ac58cSKevin Wolf 
1654797ac58cSKevin Wolf static int length_f(BlockDriverState *bs, int argc, char **argv)
1655797ac58cSKevin Wolf {
1656797ac58cSKevin Wolf     int64_t size;
1657797ac58cSKevin Wolf     char s1[64];
1658797ac58cSKevin Wolf 
1659797ac58cSKevin Wolf     size = bdrv_getlength(bs);
1660797ac58cSKevin Wolf     if (size < 0) {
1661797ac58cSKevin Wolf         printf("getlength: %s\n", strerror(-size));
1662797ac58cSKevin Wolf         return 0;
1663797ac58cSKevin Wolf     }
1664797ac58cSKevin Wolf 
1665797ac58cSKevin Wolf     cvtstr(size, s1, sizeof(s1));
1666797ac58cSKevin Wolf     printf("%s\n", s1);
1667797ac58cSKevin Wolf     return 0;
1668797ac58cSKevin Wolf }
1669797ac58cSKevin Wolf 
1670797ac58cSKevin Wolf 
1671797ac58cSKevin Wolf static const cmdinfo_t length_cmd = {
1672797ac58cSKevin Wolf     .name   = "length",
1673797ac58cSKevin Wolf     .altname    = "l",
1674797ac58cSKevin Wolf     .cfunc      = length_f,
1675797ac58cSKevin Wolf     .oneline    = "gets the length of the current file",
1676797ac58cSKevin Wolf };
1677797ac58cSKevin Wolf 
1678797ac58cSKevin Wolf 
1679797ac58cSKevin Wolf static int info_f(BlockDriverState *bs, int argc, char **argv)
1680797ac58cSKevin Wolf {
1681797ac58cSKevin Wolf     BlockDriverInfo bdi;
1682a8d8ecb7SMax Reitz     ImageInfoSpecific *spec_info;
1683797ac58cSKevin Wolf     char s1[64], s2[64];
1684797ac58cSKevin Wolf     int ret;
1685797ac58cSKevin Wolf 
1686797ac58cSKevin Wolf     if (bs->drv && bs->drv->format_name) {
1687797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->format_name);
1688797ac58cSKevin Wolf     }
1689797ac58cSKevin Wolf     if (bs->drv && bs->drv->protocol_name) {
1690797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->protocol_name);
1691797ac58cSKevin Wolf     }
1692797ac58cSKevin Wolf 
1693797ac58cSKevin Wolf     ret = bdrv_get_info(bs, &bdi);
1694797ac58cSKevin Wolf     if (ret) {
1695797ac58cSKevin Wolf         return 0;
1696797ac58cSKevin Wolf     }
1697797ac58cSKevin Wolf 
1698797ac58cSKevin Wolf     cvtstr(bdi.cluster_size, s1, sizeof(s1));
1699797ac58cSKevin Wolf     cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1700797ac58cSKevin Wolf 
1701797ac58cSKevin Wolf     printf("cluster size: %s\n", s1);
1702797ac58cSKevin Wolf     printf("vm state offset: %s\n", s2);
1703797ac58cSKevin Wolf 
1704a8d8ecb7SMax Reitz     spec_info = bdrv_get_specific_info(bs);
1705a8d8ecb7SMax Reitz     if (spec_info) {
1706a8d8ecb7SMax Reitz         printf("Format specific information:\n");
1707a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1708a8d8ecb7SMax Reitz         qapi_free_ImageInfoSpecific(spec_info);
1709a8d8ecb7SMax Reitz     }
1710a8d8ecb7SMax Reitz 
1711797ac58cSKevin Wolf     return 0;
1712797ac58cSKevin Wolf }
1713797ac58cSKevin Wolf 
1714797ac58cSKevin Wolf 
1715797ac58cSKevin Wolf 
1716797ac58cSKevin Wolf static const cmdinfo_t info_cmd = {
1717797ac58cSKevin Wolf     .name       = "info",
1718797ac58cSKevin Wolf     .altname    = "i",
1719797ac58cSKevin Wolf     .cfunc      = info_f,
1720797ac58cSKevin Wolf     .oneline    = "prints information about the current file",
1721797ac58cSKevin Wolf };
1722797ac58cSKevin Wolf 
1723797ac58cSKevin Wolf static void discard_help(void)
1724797ac58cSKevin Wolf {
1725797ac58cSKevin Wolf     printf(
1726797ac58cSKevin Wolf "\n"
1727797ac58cSKevin Wolf " discards a range of bytes from the given offset\n"
1728797ac58cSKevin Wolf "\n"
1729797ac58cSKevin Wolf " Example:\n"
1730797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1731797ac58cSKevin Wolf "\n"
1732797ac58cSKevin Wolf " Discards a segment of the currently open file.\n"
1733797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1734797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1735797ac58cSKevin Wolf "\n");
1736797ac58cSKevin Wolf }
1737797ac58cSKevin Wolf 
1738797ac58cSKevin Wolf static int discard_f(BlockDriverState *bs, int argc, char **argv);
1739797ac58cSKevin Wolf 
1740797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = {
1741797ac58cSKevin Wolf     .name       = "discard",
1742797ac58cSKevin Wolf     .altname    = "d",
1743797ac58cSKevin Wolf     .cfunc      = discard_f,
1744797ac58cSKevin Wolf     .argmin     = 2,
1745797ac58cSKevin Wolf     .argmax     = -1,
1746797ac58cSKevin Wolf     .args       = "[-Cq] off len",
1747797ac58cSKevin Wolf     .oneline    = "discards a number of bytes at a specified offset",
1748797ac58cSKevin Wolf     .help       = discard_help,
1749797ac58cSKevin Wolf };
1750797ac58cSKevin Wolf 
1751797ac58cSKevin Wolf static int discard_f(BlockDriverState *bs, int argc, char **argv)
1752797ac58cSKevin Wolf {
1753797ac58cSKevin Wolf     struct timeval t1, t2;
1754797ac58cSKevin Wolf     int Cflag = 0, qflag = 0;
1755797ac58cSKevin Wolf     int c, ret;
1756797ac58cSKevin Wolf     int64_t offset;
1757797ac58cSKevin Wolf     int count;
1758797ac58cSKevin Wolf 
1759797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "Cq")) != EOF) {
1760797ac58cSKevin Wolf         switch (c) {
1761797ac58cSKevin Wolf         case 'C':
1762797ac58cSKevin Wolf             Cflag = 1;
1763797ac58cSKevin Wolf             break;
1764797ac58cSKevin Wolf         case 'q':
1765797ac58cSKevin Wolf             qflag = 1;
1766797ac58cSKevin Wolf             break;
1767797ac58cSKevin Wolf         default:
1768c2cdf5c5SKevin Wolf             return qemuio_command_usage(&discard_cmd);
1769797ac58cSKevin Wolf         }
1770797ac58cSKevin Wolf     }
1771797ac58cSKevin Wolf 
1772797ac58cSKevin Wolf     if (optind != argc - 2) {
1773c2cdf5c5SKevin Wolf         return qemuio_command_usage(&discard_cmd);
1774797ac58cSKevin Wolf     }
1775797ac58cSKevin Wolf 
1776797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1777797ac58cSKevin Wolf     if (offset < 0) {
1778797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1779797ac58cSKevin Wolf         return 0;
1780797ac58cSKevin Wolf     }
1781797ac58cSKevin Wolf 
1782797ac58cSKevin Wolf     optind++;
1783797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1784797ac58cSKevin Wolf     if (count < 0) {
1785797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1786797ac58cSKevin Wolf         return 0;
1787797ac58cSKevin Wolf     }
1788797ac58cSKevin Wolf 
1789797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1790797ac58cSKevin Wolf     ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1791797ac58cSKevin Wolf                        count >> BDRV_SECTOR_BITS);
1792797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1793797ac58cSKevin Wolf 
1794797ac58cSKevin Wolf     if (ret < 0) {
1795797ac58cSKevin Wolf         printf("discard failed: %s\n", strerror(-ret));
1796797ac58cSKevin Wolf         goto out;
1797797ac58cSKevin Wolf     }
1798797ac58cSKevin Wolf 
1799797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1800797ac58cSKevin Wolf     if (!qflag) {
1801797ac58cSKevin Wolf         t2 = tsub(t2, t1);
1802797ac58cSKevin Wolf         print_report("discard", &t2, offset, count, count, 1, Cflag);
1803797ac58cSKevin Wolf     }
1804797ac58cSKevin Wolf 
1805797ac58cSKevin Wolf out:
1806797ac58cSKevin Wolf     return 0;
1807797ac58cSKevin Wolf }
1808797ac58cSKevin Wolf 
1809797ac58cSKevin Wolf static int alloc_f(BlockDriverState *bs, int argc, char **argv)
1810797ac58cSKevin Wolf {
1811797ac58cSKevin Wolf     int64_t offset, sector_num;
1812797ac58cSKevin Wolf     int nb_sectors, remaining;
1813797ac58cSKevin Wolf     char s1[64];
1814797ac58cSKevin Wolf     int num, sum_alloc;
1815797ac58cSKevin Wolf     int ret;
1816797ac58cSKevin Wolf 
1817797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1818797ac58cSKevin Wolf     if (offset < 0) {
1819797ac58cSKevin Wolf         printf("non-numeric offset argument -- %s\n", argv[1]);
1820797ac58cSKevin Wolf         return 0;
1821797ac58cSKevin Wolf     } else if (offset & 0x1ff) {
1822797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1823797ac58cSKevin Wolf                offset);
1824797ac58cSKevin Wolf         return 0;
1825797ac58cSKevin Wolf     }
1826797ac58cSKevin Wolf 
1827797ac58cSKevin Wolf     if (argc == 3) {
1828797ac58cSKevin Wolf         nb_sectors = cvtnum(argv[2]);
1829797ac58cSKevin Wolf         if (nb_sectors < 0) {
1830797ac58cSKevin Wolf             printf("non-numeric length argument -- %s\n", argv[2]);
1831797ac58cSKevin Wolf             return 0;
1832797ac58cSKevin Wolf         }
1833797ac58cSKevin Wolf     } else {
1834797ac58cSKevin Wolf         nb_sectors = 1;
1835797ac58cSKevin Wolf     }
1836797ac58cSKevin Wolf 
1837797ac58cSKevin Wolf     remaining = nb_sectors;
1838797ac58cSKevin Wolf     sum_alloc = 0;
1839797ac58cSKevin Wolf     sector_num = offset >> 9;
1840797ac58cSKevin Wolf     while (remaining) {
1841797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1842d663640cSPaolo Bonzini         if (ret < 0) {
1843d663640cSPaolo Bonzini             printf("is_allocated failed: %s\n", strerror(-ret));
1844d663640cSPaolo Bonzini             return 0;
1845d663640cSPaolo Bonzini         }
1846797ac58cSKevin Wolf         sector_num += num;
1847797ac58cSKevin Wolf         remaining -= num;
1848797ac58cSKevin Wolf         if (ret) {
1849797ac58cSKevin Wolf             sum_alloc += num;
1850797ac58cSKevin Wolf         }
1851797ac58cSKevin Wolf         if (num == 0) {
1852797ac58cSKevin Wolf             nb_sectors -= remaining;
1853797ac58cSKevin Wolf             remaining = 0;
1854797ac58cSKevin Wolf         }
1855797ac58cSKevin Wolf     }
1856797ac58cSKevin Wolf 
1857797ac58cSKevin Wolf     cvtstr(offset, s1, sizeof(s1));
1858797ac58cSKevin Wolf 
1859797ac58cSKevin Wolf     printf("%d/%d sectors allocated at offset %s\n",
1860797ac58cSKevin Wolf            sum_alloc, nb_sectors, s1);
1861797ac58cSKevin Wolf     return 0;
1862797ac58cSKevin Wolf }
1863797ac58cSKevin Wolf 
1864797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = {
1865797ac58cSKevin Wolf     .name       = "alloc",
1866797ac58cSKevin Wolf     .altname    = "a",
1867797ac58cSKevin Wolf     .argmin     = 1,
1868797ac58cSKevin Wolf     .argmax     = 2,
1869797ac58cSKevin Wolf     .cfunc      = alloc_f,
1870797ac58cSKevin Wolf     .args       = "off [sectors]",
1871797ac58cSKevin Wolf     .oneline    = "checks if a sector is present in the file",
1872797ac58cSKevin Wolf };
1873797ac58cSKevin Wolf 
1874797ac58cSKevin Wolf 
1875797ac58cSKevin Wolf static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1876797ac58cSKevin Wolf                             int64_t nb_sectors, int64_t *pnum)
1877797ac58cSKevin Wolf {
1878797ac58cSKevin Wolf     int num, num_checked;
1879797ac58cSKevin Wolf     int ret, firstret;
1880797ac58cSKevin Wolf 
1881797ac58cSKevin Wolf     num_checked = MIN(nb_sectors, INT_MAX);
1882797ac58cSKevin Wolf     ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1883797ac58cSKevin Wolf     if (ret < 0) {
1884797ac58cSKevin Wolf         return ret;
1885797ac58cSKevin Wolf     }
1886797ac58cSKevin Wolf 
1887797ac58cSKevin Wolf     firstret = ret;
1888797ac58cSKevin Wolf     *pnum = num;
1889797ac58cSKevin Wolf 
1890797ac58cSKevin Wolf     while (nb_sectors > 0 && ret == firstret) {
1891797ac58cSKevin Wolf         sector_num += num;
1892797ac58cSKevin Wolf         nb_sectors -= num;
1893797ac58cSKevin Wolf 
1894797ac58cSKevin Wolf         num_checked = MIN(nb_sectors, INT_MAX);
1895797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1896797ac58cSKevin Wolf         if (ret == firstret) {
1897797ac58cSKevin Wolf             *pnum += num;
1898797ac58cSKevin Wolf         } else {
1899797ac58cSKevin Wolf             break;
1900797ac58cSKevin Wolf         }
1901797ac58cSKevin Wolf     }
1902797ac58cSKevin Wolf 
1903797ac58cSKevin Wolf     return firstret;
1904797ac58cSKevin Wolf }
1905797ac58cSKevin Wolf 
1906797ac58cSKevin Wolf static int map_f(BlockDriverState *bs, int argc, char **argv)
1907797ac58cSKevin Wolf {
1908797ac58cSKevin Wolf     int64_t offset;
1909797ac58cSKevin Wolf     int64_t nb_sectors;
1910797ac58cSKevin Wolf     char s1[64];
1911797ac58cSKevin Wolf     int64_t num;
1912797ac58cSKevin Wolf     int ret;
1913797ac58cSKevin Wolf     const char *retstr;
1914797ac58cSKevin Wolf 
1915797ac58cSKevin Wolf     offset = 0;
1916797ac58cSKevin Wolf     nb_sectors = bs->total_sectors;
1917797ac58cSKevin Wolf 
1918797ac58cSKevin Wolf     do {
1919797ac58cSKevin Wolf         ret = map_is_allocated(bs, offset, nb_sectors, &num);
1920797ac58cSKevin Wolf         if (ret < 0) {
1921797ac58cSKevin Wolf             error_report("Failed to get allocation status: %s", strerror(-ret));
1922797ac58cSKevin Wolf             return 0;
1923797ac58cSKevin Wolf         }
1924797ac58cSKevin Wolf 
1925797ac58cSKevin Wolf         retstr = ret ? "    allocated" : "not allocated";
1926797ac58cSKevin Wolf         cvtstr(offset << 9ULL, s1, sizeof(s1));
1927797ac58cSKevin Wolf         printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1928797ac58cSKevin Wolf                "at offset %s (%d)\n",
1929797ac58cSKevin Wolf                offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1930797ac58cSKevin Wolf 
1931797ac58cSKevin Wolf         offset += num;
1932797ac58cSKevin Wolf         nb_sectors -= num;
1933797ac58cSKevin Wolf     } while (offset < bs->total_sectors);
1934797ac58cSKevin Wolf 
1935797ac58cSKevin Wolf     return 0;
1936797ac58cSKevin Wolf }
1937797ac58cSKevin Wolf 
1938797ac58cSKevin Wolf static const cmdinfo_t map_cmd = {
1939797ac58cSKevin Wolf        .name           = "map",
1940797ac58cSKevin Wolf        .argmin         = 0,
1941797ac58cSKevin Wolf        .argmax         = 0,
1942797ac58cSKevin Wolf        .cfunc          = map_f,
1943797ac58cSKevin Wolf        .args           = "",
1944797ac58cSKevin Wolf        .oneline        = "prints the allocated areas of a file",
1945797ac58cSKevin Wolf };
1946797ac58cSKevin Wolf 
1947797ac58cSKevin Wolf static int break_f(BlockDriverState *bs, int argc, char **argv)
1948797ac58cSKevin Wolf {
1949797ac58cSKevin Wolf     int ret;
1950797ac58cSKevin Wolf 
1951797ac58cSKevin Wolf     ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1952797ac58cSKevin Wolf     if (ret < 0) {
1953797ac58cSKevin Wolf         printf("Could not set breakpoint: %s\n", strerror(-ret));
1954797ac58cSKevin Wolf     }
1955797ac58cSKevin Wolf 
1956797ac58cSKevin Wolf     return 0;
1957797ac58cSKevin Wolf }
1958797ac58cSKevin Wolf 
1959*4cc70e93SFam Zheng static int remove_break_f(BlockDriverState *bs, int argc, char **argv)
1960*4cc70e93SFam Zheng {
1961*4cc70e93SFam Zheng     int ret;
1962*4cc70e93SFam Zheng 
1963*4cc70e93SFam Zheng     ret = bdrv_debug_remove_breakpoint(bs, argv[1]);
1964*4cc70e93SFam Zheng     if (ret < 0) {
1965*4cc70e93SFam Zheng         printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
1966*4cc70e93SFam Zheng     }
1967*4cc70e93SFam Zheng 
1968*4cc70e93SFam Zheng     return 0;
1969*4cc70e93SFam Zheng }
1970*4cc70e93SFam Zheng 
1971797ac58cSKevin Wolf static const cmdinfo_t break_cmd = {
1972797ac58cSKevin Wolf        .name           = "break",
1973797ac58cSKevin Wolf        .argmin         = 2,
1974797ac58cSKevin Wolf        .argmax         = 2,
1975797ac58cSKevin Wolf        .cfunc          = break_f,
1976797ac58cSKevin Wolf        .args           = "event tag",
1977797ac58cSKevin Wolf        .oneline        = "sets a breakpoint on event and tags the stopped "
1978797ac58cSKevin Wolf                          "request as tag",
1979797ac58cSKevin Wolf };
1980797ac58cSKevin Wolf 
1981*4cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = {
1982*4cc70e93SFam Zheng        .name           = "remove_break",
1983*4cc70e93SFam Zheng        .argmin         = 1,
1984*4cc70e93SFam Zheng        .argmax         = 1,
1985*4cc70e93SFam Zheng        .cfunc          = remove_break_f,
1986*4cc70e93SFam Zheng        .args           = "tag",
1987*4cc70e93SFam Zheng        .oneline        = "remove a breakpoint by tag",
1988*4cc70e93SFam Zheng };
1989*4cc70e93SFam Zheng 
1990797ac58cSKevin Wolf static int resume_f(BlockDriverState *bs, int argc, char **argv)
1991797ac58cSKevin Wolf {
1992797ac58cSKevin Wolf     int ret;
1993797ac58cSKevin Wolf 
1994797ac58cSKevin Wolf     ret = bdrv_debug_resume(bs, argv[1]);
1995797ac58cSKevin Wolf     if (ret < 0) {
1996797ac58cSKevin Wolf         printf("Could not resume request: %s\n", strerror(-ret));
1997797ac58cSKevin Wolf     }
1998797ac58cSKevin Wolf 
1999797ac58cSKevin Wolf     return 0;
2000797ac58cSKevin Wolf }
2001797ac58cSKevin Wolf 
2002797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = {
2003797ac58cSKevin Wolf        .name           = "resume",
2004797ac58cSKevin Wolf        .argmin         = 1,
2005797ac58cSKevin Wolf        .argmax         = 1,
2006797ac58cSKevin Wolf        .cfunc          = resume_f,
2007797ac58cSKevin Wolf        .args           = "tag",
2008797ac58cSKevin Wolf        .oneline        = "resumes the request tagged as tag",
2009797ac58cSKevin Wolf };
2010797ac58cSKevin Wolf 
2011797ac58cSKevin Wolf static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
2012797ac58cSKevin Wolf {
2013797ac58cSKevin Wolf     while (!bdrv_debug_is_suspended(bs, argv[1])) {
2014797ac58cSKevin Wolf         qemu_aio_wait();
2015797ac58cSKevin Wolf     }
2016797ac58cSKevin Wolf 
2017797ac58cSKevin Wolf     return 0;
2018797ac58cSKevin Wolf }
2019797ac58cSKevin Wolf 
2020797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = {
2021797ac58cSKevin Wolf        .name           = "wait_break",
2022797ac58cSKevin Wolf        .argmin         = 1,
2023797ac58cSKevin Wolf        .argmax         = 1,
2024797ac58cSKevin Wolf        .cfunc          = wait_break_f,
2025797ac58cSKevin Wolf        .args           = "tag",
2026797ac58cSKevin Wolf        .oneline        = "waits for the suspension of a request",
2027797ac58cSKevin Wolf };
2028797ac58cSKevin Wolf 
2029797ac58cSKevin Wolf static int abort_f(BlockDriverState *bs, int argc, char **argv)
2030797ac58cSKevin Wolf {
2031797ac58cSKevin Wolf     abort();
2032797ac58cSKevin Wolf }
2033797ac58cSKevin Wolf 
2034797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = {
2035797ac58cSKevin Wolf        .name           = "abort",
2036797ac58cSKevin Wolf        .cfunc          = abort_f,
2037797ac58cSKevin Wolf        .flags          = CMD_NOFILE_OK,
2038797ac58cSKevin Wolf        .oneline        = "simulate a program crash using abort(3)",
2039797ac58cSKevin Wolf };
2040797ac58cSKevin Wolf 
2041f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2042f18a834aSKevin Wolf {
2043f18a834aSKevin Wolf     if (cmd) {
2044f18a834aSKevin Wolf         printf("%s ", cmd);
2045f18a834aSKevin Wolf     } else {
2046f18a834aSKevin Wolf         printf("%s ", ct->name);
2047f18a834aSKevin Wolf         if (ct->altname) {
2048f18a834aSKevin Wolf             printf("(or %s) ", ct->altname);
2049f18a834aSKevin Wolf         }
2050f18a834aSKevin Wolf     }
2051f18a834aSKevin Wolf 
2052f18a834aSKevin Wolf     if (ct->args) {
2053f18a834aSKevin Wolf         printf("%s ", ct->args);
2054f18a834aSKevin Wolf     }
2055f18a834aSKevin Wolf     printf("-- %s\n", ct->oneline);
2056f18a834aSKevin Wolf }
2057f18a834aSKevin Wolf 
2058f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2059f18a834aSKevin Wolf {
2060f18a834aSKevin Wolf     help_oneline(cmd, ct);
2061f18a834aSKevin Wolf     if (ct->help) {
2062f18a834aSKevin Wolf         ct->help();
2063f18a834aSKevin Wolf     }
2064f18a834aSKevin Wolf }
2065f18a834aSKevin Wolf 
2066f18a834aSKevin Wolf static void help_all(void)
2067f18a834aSKevin Wolf {
2068f18a834aSKevin Wolf     const cmdinfo_t *ct;
2069f18a834aSKevin Wolf 
2070f18a834aSKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2071f18a834aSKevin Wolf         help_oneline(ct->name, ct);
2072f18a834aSKevin Wolf     }
2073f18a834aSKevin Wolf     printf("\nUse 'help commandname' for extended help.\n");
2074f18a834aSKevin Wolf }
2075f18a834aSKevin Wolf 
2076f18a834aSKevin Wolf static int help_f(BlockDriverState *bs, int argc, char **argv)
2077f18a834aSKevin Wolf {
2078f18a834aSKevin Wolf     const cmdinfo_t *ct;
2079f18a834aSKevin Wolf 
2080f18a834aSKevin Wolf     if (argc == 1) {
2081f18a834aSKevin Wolf         help_all();
2082f18a834aSKevin Wolf         return 0;
2083f18a834aSKevin Wolf     }
2084f18a834aSKevin Wolf 
2085f18a834aSKevin Wolf     ct = find_command(argv[1]);
2086f18a834aSKevin Wolf     if (ct == NULL) {
2087f18a834aSKevin Wolf         printf("command %s not found\n", argv[1]);
2088f18a834aSKevin Wolf         return 0;
2089f18a834aSKevin Wolf     }
2090f18a834aSKevin Wolf 
2091f18a834aSKevin Wolf     help_onecmd(argv[1], ct);
2092f18a834aSKevin Wolf     return 0;
2093f18a834aSKevin Wolf }
2094f18a834aSKevin Wolf 
2095f18a834aSKevin Wolf static const cmdinfo_t help_cmd = {
2096f18a834aSKevin Wolf     .name       = "help",
2097f18a834aSKevin Wolf     .altname    = "?",
2098f18a834aSKevin Wolf     .cfunc      = help_f,
2099f18a834aSKevin Wolf     .argmin     = 0,
2100f18a834aSKevin Wolf     .argmax     = 1,
2101f18a834aSKevin Wolf     .flags      = CMD_FLAG_GLOBAL,
2102f18a834aSKevin Wolf     .args       = "[command]",
2103f18a834aSKevin Wolf     .oneline    = "help for one or all commands",
2104f18a834aSKevin Wolf };
2105f18a834aSKevin Wolf 
21063d21994fSKevin Wolf bool qemuio_command(BlockDriverState *bs, const char *cmd)
2107dd583296SKevin Wolf {
2108dd583296SKevin Wolf     char *input;
2109dd583296SKevin Wolf     const cmdinfo_t *ct;
2110dd583296SKevin Wolf     char **v;
2111dd583296SKevin Wolf     int c;
2112dd583296SKevin Wolf     bool done = false;
2113dd583296SKevin Wolf 
2114dd583296SKevin Wolf     input = g_strdup(cmd);
2115dd583296SKevin Wolf     v = breakline(input, &c);
2116dd583296SKevin Wolf     if (c) {
2117dd583296SKevin Wolf         ct = find_command(v[0]);
2118dd583296SKevin Wolf         if (ct) {
21193d21994fSKevin Wolf             done = command(bs, ct, c, v);
2120dd583296SKevin Wolf         } else {
2121dd583296SKevin Wolf             fprintf(stderr, "command \"%s\" not found\n", v[0]);
2122dd583296SKevin Wolf         }
2123dd583296SKevin Wolf     }
2124dd583296SKevin Wolf     g_free(input);
2125dd583296SKevin Wolf     g_free(v);
2126dd583296SKevin Wolf 
2127dd583296SKevin Wolf     return done;
2128dd583296SKevin Wolf }
2129dd583296SKevin Wolf 
2130797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void)
2131797ac58cSKevin Wolf {
2132797ac58cSKevin Wolf     /* initialize commands */
2133c2cdf5c5SKevin Wolf     qemuio_add_command(&help_cmd);
2134c2cdf5c5SKevin Wolf     qemuio_add_command(&read_cmd);
2135c2cdf5c5SKevin Wolf     qemuio_add_command(&readv_cmd);
2136c2cdf5c5SKevin Wolf     qemuio_add_command(&write_cmd);
2137c2cdf5c5SKevin Wolf     qemuio_add_command(&writev_cmd);
2138c2cdf5c5SKevin Wolf     qemuio_add_command(&multiwrite_cmd);
2139c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_read_cmd);
2140c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_write_cmd);
2141c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_flush_cmd);
2142c2cdf5c5SKevin Wolf     qemuio_add_command(&flush_cmd);
2143c2cdf5c5SKevin Wolf     qemuio_add_command(&truncate_cmd);
2144c2cdf5c5SKevin Wolf     qemuio_add_command(&length_cmd);
2145c2cdf5c5SKevin Wolf     qemuio_add_command(&info_cmd);
2146c2cdf5c5SKevin Wolf     qemuio_add_command(&discard_cmd);
2147c2cdf5c5SKevin Wolf     qemuio_add_command(&alloc_cmd);
2148c2cdf5c5SKevin Wolf     qemuio_add_command(&map_cmd);
2149c2cdf5c5SKevin Wolf     qemuio_add_command(&break_cmd);
2150*4cc70e93SFam Zheng     qemuio_add_command(&remove_break_cmd);
2151c2cdf5c5SKevin Wolf     qemuio_add_command(&resume_cmd);
2152c2cdf5c5SKevin Wolf     qemuio_add_command(&wait_break_cmd);
2153c2cdf5c5SKevin Wolf     qemuio_add_command(&abort_cmd);
2154797ac58cSKevin Wolf }
2155