2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 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.
10 #include <sys/types.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
24 static BlockDriverState
*bs
;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len
, int pattern
)
41 len
+= MISALIGN_OFFSET
;
42 buf
= qemu_memalign(512, len
);
43 memset(buf
, pattern
, len
);
45 buf
+= MISALIGN_OFFSET
;
49 static void qemu_io_free(void *p
)
57 dump_buffer(const void *buffer
, int64_t offset
, int len
)
62 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
65 printf("%08llx: ", (unsigned long long)offset
+ i
);
66 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
69 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
80 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
81 int count
, int total
, int cnt
, int Cflag
)
83 char s1
[64], s2
[64], ts
[64];
85 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
87 cvtstr((double)total
, s1
, sizeof(s1
));
88 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op
, total
, count
, (long long)offset
);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
96 tdiv((double)total
, *t
),
97 tdiv((double)cnt
, *t
));
101 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
105 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
112 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
116 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
123 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
125 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
131 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
133 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
139 #define NOT_DONE 0x7fffffff
140 static void aio_rw_done(void *opaque
, int ret
)
142 *(int *)opaque
= ret
;
145 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
147 BlockDriverAIOCB
*acb
;
148 int async_ret
= NOT_DONE
;
150 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
151 aio_rw_done
, &async_ret
);
155 while (async_ret
== NOT_DONE
)
159 return async_ret
< 0 ? async_ret
: 1;
162 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
164 BlockDriverAIOCB
*acb
;
165 int async_ret
= NOT_DONE
;
167 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
168 aio_rw_done
, &async_ret
);
172 while (async_ret
== NOT_DONE
)
176 return async_ret
< 0 ? async_ret
: 1;
180 static const cmdinfo_t read_cmd
;
187 " reads a range of bytes from the given offset\n"
190 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
192 " Reads a segment of the currently open file, optionally dumping it to the\n"
193 " standard output stream (with -v option) for subsequent inspection.\n"
194 " -C, -- report statistics in a machine parsable format\n"
195 " -l, -- length for pattern verification (only with -P)\n"
196 " -p, -- use bdrv_pread to read the file\n"
197 " -P, -- use a pattern to verify read data\n"
198 " -q, -- quite mode, do not show I/O statistics\n"
199 " -s, -- start offset for pattern verification (only with -P)\n"
200 " -v, -- dump buffer to standard output\n"
205 read_f(int argc
, char **argv
)
207 struct timeval t1
, t2
;
208 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
209 int Pflag
= 0, sflag
= 0, lflag
= 0;
214 /* Some compilers get confused and warn if this is not initialized. */
216 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
218 while ((c
= getopt(argc
, argv
, "Cl:pP:qs:v")) != EOF
) {
225 pattern_count
= cvtnum(optarg
);
226 if (pattern_count
< 0) {
227 printf("non-numeric length argument -- %s\n", optarg
);
236 pattern
= atoi(optarg
);
243 pattern_offset
= cvtnum(optarg
);
244 if (pattern_offset
< 0) {
245 printf("non-numeric length argument -- %s\n", optarg
);
253 return command_usage(&read_cmd
);
257 if (optind
!= argc
- 2)
258 return command_usage(&read_cmd
);
260 offset
= cvtnum(argv
[optind
]);
262 printf("non-numeric length argument -- %s\n", argv
[optind
]);
267 count
= cvtnum(argv
[optind
]);
269 printf("non-numeric length argument -- %s\n", argv
[optind
]);
273 if (!Pflag
&& (lflag
|| sflag
)) {
274 return command_usage(&read_cmd
);
278 pattern_count
= count
- pattern_offset
;
281 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
282 printf("pattern verfication range exceeds end of read data\n");
287 if (offset
& 0x1ff) {
288 printf("offset %lld is not sector aligned\n",
293 printf("count %d is not sector aligned\n",
299 buf
= qemu_io_alloc(count
, 0xab);
301 gettimeofday(&t1
, NULL
);
303 cnt
= do_pread(buf
, offset
, count
, &total
);
305 cnt
= do_read(buf
, offset
, count
, &total
);
306 gettimeofday(&t2
, NULL
);
309 printf("read failed: %s\n", strerror(-cnt
));
314 void* cmp_buf
= malloc(pattern_count
);
315 memset(cmp_buf
, pattern
, pattern_count
);
316 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
317 printf("Pattern verification failed at offset %lld, "
319 (long long) offset
+ pattern_offset
, pattern_count
);
328 dump_buffer(buf
, offset
, count
);
330 /* Finally, report back -- -C gives a parsable format */
332 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
339 static const cmdinfo_t read_cmd
= {
345 .args
= "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
346 .oneline
= "reads a number of bytes at a specified offset",
350 static const cmdinfo_t readv_cmd
;
357 " reads a range of bytes from the given offset into multiple buffers\n"
360 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
362 " Reads a segment of the currently open file, optionally dumping it to the\n"
363 " standard output stream (with -v option) for subsequent inspection.\n"
364 " Uses multiple iovec buffers if more than one byte range is specified.\n"
365 " -C, -- report statistics in a machine parsable format\n"
366 " -P, -- use a pattern to verify read data\n"
367 " -v, -- dump buffer to standard output\n"
368 " -q, -- quite mode, do not show I/O statistics\n"
373 readv_f(int argc
, char **argv
)
375 struct timeval t1
, t2
;
376 int Cflag
= 0, qflag
= 0, vflag
= 0;
380 int count
= 0, total
;
386 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
393 pattern
= atoi(optarg
);
402 return command_usage(&readv_cmd
);
406 if (optind
> argc
- 2)
407 return command_usage(&readv_cmd
);
410 offset
= cvtnum(argv
[optind
]);
412 printf("non-numeric length argument -- %s\n", argv
[optind
]);
417 if (offset
& 0x1ff) {
418 printf("offset %lld is not sector aligned\n",
424 printf("count %d is not sector aligned\n",
429 for (i
= optind
; i
< argc
; i
++) {
432 len
= cvtnum(argv
[i
]);
434 printf("non-numeric length argument -- %s\n", argv
[i
]);
440 nr_iov
= argc
- optind
;
441 qemu_iovec_init(&qiov
, nr_iov
);
442 buf
= p
= qemu_io_alloc(count
, 0xab);
443 for (i
= 0; i
< nr_iov
; i
++) {
446 len
= cvtnum(argv
[optind
]);
448 printf("non-numeric length argument -- %s\n",
453 qemu_iovec_add(&qiov
, p
, len
);
458 gettimeofday(&t1
, NULL
);
459 cnt
= do_aio_readv(&qiov
, offset
, &total
);
460 gettimeofday(&t2
, NULL
);
463 printf("readv failed: %s\n", strerror(-cnt
));
468 void* cmp_buf
= malloc(count
);
469 memset(cmp_buf
, pattern
, count
);
470 if (memcmp(buf
, cmp_buf
, count
)) {
471 printf("Pattern verification failed at offset %lld, "
473 (long long) offset
, count
);
482 dump_buffer(buf
, offset
, qiov
.size
);
484 /* Finally, report back -- -C gives a parsable format */
486 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
493 static const cmdinfo_t readv_cmd
= {
498 .args
= "[-Cqv] [-P pattern ] off len [len..]",
499 .oneline
= "reads a number of bytes at a specified offset",
503 static const cmdinfo_t write_cmd
;
510 " writes a range of bytes from the given offset\n"
513 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
515 " Writes into a segment of the currently open file, using a buffer\n"
516 " filled with a set pattern (0xcdcdcdcd).\n"
517 " -p, -- use bdrv_pwrite to write the file\n"
518 " -P, -- use different pattern to fill file\n"
519 " -C, -- report statistics in a machine parsable format\n"
520 " -q, -- quite mode, do not show I/O statistics\n"
525 write_f(int argc
, char **argv
)
527 struct timeval t1
, t2
;
528 int Cflag
= 0, pflag
= 0, qflag
= 0;
533 /* Some compilers get confused and warn if this is not initialized. */
537 while ((c
= getopt(argc
, argv
, "CpP:q")) != EOF
) {
546 pattern
= atoi(optarg
);
552 return command_usage(&write_cmd
);
556 if (optind
!= argc
- 2)
557 return command_usage(&write_cmd
);
559 offset
= cvtnum(argv
[optind
]);
561 printf("non-numeric length argument -- %s\n", argv
[optind
]);
566 count
= cvtnum(argv
[optind
]);
568 printf("non-numeric length argument -- %s\n", argv
[optind
]);
573 if (offset
& 0x1ff) {
574 printf("offset %lld is not sector aligned\n",
580 printf("count %d is not sector aligned\n",
586 buf
= qemu_io_alloc(count
, pattern
);
588 gettimeofday(&t1
, NULL
);
590 cnt
= do_pwrite(buf
, offset
, count
, &total
);
592 cnt
= do_write(buf
, offset
, count
, &total
);
593 gettimeofday(&t2
, NULL
);
596 printf("write failed: %s\n", strerror(-cnt
));
603 /* Finally, report back -- -C gives a parsable format */
605 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
612 static const cmdinfo_t write_cmd
= {
618 .args
= "[-aCpq] [-P pattern ] off len",
619 .oneline
= "writes a number of bytes at a specified offset",
623 static const cmdinfo_t writev_cmd
;
630 " writes a range of bytes from the given offset source from multiple buffers\n"
633 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
635 " Writes into a segment of the currently open file, using a buffer\n"
636 " filled with a set pattern (0xcdcdcdcd).\n"
637 " -P, -- use different pattern to fill file\n"
638 " -C, -- report statistics in a machine parsable format\n"
639 " -q, -- quite mode, do not show I/O statistics\n"
644 writev_f(int argc
, char **argv
)
646 struct timeval t1
, t2
;
647 int Cflag
= 0, qflag
= 0;
651 int count
= 0, total
;
656 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
665 pattern
= atoi(optarg
);
668 return command_usage(&writev_cmd
);
672 if (optind
> argc
- 2)
673 return command_usage(&writev_cmd
);
675 offset
= cvtnum(argv
[optind
]);
677 printf("non-numeric length argument -- %s\n", argv
[optind
]);
682 if (offset
& 0x1ff) {
683 printf("offset %lld is not sector aligned\n",
689 printf("count %d is not sector aligned\n",
695 for (i
= optind
; i
< argc
; i
++) {
698 len
= cvtnum(argv
[optind
]);
700 printf("non-numeric length argument -- %s\n", argv
[i
]);
706 nr_iov
= argc
- optind
;
707 qemu_iovec_init(&qiov
, nr_iov
);
708 buf
= p
= qemu_io_alloc(count
, pattern
);
709 for (i
= 0; i
< nr_iov
; i
++) {
712 len
= cvtnum(argv
[optind
]);
714 printf("non-numeric length argument -- %s\n",
719 qemu_iovec_add(&qiov
, p
, len
);
724 gettimeofday(&t1
, NULL
);
725 cnt
= do_aio_writev(&qiov
, offset
, &total
);
726 gettimeofday(&t2
, NULL
);
729 printf("writev failed: %s\n", strerror(-cnt
));
736 /* Finally, report back -- -C gives a parsable format */
738 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
745 static const cmdinfo_t writev_cmd
= {
750 .args
= "[-Cq] [-P pattern ] off len [len..]",
751 .oneline
= "writes a number of bytes at a specified offset",
768 aio_write_done(void *opaque
, int ret
)
770 struct aio_ctx
*ctx
= opaque
;
773 gettimeofday(&t2
, NULL
);
777 printf("aio_write failed: %s\n", strerror(-ret
));
785 /* Finally, report back -- -C gives a parsable format */
786 t2
= tsub(t2
, ctx
->t1
);
787 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
788 ctx
->qiov
.size
, 1, ctx
->Cflag
);
790 qemu_io_free(ctx
->buf
);
794 static const cmdinfo_t aio_read_cmd
;
797 aio_read_done(void *opaque
, int ret
)
799 struct aio_ctx
*ctx
= opaque
;
802 gettimeofday(&t2
, NULL
);
805 printf("readv failed: %s\n", strerror(-ret
));
810 void *cmp_buf
= malloc(ctx
->qiov
.size
);
812 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
813 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
814 printf("Pattern verification failed at offset %lld, "
816 (long long) ctx
->offset
, ctx
->qiov
.size
);
826 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
829 /* Finally, report back -- -C gives a parsable format */
830 t2
= tsub(t2
, ctx
->t1
);
831 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
832 ctx
->qiov
.size
, 1, ctx
->Cflag
);
834 qemu_io_free(ctx
->buf
);
843 " asynchronously reads a range of bytes from the given offset\n"
846 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
848 " Reads a segment of the currently open file, optionally dumping it to the\n"
849 " standard output stream (with -v option) for subsequent inspection.\n"
850 " The read is performed asynchronously and should the aio_flush command \n"
851 " should be used to ensure all outstanding aio requests have been completed\n"
852 " -C, -- report statistics in a machine parsable format\n"
853 " -P, -- use a pattern to verify read data\n"
854 " -v, -- dump buffer to standard output\n"
855 " -q, -- quite mode, do not show I/O statistics\n"
860 aio_read_f(int argc
, char **argv
)
865 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
866 BlockDriverAIOCB
*acb
;
868 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
875 ctx
->pattern
= atoi(optarg
);
884 return command_usage(&aio_read_cmd
);
888 if (optind
> argc
- 2)
889 return command_usage(&aio_read_cmd
);
892 ctx
->offset
= cvtnum(argv
[optind
]);
893 if (ctx
->offset
< 0) {
894 printf("non-numeric length argument -- %s\n", argv
[optind
]);
899 if (ctx
->offset
& 0x1ff) {
900 printf("offset %lld is not sector aligned\n",
901 (long long)ctx
->offset
);
906 printf("count %d is not sector aligned\n",
911 for (i
= optind
; i
< argc
; i
++) {
914 len
= cvtnum(argv
[i
]);
916 printf("non-numeric length argument -- %s\n", argv
[i
]);
922 nr_iov
= argc
- optind
;
923 qemu_iovec_init(&ctx
->qiov
, nr_iov
);
924 ctx
->buf
= p
= qemu_io_alloc(count
, 0xab);
925 for (i
= 0; i
< nr_iov
; i
++) {
928 len
= cvtnum(argv
[optind
]);
930 printf("non-numeric length argument -- %s\n",
935 qemu_iovec_add(&ctx
->qiov
, p
, len
);
940 gettimeofday(&ctx
->t1
, NULL
);
941 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
942 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
949 static const cmdinfo_t aio_read_cmd
= {
954 .args
= "[-Cqv] [-P pattern ] off len [len..]",
955 .oneline
= "asynchronously reads a number of bytes",
956 .help
= aio_read_help
,
959 static const cmdinfo_t aio_write_cmd
;
966 " asynchronously writes a range of bytes from the given offset source \n"
967 " from multiple buffers\n"
970 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
972 " Writes into a segment of the currently open file, using a buffer\n"
973 " filled with a set pattern (0xcdcdcdcd).\n"
974 " The write is performed asynchronously and should the aio_flush command \n"
975 " should be used to ensure all outstanding aio requests have been completed\n"
976 " -P, -- use different pattern to fill file\n"
977 " -C, -- report statistics in a machine parsable format\n"
978 " -q, -- quite mode, do not show I/O statistics\n"
984 aio_write_f(int argc
, char **argv
)
990 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
991 BlockDriverAIOCB
*acb
;
993 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1002 pattern
= atoi(optarg
);
1005 return command_usage(&aio_write_cmd
);
1009 if (optind
> argc
- 2)
1010 return command_usage(&aio_write_cmd
);
1012 ctx
->offset
= cvtnum(argv
[optind
]);
1013 if (ctx
->offset
< 0) {
1014 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1019 if (ctx
->offset
& 0x1ff) {
1020 printf("offset %lld is not sector aligned\n",
1021 (long long)ctx
->offset
);
1025 if (count
& 0x1ff) {
1026 printf("count %d is not sector aligned\n",
1031 for (i
= optind
; i
< argc
; i
++) {
1034 len
= cvtnum(argv
[optind
]);
1036 printf("non-numeric length argument -- %s\n", argv
[i
]);
1042 nr_iov
= argc
- optind
;
1043 qemu_iovec_init(&ctx
->qiov
, nr_iov
);
1044 ctx
->buf
= p
= qemu_io_alloc(count
, pattern
);
1045 for (i
= 0; i
< nr_iov
; i
++) {
1048 len
= cvtnum(argv
[optind
]);
1050 printf("non-numeric length argument -- %s\n",
1055 qemu_iovec_add(&ctx
->qiov
, p
, len
);
1060 gettimeofday(&ctx
->t1
, NULL
);
1061 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1062 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1069 static const cmdinfo_t aio_write_cmd
= {
1070 .name
= "aio_write",
1071 .cfunc
= aio_write_f
,
1074 .args
= "[-Cq] [-P pattern ] off len [len..]",
1075 .oneline
= "asynchronously writes a number of bytes",
1076 .help
= aio_write_help
,
1080 aio_flush_f(int argc
, char **argv
)
1086 static const cmdinfo_t aio_flush_cmd
= {
1087 .name
= "aio_flush",
1088 .cfunc
= aio_flush_f
,
1089 .oneline
= "completes all outstanding aio requets"
1093 flush_f(int argc
, char **argv
)
1099 static const cmdinfo_t flush_cmd
= {
1103 .oneline
= "flush all in-core file state to disk",
1107 truncate_f(int argc
, char **argv
)
1112 offset
= cvtnum(argv
[1]);
1114 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1118 ret
= bdrv_truncate(bs
, offset
);
1120 printf("truncate: %s", strerror(ret
));
1127 static const cmdinfo_t truncate_cmd
= {
1130 .cfunc
= truncate_f
,
1134 .oneline
= "truncates the current file at the given offset",
1138 length_f(int argc
, char **argv
)
1143 size
= bdrv_getlength(bs
);
1145 printf("getlength: %s", strerror(size
));
1149 cvtstr(size
, s1
, sizeof(s1
));
1155 static const cmdinfo_t length_cmd
= {
1159 .oneline
= "gets the length of the current file",
1164 info_f(int argc
, char **argv
)
1166 BlockDriverInfo bdi
;
1167 char s1
[64], s2
[64];
1170 if (bs
->drv
&& bs
->drv
->format_name
)
1171 printf("format name: %s\n", bs
->drv
->format_name
);
1172 if (bs
->drv
&& bs
->drv
->protocol_name
)
1173 printf("format name: %s\n", bs
->drv
->protocol_name
);
1175 ret
= bdrv_get_info(bs
, &bdi
);
1179 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1180 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1182 printf("cluster size: %s\n", s1
);
1183 printf("vm state offset: %s\n", s2
);
1190 static const cmdinfo_t info_cmd
= {
1194 .oneline
= "prints information about the current file",
1198 alloc_f(int argc
, char **argv
)
1207 offset
= cvtnum(argv
[1]);
1208 if (offset
& 0x1ff) {
1209 printf("offset %lld is not sector aligned\n",
1215 nb_sectors
= cvtnum(argv
[2]);
1219 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1221 cvtstr(offset
, s1
, sizeof(s1
));
1223 retstr
= ret
? "allocated" : "not allocated";
1224 if (nb_sectors
== 1)
1225 printf("sector %s at offset %s\n", retstr
, s1
);
1227 printf("%d/%d sectors %s at offset %s\n",
1228 num
, nb_sectors
, retstr
, s1
);
1232 static const cmdinfo_t alloc_cmd
= {
1238 .args
= "off [sectors]",
1239 .oneline
= "checks if a sector is present in the file",
1243 close_f(int argc
, char **argv
)
1250 static const cmdinfo_t close_cmd
= {
1254 .oneline
= "close the current open file",
1257 static int openfile(char *name
, int flags
)
1260 fprintf(stderr
, "file open already, try 'help close'\n");
1264 bs
= bdrv_new("hda");
1268 if (bdrv_open(bs
, name
, flags
) == -1) {
1269 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1282 " opens a new file in the requested mode\n"
1285 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1287 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1288 " -C, -- create new file if it doesn't exist\n"
1289 " -r, -- open file read-only\n"
1290 " -s, -- use snapshot file\n"
1291 " -n, -- disable host cache\n"
1295 static const cmdinfo_t open_cmd
;
1298 open_f(int argc
, char **argv
)
1304 while ((c
= getopt(argc
, argv
, "snCr")) != EOF
) {
1307 flags
|= BDRV_O_SNAPSHOT
;
1310 flags
|= BDRV_O_NOCACHE
;
1313 flags
|= BDRV_O_CREAT
;
1319 return command_usage(&open_cmd
);
1324 flags
|= BDRV_O_RDONLY
;
1326 flags
|= BDRV_O_RDWR
;
1328 if (optind
!= argc
- 1)
1329 return command_usage(&open_cmd
);
1331 return openfile(argv
[optind
], flags
);
1334 static const cmdinfo_t open_cmd
= {
1340 .flags
= CMD_NOFILE_OK
,
1341 .args
= "[-Crsn] [path]",
1342 .oneline
= "open the file specified by path",
1350 /* only one device allowed so far */
1358 const cmdinfo_t
*ct
)
1360 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1362 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1363 fprintf(stderr
, "no file open, try 'help open'\n");
1369 static void usage(const char *name
)
1372 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1373 "QEMU Disk exerciser\n"
1375 " -C, --create create new file if it doesn't exist\n"
1376 " -c, --cmd command to execute\n"
1377 " -r, --read-only export read-only\n"
1378 " -s, --snapshot use snapshot file\n"
1379 " -n, --nocache disable host cache\n"
1380 " -m, --misalign misalign allocations for O_DIRECT\n"
1381 " -h, --help display this help and exit\n"
1382 " -V, --version output version information and exit\n"
1388 int main(int argc
, char **argv
)
1391 const char *sopt
= "hVc:Crsnm";
1392 struct option lopt
[] = {
1393 { "help", 0, 0, 'h' },
1394 { "version", 0, 0, 'V' },
1395 { "offset", 1, 0, 'o' },
1396 { "cmd", 1, 0, 'c' },
1397 { "create", 0, 0, 'C' },
1398 { "read-only", 0, 0, 'r' },
1399 { "snapshot", 0, 0, 's' },
1400 { "nocache", 0, 0, 'n' },
1401 { "misalign", 0, 0, 'm' },
1408 progname
= basename(argv
[0]);
1410 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1413 flags
|= BDRV_O_SNAPSHOT
;
1416 flags
|= BDRV_O_NOCACHE
;
1419 add_user_command(optarg
);
1422 flags
|= BDRV_O_CREAT
;
1431 printf("%s version %s\n", progname
, VERSION
);
1442 if ((argc
- optind
) > 1) {
1449 /* initialize commands */
1452 add_command(&open_cmd
);
1453 add_command(&close_cmd
);
1454 add_command(&read_cmd
);
1455 add_command(&readv_cmd
);
1456 add_command(&write_cmd
);
1457 add_command(&writev_cmd
);
1458 add_command(&aio_read_cmd
);
1459 add_command(&aio_write_cmd
);
1460 add_command(&aio_flush_cmd
);
1461 add_command(&flush_cmd
);
1462 add_command(&truncate_cmd
);
1463 add_command(&length_cmd
);
1464 add_command(&info_cmd
);
1465 add_command(&alloc_cmd
);
1467 add_args_command(init_args_command
);
1468 add_check_command(init_check_command
);
1470 /* open the device */
1472 flags
|= BDRV_O_RDONLY
;
1474 flags
|= BDRV_O_RDWR
;
1476 if ((argc
- optind
) == 1)
1477 openfile(argv
[optind
], flags
);
1481 * Make sure all outstanding requests get flushed the program exits.