blob: c2fbaae0fdda03154806954e31da051204413072 [file] [log] [blame]
Kevin Wolf797ac582013-06-05 14:19:31 +02001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
Eric Blake093ea232016-05-07 21:16:43 -06004 * Copyright (C) 2009-2016 Red Hat, Inc.
Kevin Wolf797ac582013-06-05 14:19:31 +02005 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010012#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020013#include "qemu-io.h"
Max Reitz4c7b7e92015-02-05 13:58:22 -050014#include "sysemu/block-backend.h"
15#include "block/block.h"
16#include "block/block_int.h" /* for info_f() */
Max Reitza8d8ecb2013-10-09 10:46:17 +020017#include "block/qapi.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010020#include "qemu/option.h"
Kevin Wolfcd33d022014-01-15 15:39:10 +010021#include "qemu/timer.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Kevin Wolf797ac582013-06-05 14:19:31 +020023
24#define CMD_NOFILE_OK 0x01
25
Stefan Weilf9883882014-03-05 22:23:00 +010026bool qemuio_misalign;
Kevin Wolf797ac582013-06-05 14:19:31 +020027
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020028static cmdinfo_t *cmdtab;
29static int ncmds;
30
31static int compare_cmdname(const void *a, const void *b)
32{
33 return strcmp(((const cmdinfo_t *)a)->name,
34 ((const cmdinfo_t *)b)->name);
35}
36
37void qemuio_add_command(const cmdinfo_t *ci)
38{
Peter Maydell6aabeb52017-03-31 14:38:49 +010039 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
40 * flags allow it not to be, so that combination is invalid.
41 * Catch it now rather than letting it manifest as a crash if a
42 * particular set of command line options are used.
43 */
44 assert(ci->perm == 0 ||
45 (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
Markus Armbruster02c4f262014-08-19 10:31:09 +020046 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020047 cmdtab[ncmds - 1] = *ci;
48 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
49}
50
Max Reitzb444d0e2018-05-09 21:42:58 +020051void qemuio_command_usage(const cmdinfo_t *ci)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020052{
53 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020054}
55
Max Reitz4c7b7e92015-02-05 13:58:22 -050056static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020057{
58 if (ct->flags & CMD_FLAG_GLOBAL) {
59 return 1;
60 }
Max Reitz4c7b7e92015-02-05 13:58:22 -050061 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020062 fprintf(stderr, "no file open, try 'help open'\n");
63 return 0;
64 }
65 return 1;
66}
67
Max Reitzb444d0e2018-05-09 21:42:58 +020068static void command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
69 char **argv)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020070{
71 char *cmd = argv[0];
72
Max Reitz4c7b7e92015-02-05 13:58:22 -050073 if (!init_check_command(blk, ct)) {
Max Reitzb444d0e2018-05-09 21:42:58 +020074 return;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020075 }
76
77 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
78 if (ct->argmax == -1) {
79 fprintf(stderr,
80 "bad argument count %d to %s, expected at least %d arguments\n",
81 argc-1, cmd, ct->argmin);
82 } else if (ct->argmin == ct->argmax) {
83 fprintf(stderr,
84 "bad argument count %d to %s, expected %d arguments\n",
85 argc-1, cmd, ct->argmin);
86 } else {
87 fprintf(stderr,
88 "bad argument count %d to %s, expected between %d and %d arguments\n",
89 argc-1, cmd, ct->argmin, ct->argmax);
90 }
Max Reitzb444d0e2018-05-09 21:42:58 +020091 return;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020092 }
Kevin Wolf887354b2017-02-10 16:24:56 +010093
94 /* Request additional permissions if necessary for this command. The caller
95 * is responsible for restoring the original permissions afterwards if this
96 * is what it wants. */
97 if (ct->perm && blk_is_available(blk)) {
98 uint64_t orig_perm, orig_shared_perm;
99 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
100
101 if (ct->perm & ~orig_perm) {
102 uint64_t new_perm;
103 Error *local_err = NULL;
104 int ret;
105
106 new_perm = orig_perm | ct->perm;
107
108 ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
109 if (ret < 0) {
110 error_report_err(local_err);
Max Reitzb444d0e2018-05-09 21:42:58 +0200111 return;
Kevin Wolf887354b2017-02-10 16:24:56 +0100112 }
113 }
114 }
115
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200116 optind = 0;
Max Reitzb444d0e2018-05-09 21:42:58 +0200117 ct->cfunc(blk, argc, argv);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200118}
119
120static const cmdinfo_t *find_command(const char *cmd)
121{
122 cmdinfo_t *ct;
123
124 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
125 if (strcmp(ct->name, cmd) == 0 ||
126 (ct->altname && strcmp(ct->altname, cmd) == 0))
127 {
128 return (const cmdinfo_t *)ct;
129 }
130 }
131 return NULL;
132}
133
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100134/* Invoke fn() for commands with a matching prefix */
135void qemuio_complete_command(const char *input,
136 void (*fn)(const char *cmd, void *opaque),
137 void *opaque)
138{
139 cmdinfo_t *ct;
140 size_t input_len = strlen(input);
141
142 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
143 if (strncmp(input, ct->name, input_len) == 0) {
144 fn(ct->name, opaque);
145 }
146 }
147}
148
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200149static char **breakline(char *input, int *count)
150{
151 int c = 0;
152 char *p;
Markus Armbruster5839e532014-08-19 10:31:08 +0200153 char **rval = g_new0(char *, 1);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200154
155 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
156 if (!*p) {
157 continue;
158 }
159 c++;
Markus Armbruster08193dd2014-08-19 10:31:10 +0200160 rval = g_renew(char *, rval, (c + 1));
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200161 rval[c - 1] = p;
162 rval[c] = NULL;
163 }
164 *count = c;
165 return rval;
166}
167
Kevin Wolf797ac582013-06-05 14:19:31 +0200168static int64_t cvtnum(const char *s)
169{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100170 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100171 uint64_t value;
John Snowef5a7882015-11-05 18:53:03 -0500172
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100173 err = qemu_strtosz(s, NULL, &value);
174 if (err < 0) {
175 return err;
176 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100177 if (value > INT64_MAX) {
178 return -ERANGE;
179 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100180 return value;
Kevin Wolf797ac582013-06-05 14:19:31 +0200181}
182
John Snowa9ecfa02015-11-05 18:53:04 -0500183static void print_cvtnum_err(int64_t rc, const char *arg)
184{
185 switch (rc) {
186 case -EINVAL:
187 printf("Parsing error: non-numeric argument,"
188 " or extraneous/unrecognized suffix -- %s\n", arg);
189 break;
190 case -ERANGE:
191 printf("Parsing error: argument too large -- %s\n", arg);
192 break;
193 default:
194 printf("Parsing error: %s\n", arg);
195 }
196}
197
Kevin Wolf0b613882013-06-05 14:19:38 +0200198#define EXABYTES(x) ((long long)(x) << 60)
199#define PETABYTES(x) ((long long)(x) << 50)
200#define TERABYTES(x) ((long long)(x) << 40)
201#define GIGABYTES(x) ((long long)(x) << 30)
202#define MEGABYTES(x) ((long long)(x) << 20)
203#define KILOBYTES(x) ((long long)(x) << 10)
204
205#define TO_EXABYTES(x) ((x) / EXABYTES(1))
206#define TO_PETABYTES(x) ((x) / PETABYTES(1))
207#define TO_TERABYTES(x) ((x) / TERABYTES(1))
208#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
209#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
210#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
211
212static void cvtstr(double value, char *str, size_t size)
213{
214 char *trim;
215 const char *suffix;
216
217 if (value >= EXABYTES(1)) {
218 suffix = " EiB";
219 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
220 } else if (value >= PETABYTES(1)) {
221 suffix = " PiB";
222 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
223 } else if (value >= TERABYTES(1)) {
224 suffix = " TiB";
225 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
226 } else if (value >= GIGABYTES(1)) {
227 suffix = " GiB";
228 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
229 } else if (value >= MEGABYTES(1)) {
230 suffix = " MiB";
231 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
232 } else if (value >= KILOBYTES(1)) {
233 suffix = " KiB";
234 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
235 } else {
236 suffix = " bytes";
237 snprintf(str, size - 6, "%f", value);
238 }
239
240 trim = strstr(str, ".000");
241 if (trim) {
242 strcpy(trim, suffix);
243 } else {
244 strcat(str, suffix);
245 }
246}
247
248
249
250static struct timeval tsub(struct timeval t1, struct timeval t2)
251{
252 t1.tv_usec -= t2.tv_usec;
253 if (t1.tv_usec < 0) {
254 t1.tv_usec += 1000000;
255 t1.tv_sec--;
256 }
257 t1.tv_sec -= t2.tv_sec;
258 return t1;
259}
260
261static double tdiv(double value, struct timeval tv)
262{
263 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
264}
265
266#define HOURS(sec) ((sec) / (60 * 60))
267#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
268#define SECONDS(sec) ((sec) % 60)
269
270enum {
271 DEFAULT_TIME = 0x0,
272 TERSE_FIXED_TIME = 0x1,
273 VERBOSE_FIXED_TIME = 0x2,
274};
275
276static void timestr(struct timeval *tv, char *ts, size_t size, int format)
277{
278 double usec = (double)tv->tv_usec / 1000000.0;
279
280 if (format & TERSE_FIXED_TIME) {
281 if (!HOURS(tv->tv_sec)) {
282 snprintf(ts, size, "%u:%02u.%02u",
283 (unsigned int) MINUTES(tv->tv_sec),
284 (unsigned int) SECONDS(tv->tv_sec),
285 (unsigned int) (usec * 100));
286 return;
287 }
288 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
289 }
290
291 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
292 snprintf(ts, size, "%u:%02u:%02u.%02u",
293 (unsigned int) HOURS(tv->tv_sec),
294 (unsigned int) MINUTES(tv->tv_sec),
295 (unsigned int) SECONDS(tv->tv_sec),
296 (unsigned int) (usec * 100));
297 } else {
298 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
299 }
300}
301
Kevin Wolf797ac582013-06-05 14:19:31 +0200302/*
303 * Parse the pattern argument to various sub-commands.
304 *
305 * Because the pattern is used as an argument to memset it must evaluate
306 * to an unsigned integer that fits into a single byte.
307 */
308static int parse_pattern(const char *arg)
309{
310 char *endptr = NULL;
311 long pattern;
312
313 pattern = strtol(arg, &endptr, 0);
314 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
315 printf("%s is not a valid pattern byte\n", arg);
316 return -1;
317 }
318
319 return pattern;
320}
321
322/*
323 * Memory allocation helpers.
324 *
325 * Make sure memory is aligned by default, or purposefully misaligned if
326 * that is specified on the command line.
327 */
328
329#define MISALIGN_OFFSET 16
Max Reitz4c7b7e92015-02-05 13:58:22 -0500330static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200331{
332 void *buf;
333
334 if (qemuio_misalign) {
335 len += MISALIGN_OFFSET;
336 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500337 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200338 memset(buf, pattern, len);
339 if (qemuio_misalign) {
340 buf += MISALIGN_OFFSET;
341 }
342 return buf;
343}
344
345static void qemu_io_free(void *p)
346{
347 if (qemuio_misalign) {
348 p -= MISALIGN_OFFSET;
349 }
350 qemu_vfree(p);
351}
352
John Snow9b0beaf2015-11-05 18:53:02 -0500353static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200354{
John Snow9b0beaf2015-11-05 18:53:02 -0500355 uint64_t i;
356 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200357 const uint8_t *p;
358
359 for (i = 0, p = buffer; i < len; i += 16) {
360 const uint8_t *s = p;
361
362 printf("%08" PRIx64 ": ", offset + i);
363 for (j = 0; j < 16 && i + j < len; j++, p++) {
364 printf("%02x ", *p);
365 }
366 printf(" ");
367 for (j = 0; j < 16 && i + j < len; j++, s++) {
368 if (isalnum(*s)) {
369 printf("%c", *s);
370 } else {
371 printf(".");
372 }
373 }
374 printf("\n");
375 }
376}
377
378static void print_report(const char *op, struct timeval *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600379 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200380{
381 char s1[64], s2[64], ts[64];
382
383 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
384 if (!Cflag) {
385 cvtstr((double)total, s1, sizeof(s1));
386 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500387 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200388 op, total, count, offset);
389 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
390 s1, cnt, ts, s2, tdiv((double)cnt, *t));
391 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500392 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200393 total, cnt, ts,
394 tdiv((double)total, *t),
395 tdiv((double)cnt, *t));
396 }
397}
398
399/*
400 * Parse multiple length statements for vectored I/O, and construct an I/O
401 * vector matching it.
402 */
403static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500404create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200405 int pattern)
406{
407 size_t *sizes = g_new0(size_t, nr_iov);
408 size_t count = 0;
409 void *buf = NULL;
410 void *p;
411 int i;
412
413 for (i = 0; i < nr_iov; i++) {
414 char *arg = argv[i];
415 int64_t len;
416
417 len = cvtnum(arg);
418 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500419 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200420 goto fail;
421 }
422
Alberto Garcia3026c462017-01-31 18:09:54 +0200423 if (len > BDRV_REQUEST_MAX_BYTES) {
424 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
425 (uint64_t)BDRV_REQUEST_MAX_BYTES);
426 goto fail;
427 }
428
429 if (count > BDRV_REQUEST_MAX_BYTES - len) {
430 printf("The total number of bytes exceed the maximum size %" PRIu64
431 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200432 goto fail;
433 }
434
Kevin Wolf797ac582013-06-05 14:19:31 +0200435 sizes[i] = len;
436 count += len;
437 }
438
439 qemu_iovec_init(qiov, nr_iov);
440
Max Reitz4c7b7e92015-02-05 13:58:22 -0500441 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200442
443 for (i = 0; i < nr_iov; i++) {
444 qemu_iovec_add(qiov, p, sizes[i]);
445 p += sizes[i];
446 }
447
448fail:
449 g_free(sizes);
450 return buf;
451}
452
John Snow9b0beaf2015-11-05 18:53:02 -0500453static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300454 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200455{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300456 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500457 return -ERANGE;
458 }
459
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300460 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200461 if (*total < 0) {
462 return *total;
463 }
464 return 1;
465}
466
John Snow9b0beaf2015-11-05 18:53:02 -0500467static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300468 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200469{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300470 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500471 return -ERANGE;
472 }
473
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300474 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200475 if (*total < 0) {
476 return *total;
477 }
478 return 1;
479}
480
481typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500482 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200483 int64_t offset;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300484 int64_t bytes;
John Snow9b0beaf2015-11-05 18:53:02 -0500485 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600486 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200487 int ret;
488 bool done;
489} CoWriteZeroes;
490
Eric Blaked004bd52016-05-24 16:25:20 -0600491static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
Kevin Wolf797ac582013-06-05 14:19:31 +0200492{
493 CoWriteZeroes *data = opaque;
494
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300495 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
Eric Blaked004bd52016-05-24 16:25:20 -0600496 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200497 data->done = true;
498 if (data->ret < 0) {
499 *data->total = data->ret;
500 return;
501 }
502
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300503 *data->total = data->bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200504}
505
Eric Blaked004bd52016-05-24 16:25:20 -0600506static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300507 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200508{
509 Coroutine *co;
510 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500511 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200512 .offset = offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300513 .bytes = bytes,
Kevin Wolf797ac582013-06-05 14:19:31 +0200514 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600515 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200516 .done = false,
517 };
518
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300519 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500520 return -ERANGE;
521 }
522
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200523 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
Fam Zheng324ec3e2017-04-10 20:16:18 +0800524 bdrv_coroutine_enter(blk_bs(blk), co);
Kevin Wolf797ac582013-06-05 14:19:31 +0200525 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500526 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200527 }
528 if (data.ret < 0) {
529 return data.ret;
530 } else {
531 return 1;
532 }
533}
534
Max Reitz4c7b7e92015-02-05 13:58:22 -0500535static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300536 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200537{
538 int ret;
539
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300540 if (bytes >> 9 > BDRV_REQUEST_MAX_SECTORS) {
John Snow9b0beaf2015-11-05 18:53:02 -0500541 return -ERANGE;
542 }
543
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300544 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200545 if (ret < 0) {
546 return ret;
547 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300548 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200549 return 1;
550}
551
Max Reitz4c7b7e92015-02-05 13:58:22 -0500552static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500553 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200554{
John Snow9b0beaf2015-11-05 18:53:02 -0500555 if (count > INT_MAX) {
556 return -ERANGE;
557 }
558
Max Reitz4c7b7e92015-02-05 13:58:22 -0500559 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200560 if (*total < 0) {
561 return *total;
562 }
563 return 1;
564}
565
Max Reitz4c7b7e92015-02-05 13:58:22 -0500566static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500567 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200568{
John Snow9b0beaf2015-11-05 18:53:02 -0500569 if (count > INT_MAX) {
570 return -ERANGE;
571 }
572
Max Reitz4c7b7e92015-02-05 13:58:22 -0500573 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200574 if (*total < 0) {
575 return *total;
576 }
577 return 1;
578}
579
580#define NOT_DONE 0x7fffffff
581static void aio_rw_done(void *opaque, int ret)
582{
583 *(int *)opaque = ret;
584}
585
Max Reitz4c7b7e92015-02-05 13:58:22 -0500586static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200587 int64_t offset, int *total)
588{
589 int async_ret = NOT_DONE;
590
Eric Blake7b3f9712016-05-06 10:26:44 -0600591 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200592 while (async_ret == NOT_DONE) {
593 main_loop_wait(false);
594 }
595
596 *total = qiov->size;
597 return async_ret < 0 ? async_ret : 1;
598}
599
Max Reitz4c7b7e92015-02-05 13:58:22 -0500600static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600601 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200602{
603 int async_ret = NOT_DONE;
604
Eric Blake770e0e02016-05-07 21:16:44 -0600605 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200606 while (async_ret == NOT_DONE) {
607 main_loop_wait(false);
608 }
609
610 *total = qiov->size;
611 return async_ret < 0 ? async_ret : 1;
612}
613
Kevin Wolf797ac582013-06-05 14:19:31 +0200614static void read_help(void)
615{
616 printf(
617"\n"
618" reads a range of bytes from the given offset\n"
619"\n"
620" Example:\n"
621" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
622"\n"
623" Reads a segment of the currently open file, optionally dumping it to the\n"
624" standard output stream (with -v option) for subsequent inspection.\n"
625" -b, -- read from the VM state rather than the virtual disk\n"
626" -C, -- report statistics in a machine parsable format\n"
627" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600628" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200629" -P, -- use a pattern to verify read data\n"
630" -q, -- quiet mode, do not show I/O statistics\n"
631" -s, -- start offset for pattern verification (only with -P)\n"
632" -v, -- dump buffer to standard output\n"
633"\n");
634}
635
Max Reitzb444d0e2018-05-09 21:42:58 +0200636static void read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200637
638static const cmdinfo_t read_cmd = {
639 .name = "read",
640 .altname = "r",
641 .cfunc = read_f,
642 .argmin = 2,
643 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600644 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200645 .oneline = "reads a number of bytes at a specified offset",
646 .help = read_help,
647};
648
Max Reitzb444d0e2018-05-09 21:42:58 +0200649static void read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200650{
651 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600652 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600653 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200654 int c, cnt;
655 char *buf;
656 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500657 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200658 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500659 int64_t total = 0;
660 int pattern = 0;
661 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200662
Eric Blakeb062ad82015-05-12 09:10:56 -0600663 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200664 switch (c) {
665 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600666 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200667 break;
668 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600669 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200670 break;
671 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600672 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200673 pattern_count = cvtnum(optarg);
674 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500675 print_cvtnum_err(pattern_count, optarg);
Max Reitzb444d0e2018-05-09 21:42:58 +0200676 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200677 }
678 break;
679 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600680 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200681 break;
682 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600683 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200684 pattern = parse_pattern(optarg);
685 if (pattern < 0) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200686 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200687 }
688 break;
689 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600690 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200691 break;
692 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600693 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200694 pattern_offset = cvtnum(optarg);
695 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500696 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb444d0e2018-05-09 21:42:58 +0200697 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200698 }
699 break;
700 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600701 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200702 break;
703 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200704 qemuio_command_usage(&read_cmd);
705 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200706 }
707 }
708
709 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200710 qemuio_command_usage(&read_cmd);
711 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200712 }
713
Kevin Wolf797ac582013-06-05 14:19:31 +0200714 offset = cvtnum(argv[optind]);
715 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500716 print_cvtnum_err(offset, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +0200717 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200718 }
719
720 optind++;
721 count = cvtnum(argv[optind]);
722 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500723 print_cvtnum_err(count, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +0200724 return;
Alberto Garcia3026c462017-01-31 18:09:54 +0200725 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500726 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200727 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +0200728 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200729 }
730
731 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200732 qemuio_command_usage(&read_cmd);
733 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200734 }
735
736 if (!lflag) {
737 pattern_count = count - pattern_offset;
738 }
739
740 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
741 printf("pattern verification range exceeds end of read data\n");
Max Reitzb444d0e2018-05-09 21:42:58 +0200742 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200743 }
744
Eric Blake093ea232016-05-07 21:16:43 -0600745 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500746 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
747 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200748 offset);
Max Reitzb444d0e2018-05-09 21:42:58 +0200749 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200750 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500751 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
752 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200753 count);
Max Reitzb444d0e2018-05-09 21:42:58 +0200754 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200755 }
756 }
757
Max Reitz4c7b7e92015-02-05 13:58:22 -0500758 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200759
760 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -0600761 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500762 cnt = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200763 } else {
Eric Blake7b3f9712016-05-06 10:26:44 -0600764 cnt = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200765 }
766 gettimeofday(&t2, NULL);
767
768 if (cnt < 0) {
769 printf("read failed: %s\n", strerror(-cnt));
770 goto out;
771 }
772
773 if (Pflag) {
774 void *cmp_buf = g_malloc(pattern_count);
775 memset(cmp_buf, pattern, pattern_count);
776 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
777 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500778 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200779 offset + pattern_offset, pattern_count);
780 }
781 g_free(cmp_buf);
782 }
783
784 if (qflag) {
785 goto out;
786 }
787
788 if (vflag) {
789 dump_buffer(buf, offset, count);
790 }
791
792 /* Finally, report back -- -C gives a parsable format */
793 t2 = tsub(t2, t1);
794 print_report("read", &t2, offset, count, total, cnt, Cflag);
795
796out:
797 qemu_io_free(buf);
Kevin Wolf797ac582013-06-05 14:19:31 +0200798}
799
800static void readv_help(void)
801{
802 printf(
803"\n"
804" reads a range of bytes from the given offset into multiple buffers\n"
805"\n"
806" Example:\n"
807" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
808"\n"
809" Reads a segment of the currently open file, optionally dumping it to the\n"
810" standard output stream (with -v option) for subsequent inspection.\n"
811" Uses multiple iovec buffers if more than one byte range is specified.\n"
812" -C, -- report statistics in a machine parsable format\n"
813" -P, -- use a pattern to verify read data\n"
814" -v, -- dump buffer to standard output\n"
815" -q, -- quiet mode, do not show I/O statistics\n"
816"\n");
817}
818
Max Reitzb444d0e2018-05-09 21:42:58 +0200819static void readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200820
821static const cmdinfo_t readv_cmd = {
822 .name = "readv",
823 .cfunc = readv_f,
824 .argmin = 2,
825 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600826 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200827 .oneline = "reads a number of bytes at a specified offset",
828 .help = readv_help,
829};
830
Max Reitzb444d0e2018-05-09 21:42:58 +0200831static void readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200832{
833 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600834 bool Cflag = false, qflag = false, vflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200835 int c, cnt;
836 char *buf;
837 int64_t offset;
838 /* Some compilers get confused and warn if this is not initialized. */
839 int total = 0;
840 int nr_iov;
841 QEMUIOVector qiov;
842 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600843 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200844
Eric Blakeb062ad82015-05-12 09:10:56 -0600845 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200846 switch (c) {
847 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600848 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200849 break;
850 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600851 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200852 pattern = parse_pattern(optarg);
853 if (pattern < 0) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200854 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200855 }
856 break;
857 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600858 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200859 break;
860 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600861 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200862 break;
863 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200864 qemuio_command_usage(&readv_cmd);
865 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200866 }
867 }
868
869 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200870 qemuio_command_usage(&readv_cmd);
871 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200872 }
873
874
875 offset = cvtnum(argv[optind]);
876 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500877 print_cvtnum_err(offset, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +0200878 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200879 }
880 optind++;
881
Kevin Wolf797ac582013-06-05 14:19:31 +0200882 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500883 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200884 if (buf == NULL) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200885 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200886 }
887
888 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -0500889 cnt = do_aio_readv(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200890 gettimeofday(&t2, NULL);
891
892 if (cnt < 0) {
893 printf("readv failed: %s\n", strerror(-cnt));
894 goto out;
895 }
896
897 if (Pflag) {
898 void *cmp_buf = g_malloc(qiov.size);
899 memset(cmp_buf, pattern, qiov.size);
900 if (memcmp(buf, cmp_buf, qiov.size)) {
901 printf("Pattern verification failed at offset %"
902 PRId64 ", %zd bytes\n", offset, qiov.size);
903 }
904 g_free(cmp_buf);
905 }
906
907 if (qflag) {
908 goto out;
909 }
910
911 if (vflag) {
912 dump_buffer(buf, offset, qiov.size);
913 }
914
915 /* Finally, report back -- -C gives a parsable format */
916 t2 = tsub(t2, t1);
917 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
918
919out:
920 qemu_iovec_destroy(&qiov);
921 qemu_io_free(buf);
Kevin Wolf797ac582013-06-05 14:19:31 +0200922}
923
924static void write_help(void)
925{
926 printf(
927"\n"
928" writes a range of bytes from the given offset\n"
929"\n"
930" Example:\n"
931" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
932"\n"
933" Writes into a segment of the currently open file, using a buffer\n"
934" filled with a set pattern (0xcdcdcdcd).\n"
935" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500936" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -0600937" -f, -- use Force Unit Access semantics\n"
Eric Blake093ea232016-05-07 21:16:43 -0600938" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200939" -P, -- use different pattern to fill file\n"
940" -C, -- report statistics in a machine parsable format\n"
941" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -0600942" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -0600943" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200944"\n");
945}
946
Max Reitzb444d0e2018-05-09 21:42:58 +0200947static void write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200948
949static const cmdinfo_t write_cmd = {
950 .name = "write",
951 .altname = "w",
952 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +0100953 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +0200954 .argmin = 2,
955 .argmax = -1,
Eric Blakec2e001c2016-05-07 21:16:45 -0600956 .args = "[-bcCfquz] [-P pattern] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200957 .oneline = "writes a number of bytes at a specified offset",
958 .help = write_help,
959};
960
Max Reitzb444d0e2018-05-09 21:42:58 +0200961static void write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200962{
963 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600964 bool Cflag = false, qflag = false, bflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600965 bool Pflag = false, zflag = false, cflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -0600966 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200967 int c, cnt;
968 char *buf = NULL;
969 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500970 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200971 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500972 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200973 int pattern = 0xcd;
974
Eric Blakec2e001c2016-05-07 21:16:45 -0600975 while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200976 switch (c) {
977 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600978 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200979 break;
980 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -0600981 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200982 break;
983 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600984 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200985 break;
Eric Blake770e0e02016-05-07 21:16:44 -0600986 case 'f':
987 flags |= BDRV_REQ_FUA;
988 break;
Kevin Wolf797ac582013-06-05 14:19:31 +0200989 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600990 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200991 break;
992 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600993 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200994 pattern = parse_pattern(optarg);
995 if (pattern < 0) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200996 return;
Kevin Wolf797ac582013-06-05 14:19:31 +0200997 }
998 break;
999 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001000 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001001 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001002 case 'u':
1003 flags |= BDRV_REQ_MAY_UNMAP;
1004 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001005 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001006 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001007 break;
1008 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001009 qemuio_command_usage(&write_cmd);
1010 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001011 }
1012 }
1013
1014 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001015 qemuio_command_usage(&write_cmd);
1016 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001017 }
1018
Eric Blake093ea232016-05-07 21:16:43 -06001019 if (bflag && zflag) {
1020 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb444d0e2018-05-09 21:42:58 +02001021 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001022 }
1023
Eric Blake770e0e02016-05-07 21:16:44 -06001024 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1025 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb444d0e2018-05-09 21:42:58 +02001026 return;
Eric Blake770e0e02016-05-07 21:16:44 -06001027 }
1028
Eric Blakec2e001c2016-05-07 21:16:45 -06001029 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1030 printf("-u requires -z to be specified\n");
Max Reitzb444d0e2018-05-09 21:42:58 +02001031 return;
Eric Blakec2e001c2016-05-07 21:16:45 -06001032 }
1033
Kevin Wolf797ac582013-06-05 14:19:31 +02001034 if (zflag && Pflag) {
1035 printf("-z and -P cannot be specified at the same time\n");
Max Reitzb444d0e2018-05-09 21:42:58 +02001036 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001037 }
1038
1039 offset = cvtnum(argv[optind]);
1040 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001041 print_cvtnum_err(offset, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001042 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001043 }
1044
1045 optind++;
1046 count = cvtnum(argv[optind]);
1047 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001048 print_cvtnum_err(count, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001049 return;
Alberto Garcia3026c462017-01-31 18:09:54 +02001050 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001051 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001052 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001053 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001054 }
1055
Eric Blake093ea232016-05-07 21:16:43 -06001056 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001057 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1058 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001059 offset);
Max Reitzb444d0e2018-05-09 21:42:58 +02001060 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001061 }
1062
Eric Blake1bce6b42017-04-29 14:14:11 -05001063 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1064 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001065 count);
Max Reitzb444d0e2018-05-09 21:42:58 +02001066 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001067 }
1068 }
1069
1070 if (!zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001071 buf = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001072 }
1073
1074 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -06001075 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001076 cnt = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001077 } else if (zflag) {
Eric Blaked004bd52016-05-24 16:25:20 -06001078 cnt = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001079 } else if (cflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001080 cnt = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001081 } else {
Eric Blake770e0e02016-05-07 21:16:44 -06001082 cnt = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001083 }
1084 gettimeofday(&t2, NULL);
1085
1086 if (cnt < 0) {
1087 printf("write failed: %s\n", strerror(-cnt));
1088 goto out;
1089 }
1090
1091 if (qflag) {
1092 goto out;
1093 }
1094
1095 /* Finally, report back -- -C gives a parsable format */
1096 t2 = tsub(t2, t1);
1097 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1098
1099out:
1100 if (!zflag) {
1101 qemu_io_free(buf);
1102 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001103}
1104
1105static void
1106writev_help(void)
1107{
1108 printf(
1109"\n"
1110" writes a range of bytes from the given offset source from multiple buffers\n"
1111"\n"
1112" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001113" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001114"\n"
1115" Writes into a segment of the currently open file, using a buffer\n"
1116" filled with a set pattern (0xcdcdcdcd).\n"
1117" -P, -- use different pattern to fill file\n"
1118" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001119" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001120" -q, -- quiet mode, do not show I/O statistics\n"
1121"\n");
1122}
1123
Max Reitzb444d0e2018-05-09 21:42:58 +02001124static void writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001125
1126static const cmdinfo_t writev_cmd = {
1127 .name = "writev",
1128 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001129 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001130 .argmin = 2,
1131 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001132 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001133 .oneline = "writes a number of bytes at a specified offset",
1134 .help = writev_help,
1135};
1136
Max Reitzb444d0e2018-05-09 21:42:58 +02001137static void writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001138{
1139 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001140 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001141 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001142 int c, cnt;
1143 char *buf;
1144 int64_t offset;
1145 /* Some compilers get confused and warn if this is not initialized. */
1146 int total = 0;
1147 int nr_iov;
1148 int pattern = 0xcd;
1149 QEMUIOVector qiov;
1150
Eric Blake4ca1d342016-05-16 10:43:01 -06001151 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001152 switch (c) {
1153 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001154 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001155 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001156 case 'f':
1157 flags |= BDRV_REQ_FUA;
1158 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001159 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001160 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001161 break;
1162 case 'P':
1163 pattern = parse_pattern(optarg);
1164 if (pattern < 0) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001165 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001166 }
1167 break;
1168 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001169 qemuio_command_usage(&writev_cmd);
1170 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001171 }
1172 }
1173
1174 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001175 qemuio_command_usage(&writev_cmd);
1176 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001177 }
1178
1179 offset = cvtnum(argv[optind]);
1180 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001181 print_cvtnum_err(offset, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001182 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001183 }
1184 optind++;
1185
Kevin Wolf797ac582013-06-05 14:19:31 +02001186 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001187 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001188 if (buf == NULL) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001189 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001190 }
1191
1192 gettimeofday(&t1, NULL);
Eric Blake770e0e02016-05-07 21:16:44 -06001193 cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001194 gettimeofday(&t2, NULL);
1195
1196 if (cnt < 0) {
1197 printf("writev failed: %s\n", strerror(-cnt));
1198 goto out;
1199 }
1200
1201 if (qflag) {
1202 goto out;
1203 }
1204
1205 /* Finally, report back -- -C gives a parsable format */
1206 t2 = tsub(t2, t1);
1207 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1208out:
1209 qemu_iovec_destroy(&qiov);
1210 qemu_io_free(buf);
Kevin Wolf797ac582013-06-05 14:19:31 +02001211}
1212
Kevin Wolf797ac582013-06-05 14:19:31 +02001213struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001214 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001215 QEMUIOVector qiov;
1216 int64_t offset;
1217 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001218 bool qflag;
1219 bool vflag;
1220 bool Cflag;
1221 bool Pflag;
1222 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001223 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001224 int pattern;
1225 struct timeval t1;
1226};
1227
1228static void aio_write_done(void *opaque, int ret)
1229{
1230 struct aio_ctx *ctx = opaque;
1231 struct timeval t2;
1232
1233 gettimeofday(&t2, NULL);
1234
1235
1236 if (ret < 0) {
1237 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001238 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001239 goto out;
1240 }
1241
Max Reitz4c7b7e92015-02-05 13:58:22 -05001242 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001243
Kevin Wolf797ac582013-06-05 14:19:31 +02001244 if (ctx->qflag) {
1245 goto out;
1246 }
1247
1248 /* Finally, report back -- -C gives a parsable format */
1249 t2 = tsub(t2, ctx->t1);
1250 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1251 ctx->qiov.size, 1, ctx->Cflag);
1252out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001253 if (!ctx->zflag) {
1254 qemu_io_free(ctx->buf);
1255 qemu_iovec_destroy(&ctx->qiov);
1256 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001257 g_free(ctx);
1258}
1259
1260static void aio_read_done(void *opaque, int ret)
1261{
1262 struct aio_ctx *ctx = opaque;
1263 struct timeval t2;
1264
1265 gettimeofday(&t2, NULL);
1266
1267 if (ret < 0) {
1268 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001269 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001270 goto out;
1271 }
1272
1273 if (ctx->Pflag) {
1274 void *cmp_buf = g_malloc(ctx->qiov.size);
1275
1276 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1277 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1278 printf("Pattern verification failed at offset %"
1279 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1280 }
1281 g_free(cmp_buf);
1282 }
1283
Max Reitz4c7b7e92015-02-05 13:58:22 -05001284 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001285
Kevin Wolf797ac582013-06-05 14:19:31 +02001286 if (ctx->qflag) {
1287 goto out;
1288 }
1289
1290 if (ctx->vflag) {
1291 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1292 }
1293
1294 /* Finally, report back -- -C gives a parsable format */
1295 t2 = tsub(t2, ctx->t1);
1296 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1297 ctx->qiov.size, 1, ctx->Cflag);
1298out:
1299 qemu_io_free(ctx->buf);
1300 qemu_iovec_destroy(&ctx->qiov);
1301 g_free(ctx);
1302}
1303
1304static void aio_read_help(void)
1305{
1306 printf(
1307"\n"
1308" asynchronously reads a range of bytes from the given offset\n"
1309"\n"
1310" Example:\n"
1311" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1312"\n"
1313" Reads a segment of the currently open file, optionally dumping it to the\n"
1314" standard output stream (with -v option) for subsequent inspection.\n"
1315" The read is performed asynchronously and the aio_flush command must be\n"
1316" used to ensure all outstanding aio requests have been completed.\n"
1317" -C, -- report statistics in a machine parsable format\n"
1318" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001319" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001320" -v, -- dump buffer to standard output\n"
1321" -q, -- quiet mode, do not show I/O statistics\n"
1322"\n");
1323}
1324
Max Reitzb444d0e2018-05-09 21:42:58 +02001325static void aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001326
1327static const cmdinfo_t aio_read_cmd = {
1328 .name = "aio_read",
1329 .cfunc = aio_read_f,
1330 .argmin = 2,
1331 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001332 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001333 .oneline = "asynchronously reads a number of bytes",
1334 .help = aio_read_help,
1335};
1336
Max Reitzb444d0e2018-05-09 21:42:58 +02001337static void aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001338{
1339 int nr_iov, c;
1340 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1341
Max Reitz4c7b7e92015-02-05 13:58:22 -05001342 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001343 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001344 switch (c) {
1345 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001346 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001347 break;
1348 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001349 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001350 ctx->pattern = parse_pattern(optarg);
1351 if (ctx->pattern < 0) {
1352 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001353 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001354 }
1355 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001356 case 'i':
1357 printf("injecting invalid read request\n");
1358 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1359 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001360 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001361 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001362 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001363 break;
1364 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001365 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001366 break;
1367 default:
1368 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001369 qemuio_command_usage(&aio_read_cmd);
1370 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001371 }
1372 }
1373
1374 if (optind > argc - 2) {
1375 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001376 qemuio_command_usage(&aio_read_cmd);
1377 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001378 }
1379
1380 ctx->offset = cvtnum(argv[optind]);
1381 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001382 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001383 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001384 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001385 }
1386 optind++;
1387
Kevin Wolf797ac582013-06-05 14:19:31 +02001388 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001389 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001390 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001391 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001392 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001393 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001394 }
1395
1396 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001397 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1398 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001399 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Kevin Wolf797ac582013-06-05 14:19:31 +02001400}
1401
1402static void aio_write_help(void)
1403{
1404 printf(
1405"\n"
1406" asynchronously writes a range of bytes from the given offset source\n"
1407" from multiple buffers\n"
1408"\n"
1409" Example:\n"
1410" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1411"\n"
1412" Writes into a segment of the currently open file, using a buffer\n"
1413" filled with a set pattern (0xcdcdcdcd).\n"
1414" The write is performed asynchronously and the aio_flush command must be\n"
1415" used to ensure all outstanding aio requests have been completed.\n"
1416" -P, -- use different pattern to fill file\n"
1417" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001418" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001419" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001420" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001421" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001422" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001423"\n");
1424}
1425
Max Reitzb444d0e2018-05-09 21:42:58 +02001426static void aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001427
1428static const cmdinfo_t aio_write_cmd = {
1429 .name = "aio_write",
1430 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001431 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001432 .argmin = 2,
1433 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001434 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001435 .oneline = "asynchronously writes a number of bytes",
1436 .help = aio_write_help,
1437};
1438
Max Reitzb444d0e2018-05-09 21:42:58 +02001439static void aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001440{
1441 int nr_iov, c;
1442 int pattern = 0xcd;
1443 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001444 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001445
Max Reitz4c7b7e92015-02-05 13:58:22 -05001446 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001447 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001448 switch (c) {
1449 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001450 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001451 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001452 case 'f':
1453 flags |= BDRV_REQ_FUA;
1454 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001455 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001456 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001457 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001458 case 'u':
1459 flags |= BDRV_REQ_MAY_UNMAP;
1460 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001461 case 'P':
1462 pattern = parse_pattern(optarg);
1463 if (pattern < 0) {
1464 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001465 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001466 }
1467 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001468 case 'i':
1469 printf("injecting invalid write request\n");
1470 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1471 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001472 return;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001473 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001474 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001475 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001476 default:
1477 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001478 qemuio_command_usage(&aio_write_cmd);
1479 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001480 }
1481 }
1482
1483 if (optind > argc - 2) {
1484 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001485 qemuio_command_usage(&aio_write_cmd);
1486 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001487 }
1488
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001489 if (ctx->zflag && optind != argc - 2) {
1490 printf("-z supports only a single length parameter\n");
1491 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001492 return;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001493 }
1494
Eric Blakec2e001c2016-05-07 21:16:45 -06001495 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1496 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001497 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001498 return;
Eric Blakec2e001c2016-05-07 21:16:45 -06001499 }
1500
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001501 if (ctx->zflag && ctx->Pflag) {
1502 printf("-z and -P cannot be specified at the same time\n");
1503 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001504 return;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001505 }
1506
Kevin Wolf797ac582013-06-05 14:19:31 +02001507 ctx->offset = cvtnum(argv[optind]);
1508 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001509 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001510 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001511 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001512 }
1513 optind++;
1514
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001515 if (ctx->zflag) {
1516 int64_t count = cvtnum(argv[optind]);
1517 if (count < 0) {
1518 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001519 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001520 return;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001521 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001522
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001523 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001524 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1525 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001526 } else {
1527 nr_iov = argc - optind;
1528 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1529 pattern);
1530 if (ctx->buf == NULL) {
1531 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1532 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001533 return;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001534 }
1535
1536 gettimeofday(&ctx->t1, NULL);
1537 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1538 BLOCK_ACCT_WRITE);
1539
Eric Blake770e0e02016-05-07 21:16:44 -06001540 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1541 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001542 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001543}
1544
Max Reitzb444d0e2018-05-09 21:42:58 +02001545static void aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001546{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001547 BlockAcctCookie cookie;
1548 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001549 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001550 block_acct_done(blk_get_stats(blk), &cookie);
Kevin Wolf797ac582013-06-05 14:19:31 +02001551}
1552
1553static const cmdinfo_t aio_flush_cmd = {
1554 .name = "aio_flush",
1555 .cfunc = aio_flush_f,
1556 .oneline = "completes all outstanding aio requests"
1557};
1558
Max Reitzb444d0e2018-05-09 21:42:58 +02001559static void flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001560{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001561 blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001562}
1563
1564static const cmdinfo_t flush_cmd = {
1565 .name = "flush",
1566 .altname = "f",
1567 .cfunc = flush_f,
1568 .oneline = "flush all in-core file state to disk",
1569};
1570
Max Reitzb444d0e2018-05-09 21:42:58 +02001571static void truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001572{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001573 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001574 int64_t offset;
1575 int ret;
1576
1577 offset = cvtnum(argv[1]);
1578 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001579 print_cvtnum_err(offset, argv[1]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001580 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001581 }
1582
Max Reitz3a691c52017-06-13 22:20:54 +02001583 ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001584 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001585 error_report_err(local_err);
Max Reitzb444d0e2018-05-09 21:42:58 +02001586 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001587 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001588}
1589
1590static const cmdinfo_t truncate_cmd = {
1591 .name = "truncate",
1592 .altname = "t",
1593 .cfunc = truncate_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001594 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001595 .argmin = 1,
1596 .argmax = 1,
1597 .args = "off",
1598 .oneline = "truncates the current file at the given offset",
1599};
1600
Max Reitzb444d0e2018-05-09 21:42:58 +02001601static void length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001602{
1603 int64_t size;
1604 char s1[64];
1605
Max Reitz4c7b7e92015-02-05 13:58:22 -05001606 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001607 if (size < 0) {
1608 printf("getlength: %s\n", strerror(-size));
Max Reitzb444d0e2018-05-09 21:42:58 +02001609 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001610 }
1611
1612 cvtstr(size, s1, sizeof(s1));
1613 printf("%s\n", s1);
Kevin Wolf797ac582013-06-05 14:19:31 +02001614}
1615
1616
1617static const cmdinfo_t length_cmd = {
1618 .name = "length",
1619 .altname = "l",
1620 .cfunc = length_f,
1621 .oneline = "gets the length of the current file",
1622};
1623
1624
Max Reitzb444d0e2018-05-09 21:42:58 +02001625static void info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001626{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001627 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001628 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001629 ImageInfoSpecific *spec_info;
Kevin Wolf797ac582013-06-05 14:19:31 +02001630 char s1[64], s2[64];
1631 int ret;
1632
1633 if (bs->drv && bs->drv->format_name) {
1634 printf("format name: %s\n", bs->drv->format_name);
1635 }
1636 if (bs->drv && bs->drv->protocol_name) {
1637 printf("format name: %s\n", bs->drv->protocol_name);
1638 }
1639
1640 ret = bdrv_get_info(bs, &bdi);
1641 if (ret) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001642 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001643 }
1644
1645 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1646 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1647
1648 printf("cluster size: %s\n", s1);
1649 printf("vm state offset: %s\n", s2);
1650
Max Reitza8d8ecb2013-10-09 10:46:17 +02001651 spec_info = bdrv_get_specific_info(bs);
1652 if (spec_info) {
1653 printf("Format specific information:\n");
1654 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1655 qapi_free_ImageInfoSpecific(spec_info);
1656 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001657}
1658
1659
1660
1661static const cmdinfo_t info_cmd = {
1662 .name = "info",
1663 .altname = "i",
1664 .cfunc = info_f,
1665 .oneline = "prints information about the current file",
1666};
1667
1668static void discard_help(void)
1669{
1670 printf(
1671"\n"
1672" discards a range of bytes from the given offset\n"
1673"\n"
1674" Example:\n"
1675" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1676"\n"
1677" Discards a segment of the currently open file.\n"
1678" -C, -- report statistics in a machine parsable format\n"
1679" -q, -- quiet mode, do not show I/O statistics\n"
1680"\n");
1681}
1682
Max Reitzb444d0e2018-05-09 21:42:58 +02001683static void discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001684
1685static const cmdinfo_t discard_cmd = {
1686 .name = "discard",
1687 .altname = "d",
1688 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001689 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001690 .argmin = 2,
1691 .argmax = -1,
1692 .args = "[-Cq] off len",
1693 .oneline = "discards a number of bytes at a specified offset",
1694 .help = discard_help,
1695};
1696
Max Reitzb444d0e2018-05-09 21:42:58 +02001697static void discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001698{
1699 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001700 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001701 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001702 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02001703
Eric Blakeb062ad82015-05-12 09:10:56 -06001704 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001705 switch (c) {
1706 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001707 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001708 break;
1709 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001710 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001711 break;
1712 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001713 qemuio_command_usage(&discard_cmd);
1714 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001715 }
1716 }
1717
1718 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001719 qemuio_command_usage(&discard_cmd);
1720 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001721 }
1722
1723 offset = cvtnum(argv[optind]);
1724 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001725 print_cvtnum_err(offset, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001726 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001727 }
1728
1729 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001730 bytes = cvtnum(argv[optind]);
1731 if (bytes < 0) {
1732 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001733 return;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001734 } else if (bytes >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
John Snow9b0beaf2015-11-05 18:53:02 -05001735 printf("length cannot exceed %"PRIu64", given %s\n",
Max Reitza3674672016-06-20 16:26:22 +02001736 (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
John Snow9b0beaf2015-11-05 18:53:02 -05001737 argv[optind]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001738 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001739 }
1740
1741 gettimeofday(&t1, NULL);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001742 ret = blk_pdiscard(blk, offset, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +02001743 gettimeofday(&t2, NULL);
1744
1745 if (ret < 0) {
1746 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb444d0e2018-05-09 21:42:58 +02001747 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001748 }
1749
1750 /* Finally, report back -- -C gives a parsable format */
1751 if (!qflag) {
1752 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001753 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02001754 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001755}
1756
Max Reitzb444d0e2018-05-09 21:42:58 +02001757static void alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001758{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001759 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05001760 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001761 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05001762 int ret;
1763 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001764
Eric Blaked6a644b2017-07-07 07:44:57 -05001765 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001766 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001767 print_cvtnum_err(offset, argv[1]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001768 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001769 }
1770
1771 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05001772 count = cvtnum(argv[2]);
1773 if (count < 0) {
1774 print_cvtnum_err(count, argv[2]);
Max Reitzb444d0e2018-05-09 21:42:58 +02001775 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001776 }
1777 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05001778 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02001779 }
1780
Eric Blaked6a644b2017-07-07 07:44:57 -05001781 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001782 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001783 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001784 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001785 if (ret < 0) {
1786 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb444d0e2018-05-09 21:42:58 +02001787 return;
Paolo Bonzinid6636402013-09-04 19:00:25 +02001788 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001789 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001790 remaining -= num;
1791 if (ret) {
1792 sum_alloc += num;
1793 }
1794 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001795 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001796 remaining = 0;
1797 }
1798 }
1799
Eric Blaked6a644b2017-07-07 07:44:57 -05001800 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02001801
Eric Blake4401fdc2017-04-29 14:14:12 -05001802 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001803 sum_alloc, count, s1);
Kevin Wolf797ac582013-06-05 14:19:31 +02001804}
1805
1806static const cmdinfo_t alloc_cmd = {
1807 .name = "alloc",
1808 .altname = "a",
1809 .argmin = 1,
1810 .argmax = 2,
1811 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05001812 .args = "offset [count]",
1813 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02001814};
1815
1816
Eric Blaked6a644b2017-07-07 07:44:57 -05001817static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1818 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02001819{
Eric Blaked6a644b2017-07-07 07:44:57 -05001820 int64_t num;
1821 int num_checked;
Kevin Wolf797ac582013-06-05 14:19:31 +02001822 int ret, firstret;
1823
Eric Blaked6a644b2017-07-07 07:44:57 -05001824 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1825 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001826 if (ret < 0) {
1827 return ret;
1828 }
1829
1830 firstret = ret;
1831 *pnum = num;
1832
Eric Blaked6a644b2017-07-07 07:44:57 -05001833 while (bytes > 0 && ret == firstret) {
1834 offset += num;
1835 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001836
Eric Blaked6a644b2017-07-07 07:44:57 -05001837 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1838 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001839 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001840 *pnum += num;
1841 } else {
1842 break;
1843 }
1844 }
1845
1846 return firstret;
1847}
1848
Max Reitzb444d0e2018-05-09 21:42:58 +02001849static void map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001850{
Eric Blaked6a644b2017-07-07 07:44:57 -05001851 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05001852 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02001853 int64_t num;
1854 int ret;
1855 const char *retstr;
1856
1857 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05001858 bytes = blk_getlength(blk);
1859 if (bytes < 0) {
1860 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb444d0e2018-05-09 21:42:58 +02001861 return;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001862 }
1863
Eric Blaked6a644b2017-07-07 07:44:57 -05001864 while (bytes) {
1865 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001866 if (ret < 0) {
1867 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb444d0e2018-05-09 21:42:58 +02001868 return;
Max Reitz4b25bbc2014-10-22 17:00:16 +02001869 } else if (!num) {
1870 error_report("Unexpected end of image");
Max Reitzb444d0e2018-05-09 21:42:58 +02001871 return;
Kevin Wolf797ac582013-06-05 14:19:31 +02001872 }
1873
1874 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05001875 cvtstr(num, s1, sizeof(s1));
1876 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05001877 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001878 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02001879
1880 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05001881 bytes -= num;
1882 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001883}
1884
1885static const cmdinfo_t map_cmd = {
1886 .name = "map",
1887 .argmin = 0,
1888 .argmax = 0,
1889 .cfunc = map_f,
1890 .args = "",
1891 .oneline = "prints the allocated areas of a file",
1892};
1893
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001894static void reopen_help(void)
1895{
1896 printf(
1897"\n"
1898" Changes the open options of an already opened image\n"
1899"\n"
1900" Example:\n"
1901" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1902"\n"
1903" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02001904" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001905" -c, -- Change the cache mode to the given value\n"
1906" -o, -- Changes block driver options (cf. 'open' command)\n"
1907"\n");
1908}
1909
Max Reitzb444d0e2018-05-09 21:42:58 +02001910static void reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001911
1912static QemuOptsList reopen_opts = {
1913 .name = "reopen",
1914 .merge_lists = true,
1915 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
1916 .desc = {
1917 /* no elements => accept any params */
1918 { /* end of list */ }
1919 },
1920};
1921
1922static const cmdinfo_t reopen_cmd = {
1923 .name = "reopen",
1924 .argmin = 0,
1925 .argmax = -1,
1926 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02001927 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001928 .oneline = "reopens an image with new options",
1929 .help = reopen_help,
1930};
1931
Max Reitzb444d0e2018-05-09 21:42:58 +02001932static void reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001933{
1934 BlockDriverState *bs = blk_bs(blk);
1935 QemuOpts *qopts;
1936 QDict *opts;
1937 int c;
1938 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001939 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02001940 bool has_rw_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001941
1942 BlockReopenQueue *brq;
1943 Error *local_err = NULL;
1944
Kevin Wolfea922032017-08-03 17:03:00 +02001945 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001946 switch (c) {
1947 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001948 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001949 error_report("Invalid cache option: %s", optarg);
Max Reitzb444d0e2018-05-09 21:42:58 +02001950 return;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001951 }
1952 break;
1953 case 'o':
1954 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
1955 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02001956 return;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001957 }
1958 break;
1959 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02001960 if (has_rw_option) {
1961 error_report("Only one -r/-w option may be given");
Max Reitzb444d0e2018-05-09 21:42:58 +02001962 return;
Kevin Wolfea922032017-08-03 17:03:00 +02001963 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001964 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02001965 has_rw_option = true;
1966 break;
1967 case 'w':
1968 if (has_rw_option) {
1969 error_report("Only one -r/-w option may be given");
Max Reitzb444d0e2018-05-09 21:42:58 +02001970 return;
Kevin Wolfea922032017-08-03 17:03:00 +02001971 }
1972 flags |= BDRV_O_RDWR;
1973 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001974 break;
1975 default:
1976 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02001977 qemuio_command_usage(&reopen_cmd);
1978 return;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001979 }
1980 }
1981
1982 if (optind != argc) {
1983 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02001984 qemuio_command_usage(&reopen_cmd);
1985 return;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001986 }
1987
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001988 if (writethrough != blk_enable_write_cache(blk) &&
1989 blk_get_attached_dev(blk))
1990 {
1991 error_report("Cannot change cache.writeback: Device attached");
1992 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02001993 return;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001994 }
1995
Kevin Wolff3adefb2017-09-22 14:50:12 +02001996 if (!(flags & BDRV_O_RDWR)) {
1997 uint64_t orig_perm, orig_shared_perm;
1998
1999 bdrv_drain(bs);
2000
2001 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2002 blk_set_perm(blk,
2003 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2004 orig_shared_perm,
2005 &error_abort);
2006 }
2007
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002008 qopts = qemu_opts_find(&reopen_opts, NULL);
2009 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
2010 qemu_opts_reset(&reopen_opts);
2011
Kevin Wolf1a63a902017-12-06 20:24:44 +01002012 bdrv_subtree_drained_begin(bs);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002013 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
Paolo Bonzini720150f2016-10-27 12:49:02 +02002014 bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002015 bdrv_subtree_drained_end(bs);
2016
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002017 if (local_err) {
2018 error_report_err(local_err);
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002019 } else {
2020 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002021 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002022}
2023
Max Reitzb444d0e2018-05-09 21:42:58 +02002024static void break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002025{
2026 int ret;
2027
Max Reitz4c7b7e92015-02-05 13:58:22 -05002028 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002029 if (ret < 0) {
2030 printf("Could not set breakpoint: %s\n", strerror(-ret));
2031 }
Kevin Wolf797ac582013-06-05 14:19:31 +02002032}
2033
Max Reitzb444d0e2018-05-09 21:42:58 +02002034static void remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002035{
2036 int ret;
2037
Max Reitz4c7b7e92015-02-05 13:58:22 -05002038 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002039 if (ret < 0) {
2040 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2041 }
Fam Zheng4cc70e92013-11-20 10:01:54 +08002042}
2043
Kevin Wolf797ac582013-06-05 14:19:31 +02002044static const cmdinfo_t break_cmd = {
2045 .name = "break",
2046 .argmin = 2,
2047 .argmax = 2,
2048 .cfunc = break_f,
2049 .args = "event tag",
2050 .oneline = "sets a breakpoint on event and tags the stopped "
2051 "request as tag",
2052};
2053
Fam Zheng4cc70e92013-11-20 10:01:54 +08002054static const cmdinfo_t remove_break_cmd = {
2055 .name = "remove_break",
2056 .argmin = 1,
2057 .argmax = 1,
2058 .cfunc = remove_break_f,
2059 .args = "tag",
2060 .oneline = "remove a breakpoint by tag",
2061};
2062
Max Reitzb444d0e2018-05-09 21:42:58 +02002063static void resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002064{
2065 int ret;
2066
Max Reitz4c7b7e92015-02-05 13:58:22 -05002067 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002068 if (ret < 0) {
2069 printf("Could not resume request: %s\n", strerror(-ret));
2070 }
Kevin Wolf797ac582013-06-05 14:19:31 +02002071}
2072
2073static const cmdinfo_t resume_cmd = {
2074 .name = "resume",
2075 .argmin = 1,
2076 .argmax = 1,
2077 .cfunc = resume_f,
2078 .args = "tag",
2079 .oneline = "resumes the request tagged as tag",
2080};
2081
Max Reitzb444d0e2018-05-09 21:42:58 +02002082static void wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002083{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002084 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2085 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002086 }
Kevin Wolf797ac582013-06-05 14:19:31 +02002087}
2088
2089static const cmdinfo_t wait_break_cmd = {
2090 .name = "wait_break",
2091 .argmin = 1,
2092 .argmax = 1,
2093 .cfunc = wait_break_f,
2094 .args = "tag",
2095 .oneline = "waits for the suspension of a request",
2096};
2097
Max Reitzb444d0e2018-05-09 21:42:58 +02002098static void abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002099{
2100 abort();
2101}
2102
2103static const cmdinfo_t abort_cmd = {
2104 .name = "abort",
2105 .cfunc = abort_f,
2106 .flags = CMD_NOFILE_OK,
2107 .oneline = "simulate a program crash using abort(3)",
2108};
2109
Max Reitz0e82dc72014-12-08 10:48:10 +01002110static void sigraise_help(void)
2111{
2112 printf(
2113"\n"
2114" raises the given signal\n"
2115"\n"
2116" Example:\n"
2117" 'sigraise %i' - raises SIGTERM\n"
2118"\n"
2119" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2120" given to sigraise.\n"
2121"\n", SIGTERM);
2122}
2123
Max Reitzb444d0e2018-05-09 21:42:58 +02002124static void sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002125
2126static const cmdinfo_t sigraise_cmd = {
2127 .name = "sigraise",
2128 .cfunc = sigraise_f,
2129 .argmin = 1,
2130 .argmax = 1,
2131 .flags = CMD_NOFILE_OK,
2132 .args = "signal",
2133 .oneline = "raises a signal",
2134 .help = sigraise_help,
2135};
2136
Max Reitzb444d0e2018-05-09 21:42:58 +02002137static void sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002138{
John Snow9b0beaf2015-11-05 18:53:02 -05002139 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002140 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002141 print_cvtnum_err(sig, argv[1]);
Max Reitzb444d0e2018-05-09 21:42:58 +02002142 return;
John Snow9b0beaf2015-11-05 18:53:02 -05002143 } else if (sig > NSIG) {
2144 printf("signal argument '%s' is too large to be a valid signal\n",
2145 argv[1]);
Max Reitzb444d0e2018-05-09 21:42:58 +02002146 return;
Max Reitz0e82dc72014-12-08 10:48:10 +01002147 }
2148
2149 /* Using raise() to kill this process does not necessarily flush all open
2150 * streams. At least stdout and stderr (although the latter should be
2151 * non-buffered anyway) should be flushed, though. */
2152 fflush(stdout);
2153 fflush(stderr);
2154
2155 raise(sig);
Max Reitz0e82dc72014-12-08 10:48:10 +01002156}
2157
Kevin Wolfcd33d022014-01-15 15:39:10 +01002158static void sleep_cb(void *opaque)
2159{
2160 bool *expired = opaque;
2161 *expired = true;
2162}
2163
Max Reitzb444d0e2018-05-09 21:42:58 +02002164static void sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002165{
2166 char *endptr;
2167 long ms;
2168 struct QEMUTimer *timer;
2169 bool expired = false;
2170
2171 ms = strtol(argv[1], &endptr, 0);
2172 if (ms < 0 || *endptr != '\0') {
2173 printf("%s is not a valid number\n", argv[1]);
Max Reitzb444d0e2018-05-09 21:42:58 +02002174 return;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002175 }
2176
2177 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2178 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2179
2180 while (!expired) {
2181 main_loop_wait(false);
2182 }
2183
2184 timer_free(timer);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002185}
2186
2187static const cmdinfo_t sleep_cmd = {
2188 .name = "sleep",
2189 .argmin = 1,
2190 .argmax = 1,
2191 .cfunc = sleep_f,
2192 .flags = CMD_NOFILE_OK,
2193 .oneline = "waits for the given value in milliseconds",
2194};
2195
Kevin Wolff18a8342013-06-05 14:19:33 +02002196static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2197{
2198 if (cmd) {
2199 printf("%s ", cmd);
2200 } else {
2201 printf("%s ", ct->name);
2202 if (ct->altname) {
2203 printf("(or %s) ", ct->altname);
2204 }
2205 }
2206
2207 if (ct->args) {
2208 printf("%s ", ct->args);
2209 }
2210 printf("-- %s\n", ct->oneline);
2211}
2212
2213static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2214{
2215 help_oneline(cmd, ct);
2216 if (ct->help) {
2217 ct->help();
2218 }
2219}
2220
2221static void help_all(void)
2222{
2223 const cmdinfo_t *ct;
2224
2225 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2226 help_oneline(ct->name, ct);
2227 }
2228 printf("\nUse 'help commandname' for extended help.\n");
2229}
2230
Max Reitzb444d0e2018-05-09 21:42:58 +02002231static void help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002232{
2233 const cmdinfo_t *ct;
2234
2235 if (argc == 1) {
2236 help_all();
Max Reitzb444d0e2018-05-09 21:42:58 +02002237 return;
Kevin Wolff18a8342013-06-05 14:19:33 +02002238 }
2239
2240 ct = find_command(argv[1]);
2241 if (ct == NULL) {
2242 printf("command %s not found\n", argv[1]);
Max Reitzb444d0e2018-05-09 21:42:58 +02002243 return;
Kevin Wolff18a8342013-06-05 14:19:33 +02002244 }
2245
2246 help_onecmd(argv[1], ct);
Kevin Wolff18a8342013-06-05 14:19:33 +02002247}
2248
2249static const cmdinfo_t help_cmd = {
2250 .name = "help",
2251 .altname = "?",
2252 .cfunc = help_f,
2253 .argmin = 0,
2254 .argmax = 1,
2255 .flags = CMD_FLAG_GLOBAL,
2256 .args = "[command]",
2257 .oneline = "help for one or all commands",
2258};
2259
Max Reitzb444d0e2018-05-09 21:42:58 +02002260void qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002261{
Paolo Bonzini15afd942016-10-27 12:49:03 +02002262 AioContext *ctx;
Kevin Wolfdd583292013-06-05 14:19:32 +02002263 char *input;
2264 const cmdinfo_t *ct;
2265 char **v;
2266 int c;
Kevin Wolfdd583292013-06-05 14:19:32 +02002267
2268 input = g_strdup(cmd);
2269 v = breakline(input, &c);
2270 if (c) {
2271 ct = find_command(v[0]);
2272 if (ct) {
Paolo Bonzini15afd942016-10-27 12:49:03 +02002273 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2274 aio_context_acquire(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02002275 command(blk, ct, c, v);
Paolo Bonzini15afd942016-10-27 12:49:03 +02002276 aio_context_release(ctx);
Kevin Wolfdd583292013-06-05 14:19:32 +02002277 } else {
2278 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2279 }
2280 }
2281 g_free(input);
2282 g_free(v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002283}
2284
Kevin Wolf797ac582013-06-05 14:19:31 +02002285static void __attribute((constructor)) init_qemuio_commands(void)
2286{
2287 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002288 qemuio_add_command(&help_cmd);
2289 qemuio_add_command(&read_cmd);
2290 qemuio_add_command(&readv_cmd);
2291 qemuio_add_command(&write_cmd);
2292 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002293 qemuio_add_command(&aio_read_cmd);
2294 qemuio_add_command(&aio_write_cmd);
2295 qemuio_add_command(&aio_flush_cmd);
2296 qemuio_add_command(&flush_cmd);
2297 qemuio_add_command(&truncate_cmd);
2298 qemuio_add_command(&length_cmd);
2299 qemuio_add_command(&info_cmd);
2300 qemuio_add_command(&discard_cmd);
2301 qemuio_add_command(&alloc_cmd);
2302 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002303 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002304 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002305 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002306 qemuio_add_command(&resume_cmd);
2307 qemuio_add_command(&wait_break_cmd);
2308 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002309 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002310 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002311}