2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009-2016 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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.
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qapi/qmp/qdict.h"
15 #include "sysemu/block-backend.h"
16 #include "block/block.h"
17 #include "block/block_int.h" /* for info_f() */
18 #include "block/qapi.h"
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/option.h"
22 #include "qemu/timer.h"
23 #include "qemu/cutils.h"
24 #include "qemu/memalign.h"
26 #define CMD_NOFILE_OK 0x01
30 static cmdinfo_t
*cmdtab
;
33 static int compare_cmdname(const void *a
, const void *b
)
35 return strcmp(((const cmdinfo_t
*)a
)->name
,
36 ((const cmdinfo_t
*)b
)->name
);
39 void qemuio_add_command(const cmdinfo_t
*ci
)
41 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
42 * flags allow it not to be, so that combination is invalid.
43 * Catch it now rather than letting it manifest as a crash if a
44 * particular set of command line options are used.
46 assert(ci
->perm
== 0 ||
47 (ci
->flags
& (CMD_FLAG_GLOBAL
| CMD_NOFILE_OK
)) == 0);
48 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
49 cmdtab
[ncmds
- 1] = *ci
;
50 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
53 void qemuio_command_usage(const cmdinfo_t
*ci
)
55 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
58 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
60 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
63 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
64 fprintf(stderr
, "no file open, try 'help open'\n");
70 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
75 if (!init_check_command(blk
, ct
)) {
79 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
80 if (ct
->argmax
== -1) {
82 "bad argument count %d to %s, expected at least %d arguments\n",
83 argc
-1, cmd
, ct
->argmin
);
84 } else if (ct
->argmin
== ct
->argmax
) {
86 "bad argument count %d to %s, expected %d arguments\n",
87 argc
-1, cmd
, ct
->argmin
);
90 "bad argument count %d to %s, expected between %d and %d arguments\n",
91 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
97 * Request additional permissions if necessary for this command. The caller
98 * is responsible for restoring the original permissions afterwards if this
101 * Coverity thinks that blk may be NULL in the following if condition. It's
102 * not so: in init_check_command() we fail if blk is NULL for command with
103 * both CMD_FLAG_GLOBAL and CMD_NOFILE_OK flags unset. And in
104 * qemuio_add_command() we assert that command with non-zero .perm field
105 * doesn't set this flags. So, the following assertion is to silence
108 assert(blk
|| !ct
->perm
);
109 if (ct
->perm
&& blk_is_available(blk
)) {
110 uint64_t orig_perm
, orig_shared_perm
;
111 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
113 if (ct
->perm
& ~orig_perm
) {
115 Error
*local_err
= NULL
;
118 new_perm
= orig_perm
| ct
->perm
;
120 ret
= blk_set_perm(blk
, new_perm
, orig_shared_perm
, &local_err
);
122 error_report_err(local_err
);
129 return ct
->cfunc(blk
, argc
, argv
);
132 static const cmdinfo_t
*find_command(const char *cmd
)
136 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
137 if (strcmp(ct
->name
, cmd
) == 0 ||
138 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
140 return (const cmdinfo_t
*)ct
;
146 /* Invoke fn() for commands with a matching prefix */
147 void qemuio_complete_command(const char *input
,
148 void (*fn
)(const char *cmd
, void *opaque
),
152 size_t input_len
= strlen(input
);
154 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
155 if (strncmp(input
, ct
->name
, input_len
) == 0) {
156 fn(ct
->name
, opaque
);
161 static char **breakline(char *input
, int *count
)
165 char **rval
= g_new0(char *, 1);
167 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
172 rval
= g_renew(char *, rval
, (c
+ 1));
180 static int64_t cvtnum(const char *s
)
185 err
= qemu_strtosz(s
, NULL
, &value
);
189 if (value
> INT64_MAX
) {
195 static void print_cvtnum_err(int64_t rc
, const char *arg
)
199 printf("Parsing error: non-numeric argument,"
200 " or extraneous/unrecognized suffix -- %s\n", arg
);
203 printf("Parsing error: argument too large -- %s\n", arg
);
206 printf("Parsing error: %s\n", arg
);
210 #define EXABYTES(x) ((long long)(x) << 60)
211 #define PETABYTES(x) ((long long)(x) << 50)
212 #define TERABYTES(x) ((long long)(x) << 40)
213 #define GIGABYTES(x) ((long long)(x) << 30)
214 #define MEGABYTES(x) ((long long)(x) << 20)
215 #define KILOBYTES(x) ((long long)(x) << 10)
217 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
218 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
219 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
220 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
221 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
222 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
224 static void cvtstr(double value
, char *str
, size_t size
)
229 if (value
>= EXABYTES(1)) {
231 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
232 } else if (value
>= PETABYTES(1)) {
234 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
235 } else if (value
>= TERABYTES(1)) {
237 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
238 } else if (value
>= GIGABYTES(1)) {
240 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
241 } else if (value
>= MEGABYTES(1)) {
243 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
244 } else if (value
>= KILOBYTES(1)) {
246 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
249 snprintf(str
, size
- 6, "%f", value
);
252 trim
= strstr(str
, ".000");
254 strcpy(trim
, suffix
);
262 static struct timespec
tsub(struct timespec t1
, struct timespec t2
)
264 t1
.tv_nsec
-= t2
.tv_nsec
;
265 if (t1
.tv_nsec
< 0) {
266 t1
.tv_nsec
+= NANOSECONDS_PER_SECOND
;
269 t1
.tv_sec
-= t2
.tv_sec
;
273 static double tdiv(double value
, struct timespec tv
)
275 double seconds
= tv
.tv_sec
+ (tv
.tv_nsec
/ 1e9
);
276 return value
/ seconds
;
279 #define HOURS(sec) ((sec) / (60 * 60))
280 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
281 #define SECONDS(sec) ((sec) % 60)
285 TERSE_FIXED_TIME
= 0x1,
286 VERBOSE_FIXED_TIME
= 0x2,
289 static void timestr(struct timespec
*tv
, char *ts
, size_t size
, int format
)
291 double frac_sec
= tv
->tv_nsec
/ 1e9
;
293 if (format
& TERSE_FIXED_TIME
) {
294 if (!HOURS(tv
->tv_sec
)) {
295 snprintf(ts
, size
, "%u:%05.2f",
296 (unsigned int) MINUTES(tv
->tv_sec
),
297 SECONDS(tv
->tv_sec
) + frac_sec
);
300 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
303 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
304 snprintf(ts
, size
, "%u:%02u:%05.2f",
305 (unsigned int) HOURS(tv
->tv_sec
),
306 (unsigned int) MINUTES(tv
->tv_sec
),
307 SECONDS(tv
->tv_sec
) + frac_sec
);
309 snprintf(ts
, size
, "%05.2f sec", frac_sec
);
314 * Parse the pattern argument to various sub-commands.
316 * Because the pattern is used as an argument to memset it must evaluate
317 * to an unsigned integer that fits into a single byte.
319 static int parse_pattern(const char *arg
)
324 pattern
= strtol(arg
, &endptr
, 0);
325 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
326 printf("%s is not a valid pattern byte\n", arg
);
334 * Memory allocation helpers.
336 * Make sure memory is aligned by default, or purposefully misaligned if
337 * that is specified on the command line.
340 #define MISALIGN_OFFSET 16
341 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
,
346 if (qemuio_misalign
) {
347 len
+= MISALIGN_OFFSET
;
349 buf
= blk_blockalign(blk
, len
);
350 memset(buf
, pattern
, len
);
352 blk_register_buf(blk
, buf
, len
, &error_abort
);
354 if (qemuio_misalign
) {
355 buf
+= MISALIGN_OFFSET
;
360 static void qemu_io_free(BlockBackend
*blk
, void *p
, size_t len
,
363 if (qemuio_misalign
) {
364 p
-= MISALIGN_OFFSET
;
365 len
+= MISALIGN_OFFSET
;
367 if (unregister_buf
) {
368 blk_unregister_buf(blk
, p
, len
);
374 * qemu_io_alloc_from_file()
376 * Allocates the buffer and populates it with the content of the given file
377 * up to @len bytes. If the file length is less than @len, then the buffer
378 * is populated with the file content cyclically.
380 * @blk - the block backend where the buffer content is going to be written to
381 * @len - the buffer length
382 * @file_name - the file to read the content from
383 * @register_buf - call blk_register_buf()
385 * Returns: the buffer pointer on success
388 static void *qemu_io_alloc_from_file(BlockBackend
*blk
, size_t len
,
389 const char *file_name
, bool register_buf
)
391 size_t alloc_len
= len
+ (qemuio_misalign
? MISALIGN_OFFSET
: 0);
392 char *alloc_buf
, *buf
, *end
;
393 FILE *f
= fopen(file_name
, "r");
401 alloc_buf
= buf
= blk_blockalign(blk
, alloc_len
);
403 if (qemuio_misalign
) {
404 buf
+= MISALIGN_OFFSET
;
407 pattern_len
= fread(buf
, 1, len
, f
);
414 if (pattern_len
== 0) {
415 fprintf(stderr
, "%s: file is empty\n", file_name
);
423 blk_register_buf(blk
, alloc_buf
, alloc_len
, &error_abort
);
427 for (char *p
= buf
+ pattern_len
; p
< end
; p
+= pattern_len
) {
428 memcpy(p
, buf
, MIN(pattern_len
, end
- p
));
435 * This code path is only taken before blk_register_buf() is called, so
436 * hardcode the qemu_io_free() unregister_buf argument to false.
438 qemu_io_free(blk
, alloc_buf
, alloc_len
, false);
445 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
451 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
452 const uint8_t *s
= p
;
454 printf("%08" PRIx64
": ", offset
+ i
);
455 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
459 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
470 static void print_report(const char *op
, struct timespec
*t
, int64_t offset
,
471 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
473 char s1
[64], s2
[64], ts
[64];
475 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
477 cvtstr((double)total
, s1
, sizeof(s1
));
478 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
479 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
480 op
, total
, count
, offset
);
481 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
482 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
483 } else {/* bytes,ops,time,bytes/sec,ops/sec */
484 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
486 tdiv((double)total
, *t
),
487 tdiv((double)cnt
, *t
));
492 * Parse multiple length statements for vectored I/O, and construct an I/O
493 * vector matching it.
496 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
497 int pattern
, bool register_buf
)
499 size_t *sizes
= g_new0(size_t, nr_iov
);
505 for (i
= 0; i
< nr_iov
; i
++) {
511 print_cvtnum_err(len
, arg
);
515 if (len
> BDRV_REQUEST_MAX_BYTES
) {
516 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
517 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
521 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
522 printf("The total number of bytes exceed the maximum size %" PRIu64
523 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
531 qemu_iovec_init(qiov
, nr_iov
);
533 buf
= p
= qemu_io_alloc(blk
, count
, pattern
, register_buf
);
535 for (i
= 0; i
< nr_iov
; i
++) {
536 qemu_iovec_add(qiov
, p
, sizes
[i
]);
545 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
546 int64_t bytes
, BdrvRequestFlags flags
, int64_t *total
)
550 if (bytes
> INT_MAX
) {
554 ret
= blk_pread(blk
, offset
, bytes
, (uint8_t *)buf
, flags
);
562 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
563 int64_t bytes
, BdrvRequestFlags flags
, int64_t *total
)
567 if (bytes
> INT_MAX
) {
571 ret
= blk_pwrite(blk
, offset
, bytes
, (uint8_t *)buf
, flags
);
579 static int do_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
580 int64_t bytes
, BdrvRequestFlags flags
,
583 int ret
= blk_pwrite_zeroes(blk
, offset
, bytes
,
584 flags
| BDRV_REQ_ZERO_WRITE
);
593 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
594 int64_t bytes
, int64_t *total
)
598 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
602 ret
= blk_pwrite_compressed(blk
, offset
, bytes
, buf
);
610 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
611 int64_t count
, int64_t *total
)
613 if (count
> INT_MAX
) {
617 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
624 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
625 int64_t count
, int64_t *total
)
627 if (count
> INT_MAX
) {
631 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
638 #define NOT_DONE 0x7fffffff
639 static void aio_rw_done(void *opaque
, int ret
)
641 *(int *)opaque
= ret
;
644 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
645 int64_t offset
, BdrvRequestFlags flags
, int *total
)
647 int async_ret
= NOT_DONE
;
649 blk_aio_preadv(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
650 while (async_ret
== NOT_DONE
) {
651 main_loop_wait(false);
655 return async_ret
< 0 ? async_ret
: 1;
658 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
659 int64_t offset
, BdrvRequestFlags flags
, int *total
)
661 int async_ret
= NOT_DONE
;
663 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
664 while (async_ret
== NOT_DONE
) {
665 main_loop_wait(false);
669 return async_ret
< 0 ? async_ret
: 1;
672 static void read_help(void)
676 " reads a range of bytes from the given offset\n"
679 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
681 " Reads a segment of the currently open file, optionally dumping it to the\n"
682 " standard output stream (with -v option) for subsequent inspection.\n"
683 " -b, -- read from the VM state rather than the virtual disk\n"
684 " -C, -- report statistics in a machine parsable format\n"
685 " -l, -- length for pattern verification (only with -P)\n"
686 " -p, -- ignored for backwards compatibility\n"
687 " -P, -- use a pattern to verify read data\n"
688 " -q, -- quiet mode, do not show I/O statistics\n"
689 " -r, -- register I/O buffer\n"
690 " -s, -- start offset for pattern verification (only with -P)\n"
691 " -v, -- dump buffer to standard output\n"
695 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
697 static const cmdinfo_t read_cmd
= {
703 .args
= "[-abCqrv] [-P pattern [-s off] [-l len]] off len",
704 .oneline
= "reads a number of bytes at a specified offset",
708 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
710 struct timespec t1
, t2
;
711 bool Cflag
= false, qflag
= false, vflag
= false;
712 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
717 /* Some compilers get confused and warn if this is not initialized. */
720 int64_t pattern_offset
= 0, pattern_count
= 0;
721 BdrvRequestFlags flags
= 0;
723 while ((c
= getopt(argc
, argv
, "bCl:pP:qrs:v")) != -1) {
733 pattern_count
= cvtnum(optarg
);
734 if (pattern_count
< 0) {
735 print_cvtnum_err(pattern_count
, optarg
);
736 return pattern_count
;
740 /* Ignored for backwards compatibility */
744 pattern
= parse_pattern(optarg
);
753 flags
|= BDRV_REQ_REGISTERED_BUF
;
757 pattern_offset
= cvtnum(optarg
);
758 if (pattern_offset
< 0) {
759 print_cvtnum_err(pattern_offset
, optarg
);
760 return pattern_offset
;
767 qemuio_command_usage(&read_cmd
);
772 if (optind
!= argc
- 2) {
773 qemuio_command_usage(&read_cmd
);
777 offset
= cvtnum(argv
[optind
]);
779 print_cvtnum_err(offset
, argv
[optind
]);
784 count
= cvtnum(argv
[optind
]);
786 print_cvtnum_err(count
, argv
[optind
]);
788 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
789 printf("length cannot exceed %" PRIu64
", given %s\n",
790 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
794 if (!Pflag
&& (lflag
|| sflag
)) {
795 qemuio_command_usage(&read_cmd
);
800 pattern_count
= count
- pattern_offset
;
803 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
804 printf("pattern verification range exceeds end of read data\n");
809 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
810 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
814 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
815 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
819 if (flags
& BDRV_REQ_REGISTERED_BUF
) {
820 printf("I/O buffer registration is not supported when reading "
826 buf
= qemu_io_alloc(blk
, count
, 0xab, flags
& BDRV_REQ_REGISTERED_BUF
);
828 clock_gettime(CLOCK_MONOTONIC
, &t1
);
830 ret
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
832 ret
= do_pread(blk
, buf
, offset
, count
, flags
, &total
);
834 clock_gettime(CLOCK_MONOTONIC
, &t2
);
837 printf("read failed: %s\n", strerror(-ret
));
845 void *cmp_buf
= g_malloc(pattern_count
);
846 memset(cmp_buf
, pattern
, pattern_count
);
847 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
848 printf("Pattern verification failed at offset %"
849 PRId64
", %"PRId64
" bytes\n",
850 offset
+ pattern_offset
, pattern_count
);
861 dump_buffer(buf
, offset
, count
);
864 /* Finally, report back -- -C gives a parsable format */
866 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
869 qemu_io_free(blk
, buf
, count
, flags
& BDRV_REQ_REGISTERED_BUF
);
873 static void readv_help(void)
877 " reads a range of bytes from the given offset into multiple buffers\n"
880 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
882 " Reads a segment of the currently open file, optionally dumping it to the\n"
883 " standard output stream (with -v option) for subsequent inspection.\n"
884 " Uses multiple iovec buffers if more than one byte range is specified.\n"
885 " -C, -- report statistics in a machine parsable format\n"
886 " -P, -- use a pattern to verify read data\n"
887 " -q, -- quiet mode, do not show I/O statistics\n"
888 " -r, -- register I/O buffer\n"
889 " -v, -- dump buffer to standard output\n"
893 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
895 static const cmdinfo_t readv_cmd
= {
900 .args
= "[-Cqrv] [-P pattern] off len [len..]",
901 .oneline
= "reads a number of bytes at a specified offset",
905 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
907 struct timespec t1
, t2
;
908 bool Cflag
= false, qflag
= false, vflag
= false;
912 /* Some compilers get confused and warn if this is not initialized. */
918 BdrvRequestFlags flags
= 0;
920 while ((c
= getopt(argc
, argv
, "CP:qrv")) != -1) {
927 pattern
= parse_pattern(optarg
);
936 flags
|= BDRV_REQ_REGISTERED_BUF
;
942 qemuio_command_usage(&readv_cmd
);
947 if (optind
> argc
- 2) {
948 qemuio_command_usage(&readv_cmd
);
953 offset
= cvtnum(argv
[optind
]);
955 print_cvtnum_err(offset
, argv
[optind
]);
960 nr_iov
= argc
- optind
;
961 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab,
962 flags
& BDRV_REQ_REGISTERED_BUF
);
967 clock_gettime(CLOCK_MONOTONIC
, &t1
);
968 ret
= do_aio_readv(blk
, &qiov
, offset
, flags
, &total
);
969 clock_gettime(CLOCK_MONOTONIC
, &t2
);
972 printf("readv failed: %s\n", strerror(-ret
));
980 void *cmp_buf
= g_malloc(qiov
.size
);
981 memset(cmp_buf
, pattern
, qiov
.size
);
982 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
983 printf("Pattern verification failed at offset %"
984 PRId64
", %zu bytes\n", offset
, qiov
.size
);
995 dump_buffer(buf
, offset
, qiov
.size
);
998 /* Finally, report back -- -C gives a parsable format */
1000 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1003 qemu_io_free(blk
, buf
, qiov
.size
, flags
& BDRV_REQ_REGISTERED_BUF
);
1004 qemu_iovec_destroy(&qiov
);
1008 static void write_help(void)
1012 " writes a range of bytes from the given offset\n"
1015 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1017 " Writes into a segment of the currently open file, using a buffer\n"
1018 " filled with a set pattern (0xcdcdcdcd).\n"
1019 " -b, -- write to the VM state rather than the virtual disk\n"
1020 " -c, -- write compressed data with blk_write_compressed\n"
1021 " -C, -- report statistics in a machine parsable format\n"
1022 " -f, -- use Force Unit Access semantics\n"
1023 " -n, -- with -z, don't allow slow fallback\n"
1024 " -p, -- ignored for backwards compatibility\n"
1025 " -P, -- use different pattern to fill file\n"
1026 " -q, -- quiet mode, do not show I/O statistics\n"
1027 " -r, -- register I/O buffer\n"
1028 " -s, -- use a pattern file to fill the write buffer\n"
1029 " -u, -- with -z, allow unmapping\n"
1030 " -z, -- write zeroes using blk_pwrite_zeroes\n"
1034 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
1036 static const cmdinfo_t write_cmd
= {
1040 .perm
= BLK_PERM_WRITE
,
1043 .args
= "[-bcCfnqruz] [-P pattern | -s source_file] off len",
1044 .oneline
= "writes a number of bytes at a specified offset",
1048 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
1050 struct timespec t1
, t2
;
1051 bool Cflag
= false, qflag
= false, bflag
= false;
1052 bool Pflag
= false, zflag
= false, cflag
= false, sflag
= false;
1053 BdrvRequestFlags flags
= 0;
1058 /* Some compilers get confused and warn if this is not initialized. */
1061 const char *file_name
= NULL
;
1063 while ((c
= getopt(argc
, argv
, "bcCfnpP:qrs:uz")) != -1) {
1075 flags
|= BDRV_REQ_FUA
;
1078 flags
|= BDRV_REQ_NO_FALLBACK
;
1081 /* Ignored for backwards compatibility */
1085 pattern
= parse_pattern(optarg
);
1094 flags
|= BDRV_REQ_REGISTERED_BUF
;
1101 flags
|= BDRV_REQ_MAY_UNMAP
;
1107 qemuio_command_usage(&write_cmd
);
1112 if (optind
!= argc
- 2) {
1113 qemuio_command_usage(&write_cmd
);
1117 if (bflag
&& zflag
) {
1118 printf("-b and -z cannot be specified at the same time\n");
1122 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1123 printf("-f and -b or -c cannot be specified at the same time\n");
1127 if ((flags
& BDRV_REQ_NO_FALLBACK
) && !zflag
) {
1128 printf("-n requires -z to be specified\n");
1132 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1133 printf("-u requires -z to be specified\n");
1137 if (zflag
+ Pflag
+ sflag
> 1) {
1138 printf("Only one of -z, -P, and -s "
1139 "can be specified at the same time\n");
1143 offset
= cvtnum(argv
[optind
]);
1145 print_cvtnum_err(offset
, argv
[optind
]);
1150 count
= cvtnum(argv
[optind
]);
1152 print_cvtnum_err(count
, argv
[optind
]);
1154 } else if (count
> BDRV_REQUEST_MAX_BYTES
&&
1155 !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1156 printf("length cannot exceed %" PRIu64
" without -n, given %s\n",
1157 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1161 if (bflag
|| cflag
) {
1162 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1163 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1168 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1169 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1176 if (flags
& BDRV_REQ_REGISTERED_BUF
) {
1177 printf("cannot combine zero write with registered I/O buffer\n");
1182 buf
= qemu_io_alloc_from_file(blk
, count
, file_name
,
1183 flags
& BDRV_REQ_REGISTERED_BUF
);
1188 buf
= qemu_io_alloc(blk
, count
, pattern
,
1189 flags
& BDRV_REQ_REGISTERED_BUF
);
1193 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1195 ret
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1197 ret
= do_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1199 ret
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1201 ret
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1203 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1206 printf("write failed: %s\n", strerror(-ret
));
1217 /* Finally, report back -- -C gives a parsable format */
1219 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1223 qemu_io_free(blk
, buf
, count
, flags
& BDRV_REQ_REGISTERED_BUF
);
1233 " writes a range of bytes from the given offset source from multiple buffers\n"
1236 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1238 " Writes into a segment of the currently open file, using a buffer\n"
1239 " filled with a set pattern (0xcdcdcdcd).\n"
1240 " -C, -- report statistics in a machine parsable format\n"
1241 " -f, -- use Force Unit Access semantics\n"
1242 " -P, -- use different pattern to fill file\n"
1243 " -q, -- quiet mode, do not show I/O statistics\n"
1244 " -r, -- register I/O buffer\n"
1248 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1250 static const cmdinfo_t writev_cmd
= {
1253 .perm
= BLK_PERM_WRITE
,
1256 .args
= "[-Cfqr] [-P pattern] off len [len..]",
1257 .oneline
= "writes a number of bytes at a specified offset",
1258 .help
= writev_help
,
1261 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1263 struct timespec t1
, t2
;
1264 bool Cflag
= false, qflag
= false;
1265 BdrvRequestFlags flags
= 0;
1269 /* Some compilers get confused and warn if this is not initialized. */
1275 while ((c
= getopt(argc
, argv
, "CfP:qr")) != -1) {
1281 flags
|= BDRV_REQ_FUA
;
1287 flags
|= BDRV_REQ_REGISTERED_BUF
;
1290 pattern
= parse_pattern(optarg
);
1296 qemuio_command_usage(&writev_cmd
);
1301 if (optind
> argc
- 2) {
1302 qemuio_command_usage(&writev_cmd
);
1306 offset
= cvtnum(argv
[optind
]);
1308 print_cvtnum_err(offset
, argv
[optind
]);
1313 nr_iov
= argc
- optind
;
1314 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
,
1315 flags
& BDRV_REQ_REGISTERED_BUF
);
1320 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1321 ret
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1322 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1325 printf("writev failed: %s\n", strerror(-ret
));
1336 /* Finally, report back -- -C gives a parsable format */
1338 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1340 qemu_io_free(blk
, buf
, qiov
.size
, flags
& BDRV_REQ_REGISTERED_BUF
);
1341 qemu_iovec_destroy(&qiov
);
1355 BlockAcctCookie acct
;
1357 BdrvRequestFlags flags
;
1361 static void aio_write_done(void *opaque
, int ret
)
1363 struct aio_ctx
*ctx
= opaque
;
1366 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1370 printf("aio_write failed: %s\n", strerror(-ret
));
1371 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1375 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1381 /* Finally, report back -- -C gives a parsable format */
1382 t2
= tsub(t2
, ctx
->t1
);
1383 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1384 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1387 qemu_io_free(ctx
->blk
, ctx
->buf
, ctx
->qiov
.size
,
1388 ctx
->flags
& BDRV_REQ_REGISTERED_BUF
);
1389 qemu_iovec_destroy(&ctx
->qiov
);
1394 static void aio_read_done(void *opaque
, int ret
)
1396 struct aio_ctx
*ctx
= opaque
;
1399 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1402 printf("readv failed: %s\n", strerror(-ret
));
1403 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1408 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1410 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1411 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1412 printf("Pattern verification failed at offset %"
1413 PRId64
", %zu bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1418 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1425 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1428 /* Finally, report back -- -C gives a parsable format */
1429 t2
= tsub(t2
, ctx
->t1
);
1430 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1431 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1433 qemu_io_free(ctx
->blk
, ctx
->buf
, ctx
->qiov
.size
,
1434 ctx
->flags
& BDRV_REQ_REGISTERED_BUF
);
1435 qemu_iovec_destroy(&ctx
->qiov
);
1439 static void aio_read_help(void)
1443 " asynchronously reads a range of bytes from the given offset\n"
1446 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1448 " Reads a segment of the currently open file, optionally dumping it to the\n"
1449 " standard output stream (with -v option) for subsequent inspection.\n"
1450 " The read is performed asynchronously and the aio_flush command must be\n"
1451 " used to ensure all outstanding aio requests have been completed.\n"
1452 " Note that due to its asynchronous nature, this command will be\n"
1453 " considered successful once the request is submitted, independently\n"
1454 " of potential I/O errors or pattern mismatches.\n"
1455 " -C, -- report statistics in a machine parsable format\n"
1456 " -i, -- treat request as invalid, for exercising stats\n"
1457 " -P, -- use a pattern to verify read data\n"
1458 " -q, -- quiet mode, do not show I/O statistics\n"
1459 " -r, -- register I/O buffer\n"
1460 " -v, -- dump buffer to standard output\n"
1464 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1466 static const cmdinfo_t aio_read_cmd
= {
1468 .cfunc
= aio_read_f
,
1471 .args
= "[-Ciqrv] [-P pattern] off len [len..]",
1472 .oneline
= "asynchronously reads a number of bytes",
1473 .help
= aio_read_help
,
1476 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1479 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1482 while ((c
= getopt(argc
, argv
, "CiP:qrv")) != -1) {
1489 ctx
->pattern
= parse_pattern(optarg
);
1490 if (ctx
->pattern
< 0) {
1496 printf("injecting invalid read request\n");
1497 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1504 ctx
->flags
|= BDRV_REQ_REGISTERED_BUF
;
1511 qemuio_command_usage(&aio_read_cmd
);
1516 if (optind
> argc
- 2) {
1518 qemuio_command_usage(&aio_read_cmd
);
1522 ctx
->offset
= cvtnum(argv
[optind
]);
1523 if (ctx
->offset
< 0) {
1524 int ret
= ctx
->offset
;
1525 print_cvtnum_err(ret
, argv
[optind
]);
1531 nr_iov
= argc
- optind
;
1532 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab,
1533 ctx
->flags
& BDRV_REQ_REGISTERED_BUF
);
1534 if (ctx
->buf
== NULL
) {
1535 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1540 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1541 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1543 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, ctx
->flags
, aio_read_done
,
1548 static void aio_write_help(void)
1552 " asynchronously writes a range of bytes from the given offset source\n"
1553 " from multiple buffers\n"
1556 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1558 " Writes into a segment of the currently open file, using a buffer\n"
1559 " filled with a set pattern (0xcdcdcdcd).\n"
1560 " The write is performed asynchronously and the aio_flush command must be\n"
1561 " used to ensure all outstanding aio requests have been completed.\n"
1562 " Note that due to its asynchronous nature, this command will be\n"
1563 " considered successful once the request is submitted, independently\n"
1564 " of potential I/O errors or pattern mismatches.\n"
1565 " -C, -- report statistics in a machine parsable format\n"
1566 " -f, -- use Force Unit Access semantics\n"
1567 " -i, -- treat request as invalid, for exercising stats\n"
1568 " -P, -- use different pattern to fill file\n"
1569 " -q, -- quiet mode, do not show I/O statistics\n"
1570 " -r, -- register I/O buffer\n"
1571 " -u, -- with -z, allow unmapping\n"
1572 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1576 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1578 static const cmdinfo_t aio_write_cmd
= {
1579 .name
= "aio_write",
1580 .cfunc
= aio_write_f
,
1581 .perm
= BLK_PERM_WRITE
,
1584 .args
= "[-Cfiqruz] [-P pattern] off len [len..]",
1585 .oneline
= "asynchronously writes a number of bytes",
1586 .help
= aio_write_help
,
1589 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1593 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1596 while ((c
= getopt(argc
, argv
, "CfiP:qruz")) != -1) {
1602 ctx
->flags
|= BDRV_REQ_FUA
;
1608 ctx
->flags
|= BDRV_REQ_REGISTERED_BUF
;
1611 ctx
->flags
|= BDRV_REQ_MAY_UNMAP
;
1614 pattern
= parse_pattern(optarg
);
1621 printf("injecting invalid write request\n");
1622 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1630 qemuio_command_usage(&aio_write_cmd
);
1635 if (optind
> argc
- 2) {
1637 qemuio_command_usage(&aio_write_cmd
);
1641 if (ctx
->zflag
&& optind
!= argc
- 2) {
1642 printf("-z supports only a single length parameter\n");
1647 if ((ctx
->flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1648 printf("-u requires -z to be specified\n");
1653 if (ctx
->zflag
&& ctx
->Pflag
) {
1654 printf("-z and -P cannot be specified at the same time\n");
1659 if (ctx
->zflag
&& (ctx
->flags
& BDRV_REQ_REGISTERED_BUF
)) {
1660 printf("cannot combine zero write with registered I/O buffer\n");
1665 ctx
->offset
= cvtnum(argv
[optind
]);
1666 if (ctx
->offset
< 0) {
1667 int ret
= ctx
->offset
;
1668 print_cvtnum_err(ret
, argv
[optind
]);
1675 int64_t count
= cvtnum(argv
[optind
]);
1677 print_cvtnum_err(count
, argv
[optind
]);
1682 ctx
->qiov
.size
= count
;
1683 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, ctx
->flags
,
1684 aio_write_done
, ctx
);
1686 nr_iov
= argc
- optind
;
1687 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1688 pattern
, ctx
->flags
& BDRV_REQ_REGISTERED_BUF
);
1689 if (ctx
->buf
== NULL
) {
1690 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1695 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1696 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1699 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, ctx
->flags
,
1700 aio_write_done
, ctx
);
1706 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1708 BlockAcctCookie cookie
;
1709 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1711 block_acct_done(blk_get_stats(blk
), &cookie
);
1715 static const cmdinfo_t aio_flush_cmd
= {
1716 .name
= "aio_flush",
1717 .cfunc
= aio_flush_f
,
1718 .oneline
= "completes all outstanding aio requests"
1721 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1723 return blk_flush(blk
);
1726 static const cmdinfo_t flush_cmd
= {
1730 .oneline
= "flush all in-core file state to disk",
1733 static inline int64_t tosector(int64_t bytes
)
1735 return bytes
>> BDRV_SECTOR_BITS
;
1738 static int zone_report_f(BlockBackend
*blk
, int argc
, char **argv
)
1743 unsigned int nr_zones
;
1746 offset
= cvtnum(argv
[optind
]);
1748 print_cvtnum_err(offset
, argv
[optind
]);
1752 val
= cvtnum(argv
[optind
]);
1754 print_cvtnum_err(val
, argv
[optind
]);
1757 if (val
> UINT_MAX
) {
1758 printf("Number of zones must be less than 2^32\n");
1763 g_autofree BlockZoneDescriptor
*zones
= NULL
;
1764 zones
= g_new(BlockZoneDescriptor
, nr_zones
);
1765 ret
= blk_zone_report(blk
, offset
, &nr_zones
, zones
);
1767 printf("zone report failed: %s\n", strerror(-ret
));
1769 for (int i
= 0; i
< nr_zones
; ++i
) {
1770 printf("start: 0x%" PRIx64
", len 0x%" PRIx64
", "
1771 "cap"" 0x%" PRIx64
", wptr 0x%" PRIx64
", "
1772 "zcond:%u, [type: %u]\n",
1773 tosector(zones
[i
].start
), tosector(zones
[i
].length
),
1774 tosector(zones
[i
].cap
), tosector(zones
[i
].wp
),
1775 zones
[i
].state
, zones
[i
].type
);
1781 static const cmdinfo_t zone_report_cmd
= {
1782 .name
= "zone_report",
1784 .cfunc
= zone_report_f
,
1787 .args
= "offset number",
1788 .oneline
= "report zone information",
1791 static int zone_open_f(BlockBackend
*blk
, int argc
, char **argv
)
1794 int64_t offset
, len
;
1796 offset
= cvtnum(argv
[optind
]);
1798 print_cvtnum_err(offset
, argv
[optind
]);
1802 len
= cvtnum(argv
[optind
]);
1804 print_cvtnum_err(len
, argv
[optind
]);
1807 ret
= blk_zone_mgmt(blk
, BLK_ZO_OPEN
, offset
, len
);
1809 printf("zone open failed: %s\n", strerror(-ret
));
1814 static const cmdinfo_t zone_open_cmd
= {
1815 .name
= "zone_open",
1817 .cfunc
= zone_open_f
,
1820 .args
= "offset len",
1821 .oneline
= "explicit open a range of zones in zone block device",
1824 static int zone_close_f(BlockBackend
*blk
, int argc
, char **argv
)
1827 int64_t offset
, len
;
1829 offset
= cvtnum(argv
[optind
]);
1831 print_cvtnum_err(offset
, argv
[optind
]);
1835 len
= cvtnum(argv
[optind
]);
1837 print_cvtnum_err(len
, argv
[optind
]);
1840 ret
= blk_zone_mgmt(blk
, BLK_ZO_CLOSE
, offset
, len
);
1842 printf("zone close failed: %s\n", strerror(-ret
));
1847 static const cmdinfo_t zone_close_cmd
= {
1848 .name
= "zone_close",
1850 .cfunc
= zone_close_f
,
1853 .args
= "offset len",
1854 .oneline
= "close a range of zones in zone block device",
1857 static int zone_finish_f(BlockBackend
*blk
, int argc
, char **argv
)
1860 int64_t offset
, len
;
1862 offset
= cvtnum(argv
[optind
]);
1864 print_cvtnum_err(offset
, argv
[optind
]);
1868 len
= cvtnum(argv
[optind
]);
1870 print_cvtnum_err(len
, argv
[optind
]);
1873 ret
= blk_zone_mgmt(blk
, BLK_ZO_FINISH
, offset
, len
);
1875 printf("zone finish failed: %s\n", strerror(-ret
));
1880 static const cmdinfo_t zone_finish_cmd
= {
1881 .name
= "zone_finish",
1883 .cfunc
= zone_finish_f
,
1886 .args
= "offset len",
1887 .oneline
= "finish a range of zones in zone block device",
1890 static int zone_reset_f(BlockBackend
*blk
, int argc
, char **argv
)
1893 int64_t offset
, len
;
1895 offset
= cvtnum(argv
[optind
]);
1897 print_cvtnum_err(offset
, argv
[optind
]);
1901 len
= cvtnum(argv
[optind
]);
1903 print_cvtnum_err(len
, argv
[optind
]);
1906 ret
= blk_zone_mgmt(blk
, BLK_ZO_RESET
, offset
, len
);
1908 printf("zone reset failed: %s\n", strerror(-ret
));
1913 static const cmdinfo_t zone_reset_cmd
= {
1914 .name
= "zone_reset",
1916 .cfunc
= zone_reset_f
,
1919 .args
= "offset len",
1920 .oneline
= "reset a zone write pointer in zone block device",
1923 static int do_aio_zone_append(BlockBackend
*blk
, QEMUIOVector
*qiov
,
1924 int64_t *offset
, int flags
, int *total
)
1926 int async_ret
= NOT_DONE
;
1928 blk_aio_zone_append(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
1929 while (async_ret
== NOT_DONE
) {
1930 main_loop_wait(false);
1933 *total
= qiov
->size
;
1934 return async_ret
< 0 ? async_ret
: 1;
1937 static int zone_append_f(BlockBackend
*blk
, int argc
, char **argv
)
1949 if (optind
> argc
- 3) {
1953 if ((c
= getopt(argc
, argv
, "p")) != -1) {
1957 offset
= cvtnum(argv
[optind
]);
1959 print_cvtnum_err(offset
, argv
[optind
]);
1963 nr_iov
= argc
- optind
;
1964 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
,
1965 flags
& BDRV_REQ_REGISTERED_BUF
);
1969 ret
= do_aio_zone_append(blk
, &qiov
, &offset
, flags
, &total
);
1971 printf("zone append failed: %s\n", strerror(-ret
));
1976 printf("After zap done, the append sector is 0x%" PRIx64
"\n",
1981 qemu_io_free(blk
, buf
, qiov
.size
,
1982 flags
& BDRV_REQ_REGISTERED_BUF
);
1983 qemu_iovec_destroy(&qiov
);
1987 static const cmdinfo_t zone_append_cmd
= {
1988 .name
= "zone_append",
1990 .cfunc
= zone_append_f
,
1993 .args
= "offset len [len..]",
1994 .oneline
= "append write a number of bytes at a specified offset",
1997 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
);
1998 static const cmdinfo_t truncate_cmd
= {
2001 .cfunc
= truncate_f
,
2002 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
2005 .args
= "[-m prealloc_mode] off",
2006 .oneline
= "truncates the current file at the given offset",
2009 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
2011 Error
*local_err
= NULL
;
2014 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
2016 while ((c
= getopt(argc
, argv
, "m:")) != -1) {
2019 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
2020 PREALLOC_MODE__MAX
, NULL
);
2021 if (prealloc
== PREALLOC_MODE__MAX
) {
2022 error_report("Invalid preallocation mode '%s'", optarg
);
2027 qemuio_command_usage(&truncate_cmd
);
2032 offset
= cvtnum(argv
[optind
]);
2034 print_cvtnum_err(offset
, argv
[1]);
2039 * qemu-io is a debugging tool, so let us be strict here and pass
2040 * exact=true. It is better to err on the "emit more errors" side
2041 * than to be overly permissive.
2043 ret
= blk_truncate(blk
, offset
, false, prealloc
, 0, &local_err
);
2045 error_report_err(local_err
);
2052 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
2057 size
= blk_getlength(blk
);
2059 printf("getlength: %s\n", strerror(-size
));
2063 cvtstr(size
, s1
, sizeof(s1
));
2069 static const cmdinfo_t length_cmd
= {
2073 .oneline
= "gets the length of the current file",
2077 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
2079 BlockDriverState
*bs
= blk_bs(blk
);
2080 BlockDriverInfo bdi
;
2081 ImageInfoSpecific
*spec_info
;
2082 Error
*local_err
= NULL
;
2083 char s1
[64], s2
[64];
2086 GLOBAL_STATE_CODE();
2087 GRAPH_RDLOCK_GUARD_MAINLOOP();
2089 if (bs
->drv
&& bs
->drv
->format_name
) {
2090 printf("format name: %s\n", bs
->drv
->format_name
);
2092 if (bs
->drv
&& bs
->drv
->protocol_name
) {
2093 printf("format name: %s\n", bs
->drv
->protocol_name
);
2096 ret
= bdrv_get_info(bs
, &bdi
);
2101 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
2102 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
2104 printf("cluster size: %s\n", s1
);
2105 printf("vm state offset: %s\n", s2
);
2107 spec_info
= bdrv_get_specific_info(bs
, &local_err
);
2109 error_report_err(local_err
);
2113 bdrv_image_info_specific_dump(spec_info
,
2114 "Format specific information:\n",
2116 qapi_free_ImageInfoSpecific(spec_info
);
2124 static const cmdinfo_t info_cmd
= {
2128 .oneline
= "prints information about the current file",
2131 static void discard_help(void)
2135 " discards a range of bytes from the given offset\n"
2138 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
2140 " Discards a segment of the currently open file.\n"
2141 " -C, -- report statistics in a machine parsable format\n"
2142 " -q, -- quiet mode, do not show I/O statistics\n"
2146 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
2148 static const cmdinfo_t discard_cmd
= {
2152 .perm
= BLK_PERM_WRITE
,
2155 .args
= "[-Cq] off len",
2156 .oneline
= "discards a number of bytes at a specified offset",
2157 .help
= discard_help
,
2160 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
2162 struct timespec t1
, t2
;
2163 bool Cflag
= false, qflag
= false;
2165 int64_t offset
, bytes
;
2167 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
2176 qemuio_command_usage(&discard_cmd
);
2181 if (optind
!= argc
- 2) {
2182 qemuio_command_usage(&discard_cmd
);
2186 offset
= cvtnum(argv
[optind
]);
2188 print_cvtnum_err(offset
, argv
[optind
]);
2193 bytes
= cvtnum(argv
[optind
]);
2195 print_cvtnum_err(bytes
, argv
[optind
]);
2197 } else if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
2198 printf("length cannot exceed %"PRIu64
", given %s\n",
2199 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
2203 clock_gettime(CLOCK_MONOTONIC
, &t1
);
2204 ret
= blk_pdiscard(blk
, offset
, bytes
);
2205 clock_gettime(CLOCK_MONOTONIC
, &t2
);
2208 printf("discard failed: %s\n", strerror(-ret
));
2212 /* Finally, report back -- -C gives a parsable format */
2215 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
2221 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
2223 BlockDriverState
*bs
= blk_bs(blk
);
2224 int64_t offset
, start
, remaining
, count
;
2227 int64_t num
, sum_alloc
;
2229 start
= offset
= cvtnum(argv
[1]);
2231 print_cvtnum_err(offset
, argv
[1]);
2236 count
= cvtnum(argv
[2]);
2238 print_cvtnum_err(count
, argv
[2]);
2242 count
= BDRV_SECTOR_SIZE
;
2248 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
2250 printf("is_allocated failed: %s\n", strerror(-ret
));
2264 cvtstr(start
, s1
, sizeof(s1
));
2266 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
2267 sum_alloc
, count
, s1
);
2271 static const cmdinfo_t alloc_cmd
= {
2277 .args
= "offset [count]",
2278 .oneline
= "checks if offset is allocated in the file",
2282 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
2283 int64_t bytes
, int64_t *pnum
)
2288 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &num
);
2296 while (bytes
> 0 && ret
== firstret
) {
2300 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &num
);
2301 if (ret
== firstret
&& num
) {
2311 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
2313 int64_t offset
, bytes
;
2314 char s1
[64], s2
[64];
2320 bytes
= blk_getlength(blk
);
2322 error_report("Failed to query image length: %s", strerror(-bytes
));
2327 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
2329 error_report("Failed to get allocation status: %s", strerror(-ret
));
2332 error_report("Unexpected end of image");
2336 retstr
= ret
? " allocated" : "not allocated";
2337 cvtstr(num
, s1
, sizeof(s1
));
2338 cvtstr(offset
, s2
, sizeof(s2
));
2339 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
2340 s1
, num
, retstr
, s2
, offset
);
2349 static const cmdinfo_t map_cmd
= {
2355 .oneline
= "prints the allocated areas of a file",
2358 static void reopen_help(void)
2362 " Changes the open options of an already opened image\n"
2365 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2367 " -r, -- Reopen the image read-only\n"
2368 " -w, -- Reopen the image read-write\n"
2369 " -c, -- Change the cache mode to the given value\n"
2370 " -o, -- Changes block driver options (cf. 'open' command)\n"
2374 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2376 static QemuOptsList reopen_opts
= {
2378 .merge_lists
= true,
2379 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2381 /* no elements => accept any params */
2382 { /* end of list */ }
2386 static const cmdinfo_t reopen_cmd
= {
2391 .args
= "[(-r|-w)] [-c cache] [-o options]",
2392 .oneline
= "reopens an image with new options",
2393 .help
= reopen_help
,
2396 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2398 BlockDriverState
*bs
= blk_bs(blk
);
2402 int flags
= bs
->open_flags
;
2403 bool writethrough
= !blk_enable_write_cache(blk
);
2404 bool has_rw_option
= false;
2405 bool has_cache_option
= false;
2406 Error
*local_err
= NULL
;
2408 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
2411 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
2412 error_report("Invalid cache option: %s", optarg
);
2415 has_cache_option
= true;
2418 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2419 qemu_opts_reset(&reopen_opts
);
2424 if (has_rw_option
) {
2425 error_report("Only one -r/-w option may be given");
2428 flags
&= ~BDRV_O_RDWR
;
2429 has_rw_option
= true;
2432 if (has_rw_option
) {
2433 error_report("Only one -r/-w option may be given");
2436 flags
|= BDRV_O_RDWR
;
2437 has_rw_option
= true;
2440 qemu_opts_reset(&reopen_opts
);
2441 qemuio_command_usage(&reopen_cmd
);
2446 if (optind
!= argc
) {
2447 qemu_opts_reset(&reopen_opts
);
2448 qemuio_command_usage(&reopen_cmd
);
2452 if (!writethrough
!= blk_enable_write_cache(blk
) &&
2453 blk_get_attached_dev(blk
))
2455 error_report("Cannot change cache.writeback: Device attached");
2456 qemu_opts_reset(&reopen_opts
);
2460 if (!(flags
& BDRV_O_RDWR
)) {
2461 uint64_t orig_perm
, orig_shared_perm
;
2465 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2467 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2472 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2473 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : qdict_new();
2474 qemu_opts_reset(&reopen_opts
);
2476 if (qdict_haskey(opts
, BDRV_OPT_READ_ONLY
)) {
2477 if (has_rw_option
) {
2478 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY
"'");
2479 qobject_unref(opts
);
2483 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !(flags
& BDRV_O_RDWR
));
2486 if (qdict_haskey(opts
, BDRV_OPT_CACHE_DIRECT
) ||
2487 qdict_haskey(opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
2488 if (has_cache_option
) {
2489 error_report("Cannot set both -c and the cache options");
2490 qobject_unref(opts
);
2494 qdict_put_bool(opts
, BDRV_OPT_CACHE_DIRECT
, flags
& BDRV_O_NOCACHE
);
2495 qdict_put_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, flags
& BDRV_O_NO_FLUSH
);
2498 bdrv_reopen(bs
, opts
, true, &local_err
);
2501 error_report_err(local_err
);
2505 blk_set_enable_write_cache(blk
, !writethrough
);
2509 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2513 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2515 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2522 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2526 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2528 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2535 static const cmdinfo_t break_cmd
= {
2540 .args
= "event tag",
2541 .oneline
= "sets a breakpoint on event and tags the stopped "
2545 static const cmdinfo_t remove_break_cmd
= {
2546 .name
= "remove_break",
2549 .cfunc
= remove_break_f
,
2551 .oneline
= "remove a breakpoint by tag",
2554 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2558 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2560 printf("Could not resume request: %s\n", strerror(-ret
));
2567 static const cmdinfo_t resume_cmd
= {
2573 .oneline
= "resumes the request tagged as tag",
2576 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2578 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2579 aio_poll(blk_get_aio_context(blk
), true);
2584 static const cmdinfo_t wait_break_cmd
= {
2585 .name
= "wait_break",
2588 .cfunc
= wait_break_f
,
2590 .oneline
= "waits for the suspension of a request",
2593 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2598 static const cmdinfo_t abort_cmd
= {
2601 .flags
= CMD_NOFILE_OK
,
2602 .oneline
= "simulate a program crash using abort(3)",
2605 static void sigraise_help(void)
2609 " raises the given signal\n"
2612 " 'sigraise %i' - raises SIGTERM\n"
2614 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2615 " given to sigraise.\n"
2619 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2621 static const cmdinfo_t sigraise_cmd
= {
2623 .cfunc
= sigraise_f
,
2626 .flags
= CMD_NOFILE_OK
,
2628 .oneline
= "raises a signal",
2629 .help
= sigraise_help
,
2632 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2634 int64_t sig
= cvtnum(argv
[1]);
2636 print_cvtnum_err(sig
, argv
[1]);
2638 } else if (sig
> NSIG
) {
2639 printf("signal argument '%s' is too large to be a valid signal\n",
2644 /* Using raise() to kill this process does not necessarily flush all open
2645 * streams. At least stdout and stderr (although the latter should be
2646 * non-buffered anyway) should be flushed, though. */
2655 static void sleep_cb(void *opaque
)
2657 bool *expired
= opaque
;
2661 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2665 struct QEMUTimer
*timer
;
2666 bool expired
= false;
2668 ms
= strtol(argv
[1], &endptr
, 0);
2669 if (ms
< 0 || *endptr
!= '\0') {
2670 printf("%s is not a valid number\n", argv
[1]);
2674 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2675 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2678 main_loop_wait(false);
2685 static const cmdinfo_t sleep_cmd
= {
2690 .flags
= CMD_NOFILE_OK
,
2691 .oneline
= "waits for the given value in milliseconds",
2694 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2699 printf("%s ", ct
->args
);
2701 printf("-- %s\n", ct
->oneline
);
2704 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2706 help_oneline(cmd
, ct
);
2712 static void help_all(void)
2714 const cmdinfo_t
*ct
;
2716 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2717 help_oneline(ct
->name
, ct
);
2719 printf("\nUse 'help commandname' for extended help.\n");
2722 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2724 const cmdinfo_t
*ct
;
2731 ct
= find_command(argv
[1]);
2733 printf("command %s not found\n", argv
[1]);
2737 help_onecmd(argv
[1], ct
);
2741 static const cmdinfo_t help_cmd
= {
2747 .flags
= CMD_FLAG_GLOBAL
,
2748 .args
= "[command]",
2749 .oneline
= "help for one or all commands",
2753 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2754 * context acquired if blk is NULL.
2756 int qemuio_command(BlockBackend
*blk
, const char *cmd
)
2759 const cmdinfo_t
*ct
;
2764 input
= g_strdup(cmd
);
2765 v
= breakline(input
, &c
);
2767 ct
= find_command(v
[0]);
2769 ret
= command(blk
, ct
, c
, v
);
2771 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2781 static void __attribute((constructor
)) init_qemuio_commands(void)
2783 /* initialize commands */
2784 qemuio_add_command(&help_cmd
);
2785 qemuio_add_command(&read_cmd
);
2786 qemuio_add_command(&readv_cmd
);
2787 qemuio_add_command(&write_cmd
);
2788 qemuio_add_command(&writev_cmd
);
2789 qemuio_add_command(&aio_read_cmd
);
2790 qemuio_add_command(&aio_write_cmd
);
2791 qemuio_add_command(&aio_flush_cmd
);
2792 qemuio_add_command(&flush_cmd
);
2793 qemuio_add_command(&zone_report_cmd
);
2794 qemuio_add_command(&zone_open_cmd
);
2795 qemuio_add_command(&zone_close_cmd
);
2796 qemuio_add_command(&zone_finish_cmd
);
2797 qemuio_add_command(&zone_reset_cmd
);
2798 qemuio_add_command(&zone_append_cmd
);
2799 qemuio_add_command(&truncate_cmd
);
2800 qemuio_add_command(&length_cmd
);
2801 qemuio_add_command(&info_cmd
);
2802 qemuio_add_command(&discard_cmd
);
2803 qemuio_add_command(&alloc_cmd
);
2804 qemuio_add_command(&map_cmd
);
2805 qemuio_add_command(&reopen_cmd
);
2806 qemuio_add_command(&break_cmd
);
2807 qemuio_add_command(&remove_break_cmd
);
2808 qemuio_add_command(&resume_cmd
);
2809 qemuio_add_command(&wait_break_cmd
);
2810 qemuio_add_command(&abort_cmd
);
2811 qemuio_add_command(&sleep_cmd
);
2812 qemuio_add_command(&sigraise_cmd
);