]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-io-cmds.c
target/ppc: Eliminate htab_base and htab_mask variables
[mirror_qemu.git] / qemu-io-cmds.c
CommitLineData
797ac58c
KW
1/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
093ea232 4 * Copyright (C) 2009-2016 Red Hat, Inc.
797ac58c
KW
5 * 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
80c71a24 11#include "qemu/osdep.h"
da34e65c 12#include "qapi/error.h"
3d21994f 13#include "qemu-io.h"
4c7b7e9b
HR
14#include "sysemu/block-backend.h"
15#include "block/block.h"
16#include "block/block_int.h" /* for info_f() */
a8d8ecb7 17#include "block/qapi.h"
d49b6836 18#include "qemu/error-report.h"
6a1751b7 19#include "qemu/main-loop.h"
cd33d02a 20#include "qemu/timer.h"
f348b6d1 21#include "qemu/cutils.h"
797ac58c
KW
22
23#define CMD_NOFILE_OK 0x01
24
f9883880 25bool qemuio_misalign;
797ac58c 26
c2cdf5c5
KW
27static cmdinfo_t *cmdtab;
28static int ncmds;
29
30static int compare_cmdname(const void *a, const void *b)
31{
32 return strcmp(((const cmdinfo_t *)a)->name,
33 ((const cmdinfo_t *)b)->name);
34}
35
36void qemuio_add_command(const cmdinfo_t *ci)
37{
02c4f26b 38 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
c2cdf5c5
KW
39 cmdtab[ncmds - 1] = *ci;
40 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
41}
42
43int qemuio_command_usage(const cmdinfo_t *ci)
44{
45 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
46 return 0;
47}
48
4c7b7e9b 49static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
c2cdf5c5
KW
50{
51 if (ct->flags & CMD_FLAG_GLOBAL) {
52 return 1;
53 }
4c7b7e9b 54 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
c2cdf5c5
KW
55 fprintf(stderr, "no file open, try 'help open'\n");
56 return 0;
57 }
58 return 1;
59}
60
4c7b7e9b 61static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
3d21994f 62 char **argv)
c2cdf5c5
KW
63{
64 char *cmd = argv[0];
65
4c7b7e9b 66 if (!init_check_command(blk, ct)) {
c2cdf5c5
KW
67 return 0;
68 }
69
70 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
71 if (ct->argmax == -1) {
72 fprintf(stderr,
73 "bad argument count %d to %s, expected at least %d arguments\n",
74 argc-1, cmd, ct->argmin);
75 } else if (ct->argmin == ct->argmax) {
76 fprintf(stderr,
77 "bad argument count %d to %s, expected %d arguments\n",
78 argc-1, cmd, ct->argmin);
79 } else {
80 fprintf(stderr,
81 "bad argument count %d to %s, expected between %d and %d arguments\n",
82 argc-1, cmd, ct->argmin, ct->argmax);
83 }
84 return 0;
85 }
86 optind = 0;
4c7b7e9b 87 return ct->cfunc(blk, argc, argv);
c2cdf5c5
KW
88}
89
90static const cmdinfo_t *find_command(const char *cmd)
91{
92 cmdinfo_t *ct;
93
94 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
95 if (strcmp(ct->name, cmd) == 0 ||
96 (ct->altname && strcmp(ct->altname, cmd) == 0))
97 {
98 return (const cmdinfo_t *)ct;
99 }
100 }
101 return NULL;
102}
103
4694020d
SH
104/* Invoke fn() for commands with a matching prefix */
105void qemuio_complete_command(const char *input,
106 void (*fn)(const char *cmd, void *opaque),
107 void *opaque)
108{
109 cmdinfo_t *ct;
110 size_t input_len = strlen(input);
111
112 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
113 if (strncmp(input, ct->name, input_len) == 0) {
114 fn(ct->name, opaque);
115 }
116 }
117}
118
c2cdf5c5
KW
119static char **breakline(char *input, int *count)
120{
121 int c = 0;
122 char *p;
5839e53b 123 char **rval = g_new0(char *, 1);
c2cdf5c5
KW
124
125 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
126 if (!*p) {
127 continue;
128 }
129 c++;
08193dd5 130 rval = g_renew(char *, rval, (c + 1));
c2cdf5c5
KW
131 rval[c - 1] = p;
132 rval[c] = NULL;
133 }
134 *count = c;
135 return rval;
136}
137
797ac58c
KW
138static int64_t cvtnum(const char *s)
139{
f17fd4fd 140 int err;
f46bfdbf 141 uint64_t value;
ef5a7885 142
f17fd4fd
MA
143 err = qemu_strtosz(s, NULL, &value);
144 if (err < 0) {
145 return err;
146 }
f46bfdbf
MA
147 if (value > INT64_MAX) {
148 return -ERANGE;
149 }
f17fd4fd 150 return value;
797ac58c
KW
151}
152
a9ecfa00
JS
153static void print_cvtnum_err(int64_t rc, const char *arg)
154{
155 switch (rc) {
156 case -EINVAL:
157 printf("Parsing error: non-numeric argument,"
158 " or extraneous/unrecognized suffix -- %s\n", arg);
159 break;
160 case -ERANGE:
161 printf("Parsing error: argument too large -- %s\n", arg);
162 break;
163 default:
164 printf("Parsing error: %s\n", arg);
165 }
166}
167
0b613881
KW
168#define EXABYTES(x) ((long long)(x) << 60)
169#define PETABYTES(x) ((long long)(x) << 50)
170#define TERABYTES(x) ((long long)(x) << 40)
171#define GIGABYTES(x) ((long long)(x) << 30)
172#define MEGABYTES(x) ((long long)(x) << 20)
173#define KILOBYTES(x) ((long long)(x) << 10)
174
175#define TO_EXABYTES(x) ((x) / EXABYTES(1))
176#define TO_PETABYTES(x) ((x) / PETABYTES(1))
177#define TO_TERABYTES(x) ((x) / TERABYTES(1))
178#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
179#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
180#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
181
182static void cvtstr(double value, char *str, size_t size)
183{
184 char *trim;
185 const char *suffix;
186
187 if (value >= EXABYTES(1)) {
188 suffix = " EiB";
189 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
190 } else if (value >= PETABYTES(1)) {
191 suffix = " PiB";
192 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
193 } else if (value >= TERABYTES(1)) {
194 suffix = " TiB";
195 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
196 } else if (value >= GIGABYTES(1)) {
197 suffix = " GiB";
198 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
199 } else if (value >= MEGABYTES(1)) {
200 suffix = " MiB";
201 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
202 } else if (value >= KILOBYTES(1)) {
203 suffix = " KiB";
204 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
205 } else {
206 suffix = " bytes";
207 snprintf(str, size - 6, "%f", value);
208 }
209
210 trim = strstr(str, ".000");
211 if (trim) {
212 strcpy(trim, suffix);
213 } else {
214 strcat(str, suffix);
215 }
216}
217
218
219
220static struct timeval tsub(struct timeval t1, struct timeval t2)
221{
222 t1.tv_usec -= t2.tv_usec;
223 if (t1.tv_usec < 0) {
224 t1.tv_usec += 1000000;
225 t1.tv_sec--;
226 }
227 t1.tv_sec -= t2.tv_sec;
228 return t1;
229}
230
231static double tdiv(double value, struct timeval tv)
232{
233 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
234}
235
236#define HOURS(sec) ((sec) / (60 * 60))
237#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
238#define SECONDS(sec) ((sec) % 60)
239
240enum {
241 DEFAULT_TIME = 0x0,
242 TERSE_FIXED_TIME = 0x1,
243 VERBOSE_FIXED_TIME = 0x2,
244};
245
246static void timestr(struct timeval *tv, char *ts, size_t size, int format)
247{
248 double usec = (double)tv->tv_usec / 1000000.0;
249
250 if (format & TERSE_FIXED_TIME) {
251 if (!HOURS(tv->tv_sec)) {
252 snprintf(ts, size, "%u:%02u.%02u",
253 (unsigned int) MINUTES(tv->tv_sec),
254 (unsigned int) SECONDS(tv->tv_sec),
255 (unsigned int) (usec * 100));
256 return;
257 }
258 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
259 }
260
261 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
262 snprintf(ts, size, "%u:%02u:%02u.%02u",
263 (unsigned int) HOURS(tv->tv_sec),
264 (unsigned int) MINUTES(tv->tv_sec),
265 (unsigned int) SECONDS(tv->tv_sec),
266 (unsigned int) (usec * 100));
267 } else {
268 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
269 }
270}
271
797ac58c
KW
272/*
273 * Parse the pattern argument to various sub-commands.
274 *
275 * Because the pattern is used as an argument to memset it must evaluate
276 * to an unsigned integer that fits into a single byte.
277 */
278static int parse_pattern(const char *arg)
279{
280 char *endptr = NULL;
281 long pattern;
282
283 pattern = strtol(arg, &endptr, 0);
284 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
285 printf("%s is not a valid pattern byte\n", arg);
286 return -1;
287 }
288
289 return pattern;
290}
291
292/*
293 * Memory allocation helpers.
294 *
295 * Make sure memory is aligned by default, or purposefully misaligned if
296 * that is specified on the command line.
297 */
298
299#define MISALIGN_OFFSET 16
4c7b7e9b 300static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
797ac58c
KW
301{
302 void *buf;
303
304 if (qemuio_misalign) {
305 len += MISALIGN_OFFSET;
306 }
4c7b7e9b 307 buf = blk_blockalign(blk, len);
797ac58c
KW
308 memset(buf, pattern, len);
309 if (qemuio_misalign) {
310 buf += MISALIGN_OFFSET;
311 }
312 return buf;
313}
314
315static void qemu_io_free(void *p)
316{
317 if (qemuio_misalign) {
318 p -= MISALIGN_OFFSET;
319 }
320 qemu_vfree(p);
321}
322
9b0beaf3 323static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
797ac58c 324{
9b0beaf3
JS
325 uint64_t i;
326 int j;
797ac58c
KW
327 const uint8_t *p;
328
329 for (i = 0, p = buffer; i < len; i += 16) {
330 const uint8_t *s = p;
331
332 printf("%08" PRIx64 ": ", offset + i);
333 for (j = 0; j < 16 && i + j < len; j++, p++) {
334 printf("%02x ", *p);
335 }
336 printf(" ");
337 for (j = 0; j < 16 && i + j < len; j++, s++) {
338 if (isalnum(*s)) {
339 printf("%c", *s);
340 } else {
341 printf(".");
342 }
343 }
344 printf("\n");
345 }
346}
347
348static void print_report(const char *op, struct timeval *t, int64_t offset,
dc38852a 349 int64_t count, int64_t total, int cnt, bool Cflag)
797ac58c
KW
350{
351 char s1[64], s2[64], ts[64];
352
353 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
354 if (!Cflag) {
355 cvtstr((double)total, s1, sizeof(s1));
356 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
9b0beaf3 357 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
797ac58c
KW
358 op, total, count, offset);
359 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
360 s1, cnt, ts, s2, tdiv((double)cnt, *t));
361 } else {/* bytes,ops,time,bytes/sec,ops/sec */
9b0beaf3 362 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
797ac58c
KW
363 total, cnt, ts,
364 tdiv((double)total, *t),
365 tdiv((double)cnt, *t));
366 }
367}
368
369/*
370 * Parse multiple length statements for vectored I/O, and construct an I/O
371 * vector matching it.
372 */
373static void *
4c7b7e9b 374create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
797ac58c
KW
375 int pattern)
376{
377 size_t *sizes = g_new0(size_t, nr_iov);
378 size_t count = 0;
379 void *buf = NULL;
380 void *p;
381 int i;
382
383 for (i = 0; i < nr_iov; i++) {
384 char *arg = argv[i];
385 int64_t len;
386
387 len = cvtnum(arg);
388 if (len < 0) {
a9ecfa00 389 print_cvtnum_err(len, arg);
797ac58c
KW
390 goto fail;
391 }
392
3026c468
AG
393 if (len > BDRV_REQUEST_MAX_BYTES) {
394 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
395 (uint64_t)BDRV_REQUEST_MAX_BYTES);
396 goto fail;
397 }
398
399 if (count > BDRV_REQUEST_MAX_BYTES - len) {
400 printf("The total number of bytes exceed the maximum size %" PRIu64
401 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
797ac58c
KW
402 goto fail;
403 }
404
797ac58c
KW
405 sizes[i] = len;
406 count += len;
407 }
408
409 qemu_iovec_init(qiov, nr_iov);
410
4c7b7e9b 411 buf = p = qemu_io_alloc(blk, count, pattern);
797ac58c
KW
412
413 for (i = 0; i < nr_iov; i++) {
414 qemu_iovec_add(qiov, p, sizes[i]);
415 p += sizes[i];
416 }
417
418fail:
419 g_free(sizes);
420 return buf;
421}
422
9b0beaf3
JS
423static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
424 int64_t count, int64_t *total)
797ac58c 425{
9b0beaf3
JS
426 if (count > INT_MAX) {
427 return -ERANGE;
428 }
429
4c7b7e9b 430 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
797ac58c
KW
431 if (*total < 0) {
432 return *total;
433 }
434 return 1;
435}
436
9b0beaf3 437static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
770e0e0e 438 int64_t count, int flags, int64_t *total)
797ac58c 439{
9b0beaf3
JS
440 if (count > INT_MAX) {
441 return -ERANGE;
442 }
443
770e0e0e 444 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, flags);
797ac58c
KW
445 if (*total < 0) {
446 return *total;
447 }
448 return 1;
449}
450
451typedef struct {
4c7b7e9b 452 BlockBackend *blk;
797ac58c 453 int64_t offset;
9b0beaf3
JS
454 int64_t count;
455 int64_t *total;
770e0e0e 456 int flags;
797ac58c
KW
457 int ret;
458 bool done;
459} CoWriteZeroes;
460
d004bd52 461static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
797ac58c
KW
462{
463 CoWriteZeroes *data = opaque;
464
d004bd52
EB
465 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->count,
466 data->flags);
797ac58c
KW
467 data->done = true;
468 if (data->ret < 0) {
469 *data->total = data->ret;
470 return;
471 }
472
473 *data->total = data->count;
474}
475
d004bd52
EB
476static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
477 int64_t count, int flags, int64_t *total)
797ac58c
KW
478{
479 Coroutine *co;
480 CoWriteZeroes data = {
4c7b7e9b 481 .blk = blk,
797ac58c
KW
482 .offset = offset,
483 .count = count,
484 .total = total,
770e0e0e 485 .flags = flags,
797ac58c
KW
486 .done = false,
487 };
488
a3674679 489 if (count > INT_MAX) {
9b0beaf3
JS
490 return -ERANGE;
491 }
492
0b8b8753
PB
493 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
494 qemu_coroutine_enter(co);
797ac58c 495 while (!data.done) {
4c7b7e9b 496 aio_poll(blk_get_aio_context(blk), true);
797ac58c
KW
497 }
498 if (data.ret < 0) {
499 return data.ret;
500 } else {
501 return 1;
502 }
503}
504
4c7b7e9b 505static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
9b0beaf3 506 int64_t count, int64_t *total)
797ac58c
KW
507{
508 int ret;
509
a3674679 510 if (count >> 9 > BDRV_REQUEST_MAX_SECTORS) {
9b0beaf3
JS
511 return -ERANGE;
512 }
513
fe5c1355 514 ret = blk_pwrite_compressed(blk, offset, buf, count);
797ac58c
KW
515 if (ret < 0) {
516 return ret;
517 }
518 *total = count;
519 return 1;
520}
521
4c7b7e9b 522static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
9b0beaf3 523 int64_t count, int64_t *total)
797ac58c 524{
9b0beaf3
JS
525 if (count > INT_MAX) {
526 return -ERANGE;
527 }
528
4c7b7e9b 529 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
797ac58c
KW
530 if (*total < 0) {
531 return *total;
532 }
533 return 1;
534}
535
4c7b7e9b 536static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
9b0beaf3 537 int64_t count, int64_t *total)
797ac58c 538{
9b0beaf3
JS
539 if (count > INT_MAX) {
540 return -ERANGE;
541 }
542
4c7b7e9b 543 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
797ac58c
KW
544 if (*total < 0) {
545 return *total;
546 }
547 return 1;
548}
549
550#define NOT_DONE 0x7fffffff
551static void aio_rw_done(void *opaque, int ret)
552{
553 *(int *)opaque = ret;
554}
555
4c7b7e9b 556static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
797ac58c
KW
557 int64_t offset, int *total)
558{
559 int async_ret = NOT_DONE;
560
7b3f9712 561 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
797ac58c
KW
562 while (async_ret == NOT_DONE) {
563 main_loop_wait(false);
564 }
565
566 *total = qiov->size;
567 return async_ret < 0 ? async_ret : 1;
568}
569
4c7b7e9b 570static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
770e0e0e 571 int64_t offset, int flags, int *total)
797ac58c
KW
572{
573 int async_ret = NOT_DONE;
574
770e0e0e 575 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
797ac58c
KW
576 while (async_ret == NOT_DONE) {
577 main_loop_wait(false);
578 }
579
580 *total = qiov->size;
581 return async_ret < 0 ? async_ret : 1;
582}
583
797ac58c
KW
584static void read_help(void)
585{
586 printf(
587"\n"
588" reads a range of bytes from the given offset\n"
589"\n"
590" Example:\n"
591" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
592"\n"
593" Reads a segment of the currently open file, optionally dumping it to the\n"
594" standard output stream (with -v option) for subsequent inspection.\n"
595" -b, -- read from the VM state rather than the virtual disk\n"
596" -C, -- report statistics in a machine parsable format\n"
597" -l, -- length for pattern verification (only with -P)\n"
093ea232 598" -p, -- ignored for backwards compatibility\n"
797ac58c
KW
599" -P, -- use a pattern to verify read data\n"
600" -q, -- quiet mode, do not show I/O statistics\n"
601" -s, -- start offset for pattern verification (only with -P)\n"
602" -v, -- dump buffer to standard output\n"
603"\n");
604}
605
4c7b7e9b 606static int read_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
607
608static const cmdinfo_t read_cmd = {
609 .name = "read",
610 .altname = "r",
611 .cfunc = read_f,
612 .argmin = 2,
613 .argmax = -1,
093ea232 614 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
797ac58c
KW
615 .oneline = "reads a number of bytes at a specified offset",
616 .help = read_help,
617};
618
4c7b7e9b 619static int read_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
620{
621 struct timeval t1, t2;
093ea232 622 bool Cflag = false, qflag = false, vflag = false;
dc38852a 623 bool Pflag = false, sflag = false, lflag = false, bflag = false;
797ac58c
KW
624 int c, cnt;
625 char *buf;
626 int64_t offset;
9b0beaf3 627 int64_t count;
797ac58c 628 /* Some compilers get confused and warn if this is not initialized. */
9b0beaf3
JS
629 int64_t total = 0;
630 int pattern = 0;
631 int64_t pattern_offset = 0, pattern_count = 0;
797ac58c 632
b062ad86 633 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
797ac58c
KW
634 switch (c) {
635 case 'b':
dc38852a 636 bflag = true;
797ac58c
KW
637 break;
638 case 'C':
dc38852a 639 Cflag = true;
797ac58c
KW
640 break;
641 case 'l':
dc38852a 642 lflag = true;
797ac58c
KW
643 pattern_count = cvtnum(optarg);
644 if (pattern_count < 0) {
a9ecfa00 645 print_cvtnum_err(pattern_count, optarg);
797ac58c
KW
646 return 0;
647 }
648 break;
649 case 'p':
093ea232 650 /* Ignored for backwards compatibility */
797ac58c
KW
651 break;
652 case 'P':
dc38852a 653 Pflag = true;
797ac58c
KW
654 pattern = parse_pattern(optarg);
655 if (pattern < 0) {
656 return 0;
657 }
658 break;
659 case 'q':
dc38852a 660 qflag = true;
797ac58c
KW
661 break;
662 case 's':
dc38852a 663 sflag = true;
797ac58c
KW
664 pattern_offset = cvtnum(optarg);
665 if (pattern_offset < 0) {
a9ecfa00 666 print_cvtnum_err(pattern_offset, optarg);
797ac58c
KW
667 return 0;
668 }
669 break;
670 case 'v':
dc38852a 671 vflag = true;
797ac58c
KW
672 break;
673 default:
c2cdf5c5 674 return qemuio_command_usage(&read_cmd);
797ac58c
KW
675 }
676 }
677
678 if (optind != argc - 2) {
c2cdf5c5 679 return qemuio_command_usage(&read_cmd);
797ac58c
KW
680 }
681
797ac58c
KW
682 offset = cvtnum(argv[optind]);
683 if (offset < 0) {
a9ecfa00 684 print_cvtnum_err(offset, argv[optind]);
797ac58c
KW
685 return 0;
686 }
687
688 optind++;
689 count = cvtnum(argv[optind]);
690 if (count < 0) {
a9ecfa00 691 print_cvtnum_err(count, argv[optind]);
797ac58c 692 return 0;
3026c468 693 } else if (count > BDRV_REQUEST_MAX_BYTES) {
9b0beaf3 694 printf("length cannot exceed %" PRIu64 ", given %s\n",
3026c468 695 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
9b0beaf3 696 return 0;
797ac58c
KW
697 }
698
699 if (!Pflag && (lflag || sflag)) {
c2cdf5c5 700 return qemuio_command_usage(&read_cmd);
797ac58c
KW
701 }
702
703 if (!lflag) {
704 pattern_count = count - pattern_offset;
705 }
706
707 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
708 printf("pattern verification range exceeds end of read data\n");
709 return 0;
710 }
711
093ea232 712 if (bflag) {
797ac58c
KW
713 if (offset & 0x1ff) {
714 printf("offset %" PRId64 " is not sector aligned\n",
715 offset);
716 return 0;
717 }
718 if (count & 0x1ff) {
9b0beaf3 719 printf("count %"PRId64" is not sector aligned\n",
797ac58c
KW
720 count);
721 return 0;
722 }
723 }
724
4c7b7e9b 725 buf = qemu_io_alloc(blk, count, 0xab);
797ac58c
KW
726
727 gettimeofday(&t1, NULL);
7b3f9712 728 if (bflag) {
4c7b7e9b 729 cnt = do_load_vmstate(blk, buf, offset, count, &total);
797ac58c 730 } else {
7b3f9712 731 cnt = do_pread(blk, buf, offset, count, &total);
797ac58c
KW
732 }
733 gettimeofday(&t2, NULL);
734
735 if (cnt < 0) {
736 printf("read failed: %s\n", strerror(-cnt));
737 goto out;
738 }
739
740 if (Pflag) {
741 void *cmp_buf = g_malloc(pattern_count);
742 memset(cmp_buf, pattern, pattern_count);
743 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
744 printf("Pattern verification failed at offset %"
9b0beaf3 745 PRId64 ", %"PRId64" bytes\n",
797ac58c
KW
746 offset + pattern_offset, pattern_count);
747 }
748 g_free(cmp_buf);
749 }
750
751 if (qflag) {
752 goto out;
753 }
754
755 if (vflag) {
756 dump_buffer(buf, offset, count);
757 }
758
759 /* Finally, report back -- -C gives a parsable format */
760 t2 = tsub(t2, t1);
761 print_report("read", &t2, offset, count, total, cnt, Cflag);
762
763out:
764 qemu_io_free(buf);
765
766 return 0;
767}
768
769static void readv_help(void)
770{
771 printf(
772"\n"
773" reads a range of bytes from the given offset into multiple buffers\n"
774"\n"
775" Example:\n"
776" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
777"\n"
778" Reads a segment of the currently open file, optionally dumping it to the\n"
779" standard output stream (with -v option) for subsequent inspection.\n"
780" Uses multiple iovec buffers if more than one byte range is specified.\n"
781" -C, -- report statistics in a machine parsable format\n"
782" -P, -- use a pattern to verify read data\n"
783" -v, -- dump buffer to standard output\n"
784" -q, -- quiet mode, do not show I/O statistics\n"
785"\n");
786}
787
4c7b7e9b 788static int readv_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
789
790static const cmdinfo_t readv_cmd = {
791 .name = "readv",
792 .cfunc = readv_f,
793 .argmin = 2,
794 .argmax = -1,
093ea232 795 .args = "[-Cqv] [-P pattern] off len [len..]",
797ac58c
KW
796 .oneline = "reads a number of bytes at a specified offset",
797 .help = readv_help,
798};
799
4c7b7e9b 800static int readv_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
801{
802 struct timeval t1, t2;
dc38852a 803 bool Cflag = false, qflag = false, vflag = false;
797ac58c
KW
804 int c, cnt;
805 char *buf;
806 int64_t offset;
807 /* Some compilers get confused and warn if this is not initialized. */
808 int total = 0;
809 int nr_iov;
810 QEMUIOVector qiov;
811 int pattern = 0;
dc38852a 812 bool Pflag = false;
797ac58c 813
b062ad86 814 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
797ac58c
KW
815 switch (c) {
816 case 'C':
dc38852a 817 Cflag = true;
797ac58c
KW
818 break;
819 case 'P':
dc38852a 820 Pflag = true;
797ac58c
KW
821 pattern = parse_pattern(optarg);
822 if (pattern < 0) {
823 return 0;
824 }
825 break;
826 case 'q':
dc38852a 827 qflag = true;
797ac58c
KW
828 break;
829 case 'v':
dc38852a 830 vflag = true;
797ac58c
KW
831 break;
832 default:
c2cdf5c5 833 return qemuio_command_usage(&readv_cmd);
797ac58c
KW
834 }
835 }
836
837 if (optind > argc - 2) {
c2cdf5c5 838 return qemuio_command_usage(&readv_cmd);
797ac58c
KW
839 }
840
841
842 offset = cvtnum(argv[optind]);
843 if (offset < 0) {
a9ecfa00 844 print_cvtnum_err(offset, argv[optind]);
797ac58c
KW
845 return 0;
846 }
847 optind++;
848
797ac58c 849 nr_iov = argc - optind;
4c7b7e9b 850 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
797ac58c
KW
851 if (buf == NULL) {
852 return 0;
853 }
854
855 gettimeofday(&t1, NULL);
4c7b7e9b 856 cnt = do_aio_readv(blk, &qiov, offset, &total);
797ac58c
KW
857 gettimeofday(&t2, NULL);
858
859 if (cnt < 0) {
860 printf("readv failed: %s\n", strerror(-cnt));
861 goto out;
862 }
863
864 if (Pflag) {
865 void *cmp_buf = g_malloc(qiov.size);
866 memset(cmp_buf, pattern, qiov.size);
867 if (memcmp(buf, cmp_buf, qiov.size)) {
868 printf("Pattern verification failed at offset %"
869 PRId64 ", %zd bytes\n", offset, qiov.size);
870 }
871 g_free(cmp_buf);
872 }
873
874 if (qflag) {
875 goto out;
876 }
877
878 if (vflag) {
879 dump_buffer(buf, offset, qiov.size);
880 }
881
882 /* Finally, report back -- -C gives a parsable format */
883 t2 = tsub(t2, t1);
884 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
885
886out:
887 qemu_iovec_destroy(&qiov);
888 qemu_io_free(buf);
889 return 0;
890}
891
892static void write_help(void)
893{
894 printf(
895"\n"
896" writes a range of bytes from the given offset\n"
897"\n"
898" Example:\n"
899" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
900"\n"
901" Writes into a segment of the currently open file, using a buffer\n"
902" filled with a set pattern (0xcdcdcdcd).\n"
903" -b, -- write to the VM state rather than the virtual disk\n"
4c7b7e9b 904" -c, -- write compressed data with blk_write_compressed\n"
770e0e0e 905" -f, -- use Force Unit Access semantics\n"
093ea232 906" -p, -- ignored for backwards compatibility\n"
797ac58c
KW
907" -P, -- use different pattern to fill file\n"
908" -C, -- report statistics in a machine parsable format\n"
909" -q, -- quiet mode, do not show I/O statistics\n"
c2e001cc 910" -u, -- with -z, allow unmapping\n"
d004bd52 911" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
797ac58c
KW
912"\n");
913}
914
4c7b7e9b 915static int write_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
916
917static const cmdinfo_t write_cmd = {
918 .name = "write",
919 .altname = "w",
920 .cfunc = write_f,
921 .argmin = 2,
922 .argmax = -1,
c2e001cc 923 .args = "[-bcCfquz] [-P pattern] off len",
797ac58c
KW
924 .oneline = "writes a number of bytes at a specified offset",
925 .help = write_help,
926};
927
4c7b7e9b 928static int write_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
929{
930 struct timeval t1, t2;
093ea232 931 bool Cflag = false, qflag = false, bflag = false;
dc38852a 932 bool Pflag = false, zflag = false, cflag = false;
770e0e0e 933 int flags = 0;
797ac58c
KW
934 int c, cnt;
935 char *buf = NULL;
936 int64_t offset;
9b0beaf3 937 int64_t count;
797ac58c 938 /* Some compilers get confused and warn if this is not initialized. */
9b0beaf3 939 int64_t total = 0;
797ac58c
KW
940 int pattern = 0xcd;
941
c2e001cc 942 while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
797ac58c
KW
943 switch (c) {
944 case 'b':
dc38852a 945 bflag = true;
797ac58c
KW
946 break;
947 case 'c':
dc38852a 948 cflag = true;
797ac58c
KW
949 break;
950 case 'C':
dc38852a 951 Cflag = true;
797ac58c 952 break;
770e0e0e
EB
953 case 'f':
954 flags |= BDRV_REQ_FUA;
955 break;
797ac58c 956 case 'p':
093ea232 957 /* Ignored for backwards compatibility */
797ac58c
KW
958 break;
959 case 'P':
dc38852a 960 Pflag = true;
797ac58c
KW
961 pattern = parse_pattern(optarg);
962 if (pattern < 0) {
963 return 0;
964 }
965 break;
966 case 'q':
dc38852a 967 qflag = true;
797ac58c 968 break;
c2e001cc
EB
969 case 'u':
970 flags |= BDRV_REQ_MAY_UNMAP;
971 break;
797ac58c 972 case 'z':
dc38852a 973 zflag = true;
797ac58c
KW
974 break;
975 default:
c2cdf5c5 976 return qemuio_command_usage(&write_cmd);
797ac58c
KW
977 }
978 }
979
980 if (optind != argc - 2) {
c2cdf5c5 981 return qemuio_command_usage(&write_cmd);
797ac58c
KW
982 }
983
093ea232
EB
984 if (bflag && zflag) {
985 printf("-b and -z cannot be specified at the same time\n");
797ac58c
KW
986 return 0;
987 }
988
770e0e0e
EB
989 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
990 printf("-f and -b or -c cannot be specified at the same time\n");
991 return 0;
992 }
993
c2e001cc
EB
994 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
995 printf("-u requires -z to be specified\n");
996 return 0;
997 }
998
797ac58c
KW
999 if (zflag && Pflag) {
1000 printf("-z and -P cannot be specified at the same time\n");
1001 return 0;
1002 }
1003
1004 offset = cvtnum(argv[optind]);
1005 if (offset < 0) {
a9ecfa00 1006 print_cvtnum_err(offset, argv[optind]);
797ac58c
KW
1007 return 0;
1008 }
1009
1010 optind++;
1011 count = cvtnum(argv[optind]);
1012 if (count < 0) {
a9ecfa00 1013 print_cvtnum_err(count, argv[optind]);
797ac58c 1014 return 0;
3026c468 1015 } else if (count > BDRV_REQUEST_MAX_BYTES) {
9b0beaf3 1016 printf("length cannot exceed %" PRIu64 ", given %s\n",
3026c468 1017 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
9b0beaf3 1018 return 0;
797ac58c
KW
1019 }
1020
093ea232 1021 if (bflag || cflag) {
797ac58c
KW
1022 if (offset & 0x1ff) {
1023 printf("offset %" PRId64 " is not sector aligned\n",
1024 offset);
1025 return 0;
1026 }
1027
1028 if (count & 0x1ff) {
9b0beaf3 1029 printf("count %"PRId64" is not sector aligned\n",
797ac58c
KW
1030 count);
1031 return 0;
1032 }
1033 }
1034
1035 if (!zflag) {
4c7b7e9b 1036 buf = qemu_io_alloc(blk, count, pattern);
797ac58c
KW
1037 }
1038
1039 gettimeofday(&t1, NULL);
7b3f9712 1040 if (bflag) {
4c7b7e9b 1041 cnt = do_save_vmstate(blk, buf, offset, count, &total);
797ac58c 1042 } else if (zflag) {
d004bd52 1043 cnt = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
797ac58c 1044 } else if (cflag) {
4c7b7e9b 1045 cnt = do_write_compressed(blk, buf, offset, count, &total);
797ac58c 1046 } else {
770e0e0e 1047 cnt = do_pwrite(blk, buf, offset, count, flags, &total);
797ac58c
KW
1048 }
1049 gettimeofday(&t2, NULL);
1050
1051 if (cnt < 0) {
1052 printf("write failed: %s\n", strerror(-cnt));
1053 goto out;
1054 }
1055
1056 if (qflag) {
1057 goto out;
1058 }
1059
1060 /* Finally, report back -- -C gives a parsable format */
1061 t2 = tsub(t2, t1);
1062 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1063
1064out:
1065 if (!zflag) {
1066 qemu_io_free(buf);
1067 }
1068
1069 return 0;
1070}
1071
1072static void
1073writev_help(void)
1074{
1075 printf(
1076"\n"
1077" writes a range of bytes from the given offset source from multiple buffers\n"
1078"\n"
1079" Example:\n"
6e6507c0 1080" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
797ac58c
KW
1081"\n"
1082" Writes into a segment of the currently open file, using a buffer\n"
1083" filled with a set pattern (0xcdcdcdcd).\n"
1084" -P, -- use different pattern to fill file\n"
1085" -C, -- report statistics in a machine parsable format\n"
770e0e0e 1086" -f, -- use Force Unit Access semantics\n"
797ac58c
KW
1087" -q, -- quiet mode, do not show I/O statistics\n"
1088"\n");
1089}
1090
4c7b7e9b 1091static int writev_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
1092
1093static const cmdinfo_t writev_cmd = {
1094 .name = "writev",
1095 .cfunc = writev_f,
1096 .argmin = 2,
1097 .argmax = -1,
770e0e0e 1098 .args = "[-Cfq] [-P pattern] off len [len..]",
797ac58c
KW
1099 .oneline = "writes a number of bytes at a specified offset",
1100 .help = writev_help,
1101};
1102
4c7b7e9b 1103static int writev_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1104{
1105 struct timeval t1, t2;
dc38852a 1106 bool Cflag = false, qflag = false;
770e0e0e 1107 int flags = 0;
797ac58c
KW
1108 int c, cnt;
1109 char *buf;
1110 int64_t offset;
1111 /* Some compilers get confused and warn if this is not initialized. */
1112 int total = 0;
1113 int nr_iov;
1114 int pattern = 0xcd;
1115 QEMUIOVector qiov;
1116
4ca1d340 1117 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
797ac58c
KW
1118 switch (c) {
1119 case 'C':
dc38852a 1120 Cflag = true;
797ac58c 1121 break;
770e0e0e
EB
1122 case 'f':
1123 flags |= BDRV_REQ_FUA;
1124 break;
797ac58c 1125 case 'q':
dc38852a 1126 qflag = true;
797ac58c
KW
1127 break;
1128 case 'P':
1129 pattern = parse_pattern(optarg);
1130 if (pattern < 0) {
1131 return 0;
1132 }
1133 break;
1134 default:
c2cdf5c5 1135 return qemuio_command_usage(&writev_cmd);
797ac58c
KW
1136 }
1137 }
1138
1139 if (optind > argc - 2) {
c2cdf5c5 1140 return qemuio_command_usage(&writev_cmd);
797ac58c
KW
1141 }
1142
1143 offset = cvtnum(argv[optind]);
1144 if (offset < 0) {
a9ecfa00 1145 print_cvtnum_err(offset, argv[optind]);
797ac58c
KW
1146 return 0;
1147 }
1148 optind++;
1149
797ac58c 1150 nr_iov = argc - optind;
4c7b7e9b 1151 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
797ac58c
KW
1152 if (buf == NULL) {
1153 return 0;
1154 }
1155
1156 gettimeofday(&t1, NULL);
770e0e0e 1157 cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
797ac58c
KW
1158 gettimeofday(&t2, NULL);
1159
1160 if (cnt < 0) {
1161 printf("writev failed: %s\n", strerror(-cnt));
1162 goto out;
1163 }
1164
1165 if (qflag) {
1166 goto out;
1167 }
1168
1169 /* Finally, report back -- -C gives a parsable format */
1170 t2 = tsub(t2, t1);
1171 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1172out:
1173 qemu_iovec_destroy(&qiov);
1174 qemu_io_free(buf);
1175 return 0;
1176}
1177
797ac58c 1178struct aio_ctx {
4c7b7e9b 1179 BlockBackend *blk;
797ac58c
KW
1180 QEMUIOVector qiov;
1181 int64_t offset;
1182 char *buf;
dc38852a
EB
1183 bool qflag;
1184 bool vflag;
1185 bool Cflag;
1186 bool Pflag;
1187 bool zflag;
a91f9584 1188 BlockAcctCookie acct;
797ac58c
KW
1189 int pattern;
1190 struct timeval t1;
1191};
1192
1193static void aio_write_done(void *opaque, int ret)
1194{
1195 struct aio_ctx *ctx = opaque;
1196 struct timeval t2;
1197
1198 gettimeofday(&t2, NULL);
1199
1200
1201 if (ret < 0) {
1202 printf("aio_write failed: %s\n", strerror(-ret));
556c2b60 1203 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
797ac58c
KW
1204 goto out;
1205 }
1206
4c7b7e9b 1207 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
a91f9584 1208
797ac58c
KW
1209 if (ctx->qflag) {
1210 goto out;
1211 }
1212
1213 /* Finally, report back -- -C gives a parsable format */
1214 t2 = tsub(t2, ctx->t1);
1215 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1216 ctx->qiov.size, 1, ctx->Cflag);
1217out:
5ceb7765
KW
1218 if (!ctx->zflag) {
1219 qemu_io_free(ctx->buf);
1220 qemu_iovec_destroy(&ctx->qiov);
1221 }
797ac58c
KW
1222 g_free(ctx);
1223}
1224
1225static void aio_read_done(void *opaque, int ret)
1226{
1227 struct aio_ctx *ctx = opaque;
1228 struct timeval t2;
1229
1230 gettimeofday(&t2, NULL);
1231
1232 if (ret < 0) {
1233 printf("readv failed: %s\n", strerror(-ret));
556c2b60 1234 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
797ac58c
KW
1235 goto out;
1236 }
1237
1238 if (ctx->Pflag) {
1239 void *cmp_buf = g_malloc(ctx->qiov.size);
1240
1241 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1242 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1243 printf("Pattern verification failed at offset %"
1244 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1245 }
1246 g_free(cmp_buf);
1247 }
1248
4c7b7e9b 1249 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
a91f9584 1250
797ac58c
KW
1251 if (ctx->qflag) {
1252 goto out;
1253 }
1254
1255 if (ctx->vflag) {
1256 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1257 }
1258
1259 /* Finally, report back -- -C gives a parsable format */
1260 t2 = tsub(t2, ctx->t1);
1261 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1262 ctx->qiov.size, 1, ctx->Cflag);
1263out:
1264 qemu_io_free(ctx->buf);
1265 qemu_iovec_destroy(&ctx->qiov);
1266 g_free(ctx);
1267}
1268
1269static void aio_read_help(void)
1270{
1271 printf(
1272"\n"
1273" asynchronously reads a range of bytes from the given offset\n"
1274"\n"
1275" Example:\n"
1276" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1277"\n"
1278" Reads a segment of the currently open file, optionally dumping it to the\n"
1279" standard output stream (with -v option) for subsequent inspection.\n"
1280" The read is performed asynchronously and the aio_flush command must be\n"
1281" used to ensure all outstanding aio requests have been completed.\n"
1282" -C, -- report statistics in a machine parsable format\n"
1283" -P, -- use a pattern to verify read data\n"
37546ff2 1284" -i, -- treat request as invalid, for exercising stats\n"
797ac58c
KW
1285" -v, -- dump buffer to standard output\n"
1286" -q, -- quiet mode, do not show I/O statistics\n"
1287"\n");
1288}
1289
4c7b7e9b 1290static int aio_read_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
1291
1292static const cmdinfo_t aio_read_cmd = {
1293 .name = "aio_read",
1294 .cfunc = aio_read_f,
1295 .argmin = 2,
1296 .argmax = -1,
37546ff2 1297 .args = "[-Ciqv] [-P pattern] off len [len..]",
797ac58c
KW
1298 .oneline = "asynchronously reads a number of bytes",
1299 .help = aio_read_help,
1300};
1301
4c7b7e9b 1302static int aio_read_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1303{
1304 int nr_iov, c;
1305 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1306
4c7b7e9b 1307 ctx->blk = blk;
37546ff2 1308 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
797ac58c
KW
1309 switch (c) {
1310 case 'C':
dc38852a 1311 ctx->Cflag = true;
797ac58c
KW
1312 break;
1313 case 'P':
dc38852a 1314 ctx->Pflag = true;
797ac58c
KW
1315 ctx->pattern = parse_pattern(optarg);
1316 if (ctx->pattern < 0) {
1317 g_free(ctx);
1318 return 0;
1319 }
1320 break;
37546ff2
EB
1321 case 'i':
1322 printf("injecting invalid read request\n");
1323 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1324 g_free(ctx);
1325 return 0;
797ac58c 1326 case 'q':
dc38852a 1327 ctx->qflag = true;
797ac58c
KW
1328 break;
1329 case 'v':
dc38852a 1330 ctx->vflag = true;
797ac58c
KW
1331 break;
1332 default:
1333 g_free(ctx);
c2cdf5c5 1334 return qemuio_command_usage(&aio_read_cmd);
797ac58c
KW
1335 }
1336 }
1337
1338 if (optind > argc - 2) {
1339 g_free(ctx);
c2cdf5c5 1340 return qemuio_command_usage(&aio_read_cmd);
797ac58c
KW
1341 }
1342
1343 ctx->offset = cvtnum(argv[optind]);
1344 if (ctx->offset < 0) {
a9ecfa00 1345 print_cvtnum_err(ctx->offset, argv[optind]);
797ac58c
KW
1346 g_free(ctx);
1347 return 0;
1348 }
1349 optind++;
1350
797ac58c 1351 nr_iov = argc - optind;
4c7b7e9b 1352 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
797ac58c 1353 if (ctx->buf == NULL) {
556c2b60 1354 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
797ac58c
KW
1355 g_free(ctx);
1356 return 0;
1357 }
1358
1359 gettimeofday(&ctx->t1, NULL);
4c7b7e9b
HR
1360 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1361 BLOCK_ACCT_READ);
7b3f9712 1362 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
797ac58c
KW
1363 return 0;
1364}
1365
1366static void aio_write_help(void)
1367{
1368 printf(
1369"\n"
1370" asynchronously writes a range of bytes from the given offset source\n"
1371" from multiple buffers\n"
1372"\n"
1373" Example:\n"
1374" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1375"\n"
1376" Writes into a segment of the currently open file, using a buffer\n"
1377" filled with a set pattern (0xcdcdcdcd).\n"
1378" The write is performed asynchronously and the aio_flush command must be\n"
1379" used to ensure all outstanding aio requests have been completed.\n"
1380" -P, -- use different pattern to fill file\n"
1381" -C, -- report statistics in a machine parsable format\n"
770e0e0e 1382" -f, -- use Force Unit Access semantics\n"
37546ff2 1383" -i, -- treat request as invalid, for exercising stats\n"
797ac58c 1384" -q, -- quiet mode, do not show I/O statistics\n"
c2e001cc 1385" -u, -- with -z, allow unmapping\n"
d004bd52 1386" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
797ac58c
KW
1387"\n");
1388}
1389
4c7b7e9b 1390static int aio_write_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
1391
1392static const cmdinfo_t aio_write_cmd = {
1393 .name = "aio_write",
1394 .cfunc = aio_write_f,
1395 .argmin = 2,
1396 .argmax = -1,
37546ff2 1397 .args = "[-Cfiquz] [-P pattern] off len [len..]",
797ac58c
KW
1398 .oneline = "asynchronously writes a number of bytes",
1399 .help = aio_write_help,
1400};
1401
4c7b7e9b 1402static int aio_write_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1403{
1404 int nr_iov, c;
1405 int pattern = 0xcd;
1406 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
770e0e0e 1407 int flags = 0;
797ac58c 1408
4c7b7e9b 1409 ctx->blk = blk;
37546ff2 1410 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
797ac58c
KW
1411 switch (c) {
1412 case 'C':
dc38852a 1413 ctx->Cflag = true;
797ac58c 1414 break;
770e0e0e
EB
1415 case 'f':
1416 flags |= BDRV_REQ_FUA;
1417 break;
797ac58c 1418 case 'q':
dc38852a 1419 ctx->qflag = true;
797ac58c 1420 break;
c2e001cc
EB
1421 case 'u':
1422 flags |= BDRV_REQ_MAY_UNMAP;
1423 break;
797ac58c
KW
1424 case 'P':
1425 pattern = parse_pattern(optarg);
1426 if (pattern < 0) {
1427 g_free(ctx);
1428 return 0;
1429 }
1430 break;
37546ff2
EB
1431 case 'i':
1432 printf("injecting invalid write request\n");
1433 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1434 g_free(ctx);
1435 return 0;
5ceb7765 1436 case 'z':
dc38852a 1437 ctx->zflag = true;
5ceb7765 1438 break;
797ac58c
KW
1439 default:
1440 g_free(ctx);
c2cdf5c5 1441 return qemuio_command_usage(&aio_write_cmd);
797ac58c
KW
1442 }
1443 }
1444
1445 if (optind > argc - 2) {
1446 g_free(ctx);
c2cdf5c5 1447 return qemuio_command_usage(&aio_write_cmd);
797ac58c
KW
1448 }
1449
5ceb7765
KW
1450 if (ctx->zflag && optind != argc - 2) {
1451 printf("-z supports only a single length parameter\n");
1452 g_free(ctx);
1453 return 0;
1454 }
1455
c2e001cc
EB
1456 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1457 printf("-u requires -z to be specified\n");
4ca1d340 1458 g_free(ctx);
c2e001cc
EB
1459 return 0;
1460 }
1461
5ceb7765
KW
1462 if (ctx->zflag && ctx->Pflag) {
1463 printf("-z and -P cannot be specified at the same time\n");
1464 g_free(ctx);
1465 return 0;
1466 }
1467
797ac58c
KW
1468 ctx->offset = cvtnum(argv[optind]);
1469 if (ctx->offset < 0) {
a9ecfa00 1470 print_cvtnum_err(ctx->offset, argv[optind]);
797ac58c
KW
1471 g_free(ctx);
1472 return 0;
1473 }
1474 optind++;
1475
5ceb7765
KW
1476 if (ctx->zflag) {
1477 int64_t count = cvtnum(argv[optind]);
1478 if (count < 0) {
1479 print_cvtnum_err(count, argv[optind]);
0e01b76e 1480 g_free(ctx);
5ceb7765
KW
1481 return 0;
1482 }
797ac58c 1483
5ceb7765 1484 ctx->qiov.size = count;
d004bd52
EB
1485 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1486 ctx);
5ceb7765
KW
1487 } else {
1488 nr_iov = argc - optind;
1489 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1490 pattern);
1491 if (ctx->buf == NULL) {
1492 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1493 g_free(ctx);
1494 return 0;
1495 }
1496
1497 gettimeofday(&ctx->t1, NULL);
1498 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1499 BLOCK_ACCT_WRITE);
1500
770e0e0e
EB
1501 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1502 ctx);
5ceb7765 1503 }
797ac58c
KW
1504 return 0;
1505}
1506
4c7b7e9b 1507static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
797ac58c 1508{
556c2b60
AG
1509 BlockAcctCookie cookie;
1510 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
4c7b7e9b 1511 blk_drain_all();
556c2b60 1512 block_acct_done(blk_get_stats(blk), &cookie);
797ac58c
KW
1513 return 0;
1514}
1515
1516static const cmdinfo_t aio_flush_cmd = {
1517 .name = "aio_flush",
1518 .cfunc = aio_flush_f,
1519 .oneline = "completes all outstanding aio requests"
1520};
1521
4c7b7e9b 1522static int flush_f(BlockBackend *blk, int argc, char **argv)
797ac58c 1523{
4c7b7e9b 1524 blk_flush(blk);
797ac58c
KW
1525 return 0;
1526}
1527
1528static const cmdinfo_t flush_cmd = {
1529 .name = "flush",
1530 .altname = "f",
1531 .cfunc = flush_f,
1532 .oneline = "flush all in-core file state to disk",
1533};
1534
4c7b7e9b 1535static int truncate_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1536{
1537 int64_t offset;
1538 int ret;
1539
1540 offset = cvtnum(argv[1]);
1541 if (offset < 0) {
a9ecfa00 1542 print_cvtnum_err(offset, argv[1]);
797ac58c
KW
1543 return 0;
1544 }
1545
4c7b7e9b 1546 ret = blk_truncate(blk, offset);
797ac58c
KW
1547 if (ret < 0) {
1548 printf("truncate: %s\n", strerror(-ret));
1549 return 0;
1550 }
1551
1552 return 0;
1553}
1554
1555static const cmdinfo_t truncate_cmd = {
1556 .name = "truncate",
1557 .altname = "t",
1558 .cfunc = truncate_f,
1559 .argmin = 1,
1560 .argmax = 1,
1561 .args = "off",
1562 .oneline = "truncates the current file at the given offset",
1563};
1564
4c7b7e9b 1565static int length_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1566{
1567 int64_t size;
1568 char s1[64];
1569
4c7b7e9b 1570 size = blk_getlength(blk);
797ac58c
KW
1571 if (size < 0) {
1572 printf("getlength: %s\n", strerror(-size));
1573 return 0;
1574 }
1575
1576 cvtstr(size, s1, sizeof(s1));
1577 printf("%s\n", s1);
1578 return 0;
1579}
1580
1581
1582static const cmdinfo_t length_cmd = {
1583 .name = "length",
1584 .altname = "l",
1585 .cfunc = length_f,
1586 .oneline = "gets the length of the current file",
1587};
1588
1589
4c7b7e9b 1590static int info_f(BlockBackend *blk, int argc, char **argv)
797ac58c 1591{
4c7b7e9b 1592 BlockDriverState *bs = blk_bs(blk);
797ac58c 1593 BlockDriverInfo bdi;
a8d8ecb7 1594 ImageInfoSpecific *spec_info;
797ac58c
KW
1595 char s1[64], s2[64];
1596 int ret;
1597
1598 if (bs->drv && bs->drv->format_name) {
1599 printf("format name: %s\n", bs->drv->format_name);
1600 }
1601 if (bs->drv && bs->drv->protocol_name) {
1602 printf("format name: %s\n", bs->drv->protocol_name);
1603 }
1604
1605 ret = bdrv_get_info(bs, &bdi);
1606 if (ret) {
1607 return 0;
1608 }
1609
1610 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1611 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1612
1613 printf("cluster size: %s\n", s1);
1614 printf("vm state offset: %s\n", s2);
1615
a8d8ecb7
HR
1616 spec_info = bdrv_get_specific_info(bs);
1617 if (spec_info) {
1618 printf("Format specific information:\n");
1619 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1620 qapi_free_ImageInfoSpecific(spec_info);
1621 }
1622
797ac58c
KW
1623 return 0;
1624}
1625
1626
1627
1628static const cmdinfo_t info_cmd = {
1629 .name = "info",
1630 .altname = "i",
1631 .cfunc = info_f,
1632 .oneline = "prints information about the current file",
1633};
1634
1635static void discard_help(void)
1636{
1637 printf(
1638"\n"
1639" discards a range of bytes from the given offset\n"
1640"\n"
1641" Example:\n"
1642" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1643"\n"
1644" Discards a segment of the currently open file.\n"
1645" -C, -- report statistics in a machine parsable format\n"
1646" -q, -- quiet mode, do not show I/O statistics\n"
1647"\n");
1648}
1649
4c7b7e9b 1650static int discard_f(BlockBackend *blk, int argc, char **argv);
797ac58c
KW
1651
1652static const cmdinfo_t discard_cmd = {
1653 .name = "discard",
1654 .altname = "d",
1655 .cfunc = discard_f,
1656 .argmin = 2,
1657 .argmax = -1,
1658 .args = "[-Cq] off len",
1659 .oneline = "discards a number of bytes at a specified offset",
1660 .help = discard_help,
1661};
1662
4c7b7e9b 1663static int discard_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1664{
1665 struct timeval t1, t2;
dc38852a 1666 bool Cflag = false, qflag = false;
797ac58c 1667 int c, ret;
9b0beaf3 1668 int64_t offset, count;
797ac58c 1669
b062ad86 1670 while ((c = getopt(argc, argv, "Cq")) != -1) {
797ac58c
KW
1671 switch (c) {
1672 case 'C':
dc38852a 1673 Cflag = true;
797ac58c
KW
1674 break;
1675 case 'q':
dc38852a 1676 qflag = true;
797ac58c
KW
1677 break;
1678 default:
c2cdf5c5 1679 return qemuio_command_usage(&discard_cmd);
797ac58c
KW
1680 }
1681 }
1682
1683 if (optind != argc - 2) {
c2cdf5c5 1684 return qemuio_command_usage(&discard_cmd);
797ac58c
KW
1685 }
1686
1687 offset = cvtnum(argv[optind]);
1688 if (offset < 0) {
a9ecfa00 1689 print_cvtnum_err(offset, argv[optind]);
797ac58c
KW
1690 return 0;
1691 }
1692
1693 optind++;
1694 count = cvtnum(argv[optind]);
1695 if (count < 0) {
a9ecfa00 1696 print_cvtnum_err(count, argv[optind]);
797ac58c 1697 return 0;
a3674679 1698 } else if (count >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
9b0beaf3 1699 printf("length cannot exceed %"PRIu64", given %s\n",
a3674679 1700 (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
9b0beaf3
JS
1701 argv[optind]);
1702 return 0;
797ac58c
KW
1703 }
1704
1705 gettimeofday(&t1, NULL);
1c6c4bb7 1706 ret = blk_pdiscard(blk, offset, count);
797ac58c
KW
1707 gettimeofday(&t2, NULL);
1708
1709 if (ret < 0) {
1710 printf("discard failed: %s\n", strerror(-ret));
1711 goto out;
1712 }
1713
1714 /* Finally, report back -- -C gives a parsable format */
1715 if (!qflag) {
1716 t2 = tsub(t2, t1);
1717 print_report("discard", &t2, offset, count, count, 1, Cflag);
1718 }
1719
1720out:
1721 return 0;
1722}
1723
4c7b7e9b 1724static int alloc_f(BlockBackend *blk, int argc, char **argv)
797ac58c 1725{
4c7b7e9b 1726 BlockDriverState *bs = blk_bs(blk);
9b0beaf3 1727 int64_t offset, sector_num, nb_sectors, remaining;
797ac58c 1728 char s1[64];
9b0beaf3
JS
1729 int num, ret;
1730 int64_t sum_alloc;
797ac58c
KW
1731
1732 offset = cvtnum(argv[1]);
1733 if (offset < 0) {
a9ecfa00 1734 print_cvtnum_err(offset, argv[1]);
797ac58c
KW
1735 return 0;
1736 } else if (offset & 0x1ff) {
1737 printf("offset %" PRId64 " is not sector aligned\n",
1738 offset);
1739 return 0;
1740 }
1741
1742 if (argc == 3) {
1743 nb_sectors = cvtnum(argv[2]);
1744 if (nb_sectors < 0) {
a9ecfa00 1745 print_cvtnum_err(nb_sectors, argv[2]);
797ac58c 1746 return 0;
9b0beaf3
JS
1747 } else if (nb_sectors > INT_MAX) {
1748 printf("length argument cannot exceed %d, given %s\n",
1749 INT_MAX, argv[2]);
1750 return 0;
797ac58c
KW
1751 }
1752 } else {
1753 nb_sectors = 1;
1754 }
1755
1756 remaining = nb_sectors;
1757 sum_alloc = 0;
1758 sector_num = offset >> 9;
1759 while (remaining) {
1760 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
d663640c
PB
1761 if (ret < 0) {
1762 printf("is_allocated failed: %s\n", strerror(-ret));
1763 return 0;
1764 }
797ac58c
KW
1765 sector_num += num;
1766 remaining -= num;
1767 if (ret) {
1768 sum_alloc += num;
1769 }
1770 if (num == 0) {
1771 nb_sectors -= remaining;
1772 remaining = 0;
1773 }
1774 }
1775
1776 cvtstr(offset, s1, sizeof(s1));
1777
9b0beaf3 1778 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
797ac58c
KW
1779 sum_alloc, nb_sectors, s1);
1780 return 0;
1781}
1782
1783static const cmdinfo_t alloc_cmd = {
1784 .name = "alloc",
1785 .altname = "a",
1786 .argmin = 1,
1787 .argmax = 2,
1788 .cfunc = alloc_f,
1789 .args = "off [sectors]",
1790 .oneline = "checks if a sector is present in the file",
1791};
1792
1793
1794static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1795 int64_t nb_sectors, int64_t *pnum)
1796{
1797 int num, num_checked;
1798 int ret, firstret;
1799
1800 num_checked = MIN(nb_sectors, INT_MAX);
1801 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1802 if (ret < 0) {
1803 return ret;
1804 }
1805
1806 firstret = ret;
1807 *pnum = num;
1808
1809 while (nb_sectors > 0 && ret == firstret) {
1810 sector_num += num;
1811 nb_sectors -= num;
1812
1813 num_checked = MIN(nb_sectors, INT_MAX);
1814 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
4b25bbc4 1815 if (ret == firstret && num) {
797ac58c
KW
1816 *pnum += num;
1817 } else {
1818 break;
1819 }
1820 }
1821
1822 return firstret;
1823}
1824
4c7b7e9b 1825static int map_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1826{
1827 int64_t offset;
4c7b7e9b 1828 int64_t nb_sectors, total_sectors;
797ac58c
KW
1829 char s1[64];
1830 int64_t num;
1831 int ret;
1832 const char *retstr;
1833
1834 offset = 0;
4c7b7e9b
HR
1835 total_sectors = blk_nb_sectors(blk);
1836 if (total_sectors < 0) {
1837 error_report("Failed to query image length: %s",
1838 strerror(-total_sectors));
1839 return 0;
1840 }
1841
1842 nb_sectors = total_sectors;
797ac58c
KW
1843
1844 do {
4c7b7e9b 1845 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
797ac58c
KW
1846 if (ret < 0) {
1847 error_report("Failed to get allocation status: %s", strerror(-ret));
1848 return 0;
4b25bbc4
HR
1849 } else if (!num) {
1850 error_report("Unexpected end of image");
1851 return 0;
797ac58c
KW
1852 }
1853
1854 retstr = ret ? " allocated" : "not allocated";
1855 cvtstr(offset << 9ULL, s1, sizeof(s1));
1856 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1857 "at offset %s (%d)\n",
1858 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1859
1860 offset += num;
1861 nb_sectors -= num;
4c7b7e9b 1862 } while (offset < total_sectors);
797ac58c
KW
1863
1864 return 0;
1865}
1866
1867static const cmdinfo_t map_cmd = {
1868 .name = "map",
1869 .argmin = 0,
1870 .argmax = 0,
1871 .cfunc = map_f,
1872 .args = "",
1873 .oneline = "prints the allocated areas of a file",
1874};
1875
5bbd2e59
KW
1876static void reopen_help(void)
1877{
1878 printf(
1879"\n"
1880" Changes the open options of an already opened image\n"
1881"\n"
1882" Example:\n"
1883" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1884"\n"
1885" -r, -- Reopen the image read-only\n"
1886" -c, -- Change the cache mode to the given value\n"
1887" -o, -- Changes block driver options (cf. 'open' command)\n"
1888"\n");
1889}
1890
1891static int reopen_f(BlockBackend *blk, int argc, char **argv);
1892
1893static QemuOptsList reopen_opts = {
1894 .name = "reopen",
1895 .merge_lists = true,
1896 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
1897 .desc = {
1898 /* no elements => accept any params */
1899 { /* end of list */ }
1900 },
1901};
1902
1903static const cmdinfo_t reopen_cmd = {
1904 .name = "reopen",
1905 .argmin = 0,
1906 .argmax = -1,
1907 .cfunc = reopen_f,
1908 .args = "[-r] [-c cache] [-o options]",
1909 .oneline = "reopens an image with new options",
1910 .help = reopen_help,
1911};
1912
1913static int reopen_f(BlockBackend *blk, int argc, char **argv)
1914{
1915 BlockDriverState *bs = blk_bs(blk);
1916 QemuOpts *qopts;
1917 QDict *opts;
1918 int c;
1919 int flags = bs->open_flags;
19dbecdc 1920 bool writethrough = !blk_enable_write_cache(blk);
5bbd2e59
KW
1921
1922 BlockReopenQueue *brq;
1923 Error *local_err = NULL;
1924
1925 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
1926 switch (c) {
1927 case 'c':
19dbecdc 1928 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
5bbd2e59
KW
1929 error_report("Invalid cache option: %s", optarg);
1930 return 0;
1931 }
1932 break;
1933 case 'o':
1934 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
1935 qemu_opts_reset(&reopen_opts);
1936 return 0;
1937 }
1938 break;
1939 case 'r':
1940 flags &= ~BDRV_O_RDWR;
1941 break;
1942 default:
1943 qemu_opts_reset(&reopen_opts);
1944 return qemuio_command_usage(&reopen_cmd);
1945 }
1946 }
1947
1948 if (optind != argc) {
1949 qemu_opts_reset(&reopen_opts);
1950 return qemuio_command_usage(&reopen_cmd);
1951 }
1952
19dbecdc
KW
1953 if (writethrough != blk_enable_write_cache(blk) &&
1954 blk_get_attached_dev(blk))
1955 {
1956 error_report("Cannot change cache.writeback: Device attached");
1957 qemu_opts_reset(&reopen_opts);
1958 return 0;
1959 }
1960
5bbd2e59
KW
1961 qopts = qemu_opts_find(&reopen_opts, NULL);
1962 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
1963 qemu_opts_reset(&reopen_opts);
1964
1965 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
720150f3 1966 bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
5bbd2e59
KW
1967 if (local_err) {
1968 error_report_err(local_err);
19dbecdc
KW
1969 } else {
1970 blk_set_enable_write_cache(blk, !writethrough);
5bbd2e59
KW
1971 }
1972
1973 return 0;
1974}
1975
4c7b7e9b 1976static int break_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
1977{
1978 int ret;
1979
4c7b7e9b 1980 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
797ac58c
KW
1981 if (ret < 0) {
1982 printf("Could not set breakpoint: %s\n", strerror(-ret));
1983 }
1984
1985 return 0;
1986}
1987
4c7b7e9b 1988static int remove_break_f(BlockBackend *blk, int argc, char **argv)
4cc70e93
FZ
1989{
1990 int ret;
1991
4c7b7e9b 1992 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
4cc70e93
FZ
1993 if (ret < 0) {
1994 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
1995 }
1996
1997 return 0;
1998}
1999
797ac58c
KW
2000static const cmdinfo_t break_cmd = {
2001 .name = "break",
2002 .argmin = 2,
2003 .argmax = 2,
2004 .cfunc = break_f,
2005 .args = "event tag",
2006 .oneline = "sets a breakpoint on event and tags the stopped "
2007 "request as tag",
2008};
2009
4cc70e93
FZ
2010static const cmdinfo_t remove_break_cmd = {
2011 .name = "remove_break",
2012 .argmin = 1,
2013 .argmax = 1,
2014 .cfunc = remove_break_f,
2015 .args = "tag",
2016 .oneline = "remove a breakpoint by tag",
2017};
2018
4c7b7e9b 2019static int resume_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
2020{
2021 int ret;
2022
4c7b7e9b 2023 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
797ac58c
KW
2024 if (ret < 0) {
2025 printf("Could not resume request: %s\n", strerror(-ret));
2026 }
2027
2028 return 0;
2029}
2030
2031static const cmdinfo_t resume_cmd = {
2032 .name = "resume",
2033 .argmin = 1,
2034 .argmax = 1,
2035 .cfunc = resume_f,
2036 .args = "tag",
2037 .oneline = "resumes the request tagged as tag",
2038};
2039
4c7b7e9b 2040static int wait_break_f(BlockBackend *blk, int argc, char **argv)
797ac58c 2041{
4c7b7e9b
HR
2042 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2043 aio_poll(blk_get_aio_context(blk), true);
797ac58c
KW
2044 }
2045
2046 return 0;
2047}
2048
2049static const cmdinfo_t wait_break_cmd = {
2050 .name = "wait_break",
2051 .argmin = 1,
2052 .argmax = 1,
2053 .cfunc = wait_break_f,
2054 .args = "tag",
2055 .oneline = "waits for the suspension of a request",
2056};
2057
4c7b7e9b 2058static int abort_f(BlockBackend *blk, int argc, char **argv)
797ac58c
KW
2059{
2060 abort();
2061}
2062
2063static const cmdinfo_t abort_cmd = {
2064 .name = "abort",
2065 .cfunc = abort_f,
2066 .flags = CMD_NOFILE_OK,
2067 .oneline = "simulate a program crash using abort(3)",
2068};
2069
0e82dc7b
HR
2070static void sigraise_help(void)
2071{
2072 printf(
2073"\n"
2074" raises the given signal\n"
2075"\n"
2076" Example:\n"
2077" 'sigraise %i' - raises SIGTERM\n"
2078"\n"
2079" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2080" given to sigraise.\n"
2081"\n", SIGTERM);
2082}
2083
4c7b7e9b 2084static int sigraise_f(BlockBackend *blk, int argc, char **argv);
0e82dc7b
HR
2085
2086static const cmdinfo_t sigraise_cmd = {
2087 .name = "sigraise",
2088 .cfunc = sigraise_f,
2089 .argmin = 1,
2090 .argmax = 1,
2091 .flags = CMD_NOFILE_OK,
2092 .args = "signal",
2093 .oneline = "raises a signal",
2094 .help = sigraise_help,
2095};
2096
4c7b7e9b 2097static int sigraise_f(BlockBackend *blk, int argc, char **argv)
0e82dc7b 2098{
9b0beaf3 2099 int64_t sig = cvtnum(argv[1]);
0e82dc7b 2100 if (sig < 0) {
a9ecfa00 2101 print_cvtnum_err(sig, argv[1]);
0e82dc7b 2102 return 0;
9b0beaf3
JS
2103 } else if (sig > NSIG) {
2104 printf("signal argument '%s' is too large to be a valid signal\n",
2105 argv[1]);
2106 return 0;
0e82dc7b
HR
2107 }
2108
2109 /* Using raise() to kill this process does not necessarily flush all open
2110 * streams. At least stdout and stderr (although the latter should be
2111 * non-buffered anyway) should be flushed, though. */
2112 fflush(stdout);
2113 fflush(stderr);
2114
2115 raise(sig);
2116 return 0;
2117}
2118
cd33d02a
KW
2119static void sleep_cb(void *opaque)
2120{
2121 bool *expired = opaque;
2122 *expired = true;
2123}
2124
4c7b7e9b 2125static int sleep_f(BlockBackend *blk, int argc, char **argv)
cd33d02a
KW
2126{
2127 char *endptr;
2128 long ms;
2129 struct QEMUTimer *timer;
2130 bool expired = false;
2131
2132 ms = strtol(argv[1], &endptr, 0);
2133 if (ms < 0 || *endptr != '\0') {
2134 printf("%s is not a valid number\n", argv[1]);
2135 return 0;
2136 }
2137
2138 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2139 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2140
2141 while (!expired) {
2142 main_loop_wait(false);
2143 }
2144
2145 timer_free(timer);
2146
2147 return 0;
2148}
2149
2150static const cmdinfo_t sleep_cmd = {
2151 .name = "sleep",
2152 .argmin = 1,
2153 .argmax = 1,
2154 .cfunc = sleep_f,
2155 .flags = CMD_NOFILE_OK,
2156 .oneline = "waits for the given value in milliseconds",
2157};
2158
f18a834a
KW
2159static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2160{
2161 if (cmd) {
2162 printf("%s ", cmd);
2163 } else {
2164 printf("%s ", ct->name);
2165 if (ct->altname) {
2166 printf("(or %s) ", ct->altname);
2167 }
2168 }
2169
2170 if (ct->args) {
2171 printf("%s ", ct->args);
2172 }
2173 printf("-- %s\n", ct->oneline);
2174}
2175
2176static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2177{
2178 help_oneline(cmd, ct);
2179 if (ct->help) {
2180 ct->help();
2181 }
2182}
2183
2184static void help_all(void)
2185{
2186 const cmdinfo_t *ct;
2187
2188 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2189 help_oneline(ct->name, ct);
2190 }
2191 printf("\nUse 'help commandname' for extended help.\n");
2192}
2193
4c7b7e9b 2194static int help_f(BlockBackend *blk, int argc, char **argv)
f18a834a
KW
2195{
2196 const cmdinfo_t *ct;
2197
2198 if (argc == 1) {
2199 help_all();
2200 return 0;
2201 }
2202
2203 ct = find_command(argv[1]);
2204 if (ct == NULL) {
2205 printf("command %s not found\n", argv[1]);
2206 return 0;
2207 }
2208
2209 help_onecmd(argv[1], ct);
2210 return 0;
2211}
2212
2213static const cmdinfo_t help_cmd = {
2214 .name = "help",
2215 .altname = "?",
2216 .cfunc = help_f,
2217 .argmin = 0,
2218 .argmax = 1,
2219 .flags = CMD_FLAG_GLOBAL,
2220 .args = "[command]",
2221 .oneline = "help for one or all commands",
2222};
2223
4c7b7e9b 2224bool qemuio_command(BlockBackend *blk, const char *cmd)
dd583296 2225{
15afd94a 2226 AioContext *ctx;
dd583296
KW
2227 char *input;
2228 const cmdinfo_t *ct;
2229 char **v;
2230 int c;
2231 bool done = false;
2232
2233 input = g_strdup(cmd);
2234 v = breakline(input, &c);
2235 if (c) {
2236 ct = find_command(v[0]);
2237 if (ct) {
15afd94a
PB
2238 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2239 aio_context_acquire(ctx);
4c7b7e9b 2240 done = command(blk, ct, c, v);
15afd94a 2241 aio_context_release(ctx);
dd583296
KW
2242 } else {
2243 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2244 }
2245 }
2246 g_free(input);
2247 g_free(v);
2248
2249 return done;
2250}
2251
797ac58c
KW
2252static void __attribute((constructor)) init_qemuio_commands(void)
2253{
2254 /* initialize commands */
c2cdf5c5
KW
2255 qemuio_add_command(&help_cmd);
2256 qemuio_add_command(&read_cmd);
2257 qemuio_add_command(&readv_cmd);
2258 qemuio_add_command(&write_cmd);
2259 qemuio_add_command(&writev_cmd);
c2cdf5c5
KW
2260 qemuio_add_command(&aio_read_cmd);
2261 qemuio_add_command(&aio_write_cmd);
2262 qemuio_add_command(&aio_flush_cmd);
2263 qemuio_add_command(&flush_cmd);
2264 qemuio_add_command(&truncate_cmd);
2265 qemuio_add_command(&length_cmd);
2266 qemuio_add_command(&info_cmd);
2267 qemuio_add_command(&discard_cmd);
2268 qemuio_add_command(&alloc_cmd);
2269 qemuio_add_command(&map_cmd);
5bbd2e59 2270 qemuio_add_command(&reopen_cmd);
c2cdf5c5 2271 qemuio_add_command(&break_cmd);
4cc70e93 2272 qemuio_add_command(&remove_break_cmd);
c2cdf5c5
KW
2273 qemuio_add_command(&resume_cmd);
2274 qemuio_add_command(&wait_break_cmd);
2275 qemuio_add_command(&abort_cmd);
cd33d02a 2276 qemuio_add_command(&sleep_cmd);
0e82dc7b 2277 qemuio_add_command(&sigraise_cmd);
797ac58c 2278}