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
)
544 if (bytes
> INT_MAX
) {
548 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, bytes
);
555 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
556 int64_t bytes
, int flags
, int64_t *total
)
558 if (bytes
> INT_MAX
) {
562 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, bytes
, flags
);
579 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
581 CoWriteZeroes
*data
= opaque
;
583 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->bytes
,
587 *data
->total
= data
->ret
;
591 *data
->total
= data
->bytes
;
594 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
595 int64_t bytes
, int flags
, int64_t *total
)
598 CoWriteZeroes data
= {
607 if (bytes
> INT_MAX
) {
611 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
612 bdrv_coroutine_enter(blk_bs(blk
), co
);
614 aio_poll(blk_get_aio_context(blk
), true);
623 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
624 int64_t bytes
, int64_t *total
)
628 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
632 ret
= blk_pwrite_compressed(blk
, offset
, buf
, bytes
);
640 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
641 int64_t count
, int64_t *total
)
643 if (count
> INT_MAX
) {
647 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
654 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
655 int64_t count
, int64_t *total
)
657 if (count
> INT_MAX
) {
661 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
668 #define NOT_DONE 0x7fffffff
669 static void aio_rw_done(void *opaque
, int ret
)
671 *(int *)opaque
= ret
;
674 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
675 int64_t offset
, int *total
)
677 int async_ret
= NOT_DONE
;
679 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
680 while (async_ret
== NOT_DONE
) {
681 main_loop_wait(false);
685 return async_ret
< 0 ? async_ret
: 1;
688 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
689 int64_t offset
, int flags
, int *total
)
691 int async_ret
= NOT_DONE
;
693 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
694 while (async_ret
== NOT_DONE
) {
695 main_loop_wait(false);
699 return async_ret
< 0 ? async_ret
: 1;
702 static void read_help(void)
706 " reads a range of bytes from the given offset\n"
709 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
711 " Reads a segment of the currently open file, optionally dumping it to the\n"
712 " standard output stream (with -v option) for subsequent inspection.\n"
713 " -b, -- read from the VM state rather than the virtual disk\n"
714 " -C, -- report statistics in a machine parsable format\n"
715 " -l, -- length for pattern verification (only with -P)\n"
716 " -p, -- ignored for backwards compatibility\n"
717 " -P, -- use a pattern to verify read data\n"
718 " -q, -- quiet mode, do not show I/O statistics\n"
719 " -s, -- start offset for pattern verification (only with -P)\n"
720 " -v, -- dump buffer to standard output\n"
724 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
726 static const cmdinfo_t read_cmd
= {
732 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
733 .oneline
= "reads a number of bytes at a specified offset",
737 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
739 struct timespec t1
, t2
;
740 bool Cflag
= false, qflag
= false, vflag
= false;
741 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
746 /* Some compilers get confused and warn if this is not initialized. */
749 int64_t pattern_offset
= 0, pattern_count
= 0;
751 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
761 pattern_count
= cvtnum(optarg
);
762 if (pattern_count
< 0) {
763 print_cvtnum_err(pattern_count
, optarg
);
764 return pattern_count
;
768 /* Ignored for backwards compatibility */
772 pattern
= parse_pattern(optarg
);
782 pattern_offset
= cvtnum(optarg
);
783 if (pattern_offset
< 0) {
784 print_cvtnum_err(pattern_offset
, optarg
);
785 return pattern_offset
;
792 qemuio_command_usage(&read_cmd
);
797 if (optind
!= argc
- 2) {
798 qemuio_command_usage(&read_cmd
);
802 offset
= cvtnum(argv
[optind
]);
804 print_cvtnum_err(offset
, argv
[optind
]);
809 count
= cvtnum(argv
[optind
]);
811 print_cvtnum_err(count
, argv
[optind
]);
813 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
814 printf("length cannot exceed %" PRIu64
", given %s\n",
815 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
819 if (!Pflag
&& (lflag
|| sflag
)) {
820 qemuio_command_usage(&read_cmd
);
825 pattern_count
= count
- pattern_offset
;
828 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
829 printf("pattern verification range exceeds end of read data\n");
834 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
835 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
839 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
840 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
846 buf
= qemu_io_alloc(blk
, count
, 0xab);
848 clock_gettime(CLOCK_MONOTONIC
, &t1
);
850 ret
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
852 ret
= do_pread(blk
, buf
, offset
, count
, &total
);
854 clock_gettime(CLOCK_MONOTONIC
, &t2
);
857 printf("read failed: %s\n", strerror(-ret
));
865 void *cmp_buf
= g_malloc(pattern_count
);
866 memset(cmp_buf
, pattern
, pattern_count
);
867 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
868 printf("Pattern verification failed at offset %"
869 PRId64
", %"PRId64
" bytes\n",
870 offset
+ pattern_offset
, pattern_count
);
881 dump_buffer(buf
, offset
, count
);
884 /* Finally, report back -- -C gives a parsable format */
886 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
893 static void readv_help(void)
897 " reads a range of bytes from the given offset into multiple buffers\n"
900 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
902 " Reads a segment of the currently open file, optionally dumping it to the\n"
903 " standard output stream (with -v option) for subsequent inspection.\n"
904 " Uses multiple iovec buffers if more than one byte range is specified.\n"
905 " -C, -- report statistics in a machine parsable format\n"
906 " -P, -- use a pattern to verify read data\n"
907 " -v, -- dump buffer to standard output\n"
908 " -q, -- quiet mode, do not show I/O statistics\n"
912 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
914 static const cmdinfo_t readv_cmd
= {
919 .args
= "[-Cqv] [-P pattern] off len [len..]",
920 .oneline
= "reads a number of bytes at a specified offset",
924 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
926 struct timespec t1
, t2
;
927 bool Cflag
= false, qflag
= false, vflag
= false;
931 /* Some compilers get confused and warn if this is not initialized. */
938 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
945 pattern
= parse_pattern(optarg
);
957 qemuio_command_usage(&readv_cmd
);
962 if (optind
> argc
- 2) {
963 qemuio_command_usage(&readv_cmd
);
968 offset
= cvtnum(argv
[optind
]);
970 print_cvtnum_err(offset
, argv
[optind
]);
975 nr_iov
= argc
- optind
;
976 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
981 clock_gettime(CLOCK_MONOTONIC
, &t1
);
982 ret
= do_aio_readv(blk
, &qiov
, offset
, &total
);
983 clock_gettime(CLOCK_MONOTONIC
, &t2
);
986 printf("readv failed: %s\n", strerror(-ret
));
994 void *cmp_buf
= g_malloc(qiov
.size
);
995 memset(cmp_buf
, pattern
, qiov
.size
);
996 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
997 printf("Pattern verification failed at offset %"
998 PRId64
", %zu bytes\n", offset
, qiov
.size
);
1009 dump_buffer(buf
, offset
, qiov
.size
);
1012 /* Finally, report back -- -C gives a parsable format */
1014 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1017 qemu_iovec_destroy(&qiov
);
1022 static void write_help(void)
1026 " writes a range of bytes from the given offset\n"
1029 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1031 " Writes into a segment of the currently open file, using a buffer\n"
1032 " filled with a set pattern (0xcdcdcdcd).\n"
1033 " -b, -- write to the VM state rather than the virtual disk\n"
1034 " -c, -- write compressed data with blk_write_compressed\n"
1035 " -f, -- use Force Unit Access semantics\n"
1036 " -n, -- with -z, don't allow slow fallback\n"
1037 " -p, -- ignored for backwards compatibility\n"
1038 " -P, -- use different pattern to fill file\n"
1039 " -s, -- use a pattern file to fill the write buffer\n"
1040 " -C, -- report statistics in a machine parsable format\n"
1041 " -q, -- quiet mode, do not show I/O statistics\n"
1042 " -u, -- with -z, allow unmapping\n"
1043 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
1047 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
1049 static const cmdinfo_t write_cmd
= {
1053 .perm
= BLK_PERM_WRITE
,
1056 .args
= "[-bcCfnquz] [-P pattern | -s source_file] off len",
1057 .oneline
= "writes a number of bytes at a specified offset",
1061 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
1063 struct timespec t1
, t2
;
1064 bool Cflag
= false, qflag
= false, bflag
= false;
1065 bool Pflag
= false, zflag
= false, cflag
= false, sflag
= false;
1071 /* Some compilers get confused and warn if this is not initialized. */
1074 const char *file_name
= NULL
;
1076 while ((c
= getopt(argc
, argv
, "bcCfnpP:qs:uz")) != -1) {
1088 flags
|= BDRV_REQ_FUA
;
1091 flags
|= BDRV_REQ_NO_FALLBACK
;
1094 /* Ignored for backwards compatibility */
1098 pattern
= parse_pattern(optarg
);
1111 flags
|= BDRV_REQ_MAY_UNMAP
;
1117 qemuio_command_usage(&write_cmd
);
1122 if (optind
!= argc
- 2) {
1123 qemuio_command_usage(&write_cmd
);
1127 if (bflag
&& zflag
) {
1128 printf("-b and -z cannot be specified at the same time\n");
1132 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
1133 printf("-f and -b or -c cannot be specified at the same time\n");
1137 if ((flags
& BDRV_REQ_NO_FALLBACK
) && !zflag
) {
1138 printf("-n requires -z to be specified\n");
1142 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
1143 printf("-u requires -z to be specified\n");
1147 if (zflag
+ Pflag
+ sflag
> 1) {
1148 printf("Only one of -z, -P, and -s "
1149 "can be specified at the same time\n");
1153 offset
= cvtnum(argv
[optind
]);
1155 print_cvtnum_err(offset
, argv
[optind
]);
1160 count
= cvtnum(argv
[optind
]);
1162 print_cvtnum_err(count
, argv
[optind
]);
1164 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1165 printf("length cannot exceed %" PRIu64
", given %s\n",
1166 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1170 if (bflag
|| cflag
) {
1171 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
1172 printf("%" PRId64
" is not a sector-aligned value for 'offset'\n",
1177 if (!QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
)) {
1178 printf("%"PRId64
" is not a sector-aligned value for 'count'\n",
1186 buf
= qemu_io_alloc_from_file(blk
, count
, file_name
);
1191 buf
= qemu_io_alloc(blk
, count
, pattern
);
1195 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1197 ret
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1199 ret
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1201 ret
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1203 ret
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1205 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1208 printf("write failed: %s\n", strerror(-ret
));
1219 /* Finally, report back -- -C gives a parsable format */
1221 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1235 " writes a range of bytes from the given offset source from multiple buffers\n"
1238 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1240 " Writes into a segment of the currently open file, using a buffer\n"
1241 " filled with a set pattern (0xcdcdcdcd).\n"
1242 " -P, -- use different pattern to fill file\n"
1243 " -C, -- report statistics in a machine parsable format\n"
1244 " -f, -- use Force Unit Access semantics\n"
1245 " -q, -- quiet mode, do not show I/O statistics\n"
1249 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1251 static const cmdinfo_t writev_cmd
= {
1254 .perm
= BLK_PERM_WRITE
,
1257 .args
= "[-Cfq] [-P pattern] off len [len..]",
1258 .oneline
= "writes a number of bytes at a specified offset",
1259 .help
= writev_help
,
1262 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1264 struct timespec t1
, t2
;
1265 bool Cflag
= false, qflag
= false;
1270 /* Some compilers get confused and warn if this is not initialized. */
1276 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1282 flags
|= BDRV_REQ_FUA
;
1288 pattern
= parse_pattern(optarg
);
1294 qemuio_command_usage(&writev_cmd
);
1299 if (optind
> argc
- 2) {
1300 qemuio_command_usage(&writev_cmd
);
1304 offset
= cvtnum(argv
[optind
]);
1306 print_cvtnum_err(offset
, argv
[optind
]);
1311 nr_iov
= argc
- optind
;
1312 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1317 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1318 ret
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1319 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1322 printf("writev failed: %s\n", strerror(-ret
));
1333 /* Finally, report back -- -C gives a parsable format */
1335 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1337 qemu_iovec_destroy(&qiov
);
1352 BlockAcctCookie acct
;
1357 static void aio_write_done(void *opaque
, int ret
)
1359 struct aio_ctx
*ctx
= opaque
;
1362 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1366 printf("aio_write failed: %s\n", strerror(-ret
));
1367 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1371 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1377 /* Finally, report back -- -C gives a parsable format */
1378 t2
= tsub(t2
, ctx
->t1
);
1379 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1380 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1383 qemu_io_free(ctx
->buf
);
1384 qemu_iovec_destroy(&ctx
->qiov
);
1389 static void aio_read_done(void *opaque
, int ret
)
1391 struct aio_ctx
*ctx
= opaque
;
1394 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1397 printf("readv failed: %s\n", strerror(-ret
));
1398 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1403 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1405 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1406 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1407 printf("Pattern verification failed at offset %"
1408 PRId64
", %zu bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1413 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1420 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1423 /* Finally, report back -- -C gives a parsable format */
1424 t2
= tsub(t2
, ctx
->t1
);
1425 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1426 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1428 qemu_io_free(ctx
->buf
);
1429 qemu_iovec_destroy(&ctx
->qiov
);
1433 static void aio_read_help(void)
1437 " asynchronously reads a range of bytes from the given offset\n"
1440 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1442 " Reads a segment of the currently open file, optionally dumping it to the\n"
1443 " standard output stream (with -v option) for subsequent inspection.\n"
1444 " The read is performed asynchronously and the aio_flush command must be\n"
1445 " used to ensure all outstanding aio requests have been completed.\n"
1446 " Note that due to its asynchronous nature, this command will be\n"
1447 " considered successful once the request is submitted, independently\n"
1448 " of potential I/O errors or pattern mismatches.\n"
1449 " -C, -- report statistics in a machine parsable format\n"
1450 " -P, -- use a pattern to verify read data\n"
1451 " -i, -- treat request as invalid, for exercising stats\n"
1452 " -v, -- dump buffer to standard output\n"
1453 " -q, -- quiet mode, do not show I/O statistics\n"
1457 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1459 static const cmdinfo_t aio_read_cmd
= {
1461 .cfunc
= aio_read_f
,
1464 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1465 .oneline
= "asynchronously reads a number of bytes",
1466 .help
= aio_read_help
,
1469 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1472 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1475 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1482 ctx
->pattern
= parse_pattern(optarg
);
1483 if (ctx
->pattern
< 0) {
1489 printf("injecting invalid read request\n");
1490 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1501 qemuio_command_usage(&aio_read_cmd
);
1506 if (optind
> argc
- 2) {
1508 qemuio_command_usage(&aio_read_cmd
);
1512 ctx
->offset
= cvtnum(argv
[optind
]);
1513 if (ctx
->offset
< 0) {
1514 int ret
= ctx
->offset
;
1515 print_cvtnum_err(ret
, argv
[optind
]);
1521 nr_iov
= argc
- optind
;
1522 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1523 if (ctx
->buf
== NULL
) {
1524 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1529 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1530 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1532 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1536 static void aio_write_help(void)
1540 " asynchronously writes a range of bytes from the given offset source\n"
1541 " from multiple buffers\n"
1544 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1546 " Writes into a segment of the currently open file, using a buffer\n"
1547 " filled with a set pattern (0xcdcdcdcd).\n"
1548 " The write is performed asynchronously and the aio_flush command must be\n"
1549 " used to ensure all outstanding aio requests have been completed.\n"
1550 " Note that due to its asynchronous nature, this command will be\n"
1551 " considered successful once the request is submitted, independently\n"
1552 " of potential I/O errors or pattern mismatches.\n"
1553 " -P, -- use different pattern to fill file\n"
1554 " -C, -- report statistics in a machine parsable format\n"
1555 " -f, -- use Force Unit Access semantics\n"
1556 " -i, -- treat request as invalid, for exercising stats\n"
1557 " -q, -- quiet mode, do not show I/O statistics\n"
1558 " -u, -- with -z, allow unmapping\n"
1559 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1563 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1565 static const cmdinfo_t aio_write_cmd
= {
1566 .name
= "aio_write",
1567 .cfunc
= aio_write_f
,
1568 .perm
= BLK_PERM_WRITE
,
1571 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1572 .oneline
= "asynchronously writes a number of bytes",
1573 .help
= aio_write_help
,
1576 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1580 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1584 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1590 flags
|= BDRV_REQ_FUA
;
1596 flags
|= BDRV_REQ_MAY_UNMAP
;
1599 pattern
= parse_pattern(optarg
);
1606 printf("injecting invalid write request\n");
1607 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1615 qemuio_command_usage(&aio_write_cmd
);
1620 if (optind
> argc
- 2) {
1622 qemuio_command_usage(&aio_write_cmd
);
1626 if (ctx
->zflag
&& optind
!= argc
- 2) {
1627 printf("-z supports only a single length parameter\n");
1632 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1633 printf("-u requires -z to be specified\n");
1638 if (ctx
->zflag
&& ctx
->Pflag
) {
1639 printf("-z and -P cannot be specified at the same time\n");
1644 ctx
->offset
= cvtnum(argv
[optind
]);
1645 if (ctx
->offset
< 0) {
1646 int ret
= ctx
->offset
;
1647 print_cvtnum_err(ret
, argv
[optind
]);
1654 int64_t count
= cvtnum(argv
[optind
]);
1656 print_cvtnum_err(count
, argv
[optind
]);
1661 ctx
->qiov
.size
= count
;
1662 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1665 nr_iov
= argc
- optind
;
1666 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1668 if (ctx
->buf
== NULL
) {
1669 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1674 clock_gettime(CLOCK_MONOTONIC
, &ctx
->t1
);
1675 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1678 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1685 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1687 BlockAcctCookie cookie
;
1688 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1690 block_acct_done(blk_get_stats(blk
), &cookie
);
1694 static const cmdinfo_t aio_flush_cmd
= {
1695 .name
= "aio_flush",
1696 .cfunc
= aio_flush_f
,
1697 .oneline
= "completes all outstanding aio requests"
1700 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1702 return blk_flush(blk
);
1705 static const cmdinfo_t flush_cmd
= {
1709 .oneline
= "flush all in-core file state to disk",
1712 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
);
1713 static const cmdinfo_t truncate_cmd
= {
1716 .cfunc
= truncate_f
,
1717 .perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1720 .args
= "[-m prealloc_mode] off",
1721 .oneline
= "truncates the current file at the given offset",
1724 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1726 Error
*local_err
= NULL
;
1729 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
1731 while ((c
= getopt(argc
, argv
, "m:")) != -1) {
1734 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
1735 PREALLOC_MODE__MAX
, NULL
);
1736 if (prealloc
== PREALLOC_MODE__MAX
) {
1737 error_report("Invalid preallocation mode '%s'", optarg
);
1742 qemuio_command_usage(&truncate_cmd
);
1747 offset
= cvtnum(argv
[optind
]);
1749 print_cvtnum_err(offset
, argv
[1]);
1754 * qemu-io is a debugging tool, so let us be strict here and pass
1755 * exact=true. It is better to err on the "emit more errors" side
1756 * than to be overly permissive.
1758 ret
= blk_truncate(blk
, offset
, false, prealloc
, 0, &local_err
);
1760 error_report_err(local_err
);
1767 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1772 size
= blk_getlength(blk
);
1774 printf("getlength: %s\n", strerror(-size
));
1778 cvtstr(size
, s1
, sizeof(s1
));
1784 static const cmdinfo_t length_cmd
= {
1788 .oneline
= "gets the length of the current file",
1792 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1794 BlockDriverState
*bs
= blk_bs(blk
);
1795 BlockDriverInfo bdi
;
1796 ImageInfoSpecific
*spec_info
;
1797 Error
*local_err
= NULL
;
1798 char s1
[64], s2
[64];
1801 if (bs
->drv
&& bs
->drv
->format_name
) {
1802 printf("format name: %s\n", bs
->drv
->format_name
);
1804 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1805 printf("format name: %s\n", bs
->drv
->protocol_name
);
1808 ret
= bdrv_get_info(bs
, &bdi
);
1813 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1814 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1816 printf("cluster size: %s\n", s1
);
1817 printf("vm state offset: %s\n", s2
);
1819 spec_info
= bdrv_get_specific_info(bs
, &local_err
);
1821 error_report_err(local_err
);
1825 printf("Format specific information:\n");
1826 bdrv_image_info_specific_dump(spec_info
);
1827 qapi_free_ImageInfoSpecific(spec_info
);
1835 static const cmdinfo_t info_cmd
= {
1839 .oneline
= "prints information about the current file",
1842 static void discard_help(void)
1846 " discards a range of bytes from the given offset\n"
1849 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1851 " Discards a segment of the currently open file.\n"
1852 " -C, -- report statistics in a machine parsable format\n"
1853 " -q, -- quiet mode, do not show I/O statistics\n"
1857 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1859 static const cmdinfo_t discard_cmd
= {
1863 .perm
= BLK_PERM_WRITE
,
1866 .args
= "[-Cq] off len",
1867 .oneline
= "discards a number of bytes at a specified offset",
1868 .help
= discard_help
,
1871 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1873 struct timespec t1
, t2
;
1874 bool Cflag
= false, qflag
= false;
1876 int64_t offset
, bytes
;
1878 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1887 qemuio_command_usage(&discard_cmd
);
1892 if (optind
!= argc
- 2) {
1893 qemuio_command_usage(&discard_cmd
);
1897 offset
= cvtnum(argv
[optind
]);
1899 print_cvtnum_err(offset
, argv
[optind
]);
1904 bytes
= cvtnum(argv
[optind
]);
1906 print_cvtnum_err(bytes
, argv
[optind
]);
1908 } else if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
1909 printf("length cannot exceed %"PRIu64
", given %s\n",
1910 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1914 clock_gettime(CLOCK_MONOTONIC
, &t1
);
1915 ret
= blk_pdiscard(blk
, offset
, bytes
);
1916 clock_gettime(CLOCK_MONOTONIC
, &t2
);
1919 printf("discard failed: %s\n", strerror(-ret
));
1923 /* Finally, report back -- -C gives a parsable format */
1926 print_report("discard", &t2
, offset
, bytes
, bytes
, 1, Cflag
);
1932 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1934 BlockDriverState
*bs
= blk_bs(blk
);
1935 int64_t offset
, start
, remaining
, count
;
1938 int64_t num
, sum_alloc
;
1940 start
= offset
= cvtnum(argv
[1]);
1942 print_cvtnum_err(offset
, argv
[1]);
1947 count
= cvtnum(argv
[2]);
1949 print_cvtnum_err(count
, argv
[2]);
1953 count
= BDRV_SECTOR_SIZE
;
1959 ret
= bdrv_is_allocated(bs
, offset
, remaining
, &num
);
1961 printf("is_allocated failed: %s\n", strerror(-ret
));
1975 cvtstr(start
, s1
, sizeof(s1
));
1977 printf("%"PRId64
"/%"PRId64
" bytes allocated at offset %s\n",
1978 sum_alloc
, count
, s1
);
1982 static const cmdinfo_t alloc_cmd
= {
1988 .args
= "offset [count]",
1989 .oneline
= "checks if offset is allocated in the file",
1993 static int map_is_allocated(BlockDriverState
*bs
, int64_t offset
,
1994 int64_t bytes
, int64_t *pnum
)
2000 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
2001 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
2009 while (bytes
> 0 && ret
== firstret
) {
2013 num_checked
= MIN(bytes
, BDRV_REQUEST_MAX_BYTES
);
2014 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
2015 if (ret
== firstret
&& num
) {
2025 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
2027 int64_t offset
, bytes
;
2028 char s1
[64], s2
[64];
2034 bytes
= blk_getlength(blk
);
2036 error_report("Failed to query image length: %s", strerror(-bytes
));
2041 ret
= map_is_allocated(blk_bs(blk
), offset
, bytes
, &num
);
2043 error_report("Failed to get allocation status: %s", strerror(-ret
));
2046 error_report("Unexpected end of image");
2050 retstr
= ret
? " allocated" : "not allocated";
2051 cvtstr(num
, s1
, sizeof(s1
));
2052 cvtstr(offset
, s2
, sizeof(s2
));
2053 printf("%s (0x%" PRIx64
") bytes %s at offset %s (0x%" PRIx64
")\n",
2054 s1
, num
, retstr
, s2
, offset
);
2063 static const cmdinfo_t map_cmd
= {
2069 .oneline
= "prints the allocated areas of a file",
2072 static void reopen_help(void)
2076 " Changes the open options of an already opened image\n"
2079 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2081 " -r, -- Reopen the image read-only\n"
2082 " -w, -- Reopen the image read-write\n"
2083 " -c, -- Change the cache mode to the given value\n"
2084 " -o, -- Changes block driver options (cf. 'open' command)\n"
2088 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2090 static QemuOptsList reopen_opts
= {
2092 .merge_lists
= true,
2093 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2095 /* no elements => accept any params */
2096 { /* end of list */ }
2100 static const cmdinfo_t reopen_cmd
= {
2105 .args
= "[(-r|-w)] [-c cache] [-o options]",
2106 .oneline
= "reopens an image with new options",
2107 .help
= reopen_help
,
2110 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2112 BlockDriverState
*bs
= blk_bs(blk
);
2116 int flags
= bs
->open_flags
;
2117 bool writethrough
= !blk_enable_write_cache(blk
);
2118 bool has_rw_option
= false;
2119 bool has_cache_option
= false;
2120 Error
*local_err
= NULL
;
2122 while ((c
= getopt(argc
, argv
, "c:o:rw")) != -1) {
2125 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
2126 error_report("Invalid cache option: %s", optarg
);
2129 has_cache_option
= true;
2132 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2133 qemu_opts_reset(&reopen_opts
);
2138 if (has_rw_option
) {
2139 error_report("Only one -r/-w option may be given");
2142 flags
&= ~BDRV_O_RDWR
;
2143 has_rw_option
= true;
2146 if (has_rw_option
) {
2147 error_report("Only one -r/-w option may be given");
2150 flags
|= BDRV_O_RDWR
;
2151 has_rw_option
= true;
2154 qemu_opts_reset(&reopen_opts
);
2155 qemuio_command_usage(&reopen_cmd
);
2160 if (optind
!= argc
) {
2161 qemu_opts_reset(&reopen_opts
);
2162 qemuio_command_usage(&reopen_cmd
);
2166 if (!writethrough
!= blk_enable_write_cache(blk
) &&
2167 blk_get_attached_dev(blk
))
2169 error_report("Cannot change cache.writeback: Device attached");
2170 qemu_opts_reset(&reopen_opts
);
2174 if (!(flags
& BDRV_O_RDWR
)) {
2175 uint64_t orig_perm
, orig_shared_perm
;
2179 blk_get_perm(blk
, &orig_perm
, &orig_shared_perm
);
2181 orig_perm
& ~(BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
),
2186 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2187 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : qdict_new();
2188 qemu_opts_reset(&reopen_opts
);
2190 if (qdict_haskey(opts
, BDRV_OPT_READ_ONLY
)) {
2191 if (has_rw_option
) {
2192 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY
"'");
2193 qobject_unref(opts
);
2197 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !(flags
& BDRV_O_RDWR
));
2200 if (qdict_haskey(opts
, BDRV_OPT_CACHE_DIRECT
) ||
2201 qdict_haskey(opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
2202 if (has_cache_option
) {
2203 error_report("Cannot set both -c and the cache options");
2204 qobject_unref(opts
);
2208 qdict_put_bool(opts
, BDRV_OPT_CACHE_DIRECT
, flags
& BDRV_O_NOCACHE
);
2209 qdict_put_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, flags
& BDRV_O_NO_FLUSH
);
2212 bdrv_reopen(bs
, opts
, true, &local_err
);
2215 error_report_err(local_err
);
2219 blk_set_enable_write_cache(blk
, !writethrough
);
2223 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2227 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2229 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2236 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2240 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2242 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2249 static const cmdinfo_t break_cmd
= {
2254 .args
= "event tag",
2255 .oneline
= "sets a breakpoint on event and tags the stopped "
2259 static const cmdinfo_t remove_break_cmd
= {
2260 .name
= "remove_break",
2263 .cfunc
= remove_break_f
,
2265 .oneline
= "remove a breakpoint by tag",
2268 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2272 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2274 printf("Could not resume request: %s\n", strerror(-ret
));
2281 static const cmdinfo_t resume_cmd
= {
2287 .oneline
= "resumes the request tagged as tag",
2290 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2292 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2293 aio_poll(blk_get_aio_context(blk
), true);
2298 static const cmdinfo_t wait_break_cmd
= {
2299 .name
= "wait_break",
2302 .cfunc
= wait_break_f
,
2304 .oneline
= "waits for the suspension of a request",
2307 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2312 static const cmdinfo_t abort_cmd
= {
2315 .flags
= CMD_NOFILE_OK
,
2316 .oneline
= "simulate a program crash using abort(3)",
2319 static void sigraise_help(void)
2323 " raises the given signal\n"
2326 " 'sigraise %i' - raises SIGTERM\n"
2328 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2329 " given to sigraise.\n"
2333 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2335 static const cmdinfo_t sigraise_cmd
= {
2337 .cfunc
= sigraise_f
,
2340 .flags
= CMD_NOFILE_OK
,
2342 .oneline
= "raises a signal",
2343 .help
= sigraise_help
,
2346 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2348 int64_t sig
= cvtnum(argv
[1]);
2350 print_cvtnum_err(sig
, argv
[1]);
2352 } else if (sig
> NSIG
) {
2353 printf("signal argument '%s' is too large to be a valid signal\n",
2358 /* Using raise() to kill this process does not necessarily flush all open
2359 * streams. At least stdout and stderr (although the latter should be
2360 * non-buffered anyway) should be flushed, though. */
2369 static void sleep_cb(void *opaque
)
2371 bool *expired
= opaque
;
2375 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2379 struct QEMUTimer
*timer
;
2380 bool expired
= false;
2382 ms
= strtol(argv
[1], &endptr
, 0);
2383 if (ms
< 0 || *endptr
!= '\0') {
2384 printf("%s is not a valid number\n", argv
[1]);
2388 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2389 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2392 main_loop_wait(false);
2399 static const cmdinfo_t sleep_cmd
= {
2404 .flags
= CMD_NOFILE_OK
,
2405 .oneline
= "waits for the given value in milliseconds",
2408 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2413 printf("%s ", ct
->args
);
2415 printf("-- %s\n", ct
->oneline
);
2418 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2420 help_oneline(cmd
, ct
);
2426 static void help_all(void)
2428 const cmdinfo_t
*ct
;
2430 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2431 help_oneline(ct
->name
, ct
);
2433 printf("\nUse 'help commandname' for extended help.\n");
2436 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2438 const cmdinfo_t
*ct
;
2445 ct
= find_command(argv
[1]);
2447 printf("command %s not found\n", argv
[1]);
2451 help_onecmd(argv
[1], ct
);
2455 static const cmdinfo_t help_cmd
= {
2461 .flags
= CMD_FLAG_GLOBAL
,
2462 .args
= "[command]",
2463 .oneline
= "help for one or all commands",
2467 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2468 * context acquired if blk is NULL.
2470 int qemuio_command(BlockBackend
*blk
, const char *cmd
)
2473 const cmdinfo_t
*ct
;
2478 input
= g_strdup(cmd
);
2479 v
= breakline(input
, &c
);
2481 ct
= find_command(v
[0]);
2483 ret
= command(blk
, ct
, c
, v
);
2485 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2495 static void __attribute((constructor
)) init_qemuio_commands(void)
2497 /* initialize commands */
2498 qemuio_add_command(&help_cmd
);
2499 qemuio_add_command(&read_cmd
);
2500 qemuio_add_command(&readv_cmd
);
2501 qemuio_add_command(&write_cmd
);
2502 qemuio_add_command(&writev_cmd
);
2503 qemuio_add_command(&aio_read_cmd
);
2504 qemuio_add_command(&aio_write_cmd
);
2505 qemuio_add_command(&aio_flush_cmd
);
2506 qemuio_add_command(&flush_cmd
);
2507 qemuio_add_command(&truncate_cmd
);
2508 qemuio_add_command(&length_cmd
);
2509 qemuio_add_command(&info_cmd
);
2510 qemuio_add_command(&discard_cmd
);
2511 qemuio_add_command(&alloc_cmd
);
2512 qemuio_add_command(&map_cmd
);
2513 qemuio_add_command(&reopen_cmd
);
2514 qemuio_add_command(&break_cmd
);
2515 qemuio_add_command(&remove_break_cmd
);
2516 qemuio_add_command(&resume_cmd
);
2517 qemuio_add_command(&wait_break_cmd
);
2518 qemuio_add_command(&abort_cmd
);
2519 qemuio_add_command(&sleep_cmd
);
2520 qemuio_add_command(&sigraise_cmd
);