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
));
102 * Parse multiple length statements for vectored I/O, and construct an I/O
103 * vector matching it.
106 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
108 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
113 for (i
= 0; i
< nr_iov
; i
++) {
119 printf("non-numeric length argument -- %s\n", arg
);
123 /* should be SIZE_T_MAX, but that doesn't exist */
124 if (len
> UINT_MAX
) {
125 printf("too large length argument -- %s\n", arg
);
130 printf("length argument %lld is not sector aligned\n",
139 qemu_iovec_init(qiov
, nr_iov
);
141 buf
= p
= qemu_io_alloc(count
, pattern
);
143 for (i
= 0; i
< nr_iov
; i
++) {
144 qemu_iovec_add(qiov
, p
, sizes
[i
]);
152 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
156 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
163 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
167 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
174 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
176 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
182 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
184 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
190 #define NOT_DONE 0x7fffffff
191 static void aio_rw_done(void *opaque
, int ret
)
193 *(int *)opaque
= ret
;
196 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
198 BlockDriverAIOCB
*acb
;
199 int async_ret
= NOT_DONE
;
201 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
202 aio_rw_done
, &async_ret
);
206 while (async_ret
== NOT_DONE
)
210 return async_ret
< 0 ? async_ret
: 1;
213 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
215 BlockDriverAIOCB
*acb
;
216 int async_ret
= NOT_DONE
;
218 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
219 aio_rw_done
, &async_ret
);
223 while (async_ret
== NOT_DONE
)
227 return async_ret
< 0 ? async_ret
: 1;
231 static const cmdinfo_t read_cmd
;
238 " reads a range of bytes from the given offset\n"
241 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
243 " Reads a segment of the currently open file, optionally dumping it to the\n"
244 " standard output stream (with -v option) for subsequent inspection.\n"
245 " -C, -- report statistics in a machine parsable format\n"
246 " -l, -- length for pattern verification (only with -P)\n"
247 " -p, -- use bdrv_pread to read the file\n"
248 " -P, -- use a pattern to verify read data\n"
249 " -q, -- quite mode, do not show I/O statistics\n"
250 " -s, -- start offset for pattern verification (only with -P)\n"
251 " -v, -- dump buffer to standard output\n"
256 read_f(int argc
, char **argv
)
258 struct timeval t1
, t2
;
259 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
260 int Pflag
= 0, sflag
= 0, lflag
= 0;
265 /* Some compilers get confused and warn if this is not initialized. */
267 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
269 while ((c
= getopt(argc
, argv
, "Cl:pP:qs:v")) != EOF
) {
276 pattern_count
= cvtnum(optarg
);
277 if (pattern_count
< 0) {
278 printf("non-numeric length argument -- %s\n", optarg
);
287 pattern
= atoi(optarg
);
294 pattern_offset
= cvtnum(optarg
);
295 if (pattern_offset
< 0) {
296 printf("non-numeric length argument -- %s\n", optarg
);
304 return command_usage(&read_cmd
);
308 if (optind
!= argc
- 2)
309 return command_usage(&read_cmd
);
311 offset
= cvtnum(argv
[optind
]);
313 printf("non-numeric length argument -- %s\n", argv
[optind
]);
318 count
= cvtnum(argv
[optind
]);
320 printf("non-numeric length argument -- %s\n", argv
[optind
]);
324 if (!Pflag
&& (lflag
|| sflag
)) {
325 return command_usage(&read_cmd
);
329 pattern_count
= count
- pattern_offset
;
332 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
333 printf("pattern verfication range exceeds end of read data\n");
338 if (offset
& 0x1ff) {
339 printf("offset %lld is not sector aligned\n",
344 printf("count %d is not sector aligned\n",
350 buf
= qemu_io_alloc(count
, 0xab);
352 gettimeofday(&t1
, NULL
);
354 cnt
= do_pread(buf
, offset
, count
, &total
);
356 cnt
= do_read(buf
, offset
, count
, &total
);
357 gettimeofday(&t2
, NULL
);
360 printf("read failed: %s\n", strerror(-cnt
));
365 void* cmp_buf
= malloc(pattern_count
);
366 memset(cmp_buf
, pattern
, pattern_count
);
367 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
368 printf("Pattern verification failed at offset %lld, "
370 (long long) offset
+ pattern_offset
, pattern_count
);
379 dump_buffer(buf
, offset
, count
);
381 /* Finally, report back -- -C gives a parsable format */
383 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
391 static const cmdinfo_t read_cmd
= {
397 .args
= "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
398 .oneline
= "reads a number of bytes at a specified offset",
402 static const cmdinfo_t readv_cmd
;
409 " reads a range of bytes from the given offset into multiple buffers\n"
412 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
414 " Reads a segment of the currently open file, optionally dumping it to the\n"
415 " standard output stream (with -v option) for subsequent inspection.\n"
416 " Uses multiple iovec buffers if more than one byte range is specified.\n"
417 " -C, -- report statistics in a machine parsable format\n"
418 " -P, -- use a pattern to verify read data\n"
419 " -v, -- dump buffer to standard output\n"
420 " -q, -- quite mode, do not show I/O statistics\n"
425 readv_f(int argc
, char **argv
)
427 struct timeval t1
, t2
;
428 int Cflag
= 0, qflag
= 0, vflag
= 0;
438 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
445 pattern
= atoi(optarg
);
454 return command_usage(&readv_cmd
);
458 if (optind
> argc
- 2)
459 return command_usage(&readv_cmd
);
462 offset
= cvtnum(argv
[optind
]);
464 printf("non-numeric length argument -- %s\n", argv
[optind
]);
469 if (offset
& 0x1ff) {
470 printf("offset %lld is not sector aligned\n",
475 nr_iov
= argc
- optind
;
476 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
478 gettimeofday(&t1
, NULL
);
479 cnt
= do_aio_readv(&qiov
, offset
, &total
);
480 gettimeofday(&t2
, NULL
);
483 printf("readv failed: %s\n", strerror(-cnt
));
488 void* cmp_buf
= malloc(qiov
.size
);
489 memset(cmp_buf
, pattern
, qiov
.size
);
490 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
491 printf("Pattern verification failed at offset %lld, "
493 (long long) offset
, qiov
.size
);
502 dump_buffer(buf
, offset
, qiov
.size
);
504 /* Finally, report back -- -C gives a parsable format */
506 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
513 static const cmdinfo_t readv_cmd
= {
518 .args
= "[-Cqv] [-P pattern ] off len [len..]",
519 .oneline
= "reads a number of bytes at a specified offset",
523 static const cmdinfo_t write_cmd
;
530 " writes a range of bytes from the given offset\n"
533 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
535 " Writes into a segment of the currently open file, using a buffer\n"
536 " filled with a set pattern (0xcdcdcdcd).\n"
537 " -p, -- use bdrv_pwrite to write the file\n"
538 " -P, -- use different pattern to fill file\n"
539 " -C, -- report statistics in a machine parsable format\n"
540 " -q, -- quite mode, do not show I/O statistics\n"
545 write_f(int argc
, char **argv
)
547 struct timeval t1
, t2
;
548 int Cflag
= 0, pflag
= 0, qflag
= 0;
553 /* Some compilers get confused and warn if this is not initialized. */
557 while ((c
= getopt(argc
, argv
, "CpP:q")) != EOF
) {
566 pattern
= atoi(optarg
);
572 return command_usage(&write_cmd
);
576 if (optind
!= argc
- 2)
577 return command_usage(&write_cmd
);
579 offset
= cvtnum(argv
[optind
]);
581 printf("non-numeric length argument -- %s\n", argv
[optind
]);
586 count
= cvtnum(argv
[optind
]);
588 printf("non-numeric length argument -- %s\n", argv
[optind
]);
593 if (offset
& 0x1ff) {
594 printf("offset %lld is not sector aligned\n",
600 printf("count %d is not sector aligned\n",
606 buf
= qemu_io_alloc(count
, pattern
);
608 gettimeofday(&t1
, NULL
);
610 cnt
= do_pwrite(buf
, offset
, count
, &total
);
612 cnt
= do_write(buf
, offset
, count
, &total
);
613 gettimeofday(&t2
, NULL
);
616 printf("write failed: %s\n", strerror(-cnt
));
623 /* Finally, report back -- -C gives a parsable format */
625 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
633 static const cmdinfo_t write_cmd
= {
639 .args
= "[-aCpq] [-P pattern ] off len",
640 .oneline
= "writes a number of bytes at a specified offset",
644 static const cmdinfo_t writev_cmd
;
651 " writes a range of bytes from the given offset source from multiple buffers\n"
654 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
656 " Writes into a segment of the currently open file, using a buffer\n"
657 " filled with a set pattern (0xcdcdcdcd).\n"
658 " -P, -- use different pattern to fill file\n"
659 " -C, -- report statistics in a machine parsable format\n"
660 " -q, -- quite mode, do not show I/O statistics\n"
665 writev_f(int argc
, char **argv
)
667 struct timeval t1
, t2
;
668 int Cflag
= 0, qflag
= 0;
677 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
686 pattern
= atoi(optarg
);
689 return command_usage(&writev_cmd
);
693 if (optind
> argc
- 2)
694 return command_usage(&writev_cmd
);
696 offset
= cvtnum(argv
[optind
]);
698 printf("non-numeric length argument -- %s\n", argv
[optind
]);
703 if (offset
& 0x1ff) {
704 printf("offset %lld is not sector aligned\n",
709 nr_iov
= argc
- optind
;
710 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
712 gettimeofday(&t1
, NULL
);
713 cnt
= do_aio_writev(&qiov
, offset
, &total
);
714 gettimeofday(&t2
, NULL
);
717 printf("writev failed: %s\n", strerror(-cnt
));
724 /* Finally, report back -- -C gives a parsable format */
726 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
732 static const cmdinfo_t writev_cmd
= {
737 .args
= "[-Cq] [-P pattern ] off len [len..]",
738 .oneline
= "writes a number of bytes at a specified offset",
755 aio_write_done(void *opaque
, int ret
)
757 struct aio_ctx
*ctx
= opaque
;
760 gettimeofday(&t2
, NULL
);
764 printf("aio_write failed: %s\n", strerror(-ret
));
772 /* Finally, report back -- -C gives a parsable format */
773 t2
= tsub(t2
, ctx
->t1
);
774 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
775 ctx
->qiov
.size
, 1, ctx
->Cflag
);
777 qemu_io_free(ctx
->buf
);
781 static const cmdinfo_t aio_read_cmd
;
784 aio_read_done(void *opaque
, int ret
)
786 struct aio_ctx
*ctx
= opaque
;
789 gettimeofday(&t2
, NULL
);
792 printf("readv failed: %s\n", strerror(-ret
));
797 void *cmp_buf
= malloc(ctx
->qiov
.size
);
799 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
800 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
801 printf("Pattern verification failed at offset %lld, "
803 (long long) ctx
->offset
, ctx
->qiov
.size
);
813 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
816 /* Finally, report back -- -C gives a parsable format */
817 t2
= tsub(t2
, ctx
->t1
);
818 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
819 ctx
->qiov
.size
, 1, ctx
->Cflag
);
821 qemu_io_free(ctx
->buf
);
830 " asynchronously reads a range of bytes from the given offset\n"
833 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
835 " Reads a segment of the currently open file, optionally dumping it to the\n"
836 " standard output stream (with -v option) for subsequent inspection.\n"
837 " The read is performed asynchronously and should the aio_flush command \n"
838 " should be used to ensure all outstanding aio requests have been completed\n"
839 " -C, -- report statistics in a machine parsable format\n"
840 " -P, -- use a pattern to verify read data\n"
841 " -v, -- dump buffer to standard output\n"
842 " -q, -- quite mode, do not show I/O statistics\n"
847 aio_read_f(int argc
, char **argv
)
850 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
851 BlockDriverAIOCB
*acb
;
853 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
860 ctx
->pattern
= atoi(optarg
);
870 return command_usage(&aio_read_cmd
);
874 if (optind
> argc
- 2) {
876 return command_usage(&aio_read_cmd
);
879 ctx
->offset
= cvtnum(argv
[optind
]);
880 if (ctx
->offset
< 0) {
881 printf("non-numeric length argument -- %s\n", argv
[optind
]);
887 if (ctx
->offset
& 0x1ff) {
888 printf("offset %lld is not sector aligned\n",
889 (long long)ctx
->offset
);
894 nr_iov
= argc
- optind
;
895 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
897 gettimeofday(&ctx
->t1
, NULL
);
898 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
899 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
909 static const cmdinfo_t aio_read_cmd
= {
914 .args
= "[-Cqv] [-P pattern ] off len [len..]",
915 .oneline
= "asynchronously reads a number of bytes",
916 .help
= aio_read_help
,
919 static const cmdinfo_t aio_write_cmd
;
926 " asynchronously writes a range of bytes from the given offset source \n"
927 " from multiple buffers\n"
930 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
932 " Writes into a segment of the currently open file, using a buffer\n"
933 " filled with a set pattern (0xcdcdcdcd).\n"
934 " The write is performed asynchronously and should the aio_flush command \n"
935 " should be used to ensure all outstanding aio requests have been completed\n"
936 " -P, -- use different pattern to fill file\n"
937 " -C, -- report statistics in a machine parsable format\n"
938 " -q, -- quite mode, do not show I/O statistics\n"
944 aio_write_f(int argc
, char **argv
)
948 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
949 BlockDriverAIOCB
*acb
;
951 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
960 pattern
= atoi(optarg
);
964 return command_usage(&aio_write_cmd
);
968 if (optind
> argc
- 2) {
970 return command_usage(&aio_write_cmd
);
973 ctx
->offset
= cvtnum(argv
[optind
]);
974 if (ctx
->offset
< 0) {
975 printf("non-numeric length argument -- %s\n", argv
[optind
]);
981 if (ctx
->offset
& 0x1ff) {
982 printf("offset %lld is not sector aligned\n",
983 (long long)ctx
->offset
);
988 nr_iov
= argc
- optind
;
989 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
991 gettimeofday(&ctx
->t1
, NULL
);
992 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
993 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1003 static const cmdinfo_t aio_write_cmd
= {
1004 .name
= "aio_write",
1005 .cfunc
= aio_write_f
,
1008 .args
= "[-Cq] [-P pattern ] off len [len..]",
1009 .oneline
= "asynchronously writes a number of bytes",
1010 .help
= aio_write_help
,
1014 aio_flush_f(int argc
, char **argv
)
1020 static const cmdinfo_t aio_flush_cmd
= {
1021 .name
= "aio_flush",
1022 .cfunc
= aio_flush_f
,
1023 .oneline
= "completes all outstanding aio requets"
1027 flush_f(int argc
, char **argv
)
1033 static const cmdinfo_t flush_cmd
= {
1037 .oneline
= "flush all in-core file state to disk",
1041 truncate_f(int argc
, char **argv
)
1046 offset
= cvtnum(argv
[1]);
1048 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1052 ret
= bdrv_truncate(bs
, offset
);
1054 printf("truncate: %s", strerror(ret
));
1061 static const cmdinfo_t truncate_cmd
= {
1064 .cfunc
= truncate_f
,
1068 .oneline
= "truncates the current file at the given offset",
1072 length_f(int argc
, char **argv
)
1077 size
= bdrv_getlength(bs
);
1079 printf("getlength: %s", strerror(size
));
1083 cvtstr(size
, s1
, sizeof(s1
));
1089 static const cmdinfo_t length_cmd
= {
1093 .oneline
= "gets the length of the current file",
1098 info_f(int argc
, char **argv
)
1100 BlockDriverInfo bdi
;
1101 char s1
[64], s2
[64];
1104 if (bs
->drv
&& bs
->drv
->format_name
)
1105 printf("format name: %s\n", bs
->drv
->format_name
);
1106 if (bs
->drv
&& bs
->drv
->protocol_name
)
1107 printf("format name: %s\n", bs
->drv
->protocol_name
);
1109 ret
= bdrv_get_info(bs
, &bdi
);
1113 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1114 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1116 printf("cluster size: %s\n", s1
);
1117 printf("vm state offset: %s\n", s2
);
1124 static const cmdinfo_t info_cmd
= {
1128 .oneline
= "prints information about the current file",
1132 alloc_f(int argc
, char **argv
)
1141 offset
= cvtnum(argv
[1]);
1142 if (offset
& 0x1ff) {
1143 printf("offset %lld is not sector aligned\n",
1149 nb_sectors
= cvtnum(argv
[2]);
1153 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1155 cvtstr(offset
, s1
, sizeof(s1
));
1157 retstr
= ret
? "allocated" : "not allocated";
1158 if (nb_sectors
== 1)
1159 printf("sector %s at offset %s\n", retstr
, s1
);
1161 printf("%d/%d sectors %s at offset %s\n",
1162 num
, nb_sectors
, retstr
, s1
);
1166 static const cmdinfo_t alloc_cmd
= {
1172 .args
= "off [sectors]",
1173 .oneline
= "checks if a sector is present in the file",
1177 close_f(int argc
, char **argv
)
1184 static const cmdinfo_t close_cmd
= {
1188 .oneline
= "close the current open file",
1191 static int openfile(char *name
, int flags
, int growable
)
1194 fprintf(stderr
, "file open already, try 'help close'\n");
1198 bs
= bdrv_new("hda");
1202 if (bdrv_open(bs
, name
, flags
) == -1) {
1203 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1210 if (!bs
->drv
|| !bs
->drv
->protocol_name
) {
1212 "%s: only protocols can be opened growable\n",
1227 " opens a new file in the requested mode\n"
1230 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1232 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1233 " -C, -- create new file if it doesn't exist\n"
1234 " -r, -- open file read-only\n"
1235 " -s, -- use snapshot file\n"
1236 " -n, -- disable host cache\n"
1237 " -g, -- allow file to grow (only applies to protocols)"
1241 static const cmdinfo_t open_cmd
;
1244 open_f(int argc
, char **argv
)
1251 while ((c
= getopt(argc
, argv
, "snCrg")) != EOF
) {
1254 flags
|= BDRV_O_SNAPSHOT
;
1257 flags
|= BDRV_O_NOCACHE
;
1260 flags
|= BDRV_O_CREAT
;
1269 return command_usage(&open_cmd
);
1274 flags
|= BDRV_O_RDONLY
;
1276 flags
|= BDRV_O_RDWR
;
1278 if (optind
!= argc
- 1)
1279 return command_usage(&open_cmd
);
1281 return openfile(argv
[optind
], flags
, growable
);
1284 static const cmdinfo_t open_cmd
= {
1290 .flags
= CMD_NOFILE_OK
,
1291 .args
= "[-Crsn] [path]",
1292 .oneline
= "open the file specified by path",
1300 /* only one device allowed so far */
1308 const cmdinfo_t
*ct
)
1310 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1312 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1313 fprintf(stderr
, "no file open, try 'help open'\n");
1319 static void usage(const char *name
)
1322 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1323 "QEMU Disk exerciser\n"
1325 " -C, --create create new file if it doesn't exist\n"
1326 " -c, --cmd command to execute\n"
1327 " -r, --read-only export read-only\n"
1328 " -s, --snapshot use snapshot file\n"
1329 " -n, --nocache disable host cache\n"
1330 " -m, --misalign misalign allocations for O_DIRECT\n"
1331 " -h, --help display this help and exit\n"
1332 " -V, --version output version information and exit\n"
1338 int main(int argc
, char **argv
)
1342 const char *sopt
= "hVc:Crsnmg";
1343 struct option lopt
[] = {
1344 { "help", 0, 0, 'h' },
1345 { "version", 0, 0, 'V' },
1346 { "offset", 1, 0, 'o' },
1347 { "cmd", 1, 0, 'c' },
1348 { "create", 0, 0, 'C' },
1349 { "read-only", 0, 0, 'r' },
1350 { "snapshot", 0, 0, 's' },
1351 { "nocache", 0, 0, 'n' },
1352 { "misalign", 0, 0, 'm' },
1353 { "growable", 0, 0, 'g' },
1360 progname
= basename(argv
[0]);
1362 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1365 flags
|= BDRV_O_SNAPSHOT
;
1368 flags
|= BDRV_O_NOCACHE
;
1371 add_user_command(optarg
);
1374 flags
|= BDRV_O_CREAT
;
1386 printf("%s version %s\n", progname
, VERSION
);
1397 if ((argc
- optind
) > 1) {
1404 /* initialize commands */
1407 add_command(&open_cmd
);
1408 add_command(&close_cmd
);
1409 add_command(&read_cmd
);
1410 add_command(&readv_cmd
);
1411 add_command(&write_cmd
);
1412 add_command(&writev_cmd
);
1413 add_command(&aio_read_cmd
);
1414 add_command(&aio_write_cmd
);
1415 add_command(&aio_flush_cmd
);
1416 add_command(&flush_cmd
);
1417 add_command(&truncate_cmd
);
1418 add_command(&length_cmd
);
1419 add_command(&info_cmd
);
1420 add_command(&alloc_cmd
);
1422 add_args_command(init_args_command
);
1423 add_check_command(init_check_command
);
1425 /* open the device */
1427 flags
|= BDRV_O_RDONLY
;
1429 flags
|= BDRV_O_RDWR
;
1431 if ((argc
- optind
) == 1)
1432 openfile(argv
[optind
], flags
, growable
);
1436 * Make sure all outstanding requests get flushed the program exits.