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