xref: /qemu/qemu-io-cmds.c (revision f17fd4fd)
1797ac58cSKevin Wolf /*
2797ac58cSKevin Wolf  * Command line utility to exercise the QEMU I/O path.
3797ac58cSKevin Wolf  *
4093ea232SEric Blake  * Copyright (C) 2009-2016 Red Hat, Inc.
5797ac58cSKevin Wolf  * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6797ac58cSKevin Wolf  *
7797ac58cSKevin Wolf  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8797ac58cSKevin Wolf  * See the COPYING file in the top-level directory.
9797ac58cSKevin Wolf  */
10797ac58cSKevin Wolf 
1180c71a24SPeter Maydell #include "qemu/osdep.h"
12da34e65cSMarkus Armbruster #include "qapi/error.h"
133d21994fSKevin Wolf #include "qemu-io.h"
144c7b7e9bSMax Reitz #include "sysemu/block-backend.h"
154c7b7e9bSMax Reitz #include "block/block.h"
164c7b7e9bSMax Reitz #include "block/block_int.h" /* for info_f() */
17a8d8ecb7SMax Reitz #include "block/qapi.h"
18d49b6836SMarkus Armbruster #include "qemu/error-report.h"
196a1751b7SAlex Bligh #include "qemu/main-loop.h"
20cd33d02aSKevin Wolf #include "qemu/timer.h"
21f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
22797ac58cSKevin Wolf 
23797ac58cSKevin Wolf #define CMD_NOFILE_OK   0x01
24797ac58cSKevin Wolf 
25f9883880SStefan Weil bool qemuio_misalign;
26797ac58cSKevin Wolf 
27c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab;
28c2cdf5c5SKevin Wolf static int ncmds;
29c2cdf5c5SKevin Wolf 
30c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b)
31c2cdf5c5SKevin Wolf {
32c2cdf5c5SKevin Wolf     return strcmp(((const cmdinfo_t *)a)->name,
33c2cdf5c5SKevin Wolf                   ((const cmdinfo_t *)b)->name);
34c2cdf5c5SKevin Wolf }
35c2cdf5c5SKevin Wolf 
36c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci)
37c2cdf5c5SKevin Wolf {
3802c4f26bSMarkus Armbruster     cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
39c2cdf5c5SKevin Wolf     cmdtab[ncmds - 1] = *ci;
40c2cdf5c5SKevin Wolf     qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
41c2cdf5c5SKevin Wolf }
42c2cdf5c5SKevin Wolf 
43c2cdf5c5SKevin Wolf int qemuio_command_usage(const cmdinfo_t *ci)
44c2cdf5c5SKevin Wolf {
45c2cdf5c5SKevin Wolf     printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
46c2cdf5c5SKevin Wolf     return 0;
47c2cdf5c5SKevin Wolf }
48c2cdf5c5SKevin Wolf 
494c7b7e9bSMax Reitz static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
50c2cdf5c5SKevin Wolf {
51c2cdf5c5SKevin Wolf     if (ct->flags & CMD_FLAG_GLOBAL) {
52c2cdf5c5SKevin Wolf         return 1;
53c2cdf5c5SKevin Wolf     }
544c7b7e9bSMax Reitz     if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
55c2cdf5c5SKevin Wolf         fprintf(stderr, "no file open, try 'help open'\n");
56c2cdf5c5SKevin Wolf         return 0;
57c2cdf5c5SKevin Wolf     }
58c2cdf5c5SKevin Wolf     return 1;
59c2cdf5c5SKevin Wolf }
60c2cdf5c5SKevin Wolf 
614c7b7e9bSMax Reitz static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
623d21994fSKevin Wolf                    char **argv)
63c2cdf5c5SKevin Wolf {
64c2cdf5c5SKevin Wolf     char *cmd = argv[0];
65c2cdf5c5SKevin Wolf 
664c7b7e9bSMax Reitz     if (!init_check_command(blk, ct)) {
67c2cdf5c5SKevin Wolf         return 0;
68c2cdf5c5SKevin Wolf     }
69c2cdf5c5SKevin Wolf 
70c2cdf5c5SKevin Wolf     if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
71c2cdf5c5SKevin Wolf         if (ct->argmax == -1) {
72c2cdf5c5SKevin Wolf             fprintf(stderr,
73c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected at least %d arguments\n",
74c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
75c2cdf5c5SKevin Wolf         } else if (ct->argmin == ct->argmax) {
76c2cdf5c5SKevin Wolf             fprintf(stderr,
77c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected %d arguments\n",
78c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
79c2cdf5c5SKevin Wolf         } else {
80c2cdf5c5SKevin Wolf             fprintf(stderr,
81c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected between %d and %d arguments\n",
82c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin, ct->argmax);
83c2cdf5c5SKevin Wolf         }
84c2cdf5c5SKevin Wolf         return 0;
85c2cdf5c5SKevin Wolf     }
86c2cdf5c5SKevin Wolf     optind = 0;
874c7b7e9bSMax Reitz     return ct->cfunc(blk, argc, argv);
88c2cdf5c5SKevin Wolf }
89c2cdf5c5SKevin Wolf 
90c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd)
91c2cdf5c5SKevin Wolf {
92c2cdf5c5SKevin Wolf     cmdinfo_t *ct;
93c2cdf5c5SKevin Wolf 
94c2cdf5c5SKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
95c2cdf5c5SKevin Wolf         if (strcmp(ct->name, cmd) == 0 ||
96c2cdf5c5SKevin Wolf             (ct->altname && strcmp(ct->altname, cmd) == 0))
97c2cdf5c5SKevin Wolf         {
98c2cdf5c5SKevin Wolf             return (const cmdinfo_t *)ct;
99c2cdf5c5SKevin Wolf         }
100c2cdf5c5SKevin Wolf     }
101c2cdf5c5SKevin Wolf     return NULL;
102c2cdf5c5SKevin Wolf }
103c2cdf5c5SKevin Wolf 
1044694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */
1054694020dSStefan Hajnoczi void qemuio_complete_command(const char *input,
1064694020dSStefan Hajnoczi                              void (*fn)(const char *cmd, void *opaque),
1074694020dSStefan Hajnoczi                              void *opaque)
1084694020dSStefan Hajnoczi {
1094694020dSStefan Hajnoczi     cmdinfo_t *ct;
1104694020dSStefan Hajnoczi     size_t input_len = strlen(input);
1114694020dSStefan Hajnoczi 
1124694020dSStefan Hajnoczi     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
1134694020dSStefan Hajnoczi         if (strncmp(input, ct->name, input_len) == 0) {
1144694020dSStefan Hajnoczi             fn(ct->name, opaque);
1154694020dSStefan Hajnoczi         }
1164694020dSStefan Hajnoczi     }
1174694020dSStefan Hajnoczi }
1184694020dSStefan Hajnoczi 
119c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count)
120c2cdf5c5SKevin Wolf {
121c2cdf5c5SKevin Wolf     int c = 0;
122c2cdf5c5SKevin Wolf     char *p;
1235839e53bSMarkus Armbruster     char **rval = g_new0(char *, 1);
124c2cdf5c5SKevin Wolf 
125c2cdf5c5SKevin Wolf     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
126c2cdf5c5SKevin Wolf         if (!*p) {
127c2cdf5c5SKevin Wolf             continue;
128c2cdf5c5SKevin Wolf         }
129c2cdf5c5SKevin Wolf         c++;
13008193dd5SMarkus Armbruster         rval = g_renew(char *, rval, (c + 1));
131c2cdf5c5SKevin Wolf         rval[c - 1] = p;
132c2cdf5c5SKevin Wolf         rval[c] = NULL;
133c2cdf5c5SKevin Wolf     }
134c2cdf5c5SKevin Wolf     *count = c;
135c2cdf5c5SKevin Wolf     return rval;
136c2cdf5c5SKevin Wolf }
137c2cdf5c5SKevin Wolf 
138797ac58cSKevin Wolf static int64_t cvtnum(const char *s)
139797ac58cSKevin Wolf {
140*f17fd4fdSMarkus Armbruster     int err;
141*f17fd4fdSMarkus Armbruster     int64_t value;
142ef5a7885SJohn Snow 
143*f17fd4fdSMarkus Armbruster     err = qemu_strtosz(s, NULL, &value);
144*f17fd4fdSMarkus Armbruster     if (err < 0) {
145*f17fd4fdSMarkus Armbruster         return err;
146*f17fd4fdSMarkus Armbruster     }
147*f17fd4fdSMarkus Armbruster     return value;
148797ac58cSKevin Wolf }
149797ac58cSKevin Wolf 
150a9ecfa00SJohn Snow static void print_cvtnum_err(int64_t rc, const char *arg)
151a9ecfa00SJohn Snow {
152a9ecfa00SJohn Snow     switch (rc) {
153a9ecfa00SJohn Snow     case -EINVAL:
154a9ecfa00SJohn Snow         printf("Parsing error: non-numeric argument,"
155a9ecfa00SJohn Snow                " or extraneous/unrecognized suffix -- %s\n", arg);
156a9ecfa00SJohn Snow         break;
157a9ecfa00SJohn Snow     case -ERANGE:
158a9ecfa00SJohn Snow         printf("Parsing error: argument too large -- %s\n", arg);
159a9ecfa00SJohn Snow         break;
160a9ecfa00SJohn Snow     default:
161a9ecfa00SJohn Snow         printf("Parsing error: %s\n", arg);
162a9ecfa00SJohn Snow     }
163a9ecfa00SJohn Snow }
164a9ecfa00SJohn Snow 
1650b613881SKevin Wolf #define EXABYTES(x)     ((long long)(x) << 60)
1660b613881SKevin Wolf #define PETABYTES(x)    ((long long)(x) << 50)
1670b613881SKevin Wolf #define TERABYTES(x)    ((long long)(x) << 40)
1680b613881SKevin Wolf #define GIGABYTES(x)    ((long long)(x) << 30)
1690b613881SKevin Wolf #define MEGABYTES(x)    ((long long)(x) << 20)
1700b613881SKevin Wolf #define KILOBYTES(x)    ((long long)(x) << 10)
1710b613881SKevin Wolf 
1720b613881SKevin Wolf #define TO_EXABYTES(x)  ((x) / EXABYTES(1))
1730b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1))
1740b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1))
1750b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
1760b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
1770b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
1780b613881SKevin Wolf 
1790b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size)
1800b613881SKevin Wolf {
1810b613881SKevin Wolf     char *trim;
1820b613881SKevin Wolf     const char *suffix;
1830b613881SKevin Wolf 
1840b613881SKevin Wolf     if (value >= EXABYTES(1)) {
1850b613881SKevin Wolf         suffix = " EiB";
1860b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
1870b613881SKevin Wolf     } else if (value >= PETABYTES(1)) {
1880b613881SKevin Wolf         suffix = " PiB";
1890b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
1900b613881SKevin Wolf     } else if (value >= TERABYTES(1)) {
1910b613881SKevin Wolf         suffix = " TiB";
1920b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
1930b613881SKevin Wolf     } else if (value >= GIGABYTES(1)) {
1940b613881SKevin Wolf         suffix = " GiB";
1950b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
1960b613881SKevin Wolf     } else if (value >= MEGABYTES(1)) {
1970b613881SKevin Wolf         suffix = " MiB";
1980b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
1990b613881SKevin Wolf     } else if (value >= KILOBYTES(1)) {
2000b613881SKevin Wolf         suffix = " KiB";
2010b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
2020b613881SKevin Wolf     } else {
2030b613881SKevin Wolf         suffix = " bytes";
2040b613881SKevin Wolf         snprintf(str, size - 6, "%f", value);
2050b613881SKevin Wolf     }
2060b613881SKevin Wolf 
2070b613881SKevin Wolf     trim = strstr(str, ".000");
2080b613881SKevin Wolf     if (trim) {
2090b613881SKevin Wolf         strcpy(trim, suffix);
2100b613881SKevin Wolf     } else {
2110b613881SKevin Wolf         strcat(str, suffix);
2120b613881SKevin Wolf     }
2130b613881SKevin Wolf }
2140b613881SKevin Wolf 
2150b613881SKevin Wolf 
2160b613881SKevin Wolf 
2170b613881SKevin Wolf static struct timeval tsub(struct timeval t1, struct timeval t2)
2180b613881SKevin Wolf {
2190b613881SKevin Wolf     t1.tv_usec -= t2.tv_usec;
2200b613881SKevin Wolf     if (t1.tv_usec < 0) {
2210b613881SKevin Wolf         t1.tv_usec += 1000000;
2220b613881SKevin Wolf         t1.tv_sec--;
2230b613881SKevin Wolf     }
2240b613881SKevin Wolf     t1.tv_sec -= t2.tv_sec;
2250b613881SKevin Wolf     return t1;
2260b613881SKevin Wolf }
2270b613881SKevin Wolf 
2280b613881SKevin Wolf static double tdiv(double value, struct timeval tv)
2290b613881SKevin Wolf {
2300b613881SKevin Wolf     return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
2310b613881SKevin Wolf }
2320b613881SKevin Wolf 
2330b613881SKevin Wolf #define HOURS(sec)      ((sec) / (60 * 60))
2340b613881SKevin Wolf #define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
2350b613881SKevin Wolf #define SECONDS(sec)    ((sec) % 60)
2360b613881SKevin Wolf 
2370b613881SKevin Wolf enum {
2380b613881SKevin Wolf     DEFAULT_TIME        = 0x0,
2390b613881SKevin Wolf     TERSE_FIXED_TIME    = 0x1,
2400b613881SKevin Wolf     VERBOSE_FIXED_TIME  = 0x2,
2410b613881SKevin Wolf };
2420b613881SKevin Wolf 
2430b613881SKevin Wolf static void timestr(struct timeval *tv, char *ts, size_t size, int format)
2440b613881SKevin Wolf {
2450b613881SKevin Wolf     double usec = (double)tv->tv_usec / 1000000.0;
2460b613881SKevin Wolf 
2470b613881SKevin Wolf     if (format & TERSE_FIXED_TIME) {
2480b613881SKevin Wolf         if (!HOURS(tv->tv_sec)) {
2490b613881SKevin Wolf             snprintf(ts, size, "%u:%02u.%02u",
2500b613881SKevin Wolf                     (unsigned int) MINUTES(tv->tv_sec),
2510b613881SKevin Wolf                     (unsigned int) SECONDS(tv->tv_sec),
2520b613881SKevin Wolf                     (unsigned int) (usec * 100));
2530b613881SKevin Wolf             return;
2540b613881SKevin Wolf         }
2550b613881SKevin Wolf         format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
2560b613881SKevin Wolf     }
2570b613881SKevin Wolf 
2580b613881SKevin Wolf     if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
2590b613881SKevin Wolf         snprintf(ts, size, "%u:%02u:%02u.%02u",
2600b613881SKevin Wolf                 (unsigned int) HOURS(tv->tv_sec),
2610b613881SKevin Wolf                 (unsigned int) MINUTES(tv->tv_sec),
2620b613881SKevin Wolf                 (unsigned int) SECONDS(tv->tv_sec),
2630b613881SKevin Wolf                 (unsigned int) (usec * 100));
2640b613881SKevin Wolf     } else {
2650b613881SKevin Wolf         snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
2660b613881SKevin Wolf     }
2670b613881SKevin Wolf }
2680b613881SKevin Wolf 
269797ac58cSKevin Wolf /*
270797ac58cSKevin Wolf  * Parse the pattern argument to various sub-commands.
271797ac58cSKevin Wolf  *
272797ac58cSKevin Wolf  * Because the pattern is used as an argument to memset it must evaluate
273797ac58cSKevin Wolf  * to an unsigned integer that fits into a single byte.
274797ac58cSKevin Wolf  */
275797ac58cSKevin Wolf static int parse_pattern(const char *arg)
276797ac58cSKevin Wolf {
277797ac58cSKevin Wolf     char *endptr = NULL;
278797ac58cSKevin Wolf     long pattern;
279797ac58cSKevin Wolf 
280797ac58cSKevin Wolf     pattern = strtol(arg, &endptr, 0);
281797ac58cSKevin Wolf     if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
282797ac58cSKevin Wolf         printf("%s is not a valid pattern byte\n", arg);
283797ac58cSKevin Wolf         return -1;
284797ac58cSKevin Wolf     }
285797ac58cSKevin Wolf 
286797ac58cSKevin Wolf     return pattern;
287797ac58cSKevin Wolf }
288797ac58cSKevin Wolf 
289797ac58cSKevin Wolf /*
290797ac58cSKevin Wolf  * Memory allocation helpers.
291797ac58cSKevin Wolf  *
292797ac58cSKevin Wolf  * Make sure memory is aligned by default, or purposefully misaligned if
293797ac58cSKevin Wolf  * that is specified on the command line.
294797ac58cSKevin Wolf  */
295797ac58cSKevin Wolf 
296797ac58cSKevin Wolf #define MISALIGN_OFFSET     16
2974c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
298797ac58cSKevin Wolf {
299797ac58cSKevin Wolf     void *buf;
300797ac58cSKevin Wolf 
301797ac58cSKevin Wolf     if (qemuio_misalign) {
302797ac58cSKevin Wolf         len += MISALIGN_OFFSET;
303797ac58cSKevin Wolf     }
3044c7b7e9bSMax Reitz     buf = blk_blockalign(blk, len);
305797ac58cSKevin Wolf     memset(buf, pattern, len);
306797ac58cSKevin Wolf     if (qemuio_misalign) {
307797ac58cSKevin Wolf         buf += MISALIGN_OFFSET;
308797ac58cSKevin Wolf     }
309797ac58cSKevin Wolf     return buf;
310797ac58cSKevin Wolf }
311797ac58cSKevin Wolf 
312797ac58cSKevin Wolf static void qemu_io_free(void *p)
313797ac58cSKevin Wolf {
314797ac58cSKevin Wolf     if (qemuio_misalign) {
315797ac58cSKevin Wolf         p -= MISALIGN_OFFSET;
316797ac58cSKevin Wolf     }
317797ac58cSKevin Wolf     qemu_vfree(p);
318797ac58cSKevin Wolf }
319797ac58cSKevin Wolf 
3209b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
321797ac58cSKevin Wolf {
3229b0beaf3SJohn Snow     uint64_t i;
3239b0beaf3SJohn Snow     int j;
324797ac58cSKevin Wolf     const uint8_t *p;
325797ac58cSKevin Wolf 
326797ac58cSKevin Wolf     for (i = 0, p = buffer; i < len; i += 16) {
327797ac58cSKevin Wolf         const uint8_t *s = p;
328797ac58cSKevin Wolf 
329797ac58cSKevin Wolf         printf("%08" PRIx64 ":  ", offset + i);
330797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, p++) {
331797ac58cSKevin Wolf             printf("%02x ", *p);
332797ac58cSKevin Wolf         }
333797ac58cSKevin Wolf         printf(" ");
334797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, s++) {
335797ac58cSKevin Wolf             if (isalnum(*s)) {
336797ac58cSKevin Wolf                 printf("%c", *s);
337797ac58cSKevin Wolf             } else {
338797ac58cSKevin Wolf                 printf(".");
339797ac58cSKevin Wolf             }
340797ac58cSKevin Wolf         }
341797ac58cSKevin Wolf         printf("\n");
342797ac58cSKevin Wolf     }
343797ac58cSKevin Wolf }
344797ac58cSKevin Wolf 
345797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset,
346dc38852aSEric Blake                          int64_t count, int64_t total, int cnt, bool Cflag)
347797ac58cSKevin Wolf {
348797ac58cSKevin Wolf     char s1[64], s2[64], ts[64];
349797ac58cSKevin Wolf 
350797ac58cSKevin Wolf     timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
351797ac58cSKevin Wolf     if (!Cflag) {
352797ac58cSKevin Wolf         cvtstr((double)total, s1, sizeof(s1));
353797ac58cSKevin Wolf         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
3549b0beaf3SJohn Snow         printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
355797ac58cSKevin Wolf                op, total, count, offset);
356797ac58cSKevin Wolf         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
357797ac58cSKevin Wolf                s1, cnt, ts, s2, tdiv((double)cnt, *t));
358797ac58cSKevin Wolf     } else {/* bytes,ops,time,bytes/sec,ops/sec */
3599b0beaf3SJohn Snow         printf("%"PRId64",%d,%s,%.3f,%.3f\n",
360797ac58cSKevin Wolf             total, cnt, ts,
361797ac58cSKevin Wolf             tdiv((double)total, *t),
362797ac58cSKevin Wolf             tdiv((double)cnt, *t));
363797ac58cSKevin Wolf     }
364797ac58cSKevin Wolf }
365797ac58cSKevin Wolf 
366797ac58cSKevin Wolf /*
367797ac58cSKevin Wolf  * Parse multiple length statements for vectored I/O, and construct an I/O
368797ac58cSKevin Wolf  * vector matching it.
369797ac58cSKevin Wolf  */
370797ac58cSKevin Wolf static void *
3714c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
372797ac58cSKevin Wolf              int pattern)
373797ac58cSKevin Wolf {
374797ac58cSKevin Wolf     size_t *sizes = g_new0(size_t, nr_iov);
375797ac58cSKevin Wolf     size_t count = 0;
376797ac58cSKevin Wolf     void *buf = NULL;
377797ac58cSKevin Wolf     void *p;
378797ac58cSKevin Wolf     int i;
379797ac58cSKevin Wolf 
380797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
381797ac58cSKevin Wolf         char *arg = argv[i];
382797ac58cSKevin Wolf         int64_t len;
383797ac58cSKevin Wolf 
384797ac58cSKevin Wolf         len = cvtnum(arg);
385797ac58cSKevin Wolf         if (len < 0) {
386a9ecfa00SJohn Snow             print_cvtnum_err(len, arg);
387797ac58cSKevin Wolf             goto fail;
388797ac58cSKevin Wolf         }
389797ac58cSKevin Wolf 
3903026c468SAlberto Garcia         if (len > BDRV_REQUEST_MAX_BYTES) {
3913026c468SAlberto Garcia             printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
3923026c468SAlberto Garcia                    (uint64_t)BDRV_REQUEST_MAX_BYTES);
3933026c468SAlberto Garcia             goto fail;
3943026c468SAlberto Garcia         }
3953026c468SAlberto Garcia 
3963026c468SAlberto Garcia         if (count > BDRV_REQUEST_MAX_BYTES - len) {
3973026c468SAlberto Garcia             printf("The total number of bytes exceed the maximum size %" PRIu64
3983026c468SAlberto Garcia                    "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
399797ac58cSKevin Wolf             goto fail;
400797ac58cSKevin Wolf         }
401797ac58cSKevin Wolf 
402797ac58cSKevin Wolf         sizes[i] = len;
403797ac58cSKevin Wolf         count += len;
404797ac58cSKevin Wolf     }
405797ac58cSKevin Wolf 
406797ac58cSKevin Wolf     qemu_iovec_init(qiov, nr_iov);
407797ac58cSKevin Wolf 
4084c7b7e9bSMax Reitz     buf = p = qemu_io_alloc(blk, count, pattern);
409797ac58cSKevin Wolf 
410797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
411797ac58cSKevin Wolf         qemu_iovec_add(qiov, p, sizes[i]);
412797ac58cSKevin Wolf         p += sizes[i];
413797ac58cSKevin Wolf     }
414797ac58cSKevin Wolf 
415797ac58cSKevin Wolf fail:
416797ac58cSKevin Wolf     g_free(sizes);
417797ac58cSKevin Wolf     return buf;
418797ac58cSKevin Wolf }
419797ac58cSKevin Wolf 
4209b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
4219b0beaf3SJohn Snow                     int64_t count, int64_t *total)
422797ac58cSKevin Wolf {
4239b0beaf3SJohn Snow     if (count > INT_MAX) {
4249b0beaf3SJohn Snow         return -ERANGE;
4259b0beaf3SJohn Snow     }
4269b0beaf3SJohn Snow 
4274c7b7e9bSMax Reitz     *total = blk_pread(blk, offset, (uint8_t *)buf, count);
428797ac58cSKevin Wolf     if (*total < 0) {
429797ac58cSKevin Wolf         return *total;
430797ac58cSKevin Wolf     }
431797ac58cSKevin Wolf     return 1;
432797ac58cSKevin Wolf }
433797ac58cSKevin Wolf 
4349b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
435770e0e0eSEric Blake                      int64_t count, int flags, int64_t *total)
436797ac58cSKevin Wolf {
4379b0beaf3SJohn Snow     if (count > INT_MAX) {
4389b0beaf3SJohn Snow         return -ERANGE;
4399b0beaf3SJohn Snow     }
4409b0beaf3SJohn Snow 
441770e0e0eSEric Blake     *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, flags);
442797ac58cSKevin Wolf     if (*total < 0) {
443797ac58cSKevin Wolf         return *total;
444797ac58cSKevin Wolf     }
445797ac58cSKevin Wolf     return 1;
446797ac58cSKevin Wolf }
447797ac58cSKevin Wolf 
448797ac58cSKevin Wolf typedef struct {
4494c7b7e9bSMax Reitz     BlockBackend *blk;
450797ac58cSKevin Wolf     int64_t offset;
4519b0beaf3SJohn Snow     int64_t count;
4529b0beaf3SJohn Snow     int64_t *total;
453770e0e0eSEric Blake     int flags;
454797ac58cSKevin Wolf     int ret;
455797ac58cSKevin Wolf     bool done;
456797ac58cSKevin Wolf } CoWriteZeroes;
457797ac58cSKevin Wolf 
458d004bd52SEric Blake static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
459797ac58cSKevin Wolf {
460797ac58cSKevin Wolf     CoWriteZeroes *data = opaque;
461797ac58cSKevin Wolf 
462d004bd52SEric Blake     data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->count,
463770e0e0eSEric Blake                                      data->flags);
464797ac58cSKevin Wolf     data->done = true;
465797ac58cSKevin Wolf     if (data->ret < 0) {
466797ac58cSKevin Wolf         *data->total = data->ret;
467797ac58cSKevin Wolf         return;
468797ac58cSKevin Wolf     }
469797ac58cSKevin Wolf 
470797ac58cSKevin Wolf     *data->total = data->count;
471797ac58cSKevin Wolf }
472797ac58cSKevin Wolf 
473d004bd52SEric Blake static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
474d004bd52SEric Blake                                int64_t count, int flags, int64_t *total)
475797ac58cSKevin Wolf {
476797ac58cSKevin Wolf     Coroutine *co;
477797ac58cSKevin Wolf     CoWriteZeroes data = {
4784c7b7e9bSMax Reitz         .blk    = blk,
479797ac58cSKevin Wolf         .offset = offset,
480797ac58cSKevin Wolf         .count  = count,
481797ac58cSKevin Wolf         .total  = total,
482770e0e0eSEric Blake         .flags  = flags,
483797ac58cSKevin Wolf         .done   = false,
484797ac58cSKevin Wolf     };
485797ac58cSKevin Wolf 
486a3674679SMax Reitz     if (count > INT_MAX) {
4879b0beaf3SJohn Snow         return -ERANGE;
4889b0beaf3SJohn Snow     }
4899b0beaf3SJohn Snow 
4900b8b8753SPaolo Bonzini     co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
4910b8b8753SPaolo Bonzini     qemu_coroutine_enter(co);
492797ac58cSKevin Wolf     while (!data.done) {
4934c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
494797ac58cSKevin Wolf     }
495797ac58cSKevin Wolf     if (data.ret < 0) {
496797ac58cSKevin Wolf         return data.ret;
497797ac58cSKevin Wolf     } else {
498797ac58cSKevin Wolf         return 1;
499797ac58cSKevin Wolf     }
500797ac58cSKevin Wolf }
501797ac58cSKevin Wolf 
5024c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
5039b0beaf3SJohn Snow                                int64_t count, int64_t *total)
504797ac58cSKevin Wolf {
505797ac58cSKevin Wolf     int ret;
506797ac58cSKevin Wolf 
507a3674679SMax Reitz     if (count >> 9 > BDRV_REQUEST_MAX_SECTORS) {
5089b0beaf3SJohn Snow         return -ERANGE;
5099b0beaf3SJohn Snow     }
5109b0beaf3SJohn Snow 
511fe5c1355SPavel Butsykin     ret = blk_pwrite_compressed(blk, offset, buf, count);
512797ac58cSKevin Wolf     if (ret < 0) {
513797ac58cSKevin Wolf         return ret;
514797ac58cSKevin Wolf     }
515797ac58cSKevin Wolf     *total = count;
516797ac58cSKevin Wolf     return 1;
517797ac58cSKevin Wolf }
518797ac58cSKevin Wolf 
5194c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5209b0beaf3SJohn Snow                            int64_t count, int64_t *total)
521797ac58cSKevin Wolf {
5229b0beaf3SJohn Snow     if (count > INT_MAX) {
5239b0beaf3SJohn Snow         return -ERANGE;
5249b0beaf3SJohn Snow     }
5259b0beaf3SJohn Snow 
5264c7b7e9bSMax Reitz     *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
527797ac58cSKevin Wolf     if (*total < 0) {
528797ac58cSKevin Wolf         return *total;
529797ac58cSKevin Wolf     }
530797ac58cSKevin Wolf     return 1;
531797ac58cSKevin Wolf }
532797ac58cSKevin Wolf 
5334c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5349b0beaf3SJohn Snow                            int64_t count, int64_t *total)
535797ac58cSKevin Wolf {
5369b0beaf3SJohn Snow     if (count > INT_MAX) {
5379b0beaf3SJohn Snow         return -ERANGE;
5389b0beaf3SJohn Snow     }
5399b0beaf3SJohn Snow 
5404c7b7e9bSMax Reitz     *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
541797ac58cSKevin Wolf     if (*total < 0) {
542797ac58cSKevin Wolf         return *total;
543797ac58cSKevin Wolf     }
544797ac58cSKevin Wolf     return 1;
545797ac58cSKevin Wolf }
546797ac58cSKevin Wolf 
547797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff
548797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret)
549797ac58cSKevin Wolf {
550797ac58cSKevin Wolf     *(int *)opaque = ret;
551797ac58cSKevin Wolf }
552797ac58cSKevin Wolf 
5534c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
554797ac58cSKevin Wolf                         int64_t offset, int *total)
555797ac58cSKevin Wolf {
556797ac58cSKevin Wolf     int async_ret = NOT_DONE;
557797ac58cSKevin Wolf 
5587b3f9712SEric Blake     blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
559797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
560797ac58cSKevin Wolf         main_loop_wait(false);
561797ac58cSKevin Wolf     }
562797ac58cSKevin Wolf 
563797ac58cSKevin Wolf     *total = qiov->size;
564797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
565797ac58cSKevin Wolf }
566797ac58cSKevin Wolf 
5674c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
568770e0e0eSEric Blake                          int64_t offset, int flags, int *total)
569797ac58cSKevin Wolf {
570797ac58cSKevin Wolf     int async_ret = NOT_DONE;
571797ac58cSKevin Wolf 
572770e0e0eSEric Blake     blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
573797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
574797ac58cSKevin Wolf         main_loop_wait(false);
575797ac58cSKevin Wolf     }
576797ac58cSKevin Wolf 
577797ac58cSKevin Wolf     *total = qiov->size;
578797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
579797ac58cSKevin Wolf }
580797ac58cSKevin Wolf 
581797ac58cSKevin Wolf static void read_help(void)
582797ac58cSKevin Wolf {
583797ac58cSKevin Wolf     printf(
584797ac58cSKevin Wolf "\n"
585797ac58cSKevin Wolf " reads a range of bytes from the given offset\n"
586797ac58cSKevin Wolf "\n"
587797ac58cSKevin Wolf " Example:\n"
588797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
589797ac58cSKevin Wolf "\n"
590797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
591797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
592797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n"
593797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
594797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n"
595093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
596797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
597797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
598797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n"
599797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
600797ac58cSKevin Wolf "\n");
601797ac58cSKevin Wolf }
602797ac58cSKevin Wolf 
6034c7b7e9bSMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv);
604797ac58cSKevin Wolf 
605797ac58cSKevin Wolf static const cmdinfo_t read_cmd = {
606797ac58cSKevin Wolf     .name       = "read",
607797ac58cSKevin Wolf     .altname    = "r",
608797ac58cSKevin Wolf     .cfunc      = read_f,
609797ac58cSKevin Wolf     .argmin     = 2,
610797ac58cSKevin Wolf     .argmax     = -1,
611093ea232SEric Blake     .args       = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
612797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
613797ac58cSKevin Wolf     .help       = read_help,
614797ac58cSKevin Wolf };
615797ac58cSKevin Wolf 
6164c7b7e9bSMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv)
617797ac58cSKevin Wolf {
618797ac58cSKevin Wolf     struct timeval t1, t2;
619093ea232SEric Blake     bool Cflag = false, qflag = false, vflag = false;
620dc38852aSEric Blake     bool Pflag = false, sflag = false, lflag = false, bflag = false;
621797ac58cSKevin Wolf     int c, cnt;
622797ac58cSKevin Wolf     char *buf;
623797ac58cSKevin Wolf     int64_t offset;
6249b0beaf3SJohn Snow     int64_t count;
625797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
6269b0beaf3SJohn Snow     int64_t total = 0;
6279b0beaf3SJohn Snow     int pattern = 0;
6289b0beaf3SJohn Snow     int64_t pattern_offset = 0, pattern_count = 0;
629797ac58cSKevin Wolf 
630b062ad86SEric Blake     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
631797ac58cSKevin Wolf         switch (c) {
632797ac58cSKevin Wolf         case 'b':
633dc38852aSEric Blake             bflag = true;
634797ac58cSKevin Wolf             break;
635797ac58cSKevin Wolf         case 'C':
636dc38852aSEric Blake             Cflag = true;
637797ac58cSKevin Wolf             break;
638797ac58cSKevin Wolf         case 'l':
639dc38852aSEric Blake             lflag = true;
640797ac58cSKevin Wolf             pattern_count = cvtnum(optarg);
641797ac58cSKevin Wolf             if (pattern_count < 0) {
642a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_count, optarg);
643797ac58cSKevin Wolf                 return 0;
644797ac58cSKevin Wolf             }
645797ac58cSKevin Wolf             break;
646797ac58cSKevin Wolf         case 'p':
647093ea232SEric Blake             /* Ignored for backwards compatibility */
648797ac58cSKevin Wolf             break;
649797ac58cSKevin Wolf         case 'P':
650dc38852aSEric Blake             Pflag = true;
651797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
652797ac58cSKevin Wolf             if (pattern < 0) {
653797ac58cSKevin Wolf                 return 0;
654797ac58cSKevin Wolf             }
655797ac58cSKevin Wolf             break;
656797ac58cSKevin Wolf         case 'q':
657dc38852aSEric Blake             qflag = true;
658797ac58cSKevin Wolf             break;
659797ac58cSKevin Wolf         case 's':
660dc38852aSEric Blake             sflag = true;
661797ac58cSKevin Wolf             pattern_offset = cvtnum(optarg);
662797ac58cSKevin Wolf             if (pattern_offset < 0) {
663a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_offset, optarg);
664797ac58cSKevin Wolf                 return 0;
665797ac58cSKevin Wolf             }
666797ac58cSKevin Wolf             break;
667797ac58cSKevin Wolf         case 'v':
668dc38852aSEric Blake             vflag = true;
669797ac58cSKevin Wolf             break;
670797ac58cSKevin Wolf         default:
671c2cdf5c5SKevin Wolf             return qemuio_command_usage(&read_cmd);
672797ac58cSKevin Wolf         }
673797ac58cSKevin Wolf     }
674797ac58cSKevin Wolf 
675797ac58cSKevin Wolf     if (optind != argc - 2) {
676c2cdf5c5SKevin Wolf         return qemuio_command_usage(&read_cmd);
677797ac58cSKevin Wolf     }
678797ac58cSKevin Wolf 
679797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
680797ac58cSKevin Wolf     if (offset < 0) {
681a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
682797ac58cSKevin Wolf         return 0;
683797ac58cSKevin Wolf     }
684797ac58cSKevin Wolf 
685797ac58cSKevin Wolf     optind++;
686797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
687797ac58cSKevin Wolf     if (count < 0) {
688a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
689797ac58cSKevin Wolf         return 0;
6903026c468SAlberto Garcia     } else if (count > BDRV_REQUEST_MAX_BYTES) {
6919b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
6923026c468SAlberto Garcia                (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
6939b0beaf3SJohn Snow         return 0;
694797ac58cSKevin Wolf     }
695797ac58cSKevin Wolf 
696797ac58cSKevin Wolf     if (!Pflag && (lflag || sflag)) {
697c2cdf5c5SKevin Wolf         return qemuio_command_usage(&read_cmd);
698797ac58cSKevin Wolf     }
699797ac58cSKevin Wolf 
700797ac58cSKevin Wolf     if (!lflag) {
701797ac58cSKevin Wolf         pattern_count = count - pattern_offset;
702797ac58cSKevin Wolf     }
703797ac58cSKevin Wolf 
704797ac58cSKevin Wolf     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
705797ac58cSKevin Wolf         printf("pattern verification range exceeds end of read data\n");
706797ac58cSKevin Wolf         return 0;
707797ac58cSKevin Wolf     }
708797ac58cSKevin Wolf 
709093ea232SEric Blake     if (bflag) {
710797ac58cSKevin Wolf         if (offset & 0x1ff) {
711797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
712797ac58cSKevin Wolf                    offset);
713797ac58cSKevin Wolf             return 0;
714797ac58cSKevin Wolf         }
715797ac58cSKevin Wolf         if (count & 0x1ff) {
7169b0beaf3SJohn Snow             printf("count %"PRId64" is not sector aligned\n",
717797ac58cSKevin Wolf                    count);
718797ac58cSKevin Wolf             return 0;
719797ac58cSKevin Wolf         }
720797ac58cSKevin Wolf     }
721797ac58cSKevin Wolf 
7224c7b7e9bSMax Reitz     buf = qemu_io_alloc(blk, count, 0xab);
723797ac58cSKevin Wolf 
724797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
7257b3f9712SEric Blake     if (bflag) {
7264c7b7e9bSMax Reitz         cnt = do_load_vmstate(blk, buf, offset, count, &total);
727797ac58cSKevin Wolf     } else {
7287b3f9712SEric Blake         cnt = do_pread(blk, buf, offset, count, &total);
729797ac58cSKevin Wolf     }
730797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
731797ac58cSKevin Wolf 
732797ac58cSKevin Wolf     if (cnt < 0) {
733797ac58cSKevin Wolf         printf("read failed: %s\n", strerror(-cnt));
734797ac58cSKevin Wolf         goto out;
735797ac58cSKevin Wolf     }
736797ac58cSKevin Wolf 
737797ac58cSKevin Wolf     if (Pflag) {
738797ac58cSKevin Wolf         void *cmp_buf = g_malloc(pattern_count);
739797ac58cSKevin Wolf         memset(cmp_buf, pattern, pattern_count);
740797ac58cSKevin Wolf         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
741797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
7429b0beaf3SJohn Snow                    PRId64 ", %"PRId64" bytes\n",
743797ac58cSKevin Wolf                    offset + pattern_offset, pattern_count);
744797ac58cSKevin Wolf         }
745797ac58cSKevin Wolf         g_free(cmp_buf);
746797ac58cSKevin Wolf     }
747797ac58cSKevin Wolf 
748797ac58cSKevin Wolf     if (qflag) {
749797ac58cSKevin Wolf         goto out;
750797ac58cSKevin Wolf     }
751797ac58cSKevin Wolf 
752797ac58cSKevin Wolf     if (vflag) {
753797ac58cSKevin Wolf         dump_buffer(buf, offset, count);
754797ac58cSKevin Wolf     }
755797ac58cSKevin Wolf 
756797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
757797ac58cSKevin Wolf     t2 = tsub(t2, t1);
758797ac58cSKevin Wolf     print_report("read", &t2, offset, count, total, cnt, Cflag);
759797ac58cSKevin Wolf 
760797ac58cSKevin Wolf out:
761797ac58cSKevin Wolf     qemu_io_free(buf);
762797ac58cSKevin Wolf 
763797ac58cSKevin Wolf     return 0;
764797ac58cSKevin Wolf }
765797ac58cSKevin Wolf 
766797ac58cSKevin Wolf static void readv_help(void)
767797ac58cSKevin Wolf {
768797ac58cSKevin Wolf     printf(
769797ac58cSKevin Wolf "\n"
770797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n"
771797ac58cSKevin Wolf "\n"
772797ac58cSKevin Wolf " Example:\n"
773797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
774797ac58cSKevin Wolf "\n"
775797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
776797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
777797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n"
778797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
779797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
780797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
781797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
782797ac58cSKevin Wolf "\n");
783797ac58cSKevin Wolf }
784797ac58cSKevin Wolf 
7854c7b7e9bSMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv);
786797ac58cSKevin Wolf 
787797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = {
788797ac58cSKevin Wolf     .name       = "readv",
789797ac58cSKevin Wolf     .cfunc      = readv_f,
790797ac58cSKevin Wolf     .argmin     = 2,
791797ac58cSKevin Wolf     .argmax     = -1,
792797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern] off len [len..]",
793797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
794797ac58cSKevin Wolf     .help       = readv_help,
795797ac58cSKevin Wolf };
796797ac58cSKevin Wolf 
7974c7b7e9bSMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv)
798797ac58cSKevin Wolf {
799797ac58cSKevin Wolf     struct timeval t1, t2;
800dc38852aSEric Blake     bool Cflag = false, qflag = false, vflag = false;
801797ac58cSKevin Wolf     int c, cnt;
802797ac58cSKevin Wolf     char *buf;
803797ac58cSKevin Wolf     int64_t offset;
804797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
805797ac58cSKevin Wolf     int total = 0;
806797ac58cSKevin Wolf     int nr_iov;
807797ac58cSKevin Wolf     QEMUIOVector qiov;
808797ac58cSKevin Wolf     int pattern = 0;
809dc38852aSEric Blake     bool Pflag = false;
810797ac58cSKevin Wolf 
811b062ad86SEric Blake     while ((c = getopt(argc, argv, "CP:qv")) != -1) {
812797ac58cSKevin Wolf         switch (c) {
813797ac58cSKevin Wolf         case 'C':
814dc38852aSEric Blake             Cflag = true;
815797ac58cSKevin Wolf             break;
816797ac58cSKevin Wolf         case 'P':
817dc38852aSEric Blake             Pflag = true;
818797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
819797ac58cSKevin Wolf             if (pattern < 0) {
820797ac58cSKevin Wolf                 return 0;
821797ac58cSKevin Wolf             }
822797ac58cSKevin Wolf             break;
823797ac58cSKevin Wolf         case 'q':
824dc38852aSEric Blake             qflag = true;
825797ac58cSKevin Wolf             break;
826797ac58cSKevin Wolf         case 'v':
827dc38852aSEric Blake             vflag = true;
828797ac58cSKevin Wolf             break;
829797ac58cSKevin Wolf         default:
830c2cdf5c5SKevin Wolf             return qemuio_command_usage(&readv_cmd);
831797ac58cSKevin Wolf         }
832797ac58cSKevin Wolf     }
833797ac58cSKevin Wolf 
834797ac58cSKevin Wolf     if (optind > argc - 2) {
835c2cdf5c5SKevin Wolf         return qemuio_command_usage(&readv_cmd);
836797ac58cSKevin Wolf     }
837797ac58cSKevin Wolf 
838797ac58cSKevin Wolf 
839797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
840797ac58cSKevin Wolf     if (offset < 0) {
841a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
842797ac58cSKevin Wolf         return 0;
843797ac58cSKevin Wolf     }
844797ac58cSKevin Wolf     optind++;
845797ac58cSKevin Wolf 
846797ac58cSKevin Wolf     nr_iov = argc - optind;
8474c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
848797ac58cSKevin Wolf     if (buf == NULL) {
849797ac58cSKevin Wolf         return 0;
850797ac58cSKevin Wolf     }
851797ac58cSKevin Wolf 
852797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
8534c7b7e9bSMax Reitz     cnt = do_aio_readv(blk, &qiov, offset, &total);
854797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
855797ac58cSKevin Wolf 
856797ac58cSKevin Wolf     if (cnt < 0) {
857797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-cnt));
858797ac58cSKevin Wolf         goto out;
859797ac58cSKevin Wolf     }
860797ac58cSKevin Wolf 
861797ac58cSKevin Wolf     if (Pflag) {
862797ac58cSKevin Wolf         void *cmp_buf = g_malloc(qiov.size);
863797ac58cSKevin Wolf         memset(cmp_buf, pattern, qiov.size);
864797ac58cSKevin Wolf         if (memcmp(buf, cmp_buf, qiov.size)) {
865797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
866797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", offset, qiov.size);
867797ac58cSKevin Wolf         }
868797ac58cSKevin Wolf         g_free(cmp_buf);
869797ac58cSKevin Wolf     }
870797ac58cSKevin Wolf 
871797ac58cSKevin Wolf     if (qflag) {
872797ac58cSKevin Wolf         goto out;
873797ac58cSKevin Wolf     }
874797ac58cSKevin Wolf 
875797ac58cSKevin Wolf     if (vflag) {
876797ac58cSKevin Wolf         dump_buffer(buf, offset, qiov.size);
877797ac58cSKevin Wolf     }
878797ac58cSKevin Wolf 
879797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
880797ac58cSKevin Wolf     t2 = tsub(t2, t1);
881797ac58cSKevin Wolf     print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
882797ac58cSKevin Wolf 
883797ac58cSKevin Wolf out:
884797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
885797ac58cSKevin Wolf     qemu_io_free(buf);
886797ac58cSKevin Wolf     return 0;
887797ac58cSKevin Wolf }
888797ac58cSKevin Wolf 
889797ac58cSKevin Wolf static void write_help(void)
890797ac58cSKevin Wolf {
891797ac58cSKevin Wolf     printf(
892797ac58cSKevin Wolf "\n"
893797ac58cSKevin Wolf " writes a range of bytes from the given offset\n"
894797ac58cSKevin Wolf "\n"
895797ac58cSKevin Wolf " Example:\n"
896797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
897797ac58cSKevin Wolf "\n"
898797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
899797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
900797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n"
9014c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n"
902770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
903093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
904797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
905797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
906797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
907c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
908d004bd52SEric Blake " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
909797ac58cSKevin Wolf "\n");
910797ac58cSKevin Wolf }
911797ac58cSKevin Wolf 
9124c7b7e9bSMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv);
913797ac58cSKevin Wolf 
914797ac58cSKevin Wolf static const cmdinfo_t write_cmd = {
915797ac58cSKevin Wolf     .name       = "write",
916797ac58cSKevin Wolf     .altname    = "w",
917797ac58cSKevin Wolf     .cfunc      = write_f,
918797ac58cSKevin Wolf     .argmin     = 2,
919797ac58cSKevin Wolf     .argmax     = -1,
920c2e001ccSEric Blake     .args       = "[-bcCfquz] [-P pattern] off len",
921797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
922797ac58cSKevin Wolf     .help       = write_help,
923797ac58cSKevin Wolf };
924797ac58cSKevin Wolf 
9254c7b7e9bSMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv)
926797ac58cSKevin Wolf {
927797ac58cSKevin Wolf     struct timeval t1, t2;
928093ea232SEric Blake     bool Cflag = false, qflag = false, bflag = false;
929dc38852aSEric Blake     bool Pflag = false, zflag = false, cflag = false;
930770e0e0eSEric Blake     int flags = 0;
931797ac58cSKevin Wolf     int c, cnt;
932797ac58cSKevin Wolf     char *buf = NULL;
933797ac58cSKevin Wolf     int64_t offset;
9349b0beaf3SJohn Snow     int64_t count;
935797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
9369b0beaf3SJohn Snow     int64_t total = 0;
937797ac58cSKevin Wolf     int pattern = 0xcd;
938797ac58cSKevin Wolf 
939c2e001ccSEric Blake     while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
940797ac58cSKevin Wolf         switch (c) {
941797ac58cSKevin Wolf         case 'b':
942dc38852aSEric Blake             bflag = true;
943797ac58cSKevin Wolf             break;
944797ac58cSKevin Wolf         case 'c':
945dc38852aSEric Blake             cflag = true;
946797ac58cSKevin Wolf             break;
947797ac58cSKevin Wolf         case 'C':
948dc38852aSEric Blake             Cflag = true;
949797ac58cSKevin Wolf             break;
950770e0e0eSEric Blake         case 'f':
951770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
952770e0e0eSEric Blake             break;
953797ac58cSKevin Wolf         case 'p':
954093ea232SEric Blake             /* Ignored for backwards compatibility */
955797ac58cSKevin Wolf             break;
956797ac58cSKevin Wolf         case 'P':
957dc38852aSEric Blake             Pflag = true;
958797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
959797ac58cSKevin Wolf             if (pattern < 0) {
960797ac58cSKevin Wolf                 return 0;
961797ac58cSKevin Wolf             }
962797ac58cSKevin Wolf             break;
963797ac58cSKevin Wolf         case 'q':
964dc38852aSEric Blake             qflag = true;
965797ac58cSKevin Wolf             break;
966c2e001ccSEric Blake         case 'u':
967c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
968c2e001ccSEric Blake             break;
969797ac58cSKevin Wolf         case 'z':
970dc38852aSEric Blake             zflag = true;
971797ac58cSKevin Wolf             break;
972797ac58cSKevin Wolf         default:
973c2cdf5c5SKevin Wolf             return qemuio_command_usage(&write_cmd);
974797ac58cSKevin Wolf         }
975797ac58cSKevin Wolf     }
976797ac58cSKevin Wolf 
977797ac58cSKevin Wolf     if (optind != argc - 2) {
978c2cdf5c5SKevin Wolf         return qemuio_command_usage(&write_cmd);
979797ac58cSKevin Wolf     }
980797ac58cSKevin Wolf 
981093ea232SEric Blake     if (bflag && zflag) {
982093ea232SEric Blake         printf("-b and -z cannot be specified at the same time\n");
983797ac58cSKevin Wolf         return 0;
984797ac58cSKevin Wolf     }
985797ac58cSKevin Wolf 
986770e0e0eSEric Blake     if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
987770e0e0eSEric Blake         printf("-f and -b or -c cannot be specified at the same time\n");
988770e0e0eSEric Blake         return 0;
989770e0e0eSEric Blake     }
990770e0e0eSEric Blake 
991c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
992c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
993c2e001ccSEric Blake         return 0;
994c2e001ccSEric Blake     }
995c2e001ccSEric Blake 
996797ac58cSKevin Wolf     if (zflag && Pflag) {
997797ac58cSKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
998797ac58cSKevin Wolf         return 0;
999797ac58cSKevin Wolf     }
1000797ac58cSKevin Wolf 
1001797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1002797ac58cSKevin Wolf     if (offset < 0) {
1003a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1004797ac58cSKevin Wolf         return 0;
1005797ac58cSKevin Wolf     }
1006797ac58cSKevin Wolf 
1007797ac58cSKevin Wolf     optind++;
1008797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1009797ac58cSKevin Wolf     if (count < 0) {
1010a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
1011797ac58cSKevin Wolf         return 0;
10123026c468SAlberto Garcia     } else if (count > BDRV_REQUEST_MAX_BYTES) {
10139b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
10143026c468SAlberto Garcia                (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
10159b0beaf3SJohn Snow         return 0;
1016797ac58cSKevin Wolf     }
1017797ac58cSKevin Wolf 
1018093ea232SEric Blake     if (bflag || cflag) {
1019797ac58cSKevin Wolf         if (offset & 0x1ff) {
1020797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
1021797ac58cSKevin Wolf                    offset);
1022797ac58cSKevin Wolf             return 0;
1023797ac58cSKevin Wolf         }
1024797ac58cSKevin Wolf 
1025797ac58cSKevin Wolf         if (count & 0x1ff) {
10269b0beaf3SJohn Snow             printf("count %"PRId64" is not sector aligned\n",
1027797ac58cSKevin Wolf                    count);
1028797ac58cSKevin Wolf             return 0;
1029797ac58cSKevin Wolf         }
1030797ac58cSKevin Wolf     }
1031797ac58cSKevin Wolf 
1032797ac58cSKevin Wolf     if (!zflag) {
10334c7b7e9bSMax Reitz         buf = qemu_io_alloc(blk, count, pattern);
1034797ac58cSKevin Wolf     }
1035797ac58cSKevin Wolf 
1036797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
10377b3f9712SEric Blake     if (bflag) {
10384c7b7e9bSMax Reitz         cnt = do_save_vmstate(blk, buf, offset, count, &total);
1039797ac58cSKevin Wolf     } else if (zflag) {
1040d004bd52SEric Blake         cnt = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
1041797ac58cSKevin Wolf     } else if (cflag) {
10424c7b7e9bSMax Reitz         cnt = do_write_compressed(blk, buf, offset, count, &total);
1043797ac58cSKevin Wolf     } else {
1044770e0e0eSEric Blake         cnt = do_pwrite(blk, buf, offset, count, flags, &total);
1045797ac58cSKevin Wolf     }
1046797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1047797ac58cSKevin Wolf 
1048797ac58cSKevin Wolf     if (cnt < 0) {
1049797ac58cSKevin Wolf         printf("write failed: %s\n", strerror(-cnt));
1050797ac58cSKevin Wolf         goto out;
1051797ac58cSKevin Wolf     }
1052797ac58cSKevin Wolf 
1053797ac58cSKevin Wolf     if (qflag) {
1054797ac58cSKevin Wolf         goto out;
1055797ac58cSKevin Wolf     }
1056797ac58cSKevin Wolf 
1057797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1058797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1059797ac58cSKevin Wolf     print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1060797ac58cSKevin Wolf 
1061797ac58cSKevin Wolf out:
1062797ac58cSKevin Wolf     if (!zflag) {
1063797ac58cSKevin Wolf         qemu_io_free(buf);
1064797ac58cSKevin Wolf     }
1065797ac58cSKevin Wolf 
1066797ac58cSKevin Wolf     return 0;
1067797ac58cSKevin Wolf }
1068797ac58cSKevin Wolf 
1069797ac58cSKevin Wolf static void
1070797ac58cSKevin Wolf writev_help(void)
1071797ac58cSKevin Wolf {
1072797ac58cSKevin Wolf     printf(
1073797ac58cSKevin Wolf "\n"
1074797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n"
1075797ac58cSKevin Wolf "\n"
1076797ac58cSKevin Wolf " Example:\n"
10776e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1078797ac58cSKevin Wolf "\n"
1079797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1080797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1081797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1082797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1083770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
1084797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1085797ac58cSKevin Wolf "\n");
1086797ac58cSKevin Wolf }
1087797ac58cSKevin Wolf 
10884c7b7e9bSMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv);
1089797ac58cSKevin Wolf 
1090797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = {
1091797ac58cSKevin Wolf     .name       = "writev",
1092797ac58cSKevin Wolf     .cfunc      = writev_f,
1093797ac58cSKevin Wolf     .argmin     = 2,
1094797ac58cSKevin Wolf     .argmax     = -1,
1095770e0e0eSEric Blake     .args       = "[-Cfq] [-P pattern] off len [len..]",
1096797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
1097797ac58cSKevin Wolf     .help       = writev_help,
1098797ac58cSKevin Wolf };
1099797ac58cSKevin Wolf 
11004c7b7e9bSMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv)
1101797ac58cSKevin Wolf {
1102797ac58cSKevin Wolf     struct timeval t1, t2;
1103dc38852aSEric Blake     bool Cflag = false, qflag = false;
1104770e0e0eSEric Blake     int flags = 0;
1105797ac58cSKevin Wolf     int c, cnt;
1106797ac58cSKevin Wolf     char *buf;
1107797ac58cSKevin Wolf     int64_t offset;
1108797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1109797ac58cSKevin Wolf     int total = 0;
1110797ac58cSKevin Wolf     int nr_iov;
1111797ac58cSKevin Wolf     int pattern = 0xcd;
1112797ac58cSKevin Wolf     QEMUIOVector qiov;
1113797ac58cSKevin Wolf 
11144ca1d340SEric Blake     while ((c = getopt(argc, argv, "CfqP:")) != -1) {
1115797ac58cSKevin Wolf         switch (c) {
1116797ac58cSKevin Wolf         case 'C':
1117dc38852aSEric Blake             Cflag = true;
1118797ac58cSKevin Wolf             break;
1119770e0e0eSEric Blake         case 'f':
1120770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1121770e0e0eSEric Blake             break;
1122797ac58cSKevin Wolf         case 'q':
1123dc38852aSEric Blake             qflag = true;
1124797ac58cSKevin Wolf             break;
1125797ac58cSKevin Wolf         case 'P':
1126797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1127797ac58cSKevin Wolf             if (pattern < 0) {
1128797ac58cSKevin Wolf                 return 0;
1129797ac58cSKevin Wolf             }
1130797ac58cSKevin Wolf             break;
1131797ac58cSKevin Wolf         default:
1132c2cdf5c5SKevin Wolf             return qemuio_command_usage(&writev_cmd);
1133797ac58cSKevin Wolf         }
1134797ac58cSKevin Wolf     }
1135797ac58cSKevin Wolf 
1136797ac58cSKevin Wolf     if (optind > argc - 2) {
1137c2cdf5c5SKevin Wolf         return qemuio_command_usage(&writev_cmd);
1138797ac58cSKevin Wolf     }
1139797ac58cSKevin Wolf 
1140797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1141797ac58cSKevin Wolf     if (offset < 0) {
1142a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1143797ac58cSKevin Wolf         return 0;
1144797ac58cSKevin Wolf     }
1145797ac58cSKevin Wolf     optind++;
1146797ac58cSKevin Wolf 
1147797ac58cSKevin Wolf     nr_iov = argc - optind;
11484c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1149797ac58cSKevin Wolf     if (buf == NULL) {
1150797ac58cSKevin Wolf         return 0;
1151797ac58cSKevin Wolf     }
1152797ac58cSKevin Wolf 
1153797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1154770e0e0eSEric Blake     cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
1155797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1156797ac58cSKevin Wolf 
1157797ac58cSKevin Wolf     if (cnt < 0) {
1158797ac58cSKevin Wolf         printf("writev failed: %s\n", strerror(-cnt));
1159797ac58cSKevin Wolf         goto out;
1160797ac58cSKevin Wolf     }
1161797ac58cSKevin Wolf 
1162797ac58cSKevin Wolf     if (qflag) {
1163797ac58cSKevin Wolf         goto out;
1164797ac58cSKevin Wolf     }
1165797ac58cSKevin Wolf 
1166797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1167797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1168797ac58cSKevin Wolf     print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1169797ac58cSKevin Wolf out:
1170797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
1171797ac58cSKevin Wolf     qemu_io_free(buf);
1172797ac58cSKevin Wolf     return 0;
1173797ac58cSKevin Wolf }
1174797ac58cSKevin Wolf 
1175797ac58cSKevin Wolf struct aio_ctx {
11764c7b7e9bSMax Reitz     BlockBackend *blk;
1177797ac58cSKevin Wolf     QEMUIOVector qiov;
1178797ac58cSKevin Wolf     int64_t offset;
1179797ac58cSKevin Wolf     char *buf;
1180dc38852aSEric Blake     bool qflag;
1181dc38852aSEric Blake     bool vflag;
1182dc38852aSEric Blake     bool Cflag;
1183dc38852aSEric Blake     bool Pflag;
1184dc38852aSEric Blake     bool zflag;
1185a91f9584SFam Zheng     BlockAcctCookie acct;
1186797ac58cSKevin Wolf     int pattern;
1187797ac58cSKevin Wolf     struct timeval t1;
1188797ac58cSKevin Wolf };
1189797ac58cSKevin Wolf 
1190797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret)
1191797ac58cSKevin Wolf {
1192797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1193797ac58cSKevin Wolf     struct timeval t2;
1194797ac58cSKevin Wolf 
1195797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1196797ac58cSKevin Wolf 
1197797ac58cSKevin Wolf 
1198797ac58cSKevin Wolf     if (ret < 0) {
1199797ac58cSKevin Wolf         printf("aio_write failed: %s\n", strerror(-ret));
1200556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1201797ac58cSKevin Wolf         goto out;
1202797ac58cSKevin Wolf     }
1203797ac58cSKevin Wolf 
12044c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1205a91f9584SFam Zheng 
1206797ac58cSKevin Wolf     if (ctx->qflag) {
1207797ac58cSKevin Wolf         goto out;
1208797ac58cSKevin Wolf     }
1209797ac58cSKevin Wolf 
1210797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1211797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1212797ac58cSKevin Wolf     print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1213797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1214797ac58cSKevin Wolf out:
12155ceb7765SKevin Wolf     if (!ctx->zflag) {
1216797ac58cSKevin Wolf         qemu_io_free(ctx->buf);
1217797ac58cSKevin Wolf         qemu_iovec_destroy(&ctx->qiov);
12185ceb7765SKevin Wolf     }
1219797ac58cSKevin Wolf     g_free(ctx);
1220797ac58cSKevin Wolf }
1221797ac58cSKevin Wolf 
1222797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret)
1223797ac58cSKevin Wolf {
1224797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1225797ac58cSKevin Wolf     struct timeval t2;
1226797ac58cSKevin Wolf 
1227797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1228797ac58cSKevin Wolf 
1229797ac58cSKevin Wolf     if (ret < 0) {
1230797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-ret));
1231556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1232797ac58cSKevin Wolf         goto out;
1233797ac58cSKevin Wolf     }
1234797ac58cSKevin Wolf 
1235797ac58cSKevin Wolf     if (ctx->Pflag) {
1236797ac58cSKevin Wolf         void *cmp_buf = g_malloc(ctx->qiov.size);
1237797ac58cSKevin Wolf 
1238797ac58cSKevin Wolf         memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1239797ac58cSKevin Wolf         if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1240797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
1241797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1242797ac58cSKevin Wolf         }
1243797ac58cSKevin Wolf         g_free(cmp_buf);
1244797ac58cSKevin Wolf     }
1245797ac58cSKevin Wolf 
12464c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1247a91f9584SFam Zheng 
1248797ac58cSKevin Wolf     if (ctx->qflag) {
1249797ac58cSKevin Wolf         goto out;
1250797ac58cSKevin Wolf     }
1251797ac58cSKevin Wolf 
1252797ac58cSKevin Wolf     if (ctx->vflag) {
1253797ac58cSKevin Wolf         dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1254797ac58cSKevin Wolf     }
1255797ac58cSKevin Wolf 
1256797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1257797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1258797ac58cSKevin Wolf     print_report("read", &t2, ctx->offset, ctx->qiov.size,
1259797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1260797ac58cSKevin Wolf out:
1261797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1262797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1263797ac58cSKevin Wolf     g_free(ctx);
1264797ac58cSKevin Wolf }
1265797ac58cSKevin Wolf 
1266797ac58cSKevin Wolf static void aio_read_help(void)
1267797ac58cSKevin Wolf {
1268797ac58cSKevin Wolf     printf(
1269797ac58cSKevin Wolf "\n"
1270797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n"
1271797ac58cSKevin Wolf "\n"
1272797ac58cSKevin Wolf " Example:\n"
1273797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1274797ac58cSKevin Wolf "\n"
1275797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
1276797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
1277797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n"
1278797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1279797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1280797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
128137546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n"
1282797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
1283797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1284797ac58cSKevin Wolf "\n");
1285797ac58cSKevin Wolf }
1286797ac58cSKevin Wolf 
12874c7b7e9bSMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1288797ac58cSKevin Wolf 
1289797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = {
1290797ac58cSKevin Wolf     .name       = "aio_read",
1291797ac58cSKevin Wolf     .cfunc      = aio_read_f,
1292797ac58cSKevin Wolf     .argmin     = 2,
1293797ac58cSKevin Wolf     .argmax     = -1,
129437546ff2SEric Blake     .args       = "[-Ciqv] [-P pattern] off len [len..]",
1295797ac58cSKevin Wolf     .oneline    = "asynchronously reads a number of bytes",
1296797ac58cSKevin Wolf     .help       = aio_read_help,
1297797ac58cSKevin Wolf };
1298797ac58cSKevin Wolf 
12994c7b7e9bSMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1300797ac58cSKevin Wolf {
1301797ac58cSKevin Wolf     int nr_iov, c;
1302797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1303797ac58cSKevin Wolf 
13044c7b7e9bSMax Reitz     ctx->blk = blk;
130537546ff2SEric Blake     while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
1306797ac58cSKevin Wolf         switch (c) {
1307797ac58cSKevin Wolf         case 'C':
1308dc38852aSEric Blake             ctx->Cflag = true;
1309797ac58cSKevin Wolf             break;
1310797ac58cSKevin Wolf         case 'P':
1311dc38852aSEric Blake             ctx->Pflag = true;
1312797ac58cSKevin Wolf             ctx->pattern = parse_pattern(optarg);
1313797ac58cSKevin Wolf             if (ctx->pattern < 0) {
1314797ac58cSKevin Wolf                 g_free(ctx);
1315797ac58cSKevin Wolf                 return 0;
1316797ac58cSKevin Wolf             }
1317797ac58cSKevin Wolf             break;
131837546ff2SEric Blake         case 'i':
131937546ff2SEric Blake             printf("injecting invalid read request\n");
132037546ff2SEric Blake             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
132137546ff2SEric Blake             g_free(ctx);
132237546ff2SEric Blake             return 0;
1323797ac58cSKevin Wolf         case 'q':
1324dc38852aSEric Blake             ctx->qflag = true;
1325797ac58cSKevin Wolf             break;
1326797ac58cSKevin Wolf         case 'v':
1327dc38852aSEric Blake             ctx->vflag = true;
1328797ac58cSKevin Wolf             break;
1329797ac58cSKevin Wolf         default:
1330797ac58cSKevin Wolf             g_free(ctx);
1331c2cdf5c5SKevin Wolf             return qemuio_command_usage(&aio_read_cmd);
1332797ac58cSKevin Wolf         }
1333797ac58cSKevin Wolf     }
1334797ac58cSKevin Wolf 
1335797ac58cSKevin Wolf     if (optind > argc - 2) {
1336797ac58cSKevin Wolf         g_free(ctx);
1337c2cdf5c5SKevin Wolf         return qemuio_command_usage(&aio_read_cmd);
1338797ac58cSKevin Wolf     }
1339797ac58cSKevin Wolf 
1340797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1341797ac58cSKevin Wolf     if (ctx->offset < 0) {
1342a9ecfa00SJohn Snow         print_cvtnum_err(ctx->offset, argv[optind]);
1343797ac58cSKevin Wolf         g_free(ctx);
1344797ac58cSKevin Wolf         return 0;
1345797ac58cSKevin Wolf     }
1346797ac58cSKevin Wolf     optind++;
1347797ac58cSKevin Wolf 
1348797ac58cSKevin Wolf     nr_iov = argc - optind;
13494c7b7e9bSMax Reitz     ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1350797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1351556c2b60SAlberto Garcia         block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1352797ac58cSKevin Wolf         g_free(ctx);
1353797ac58cSKevin Wolf         return 0;
1354797ac58cSKevin Wolf     }
1355797ac58cSKevin Wolf 
1356797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
13574c7b7e9bSMax Reitz     block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
13584c7b7e9bSMax Reitz                      BLOCK_ACCT_READ);
13597b3f9712SEric Blake     blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1360797ac58cSKevin Wolf     return 0;
1361797ac58cSKevin Wolf }
1362797ac58cSKevin Wolf 
1363797ac58cSKevin Wolf static void aio_write_help(void)
1364797ac58cSKevin Wolf {
1365797ac58cSKevin Wolf     printf(
1366797ac58cSKevin Wolf "\n"
1367797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n"
1368797ac58cSKevin Wolf " from multiple buffers\n"
1369797ac58cSKevin Wolf "\n"
1370797ac58cSKevin Wolf " Example:\n"
1371797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1372797ac58cSKevin Wolf "\n"
1373797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1374797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1375797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n"
1376797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1377797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1378797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1379770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
138037546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n"
1381797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1382c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
1383d004bd52SEric Blake " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1384797ac58cSKevin Wolf "\n");
1385797ac58cSKevin Wolf }
1386797ac58cSKevin Wolf 
13874c7b7e9bSMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1388797ac58cSKevin Wolf 
1389797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = {
1390797ac58cSKevin Wolf     .name       = "aio_write",
1391797ac58cSKevin Wolf     .cfunc      = aio_write_f,
1392797ac58cSKevin Wolf     .argmin     = 2,
1393797ac58cSKevin Wolf     .argmax     = -1,
139437546ff2SEric Blake     .args       = "[-Cfiquz] [-P pattern] off len [len..]",
1395797ac58cSKevin Wolf     .oneline    = "asynchronously writes a number of bytes",
1396797ac58cSKevin Wolf     .help       = aio_write_help,
1397797ac58cSKevin Wolf };
1398797ac58cSKevin Wolf 
13994c7b7e9bSMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1400797ac58cSKevin Wolf {
1401797ac58cSKevin Wolf     int nr_iov, c;
1402797ac58cSKevin Wolf     int pattern = 0xcd;
1403797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1404770e0e0eSEric Blake     int flags = 0;
1405797ac58cSKevin Wolf 
14064c7b7e9bSMax Reitz     ctx->blk = blk;
140737546ff2SEric Blake     while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
1408797ac58cSKevin Wolf         switch (c) {
1409797ac58cSKevin Wolf         case 'C':
1410dc38852aSEric Blake             ctx->Cflag = true;
1411797ac58cSKevin Wolf             break;
1412770e0e0eSEric Blake         case 'f':
1413770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1414770e0e0eSEric Blake             break;
1415797ac58cSKevin Wolf         case 'q':
1416dc38852aSEric Blake             ctx->qflag = true;
1417797ac58cSKevin Wolf             break;
1418c2e001ccSEric Blake         case 'u':
1419c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1420c2e001ccSEric Blake             break;
1421797ac58cSKevin Wolf         case 'P':
1422797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1423797ac58cSKevin Wolf             if (pattern < 0) {
1424797ac58cSKevin Wolf                 g_free(ctx);
1425797ac58cSKevin Wolf                 return 0;
1426797ac58cSKevin Wolf             }
1427797ac58cSKevin Wolf             break;
142837546ff2SEric Blake         case 'i':
142937546ff2SEric Blake             printf("injecting invalid write request\n");
143037546ff2SEric Blake             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
143137546ff2SEric Blake             g_free(ctx);
143237546ff2SEric Blake             return 0;
14335ceb7765SKevin Wolf         case 'z':
1434dc38852aSEric Blake             ctx->zflag = true;
14355ceb7765SKevin Wolf             break;
1436797ac58cSKevin Wolf         default:
1437797ac58cSKevin Wolf             g_free(ctx);
1438c2cdf5c5SKevin Wolf             return qemuio_command_usage(&aio_write_cmd);
1439797ac58cSKevin Wolf         }
1440797ac58cSKevin Wolf     }
1441797ac58cSKevin Wolf 
1442797ac58cSKevin Wolf     if (optind > argc - 2) {
1443797ac58cSKevin Wolf         g_free(ctx);
1444c2cdf5c5SKevin Wolf         return qemuio_command_usage(&aio_write_cmd);
1445797ac58cSKevin Wolf     }
1446797ac58cSKevin Wolf 
14475ceb7765SKevin Wolf     if (ctx->zflag && optind != argc - 2) {
14485ceb7765SKevin Wolf         printf("-z supports only a single length parameter\n");
14495ceb7765SKevin Wolf         g_free(ctx);
14505ceb7765SKevin Wolf         return 0;
14515ceb7765SKevin Wolf     }
14525ceb7765SKevin Wolf 
1453c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1454c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
14554ca1d340SEric Blake         g_free(ctx);
1456c2e001ccSEric Blake         return 0;
1457c2e001ccSEric Blake     }
1458c2e001ccSEric Blake 
14595ceb7765SKevin Wolf     if (ctx->zflag && ctx->Pflag) {
14605ceb7765SKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
14615ceb7765SKevin Wolf         g_free(ctx);
14625ceb7765SKevin Wolf         return 0;
14635ceb7765SKevin Wolf     }
14645ceb7765SKevin Wolf 
1465797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1466797ac58cSKevin Wolf     if (ctx->offset < 0) {
1467a9ecfa00SJohn Snow         print_cvtnum_err(ctx->offset, argv[optind]);
1468797ac58cSKevin Wolf         g_free(ctx);
1469797ac58cSKevin Wolf         return 0;
1470797ac58cSKevin Wolf     }
1471797ac58cSKevin Wolf     optind++;
1472797ac58cSKevin Wolf 
14735ceb7765SKevin Wolf     if (ctx->zflag) {
14745ceb7765SKevin Wolf         int64_t count = cvtnum(argv[optind]);
14755ceb7765SKevin Wolf         if (count < 0) {
14765ceb7765SKevin Wolf             print_cvtnum_err(count, argv[optind]);
14770e01b76eSKevin Wolf             g_free(ctx);
14785ceb7765SKevin Wolf             return 0;
14795ceb7765SKevin Wolf         }
14805ceb7765SKevin Wolf 
14815ceb7765SKevin Wolf         ctx->qiov.size = count;
1482d004bd52SEric Blake         blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1483770e0e0eSEric Blake                               ctx);
14845ceb7765SKevin Wolf     } else {
1485797ac58cSKevin Wolf         nr_iov = argc - optind;
14865ceb7765SKevin Wolf         ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
14875ceb7765SKevin Wolf                                 pattern);
1488797ac58cSKevin Wolf         if (ctx->buf == NULL) {
1489556c2b60SAlberto Garcia             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1490797ac58cSKevin Wolf             g_free(ctx);
1491797ac58cSKevin Wolf             return 0;
1492797ac58cSKevin Wolf         }
1493797ac58cSKevin Wolf 
1494797ac58cSKevin Wolf         gettimeofday(&ctx->t1, NULL);
14954c7b7e9bSMax Reitz         block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
14964c7b7e9bSMax Reitz                          BLOCK_ACCT_WRITE);
14975ceb7765SKevin Wolf 
1498770e0e0eSEric Blake         blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1499770e0e0eSEric Blake                         ctx);
15005ceb7765SKevin Wolf     }
1501797ac58cSKevin Wolf     return 0;
1502797ac58cSKevin Wolf }
1503797ac58cSKevin Wolf 
15044c7b7e9bSMax Reitz static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1505797ac58cSKevin Wolf {
1506556c2b60SAlberto Garcia     BlockAcctCookie cookie;
1507556c2b60SAlberto Garcia     block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
15084c7b7e9bSMax Reitz     blk_drain_all();
1509556c2b60SAlberto Garcia     block_acct_done(blk_get_stats(blk), &cookie);
1510797ac58cSKevin Wolf     return 0;
1511797ac58cSKevin Wolf }
1512797ac58cSKevin Wolf 
1513797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = {
1514797ac58cSKevin Wolf     .name       = "aio_flush",
1515797ac58cSKevin Wolf     .cfunc      = aio_flush_f,
1516797ac58cSKevin Wolf     .oneline    = "completes all outstanding aio requests"
1517797ac58cSKevin Wolf };
1518797ac58cSKevin Wolf 
15194c7b7e9bSMax Reitz static int flush_f(BlockBackend *blk, int argc, char **argv)
1520797ac58cSKevin Wolf {
15214c7b7e9bSMax Reitz     blk_flush(blk);
1522797ac58cSKevin Wolf     return 0;
1523797ac58cSKevin Wolf }
1524797ac58cSKevin Wolf 
1525797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = {
1526797ac58cSKevin Wolf     .name       = "flush",
1527797ac58cSKevin Wolf     .altname    = "f",
1528797ac58cSKevin Wolf     .cfunc      = flush_f,
1529797ac58cSKevin Wolf     .oneline    = "flush all in-core file state to disk",
1530797ac58cSKevin Wolf };
1531797ac58cSKevin Wolf 
15324c7b7e9bSMax Reitz static int truncate_f(BlockBackend *blk, int argc, char **argv)
1533797ac58cSKevin Wolf {
1534797ac58cSKevin Wolf     int64_t offset;
1535797ac58cSKevin Wolf     int ret;
1536797ac58cSKevin Wolf 
1537797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1538797ac58cSKevin Wolf     if (offset < 0) {
1539a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1540797ac58cSKevin Wolf         return 0;
1541797ac58cSKevin Wolf     }
1542797ac58cSKevin Wolf 
15434c7b7e9bSMax Reitz     ret = blk_truncate(blk, offset);
1544797ac58cSKevin Wolf     if (ret < 0) {
1545797ac58cSKevin Wolf         printf("truncate: %s\n", strerror(-ret));
1546797ac58cSKevin Wolf         return 0;
1547797ac58cSKevin Wolf     }
1548797ac58cSKevin Wolf 
1549797ac58cSKevin Wolf     return 0;
1550797ac58cSKevin Wolf }
1551797ac58cSKevin Wolf 
1552797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = {
1553797ac58cSKevin Wolf     .name       = "truncate",
1554797ac58cSKevin Wolf     .altname    = "t",
1555797ac58cSKevin Wolf     .cfunc      = truncate_f,
1556797ac58cSKevin Wolf     .argmin     = 1,
1557797ac58cSKevin Wolf     .argmax     = 1,
1558797ac58cSKevin Wolf     .args       = "off",
1559797ac58cSKevin Wolf     .oneline    = "truncates the current file at the given offset",
1560797ac58cSKevin Wolf };
1561797ac58cSKevin Wolf 
15624c7b7e9bSMax Reitz static int length_f(BlockBackend *blk, int argc, char **argv)
1563797ac58cSKevin Wolf {
1564797ac58cSKevin Wolf     int64_t size;
1565797ac58cSKevin Wolf     char s1[64];
1566797ac58cSKevin Wolf 
15674c7b7e9bSMax Reitz     size = blk_getlength(blk);
1568797ac58cSKevin Wolf     if (size < 0) {
1569797ac58cSKevin Wolf         printf("getlength: %s\n", strerror(-size));
1570797ac58cSKevin Wolf         return 0;
1571797ac58cSKevin Wolf     }
1572797ac58cSKevin Wolf 
1573797ac58cSKevin Wolf     cvtstr(size, s1, sizeof(s1));
1574797ac58cSKevin Wolf     printf("%s\n", s1);
1575797ac58cSKevin Wolf     return 0;
1576797ac58cSKevin Wolf }
1577797ac58cSKevin Wolf 
1578797ac58cSKevin Wolf 
1579797ac58cSKevin Wolf static const cmdinfo_t length_cmd = {
1580797ac58cSKevin Wolf     .name   = "length",
1581797ac58cSKevin Wolf     .altname    = "l",
1582797ac58cSKevin Wolf     .cfunc      = length_f,
1583797ac58cSKevin Wolf     .oneline    = "gets the length of the current file",
1584797ac58cSKevin Wolf };
1585797ac58cSKevin Wolf 
1586797ac58cSKevin Wolf 
15874c7b7e9bSMax Reitz static int info_f(BlockBackend *blk, int argc, char **argv)
1588797ac58cSKevin Wolf {
15894c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
1590797ac58cSKevin Wolf     BlockDriverInfo bdi;
1591a8d8ecb7SMax Reitz     ImageInfoSpecific *spec_info;
1592797ac58cSKevin Wolf     char s1[64], s2[64];
1593797ac58cSKevin Wolf     int ret;
1594797ac58cSKevin Wolf 
1595797ac58cSKevin Wolf     if (bs->drv && bs->drv->format_name) {
1596797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->format_name);
1597797ac58cSKevin Wolf     }
1598797ac58cSKevin Wolf     if (bs->drv && bs->drv->protocol_name) {
1599797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->protocol_name);
1600797ac58cSKevin Wolf     }
1601797ac58cSKevin Wolf 
1602797ac58cSKevin Wolf     ret = bdrv_get_info(bs, &bdi);
1603797ac58cSKevin Wolf     if (ret) {
1604797ac58cSKevin Wolf         return 0;
1605797ac58cSKevin Wolf     }
1606797ac58cSKevin Wolf 
1607797ac58cSKevin Wolf     cvtstr(bdi.cluster_size, s1, sizeof(s1));
1608797ac58cSKevin Wolf     cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1609797ac58cSKevin Wolf 
1610797ac58cSKevin Wolf     printf("cluster size: %s\n", s1);
1611797ac58cSKevin Wolf     printf("vm state offset: %s\n", s2);
1612797ac58cSKevin Wolf 
1613a8d8ecb7SMax Reitz     spec_info = bdrv_get_specific_info(bs);
1614a8d8ecb7SMax Reitz     if (spec_info) {
1615a8d8ecb7SMax Reitz         printf("Format specific information:\n");
1616a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1617a8d8ecb7SMax Reitz         qapi_free_ImageInfoSpecific(spec_info);
1618a8d8ecb7SMax Reitz     }
1619a8d8ecb7SMax Reitz 
1620797ac58cSKevin Wolf     return 0;
1621797ac58cSKevin Wolf }
1622797ac58cSKevin Wolf 
1623797ac58cSKevin Wolf 
1624797ac58cSKevin Wolf 
1625797ac58cSKevin Wolf static const cmdinfo_t info_cmd = {
1626797ac58cSKevin Wolf     .name       = "info",
1627797ac58cSKevin Wolf     .altname    = "i",
1628797ac58cSKevin Wolf     .cfunc      = info_f,
1629797ac58cSKevin Wolf     .oneline    = "prints information about the current file",
1630797ac58cSKevin Wolf };
1631797ac58cSKevin Wolf 
1632797ac58cSKevin Wolf static void discard_help(void)
1633797ac58cSKevin Wolf {
1634797ac58cSKevin Wolf     printf(
1635797ac58cSKevin Wolf "\n"
1636797ac58cSKevin Wolf " discards a range of bytes from the given offset\n"
1637797ac58cSKevin Wolf "\n"
1638797ac58cSKevin Wolf " Example:\n"
1639797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1640797ac58cSKevin Wolf "\n"
1641797ac58cSKevin Wolf " Discards a segment of the currently open file.\n"
1642797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1643797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1644797ac58cSKevin Wolf "\n");
1645797ac58cSKevin Wolf }
1646797ac58cSKevin Wolf 
16474c7b7e9bSMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv);
1648797ac58cSKevin Wolf 
1649797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = {
1650797ac58cSKevin Wolf     .name       = "discard",
1651797ac58cSKevin Wolf     .altname    = "d",
1652797ac58cSKevin Wolf     .cfunc      = discard_f,
1653797ac58cSKevin Wolf     .argmin     = 2,
1654797ac58cSKevin Wolf     .argmax     = -1,
1655797ac58cSKevin Wolf     .args       = "[-Cq] off len",
1656797ac58cSKevin Wolf     .oneline    = "discards a number of bytes at a specified offset",
1657797ac58cSKevin Wolf     .help       = discard_help,
1658797ac58cSKevin Wolf };
1659797ac58cSKevin Wolf 
16604c7b7e9bSMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv)
1661797ac58cSKevin Wolf {
1662797ac58cSKevin Wolf     struct timeval t1, t2;
1663dc38852aSEric Blake     bool Cflag = false, qflag = false;
1664797ac58cSKevin Wolf     int c, ret;
16659b0beaf3SJohn Snow     int64_t offset, count;
1666797ac58cSKevin Wolf 
1667b062ad86SEric Blake     while ((c = getopt(argc, argv, "Cq")) != -1) {
1668797ac58cSKevin Wolf         switch (c) {
1669797ac58cSKevin Wolf         case 'C':
1670dc38852aSEric Blake             Cflag = true;
1671797ac58cSKevin Wolf             break;
1672797ac58cSKevin Wolf         case 'q':
1673dc38852aSEric Blake             qflag = true;
1674797ac58cSKevin Wolf             break;
1675797ac58cSKevin Wolf         default:
1676c2cdf5c5SKevin Wolf             return qemuio_command_usage(&discard_cmd);
1677797ac58cSKevin Wolf         }
1678797ac58cSKevin Wolf     }
1679797ac58cSKevin Wolf 
1680797ac58cSKevin Wolf     if (optind != argc - 2) {
1681c2cdf5c5SKevin Wolf         return qemuio_command_usage(&discard_cmd);
1682797ac58cSKevin Wolf     }
1683797ac58cSKevin Wolf 
1684797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1685797ac58cSKevin Wolf     if (offset < 0) {
1686a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1687797ac58cSKevin Wolf         return 0;
1688797ac58cSKevin Wolf     }
1689797ac58cSKevin Wolf 
1690797ac58cSKevin Wolf     optind++;
1691797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1692797ac58cSKevin Wolf     if (count < 0) {
1693a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
1694797ac58cSKevin Wolf         return 0;
1695a3674679SMax Reitz     } else if (count >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
16969b0beaf3SJohn Snow         printf("length cannot exceed %"PRIu64", given %s\n",
1697a3674679SMax Reitz                (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
16989b0beaf3SJohn Snow                argv[optind]);
16999b0beaf3SJohn Snow         return 0;
1700797ac58cSKevin Wolf     }
1701797ac58cSKevin Wolf 
1702797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
17031c6c4bb7SEric Blake     ret = blk_pdiscard(blk, offset, count);
1704797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1705797ac58cSKevin Wolf 
1706797ac58cSKevin Wolf     if (ret < 0) {
1707797ac58cSKevin Wolf         printf("discard failed: %s\n", strerror(-ret));
1708797ac58cSKevin Wolf         goto out;
1709797ac58cSKevin Wolf     }
1710797ac58cSKevin Wolf 
1711797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1712797ac58cSKevin Wolf     if (!qflag) {
1713797ac58cSKevin Wolf         t2 = tsub(t2, t1);
1714797ac58cSKevin Wolf         print_report("discard", &t2, offset, count, count, 1, Cflag);
1715797ac58cSKevin Wolf     }
1716797ac58cSKevin Wolf 
1717797ac58cSKevin Wolf out:
1718797ac58cSKevin Wolf     return 0;
1719797ac58cSKevin Wolf }
1720797ac58cSKevin Wolf 
17214c7b7e9bSMax Reitz static int alloc_f(BlockBackend *blk, int argc, char **argv)
1722797ac58cSKevin Wolf {
17234c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
17249b0beaf3SJohn Snow     int64_t offset, sector_num, nb_sectors, remaining;
1725797ac58cSKevin Wolf     char s1[64];
17269b0beaf3SJohn Snow     int num, ret;
17279b0beaf3SJohn Snow     int64_t sum_alloc;
1728797ac58cSKevin Wolf 
1729797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1730797ac58cSKevin Wolf     if (offset < 0) {
1731a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1732797ac58cSKevin Wolf         return 0;
1733797ac58cSKevin Wolf     } else if (offset & 0x1ff) {
1734797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1735797ac58cSKevin Wolf                offset);
1736797ac58cSKevin Wolf         return 0;
1737797ac58cSKevin Wolf     }
1738797ac58cSKevin Wolf 
1739797ac58cSKevin Wolf     if (argc == 3) {
1740797ac58cSKevin Wolf         nb_sectors = cvtnum(argv[2]);
1741797ac58cSKevin Wolf         if (nb_sectors < 0) {
1742a9ecfa00SJohn Snow             print_cvtnum_err(nb_sectors, argv[2]);
1743797ac58cSKevin Wolf             return 0;
17449b0beaf3SJohn Snow         } else if (nb_sectors > INT_MAX) {
17459b0beaf3SJohn Snow             printf("length argument cannot exceed %d, given %s\n",
17469b0beaf3SJohn Snow                    INT_MAX, argv[2]);
17479b0beaf3SJohn Snow             return 0;
1748797ac58cSKevin Wolf         }
1749797ac58cSKevin Wolf     } else {
1750797ac58cSKevin Wolf         nb_sectors = 1;
1751797ac58cSKevin Wolf     }
1752797ac58cSKevin Wolf 
1753797ac58cSKevin Wolf     remaining = nb_sectors;
1754797ac58cSKevin Wolf     sum_alloc = 0;
1755797ac58cSKevin Wolf     sector_num = offset >> 9;
1756797ac58cSKevin Wolf     while (remaining) {
1757797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1758d663640cSPaolo Bonzini         if (ret < 0) {
1759d663640cSPaolo Bonzini             printf("is_allocated failed: %s\n", strerror(-ret));
1760d663640cSPaolo Bonzini             return 0;
1761d663640cSPaolo Bonzini         }
1762797ac58cSKevin Wolf         sector_num += num;
1763797ac58cSKevin Wolf         remaining -= num;
1764797ac58cSKevin Wolf         if (ret) {
1765797ac58cSKevin Wolf             sum_alloc += num;
1766797ac58cSKevin Wolf         }
1767797ac58cSKevin Wolf         if (num == 0) {
1768797ac58cSKevin Wolf             nb_sectors -= remaining;
1769797ac58cSKevin Wolf             remaining = 0;
1770797ac58cSKevin Wolf         }
1771797ac58cSKevin Wolf     }
1772797ac58cSKevin Wolf 
1773797ac58cSKevin Wolf     cvtstr(offset, s1, sizeof(s1));
1774797ac58cSKevin Wolf 
17759b0beaf3SJohn Snow     printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
1776797ac58cSKevin Wolf            sum_alloc, nb_sectors, s1);
1777797ac58cSKevin Wolf     return 0;
1778797ac58cSKevin Wolf }
1779797ac58cSKevin Wolf 
1780797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = {
1781797ac58cSKevin Wolf     .name       = "alloc",
1782797ac58cSKevin Wolf     .altname    = "a",
1783797ac58cSKevin Wolf     .argmin     = 1,
1784797ac58cSKevin Wolf     .argmax     = 2,
1785797ac58cSKevin Wolf     .cfunc      = alloc_f,
1786797ac58cSKevin Wolf     .args       = "off [sectors]",
1787797ac58cSKevin Wolf     .oneline    = "checks if a sector is present in the file",
1788797ac58cSKevin Wolf };
1789797ac58cSKevin Wolf 
1790797ac58cSKevin Wolf 
1791797ac58cSKevin Wolf static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1792797ac58cSKevin Wolf                             int64_t nb_sectors, int64_t *pnum)
1793797ac58cSKevin Wolf {
1794797ac58cSKevin Wolf     int num, num_checked;
1795797ac58cSKevin Wolf     int ret, firstret;
1796797ac58cSKevin Wolf 
1797797ac58cSKevin Wolf     num_checked = MIN(nb_sectors, INT_MAX);
1798797ac58cSKevin Wolf     ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1799797ac58cSKevin Wolf     if (ret < 0) {
1800797ac58cSKevin Wolf         return ret;
1801797ac58cSKevin Wolf     }
1802797ac58cSKevin Wolf 
1803797ac58cSKevin Wolf     firstret = ret;
1804797ac58cSKevin Wolf     *pnum = num;
1805797ac58cSKevin Wolf 
1806797ac58cSKevin Wolf     while (nb_sectors > 0 && ret == firstret) {
1807797ac58cSKevin Wolf         sector_num += num;
1808797ac58cSKevin Wolf         nb_sectors -= num;
1809797ac58cSKevin Wolf 
1810797ac58cSKevin Wolf         num_checked = MIN(nb_sectors, INT_MAX);
1811797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
18124b25bbc4SMax Reitz         if (ret == firstret && num) {
1813797ac58cSKevin Wolf             *pnum += num;
1814797ac58cSKevin Wolf         } else {
1815797ac58cSKevin Wolf             break;
1816797ac58cSKevin Wolf         }
1817797ac58cSKevin Wolf     }
1818797ac58cSKevin Wolf 
1819797ac58cSKevin Wolf     return firstret;
1820797ac58cSKevin Wolf }
1821797ac58cSKevin Wolf 
18224c7b7e9bSMax Reitz static int map_f(BlockBackend *blk, int argc, char **argv)
1823797ac58cSKevin Wolf {
1824797ac58cSKevin Wolf     int64_t offset;
18254c7b7e9bSMax Reitz     int64_t nb_sectors, total_sectors;
1826797ac58cSKevin Wolf     char s1[64];
1827797ac58cSKevin Wolf     int64_t num;
1828797ac58cSKevin Wolf     int ret;
1829797ac58cSKevin Wolf     const char *retstr;
1830797ac58cSKevin Wolf 
1831797ac58cSKevin Wolf     offset = 0;
18324c7b7e9bSMax Reitz     total_sectors = blk_nb_sectors(blk);
18334c7b7e9bSMax Reitz     if (total_sectors < 0) {
18344c7b7e9bSMax Reitz         error_report("Failed to query image length: %s",
18354c7b7e9bSMax Reitz                      strerror(-total_sectors));
18364c7b7e9bSMax Reitz         return 0;
18374c7b7e9bSMax Reitz     }
18384c7b7e9bSMax Reitz 
18394c7b7e9bSMax Reitz     nb_sectors = total_sectors;
1840797ac58cSKevin Wolf 
1841797ac58cSKevin Wolf     do {
18424c7b7e9bSMax Reitz         ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
1843797ac58cSKevin Wolf         if (ret < 0) {
1844797ac58cSKevin Wolf             error_report("Failed to get allocation status: %s", strerror(-ret));
1845797ac58cSKevin Wolf             return 0;
18464b25bbc4SMax Reitz         } else if (!num) {
18474b25bbc4SMax Reitz             error_report("Unexpected end of image");
18484b25bbc4SMax Reitz             return 0;
1849797ac58cSKevin Wolf         }
1850797ac58cSKevin Wolf 
1851797ac58cSKevin Wolf         retstr = ret ? "    allocated" : "not allocated";
1852797ac58cSKevin Wolf         cvtstr(offset << 9ULL, s1, sizeof(s1));
1853797ac58cSKevin Wolf         printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1854797ac58cSKevin Wolf                "at offset %s (%d)\n",
1855797ac58cSKevin Wolf                offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1856797ac58cSKevin Wolf 
1857797ac58cSKevin Wolf         offset += num;
1858797ac58cSKevin Wolf         nb_sectors -= num;
18594c7b7e9bSMax Reitz     } while (offset < total_sectors);
1860797ac58cSKevin Wolf 
1861797ac58cSKevin Wolf     return 0;
1862797ac58cSKevin Wolf }
1863797ac58cSKevin Wolf 
1864797ac58cSKevin Wolf static const cmdinfo_t map_cmd = {
1865797ac58cSKevin Wolf        .name           = "map",
1866797ac58cSKevin Wolf        .argmin         = 0,
1867797ac58cSKevin Wolf        .argmax         = 0,
1868797ac58cSKevin Wolf        .cfunc          = map_f,
1869797ac58cSKevin Wolf        .args           = "",
1870797ac58cSKevin Wolf        .oneline        = "prints the allocated areas of a file",
1871797ac58cSKevin Wolf };
1872797ac58cSKevin Wolf 
18735bbd2e59SKevin Wolf static void reopen_help(void)
18745bbd2e59SKevin Wolf {
18755bbd2e59SKevin Wolf     printf(
18765bbd2e59SKevin Wolf "\n"
18775bbd2e59SKevin Wolf " Changes the open options of an already opened image\n"
18785bbd2e59SKevin Wolf "\n"
18795bbd2e59SKevin Wolf " Example:\n"
18805bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
18815bbd2e59SKevin Wolf "\n"
18825bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n"
18835bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n"
18845bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n"
18855bbd2e59SKevin Wolf "\n");
18865bbd2e59SKevin Wolf }
18875bbd2e59SKevin Wolf 
18885bbd2e59SKevin Wolf static int reopen_f(BlockBackend *blk, int argc, char **argv);
18895bbd2e59SKevin Wolf 
18905bbd2e59SKevin Wolf static QemuOptsList reopen_opts = {
18915bbd2e59SKevin Wolf     .name = "reopen",
18925bbd2e59SKevin Wolf     .merge_lists = true,
18935bbd2e59SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
18945bbd2e59SKevin Wolf     .desc = {
18955bbd2e59SKevin Wolf         /* no elements => accept any params */
18965bbd2e59SKevin Wolf         { /* end of list */ }
18975bbd2e59SKevin Wolf     },
18985bbd2e59SKevin Wolf };
18995bbd2e59SKevin Wolf 
19005bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = {
19015bbd2e59SKevin Wolf        .name           = "reopen",
19025bbd2e59SKevin Wolf        .argmin         = 0,
19035bbd2e59SKevin Wolf        .argmax         = -1,
19045bbd2e59SKevin Wolf        .cfunc          = reopen_f,
19055bbd2e59SKevin Wolf        .args           = "[-r] [-c cache] [-o options]",
19065bbd2e59SKevin Wolf        .oneline        = "reopens an image with new options",
19075bbd2e59SKevin Wolf        .help           = reopen_help,
19085bbd2e59SKevin Wolf };
19095bbd2e59SKevin Wolf 
19105bbd2e59SKevin Wolf static int reopen_f(BlockBackend *blk, int argc, char **argv)
19115bbd2e59SKevin Wolf {
19125bbd2e59SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
19135bbd2e59SKevin Wolf     QemuOpts *qopts;
19145bbd2e59SKevin Wolf     QDict *opts;
19155bbd2e59SKevin Wolf     int c;
19165bbd2e59SKevin Wolf     int flags = bs->open_flags;
191719dbecdcSKevin Wolf     bool writethrough = !blk_enable_write_cache(blk);
19185bbd2e59SKevin Wolf 
19195bbd2e59SKevin Wolf     BlockReopenQueue *brq;
19205bbd2e59SKevin Wolf     Error *local_err = NULL;
19215bbd2e59SKevin Wolf 
19225bbd2e59SKevin Wolf     while ((c = getopt(argc, argv, "c:o:r")) != -1) {
19235bbd2e59SKevin Wolf         switch (c) {
19245bbd2e59SKevin Wolf         case 'c':
192519dbecdcSKevin Wolf             if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
19265bbd2e59SKevin Wolf                 error_report("Invalid cache option: %s", optarg);
19275bbd2e59SKevin Wolf                 return 0;
19285bbd2e59SKevin Wolf             }
19295bbd2e59SKevin Wolf             break;
19305bbd2e59SKevin Wolf         case 'o':
19315bbd2e59SKevin Wolf             if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
19325bbd2e59SKevin Wolf                 qemu_opts_reset(&reopen_opts);
19335bbd2e59SKevin Wolf                 return 0;
19345bbd2e59SKevin Wolf             }
19355bbd2e59SKevin Wolf             break;
19365bbd2e59SKevin Wolf         case 'r':
19375bbd2e59SKevin Wolf             flags &= ~BDRV_O_RDWR;
19385bbd2e59SKevin Wolf             break;
19395bbd2e59SKevin Wolf         default:
19405bbd2e59SKevin Wolf             qemu_opts_reset(&reopen_opts);
19415bbd2e59SKevin Wolf             return qemuio_command_usage(&reopen_cmd);
19425bbd2e59SKevin Wolf         }
19435bbd2e59SKevin Wolf     }
19445bbd2e59SKevin Wolf 
19455bbd2e59SKevin Wolf     if (optind != argc) {
19465bbd2e59SKevin Wolf         qemu_opts_reset(&reopen_opts);
19475bbd2e59SKevin Wolf         return qemuio_command_usage(&reopen_cmd);
19485bbd2e59SKevin Wolf     }
19495bbd2e59SKevin Wolf 
195019dbecdcSKevin Wolf     if (writethrough != blk_enable_write_cache(blk) &&
195119dbecdcSKevin Wolf         blk_get_attached_dev(blk))
195219dbecdcSKevin Wolf     {
195319dbecdcSKevin Wolf         error_report("Cannot change cache.writeback: Device attached");
195419dbecdcSKevin Wolf         qemu_opts_reset(&reopen_opts);
195519dbecdcSKevin Wolf         return 0;
195619dbecdcSKevin Wolf     }
195719dbecdcSKevin Wolf 
19585bbd2e59SKevin Wolf     qopts = qemu_opts_find(&reopen_opts, NULL);
19595bbd2e59SKevin Wolf     opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
19605bbd2e59SKevin Wolf     qemu_opts_reset(&reopen_opts);
19615bbd2e59SKevin Wolf 
19625bbd2e59SKevin Wolf     brq = bdrv_reopen_queue(NULL, bs, opts, flags);
1963720150f3SPaolo Bonzini     bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
19645bbd2e59SKevin Wolf     if (local_err) {
19655bbd2e59SKevin Wolf         error_report_err(local_err);
196619dbecdcSKevin Wolf     } else {
196719dbecdcSKevin Wolf         blk_set_enable_write_cache(blk, !writethrough);
19685bbd2e59SKevin Wolf     }
19695bbd2e59SKevin Wolf 
19705bbd2e59SKevin Wolf     return 0;
19715bbd2e59SKevin Wolf }
19725bbd2e59SKevin Wolf 
19734c7b7e9bSMax Reitz static int break_f(BlockBackend *blk, int argc, char **argv)
1974797ac58cSKevin Wolf {
1975797ac58cSKevin Wolf     int ret;
1976797ac58cSKevin Wolf 
19774c7b7e9bSMax Reitz     ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
1978797ac58cSKevin Wolf     if (ret < 0) {
1979797ac58cSKevin Wolf         printf("Could not set breakpoint: %s\n", strerror(-ret));
1980797ac58cSKevin Wolf     }
1981797ac58cSKevin Wolf 
1982797ac58cSKevin Wolf     return 0;
1983797ac58cSKevin Wolf }
1984797ac58cSKevin Wolf 
19854c7b7e9bSMax Reitz static int remove_break_f(BlockBackend *blk, int argc, char **argv)
19864cc70e93SFam Zheng {
19874cc70e93SFam Zheng     int ret;
19884cc70e93SFam Zheng 
19894c7b7e9bSMax Reitz     ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
19904cc70e93SFam Zheng     if (ret < 0) {
19914cc70e93SFam Zheng         printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
19924cc70e93SFam Zheng     }
19934cc70e93SFam Zheng 
19944cc70e93SFam Zheng     return 0;
19954cc70e93SFam Zheng }
19964cc70e93SFam Zheng 
1997797ac58cSKevin Wolf static const cmdinfo_t break_cmd = {
1998797ac58cSKevin Wolf        .name           = "break",
1999797ac58cSKevin Wolf        .argmin         = 2,
2000797ac58cSKevin Wolf        .argmax         = 2,
2001797ac58cSKevin Wolf        .cfunc          = break_f,
2002797ac58cSKevin Wolf        .args           = "event tag",
2003797ac58cSKevin Wolf        .oneline        = "sets a breakpoint on event and tags the stopped "
2004797ac58cSKevin Wolf                          "request as tag",
2005797ac58cSKevin Wolf };
2006797ac58cSKevin Wolf 
20074cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = {
20084cc70e93SFam Zheng        .name           = "remove_break",
20094cc70e93SFam Zheng        .argmin         = 1,
20104cc70e93SFam Zheng        .argmax         = 1,
20114cc70e93SFam Zheng        .cfunc          = remove_break_f,
20124cc70e93SFam Zheng        .args           = "tag",
20134cc70e93SFam Zheng        .oneline        = "remove a breakpoint by tag",
20144cc70e93SFam Zheng };
20154cc70e93SFam Zheng 
20164c7b7e9bSMax Reitz static int resume_f(BlockBackend *blk, int argc, char **argv)
2017797ac58cSKevin Wolf {
2018797ac58cSKevin Wolf     int ret;
2019797ac58cSKevin Wolf 
20204c7b7e9bSMax Reitz     ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2021797ac58cSKevin Wolf     if (ret < 0) {
2022797ac58cSKevin Wolf         printf("Could not resume request: %s\n", strerror(-ret));
2023797ac58cSKevin Wolf     }
2024797ac58cSKevin Wolf 
2025797ac58cSKevin Wolf     return 0;
2026797ac58cSKevin Wolf }
2027797ac58cSKevin Wolf 
2028797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = {
2029797ac58cSKevin Wolf        .name           = "resume",
2030797ac58cSKevin Wolf        .argmin         = 1,
2031797ac58cSKevin Wolf        .argmax         = 1,
2032797ac58cSKevin Wolf        .cfunc          = resume_f,
2033797ac58cSKevin Wolf        .args           = "tag",
2034797ac58cSKevin Wolf        .oneline        = "resumes the request tagged as tag",
2035797ac58cSKevin Wolf };
2036797ac58cSKevin Wolf 
20374c7b7e9bSMax Reitz static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2038797ac58cSKevin Wolf {
20394c7b7e9bSMax Reitz     while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
20404c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
2041797ac58cSKevin Wolf     }
2042797ac58cSKevin Wolf 
2043797ac58cSKevin Wolf     return 0;
2044797ac58cSKevin Wolf }
2045797ac58cSKevin Wolf 
2046797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = {
2047797ac58cSKevin Wolf        .name           = "wait_break",
2048797ac58cSKevin Wolf        .argmin         = 1,
2049797ac58cSKevin Wolf        .argmax         = 1,
2050797ac58cSKevin Wolf        .cfunc          = wait_break_f,
2051797ac58cSKevin Wolf        .args           = "tag",
2052797ac58cSKevin Wolf        .oneline        = "waits for the suspension of a request",
2053797ac58cSKevin Wolf };
2054797ac58cSKevin Wolf 
20554c7b7e9bSMax Reitz static int abort_f(BlockBackend *blk, int argc, char **argv)
2056797ac58cSKevin Wolf {
2057797ac58cSKevin Wolf     abort();
2058797ac58cSKevin Wolf }
2059797ac58cSKevin Wolf 
2060797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = {
2061797ac58cSKevin Wolf        .name           = "abort",
2062797ac58cSKevin Wolf        .cfunc          = abort_f,
2063797ac58cSKevin Wolf        .flags          = CMD_NOFILE_OK,
2064797ac58cSKevin Wolf        .oneline        = "simulate a program crash using abort(3)",
2065797ac58cSKevin Wolf };
2066797ac58cSKevin Wolf 
20670e82dc7bSMax Reitz static void sigraise_help(void)
20680e82dc7bSMax Reitz {
20690e82dc7bSMax Reitz     printf(
20700e82dc7bSMax Reitz "\n"
20710e82dc7bSMax Reitz " raises the given signal\n"
20720e82dc7bSMax Reitz "\n"
20730e82dc7bSMax Reitz " Example:\n"
20740e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n"
20750e82dc7bSMax Reitz "\n"
20760e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
20770e82dc7bSMax Reitz " given to sigraise.\n"
20780e82dc7bSMax Reitz "\n", SIGTERM);
20790e82dc7bSMax Reitz }
20800e82dc7bSMax Reitz 
20814c7b7e9bSMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv);
20820e82dc7bSMax Reitz 
20830e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = {
20840e82dc7bSMax Reitz     .name       = "sigraise",
20850e82dc7bSMax Reitz     .cfunc      = sigraise_f,
20860e82dc7bSMax Reitz     .argmin     = 1,
20870e82dc7bSMax Reitz     .argmax     = 1,
20880e82dc7bSMax Reitz     .flags      = CMD_NOFILE_OK,
20890e82dc7bSMax Reitz     .args       = "signal",
20900e82dc7bSMax Reitz     .oneline    = "raises a signal",
20910e82dc7bSMax Reitz     .help       = sigraise_help,
20920e82dc7bSMax Reitz };
20930e82dc7bSMax Reitz 
20944c7b7e9bSMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv)
20950e82dc7bSMax Reitz {
20969b0beaf3SJohn Snow     int64_t sig = cvtnum(argv[1]);
20970e82dc7bSMax Reitz     if (sig < 0) {
2098a9ecfa00SJohn Snow         print_cvtnum_err(sig, argv[1]);
20990e82dc7bSMax Reitz         return 0;
21009b0beaf3SJohn Snow     } else if (sig > NSIG) {
21019b0beaf3SJohn Snow         printf("signal argument '%s' is too large to be a valid signal\n",
21029b0beaf3SJohn Snow                argv[1]);
21039b0beaf3SJohn Snow         return 0;
21040e82dc7bSMax Reitz     }
21050e82dc7bSMax Reitz 
21060e82dc7bSMax Reitz     /* Using raise() to kill this process does not necessarily flush all open
21070e82dc7bSMax Reitz      * streams. At least stdout and stderr (although the latter should be
21080e82dc7bSMax Reitz      * non-buffered anyway) should be flushed, though. */
21090e82dc7bSMax Reitz     fflush(stdout);
21100e82dc7bSMax Reitz     fflush(stderr);
21110e82dc7bSMax Reitz 
21120e82dc7bSMax Reitz     raise(sig);
21130e82dc7bSMax Reitz     return 0;
21140e82dc7bSMax Reitz }
21150e82dc7bSMax Reitz 
2116cd33d02aSKevin Wolf static void sleep_cb(void *opaque)
2117cd33d02aSKevin Wolf {
2118cd33d02aSKevin Wolf     bool *expired = opaque;
2119cd33d02aSKevin Wolf     *expired = true;
2120cd33d02aSKevin Wolf }
2121cd33d02aSKevin Wolf 
21224c7b7e9bSMax Reitz static int sleep_f(BlockBackend *blk, int argc, char **argv)
2123cd33d02aSKevin Wolf {
2124cd33d02aSKevin Wolf     char *endptr;
2125cd33d02aSKevin Wolf     long ms;
2126cd33d02aSKevin Wolf     struct QEMUTimer *timer;
2127cd33d02aSKevin Wolf     bool expired = false;
2128cd33d02aSKevin Wolf 
2129cd33d02aSKevin Wolf     ms = strtol(argv[1], &endptr, 0);
2130cd33d02aSKevin Wolf     if (ms < 0 || *endptr != '\0') {
2131cd33d02aSKevin Wolf         printf("%s is not a valid number\n", argv[1]);
2132cd33d02aSKevin Wolf         return 0;
2133cd33d02aSKevin Wolf     }
2134cd33d02aSKevin Wolf 
2135cd33d02aSKevin Wolf     timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2136cd33d02aSKevin Wolf     timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2137cd33d02aSKevin Wolf 
2138cd33d02aSKevin Wolf     while (!expired) {
2139cd33d02aSKevin Wolf         main_loop_wait(false);
2140cd33d02aSKevin Wolf     }
2141cd33d02aSKevin Wolf 
2142cd33d02aSKevin Wolf     timer_free(timer);
2143cd33d02aSKevin Wolf 
2144cd33d02aSKevin Wolf     return 0;
2145cd33d02aSKevin Wolf }
2146cd33d02aSKevin Wolf 
2147cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = {
2148cd33d02aSKevin Wolf        .name           = "sleep",
2149cd33d02aSKevin Wolf        .argmin         = 1,
2150cd33d02aSKevin Wolf        .argmax         = 1,
2151cd33d02aSKevin Wolf        .cfunc          = sleep_f,
2152cd33d02aSKevin Wolf        .flags          = CMD_NOFILE_OK,
2153cd33d02aSKevin Wolf        .oneline        = "waits for the given value in milliseconds",
2154cd33d02aSKevin Wolf };
2155cd33d02aSKevin Wolf 
2156f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2157f18a834aSKevin Wolf {
2158f18a834aSKevin Wolf     if (cmd) {
2159f18a834aSKevin Wolf         printf("%s ", cmd);
2160f18a834aSKevin Wolf     } else {
2161f18a834aSKevin Wolf         printf("%s ", ct->name);
2162f18a834aSKevin Wolf         if (ct->altname) {
2163f18a834aSKevin Wolf             printf("(or %s) ", ct->altname);
2164f18a834aSKevin Wolf         }
2165f18a834aSKevin Wolf     }
2166f18a834aSKevin Wolf 
2167f18a834aSKevin Wolf     if (ct->args) {
2168f18a834aSKevin Wolf         printf("%s ", ct->args);
2169f18a834aSKevin Wolf     }
2170f18a834aSKevin Wolf     printf("-- %s\n", ct->oneline);
2171f18a834aSKevin Wolf }
2172f18a834aSKevin Wolf 
2173f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2174f18a834aSKevin Wolf {
2175f18a834aSKevin Wolf     help_oneline(cmd, ct);
2176f18a834aSKevin Wolf     if (ct->help) {
2177f18a834aSKevin Wolf         ct->help();
2178f18a834aSKevin Wolf     }
2179f18a834aSKevin Wolf }
2180f18a834aSKevin Wolf 
2181f18a834aSKevin Wolf static void help_all(void)
2182f18a834aSKevin Wolf {
2183f18a834aSKevin Wolf     const cmdinfo_t *ct;
2184f18a834aSKevin Wolf 
2185f18a834aSKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2186f18a834aSKevin Wolf         help_oneline(ct->name, ct);
2187f18a834aSKevin Wolf     }
2188f18a834aSKevin Wolf     printf("\nUse 'help commandname' for extended help.\n");
2189f18a834aSKevin Wolf }
2190f18a834aSKevin Wolf 
21914c7b7e9bSMax Reitz static int help_f(BlockBackend *blk, int argc, char **argv)
2192f18a834aSKevin Wolf {
2193f18a834aSKevin Wolf     const cmdinfo_t *ct;
2194f18a834aSKevin Wolf 
2195f18a834aSKevin Wolf     if (argc == 1) {
2196f18a834aSKevin Wolf         help_all();
2197f18a834aSKevin Wolf         return 0;
2198f18a834aSKevin Wolf     }
2199f18a834aSKevin Wolf 
2200f18a834aSKevin Wolf     ct = find_command(argv[1]);
2201f18a834aSKevin Wolf     if (ct == NULL) {
2202f18a834aSKevin Wolf         printf("command %s not found\n", argv[1]);
2203f18a834aSKevin Wolf         return 0;
2204f18a834aSKevin Wolf     }
2205f18a834aSKevin Wolf 
2206f18a834aSKevin Wolf     help_onecmd(argv[1], ct);
2207f18a834aSKevin Wolf     return 0;
2208f18a834aSKevin Wolf }
2209f18a834aSKevin Wolf 
2210f18a834aSKevin Wolf static const cmdinfo_t help_cmd = {
2211f18a834aSKevin Wolf     .name       = "help",
2212f18a834aSKevin Wolf     .altname    = "?",
2213f18a834aSKevin Wolf     .cfunc      = help_f,
2214f18a834aSKevin Wolf     .argmin     = 0,
2215f18a834aSKevin Wolf     .argmax     = 1,
2216f18a834aSKevin Wolf     .flags      = CMD_FLAG_GLOBAL,
2217f18a834aSKevin Wolf     .args       = "[command]",
2218f18a834aSKevin Wolf     .oneline    = "help for one or all commands",
2219f18a834aSKevin Wolf };
2220f18a834aSKevin Wolf 
22214c7b7e9bSMax Reitz bool qemuio_command(BlockBackend *blk, const char *cmd)
2222dd583296SKevin Wolf {
222315afd94aSPaolo Bonzini     AioContext *ctx;
2224dd583296SKevin Wolf     char *input;
2225dd583296SKevin Wolf     const cmdinfo_t *ct;
2226dd583296SKevin Wolf     char **v;
2227dd583296SKevin Wolf     int c;
2228dd583296SKevin Wolf     bool done = false;
2229dd583296SKevin Wolf 
2230dd583296SKevin Wolf     input = g_strdup(cmd);
2231dd583296SKevin Wolf     v = breakline(input, &c);
2232dd583296SKevin Wolf     if (c) {
2233dd583296SKevin Wolf         ct = find_command(v[0]);
2234dd583296SKevin Wolf         if (ct) {
223515afd94aSPaolo Bonzini             ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
223615afd94aSPaolo Bonzini             aio_context_acquire(ctx);
22374c7b7e9bSMax Reitz             done = command(blk, ct, c, v);
223815afd94aSPaolo Bonzini             aio_context_release(ctx);
2239dd583296SKevin Wolf         } else {
2240dd583296SKevin Wolf             fprintf(stderr, "command \"%s\" not found\n", v[0]);
2241dd583296SKevin Wolf         }
2242dd583296SKevin Wolf     }
2243dd583296SKevin Wolf     g_free(input);
2244dd583296SKevin Wolf     g_free(v);
2245dd583296SKevin Wolf 
2246dd583296SKevin Wolf     return done;
2247dd583296SKevin Wolf }
2248dd583296SKevin Wolf 
2249797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void)
2250797ac58cSKevin Wolf {
2251797ac58cSKevin Wolf     /* initialize commands */
2252c2cdf5c5SKevin Wolf     qemuio_add_command(&help_cmd);
2253c2cdf5c5SKevin Wolf     qemuio_add_command(&read_cmd);
2254c2cdf5c5SKevin Wolf     qemuio_add_command(&readv_cmd);
2255c2cdf5c5SKevin Wolf     qemuio_add_command(&write_cmd);
2256c2cdf5c5SKevin Wolf     qemuio_add_command(&writev_cmd);
2257c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_read_cmd);
2258c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_write_cmd);
2259c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_flush_cmd);
2260c2cdf5c5SKevin Wolf     qemuio_add_command(&flush_cmd);
2261c2cdf5c5SKevin Wolf     qemuio_add_command(&truncate_cmd);
2262c2cdf5c5SKevin Wolf     qemuio_add_command(&length_cmd);
2263c2cdf5c5SKevin Wolf     qemuio_add_command(&info_cmd);
2264c2cdf5c5SKevin Wolf     qemuio_add_command(&discard_cmd);
2265c2cdf5c5SKevin Wolf     qemuio_add_command(&alloc_cmd);
2266c2cdf5c5SKevin Wolf     qemuio_add_command(&map_cmd);
22675bbd2e59SKevin Wolf     qemuio_add_command(&reopen_cmd);
2268c2cdf5c5SKevin Wolf     qemuio_add_command(&break_cmd);
22694cc70e93SFam Zheng     qemuio_add_command(&remove_break_cmd);
2270c2cdf5c5SKevin Wolf     qemuio_add_command(&resume_cmd);
2271c2cdf5c5SKevin Wolf     qemuio_add_command(&wait_break_cmd);
2272c2cdf5c5SKevin Wolf     qemuio_add_command(&abort_cmd);
2273cd33d02aSKevin Wolf     qemuio_add_command(&sleep_cmd);
22740e82dc7bSMax Reitz     qemuio_add_command(&sigraise_cmd);
2275797ac58cSKevin Wolf }
2276