xref: /qemu/qemu-io-cmds.c (revision c2e001cc)
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"
21a91f9584SFam Zheng #include "sysemu/block-backend.h"
22f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
23797ac58cSKevin Wolf 
24797ac58cSKevin Wolf #define CMD_NOFILE_OK   0x01
25797ac58cSKevin Wolf 
26f9883880SStefan Weil bool qemuio_misalign;
27797ac58cSKevin Wolf 
28c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab;
29c2cdf5c5SKevin Wolf static int ncmds;
30c2cdf5c5SKevin Wolf 
31c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b)
32c2cdf5c5SKevin Wolf {
33c2cdf5c5SKevin Wolf     return strcmp(((const cmdinfo_t *)a)->name,
34c2cdf5c5SKevin Wolf                   ((const cmdinfo_t *)b)->name);
35c2cdf5c5SKevin Wolf }
36c2cdf5c5SKevin Wolf 
37c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci)
38c2cdf5c5SKevin Wolf {
3902c4f26bSMarkus Armbruster     cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
40c2cdf5c5SKevin Wolf     cmdtab[ncmds - 1] = *ci;
41c2cdf5c5SKevin Wolf     qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
42c2cdf5c5SKevin Wolf }
43c2cdf5c5SKevin Wolf 
44c2cdf5c5SKevin Wolf int qemuio_command_usage(const cmdinfo_t *ci)
45c2cdf5c5SKevin Wolf {
46c2cdf5c5SKevin Wolf     printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
47c2cdf5c5SKevin Wolf     return 0;
48c2cdf5c5SKevin Wolf }
49c2cdf5c5SKevin Wolf 
504c7b7e9bSMax Reitz static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
51c2cdf5c5SKevin Wolf {
52c2cdf5c5SKevin Wolf     if (ct->flags & CMD_FLAG_GLOBAL) {
53c2cdf5c5SKevin Wolf         return 1;
54c2cdf5c5SKevin Wolf     }
554c7b7e9bSMax Reitz     if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
56c2cdf5c5SKevin Wolf         fprintf(stderr, "no file open, try 'help open'\n");
57c2cdf5c5SKevin Wolf         return 0;
58c2cdf5c5SKevin Wolf     }
59c2cdf5c5SKevin Wolf     return 1;
60c2cdf5c5SKevin Wolf }
61c2cdf5c5SKevin Wolf 
624c7b7e9bSMax Reitz static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
633d21994fSKevin Wolf                    char **argv)
64c2cdf5c5SKevin Wolf {
65c2cdf5c5SKevin Wolf     char *cmd = argv[0];
66c2cdf5c5SKevin Wolf 
674c7b7e9bSMax Reitz     if (!init_check_command(blk, ct)) {
68c2cdf5c5SKevin Wolf         return 0;
69c2cdf5c5SKevin Wolf     }
70c2cdf5c5SKevin Wolf 
71c2cdf5c5SKevin Wolf     if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
72c2cdf5c5SKevin Wolf         if (ct->argmax == -1) {
73c2cdf5c5SKevin Wolf             fprintf(stderr,
74c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected at least %d arguments\n",
75c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
76c2cdf5c5SKevin Wolf         } else if (ct->argmin == ct->argmax) {
77c2cdf5c5SKevin Wolf             fprintf(stderr,
78c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected %d arguments\n",
79c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin);
80c2cdf5c5SKevin Wolf         } else {
81c2cdf5c5SKevin Wolf             fprintf(stderr,
82c2cdf5c5SKevin Wolf                     "bad argument count %d to %s, expected between %d and %d arguments\n",
83c2cdf5c5SKevin Wolf                     argc-1, cmd, ct->argmin, ct->argmax);
84c2cdf5c5SKevin Wolf         }
85c2cdf5c5SKevin Wolf         return 0;
86c2cdf5c5SKevin Wolf     }
87c2cdf5c5SKevin Wolf     optind = 0;
884c7b7e9bSMax Reitz     return ct->cfunc(blk, argc, argv);
89c2cdf5c5SKevin Wolf }
90c2cdf5c5SKevin Wolf 
91c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd)
92c2cdf5c5SKevin Wolf {
93c2cdf5c5SKevin Wolf     cmdinfo_t *ct;
94c2cdf5c5SKevin Wolf 
95c2cdf5c5SKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
96c2cdf5c5SKevin Wolf         if (strcmp(ct->name, cmd) == 0 ||
97c2cdf5c5SKevin Wolf             (ct->altname && strcmp(ct->altname, cmd) == 0))
98c2cdf5c5SKevin Wolf         {
99c2cdf5c5SKevin Wolf             return (const cmdinfo_t *)ct;
100c2cdf5c5SKevin Wolf         }
101c2cdf5c5SKevin Wolf     }
102c2cdf5c5SKevin Wolf     return NULL;
103c2cdf5c5SKevin Wolf }
104c2cdf5c5SKevin Wolf 
1054694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */
1064694020dSStefan Hajnoczi void qemuio_complete_command(const char *input,
1074694020dSStefan Hajnoczi                              void (*fn)(const char *cmd, void *opaque),
1084694020dSStefan Hajnoczi                              void *opaque)
1094694020dSStefan Hajnoczi {
1104694020dSStefan Hajnoczi     cmdinfo_t *ct;
1114694020dSStefan Hajnoczi     size_t input_len = strlen(input);
1124694020dSStefan Hajnoczi 
1134694020dSStefan Hajnoczi     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
1144694020dSStefan Hajnoczi         if (strncmp(input, ct->name, input_len) == 0) {
1154694020dSStefan Hajnoczi             fn(ct->name, opaque);
1164694020dSStefan Hajnoczi         }
1174694020dSStefan Hajnoczi     }
1184694020dSStefan Hajnoczi }
1194694020dSStefan Hajnoczi 
120c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count)
121c2cdf5c5SKevin Wolf {
122c2cdf5c5SKevin Wolf     int c = 0;
123c2cdf5c5SKevin Wolf     char *p;
1245839e53bSMarkus Armbruster     char **rval = g_new0(char *, 1);
125c2cdf5c5SKevin Wolf 
126c2cdf5c5SKevin Wolf     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
127c2cdf5c5SKevin Wolf         if (!*p) {
128c2cdf5c5SKevin Wolf             continue;
129c2cdf5c5SKevin Wolf         }
130c2cdf5c5SKevin Wolf         c++;
13108193dd5SMarkus Armbruster         rval = g_renew(char *, rval, (c + 1));
132c2cdf5c5SKevin Wolf         rval[c - 1] = p;
133c2cdf5c5SKevin Wolf         rval[c] = NULL;
134c2cdf5c5SKevin Wolf     }
135c2cdf5c5SKevin Wolf     *count = c;
136c2cdf5c5SKevin Wolf     return rval;
137c2cdf5c5SKevin Wolf }
138c2cdf5c5SKevin Wolf 
139797ac58cSKevin Wolf static int64_t cvtnum(const char *s)
140797ac58cSKevin Wolf {
141797ac58cSKevin Wolf     char *end;
142ef5a7885SJohn Snow     int64_t ret;
143ef5a7885SJohn Snow 
144ef5a7885SJohn Snow     ret = qemu_strtosz_suffix(s, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
145ef5a7885SJohn Snow     if (*end != '\0') {
146ef5a7885SJohn Snow         /* Detritus at the end of the string */
147ef5a7885SJohn Snow         return -EINVAL;
148ef5a7885SJohn Snow     }
149ef5a7885SJohn Snow     return ret;
150797ac58cSKevin Wolf }
151797ac58cSKevin Wolf 
152a9ecfa00SJohn Snow static void print_cvtnum_err(int64_t rc, const char *arg)
153a9ecfa00SJohn Snow {
154a9ecfa00SJohn Snow     switch (rc) {
155a9ecfa00SJohn Snow     case -EINVAL:
156a9ecfa00SJohn Snow         printf("Parsing error: non-numeric argument,"
157a9ecfa00SJohn Snow                " or extraneous/unrecognized suffix -- %s\n", arg);
158a9ecfa00SJohn Snow         break;
159a9ecfa00SJohn Snow     case -ERANGE:
160a9ecfa00SJohn Snow         printf("Parsing error: argument too large -- %s\n", arg);
161a9ecfa00SJohn Snow         break;
162a9ecfa00SJohn Snow     default:
163a9ecfa00SJohn Snow         printf("Parsing error: %s\n", arg);
164a9ecfa00SJohn Snow     }
165a9ecfa00SJohn Snow }
166a9ecfa00SJohn Snow 
1670b613881SKevin Wolf #define EXABYTES(x)     ((long long)(x) << 60)
1680b613881SKevin Wolf #define PETABYTES(x)    ((long long)(x) << 50)
1690b613881SKevin Wolf #define TERABYTES(x)    ((long long)(x) << 40)
1700b613881SKevin Wolf #define GIGABYTES(x)    ((long long)(x) << 30)
1710b613881SKevin Wolf #define MEGABYTES(x)    ((long long)(x) << 20)
1720b613881SKevin Wolf #define KILOBYTES(x)    ((long long)(x) << 10)
1730b613881SKevin Wolf 
1740b613881SKevin Wolf #define TO_EXABYTES(x)  ((x) / EXABYTES(1))
1750b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1))
1760b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1))
1770b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
1780b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
1790b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
1800b613881SKevin Wolf 
1810b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size)
1820b613881SKevin Wolf {
1830b613881SKevin Wolf     char *trim;
1840b613881SKevin Wolf     const char *suffix;
1850b613881SKevin Wolf 
1860b613881SKevin Wolf     if (value >= EXABYTES(1)) {
1870b613881SKevin Wolf         suffix = " EiB";
1880b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
1890b613881SKevin Wolf     } else if (value >= PETABYTES(1)) {
1900b613881SKevin Wolf         suffix = " PiB";
1910b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
1920b613881SKevin Wolf     } else if (value >= TERABYTES(1)) {
1930b613881SKevin Wolf         suffix = " TiB";
1940b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
1950b613881SKevin Wolf     } else if (value >= GIGABYTES(1)) {
1960b613881SKevin Wolf         suffix = " GiB";
1970b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
1980b613881SKevin Wolf     } else if (value >= MEGABYTES(1)) {
1990b613881SKevin Wolf         suffix = " MiB";
2000b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
2010b613881SKevin Wolf     } else if (value >= KILOBYTES(1)) {
2020b613881SKevin Wolf         suffix = " KiB";
2030b613881SKevin Wolf         snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
2040b613881SKevin Wolf     } else {
2050b613881SKevin Wolf         suffix = " bytes";
2060b613881SKevin Wolf         snprintf(str, size - 6, "%f", value);
2070b613881SKevin Wolf     }
2080b613881SKevin Wolf 
2090b613881SKevin Wolf     trim = strstr(str, ".000");
2100b613881SKevin Wolf     if (trim) {
2110b613881SKevin Wolf         strcpy(trim, suffix);
2120b613881SKevin Wolf     } else {
2130b613881SKevin Wolf         strcat(str, suffix);
2140b613881SKevin Wolf     }
2150b613881SKevin Wolf }
2160b613881SKevin Wolf 
2170b613881SKevin Wolf 
2180b613881SKevin Wolf 
2190b613881SKevin Wolf static struct timeval tsub(struct timeval t1, struct timeval t2)
2200b613881SKevin Wolf {
2210b613881SKevin Wolf     t1.tv_usec -= t2.tv_usec;
2220b613881SKevin Wolf     if (t1.tv_usec < 0) {
2230b613881SKevin Wolf         t1.tv_usec += 1000000;
2240b613881SKevin Wolf         t1.tv_sec--;
2250b613881SKevin Wolf     }
2260b613881SKevin Wolf     t1.tv_sec -= t2.tv_sec;
2270b613881SKevin Wolf     return t1;
2280b613881SKevin Wolf }
2290b613881SKevin Wolf 
2300b613881SKevin Wolf static double tdiv(double value, struct timeval tv)
2310b613881SKevin Wolf {
2320b613881SKevin Wolf     return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
2330b613881SKevin Wolf }
2340b613881SKevin Wolf 
2350b613881SKevin Wolf #define HOURS(sec)      ((sec) / (60 * 60))
2360b613881SKevin Wolf #define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
2370b613881SKevin Wolf #define SECONDS(sec)    ((sec) % 60)
2380b613881SKevin Wolf 
2390b613881SKevin Wolf enum {
2400b613881SKevin Wolf     DEFAULT_TIME        = 0x0,
2410b613881SKevin Wolf     TERSE_FIXED_TIME    = 0x1,
2420b613881SKevin Wolf     VERBOSE_FIXED_TIME  = 0x2,
2430b613881SKevin Wolf };
2440b613881SKevin Wolf 
2450b613881SKevin Wolf static void timestr(struct timeval *tv, char *ts, size_t size, int format)
2460b613881SKevin Wolf {
2470b613881SKevin Wolf     double usec = (double)tv->tv_usec / 1000000.0;
2480b613881SKevin Wolf 
2490b613881SKevin Wolf     if (format & TERSE_FIXED_TIME) {
2500b613881SKevin Wolf         if (!HOURS(tv->tv_sec)) {
2510b613881SKevin Wolf             snprintf(ts, size, "%u:%02u.%02u",
2520b613881SKevin Wolf                     (unsigned int) MINUTES(tv->tv_sec),
2530b613881SKevin Wolf                     (unsigned int) SECONDS(tv->tv_sec),
2540b613881SKevin Wolf                     (unsigned int) (usec * 100));
2550b613881SKevin Wolf             return;
2560b613881SKevin Wolf         }
2570b613881SKevin Wolf         format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
2580b613881SKevin Wolf     }
2590b613881SKevin Wolf 
2600b613881SKevin Wolf     if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
2610b613881SKevin Wolf         snprintf(ts, size, "%u:%02u:%02u.%02u",
2620b613881SKevin Wolf                 (unsigned int) HOURS(tv->tv_sec),
2630b613881SKevin Wolf                 (unsigned int) MINUTES(tv->tv_sec),
2640b613881SKevin Wolf                 (unsigned int) SECONDS(tv->tv_sec),
2650b613881SKevin Wolf                 (unsigned int) (usec * 100));
2660b613881SKevin Wolf     } else {
2670b613881SKevin Wolf         snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
2680b613881SKevin Wolf     }
2690b613881SKevin Wolf }
2700b613881SKevin Wolf 
271797ac58cSKevin Wolf /*
272797ac58cSKevin Wolf  * Parse the pattern argument to various sub-commands.
273797ac58cSKevin Wolf  *
274797ac58cSKevin Wolf  * Because the pattern is used as an argument to memset it must evaluate
275797ac58cSKevin Wolf  * to an unsigned integer that fits into a single byte.
276797ac58cSKevin Wolf  */
277797ac58cSKevin Wolf static int parse_pattern(const char *arg)
278797ac58cSKevin Wolf {
279797ac58cSKevin Wolf     char *endptr = NULL;
280797ac58cSKevin Wolf     long pattern;
281797ac58cSKevin Wolf 
282797ac58cSKevin Wolf     pattern = strtol(arg, &endptr, 0);
283797ac58cSKevin Wolf     if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
284797ac58cSKevin Wolf         printf("%s is not a valid pattern byte\n", arg);
285797ac58cSKevin Wolf         return -1;
286797ac58cSKevin Wolf     }
287797ac58cSKevin Wolf 
288797ac58cSKevin Wolf     return pattern;
289797ac58cSKevin Wolf }
290797ac58cSKevin Wolf 
291797ac58cSKevin Wolf /*
292797ac58cSKevin Wolf  * Memory allocation helpers.
293797ac58cSKevin Wolf  *
294797ac58cSKevin Wolf  * Make sure memory is aligned by default, or purposefully misaligned if
295797ac58cSKevin Wolf  * that is specified on the command line.
296797ac58cSKevin Wolf  */
297797ac58cSKevin Wolf 
298797ac58cSKevin Wolf #define MISALIGN_OFFSET     16
2994c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
300797ac58cSKevin Wolf {
301797ac58cSKevin Wolf     void *buf;
302797ac58cSKevin Wolf 
303797ac58cSKevin Wolf     if (qemuio_misalign) {
304797ac58cSKevin Wolf         len += MISALIGN_OFFSET;
305797ac58cSKevin Wolf     }
3064c7b7e9bSMax Reitz     buf = blk_blockalign(blk, len);
307797ac58cSKevin Wolf     memset(buf, pattern, len);
308797ac58cSKevin Wolf     if (qemuio_misalign) {
309797ac58cSKevin Wolf         buf += MISALIGN_OFFSET;
310797ac58cSKevin Wolf     }
311797ac58cSKevin Wolf     return buf;
312797ac58cSKevin Wolf }
313797ac58cSKevin Wolf 
314797ac58cSKevin Wolf static void qemu_io_free(void *p)
315797ac58cSKevin Wolf {
316797ac58cSKevin Wolf     if (qemuio_misalign) {
317797ac58cSKevin Wolf         p -= MISALIGN_OFFSET;
318797ac58cSKevin Wolf     }
319797ac58cSKevin Wolf     qemu_vfree(p);
320797ac58cSKevin Wolf }
321797ac58cSKevin Wolf 
3229b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
323797ac58cSKevin Wolf {
3249b0beaf3SJohn Snow     uint64_t i;
3259b0beaf3SJohn Snow     int j;
326797ac58cSKevin Wolf     const uint8_t *p;
327797ac58cSKevin Wolf 
328797ac58cSKevin Wolf     for (i = 0, p = buffer; i < len; i += 16) {
329797ac58cSKevin Wolf         const uint8_t *s = p;
330797ac58cSKevin Wolf 
331797ac58cSKevin Wolf         printf("%08" PRIx64 ":  ", offset + i);
332797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, p++) {
333797ac58cSKevin Wolf             printf("%02x ", *p);
334797ac58cSKevin Wolf         }
335797ac58cSKevin Wolf         printf(" ");
336797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, s++) {
337797ac58cSKevin Wolf             if (isalnum(*s)) {
338797ac58cSKevin Wolf                 printf("%c", *s);
339797ac58cSKevin Wolf             } else {
340797ac58cSKevin Wolf                 printf(".");
341797ac58cSKevin Wolf             }
342797ac58cSKevin Wolf         }
343797ac58cSKevin Wolf         printf("\n");
344797ac58cSKevin Wolf     }
345797ac58cSKevin Wolf }
346797ac58cSKevin Wolf 
347797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset,
348dc38852aSEric Blake                          int64_t count, int64_t total, int cnt, bool Cflag)
349797ac58cSKevin Wolf {
350797ac58cSKevin Wolf     char s1[64], s2[64], ts[64];
351797ac58cSKevin Wolf 
352797ac58cSKevin Wolf     timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
353797ac58cSKevin Wolf     if (!Cflag) {
354797ac58cSKevin Wolf         cvtstr((double)total, s1, sizeof(s1));
355797ac58cSKevin Wolf         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
3569b0beaf3SJohn Snow         printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
357797ac58cSKevin Wolf                op, total, count, offset);
358797ac58cSKevin Wolf         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
359797ac58cSKevin Wolf                s1, cnt, ts, s2, tdiv((double)cnt, *t));
360797ac58cSKevin Wolf     } else {/* bytes,ops,time,bytes/sec,ops/sec */
3619b0beaf3SJohn Snow         printf("%"PRId64",%d,%s,%.3f,%.3f\n",
362797ac58cSKevin Wolf             total, cnt, ts,
363797ac58cSKevin Wolf             tdiv((double)total, *t),
364797ac58cSKevin Wolf             tdiv((double)cnt, *t));
365797ac58cSKevin Wolf     }
366797ac58cSKevin Wolf }
367797ac58cSKevin Wolf 
368797ac58cSKevin Wolf /*
369797ac58cSKevin Wolf  * Parse multiple length statements for vectored I/O, and construct an I/O
370797ac58cSKevin Wolf  * vector matching it.
371797ac58cSKevin Wolf  */
372797ac58cSKevin Wolf static void *
3734c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
374797ac58cSKevin Wolf              int pattern)
375797ac58cSKevin Wolf {
376797ac58cSKevin Wolf     size_t *sizes = g_new0(size_t, nr_iov);
377797ac58cSKevin Wolf     size_t count = 0;
378797ac58cSKevin Wolf     void *buf = NULL;
379797ac58cSKevin Wolf     void *p;
380797ac58cSKevin Wolf     int i;
381797ac58cSKevin Wolf 
382797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
383797ac58cSKevin Wolf         char *arg = argv[i];
384797ac58cSKevin Wolf         int64_t len;
385797ac58cSKevin Wolf 
386797ac58cSKevin Wolf         len = cvtnum(arg);
387797ac58cSKevin Wolf         if (len < 0) {
388a9ecfa00SJohn Snow             print_cvtnum_err(len, arg);
389797ac58cSKevin Wolf             goto fail;
390797ac58cSKevin Wolf         }
391797ac58cSKevin Wolf 
392797ac58cSKevin Wolf         /* should be SIZE_T_MAX, but that doesn't exist */
393797ac58cSKevin Wolf         if (len > INT_MAX) {
394a9ecfa00SJohn Snow             printf("Argument '%s' exceeds maximum size %d\n", arg, INT_MAX);
395797ac58cSKevin Wolf             goto fail;
396797ac58cSKevin Wolf         }
397797ac58cSKevin Wolf 
398797ac58cSKevin Wolf         sizes[i] = len;
399797ac58cSKevin Wolf         count += len;
400797ac58cSKevin Wolf     }
401797ac58cSKevin Wolf 
402797ac58cSKevin Wolf     qemu_iovec_init(qiov, nr_iov);
403797ac58cSKevin Wolf 
4044c7b7e9bSMax Reitz     buf = p = qemu_io_alloc(blk, count, pattern);
405797ac58cSKevin Wolf 
406797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
407797ac58cSKevin Wolf         qemu_iovec_add(qiov, p, sizes[i]);
408797ac58cSKevin Wolf         p += sizes[i];
409797ac58cSKevin Wolf     }
410797ac58cSKevin Wolf 
411797ac58cSKevin Wolf fail:
412797ac58cSKevin Wolf     g_free(sizes);
413797ac58cSKevin Wolf     return buf;
414797ac58cSKevin Wolf }
415797ac58cSKevin Wolf 
4169b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
4179b0beaf3SJohn Snow                     int64_t count, int64_t *total)
418797ac58cSKevin Wolf {
4199b0beaf3SJohn Snow     if (count > INT_MAX) {
4209b0beaf3SJohn Snow         return -ERANGE;
4219b0beaf3SJohn Snow     }
4229b0beaf3SJohn Snow 
4234c7b7e9bSMax Reitz     *total = blk_pread(blk, offset, (uint8_t *)buf, count);
424797ac58cSKevin Wolf     if (*total < 0) {
425797ac58cSKevin Wolf         return *total;
426797ac58cSKevin Wolf     }
427797ac58cSKevin Wolf     return 1;
428797ac58cSKevin Wolf }
429797ac58cSKevin Wolf 
4309b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
431770e0e0eSEric Blake                      int64_t count, int flags, int64_t *total)
432797ac58cSKevin Wolf {
4339b0beaf3SJohn Snow     if (count > INT_MAX) {
4349b0beaf3SJohn Snow         return -ERANGE;
4359b0beaf3SJohn Snow     }
4369b0beaf3SJohn Snow 
437770e0e0eSEric Blake     *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, flags);
438797ac58cSKevin Wolf     if (*total < 0) {
439797ac58cSKevin Wolf         return *total;
440797ac58cSKevin Wolf     }
441797ac58cSKevin Wolf     return 1;
442797ac58cSKevin Wolf }
443797ac58cSKevin Wolf 
444797ac58cSKevin Wolf typedef struct {
4454c7b7e9bSMax Reitz     BlockBackend *blk;
446797ac58cSKevin Wolf     int64_t offset;
4479b0beaf3SJohn Snow     int64_t count;
4489b0beaf3SJohn Snow     int64_t *total;
449770e0e0eSEric Blake     int flags;
450797ac58cSKevin Wolf     int ret;
451797ac58cSKevin Wolf     bool done;
452797ac58cSKevin Wolf } CoWriteZeroes;
453797ac58cSKevin Wolf 
454797ac58cSKevin Wolf static void coroutine_fn co_write_zeroes_entry(void *opaque)
455797ac58cSKevin Wolf {
456797ac58cSKevin Wolf     CoWriteZeroes *data = opaque;
457797ac58cSKevin Wolf 
458770e0e0eSEric Blake     data->ret = blk_co_write_zeroes(data->blk, data->offset, data->count,
459770e0e0eSEric Blake                                     data->flags);
460797ac58cSKevin Wolf     data->done = true;
461797ac58cSKevin Wolf     if (data->ret < 0) {
462797ac58cSKevin Wolf         *data->total = data->ret;
463797ac58cSKevin Wolf         return;
464797ac58cSKevin Wolf     }
465797ac58cSKevin Wolf 
466797ac58cSKevin Wolf     *data->total = data->count;
467797ac58cSKevin Wolf }
468797ac58cSKevin Wolf 
4699b0beaf3SJohn Snow static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count,
470770e0e0eSEric Blake                               int flags, int64_t *total)
471797ac58cSKevin Wolf {
472797ac58cSKevin Wolf     Coroutine *co;
473797ac58cSKevin Wolf     CoWriteZeroes data = {
4744c7b7e9bSMax Reitz         .blk    = blk,
475797ac58cSKevin Wolf         .offset = offset,
476797ac58cSKevin Wolf         .count  = count,
477797ac58cSKevin Wolf         .total  = total,
478770e0e0eSEric Blake         .flags  = flags,
479797ac58cSKevin Wolf         .done   = false,
480797ac58cSKevin Wolf     };
481797ac58cSKevin Wolf 
4829b0beaf3SJohn Snow     if (count >> BDRV_SECTOR_BITS > INT_MAX) {
4839b0beaf3SJohn Snow         return -ERANGE;
4849b0beaf3SJohn Snow     }
4859b0beaf3SJohn Snow 
486797ac58cSKevin Wolf     co = qemu_coroutine_create(co_write_zeroes_entry);
487797ac58cSKevin Wolf     qemu_coroutine_enter(co, &data);
488797ac58cSKevin Wolf     while (!data.done) {
4894c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
490797ac58cSKevin Wolf     }
491797ac58cSKevin Wolf     if (data.ret < 0) {
492797ac58cSKevin Wolf         return data.ret;
493797ac58cSKevin Wolf     } else {
494797ac58cSKevin Wolf         return 1;
495797ac58cSKevin Wolf     }
496797ac58cSKevin Wolf }
497797ac58cSKevin Wolf 
4984c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
4999b0beaf3SJohn Snow                                int64_t count, int64_t *total)
500797ac58cSKevin Wolf {
501797ac58cSKevin Wolf     int ret;
502797ac58cSKevin Wolf 
5039b0beaf3SJohn Snow     if (count >> 9 > INT_MAX) {
5049b0beaf3SJohn Snow         return -ERANGE;
5059b0beaf3SJohn Snow     }
5069b0beaf3SJohn Snow 
5074c7b7e9bSMax Reitz     ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
508797ac58cSKevin Wolf     if (ret < 0) {
509797ac58cSKevin Wolf         return ret;
510797ac58cSKevin Wolf     }
511797ac58cSKevin Wolf     *total = count;
512797ac58cSKevin Wolf     return 1;
513797ac58cSKevin Wolf }
514797ac58cSKevin Wolf 
5154c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5169b0beaf3SJohn Snow                            int64_t count, int64_t *total)
517797ac58cSKevin Wolf {
5189b0beaf3SJohn Snow     if (count > INT_MAX) {
5199b0beaf3SJohn Snow         return -ERANGE;
5209b0beaf3SJohn Snow     }
5219b0beaf3SJohn Snow 
5224c7b7e9bSMax Reitz     *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
523797ac58cSKevin Wolf     if (*total < 0) {
524797ac58cSKevin Wolf         return *total;
525797ac58cSKevin Wolf     }
526797ac58cSKevin Wolf     return 1;
527797ac58cSKevin Wolf }
528797ac58cSKevin Wolf 
5294c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
5309b0beaf3SJohn Snow                            int64_t count, int64_t *total)
531797ac58cSKevin Wolf {
5329b0beaf3SJohn Snow     if (count > INT_MAX) {
5339b0beaf3SJohn Snow         return -ERANGE;
5349b0beaf3SJohn Snow     }
5359b0beaf3SJohn Snow 
5364c7b7e9bSMax Reitz     *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
537797ac58cSKevin Wolf     if (*total < 0) {
538797ac58cSKevin Wolf         return *total;
539797ac58cSKevin Wolf     }
540797ac58cSKevin Wolf     return 1;
541797ac58cSKevin Wolf }
542797ac58cSKevin Wolf 
543797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff
544797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret)
545797ac58cSKevin Wolf {
546797ac58cSKevin Wolf     *(int *)opaque = ret;
547797ac58cSKevin Wolf }
548797ac58cSKevin Wolf 
5494c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
550797ac58cSKevin Wolf                         int64_t offset, int *total)
551797ac58cSKevin Wolf {
552797ac58cSKevin Wolf     int async_ret = NOT_DONE;
553797ac58cSKevin Wolf 
5547b3f9712SEric Blake     blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
555797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
556797ac58cSKevin Wolf         main_loop_wait(false);
557797ac58cSKevin Wolf     }
558797ac58cSKevin Wolf 
559797ac58cSKevin Wolf     *total = qiov->size;
560797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
561797ac58cSKevin Wolf }
562797ac58cSKevin Wolf 
5634c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
564770e0e0eSEric Blake                          int64_t offset, int flags, int *total)
565797ac58cSKevin Wolf {
566797ac58cSKevin Wolf     int async_ret = NOT_DONE;
567797ac58cSKevin Wolf 
568770e0e0eSEric Blake     blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
569797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
570797ac58cSKevin Wolf         main_loop_wait(false);
571797ac58cSKevin Wolf     }
572797ac58cSKevin Wolf 
573797ac58cSKevin Wolf     *total = qiov->size;
574797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
575797ac58cSKevin Wolf }
576797ac58cSKevin Wolf 
577797ac58cSKevin Wolf struct multiwrite_async_ret {
578797ac58cSKevin Wolf     int num_done;
579797ac58cSKevin Wolf     int error;
580797ac58cSKevin Wolf };
581797ac58cSKevin Wolf 
582797ac58cSKevin Wolf static void multiwrite_cb(void *opaque, int ret)
583797ac58cSKevin Wolf {
584797ac58cSKevin Wolf     struct multiwrite_async_ret *async_ret = opaque;
585797ac58cSKevin Wolf 
586797ac58cSKevin Wolf     async_ret->num_done++;
587797ac58cSKevin Wolf     if (ret < 0) {
588797ac58cSKevin Wolf         async_ret->error = ret;
589797ac58cSKevin Wolf     }
590797ac58cSKevin Wolf }
591797ac58cSKevin Wolf 
5924c7b7e9bSMax Reitz static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
593797ac58cSKevin Wolf                              int num_reqs, int *total)
594797ac58cSKevin Wolf {
595797ac58cSKevin Wolf     int i, ret;
596797ac58cSKevin Wolf     struct multiwrite_async_ret async_ret = {
597797ac58cSKevin Wolf         .num_done = 0,
598797ac58cSKevin Wolf         .error = 0,
599797ac58cSKevin Wolf     };
600797ac58cSKevin Wolf 
601797ac58cSKevin Wolf     *total = 0;
602797ac58cSKevin Wolf     for (i = 0; i < num_reqs; i++) {
603797ac58cSKevin Wolf         reqs[i].cb = multiwrite_cb;
604797ac58cSKevin Wolf         reqs[i].opaque = &async_ret;
605797ac58cSKevin Wolf         *total += reqs[i].qiov->size;
606797ac58cSKevin Wolf     }
607797ac58cSKevin Wolf 
6084c7b7e9bSMax Reitz     ret = blk_aio_multiwrite(blk, reqs, num_reqs);
609797ac58cSKevin Wolf     if (ret < 0) {
610797ac58cSKevin Wolf         return ret;
611797ac58cSKevin Wolf     }
612797ac58cSKevin Wolf 
613797ac58cSKevin Wolf     while (async_ret.num_done < num_reqs) {
614797ac58cSKevin Wolf         main_loop_wait(false);
615797ac58cSKevin Wolf     }
616797ac58cSKevin Wolf 
617797ac58cSKevin Wolf     return async_ret.error < 0 ? async_ret.error : 1;
618797ac58cSKevin Wolf }
619797ac58cSKevin Wolf 
620797ac58cSKevin Wolf static void read_help(void)
621797ac58cSKevin Wolf {
622797ac58cSKevin Wolf     printf(
623797ac58cSKevin Wolf "\n"
624797ac58cSKevin Wolf " reads a range of bytes from the given offset\n"
625797ac58cSKevin Wolf "\n"
626797ac58cSKevin Wolf " Example:\n"
627797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
628797ac58cSKevin Wolf "\n"
629797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
630797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
631797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n"
632797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
633797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n"
634093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
635797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
636797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
637797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n"
638797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
639797ac58cSKevin Wolf "\n");
640797ac58cSKevin Wolf }
641797ac58cSKevin Wolf 
6424c7b7e9bSMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv);
643797ac58cSKevin Wolf 
644797ac58cSKevin Wolf static const cmdinfo_t read_cmd = {
645797ac58cSKevin Wolf     .name       = "read",
646797ac58cSKevin Wolf     .altname    = "r",
647797ac58cSKevin Wolf     .cfunc      = read_f,
648797ac58cSKevin Wolf     .argmin     = 2,
649797ac58cSKevin Wolf     .argmax     = -1,
650093ea232SEric Blake     .args       = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
651797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
652797ac58cSKevin Wolf     .help       = read_help,
653797ac58cSKevin Wolf };
654797ac58cSKevin Wolf 
6554c7b7e9bSMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv)
656797ac58cSKevin Wolf {
657797ac58cSKevin Wolf     struct timeval t1, t2;
658093ea232SEric Blake     bool Cflag = false, qflag = false, vflag = false;
659dc38852aSEric Blake     bool Pflag = false, sflag = false, lflag = false, bflag = false;
660797ac58cSKevin Wolf     int c, cnt;
661797ac58cSKevin Wolf     char *buf;
662797ac58cSKevin Wolf     int64_t offset;
6639b0beaf3SJohn Snow     int64_t count;
664797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
6659b0beaf3SJohn Snow     int64_t total = 0;
6669b0beaf3SJohn Snow     int pattern = 0;
6679b0beaf3SJohn Snow     int64_t pattern_offset = 0, pattern_count = 0;
668797ac58cSKevin Wolf 
669b062ad86SEric Blake     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
670797ac58cSKevin Wolf         switch (c) {
671797ac58cSKevin Wolf         case 'b':
672dc38852aSEric Blake             bflag = true;
673797ac58cSKevin Wolf             break;
674797ac58cSKevin Wolf         case 'C':
675dc38852aSEric Blake             Cflag = true;
676797ac58cSKevin Wolf             break;
677797ac58cSKevin Wolf         case 'l':
678dc38852aSEric Blake             lflag = true;
679797ac58cSKevin Wolf             pattern_count = cvtnum(optarg);
680797ac58cSKevin Wolf             if (pattern_count < 0) {
681a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_count, optarg);
682797ac58cSKevin Wolf                 return 0;
683797ac58cSKevin Wolf             }
684797ac58cSKevin Wolf             break;
685797ac58cSKevin Wolf         case 'p':
686093ea232SEric Blake             /* Ignored for backwards compatibility */
687797ac58cSKevin Wolf             break;
688797ac58cSKevin Wolf         case 'P':
689dc38852aSEric Blake             Pflag = true;
690797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
691797ac58cSKevin Wolf             if (pattern < 0) {
692797ac58cSKevin Wolf                 return 0;
693797ac58cSKevin Wolf             }
694797ac58cSKevin Wolf             break;
695797ac58cSKevin Wolf         case 'q':
696dc38852aSEric Blake             qflag = true;
697797ac58cSKevin Wolf             break;
698797ac58cSKevin Wolf         case 's':
699dc38852aSEric Blake             sflag = true;
700797ac58cSKevin Wolf             pattern_offset = cvtnum(optarg);
701797ac58cSKevin Wolf             if (pattern_offset < 0) {
702a9ecfa00SJohn Snow                 print_cvtnum_err(pattern_offset, optarg);
703797ac58cSKevin Wolf                 return 0;
704797ac58cSKevin Wolf             }
705797ac58cSKevin Wolf             break;
706797ac58cSKevin Wolf         case 'v':
707dc38852aSEric Blake             vflag = true;
708797ac58cSKevin Wolf             break;
709797ac58cSKevin Wolf         default:
710c2cdf5c5SKevin Wolf             return qemuio_command_usage(&read_cmd);
711797ac58cSKevin Wolf         }
712797ac58cSKevin Wolf     }
713797ac58cSKevin Wolf 
714797ac58cSKevin Wolf     if (optind != argc - 2) {
715c2cdf5c5SKevin Wolf         return qemuio_command_usage(&read_cmd);
716797ac58cSKevin Wolf     }
717797ac58cSKevin Wolf 
718797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
719797ac58cSKevin Wolf     if (offset < 0) {
720a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
721797ac58cSKevin Wolf         return 0;
722797ac58cSKevin Wolf     }
723797ac58cSKevin Wolf 
724797ac58cSKevin Wolf     optind++;
725797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
726797ac58cSKevin Wolf     if (count < 0) {
727a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
728797ac58cSKevin Wolf         return 0;
7299b0beaf3SJohn Snow     } else if (count > SIZE_MAX) {
7309b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
7319b0beaf3SJohn Snow                (uint64_t) SIZE_MAX, argv[optind]);
7329b0beaf3SJohn Snow         return 0;
733797ac58cSKevin Wolf     }
734797ac58cSKevin Wolf 
735797ac58cSKevin Wolf     if (!Pflag && (lflag || sflag)) {
736c2cdf5c5SKevin Wolf         return qemuio_command_usage(&read_cmd);
737797ac58cSKevin Wolf     }
738797ac58cSKevin Wolf 
739797ac58cSKevin Wolf     if (!lflag) {
740797ac58cSKevin Wolf         pattern_count = count - pattern_offset;
741797ac58cSKevin Wolf     }
742797ac58cSKevin Wolf 
743797ac58cSKevin Wolf     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
744797ac58cSKevin Wolf         printf("pattern verification range exceeds end of read data\n");
745797ac58cSKevin Wolf         return 0;
746797ac58cSKevin Wolf     }
747797ac58cSKevin Wolf 
748093ea232SEric Blake     if (bflag) {
749797ac58cSKevin Wolf         if (offset & 0x1ff) {
750797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
751797ac58cSKevin Wolf                    offset);
752797ac58cSKevin Wolf             return 0;
753797ac58cSKevin Wolf         }
754797ac58cSKevin Wolf         if (count & 0x1ff) {
7559b0beaf3SJohn Snow             printf("count %"PRId64" is not sector aligned\n",
756797ac58cSKevin Wolf                    count);
757797ac58cSKevin Wolf             return 0;
758797ac58cSKevin Wolf         }
759797ac58cSKevin Wolf     }
760797ac58cSKevin Wolf 
7614c7b7e9bSMax Reitz     buf = qemu_io_alloc(blk, count, 0xab);
762797ac58cSKevin Wolf 
763797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
7647b3f9712SEric Blake     if (bflag) {
7654c7b7e9bSMax Reitz         cnt = do_load_vmstate(blk, buf, offset, count, &total);
766797ac58cSKevin Wolf     } else {
7677b3f9712SEric Blake         cnt = do_pread(blk, buf, offset, count, &total);
768797ac58cSKevin Wolf     }
769797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
770797ac58cSKevin Wolf 
771797ac58cSKevin Wolf     if (cnt < 0) {
772797ac58cSKevin Wolf         printf("read failed: %s\n", strerror(-cnt));
773797ac58cSKevin Wolf         goto out;
774797ac58cSKevin Wolf     }
775797ac58cSKevin Wolf 
776797ac58cSKevin Wolf     if (Pflag) {
777797ac58cSKevin Wolf         void *cmp_buf = g_malloc(pattern_count);
778797ac58cSKevin Wolf         memset(cmp_buf, pattern, pattern_count);
779797ac58cSKevin Wolf         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
780797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
7819b0beaf3SJohn Snow                    PRId64 ", %"PRId64" bytes\n",
782797ac58cSKevin Wolf                    offset + pattern_offset, pattern_count);
783797ac58cSKevin Wolf         }
784797ac58cSKevin Wolf         g_free(cmp_buf);
785797ac58cSKevin Wolf     }
786797ac58cSKevin Wolf 
787797ac58cSKevin Wolf     if (qflag) {
788797ac58cSKevin Wolf         goto out;
789797ac58cSKevin Wolf     }
790797ac58cSKevin Wolf 
791797ac58cSKevin Wolf     if (vflag) {
792797ac58cSKevin Wolf         dump_buffer(buf, offset, count);
793797ac58cSKevin Wolf     }
794797ac58cSKevin Wolf 
795797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
796797ac58cSKevin Wolf     t2 = tsub(t2, t1);
797797ac58cSKevin Wolf     print_report("read", &t2, offset, count, total, cnt, Cflag);
798797ac58cSKevin Wolf 
799797ac58cSKevin Wolf out:
800797ac58cSKevin Wolf     qemu_io_free(buf);
801797ac58cSKevin Wolf 
802797ac58cSKevin Wolf     return 0;
803797ac58cSKevin Wolf }
804797ac58cSKevin Wolf 
805797ac58cSKevin Wolf static void readv_help(void)
806797ac58cSKevin Wolf {
807797ac58cSKevin Wolf     printf(
808797ac58cSKevin Wolf "\n"
809797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n"
810797ac58cSKevin Wolf "\n"
811797ac58cSKevin Wolf " Example:\n"
812797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
813797ac58cSKevin Wolf "\n"
814797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
815797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
816797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n"
817797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
818797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
819797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
820797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
821797ac58cSKevin Wolf "\n");
822797ac58cSKevin Wolf }
823797ac58cSKevin Wolf 
8244c7b7e9bSMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv);
825797ac58cSKevin Wolf 
826797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = {
827797ac58cSKevin Wolf     .name       = "readv",
828797ac58cSKevin Wolf     .cfunc      = readv_f,
829797ac58cSKevin Wolf     .argmin     = 2,
830797ac58cSKevin Wolf     .argmax     = -1,
831797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern] off len [len..]",
832797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
833797ac58cSKevin Wolf     .help       = readv_help,
834797ac58cSKevin Wolf };
835797ac58cSKevin Wolf 
8364c7b7e9bSMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv)
837797ac58cSKevin Wolf {
838797ac58cSKevin Wolf     struct timeval t1, t2;
839dc38852aSEric Blake     bool Cflag = false, qflag = false, vflag = false;
840797ac58cSKevin Wolf     int c, cnt;
841797ac58cSKevin Wolf     char *buf;
842797ac58cSKevin Wolf     int64_t offset;
843797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
844797ac58cSKevin Wolf     int total = 0;
845797ac58cSKevin Wolf     int nr_iov;
846797ac58cSKevin Wolf     QEMUIOVector qiov;
847797ac58cSKevin Wolf     int pattern = 0;
848dc38852aSEric Blake     bool Pflag = false;
849797ac58cSKevin Wolf 
850b062ad86SEric Blake     while ((c = getopt(argc, argv, "CP:qv")) != -1) {
851797ac58cSKevin Wolf         switch (c) {
852797ac58cSKevin Wolf         case 'C':
853dc38852aSEric Blake             Cflag = true;
854797ac58cSKevin Wolf             break;
855797ac58cSKevin Wolf         case 'P':
856dc38852aSEric Blake             Pflag = true;
857797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
858797ac58cSKevin Wolf             if (pattern < 0) {
859797ac58cSKevin Wolf                 return 0;
860797ac58cSKevin Wolf             }
861797ac58cSKevin Wolf             break;
862797ac58cSKevin Wolf         case 'q':
863dc38852aSEric Blake             qflag = true;
864797ac58cSKevin Wolf             break;
865797ac58cSKevin Wolf         case 'v':
866dc38852aSEric Blake             vflag = true;
867797ac58cSKevin Wolf             break;
868797ac58cSKevin Wolf         default:
869c2cdf5c5SKevin Wolf             return qemuio_command_usage(&readv_cmd);
870797ac58cSKevin Wolf         }
871797ac58cSKevin Wolf     }
872797ac58cSKevin Wolf 
873797ac58cSKevin Wolf     if (optind > argc - 2) {
874c2cdf5c5SKevin Wolf         return qemuio_command_usage(&readv_cmd);
875797ac58cSKevin Wolf     }
876797ac58cSKevin Wolf 
877797ac58cSKevin Wolf 
878797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
879797ac58cSKevin Wolf     if (offset < 0) {
880a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
881797ac58cSKevin Wolf         return 0;
882797ac58cSKevin Wolf     }
883797ac58cSKevin Wolf     optind++;
884797ac58cSKevin Wolf 
885797ac58cSKevin Wolf     nr_iov = argc - optind;
8864c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
887797ac58cSKevin Wolf     if (buf == NULL) {
888797ac58cSKevin Wolf         return 0;
889797ac58cSKevin Wolf     }
890797ac58cSKevin Wolf 
891797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
8924c7b7e9bSMax Reitz     cnt = do_aio_readv(blk, &qiov, offset, &total);
893797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
894797ac58cSKevin Wolf 
895797ac58cSKevin Wolf     if (cnt < 0) {
896797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-cnt));
897797ac58cSKevin Wolf         goto out;
898797ac58cSKevin Wolf     }
899797ac58cSKevin Wolf 
900797ac58cSKevin Wolf     if (Pflag) {
901797ac58cSKevin Wolf         void *cmp_buf = g_malloc(qiov.size);
902797ac58cSKevin Wolf         memset(cmp_buf, pattern, qiov.size);
903797ac58cSKevin Wolf         if (memcmp(buf, cmp_buf, qiov.size)) {
904797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
905797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", offset, qiov.size);
906797ac58cSKevin Wolf         }
907797ac58cSKevin Wolf         g_free(cmp_buf);
908797ac58cSKevin Wolf     }
909797ac58cSKevin Wolf 
910797ac58cSKevin Wolf     if (qflag) {
911797ac58cSKevin Wolf         goto out;
912797ac58cSKevin Wolf     }
913797ac58cSKevin Wolf 
914797ac58cSKevin Wolf     if (vflag) {
915797ac58cSKevin Wolf         dump_buffer(buf, offset, qiov.size);
916797ac58cSKevin Wolf     }
917797ac58cSKevin Wolf 
918797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
919797ac58cSKevin Wolf     t2 = tsub(t2, t1);
920797ac58cSKevin Wolf     print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
921797ac58cSKevin Wolf 
922797ac58cSKevin Wolf out:
923797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
924797ac58cSKevin Wolf     qemu_io_free(buf);
925797ac58cSKevin Wolf     return 0;
926797ac58cSKevin Wolf }
927797ac58cSKevin Wolf 
928797ac58cSKevin Wolf static void write_help(void)
929797ac58cSKevin Wolf {
930797ac58cSKevin Wolf     printf(
931797ac58cSKevin Wolf "\n"
932797ac58cSKevin Wolf " writes a range of bytes from the given offset\n"
933797ac58cSKevin Wolf "\n"
934797ac58cSKevin Wolf " Example:\n"
935797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
936797ac58cSKevin Wolf "\n"
937797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
938797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
939797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n"
9404c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n"
941770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
942093ea232SEric Blake " -p, -- ignored for backwards compatibility\n"
943797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
944797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
945797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
946*c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
9474c7b7e9bSMax Reitz " -z, -- write zeroes using blk_co_write_zeroes\n"
948797ac58cSKevin Wolf "\n");
949797ac58cSKevin Wolf }
950797ac58cSKevin Wolf 
9514c7b7e9bSMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv);
952797ac58cSKevin Wolf 
953797ac58cSKevin Wolf static const cmdinfo_t write_cmd = {
954797ac58cSKevin Wolf     .name       = "write",
955797ac58cSKevin Wolf     .altname    = "w",
956797ac58cSKevin Wolf     .cfunc      = write_f,
957797ac58cSKevin Wolf     .argmin     = 2,
958797ac58cSKevin Wolf     .argmax     = -1,
959*c2e001ccSEric Blake     .args       = "[-bcCfquz] [-P pattern] off len",
960797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
961797ac58cSKevin Wolf     .help       = write_help,
962797ac58cSKevin Wolf };
963797ac58cSKevin Wolf 
9644c7b7e9bSMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv)
965797ac58cSKevin Wolf {
966797ac58cSKevin Wolf     struct timeval t1, t2;
967093ea232SEric Blake     bool Cflag = false, qflag = false, bflag = false;
968dc38852aSEric Blake     bool Pflag = false, zflag = false, cflag = false;
969770e0e0eSEric Blake     int flags = 0;
970797ac58cSKevin Wolf     int c, cnt;
971797ac58cSKevin Wolf     char *buf = NULL;
972797ac58cSKevin Wolf     int64_t offset;
9739b0beaf3SJohn Snow     int64_t count;
974797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
9759b0beaf3SJohn Snow     int64_t total = 0;
976797ac58cSKevin Wolf     int pattern = 0xcd;
977797ac58cSKevin Wolf 
978*c2e001ccSEric Blake     while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
979797ac58cSKevin Wolf         switch (c) {
980797ac58cSKevin Wolf         case 'b':
981dc38852aSEric Blake             bflag = true;
982797ac58cSKevin Wolf             break;
983797ac58cSKevin Wolf         case 'c':
984dc38852aSEric Blake             cflag = true;
985797ac58cSKevin Wolf             break;
986797ac58cSKevin Wolf         case 'C':
987dc38852aSEric Blake             Cflag = true;
988797ac58cSKevin Wolf             break;
989770e0e0eSEric Blake         case 'f':
990770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
991770e0e0eSEric Blake             break;
992797ac58cSKevin Wolf         case 'p':
993093ea232SEric Blake             /* Ignored for backwards compatibility */
994797ac58cSKevin Wolf             break;
995797ac58cSKevin Wolf         case 'P':
996dc38852aSEric Blake             Pflag = true;
997797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
998797ac58cSKevin Wolf             if (pattern < 0) {
999797ac58cSKevin Wolf                 return 0;
1000797ac58cSKevin Wolf             }
1001797ac58cSKevin Wolf             break;
1002797ac58cSKevin Wolf         case 'q':
1003dc38852aSEric Blake             qflag = true;
1004797ac58cSKevin Wolf             break;
1005*c2e001ccSEric Blake         case 'u':
1006*c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1007*c2e001ccSEric Blake             break;
1008797ac58cSKevin Wolf         case 'z':
1009dc38852aSEric Blake             zflag = true;
1010797ac58cSKevin Wolf             break;
1011797ac58cSKevin Wolf         default:
1012c2cdf5c5SKevin Wolf             return qemuio_command_usage(&write_cmd);
1013797ac58cSKevin Wolf         }
1014797ac58cSKevin Wolf     }
1015797ac58cSKevin Wolf 
1016797ac58cSKevin Wolf     if (optind != argc - 2) {
1017c2cdf5c5SKevin Wolf         return qemuio_command_usage(&write_cmd);
1018797ac58cSKevin Wolf     }
1019797ac58cSKevin Wolf 
1020093ea232SEric Blake     if (bflag && zflag) {
1021093ea232SEric Blake         printf("-b and -z cannot be specified at the same time\n");
1022797ac58cSKevin Wolf         return 0;
1023797ac58cSKevin Wolf     }
1024797ac58cSKevin Wolf 
1025770e0e0eSEric Blake     if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1026770e0e0eSEric Blake         printf("-f and -b or -c cannot be specified at the same time\n");
1027770e0e0eSEric Blake         return 0;
1028770e0e0eSEric Blake     }
1029770e0e0eSEric Blake 
1030*c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1031*c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
1032*c2e001ccSEric Blake         return 0;
1033*c2e001ccSEric Blake     }
1034*c2e001ccSEric Blake 
1035797ac58cSKevin Wolf     if (zflag && Pflag) {
1036797ac58cSKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
1037797ac58cSKevin Wolf         return 0;
1038797ac58cSKevin Wolf     }
1039797ac58cSKevin Wolf 
1040797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1041797ac58cSKevin Wolf     if (offset < 0) {
1042a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1043797ac58cSKevin Wolf         return 0;
1044797ac58cSKevin Wolf     }
1045797ac58cSKevin Wolf 
1046797ac58cSKevin Wolf     optind++;
1047797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1048797ac58cSKevin Wolf     if (count < 0) {
1049a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
1050797ac58cSKevin Wolf         return 0;
10519b0beaf3SJohn Snow     } else if (count > SIZE_MAX) {
10529b0beaf3SJohn Snow         printf("length cannot exceed %" PRIu64 ", given %s\n",
10539b0beaf3SJohn Snow                (uint64_t) SIZE_MAX, argv[optind]);
10549b0beaf3SJohn Snow         return 0;
1055797ac58cSKevin Wolf     }
1056797ac58cSKevin Wolf 
1057093ea232SEric Blake     if (bflag || cflag) {
1058797ac58cSKevin Wolf         if (offset & 0x1ff) {
1059797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
1060797ac58cSKevin Wolf                    offset);
1061797ac58cSKevin Wolf             return 0;
1062797ac58cSKevin Wolf         }
1063797ac58cSKevin Wolf 
1064797ac58cSKevin Wolf         if (count & 0x1ff) {
10659b0beaf3SJohn Snow             printf("count %"PRId64" is not sector aligned\n",
1066797ac58cSKevin Wolf                    count);
1067797ac58cSKevin Wolf             return 0;
1068797ac58cSKevin Wolf         }
1069797ac58cSKevin Wolf     }
1070797ac58cSKevin Wolf 
1071797ac58cSKevin Wolf     if (!zflag) {
10724c7b7e9bSMax Reitz         buf = qemu_io_alloc(blk, count, pattern);
1073797ac58cSKevin Wolf     }
1074797ac58cSKevin Wolf 
1075797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
10767b3f9712SEric Blake     if (bflag) {
10774c7b7e9bSMax Reitz         cnt = do_save_vmstate(blk, buf, offset, count, &total);
1078797ac58cSKevin Wolf     } else if (zflag) {
1079770e0e0eSEric Blake         cnt = do_co_write_zeroes(blk, offset, count, flags, &total);
1080797ac58cSKevin Wolf     } else if (cflag) {
10814c7b7e9bSMax Reitz         cnt = do_write_compressed(blk, buf, offset, count, &total);
1082797ac58cSKevin Wolf     } else {
1083770e0e0eSEric Blake         cnt = do_pwrite(blk, buf, offset, count, flags, &total);
1084797ac58cSKevin Wolf     }
1085797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1086797ac58cSKevin Wolf 
1087797ac58cSKevin Wolf     if (cnt < 0) {
1088797ac58cSKevin Wolf         printf("write failed: %s\n", strerror(-cnt));
1089797ac58cSKevin Wolf         goto out;
1090797ac58cSKevin Wolf     }
1091797ac58cSKevin Wolf 
1092797ac58cSKevin Wolf     if (qflag) {
1093797ac58cSKevin Wolf         goto out;
1094797ac58cSKevin Wolf     }
1095797ac58cSKevin Wolf 
1096797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1097797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1098797ac58cSKevin Wolf     print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1099797ac58cSKevin Wolf 
1100797ac58cSKevin Wolf out:
1101797ac58cSKevin Wolf     if (!zflag) {
1102797ac58cSKevin Wolf         qemu_io_free(buf);
1103797ac58cSKevin Wolf     }
1104797ac58cSKevin Wolf 
1105797ac58cSKevin Wolf     return 0;
1106797ac58cSKevin Wolf }
1107797ac58cSKevin Wolf 
1108797ac58cSKevin Wolf static void
1109797ac58cSKevin Wolf writev_help(void)
1110797ac58cSKevin Wolf {
1111797ac58cSKevin Wolf     printf(
1112797ac58cSKevin Wolf "\n"
1113797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n"
1114797ac58cSKevin Wolf "\n"
1115797ac58cSKevin Wolf " Example:\n"
11166e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1117797ac58cSKevin Wolf "\n"
1118797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1119797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1120797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1121797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1122770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
1123797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1124797ac58cSKevin Wolf "\n");
1125797ac58cSKevin Wolf }
1126797ac58cSKevin Wolf 
11274c7b7e9bSMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv);
1128797ac58cSKevin Wolf 
1129797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = {
1130797ac58cSKevin Wolf     .name       = "writev",
1131797ac58cSKevin Wolf     .cfunc      = writev_f,
1132797ac58cSKevin Wolf     .argmin     = 2,
1133797ac58cSKevin Wolf     .argmax     = -1,
1134770e0e0eSEric Blake     .args       = "[-Cfq] [-P pattern] off len [len..]",
1135797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
1136797ac58cSKevin Wolf     .help       = writev_help,
1137797ac58cSKevin Wolf };
1138797ac58cSKevin Wolf 
11394c7b7e9bSMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv)
1140797ac58cSKevin Wolf {
1141797ac58cSKevin Wolf     struct timeval t1, t2;
1142dc38852aSEric Blake     bool Cflag = false, qflag = false;
1143770e0e0eSEric Blake     int flags = 0;
1144797ac58cSKevin Wolf     int c, cnt;
1145797ac58cSKevin Wolf     char *buf;
1146797ac58cSKevin Wolf     int64_t offset;
1147797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1148797ac58cSKevin Wolf     int total = 0;
1149797ac58cSKevin Wolf     int nr_iov;
1150797ac58cSKevin Wolf     int pattern = 0xcd;
1151797ac58cSKevin Wolf     QEMUIOVector qiov;
1152797ac58cSKevin Wolf 
1153b062ad86SEric Blake     while ((c = getopt(argc, argv, "CqP:")) != -1) {
1154797ac58cSKevin Wolf         switch (c) {
1155797ac58cSKevin Wolf         case 'C':
1156dc38852aSEric Blake             Cflag = true;
1157797ac58cSKevin Wolf             break;
1158770e0e0eSEric Blake         case 'f':
1159770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1160770e0e0eSEric Blake             break;
1161797ac58cSKevin Wolf         case 'q':
1162dc38852aSEric Blake             qflag = true;
1163797ac58cSKevin Wolf             break;
1164797ac58cSKevin Wolf         case 'P':
1165797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1166797ac58cSKevin Wolf             if (pattern < 0) {
1167797ac58cSKevin Wolf                 return 0;
1168797ac58cSKevin Wolf             }
1169797ac58cSKevin Wolf             break;
1170797ac58cSKevin Wolf         default:
1171c2cdf5c5SKevin Wolf             return qemuio_command_usage(&writev_cmd);
1172797ac58cSKevin Wolf         }
1173797ac58cSKevin Wolf     }
1174797ac58cSKevin Wolf 
1175797ac58cSKevin Wolf     if (optind > argc - 2) {
1176c2cdf5c5SKevin Wolf         return qemuio_command_usage(&writev_cmd);
1177797ac58cSKevin Wolf     }
1178797ac58cSKevin Wolf 
1179797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1180797ac58cSKevin Wolf     if (offset < 0) {
1181a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1182797ac58cSKevin Wolf         return 0;
1183797ac58cSKevin Wolf     }
1184797ac58cSKevin Wolf     optind++;
1185797ac58cSKevin Wolf 
1186797ac58cSKevin Wolf     nr_iov = argc - optind;
11874c7b7e9bSMax Reitz     buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1188797ac58cSKevin Wolf     if (buf == NULL) {
1189797ac58cSKevin Wolf         return 0;
1190797ac58cSKevin Wolf     }
1191797ac58cSKevin Wolf 
1192797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1193770e0e0eSEric Blake     cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
1194797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1195797ac58cSKevin Wolf 
1196797ac58cSKevin Wolf     if (cnt < 0) {
1197797ac58cSKevin Wolf         printf("writev failed: %s\n", strerror(-cnt));
1198797ac58cSKevin Wolf         goto out;
1199797ac58cSKevin Wolf     }
1200797ac58cSKevin Wolf 
1201797ac58cSKevin Wolf     if (qflag) {
1202797ac58cSKevin Wolf         goto out;
1203797ac58cSKevin Wolf     }
1204797ac58cSKevin Wolf 
1205797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1206797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1207797ac58cSKevin Wolf     print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1208797ac58cSKevin Wolf out:
1209797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
1210797ac58cSKevin Wolf     qemu_io_free(buf);
1211797ac58cSKevin Wolf     return 0;
1212797ac58cSKevin Wolf }
1213797ac58cSKevin Wolf 
1214797ac58cSKevin Wolf static void multiwrite_help(void)
1215797ac58cSKevin Wolf {
1216797ac58cSKevin Wolf     printf(
1217797ac58cSKevin Wolf "\n"
1218797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers,\n"
1219797ac58cSKevin Wolf " in a batch of requests that may be merged by qemu\n"
1220797ac58cSKevin Wolf "\n"
1221797ac58cSKevin Wolf " Example:\n"
1222797ac58cSKevin Wolf " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1223797ac58cSKevin Wolf "  writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1224797ac58cSKevin Wolf "\n"
1225797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1226797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1227797ac58cSKevin Wolf " by one for each request contained in the multiwrite command.\n"
1228797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1229797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1230797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1231797ac58cSKevin Wolf "\n");
1232797ac58cSKevin Wolf }
1233797ac58cSKevin Wolf 
12344c7b7e9bSMax Reitz static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
1235797ac58cSKevin Wolf 
1236797ac58cSKevin Wolf static const cmdinfo_t multiwrite_cmd = {
1237797ac58cSKevin Wolf     .name       = "multiwrite",
1238797ac58cSKevin Wolf     .cfunc      = multiwrite_f,
1239797ac58cSKevin Wolf     .argmin     = 2,
1240797ac58cSKevin Wolf     .argmax     = -1,
1241797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1242797ac58cSKevin Wolf     .oneline    = "issues multiple write requests at once",
1243797ac58cSKevin Wolf     .help       = multiwrite_help,
1244797ac58cSKevin Wolf };
1245797ac58cSKevin Wolf 
12464c7b7e9bSMax Reitz static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
1247797ac58cSKevin Wolf {
1248797ac58cSKevin Wolf     struct timeval t1, t2;
1249dc38852aSEric Blake     bool Cflag = false, qflag = false;
1250797ac58cSKevin Wolf     int c, cnt;
1251797ac58cSKevin Wolf     char **buf;
1252797ac58cSKevin Wolf     int64_t offset, first_offset = 0;
1253797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1254797ac58cSKevin Wolf     int total = 0;
1255797ac58cSKevin Wolf     int nr_iov;
1256797ac58cSKevin Wolf     int nr_reqs;
1257797ac58cSKevin Wolf     int pattern = 0xcd;
1258797ac58cSKevin Wolf     QEMUIOVector *qiovs;
1259797ac58cSKevin Wolf     int i;
1260797ac58cSKevin Wolf     BlockRequest *reqs;
1261797ac58cSKevin Wolf 
1262b062ad86SEric Blake     while ((c = getopt(argc, argv, "CqP:")) != -1) {
1263797ac58cSKevin Wolf         switch (c) {
1264797ac58cSKevin Wolf         case 'C':
1265dc38852aSEric Blake             Cflag = true;
1266797ac58cSKevin Wolf             break;
1267797ac58cSKevin Wolf         case 'q':
1268dc38852aSEric Blake             qflag = true;
1269797ac58cSKevin Wolf             break;
1270797ac58cSKevin Wolf         case 'P':
1271797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1272797ac58cSKevin Wolf             if (pattern < 0) {
1273797ac58cSKevin Wolf                 return 0;
1274797ac58cSKevin Wolf             }
1275797ac58cSKevin Wolf             break;
1276797ac58cSKevin Wolf         default:
1277c2cdf5c5SKevin Wolf             return qemuio_command_usage(&writev_cmd);
1278797ac58cSKevin Wolf         }
1279797ac58cSKevin Wolf     }
1280797ac58cSKevin Wolf 
1281797ac58cSKevin Wolf     if (optind > argc - 2) {
1282c2cdf5c5SKevin Wolf         return qemuio_command_usage(&writev_cmd);
1283797ac58cSKevin Wolf     }
1284797ac58cSKevin Wolf 
1285797ac58cSKevin Wolf     nr_reqs = 1;
1286797ac58cSKevin Wolf     for (i = optind; i < argc; i++) {
1287797ac58cSKevin Wolf         if (!strcmp(argv[i], ";")) {
1288797ac58cSKevin Wolf             nr_reqs++;
1289797ac58cSKevin Wolf         }
1290797ac58cSKevin Wolf     }
1291797ac58cSKevin Wolf 
129202c4f26bSMarkus Armbruster     reqs = g_new0(BlockRequest, nr_reqs);
129302c4f26bSMarkus Armbruster     buf = g_new0(char *, nr_reqs);
129402c4f26bSMarkus Armbruster     qiovs = g_new(QEMUIOVector, nr_reqs);
1295797ac58cSKevin Wolf 
1296797ac58cSKevin Wolf     for (i = 0; i < nr_reqs && optind < argc; i++) {
1297797ac58cSKevin Wolf         int j;
1298797ac58cSKevin Wolf 
1299797ac58cSKevin Wolf         /* Read the offset of the request */
1300797ac58cSKevin Wolf         offset = cvtnum(argv[optind]);
1301797ac58cSKevin Wolf         if (offset < 0) {
1302a9ecfa00SJohn Snow             print_cvtnum_err(offset, argv[optind]);
1303797ac58cSKevin Wolf             goto out;
1304797ac58cSKevin Wolf         }
1305797ac58cSKevin Wolf         optind++;
1306797ac58cSKevin Wolf 
1307797ac58cSKevin Wolf         if (offset & 0x1ff) {
1308797ac58cSKevin Wolf             printf("offset %lld is not sector aligned\n",
1309797ac58cSKevin Wolf                    (long long)offset);
1310797ac58cSKevin Wolf             goto out;
1311797ac58cSKevin Wolf         }
1312797ac58cSKevin Wolf 
1313797ac58cSKevin Wolf         if (i == 0) {
1314797ac58cSKevin Wolf             first_offset = offset;
1315797ac58cSKevin Wolf         }
1316797ac58cSKevin Wolf 
1317797ac58cSKevin Wolf         /* Read lengths for qiov entries */
1318797ac58cSKevin Wolf         for (j = optind; j < argc; j++) {
1319797ac58cSKevin Wolf             if (!strcmp(argv[j], ";")) {
1320797ac58cSKevin Wolf                 break;
1321797ac58cSKevin Wolf             }
1322797ac58cSKevin Wolf         }
1323797ac58cSKevin Wolf 
1324797ac58cSKevin Wolf         nr_iov = j - optind;
1325797ac58cSKevin Wolf 
1326797ac58cSKevin Wolf         /* Build request */
13274c7b7e9bSMax Reitz         buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
1328797ac58cSKevin Wolf         if (buf[i] == NULL) {
1329797ac58cSKevin Wolf             goto out;
1330797ac58cSKevin Wolf         }
1331797ac58cSKevin Wolf 
1332797ac58cSKevin Wolf         reqs[i].qiov = &qiovs[i];
1333797ac58cSKevin Wolf         reqs[i].sector = offset >> 9;
1334797ac58cSKevin Wolf         reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1335797ac58cSKevin Wolf 
1336797ac58cSKevin Wolf         optind = j + 1;
1337797ac58cSKevin Wolf 
1338797ac58cSKevin Wolf         pattern++;
1339797ac58cSKevin Wolf     }
1340797ac58cSKevin Wolf 
1341797ac58cSKevin Wolf     /* If there were empty requests at the end, ignore them */
1342797ac58cSKevin Wolf     nr_reqs = i;
1343797ac58cSKevin Wolf 
1344797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
13454c7b7e9bSMax Reitz     cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
1346797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1347797ac58cSKevin Wolf 
1348797ac58cSKevin Wolf     if (cnt < 0) {
1349797ac58cSKevin Wolf         printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1350797ac58cSKevin Wolf         goto out;
1351797ac58cSKevin Wolf     }
1352797ac58cSKevin Wolf 
1353797ac58cSKevin Wolf     if (qflag) {
1354797ac58cSKevin Wolf         goto out;
1355797ac58cSKevin Wolf     }
1356797ac58cSKevin Wolf 
1357797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1358797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1359797ac58cSKevin Wolf     print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1360797ac58cSKevin Wolf out:
1361797ac58cSKevin Wolf     for (i = 0; i < nr_reqs; i++) {
1362797ac58cSKevin Wolf         qemu_io_free(buf[i]);
1363797ac58cSKevin Wolf         if (reqs[i].qiov != NULL) {
1364797ac58cSKevin Wolf             qemu_iovec_destroy(&qiovs[i]);
1365797ac58cSKevin Wolf         }
1366797ac58cSKevin Wolf     }
1367797ac58cSKevin Wolf     g_free(buf);
1368797ac58cSKevin Wolf     g_free(reqs);
1369797ac58cSKevin Wolf     g_free(qiovs);
1370797ac58cSKevin Wolf     return 0;
1371797ac58cSKevin Wolf }
1372797ac58cSKevin Wolf 
1373797ac58cSKevin Wolf struct aio_ctx {
13744c7b7e9bSMax Reitz     BlockBackend *blk;
1375797ac58cSKevin Wolf     QEMUIOVector qiov;
1376797ac58cSKevin Wolf     int64_t offset;
1377797ac58cSKevin Wolf     char *buf;
1378dc38852aSEric Blake     bool qflag;
1379dc38852aSEric Blake     bool vflag;
1380dc38852aSEric Blake     bool Cflag;
1381dc38852aSEric Blake     bool Pflag;
1382dc38852aSEric Blake     bool zflag;
1383a91f9584SFam Zheng     BlockAcctCookie acct;
1384797ac58cSKevin Wolf     int pattern;
1385797ac58cSKevin Wolf     struct timeval t1;
1386797ac58cSKevin Wolf };
1387797ac58cSKevin Wolf 
1388797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret)
1389797ac58cSKevin Wolf {
1390797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1391797ac58cSKevin Wolf     struct timeval t2;
1392797ac58cSKevin Wolf 
1393797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1394797ac58cSKevin Wolf 
1395797ac58cSKevin Wolf 
1396797ac58cSKevin Wolf     if (ret < 0) {
1397797ac58cSKevin Wolf         printf("aio_write failed: %s\n", strerror(-ret));
1398556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1399797ac58cSKevin Wolf         goto out;
1400797ac58cSKevin Wolf     }
1401797ac58cSKevin Wolf 
14024c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1403a91f9584SFam Zheng 
1404797ac58cSKevin Wolf     if (ctx->qflag) {
1405797ac58cSKevin Wolf         goto out;
1406797ac58cSKevin Wolf     }
1407797ac58cSKevin Wolf 
1408797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1409797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1410797ac58cSKevin Wolf     print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1411797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1412797ac58cSKevin Wolf out:
14135ceb7765SKevin Wolf     if (!ctx->zflag) {
1414797ac58cSKevin Wolf         qemu_io_free(ctx->buf);
1415797ac58cSKevin Wolf         qemu_iovec_destroy(&ctx->qiov);
14165ceb7765SKevin Wolf     }
1417797ac58cSKevin Wolf     g_free(ctx);
1418797ac58cSKevin Wolf }
1419797ac58cSKevin Wolf 
1420797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret)
1421797ac58cSKevin Wolf {
1422797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1423797ac58cSKevin Wolf     struct timeval t2;
1424797ac58cSKevin Wolf 
1425797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1426797ac58cSKevin Wolf 
1427797ac58cSKevin Wolf     if (ret < 0) {
1428797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-ret));
1429556c2b60SAlberto Garcia         block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1430797ac58cSKevin Wolf         goto out;
1431797ac58cSKevin Wolf     }
1432797ac58cSKevin Wolf 
1433797ac58cSKevin Wolf     if (ctx->Pflag) {
1434797ac58cSKevin Wolf         void *cmp_buf = g_malloc(ctx->qiov.size);
1435797ac58cSKevin Wolf 
1436797ac58cSKevin Wolf         memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1437797ac58cSKevin Wolf         if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1438797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
1439797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1440797ac58cSKevin Wolf         }
1441797ac58cSKevin Wolf         g_free(cmp_buf);
1442797ac58cSKevin Wolf     }
1443797ac58cSKevin Wolf 
14444c7b7e9bSMax Reitz     block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1445a91f9584SFam Zheng 
1446797ac58cSKevin Wolf     if (ctx->qflag) {
1447797ac58cSKevin Wolf         goto out;
1448797ac58cSKevin Wolf     }
1449797ac58cSKevin Wolf 
1450797ac58cSKevin Wolf     if (ctx->vflag) {
1451797ac58cSKevin Wolf         dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1452797ac58cSKevin Wolf     }
1453797ac58cSKevin Wolf 
1454797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1455797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1456797ac58cSKevin Wolf     print_report("read", &t2, ctx->offset, ctx->qiov.size,
1457797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1458797ac58cSKevin Wolf out:
1459797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1460797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1461797ac58cSKevin Wolf     g_free(ctx);
1462797ac58cSKevin Wolf }
1463797ac58cSKevin Wolf 
1464797ac58cSKevin Wolf static void aio_read_help(void)
1465797ac58cSKevin Wolf {
1466797ac58cSKevin Wolf     printf(
1467797ac58cSKevin Wolf "\n"
1468797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n"
1469797ac58cSKevin Wolf "\n"
1470797ac58cSKevin Wolf " Example:\n"
1471797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1472797ac58cSKevin Wolf "\n"
1473797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
1474797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
1475797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n"
1476797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1477797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1478797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
1479797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
1480797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1481797ac58cSKevin Wolf "\n");
1482797ac58cSKevin Wolf }
1483797ac58cSKevin Wolf 
14844c7b7e9bSMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1485797ac58cSKevin Wolf 
1486797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = {
1487797ac58cSKevin Wolf     .name       = "aio_read",
1488797ac58cSKevin Wolf     .cfunc      = aio_read_f,
1489797ac58cSKevin Wolf     .argmin     = 2,
1490797ac58cSKevin Wolf     .argmax     = -1,
1491797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern] off len [len..]",
1492797ac58cSKevin Wolf     .oneline    = "asynchronously reads a number of bytes",
1493797ac58cSKevin Wolf     .help       = aio_read_help,
1494797ac58cSKevin Wolf };
1495797ac58cSKevin Wolf 
14964c7b7e9bSMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1497797ac58cSKevin Wolf {
1498797ac58cSKevin Wolf     int nr_iov, c;
1499797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1500797ac58cSKevin Wolf 
15014c7b7e9bSMax Reitz     ctx->blk = blk;
1502b062ad86SEric Blake     while ((c = getopt(argc, argv, "CP:qv")) != -1) {
1503797ac58cSKevin Wolf         switch (c) {
1504797ac58cSKevin Wolf         case 'C':
1505dc38852aSEric Blake             ctx->Cflag = true;
1506797ac58cSKevin Wolf             break;
1507797ac58cSKevin Wolf         case 'P':
1508dc38852aSEric Blake             ctx->Pflag = true;
1509797ac58cSKevin Wolf             ctx->pattern = parse_pattern(optarg);
1510797ac58cSKevin Wolf             if (ctx->pattern < 0) {
1511797ac58cSKevin Wolf                 g_free(ctx);
1512797ac58cSKevin Wolf                 return 0;
1513797ac58cSKevin Wolf             }
1514797ac58cSKevin Wolf             break;
1515797ac58cSKevin Wolf         case 'q':
1516dc38852aSEric Blake             ctx->qflag = true;
1517797ac58cSKevin Wolf             break;
1518797ac58cSKevin Wolf         case 'v':
1519dc38852aSEric Blake             ctx->vflag = true;
1520797ac58cSKevin Wolf             break;
1521797ac58cSKevin Wolf         default:
1522797ac58cSKevin Wolf             g_free(ctx);
1523c2cdf5c5SKevin Wolf             return qemuio_command_usage(&aio_read_cmd);
1524797ac58cSKevin Wolf         }
1525797ac58cSKevin Wolf     }
1526797ac58cSKevin Wolf 
1527797ac58cSKevin Wolf     if (optind > argc - 2) {
1528797ac58cSKevin Wolf         g_free(ctx);
1529c2cdf5c5SKevin Wolf         return qemuio_command_usage(&aio_read_cmd);
1530797ac58cSKevin Wolf     }
1531797ac58cSKevin Wolf 
1532797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1533797ac58cSKevin Wolf     if (ctx->offset < 0) {
1534a9ecfa00SJohn Snow         print_cvtnum_err(ctx->offset, argv[optind]);
1535797ac58cSKevin Wolf         g_free(ctx);
1536797ac58cSKevin Wolf         return 0;
1537797ac58cSKevin Wolf     }
1538797ac58cSKevin Wolf     optind++;
1539797ac58cSKevin Wolf 
1540797ac58cSKevin Wolf     nr_iov = argc - optind;
15414c7b7e9bSMax Reitz     ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1542797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1543556c2b60SAlberto Garcia         block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1544797ac58cSKevin Wolf         g_free(ctx);
1545797ac58cSKevin Wolf         return 0;
1546797ac58cSKevin Wolf     }
1547797ac58cSKevin Wolf 
1548797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
15494c7b7e9bSMax Reitz     block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
15504c7b7e9bSMax Reitz                      BLOCK_ACCT_READ);
15517b3f9712SEric Blake     blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
1552797ac58cSKevin Wolf     return 0;
1553797ac58cSKevin Wolf }
1554797ac58cSKevin Wolf 
1555797ac58cSKevin Wolf static void aio_write_help(void)
1556797ac58cSKevin Wolf {
1557797ac58cSKevin Wolf     printf(
1558797ac58cSKevin Wolf "\n"
1559797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n"
1560797ac58cSKevin Wolf " from multiple buffers\n"
1561797ac58cSKevin Wolf "\n"
1562797ac58cSKevin Wolf " Example:\n"
1563797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1564797ac58cSKevin Wolf "\n"
1565797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1566797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1567797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n"
1568797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1569797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1570797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1571770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n"
1572797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1573*c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n"
15745ceb7765SKevin Wolf " -z, -- write zeroes using blk_aio_write_zeroes\n"
1575797ac58cSKevin Wolf "\n");
1576797ac58cSKevin Wolf }
1577797ac58cSKevin Wolf 
15784c7b7e9bSMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1579797ac58cSKevin Wolf 
1580797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = {
1581797ac58cSKevin Wolf     .name       = "aio_write",
1582797ac58cSKevin Wolf     .cfunc      = aio_write_f,
1583797ac58cSKevin Wolf     .argmin     = 2,
1584797ac58cSKevin Wolf     .argmax     = -1,
1585*c2e001ccSEric Blake     .args       = "[-Cfquz] [-P pattern] off len [len..]",
1586797ac58cSKevin Wolf     .oneline    = "asynchronously writes a number of bytes",
1587797ac58cSKevin Wolf     .help       = aio_write_help,
1588797ac58cSKevin Wolf };
1589797ac58cSKevin Wolf 
15904c7b7e9bSMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1591797ac58cSKevin Wolf {
1592797ac58cSKevin Wolf     int nr_iov, c;
1593797ac58cSKevin Wolf     int pattern = 0xcd;
1594797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1595770e0e0eSEric Blake     int flags = 0;
1596797ac58cSKevin Wolf 
15974c7b7e9bSMax Reitz     ctx->blk = blk;
1598770e0e0eSEric Blake     while ((c = getopt(argc, argv, "CfqP:z")) != -1) {
1599797ac58cSKevin Wolf         switch (c) {
1600797ac58cSKevin Wolf         case 'C':
1601dc38852aSEric Blake             ctx->Cflag = true;
1602797ac58cSKevin Wolf             break;
1603770e0e0eSEric Blake         case 'f':
1604770e0e0eSEric Blake             flags |= BDRV_REQ_FUA;
1605770e0e0eSEric Blake             break;
1606797ac58cSKevin Wolf         case 'q':
1607dc38852aSEric Blake             ctx->qflag = true;
1608797ac58cSKevin Wolf             break;
1609*c2e001ccSEric Blake         case 'u':
1610*c2e001ccSEric Blake             flags |= BDRV_REQ_MAY_UNMAP;
1611*c2e001ccSEric Blake             break;
1612797ac58cSKevin Wolf         case 'P':
1613797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1614797ac58cSKevin Wolf             if (pattern < 0) {
1615797ac58cSKevin Wolf                 g_free(ctx);
1616797ac58cSKevin Wolf                 return 0;
1617797ac58cSKevin Wolf             }
1618797ac58cSKevin Wolf             break;
16195ceb7765SKevin Wolf         case 'z':
1620dc38852aSEric Blake             ctx->zflag = true;
16215ceb7765SKevin Wolf             break;
1622797ac58cSKevin Wolf         default:
1623797ac58cSKevin Wolf             g_free(ctx);
1624c2cdf5c5SKevin Wolf             return qemuio_command_usage(&aio_write_cmd);
1625797ac58cSKevin Wolf         }
1626797ac58cSKevin Wolf     }
1627797ac58cSKevin Wolf 
1628797ac58cSKevin Wolf     if (optind > argc - 2) {
1629797ac58cSKevin Wolf         g_free(ctx);
1630c2cdf5c5SKevin Wolf         return qemuio_command_usage(&aio_write_cmd);
1631797ac58cSKevin Wolf     }
1632797ac58cSKevin Wolf 
16335ceb7765SKevin Wolf     if (ctx->zflag && optind != argc - 2) {
16345ceb7765SKevin Wolf         printf("-z supports only a single length parameter\n");
16355ceb7765SKevin Wolf         g_free(ctx);
16365ceb7765SKevin Wolf         return 0;
16375ceb7765SKevin Wolf     }
16385ceb7765SKevin Wolf 
1639*c2e001ccSEric Blake     if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1640*c2e001ccSEric Blake         printf("-u requires -z to be specified\n");
1641*c2e001ccSEric Blake         return 0;
1642*c2e001ccSEric Blake     }
1643*c2e001ccSEric Blake 
16445ceb7765SKevin Wolf     if (ctx->zflag && ctx->Pflag) {
16455ceb7765SKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
16465ceb7765SKevin Wolf         g_free(ctx);
16475ceb7765SKevin Wolf         return 0;
16485ceb7765SKevin Wolf     }
16495ceb7765SKevin Wolf 
1650797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1651797ac58cSKevin Wolf     if (ctx->offset < 0) {
1652a9ecfa00SJohn Snow         print_cvtnum_err(ctx->offset, argv[optind]);
1653797ac58cSKevin Wolf         g_free(ctx);
1654797ac58cSKevin Wolf         return 0;
1655797ac58cSKevin Wolf     }
1656797ac58cSKevin Wolf     optind++;
1657797ac58cSKevin Wolf 
16585ceb7765SKevin Wolf     if (ctx->zflag) {
16595ceb7765SKevin Wolf         int64_t count = cvtnum(argv[optind]);
16605ceb7765SKevin Wolf         if (count < 0) {
16615ceb7765SKevin Wolf             print_cvtnum_err(count, argv[optind]);
16620e01b76eSKevin Wolf             g_free(ctx);
16635ceb7765SKevin Wolf             return 0;
16645ceb7765SKevin Wolf         }
16655ceb7765SKevin Wolf 
16665ceb7765SKevin Wolf         ctx->qiov.size = count;
1667770e0e0eSEric Blake         blk_aio_write_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1668770e0e0eSEric Blake                              ctx);
16695ceb7765SKevin Wolf     } else {
1670797ac58cSKevin Wolf         nr_iov = argc - optind;
16715ceb7765SKevin Wolf         ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
16725ceb7765SKevin Wolf                                 pattern);
1673797ac58cSKevin Wolf         if (ctx->buf == NULL) {
1674556c2b60SAlberto Garcia             block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1675797ac58cSKevin Wolf             g_free(ctx);
1676797ac58cSKevin Wolf             return 0;
1677797ac58cSKevin Wolf         }
1678797ac58cSKevin Wolf 
1679797ac58cSKevin Wolf         gettimeofday(&ctx->t1, NULL);
16804c7b7e9bSMax Reitz         block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
16814c7b7e9bSMax Reitz                          BLOCK_ACCT_WRITE);
16825ceb7765SKevin Wolf 
1683770e0e0eSEric Blake         blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1684770e0e0eSEric Blake                         ctx);
16855ceb7765SKevin Wolf     }
1686797ac58cSKevin Wolf     return 0;
1687797ac58cSKevin Wolf }
1688797ac58cSKevin Wolf 
16894c7b7e9bSMax Reitz static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1690797ac58cSKevin Wolf {
1691556c2b60SAlberto Garcia     BlockAcctCookie cookie;
1692556c2b60SAlberto Garcia     block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
16934c7b7e9bSMax Reitz     blk_drain_all();
1694556c2b60SAlberto Garcia     block_acct_done(blk_get_stats(blk), &cookie);
1695797ac58cSKevin Wolf     return 0;
1696797ac58cSKevin Wolf }
1697797ac58cSKevin Wolf 
1698797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = {
1699797ac58cSKevin Wolf     .name       = "aio_flush",
1700797ac58cSKevin Wolf     .cfunc      = aio_flush_f,
1701797ac58cSKevin Wolf     .oneline    = "completes all outstanding aio requests"
1702797ac58cSKevin Wolf };
1703797ac58cSKevin Wolf 
17044c7b7e9bSMax Reitz static int flush_f(BlockBackend *blk, int argc, char **argv)
1705797ac58cSKevin Wolf {
17064c7b7e9bSMax Reitz     blk_flush(blk);
1707797ac58cSKevin Wolf     return 0;
1708797ac58cSKevin Wolf }
1709797ac58cSKevin Wolf 
1710797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = {
1711797ac58cSKevin Wolf     .name       = "flush",
1712797ac58cSKevin Wolf     .altname    = "f",
1713797ac58cSKevin Wolf     .cfunc      = flush_f,
1714797ac58cSKevin Wolf     .oneline    = "flush all in-core file state to disk",
1715797ac58cSKevin Wolf };
1716797ac58cSKevin Wolf 
17174c7b7e9bSMax Reitz static int truncate_f(BlockBackend *blk, int argc, char **argv)
1718797ac58cSKevin Wolf {
1719797ac58cSKevin Wolf     int64_t offset;
1720797ac58cSKevin Wolf     int ret;
1721797ac58cSKevin Wolf 
1722797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1723797ac58cSKevin Wolf     if (offset < 0) {
1724a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1725797ac58cSKevin Wolf         return 0;
1726797ac58cSKevin Wolf     }
1727797ac58cSKevin Wolf 
17284c7b7e9bSMax Reitz     ret = blk_truncate(blk, offset);
1729797ac58cSKevin Wolf     if (ret < 0) {
1730797ac58cSKevin Wolf         printf("truncate: %s\n", strerror(-ret));
1731797ac58cSKevin Wolf         return 0;
1732797ac58cSKevin Wolf     }
1733797ac58cSKevin Wolf 
1734797ac58cSKevin Wolf     return 0;
1735797ac58cSKevin Wolf }
1736797ac58cSKevin Wolf 
1737797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = {
1738797ac58cSKevin Wolf     .name       = "truncate",
1739797ac58cSKevin Wolf     .altname    = "t",
1740797ac58cSKevin Wolf     .cfunc      = truncate_f,
1741797ac58cSKevin Wolf     .argmin     = 1,
1742797ac58cSKevin Wolf     .argmax     = 1,
1743797ac58cSKevin Wolf     .args       = "off",
1744797ac58cSKevin Wolf     .oneline    = "truncates the current file at the given offset",
1745797ac58cSKevin Wolf };
1746797ac58cSKevin Wolf 
17474c7b7e9bSMax Reitz static int length_f(BlockBackend *blk, int argc, char **argv)
1748797ac58cSKevin Wolf {
1749797ac58cSKevin Wolf     int64_t size;
1750797ac58cSKevin Wolf     char s1[64];
1751797ac58cSKevin Wolf 
17524c7b7e9bSMax Reitz     size = blk_getlength(blk);
1753797ac58cSKevin Wolf     if (size < 0) {
1754797ac58cSKevin Wolf         printf("getlength: %s\n", strerror(-size));
1755797ac58cSKevin Wolf         return 0;
1756797ac58cSKevin Wolf     }
1757797ac58cSKevin Wolf 
1758797ac58cSKevin Wolf     cvtstr(size, s1, sizeof(s1));
1759797ac58cSKevin Wolf     printf("%s\n", s1);
1760797ac58cSKevin Wolf     return 0;
1761797ac58cSKevin Wolf }
1762797ac58cSKevin Wolf 
1763797ac58cSKevin Wolf 
1764797ac58cSKevin Wolf static const cmdinfo_t length_cmd = {
1765797ac58cSKevin Wolf     .name   = "length",
1766797ac58cSKevin Wolf     .altname    = "l",
1767797ac58cSKevin Wolf     .cfunc      = length_f,
1768797ac58cSKevin Wolf     .oneline    = "gets the length of the current file",
1769797ac58cSKevin Wolf };
1770797ac58cSKevin Wolf 
1771797ac58cSKevin Wolf 
17724c7b7e9bSMax Reitz static int info_f(BlockBackend *blk, int argc, char **argv)
1773797ac58cSKevin Wolf {
17744c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
1775797ac58cSKevin Wolf     BlockDriverInfo bdi;
1776a8d8ecb7SMax Reitz     ImageInfoSpecific *spec_info;
1777797ac58cSKevin Wolf     char s1[64], s2[64];
1778797ac58cSKevin Wolf     int ret;
1779797ac58cSKevin Wolf 
1780797ac58cSKevin Wolf     if (bs->drv && bs->drv->format_name) {
1781797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->format_name);
1782797ac58cSKevin Wolf     }
1783797ac58cSKevin Wolf     if (bs->drv && bs->drv->protocol_name) {
1784797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->protocol_name);
1785797ac58cSKevin Wolf     }
1786797ac58cSKevin Wolf 
1787797ac58cSKevin Wolf     ret = bdrv_get_info(bs, &bdi);
1788797ac58cSKevin Wolf     if (ret) {
1789797ac58cSKevin Wolf         return 0;
1790797ac58cSKevin Wolf     }
1791797ac58cSKevin Wolf 
1792797ac58cSKevin Wolf     cvtstr(bdi.cluster_size, s1, sizeof(s1));
1793797ac58cSKevin Wolf     cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1794797ac58cSKevin Wolf 
1795797ac58cSKevin Wolf     printf("cluster size: %s\n", s1);
1796797ac58cSKevin Wolf     printf("vm state offset: %s\n", s2);
1797797ac58cSKevin Wolf 
1798a8d8ecb7SMax Reitz     spec_info = bdrv_get_specific_info(bs);
1799a8d8ecb7SMax Reitz     if (spec_info) {
1800a8d8ecb7SMax Reitz         printf("Format specific information:\n");
1801a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1802a8d8ecb7SMax Reitz         qapi_free_ImageInfoSpecific(spec_info);
1803a8d8ecb7SMax Reitz     }
1804a8d8ecb7SMax Reitz 
1805797ac58cSKevin Wolf     return 0;
1806797ac58cSKevin Wolf }
1807797ac58cSKevin Wolf 
1808797ac58cSKevin Wolf 
1809797ac58cSKevin Wolf 
1810797ac58cSKevin Wolf static const cmdinfo_t info_cmd = {
1811797ac58cSKevin Wolf     .name       = "info",
1812797ac58cSKevin Wolf     .altname    = "i",
1813797ac58cSKevin Wolf     .cfunc      = info_f,
1814797ac58cSKevin Wolf     .oneline    = "prints information about the current file",
1815797ac58cSKevin Wolf };
1816797ac58cSKevin Wolf 
1817797ac58cSKevin Wolf static void discard_help(void)
1818797ac58cSKevin Wolf {
1819797ac58cSKevin Wolf     printf(
1820797ac58cSKevin Wolf "\n"
1821797ac58cSKevin Wolf " discards a range of bytes from the given offset\n"
1822797ac58cSKevin Wolf "\n"
1823797ac58cSKevin Wolf " Example:\n"
1824797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1825797ac58cSKevin Wolf "\n"
1826797ac58cSKevin Wolf " Discards a segment of the currently open file.\n"
1827797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1828797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1829797ac58cSKevin Wolf "\n");
1830797ac58cSKevin Wolf }
1831797ac58cSKevin Wolf 
18324c7b7e9bSMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv);
1833797ac58cSKevin Wolf 
1834797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = {
1835797ac58cSKevin Wolf     .name       = "discard",
1836797ac58cSKevin Wolf     .altname    = "d",
1837797ac58cSKevin Wolf     .cfunc      = discard_f,
1838797ac58cSKevin Wolf     .argmin     = 2,
1839797ac58cSKevin Wolf     .argmax     = -1,
1840797ac58cSKevin Wolf     .args       = "[-Cq] off len",
1841797ac58cSKevin Wolf     .oneline    = "discards a number of bytes at a specified offset",
1842797ac58cSKevin Wolf     .help       = discard_help,
1843797ac58cSKevin Wolf };
1844797ac58cSKevin Wolf 
18454c7b7e9bSMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv)
1846797ac58cSKevin Wolf {
1847797ac58cSKevin Wolf     struct timeval t1, t2;
1848dc38852aSEric Blake     bool Cflag = false, qflag = false;
1849797ac58cSKevin Wolf     int c, ret;
18509b0beaf3SJohn Snow     int64_t offset, count;
1851797ac58cSKevin Wolf 
1852b062ad86SEric Blake     while ((c = getopt(argc, argv, "Cq")) != -1) {
1853797ac58cSKevin Wolf         switch (c) {
1854797ac58cSKevin Wolf         case 'C':
1855dc38852aSEric Blake             Cflag = true;
1856797ac58cSKevin Wolf             break;
1857797ac58cSKevin Wolf         case 'q':
1858dc38852aSEric Blake             qflag = true;
1859797ac58cSKevin Wolf             break;
1860797ac58cSKevin Wolf         default:
1861c2cdf5c5SKevin Wolf             return qemuio_command_usage(&discard_cmd);
1862797ac58cSKevin Wolf         }
1863797ac58cSKevin Wolf     }
1864797ac58cSKevin Wolf 
1865797ac58cSKevin Wolf     if (optind != argc - 2) {
1866c2cdf5c5SKevin Wolf         return qemuio_command_usage(&discard_cmd);
1867797ac58cSKevin Wolf     }
1868797ac58cSKevin Wolf 
1869797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1870797ac58cSKevin Wolf     if (offset < 0) {
1871a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[optind]);
1872797ac58cSKevin Wolf         return 0;
1873797ac58cSKevin Wolf     }
1874797ac58cSKevin Wolf 
1875797ac58cSKevin Wolf     optind++;
1876797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1877797ac58cSKevin Wolf     if (count < 0) {
1878a9ecfa00SJohn Snow         print_cvtnum_err(count, argv[optind]);
1879797ac58cSKevin Wolf         return 0;
18809b0beaf3SJohn Snow     } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
18819b0beaf3SJohn Snow         printf("length cannot exceed %"PRIu64", given %s\n",
18829b0beaf3SJohn Snow                (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
18839b0beaf3SJohn Snow                argv[optind]);
18849b0beaf3SJohn Snow         return 0;
1885797ac58cSKevin Wolf     }
1886797ac58cSKevin Wolf 
1887797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
18884c7b7e9bSMax Reitz     ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1889797ac58cSKevin Wolf                       count >> BDRV_SECTOR_BITS);
1890797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1891797ac58cSKevin Wolf 
1892797ac58cSKevin Wolf     if (ret < 0) {
1893797ac58cSKevin Wolf         printf("discard failed: %s\n", strerror(-ret));
1894797ac58cSKevin Wolf         goto out;
1895797ac58cSKevin Wolf     }
1896797ac58cSKevin Wolf 
1897797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1898797ac58cSKevin Wolf     if (!qflag) {
1899797ac58cSKevin Wolf         t2 = tsub(t2, t1);
1900797ac58cSKevin Wolf         print_report("discard", &t2, offset, count, count, 1, Cflag);
1901797ac58cSKevin Wolf     }
1902797ac58cSKevin Wolf 
1903797ac58cSKevin Wolf out:
1904797ac58cSKevin Wolf     return 0;
1905797ac58cSKevin Wolf }
1906797ac58cSKevin Wolf 
19074c7b7e9bSMax Reitz static int alloc_f(BlockBackend *blk, int argc, char **argv)
1908797ac58cSKevin Wolf {
19094c7b7e9bSMax Reitz     BlockDriverState *bs = blk_bs(blk);
19109b0beaf3SJohn Snow     int64_t offset, sector_num, nb_sectors, remaining;
1911797ac58cSKevin Wolf     char s1[64];
19129b0beaf3SJohn Snow     int num, ret;
19139b0beaf3SJohn Snow     int64_t sum_alloc;
1914797ac58cSKevin Wolf 
1915797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1916797ac58cSKevin Wolf     if (offset < 0) {
1917a9ecfa00SJohn Snow         print_cvtnum_err(offset, argv[1]);
1918797ac58cSKevin Wolf         return 0;
1919797ac58cSKevin Wolf     } else if (offset & 0x1ff) {
1920797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1921797ac58cSKevin Wolf                offset);
1922797ac58cSKevin Wolf         return 0;
1923797ac58cSKevin Wolf     }
1924797ac58cSKevin Wolf 
1925797ac58cSKevin Wolf     if (argc == 3) {
1926797ac58cSKevin Wolf         nb_sectors = cvtnum(argv[2]);
1927797ac58cSKevin Wolf         if (nb_sectors < 0) {
1928a9ecfa00SJohn Snow             print_cvtnum_err(nb_sectors, argv[2]);
1929797ac58cSKevin Wolf             return 0;
19309b0beaf3SJohn Snow         } else if (nb_sectors > INT_MAX) {
19319b0beaf3SJohn Snow             printf("length argument cannot exceed %d, given %s\n",
19329b0beaf3SJohn Snow                    INT_MAX, argv[2]);
19339b0beaf3SJohn Snow             return 0;
1934797ac58cSKevin Wolf         }
1935797ac58cSKevin Wolf     } else {
1936797ac58cSKevin Wolf         nb_sectors = 1;
1937797ac58cSKevin Wolf     }
1938797ac58cSKevin Wolf 
1939797ac58cSKevin Wolf     remaining = nb_sectors;
1940797ac58cSKevin Wolf     sum_alloc = 0;
1941797ac58cSKevin Wolf     sector_num = offset >> 9;
1942797ac58cSKevin Wolf     while (remaining) {
1943797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1944d663640cSPaolo Bonzini         if (ret < 0) {
1945d663640cSPaolo Bonzini             printf("is_allocated failed: %s\n", strerror(-ret));
1946d663640cSPaolo Bonzini             return 0;
1947d663640cSPaolo Bonzini         }
1948797ac58cSKevin Wolf         sector_num += num;
1949797ac58cSKevin Wolf         remaining -= num;
1950797ac58cSKevin Wolf         if (ret) {
1951797ac58cSKevin Wolf             sum_alloc += num;
1952797ac58cSKevin Wolf         }
1953797ac58cSKevin Wolf         if (num == 0) {
1954797ac58cSKevin Wolf             nb_sectors -= remaining;
1955797ac58cSKevin Wolf             remaining = 0;
1956797ac58cSKevin Wolf         }
1957797ac58cSKevin Wolf     }
1958797ac58cSKevin Wolf 
1959797ac58cSKevin Wolf     cvtstr(offset, s1, sizeof(s1));
1960797ac58cSKevin Wolf 
19619b0beaf3SJohn Snow     printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
1962797ac58cSKevin Wolf            sum_alloc, nb_sectors, s1);
1963797ac58cSKevin Wolf     return 0;
1964797ac58cSKevin Wolf }
1965797ac58cSKevin Wolf 
1966797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = {
1967797ac58cSKevin Wolf     .name       = "alloc",
1968797ac58cSKevin Wolf     .altname    = "a",
1969797ac58cSKevin Wolf     .argmin     = 1,
1970797ac58cSKevin Wolf     .argmax     = 2,
1971797ac58cSKevin Wolf     .cfunc      = alloc_f,
1972797ac58cSKevin Wolf     .args       = "off [sectors]",
1973797ac58cSKevin Wolf     .oneline    = "checks if a sector is present in the file",
1974797ac58cSKevin Wolf };
1975797ac58cSKevin Wolf 
1976797ac58cSKevin Wolf 
1977797ac58cSKevin Wolf static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1978797ac58cSKevin Wolf                             int64_t nb_sectors, int64_t *pnum)
1979797ac58cSKevin Wolf {
1980797ac58cSKevin Wolf     int num, num_checked;
1981797ac58cSKevin Wolf     int ret, firstret;
1982797ac58cSKevin Wolf 
1983797ac58cSKevin Wolf     num_checked = MIN(nb_sectors, INT_MAX);
1984797ac58cSKevin Wolf     ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1985797ac58cSKevin Wolf     if (ret < 0) {
1986797ac58cSKevin Wolf         return ret;
1987797ac58cSKevin Wolf     }
1988797ac58cSKevin Wolf 
1989797ac58cSKevin Wolf     firstret = ret;
1990797ac58cSKevin Wolf     *pnum = num;
1991797ac58cSKevin Wolf 
1992797ac58cSKevin Wolf     while (nb_sectors > 0 && ret == firstret) {
1993797ac58cSKevin Wolf         sector_num += num;
1994797ac58cSKevin Wolf         nb_sectors -= num;
1995797ac58cSKevin Wolf 
1996797ac58cSKevin Wolf         num_checked = MIN(nb_sectors, INT_MAX);
1997797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
19984b25bbc4SMax Reitz         if (ret == firstret && num) {
1999797ac58cSKevin Wolf             *pnum += num;
2000797ac58cSKevin Wolf         } else {
2001797ac58cSKevin Wolf             break;
2002797ac58cSKevin Wolf         }
2003797ac58cSKevin Wolf     }
2004797ac58cSKevin Wolf 
2005797ac58cSKevin Wolf     return firstret;
2006797ac58cSKevin Wolf }
2007797ac58cSKevin Wolf 
20084c7b7e9bSMax Reitz static int map_f(BlockBackend *blk, int argc, char **argv)
2009797ac58cSKevin Wolf {
2010797ac58cSKevin Wolf     int64_t offset;
20114c7b7e9bSMax Reitz     int64_t nb_sectors, total_sectors;
2012797ac58cSKevin Wolf     char s1[64];
2013797ac58cSKevin Wolf     int64_t num;
2014797ac58cSKevin Wolf     int ret;
2015797ac58cSKevin Wolf     const char *retstr;
2016797ac58cSKevin Wolf 
2017797ac58cSKevin Wolf     offset = 0;
20184c7b7e9bSMax Reitz     total_sectors = blk_nb_sectors(blk);
20194c7b7e9bSMax Reitz     if (total_sectors < 0) {
20204c7b7e9bSMax Reitz         error_report("Failed to query image length: %s",
20214c7b7e9bSMax Reitz                      strerror(-total_sectors));
20224c7b7e9bSMax Reitz         return 0;
20234c7b7e9bSMax Reitz     }
20244c7b7e9bSMax Reitz 
20254c7b7e9bSMax Reitz     nb_sectors = total_sectors;
2026797ac58cSKevin Wolf 
2027797ac58cSKevin Wolf     do {
20284c7b7e9bSMax Reitz         ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
2029797ac58cSKevin Wolf         if (ret < 0) {
2030797ac58cSKevin Wolf             error_report("Failed to get allocation status: %s", strerror(-ret));
2031797ac58cSKevin Wolf             return 0;
20324b25bbc4SMax Reitz         } else if (!num) {
20334b25bbc4SMax Reitz             error_report("Unexpected end of image");
20344b25bbc4SMax Reitz             return 0;
2035797ac58cSKevin Wolf         }
2036797ac58cSKevin Wolf 
2037797ac58cSKevin Wolf         retstr = ret ? "    allocated" : "not allocated";
2038797ac58cSKevin Wolf         cvtstr(offset << 9ULL, s1, sizeof(s1));
2039797ac58cSKevin Wolf         printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
2040797ac58cSKevin Wolf                "at offset %s (%d)\n",
2041797ac58cSKevin Wolf                offset << 9ULL, num, nb_sectors, retstr, s1, ret);
2042797ac58cSKevin Wolf 
2043797ac58cSKevin Wolf         offset += num;
2044797ac58cSKevin Wolf         nb_sectors -= num;
20454c7b7e9bSMax Reitz     } while (offset < total_sectors);
2046797ac58cSKevin Wolf 
2047797ac58cSKevin Wolf     return 0;
2048797ac58cSKevin Wolf }
2049797ac58cSKevin Wolf 
2050797ac58cSKevin Wolf static const cmdinfo_t map_cmd = {
2051797ac58cSKevin Wolf        .name           = "map",
2052797ac58cSKevin Wolf        .argmin         = 0,
2053797ac58cSKevin Wolf        .argmax         = 0,
2054797ac58cSKevin Wolf        .cfunc          = map_f,
2055797ac58cSKevin Wolf        .args           = "",
2056797ac58cSKevin Wolf        .oneline        = "prints the allocated areas of a file",
2057797ac58cSKevin Wolf };
2058797ac58cSKevin Wolf 
20595bbd2e59SKevin Wolf static void reopen_help(void)
20605bbd2e59SKevin Wolf {
20615bbd2e59SKevin Wolf     printf(
20625bbd2e59SKevin Wolf "\n"
20635bbd2e59SKevin Wolf " Changes the open options of an already opened image\n"
20645bbd2e59SKevin Wolf "\n"
20655bbd2e59SKevin Wolf " Example:\n"
20665bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
20675bbd2e59SKevin Wolf "\n"
20685bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n"
20695bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n"
20705bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n"
20715bbd2e59SKevin Wolf "\n");
20725bbd2e59SKevin Wolf }
20735bbd2e59SKevin Wolf 
20745bbd2e59SKevin Wolf static int reopen_f(BlockBackend *blk, int argc, char **argv);
20755bbd2e59SKevin Wolf 
20765bbd2e59SKevin Wolf static QemuOptsList reopen_opts = {
20775bbd2e59SKevin Wolf     .name = "reopen",
20785bbd2e59SKevin Wolf     .merge_lists = true,
20795bbd2e59SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
20805bbd2e59SKevin Wolf     .desc = {
20815bbd2e59SKevin Wolf         /* no elements => accept any params */
20825bbd2e59SKevin Wolf         { /* end of list */ }
20835bbd2e59SKevin Wolf     },
20845bbd2e59SKevin Wolf };
20855bbd2e59SKevin Wolf 
20865bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = {
20875bbd2e59SKevin Wolf        .name           = "reopen",
20885bbd2e59SKevin Wolf        .argmin         = 0,
20895bbd2e59SKevin Wolf        .argmax         = -1,
20905bbd2e59SKevin Wolf        .cfunc          = reopen_f,
20915bbd2e59SKevin Wolf        .args           = "[-r] [-c cache] [-o options]",
20925bbd2e59SKevin Wolf        .oneline        = "reopens an image with new options",
20935bbd2e59SKevin Wolf        .help           = reopen_help,
20945bbd2e59SKevin Wolf };
20955bbd2e59SKevin Wolf 
20965bbd2e59SKevin Wolf static int reopen_f(BlockBackend *blk, int argc, char **argv)
20975bbd2e59SKevin Wolf {
20985bbd2e59SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
20995bbd2e59SKevin Wolf     QemuOpts *qopts;
21005bbd2e59SKevin Wolf     QDict *opts;
21015bbd2e59SKevin Wolf     int c;
21025bbd2e59SKevin Wolf     int flags = bs->open_flags;
210319dbecdcSKevin Wolf     bool writethrough = !blk_enable_write_cache(blk);
21045bbd2e59SKevin Wolf 
21055bbd2e59SKevin Wolf     BlockReopenQueue *brq;
21065bbd2e59SKevin Wolf     Error *local_err = NULL;
21075bbd2e59SKevin Wolf 
21085bbd2e59SKevin Wolf     while ((c = getopt(argc, argv, "c:o:r")) != -1) {
21095bbd2e59SKevin Wolf         switch (c) {
21105bbd2e59SKevin Wolf         case 'c':
211119dbecdcSKevin Wolf             if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
21125bbd2e59SKevin Wolf                 error_report("Invalid cache option: %s", optarg);
21135bbd2e59SKevin Wolf                 return 0;
21145bbd2e59SKevin Wolf             }
21155bbd2e59SKevin Wolf             break;
21165bbd2e59SKevin Wolf         case 'o':
21175bbd2e59SKevin Wolf             if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
21185bbd2e59SKevin Wolf                 qemu_opts_reset(&reopen_opts);
21195bbd2e59SKevin Wolf                 return 0;
21205bbd2e59SKevin Wolf             }
21215bbd2e59SKevin Wolf             break;
21225bbd2e59SKevin Wolf         case 'r':
21235bbd2e59SKevin Wolf             flags &= ~BDRV_O_RDWR;
21245bbd2e59SKevin Wolf             break;
21255bbd2e59SKevin Wolf         default:
21265bbd2e59SKevin Wolf             qemu_opts_reset(&reopen_opts);
21275bbd2e59SKevin Wolf             return qemuio_command_usage(&reopen_cmd);
21285bbd2e59SKevin Wolf         }
21295bbd2e59SKevin Wolf     }
21305bbd2e59SKevin Wolf 
21315bbd2e59SKevin Wolf     if (optind != argc) {
21325bbd2e59SKevin Wolf         qemu_opts_reset(&reopen_opts);
21335bbd2e59SKevin Wolf         return qemuio_command_usage(&reopen_cmd);
21345bbd2e59SKevin Wolf     }
21355bbd2e59SKevin Wolf 
213619dbecdcSKevin Wolf     if (writethrough != blk_enable_write_cache(blk) &&
213719dbecdcSKevin Wolf         blk_get_attached_dev(blk))
213819dbecdcSKevin Wolf     {
213919dbecdcSKevin Wolf         error_report("Cannot change cache.writeback: Device attached");
214019dbecdcSKevin Wolf         qemu_opts_reset(&reopen_opts);
214119dbecdcSKevin Wolf         return 0;
214219dbecdcSKevin Wolf     }
214319dbecdcSKevin Wolf 
21445bbd2e59SKevin Wolf     qopts = qemu_opts_find(&reopen_opts, NULL);
21455bbd2e59SKevin Wolf     opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
21465bbd2e59SKevin Wolf     qemu_opts_reset(&reopen_opts);
21475bbd2e59SKevin Wolf 
21485bbd2e59SKevin Wolf     brq = bdrv_reopen_queue(NULL, bs, opts, flags);
21495bbd2e59SKevin Wolf     bdrv_reopen_multiple(brq, &local_err);
21505bbd2e59SKevin Wolf     if (local_err) {
21515bbd2e59SKevin Wolf         error_report_err(local_err);
215219dbecdcSKevin Wolf     } else {
215319dbecdcSKevin Wolf         blk_set_enable_write_cache(blk, !writethrough);
21545bbd2e59SKevin Wolf     }
21555bbd2e59SKevin Wolf 
21565bbd2e59SKevin Wolf     return 0;
21575bbd2e59SKevin Wolf }
21585bbd2e59SKevin Wolf 
21594c7b7e9bSMax Reitz static int break_f(BlockBackend *blk, int argc, char **argv)
2160797ac58cSKevin Wolf {
2161797ac58cSKevin Wolf     int ret;
2162797ac58cSKevin Wolf 
21634c7b7e9bSMax Reitz     ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2164797ac58cSKevin Wolf     if (ret < 0) {
2165797ac58cSKevin Wolf         printf("Could not set breakpoint: %s\n", strerror(-ret));
2166797ac58cSKevin Wolf     }
2167797ac58cSKevin Wolf 
2168797ac58cSKevin Wolf     return 0;
2169797ac58cSKevin Wolf }
2170797ac58cSKevin Wolf 
21714c7b7e9bSMax Reitz static int remove_break_f(BlockBackend *blk, int argc, char **argv)
21724cc70e93SFam Zheng {
21734cc70e93SFam Zheng     int ret;
21744cc70e93SFam Zheng 
21754c7b7e9bSMax Reitz     ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
21764cc70e93SFam Zheng     if (ret < 0) {
21774cc70e93SFam Zheng         printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
21784cc70e93SFam Zheng     }
21794cc70e93SFam Zheng 
21804cc70e93SFam Zheng     return 0;
21814cc70e93SFam Zheng }
21824cc70e93SFam Zheng 
2183797ac58cSKevin Wolf static const cmdinfo_t break_cmd = {
2184797ac58cSKevin Wolf        .name           = "break",
2185797ac58cSKevin Wolf        .argmin         = 2,
2186797ac58cSKevin Wolf        .argmax         = 2,
2187797ac58cSKevin Wolf        .cfunc          = break_f,
2188797ac58cSKevin Wolf        .args           = "event tag",
2189797ac58cSKevin Wolf        .oneline        = "sets a breakpoint on event and tags the stopped "
2190797ac58cSKevin Wolf                          "request as tag",
2191797ac58cSKevin Wolf };
2192797ac58cSKevin Wolf 
21934cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = {
21944cc70e93SFam Zheng        .name           = "remove_break",
21954cc70e93SFam Zheng        .argmin         = 1,
21964cc70e93SFam Zheng        .argmax         = 1,
21974cc70e93SFam Zheng        .cfunc          = remove_break_f,
21984cc70e93SFam Zheng        .args           = "tag",
21994cc70e93SFam Zheng        .oneline        = "remove a breakpoint by tag",
22004cc70e93SFam Zheng };
22014cc70e93SFam Zheng 
22024c7b7e9bSMax Reitz static int resume_f(BlockBackend *blk, int argc, char **argv)
2203797ac58cSKevin Wolf {
2204797ac58cSKevin Wolf     int ret;
2205797ac58cSKevin Wolf 
22064c7b7e9bSMax Reitz     ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2207797ac58cSKevin Wolf     if (ret < 0) {
2208797ac58cSKevin Wolf         printf("Could not resume request: %s\n", strerror(-ret));
2209797ac58cSKevin Wolf     }
2210797ac58cSKevin Wolf 
2211797ac58cSKevin Wolf     return 0;
2212797ac58cSKevin Wolf }
2213797ac58cSKevin Wolf 
2214797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = {
2215797ac58cSKevin Wolf        .name           = "resume",
2216797ac58cSKevin Wolf        .argmin         = 1,
2217797ac58cSKevin Wolf        .argmax         = 1,
2218797ac58cSKevin Wolf        .cfunc          = resume_f,
2219797ac58cSKevin Wolf        .args           = "tag",
2220797ac58cSKevin Wolf        .oneline        = "resumes the request tagged as tag",
2221797ac58cSKevin Wolf };
2222797ac58cSKevin Wolf 
22234c7b7e9bSMax Reitz static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2224797ac58cSKevin Wolf {
22254c7b7e9bSMax Reitz     while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
22264c7b7e9bSMax Reitz         aio_poll(blk_get_aio_context(blk), true);
2227797ac58cSKevin Wolf     }
2228797ac58cSKevin Wolf 
2229797ac58cSKevin Wolf     return 0;
2230797ac58cSKevin Wolf }
2231797ac58cSKevin Wolf 
2232797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = {
2233797ac58cSKevin Wolf        .name           = "wait_break",
2234797ac58cSKevin Wolf        .argmin         = 1,
2235797ac58cSKevin Wolf        .argmax         = 1,
2236797ac58cSKevin Wolf        .cfunc          = wait_break_f,
2237797ac58cSKevin Wolf        .args           = "tag",
2238797ac58cSKevin Wolf        .oneline        = "waits for the suspension of a request",
2239797ac58cSKevin Wolf };
2240797ac58cSKevin Wolf 
22414c7b7e9bSMax Reitz static int abort_f(BlockBackend *blk, int argc, char **argv)
2242797ac58cSKevin Wolf {
2243797ac58cSKevin Wolf     abort();
2244797ac58cSKevin Wolf }
2245797ac58cSKevin Wolf 
2246797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = {
2247797ac58cSKevin Wolf        .name           = "abort",
2248797ac58cSKevin Wolf        .cfunc          = abort_f,
2249797ac58cSKevin Wolf        .flags          = CMD_NOFILE_OK,
2250797ac58cSKevin Wolf        .oneline        = "simulate a program crash using abort(3)",
2251797ac58cSKevin Wolf };
2252797ac58cSKevin Wolf 
22530e82dc7bSMax Reitz static void sigraise_help(void)
22540e82dc7bSMax Reitz {
22550e82dc7bSMax Reitz     printf(
22560e82dc7bSMax Reitz "\n"
22570e82dc7bSMax Reitz " raises the given signal\n"
22580e82dc7bSMax Reitz "\n"
22590e82dc7bSMax Reitz " Example:\n"
22600e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n"
22610e82dc7bSMax Reitz "\n"
22620e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
22630e82dc7bSMax Reitz " given to sigraise.\n"
22640e82dc7bSMax Reitz "\n", SIGTERM);
22650e82dc7bSMax Reitz }
22660e82dc7bSMax Reitz 
22674c7b7e9bSMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv);
22680e82dc7bSMax Reitz 
22690e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = {
22700e82dc7bSMax Reitz     .name       = "sigraise",
22710e82dc7bSMax Reitz     .cfunc      = sigraise_f,
22720e82dc7bSMax Reitz     .argmin     = 1,
22730e82dc7bSMax Reitz     .argmax     = 1,
22740e82dc7bSMax Reitz     .flags      = CMD_NOFILE_OK,
22750e82dc7bSMax Reitz     .args       = "signal",
22760e82dc7bSMax Reitz     .oneline    = "raises a signal",
22770e82dc7bSMax Reitz     .help       = sigraise_help,
22780e82dc7bSMax Reitz };
22790e82dc7bSMax Reitz 
22804c7b7e9bSMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv)
22810e82dc7bSMax Reitz {
22829b0beaf3SJohn Snow     int64_t sig = cvtnum(argv[1]);
22830e82dc7bSMax Reitz     if (sig < 0) {
2284a9ecfa00SJohn Snow         print_cvtnum_err(sig, argv[1]);
22850e82dc7bSMax Reitz         return 0;
22869b0beaf3SJohn Snow     } else if (sig > NSIG) {
22879b0beaf3SJohn Snow         printf("signal argument '%s' is too large to be a valid signal\n",
22889b0beaf3SJohn Snow                argv[1]);
22899b0beaf3SJohn Snow         return 0;
22900e82dc7bSMax Reitz     }
22910e82dc7bSMax Reitz 
22920e82dc7bSMax Reitz     /* Using raise() to kill this process does not necessarily flush all open
22930e82dc7bSMax Reitz      * streams. At least stdout and stderr (although the latter should be
22940e82dc7bSMax Reitz      * non-buffered anyway) should be flushed, though. */
22950e82dc7bSMax Reitz     fflush(stdout);
22960e82dc7bSMax Reitz     fflush(stderr);
22970e82dc7bSMax Reitz 
22980e82dc7bSMax Reitz     raise(sig);
22990e82dc7bSMax Reitz     return 0;
23000e82dc7bSMax Reitz }
23010e82dc7bSMax Reitz 
2302cd33d02aSKevin Wolf static void sleep_cb(void *opaque)
2303cd33d02aSKevin Wolf {
2304cd33d02aSKevin Wolf     bool *expired = opaque;
2305cd33d02aSKevin Wolf     *expired = true;
2306cd33d02aSKevin Wolf }
2307cd33d02aSKevin Wolf 
23084c7b7e9bSMax Reitz static int sleep_f(BlockBackend *blk, int argc, char **argv)
2309cd33d02aSKevin Wolf {
2310cd33d02aSKevin Wolf     char *endptr;
2311cd33d02aSKevin Wolf     long ms;
2312cd33d02aSKevin Wolf     struct QEMUTimer *timer;
2313cd33d02aSKevin Wolf     bool expired = false;
2314cd33d02aSKevin Wolf 
2315cd33d02aSKevin Wolf     ms = strtol(argv[1], &endptr, 0);
2316cd33d02aSKevin Wolf     if (ms < 0 || *endptr != '\0') {
2317cd33d02aSKevin Wolf         printf("%s is not a valid number\n", argv[1]);
2318cd33d02aSKevin Wolf         return 0;
2319cd33d02aSKevin Wolf     }
2320cd33d02aSKevin Wolf 
2321cd33d02aSKevin Wolf     timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2322cd33d02aSKevin Wolf     timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2323cd33d02aSKevin Wolf 
2324cd33d02aSKevin Wolf     while (!expired) {
2325cd33d02aSKevin Wolf         main_loop_wait(false);
2326cd33d02aSKevin Wolf     }
2327cd33d02aSKevin Wolf 
2328cd33d02aSKevin Wolf     timer_free(timer);
2329cd33d02aSKevin Wolf 
2330cd33d02aSKevin Wolf     return 0;
2331cd33d02aSKevin Wolf }
2332cd33d02aSKevin Wolf 
2333cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = {
2334cd33d02aSKevin Wolf        .name           = "sleep",
2335cd33d02aSKevin Wolf        .argmin         = 1,
2336cd33d02aSKevin Wolf        .argmax         = 1,
2337cd33d02aSKevin Wolf        .cfunc          = sleep_f,
2338cd33d02aSKevin Wolf        .flags          = CMD_NOFILE_OK,
2339cd33d02aSKevin Wolf        .oneline        = "waits for the given value in milliseconds",
2340cd33d02aSKevin Wolf };
2341cd33d02aSKevin Wolf 
2342f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2343f18a834aSKevin Wolf {
2344f18a834aSKevin Wolf     if (cmd) {
2345f18a834aSKevin Wolf         printf("%s ", cmd);
2346f18a834aSKevin Wolf     } else {
2347f18a834aSKevin Wolf         printf("%s ", ct->name);
2348f18a834aSKevin Wolf         if (ct->altname) {
2349f18a834aSKevin Wolf             printf("(or %s) ", ct->altname);
2350f18a834aSKevin Wolf         }
2351f18a834aSKevin Wolf     }
2352f18a834aSKevin Wolf 
2353f18a834aSKevin Wolf     if (ct->args) {
2354f18a834aSKevin Wolf         printf("%s ", ct->args);
2355f18a834aSKevin Wolf     }
2356f18a834aSKevin Wolf     printf("-- %s\n", ct->oneline);
2357f18a834aSKevin Wolf }
2358f18a834aSKevin Wolf 
2359f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2360f18a834aSKevin Wolf {
2361f18a834aSKevin Wolf     help_oneline(cmd, ct);
2362f18a834aSKevin Wolf     if (ct->help) {
2363f18a834aSKevin Wolf         ct->help();
2364f18a834aSKevin Wolf     }
2365f18a834aSKevin Wolf }
2366f18a834aSKevin Wolf 
2367f18a834aSKevin Wolf static void help_all(void)
2368f18a834aSKevin Wolf {
2369f18a834aSKevin Wolf     const cmdinfo_t *ct;
2370f18a834aSKevin Wolf 
2371f18a834aSKevin Wolf     for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2372f18a834aSKevin Wolf         help_oneline(ct->name, ct);
2373f18a834aSKevin Wolf     }
2374f18a834aSKevin Wolf     printf("\nUse 'help commandname' for extended help.\n");
2375f18a834aSKevin Wolf }
2376f18a834aSKevin Wolf 
23774c7b7e9bSMax Reitz static int help_f(BlockBackend *blk, int argc, char **argv)
2378f18a834aSKevin Wolf {
2379f18a834aSKevin Wolf     const cmdinfo_t *ct;
2380f18a834aSKevin Wolf 
2381f18a834aSKevin Wolf     if (argc == 1) {
2382f18a834aSKevin Wolf         help_all();
2383f18a834aSKevin Wolf         return 0;
2384f18a834aSKevin Wolf     }
2385f18a834aSKevin Wolf 
2386f18a834aSKevin Wolf     ct = find_command(argv[1]);
2387f18a834aSKevin Wolf     if (ct == NULL) {
2388f18a834aSKevin Wolf         printf("command %s not found\n", argv[1]);
2389f18a834aSKevin Wolf         return 0;
2390f18a834aSKevin Wolf     }
2391f18a834aSKevin Wolf 
2392f18a834aSKevin Wolf     help_onecmd(argv[1], ct);
2393f18a834aSKevin Wolf     return 0;
2394f18a834aSKevin Wolf }
2395f18a834aSKevin Wolf 
2396f18a834aSKevin Wolf static const cmdinfo_t help_cmd = {
2397f18a834aSKevin Wolf     .name       = "help",
2398f18a834aSKevin Wolf     .altname    = "?",
2399f18a834aSKevin Wolf     .cfunc      = help_f,
2400f18a834aSKevin Wolf     .argmin     = 0,
2401f18a834aSKevin Wolf     .argmax     = 1,
2402f18a834aSKevin Wolf     .flags      = CMD_FLAG_GLOBAL,
2403f18a834aSKevin Wolf     .args       = "[command]",
2404f18a834aSKevin Wolf     .oneline    = "help for one or all commands",
2405f18a834aSKevin Wolf };
2406f18a834aSKevin Wolf 
24074c7b7e9bSMax Reitz bool qemuio_command(BlockBackend *blk, const char *cmd)
2408dd583296SKevin Wolf {
2409dd583296SKevin Wolf     char *input;
2410dd583296SKevin Wolf     const cmdinfo_t *ct;
2411dd583296SKevin Wolf     char **v;
2412dd583296SKevin Wolf     int c;
2413dd583296SKevin Wolf     bool done = false;
2414dd583296SKevin Wolf 
2415dd583296SKevin Wolf     input = g_strdup(cmd);
2416dd583296SKevin Wolf     v = breakline(input, &c);
2417dd583296SKevin Wolf     if (c) {
2418dd583296SKevin Wolf         ct = find_command(v[0]);
2419dd583296SKevin Wolf         if (ct) {
24204c7b7e9bSMax Reitz             done = command(blk, ct, c, v);
2421dd583296SKevin Wolf         } else {
2422dd583296SKevin Wolf             fprintf(stderr, "command \"%s\" not found\n", v[0]);
2423dd583296SKevin Wolf         }
2424dd583296SKevin Wolf     }
2425dd583296SKevin Wolf     g_free(input);
2426dd583296SKevin Wolf     g_free(v);
2427dd583296SKevin Wolf 
2428dd583296SKevin Wolf     return done;
2429dd583296SKevin Wolf }
2430dd583296SKevin Wolf 
2431797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void)
2432797ac58cSKevin Wolf {
2433797ac58cSKevin Wolf     /* initialize commands */
2434c2cdf5c5SKevin Wolf     qemuio_add_command(&help_cmd);
2435c2cdf5c5SKevin Wolf     qemuio_add_command(&read_cmd);
2436c2cdf5c5SKevin Wolf     qemuio_add_command(&readv_cmd);
2437c2cdf5c5SKevin Wolf     qemuio_add_command(&write_cmd);
2438c2cdf5c5SKevin Wolf     qemuio_add_command(&writev_cmd);
2439c2cdf5c5SKevin Wolf     qemuio_add_command(&multiwrite_cmd);
2440c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_read_cmd);
2441c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_write_cmd);
2442c2cdf5c5SKevin Wolf     qemuio_add_command(&aio_flush_cmd);
2443c2cdf5c5SKevin Wolf     qemuio_add_command(&flush_cmd);
2444c2cdf5c5SKevin Wolf     qemuio_add_command(&truncate_cmd);
2445c2cdf5c5SKevin Wolf     qemuio_add_command(&length_cmd);
2446c2cdf5c5SKevin Wolf     qemuio_add_command(&info_cmd);
2447c2cdf5c5SKevin Wolf     qemuio_add_command(&discard_cmd);
2448c2cdf5c5SKevin Wolf     qemuio_add_command(&alloc_cmd);
2449c2cdf5c5SKevin Wolf     qemuio_add_command(&map_cmd);
24505bbd2e59SKevin Wolf     qemuio_add_command(&reopen_cmd);
2451c2cdf5c5SKevin Wolf     qemuio_add_command(&break_cmd);
24524cc70e93SFam Zheng     qemuio_add_command(&remove_break_cmd);
2453c2cdf5c5SKevin Wolf     qemuio_add_command(&resume_cmd);
2454c2cdf5c5SKevin Wolf     qemuio_add_command(&wait_break_cmd);
2455c2cdf5c5SKevin Wolf     qemuio_add_command(&abort_cmd);
2456cd33d02aSKevin Wolf     qemuio_add_command(&sleep_cmd);
24570e82dc7bSMax Reitz     qemuio_add_command(&sigraise_cmd);
2458797ac58cSKevin Wolf }
2459