xref: /qemu/qemu-io-cmds.c (revision dc900c35)
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"
13*dc900c35SAlberto Garcia #include "qapi/qmp/qdict.h"
143d21994fSKevin Wolf #include "qemu-io.h"
154c7b7e9bSMax Reitz #include "sysemu/block-backend.h"
164c7b7e9bSMax Reitz #include "block/block.h"
174c7b7e9bSMax Reitz #include "block/block_int.h" /* for info_f() */
18a8d8ecb7SMax Reitz #include "block/qapi.h"
19d49b6836SMarkus Armbruster #include "qemu/error-report.h"
206a1751b7SAlex Bligh #include "qemu/main-loop.h"
21922a01a0SMarkus Armbruster #include "qemu/option.h"
22cd33d02aSKevin Wolf #include "qemu/timer.h"
23f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
24797ac58cSKevin Wolf 
25797ac58cSKevin Wolf #define CMD_NOFILE_OK   0x01
26797ac58cSKevin Wolf 
27f9883880SStefan Weil bool qemuio_misalign;
28797ac58cSKevin Wolf 
29c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab;
30c2cdf5c5SKevin Wolf static int ncmds;
31c2cdf5c5SKevin Wolf 
32c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b)
33c2cdf5c5SKevin Wolf {
34c2cdf5c5SKevin Wolf     return strcmp(((const cmdinfo_t *)a)->name,
35c2cdf5c5SKevin Wolf                   ((const cmdinfo_t *)b)->name);
36c2cdf5c5SKevin Wolf }
37c2cdf5c5SKevin Wolf 
38c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci)
39c2cdf5c5SKevin Wolf {
406aabeb58SPeter Maydell     /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
416aabeb58SPeter Maydell      * flags allow it not to be, so that combination is invalid.
426aabeb58SPeter Maydell      * Catch it now rather than letting it manifest as a crash if a
436aabeb58SPeter Maydell      * particular set of command line options are used.
446aabeb58SPeter Maydell      */
456aabeb58SPeter Maydell     assert(ci->perm == 0 ||
466aabeb58SPeter Maydell            (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
4702c4f26bSMarkus Armbruster     cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
48c2cdf5c5SKevin Wolf     cmdtab[ncmds - 1] = *ci;
49c2cdf5c5SKevin Wolf     qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
50c2cdf5c5SKevin Wolf }
51c2cdf5c5SKevin Wolf 
52b444d0e9SMax Reitz void qemuio_command_usage(const cmdinfo_t *ci)
53c2cdf5c5SKevin Wolf {
54c2cdf5c5SKevin Wolf     printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
55c2cdf5c5SKevin Wolf }
56c2cdf5c5SKevin Wolf 
574c7b7e9bSMax Reitz static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
58c2cdf5c5SKevin Wolf {
59c2cdf5c5SKevin Wolf     if (ct->flags & CMD_FLAG_GLOBAL) {
60c2cdf5c5SKevin Wolf         return 1;
61c2cdf5c5SKevin Wolf     }
624c7b7e9bSMax Reitz     if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
63c2cdf5c5SKevin Wolf         fprintf(stderr, "no file open, try 'help open'\n");
64c2cdf5c5SKevin Wolf         return 0;
65c2cdf5c5SKevin Wolf     }
66c2cdf5c5SKevin Wolf     return 1;
67c2cdf5c5SKevin Wolf }
68c2cdf5c5SKevin Wolf 
69b32d7a39SMax Reitz static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
703d21994fSKevin Wolf                    char **argv)
71c2cdf5c5SKevin Wolf {
72c2cdf5c5SKevin Wolf     char *cmd = argv[0];
73c2cdf5c5SKevin Wolf 
744c7b7e9bSMax Reitz     if (!init_check_command(blk, ct)) {
75b32d7a39SMax Reitz         return -EINVAL;
76c2cdf5c5SKevin Wolf     }
77c2cdf5c5SKevin Wolf 
78c2cdf5c5SKevin Wolf     if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
79c2cdf5c5SKevin Wolf         if (ct->argmax == -1) {
80c2cdf5c5SKevin Wolf             fprintf(stderr,
81c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected at least %d arguments\n",
82c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
83c2cdf5c5SKevin Wolf         } else if (ct->argmin == ct->argmax) {
84c2cdf5c5SKevin Wolf             fprintf(stderr,
85c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected %d arguments\n",
86c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
87c2cdf5c5SKevin Wolf         } else {
88c2cdf5c5SKevin Wolf             fprintf(stderr,
89c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected between %d and %d arguments\n",
90c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin, ct->argmax);
91c2cdf5c5SKevin Wolf         }
92b32d7a39SMax Reitz         return -EINVAL;
93c2cdf5c5SKevin Wolf     }
94887354bdSKevin Wolf 
95887354bdSKevin Wolf     /* Request additional permissions if necessary for this command. The caller
96887354bdSKevin Wolf      * is responsible for restoring the original permissions afterwards if this
97887354bdSKevin Wolf      * is what it wants. */
98887354bdSKevin Wolf     if (ct->perm && blk_is_available(blk)) {
99887354bdSKevin Wolf         uint64_t orig_perm, orig_shared_perm;
100887354bdSKevin Wolf         blk_get_perm(blk, &orig_perm, &orig_shared_perm);
101887354bdSKevin Wolf 
102887354bdSKevin Wolf         if (ct->perm & ~orig_perm) {
103887354bdSKevin Wolf             uint64_t new_perm;
104887354bdSKevin Wolf             Error *local_err = NULL;
105887354bdSKevin Wolf             int ret;
106887354bdSKevin Wolf 
107887354bdSKevin Wolf             new_perm = orig_perm | ct->perm;
108887354bdSKevin Wolf 
109887354bdSKevin Wolf             ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
110887354bdSKevin Wolf             if (ret < 0) {
111887354bdSKevin Wolf                 error_report_err(local_err);
112b32d7a39SMax Reitz                 return ret;
113887354bdSKevin Wolf             }
114887354bdSKevin Wolf         }
115887354bdSKevin Wolf     }
116887354bdSKevin Wolf 
117c2cdf5c5SKevin Wolf     optind = 0;
118b32d7a39SMax Reitz     return ct->cfunc(blk, argc, argv);
119c2cdf5c5SKevin Wolf }
120c2cdf5c5SKevin Wolf 
121c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd)
122c2cdf5c5SKevin Wolf {
123c2cdf5c5SKevin Wolf     cmdinfo_t *ct;
124c2cdf5c5SKevin Wolf 
125c2cdf5c5SKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
126c2cdf5c5SKevin Wolf         if (strcmp(ct->name, cmd) == 0 ||
127c2cdf5c5SKevin Wolf             (ct->altname && strcmp(ct->altname, cmd) == 0))
128c2cdf5c5SKevin Wolf         {
129c2cdf5c5SKevin Wolf             return (const cmdinfo_t *)ct;
130c2cdf5c5SKevin Wolf         }
131c2cdf5c5SKevin Wolf     }
132c2cdf5c5SKevin Wolf     return NULL;
133c2cdf5c5SKevin Wolf }
134c2cdf5c5SKevin Wolf 
1354694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */
1364694020dSStefan Hajnoczi void qemuio_complete_command(const char *input,
1374694020dSStefan Hajnoczi                              void (*fn)(const char *cmd, void *opaque),
1384694020dSStefan Hajnoczi                              void *opaque)
1394694020dSStefan Hajnoczi {
1404694020dSStefan Hajnoczi     cmdinfo_t *ct;
1414694020dSStefan Hajnoczi     size_t input_len = strlen(input);
1424694020dSStefan Hajnoczi 
1434694020dSStefan Hajnoczi     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
1444694020dSStefan Hajnoczi         if (strncmp(input, ct->name, input_len) == 0) {
1454694020dSStefan Hajnoczi             fn(ct->name, opaque);
1464694020dSStefan Hajnoczi         }
1474694020dSStefan Hajnoczi     }
1484694020dSStefan Hajnoczi }
1494694020dSStefan Hajnoczi 
150c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count)
151c2cdf5c5SKevin Wolf {
152c2cdf5c5SKevin Wolf     int c = 0;
153c2cdf5c5SKevin Wolf     char *p;
1545839e53bSMarkus Armbruster     char **rval = g_new0(char *, 1);
155c2cdf5c5SKevin Wolf 
156c2cdf5c5SKevin Wolf     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
157c2cdf5c5SKevin Wolf         if (!*p) {
158c2cdf5c5SKevin Wolf             continue;
159c2cdf5c5SKevin Wolf         }
160c2cdf5c5SKevin Wolf         c++;
16108193dd5SMarkus Armbruster         rval = g_renew(char *, rval, (c + 1));
162c2cdf5c5SKevin Wolf         rval[c - 1] = p;
163c2cdf5c5SKevin Wolf         rval[c] = NULL;
164c2cdf5c5SKevin Wolf     }
165c2cdf5c5SKevin Wolf     *count = c;
166c2cdf5c5SKevin Wolf     return rval;
167c2cdf5c5SKevin Wolf }
168c2cdf5c5SKevin Wolf 
169797ac58cSKevin Wolf static int64_t cvtnum(const char *s)
170797ac58cSKevin Wolf {
171f17fd4fdSMarkus Armbruster     int err;
172f46bfdbfSMarkus Armbruster     uint64_t value;
173ef5a7885SJohn Snow 
174f17fd4fdSMarkus Armbruster     err = qemu_strtosz(s, NULL, &value);
175f17fd4fdSMarkus Armbruster     if (err < 0) {
176f17fd4fdSMarkus Armbruster         return err;
177f17fd4fdSMarkus Armbruster     }
178f46bfdbfSMarkus Armbruster     if (value > INT64_MAX) {
179f46bfdbfSMarkus Armbruster         return -ERANGE;
180f46bfdbfSMarkus Armbruster     }
181f17fd4fdSMarkus Armbruster     return value;
182797ac58cSKevin Wolf }
183797ac58cSKevin Wolf 
184a9ecfa00SJohn Snow static void print_cvtnum_err(int64_t rc, const char *arg)
185a9ecfa00SJohn Snow {
186a9ecfa00SJohn Snow     switch (rc) {
187a9ecfa00SJohn Snow     case -EINVAL:
188a9ecfa00SJohn Snow         printf("Parsing error: non-numeric argument,"
189a9ecfa00SJohn Snow                " or extraneous/unrecognized suffix -- %s\n", arg);
190a9ecfa00SJohn Snow         break;
191a9ecfa00SJohn Snow     case -ERANGE:
192a9ecfa00SJohn Snow         printf("Parsing error: argument too large -- %s\n", arg);
193a9ecfa00SJohn Snow         break;
194a9ecfa00SJohn Snow     default:
195a9ecfa00SJohn Snow         printf("Parsing error: %s\n", arg);
196a9ecfa00SJohn Snow     }
197a9ecfa00SJohn Snow }
198a9ecfa00SJohn Snow 
1990b613881SKevin Wolf #define EXABYTES(x)     ((long long)(x) << 60)
2000b613881SKevin Wolf #define PETABYTES(x)    ((long long)(x) << 50)
2010b613881SKevin Wolf #define TERABYTES(x)    ((long long)(x) << 40)
2020b613881SKevin Wolf #define GIGABYTES(x)    ((long long)(x) << 30)
2030b613881SKevin Wolf #define MEGABYTES(x)    ((long long)(x) << 20)
2040b613881SKevin Wolf #define KILOBYTES(x)    ((long long)(x) << 10)
2050b613881SKevin Wolf 
2060b613881SKevin Wolf #define TO_EXABYTES(x)  ((x) / EXABYTES(1))
2070b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1))
2080b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1))
2090b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
2100b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
2110b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
2120b613881SKevin Wolf 
2130b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size)
2140b613881SKevin Wolf {
2150b613881SKevin Wolf     char *trim;
2160b613881SKevin Wolf     const char *suffix;
2170b613881SKevin Wolf 
2180b613881SKevin Wolf     if (value >= EXABYTES(1)) {
2190b613881SKevin Wolf         suffix = " EiB";
2200b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
2210b613881SKevin Wolf     } else if (value >= PETABYTES(1)) {
2220b613881SKevin Wolf         suffix = " PiB";
2230b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
2240b613881SKevin Wolf     } else if (value >= TERABYTES(1)) {
2250b613881SKevin Wolf         suffix = " TiB";
2260b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
2270b613881SKevin Wolf     } else if (value >= GIGABYTES(1)) {
2280b613881SKevin Wolf         suffix = " GiB";
2290b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
2300b613881SKevin Wolf     } else if (value >= MEGABYTES(1)) {
2310b613881SKevin Wolf         suffix = " MiB";
2320b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
2330b613881SKevin Wolf     } else if (value >= KILOBYTES(1)) {
2340b613881SKevin Wolf         suffix = " KiB";
2350b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
2360b613881SKevin Wolf     } else {
2370b613881SKevin Wolf         suffix = " bytes";
2380b613881SKevin Wolf         snprintf(str, size - 6, "%f", value);
2390b613881SKevin Wolf     }
2400b613881SKevin Wolf 
2410b613881SKevin Wolf     trim = strstr(str, ".000");
2420b613881SKevin Wolf     if (trim) {
2430b613881SKevin Wolf         strcpy(trim, suffix);
2440b613881SKevin Wolf     } else {
2450b613881SKevin Wolf         strcat(str, suffix);
2460b613881SKevin Wolf     }
2470b613881SKevin Wolf }
2480b613881SKevin Wolf 
2490b613881SKevin Wolf 
2500b613881SKevin Wolf 
2510b613881SKevin Wolf static struct timeval tsub(struct timeval t1, struct timeval t2)
2520b613881SKevin Wolf {
2530b613881SKevin Wolf     t1.tv_usec -= t2.tv_usec;
2540b613881SKevin Wolf     if (t1.tv_usec < 0) {
2550b613881SKevin Wolf         t1.tv_usec += 1000000;
2560b613881SKevin Wolf         t1.tv_sec--;
2570b613881SKevin Wolf     }
2580b613881SKevin Wolf     t1.tv_sec -= t2.tv_sec;
2590b613881SKevin Wolf     return t1;
2600b613881SKevin Wolf }
2610b613881SKevin Wolf 
2620b613881SKevin Wolf static double tdiv(double value, struct timeval tv)
2630b613881SKevin Wolf {
2640b613881SKevin Wolf     return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
2650b613881SKevin Wolf }
2660b613881SKevin Wolf 
2670b613881SKevin Wolf #define HOURS(sec)      ((sec) / (60 * 60))
2680b613881SKevin Wolf #define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
2690b613881SKevin Wolf #define SECONDS(sec)    ((sec) % 60)
2700b613881SKevin Wolf 
2710b613881SKevin Wolf enum {
2720b613881SKevin Wolf     DEFAULT_TIME        = 0x0,
2730b613881SKevin Wolf     TERSE_FIXED_TIME    = 0x1,
2740b613881SKevin Wolf     VERBOSE_FIXED_TIME  = 0x2,
2750b613881SKevin Wolf };
2760b613881SKevin Wolf 
2770b613881SKevin Wolf static void timestr(struct timeval *tv, char *ts, size_t size, int format)
2780b613881SKevin Wolf {
2790b613881SKevin Wolf     double usec = (double)tv->tv_usec / 1000000.0;
2800b613881SKevin Wolf 
2810b613881SKevin Wolf     if (format & TERSE_FIXED_TIME) {
2820b613881SKevin Wolf         if (!HOURS(tv->tv_sec)) {
2830b613881SKevin Wolf             snprintf(ts, size, "%u:%02u.%02u",
2840b613881SKevin Wolf                     (unsigned int) MINUTES(tv->tv_sec),
2850b613881SKevin Wolf                     (unsigned int) SECONDS(tv->tv_sec),
2860b613881SKevin Wolf                     (unsigned int) (usec * 100));
2870b613881SKevin Wolf             return;
2880b613881SKevin Wolf         }
2890b613881SKevin Wolf         format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
2900b613881SKevin Wolf     }
2910b613881SKevin Wolf 
2920b613881SKevin Wolf     if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
2930b613881SKevin Wolf         snprintf(ts, size, "%u:%02u:%02u.%02u",
2940b613881SKevin Wolf                 (unsigned int) HOURS(tv->tv_sec),
2950b613881SKevin Wolf                 (unsigned int) MINUTES(tv->tv_sec),
2960b613881SKevin Wolf                 (unsigned int) SECONDS(tv->tv_sec),
2970b613881SKevin Wolf                 (unsigned int) (usec * 100));
2980b613881SKevin Wolf     } else {
2990b613881SKevin Wolf         snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
3000b613881SKevin Wolf     }
3010b613881SKevin Wolf }
3020b613881SKevin Wolf 
303797ac58cSKevin Wolf /*
304797ac58cSKevin Wolf  * Parse the pattern argument to various sub-commands.
305797ac58cSKevin Wolf  *
306797ac58cSKevin Wolf  * Because the pattern is used as an argument to memset it must evaluate
307797ac58cSKevin Wolf  * to an unsigned integer that fits into a single byte.
308797ac58cSKevin Wolf  */
309797ac58cSKevin Wolf static int parse_pattern(const char *arg)
310797ac58cSKevin Wolf {
311797ac58cSKevin Wolf     char *endptr = NULL;
312797ac58cSKevin Wolf     long pattern;
313797ac58cSKevin Wolf 
314797ac58cSKevin Wolf     pattern = strtol(arg, &endptr, 0);
315797ac58cSKevin Wolf     if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
316797ac58cSKevin Wolf         printf("%s is not a valid pattern byte\n", arg);
317797ac58cSKevin Wolf         return -1;
318797ac58cSKevin Wolf     }
319797ac58cSKevin Wolf 
320797ac58cSKevin Wolf     return pattern;
321797ac58cSKevin Wolf }
322797ac58cSKevin Wolf 
323797ac58cSKevin Wolf /*
324797ac58cSKevin Wolf  * Memory allocation helpers.
325797ac58cSKevin Wolf  *
326797ac58cSKevin Wolf  * Make sure memory is aligned by default, or purposefully misaligned if
327797ac58cSKevin Wolf  * that is specified on the command line.
328797ac58cSKevin Wolf  */
329797ac58cSKevin Wolf 
330797ac58cSKevin Wolf #define MISALIGN_OFFSET     16
3314c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
332797ac58cSKevin Wolf {
333797ac58cSKevin Wolf     void *buf;
334797ac58cSKevin Wolf 
335797ac58cSKevin Wolf     if (qemuio_misalign) {
336797ac58cSKevin Wolf         len += MISALIGN_OFFSET;
337797ac58cSKevin Wolf     }
3384c7b7e9bSMax Reitz     buf = blk_blockalign(blk, len);
339797ac58cSKevin Wolf     memset(buf, pattern, len);
340797ac58cSKevin Wolf     if (qemuio_misalign) {
341797ac58cSKevin Wolf         buf += MISALIGN_OFFSET;
342797ac58cSKevin Wolf     }
343797ac58cSKevin Wolf     return buf;
344797ac58cSKevin Wolf }
345797ac58cSKevin Wolf 
346797ac58cSKevin Wolf static void qemu_io_free(void *p)
347797ac58cSKevin Wolf {
348797ac58cSKevin Wolf     if (qemuio_misalign) {
349797ac58cSKevin Wolf         p -= MISALIGN_OFFSET;
350797ac58cSKevin Wolf     }
351797ac58cSKevin Wolf     qemu_vfree(p);
352797ac58cSKevin Wolf }
353797ac58cSKevin Wolf 
3549b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
355797ac58cSKevin Wolf {
3569b0beaf3SJohn Snow     uint64_t i;
3579b0beaf3SJohn Snow     int j;
358797ac58cSKevin Wolf     const uint8_t *p;
359797ac58cSKevin Wolf 
360797ac58cSKevin Wolf     for (i = 0, p = buffer; i < len; i += 16) {
361797ac58cSKevin Wolf         const uint8_t *s = p;
362797ac58cSKevin Wolf 
363797ac58cSKevin Wolf         printf("%08" PRIx64 ":  ", offset + i);
364797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, p++) {
365797ac58cSKevin Wolf             printf("%02x ", *p);
366797ac58cSKevin Wolf         }
367797ac58cSKevin Wolf         printf(" ");
368797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, s++) {
369797ac58cSKevin Wolf             if (isalnum(*s)) {
370797ac58cSKevin Wolf                 printf("%c", *s);
371797ac58cSKevin Wolf             } else {
372797ac58cSKevin Wolf                 printf(".");
373797ac58cSKevin Wolf             }
374797ac58cSKevin Wolf         }
375797ac58cSKevin Wolf         printf("\n");
376797ac58cSKevin Wolf     }
377797ac58cSKevin Wolf }
378797ac58cSKevin Wolf 
379797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset,
380dc38852aSEric Blake                          int64_t count, int64_t total, int cnt, bool Cflag)
381797ac58cSKevin Wolf {
382797ac58cSKevin Wolf     char s1[64], s2[64], ts[64];
383797ac58cSKevin Wolf 
384797ac58cSKevin Wolf     timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
385797ac58cSKevin Wolf     if (!Cflag) {
386797ac58cSKevin Wolf         cvtstr((double)total, s1, sizeof(s1));
387797ac58cSKevin Wolf         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
3889b0beaf3SJohn Snow         printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
389797ac58cSKevin Wolf                op, total, count, offset);
390797ac58cSKevin Wolf         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
391797ac58cSKevin Wolf                s1, cnt, ts, s2, tdiv((double)cnt, *t));
392797ac58cSKevin Wolf     } else {/* bytes,ops,time,bytes/sec,ops/sec */
3939b0beaf3SJohn Snow         printf("%"PRId64",%d,%s,%.3f,%.3f\n",
394797ac58cSKevin Wolf             total, cnt, ts,
395797ac58cSKevin Wolf             tdiv((double)total, *t),
396797ac58cSKevin Wolf             tdiv((double)cnt, *t));
397797ac58cSKevin Wolf     }
398797ac58cSKevin Wolf }
399797ac58cSKevin Wolf 
400797ac58cSKevin Wolf /*
401797ac58cSKevin Wolf  * Parse multiple length statements for vectored I/O, and construct an I/O
402797ac58cSKevin Wolf  * vector matching it.
403797ac58cSKevin Wolf  */
404797ac58cSKevin Wolf static void *
4054c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
406797ac58cSKevin Wolf              int pattern)
407797ac58cSKevin Wolf {
408797ac58cSKevin Wolf     size_t *sizes = g_new0(size_t, nr_iov);
409797ac58cSKevin Wolf     size_t count = 0;
410797ac58cSKevin Wolf     void *buf = NULL;
411797ac58cSKevin Wolf     void *p;
412797ac58cSKevin Wolf     int i;
413797ac58cSKevin Wolf 
414797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
415797ac58cSKevin Wolf         char *arg = argv[i];
416797ac58cSKevin Wolf         int64_t len;
417797ac58cSKevin Wolf 
418797ac58cSKevin Wolf         len = cvtnum(arg);
419797ac58cSKevin Wolf         if (len < 0) {
420a9ecfa00SJohn Snow             print_cvtnum_err(len, arg);
421797ac58cSKevin Wolf             goto fail;
422797ac58cSKevin Wolf         }
423797ac58cSKevin Wolf 
4243026c468SAlberto Garcia         if (len > BDRV_REQUEST_MAX_BYTES) {
4253026c468SAlberto Garcia             printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
4263026c468SAlberto Garcia                    (uint64_t)BDRV_REQUEST_MAX_BYTES);
4273026c468SAlberto Garcia             goto fail;
4283026c468SAlberto Garcia         }
4293026c468SAlberto Garcia 
4303026c468SAlberto Garcia         if (count > BDRV_REQUEST_MAX_BYTES - len) {
4313026c468SAlberto Garcia             printf("The total number of bytes exceed the maximum size %" PRIu64
4323026c468SAlberto Garcia                    "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
433797ac58cSKevin Wolf             goto fail;
434797ac58cSKevin Wolf         }
435797ac58cSKevin Wolf 
436797ac58cSKevin Wolf         sizes[i] = len;
437797ac58cSKevin Wolf         count += len;
438797ac58cSKevin Wolf     }
439797ac58cSKevin Wolf 
440797ac58cSKevin Wolf     qemu_iovec_init(qiov, nr_iov);
441797ac58cSKevin Wolf 
4424c7b7e9bSMax Reitz     buf = p = qemu_io_alloc(blk, count, pattern);
443797ac58cSKevin Wolf 
444797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
445797ac58cSKevin Wolf         qemu_iovec_add(qiov, p, sizes[i]);
446797ac58cSKevin Wolf         p += sizes[i];
447797ac58cSKevin Wolf     }
448797ac58cSKevin Wolf 
449797ac58cSKevin Wolf fail:
450797ac58cSKevin Wolf     g_free(sizes);
451797ac58cSKevin Wolf     return buf;
452797ac58cSKevin Wolf }
453797ac58cSKevin Wolf 
4549b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
455f5a5ca79SManos Pitsidianakis                     int64_t bytes, int64_t *total)
456797ac58cSKevin Wolf {
457f5a5ca79SManos Pitsidianakis     if (bytes > INT_MAX) {
4589b0beaf3SJohn Snow         return -ERANGE;
4599b0beaf3SJohn Snow     }
4609b0beaf3SJohn Snow 
461f5a5ca79SManos Pitsidianakis     *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
462797ac58cSKevin Wolf     if (*total < 0) {
463797ac58cSKevin Wolf         return *total;
464797ac58cSKevin Wolf     }
465797ac58cSKevin Wolf     return 1;
466797ac58cSKevin Wolf }
467797ac58cSKevin Wolf 
4689b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
469f5a5ca79SManos Pitsidianakis                      int64_t bytes, int flags, int64_t *total)
470797ac58cSKevin Wolf {
471f5a5ca79SManos Pitsidianakis     if (bytes > INT_MAX) {
4729b0beaf3SJohn Snow         return -ERANGE;
4739b0beaf3SJohn Snow     }
4749b0beaf3SJohn Snow 
475f5a5ca79SManos Pitsidianakis     *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
476797ac58cSKevin Wolf     if (*total < 0) {
477797ac58cSKevin Wolf         return *total;
478797ac58cSKevin Wolf     }
479797ac58cSKevin Wolf     return 1;
480797ac58cSKevin Wolf }
481797ac58cSKevin Wolf 
482797ac58cSKevin Wolf typedef struct {
4834c7b7e9bSMax Reitz     BlockBackend *blk;
484797ac58cSKevin Wolf     int64_t offset;
485f5a5ca79SManos Pitsidianakis     int64_t bytes;
4869b0beaf3SJohn Snow     int64_t *total;
487770e0e0eSEric Blake     int flags;
488797ac58cSKevin Wolf     int ret;
489797ac58cSKevin Wolf     bool done;
490797ac58cSKevin Wolf } CoWriteZeroes;
491797ac58cSKevin Wolf 
492d004bd52SEric Blake static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
493797ac58cSKevin Wolf {
494797ac58cSKevin Wolf     CoWriteZeroes *data = opaque;
495797ac58cSKevin Wolf 
496f5a5ca79SManos Pitsidianakis     data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
497770e0e0eSEric Blake                                      data->flags);
498797ac58cSKevin Wolf     data->done = true;
499797ac58cSKevin Wolf     if (data->ret < 0) {
500797ac58cSKevin Wolf         *data->total = data->ret;
501797ac58cSKevin Wolf         return;
502797ac58cSKevin Wolf     }
503797ac58cSKevin Wolf 
504f5a5ca79SManos Pitsidianakis     *data->total = data->bytes;
505797ac58cSKevin Wolf }
506797ac58cSKevin Wolf 
507d004bd52SEric Blake static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
508f5a5ca79SManos Pitsidianakis                                int64_t bytes, int flags, int64_t *total)
509797ac58cSKevin Wolf {
510797ac58cSKevin Wolf     Coroutine *co;
511797ac58cSKevin Wolf     CoWriteZeroes data = {
5124c7b7e9bSMax Reitz         .blk    = blk,
513797ac58cSKevin Wolf         .offset = offset,
514f5a5ca79SManos Pitsidianakis         .bytes  = bytes,
515797ac58cSKevin Wolf         .total  = total,
516770e0e0eSEric Blake         .flags  = flags,
517797ac58cSKevin Wolf         .done   = false,
518797ac58cSKevin Wolf     };
519797ac58cSKevin Wolf 
520f5a5ca79SManos Pitsidianakis     if (bytes > INT_MAX) {
5219b0beaf3SJohn Snow         return -ERANGE;
5229b0beaf3SJohn Snow     }
5239b0beaf3SJohn Snow 
5240b8b8753SPaolo Bonzini     co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
525324ec3e4SFam Zheng     bdrv_coroutine_enter(blk_bs(blk), co);
526797ac58cSKevin Wolf     while (!data.done) {
5274c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
528797ac58cSKevin Wolf     }
529797ac58cSKevin Wolf     if (data.ret < 0) {
530797ac58cSKevin Wolf         return data.ret;
531797ac58cSKevin Wolf     } else {
532797ac58cSKevin Wolf         return 1;
533797ac58cSKevin Wolf     }
534797ac58cSKevin Wolf }
535797ac58cSKevin Wolf 
5364c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
537f5a5ca79SManos Pitsidianakis                                int64_t bytes, int64_t *total)
538797ac58cSKevin Wolf {
539797ac58cSKevin Wolf     int ret;
540797ac58cSKevin Wolf 
541f5a5ca79SManos Pitsidianakis     if (bytes >> 9 > BDRV_REQUEST_MAX_SECTORS) {
5429b0beaf3SJohn Snow         return -ERANGE;
5439b0beaf3SJohn Snow     }
5449b0beaf3SJohn Snow 
545f5a5ca79SManos Pitsidianakis     ret = blk_pwrite_compressed(blk, offset, buf, bytes);
546797ac58cSKevin Wolf     if (ret < 0) {
547797ac58cSKevin Wolf         return ret;
548797ac58cSKevin Wolf     }
549f5a5ca79SManos Pitsidianakis     *total = bytes;
550797ac58cSKevin Wolf     return 1;
551797ac58cSKevin Wolf }
552797ac58cSKevin Wolf 
5534c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5549b0beaf3SJohn Snow                            int64_t count, int64_t *total)
555797ac58cSKevin Wolf {
5569b0beaf3SJohn Snow     if (count > INT_MAX) {
5579b0beaf3SJohn Snow         return -ERANGE;
5589b0beaf3SJohn Snow     }
5599b0beaf3SJohn Snow 
5604c7b7e9bSMax Reitz     *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
561797ac58cSKevin Wolf     if (*total < 0) {
562797ac58cSKevin Wolf         return *total;
563797ac58cSKevin Wolf     }
564797ac58cSKevin Wolf     return 1;
565797ac58cSKevin Wolf }
566797ac58cSKevin Wolf 
5674c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5689b0beaf3SJohn Snow                            int64_t count, int64_t *total)
569797ac58cSKevin Wolf {
5709b0beaf3SJohn Snow     if (count > INT_MAX) {
5719b0beaf3SJohn Snow         return -ERANGE;
5729b0beaf3SJohn Snow     }
5739b0beaf3SJohn Snow 
5744c7b7e9bSMax Reitz     *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
575797ac58cSKevin Wolf     if (*total < 0) {
576797ac58cSKevin Wolf         return *total;
577797ac58cSKevin Wolf     }
578797ac58cSKevin Wolf     return 1;
579797ac58cSKevin Wolf }
580797ac58cSKevin Wolf 
581797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff
582797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret)
583797ac58cSKevin Wolf {
584797ac58cSKevin Wolf     *(int *)opaque = ret;
585797ac58cSKevin Wolf }
586797ac58cSKevin Wolf 
5874c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
588797ac58cSKevin Wolf                         int64_t offset, int *total)
589797ac58cSKevin Wolf {
590797ac58cSKevin Wolf     int async_ret = NOT_DONE;
591797ac58cSKevin Wolf 
5927b3f9712SEric Blake     blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
593797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
594797ac58cSKevin Wolf         main_loop_wait(false);
595797ac58cSKevin Wolf     }
596797ac58cSKevin Wolf 
597797ac58cSKevin Wolf     *total = qiov->size;
598797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
599797ac58cSKevin Wolf }
600797ac58cSKevin Wolf 
6014c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
602770e0e0eSEric Blake                          int64_t offset, int flags, int *total)
603797ac58cSKevin Wolf {
604797ac58cSKevin Wolf     int async_ret = NOT_DONE;
605797ac58cSKevin Wolf 
606770e0e0eSEric Blake     blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
607797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
608797ac58cSKevin Wolf         main_loop_wait(false);
609797ac58cSKevin Wolf     }
610797ac58cSKevin Wolf 
611797ac58cSKevin Wolf     *total = qiov->size;
612797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
613797ac58cSKevin Wolf }
614797ac58cSKevin Wolf 
615797ac58cSKevin Wolf static void read_help(void)
616797ac58cSKevin Wolf {
617797ac58cSKevin Wolf     printf(
618797ac58cSKevin Wolf "\n"
619797ac58cSKevin Wolf " reads a range of bytes from the given offset\n"
620797ac58cSKevin Wolf "\n"
621797ac58cSKevin Wolf " Example:\n"
622797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
623797ac58cSKevin Wolf "\n"
624797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
625797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
626797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n"
627797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
628797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n"
629093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
630797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
631797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
632797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n"
633797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
634797ac58cSKevin Wolf "\n");
635797ac58cSKevin Wolf }
636797ac58cSKevin Wolf 
637b32d7a39SMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv);
638797ac58cSKevin Wolf 
639797ac58cSKevin Wolf static const cmdinfo_t read_cmd = {
640797ac58cSKevin Wolf     .name       = "read",
641797ac58cSKevin Wolf     .altname    = "r",
642797ac58cSKevin Wolf     .cfunc      = read_f,
643797ac58cSKevin Wolf     .argmin     = 2,
644797ac58cSKevin Wolf     .argmax     = -1,
645093ea232SEric Blake     .args       = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
646797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
647797ac58cSKevin Wolf     .help       = read_help,
648797ac58cSKevin Wolf };
649797ac58cSKevin Wolf 
650b32d7a39SMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv)
651797ac58cSKevin Wolf {
652797ac58cSKevin Wolf     struct timeval t1, t2;
653093ea232SEric Blake     bool Cflag = false, qflag = false, vflag = false;
654dc38852aSEric Blake     bool Pflag = false, sflag = false, lflag = false, bflag = false;
655b32d7a39SMax Reitz     int c, cnt, ret;
656797ac58cSKevin Wolf     char *buf;
657797ac58cSKevin Wolf     int64_t offset;
6589b0beaf3SJohn Snow     int64_t count;
659797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
6609b0beaf3SJohn Snow     int64_t total = 0;
6619b0beaf3SJohn Snow     int pattern = 0;
6629b0beaf3SJohn Snow     int64_t pattern_offset = 0, pattern_count = 0;
663797ac58cSKevin Wolf 
664b062ad86SEric Blake     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
665797ac58cSKevin Wolf         switch (c) {
666797ac58cSKevin Wolf         case 'b':
667dc38852aSEric Blake             bflag = true;
668797ac58cSKevin Wolf             break;
669797ac58cSKevin Wolf         case 'C':
670dc38852aSEric Blake             Cflag = true;
671797ac58cSKevin Wolf             break;
672797ac58cSKevin Wolf         case 'l':
673dc38852aSEric Blake             lflag = true;
674797ac58cSKevin Wolf             pattern_count = cvtnum(optarg);
675797ac58cSKevin Wolf             if (pattern_count < 0) {
676a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_count, optarg);
677b32d7a39SMax Reitz                 return pattern_count;
678797ac58cSKevin Wolf             }
679797ac58cSKevin Wolf             break;
680797ac58cSKevin Wolf         case 'p':
681093ea232SEric Blake             /* Ignored for backwards compatibility */
682797ac58cSKevin Wolf             break;
683797ac58cSKevin Wolf         case 'P':
684dc38852aSEric Blake             Pflag = true;
685797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
686797ac58cSKevin Wolf             if (pattern < 0) {
687b32d7a39SMax Reitz                 return -EINVAL;
688797ac58cSKevin Wolf             }
689797ac58cSKevin Wolf             break;
690797ac58cSKevin Wolf         case 'q':
691dc38852aSEric Blake             qflag = true;
692797ac58cSKevin Wolf             break;
693797ac58cSKevin Wolf         case 's':
694dc38852aSEric Blake             sflag = true;
695797ac58cSKevin Wolf             pattern_offset = cvtnum(optarg);
696797ac58cSKevin Wolf             if (pattern_offset < 0) {
697a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_offset, optarg);
698b32d7a39SMax Reitz                 return pattern_offset;
699797ac58cSKevin Wolf             }
700797ac58cSKevin Wolf             break;
701797ac58cSKevin Wolf         case 'v':
702dc38852aSEric Blake             vflag = true;
703797ac58cSKevin Wolf             break;
704797ac58cSKevin Wolf         default:
705b444d0e9SMax Reitz             qemuio_command_usage(&read_cmd);
706b32d7a39SMax Reitz             return -EINVAL;
707797ac58cSKevin Wolf         }
708797ac58cSKevin Wolf     }
709797ac58cSKevin Wolf 
710797ac58cSKevin Wolf     if (optind != argc - 2) {
711b444d0e9SMax Reitz         qemuio_command_usage(&read_cmd);
712b32d7a39SMax Reitz         return -EINVAL;
713797ac58cSKevin Wolf     }
714797ac58cSKevin Wolf 
715797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
716797ac58cSKevin Wolf     if (offset < 0) {
717a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
718b32d7a39SMax Reitz         return offset;
719797ac58cSKevin Wolf     }
720797ac58cSKevin Wolf 
721797ac58cSKevin Wolf     optind++;
722797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
723797ac58cSKevin Wolf     if (count < 0) {
724a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
725b32d7a39SMax Reitz         return count;
7263026c468SAlberto Garcia     } else if (count > BDRV_REQUEST_MAX_BYTES) {
7279b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
7283026c468SAlberto Garcia                (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
729b32d7a39SMax Reitz         return -EINVAL;
730797ac58cSKevin Wolf     }
731797ac58cSKevin Wolf 
732797ac58cSKevin Wolf     if (!Pflag && (lflag || sflag)) {
733b444d0e9SMax Reitz         qemuio_command_usage(&read_cmd);
734b32d7a39SMax Reitz         return -EINVAL;
735797ac58cSKevin Wolf     }
736797ac58cSKevin Wolf 
737797ac58cSKevin Wolf     if (!lflag) {
738797ac58cSKevin Wolf         pattern_count = count - pattern_offset;
739797ac58cSKevin Wolf     }
740797ac58cSKevin Wolf 
741797ac58cSKevin Wolf     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
742797ac58cSKevin Wolf         printf("pattern verification range exceeds end of read data\n");
743b32d7a39SMax Reitz         return -EINVAL;
744797ac58cSKevin Wolf     }
745797ac58cSKevin Wolf 
746093ea232SEric Blake     if (bflag) {
7471bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
7481bce6b4cSEric Blake             printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
749797ac58cSKevin Wolf                    offset);
750b32d7a39SMax Reitz             return -EINVAL;
751797ac58cSKevin Wolf         }
7521bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
7531bce6b4cSEric Blake             printf("%"PRId64" is not a sector-aligned value for 'count'\n",
754797ac58cSKevin Wolf                    count);
755b32d7a39SMax Reitz             return -EINVAL;
756797ac58cSKevin Wolf         }
757797ac58cSKevin Wolf     }
758797ac58cSKevin Wolf 
7594c7b7e9bSMax Reitz     buf = qemu_io_alloc(blk, count, 0xab);
760797ac58cSKevin Wolf 
761797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
7627b3f9712SEric Blake     if (bflag) {
763b32d7a39SMax Reitz         ret = do_load_vmstate(blk, buf, offset, count, &total);
764797ac58cSKevin Wolf     } else {
765b32d7a39SMax Reitz         ret = do_pread(blk, buf, offset, count, &total);
766797ac58cSKevin Wolf     }
767797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
768797ac58cSKevin Wolf 
769b32d7a39SMax Reitz     if (ret < 0) {
770b32d7a39SMax Reitz         printf("read failed: %s\n", strerror(-ret));
771797ac58cSKevin Wolf         goto out;
772797ac58cSKevin Wolf     }
773b32d7a39SMax Reitz     cnt = ret;
774b32d7a39SMax Reitz 
775b32d7a39SMax Reitz     ret = 0;
776797ac58cSKevin Wolf 
777797ac58cSKevin Wolf     if (Pflag) {
778797ac58cSKevin Wolf         void *cmp_buf = g_malloc(pattern_count);
779797ac58cSKevin Wolf         memset(cmp_buf, pattern, pattern_count);
780797ac58cSKevin Wolf         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
781797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
7829b0beaf3SJohn Snow                    PRId64 ", %"PRId64" bytes\n",
783797ac58cSKevin Wolf                    offset + pattern_offset, pattern_count);
784b32d7a39SMax Reitz             ret = -EINVAL;
785797ac58cSKevin Wolf         }
786797ac58cSKevin Wolf         g_free(cmp_buf);
787797ac58cSKevin Wolf     }
788797ac58cSKevin Wolf 
789797ac58cSKevin Wolf     if (qflag) {
790797ac58cSKevin Wolf         goto out;
791797ac58cSKevin Wolf     }
792797ac58cSKevin Wolf 
793797ac58cSKevin Wolf     if (vflag) {
794797ac58cSKevin Wolf         dump_buffer(buf, offset, count);
795797ac58cSKevin Wolf     }
796797ac58cSKevin Wolf 
797797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
798797ac58cSKevin Wolf     t2 = tsub(t2, t1);
799797ac58cSKevin Wolf     print_report("read", &t2, offset, count, total, cnt, Cflag);
800797ac58cSKevin Wolf 
801797ac58cSKevin Wolf out:
802797ac58cSKevin Wolf     qemu_io_free(buf);
803b32d7a39SMax Reitz     return ret;
804797ac58cSKevin Wolf }
805797ac58cSKevin Wolf 
806797ac58cSKevin Wolf static void readv_help(void)
807797ac58cSKevin Wolf {
808797ac58cSKevin Wolf     printf(
809797ac58cSKevin Wolf "\n"
810797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n"
811797ac58cSKevin Wolf "\n"
812797ac58cSKevin Wolf " Example:\n"
813797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
814797ac58cSKevin Wolf "\n"
815797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
816797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
817797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n"
818797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
819797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
820797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
821797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
822797ac58cSKevin Wolf "\n");
823797ac58cSKevin Wolf }
824797ac58cSKevin Wolf 
825b32d7a39SMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv);
826797ac58cSKevin Wolf 
827797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = {
828797ac58cSKevin Wolf     .name       = "readv",
829797ac58cSKevin Wolf     .cfunc      = readv_f,
830797ac58cSKevin Wolf     .argmin     = 2,
831797ac58cSKevin Wolf     .argmax     = -1,
832797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern] off len [len..]",
833797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
834797ac58cSKevin Wolf     .help       = readv_help,
835797ac58cSKevin Wolf };
836797ac58cSKevin Wolf 
837b32d7a39SMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv)
838797ac58cSKevin Wolf {
839797ac58cSKevin Wolf     struct timeval t1, t2;
840dc38852aSEric Blake     bool Cflag = false, qflag = false, vflag = false;
841b32d7a39SMax Reitz     int c, cnt, ret;
842797ac58cSKevin Wolf     char *buf;
843797ac58cSKevin Wolf     int64_t offset;
844797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
845797ac58cSKevin Wolf     int total = 0;
846797ac58cSKevin Wolf     int nr_iov;
847797ac58cSKevin Wolf     QEMUIOVector qiov;
848797ac58cSKevin Wolf     int pattern = 0;
849dc38852aSEric Blake     bool Pflag = false;
850797ac58cSKevin Wolf 
851b062ad86SEric Blake     while ((c = getopt(argc, argv, "CP:qv")) != -1) {
852797ac58cSKevin Wolf         switch (c) {
853797ac58cSKevin Wolf         case 'C':
854dc38852aSEric Blake             Cflag = true;
855797ac58cSKevin Wolf             break;
856797ac58cSKevin Wolf         case 'P':
857dc38852aSEric Blake             Pflag = true;
858797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
859797ac58cSKevin Wolf             if (pattern < 0) {
860b32d7a39SMax Reitz                 return -EINVAL;
861797ac58cSKevin Wolf             }
862797ac58cSKevin Wolf             break;
863797ac58cSKevin Wolf         case 'q':
864dc38852aSEric Blake             qflag = true;
865797ac58cSKevin Wolf             break;
866797ac58cSKevin Wolf         case 'v':
867dc38852aSEric Blake             vflag = true;
868797ac58cSKevin Wolf             break;
869797ac58cSKevin Wolf         default:
870b444d0e9SMax Reitz             qemuio_command_usage(&readv_cmd);
871b32d7a39SMax Reitz             return -EINVAL;
872797ac58cSKevin Wolf         }
873797ac58cSKevin Wolf     }
874797ac58cSKevin Wolf 
875797ac58cSKevin Wolf     if (optind > argc - 2) {
876b444d0e9SMax Reitz         qemuio_command_usage(&readv_cmd);
877b32d7a39SMax Reitz         return -EINVAL;
878797ac58cSKevin Wolf     }
879797ac58cSKevin Wolf 
880797ac58cSKevin Wolf 
881797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
882797ac58cSKevin Wolf     if (offset < 0) {
883a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
884b32d7a39SMax Reitz         return offset;
885797ac58cSKevin Wolf     }
886797ac58cSKevin Wolf     optind++;
887797ac58cSKevin Wolf 
888797ac58cSKevin Wolf     nr_iov = argc - optind;
8894c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
890797ac58cSKevin Wolf     if (buf == NULL) {
891b32d7a39SMax Reitz         return -EINVAL;
892797ac58cSKevin Wolf     }
893797ac58cSKevin Wolf 
894797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
895b32d7a39SMax Reitz     ret = do_aio_readv(blk, &qiov, offset, &total);
896797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
897797ac58cSKevin Wolf 
898b32d7a39SMax Reitz     if (ret < 0) {
899b32d7a39SMax Reitz         printf("readv failed: %s\n", strerror(-ret));
900797ac58cSKevin Wolf         goto out;
901797ac58cSKevin Wolf     }
902b32d7a39SMax Reitz     cnt = ret;
903b32d7a39SMax Reitz 
904b32d7a39SMax Reitz     ret = 0;
905797ac58cSKevin Wolf 
906797ac58cSKevin Wolf     if (Pflag) {
907797ac58cSKevin Wolf         void *cmp_buf = g_malloc(qiov.size);
908797ac58cSKevin Wolf         memset(cmp_buf, pattern, qiov.size);
909797ac58cSKevin Wolf         if (memcmp(buf, cmp_buf, qiov.size)) {
910797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
911cf67b692SStefan Weil                    PRId64 ", %zu bytes\n", offset, qiov.size);
912b32d7a39SMax Reitz             ret = -EINVAL;
913797ac58cSKevin Wolf         }
914797ac58cSKevin Wolf         g_free(cmp_buf);
915797ac58cSKevin Wolf     }
916797ac58cSKevin Wolf 
917797ac58cSKevin Wolf     if (qflag) {
918797ac58cSKevin Wolf         goto out;
919797ac58cSKevin Wolf     }
920797ac58cSKevin Wolf 
921797ac58cSKevin Wolf     if (vflag) {
922797ac58cSKevin Wolf         dump_buffer(buf, offset, qiov.size);
923797ac58cSKevin Wolf     }
924797ac58cSKevin Wolf 
925797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
926797ac58cSKevin Wolf     t2 = tsub(t2, t1);
927797ac58cSKevin Wolf     print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
928797ac58cSKevin Wolf 
929797ac58cSKevin Wolf out:
930797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
931797ac58cSKevin Wolf     qemu_io_free(buf);
932b32d7a39SMax Reitz     return ret;
933797ac58cSKevin Wolf }
934797ac58cSKevin Wolf 
935797ac58cSKevin Wolf static void write_help(void)
936797ac58cSKevin Wolf {
937797ac58cSKevin Wolf     printf(
938797ac58cSKevin Wolf "\n"
939797ac58cSKevin Wolf " writes a range of bytes from the given offset\n"
940797ac58cSKevin Wolf "\n"
941797ac58cSKevin Wolf " Example:\n"
942797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
943797ac58cSKevin Wolf "\n"
944797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
945797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
946797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n"
9474c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n"
948770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
949093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
950797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
951797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
952797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
953c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
954d004bd52SEric Blake " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
955797ac58cSKevin Wolf "\n");
956797ac58cSKevin Wolf }
957797ac58cSKevin Wolf 
958b32d7a39SMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv);
959797ac58cSKevin Wolf 
960797ac58cSKevin Wolf static const cmdinfo_t write_cmd = {
961797ac58cSKevin Wolf     .name       = "write",
962797ac58cSKevin Wolf     .altname    = "w",
963797ac58cSKevin Wolf     .cfunc      = write_f,
964887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
965797ac58cSKevin Wolf     .argmin     = 2,
966797ac58cSKevin Wolf     .argmax     = -1,
967c2e001ccSEric Blake     .args       = "[-bcCfquz] [-P pattern] off len",
968797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
969797ac58cSKevin Wolf     .help       = write_help,
970797ac58cSKevin Wolf };
971797ac58cSKevin Wolf 
972b32d7a39SMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv)
973797ac58cSKevin Wolf {
974797ac58cSKevin Wolf     struct timeval t1, t2;
975093ea232SEric Blake     bool Cflag = false, qflag = false, bflag = false;
976dc38852aSEric Blake     bool Pflag = false, zflag = false, cflag = false;
977770e0e0eSEric Blake     int flags = 0;
978b32d7a39SMax Reitz     int c, cnt, ret;
979797ac58cSKevin Wolf     char *buf = NULL;
980797ac58cSKevin Wolf     int64_t offset;
9819b0beaf3SJohn Snow     int64_t count;
982797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
9839b0beaf3SJohn Snow     int64_t total = 0;
984797ac58cSKevin Wolf     int pattern = 0xcd;
985797ac58cSKevin Wolf 
986c2e001ccSEric Blake     while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
987797ac58cSKevin Wolf         switch (c) {
988797ac58cSKevin Wolf         case 'b':
989dc38852aSEric Blake             bflag = true;
990797ac58cSKevin Wolf             break;
991797ac58cSKevin Wolf         case 'c':
992dc38852aSEric Blake             cflag = true;
993797ac58cSKevin Wolf             break;
994797ac58cSKevin Wolf         case 'C':
995dc38852aSEric Blake             Cflag = true;
996797ac58cSKevin Wolf             break;
997770e0e0eSEric Blake         case 'f':
998770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
999770e0e0eSEric Blake             break;
1000797ac58cSKevin Wolf         case 'p':
1001093ea232SEric Blake             /* Ignored for backwards compatibility */
1002797ac58cSKevin Wolf             break;
1003797ac58cSKevin Wolf         case 'P':
1004dc38852aSEric Blake             Pflag = true;
1005797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1006797ac58cSKevin Wolf             if (pattern < 0) {
1007b32d7a39SMax Reitz                 return -EINVAL;
1008797ac58cSKevin Wolf             }
1009797ac58cSKevin Wolf             break;
1010797ac58cSKevin Wolf         case 'q':
1011dc38852aSEric Blake             qflag = true;
1012797ac58cSKevin Wolf             break;
1013c2e001ccSEric Blake         case 'u':
1014c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1015c2e001ccSEric Blake             break;
1016797ac58cSKevin Wolf         case 'z':
1017dc38852aSEric Blake             zflag = true;
1018797ac58cSKevin Wolf             break;
1019797ac58cSKevin Wolf         default:
1020b444d0e9SMax Reitz             qemuio_command_usage(&write_cmd);
1021b32d7a39SMax Reitz             return -EINVAL;
1022797ac58cSKevin Wolf         }
1023797ac58cSKevin Wolf     }
1024797ac58cSKevin Wolf 
1025797ac58cSKevin Wolf     if (optind != argc - 2) {
1026b444d0e9SMax Reitz         qemuio_command_usage(&write_cmd);
1027b32d7a39SMax Reitz         return -EINVAL;
1028797ac58cSKevin Wolf     }
1029797ac58cSKevin Wolf 
1030093ea232SEric Blake     if (bflag && zflag) {
1031093ea232SEric Blake         printf("-b and -z cannot be specified at the same time\n");
1032b32d7a39SMax Reitz         return -EINVAL;
1033797ac58cSKevin Wolf     }
1034797ac58cSKevin Wolf 
1035770e0e0eSEric Blake     if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1036770e0e0eSEric Blake         printf("-f and -b or -c cannot be specified at the same time\n");
1037b32d7a39SMax Reitz         return -EINVAL;
1038770e0e0eSEric Blake     }
1039770e0e0eSEric Blake 
1040c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1041c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
1042b32d7a39SMax Reitz         return -EINVAL;
1043c2e001ccSEric Blake     }
1044c2e001ccSEric Blake 
1045797ac58cSKevin Wolf     if (zflag && Pflag) {
1046797ac58cSKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
1047b32d7a39SMax Reitz         return -EINVAL;
1048797ac58cSKevin Wolf     }
1049797ac58cSKevin Wolf 
1050797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1051797ac58cSKevin Wolf     if (offset < 0) {
1052a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1053b32d7a39SMax Reitz         return offset;
1054797ac58cSKevin Wolf     }
1055797ac58cSKevin Wolf 
1056797ac58cSKevin Wolf     optind++;
1057797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1058797ac58cSKevin Wolf     if (count < 0) {
1059a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
1060b32d7a39SMax Reitz         return count;
10613026c468SAlberto Garcia     } else if (count > BDRV_REQUEST_MAX_BYTES) {
10629b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
10633026c468SAlberto Garcia                (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
1064b32d7a39SMax Reitz         return -EINVAL;
1065797ac58cSKevin Wolf     }
1066797ac58cSKevin Wolf 
1067093ea232SEric Blake     if (bflag || cflag) {
10681bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
10691bce6b4cSEric Blake             printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
1070797ac58cSKevin Wolf                    offset);
1071b32d7a39SMax Reitz             return -EINVAL;
1072797ac58cSKevin Wolf         }
1073797ac58cSKevin Wolf 
10741bce6b4cSEric Blake         if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
10751bce6b4cSEric Blake             printf("%"PRId64" is not a sector-aligned value for 'count'\n",
1076797ac58cSKevin Wolf                    count);
1077b32d7a39SMax Reitz             return -EINVAL;
1078797ac58cSKevin Wolf         }
1079797ac58cSKevin Wolf     }
1080797ac58cSKevin Wolf 
1081797ac58cSKevin Wolf     if (!zflag) {
10824c7b7e9bSMax Reitz         buf = qemu_io_alloc(blk, count, pattern);
1083797ac58cSKevin Wolf     }
1084797ac58cSKevin Wolf 
1085797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
10867b3f9712SEric Blake     if (bflag) {
1087b32d7a39SMax Reitz         ret = do_save_vmstate(blk, buf, offset, count, &total);
1088797ac58cSKevin Wolf     } else if (zflag) {
1089b32d7a39SMax Reitz         ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
1090797ac58cSKevin Wolf     } else if (cflag) {
1091b32d7a39SMax Reitz         ret = do_write_compressed(blk, buf, offset, count, &total);
1092797ac58cSKevin Wolf     } else {
1093b32d7a39SMax Reitz         ret = do_pwrite(blk, buf, offset, count, flags, &total);
1094797ac58cSKevin Wolf     }
1095797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1096797ac58cSKevin Wolf 
1097b32d7a39SMax Reitz     if (ret < 0) {
1098b32d7a39SMax Reitz         printf("write failed: %s\n", strerror(-ret));
1099797ac58cSKevin Wolf         goto out;
1100797ac58cSKevin Wolf     }
1101b32d7a39SMax Reitz     cnt = ret;
1102b32d7a39SMax Reitz 
1103b32d7a39SMax Reitz     ret = 0;
1104797ac58cSKevin Wolf 
1105797ac58cSKevin Wolf     if (qflag) {
1106797ac58cSKevin Wolf         goto out;
1107797ac58cSKevin Wolf     }
1108797ac58cSKevin Wolf 
1109797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1110797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1111797ac58cSKevin Wolf     print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1112797ac58cSKevin Wolf 
1113797ac58cSKevin Wolf out:
1114797ac58cSKevin Wolf     if (!zflag) {
1115797ac58cSKevin Wolf         qemu_io_free(buf);
1116797ac58cSKevin Wolf     }
1117b32d7a39SMax Reitz     return ret;
1118797ac58cSKevin Wolf }
1119797ac58cSKevin Wolf 
1120797ac58cSKevin Wolf static void
1121797ac58cSKevin Wolf writev_help(void)
1122797ac58cSKevin Wolf {
1123797ac58cSKevin Wolf     printf(
1124797ac58cSKevin Wolf "\n"
1125797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n"
1126797ac58cSKevin Wolf "\n"
1127797ac58cSKevin Wolf " Example:\n"
11286e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1129797ac58cSKevin Wolf "\n"
1130797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1131797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1132797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1133797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1134770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
1135797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1136797ac58cSKevin Wolf "\n");
1137797ac58cSKevin Wolf }
1138797ac58cSKevin Wolf 
1139b32d7a39SMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv);
1140797ac58cSKevin Wolf 
1141797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = {
1142797ac58cSKevin Wolf     .name       = "writev",
1143797ac58cSKevin Wolf     .cfunc      = writev_f,
1144887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
1145797ac58cSKevin Wolf     .argmin     = 2,
1146797ac58cSKevin Wolf     .argmax     = -1,
1147770e0e0eSEric Blake     .args       = "[-Cfq] [-P pattern] off len [len..]",
1148797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
1149797ac58cSKevin Wolf     .help       = writev_help,
1150797ac58cSKevin Wolf };
1151797ac58cSKevin Wolf 
1152b32d7a39SMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv)
1153797ac58cSKevin Wolf {
1154797ac58cSKevin Wolf     struct timeval t1, t2;
1155dc38852aSEric Blake     bool Cflag = false, qflag = false;
1156770e0e0eSEric Blake     int flags = 0;
1157b32d7a39SMax Reitz     int c, cnt, ret;
1158797ac58cSKevin Wolf     char *buf;
1159797ac58cSKevin Wolf     int64_t offset;
1160797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1161797ac58cSKevin Wolf     int total = 0;
1162797ac58cSKevin Wolf     int nr_iov;
1163797ac58cSKevin Wolf     int pattern = 0xcd;
1164797ac58cSKevin Wolf     QEMUIOVector qiov;
1165797ac58cSKevin Wolf 
11664ca1d340SEric Blake     while ((c = getopt(argc, argv, "CfqP:")) != -1) {
1167797ac58cSKevin Wolf         switch (c) {
1168797ac58cSKevin Wolf         case 'C':
1169dc38852aSEric Blake             Cflag = true;
1170797ac58cSKevin Wolf             break;
1171770e0e0eSEric Blake         case 'f':
1172770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1173770e0e0eSEric Blake             break;
1174797ac58cSKevin Wolf         case 'q':
1175dc38852aSEric Blake             qflag = true;
1176797ac58cSKevin Wolf             break;
1177797ac58cSKevin Wolf         case 'P':
1178797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1179797ac58cSKevin Wolf             if (pattern < 0) {
1180b32d7a39SMax Reitz                 return -EINVAL;
1181797ac58cSKevin Wolf             }
1182797ac58cSKevin Wolf             break;
1183797ac58cSKevin Wolf         default:
1184b444d0e9SMax Reitz             qemuio_command_usage(&writev_cmd);
1185b32d7a39SMax Reitz             return -EINVAL;
1186797ac58cSKevin Wolf         }
1187797ac58cSKevin Wolf     }
1188797ac58cSKevin Wolf 
1189797ac58cSKevin Wolf     if (optind > argc - 2) {
1190b444d0e9SMax Reitz         qemuio_command_usage(&writev_cmd);
1191b32d7a39SMax Reitz         return -EINVAL;
1192797ac58cSKevin Wolf     }
1193797ac58cSKevin Wolf 
1194797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1195797ac58cSKevin Wolf     if (offset < 0) {
1196a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1197b32d7a39SMax Reitz         return offset;
1198797ac58cSKevin Wolf     }
1199797ac58cSKevin Wolf     optind++;
1200797ac58cSKevin Wolf 
1201797ac58cSKevin Wolf     nr_iov = argc - optind;
12024c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1203797ac58cSKevin Wolf     if (buf == NULL) {
1204b32d7a39SMax Reitz         return -EINVAL;
1205797ac58cSKevin Wolf     }
1206797ac58cSKevin Wolf 
1207797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1208b32d7a39SMax Reitz     ret = do_aio_writev(blk, &qiov, offset, flags, &total);
1209797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1210797ac58cSKevin Wolf 
1211b32d7a39SMax Reitz     if (ret < 0) {
1212b32d7a39SMax Reitz         printf("writev failed: %s\n", strerror(-ret));
1213797ac58cSKevin Wolf         goto out;
1214797ac58cSKevin Wolf     }
1215b32d7a39SMax Reitz     cnt = ret;
1216b32d7a39SMax Reitz 
1217b32d7a39SMax Reitz     ret = 0;
1218797ac58cSKevin Wolf 
1219797ac58cSKevin Wolf     if (qflag) {
1220797ac58cSKevin Wolf         goto out;
1221797ac58cSKevin Wolf     }
1222797ac58cSKevin Wolf 
1223797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1224797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1225797ac58cSKevin Wolf     print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1226797ac58cSKevin Wolf out:
1227797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
1228797ac58cSKevin Wolf     qemu_io_free(buf);
1229b32d7a39SMax Reitz     return ret;
1230797ac58cSKevin Wolf }
1231797ac58cSKevin Wolf 
1232797ac58cSKevin Wolf struct aio_ctx {
12334c7b7e9bSMax Reitz     BlockBackend *blk;
1234797ac58cSKevin Wolf     QEMUIOVector qiov;
1235797ac58cSKevin Wolf     int64_t offset;
1236797ac58cSKevin Wolf     char *buf;
1237dc38852aSEric Blake     bool qflag;
1238dc38852aSEric Blake     bool vflag;
1239dc38852aSEric Blake     bool Cflag;
1240dc38852aSEric Blake     bool Pflag;
1241dc38852aSEric Blake     bool zflag;
1242a91f9584SFam Zheng     BlockAcctCookie acct;
1243797ac58cSKevin Wolf     int pattern;
1244797ac58cSKevin Wolf     struct timeval t1;
1245797ac58cSKevin Wolf };
1246797ac58cSKevin Wolf 
1247797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret)
1248797ac58cSKevin Wolf {
1249797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1250797ac58cSKevin Wolf     struct timeval t2;
1251797ac58cSKevin Wolf 
1252797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1253797ac58cSKevin Wolf 
1254797ac58cSKevin Wolf 
1255797ac58cSKevin Wolf     if (ret < 0) {
1256797ac58cSKevin Wolf         printf("aio_write failed: %s\n", strerror(-ret));
1257556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1258797ac58cSKevin Wolf         goto out;
1259797ac58cSKevin Wolf     }
1260797ac58cSKevin Wolf 
12614c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1262a91f9584SFam Zheng 
1263797ac58cSKevin Wolf     if (ctx->qflag) {
1264797ac58cSKevin Wolf         goto out;
1265797ac58cSKevin Wolf     }
1266797ac58cSKevin Wolf 
1267797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1268797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1269797ac58cSKevin Wolf     print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1270797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1271797ac58cSKevin Wolf out:
12725ceb7765SKevin Wolf     if (!ctx->zflag) {
1273797ac58cSKevin Wolf         qemu_io_free(ctx->buf);
1274797ac58cSKevin Wolf         qemu_iovec_destroy(&ctx->qiov);
12755ceb7765SKevin Wolf     }
1276797ac58cSKevin Wolf     g_free(ctx);
1277797ac58cSKevin Wolf }
1278797ac58cSKevin Wolf 
1279797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret)
1280797ac58cSKevin Wolf {
1281797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1282797ac58cSKevin Wolf     struct timeval t2;
1283797ac58cSKevin Wolf 
1284797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1285797ac58cSKevin Wolf 
1286797ac58cSKevin Wolf     if (ret < 0) {
1287797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-ret));
1288556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1289797ac58cSKevin Wolf         goto out;
1290797ac58cSKevin Wolf     }
1291797ac58cSKevin Wolf 
1292797ac58cSKevin Wolf     if (ctx->Pflag) {
1293797ac58cSKevin Wolf         void *cmp_buf = g_malloc(ctx->qiov.size);
1294797ac58cSKevin Wolf 
1295797ac58cSKevin Wolf         memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1296797ac58cSKevin Wolf         if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1297797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
1298cf67b692SStefan Weil                    PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
1299797ac58cSKevin Wolf         }
1300797ac58cSKevin Wolf         g_free(cmp_buf);
1301797ac58cSKevin Wolf     }
1302797ac58cSKevin Wolf 
13034c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1304a91f9584SFam Zheng 
1305797ac58cSKevin Wolf     if (ctx->qflag) {
1306797ac58cSKevin Wolf         goto out;
1307797ac58cSKevin Wolf     }
1308797ac58cSKevin Wolf 
1309797ac58cSKevin Wolf     if (ctx->vflag) {
1310797ac58cSKevin Wolf         dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1311797ac58cSKevin Wolf     }
1312797ac58cSKevin Wolf 
1313797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1314797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1315797ac58cSKevin Wolf     print_report("read", &t2, ctx->offset, ctx->qiov.size,
1316797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1317797ac58cSKevin Wolf out:
1318797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1319797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1320797ac58cSKevin Wolf     g_free(ctx);
1321797ac58cSKevin Wolf }
1322797ac58cSKevin Wolf 
1323797ac58cSKevin Wolf static void aio_read_help(void)
1324797ac58cSKevin Wolf {
1325797ac58cSKevin Wolf     printf(
1326797ac58cSKevin Wolf "\n"
1327797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n"
1328797ac58cSKevin Wolf "\n"
1329797ac58cSKevin Wolf " Example:\n"
1330797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1331797ac58cSKevin Wolf "\n"
1332797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
1333797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
1334797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n"
1335797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1336b32d7a39SMax Reitz " Note that due to its asynchronous nature, this command will be\n"
1337b32d7a39SMax Reitz " considered successful once the request is submitted, independently\n"
1338b32d7a39SMax Reitz " of potential I/O errors or pattern mismatches.\n"
1339797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1340797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
134137546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n"
1342797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
1343797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1344797ac58cSKevin Wolf "\n");
1345797ac58cSKevin Wolf }
1346797ac58cSKevin Wolf 
1347b32d7a39SMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1348797ac58cSKevin Wolf 
1349797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = {
1350797ac58cSKevin Wolf     .name       = "aio_read",
1351797ac58cSKevin Wolf     .cfunc      = aio_read_f,
1352797ac58cSKevin Wolf     .argmin     = 2,
1353797ac58cSKevin Wolf     .argmax     = -1,
135437546ff2SEric Blake     .args       = "[-Ciqv] [-P pattern] off len [len..]",
1355797ac58cSKevin Wolf     .oneline    = "asynchronously reads a number of bytes",
1356797ac58cSKevin Wolf     .help       = aio_read_help,
1357797ac58cSKevin Wolf };
1358797ac58cSKevin Wolf 
1359b32d7a39SMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1360797ac58cSKevin Wolf {
1361797ac58cSKevin Wolf     int nr_iov, c;
1362797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1363797ac58cSKevin Wolf 
13644c7b7e9bSMax Reitz     ctx->blk = blk;
136537546ff2SEric Blake     while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
1366797ac58cSKevin Wolf         switch (c) {
1367797ac58cSKevin Wolf         case 'C':
1368dc38852aSEric Blake             ctx->Cflag = true;
1369797ac58cSKevin Wolf             break;
1370797ac58cSKevin Wolf         case 'P':
1371dc38852aSEric Blake             ctx->Pflag = true;
1372797ac58cSKevin Wolf             ctx->pattern = parse_pattern(optarg);
1373797ac58cSKevin Wolf             if (ctx->pattern < 0) {
1374797ac58cSKevin Wolf                 g_free(ctx);
1375b32d7a39SMax Reitz                 return -EINVAL;
1376797ac58cSKevin Wolf             }
1377797ac58cSKevin Wolf             break;
137837546ff2SEric Blake         case 'i':
137937546ff2SEric Blake             printf("injecting invalid read request\n");
138037546ff2SEric Blake             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
138137546ff2SEric Blake             g_free(ctx);
1382b32d7a39SMax Reitz             return 0;
1383797ac58cSKevin Wolf         case 'q':
1384dc38852aSEric Blake             ctx->qflag = true;
1385797ac58cSKevin Wolf             break;
1386797ac58cSKevin Wolf         case 'v':
1387dc38852aSEric Blake             ctx->vflag = true;
1388797ac58cSKevin Wolf             break;
1389797ac58cSKevin Wolf         default:
1390797ac58cSKevin Wolf             g_free(ctx);
1391b444d0e9SMax Reitz             qemuio_command_usage(&aio_read_cmd);
1392b32d7a39SMax Reitz             return -EINVAL;
1393797ac58cSKevin Wolf         }
1394797ac58cSKevin Wolf     }
1395797ac58cSKevin Wolf 
1396797ac58cSKevin Wolf     if (optind > argc - 2) {
1397797ac58cSKevin Wolf         g_free(ctx);
1398b444d0e9SMax Reitz         qemuio_command_usage(&aio_read_cmd);
1399b32d7a39SMax Reitz         return -EINVAL;
1400797ac58cSKevin Wolf     }
1401797ac58cSKevin Wolf 
1402797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1403797ac58cSKevin Wolf     if (ctx->offset < 0) {
1404b32d7a39SMax Reitz         int ret = ctx->offset;
1405b32d7a39SMax Reitz         print_cvtnum_err(ret, argv[optind]);
1406797ac58cSKevin Wolf         g_free(ctx);
1407b32d7a39SMax Reitz         return ret;
1408797ac58cSKevin Wolf     }
1409797ac58cSKevin Wolf     optind++;
1410797ac58cSKevin Wolf 
1411797ac58cSKevin Wolf     nr_iov = argc - optind;
14124c7b7e9bSMax Reitz     ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1413797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1414556c2b60SAlberto Garcia         block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1415797ac58cSKevin Wolf         g_free(ctx);
1416b32d7a39SMax Reitz         return -EINVAL;
1417797ac58cSKevin Wolf     }
1418797ac58cSKevin Wolf 
1419797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
14204c7b7e9bSMax Reitz     block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
14214c7b7e9bSMax Reitz                      BLOCK_ACCT_READ);
14227b3f9712SEric Blake     blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1423b32d7a39SMax Reitz     return 0;
1424797ac58cSKevin Wolf }
1425797ac58cSKevin Wolf 
1426797ac58cSKevin Wolf static void aio_write_help(void)
1427797ac58cSKevin Wolf {
1428797ac58cSKevin Wolf     printf(
1429797ac58cSKevin Wolf "\n"
1430797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n"
1431797ac58cSKevin Wolf " from multiple buffers\n"
1432797ac58cSKevin Wolf "\n"
1433797ac58cSKevin Wolf " Example:\n"
1434797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1435797ac58cSKevin Wolf "\n"
1436797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1437797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1438797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n"
1439797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1440b32d7a39SMax Reitz " Note that due to its asynchronous nature, this command will be\n"
1441b32d7a39SMax Reitz " considered successful once the request is submitted, independently\n"
1442b32d7a39SMax Reitz " of potential I/O errors or pattern mismatches.\n"
1443797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1444797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1445770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
144637546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n"
1447797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1448c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
1449d004bd52SEric Blake " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1450797ac58cSKevin Wolf "\n");
1451797ac58cSKevin Wolf }
1452797ac58cSKevin Wolf 
1453b32d7a39SMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1454797ac58cSKevin Wolf 
1455797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = {
1456797ac58cSKevin Wolf     .name       = "aio_write",
1457797ac58cSKevin Wolf     .cfunc      = aio_write_f,
1458887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
1459797ac58cSKevin Wolf     .argmin     = 2,
1460797ac58cSKevin Wolf     .argmax     = -1,
146137546ff2SEric Blake     .args       = "[-Cfiquz] [-P pattern] off len [len..]",
1462797ac58cSKevin Wolf     .oneline    = "asynchronously writes a number of bytes",
1463797ac58cSKevin Wolf     .help       = aio_write_help,
1464797ac58cSKevin Wolf };
1465797ac58cSKevin Wolf 
1466b32d7a39SMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1467797ac58cSKevin Wolf {
1468797ac58cSKevin Wolf     int nr_iov, c;
1469797ac58cSKevin Wolf     int pattern = 0xcd;
1470797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1471770e0e0eSEric Blake     int flags = 0;
1472797ac58cSKevin Wolf 
14734c7b7e9bSMax Reitz     ctx->blk = blk;
147437546ff2SEric Blake     while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
1475797ac58cSKevin Wolf         switch (c) {
1476797ac58cSKevin Wolf         case 'C':
1477dc38852aSEric Blake             ctx->Cflag = true;
1478797ac58cSKevin Wolf             break;
1479770e0e0eSEric Blake         case 'f':
1480770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1481770e0e0eSEric Blake             break;
1482797ac58cSKevin Wolf         case 'q':
1483dc38852aSEric Blake             ctx->qflag = true;
1484797ac58cSKevin Wolf             break;
1485c2e001ccSEric Blake         case 'u':
1486c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1487c2e001ccSEric Blake             break;
1488797ac58cSKevin Wolf         case 'P':
1489797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1490797ac58cSKevin Wolf             if (pattern < 0) {
1491797ac58cSKevin Wolf                 g_free(ctx);
1492b32d7a39SMax Reitz                 return -EINVAL;
1493797ac58cSKevin Wolf             }
1494797ac58cSKevin Wolf             break;
149537546ff2SEric Blake         case 'i':
149637546ff2SEric Blake             printf("injecting invalid write request\n");
149737546ff2SEric Blake             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
149837546ff2SEric Blake             g_free(ctx);
1499b32d7a39SMax Reitz             return 0;
15005ceb7765SKevin Wolf         case 'z':
1501dc38852aSEric Blake             ctx->zflag = true;
15025ceb7765SKevin Wolf             break;
1503797ac58cSKevin Wolf         default:
1504797ac58cSKevin Wolf             g_free(ctx);
1505b444d0e9SMax Reitz             qemuio_command_usage(&aio_write_cmd);
1506b32d7a39SMax Reitz             return -EINVAL;
1507797ac58cSKevin Wolf         }
1508797ac58cSKevin Wolf     }
1509797ac58cSKevin Wolf 
1510797ac58cSKevin Wolf     if (optind > argc - 2) {
1511797ac58cSKevin Wolf         g_free(ctx);
1512b444d0e9SMax Reitz         qemuio_command_usage(&aio_write_cmd);
1513b32d7a39SMax Reitz         return -EINVAL;
1514797ac58cSKevin Wolf     }
1515797ac58cSKevin Wolf 
15165ceb7765SKevin Wolf     if (ctx->zflag && optind != argc - 2) {
15175ceb7765SKevin Wolf         printf("-z supports only a single length parameter\n");
15185ceb7765SKevin Wolf         g_free(ctx);
1519b32d7a39SMax Reitz         return -EINVAL;
15205ceb7765SKevin Wolf     }
15215ceb7765SKevin Wolf 
1522c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1523c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
15244ca1d340SEric Blake         g_free(ctx);
1525b32d7a39SMax Reitz         return -EINVAL;
1526c2e001ccSEric Blake     }
1527c2e001ccSEric Blake 
15285ceb7765SKevin Wolf     if (ctx->zflag && ctx->Pflag) {
15295ceb7765SKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
15305ceb7765SKevin Wolf         g_free(ctx);
1531b32d7a39SMax Reitz         return -EINVAL;
15325ceb7765SKevin Wolf     }
15335ceb7765SKevin Wolf 
1534797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1535797ac58cSKevin Wolf     if (ctx->offset < 0) {
1536b32d7a39SMax Reitz         int ret = ctx->offset;
1537b32d7a39SMax Reitz         print_cvtnum_err(ret, argv[optind]);
1538797ac58cSKevin Wolf         g_free(ctx);
1539b32d7a39SMax Reitz         return ret;
1540797ac58cSKevin Wolf     }
1541797ac58cSKevin Wolf     optind++;
1542797ac58cSKevin Wolf 
15435ceb7765SKevin Wolf     if (ctx->zflag) {
15445ceb7765SKevin Wolf         int64_t count = cvtnum(argv[optind]);
15455ceb7765SKevin Wolf         if (count < 0) {
15465ceb7765SKevin Wolf             print_cvtnum_err(count, argv[optind]);
15470e01b76eSKevin Wolf             g_free(ctx);
1548b32d7a39SMax Reitz             return count;
15495ceb7765SKevin Wolf         }
15505ceb7765SKevin Wolf 
15515ceb7765SKevin Wolf         ctx->qiov.size = count;
1552d004bd52SEric Blake         blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1553770e0e0eSEric Blake                               ctx);
15545ceb7765SKevin Wolf     } else {
1555797ac58cSKevin Wolf         nr_iov = argc - optind;
15565ceb7765SKevin Wolf         ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
15575ceb7765SKevin Wolf                                 pattern);
1558797ac58cSKevin Wolf         if (ctx->buf == NULL) {
1559556c2b60SAlberto Garcia             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1560797ac58cSKevin Wolf             g_free(ctx);
1561b32d7a39SMax Reitz             return -EINVAL;
1562797ac58cSKevin Wolf         }
1563797ac58cSKevin Wolf 
1564797ac58cSKevin Wolf         gettimeofday(&ctx->t1, NULL);
15654c7b7e9bSMax Reitz         block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
15664c7b7e9bSMax Reitz                          BLOCK_ACCT_WRITE);
15675ceb7765SKevin Wolf 
1568770e0e0eSEric Blake         blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1569770e0e0eSEric Blake                         ctx);
15705ceb7765SKevin Wolf     }
1571b32d7a39SMax Reitz 
1572b32d7a39SMax Reitz     return 0;
1573797ac58cSKevin Wolf }
1574797ac58cSKevin Wolf 
1575b32d7a39SMax Reitz static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1576797ac58cSKevin Wolf {
1577556c2b60SAlberto Garcia     BlockAcctCookie cookie;
1578556c2b60SAlberto Garcia     block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
15794c7b7e9bSMax Reitz     blk_drain_all();
1580556c2b60SAlberto Garcia     block_acct_done(blk_get_stats(blk), &cookie);
1581b32d7a39SMax Reitz     return 0;
1582797ac58cSKevin Wolf }
1583797ac58cSKevin Wolf 
1584797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = {
1585797ac58cSKevin Wolf     .name       = "aio_flush",
1586797ac58cSKevin Wolf     .cfunc      = aio_flush_f,
1587797ac58cSKevin Wolf     .oneline    = "completes all outstanding aio requests"
1588797ac58cSKevin Wolf };
1589797ac58cSKevin Wolf 
1590b32d7a39SMax Reitz static int flush_f(BlockBackend *blk, int argc, char **argv)
1591797ac58cSKevin Wolf {
1592b32d7a39SMax Reitz     return blk_flush(blk);
1593797ac58cSKevin Wolf }
1594797ac58cSKevin Wolf 
1595797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = {
1596797ac58cSKevin Wolf     .name       = "flush",
1597797ac58cSKevin Wolf     .altname    = "f",
1598797ac58cSKevin Wolf     .cfunc      = flush_f,
1599797ac58cSKevin Wolf     .oneline    = "flush all in-core file state to disk",
1600797ac58cSKevin Wolf };
1601797ac58cSKevin Wolf 
1602b32d7a39SMax Reitz static int truncate_f(BlockBackend *blk, int argc, char **argv)
1603797ac58cSKevin Wolf {
1604ed3d2ec9SMax Reitz     Error *local_err = NULL;
1605797ac58cSKevin Wolf     int64_t offset;
1606797ac58cSKevin Wolf     int ret;
1607797ac58cSKevin Wolf 
1608797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1609797ac58cSKevin Wolf     if (offset < 0) {
1610a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1611b32d7a39SMax Reitz         return offset;
1612797ac58cSKevin Wolf     }
1613797ac58cSKevin Wolf 
16143a691c50SMax Reitz     ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
1615797ac58cSKevin Wolf     if (ret < 0) {
1616ed3d2ec9SMax Reitz         error_report_err(local_err);
1617b32d7a39SMax Reitz         return ret;
1618797ac58cSKevin Wolf     }
1619b32d7a39SMax Reitz 
1620b32d7a39SMax Reitz     return 0;
1621797ac58cSKevin Wolf }
1622797ac58cSKevin Wolf 
1623797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = {
1624797ac58cSKevin Wolf     .name       = "truncate",
1625797ac58cSKevin Wolf     .altname    = "t",
1626797ac58cSKevin Wolf     .cfunc      = truncate_f,
1627887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1628797ac58cSKevin Wolf     .argmin     = 1,
1629797ac58cSKevin Wolf     .argmax     = 1,
1630797ac58cSKevin Wolf     .args       = "off",
1631797ac58cSKevin Wolf     .oneline    = "truncates the current file at the given offset",
1632797ac58cSKevin Wolf };
1633797ac58cSKevin Wolf 
1634b32d7a39SMax Reitz static int length_f(BlockBackend *blk, int argc, char **argv)
1635797ac58cSKevin Wolf {
1636797ac58cSKevin Wolf     int64_t size;
1637797ac58cSKevin Wolf     char s1[64];
1638797ac58cSKevin Wolf 
16394c7b7e9bSMax Reitz     size = blk_getlength(blk);
1640797ac58cSKevin Wolf     if (size < 0) {
1641797ac58cSKevin Wolf         printf("getlength: %s\n", strerror(-size));
1642b32d7a39SMax Reitz         return size;
1643797ac58cSKevin Wolf     }
1644797ac58cSKevin Wolf 
1645797ac58cSKevin Wolf     cvtstr(size, s1, sizeof(s1));
1646797ac58cSKevin Wolf     printf("%s\n", s1);
1647b32d7a39SMax Reitz     return 0;
1648797ac58cSKevin Wolf }
1649797ac58cSKevin Wolf 
1650797ac58cSKevin Wolf 
1651797ac58cSKevin Wolf static const cmdinfo_t length_cmd = {
1652797ac58cSKevin Wolf     .name   = "length",
1653797ac58cSKevin Wolf     .altname    = "l",
1654797ac58cSKevin Wolf     .cfunc      = length_f,
1655797ac58cSKevin Wolf     .oneline    = "gets the length of the current file",
1656797ac58cSKevin Wolf };
1657797ac58cSKevin Wolf 
1658797ac58cSKevin Wolf 
1659b32d7a39SMax Reitz static int info_f(BlockBackend *blk, int argc, char **argv)
1660797ac58cSKevin Wolf {
16614c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
1662797ac58cSKevin Wolf     BlockDriverInfo bdi;
1663a8d8ecb7SMax Reitz     ImageInfoSpecific *spec_info;
1664797ac58cSKevin Wolf     char s1[64], s2[64];
1665797ac58cSKevin Wolf     int ret;
1666797ac58cSKevin Wolf 
1667797ac58cSKevin Wolf     if (bs->drv && bs->drv->format_name) {
1668797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->format_name);
1669797ac58cSKevin Wolf     }
1670797ac58cSKevin Wolf     if (bs->drv && bs->drv->protocol_name) {
1671797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->protocol_name);
1672797ac58cSKevin Wolf     }
1673797ac58cSKevin Wolf 
1674797ac58cSKevin Wolf     ret = bdrv_get_info(bs, &bdi);
1675797ac58cSKevin Wolf     if (ret) {
1676b32d7a39SMax Reitz         return ret;
1677797ac58cSKevin Wolf     }
1678797ac58cSKevin Wolf 
1679797ac58cSKevin Wolf     cvtstr(bdi.cluster_size, s1, sizeof(s1));
1680797ac58cSKevin Wolf     cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1681797ac58cSKevin Wolf 
1682797ac58cSKevin Wolf     printf("cluster size: %s\n", s1);
1683797ac58cSKevin Wolf     printf("vm state offset: %s\n", s2);
1684797ac58cSKevin Wolf 
1685a8d8ecb7SMax Reitz     spec_info = bdrv_get_specific_info(bs);
1686a8d8ecb7SMax Reitz     if (spec_info) {
1687a8d8ecb7SMax Reitz         printf("Format specific information:\n");
1688a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1689a8d8ecb7SMax Reitz         qapi_free_ImageInfoSpecific(spec_info);
1690a8d8ecb7SMax Reitz     }
1691b32d7a39SMax Reitz 
1692b32d7a39SMax Reitz     return 0;
1693797ac58cSKevin Wolf }
1694797ac58cSKevin Wolf 
1695797ac58cSKevin Wolf 
1696797ac58cSKevin Wolf 
1697797ac58cSKevin Wolf static const cmdinfo_t info_cmd = {
1698797ac58cSKevin Wolf     .name       = "info",
1699797ac58cSKevin Wolf     .altname    = "i",
1700797ac58cSKevin Wolf     .cfunc      = info_f,
1701797ac58cSKevin Wolf     .oneline    = "prints information about the current file",
1702797ac58cSKevin Wolf };
1703797ac58cSKevin Wolf 
1704797ac58cSKevin Wolf static void discard_help(void)
1705797ac58cSKevin Wolf {
1706797ac58cSKevin Wolf     printf(
1707797ac58cSKevin Wolf "\n"
1708797ac58cSKevin Wolf " discards a range of bytes from the given offset\n"
1709797ac58cSKevin Wolf "\n"
1710797ac58cSKevin Wolf " Example:\n"
1711797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1712797ac58cSKevin Wolf "\n"
1713797ac58cSKevin Wolf " Discards a segment of the currently open file.\n"
1714797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1715797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1716797ac58cSKevin Wolf "\n");
1717797ac58cSKevin Wolf }
1718797ac58cSKevin Wolf 
1719b32d7a39SMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv);
1720797ac58cSKevin Wolf 
1721797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = {
1722797ac58cSKevin Wolf     .name       = "discard",
1723797ac58cSKevin Wolf     .altname    = "d",
1724797ac58cSKevin Wolf     .cfunc      = discard_f,
1725887354bdSKevin Wolf     .perm       = BLK_PERM_WRITE,
1726797ac58cSKevin Wolf     .argmin     = 2,
1727797ac58cSKevin Wolf     .argmax     = -1,
1728797ac58cSKevin Wolf     .args       = "[-Cq] off len",
1729797ac58cSKevin Wolf     .oneline    = "discards a number of bytes at a specified offset",
1730797ac58cSKevin Wolf     .help       = discard_help,
1731797ac58cSKevin Wolf };
1732797ac58cSKevin Wolf 
1733b32d7a39SMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv)
1734797ac58cSKevin Wolf {
1735797ac58cSKevin Wolf     struct timeval t1, t2;
1736dc38852aSEric Blake     bool Cflag = false, qflag = false;
1737797ac58cSKevin Wolf     int c, ret;
1738f5a5ca79SManos Pitsidianakis     int64_t offset, bytes;
1739797ac58cSKevin Wolf 
1740b062ad86SEric Blake     while ((c = getopt(argc, argv, "Cq")) != -1) {
1741797ac58cSKevin Wolf         switch (c) {
1742797ac58cSKevin Wolf         case 'C':
1743dc38852aSEric Blake             Cflag = true;
1744797ac58cSKevin Wolf             break;
1745797ac58cSKevin Wolf         case 'q':
1746dc38852aSEric Blake             qflag = true;
1747797ac58cSKevin Wolf             break;
1748797ac58cSKevin Wolf         default:
1749b444d0e9SMax Reitz             qemuio_command_usage(&discard_cmd);
1750b32d7a39SMax Reitz             return -EINVAL;
1751797ac58cSKevin Wolf         }
1752797ac58cSKevin Wolf     }
1753797ac58cSKevin Wolf 
1754797ac58cSKevin Wolf     if (optind != argc - 2) {
1755b444d0e9SMax Reitz         qemuio_command_usage(&discard_cmd);
1756b32d7a39SMax Reitz         return -EINVAL;
1757797ac58cSKevin Wolf     }
1758797ac58cSKevin Wolf 
1759797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1760797ac58cSKevin Wolf     if (offset < 0) {
1761a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1762b32d7a39SMax Reitz         return offset;
1763797ac58cSKevin Wolf     }
1764797ac58cSKevin Wolf 
1765797ac58cSKevin Wolf     optind++;
1766f5a5ca79SManos Pitsidianakis     bytes = cvtnum(argv[optind]);
1767f5a5ca79SManos Pitsidianakis     if (bytes < 0) {
1768f5a5ca79SManos Pitsidianakis         print_cvtnum_err(bytes, argv[optind]);
1769b32d7a39SMax Reitz         return bytes;
1770f5a5ca79SManos Pitsidianakis     } else if (bytes >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
17719b0beaf3SJohn Snow         printf("length cannot exceed %"PRIu64", given %s\n",
1772a3674679SMax Reitz                (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
17739b0beaf3SJohn Snow                argv[optind]);
1774b32d7a39SMax Reitz         return -EINVAL;
1775797ac58cSKevin Wolf     }
1776797ac58cSKevin Wolf 
1777797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1778f5a5ca79SManos Pitsidianakis     ret = blk_pdiscard(blk, offset, bytes);
1779797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1780797ac58cSKevin Wolf 
1781797ac58cSKevin Wolf     if (ret < 0) {
1782797ac58cSKevin Wolf         printf("discard failed: %s\n", strerror(-ret));
1783b32d7a39SMax Reitz         return ret;
1784797ac58cSKevin Wolf     }
1785797ac58cSKevin Wolf 
1786797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1787797ac58cSKevin Wolf     if (!qflag) {
1788797ac58cSKevin Wolf         t2 = tsub(t2, t1);
1789f5a5ca79SManos Pitsidianakis         print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
1790797ac58cSKevin Wolf     }
1791b32d7a39SMax Reitz 
1792b32d7a39SMax Reitz     return 0;
1793797ac58cSKevin Wolf }
1794797ac58cSKevin Wolf 
1795b32d7a39SMax Reitz static int alloc_f(BlockBackend *blk, int argc, char **argv)
1796797ac58cSKevin Wolf {
17974c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
1798d6a644bbSEric Blake     int64_t offset, start, remaining, count;
1799797ac58cSKevin Wolf     char s1[64];
1800d6a644bbSEric Blake     int ret;
1801d6a644bbSEric Blake     int64_t num, sum_alloc;
1802797ac58cSKevin Wolf 
1803d6a644bbSEric Blake     start = offset = cvtnum(argv[1]);
1804797ac58cSKevin Wolf     if (offset < 0) {
1805a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1806b32d7a39SMax Reitz         return offset;
1807797ac58cSKevin Wolf     }
1808797ac58cSKevin Wolf 
1809797ac58cSKevin Wolf     if (argc == 3) {
18104401fdc7SEric Blake         count = cvtnum(argv[2]);
18114401fdc7SEric Blake         if (count < 0) {
18124401fdc7SEric Blake             print_cvtnum_err(count, argv[2]);
1813b32d7a39SMax Reitz             return count;
1814797ac58cSKevin Wolf         }
1815797ac58cSKevin Wolf     } else {
18164401fdc7SEric Blake         count = BDRV_SECTOR_SIZE;
1817797ac58cSKevin Wolf     }
1818797ac58cSKevin Wolf 
1819d6a644bbSEric Blake     remaining = count;
1820797ac58cSKevin Wolf     sum_alloc = 0;
1821797ac58cSKevin Wolf     while (remaining) {
1822d6a644bbSEric Blake         ret = bdrv_is_allocated(bs, offset, remaining, &num);
1823d663640cSPaolo Bonzini         if (ret < 0) {
1824d663640cSPaolo Bonzini             printf("is_allocated failed: %s\n", strerror(-ret));
1825b32d7a39SMax Reitz             return ret;
1826d663640cSPaolo Bonzini         }
1827d6a644bbSEric Blake         offset += num;
1828797ac58cSKevin Wolf         remaining -= num;
1829797ac58cSKevin Wolf         if (ret) {
1830797ac58cSKevin Wolf             sum_alloc += num;
1831797ac58cSKevin Wolf         }
1832797ac58cSKevin Wolf         if (num == 0) {
1833d6a644bbSEric Blake             count -= remaining;
1834797ac58cSKevin Wolf             remaining = 0;
1835797ac58cSKevin Wolf         }
1836797ac58cSKevin Wolf     }
1837797ac58cSKevin Wolf 
1838d6a644bbSEric Blake     cvtstr(start, s1, sizeof(s1));
1839797ac58cSKevin Wolf 
18404401fdc7SEric Blake     printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
1841d6a644bbSEric Blake            sum_alloc, count, s1);
1842b32d7a39SMax Reitz     return 0;
1843797ac58cSKevin Wolf }
1844797ac58cSKevin Wolf 
1845797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = {
1846797ac58cSKevin Wolf     .name       = "alloc",
1847797ac58cSKevin Wolf     .altname    = "a",
1848797ac58cSKevin Wolf     .argmin     = 1,
1849797ac58cSKevin Wolf     .argmax     = 2,
1850797ac58cSKevin Wolf     .cfunc      = alloc_f,
18514401fdc7SEric Blake     .args       = "offset [count]",
18524401fdc7SEric Blake     .oneline    = "checks if offset is allocated in the file",
1853797ac58cSKevin Wolf };
1854797ac58cSKevin Wolf 
1855797ac58cSKevin Wolf 
1856d6a644bbSEric Blake static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1857d6a644bbSEric Blake                             int64_t bytes, int64_t *pnum)
1858797ac58cSKevin Wolf {
1859d6a644bbSEric Blake     int64_t num;
1860d6a644bbSEric Blake     int num_checked;
1861797ac58cSKevin Wolf     int ret, firstret;
1862797ac58cSKevin Wolf 
1863d6a644bbSEric Blake     num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1864d6a644bbSEric Blake     ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1865797ac58cSKevin Wolf     if (ret < 0) {
1866797ac58cSKevin Wolf         return ret;
1867797ac58cSKevin Wolf     }
1868797ac58cSKevin Wolf 
1869797ac58cSKevin Wolf     firstret = ret;
1870797ac58cSKevin Wolf     *pnum = num;
1871797ac58cSKevin Wolf 
1872d6a644bbSEric Blake     while (bytes > 0 && ret == firstret) {
1873d6a644bbSEric Blake         offset += num;
1874d6a644bbSEric Blake         bytes -= num;
1875797ac58cSKevin Wolf 
1876d6a644bbSEric Blake         num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1877d6a644bbSEric Blake         ret = bdrv_is_allocated(bs, offset, num_checked, &num);
18784b25bbc4SMax Reitz         if (ret == firstret && num) {
1879797ac58cSKevin Wolf             *pnum += num;
1880797ac58cSKevin Wolf         } else {
1881797ac58cSKevin Wolf             break;
1882797ac58cSKevin Wolf         }
1883797ac58cSKevin Wolf     }
1884797ac58cSKevin Wolf 
1885797ac58cSKevin Wolf     return firstret;
1886797ac58cSKevin Wolf }
1887797ac58cSKevin Wolf 
1888b32d7a39SMax Reitz static int map_f(BlockBackend *blk, int argc, char **argv)
1889797ac58cSKevin Wolf {
1890d6a644bbSEric Blake     int64_t offset, bytes;
18916f3c90afSEric Blake     char s1[64], s2[64];
1892797ac58cSKevin Wolf     int64_t num;
1893797ac58cSKevin Wolf     int ret;
1894797ac58cSKevin Wolf     const char *retstr;
1895797ac58cSKevin Wolf 
1896797ac58cSKevin Wolf     offset = 0;
1897d6a644bbSEric Blake     bytes = blk_getlength(blk);
1898d6a644bbSEric Blake     if (bytes < 0) {
1899d6a644bbSEric Blake         error_report("Failed to query image length: %s", strerror(-bytes));
1900b32d7a39SMax Reitz         return bytes;
19014c7b7e9bSMax Reitz     }
19024c7b7e9bSMax Reitz 
1903d6a644bbSEric Blake     while (bytes) {
1904d6a644bbSEric Blake         ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
1905797ac58cSKevin Wolf         if (ret < 0) {
1906797ac58cSKevin Wolf             error_report("Failed to get allocation status: %s", strerror(-ret));
1907b32d7a39SMax Reitz             return ret;
19084b25bbc4SMax Reitz         } else if (!num) {
19094b25bbc4SMax Reitz             error_report("Unexpected end of image");
1910b32d7a39SMax Reitz             return -EIO;
1911797ac58cSKevin Wolf         }
1912797ac58cSKevin Wolf 
1913797ac58cSKevin Wolf         retstr = ret ? "    allocated" : "not allocated";
1914d6a644bbSEric Blake         cvtstr(num, s1, sizeof(s1));
1915d6a644bbSEric Blake         cvtstr(offset, s2, sizeof(s2));
19166f3c90afSEric Blake         printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
1917d6a644bbSEric Blake                s1, num, retstr, s2, offset);
1918797ac58cSKevin Wolf 
1919797ac58cSKevin Wolf         offset += num;
1920d6a644bbSEric Blake         bytes -= num;
1921d6a644bbSEric Blake     }
1922b32d7a39SMax Reitz 
1923b32d7a39SMax Reitz     return 0;
1924797ac58cSKevin Wolf }
1925797ac58cSKevin Wolf 
1926797ac58cSKevin Wolf static const cmdinfo_t map_cmd = {
1927797ac58cSKevin Wolf        .name           = "map",
1928797ac58cSKevin Wolf        .argmin         = 0,
1929797ac58cSKevin Wolf        .argmax         = 0,
1930797ac58cSKevin Wolf        .cfunc          = map_f,
1931797ac58cSKevin Wolf        .args           = "",
1932797ac58cSKevin Wolf        .oneline        = "prints the allocated areas of a file",
1933797ac58cSKevin Wolf };
1934797ac58cSKevin Wolf 
19355bbd2e59SKevin Wolf static void reopen_help(void)
19365bbd2e59SKevin Wolf {
19375bbd2e59SKevin Wolf     printf(
19385bbd2e59SKevin Wolf "\n"
19395bbd2e59SKevin Wolf " Changes the open options of an already opened image\n"
19405bbd2e59SKevin Wolf "\n"
19415bbd2e59SKevin Wolf " Example:\n"
19425bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
19435bbd2e59SKevin Wolf "\n"
19445bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n"
1945ea92203cSKevin Wolf " -w, -- Reopen the image read-write\n"
19465bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n"
19475bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n"
19485bbd2e59SKevin Wolf "\n");
19495bbd2e59SKevin Wolf }
19505bbd2e59SKevin Wolf 
1951b32d7a39SMax Reitz static int reopen_f(BlockBackend *blk, int argc, char **argv);
19525bbd2e59SKevin Wolf 
19535bbd2e59SKevin Wolf static QemuOptsList reopen_opts = {
19545bbd2e59SKevin Wolf     .name = "reopen",
19555bbd2e59SKevin Wolf     .merge_lists = true,
19565bbd2e59SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
19575bbd2e59SKevin Wolf     .desc = {
19585bbd2e59SKevin Wolf         /* no elements => accept any params */
19595bbd2e59SKevin Wolf         { /* end of list */ }
19605bbd2e59SKevin Wolf     },
19615bbd2e59SKevin Wolf };
19625bbd2e59SKevin Wolf 
19635bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = {
19645bbd2e59SKevin Wolf        .name           = "reopen",
19655bbd2e59SKevin Wolf        .argmin         = 0,
19665bbd2e59SKevin Wolf        .argmax         = -1,
19675bbd2e59SKevin Wolf        .cfunc          = reopen_f,
1968ea92203cSKevin Wolf        .args           = "[(-r|-w)] [-c cache] [-o options]",
19695bbd2e59SKevin Wolf        .oneline        = "reopens an image with new options",
19705bbd2e59SKevin Wolf        .help           = reopen_help,
19715bbd2e59SKevin Wolf };
19725bbd2e59SKevin Wolf 
1973b32d7a39SMax Reitz static int reopen_f(BlockBackend *blk, int argc, char **argv)
19745bbd2e59SKevin Wolf {
19755bbd2e59SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
19765bbd2e59SKevin Wolf     QemuOpts *qopts;
19775bbd2e59SKevin Wolf     QDict *opts;
19785bbd2e59SKevin Wolf     int c;
19795bbd2e59SKevin Wolf     int flags = bs->open_flags;
198019dbecdcSKevin Wolf     bool writethrough = !blk_enable_write_cache(blk);
1981ea92203cSKevin Wolf     bool has_rw_option = false;
1982*dc900c35SAlberto Garcia     bool has_cache_option = false;
19835bbd2e59SKevin Wolf 
19845bbd2e59SKevin Wolf     BlockReopenQueue *brq;
19855bbd2e59SKevin Wolf     Error *local_err = NULL;
19865bbd2e59SKevin Wolf 
1987ea92203cSKevin Wolf     while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
19885bbd2e59SKevin Wolf         switch (c) {
19895bbd2e59SKevin Wolf         case 'c':
199019dbecdcSKevin Wolf             if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
19915bbd2e59SKevin Wolf                 error_report("Invalid cache option: %s", optarg);
1992b32d7a39SMax Reitz                 return -EINVAL;
19935bbd2e59SKevin Wolf             }
1994*dc900c35SAlberto Garcia             has_cache_option = true;
19955bbd2e59SKevin Wolf             break;
19965bbd2e59SKevin Wolf         case 'o':
19975bbd2e59SKevin Wolf             if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
19985bbd2e59SKevin Wolf                 qemu_opts_reset(&reopen_opts);
1999b32d7a39SMax Reitz                 return -EINVAL;
20005bbd2e59SKevin Wolf             }
20015bbd2e59SKevin Wolf             break;
20025bbd2e59SKevin Wolf         case 'r':
2003ea92203cSKevin Wolf             if (has_rw_option) {
2004ea92203cSKevin Wolf                 error_report("Only one -r/-w option may be given");
2005b32d7a39SMax Reitz                 return -EINVAL;
2006ea92203cSKevin Wolf             }
20075bbd2e59SKevin Wolf             flags &= ~BDRV_O_RDWR;
2008ea92203cSKevin Wolf             has_rw_option = true;
2009ea92203cSKevin Wolf             break;
2010ea92203cSKevin Wolf         case 'w':
2011ea92203cSKevin Wolf             if (has_rw_option) {
2012ea92203cSKevin Wolf                 error_report("Only one -r/-w option may be given");
2013b32d7a39SMax Reitz                 return -EINVAL;
2014ea92203cSKevin Wolf             }
2015ea92203cSKevin Wolf             flags |= BDRV_O_RDWR;
2016ea92203cSKevin Wolf             has_rw_option = true;
20175bbd2e59SKevin Wolf             break;
20185bbd2e59SKevin Wolf         default:
20195bbd2e59SKevin Wolf             qemu_opts_reset(&reopen_opts);
2020b444d0e9SMax Reitz             qemuio_command_usage(&reopen_cmd);
2021b32d7a39SMax Reitz             return -EINVAL;
20225bbd2e59SKevin Wolf         }
20235bbd2e59SKevin Wolf     }
20245bbd2e59SKevin Wolf 
20255bbd2e59SKevin Wolf     if (optind != argc) {
20265bbd2e59SKevin Wolf         qemu_opts_reset(&reopen_opts);
2027b444d0e9SMax Reitz         qemuio_command_usage(&reopen_cmd);
2028b32d7a39SMax Reitz         return -EINVAL;
20295bbd2e59SKevin Wolf     }
20305bbd2e59SKevin Wolf 
2031a8003ec4SAlberto Garcia     if (!writethrough != blk_enable_write_cache(blk) &&
203219dbecdcSKevin Wolf         blk_get_attached_dev(blk))
203319dbecdcSKevin Wolf     {
203419dbecdcSKevin Wolf         error_report("Cannot change cache.writeback: Device attached");
203519dbecdcSKevin Wolf         qemu_opts_reset(&reopen_opts);
2036b32d7a39SMax Reitz         return -EBUSY;
203719dbecdcSKevin Wolf     }
203819dbecdcSKevin Wolf 
2039f3adefb2SKevin Wolf     if (!(flags & BDRV_O_RDWR)) {
2040f3adefb2SKevin Wolf         uint64_t orig_perm, orig_shared_perm;
2041f3adefb2SKevin Wolf 
2042f3adefb2SKevin Wolf         bdrv_drain(bs);
2043f3adefb2SKevin Wolf 
2044f3adefb2SKevin Wolf         blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2045f3adefb2SKevin Wolf         blk_set_perm(blk,
2046f3adefb2SKevin Wolf                      orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2047f3adefb2SKevin Wolf                      orig_shared_perm,
2048f3adefb2SKevin Wolf                      &error_abort);
2049f3adefb2SKevin Wolf     }
2050f3adefb2SKevin Wolf 
20515bbd2e59SKevin Wolf     qopts = qemu_opts_find(&reopen_opts, NULL);
2052*dc900c35SAlberto Garcia     opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
20535bbd2e59SKevin Wolf     qemu_opts_reset(&reopen_opts);
20545bbd2e59SKevin Wolf 
2055*dc900c35SAlberto Garcia     if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2056*dc900c35SAlberto Garcia         if (has_rw_option) {
2057*dc900c35SAlberto Garcia             error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2058*dc900c35SAlberto Garcia             qobject_unref(opts);
2059*dc900c35SAlberto Garcia             return -EINVAL;
2060*dc900c35SAlberto Garcia         }
2061*dc900c35SAlberto Garcia     } else {
2062*dc900c35SAlberto Garcia         qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2063*dc900c35SAlberto Garcia     }
2064*dc900c35SAlberto Garcia 
2065*dc900c35SAlberto Garcia     if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2066*dc900c35SAlberto Garcia         qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2067*dc900c35SAlberto Garcia         if (has_cache_option) {
2068*dc900c35SAlberto Garcia             error_report("Cannot set both -c and the cache options");
2069*dc900c35SAlberto Garcia             qobject_unref(opts);
2070*dc900c35SAlberto Garcia             return -EINVAL;
2071*dc900c35SAlberto Garcia         }
2072*dc900c35SAlberto Garcia     } else {
2073*dc900c35SAlberto Garcia         qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2074*dc900c35SAlberto Garcia         qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2075*dc900c35SAlberto Garcia     }
2076*dc900c35SAlberto Garcia 
20771a63a907SKevin Wolf     bdrv_subtree_drained_begin(bs);
20785bbd2e59SKevin Wolf     brq = bdrv_reopen_queue(NULL, bs, opts, flags);
2079720150f3SPaolo Bonzini     bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
20801a63a907SKevin Wolf     bdrv_subtree_drained_end(bs);
20811a63a907SKevin Wolf 
20825bbd2e59SKevin Wolf     if (local_err) {
20835bbd2e59SKevin Wolf         error_report_err(local_err);
2084b32d7a39SMax Reitz         return -EINVAL;
20855bbd2e59SKevin Wolf     }
20865bbd2e59SKevin Wolf 
2087b32d7a39SMax Reitz     blk_set_enable_write_cache(blk, !writethrough);
2088b32d7a39SMax Reitz     return 0;
2089b32d7a39SMax Reitz }
2090b32d7a39SMax Reitz 
2091b32d7a39SMax Reitz static int break_f(BlockBackend *blk, int argc, char **argv)
2092797ac58cSKevin Wolf {
2093797ac58cSKevin Wolf     int ret;
2094797ac58cSKevin Wolf 
20954c7b7e9bSMax Reitz     ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2096797ac58cSKevin Wolf     if (ret < 0) {
2097797ac58cSKevin Wolf         printf("Could not set breakpoint: %s\n", strerror(-ret));
2098b32d7a39SMax Reitz         return ret;
2099797ac58cSKevin Wolf     }
2100797ac58cSKevin Wolf 
2101b32d7a39SMax Reitz     return 0;
2102b32d7a39SMax Reitz }
2103b32d7a39SMax Reitz 
2104b32d7a39SMax Reitz static int remove_break_f(BlockBackend *blk, int argc, char **argv)
21054cc70e93SFam Zheng {
21064cc70e93SFam Zheng     int ret;
21074cc70e93SFam Zheng 
21084c7b7e9bSMax Reitz     ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
21094cc70e93SFam Zheng     if (ret < 0) {
21104cc70e93SFam Zheng         printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2111b32d7a39SMax Reitz         return ret;
21124cc70e93SFam Zheng     }
2113b32d7a39SMax Reitz 
2114b32d7a39SMax Reitz     return 0;
21154cc70e93SFam Zheng }
21164cc70e93SFam Zheng 
2117797ac58cSKevin Wolf static const cmdinfo_t break_cmd = {
2118797ac58cSKevin Wolf        .name           = "break",
2119797ac58cSKevin Wolf        .argmin         = 2,
2120797ac58cSKevin Wolf        .argmax         = 2,
2121797ac58cSKevin Wolf        .cfunc          = break_f,
2122797ac58cSKevin Wolf        .args           = "event tag",
2123797ac58cSKevin Wolf        .oneline        = "sets a breakpoint on event and tags the stopped "
2124797ac58cSKevin Wolf                          "request as tag",
2125797ac58cSKevin Wolf };
2126797ac58cSKevin Wolf 
21274cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = {
21284cc70e93SFam Zheng        .name           = "remove_break",
21294cc70e93SFam Zheng        .argmin         = 1,
21304cc70e93SFam Zheng        .argmax         = 1,
21314cc70e93SFam Zheng        .cfunc          = remove_break_f,
21324cc70e93SFam Zheng        .args           = "tag",
21334cc70e93SFam Zheng        .oneline        = "remove a breakpoint by tag",
21344cc70e93SFam Zheng };
21354cc70e93SFam Zheng 
2136b32d7a39SMax Reitz static int resume_f(BlockBackend *blk, int argc, char **argv)
2137797ac58cSKevin Wolf {
2138797ac58cSKevin Wolf     int ret;
2139797ac58cSKevin Wolf 
21404c7b7e9bSMax Reitz     ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2141797ac58cSKevin Wolf     if (ret < 0) {
2142797ac58cSKevin Wolf         printf("Could not resume request: %s\n", strerror(-ret));
2143b32d7a39SMax Reitz         return ret;
2144797ac58cSKevin Wolf     }
2145b32d7a39SMax Reitz 
2146b32d7a39SMax Reitz     return 0;
2147797ac58cSKevin Wolf }
2148797ac58cSKevin Wolf 
2149797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = {
2150797ac58cSKevin Wolf        .name           = "resume",
2151797ac58cSKevin Wolf        .argmin         = 1,
2152797ac58cSKevin Wolf        .argmax         = 1,
2153797ac58cSKevin Wolf        .cfunc          = resume_f,
2154797ac58cSKevin Wolf        .args           = "tag",
2155797ac58cSKevin Wolf        .oneline        = "resumes the request tagged as tag",
2156797ac58cSKevin Wolf };
2157797ac58cSKevin Wolf 
2158b32d7a39SMax Reitz static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2159797ac58cSKevin Wolf {
21604c7b7e9bSMax Reitz     while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
21614c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
2162797ac58cSKevin Wolf     }
2163b32d7a39SMax Reitz     return 0;
2164797ac58cSKevin Wolf }
2165797ac58cSKevin Wolf 
2166797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = {
2167797ac58cSKevin Wolf        .name           = "wait_break",
2168797ac58cSKevin Wolf        .argmin         = 1,
2169797ac58cSKevin Wolf        .argmax         = 1,
2170797ac58cSKevin Wolf        .cfunc          = wait_break_f,
2171797ac58cSKevin Wolf        .args           = "tag",
2172797ac58cSKevin Wolf        .oneline        = "waits for the suspension of a request",
2173797ac58cSKevin Wolf };
2174797ac58cSKevin Wolf 
2175b32d7a39SMax Reitz static int abort_f(BlockBackend *blk, int argc, char **argv)
2176797ac58cSKevin Wolf {
2177797ac58cSKevin Wolf     abort();
2178797ac58cSKevin Wolf }
2179797ac58cSKevin Wolf 
2180797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = {
2181797ac58cSKevin Wolf        .name           = "abort",
2182797ac58cSKevin Wolf        .cfunc          = abort_f,
2183797ac58cSKevin Wolf        .flags          = CMD_NOFILE_OK,
2184797ac58cSKevin Wolf        .oneline        = "simulate a program crash using abort(3)",
2185797ac58cSKevin Wolf };
2186797ac58cSKevin Wolf 
21870e82dc7bSMax Reitz static void sigraise_help(void)
21880e82dc7bSMax Reitz {
21890e82dc7bSMax Reitz     printf(
21900e82dc7bSMax Reitz "\n"
21910e82dc7bSMax Reitz " raises the given signal\n"
21920e82dc7bSMax Reitz "\n"
21930e82dc7bSMax Reitz " Example:\n"
21940e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n"
21950e82dc7bSMax Reitz "\n"
21960e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
21970e82dc7bSMax Reitz " given to sigraise.\n"
21980e82dc7bSMax Reitz "\n", SIGTERM);
21990e82dc7bSMax Reitz }
22000e82dc7bSMax Reitz 
2201b32d7a39SMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv);
22020e82dc7bSMax Reitz 
22030e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = {
22040e82dc7bSMax Reitz     .name       = "sigraise",
22050e82dc7bSMax Reitz     .cfunc      = sigraise_f,
22060e82dc7bSMax Reitz     .argmin     = 1,
22070e82dc7bSMax Reitz     .argmax     = 1,
22080e82dc7bSMax Reitz     .flags      = CMD_NOFILE_OK,
22090e82dc7bSMax Reitz     .args       = "signal",
22100e82dc7bSMax Reitz     .oneline    = "raises a signal",
22110e82dc7bSMax Reitz     .help       = sigraise_help,
22120e82dc7bSMax Reitz };
22130e82dc7bSMax Reitz 
2214b32d7a39SMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv)
22150e82dc7bSMax Reitz {
22169b0beaf3SJohn Snow     int64_t sig = cvtnum(argv[1]);
22170e82dc7bSMax Reitz     if (sig < 0) {
2218a9ecfa00SJohn Snow         print_cvtnum_err(sig, argv[1]);
2219b32d7a39SMax Reitz         return sig;
22209b0beaf3SJohn Snow     } else if (sig > NSIG) {
22219b0beaf3SJohn Snow         printf("signal argument '%s' is too large to be a valid signal\n",
22229b0beaf3SJohn Snow                argv[1]);
2223b32d7a39SMax Reitz         return -EINVAL;
22240e82dc7bSMax Reitz     }
22250e82dc7bSMax Reitz 
22260e82dc7bSMax Reitz     /* Using raise() to kill this process does not necessarily flush all open
22270e82dc7bSMax Reitz      * streams. At least stdout and stderr (although the latter should be
22280e82dc7bSMax Reitz      * non-buffered anyway) should be flushed, though. */
22290e82dc7bSMax Reitz     fflush(stdout);
22300e82dc7bSMax Reitz     fflush(stderr);
22310e82dc7bSMax Reitz 
22320e82dc7bSMax Reitz     raise(sig);
2233b32d7a39SMax Reitz 
2234b32d7a39SMax Reitz     return 0;
22350e82dc7bSMax Reitz }
22360e82dc7bSMax Reitz 
2237cd33d02aSKevin Wolf static void sleep_cb(void *opaque)
2238cd33d02aSKevin Wolf {
2239cd33d02aSKevin Wolf     bool *expired = opaque;
2240cd33d02aSKevin Wolf     *expired = true;
2241cd33d02aSKevin Wolf }
2242cd33d02aSKevin Wolf 
2243b32d7a39SMax Reitz static int sleep_f(BlockBackend *blk, int argc, char **argv)
2244cd33d02aSKevin Wolf {
2245cd33d02aSKevin Wolf     char *endptr;
2246cd33d02aSKevin Wolf     long ms;
2247cd33d02aSKevin Wolf     struct QEMUTimer *timer;
2248cd33d02aSKevin Wolf     bool expired = false;
2249cd33d02aSKevin Wolf 
2250cd33d02aSKevin Wolf     ms = strtol(argv[1], &endptr, 0);
2251cd33d02aSKevin Wolf     if (ms < 0 || *endptr != '\0') {
2252cd33d02aSKevin Wolf         printf("%s is not a valid number\n", argv[1]);
2253b32d7a39SMax Reitz         return -EINVAL;
2254cd33d02aSKevin Wolf     }
2255cd33d02aSKevin Wolf 
2256cd33d02aSKevin Wolf     timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2257cd33d02aSKevin Wolf     timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2258cd33d02aSKevin Wolf 
2259cd33d02aSKevin Wolf     while (!expired) {
2260cd33d02aSKevin Wolf         main_loop_wait(false);
2261cd33d02aSKevin Wolf     }
2262cd33d02aSKevin Wolf 
2263cd33d02aSKevin Wolf     timer_free(timer);
2264b32d7a39SMax Reitz     return 0;
2265cd33d02aSKevin Wolf }
2266cd33d02aSKevin Wolf 
2267cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = {
2268cd33d02aSKevin Wolf        .name           = "sleep",
2269cd33d02aSKevin Wolf        .argmin         = 1,
2270cd33d02aSKevin Wolf        .argmax         = 1,
2271cd33d02aSKevin Wolf        .cfunc          = sleep_f,
2272cd33d02aSKevin Wolf        .flags          = CMD_NOFILE_OK,
2273cd33d02aSKevin Wolf        .oneline        = "waits for the given value in milliseconds",
2274cd33d02aSKevin Wolf };
2275cd33d02aSKevin Wolf 
2276f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2277f18a834aSKevin Wolf {
2278f18a834aSKevin Wolf     if (cmd) {
2279f18a834aSKevin Wolf         printf("%s ", cmd);
2280f18a834aSKevin Wolf     } else {
2281f18a834aSKevin Wolf         printf("%s ", ct->name);
2282f18a834aSKevin Wolf         if (ct->altname) {
2283f18a834aSKevin Wolf             printf("(or %s) ", ct->altname);
2284f18a834aSKevin Wolf         }
2285f18a834aSKevin Wolf     }
2286f18a834aSKevin Wolf 
2287f18a834aSKevin Wolf     if (ct->args) {
2288f18a834aSKevin Wolf         printf("%s ", ct->args);
2289f18a834aSKevin Wolf     }
2290f18a834aSKevin Wolf     printf("-- %s\n", ct->oneline);
2291f18a834aSKevin Wolf }
2292f18a834aSKevin Wolf 
2293f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2294f18a834aSKevin Wolf {
2295f18a834aSKevin Wolf     help_oneline(cmd, ct);
2296f18a834aSKevin Wolf     if (ct->help) {
2297f18a834aSKevin Wolf         ct->help();
2298f18a834aSKevin Wolf     }
2299f18a834aSKevin Wolf }
2300f18a834aSKevin Wolf 
2301f18a834aSKevin Wolf static void help_all(void)
2302f18a834aSKevin Wolf {
2303f18a834aSKevin Wolf     const cmdinfo_t *ct;
2304f18a834aSKevin Wolf 
2305f18a834aSKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2306f18a834aSKevin Wolf         help_oneline(ct->name, ct);
2307f18a834aSKevin Wolf     }
2308f18a834aSKevin Wolf     printf("\nUse 'help commandname' for extended help.\n");
2309f18a834aSKevin Wolf }
2310f18a834aSKevin Wolf 
2311b32d7a39SMax Reitz static int help_f(BlockBackend *blk, int argc, char **argv)
2312f18a834aSKevin Wolf {
2313f18a834aSKevin Wolf     const cmdinfo_t *ct;
2314f18a834aSKevin Wolf 
2315f18a834aSKevin Wolf     if (argc == 1) {
2316f18a834aSKevin Wolf         help_all();
2317b32d7a39SMax Reitz         return 0;
2318f18a834aSKevin Wolf     }
2319f18a834aSKevin Wolf 
2320f18a834aSKevin Wolf     ct = find_command(argv[1]);
2321f18a834aSKevin Wolf     if (ct == NULL) {
2322f18a834aSKevin Wolf         printf("command %s not found\n", argv[1]);
2323b32d7a39SMax Reitz         return -EINVAL;
2324f18a834aSKevin Wolf     }
2325f18a834aSKevin Wolf 
2326f18a834aSKevin Wolf     help_onecmd(argv[1], ct);
2327b32d7a39SMax Reitz     return 0;
2328f18a834aSKevin Wolf }
2329f18a834aSKevin Wolf 
2330f18a834aSKevin Wolf static const cmdinfo_t help_cmd = {
2331f18a834aSKevin Wolf     .name       = "help",
2332f18a834aSKevin Wolf     .altname    = "?",
2333f18a834aSKevin Wolf     .cfunc      = help_f,
2334f18a834aSKevin Wolf     .argmin     = 0,
2335f18a834aSKevin Wolf     .argmax     = 1,
2336f18a834aSKevin Wolf     .flags      = CMD_FLAG_GLOBAL,
2337f18a834aSKevin Wolf     .args       = "[command]",
2338f18a834aSKevin Wolf     .oneline    = "help for one or all commands",
2339f18a834aSKevin Wolf };
2340f18a834aSKevin Wolf 
2341b32d7a39SMax Reitz int qemuio_command(BlockBackend *blk, const char *cmd)
2342dd583296SKevin Wolf {
234315afd94aSPaolo Bonzini     AioContext *ctx;
2344dd583296SKevin Wolf     char *input;
2345dd583296SKevin Wolf     const cmdinfo_t *ct;
2346dd583296SKevin Wolf     char **v;
2347dd583296SKevin Wolf     int c;
2348b32d7a39SMax Reitz     int ret = 0;
2349dd583296SKevin Wolf 
2350dd583296SKevin Wolf     input = g_strdup(cmd);
2351dd583296SKevin Wolf     v = breakline(input, &c);
2352dd583296SKevin Wolf     if (c) {
2353dd583296SKevin Wolf         ct = find_command(v[0]);
2354dd583296SKevin Wolf         if (ct) {
235515afd94aSPaolo Bonzini             ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
235615afd94aSPaolo Bonzini             aio_context_acquire(ctx);
2357b32d7a39SMax Reitz             ret = command(blk, ct, c, v);
235815afd94aSPaolo Bonzini             aio_context_release(ctx);
2359dd583296SKevin Wolf         } else {
2360dd583296SKevin Wolf             fprintf(stderr, "command \"%s\" not found\n", v[0]);
2361b32d7a39SMax Reitz             ret = -EINVAL;
2362dd583296SKevin Wolf         }
2363dd583296SKevin Wolf     }
2364dd583296SKevin Wolf     g_free(input);
2365dd583296SKevin Wolf     g_free(v);
2366b32d7a39SMax Reitz 
2367b32d7a39SMax Reitz     return ret;
2368dd583296SKevin Wolf }
2369dd583296SKevin Wolf 
2370797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void)
2371797ac58cSKevin Wolf {
2372797ac58cSKevin Wolf     /* initialize commands */
2373c2cdf5c5SKevin Wolf     qemuio_add_command(&help_cmd);
2374c2cdf5c5SKevin Wolf     qemuio_add_command(&read_cmd);
2375c2cdf5c5SKevin Wolf     qemuio_add_command(&readv_cmd);
2376c2cdf5c5SKevin Wolf     qemuio_add_command(&write_cmd);
2377c2cdf5c5SKevin Wolf     qemuio_add_command(&writev_cmd);
2378c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_read_cmd);
2379c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_write_cmd);
2380c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_flush_cmd);
2381c2cdf5c5SKevin Wolf     qemuio_add_command(&flush_cmd);
2382c2cdf5c5SKevin Wolf     qemuio_add_command(&truncate_cmd);
2383c2cdf5c5SKevin Wolf     qemuio_add_command(&length_cmd);
2384c2cdf5c5SKevin Wolf     qemuio_add_command(&info_cmd);
2385c2cdf5c5SKevin Wolf     qemuio_add_command(&discard_cmd);
2386c2cdf5c5SKevin Wolf     qemuio_add_command(&alloc_cmd);
2387c2cdf5c5SKevin Wolf     qemuio_add_command(&map_cmd);
23885bbd2e59SKevin Wolf     qemuio_add_command(&reopen_cmd);
2389c2cdf5c5SKevin Wolf     qemuio_add_command(&break_cmd);
23904cc70e93SFam Zheng     qemuio_add_command(&remove_break_cmd);
2391c2cdf5c5SKevin Wolf     qemuio_add_command(&resume_cmd);
2392c2cdf5c5SKevin Wolf     qemuio_add_command(&wait_break_cmd);
2393c2cdf5c5SKevin Wolf     qemuio_add_command(&abort_cmd);
2394cd33d02aSKevin Wolf     qemuio_add_command(&sleep_cmd);
23950e82dc7bSMax Reitz     qemuio_add_command(&sigraise_cmd);
2396797ac58cSKevin Wolf }
2397