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
)
345 if (qemuio_misalign
) {
346 len
+= MISALIGN_OFFSET
;
348 buf
= blk_blockalign(blk
, len
);
349 memset(buf
, pattern
, len
);
350 if (qemuio_misalign
) {
351 buf
+= MISALIGN_OFFSET
;
356 static void qemu_io_free(void *p
)
358 if (qemuio_misalign
) {
359 p
-= MISALIGN_OFFSET
;
365 * qemu_io_alloc_from_file()
367 * Allocates the buffer and populates it with the content of the given file
368 * up to @len bytes. If the file length is less than @len, then the buffer
369 * is populated with the file content cyclically.
371 * @blk - the block backend where the buffer content is going to be written to
372 * @len - the buffer length
373 * @file_name - the file to read the content from
375 * Returns: the buffer pointer on success
378 static void *qemu_io_alloc_from_file(BlockBackend
*blk
, size_t len
,
379 const char *file_name
)
381 char *buf
, *buf_origin
;
382 FILE *f
= fopen(file_name
, "r");
390 if (qemuio_misalign
) {
391 len
+= MISALIGN_OFFSET
;
394 buf_origin
= buf
= blk_blockalign(blk
, len
);
396 if (qemuio_misalign
) {
397 buf_origin
+= MISALIGN_OFFSET
;
398 buf
+= MISALIGN_OFFSET
;
399 len
-= MISALIGN_OFFSET
;
402 pattern_len
= fread(buf_origin
, 1, len
, f
);
409 if (pattern_len
== 0) {
410 fprintf(stderr
, "%s: file is empty\n", file_name
);
417 if (len
> pattern_len
) {
422 size_t len_to_copy
= MIN(pattern_len
, len
);
424 memcpy(buf
, buf_origin
, len_to_copy
);
434 qemu_io_free(buf_origin
);
441 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
447 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
448 const uint8_t *s
= p
;
450 printf("%08" PRIx64
": ", offset
+ i
);
451 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
455 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
466 static void print_report(const char *op
, struct timespec
*t
, int64_t offset
,
467 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
469 char s1
[64], s2
[64], ts
[64];
471 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
473 cvtstr((double)total
, s1
, sizeof(s1
));
474 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
475 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
476 op
, total
, count
, offset
);
477 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
478 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
479 } else {/* bytes,ops,time,bytes/sec,ops/sec */
480 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
482 tdiv((double)total
, *t
),
483 tdiv((double)cnt
, *t
));
488 * Parse multiple length statements for vectored I/O, and construct an I/O
489 * vector matching it.
492 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
495 size_t *sizes
= g_new0(size_t, nr_iov
);
501 for (i
= 0; i
< nr_iov
; i
++) {
507 print_cvtnum_err(len
, arg
);
511 if (len
> BDRV_REQUEST_MAX_BYTES
) {
512 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
513 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
517 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
518 printf("The total number of bytes exceed the maximum size %" PRIu64
519 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
527 qemu_iovec_init(qiov
, nr_iov
);
529 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
531 for (i
= 0; i
< nr_iov
; i
++) {
532 qemu_iovec_add(qiov
, p
, sizes
[i
]);
541 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
542 int64_t bytes
, int64_t *total
)
546 if (bytes
> INT_MAX
) {
550 ret
= blk_pread(blk
, offset
, bytes
, (uint8_t *)buf
, 0);
558 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
559 int64_t bytes
, int flags
, int64_t *total
)
563 if (bytes
> INT_MAX
) {
567 ret
= blk_pwrite(blk
, offset
, bytes
, (uint8_t *)buf
, flags
);
575 static int do_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
576 int64_t bytes
, int flags
, int64_t *total
)
578 int ret
= blk_pwrite_zeroes(blk
, offset
, bytes
,
579 flags
| BDRV_REQ_ZERO_WRITE
);
588 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
589 int64_t bytes
, int64_t *total
)
593 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
597 ret
= blk_pwrite_compressed(blk
, offset
, bytes
, buf
);
605 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
606 int64_t count
, int64_t *total
)
608 if (count
> INT_MAX
) {
612 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
619 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
620 int64_t count
, int64_t *total
)
622 if (count
> INT_MAX
) {
626 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
633 #define NOT_DONE 0x7fffffff
634 static void aio_rw_done(void *opaque
, int ret
)
636 *(int *)opaque
= ret
;
639 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
640 int64_t offset
, int *total
)
642 int async_ret
= NOT_DONE
;
644 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
645 while (async_ret
== NOT_DONE
) {
646 main_loop_wait(false);
650 return async_ret
< 0 ? async_ret
: 1;
653 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
654 int64_t offset
, int flags
, int *total
)
656 int async_ret
= NOT_DONE
;
658 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
659 while (async_ret
== NOT_DONE
) {
660 main_loop_wait(false);
664 return async_ret
< 0 ? async_ret
: 1;
667 static void read_help(void)
671 " reads a range of bytes from the given offset\n"
674 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
676 " Reads a segment of the currently open file, optionally dumping it to the\n"
677 " standard output stream (with -v option) for subsequent inspection.\n"
678 " -b, -- read from the VM state rather than the virtual disk\n"
679 " -C, -- report statistics in a machine parsable format\n"
680 " -l, -- length for pattern verification (only with -P)\n"
681 " -p, -- ignored for backwards compatibility\n"
682 " -P, -- use a pattern to verify read data\n"
683 " -q, -- quiet mode, do not show I/O statistics\n"
684 " -s, -- start offset for pattern verification (only with -P)\n"
685 " -v, -- dump buffer to standard output\n"
689 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
691 static const cmdinfo_t read_cmd
= {
697 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
698 .oneline
= "reads a number of bytes at a specified offset",
702 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
704 struct timespec t1
, t2
;
705 bool Cflag
= false, qflag
= false, vflag
= false;
706 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
711 /* Some compilers get confused and warn if this is not initialized. */
714 int64_t pattern_offset
= 0, pattern_count
= 0;
716 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
726 pattern_count
= cvtnum(optarg
);
727 if (pattern_count
< 0) {
728 print_cvtnum_err(pattern_count
, optarg
);
729 return pattern_count
;
733 /* Ignored for backwards compatibility */
737 pattern
= parse_pattern(optarg
);
747 pattern_offset
= cvtnum(optarg
);
748 if (pattern_offset
< 0) {
749 print_cvtnum_err(pattern_offset
, optarg
);
750 return pattern_offset
;
757 qemuio_command_usage(&read_cmd
);
762 if (optind
!= argc
- 2) {
763 qemuio_command_usage(&read_cmd
);
767 offset
= cvtnum(argv
[optind
]);
769 print_cvtnum_err(offset
, argv
[optind
]);
774 count
= cvtnum(argv
[optind
]);
776 print_cvtnum_err(count
, argv
[optind
]);
778 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
779 printf("length cannot exceed %" PRIu64
", given %s\n",
780 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
784 if (!Pflag
&& (lflag
|| sflag
)) {
785 qemuio_command_usage(&read_cmd
);
790 pattern_count
= count
- pattern_offset
;
793 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
794 printf("pattern verification range exceeds end of read data\n");
799 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
800 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
804 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
805 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
811 buf
= qemu_io_alloc(blk
, count
, 0xab);
813 clock_gettime(CLOCK_MONOTONIC
, &t1
);
815 ret
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
817 ret
= do_pread(blk
, buf
, offset
, count
, &total
);
819 clock_gettime(CLOCK_MONOTONIC
, &t2
);
822 printf("read failed: %s\n", strerror(-ret
));
830 void *cmp_buf
= g_malloc(pattern_count
);
831 memset(cmp_buf
, pattern
, pattern_count
);
832 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
833 printf("Pattern verification failed at offset %"
834 PRId64
", %"PRId64
" bytes\n",
835 offset
+ pattern_offset
, pattern_count
);
846 dump_buffer(buf
, offset
, count
);
849 /* Finally, report back -- -C gives a parsable format */
851 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
858 static void readv_help(void)
862 " reads a range of bytes from the given offset into multiple buffers\n"
865 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
867 " Reads a segment of the currently open file, optionally dumping it to the\n"
868 " standard output stream (with -v option) for subsequent inspection.\n"
869 " Uses multiple iovec buffers if more than one byte range is specified.\n"
870 " -C, -- report statistics in a machine parsable format\n"
871 " -P, -- use a pattern to verify read data\n"
872 " -v, -- dump buffer to standard output\n"
873 " -q, -- quiet mode, do not show I/O statistics\n"
877 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
879 static const cmdinfo_t readv_cmd
= {
884 .args
= "[-Cqv] [-P pattern] off len [len..]",
885 .oneline
= "reads a number of bytes at a specified offset",
889 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
891 struct timespec t1
, t2
;
892 bool Cflag
= false, qflag
= false, vflag
= false;
896 /* Some compilers get confused and warn if this is not initialized. */
903 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
910 pattern
= parse_pattern(optarg
);
922 qemuio_command_usage(&readv_cmd
);
927 if (optind
> argc
- 2) {
928 qemuio_command_usage(&readv_cmd
);
933 offset
= cvtnum(argv
[optind
]);
935 print_cvtnum_err(offset
, argv
[optind
]);
940 nr_iov
= argc
- optind
;
941 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
946 clock_gettime(CLOCK_MONOTONIC
, &t1
);
947 ret
= do_aio_readv(blk
, &qiov
, offset
, &total
);
948 clock_gettime(CLOCK_MONOTONIC
, &t2
);
951 printf("readv failed: %s\n", strerror(-ret
));
959 void *cmp_buf
= g_malloc(qiov
.size
);
960 memset(cmp_buf
, pattern
, qiov
.size
);
961 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
962 printf("Pattern verification failed at offset %"
963 PRId64
", %zu bytes\n", offset
, qiov
.size
);
974 dump_buffer(buf
, offset
, qiov
.size
);
977 /* Finally, report back -- -C gives a parsable format */
979 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
982 qemu_iovec_destroy(&qiov
);
987 static void write_help(void)
991 " writes a range of bytes from the given offset\n"
994 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
996 " Writes into a segment of the currently open file, using a buffer\n"
997 " filled with a set pattern (0xcdcdcdcd).\n"
998 " -b, -- write to the VM state rather than the virtual disk\n"
999 " -c, -- write compressed data with blk_write_compressed\n"
1000 " -f, -- use Force Unit Access semantics\n"
1001 " -n, -- with -z, don't allow slow fallback\n"
1002 " -p, -- ignored for backwards compatibility\n"
1003 " -P, -- use different pattern to fill file\n"
1004 " -s, -- use a pattern file to fill the write buffer\n"
1005 " -C, -- report statistics in a machine parsable format\n"
1006 " -q, -- quiet mode, do not show I/O statistics\n"
1007 " -u, -- with -z, allow unmapping\n"
1008 " -z, -- write zeroes using blk_pwrite_zeroes\n"
1012 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
1014 static const cmdinfo_t write_cmd
= {
1018 .perm
= BLK_PERM_WRITE
,
1021 .args
= "[-bcCfnquz] [-P pattern | -s source_file] off len",
1022 .oneline
= "writes a number of bytes at a specified offset",
1026 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
1028 struct timespec t1
, t2
;
1029 bool Cflag
= false, qflag
= false, bflag
= false;
1030 bool Pflag
= false, zflag
= false, cflag
= false, sflag
= false;
1036 /* Some compilers get confused and warn if this is not initialized. */
1039 const char *file_name
= NULL
;
1041 while ((c
= getopt(argc
, argv
, "bcCfnpP:qs:uz")) != -1) {
1053 flags
|= BDRV_REQ_FUA
;
1056 flags
|= BDRV_REQ_NO_FALLBACK
;
1059 /* Ignored for backwards compatibility */
1063 pattern
= parse_pattern(optarg
);
1076 flags
|= BDRV_REQ_MAY_UNMAP
;
1082 qemuio_command_usage(&write_cmd
);
1087 if (optind
!= argc
- 2) {
1088 qemuio_command_usage(&write_cmd
);
1092 if (bflag
&& zflag
) {
1093 printf("-b and -z cannot be specified at the same time\n");
1097 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1098 printf("-f and -b or -c cannot be specified at the same time\n");
1102 if ((flags
& BDRV_REQ_NO_FALLBACK
) && !zflag
) {
1103 printf("-n requires -z to be specified\n");
1107 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1108 printf("-u requires -z to be specified\n");
1112 if (zflag
+ Pflag
+ sflag
> 1) {
1113 printf("Only one of -z, -P, and -s "
1114 "can be specified at the same time\n");
1118 offset
= cvtnum(argv
[optind
]);
1120 print_cvtnum_err(offset
, argv
[optind
]);
1125 count
= cvtnum(argv
[optind
]);
1127 print_cvtnum_err(count
, argv
[optind
]);
1129 } else if (count
> BDRV_REQUEST_MAX_BYTES
&&
1130 !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1131 printf("length cannot exceed %" PRIu64
" without -n, given %s\n",
1132 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1136 if (bflag
|| cflag
) {
1137 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1138 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1143 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1144 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1152 buf
= qemu_io_alloc_from_file(blk
, count
, file_name
);
1157 buf
= qemu_io_alloc(blk
, count
, pattern
);
1161 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1163 ret
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1165 ret
= do_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1167 ret
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1169 ret
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1171 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1174 printf("write failed: %s\n", strerror(-ret
));
1185 /* Finally, report back -- -C gives a parsable format */
1187 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1201 " writes a range of bytes from the given offset source from multiple buffers\n"
1204 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1206 " Writes into a segment of the currently open file, using a buffer\n"
1207 " filled with a set pattern (0xcdcdcdcd).\n"
1208 " -P, -- use different pattern to fill file\n"
1209 " -C, -- report statistics in a machine parsable format\n"
1210 " -f, -- use Force Unit Access semantics\n"
1211 " -q, -- quiet mode, do not show I/O statistics\n"
1215 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1217 static const cmdinfo_t writev_cmd
= {
1220 .perm
= BLK_PERM_WRITE
,
1223 .args
= "[-Cfq] [-P pattern] off len [len..]",
1224 .oneline
= "writes a number of bytes at a specified offset",
1225 .help
= writev_help
,
1228 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1230 struct timespec t1
, t2
;
1231 bool Cflag
= false, qflag
= false;
1236 /* Some compilers get confused and warn if this is not initialized. */
1242 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1248 flags
|= BDRV_REQ_FUA
;
1254 pattern
= parse_pattern(optarg
);
1260 qemuio_command_usage(&writev_cmd
);
1265 if (optind
> argc
- 2) {
1266 qemuio_command_usage(&writev_cmd
);
1270 offset
= cvtnum(argv
[optind
]);
1272 print_cvtnum_err(offset
, argv
[optind
]);
1277 nr_iov
= argc
- optind
;
1278 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1283 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1284 ret
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1285 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1288 printf("writev failed: %s\n", strerror(-ret
));
1299 /* Finally, report back -- -C gives a parsable format */
1301 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1303 qemu_iovec_destroy(&qiov
);
1318 BlockAcctCookie acct
;
1323 static void aio_write_done(void *opaque
, int ret
)
1325 struct aio_ctx
*ctx
= opaque
;
1328 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1332 printf("aio_write failed: %s\n", strerror(-ret
));
1333 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1337 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1343 /* Finally, report back -- -C gives a parsable format */
1344 t2
= tsub(t2
, ctx
->t1
);
1345 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1346 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1349 qemu_io_free(ctx
->buf
);
1350 qemu_iovec_destroy(&ctx
->qiov
);
1355 static void aio_read_done(void *opaque
, int ret
)
1357 struct aio_ctx
*ctx
= opaque
;
1360 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1363 printf("readv failed: %s\n", strerror(-ret
));
1364 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1369 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1371 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1372 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1373 printf("Pattern verification failed at offset %"
1374 PRId64
", %zu bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1379 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1386 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1389 /* Finally, report back -- -C gives a parsable format */
1390 t2
= tsub(t2
, ctx
->t1
);
1391 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1392 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1394 qemu_io_free(ctx
->buf
);
1395 qemu_iovec_destroy(&ctx
->qiov
);
1399 static void aio_read_help(void)
1403 " asynchronously reads a range of bytes from the given offset\n"
1406 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1408 " Reads a segment of the currently open file, optionally dumping it to the\n"
1409 " standard output stream (with -v option) for subsequent inspection.\n"
1410 " The read is performed asynchronously and the aio_flush command must be\n"
1411 " used to ensure all outstanding aio requests have been completed.\n"
1412 " Note that due to its asynchronous nature, this command will be\n"
1413 " considered successful once the request is submitted, independently\n"
1414 " of potential I/O errors or pattern mismatches.\n"
1415 " -C, -- report statistics in a machine parsable format\n"
1416 " -P, -- use a pattern to verify read data\n"
1417 " -i, -- treat request as invalid, for exercising stats\n"
1418 " -v, -- dump buffer to standard output\n"
1419 " -q, -- quiet mode, do not show I/O statistics\n"
1423 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1425 static const cmdinfo_t aio_read_cmd
= {
1427 .cfunc
= aio_read_f
,
1430 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1431 .oneline
= "asynchronously reads a number of bytes",
1432 .help
= aio_read_help
,
1435 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1438 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1441 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1448 ctx
->pattern
= parse_pattern(optarg
);
1449 if (ctx
->pattern
< 0) {
1455 printf("injecting invalid read request\n");
1456 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1467 qemuio_command_usage(&aio_read_cmd
);
1472 if (optind
> argc
- 2) {
1474 qemuio_command_usage(&aio_read_cmd
);
1478 ctx
->offset
= cvtnum(argv
[optind
]);
1479 if (ctx
->offset
< 0) {
1480 int ret
= ctx
->offset
;
1481 print_cvtnum_err(ret
, argv
[optind
]);
1487 nr_iov
= argc
- optind
;
1488 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1489 if (ctx
->buf
== NULL
) {
1490 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1495 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1496 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1498 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1502 static void aio_write_help(void)
1506 " asynchronously writes a range of bytes from the given offset source\n"
1507 " from multiple buffers\n"
1510 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1512 " Writes into a segment of the currently open file, using a buffer\n"
1513 " filled with a set pattern (0xcdcdcdcd).\n"
1514 " The write is performed asynchronously and the aio_flush command must be\n"
1515 " used to ensure all outstanding aio requests have been completed.\n"
1516 " Note that due to its asynchronous nature, this command will be\n"
1517 " considered successful once the request is submitted, independently\n"
1518 " of potential I/O errors or pattern mismatches.\n"
1519 " -P, -- use different pattern to fill file\n"
1520 " -C, -- report statistics in a machine parsable format\n"
1521 " -f, -- use Force Unit Access semantics\n"
1522 " -i, -- treat request as invalid, for exercising stats\n"
1523 " -q, -- quiet mode, do not show I/O statistics\n"
1524 " -u, -- with -z, allow unmapping\n"
1525 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1529 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1531 static const cmdinfo_t aio_write_cmd
= {
1532 .name
= "aio_write",
1533 .cfunc
= aio_write_f
,
1534 .perm
= BLK_PERM_WRITE
,
1537 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1538 .oneline
= "asynchronously writes a number of bytes",
1539 .help
= aio_write_help
,
1542 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1546 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1550 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1556 flags
|= BDRV_REQ_FUA
;
1562 flags
|= BDRV_REQ_MAY_UNMAP
;
1565 pattern
= parse_pattern(optarg
);
1572 printf("injecting invalid write request\n");
1573 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1581 qemuio_command_usage(&aio_write_cmd
);
1586 if (optind
> argc
- 2) {
1588 qemuio_command_usage(&aio_write_cmd
);
1592 if (ctx
->zflag
&& optind
!= argc
- 2) {
1593 printf("-z supports only a single length parameter\n");
1598 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1599 printf("-u requires -z to be specified\n");
1604 if (ctx
->zflag
&& ctx
->Pflag
) {
1605 printf("-z and -P cannot be specified at the same time\n");
1610 ctx
->offset
= cvtnum(argv
[optind
]);
1611 if (ctx
->offset
< 0) {
1612 int ret
= ctx
->offset
;
1613 print_cvtnum_err(ret
, argv
[optind
]);
1620 int64_t count
= cvtnum(argv
[optind
]);
1622 print_cvtnum_err(count
, argv
[optind
]);
1627 ctx
->qiov
.size
= count
;
1628 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1631 nr_iov
= argc
- optind
;
1632 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1634 if (ctx
->buf
== NULL
) {
1635 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1640 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1641 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1644 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1651 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1653 BlockAcctCookie cookie
;
1654 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1656 block_acct_done(blk_get_stats(blk
), &cookie
);
1660 static const cmdinfo_t aio_flush_cmd
= {
1661 .name
= "aio_flush",
1662 .cfunc
= aio_flush_f
,
1663 .oneline
= "completes all outstanding aio requests"
1666 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1668 return blk_flush(blk
);
1671 static const cmdinfo_t flush_cmd
= {
1675 .oneline
= "flush all in-core file state to disk",
1678 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
);
1679 static const cmdinfo_t truncate_cmd
= {
1682 .cfunc
= truncate_f
,
1683 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1686 .args
= "[-m prealloc_mode] off",
1687 .oneline
= "truncates the current file at the given offset",
1690 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1692 Error
*local_err
= NULL
;
1695 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
1697 while ((c
= getopt(argc
, argv
, "m:")) != -1) {
1700 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
1701 PREALLOC_MODE__MAX
, NULL
);
1702 if (prealloc
== PREALLOC_MODE__MAX
) {
1703 error_report("Invalid preallocation mode '%s'", optarg
);
1708 qemuio_command_usage(&truncate_cmd
);
1713 offset
= cvtnum(argv
[optind
]);
1715 print_cvtnum_err(offset
, argv
[1]);
1720 * qemu-io is a debugging tool, so let us be strict here and pass
1721 * exact=true. It is better to err on the "emit more errors" side
1722 * than to be overly permissive.
1724 ret
= blk_truncate(blk
, offset
, false, prealloc
, 0, &local_err
);
1726 error_report_err(local_err
);
1733 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1738 size
= blk_getlength(blk
);
1740 printf("getlength: %s\n", strerror(-size
));
1744 cvtstr(size
, s1
, sizeof(s1
));
1750 static const cmdinfo_t length_cmd
= {
1754 .oneline
= "gets the length of the current file",
1758 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1760 BlockDriverState
*bs
= blk_bs(blk
);
1761 BlockDriverInfo bdi
;
1762 ImageInfoSpecific
*spec_info
;
1763 Error
*local_err
= NULL
;
1764 char s1
[64], s2
[64];
1767 if (bs
->drv
&& bs
->drv
->format_name
) {
1768 printf("format name: %s\n", bs
->drv
->format_name
);
1770 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1771 printf("format name: %s\n", bs
->drv
->protocol_name
);
1774 ret
= bdrv_get_info(bs
, &bdi
);
1779 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1780 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1782 printf("cluster size: %s\n", s1
);
1783 printf("vm state offset: %s\n", s2
);
1785 spec_info
= bdrv_get_specific_info(bs
, &local_err
);
1787 error_report_err(local_err
);
1791 bdrv_image_info_specific_dump(spec_info
,
1792 "Format specific information:\n",
1794 qapi_free_ImageInfoSpecific(spec_info
);
1802 static const cmdinfo_t info_cmd
= {
1806 .oneline
= "prints information about the current file",
1809 static void discard_help(void)
1813 " discards a range of bytes from the given offset\n"
1816 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1818 " Discards a segment of the currently open file.\n"
1819 " -C, -- report statistics in a machine parsable format\n"
1820 " -q, -- quiet mode, do not show I/O statistics\n"
1824 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1826 static const cmdinfo_t discard_cmd
= {
1830 .perm
= BLK_PERM_WRITE
,
1833 .args
= "[-Cq] off len",
1834 .oneline
= "discards a number of bytes at a specified offset",
1835 .help
= discard_help
,
1838 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1840 struct timespec t1
, t2
;
1841 bool Cflag
= false, qflag
= false;
1843 int64_t offset
, bytes
;
1845 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1854 qemuio_command_usage(&discard_cmd
);
1859 if (optind
!= argc
- 2) {
1860 qemuio_command_usage(&discard_cmd
);
1864 offset
= cvtnum(argv
[optind
]);
1866 print_cvtnum_err(offset
, argv
[optind
]);
1871 bytes
= cvtnum(argv
[optind
]);
1873 print_cvtnum_err(bytes
, argv
[optind
]);
1875 } else if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
1876 printf("length cannot exceed %"PRIu64
", given %s\n",
1877 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1881 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1882 ret
= blk_pdiscard(blk
, offset
, bytes
);
1883 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1886 printf("discard failed: %s\n", strerror(-ret
));
1890 /* Finally, report back -- -C gives a parsable format */
1893 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
1899 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1901 BlockDriverState
*bs
= blk_bs(blk
);
1902 int64_t offset
, start
, remaining
, count
;
1905 int64_t num
, sum_alloc
;
1907 start
= offset
= cvtnum(argv
[1]);
1909 print_cvtnum_err(offset
, argv
[1]);
1914 count
= cvtnum(argv
[2]);
1916 print_cvtnum_err(count
, argv
[2]);
1920 count
= BDRV_SECTOR_SIZE
;
1926 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
1928 printf("is_allocated failed: %s\n", strerror(-ret
));
1942 cvtstr(start
, s1
, sizeof(s1
));
1944 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
1945 sum_alloc
, count
, s1
);
1949 static const cmdinfo_t alloc_cmd
= {
1955 .args
= "offset [count]",
1956 .oneline
= "checks if offset is allocated in the file",
1960 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
1961 int64_t bytes
, int64_t *pnum
)
1966 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &num
);
1974 while (bytes
> 0 && ret
== firstret
) {
1978 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &num
);
1979 if (ret
== firstret
&& num
) {
1989 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1991 int64_t offset
, bytes
;
1992 char s1
[64], s2
[64];
1998 bytes
= blk_getlength(blk
);
2000 error_report("Failed to query image length: %s", strerror(-bytes
));
2005 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
2007 error_report("Failed to get allocation status: %s", strerror(-ret
));
2010 error_report("Unexpected end of image");
2014 retstr
= ret
? " allocated" : "not allocated";
2015 cvtstr(num
, s1
, sizeof(s1
));
2016 cvtstr(offset
, s2
, sizeof(s2
));
2017 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
2018 s1
, num
, retstr
, s2
, offset
);
2027 static const cmdinfo_t map_cmd
= {
2033 .oneline
= "prints the allocated areas of a file",
2036 static void reopen_help(void)
2040 " Changes the open options of an already opened image\n"
2043 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2045 " -r, -- Reopen the image read-only\n"
2046 " -w, -- Reopen the image read-write\n"
2047 " -c, -- Change the cache mode to the given value\n"
2048 " -o, -- Changes block driver options (cf. 'open' command)\n"
2052 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2054 static QemuOptsList reopen_opts
= {
2056 .merge_lists
= true,
2057 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2059 /* no elements => accept any params */
2060 { /* end of list */ }
2064 static const cmdinfo_t reopen_cmd
= {
2069 .args
= "[(-r|-w)] [-c cache] [-o options]",
2070 .oneline
= "reopens an image with new options",
2071 .help
= reopen_help
,
2074 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2076 BlockDriverState
*bs
= blk_bs(blk
);
2080 int flags
= bs
->open_flags
;
2081 bool writethrough
= !blk_enable_write_cache(blk
);
2082 bool has_rw_option
= false;
2083 bool has_cache_option
= false;
2084 Error
*local_err
= NULL
;
2086 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
2089 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
2090 error_report("Invalid cache option: %s", optarg
);
2093 has_cache_option
= true;
2096 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2097 qemu_opts_reset(&reopen_opts
);
2102 if (has_rw_option
) {
2103 error_report("Only one -r/-w option may be given");
2106 flags
&= ~BDRV_O_RDWR
;
2107 has_rw_option
= true;
2110 if (has_rw_option
) {
2111 error_report("Only one -r/-w option may be given");
2114 flags
|= BDRV_O_RDWR
;
2115 has_rw_option
= true;
2118 qemu_opts_reset(&reopen_opts
);
2119 qemuio_command_usage(&reopen_cmd
);
2124 if (optind
!= argc
) {
2125 qemu_opts_reset(&reopen_opts
);
2126 qemuio_command_usage(&reopen_cmd
);
2130 if (!writethrough
!= blk_enable_write_cache(blk
) &&
2131 blk_get_attached_dev(blk
))
2133 error_report("Cannot change cache.writeback: Device attached");
2134 qemu_opts_reset(&reopen_opts
);
2138 if (!(flags
& BDRV_O_RDWR
)) {
2139 uint64_t orig_perm
, orig_shared_perm
;
2143 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2145 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2150 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2151 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : qdict_new();
2152 qemu_opts_reset(&reopen_opts
);
2154 if (qdict_haskey(opts
, BDRV_OPT_READ_ONLY
)) {
2155 if (has_rw_option
) {
2156 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY
"'");
2157 qobject_unref(opts
);
2161 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !(flags
& BDRV_O_RDWR
));
2164 if (qdict_haskey(opts
, BDRV_OPT_CACHE_DIRECT
) ||
2165 qdict_haskey(opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
2166 if (has_cache_option
) {
2167 error_report("Cannot set both -c and the cache options");
2168 qobject_unref(opts
);
2172 qdict_put_bool(opts
, BDRV_OPT_CACHE_DIRECT
, flags
& BDRV_O_NOCACHE
);
2173 qdict_put_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, flags
& BDRV_O_NO_FLUSH
);
2176 bdrv_reopen(bs
, opts
, true, &local_err
);
2179 error_report_err(local_err
);
2183 blk_set_enable_write_cache(blk
, !writethrough
);
2187 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2191 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2193 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2200 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2204 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2206 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2213 static const cmdinfo_t break_cmd
= {
2218 .args
= "event tag",
2219 .oneline
= "sets a breakpoint on event and tags the stopped "
2223 static const cmdinfo_t remove_break_cmd
= {
2224 .name
= "remove_break",
2227 .cfunc
= remove_break_f
,
2229 .oneline
= "remove a breakpoint by tag",
2232 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2236 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2238 printf("Could not resume request: %s\n", strerror(-ret
));
2245 static const cmdinfo_t resume_cmd
= {
2251 .oneline
= "resumes the request tagged as tag",
2254 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2256 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2257 aio_poll(blk_get_aio_context(blk
), true);
2262 static const cmdinfo_t wait_break_cmd
= {
2263 .name
= "wait_break",
2266 .cfunc
= wait_break_f
,
2268 .oneline
= "waits for the suspension of a request",
2271 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2276 static const cmdinfo_t abort_cmd
= {
2279 .flags
= CMD_NOFILE_OK
,
2280 .oneline
= "simulate a program crash using abort(3)",
2283 static void sigraise_help(void)
2287 " raises the given signal\n"
2290 " 'sigraise %i' - raises SIGTERM\n"
2292 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2293 " given to sigraise.\n"
2297 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2299 static const cmdinfo_t sigraise_cmd
= {
2301 .cfunc
= sigraise_f
,
2304 .flags
= CMD_NOFILE_OK
,
2306 .oneline
= "raises a signal",
2307 .help
= sigraise_help
,
2310 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2312 int64_t sig
= cvtnum(argv
[1]);
2314 print_cvtnum_err(sig
, argv
[1]);
2316 } else if (sig
> NSIG
) {
2317 printf("signal argument '%s' is too large to be a valid signal\n",
2322 /* Using raise() to kill this process does not necessarily flush all open
2323 * streams. At least stdout and stderr (although the latter should be
2324 * non-buffered anyway) should be flushed, though. */
2333 static void sleep_cb(void *opaque
)
2335 bool *expired
= opaque
;
2339 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2343 struct QEMUTimer
*timer
;
2344 bool expired
= false;
2346 ms
= strtol(argv
[1], &endptr
, 0);
2347 if (ms
< 0 || *endptr
!= '\0') {
2348 printf("%s is not a valid number\n", argv
[1]);
2352 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2353 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2356 main_loop_wait(false);
2363 static const cmdinfo_t sleep_cmd
= {
2368 .flags
= CMD_NOFILE_OK
,
2369 .oneline
= "waits for the given value in milliseconds",
2372 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2377 printf("%s ", ct
->args
);
2379 printf("-- %s\n", ct
->oneline
);
2382 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2384 help_oneline(cmd
, ct
);
2390 static void help_all(void)
2392 const cmdinfo_t
*ct
;
2394 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2395 help_oneline(ct
->name
, ct
);
2397 printf("\nUse 'help commandname' for extended help.\n");
2400 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2402 const cmdinfo_t
*ct
;
2409 ct
= find_command(argv
[1]);
2411 printf("command %s not found\n", argv
[1]);
2415 help_onecmd(argv
[1], ct
);
2419 static const cmdinfo_t help_cmd
= {
2425 .flags
= CMD_FLAG_GLOBAL
,
2426 .args
= "[command]",
2427 .oneline
= "help for one or all commands",
2431 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2432 * context acquired if blk is NULL.
2434 int qemuio_command(BlockBackend
*blk
, const char *cmd
)
2437 const cmdinfo_t
*ct
;
2442 input
= g_strdup(cmd
);
2443 v
= breakline(input
, &c
);
2445 ct
= find_command(v
[0]);
2447 ret
= command(blk
, ct
, c
, v
);
2449 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2459 static void __attribute((constructor
)) init_qemuio_commands(void)
2461 /* initialize commands */
2462 qemuio_add_command(&help_cmd
);
2463 qemuio_add_command(&read_cmd
);
2464 qemuio_add_command(&readv_cmd
);
2465 qemuio_add_command(&write_cmd
);
2466 qemuio_add_command(&writev_cmd
);
2467 qemuio_add_command(&aio_read_cmd
);
2468 qemuio_add_command(&aio_write_cmd
);
2469 qemuio_add_command(&aio_flush_cmd
);
2470 qemuio_add_command(&flush_cmd
);
2471 qemuio_add_command(&truncate_cmd
);
2472 qemuio_add_command(&length_cmd
);
2473 qemuio_add_command(&info_cmd
);
2474 qemuio_add_command(&discard_cmd
);
2475 qemuio_add_command(&alloc_cmd
);
2476 qemuio_add_command(&map_cmd
);
2477 qemuio_add_command(&reopen_cmd
);
2478 qemuio_add_command(&break_cmd
);
2479 qemuio_add_command(&remove_break_cmd
);
2480 qemuio_add_command(&resume_cmd
);
2481 qemuio_add_command(&wait_break_cmd
);
2482 qemuio_add_command(&abort_cmd
);
2483 qemuio_add_command(&sleep_cmd
);
2484 qemuio_add_command(&sigraise_cmd
);