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
;
775 gettimeofday(&t2
, NULL
);
777 total
= ctx
->qiov
.size
;
780 printf("aio_write failed: %s\n", strerror(-ret
));
787 /* Finally, report back -- -C gives a parsable format */
788 t2
= tsub(t2
, ctx
->t1
);
789 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
, total
, cnt
,
792 qemu_io_free(ctx
->buf
);
796 static const cmdinfo_t aio_read_cmd
;
799 aio_read_done(void *opaque
, int ret
)
801 struct aio_ctx
*ctx
= opaque
;
806 gettimeofday(&t2
, NULL
);
808 total
= ctx
->qiov
.size
;
811 printf("readv failed: %s\n", strerror(-ret
));
816 void *cmp_buf
= malloc(total
);
818 memset(cmp_buf
, ctx
->pattern
, total
);
819 if (memcmp(ctx
->buf
, cmp_buf
, total
)) {
820 printf("Pattern verification failed at offset %lld, "
822 (long long) ctx
->offset
, total
);
831 dump_buffer(ctx
->buf
, ctx
->offset
, total
);
833 /* Finally, report back -- -C gives a parsable format */
834 t2
= tsub(t2
, ctx
->t1
);
835 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
, total
, cnt
,
838 qemu_io_free(ctx
->buf
);
848 " asynchronously reads a range of bytes from the given offset\n"
851 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
853 " Reads a segment of the currently open file, optionally dumping it to the\n"
854 " standard output stream (with -v option) for subsequent inspection.\n"
855 " The read is performed asynchronously and should the aio_flush command \n"
856 " should be used to ensure all outstanding aio requests have been completed\n"
857 " -C, -- report statistics in a machine parsable format\n"
858 " -P, -- use a pattern to verify read data\n"
859 " -v, -- dump buffer to standard output\n"
860 " -q, -- quite mode, do not show I/O statistics\n"
865 aio_read_f(int argc
, char **argv
)
870 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
871 BlockDriverAIOCB
*acb
;
875 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
882 ctx
->pattern
= atoi(optarg
);
891 return command_usage(&aio_read_cmd
);
895 if (optind
> argc
- 2)
896 return command_usage(&aio_read_cmd
);
899 ctx
->offset
= cvtnum(argv
[optind
]);
900 if (ctx
->offset
< 0) {
901 printf("non-numeric length argument -- %s\n", argv
[optind
]);
906 if (ctx
->offset
& 0x1ff) {
907 printf("offset %lld is not sector aligned\n",
908 (long long)ctx
->offset
);
913 printf("count %d is not sector aligned\n",
918 for (i
= optind
; i
< argc
; i
++) {
921 len
= cvtnum(argv
[i
]);
923 printf("non-numeric length argument -- %s\n", argv
[i
]);
929 nr_iov
= argc
- optind
;
930 qemu_iovec_init(&ctx
->qiov
, nr_iov
);
931 ctx
->buf
= p
= qemu_io_alloc(count
, 0xab);
932 for (i
= 0; i
< nr_iov
; i
++) {
935 len
= cvtnum(argv
[optind
]);
937 printf("non-numeric length argument -- %s\n",
942 qemu_iovec_add(&ctx
->qiov
, p
, len
);
947 gettimeofday(&ctx
->t1
, NULL
);
948 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
949 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
956 static const cmdinfo_t aio_read_cmd
= {
961 .args
= "[-Cqv] [-P pattern ] off len [len..]",
962 .oneline
= "asynchronously reads a number of bytes",
963 .help
= aio_read_help
,
966 static const cmdinfo_t aio_write_cmd
;
973 " asynchronously writes a range of bytes from the given offset source \n"
974 " from multiple buffers\n"
977 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
979 " Writes into a segment of the currently open file, using a buffer\n"
980 " filled with a set pattern (0xcdcdcdcd).\n"
981 " The write is performed asynchronously and should the aio_flush command \n"
982 " should be used to ensure all outstanding aio requests have been completed\n"
983 " -P, -- use different pattern to fill file\n"
984 " -C, -- report statistics in a machine parsable format\n"
985 " -q, -- quite mode, do not show I/O statistics\n"
991 aio_write_f(int argc
, char **argv
)
997 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
998 BlockDriverAIOCB
*acb
;
1000 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1009 pattern
= atoi(optarg
);
1012 return command_usage(&aio_write_cmd
);
1016 if (optind
> argc
- 2)
1017 return command_usage(&aio_write_cmd
);
1019 ctx
->offset
= cvtnum(argv
[optind
]);
1020 if (ctx
->offset
< 0) {
1021 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1026 if (ctx
->offset
& 0x1ff) {
1027 printf("offset %lld is not sector aligned\n",
1028 (long long)ctx
->offset
);
1032 if (count
& 0x1ff) {
1033 printf("count %d is not sector aligned\n",
1039 for (i
= optind
; i
< argc
; i
++) {
1042 len
= cvtnum(argv
[optind
]);
1044 printf("non-numeric length argument -- %s\n", argv
[i
]);
1050 nr_iov
= argc
- optind
;
1051 qemu_iovec_init(&ctx
->qiov
, nr_iov
);
1052 ctx
->buf
= p
= qemu_io_alloc(count
, pattern
);
1053 for (i
= 0; i
< nr_iov
; i
++) {
1056 len
= cvtnum(argv
[optind
]);
1058 printf("non-numeric length argument -- %s\n",
1063 qemu_iovec_add(&ctx
->qiov
, p
, len
);
1068 gettimeofday(&ctx
->t1
, NULL
);
1069 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1070 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1077 static const cmdinfo_t aio_write_cmd
= {
1078 .name
= "aio_write",
1079 .cfunc
= aio_write_f
,
1082 .args
= "[-Cq] [-P pattern ] off len [len..]",
1083 .oneline
= "asynchronously writes a number of bytes",
1084 .help
= aio_write_help
,
1088 aio_flush_f(int argc
, char **argv
)
1094 static const cmdinfo_t aio_flush_cmd
= {
1095 .name
= "aio_flush",
1096 .cfunc
= aio_flush_f
,
1097 .oneline
= "completes all outstanding aio requets"
1101 flush_f(int argc
, char **argv
)
1107 static const cmdinfo_t flush_cmd
= {
1111 .oneline
= "flush all in-core file state to disk",
1115 truncate_f(int argc
, char **argv
)
1120 offset
= cvtnum(argv
[1]);
1122 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1126 ret
= bdrv_truncate(bs
, offset
);
1128 printf("truncate: %s", strerror(ret
));
1135 static const cmdinfo_t truncate_cmd
= {
1138 .cfunc
= truncate_f
,
1142 .oneline
= "truncates the current file at the given offset",
1146 length_f(int argc
, char **argv
)
1151 size
= bdrv_getlength(bs
);
1153 printf("getlength: %s", strerror(size
));
1157 cvtstr(size
, s1
, sizeof(s1
));
1163 static const cmdinfo_t length_cmd
= {
1167 .oneline
= "gets the length of the current file",
1172 info_f(int argc
, char **argv
)
1174 BlockDriverInfo bdi
;
1175 char s1
[64], s2
[64];
1178 if (bs
->drv
&& bs
->drv
->format_name
)
1179 printf("format name: %s\n", bs
->drv
->format_name
);
1180 if (bs
->drv
&& bs
->drv
->protocol_name
)
1181 printf("format name: %s\n", bs
->drv
->protocol_name
);
1183 ret
= bdrv_get_info(bs
, &bdi
);
1187 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1188 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1190 printf("cluster size: %s\n", s1
);
1191 printf("vm state offset: %s\n", s2
);
1198 static const cmdinfo_t info_cmd
= {
1202 .oneline
= "prints information about the current file",
1206 alloc_f(int argc
, char **argv
)
1215 offset
= cvtnum(argv
[1]);
1216 if (offset
& 0x1ff) {
1217 printf("offset %lld is not sector aligned\n",
1223 nb_sectors
= cvtnum(argv
[2]);
1227 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1229 cvtstr(offset
, s1
, sizeof(s1
));
1231 retstr
= ret
? "allocated" : "not allocated";
1232 if (nb_sectors
== 1)
1233 printf("sector %s at offset %s\n", retstr
, s1
);
1235 printf("%d/%d sectors %s at offset %s\n",
1236 num
, nb_sectors
, retstr
, s1
);
1240 static const cmdinfo_t alloc_cmd
= {
1246 .args
= "off [sectors]",
1247 .oneline
= "checks if a sector is present in the file",
1251 close_f(int argc
, char **argv
)
1258 static const cmdinfo_t close_cmd
= {
1262 .oneline
= "close the current open file",
1265 static int openfile(char *name
, int flags
)
1268 fprintf(stderr
, "file open already, try 'help close'\n");
1272 bs
= bdrv_new("hda");
1276 if (bdrv_open(bs
, name
, flags
) == -1) {
1277 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1290 " opens a new file in the requested mode\n"
1293 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1295 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1296 " -C, -- create new file if it doesn't exist\n"
1297 " -r, -- open file read-only\n"
1298 " -s, -- use snapshot file\n"
1299 " -n, -- disable host cache\n"
1303 static const cmdinfo_t open_cmd
;
1306 open_f(int argc
, char **argv
)
1312 while ((c
= getopt(argc
, argv
, "snCr")) != EOF
) {
1315 flags
|= BDRV_O_SNAPSHOT
;
1318 flags
|= BDRV_O_NOCACHE
;
1321 flags
|= BDRV_O_CREAT
;
1327 return command_usage(&open_cmd
);
1332 flags
|= BDRV_O_RDONLY
;
1334 flags
|= BDRV_O_RDWR
;
1336 if (optind
!= argc
- 1)
1337 return command_usage(&open_cmd
);
1339 return openfile(argv
[optind
], flags
);
1342 static const cmdinfo_t open_cmd
= {
1348 .flags
= CMD_NOFILE_OK
,
1349 .args
= "[-Crsn] [path]",
1350 .oneline
= "open the file specified by path",
1358 /* only one device allowed so far */
1366 const cmdinfo_t
*ct
)
1368 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1370 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1371 fprintf(stderr
, "no file open, try 'help open'\n");
1377 static void usage(const char *name
)
1380 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1381 "QEMU Disk exerciser\n"
1383 " -C, --create create new file if it doesn't exist\n"
1384 " -c, --cmd command to execute\n"
1385 " -r, --read-only export read-only\n"
1386 " -s, --snapshot use snapshot file\n"
1387 " -n, --nocache disable host cache\n"
1388 " -m, --misalign misalign allocations for O_DIRECT\n"
1389 " -h, --help display this help and exit\n"
1390 " -V, --version output version information and exit\n"
1396 int main(int argc
, char **argv
)
1399 const char *sopt
= "hVc:Crsnm";
1400 struct option lopt
[] = {
1401 { "help", 0, 0, 'h' },
1402 { "version", 0, 0, 'V' },
1403 { "offset", 1, 0, 'o' },
1404 { "cmd", 1, 0, 'c' },
1405 { "create", 0, 0, 'C' },
1406 { "read-only", 0, 0, 'r' },
1407 { "snapshot", 0, 0, 's' },
1408 { "nocache", 0, 0, 'n' },
1409 { "misalign", 0, 0, 'm' },
1416 progname
= basename(argv
[0]);
1418 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1421 flags
|= BDRV_O_SNAPSHOT
;
1424 flags
|= BDRV_O_NOCACHE
;
1427 add_user_command(optarg
);
1430 flags
|= BDRV_O_CREAT
;
1439 printf("%s version %s\n", progname
, VERSION
);
1450 if ((argc
- optind
) > 1) {
1457 /* initialize commands */
1460 add_command(&open_cmd
);
1461 add_command(&close_cmd
);
1462 add_command(&read_cmd
);
1463 add_command(&readv_cmd
);
1464 add_command(&write_cmd
);
1465 add_command(&writev_cmd
);
1466 add_command(&aio_read_cmd
);
1467 add_command(&aio_write_cmd
);
1468 add_command(&aio_flush_cmd
);
1469 add_command(&flush_cmd
);
1470 add_command(&truncate_cmd
);
1471 add_command(&length_cmd
);
1472 add_command(&info_cmd
);
1473 add_command(&alloc_cmd
);
1475 add_args_command(init_args_command
);
1476 add_check_command(init_check_command
);
1478 /* open the device */
1480 flags
|= BDRV_O_RDONLY
;
1482 flags
|= BDRV_O_RDWR
;
1484 if ((argc
- optind
) == 1)
1485 openfile(argv
[optind
], flags
);
1489 * Make sure all outstanding requests get flushed the program exits.