xref: /qemu/qemu-io-cmds.c (revision b444d0e9)
1797ac58cSKevin Wolf /*
2797ac58cSKevin Wolf  * Command line utility to exercise the QEMU I/O path.
3797ac58cSKevin Wolf  *
4093ea232SEric Blake  * Copyright (C) 2009-2016 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 
1180c71a24SPeter Maydell #include "qemu/osdep.h"
12da34e65cSMarkus Armbruster #include "qapi/error.h"
133d21994fSKevin Wolf #include "qemu-io.h"
144c7b7e9bSMax Reitz #include "sysemu/block-backend.h"
154c7b7e9bSMax Reitz #include "block/block.h"
164c7b7e9bSMax Reitz #include "block/block_int.h" /* for info_f() */
17a8d8ecb7SMax Reitz #include "block/qapi.h"
18d49b6836SMarkus Armbruster #include "qemu/error-report.h"
196a1751b7SAlex Bligh #include "qemu/main-loop.h"
20922a01a0SMarkus Armbruster #include "qemu/option.h"
21cd33d02aSKevin Wolf #include "qemu/timer.h"
22f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
23797ac58cSKevin Wolf 
24797ac58cSKevin Wolf #define CMD_NOFILE_OK   0x01
25797ac58cSKevin Wolf 
26f9883880SStefan Weil bool qemuio_misalign;
27797ac58cSKevin Wolf 
28c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab;
29c2cdf5c5SKevin Wolf static int ncmds;
30c2cdf5c5SKevin Wolf 
31c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b)
32c2cdf5c5SKevin Wolf {
33c2cdf5c5SKevin Wolf     return strcmp(((const cmdinfo_t *)a)->name,
34c2cdf5c5SKevin Wolf                   ((const cmdinfo_t *)b)->name);
35c2cdf5c5SKevin Wolf }
36c2cdf5c5SKevin Wolf 
37c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci)
38c2cdf5c5SKevin Wolf {
396aabeb58SPeter Maydell     /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
406aabeb58SPeter Maydell      * flags allow it not to be, so that combination is invalid.
416aabeb58SPeter Maydell      * Catch it now rather than letting it manifest as a crash if a
426aabeb58SPeter Maydell      * particular set of command line options are used.
436aabeb58SPeter Maydell      */
446aabeb58SPeter Maydell     assert(ci->perm == 0 ||
456aabeb58SPeter Maydell            (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
4602c4f26bSMarkus Armbruster     cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
47c2cdf5c5SKevin Wolf     cmdtab[ncmds - 1] = *ci;
48c2cdf5c5SKevin Wolf     qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
49c2cdf5c5SKevin Wolf }
50c2cdf5c5SKevin Wolf 
51*b444d0e9SMax Reitz void qemuio_command_usage(const cmdinfo_t *ci)
52c2cdf5c5SKevin Wolf {
53c2cdf5c5SKevin Wolf     printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
54c2cdf5c5SKevin Wolf }
55c2cdf5c5SKevin Wolf 
564c7b7e9bSMax Reitz static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
57c2cdf5c5SKevin Wolf {
58c2cdf5c5SKevin Wolf     if (ct->flags & CMD_FLAG_GLOBAL) {
59c2cdf5c5SKevin Wolf         return 1;
60c2cdf5c5SKevin Wolf     }
614c7b7e9bSMax Reitz     if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
62c2cdf5c5SKevin Wolf         fprintf(stderr, "no file open, try 'help open'\n");
63c2cdf5c5SKevin Wolf         return 0;
64c2cdf5c5SKevin Wolf     }
65c2cdf5c5SKevin Wolf     return 1;
66c2cdf5c5SKevin Wolf }
67c2cdf5c5SKevin Wolf 
68*b444d0e9SMax Reitz static void command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
693d21994fSKevin Wolf                     char **argv)
70c2cdf5c5SKevin Wolf {
71c2cdf5c5SKevin Wolf     char *cmd = argv[0];
72c2cdf5c5SKevin Wolf 
734c7b7e9bSMax Reitz     if (!init_check_command(blk, ct)) {
74*b444d0e9SMax Reitz         return;
75c2cdf5c5SKevin Wolf     }
76c2cdf5c5SKevin Wolf 
77c2cdf5c5SKevin Wolf     if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
78c2cdf5c5SKevin Wolf         if (ct->argmax == -1) {
79c2cdf5c5SKevin Wolf             fprintf(stderr,
80c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected at least %d arguments\n",
81c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
82c2cdf5c5SKevin Wolf         } else if (ct->argmin == ct->argmax) {
83c2cdf5c5SKevin Wolf             fprintf(stderr,
84c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected %d arguments\n",
85c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
86c2cdf5c5SKevin Wolf         } else {
87c2cdf5c5SKevin Wolf             fprintf(stderr,
88c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected between %d and %d arguments\n",
89c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin, ct->argmax);
90c2cdf5c5SKevin Wolf         }
91*b444d0e9SMax Reitz         return;
92c2cdf5c5SKevin Wolf     }
93887354bdSKevin Wolf 
94887354bdSKevin Wolf     /* Request additional permissions if necessary for this command. The caller
95887354bdSKevin Wolf      * is responsible for restoring the original permissions afterwards if this
96887354bdSKevin Wolf      * is what it wants. */
97887354bdSKevin Wolf     if (ct->perm && blk_is_available(blk)) {
98887354bdSKevin Wolf         uint64_t orig_perm, orig_shared_perm;
99887354bdSKevin Wolf         blk_get_perm(blk, &orig_perm, &orig_shared_perm);
100887354bdSKevin Wolf 
101887354bdSKevin Wolf         if (ct->perm & ~orig_perm) {
102887354bdSKevin Wolf             uint64_t new_perm;
103887354bdSKevin Wolf             Error *local_err = NULL;
104887354bdSKevin Wolf             int ret;
105887354bdSKevin Wolf 
106887354bdSKevin Wolf             new_perm = orig_perm | ct->perm;
107887354bdSKevin Wolf 
108887354bdSKevin Wolf             ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
109887354bdSKevin Wolf             if (ret < 0) {
110887354bdSKevin Wolf                 error_report_err(local_err);
111*b444d0e9SMax Reitz                 return;
112887354bdSKevin Wolf             }
113887354bdSKevin Wolf         }
114887354bdSKevin Wolf     }
115887354bdSKevin Wolf 
116c2cdf5c5SKevin Wolf     optind = 0;
117*b444d0e9SMax Reitz     ct->cfunc(blk, argc, argv);
118c2cdf5c5SKevin Wolf }
119c2cdf5c5SKevin Wolf 
120c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd)
121c2cdf5c5SKevin Wolf {
122c2cdf5c5SKevin Wolf     cmdinfo_t *ct;
123c2cdf5c5SKevin Wolf 
124c2cdf5c5SKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
125c2cdf5c5SKevin Wolf         if (strcmp(ct->name, cmd) == 0 ||
126c2cdf5c5SKevin Wolf             (ct->altname && strcmp(ct->altname, cmd) == 0))
127c2cdf5c5SKevin Wolf         {
128c2cdf5c5SKevin Wolf             return (const cmdinfo_t *)ct;
129c2cdf5c5SKevin Wolf         }
130c2cdf5c5SKevin Wolf     }
131c2cdf5c5SKevin Wolf     return NULL;
132c2cdf5c5SKevin Wolf }
133c2cdf5c5SKevin Wolf 
1344694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */
1354694020dSStefan Hajnoczi void qemuio_complete_command(const char *input,
1364694020dSStefan Hajnoczi                              void (*fn)(const char *cmd, void *opaque),
1374694020dSStefan Hajnoczi                              void *opaque)
1384694020dSStefan Hajnoczi {
1394694020dSStefan Hajnoczi     cmdinfo_t *ct;
1404694020dSStefan Hajnoczi     size_t input_len = strlen(input);
1414694020dSStefan Hajnoczi 
1424694020dSStefan Hajnoczi     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
1434694020dSStefan Hajnoczi         if (strncmp(input, ct->name, input_len) == 0) {
1444694020dSStefan Hajnoczi             fn(ct->name, opaque);
1454694020dSStefan Hajnoczi         }
1464694020dSStefan Hajnoczi     }
1474694020dSStefan Hajnoczi }
1484694020dSStefan Hajnoczi 
149c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count)
150c2cdf5c5SKevin Wolf {
151c2cdf5c5SKevin Wolf     int c = 0;
152c2cdf5c5SKevin Wolf     char *p;
1535839e53bSMarkus Armbruster     char **rval = g_new0(char *, 1);
154c2cdf5c5SKevin Wolf 
155c2cdf5c5SKevin Wolf     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
156c2cdf5c5SKevin Wolf         if (!*p) {
157c2cdf5c5SKevin Wolf             continue;
158c2cdf5c5SKevin Wolf         }
159c2cdf5c5SKevin Wolf         c++;
16008193dd5SMarkus Armbruster         rval = g_renew(char *, rval, (c + 1));
161c2cdf5c5SKevin Wolf         rval[c - 1] = p;
162c2cdf5c5SKevin Wolf         rval[c] = NULL;
163c2cdf5c5SKevin Wolf     }
164c2cdf5c5SKevin Wolf     *count = c;
165c2cdf5c5SKevin Wolf     return rval;
166c2cdf5c5SKevin Wolf }
167c2cdf5c5SKevin Wolf 
168797ac58cSKevin Wolf static int64_t cvtnum(const char *s)
169797ac58cSKevin Wolf {
170f17fd4fdSMarkus Armbruster     int err;
171f46bfdbfSMarkus Armbruster     uint64_t value;
172ef5a7885SJohn Snow 
173f17fd4fdSMarkus Armbruster     err = qemu_strtosz(s, NULL, &value);
174f17fd4fdSMarkus Armbruster     if (err < 0) {
175f17fd4fdSMarkus Armbruster         return err;
176f17fd4fdSMarkus Armbruster     }
177f46bfdbfSMarkus Armbruster     if (value > INT64_MAX) {
178f46bfdbfSMarkus Armbruster         return -ERANGE;
179f46bfdbfSMarkus Armbruster     }
180f17fd4fdSMarkus Armbruster     return value;
181797ac58cSKevin Wolf }
182797ac58cSKevin Wolf 
183a9ecfa00SJohn Snow static void print_cvtnum_err(int64_t rc, const char *arg)
184a9ecfa00SJohn Snow {
185a9ecfa00SJohn Snow     switch (rc) {
186a9ecfa00SJohn Snow     case -EINVAL:
187a9ecfa00SJohn Snow         printf("Parsing error: non-numeric argument,"
188a9ecfa00SJohn Snow                " or extraneous/unrecognized suffix -- %s\n", arg);
189a9ecfa00SJohn Snow         break;
190a9ecfa00SJohn Snow     case -ERANGE:
191a9ecfa00SJohn Snow         printf("Parsing error: argument too large -- %s\n", arg);
192a9ecfa00SJohn Snow         break;
193a9ecfa00SJohn Snow     default:
194a9ecfa00SJohn Snow         printf("Parsing error: %s\n", arg);
195a9ecfa00SJohn Snow     }
196a9ecfa00SJohn Snow }
197a9ecfa00SJohn Snow 
1980b613881SKevin Wolf #define EXABYTES(x)     ((long long)(x) << 60)
1990b613881SKevin Wolf #define PETABYTES(x)    ((long long)(x) << 50)
2000b613881SKevin Wolf #define TERABYTES(x)    ((long long)(x) << 40)
2010b613881SKevin Wolf #define GIGABYTES(x)    ((long long)(x) << 30)
2020b613881SKevin Wolf #define MEGABYTES(x)    ((long long)(x) << 20)
2030b613881SKevin Wolf #define KILOBYTES(x)    ((long long)(x) << 10)
2040b613881SKevin Wolf 
2050b613881SKevin Wolf #define TO_EXABYTES(x)  ((x) / EXABYTES(1))
2060b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1))
2070b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1))
2080b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
2090b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
2100b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
2110b613881SKevin Wolf 
2120b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size)
2130b613881SKevin Wolf {
2140b613881SKevin Wolf     char *trim;
2150b613881SKevin Wolf     const char *suffix;
2160b613881SKevin Wolf 
2170b613881SKevin Wolf     if (value >= EXABYTES(1)) {
2180b613881SKevin Wolf         suffix = " EiB";
2190b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
2200b613881SKevin Wolf     } else if (value >= PETABYTES(1)) {
2210b613881SKevin Wolf         suffix = " PiB";
2220b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
2230b613881SKevin Wolf     } else if (value >= TERABYTES(1)) {
2240b613881SKevin Wolf         suffix = " TiB";
2250b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
2260b613881SKevin Wolf     } else if (value >= GIGABYTES(1)) {
2270b613881SKevin Wolf         suffix = " GiB";
2280b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
2290b613881SKevin Wolf     } else if (value >= MEGABYTES(1)) {
2300b613881SKevin Wolf         suffix = " MiB";
2310b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
2320b613881SKevin Wolf     } else if (value >= KILOBYTES(1)) {
2330b613881SKevin Wolf         suffix = " KiB";
2340b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
2350b613881SKevin Wolf     } else {
2360b613881SKevin Wolf         suffix = " bytes";
2370b613881SKevin Wolf         snprintf(str, size - 6, "%f", value);
2380b613881SKevin Wolf     }
2390b613881SKevin Wolf 
2400b613881SKevin Wolf     trim = strstr(str, ".000");
2410b613881SKevin Wolf     if (trim) {
2420b613881SKevin Wolf         strcpy(trim, suffix);
2430b613881SKevin Wolf     } else {
2440b613881SKevin Wolf         strcat(str, suffix);
2450b613881SKevin Wolf     }
2460b613881SKevin Wolf }
2470b613881SKevin Wolf 
2480b613881SKevin Wolf 
2490b613881SKevin Wolf 
2500b613881SKevin Wolf static struct timeval tsub(struct timeval t1, struct timeval t2)
2510b613881SKevin Wolf {
2520b613881SKevin Wolf     t1.tv_usec -= t2.tv_usec;
2530b613881SKevin Wolf     if (t1.tv_usec < 0) {
2540b613881SKevin Wolf         t1.tv_usec += 1000000;
2550b613881SKevin Wolf         t1.tv_sec--;
2560b613881SKevin Wolf     }
2570b613881SKevin Wolf     t1.tv_sec -= t2.tv_sec;
2580b613881SKevin Wolf     return t1;
2590b613881SKevin Wolf }
2600b613881SKevin Wolf 
2610b613881SKevin Wolf static double tdiv(double value, struct timeval tv)
2620b613881SKevin Wolf {
2630b613881SKevin Wolf     return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
2640b613881SKevin Wolf }
2650b613881SKevin Wolf 
2660b613881SKevin Wolf #define HOURS(sec)      ((sec) / (60 * 60))
2670b613881SKevin Wolf #define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
2680b613881SKevin Wolf #define SECONDS(sec)    ((sec) % 60)
2690b613881SKevin Wolf 
2700b613881SKevin Wolf enum {
2710b613881SKevin Wolf     DEFAULT_TIME        = 0x0,
2720b613881SKevin Wolf     TERSE_FIXED_TIME    = 0x1,
2730b613881SKevin Wolf     VERBOSE_FIXED_TIME  = 0x2,
2740b613881SKevin Wolf };
2750b613881SKevin Wolf 
2760b613881SKevin Wolf static void timestr(struct timeval *tv, char *ts, size_t size, int format)
2770b613881SKevin Wolf {
2780b613881SKevin Wolf     double usec = (double)tv->tv_usec / 1000000.0;
2790b613881SKevin Wolf 
2800b613881SKevin Wolf     if (format & TERSE_FIXED_TIME) {
2810b613881SKevin Wolf         if (!HOURS(tv->tv_sec)) {
2820b613881SKevin Wolf             snprintf(ts, size, "%u:%02u.%02u",
2830b613881SKevin Wolf                     (unsigned int) MINUTES(tv->tv_sec),
2840b613881SKevin Wolf                     (unsigned int) SECONDS(tv->tv_sec),
2850b613881SKevin Wolf                     (unsigned int) (usec * 100));
2860b613881SKevin Wolf             return;
2870b613881SKevin Wolf         }
2880b613881SKevin Wolf         format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
2890b613881SKevin Wolf     }
2900b613881SKevin Wolf 
2910b613881SKevin Wolf     if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
2920b613881SKevin Wolf         snprintf(ts, size, "%u:%02u:%02u.%02u",
2930b613881SKevin Wolf                 (unsigned int) HOURS(tv->tv_sec),
2940b613881SKevin Wolf                 (unsigned int) MINUTES(tv->tv_sec),
2950b613881SKevin Wolf                 (unsigned int) SECONDS(tv->tv_sec),
2960b613881SKevin Wolf                 (unsigned int) (usec * 100));
2970b613881SKevin Wolf     } else {
2980b613881SKevin Wolf         snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
2990b613881SKevin Wolf     }
3000b613881SKevin Wolf }
3010b613881SKevin Wolf 
302797ac58cSKevin Wolf /*
303797ac58cSKevin Wolf  * Parse the pattern argument to various sub-commands.
304797ac58cSKevin Wolf  *
305797ac58cSKevin Wolf  * Because the pattern is used as an argument to memset it must evaluate
306797ac58cSKevin Wolf  * to an unsigned integer that fits into a single byte.
307797ac58cSKevin Wolf  */
308797ac58cSKevin Wolf static int parse_pattern(const char *arg)
309797ac58cSKevin Wolf {
310797ac58cSKevin Wolf     char *endptr = NULL;
311797ac58cSKevin Wolf     long pattern;
312797ac58cSKevin Wolf 
313797ac58cSKevin Wolf     pattern = strtol(arg, &endptr, 0);
314797ac58cSKevin Wolf     if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
315797ac58cSKevin Wolf         printf("%s is not a valid pattern byte\n", arg);
316797ac58cSKevin Wolf         return -1;
317797ac58cSKevin Wolf     }
318797ac58cSKevin Wolf 
319797ac58cSKevin Wolf     return pattern;
320797ac58cSKevin Wolf }
321797ac58cSKevin Wolf 
322797ac58cSKevin Wolf /*
323797ac58cSKevin Wolf  * Memory allocation helpers.
324797ac58cSKevin Wolf  *
325797ac58cSKevin Wolf  * Make sure memory is aligned by default, or purposefully misaligned if
326797ac58cSKevin Wolf  * that is specified on the command line.
327797ac58cSKevin Wolf  */
328797ac58cSKevin Wolf 
329797ac58cSKevin Wolf #define MISALIGN_OFFSET     16
3304c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
331797ac58cSKevin Wolf {
332797ac58cSKevin Wolf     void *buf;
333797ac58cSKevin Wolf 
334797ac58cSKevin Wolf     if (qemuio_misalign) {
335797ac58cSKevin Wolf         len += MISALIGN_OFFSET;
336797ac58cSKevin Wolf     }
3374c7b7e9bSMax Reitz     buf = blk_blockalign(blk, len);
338797ac58cSKevin Wolf     memset(buf, pattern, len);
339797ac58cSKevin Wolf     if (qemuio_misalign) {
340797ac58cSKevin Wolf         buf += MISALIGN_OFFSET;
341797ac58cSKevin Wolf     }
342797ac58cSKevin Wolf     return buf;
343797ac58cSKevin Wolf }
344797ac58cSKevin Wolf 
345797ac58cSKevin Wolf static void qemu_io_free(void *p)
346797ac58cSKevin Wolf {
347797ac58cSKevin Wolf     if (qemuio_misalign) {
348797ac58cSKevin Wolf         p -= MISALIGN_OFFSET;
349797ac58cSKevin Wolf     }
350797ac58cSKevin Wolf     qemu_vfree(p);
351797ac58cSKevin Wolf }
352797ac58cSKevin Wolf 
3539b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
354797ac58cSKevin Wolf {
3559b0beaf3SJohn Snow     uint64_t i;
3569b0beaf3SJohn Snow     int j;
357797ac58cSKevin Wolf     const uint8_t *p;
358797ac58cSKevin Wolf 
359797ac58cSKevin Wolf     for (i = 0, p = buffer; i < len; i += 16) {
360797ac58cSKevin Wolf         const uint8_t *s = p;
361797ac58cSKevin Wolf 
362797ac58cSKevin Wolf         printf("%08" PRIx64 ":  ", offset + i);
363797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, p++) {
364797ac58cSKevin Wolf             printf("%02x ", *p);
365797ac58cSKevin Wolf         }
366797ac58cSKevin Wolf         printf(" ");
367797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, s++) {
368797ac58cSKevin Wolf             if (isalnum(*s)) {
369797ac58cSKevin Wolf                 printf("%c", *s);
370797ac58cSKevin Wolf             } else {
371797ac58cSKevin Wolf                 printf(".");
372797ac58cSKevin Wolf             }
373797ac58cSKevin Wolf         }
374797ac58cSKevin Wolf         printf("\n");
375797ac58cSKevin Wolf     }
376797ac58cSKevin Wolf }
377797ac58cSKevin Wolf 
378797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset,
379dc38852aSEric Blake                          int64_t count, int64_t total, int cnt, bool Cflag)
380797ac58cSKevin Wolf {
381797ac58cSKevin Wolf     char s1[64], s2[64], ts[64];
382797ac58cSKevin Wolf 
383797ac58cSKevin Wolf     timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
384797ac58cSKevin Wolf     if (!Cflag) {
385797ac58cSKevin Wolf         cvtstr((double)total, s1, sizeof(s1));
386797ac58cSKevin Wolf         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
3879b0beaf3SJohn Snow         printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
388797ac58cSKevin Wolf                op, total, count, offset);
389797ac58cSKevin Wolf         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
390797ac58cSKevin Wolf                s1, cnt, ts, s2, tdiv((double)cnt, *t));
391797ac58cSKevin Wolf     } else {/* bytes,ops,time,bytes/sec,ops/sec */
3929b0beaf3SJohn Snow         printf("%"PRId64",%d,%s,%.3f,%.3f\n",
393797ac58cSKevin Wolf             total, cnt, ts,
394797ac58cSKevin Wolf             tdiv((double)total, *t),
395797ac58cSKevin Wolf             tdiv((double)cnt, *t));
396797ac58cSKevin Wolf     }
397797ac58cSKevin Wolf }
398797ac58cSKevin Wolf 
399797ac58cSKevin Wolf /*
400797ac58cSKevin Wolf  * Parse multiple length statements for vectored I/O, and construct an I/O
401797ac58cSKevin Wolf  * vector matching it.
402797ac58cSKevin Wolf  */
403797ac58cSKevin Wolf static void *
4044c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
405797ac58cSKevin Wolf              int pattern)
406797ac58cSKevin Wolf {
407797ac58cSKevin Wolf     size_t *sizes = g_new0(size_t, nr_iov);
408797ac58cSKevin Wolf     size_t count = 0;
409797ac58cSKevin Wolf     void *buf = NULL;
410797ac58cSKevin Wolf     void *p;
411797ac58cSKevin Wolf     int i;
412797ac58cSKevin Wolf 
413797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
414797ac58cSKevin Wolf         char *arg = argv[i];
415797ac58cSKevin Wolf         int64_t len;
416797ac58cSKevin Wolf 
417797ac58cSKevin Wolf         len = cvtnum(arg);
418797ac58cSKevin Wolf         if (len < 0) {
419a9ecfa00SJohn Snow             print_cvtnum_err(len, arg);
420797ac58cSKevin Wolf             goto fail;
421797ac58cSKevin Wolf         }
422797ac58cSKevin Wolf 
4233026c468SAlberto Garcia         if (len > BDRV_REQUEST_MAX_BYTES) {
4243026c468SAlberto Garcia             printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
4253026c468SAlberto Garcia                    (uint64_t)BDRV_REQUEST_MAX_BYTES);
4263026c468SAlberto Garcia             goto fail;
4273026c468SAlberto Garcia         }
4283026c468SAlberto Garcia 
4293026c468SAlberto Garcia         if (count > BDRV_REQUEST_MAX_BYTES - len) {
4303026c468SAlberto Garcia             printf("The total number of bytes exceed the maximum size %" PRIu64
4313026c468SAlberto Garcia                    "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
432797ac58cSKevin Wolf             goto fail;
433797ac58cSKevin Wolf         }
434797ac58cSKevin Wolf 
435797ac58cSKevin Wolf         sizes[i] = len;
436797ac58cSKevin Wolf         count += len;
437797ac58cSKevin Wolf     }
438797ac58cSKevin Wolf 
439797ac58cSKevin Wolf     qemu_iovec_init(qiov, nr_iov);
440797ac58cSKevin Wolf 
4414c7b7e9bSMax Reitz     buf = p = qemu_io_alloc(blk, count, pattern);
442797ac58cSKevin Wolf 
443797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
444797ac58cSKevin Wolf         qemu_iovec_add(qiov, p, sizes[i]);
445797ac58cSKevin Wolf         p += sizes[i];
446797ac58cSKevin Wolf     }
447797ac58cSKevin Wolf 
448797ac58cSKevin Wolf fail:
449797ac58cSKevin Wolf     g_free(sizes);
450797ac58cSKevin Wolf     return buf;
451797ac58cSKevin Wolf }
452797ac58cSKevin Wolf 
4539b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
454f5a5ca79SManos Pitsidianakis                     int64_t bytes, int64_t *total)
455797ac58cSKevin Wolf {
456f5a5ca79SManos Pitsidianakis     if (bytes > INT_MAX) {
4579b0beaf3SJohn Snow         return -ERANGE;
4589b0beaf3SJohn Snow     }
4599b0beaf3SJohn Snow 
460f5a5ca79SManos Pitsidianakis     *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
461797ac58cSKevin Wolf     if (*total < 0) {
462797ac58cSKevin Wolf         return *total;
463797ac58cSKevin Wolf     }
464797ac58cSKevin Wolf     return 1;
465797ac58cSKevin Wolf }
466797ac58cSKevin Wolf 
4679b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
468f5a5ca79SManos Pitsidianakis                      int64_t bytes, int flags, int64_t *total)
469797ac58cSKevin Wolf {
470f5a5ca79SManos Pitsidianakis     if (bytes > INT_MAX) {
4719b0beaf3SJohn Snow         return -ERANGE;
4729b0beaf3SJohn Snow     }
4739b0beaf3SJohn Snow 
474f5a5ca79SManos Pitsidianakis     *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
475797ac58cSKevin Wolf     if (*total < 0) {
476797ac58cSKevin Wolf         return *total;
477797ac58cSKevin Wolf     }
478797ac58cSKevin Wolf     return 1;
479797ac58cSKevin Wolf }
480797ac58cSKevin Wolf 
481797ac58cSKevin Wolf typedef struct {
4824c7b7e9bSMax Reitz     BlockBackend *blk;
483797ac58cSKevin Wolf     int64_t offset;
484f5a5ca79SManos Pitsidianakis     int64_t bytes;
4859b0beaf3SJohn Snow     int64_t *total;
486770e0e0eSEric Blake     int flags;
487797ac58cSKevin Wolf     int ret;
488797ac58cSKevin Wolf     bool done;
489797ac58cSKevin Wolf } CoWriteZeroes;
490797ac58cSKevin Wolf 
491d004bd52SEric Blake static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
492797ac58cSKevin Wolf {
493797ac58cSKevin Wolf     CoWriteZeroes *data = opaque;
494797ac58cSKevin Wolf 
495f5a5ca79SManos Pitsidianakis     data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
496770e0e0eSEric Blake                                      data->flags);
497797ac58cSKevin Wolf     data->done = true;
498797ac58cSKevin Wolf     if (data->ret < 0) {
499797ac58cSKevin Wolf         *data->total = data->ret;
500797ac58cSKevin Wolf         return;
501797ac58cSKevin Wolf     }
502797ac58cSKevin Wolf 
503f5a5ca79SManos Pitsidianakis     *data->total = data->bytes;
504797ac58cSKevin Wolf }
505797ac58cSKevin Wolf 
506d004bd52SEric Blake static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
507f5a5ca79SManos Pitsidianakis                                int64_t bytes, int flags, int64_t *total)
508797ac58cSKevin Wolf {
509797ac58cSKevin Wolf     Coroutine *co;
510797ac58cSKevin Wolf     CoWriteZeroes data = {
5114c7b7e9bSMax Reitz         .blk    = blk,
512797ac58cSKevin Wolf         .offset = offset,
513f5a5ca79SManos Pitsidianakis         .bytes  = bytes,
514797ac58cSKevin Wolf         .total  = total,
515770e0e0eSEric Blake         .flags  = flags,
516797ac58cSKevin Wolf         .done   = false,
517797ac58cSKevin Wolf     };
518797ac58cSKevin Wolf 
519f5a5ca79SManos Pitsidianakis     if (bytes > INT_MAX) {
5209b0beaf3SJohn Snow         return -ERANGE;
5219b0beaf3SJohn Snow     }
5229b0beaf3SJohn Snow 
5230b8b8753SPaolo Bonzini     co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
524324ec3e4SFam Zheng     bdrv_coroutine_enter(blk_bs(blk), co);
525797ac58cSKevin Wolf     while (!data.done) {
5264c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
527797ac58cSKevin Wolf     }
528797ac58cSKevin Wolf     if (data.ret < 0) {
529797ac58cSKevin Wolf         return data.ret;
530797ac58cSKevin Wolf     } else {
531797ac58cSKevin Wolf         return 1;
532797ac58cSKevin Wolf     }
533797ac58cSKevin Wolf }
534797ac58cSKevin Wolf 
5354c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
536f5a5ca79SManos Pitsidianakis                                int64_t bytes, int64_t *total)
537797ac58cSKevin Wolf {
538797ac58cSKevin Wolf     int ret;
539797ac58cSKevin Wolf 
540f5a5ca79SManos Pitsidianakis     if (bytes >> 9 > BDRV_REQUEST_MAX_SECTORS) {
5419b0beaf3SJohn Snow         return -ERANGE;
5429b0beaf3SJohn Snow     }
5439b0beaf3SJohn Snow 
544f5a5ca79SManos Pitsidianakis     ret = blk_pwrite_compressed(blk, offset, buf, bytes);
545797ac58cSKevin Wolf     if (ret < 0) {
546797ac58cSKevin Wolf         return ret;
547797ac58cSKevin Wolf     }
548f5a5ca79SManos Pitsidianakis     *total = bytes;
549797ac58cSKevin Wolf     return 1;
550797ac58cSKevin Wolf }
551797ac58cSKevin Wolf 
5524c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5539b0beaf3SJohn Snow                            int64_t count, int64_t *total)
554797ac58cSKevin Wolf {
5559b0beaf3SJohn Snow     if (count > INT_MAX) {
5569b0beaf3SJohn Snow         return -ERANGE;
5579b0beaf3SJohn Snow     }
5589b0beaf3SJohn Snow 
5594c7b7e9bSMax Reitz     *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
560797ac58cSKevin Wolf     if (*total < 0) {
561797ac58cSKevin Wolf         return *total;
562797ac58cSKevin Wolf     }
563797ac58cSKevin Wolf     return 1;
564797ac58cSKevin Wolf }
565797ac58cSKevin Wolf 
5664c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5679b0beaf3SJohn Snow                            int64_t count, int64_t *total)
568797ac58cSKevin Wolf {
5699b0beaf3SJohn Snow     if (count > INT_MAX) {
5709b0beaf3SJohn Snow         return -ERANGE;
5719b0beaf3SJohn Snow     }
5729b0beaf3SJohn Snow 
5734c7b7e9bSMax Reitz     *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
574797ac58cSKevin Wolf     if (*total < 0) {
575797ac58cSKevin Wolf         return *total;
576797ac58cSKevin Wolf     }
577797ac58cSKevin Wolf     return 1;
578797ac58cSKevin Wolf }
579797ac58cSKevin Wolf 
580797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff
581797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret)
582797ac58cSKevin Wolf {
583797ac58cSKevin Wolf     *(int *)opaque = ret;
584797ac58cSKevin Wolf }
585797ac58cSKevin Wolf 
5864c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
587797ac58cSKevin Wolf                         int64_t offset, int *total)
588797ac58cSKevin Wolf {
589797ac58cSKevin Wolf     int async_ret = NOT_DONE;
590797ac58cSKevin Wolf 
5917b3f9712SEric Blake     blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
592797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
593797ac58cSKevin Wolf         main_loop_wait(false);
594797ac58cSKevin Wolf     }
595797ac58cSKevin Wolf 
596797ac58cSKevin Wolf     *total = qiov->size;
597797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
598797ac58cSKevin Wolf }
599797ac58cSKevin Wolf 
6004c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
601770e0e0eSEric Blake                          int64_t offset, int flags, int *total)
602797ac58cSKevin Wolf {
603797ac58cSKevin Wolf     int async_ret = NOT_DONE;
604797ac58cSKevin Wolf 
605770e0e0eSEric Blake     blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
606797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
607797ac58cSKevin Wolf         main_loop_wait(false);
608797ac58cSKevin Wolf     }
609797ac58cSKevin Wolf 
610797ac58cSKevin Wolf     *total = qiov->size;
611797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
612797ac58cSKevin Wolf }
613797ac58cSKevin Wolf 
614797ac58cSKevin Wolf static void read_help(void)
615797ac58cSKevin Wolf {
616797ac58cSKevin Wolf     printf(
617797ac58cSKevin Wolf "\n"
618797ac58cSKevin Wolf " reads a range of bytes from the given offset\n"
619797ac58cSKevin Wolf "\n"
620797ac58cSKevin Wolf " Example:\n"
621797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
622797ac58cSKevin Wolf "\n"
623797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
624797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
625797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n"
626797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
627797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n"
628093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
629797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
630797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
631797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n"
632797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
633797ac58cSKevin Wolf "\n");
634797ac58cSKevin Wolf }
635797ac58cSKevin Wolf 
636*b444d0e9SMax Reitz static void read_f(BlockBackend *blk, int argc, char **argv);
637797ac58cSKevin Wolf 
638797ac58cSKevin Wolf static const cmdinfo_t read_cmd = {
639797ac58cSKevin Wolf     .name       = "read",
640797ac58cSKevin Wolf     .altname    = "r",
641797ac58cSKevin Wolf     .cfunc      = read_f,
642797ac58cSKevin Wolf     .argmin     = 2,
643797ac58cSKevin Wolf     .argmax     = -1,
644093ea232SEric Blake     .args       = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
645797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
646797ac58cSKevin Wolf     .help       = read_help,
647797ac58cSKevin Wolf };
648797ac58cSKevin Wolf 
649*b444d0e9SMax Reitz static void read_f(BlockBackend *blk, int argc, char **argv)
650797ac58cSKevin Wolf {
651797ac58cSKevin Wolf     struct timeval t1, t2;
652093ea232SEric Blake     bool Cflag = false, qflag = false, vflag = false;
653dc38852aSEric Blake     bool Pflag = false, sflag = false, lflag = false, bflag = false;
654797ac58cSKevin Wolf     int c, cnt;
655797ac58cSKevin Wolf     char *buf;
656797ac58cSKevin Wolf     int64_t offset;
6579b0beaf3SJohn Snow     int64_t count;
658797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
6599b0beaf3SJohn Snow     int64_t total = 0;
6609b0beaf3SJohn Snow     int pattern = 0;
6619b0beaf3SJohn Snow     int64_t pattern_offset = 0, pattern_count = 0;
662797ac58cSKevin Wolf 
663b062ad86SEric Blake     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
664797ac58cSKevin Wolf         switch (c) {
665797ac58cSKevin Wolf         case 'b':
666dc38852aSEric Blake             bflag = true;
667797ac58cSKevin Wolf             break;
668797ac58cSKevin Wolf         case 'C':
669dc38852aSEric Blake             Cflag = true;
670797ac58cSKevin Wolf             break;
671797ac58cSKevin Wolf         case 'l':
672dc38852aSEric Blake             lflag = true;
673797ac58cSKevin Wolf             pattern_count = cvtnum(optarg);
674797ac58cSKevin Wolf             if (pattern_count < 0) {
675a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_count, optarg);
676*b444d0e9SMax Reitz                 return;
677797ac58cSKevin Wolf             }
678797ac58cSKevin Wolf             break;
679797ac58cSKevin Wolf         case 'p':
680093ea232SEric Blake             /* Ignored for backwards compatibility */
681797ac58cSKevin Wolf             break;
682797ac58cSKevin Wolf         case 'P':
683dc38852aSEric Blake             Pflag = true;
684797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
685797ac58cSKevin Wolf             if (pattern < 0) {
686*b444d0e9SMax Reitz                 return;
687797ac58cSKevin Wolf             }
688797ac58cSKevin Wolf             break;
689797ac58cSKevin Wolf         case 'q':
690dc38852aSEric Blake             qflag = true;
691797ac58cSKevin Wolf             break;
692797ac58cSKevin Wolf         case 's':
693dc38852aSEric Blake             sflag = true;
694797ac58cSKevin Wolf             pattern_offset = cvtnum(optarg);
695797ac58cSKevin Wolf             if (pattern_offset < 0) {
696a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_offset, optarg);
697*b444d0e9SMax Reitz                 return;
698797ac58cSKevin Wolf             }
699797ac58cSKevin Wolf             break;
700797ac58cSKevin Wolf         case 'v':
701dc38852aSEric Blake             vflag = true;
702797ac58cSKevin Wolf             break;
703797ac58cSKevin Wolf         default:
704*b444d0e9SMax Reitz             qemuio_command_usage(&read_cmd);
705*b444d0e9SMax Reitz             return;
706797ac58cSKevin Wolf         }
707797ac58cSKevin Wolf     }
708797ac58cSKevin Wolf 
709797ac58cSKevin Wolf     if (optind != argc - 2) {
710*b444d0e9SMax Reitz         qemuio_command_usage(&read_cmd);
711*b444d0e9SMax Reitz         return;
712797ac58cSKevin Wolf     }
713797ac58cSKevin Wolf 
714797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
715797ac58cSKevin Wolf     if (offset < 0) {
716a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
717*b444d0e9SMax Reitz         return;
718797ac58cSKevin Wolf     }
719797ac58cSKevin Wolf 
720797ac58cSKevin Wolf     optind++;
721797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
722797ac58cSKevin Wolf     if (count < 0) {
723a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
724*b444d0e9SMax Reitz         return;
7253026c468SAlberto Garcia     } else if (count > BDRV_REQUEST_MAX_BYTES) {
7269b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
7273026c468SAlberto Garcia                (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
728*b444d0e9SMax Reitz         return;
729797ac58cSKevin Wolf     }
730797ac58cSKevin Wolf 
731797ac58cSKevin Wolf     if (!Pflag && (lflag || sflag)) {
732*b444d0e9SMax Reitz         qemuio_command_usage(&read_cmd);
733*b444d0e9SMax Reitz         return;
734797ac58cSKevin Wolf     }
735797ac58cSKevin Wolf 
736797ac58cSKevin Wolf     if (!lflag) {
737797ac58cSKevin Wolf         pattern_count = count - pattern_offset;
738797ac58cSKevin Wolf     }
739797ac58cSKevin Wolf 
740797ac58cSKevin Wolf     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
741797ac58cSKevin Wolf         printf("pattern verification range exceeds end of read data\n");
742*b444d0e9SMax Reitz         return;
743797ac58cSKevin Wolf     }
744797ac58cSKevin Wolf 
745093ea232SEric Blake     if (bflag) {
7461bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
7471bce6b4cSEric Blake             printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
748797ac58cSKevin Wolf                    offset);
749*b444d0e9SMax Reitz             return;
750797ac58cSKevin Wolf         }
7511bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
7521bce6b4cSEric Blake             printf("%"PRId64" is not a sector-aligned value for 'count'\n",
753797ac58cSKevin Wolf                    count);
754*b444d0e9SMax Reitz             return;
755797ac58cSKevin Wolf         }
756797ac58cSKevin Wolf     }
757797ac58cSKevin Wolf 
7584c7b7e9bSMax Reitz     buf = qemu_io_alloc(blk, count, 0xab);
759797ac58cSKevin Wolf 
760797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
7617b3f9712SEric Blake     if (bflag) {
7624c7b7e9bSMax Reitz         cnt = do_load_vmstate(blk, buf, offset, count, &total);
763797ac58cSKevin Wolf     } else {
7647b3f9712SEric Blake         cnt = do_pread(blk, buf, offset, count, &total);
765797ac58cSKevin Wolf     }
766797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
767797ac58cSKevin Wolf 
768797ac58cSKevin Wolf     if (cnt < 0) {
769797ac58cSKevin Wolf         printf("read failed: %s\n", strerror(-cnt));
770797ac58cSKevin Wolf         goto out;
771797ac58cSKevin Wolf     }
772797ac58cSKevin Wolf 
773797ac58cSKevin Wolf     if (Pflag) {
774797ac58cSKevin Wolf         void *cmp_buf = g_malloc(pattern_count);
775797ac58cSKevin Wolf         memset(cmp_buf, pattern, pattern_count);
776797ac58cSKevin Wolf         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
777797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
7789b0beaf3SJohn Snow                    PRId64 ", %"PRId64" bytes\n",
779797ac58cSKevin Wolf                    offset + pattern_offset, pattern_count);
780797ac58cSKevin Wolf         }
781797ac58cSKevin Wolf         g_free(cmp_buf);
782797ac58cSKevin Wolf     }
783797ac58cSKevin Wolf 
784797ac58cSKevin Wolf     if (qflag) {
785797ac58cSKevin Wolf         goto out;
786797ac58cSKevin Wolf     }
787797ac58cSKevin Wolf 
788797ac58cSKevin Wolf     if (vflag) {
789797ac58cSKevin Wolf         dump_buffer(buf, offset, count);
790797ac58cSKevin Wolf     }
791797ac58cSKevin Wolf 
792797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
793797ac58cSKevin Wolf     t2 = tsub(t2, t1);
794797ac58cSKevin Wolf     print_report("read", &t2, offset, count, total, cnt, Cflag);
795797ac58cSKevin Wolf 
796797ac58cSKevin Wolf out:
797797ac58cSKevin Wolf     qemu_io_free(buf);
798797ac58cSKevin Wolf }
799797ac58cSKevin Wolf 
800797ac58cSKevin Wolf static void readv_help(void)
801797ac58cSKevin Wolf {
802797ac58cSKevin Wolf     printf(
803797ac58cSKevin Wolf "\n"
804797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n"
805797ac58cSKevin Wolf "\n"
806797ac58cSKevin Wolf " Example:\n"
807797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
808797ac58cSKevin Wolf "\n"
809797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
810797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
811797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n"
812797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
813797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
814797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
815797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
816797ac58cSKevin Wolf "\n");
817797ac58cSKevin Wolf }
818797ac58cSKevin Wolf 
819*b444d0e9SMax Reitz static void readv_f(BlockBackend *blk, int argc, char **argv);
820797ac58cSKevin Wolf 
821797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = {
822797ac58cSKevin Wolf     .name       = "readv",
823797ac58cSKevin Wolf     .cfunc      = readv_f,
824797ac58cSKevin Wolf     .argmin     = 2,
825797ac58cSKevin Wolf     .argmax     = -1,
826797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern] off len [len..]",
827797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
828797ac58cSKevin Wolf     .help       = readv_help,
829797ac58cSKevin Wolf };
830797ac58cSKevin Wolf 
831*b444d0e9SMax Reitz static void readv_f(BlockBackend *blk, int argc, char **argv)
832797ac58cSKevin Wolf {
833797ac58cSKevin Wolf     struct timeval t1, t2;
834dc38852aSEric Blake     bool Cflag = false, qflag = false, vflag = false;
835797ac58cSKevin Wolf     int c, cnt;
836797ac58cSKevin Wolf     char *buf;
837797ac58cSKevin Wolf     int64_t offset;
838797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
839797ac58cSKevin Wolf     int total = 0;
840797ac58cSKevin Wolf     int nr_iov;
841797ac58cSKevin Wolf     QEMUIOVector qiov;
842797ac58cSKevin Wolf     int pattern = 0;
843dc38852aSEric Blake     bool Pflag = false;
844797ac58cSKevin Wolf 
845b062ad86SEric Blake     while ((c = getopt(argc, argv, "CP:qv")) != -1) {
846797ac58cSKevin Wolf         switch (c) {
847797ac58cSKevin Wolf         case 'C':
848dc38852aSEric Blake             Cflag = true;
849797ac58cSKevin Wolf             break;
850797ac58cSKevin Wolf         case 'P':
851dc38852aSEric Blake             Pflag = true;
852797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
853797ac58cSKevin Wolf             if (pattern < 0) {
854*b444d0e9SMax Reitz                 return;
855797ac58cSKevin Wolf             }
856797ac58cSKevin Wolf             break;
857797ac58cSKevin Wolf         case 'q':
858dc38852aSEric Blake             qflag = true;
859797ac58cSKevin Wolf             break;
860797ac58cSKevin Wolf         case 'v':
861dc38852aSEric Blake             vflag = true;
862797ac58cSKevin Wolf             break;
863797ac58cSKevin Wolf         default:
864*b444d0e9SMax Reitz             qemuio_command_usage(&readv_cmd);
865*b444d0e9SMax Reitz             return;
866797ac58cSKevin Wolf         }
867797ac58cSKevin Wolf     }
868797ac58cSKevin Wolf 
869797ac58cSKevin Wolf     if (optind > argc - 2) {
870*b444d0e9SMax Reitz         qemuio_command_usage(&readv_cmd);
871*b444d0e9SMax Reitz         return;
872797ac58cSKevin Wolf     }
873797ac58cSKevin Wolf 
874797ac58cSKevin Wolf 
875797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
876797ac58cSKevin Wolf     if (offset < 0) {
877a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
878*b444d0e9SMax Reitz         return;
879797ac58cSKevin Wolf     }
880797ac58cSKevin Wolf     optind++;
881797ac58cSKevin Wolf 
882797ac58cSKevin Wolf     nr_iov = argc - optind;
8834c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
884797ac58cSKevin Wolf     if (buf == NULL) {
885*b444d0e9SMax Reitz         return;
886797ac58cSKevin Wolf     }
887797ac58cSKevin Wolf 
888797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
8894c7b7e9bSMax Reitz     cnt = do_aio_readv(blk, &qiov, offset, &total);
890797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
891797ac58cSKevin Wolf 
892797ac58cSKevin Wolf     if (cnt < 0) {
893797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-cnt));
894797ac58cSKevin Wolf         goto out;
895797ac58cSKevin Wolf     }
896797ac58cSKevin Wolf 
897797ac58cSKevin Wolf     if (Pflag) {
898797ac58cSKevin Wolf         void *cmp_buf = g_malloc(qiov.size);
899797ac58cSKevin Wolf         memset(cmp_buf, pattern, qiov.size);
900797ac58cSKevin Wolf         if (memcmp(buf, cmp_buf, qiov.size)) {
901797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
902797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", offset, qiov.size);
903797ac58cSKevin Wolf         }
904797ac58cSKevin Wolf         g_free(cmp_buf);
905797ac58cSKevin Wolf     }
906797ac58cSKevin Wolf 
907797ac58cSKevin Wolf     if (qflag) {
908797ac58cSKevin Wolf         goto out;
909797ac58cSKevin Wolf     }
910797ac58cSKevin Wolf 
911797ac58cSKevin Wolf     if (vflag) {
912797ac58cSKevin Wolf         dump_buffer(buf, offset, qiov.size);
913797ac58cSKevin Wolf     }
914797ac58cSKevin Wolf 
915797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
916797ac58cSKevin Wolf     t2 = tsub(t2, t1);
917797ac58cSKevin Wolf     print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
918797ac58cSKevin Wolf 
919797ac58cSKevin Wolf out:
920797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
921797ac58cSKevin Wolf     qemu_io_free(buf);
922797ac58cSKevin Wolf }
923797ac58cSKevin Wolf 
924797ac58cSKevin Wolf static void write_help(void)
925797ac58cSKevin Wolf {
926797ac58cSKevin Wolf     printf(
927797ac58cSKevin Wolf "\n"
928797ac58cSKevin Wolf " writes a range of bytes from the given offset\n"
929797ac58cSKevin Wolf "\n"
930797ac58cSKevin Wolf " Example:\n"
931797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
932797ac58cSKevin Wolf "\n"
933797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
934797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
935797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n"
9364c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n"
937770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
938093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
939797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
940797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
941797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
942c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
943d004bd52SEric Blake " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
944797ac58cSKevin Wolf "\n");
945797ac58cSKevin Wolf }
946797ac58cSKevin Wolf 
947*b444d0e9SMax Reitz static void write_f(BlockBackend *blk, int argc, char **argv);
948797ac58cSKevin Wolf 
949797ac58cSKevin Wolf static const cmdinfo_t write_cmd = {
950797ac58cSKevin Wolf     .name       = "write",
951797ac58cSKevin Wolf     .altname    = "w",
952797ac58cSKevin Wolf     .cfunc      = write_f,
953887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
954797ac58cSKevin Wolf     .argmin     = 2,
955797ac58cSKevin Wolf     .argmax     = -1,
956c2e001ccSEric Blake     .args       = "[-bcCfquz] [-P pattern] off len",
957797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
958797ac58cSKevin Wolf     .help       = write_help,
959797ac58cSKevin Wolf };
960797ac58cSKevin Wolf 
961*b444d0e9SMax Reitz static void write_f(BlockBackend *blk, int argc, char **argv)
962797ac58cSKevin Wolf {
963797ac58cSKevin Wolf     struct timeval t1, t2;
964093ea232SEric Blake     bool Cflag = false, qflag = false, bflag = false;
965dc38852aSEric Blake     bool Pflag = false, zflag = false, cflag = false;
966770e0e0eSEric Blake     int flags = 0;
967797ac58cSKevin Wolf     int c, cnt;
968797ac58cSKevin Wolf     char *buf = NULL;
969797ac58cSKevin Wolf     int64_t offset;
9709b0beaf3SJohn Snow     int64_t count;
971797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
9729b0beaf3SJohn Snow     int64_t total = 0;
973797ac58cSKevin Wolf     int pattern = 0xcd;
974797ac58cSKevin Wolf 
975c2e001ccSEric Blake     while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
976797ac58cSKevin Wolf         switch (c) {
977797ac58cSKevin Wolf         case 'b':
978dc38852aSEric Blake             bflag = true;
979797ac58cSKevin Wolf             break;
980797ac58cSKevin Wolf         case 'c':
981dc38852aSEric Blake             cflag = true;
982797ac58cSKevin Wolf             break;
983797ac58cSKevin Wolf         case 'C':
984dc38852aSEric Blake             Cflag = true;
985797ac58cSKevin Wolf             break;
986770e0e0eSEric Blake         case 'f':
987770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
988770e0e0eSEric Blake             break;
989797ac58cSKevin Wolf         case 'p':
990093ea232SEric Blake             /* Ignored for backwards compatibility */
991797ac58cSKevin Wolf             break;
992797ac58cSKevin Wolf         case 'P':
993dc38852aSEric Blake             Pflag = true;
994797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
995797ac58cSKevin Wolf             if (pattern < 0) {
996*b444d0e9SMax Reitz                 return;
997797ac58cSKevin Wolf             }
998797ac58cSKevin Wolf             break;
999797ac58cSKevin Wolf         case 'q':
1000dc38852aSEric Blake             qflag = true;
1001797ac58cSKevin Wolf             break;
1002c2e001ccSEric Blake         case 'u':
1003c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1004c2e001ccSEric Blake             break;
1005797ac58cSKevin Wolf         case 'z':
1006dc38852aSEric Blake             zflag = true;
1007797ac58cSKevin Wolf             break;
1008797ac58cSKevin Wolf         default:
1009*b444d0e9SMax Reitz             qemuio_command_usage(&write_cmd);
1010*b444d0e9SMax Reitz             return;
1011797ac58cSKevin Wolf         }
1012797ac58cSKevin Wolf     }
1013797ac58cSKevin Wolf 
1014797ac58cSKevin Wolf     if (optind != argc - 2) {
1015*b444d0e9SMax Reitz         qemuio_command_usage(&write_cmd);
1016*b444d0e9SMax Reitz         return;
1017797ac58cSKevin Wolf     }
1018797ac58cSKevin Wolf 
1019093ea232SEric Blake     if (bflag && zflag) {
1020093ea232SEric Blake         printf("-b and -z cannot be specified at the same time\n");
1021*b444d0e9SMax Reitz         return;
1022797ac58cSKevin Wolf     }
1023797ac58cSKevin Wolf 
1024770e0e0eSEric Blake     if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1025770e0e0eSEric Blake         printf("-f and -b or -c cannot be specified at the same time\n");
1026*b444d0e9SMax Reitz         return;
1027770e0e0eSEric Blake     }
1028770e0e0eSEric Blake 
1029c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1030c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
1031*b444d0e9SMax Reitz         return;
1032c2e001ccSEric Blake     }
1033c2e001ccSEric Blake 
1034797ac58cSKevin Wolf     if (zflag && Pflag) {
1035797ac58cSKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
1036*b444d0e9SMax Reitz         return;
1037797ac58cSKevin Wolf     }
1038797ac58cSKevin Wolf 
1039797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1040797ac58cSKevin Wolf     if (offset < 0) {
1041a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1042*b444d0e9SMax Reitz         return;
1043797ac58cSKevin Wolf     }
1044797ac58cSKevin Wolf 
1045797ac58cSKevin Wolf     optind++;
1046797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1047797ac58cSKevin Wolf     if (count < 0) {
1048a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
1049*b444d0e9SMax Reitz         return;
10503026c468SAlberto Garcia     } else if (count > BDRV_REQUEST_MAX_BYTES) {
10519b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
10523026c468SAlberto Garcia                (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1053*b444d0e9SMax Reitz         return;
1054797ac58cSKevin Wolf     }
1055797ac58cSKevin Wolf 
1056093ea232SEric Blake     if (bflag || cflag) {
10571bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
10581bce6b4cSEric Blake             printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
1059797ac58cSKevin Wolf                    offset);
1060*b444d0e9SMax Reitz             return;
1061797ac58cSKevin Wolf         }
1062797ac58cSKevin Wolf 
10631bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
10641bce6b4cSEric Blake             printf("%"PRId64" is not a sector-aligned value for 'count'\n",
1065797ac58cSKevin Wolf                    count);
1066*b444d0e9SMax Reitz             return;
1067797ac58cSKevin Wolf         }
1068797ac58cSKevin Wolf     }
1069797ac58cSKevin Wolf 
1070797ac58cSKevin Wolf     if (!zflag) {
10714c7b7e9bSMax Reitz         buf = qemu_io_alloc(blk, count, pattern);
1072797ac58cSKevin Wolf     }
1073797ac58cSKevin Wolf 
1074797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
10757b3f9712SEric Blake     if (bflag) {
10764c7b7e9bSMax Reitz         cnt = do_save_vmstate(blk, buf, offset, count, &total);
1077797ac58cSKevin Wolf     } else if (zflag) {
1078d004bd52SEric Blake         cnt = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
1079797ac58cSKevin Wolf     } else if (cflag) {
10804c7b7e9bSMax Reitz         cnt = do_write_compressed(blk, buf, offset, count, &total);
1081797ac58cSKevin Wolf     } else {
1082770e0e0eSEric Blake         cnt = do_pwrite(blk, buf, offset, count, flags, &total);
1083797ac58cSKevin Wolf     }
1084797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1085797ac58cSKevin Wolf 
1086797ac58cSKevin Wolf     if (cnt < 0) {
1087797ac58cSKevin Wolf         printf("write failed: %s\n", strerror(-cnt));
1088797ac58cSKevin Wolf         goto out;
1089797ac58cSKevin Wolf     }
1090797ac58cSKevin Wolf 
1091797ac58cSKevin Wolf     if (qflag) {
1092797ac58cSKevin Wolf         goto out;
1093797ac58cSKevin Wolf     }
1094797ac58cSKevin Wolf 
1095797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1096797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1097797ac58cSKevin Wolf     print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1098797ac58cSKevin Wolf 
1099797ac58cSKevin Wolf out:
1100797ac58cSKevin Wolf     if (!zflag) {
1101797ac58cSKevin Wolf         qemu_io_free(buf);
1102797ac58cSKevin Wolf     }
1103797ac58cSKevin Wolf }
1104797ac58cSKevin Wolf 
1105797ac58cSKevin Wolf static void
1106797ac58cSKevin Wolf writev_help(void)
1107797ac58cSKevin Wolf {
1108797ac58cSKevin Wolf     printf(
1109797ac58cSKevin Wolf "\n"
1110797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n"
1111797ac58cSKevin Wolf "\n"
1112797ac58cSKevin Wolf " Example:\n"
11136e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1114797ac58cSKevin Wolf "\n"
1115797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1116797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1117797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1118797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1119770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
1120797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1121797ac58cSKevin Wolf "\n");
1122797ac58cSKevin Wolf }
1123797ac58cSKevin Wolf 
1124*b444d0e9SMax Reitz static void writev_f(BlockBackend *blk, int argc, char **argv);
1125797ac58cSKevin Wolf 
1126797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = {
1127797ac58cSKevin Wolf     .name       = "writev",
1128797ac58cSKevin Wolf     .cfunc      = writev_f,
1129887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
1130797ac58cSKevin Wolf     .argmin     = 2,
1131797ac58cSKevin Wolf     .argmax     = -1,
1132770e0e0eSEric Blake     .args       = "[-Cfq] [-P pattern] off len [len..]",
1133797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
1134797ac58cSKevin Wolf     .help       = writev_help,
1135797ac58cSKevin Wolf };
1136797ac58cSKevin Wolf 
1137*b444d0e9SMax Reitz static void writev_f(BlockBackend *blk, int argc, char **argv)
1138797ac58cSKevin Wolf {
1139797ac58cSKevin Wolf     struct timeval t1, t2;
1140dc38852aSEric Blake     bool Cflag = false, qflag = false;
1141770e0e0eSEric Blake     int flags = 0;
1142797ac58cSKevin Wolf     int c, cnt;
1143797ac58cSKevin Wolf     char *buf;
1144797ac58cSKevin Wolf     int64_t offset;
1145797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1146797ac58cSKevin Wolf     int total = 0;
1147797ac58cSKevin Wolf     int nr_iov;
1148797ac58cSKevin Wolf     int pattern = 0xcd;
1149797ac58cSKevin Wolf     QEMUIOVector qiov;
1150797ac58cSKevin Wolf 
11514ca1d340SEric Blake     while ((c = getopt(argc, argv, "CfqP:")) != -1) {
1152797ac58cSKevin Wolf         switch (c) {
1153797ac58cSKevin Wolf         case 'C':
1154dc38852aSEric Blake             Cflag = true;
1155797ac58cSKevin Wolf             break;
1156770e0e0eSEric Blake         case 'f':
1157770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1158770e0e0eSEric Blake             break;
1159797ac58cSKevin Wolf         case 'q':
1160dc38852aSEric Blake             qflag = true;
1161797ac58cSKevin Wolf             break;
1162797ac58cSKevin Wolf         case 'P':
1163797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1164797ac58cSKevin Wolf             if (pattern < 0) {
1165*b444d0e9SMax Reitz                 return;
1166797ac58cSKevin Wolf             }
1167797ac58cSKevin Wolf             break;
1168797ac58cSKevin Wolf         default:
1169*b444d0e9SMax Reitz             qemuio_command_usage(&writev_cmd);
1170*b444d0e9SMax Reitz             return;
1171797ac58cSKevin Wolf         }
1172797ac58cSKevin Wolf     }
1173797ac58cSKevin Wolf 
1174797ac58cSKevin Wolf     if (optind > argc - 2) {
1175*b444d0e9SMax Reitz         qemuio_command_usage(&writev_cmd);
1176*b444d0e9SMax Reitz         return;
1177797ac58cSKevin Wolf     }
1178797ac58cSKevin Wolf 
1179797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1180797ac58cSKevin Wolf     if (offset < 0) {
1181a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1182*b444d0e9SMax Reitz         return;
1183797ac58cSKevin Wolf     }
1184797ac58cSKevin Wolf     optind++;
1185797ac58cSKevin Wolf 
1186797ac58cSKevin Wolf     nr_iov = argc - optind;
11874c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1188797ac58cSKevin Wolf     if (buf == NULL) {
1189*b444d0e9SMax Reitz         return;
1190797ac58cSKevin Wolf     }
1191797ac58cSKevin Wolf 
1192797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1193770e0e0eSEric Blake     cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
1194797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1195797ac58cSKevin Wolf 
1196797ac58cSKevin Wolf     if (cnt < 0) {
1197797ac58cSKevin Wolf         printf("writev failed: %s\n", strerror(-cnt));
1198797ac58cSKevin Wolf         goto out;
1199797ac58cSKevin Wolf     }
1200797ac58cSKevin Wolf 
1201797ac58cSKevin Wolf     if (qflag) {
1202797ac58cSKevin Wolf         goto out;
1203797ac58cSKevin Wolf     }
1204797ac58cSKevin Wolf 
1205797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1206797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1207797ac58cSKevin Wolf     print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1208797ac58cSKevin Wolf out:
1209797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
1210797ac58cSKevin Wolf     qemu_io_free(buf);
1211797ac58cSKevin Wolf }
1212797ac58cSKevin Wolf 
1213797ac58cSKevin Wolf struct aio_ctx {
12144c7b7e9bSMax Reitz     BlockBackend *blk;
1215797ac58cSKevin Wolf     QEMUIOVector qiov;
1216797ac58cSKevin Wolf     int64_t offset;
1217797ac58cSKevin Wolf     char *buf;
1218dc38852aSEric Blake     bool qflag;
1219dc38852aSEric Blake     bool vflag;
1220dc38852aSEric Blake     bool Cflag;
1221dc38852aSEric Blake     bool Pflag;
1222dc38852aSEric Blake     bool zflag;
1223a91f9584SFam Zheng     BlockAcctCookie acct;
1224797ac58cSKevin Wolf     int pattern;
1225797ac58cSKevin Wolf     struct timeval t1;
1226797ac58cSKevin Wolf };
1227797ac58cSKevin Wolf 
1228797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret)
1229797ac58cSKevin Wolf {
1230797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1231797ac58cSKevin Wolf     struct timeval t2;
1232797ac58cSKevin Wolf 
1233797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1234797ac58cSKevin Wolf 
1235797ac58cSKevin Wolf 
1236797ac58cSKevin Wolf     if (ret < 0) {
1237797ac58cSKevin Wolf         printf("aio_write failed: %s\n", strerror(-ret));
1238556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1239797ac58cSKevin Wolf         goto out;
1240797ac58cSKevin Wolf     }
1241797ac58cSKevin Wolf 
12424c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1243a91f9584SFam Zheng 
1244797ac58cSKevin Wolf     if (ctx->qflag) {
1245797ac58cSKevin Wolf         goto out;
1246797ac58cSKevin Wolf     }
1247797ac58cSKevin Wolf 
1248797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1249797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1250797ac58cSKevin Wolf     print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1251797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1252797ac58cSKevin Wolf out:
12535ceb7765SKevin Wolf     if (!ctx->zflag) {
1254797ac58cSKevin Wolf         qemu_io_free(ctx->buf);
1255797ac58cSKevin Wolf         qemu_iovec_destroy(&ctx->qiov);
12565ceb7765SKevin Wolf     }
1257797ac58cSKevin Wolf     g_free(ctx);
1258797ac58cSKevin Wolf }
1259797ac58cSKevin Wolf 
1260797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret)
1261797ac58cSKevin Wolf {
1262797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1263797ac58cSKevin Wolf     struct timeval t2;
1264797ac58cSKevin Wolf 
1265797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1266797ac58cSKevin Wolf 
1267797ac58cSKevin Wolf     if (ret < 0) {
1268797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-ret));
1269556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1270797ac58cSKevin Wolf         goto out;
1271797ac58cSKevin Wolf     }
1272797ac58cSKevin Wolf 
1273797ac58cSKevin Wolf     if (ctx->Pflag) {
1274797ac58cSKevin Wolf         void *cmp_buf = g_malloc(ctx->qiov.size);
1275797ac58cSKevin Wolf 
1276797ac58cSKevin Wolf         memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1277797ac58cSKevin Wolf         if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1278797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
1279797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1280797ac58cSKevin Wolf         }
1281797ac58cSKevin Wolf         g_free(cmp_buf);
1282797ac58cSKevin Wolf     }
1283797ac58cSKevin Wolf 
12844c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1285a91f9584SFam Zheng 
1286797ac58cSKevin Wolf     if (ctx->qflag) {
1287797ac58cSKevin Wolf         goto out;
1288797ac58cSKevin Wolf     }
1289797ac58cSKevin Wolf 
1290797ac58cSKevin Wolf     if (ctx->vflag) {
1291797ac58cSKevin Wolf         dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1292797ac58cSKevin Wolf     }
1293797ac58cSKevin Wolf 
1294797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1295797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1296797ac58cSKevin Wolf     print_report("read", &t2, ctx->offset, ctx->qiov.size,
1297797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1298797ac58cSKevin Wolf out:
1299797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1300797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1301797ac58cSKevin Wolf     g_free(ctx);
1302797ac58cSKevin Wolf }
1303797ac58cSKevin Wolf 
1304797ac58cSKevin Wolf static void aio_read_help(void)
1305797ac58cSKevin Wolf {
1306797ac58cSKevin Wolf     printf(
1307797ac58cSKevin Wolf "\n"
1308797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n"
1309797ac58cSKevin Wolf "\n"
1310797ac58cSKevin Wolf " Example:\n"
1311797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1312797ac58cSKevin Wolf "\n"
1313797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
1314797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
1315797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n"
1316797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1317797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1318797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
131937546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n"
1320797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
1321797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1322797ac58cSKevin Wolf "\n");
1323797ac58cSKevin Wolf }
1324797ac58cSKevin Wolf 
1325*b444d0e9SMax Reitz static void aio_read_f(BlockBackend *blk, int argc, char **argv);
1326797ac58cSKevin Wolf 
1327797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = {
1328797ac58cSKevin Wolf     .name       = "aio_read",
1329797ac58cSKevin Wolf     .cfunc      = aio_read_f,
1330797ac58cSKevin Wolf     .argmin     = 2,
1331797ac58cSKevin Wolf     .argmax     = -1,
133237546ff2SEric Blake     .args       = "[-Ciqv] [-P pattern] off len [len..]",
1333797ac58cSKevin Wolf     .oneline    = "asynchronously reads a number of bytes",
1334797ac58cSKevin Wolf     .help       = aio_read_help,
1335797ac58cSKevin Wolf };
1336797ac58cSKevin Wolf 
1337*b444d0e9SMax Reitz static void aio_read_f(BlockBackend *blk, int argc, char **argv)
1338797ac58cSKevin Wolf {
1339797ac58cSKevin Wolf     int nr_iov, c;
1340797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1341797ac58cSKevin Wolf 
13424c7b7e9bSMax Reitz     ctx->blk = blk;
134337546ff2SEric Blake     while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
1344797ac58cSKevin Wolf         switch (c) {
1345797ac58cSKevin Wolf         case 'C':
1346dc38852aSEric Blake             ctx->Cflag = true;
1347797ac58cSKevin Wolf             break;
1348797ac58cSKevin Wolf         case 'P':
1349dc38852aSEric Blake             ctx->Pflag = true;
1350797ac58cSKevin Wolf             ctx->pattern = parse_pattern(optarg);
1351797ac58cSKevin Wolf             if (ctx->pattern < 0) {
1352797ac58cSKevin Wolf                 g_free(ctx);
1353*b444d0e9SMax Reitz                 return;
1354797ac58cSKevin Wolf             }
1355797ac58cSKevin Wolf             break;
135637546ff2SEric Blake         case 'i':
135737546ff2SEric Blake             printf("injecting invalid read request\n");
135837546ff2SEric Blake             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
135937546ff2SEric Blake             g_free(ctx);
1360*b444d0e9SMax Reitz             return;
1361797ac58cSKevin Wolf         case 'q':
1362dc38852aSEric Blake             ctx->qflag = true;
1363797ac58cSKevin Wolf             break;
1364797ac58cSKevin Wolf         case 'v':
1365dc38852aSEric Blake             ctx->vflag = true;
1366797ac58cSKevin Wolf             break;
1367797ac58cSKevin Wolf         default:
1368797ac58cSKevin Wolf             g_free(ctx);
1369*b444d0e9SMax Reitz             qemuio_command_usage(&aio_read_cmd);
1370*b444d0e9SMax Reitz             return;
1371797ac58cSKevin Wolf         }
1372797ac58cSKevin Wolf     }
1373797ac58cSKevin Wolf 
1374797ac58cSKevin Wolf     if (optind > argc - 2) {
1375797ac58cSKevin Wolf         g_free(ctx);
1376*b444d0e9SMax Reitz         qemuio_command_usage(&aio_read_cmd);
1377*b444d0e9SMax Reitz         return;
1378797ac58cSKevin Wolf     }
1379797ac58cSKevin Wolf 
1380797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1381797ac58cSKevin Wolf     if (ctx->offset < 0) {
1382a9ecfa00SJohn Snow         print_cvtnum_err(ctx->offset, argv[optind]);
1383797ac58cSKevin Wolf         g_free(ctx);
1384*b444d0e9SMax Reitz         return;
1385797ac58cSKevin Wolf     }
1386797ac58cSKevin Wolf     optind++;
1387797ac58cSKevin Wolf 
1388797ac58cSKevin Wolf     nr_iov = argc - optind;
13894c7b7e9bSMax Reitz     ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1390797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1391556c2b60SAlberto Garcia         block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1392797ac58cSKevin Wolf         g_free(ctx);
1393*b444d0e9SMax Reitz         return;
1394797ac58cSKevin Wolf     }
1395797ac58cSKevin Wolf 
1396797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
13974c7b7e9bSMax Reitz     block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
13984c7b7e9bSMax Reitz                      BLOCK_ACCT_READ);
13997b3f9712SEric Blake     blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1400797ac58cSKevin Wolf }
1401797ac58cSKevin Wolf 
1402797ac58cSKevin Wolf static void aio_write_help(void)
1403797ac58cSKevin Wolf {
1404797ac58cSKevin Wolf     printf(
1405797ac58cSKevin Wolf "\n"
1406797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n"
1407797ac58cSKevin Wolf " from multiple buffers\n"
1408797ac58cSKevin Wolf "\n"
1409797ac58cSKevin Wolf " Example:\n"
1410797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1411797ac58cSKevin Wolf "\n"
1412797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1413797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1414797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n"
1415797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1416797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1417797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1418770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
141937546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n"
1420797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1421c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
1422d004bd52SEric Blake " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1423797ac58cSKevin Wolf "\n");
1424797ac58cSKevin Wolf }
1425797ac58cSKevin Wolf 
1426*b444d0e9SMax Reitz static void aio_write_f(BlockBackend *blk, int argc, char **argv);
1427797ac58cSKevin Wolf 
1428797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = {
1429797ac58cSKevin Wolf     .name       = "aio_write",
1430797ac58cSKevin Wolf     .cfunc      = aio_write_f,
1431887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
1432797ac58cSKevin Wolf     .argmin     = 2,
1433797ac58cSKevin Wolf     .argmax     = -1,
143437546ff2SEric Blake     .args       = "[-Cfiquz] [-P pattern] off len [len..]",
1435797ac58cSKevin Wolf     .oneline    = "asynchronously writes a number of bytes",
1436797ac58cSKevin Wolf     .help       = aio_write_help,
1437797ac58cSKevin Wolf };
1438797ac58cSKevin Wolf 
1439*b444d0e9SMax Reitz static void aio_write_f(BlockBackend *blk, int argc, char **argv)
1440797ac58cSKevin Wolf {
1441797ac58cSKevin Wolf     int nr_iov, c;
1442797ac58cSKevin Wolf     int pattern = 0xcd;
1443797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1444770e0e0eSEric Blake     int flags = 0;
1445797ac58cSKevin Wolf 
14464c7b7e9bSMax Reitz     ctx->blk = blk;
144737546ff2SEric Blake     while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
1448797ac58cSKevin Wolf         switch (c) {
1449797ac58cSKevin Wolf         case 'C':
1450dc38852aSEric Blake             ctx->Cflag = true;
1451797ac58cSKevin Wolf             break;
1452770e0e0eSEric Blake         case 'f':
1453770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1454770e0e0eSEric Blake             break;
1455797ac58cSKevin Wolf         case 'q':
1456dc38852aSEric Blake             ctx->qflag = true;
1457797ac58cSKevin Wolf             break;
1458c2e001ccSEric Blake         case 'u':
1459c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1460c2e001ccSEric Blake             break;
1461797ac58cSKevin Wolf         case 'P':
1462797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1463797ac58cSKevin Wolf             if (pattern < 0) {
1464797ac58cSKevin Wolf                 g_free(ctx);
1465*b444d0e9SMax Reitz                 return;
1466797ac58cSKevin Wolf             }
1467797ac58cSKevin Wolf             break;
146837546ff2SEric Blake         case 'i':
146937546ff2SEric Blake             printf("injecting invalid write request\n");
147037546ff2SEric Blake             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
147137546ff2SEric Blake             g_free(ctx);
1472*b444d0e9SMax Reitz             return;
14735ceb7765SKevin Wolf         case 'z':
1474dc38852aSEric Blake             ctx->zflag = true;
14755ceb7765SKevin Wolf             break;
1476797ac58cSKevin Wolf         default:
1477797ac58cSKevin Wolf             g_free(ctx);
1478*b444d0e9SMax Reitz             qemuio_command_usage(&aio_write_cmd);
1479*b444d0e9SMax Reitz             return;
1480797ac58cSKevin Wolf         }
1481797ac58cSKevin Wolf     }
1482797ac58cSKevin Wolf 
1483797ac58cSKevin Wolf     if (optind > argc - 2) {
1484797ac58cSKevin Wolf         g_free(ctx);
1485*b444d0e9SMax Reitz         qemuio_command_usage(&aio_write_cmd);
1486*b444d0e9SMax Reitz         return;
1487797ac58cSKevin Wolf     }
1488797ac58cSKevin Wolf 
14895ceb7765SKevin Wolf     if (ctx->zflag && optind != argc - 2) {
14905ceb7765SKevin Wolf         printf("-z supports only a single length parameter\n");
14915ceb7765SKevin Wolf         g_free(ctx);
1492*b444d0e9SMax Reitz         return;
14935ceb7765SKevin Wolf     }
14945ceb7765SKevin Wolf 
1495c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1496c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
14974ca1d340SEric Blake         g_free(ctx);
1498*b444d0e9SMax Reitz         return;
1499c2e001ccSEric Blake     }
1500c2e001ccSEric Blake 
15015ceb7765SKevin Wolf     if (ctx->zflag && ctx->Pflag) {
15025ceb7765SKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
15035ceb7765SKevin Wolf         g_free(ctx);
1504*b444d0e9SMax Reitz         return;
15055ceb7765SKevin Wolf     }
15065ceb7765SKevin Wolf 
1507797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1508797ac58cSKevin Wolf     if (ctx->offset < 0) {
1509a9ecfa00SJohn Snow         print_cvtnum_err(ctx->offset, argv[optind]);
1510797ac58cSKevin Wolf         g_free(ctx);
1511*b444d0e9SMax Reitz         return;
1512797ac58cSKevin Wolf     }
1513797ac58cSKevin Wolf     optind++;
1514797ac58cSKevin Wolf 
15155ceb7765SKevin Wolf     if (ctx->zflag) {
15165ceb7765SKevin Wolf         int64_t count = cvtnum(argv[optind]);
15175ceb7765SKevin Wolf         if (count < 0) {
15185ceb7765SKevin Wolf             print_cvtnum_err(count, argv[optind]);
15190e01b76eSKevin Wolf             g_free(ctx);
1520*b444d0e9SMax Reitz             return;
15215ceb7765SKevin Wolf         }
15225ceb7765SKevin Wolf 
15235ceb7765SKevin Wolf         ctx->qiov.size = count;
1524d004bd52SEric Blake         blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1525770e0e0eSEric Blake                               ctx);
15265ceb7765SKevin Wolf     } else {
1527797ac58cSKevin Wolf         nr_iov = argc - optind;
15285ceb7765SKevin Wolf         ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
15295ceb7765SKevin Wolf                                 pattern);
1530797ac58cSKevin Wolf         if (ctx->buf == NULL) {
1531556c2b60SAlberto Garcia             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1532797ac58cSKevin Wolf             g_free(ctx);
1533*b444d0e9SMax Reitz             return;
1534797ac58cSKevin Wolf         }
1535797ac58cSKevin Wolf 
1536797ac58cSKevin Wolf         gettimeofday(&ctx->t1, NULL);
15374c7b7e9bSMax Reitz         block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
15384c7b7e9bSMax Reitz                          BLOCK_ACCT_WRITE);
15395ceb7765SKevin Wolf 
1540770e0e0eSEric Blake         blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1541770e0e0eSEric Blake                         ctx);
15425ceb7765SKevin Wolf     }
1543797ac58cSKevin Wolf }
1544797ac58cSKevin Wolf 
1545*b444d0e9SMax Reitz static void aio_flush_f(BlockBackend *blk, int argc, char **argv)
1546797ac58cSKevin Wolf {
1547556c2b60SAlberto Garcia     BlockAcctCookie cookie;
1548556c2b60SAlberto Garcia     block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
15494c7b7e9bSMax Reitz     blk_drain_all();
1550556c2b60SAlberto Garcia     block_acct_done(blk_get_stats(blk), &cookie);
1551797ac58cSKevin Wolf }
1552797ac58cSKevin Wolf 
1553797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = {
1554797ac58cSKevin Wolf     .name       = "aio_flush",
1555797ac58cSKevin Wolf     .cfunc      = aio_flush_f,
1556797ac58cSKevin Wolf     .oneline    = "completes all outstanding aio requests"
1557797ac58cSKevin Wolf };
1558797ac58cSKevin Wolf 
1559*b444d0e9SMax Reitz static void flush_f(BlockBackend *blk, int argc, char **argv)
1560797ac58cSKevin Wolf {
15614c7b7e9bSMax Reitz     blk_flush(blk);
1562797ac58cSKevin Wolf }
1563797ac58cSKevin Wolf 
1564797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = {
1565797ac58cSKevin Wolf     .name       = "flush",
1566797ac58cSKevin Wolf     .altname    = "f",
1567797ac58cSKevin Wolf     .cfunc      = flush_f,
1568797ac58cSKevin Wolf     .oneline    = "flush all in-core file state to disk",
1569797ac58cSKevin Wolf };
1570797ac58cSKevin Wolf 
1571*b444d0e9SMax Reitz static void truncate_f(BlockBackend *blk, int argc, char **argv)
1572797ac58cSKevin Wolf {
1573ed3d2ec9SMax Reitz     Error *local_err = NULL;
1574797ac58cSKevin Wolf     int64_t offset;
1575797ac58cSKevin Wolf     int ret;
1576797ac58cSKevin Wolf 
1577797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1578797ac58cSKevin Wolf     if (offset < 0) {
1579a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1580*b444d0e9SMax Reitz         return;
1581797ac58cSKevin Wolf     }
1582797ac58cSKevin Wolf 
15833a691c50SMax Reitz     ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
1584797ac58cSKevin Wolf     if (ret < 0) {
1585ed3d2ec9SMax Reitz         error_report_err(local_err);
1586*b444d0e9SMax Reitz         return;
1587797ac58cSKevin Wolf     }
1588797ac58cSKevin Wolf }
1589797ac58cSKevin Wolf 
1590797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = {
1591797ac58cSKevin Wolf     .name       = "truncate",
1592797ac58cSKevin Wolf     .altname    = "t",
1593797ac58cSKevin Wolf     .cfunc      = truncate_f,
1594887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1595797ac58cSKevin Wolf     .argmin     = 1,
1596797ac58cSKevin Wolf     .argmax     = 1,
1597797ac58cSKevin Wolf     .args       = "off",
1598797ac58cSKevin Wolf     .oneline    = "truncates the current file at the given offset",
1599797ac58cSKevin Wolf };
1600797ac58cSKevin Wolf 
1601*b444d0e9SMax Reitz static void length_f(BlockBackend *blk, int argc, char **argv)
1602797ac58cSKevin Wolf {
1603797ac58cSKevin Wolf     int64_t size;
1604797ac58cSKevin Wolf     char s1[64];
1605797ac58cSKevin Wolf 
16064c7b7e9bSMax Reitz     size = blk_getlength(blk);
1607797ac58cSKevin Wolf     if (size < 0) {
1608797ac58cSKevin Wolf         printf("getlength: %s\n", strerror(-size));
1609*b444d0e9SMax Reitz         return;
1610797ac58cSKevin Wolf     }
1611797ac58cSKevin Wolf 
1612797ac58cSKevin Wolf     cvtstr(size, s1, sizeof(s1));
1613797ac58cSKevin Wolf     printf("%s\n", s1);
1614797ac58cSKevin Wolf }
1615797ac58cSKevin Wolf 
1616797ac58cSKevin Wolf 
1617797ac58cSKevin Wolf static const cmdinfo_t length_cmd = {
1618797ac58cSKevin Wolf     .name   = "length",
1619797ac58cSKevin Wolf     .altname    = "l",
1620797ac58cSKevin Wolf     .cfunc      = length_f,
1621797ac58cSKevin Wolf     .oneline    = "gets the length of the current file",
1622797ac58cSKevin Wolf };
1623797ac58cSKevin Wolf 
1624797ac58cSKevin Wolf 
1625*b444d0e9SMax Reitz static void info_f(BlockBackend *blk, int argc, char **argv)
1626797ac58cSKevin Wolf {
16274c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
1628797ac58cSKevin Wolf     BlockDriverInfo bdi;
1629a8d8ecb7SMax Reitz     ImageInfoSpecific *spec_info;
1630797ac58cSKevin Wolf     char s1[64], s2[64];
1631797ac58cSKevin Wolf     int ret;
1632797ac58cSKevin Wolf 
1633797ac58cSKevin Wolf     if (bs->drv && bs->drv->format_name) {
1634797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->format_name);
1635797ac58cSKevin Wolf     }
1636797ac58cSKevin Wolf     if (bs->drv && bs->drv->protocol_name) {
1637797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->protocol_name);
1638797ac58cSKevin Wolf     }
1639797ac58cSKevin Wolf 
1640797ac58cSKevin Wolf     ret = bdrv_get_info(bs, &bdi);
1641797ac58cSKevin Wolf     if (ret) {
1642*b444d0e9SMax Reitz         return;
1643797ac58cSKevin Wolf     }
1644797ac58cSKevin Wolf 
1645797ac58cSKevin Wolf     cvtstr(bdi.cluster_size, s1, sizeof(s1));
1646797ac58cSKevin Wolf     cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1647797ac58cSKevin Wolf 
1648797ac58cSKevin Wolf     printf("cluster size: %s\n", s1);
1649797ac58cSKevin Wolf     printf("vm state offset: %s\n", s2);
1650797ac58cSKevin Wolf 
1651a8d8ecb7SMax Reitz     spec_info = bdrv_get_specific_info(bs);
1652a8d8ecb7SMax Reitz     if (spec_info) {
1653a8d8ecb7SMax Reitz         printf("Format specific information:\n");
1654a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1655a8d8ecb7SMax Reitz         qapi_free_ImageInfoSpecific(spec_info);
1656a8d8ecb7SMax Reitz     }
1657797ac58cSKevin Wolf }
1658797ac58cSKevin Wolf 
1659797ac58cSKevin Wolf 
1660797ac58cSKevin Wolf 
1661797ac58cSKevin Wolf static const cmdinfo_t info_cmd = {
1662797ac58cSKevin Wolf     .name       = "info",
1663797ac58cSKevin Wolf     .altname    = "i",
1664797ac58cSKevin Wolf     .cfunc      = info_f,
1665797ac58cSKevin Wolf     .oneline    = "prints information about the current file",
1666797ac58cSKevin Wolf };
1667797ac58cSKevin Wolf 
1668797ac58cSKevin Wolf static void discard_help(void)
1669797ac58cSKevin Wolf {
1670797ac58cSKevin Wolf     printf(
1671797ac58cSKevin Wolf "\n"
1672797ac58cSKevin Wolf " discards a range of bytes from the given offset\n"
1673797ac58cSKevin Wolf "\n"
1674797ac58cSKevin Wolf " Example:\n"
1675797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1676797ac58cSKevin Wolf "\n"
1677797ac58cSKevin Wolf " Discards a segment of the currently open file.\n"
1678797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1679797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1680797ac58cSKevin Wolf "\n");
1681797ac58cSKevin Wolf }
1682797ac58cSKevin Wolf 
1683*b444d0e9SMax Reitz static void discard_f(BlockBackend *blk, int argc, char **argv);
1684797ac58cSKevin Wolf 
1685797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = {
1686797ac58cSKevin Wolf     .name       = "discard",
1687797ac58cSKevin Wolf     .altname    = "d",
1688797ac58cSKevin Wolf     .cfunc      = discard_f,
1689887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
1690797ac58cSKevin Wolf     .argmin     = 2,
1691797ac58cSKevin Wolf     .argmax     = -1,
1692797ac58cSKevin Wolf     .args       = "[-Cq] off len",
1693797ac58cSKevin Wolf     .oneline    = "discards a number of bytes at a specified offset",
1694797ac58cSKevin Wolf     .help       = discard_help,
1695797ac58cSKevin Wolf };
1696797ac58cSKevin Wolf 
1697*b444d0e9SMax Reitz static void discard_f(BlockBackend *blk, int argc, char **argv)
1698797ac58cSKevin Wolf {
1699797ac58cSKevin Wolf     struct timeval t1, t2;
1700dc38852aSEric Blake     bool Cflag = false, qflag = false;
1701797ac58cSKevin Wolf     int c, ret;
1702f5a5ca79SManos Pitsidianakis     int64_t offset, bytes;
1703797ac58cSKevin Wolf 
1704b062ad86SEric Blake     while ((c = getopt(argc, argv, "Cq")) != -1) {
1705797ac58cSKevin Wolf         switch (c) {
1706797ac58cSKevin Wolf         case 'C':
1707dc38852aSEric Blake             Cflag = true;
1708797ac58cSKevin Wolf             break;
1709797ac58cSKevin Wolf         case 'q':
1710dc38852aSEric Blake             qflag = true;
1711797ac58cSKevin Wolf             break;
1712797ac58cSKevin Wolf         default:
1713*b444d0e9SMax Reitz             qemuio_command_usage(&discard_cmd);
1714*b444d0e9SMax Reitz             return;
1715797ac58cSKevin Wolf         }
1716797ac58cSKevin Wolf     }
1717797ac58cSKevin Wolf 
1718797ac58cSKevin Wolf     if (optind != argc - 2) {
1719*b444d0e9SMax Reitz         qemuio_command_usage(&discard_cmd);
1720*b444d0e9SMax Reitz         return;
1721797ac58cSKevin Wolf     }
1722797ac58cSKevin Wolf 
1723797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1724797ac58cSKevin Wolf     if (offset < 0) {
1725a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1726*b444d0e9SMax Reitz         return;
1727797ac58cSKevin Wolf     }
1728797ac58cSKevin Wolf 
1729797ac58cSKevin Wolf     optind++;
1730f5a5ca79SManos Pitsidianakis     bytes = cvtnum(argv[optind]);
1731f5a5ca79SManos Pitsidianakis     if (bytes < 0) {
1732f5a5ca79SManos Pitsidianakis         print_cvtnum_err(bytes, argv[optind]);
1733*b444d0e9SMax Reitz         return;
1734f5a5ca79SManos Pitsidianakis     } else if (bytes >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
17359b0beaf3SJohn Snow         printf("length cannot exceed %"PRIu64", given %s\n",
1736a3674679SMax Reitz                (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
17379b0beaf3SJohn Snow                argv[optind]);
1738*b444d0e9SMax Reitz         return;
1739797ac58cSKevin Wolf     }
1740797ac58cSKevin Wolf 
1741797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1742f5a5ca79SManos Pitsidianakis     ret = blk_pdiscard(blk, offset, bytes);
1743797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1744797ac58cSKevin Wolf 
1745797ac58cSKevin Wolf     if (ret < 0) {
1746797ac58cSKevin Wolf         printf("discard failed: %s\n", strerror(-ret));
1747*b444d0e9SMax Reitz         return;
1748797ac58cSKevin Wolf     }
1749797ac58cSKevin Wolf 
1750797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1751797ac58cSKevin Wolf     if (!qflag) {
1752797ac58cSKevin Wolf         t2 = tsub(t2, t1);
1753f5a5ca79SManos Pitsidianakis         print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
1754797ac58cSKevin Wolf     }
1755797ac58cSKevin Wolf }
1756797ac58cSKevin Wolf 
1757*b444d0e9SMax Reitz static void alloc_f(BlockBackend *blk, int argc, char **argv)
1758797ac58cSKevin Wolf {
17594c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
1760d6a644bbSEric Blake     int64_t offset, start, remaining, count;
1761797ac58cSKevin Wolf     char s1[64];
1762d6a644bbSEric Blake     int ret;
1763d6a644bbSEric Blake     int64_t num, sum_alloc;
1764797ac58cSKevin Wolf 
1765d6a644bbSEric Blake     start = offset = cvtnum(argv[1]);
1766797ac58cSKevin Wolf     if (offset < 0) {
1767a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1768*b444d0e9SMax Reitz         return;
1769797ac58cSKevin Wolf     }
1770797ac58cSKevin Wolf 
1771797ac58cSKevin Wolf     if (argc == 3) {
17724401fdc7SEric Blake         count = cvtnum(argv[2]);
17734401fdc7SEric Blake         if (count < 0) {
17744401fdc7SEric Blake             print_cvtnum_err(count, argv[2]);
1775*b444d0e9SMax Reitz             return;
1776797ac58cSKevin Wolf         }
1777797ac58cSKevin Wolf     } else {
17784401fdc7SEric Blake         count = BDRV_SECTOR_SIZE;
1779797ac58cSKevin Wolf     }
1780797ac58cSKevin Wolf 
1781d6a644bbSEric Blake     remaining = count;
1782797ac58cSKevin Wolf     sum_alloc = 0;
1783797ac58cSKevin Wolf     while (remaining) {
1784d6a644bbSEric Blake         ret = bdrv_is_allocated(bs, offset, remaining, &num);
1785d663640cSPaolo Bonzini         if (ret < 0) {
1786d663640cSPaolo Bonzini             printf("is_allocated failed: %s\n", strerror(-ret));
1787*b444d0e9SMax Reitz             return;
1788d663640cSPaolo Bonzini         }
1789d6a644bbSEric Blake         offset += num;
1790797ac58cSKevin Wolf         remaining -= num;
1791797ac58cSKevin Wolf         if (ret) {
1792797ac58cSKevin Wolf             sum_alloc += num;
1793797ac58cSKevin Wolf         }
1794797ac58cSKevin Wolf         if (num == 0) {
1795d6a644bbSEric Blake             count -= remaining;
1796797ac58cSKevin Wolf             remaining = 0;
1797797ac58cSKevin Wolf         }
1798797ac58cSKevin Wolf     }
1799797ac58cSKevin Wolf 
1800d6a644bbSEric Blake     cvtstr(start, s1, sizeof(s1));
1801797ac58cSKevin Wolf 
18024401fdc7SEric Blake     printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
1803d6a644bbSEric Blake            sum_alloc, count, s1);
1804797ac58cSKevin Wolf }
1805797ac58cSKevin Wolf 
1806797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = {
1807797ac58cSKevin Wolf     .name       = "alloc",
1808797ac58cSKevin Wolf     .altname    = "a",
1809797ac58cSKevin Wolf     .argmin     = 1,
1810797ac58cSKevin Wolf     .argmax     = 2,
1811797ac58cSKevin Wolf     .cfunc      = alloc_f,
18124401fdc7SEric Blake     .args       = "offset [count]",
18134401fdc7SEric Blake     .oneline    = "checks if offset is allocated in the file",
1814797ac58cSKevin Wolf };
1815797ac58cSKevin Wolf 
1816797ac58cSKevin Wolf 
1817d6a644bbSEric Blake static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1818d6a644bbSEric Blake                             int64_t bytes, int64_t *pnum)
1819797ac58cSKevin Wolf {
1820d6a644bbSEric Blake     int64_t num;
1821d6a644bbSEric Blake     int num_checked;
1822797ac58cSKevin Wolf     int ret, firstret;
1823797ac58cSKevin Wolf 
1824d6a644bbSEric Blake     num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1825d6a644bbSEric Blake     ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1826797ac58cSKevin Wolf     if (ret < 0) {
1827797ac58cSKevin Wolf         return ret;
1828797ac58cSKevin Wolf     }
1829797ac58cSKevin Wolf 
1830797ac58cSKevin Wolf     firstret = ret;
1831797ac58cSKevin Wolf     *pnum = num;
1832797ac58cSKevin Wolf 
1833d6a644bbSEric Blake     while (bytes > 0 && ret == firstret) {
1834d6a644bbSEric Blake         offset += num;
1835d6a644bbSEric Blake         bytes -= num;
1836797ac58cSKevin Wolf 
1837d6a644bbSEric Blake         num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1838d6a644bbSEric Blake         ret = bdrv_is_allocated(bs, offset, num_checked, &num);
18394b25bbc4SMax Reitz         if (ret == firstret && num) {
1840797ac58cSKevin Wolf             *pnum += num;
1841797ac58cSKevin Wolf         } else {
1842797ac58cSKevin Wolf             break;
1843797ac58cSKevin Wolf         }
1844797ac58cSKevin Wolf     }
1845797ac58cSKevin Wolf 
1846797ac58cSKevin Wolf     return firstret;
1847797ac58cSKevin Wolf }
1848797ac58cSKevin Wolf 
1849*b444d0e9SMax Reitz static void map_f(BlockBackend *blk, int argc, char **argv)
1850797ac58cSKevin Wolf {
1851d6a644bbSEric Blake     int64_t offset, bytes;
18526f3c90afSEric Blake     char s1[64], s2[64];
1853797ac58cSKevin Wolf     int64_t num;
1854797ac58cSKevin Wolf     int ret;
1855797ac58cSKevin Wolf     const char *retstr;
1856797ac58cSKevin Wolf 
1857797ac58cSKevin Wolf     offset = 0;
1858d6a644bbSEric Blake     bytes = blk_getlength(blk);
1859d6a644bbSEric Blake     if (bytes < 0) {
1860d6a644bbSEric Blake         error_report("Failed to query image length: %s", strerror(-bytes));
1861*b444d0e9SMax Reitz         return;
18624c7b7e9bSMax Reitz     }
18634c7b7e9bSMax Reitz 
1864d6a644bbSEric Blake     while (bytes) {
1865d6a644bbSEric Blake         ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
1866797ac58cSKevin Wolf         if (ret < 0) {
1867797ac58cSKevin Wolf             error_report("Failed to get allocation status: %s", strerror(-ret));
1868*b444d0e9SMax Reitz             return;
18694b25bbc4SMax Reitz         } else if (!num) {
18704b25bbc4SMax Reitz             error_report("Unexpected end of image");
1871*b444d0e9SMax Reitz             return;
1872797ac58cSKevin Wolf         }
1873797ac58cSKevin Wolf 
1874797ac58cSKevin Wolf         retstr = ret ? "    allocated" : "not allocated";
1875d6a644bbSEric Blake         cvtstr(num, s1, sizeof(s1));
1876d6a644bbSEric Blake         cvtstr(offset, s2, sizeof(s2));
18776f3c90afSEric Blake         printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
1878d6a644bbSEric Blake                s1, num, retstr, s2, offset);
1879797ac58cSKevin Wolf 
1880797ac58cSKevin Wolf         offset += num;
1881d6a644bbSEric Blake         bytes -= num;
1882d6a644bbSEric Blake     }
1883797ac58cSKevin Wolf }
1884797ac58cSKevin Wolf 
1885797ac58cSKevin Wolf static const cmdinfo_t map_cmd = {
1886797ac58cSKevin Wolf        .name           = "map",
1887797ac58cSKevin Wolf        .argmin         = 0,
1888797ac58cSKevin Wolf        .argmax         = 0,
1889797ac58cSKevin Wolf        .cfunc          = map_f,
1890797ac58cSKevin Wolf        .args           = "",
1891797ac58cSKevin Wolf        .oneline        = "prints the allocated areas of a file",
1892797ac58cSKevin Wolf };
1893797ac58cSKevin Wolf 
18945bbd2e59SKevin Wolf static void reopen_help(void)
18955bbd2e59SKevin Wolf {
18965bbd2e59SKevin Wolf     printf(
18975bbd2e59SKevin Wolf "\n"
18985bbd2e59SKevin Wolf " Changes the open options of an already opened image\n"
18995bbd2e59SKevin Wolf "\n"
19005bbd2e59SKevin Wolf " Example:\n"
19015bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
19025bbd2e59SKevin Wolf "\n"
19035bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n"
1904ea92203cSKevin Wolf " -w, -- Reopen the image read-write\n"
19055bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n"
19065bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n"
19075bbd2e59SKevin Wolf "\n");
19085bbd2e59SKevin Wolf }
19095bbd2e59SKevin Wolf 
1910*b444d0e9SMax Reitz static void reopen_f(BlockBackend *blk, int argc, char **argv);
19115bbd2e59SKevin Wolf 
19125bbd2e59SKevin Wolf static QemuOptsList reopen_opts = {
19135bbd2e59SKevin Wolf     .name = "reopen",
19145bbd2e59SKevin Wolf     .merge_lists = true,
19155bbd2e59SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
19165bbd2e59SKevin Wolf     .desc = {
19175bbd2e59SKevin Wolf         /* no elements => accept any params */
19185bbd2e59SKevin Wolf         { /* end of list */ }
19195bbd2e59SKevin Wolf     },
19205bbd2e59SKevin Wolf };
19215bbd2e59SKevin Wolf 
19225bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = {
19235bbd2e59SKevin Wolf        .name           = "reopen",
19245bbd2e59SKevin Wolf        .argmin         = 0,
19255bbd2e59SKevin Wolf        .argmax         = -1,
19265bbd2e59SKevin Wolf        .cfunc          = reopen_f,
1927ea92203cSKevin Wolf        .args           = "[(-r|-w)] [-c cache] [-o options]",
19285bbd2e59SKevin Wolf        .oneline        = "reopens an image with new options",
19295bbd2e59SKevin Wolf        .help           = reopen_help,
19305bbd2e59SKevin Wolf };
19315bbd2e59SKevin Wolf 
1932*b444d0e9SMax Reitz static void reopen_f(BlockBackend *blk, int argc, char **argv)
19335bbd2e59SKevin Wolf {
19345bbd2e59SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
19355bbd2e59SKevin Wolf     QemuOpts *qopts;
19365bbd2e59SKevin Wolf     QDict *opts;
19375bbd2e59SKevin Wolf     int c;
19385bbd2e59SKevin Wolf     int flags = bs->open_flags;
193919dbecdcSKevin Wolf     bool writethrough = !blk_enable_write_cache(blk);
1940ea92203cSKevin Wolf     bool has_rw_option = false;
19415bbd2e59SKevin Wolf 
19425bbd2e59SKevin Wolf     BlockReopenQueue *brq;
19435bbd2e59SKevin Wolf     Error *local_err = NULL;
19445bbd2e59SKevin Wolf 
1945ea92203cSKevin Wolf     while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
19465bbd2e59SKevin Wolf         switch (c) {
19475bbd2e59SKevin Wolf         case 'c':
194819dbecdcSKevin Wolf             if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
19495bbd2e59SKevin Wolf                 error_report("Invalid cache option: %s", optarg);
1950*b444d0e9SMax Reitz                 return;
19515bbd2e59SKevin Wolf             }
19525bbd2e59SKevin Wolf             break;
19535bbd2e59SKevin Wolf         case 'o':
19545bbd2e59SKevin Wolf             if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
19555bbd2e59SKevin Wolf                 qemu_opts_reset(&reopen_opts);
1956*b444d0e9SMax Reitz                 return;
19575bbd2e59SKevin Wolf             }
19585bbd2e59SKevin Wolf             break;
19595bbd2e59SKevin Wolf         case 'r':
1960ea92203cSKevin Wolf             if (has_rw_option) {
1961ea92203cSKevin Wolf                 error_report("Only one -r/-w option may be given");
1962*b444d0e9SMax Reitz                 return;
1963ea92203cSKevin Wolf             }
19645bbd2e59SKevin Wolf             flags &= ~BDRV_O_RDWR;
1965ea92203cSKevin Wolf             has_rw_option = true;
1966ea92203cSKevin Wolf             break;
1967ea92203cSKevin Wolf         case 'w':
1968ea92203cSKevin Wolf             if (has_rw_option) {
1969ea92203cSKevin Wolf                 error_report("Only one -r/-w option may be given");
1970*b444d0e9SMax Reitz                 return;
1971ea92203cSKevin Wolf             }
1972ea92203cSKevin Wolf             flags |= BDRV_O_RDWR;
1973ea92203cSKevin Wolf             has_rw_option = true;
19745bbd2e59SKevin Wolf             break;
19755bbd2e59SKevin Wolf         default:
19765bbd2e59SKevin Wolf             qemu_opts_reset(&reopen_opts);
1977*b444d0e9SMax Reitz             qemuio_command_usage(&reopen_cmd);
1978*b444d0e9SMax Reitz             return;
19795bbd2e59SKevin Wolf         }
19805bbd2e59SKevin Wolf     }
19815bbd2e59SKevin Wolf 
19825bbd2e59SKevin Wolf     if (optind != argc) {
19835bbd2e59SKevin Wolf         qemu_opts_reset(&reopen_opts);
1984*b444d0e9SMax Reitz         qemuio_command_usage(&reopen_cmd);
1985*b444d0e9SMax Reitz         return;
19865bbd2e59SKevin Wolf     }
19875bbd2e59SKevin Wolf 
198819dbecdcSKevin Wolf     if (writethrough != blk_enable_write_cache(blk) &&
198919dbecdcSKevin Wolf         blk_get_attached_dev(blk))
199019dbecdcSKevin Wolf     {
199119dbecdcSKevin Wolf         error_report("Cannot change cache.writeback: Device attached");
199219dbecdcSKevin Wolf         qemu_opts_reset(&reopen_opts);
1993*b444d0e9SMax Reitz         return;
199419dbecdcSKevin Wolf     }
199519dbecdcSKevin Wolf 
1996f3adefb2SKevin Wolf     if (!(flags & BDRV_O_RDWR)) {
1997f3adefb2SKevin Wolf         uint64_t orig_perm, orig_shared_perm;
1998f3adefb2SKevin Wolf 
1999f3adefb2SKevin Wolf         bdrv_drain(bs);
2000f3adefb2SKevin Wolf 
2001f3adefb2SKevin Wolf         blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2002f3adefb2SKevin Wolf         blk_set_perm(blk,
2003f3adefb2SKevin Wolf                      orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2004f3adefb2SKevin Wolf                      orig_shared_perm,
2005f3adefb2SKevin Wolf                      &error_abort);
2006f3adefb2SKevin Wolf     }
2007f3adefb2SKevin Wolf 
20085bbd2e59SKevin Wolf     qopts = qemu_opts_find(&reopen_opts, NULL);
20095bbd2e59SKevin Wolf     opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
20105bbd2e59SKevin Wolf     qemu_opts_reset(&reopen_opts);
20115bbd2e59SKevin Wolf 
20121a63a907SKevin Wolf     bdrv_subtree_drained_begin(bs);
20135bbd2e59SKevin Wolf     brq = bdrv_reopen_queue(NULL, bs, opts, flags);
2014720150f3SPaolo Bonzini     bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
20151a63a907SKevin Wolf     bdrv_subtree_drained_end(bs);
20161a63a907SKevin Wolf 
20175bbd2e59SKevin Wolf     if (local_err) {
20185bbd2e59SKevin Wolf         error_report_err(local_err);
201919dbecdcSKevin Wolf     } else {
202019dbecdcSKevin Wolf         blk_set_enable_write_cache(blk, !writethrough);
20215bbd2e59SKevin Wolf     }
20225bbd2e59SKevin Wolf }
20235bbd2e59SKevin Wolf 
2024*b444d0e9SMax Reitz static void break_f(BlockBackend *blk, int argc, char **argv)
2025797ac58cSKevin Wolf {
2026797ac58cSKevin Wolf     int ret;
2027797ac58cSKevin Wolf 
20284c7b7e9bSMax Reitz     ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2029797ac58cSKevin Wolf     if (ret < 0) {
2030797ac58cSKevin Wolf         printf("Could not set breakpoint: %s\n", strerror(-ret));
2031797ac58cSKevin Wolf     }
2032797ac58cSKevin Wolf }
2033797ac58cSKevin Wolf 
2034*b444d0e9SMax Reitz static void remove_break_f(BlockBackend *blk, int argc, char **argv)
20354cc70e93SFam Zheng {
20364cc70e93SFam Zheng     int ret;
20374cc70e93SFam Zheng 
20384c7b7e9bSMax Reitz     ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
20394cc70e93SFam Zheng     if (ret < 0) {
20404cc70e93SFam Zheng         printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
20414cc70e93SFam Zheng     }
20424cc70e93SFam Zheng }
20434cc70e93SFam Zheng 
2044797ac58cSKevin Wolf static const cmdinfo_t break_cmd = {
2045797ac58cSKevin Wolf        .name           = "break",
2046797ac58cSKevin Wolf        .argmin         = 2,
2047797ac58cSKevin Wolf        .argmax         = 2,
2048797ac58cSKevin Wolf        .cfunc          = break_f,
2049797ac58cSKevin Wolf        .args           = "event tag",
2050797ac58cSKevin Wolf        .oneline        = "sets a breakpoint on event and tags the stopped "
2051797ac58cSKevin Wolf                          "request as tag",
2052797ac58cSKevin Wolf };
2053797ac58cSKevin Wolf 
20544cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = {
20554cc70e93SFam Zheng        .name           = "remove_break",
20564cc70e93SFam Zheng        .argmin         = 1,
20574cc70e93SFam Zheng        .argmax         = 1,
20584cc70e93SFam Zheng        .cfunc          = remove_break_f,
20594cc70e93SFam Zheng        .args           = "tag",
20604cc70e93SFam Zheng        .oneline        = "remove a breakpoint by tag",
20614cc70e93SFam Zheng };
20624cc70e93SFam Zheng 
2063*b444d0e9SMax Reitz static void resume_f(BlockBackend *blk, int argc, char **argv)
2064797ac58cSKevin Wolf {
2065797ac58cSKevin Wolf     int ret;
2066797ac58cSKevin Wolf 
20674c7b7e9bSMax Reitz     ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2068797ac58cSKevin Wolf     if (ret < 0) {
2069797ac58cSKevin Wolf         printf("Could not resume request: %s\n", strerror(-ret));
2070797ac58cSKevin Wolf     }
2071797ac58cSKevin Wolf }
2072797ac58cSKevin Wolf 
2073797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = {
2074797ac58cSKevin Wolf        .name           = "resume",
2075797ac58cSKevin Wolf        .argmin         = 1,
2076797ac58cSKevin Wolf        .argmax         = 1,
2077797ac58cSKevin Wolf        .cfunc          = resume_f,
2078797ac58cSKevin Wolf        .args           = "tag",
2079797ac58cSKevin Wolf        .oneline        = "resumes the request tagged as tag",
2080797ac58cSKevin Wolf };
2081797ac58cSKevin Wolf 
2082*b444d0e9SMax Reitz static void wait_break_f(BlockBackend *blk, int argc, char **argv)
2083797ac58cSKevin Wolf {
20844c7b7e9bSMax Reitz     while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
20854c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
2086797ac58cSKevin Wolf     }
2087797ac58cSKevin Wolf }
2088797ac58cSKevin Wolf 
2089797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = {
2090797ac58cSKevin Wolf        .name           = "wait_break",
2091797ac58cSKevin Wolf        .argmin         = 1,
2092797ac58cSKevin Wolf        .argmax         = 1,
2093797ac58cSKevin Wolf        .cfunc          = wait_break_f,
2094797ac58cSKevin Wolf        .args           = "tag",
2095797ac58cSKevin Wolf        .oneline        = "waits for the suspension of a request",
2096797ac58cSKevin Wolf };
2097797ac58cSKevin Wolf 
2098*b444d0e9SMax Reitz static void abort_f(BlockBackend *blk, int argc, char **argv)
2099797ac58cSKevin Wolf {
2100797ac58cSKevin Wolf     abort();
2101797ac58cSKevin Wolf }
2102797ac58cSKevin Wolf 
2103797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = {
2104797ac58cSKevin Wolf        .name           = "abort",
2105797ac58cSKevin Wolf        .cfunc          = abort_f,
2106797ac58cSKevin Wolf        .flags          = CMD_NOFILE_OK,
2107797ac58cSKevin Wolf        .oneline        = "simulate a program crash using abort(3)",
2108797ac58cSKevin Wolf };
2109797ac58cSKevin Wolf 
21100e82dc7bSMax Reitz static void sigraise_help(void)
21110e82dc7bSMax Reitz {
21120e82dc7bSMax Reitz     printf(
21130e82dc7bSMax Reitz "\n"
21140e82dc7bSMax Reitz " raises the given signal\n"
21150e82dc7bSMax Reitz "\n"
21160e82dc7bSMax Reitz " Example:\n"
21170e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n"
21180e82dc7bSMax Reitz "\n"
21190e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
21200e82dc7bSMax Reitz " given to sigraise.\n"
21210e82dc7bSMax Reitz "\n", SIGTERM);
21220e82dc7bSMax Reitz }
21230e82dc7bSMax Reitz 
2124*b444d0e9SMax Reitz static void sigraise_f(BlockBackend *blk, int argc, char **argv);
21250e82dc7bSMax Reitz 
21260e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = {
21270e82dc7bSMax Reitz     .name       = "sigraise",
21280e82dc7bSMax Reitz     .cfunc      = sigraise_f,
21290e82dc7bSMax Reitz     .argmin     = 1,
21300e82dc7bSMax Reitz     .argmax     = 1,
21310e82dc7bSMax Reitz     .flags      = CMD_NOFILE_OK,
21320e82dc7bSMax Reitz     .args       = "signal",
21330e82dc7bSMax Reitz     .oneline    = "raises a signal",
21340e82dc7bSMax Reitz     .help       = sigraise_help,
21350e82dc7bSMax Reitz };
21360e82dc7bSMax Reitz 
2137*b444d0e9SMax Reitz static void sigraise_f(BlockBackend *blk, int argc, char **argv)
21380e82dc7bSMax Reitz {
21399b0beaf3SJohn Snow     int64_t sig = cvtnum(argv[1]);
21400e82dc7bSMax Reitz     if (sig < 0) {
2141a9ecfa00SJohn Snow         print_cvtnum_err(sig, argv[1]);
2142*b444d0e9SMax Reitz         return;
21439b0beaf3SJohn Snow     } else if (sig > NSIG) {
21449b0beaf3SJohn Snow         printf("signal argument '%s' is too large to be a valid signal\n",
21459b0beaf3SJohn Snow                argv[1]);
2146*b444d0e9SMax Reitz         return;
21470e82dc7bSMax Reitz     }
21480e82dc7bSMax Reitz 
21490e82dc7bSMax Reitz     /* Using raise() to kill this process does not necessarily flush all open
21500e82dc7bSMax Reitz      * streams. At least stdout and stderr (although the latter should be
21510e82dc7bSMax Reitz      * non-buffered anyway) should be flushed, though. */
21520e82dc7bSMax Reitz     fflush(stdout);
21530e82dc7bSMax Reitz     fflush(stderr);
21540e82dc7bSMax Reitz 
21550e82dc7bSMax Reitz     raise(sig);
21560e82dc7bSMax Reitz }
21570e82dc7bSMax Reitz 
2158cd33d02aSKevin Wolf static void sleep_cb(void *opaque)
2159cd33d02aSKevin Wolf {
2160cd33d02aSKevin Wolf     bool *expired = opaque;
2161cd33d02aSKevin Wolf     *expired = true;
2162cd33d02aSKevin Wolf }
2163cd33d02aSKevin Wolf 
2164*b444d0e9SMax Reitz static void sleep_f(BlockBackend *blk, int argc, char **argv)
2165cd33d02aSKevin Wolf {
2166cd33d02aSKevin Wolf     char *endptr;
2167cd33d02aSKevin Wolf     long ms;
2168cd33d02aSKevin Wolf     struct QEMUTimer *timer;
2169cd33d02aSKevin Wolf     bool expired = false;
2170cd33d02aSKevin Wolf 
2171cd33d02aSKevin Wolf     ms = strtol(argv[1], &endptr, 0);
2172cd33d02aSKevin Wolf     if (ms < 0 || *endptr != '\0') {
2173cd33d02aSKevin Wolf         printf("%s is not a valid number\n", argv[1]);
2174*b444d0e9SMax Reitz         return;
2175cd33d02aSKevin Wolf     }
2176cd33d02aSKevin Wolf 
2177cd33d02aSKevin Wolf     timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2178cd33d02aSKevin Wolf     timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2179cd33d02aSKevin Wolf 
2180cd33d02aSKevin Wolf     while (!expired) {
2181cd33d02aSKevin Wolf         main_loop_wait(false);
2182cd33d02aSKevin Wolf     }
2183cd33d02aSKevin Wolf 
2184cd33d02aSKevin Wolf     timer_free(timer);
2185cd33d02aSKevin Wolf }
2186cd33d02aSKevin Wolf 
2187cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = {
2188cd33d02aSKevin Wolf        .name           = "sleep",
2189cd33d02aSKevin Wolf        .argmin         = 1,
2190cd33d02aSKevin Wolf        .argmax         = 1,
2191cd33d02aSKevin Wolf        .cfunc          = sleep_f,
2192cd33d02aSKevin Wolf        .flags          = CMD_NOFILE_OK,
2193cd33d02aSKevin Wolf        .oneline        = "waits for the given value in milliseconds",
2194cd33d02aSKevin Wolf };
2195cd33d02aSKevin Wolf 
2196f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2197f18a834aSKevin Wolf {
2198f18a834aSKevin Wolf     if (cmd) {
2199f18a834aSKevin Wolf         printf("%s ", cmd);
2200f18a834aSKevin Wolf     } else {
2201f18a834aSKevin Wolf         printf("%s ", ct->name);
2202f18a834aSKevin Wolf         if (ct->altname) {
2203f18a834aSKevin Wolf             printf("(or %s) ", ct->altname);
2204f18a834aSKevin Wolf         }
2205f18a834aSKevin Wolf     }
2206f18a834aSKevin Wolf 
2207f18a834aSKevin Wolf     if (ct->args) {
2208f18a834aSKevin Wolf         printf("%s ", ct->args);
2209f18a834aSKevin Wolf     }
2210f18a834aSKevin Wolf     printf("-- %s\n", ct->oneline);
2211f18a834aSKevin Wolf }
2212f18a834aSKevin Wolf 
2213f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2214f18a834aSKevin Wolf {
2215f18a834aSKevin Wolf     help_oneline(cmd, ct);
2216f18a834aSKevin Wolf     if (ct->help) {
2217f18a834aSKevin Wolf         ct->help();
2218f18a834aSKevin Wolf     }
2219f18a834aSKevin Wolf }
2220f18a834aSKevin Wolf 
2221f18a834aSKevin Wolf static void help_all(void)
2222f18a834aSKevin Wolf {
2223f18a834aSKevin Wolf     const cmdinfo_t *ct;
2224f18a834aSKevin Wolf 
2225f18a834aSKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2226f18a834aSKevin Wolf         help_oneline(ct->name, ct);
2227f18a834aSKevin Wolf     }
2228f18a834aSKevin Wolf     printf("\nUse 'help commandname' for extended help.\n");
2229f18a834aSKevin Wolf }
2230f18a834aSKevin Wolf 
2231*b444d0e9SMax Reitz static void help_f(BlockBackend *blk, int argc, char **argv)
2232f18a834aSKevin Wolf {
2233f18a834aSKevin Wolf     const cmdinfo_t *ct;
2234f18a834aSKevin Wolf 
2235f18a834aSKevin Wolf     if (argc == 1) {
2236f18a834aSKevin Wolf         help_all();
2237*b444d0e9SMax Reitz         return;
2238f18a834aSKevin Wolf     }
2239f18a834aSKevin Wolf 
2240f18a834aSKevin Wolf     ct = find_command(argv[1]);
2241f18a834aSKevin Wolf     if (ct == NULL) {
2242f18a834aSKevin Wolf         printf("command %s not found\n", argv[1]);
2243*b444d0e9SMax Reitz         return;
2244f18a834aSKevin Wolf     }
2245f18a834aSKevin Wolf 
2246f18a834aSKevin Wolf     help_onecmd(argv[1], ct);
2247f18a834aSKevin Wolf }
2248f18a834aSKevin Wolf 
2249f18a834aSKevin Wolf static const cmdinfo_t help_cmd = {
2250f18a834aSKevin Wolf     .name       = "help",
2251f18a834aSKevin Wolf     .altname    = "?",
2252f18a834aSKevin Wolf     .cfunc      = help_f,
2253f18a834aSKevin Wolf     .argmin     = 0,
2254f18a834aSKevin Wolf     .argmax     = 1,
2255f18a834aSKevin Wolf     .flags      = CMD_FLAG_GLOBAL,
2256f18a834aSKevin Wolf     .args       = "[command]",
2257f18a834aSKevin Wolf     .oneline    = "help for one or all commands",
2258f18a834aSKevin Wolf };
2259f18a834aSKevin Wolf 
2260*b444d0e9SMax Reitz void qemuio_command(BlockBackend *blk, const char *cmd)
2261dd583296SKevin Wolf {
226215afd94aSPaolo Bonzini     AioContext *ctx;
2263dd583296SKevin Wolf     char *input;
2264dd583296SKevin Wolf     const cmdinfo_t *ct;
2265dd583296SKevin Wolf     char **v;
2266dd583296SKevin Wolf     int c;
2267dd583296SKevin Wolf 
2268dd583296SKevin Wolf     input = g_strdup(cmd);
2269dd583296SKevin Wolf     v = breakline(input, &c);
2270dd583296SKevin Wolf     if (c) {
2271dd583296SKevin Wolf         ct = find_command(v[0]);
2272dd583296SKevin Wolf         if (ct) {
227315afd94aSPaolo Bonzini             ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
227415afd94aSPaolo Bonzini             aio_context_acquire(ctx);
2275*b444d0e9SMax Reitz             command(blk, ct, c, v);
227615afd94aSPaolo Bonzini             aio_context_release(ctx);
2277dd583296SKevin Wolf         } else {
2278dd583296SKevin Wolf             fprintf(stderr, "command \"%s\" not found\n", v[0]);
2279dd583296SKevin Wolf         }
2280dd583296SKevin Wolf     }
2281dd583296SKevin Wolf     g_free(input);
2282dd583296SKevin Wolf     g_free(v);
2283dd583296SKevin Wolf }
2284dd583296SKevin Wolf 
2285797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void)
2286797ac58cSKevin Wolf {
2287797ac58cSKevin Wolf     /* initialize commands */
2288c2cdf5c5SKevin Wolf     qemuio_add_command(&help_cmd);
2289c2cdf5c5SKevin Wolf     qemuio_add_command(&read_cmd);
2290c2cdf5c5SKevin Wolf     qemuio_add_command(&readv_cmd);
2291c2cdf5c5SKevin Wolf     qemuio_add_command(&write_cmd);
2292c2cdf5c5SKevin Wolf     qemuio_add_command(&writev_cmd);
2293c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_read_cmd);
2294c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_write_cmd);
2295c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_flush_cmd);
2296c2cdf5c5SKevin Wolf     qemuio_add_command(&flush_cmd);
2297c2cdf5c5SKevin Wolf     qemuio_add_command(&truncate_cmd);
2298c2cdf5c5SKevin Wolf     qemuio_add_command(&length_cmd);
2299c2cdf5c5SKevin Wolf     qemuio_add_command(&info_cmd);
2300c2cdf5c5SKevin Wolf     qemuio_add_command(&discard_cmd);
2301c2cdf5c5SKevin Wolf     qemuio_add_command(&alloc_cmd);
2302c2cdf5c5SKevin Wolf     qemuio_add_command(&map_cmd);
23035bbd2e59SKevin Wolf     qemuio_add_command(&reopen_cmd);
2304c2cdf5c5SKevin Wolf     qemuio_add_command(&break_cmd);
23054cc70e93SFam Zheng     qemuio_add_command(&remove_break_cmd);
2306c2cdf5c5SKevin Wolf     qemuio_add_command(&resume_cmd);
2307c2cdf5c5SKevin Wolf     qemuio_add_command(&wait_break_cmd);
2308c2cdf5c5SKevin Wolf     qemuio_add_command(&abort_cmd);
2309cd33d02aSKevin Wolf     qemuio_add_command(&sleep_cmd);
23100e82dc7bSMax Reitz     qemuio_add_command(&sigraise_cmd);
2311797ac58cSKevin Wolf }
2312