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"
25 #define CMD_NOFILE_OK 0x01
29 static cmdinfo_t
*cmdtab
;
32 static int compare_cmdname(const void *a
, const void *b
)
34 return strcmp(((const cmdinfo_t
*)a
)->name
,
35 ((const cmdinfo_t
*)b
)->name
);
38 void qemuio_add_command(const cmdinfo_t
*ci
)
40 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
41 * flags allow it not to be, so that combination is invalid.
42 * Catch it now rather than letting it manifest as a crash if a
43 * particular set of command line options are used.
45 assert(ci
->perm
== 0 ||
46 (ci
->flags
& (CMD_FLAG_GLOBAL
| CMD_NOFILE_OK
)) == 0);
47 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
48 cmdtab
[ncmds
- 1] = *ci
;
49 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
52 void qemuio_command_usage(const cmdinfo_t
*ci
)
54 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
57 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
59 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
62 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
63 fprintf(stderr
, "no file open, try 'help open'\n");
69 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
74 if (!init_check_command(blk
, ct
)) {
78 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
79 if (ct
->argmax
== -1) {
81 "bad argument count %d to %s, expected at least %d arguments\n",
82 argc
-1, cmd
, ct
->argmin
);
83 } else if (ct
->argmin
== ct
->argmax
) {
85 "bad argument count %d to %s, expected %d arguments\n",
86 argc
-1, cmd
, ct
->argmin
);
89 "bad argument count %d to %s, expected between %d and %d arguments\n",
90 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
96 * Request additional permissions if necessary for this command. The caller
97 * is responsible for restoring the original permissions afterwards if this
100 * Coverity thinks that blk may be NULL in the following if condition. It's
101 * not so: in init_check_command() we fail if blk is NULL for command with
102 * both CMD_FLAG_GLOBAL and CMD_NOFILE_OK flags unset. And in
103 * qemuio_add_command() we assert that command with non-zero .perm field
104 * doesn't set this flags. So, the following assertion is to silence
107 assert(blk
|| !ct
->perm
);
108 if (ct
->perm
&& blk_is_available(blk
)) {
109 uint64_t orig_perm
, orig_shared_perm
;
110 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
112 if (ct
->perm
& ~orig_perm
) {
114 Error
*local_err
= NULL
;
117 new_perm
= orig_perm
| ct
->perm
;
119 ret
= blk_set_perm(blk
, new_perm
, orig_shared_perm
, &local_err
);
121 error_report_err(local_err
);
128 return ct
->cfunc(blk
, argc
, argv
);
131 static const cmdinfo_t
*find_command(const char *cmd
)
135 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
136 if (strcmp(ct
->name
, cmd
) == 0 ||
137 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
139 return (const cmdinfo_t
*)ct
;
145 /* Invoke fn() for commands with a matching prefix */
146 void qemuio_complete_command(const char *input
,
147 void (*fn
)(const char *cmd
, void *opaque
),
151 size_t input_len
= strlen(input
);
153 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
154 if (strncmp(input
, ct
->name
, input_len
) == 0) {
155 fn(ct
->name
, opaque
);
160 static char **breakline(char *input
, int *count
)
164 char **rval
= g_new0(char *, 1);
166 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
171 rval
= g_renew(char *, rval
, (c
+ 1));
179 static int64_t cvtnum(const char *s
)
184 err
= qemu_strtosz(s
, NULL
, &value
);
188 if (value
> INT64_MAX
) {
194 static void print_cvtnum_err(int64_t rc
, const char *arg
)
198 printf("Parsing error: non-numeric argument,"
199 " or extraneous/unrecognized suffix -- %s\n", arg
);
202 printf("Parsing error: argument too large -- %s\n", arg
);
205 printf("Parsing error: %s\n", arg
);
209 #define EXABYTES(x) ((long long)(x) << 60)
210 #define PETABYTES(x) ((long long)(x) << 50)
211 #define TERABYTES(x) ((long long)(x) << 40)
212 #define GIGABYTES(x) ((long long)(x) << 30)
213 #define MEGABYTES(x) ((long long)(x) << 20)
214 #define KILOBYTES(x) ((long long)(x) << 10)
216 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
217 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
218 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
219 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
220 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
221 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
223 static void cvtstr(double value
, char *str
, size_t size
)
228 if (value
>= EXABYTES(1)) {
230 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
231 } else if (value
>= PETABYTES(1)) {
233 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
234 } else if (value
>= TERABYTES(1)) {
236 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
237 } else if (value
>= GIGABYTES(1)) {
239 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
240 } else if (value
>= MEGABYTES(1)) {
242 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
243 } else if (value
>= KILOBYTES(1)) {
245 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
248 snprintf(str
, size
- 6, "%f", value
);
251 trim
= strstr(str
, ".000");
253 strcpy(trim
, suffix
);
261 static struct timespec
tsub(struct timespec t1
, struct timespec t2
)
263 t1
.tv_nsec
-= t2
.tv_nsec
;
264 if (t1
.tv_nsec
< 0) {
265 t1
.tv_nsec
+= NANOSECONDS_PER_SECOND
;
268 t1
.tv_sec
-= t2
.tv_sec
;
272 static double tdiv(double value
, struct timespec tv
)
274 double seconds
= tv
.tv_sec
+ (tv
.tv_nsec
/ 1e9
);
275 return value
/ seconds
;
278 #define HOURS(sec) ((sec) / (60 * 60))
279 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
280 #define SECONDS(sec) ((sec) % 60)
284 TERSE_FIXED_TIME
= 0x1,
285 VERBOSE_FIXED_TIME
= 0x2,
288 static void timestr(struct timespec
*tv
, char *ts
, size_t size
, int format
)
290 double frac_sec
= tv
->tv_nsec
/ 1e9
;
292 if (format
& TERSE_FIXED_TIME
) {
293 if (!HOURS(tv
->tv_sec
)) {
294 snprintf(ts
, size
, "%u:%05.2f",
295 (unsigned int) MINUTES(tv
->tv_sec
),
296 SECONDS(tv
->tv_sec
) + frac_sec
);
299 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
302 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
303 snprintf(ts
, size
, "%u:%02u:%05.2f",
304 (unsigned int) HOURS(tv
->tv_sec
),
305 (unsigned int) MINUTES(tv
->tv_sec
),
306 SECONDS(tv
->tv_sec
) + frac_sec
);
308 snprintf(ts
, size
, "%05.2f sec", frac_sec
);
313 * Parse the pattern argument to various sub-commands.
315 * Because the pattern is used as an argument to memset it must evaluate
316 * to an unsigned integer that fits into a single byte.
318 static int parse_pattern(const char *arg
)
323 pattern
= strtol(arg
, &endptr
, 0);
324 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
325 printf("%s is not a valid pattern byte\n", arg
);
333 * Memory allocation helpers.
335 * Make sure memory is aligned by default, or purposefully misaligned if
336 * that is specified on the command line.
339 #define MISALIGN_OFFSET 16
340 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
344 if (qemuio_misalign
) {
345 len
+= MISALIGN_OFFSET
;
347 buf
= blk_blockalign(blk
, len
);
348 memset(buf
, pattern
, len
);
349 if (qemuio_misalign
) {
350 buf
+= MISALIGN_OFFSET
;
355 static void qemu_io_free(void *p
)
357 if (qemuio_misalign
) {
358 p
-= MISALIGN_OFFSET
;
364 * qemu_io_alloc_from_file()
366 * Allocates the buffer and populates it with the content of the given file
367 * up to @len bytes. If the file length is less than @len, then the buffer
368 * is populated with the file content cyclically.
370 * @blk - the block backend where the buffer content is going to be written to
371 * @len - the buffer length
372 * @file_name - the file to read the content from
374 * Returns: the buffer pointer on success
377 static void *qemu_io_alloc_from_file(BlockBackend
*blk
, size_t len
,
378 const char *file_name
)
380 char *buf
, *buf_origin
;
381 FILE *f
= fopen(file_name
, "r");
389 if (qemuio_misalign
) {
390 len
+= MISALIGN_OFFSET
;
393 buf_origin
= buf
= blk_blockalign(blk
, len
);
395 if (qemuio_misalign
) {
396 buf_origin
+= MISALIGN_OFFSET
;
397 buf
+= MISALIGN_OFFSET
;
398 len
-= MISALIGN_OFFSET
;
401 pattern_len
= fread(buf_origin
, 1, len
, f
);
408 if (pattern_len
== 0) {
409 fprintf(stderr
, "%s: file is empty\n", file_name
);
416 if (len
> pattern_len
) {
421 size_t len_to_copy
= MIN(pattern_len
, len
);
423 memcpy(buf
, buf_origin
, len_to_copy
);
433 qemu_io_free(buf_origin
);
440 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
446 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
447 const uint8_t *s
= p
;
449 printf("%08" PRIx64
": ", offset
+ i
);
450 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
454 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
465 static void print_report(const char *op
, struct timespec
*t
, int64_t offset
,
466 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
468 char s1
[64], s2
[64], ts
[64];
470 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
472 cvtstr((double)total
, s1
, sizeof(s1
));
473 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
474 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
475 op
, total
, count
, offset
);
476 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
477 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
478 } else {/* bytes,ops,time,bytes/sec,ops/sec */
479 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
481 tdiv((double)total
, *t
),
482 tdiv((double)cnt
, *t
));
487 * Parse multiple length statements for vectored I/O, and construct an I/O
488 * vector matching it.
491 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
494 size_t *sizes
= g_new0(size_t, nr_iov
);
500 for (i
= 0; i
< nr_iov
; i
++) {
506 print_cvtnum_err(len
, arg
);
510 if (len
> BDRV_REQUEST_MAX_BYTES
) {
511 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
512 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
516 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
517 printf("The total number of bytes exceed the maximum size %" PRIu64
518 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
526 qemu_iovec_init(qiov
, nr_iov
);
528 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
530 for (i
= 0; i
< nr_iov
; i
++) {
531 qemu_iovec_add(qiov
, p
, sizes
[i
]);
540 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
541 int64_t bytes
, int64_t *total
)
543 if (bytes
> INT_MAX
) {
547 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, bytes
);
554 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
555 int64_t bytes
, int flags
, int64_t *total
)
557 if (bytes
> INT_MAX
) {
561 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, bytes
, flags
);
578 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
580 CoWriteZeroes
*data
= opaque
;
582 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->bytes
,
586 *data
->total
= data
->ret
;
590 *data
->total
= data
->bytes
;
593 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
594 int64_t bytes
, int flags
, int64_t *total
)
597 CoWriteZeroes data
= {
606 if (bytes
> INT_MAX
) {
610 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
611 bdrv_coroutine_enter(blk_bs(blk
), co
);
613 aio_poll(blk_get_aio_context(blk
), true);
622 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
623 int64_t bytes
, int64_t *total
)
627 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
631 ret
= blk_pwrite_compressed(blk
, offset
, buf
, bytes
);
639 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
640 int64_t count
, int64_t *total
)
642 if (count
> INT_MAX
) {
646 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
653 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
654 int64_t count
, int64_t *total
)
656 if (count
> INT_MAX
) {
660 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
667 #define NOT_DONE 0x7fffffff
668 static void aio_rw_done(void *opaque
, int ret
)
670 *(int *)opaque
= ret
;
673 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
674 int64_t offset
, int *total
)
676 int async_ret
= NOT_DONE
;
678 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
679 while (async_ret
== NOT_DONE
) {
680 main_loop_wait(false);
684 return async_ret
< 0 ? async_ret
: 1;
687 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
688 int64_t offset
, int flags
, int *total
)
690 int async_ret
= NOT_DONE
;
692 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
693 while (async_ret
== NOT_DONE
) {
694 main_loop_wait(false);
698 return async_ret
< 0 ? async_ret
: 1;
701 static void read_help(void)
705 " reads a range of bytes from the given offset\n"
708 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
710 " Reads a segment of the currently open file, optionally dumping it to the\n"
711 " standard output stream (with -v option) for subsequent inspection.\n"
712 " -b, -- read from the VM state rather than the virtual disk\n"
713 " -C, -- report statistics in a machine parsable format\n"
714 " -l, -- length for pattern verification (only with -P)\n"
715 " -p, -- ignored for backwards compatibility\n"
716 " -P, -- use a pattern to verify read data\n"
717 " -q, -- quiet mode, do not show I/O statistics\n"
718 " -s, -- start offset for pattern verification (only with -P)\n"
719 " -v, -- dump buffer to standard output\n"
723 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
725 static const cmdinfo_t read_cmd
= {
731 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
732 .oneline
= "reads a number of bytes at a specified offset",
736 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
738 struct timespec t1
, t2
;
739 bool Cflag
= false, qflag
= false, vflag
= false;
740 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
745 /* Some compilers get confused and warn if this is not initialized. */
748 int64_t pattern_offset
= 0, pattern_count
= 0;
750 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
760 pattern_count
= cvtnum(optarg
);
761 if (pattern_count
< 0) {
762 print_cvtnum_err(pattern_count
, optarg
);
763 return pattern_count
;
767 /* Ignored for backwards compatibility */
771 pattern
= parse_pattern(optarg
);
781 pattern_offset
= cvtnum(optarg
);
782 if (pattern_offset
< 0) {
783 print_cvtnum_err(pattern_offset
, optarg
);
784 return pattern_offset
;
791 qemuio_command_usage(&read_cmd
);
796 if (optind
!= argc
- 2) {
797 qemuio_command_usage(&read_cmd
);
801 offset
= cvtnum(argv
[optind
]);
803 print_cvtnum_err(offset
, argv
[optind
]);
808 count
= cvtnum(argv
[optind
]);
810 print_cvtnum_err(count
, argv
[optind
]);
812 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
813 printf("length cannot exceed %" PRIu64
", given %s\n",
814 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
818 if (!Pflag
&& (lflag
|| sflag
)) {
819 qemuio_command_usage(&read_cmd
);
824 pattern_count
= count
- pattern_offset
;
827 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
828 printf("pattern verification range exceeds end of read data\n");
833 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
834 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
838 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
839 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
845 buf
= qemu_io_alloc(blk
, count
, 0xab);
847 clock_gettime(CLOCK_MONOTONIC
, &t1
);
849 ret
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
851 ret
= do_pread(blk
, buf
, offset
, count
, &total
);
853 clock_gettime(CLOCK_MONOTONIC
, &t2
);
856 printf("read failed: %s\n", strerror(-ret
));
864 void *cmp_buf
= g_malloc(pattern_count
);
865 memset(cmp_buf
, pattern
, pattern_count
);
866 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
867 printf("Pattern verification failed at offset %"
868 PRId64
", %"PRId64
" bytes\n",
869 offset
+ pattern_offset
, pattern_count
);
880 dump_buffer(buf
, offset
, count
);
883 /* Finally, report back -- -C gives a parsable format */
885 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
892 static void readv_help(void)
896 " reads a range of bytes from the given offset into multiple buffers\n"
899 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
901 " Reads a segment of the currently open file, optionally dumping it to the\n"
902 " standard output stream (with -v option) for subsequent inspection.\n"
903 " Uses multiple iovec buffers if more than one byte range is specified.\n"
904 " -C, -- report statistics in a machine parsable format\n"
905 " -P, -- use a pattern to verify read data\n"
906 " -v, -- dump buffer to standard output\n"
907 " -q, -- quiet mode, do not show I/O statistics\n"
911 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
913 static const cmdinfo_t readv_cmd
= {
918 .args
= "[-Cqv] [-P pattern] off len [len..]",
919 .oneline
= "reads a number of bytes at a specified offset",
923 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
925 struct timespec t1
, t2
;
926 bool Cflag
= false, qflag
= false, vflag
= false;
930 /* Some compilers get confused and warn if this is not initialized. */
937 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
944 pattern
= parse_pattern(optarg
);
956 qemuio_command_usage(&readv_cmd
);
961 if (optind
> argc
- 2) {
962 qemuio_command_usage(&readv_cmd
);
967 offset
= cvtnum(argv
[optind
]);
969 print_cvtnum_err(offset
, argv
[optind
]);
974 nr_iov
= argc
- optind
;
975 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
980 clock_gettime(CLOCK_MONOTONIC
, &t1
);
981 ret
= do_aio_readv(blk
, &qiov
, offset
, &total
);
982 clock_gettime(CLOCK_MONOTONIC
, &t2
);
985 printf("readv failed: %s\n", strerror(-ret
));
993 void *cmp_buf
= g_malloc(qiov
.size
);
994 memset(cmp_buf
, pattern
, qiov
.size
);
995 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
996 printf("Pattern verification failed at offset %"
997 PRId64
", %zu bytes\n", offset
, qiov
.size
);
1008 dump_buffer(buf
, offset
, qiov
.size
);
1011 /* Finally, report back -- -C gives a parsable format */
1013 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1016 qemu_iovec_destroy(&qiov
);
1021 static void write_help(void)
1025 " writes a range of bytes from the given offset\n"
1028 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1030 " Writes into a segment of the currently open file, using a buffer\n"
1031 " filled with a set pattern (0xcdcdcdcd).\n"
1032 " -b, -- write to the VM state rather than the virtual disk\n"
1033 " -c, -- write compressed data with blk_write_compressed\n"
1034 " -f, -- use Force Unit Access semantics\n"
1035 " -n, -- with -z, don't allow slow fallback\n"
1036 " -p, -- ignored for backwards compatibility\n"
1037 " -P, -- use different pattern to fill file\n"
1038 " -s, -- use a pattern file to fill the write buffer\n"
1039 " -C, -- report statistics in a machine parsable format\n"
1040 " -q, -- quiet mode, do not show I/O statistics\n"
1041 " -u, -- with -z, allow unmapping\n"
1042 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
1046 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
1048 static const cmdinfo_t write_cmd
= {
1052 .perm
= BLK_PERM_WRITE
,
1055 .args
= "[-bcCfnquz] [-P pattern | -s source_file] off len",
1056 .oneline
= "writes a number of bytes at a specified offset",
1060 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
1062 struct timespec t1
, t2
;
1063 bool Cflag
= false, qflag
= false, bflag
= false;
1064 bool Pflag
= false, zflag
= false, cflag
= false, sflag
= false;
1070 /* Some compilers get confused and warn if this is not initialized. */
1073 const char *file_name
= NULL
;
1075 while ((c
= getopt(argc
, argv
, "bcCfnpP:qs:uz")) != -1) {
1087 flags
|= BDRV_REQ_FUA
;
1090 flags
|= BDRV_REQ_NO_FALLBACK
;
1093 /* Ignored for backwards compatibility */
1097 pattern
= parse_pattern(optarg
);
1110 flags
|= BDRV_REQ_MAY_UNMAP
;
1116 qemuio_command_usage(&write_cmd
);
1121 if (optind
!= argc
- 2) {
1122 qemuio_command_usage(&write_cmd
);
1126 if (bflag
&& zflag
) {
1127 printf("-b and -z cannot be specified at the same time\n");
1131 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1132 printf("-f and -b or -c cannot be specified at the same time\n");
1136 if ((flags
& BDRV_REQ_NO_FALLBACK
) && !zflag
) {
1137 printf("-n requires -z to be specified\n");
1141 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1142 printf("-u requires -z to be specified\n");
1146 if (zflag
+ Pflag
+ sflag
> 1) {
1147 printf("Only one of -z, -P, and -s "
1148 "can be specified at the same time\n");
1152 offset
= cvtnum(argv
[optind
]);
1154 print_cvtnum_err(offset
, argv
[optind
]);
1159 count
= cvtnum(argv
[optind
]);
1161 print_cvtnum_err(count
, argv
[optind
]);
1163 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1164 printf("length cannot exceed %" PRIu64
", given %s\n",
1165 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1169 if (bflag
|| cflag
) {
1170 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1171 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1176 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1177 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1185 buf
= qemu_io_alloc_from_file(blk
, count
, file_name
);
1190 buf
= qemu_io_alloc(blk
, count
, pattern
);
1194 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1196 ret
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1198 ret
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1200 ret
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1202 ret
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1204 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1207 printf("write failed: %s\n", strerror(-ret
));
1218 /* Finally, report back -- -C gives a parsable format */
1220 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1234 " writes a range of bytes from the given offset source from multiple buffers\n"
1237 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1239 " Writes into a segment of the currently open file, using a buffer\n"
1240 " filled with a set pattern (0xcdcdcdcd).\n"
1241 " -P, -- use different pattern to fill file\n"
1242 " -C, -- report statistics in a machine parsable format\n"
1243 " -f, -- use Force Unit Access semantics\n"
1244 " -q, -- quiet mode, do not show I/O statistics\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
= "[-Cfq] [-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;
1269 /* Some compilers get confused and warn if this is not initialized. */
1275 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1281 flags
|= BDRV_REQ_FUA
;
1287 pattern
= parse_pattern(optarg
);
1293 qemuio_command_usage(&writev_cmd
);
1298 if (optind
> argc
- 2) {
1299 qemuio_command_usage(&writev_cmd
);
1303 offset
= cvtnum(argv
[optind
]);
1305 print_cvtnum_err(offset
, argv
[optind
]);
1310 nr_iov
= argc
- optind
;
1311 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1316 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1317 ret
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1318 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1321 printf("writev failed: %s\n", strerror(-ret
));
1332 /* Finally, report back -- -C gives a parsable format */
1334 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1336 qemu_iovec_destroy(&qiov
);
1351 BlockAcctCookie acct
;
1356 static void aio_write_done(void *opaque
, int ret
)
1358 struct aio_ctx
*ctx
= opaque
;
1361 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1365 printf("aio_write failed: %s\n", strerror(-ret
));
1366 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1370 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1376 /* Finally, report back -- -C gives a parsable format */
1377 t2
= tsub(t2
, ctx
->t1
);
1378 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1379 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1382 qemu_io_free(ctx
->buf
);
1383 qemu_iovec_destroy(&ctx
->qiov
);
1388 static void aio_read_done(void *opaque
, int ret
)
1390 struct aio_ctx
*ctx
= opaque
;
1393 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1396 printf("readv failed: %s\n", strerror(-ret
));
1397 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1402 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1404 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1405 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1406 printf("Pattern verification failed at offset %"
1407 PRId64
", %zu bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1412 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1419 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1422 /* Finally, report back -- -C gives a parsable format */
1423 t2
= tsub(t2
, ctx
->t1
);
1424 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1425 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1427 qemu_io_free(ctx
->buf
);
1428 qemu_iovec_destroy(&ctx
->qiov
);
1432 static void aio_read_help(void)
1436 " asynchronously reads a range of bytes from the given offset\n"
1439 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1441 " Reads a segment of the currently open file, optionally dumping it to the\n"
1442 " standard output stream (with -v option) for subsequent inspection.\n"
1443 " The read is performed asynchronously and the aio_flush command must be\n"
1444 " used to ensure all outstanding aio requests have been completed.\n"
1445 " Note that due to its asynchronous nature, this command will be\n"
1446 " considered successful once the request is submitted, independently\n"
1447 " of potential I/O errors or pattern mismatches.\n"
1448 " -C, -- report statistics in a machine parsable format\n"
1449 " -P, -- use a pattern to verify read data\n"
1450 " -i, -- treat request as invalid, for exercising stats\n"
1451 " -v, -- dump buffer to standard output\n"
1452 " -q, -- quiet mode, do not show I/O statistics\n"
1456 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1458 static const cmdinfo_t aio_read_cmd
= {
1460 .cfunc
= aio_read_f
,
1463 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1464 .oneline
= "asynchronously reads a number of bytes",
1465 .help
= aio_read_help
,
1468 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1471 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1474 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1481 ctx
->pattern
= parse_pattern(optarg
);
1482 if (ctx
->pattern
< 0) {
1488 printf("injecting invalid read request\n");
1489 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1500 qemuio_command_usage(&aio_read_cmd
);
1505 if (optind
> argc
- 2) {
1507 qemuio_command_usage(&aio_read_cmd
);
1511 ctx
->offset
= cvtnum(argv
[optind
]);
1512 if (ctx
->offset
< 0) {
1513 int ret
= ctx
->offset
;
1514 print_cvtnum_err(ret
, argv
[optind
]);
1520 nr_iov
= argc
- optind
;
1521 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1522 if (ctx
->buf
== NULL
) {
1523 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1528 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1529 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1531 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1535 static void aio_write_help(void)
1539 " asynchronously writes a range of bytes from the given offset source\n"
1540 " from multiple buffers\n"
1543 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1545 " Writes into a segment of the currently open file, using a buffer\n"
1546 " filled with a set pattern (0xcdcdcdcd).\n"
1547 " The write is performed asynchronously and the aio_flush command must be\n"
1548 " used to ensure all outstanding aio requests have been completed.\n"
1549 " Note that due to its asynchronous nature, this command will be\n"
1550 " considered successful once the request is submitted, independently\n"
1551 " of potential I/O errors or pattern mismatches.\n"
1552 " -P, -- use different pattern to fill file\n"
1553 " -C, -- report statistics in a machine parsable format\n"
1554 " -f, -- use Force Unit Access semantics\n"
1555 " -i, -- treat request as invalid, for exercising stats\n"
1556 " -q, -- quiet mode, do not show I/O statistics\n"
1557 " -u, -- with -z, allow unmapping\n"
1558 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1562 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1564 static const cmdinfo_t aio_write_cmd
= {
1565 .name
= "aio_write",
1566 .cfunc
= aio_write_f
,
1567 .perm
= BLK_PERM_WRITE
,
1570 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1571 .oneline
= "asynchronously writes a number of bytes",
1572 .help
= aio_write_help
,
1575 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1579 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1583 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1589 flags
|= BDRV_REQ_FUA
;
1595 flags
|= BDRV_REQ_MAY_UNMAP
;
1598 pattern
= parse_pattern(optarg
);
1605 printf("injecting invalid write request\n");
1606 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1614 qemuio_command_usage(&aio_write_cmd
);
1619 if (optind
> argc
- 2) {
1621 qemuio_command_usage(&aio_write_cmd
);
1625 if (ctx
->zflag
&& optind
!= argc
- 2) {
1626 printf("-z supports only a single length parameter\n");
1631 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1632 printf("-u requires -z to be specified\n");
1637 if (ctx
->zflag
&& ctx
->Pflag
) {
1638 printf("-z and -P cannot be specified at the same time\n");
1643 ctx
->offset
= cvtnum(argv
[optind
]);
1644 if (ctx
->offset
< 0) {
1645 int ret
= ctx
->offset
;
1646 print_cvtnum_err(ret
, argv
[optind
]);
1653 int64_t count
= cvtnum(argv
[optind
]);
1655 print_cvtnum_err(count
, argv
[optind
]);
1660 ctx
->qiov
.size
= count
;
1661 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1664 nr_iov
= argc
- optind
;
1665 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1667 if (ctx
->buf
== NULL
) {
1668 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1673 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1674 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1677 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1684 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1686 BlockAcctCookie cookie
;
1687 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1689 block_acct_done(blk_get_stats(blk
), &cookie
);
1693 static const cmdinfo_t aio_flush_cmd
= {
1694 .name
= "aio_flush",
1695 .cfunc
= aio_flush_f
,
1696 .oneline
= "completes all outstanding aio requests"
1699 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1701 return blk_flush(blk
);
1704 static const cmdinfo_t flush_cmd
= {
1708 .oneline
= "flush all in-core file state to disk",
1711 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
);
1712 static const cmdinfo_t truncate_cmd
= {
1715 .cfunc
= truncate_f
,
1716 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1719 .args
= "[-m prealloc_mode] off",
1720 .oneline
= "truncates the current file at the given offset",
1723 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1725 Error
*local_err
= NULL
;
1728 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
1730 while ((c
= getopt(argc
, argv
, "m:")) != -1) {
1733 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
1734 PREALLOC_MODE__MAX
, NULL
);
1735 if (prealloc
== PREALLOC_MODE__MAX
) {
1736 error_report("Invalid preallocation mode '%s'", optarg
);
1741 qemuio_command_usage(&truncate_cmd
);
1746 offset
= cvtnum(argv
[optind
]);
1748 print_cvtnum_err(offset
, argv
[1]);
1753 * qemu-io is a debugging tool, so let us be strict here and pass
1754 * exact=true. It is better to err on the "emit more errors" side
1755 * than to be overly permissive.
1757 ret
= blk_truncate(blk
, offset
, false, prealloc
, 0, &local_err
);
1759 error_report_err(local_err
);
1766 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1771 size
= blk_getlength(blk
);
1773 printf("getlength: %s\n", strerror(-size
));
1777 cvtstr(size
, s1
, sizeof(s1
));
1783 static const cmdinfo_t length_cmd
= {
1787 .oneline
= "gets the length of the current file",
1791 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1793 BlockDriverState
*bs
= blk_bs(blk
);
1794 BlockDriverInfo bdi
;
1795 ImageInfoSpecific
*spec_info
;
1796 Error
*local_err
= NULL
;
1797 char s1
[64], s2
[64];
1800 if (bs
->drv
&& bs
->drv
->format_name
) {
1801 printf("format name: %s\n", bs
->drv
->format_name
);
1803 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1804 printf("format name: %s\n", bs
->drv
->protocol_name
);
1807 ret
= bdrv_get_info(bs
, &bdi
);
1812 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1813 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1815 printf("cluster size: %s\n", s1
);
1816 printf("vm state offset: %s\n", s2
);
1818 spec_info
= bdrv_get_specific_info(bs
, &local_err
);
1820 error_report_err(local_err
);
1824 printf("Format specific information:\n");
1825 bdrv_image_info_specific_dump(spec_info
);
1826 qapi_free_ImageInfoSpecific(spec_info
);
1834 static const cmdinfo_t info_cmd
= {
1838 .oneline
= "prints information about the current file",
1841 static void discard_help(void)
1845 " discards a range of bytes from the given offset\n"
1848 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1850 " Discards a segment of the currently open file.\n"
1851 " -C, -- report statistics in a machine parsable format\n"
1852 " -q, -- quiet mode, do not show I/O statistics\n"
1856 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1858 static const cmdinfo_t discard_cmd
= {
1862 .perm
= BLK_PERM_WRITE
,
1865 .args
= "[-Cq] off len",
1866 .oneline
= "discards a number of bytes at a specified offset",
1867 .help
= discard_help
,
1870 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1872 struct timespec t1
, t2
;
1873 bool Cflag
= false, qflag
= false;
1875 int64_t offset
, bytes
;
1877 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1886 qemuio_command_usage(&discard_cmd
);
1891 if (optind
!= argc
- 2) {
1892 qemuio_command_usage(&discard_cmd
);
1896 offset
= cvtnum(argv
[optind
]);
1898 print_cvtnum_err(offset
, argv
[optind
]);
1903 bytes
= cvtnum(argv
[optind
]);
1905 print_cvtnum_err(bytes
, argv
[optind
]);
1907 } else if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
1908 printf("length cannot exceed %"PRIu64
", given %s\n",
1909 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1913 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1914 ret
= blk_pdiscard(blk
, offset
, bytes
);
1915 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1918 printf("discard failed: %s\n", strerror(-ret
));
1922 /* Finally, report back -- -C gives a parsable format */
1925 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
1931 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1933 BlockDriverState
*bs
= blk_bs(blk
);
1934 int64_t offset
, start
, remaining
, count
;
1937 int64_t num
, sum_alloc
;
1939 start
= offset
= cvtnum(argv
[1]);
1941 print_cvtnum_err(offset
, argv
[1]);
1946 count
= cvtnum(argv
[2]);
1948 print_cvtnum_err(count
, argv
[2]);
1952 count
= BDRV_SECTOR_SIZE
;
1958 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
1960 printf("is_allocated failed: %s\n", strerror(-ret
));
1974 cvtstr(start
, s1
, sizeof(s1
));
1976 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
1977 sum_alloc
, count
, s1
);
1981 static const cmdinfo_t alloc_cmd
= {
1987 .args
= "offset [count]",
1988 .oneline
= "checks if offset is allocated in the file",
1992 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
1993 int64_t bytes
, int64_t *pnum
)
1999 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
2000 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
2008 while (bytes
> 0 && ret
== firstret
) {
2012 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
2013 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
2014 if (ret
== firstret
&& num
) {
2024 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
2026 int64_t offset
, bytes
;
2027 char s1
[64], s2
[64];
2033 bytes
= blk_getlength(blk
);
2035 error_report("Failed to query image length: %s", strerror(-bytes
));
2040 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
2042 error_report("Failed to get allocation status: %s", strerror(-ret
));
2045 error_report("Unexpected end of image");
2049 retstr
= ret
? " allocated" : "not allocated";
2050 cvtstr(num
, s1
, sizeof(s1
));
2051 cvtstr(offset
, s2
, sizeof(s2
));
2052 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
2053 s1
, num
, retstr
, s2
, offset
);
2062 static const cmdinfo_t map_cmd
= {
2068 .oneline
= "prints the allocated areas of a file",
2071 static void reopen_help(void)
2075 " Changes the open options of an already opened image\n"
2078 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2080 " -r, -- Reopen the image read-only\n"
2081 " -w, -- Reopen the image read-write\n"
2082 " -c, -- Change the cache mode to the given value\n"
2083 " -o, -- Changes block driver options (cf. 'open' command)\n"
2087 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2089 static QemuOptsList reopen_opts
= {
2091 .merge_lists
= true,
2092 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2094 /* no elements => accept any params */
2095 { /* end of list */ }
2099 static const cmdinfo_t reopen_cmd
= {
2104 .args
= "[(-r|-w)] [-c cache] [-o options]",
2105 .oneline
= "reopens an image with new options",
2106 .help
= reopen_help
,
2109 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2111 BlockDriverState
*bs
= blk_bs(blk
);
2115 int flags
= bs
->open_flags
;
2116 bool writethrough
= !blk_enable_write_cache(blk
);
2117 bool has_rw_option
= false;
2118 bool has_cache_option
= false;
2120 BlockReopenQueue
*brq
;
2121 Error
*local_err
= NULL
;
2123 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
2126 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
2127 error_report("Invalid cache option: %s", optarg
);
2130 has_cache_option
= true;
2133 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2134 qemu_opts_reset(&reopen_opts
);
2139 if (has_rw_option
) {
2140 error_report("Only one -r/-w option may be given");
2143 flags
&= ~BDRV_O_RDWR
;
2144 has_rw_option
= true;
2147 if (has_rw_option
) {
2148 error_report("Only one -r/-w option may be given");
2151 flags
|= BDRV_O_RDWR
;
2152 has_rw_option
= true;
2155 qemu_opts_reset(&reopen_opts
);
2156 qemuio_command_usage(&reopen_cmd
);
2161 if (optind
!= argc
) {
2162 qemu_opts_reset(&reopen_opts
);
2163 qemuio_command_usage(&reopen_cmd
);
2167 if (!writethrough
!= blk_enable_write_cache(blk
) &&
2168 blk_get_attached_dev(blk
))
2170 error_report("Cannot change cache.writeback: Device attached");
2171 qemu_opts_reset(&reopen_opts
);
2175 if (!(flags
& BDRV_O_RDWR
)) {
2176 uint64_t orig_perm
, orig_shared_perm
;
2180 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2182 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2187 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2188 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : qdict_new();
2189 qemu_opts_reset(&reopen_opts
);
2191 if (qdict_haskey(opts
, BDRV_OPT_READ_ONLY
)) {
2192 if (has_rw_option
) {
2193 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY
"'");
2194 qobject_unref(opts
);
2198 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !(flags
& BDRV_O_RDWR
));
2201 if (qdict_haskey(opts
, BDRV_OPT_CACHE_DIRECT
) ||
2202 qdict_haskey(opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
2203 if (has_cache_option
) {
2204 error_report("Cannot set both -c and the cache options");
2205 qobject_unref(opts
);
2209 qdict_put_bool(opts
, BDRV_OPT_CACHE_DIRECT
, flags
& BDRV_O_NOCACHE
);
2210 qdict_put_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, flags
& BDRV_O_NO_FLUSH
);
2213 bdrv_subtree_drained_begin(bs
);
2214 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, true);
2215 bdrv_reopen_multiple(brq
, &local_err
);
2216 bdrv_subtree_drained_end(bs
);
2219 error_report_err(local_err
);
2223 blk_set_enable_write_cache(blk
, !writethrough
);
2227 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2231 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2233 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2240 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2244 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2246 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2253 static const cmdinfo_t break_cmd
= {
2258 .args
= "event tag",
2259 .oneline
= "sets a breakpoint on event and tags the stopped "
2263 static const cmdinfo_t remove_break_cmd
= {
2264 .name
= "remove_break",
2267 .cfunc
= remove_break_f
,
2269 .oneline
= "remove a breakpoint by tag",
2272 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2276 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2278 printf("Could not resume request: %s\n", strerror(-ret
));
2285 static const cmdinfo_t resume_cmd
= {
2291 .oneline
= "resumes the request tagged as tag",
2294 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2296 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2297 aio_poll(blk_get_aio_context(blk
), true);
2302 static const cmdinfo_t wait_break_cmd
= {
2303 .name
= "wait_break",
2306 .cfunc
= wait_break_f
,
2308 .oneline
= "waits for the suspension of a request",
2311 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2316 static const cmdinfo_t abort_cmd
= {
2319 .flags
= CMD_NOFILE_OK
,
2320 .oneline
= "simulate a program crash using abort(3)",
2323 static void sigraise_help(void)
2327 " raises the given signal\n"
2330 " 'sigraise %i' - raises SIGTERM\n"
2332 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2333 " given to sigraise.\n"
2337 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2339 static const cmdinfo_t sigraise_cmd
= {
2341 .cfunc
= sigraise_f
,
2344 .flags
= CMD_NOFILE_OK
,
2346 .oneline
= "raises a signal",
2347 .help
= sigraise_help
,
2350 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2352 int64_t sig
= cvtnum(argv
[1]);
2354 print_cvtnum_err(sig
, argv
[1]);
2356 } else if (sig
> NSIG
) {
2357 printf("signal argument '%s' is too large to be a valid signal\n",
2362 /* Using raise() to kill this process does not necessarily flush all open
2363 * streams. At least stdout and stderr (although the latter should be
2364 * non-buffered anyway) should be flushed, though. */
2373 static void sleep_cb(void *opaque
)
2375 bool *expired
= opaque
;
2379 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2383 struct QEMUTimer
*timer
;
2384 bool expired
= false;
2386 ms
= strtol(argv
[1], &endptr
, 0);
2387 if (ms
< 0 || *endptr
!= '\0') {
2388 printf("%s is not a valid number\n", argv
[1]);
2392 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2393 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2396 main_loop_wait(false);
2403 static const cmdinfo_t sleep_cmd
= {
2408 .flags
= CMD_NOFILE_OK
,
2409 .oneline
= "waits for the given value in milliseconds",
2412 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2417 printf("%s ", ct
->args
);
2419 printf("-- %s\n", ct
->oneline
);
2422 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2424 help_oneline(cmd
, ct
);
2430 static void help_all(void)
2432 const cmdinfo_t
*ct
;
2434 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2435 help_oneline(ct
->name
, ct
);
2437 printf("\nUse 'help commandname' for extended help.\n");
2440 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2442 const cmdinfo_t
*ct
;
2449 ct
= find_command(argv
[1]);
2451 printf("command %s not found\n", argv
[1]);
2455 help_onecmd(argv
[1], ct
);
2459 static const cmdinfo_t help_cmd
= {
2465 .flags
= CMD_FLAG_GLOBAL
,
2466 .args
= "[command]",
2467 .oneline
= "help for one or all commands",
2471 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2472 * context acquired if blk is NULL.
2474 int qemuio_command(BlockBackend
*blk
, const char *cmd
)
2477 const cmdinfo_t
*ct
;
2482 input
= g_strdup(cmd
);
2483 v
= breakline(input
, &c
);
2485 ct
= find_command(v
[0]);
2487 ret
= command(blk
, ct
, c
, v
);
2489 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2499 static void __attribute((constructor
)) init_qemuio_commands(void)
2501 /* initialize commands */
2502 qemuio_add_command(&help_cmd
);
2503 qemuio_add_command(&read_cmd
);
2504 qemuio_add_command(&readv_cmd
);
2505 qemuio_add_command(&write_cmd
);
2506 qemuio_add_command(&writev_cmd
);
2507 qemuio_add_command(&aio_read_cmd
);
2508 qemuio_add_command(&aio_write_cmd
);
2509 qemuio_add_command(&aio_flush_cmd
);
2510 qemuio_add_command(&flush_cmd
);
2511 qemuio_add_command(&truncate_cmd
);
2512 qemuio_add_command(&length_cmd
);
2513 qemuio_add_command(&info_cmd
);
2514 qemuio_add_command(&discard_cmd
);
2515 qemuio_add_command(&alloc_cmd
);
2516 qemuio_add_command(&map_cmd
);
2517 qemuio_add_command(&reopen_cmd
);
2518 qemuio_add_command(&break_cmd
);
2519 qemuio_add_command(&remove_break_cmd
);
2520 qemuio_add_command(&resume_cmd
);
2521 qemuio_add_command(&wait_break_cmd
);
2522 qemuio_add_command(&abort_cmd
);
2523 qemuio_add_command(&sleep_cmd
);
2524 qemuio_add_command(&sigraise_cmd
);