blob: 5882067443a5a8c7cc2531255e0c277dbb05675e [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
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.
9 */
Stefan Weilc32d7662009-08-31 22:16:16 +020010#include <sys/time.h>
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020015#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000016
17#include "qemu-common.h"
Paolo Bonzinia5a52382012-04-12 14:00:51 +020018#include "main-loop.h"
aliguorie3aff4f2009-04-05 19:14:04 +000019#include "block_int.h"
20#include "cmd.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000021#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000022
23#define VERSION "0.0.1"
24
Devin Nakamura43642b32011-07-11 11:22:16 -040025#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000026
27char *progname;
28static BlockDriverState *bs;
29
30static int misalign;
31
32/*
Christoph Hellwigcf070d72009-07-20 01:19:25 +020033 * Parse the pattern argument to various sub-commands.
34 *
35 * Because the pattern is used as an argument to memset it must evaluate
36 * to an unsigned integer that fits into a single byte.
37 */
38static int parse_pattern(const char *arg)
39{
Devin Nakamura43642b32011-07-11 11:22:16 -040040 char *endptr = NULL;
41 long pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020042
Devin Nakamura43642b32011-07-11 11:22:16 -040043 pattern = strtol(arg, &endptr, 0);
44 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
45 printf("%s is not a valid pattern byte\n", arg);
46 return -1;
47 }
Christoph Hellwigcf070d72009-07-20 01:19:25 +020048
Devin Nakamura43642b32011-07-11 11:22:16 -040049 return pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020050}
51
52/*
aliguorie3aff4f2009-04-05 19:14:04 +000053 * Memory allocation helpers.
54 *
55 * Make sure memory is aligned by default, or purposefully misaligned if
56 * that is specified on the command line.
57 */
58
Devin Nakamura43642b32011-07-11 11:22:16 -040059#define MISALIGN_OFFSET 16
aliguorie3aff4f2009-04-05 19:14:04 +000060static void *qemu_io_alloc(size_t len, int pattern)
61{
Devin Nakamura43642b32011-07-11 11:22:16 -040062 void *buf;
aliguorie3aff4f2009-04-05 19:14:04 +000063
Devin Nakamura43642b32011-07-11 11:22:16 -040064 if (misalign) {
65 len += MISALIGN_OFFSET;
66 }
67 buf = qemu_blockalign(bs, len);
68 memset(buf, pattern, len);
69 if (misalign) {
70 buf += MISALIGN_OFFSET;
71 }
72 return buf;
aliguorie3aff4f2009-04-05 19:14:04 +000073}
74
75static void qemu_io_free(void *p)
76{
Devin Nakamura43642b32011-07-11 11:22:16 -040077 if (misalign) {
78 p -= MISALIGN_OFFSET;
79 }
80 qemu_vfree(p);
aliguorie3aff4f2009-04-05 19:14:04 +000081}
82
Devin Nakamura43642b32011-07-11 11:22:16 -040083static void dump_buffer(const void *buffer, int64_t offset, int len)
aliguorie3aff4f2009-04-05 19:14:04 +000084{
Devin Nakamura43642b32011-07-11 11:22:16 -040085 int i, j;
86 const uint8_t *p;
aliguorie3aff4f2009-04-05 19:14:04 +000087
Devin Nakamura43642b32011-07-11 11:22:16 -040088 for (i = 0, p = buffer; i < len; i += 16) {
89 const uint8_t *s = p;
aliguorie3aff4f2009-04-05 19:14:04 +000090
Devin Nakamura43642b32011-07-11 11:22:16 -040091 printf("%08" PRIx64 ": ", offset + i);
92 for (j = 0; j < 16 && i + j < len; j++, p++) {
93 printf("%02x ", *p);
94 }
95 printf(" ");
96 for (j = 0; j < 16 && i + j < len; j++, s++) {
97 if (isalnum(*s)) {
98 printf("%c", *s);
99 } else {
100 printf(".");
101 }
102 }
103 printf("\n");
104 }
aliguorie3aff4f2009-04-05 19:14:04 +0000105}
106
Devin Nakamura43642b32011-07-11 11:22:16 -0400107static void print_report(const char *op, struct timeval *t, int64_t offset,
108 int count, int total, int cnt, int Cflag)
aliguorie3aff4f2009-04-05 19:14:04 +0000109{
Devin Nakamura43642b32011-07-11 11:22:16 -0400110 char s1[64], s2[64], ts[64];
aliguorie3aff4f2009-04-05 19:14:04 +0000111
Devin Nakamura43642b32011-07-11 11:22:16 -0400112 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
113 if (!Cflag) {
114 cvtstr((double)total, s1, sizeof(s1));
115 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
116 printf("%s %d/%d bytes at offset %" PRId64 "\n",
117 op, total, count, offset);
118 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
119 s1, cnt, ts, s2, tdiv((double)cnt, *t));
120 } else {/* bytes,ops,time,bytes/sec,ops/sec */
121 printf("%d,%d,%s,%.3f,%.3f\n",
122 total, cnt, ts,
123 tdiv((double)total, *t),
124 tdiv((double)cnt, *t));
125 }
aliguorie3aff4f2009-04-05 19:14:04 +0000126}
127
Christoph Hellwigcf572982009-07-10 13:33:42 +0200128/*
129 * Parse multiple length statements for vectored I/O, and construct an I/O
130 * vector matching it.
131 */
132static void *
133create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
134{
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000135 size_t *sizes = g_new0(size_t, nr_iov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400136 size_t count = 0;
137 void *buf = NULL;
138 void *p;
139 int i;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200140
Devin Nakamura43642b32011-07-11 11:22:16 -0400141 for (i = 0; i < nr_iov; i++) {
142 char *arg = argv[i];
143 int64_t len;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200144
Devin Nakamura43642b32011-07-11 11:22:16 -0400145 len = cvtnum(arg);
146 if (len < 0) {
147 printf("non-numeric length argument -- %s\n", arg);
148 goto fail;
149 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200150
Devin Nakamura43642b32011-07-11 11:22:16 -0400151 /* should be SIZE_T_MAX, but that doesn't exist */
152 if (len > INT_MAX) {
153 printf("too large length argument -- %s\n", arg);
154 goto fail;
155 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200156
Devin Nakamura43642b32011-07-11 11:22:16 -0400157 if (len & 0x1ff) {
158 printf("length argument %" PRId64
159 " is not sector aligned\n", len);
160 goto fail;
161 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200162
Devin Nakamura43642b32011-07-11 11:22:16 -0400163 sizes[i] = len;
164 count += len;
165 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200166
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 qemu_iovec_init(qiov, nr_iov);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200168
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 buf = p = qemu_io_alloc(count, pattern);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200170
Devin Nakamura43642b32011-07-11 11:22:16 -0400171 for (i = 0; i < nr_iov; i++) {
172 qemu_iovec_add(qiov, p, sizes[i]);
173 p += sizes[i];
174 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200175
Kevin Wolf40a0d7c2009-11-18 10:42:59 +0100176fail:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000177 g_free(sizes);
Devin Nakamura43642b32011-07-11 11:22:16 -0400178 return buf;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200179}
180
aliguorie3aff4f2009-04-05 19:14:04 +0000181static int do_read(char *buf, int64_t offset, int count, int *total)
182{
Devin Nakamura43642b32011-07-11 11:22:16 -0400183 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000184
Devin Nakamura43642b32011-07-11 11:22:16 -0400185 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
186 if (ret < 0) {
187 return ret;
188 }
189 *total = count;
190 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000191}
192
193static int do_write(char *buf, int64_t offset, int count, int *total)
194{
Devin Nakamura43642b32011-07-11 11:22:16 -0400195 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000196
Devin Nakamura43642b32011-07-11 11:22:16 -0400197 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
198 if (ret < 0) {
199 return ret;
200 }
201 *total = count;
202 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000203}
204
205static int do_pread(char *buf, int64_t offset, int count, int *total)
206{
Devin Nakamura43642b32011-07-11 11:22:16 -0400207 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
208 if (*total < 0) {
209 return *total;
210 }
211 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000212}
213
214static int do_pwrite(char *buf, int64_t offset, int count, int *total)
215{
Devin Nakamura43642b32011-07-11 11:22:16 -0400216 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
217 if (*total < 0) {
218 return *total;
219 }
220 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000221}
222
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000223typedef struct {
224 int64_t offset;
225 int count;
226 int *total;
227 int ret;
228 bool done;
229} CoWriteZeroes;
230
231static void coroutine_fn co_write_zeroes_entry(void *opaque)
232{
233 CoWriteZeroes *data = opaque;
234
235 data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE,
236 data->count / BDRV_SECTOR_SIZE);
237 data->done = true;
238 if (data->ret < 0) {
239 *data->total = data->ret;
240 return;
241 }
242
243 *data->total = data->count;
244}
245
246static int do_co_write_zeroes(int64_t offset, int count, int *total)
247{
248 Coroutine *co;
249 CoWriteZeroes data = {
250 .offset = offset,
251 .count = count,
252 .total = total,
253 .done = false,
254 };
255
256 co = qemu_coroutine_create(co_write_zeroes_entry);
257 qemu_coroutine_enter(co, &data);
258 while (!data.done) {
259 qemu_aio_wait();
260 }
261 if (data.ret < 0) {
262 return data.ret;
263 } else {
264 return 1;
265 }
266}
267
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200268static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
269{
Devin Nakamura43642b32011-07-11 11:22:16 -0400270 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
271 if (*total < 0) {
272 return *total;
273 }
274 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200275}
276
277static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
278{
Devin Nakamura43642b32011-07-11 11:22:16 -0400279 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
280 if (*total < 0) {
281 return *total;
282 }
283 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200284}
285
aliguorie3aff4f2009-04-05 19:14:04 +0000286#define NOT_DONE 0x7fffffff
287static void aio_rw_done(void *opaque, int ret)
288{
Devin Nakamura43642b32011-07-11 11:22:16 -0400289 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000290}
291
292static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
293{
Devin Nakamura43642b32011-07-11 11:22:16 -0400294 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000295
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100296 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
297 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400298 while (async_ret == NOT_DONE) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200299 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400300 }
aliguorie3aff4f2009-04-05 19:14:04 +0000301
Devin Nakamura43642b32011-07-11 11:22:16 -0400302 *total = qiov->size;
303 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000304}
305
306static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
307{
Devin Nakamura43642b32011-07-11 11:22:16 -0400308 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000309
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100310 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
311 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400312 while (async_ret == NOT_DONE) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200313 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400314 }
aliguorie3aff4f2009-04-05 19:14:04 +0000315
Devin Nakamura43642b32011-07-11 11:22:16 -0400316 *total = qiov->size;
317 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000318}
319
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200320struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400321 int num_done;
322 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200323};
324
325static void multiwrite_cb(void *opaque, int ret)
326{
Devin Nakamura43642b32011-07-11 11:22:16 -0400327 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200328
Devin Nakamura43642b32011-07-11 11:22:16 -0400329 async_ret->num_done++;
330 if (ret < 0) {
331 async_ret->error = ret;
332 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200333}
334
335static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
336{
Devin Nakamura43642b32011-07-11 11:22:16 -0400337 int i, ret;
338 struct multiwrite_async_ret async_ret = {
339 .num_done = 0,
340 .error = 0,
341 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200342
Devin Nakamura43642b32011-07-11 11:22:16 -0400343 *total = 0;
344 for (i = 0; i < num_reqs; i++) {
345 reqs[i].cb = multiwrite_cb;
346 reqs[i].opaque = &async_ret;
347 *total += reqs[i].qiov->size;
348 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200349
Devin Nakamura43642b32011-07-11 11:22:16 -0400350 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
351 if (ret < 0) {
352 return ret;
353 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200354
Devin Nakamura43642b32011-07-11 11:22:16 -0400355 while (async_ret.num_done < num_reqs) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200356 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400357 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200358
Devin Nakamura43642b32011-07-11 11:22:16 -0400359 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200360}
aliguorie3aff4f2009-04-05 19:14:04 +0000361
Devin Nakamura43642b32011-07-11 11:22:16 -0400362static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000363{
Devin Nakamura43642b32011-07-11 11:22:16 -0400364 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000365"\n"
366" reads a range of bytes from the given offset\n"
367"\n"
368" Example:\n"
369" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
370"\n"
371" Reads a segment of the currently open file, optionally dumping it to the\n"
372" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200373" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200374" -C, -- report statistics in a machine parsable format\n"
375" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000376" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000377" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100378" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200379" -s, -- start offset for pattern verification (only with -P)\n"
380" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000381"\n");
382}
383
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000384static int read_f(int argc, char **argv);
385
386static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400387 .name = "read",
388 .altname = "r",
389 .cfunc = read_f,
390 .argmin = 2,
391 .argmax = -1,
392 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
393 .oneline = "reads a number of bytes at a specified offset",
394 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000395};
396
Devin Nakamura43642b32011-07-11 11:22:16 -0400397static int read_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000398{
Devin Nakamura43642b32011-07-11 11:22:16 -0400399 struct timeval t1, t2;
400 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
401 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
402 int c, cnt;
403 char *buf;
404 int64_t offset;
405 int count;
406 /* Some compilers get confused and warn if this is not initialized. */
407 int total = 0;
408 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000409
Devin Nakamura43642b32011-07-11 11:22:16 -0400410 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
411 switch (c) {
412 case 'b':
413 bflag = 1;
414 break;
415 case 'C':
416 Cflag = 1;
417 break;
418 case 'l':
419 lflag = 1;
420 pattern_count = cvtnum(optarg);
421 if (pattern_count < 0) {
422 printf("non-numeric length argument -- %s\n", optarg);
423 return 0;
424 }
425 break;
426 case 'p':
427 pflag = 1;
428 break;
429 case 'P':
430 Pflag = 1;
431 pattern = parse_pattern(optarg);
432 if (pattern < 0) {
433 return 0;
434 }
435 break;
436 case 'q':
437 qflag = 1;
438 break;
439 case 's':
440 sflag = 1;
441 pattern_offset = cvtnum(optarg);
442 if (pattern_offset < 0) {
443 printf("non-numeric length argument -- %s\n", optarg);
444 return 0;
445 }
446 break;
447 case 'v':
448 vflag = 1;
449 break;
450 default:
451 return command_usage(&read_cmd);
452 }
453 }
aliguorie3aff4f2009-04-05 19:14:04 +0000454
Devin Nakamura43642b32011-07-11 11:22:16 -0400455 if (optind != argc - 2) {
456 return command_usage(&read_cmd);
457 }
aliguorie3aff4f2009-04-05 19:14:04 +0000458
Devin Nakamura43642b32011-07-11 11:22:16 -0400459 if (bflag && pflag) {
460 printf("-b and -p cannot be specified at the same time\n");
461 return 0;
462 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200463
Devin Nakamura43642b32011-07-11 11:22:16 -0400464 offset = cvtnum(argv[optind]);
465 if (offset < 0) {
466 printf("non-numeric length argument -- %s\n", argv[optind]);
467 return 0;
468 }
aliguorie3aff4f2009-04-05 19:14:04 +0000469
Devin Nakamura43642b32011-07-11 11:22:16 -0400470 optind++;
471 count = cvtnum(argv[optind]);
472 if (count < 0) {
473 printf("non-numeric length argument -- %s\n", argv[optind]);
474 return 0;
475 }
aliguorie3aff4f2009-04-05 19:14:04 +0000476
Kevin Wolfd9654a52009-04-23 15:11:13 +0200477 if (!Pflag && (lflag || sflag)) {
478 return command_usage(&read_cmd);
479 }
480
481 if (!lflag) {
482 pattern_count = count - pattern_offset;
483 }
484
485 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
Dong Xu Wang07f35072011-11-22 18:06:26 +0800486 printf("pattern verification range exceeds end of read data\n");
Kevin Wolfd9654a52009-04-23 15:11:13 +0200487 return 0;
488 }
489
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400490 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400491 if (offset & 0x1ff) {
492 printf("offset %" PRId64 " is not sector aligned\n",
493 offset);
494 return 0;
495 }
496 if (count & 0x1ff) {
497 printf("count %d is not sector aligned\n",
498 count);
499 return 0;
500 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400501 }
aliguorie3aff4f2009-04-05 19:14:04 +0000502
Devin Nakamura43642b32011-07-11 11:22:16 -0400503 buf = qemu_io_alloc(count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000504
Devin Nakamura43642b32011-07-11 11:22:16 -0400505 gettimeofday(&t1, NULL);
506 if (pflag) {
507 cnt = do_pread(buf, offset, count, &total);
508 } else if (bflag) {
509 cnt = do_load_vmstate(buf, offset, count, &total);
510 } else {
511 cnt = do_read(buf, offset, count, &total);
512 }
513 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000514
Devin Nakamura43642b32011-07-11 11:22:16 -0400515 if (cnt < 0) {
516 printf("read failed: %s\n", strerror(-cnt));
517 goto out;
518 }
aliguorie3aff4f2009-04-05 19:14:04 +0000519
Devin Nakamura43642b32011-07-11 11:22:16 -0400520 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000521 void *cmp_buf = g_malloc(pattern_count);
Devin Nakamura43642b32011-07-11 11:22:16 -0400522 memset(cmp_buf, pattern, pattern_count);
523 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
524 printf("Pattern verification failed at offset %"
525 PRId64 ", %d bytes\n",
526 offset + pattern_offset, pattern_count);
527 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000528 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400529 }
aliguorie3aff4f2009-04-05 19:14:04 +0000530
Devin Nakamura43642b32011-07-11 11:22:16 -0400531 if (qflag) {
532 goto out;
533 }
aliguoric48101a2009-04-18 15:36:23 +0000534
Devin Nakamura43642b32011-07-11 11:22:16 -0400535 if (vflag) {
536 dump_buffer(buf, offset, count);
537 }
aliguorie3aff4f2009-04-05 19:14:04 +0000538
Devin Nakamura43642b32011-07-11 11:22:16 -0400539 /* Finally, report back -- -C gives a parsable format */
540 t2 = tsub(t2, t1);
541 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000542
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200543out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000545
Devin Nakamura43642b32011-07-11 11:22:16 -0400546 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000547}
548
Devin Nakamura43642b32011-07-11 11:22:16 -0400549static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000550{
Devin Nakamura43642b32011-07-11 11:22:16 -0400551 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000552"\n"
553" reads a range of bytes from the given offset into multiple buffers\n"
554"\n"
555" Example:\n"
556" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
557"\n"
558" Reads a segment of the currently open file, optionally dumping it to the\n"
559" standard output stream (with -v option) for subsequent inspection.\n"
560" Uses multiple iovec buffers if more than one byte range is specified.\n"
561" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000562" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000563" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100564" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000565"\n");
566}
567
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000568static int readv_f(int argc, char **argv);
569
570static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400571 .name = "readv",
572 .cfunc = readv_f,
573 .argmin = 2,
574 .argmax = -1,
575 .args = "[-Cqv] [-P pattern ] off len [len..]",
576 .oneline = "reads a number of bytes at a specified offset",
577 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000578};
579
Devin Nakamura43642b32011-07-11 11:22:16 -0400580static int readv_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000581{
Devin Nakamura43642b32011-07-11 11:22:16 -0400582 struct timeval t1, t2;
583 int Cflag = 0, qflag = 0, vflag = 0;
584 int c, cnt;
585 char *buf;
586 int64_t offset;
587 /* Some compilers get confused and warn if this is not initialized. */
588 int total = 0;
589 int nr_iov;
590 QEMUIOVector qiov;
591 int pattern = 0;
592 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000593
Devin Nakamura43642b32011-07-11 11:22:16 -0400594 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
595 switch (c) {
596 case 'C':
597 Cflag = 1;
598 break;
599 case 'P':
600 Pflag = 1;
601 pattern = parse_pattern(optarg);
602 if (pattern < 0) {
603 return 0;
604 }
605 break;
606 case 'q':
607 qflag = 1;
608 break;
609 case 'v':
610 vflag = 1;
611 break;
612 default:
613 return command_usage(&readv_cmd);
614 }
615 }
aliguorie3aff4f2009-04-05 19:14:04 +0000616
Devin Nakamura43642b32011-07-11 11:22:16 -0400617 if (optind > argc - 2) {
618 return command_usage(&readv_cmd);
619 }
aliguorie3aff4f2009-04-05 19:14:04 +0000620
621
Devin Nakamura43642b32011-07-11 11:22:16 -0400622 offset = cvtnum(argv[optind]);
623 if (offset < 0) {
624 printf("non-numeric length argument -- %s\n", argv[optind]);
625 return 0;
626 }
627 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000628
Devin Nakamura43642b32011-07-11 11:22:16 -0400629 if (offset & 0x1ff) {
630 printf("offset %" PRId64 " is not sector aligned\n",
631 offset);
632 return 0;
633 }
aliguorie3aff4f2009-04-05 19:14:04 +0000634
Devin Nakamura43642b32011-07-11 11:22:16 -0400635 nr_iov = argc - optind;
636 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +0100637 if (buf == NULL) {
638 return 0;
639 }
aliguorie3aff4f2009-04-05 19:14:04 +0000640
Devin Nakamura43642b32011-07-11 11:22:16 -0400641 gettimeofday(&t1, NULL);
642 cnt = do_aio_readv(&qiov, offset, &total);
643 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000644
Devin Nakamura43642b32011-07-11 11:22:16 -0400645 if (cnt < 0) {
646 printf("readv failed: %s\n", strerror(-cnt));
647 goto out;
648 }
aliguorie3aff4f2009-04-05 19:14:04 +0000649
Devin Nakamura43642b32011-07-11 11:22:16 -0400650 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000651 void *cmp_buf = g_malloc(qiov.size);
Devin Nakamura43642b32011-07-11 11:22:16 -0400652 memset(cmp_buf, pattern, qiov.size);
653 if (memcmp(buf, cmp_buf, qiov.size)) {
654 printf("Pattern verification failed at offset %"
655 PRId64 ", %zd bytes\n", offset, qiov.size);
656 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000657 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400658 }
aliguoric48101a2009-04-18 15:36:23 +0000659
Devin Nakamura43642b32011-07-11 11:22:16 -0400660 if (qflag) {
661 goto out;
662 }
aliguorie3aff4f2009-04-05 19:14:04 +0000663
Devin Nakamura43642b32011-07-11 11:22:16 -0400664 if (vflag) {
665 dump_buffer(buf, offset, qiov.size);
666 }
aliguorie3aff4f2009-04-05 19:14:04 +0000667
Devin Nakamura43642b32011-07-11 11:22:16 -0400668 /* Finally, report back -- -C gives a parsable format */
669 t2 = tsub(t2, t1);
670 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000671
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200672out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400673 qemu_io_free(buf);
674 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000675}
676
Devin Nakamura43642b32011-07-11 11:22:16 -0400677static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000678{
Devin Nakamura43642b32011-07-11 11:22:16 -0400679 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000680"\n"
681" writes a range of bytes from the given offset\n"
682"\n"
683" Example:\n"
684" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
685"\n"
686" Writes into a segment of the currently open file, using a buffer\n"
687" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200688" -b, -- write to the VM state rather than the virtual disk\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000689" -p, -- use bdrv_pwrite to write the file\n"
690" -P, -- use different pattern to fill file\n"
691" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100692" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000693" -z, -- write zeroes using bdrv_co_write_zeroes\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000694"\n");
695}
696
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000697static int write_f(int argc, char **argv);
698
699static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400700 .name = "write",
701 .altname = "w",
702 .cfunc = write_f,
703 .argmin = 2,
704 .argmax = -1,
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000705 .args = "[-bCpqz] [-P pattern ] off len",
Devin Nakamura43642b32011-07-11 11:22:16 -0400706 .oneline = "writes a number of bytes at a specified offset",
707 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000708};
709
Devin Nakamura43642b32011-07-11 11:22:16 -0400710static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000711{
Devin Nakamura43642b32011-07-11 11:22:16 -0400712 struct timeval t1, t2;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000713 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400714 int c, cnt;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000715 char *buf = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400716 int64_t offset;
717 int count;
718 /* Some compilers get confused and warn if this is not initialized. */
719 int total = 0;
720 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000721
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000722 while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400723 switch (c) {
724 case 'b':
725 bflag = 1;
726 break;
727 case 'C':
728 Cflag = 1;
729 break;
730 case 'p':
731 pflag = 1;
732 break;
733 case 'P':
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000734 Pflag = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400735 pattern = parse_pattern(optarg);
736 if (pattern < 0) {
737 return 0;
738 }
739 break;
740 case 'q':
741 qflag = 1;
742 break;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000743 case 'z':
744 zflag = 1;
745 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400746 default:
747 return command_usage(&write_cmd);
748 }
749 }
aliguorie3aff4f2009-04-05 19:14:04 +0000750
Devin Nakamura43642b32011-07-11 11:22:16 -0400751 if (optind != argc - 2) {
752 return command_usage(&write_cmd);
753 }
aliguorie3aff4f2009-04-05 19:14:04 +0000754
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000755 if (bflag + pflag + zflag > 1) {
756 printf("-b, -p, or -z cannot be specified at the same time\n");
757 return 0;
758 }
759
760 if (zflag && Pflag) {
761 printf("-z and -P cannot be specified at the same time\n");
Devin Nakamura43642b32011-07-11 11:22:16 -0400762 return 0;
763 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200764
Devin Nakamura43642b32011-07-11 11:22:16 -0400765 offset = cvtnum(argv[optind]);
766 if (offset < 0) {
767 printf("non-numeric length argument -- %s\n", argv[optind]);
768 return 0;
769 }
aliguorie3aff4f2009-04-05 19:14:04 +0000770
Devin Nakamura43642b32011-07-11 11:22:16 -0400771 optind++;
772 count = cvtnum(argv[optind]);
773 if (count < 0) {
774 printf("non-numeric length argument -- %s\n", argv[optind]);
775 return 0;
776 }
aliguorie3aff4f2009-04-05 19:14:04 +0000777
Devin Nakamura43642b32011-07-11 11:22:16 -0400778 if (!pflag) {
779 if (offset & 0x1ff) {
780 printf("offset %" PRId64 " is not sector aligned\n",
781 offset);
782 return 0;
783 }
aliguorie3aff4f2009-04-05 19:14:04 +0000784
Devin Nakamura43642b32011-07-11 11:22:16 -0400785 if (count & 0x1ff) {
786 printf("count %d is not sector aligned\n",
787 count);
788 return 0;
789 }
790 }
aliguorie3aff4f2009-04-05 19:14:04 +0000791
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000792 if (!zflag) {
793 buf = qemu_io_alloc(count, pattern);
794 }
aliguorie3aff4f2009-04-05 19:14:04 +0000795
Devin Nakamura43642b32011-07-11 11:22:16 -0400796 gettimeofday(&t1, NULL);
797 if (pflag) {
798 cnt = do_pwrite(buf, offset, count, &total);
799 } else if (bflag) {
800 cnt = do_save_vmstate(buf, offset, count, &total);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000801 } else if (zflag) {
802 cnt = do_co_write_zeroes(offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400803 } else {
804 cnt = do_write(buf, offset, count, &total);
805 }
806 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000807
Devin Nakamura43642b32011-07-11 11:22:16 -0400808 if (cnt < 0) {
809 printf("write failed: %s\n", strerror(-cnt));
810 goto out;
811 }
aliguorie3aff4f2009-04-05 19:14:04 +0000812
Devin Nakamura43642b32011-07-11 11:22:16 -0400813 if (qflag) {
814 goto out;
815 }
aliguorie3aff4f2009-04-05 19:14:04 +0000816
Devin Nakamura43642b32011-07-11 11:22:16 -0400817 /* Finally, report back -- -C gives a parsable format */
818 t2 = tsub(t2, t1);
819 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000820
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200821out:
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000822 if (!zflag) {
823 qemu_io_free(buf);
824 }
aliguorie3aff4f2009-04-05 19:14:04 +0000825
Devin Nakamura43642b32011-07-11 11:22:16 -0400826 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000827}
828
aliguorie3aff4f2009-04-05 19:14:04 +0000829static void
830writev_help(void)
831{
Devin Nakamura43642b32011-07-11 11:22:16 -0400832 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000833"\n"
834" writes a range of bytes from the given offset source from multiple buffers\n"
835"\n"
836" Example:\n"
837" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
838"\n"
839" Writes into a segment of the currently open file, using a buffer\n"
840" filled with a set pattern (0xcdcdcdcd).\n"
841" -P, -- use different pattern to fill file\n"
842" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100843" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000844"\n");
845}
846
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000847static int writev_f(int argc, char **argv);
848
849static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400850 .name = "writev",
851 .cfunc = writev_f,
852 .argmin = 2,
853 .argmax = -1,
854 .args = "[-Cq] [-P pattern ] off len [len..]",
855 .oneline = "writes a number of bytes at a specified offset",
856 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000857};
858
Devin Nakamura43642b32011-07-11 11:22:16 -0400859static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000860{
Devin Nakamura43642b32011-07-11 11:22:16 -0400861 struct timeval t1, t2;
862 int Cflag = 0, qflag = 0;
863 int c, cnt;
864 char *buf;
865 int64_t offset;
866 /* Some compilers get confused and warn if this is not initialized. */
867 int total = 0;
868 int nr_iov;
869 int pattern = 0xcd;
870 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000871
Devin Nakamura43642b32011-07-11 11:22:16 -0400872 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
873 switch (c) {
874 case 'C':
875 Cflag = 1;
876 break;
877 case 'q':
878 qflag = 1;
879 break;
880 case 'P':
881 pattern = parse_pattern(optarg);
882 if (pattern < 0) {
883 return 0;
884 }
885 break;
886 default:
887 return command_usage(&writev_cmd);
888 }
889 }
aliguorie3aff4f2009-04-05 19:14:04 +0000890
Devin Nakamura43642b32011-07-11 11:22:16 -0400891 if (optind > argc - 2) {
892 return command_usage(&writev_cmd);
893 }
aliguorie3aff4f2009-04-05 19:14:04 +0000894
Devin Nakamura43642b32011-07-11 11:22:16 -0400895 offset = cvtnum(argv[optind]);
896 if (offset < 0) {
897 printf("non-numeric length argument -- %s\n", argv[optind]);
898 return 0;
899 }
900 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000901
Devin Nakamura43642b32011-07-11 11:22:16 -0400902 if (offset & 0x1ff) {
903 printf("offset %" PRId64 " is not sector aligned\n",
904 offset);
905 return 0;
906 }
aliguorie3aff4f2009-04-05 19:14:04 +0000907
Devin Nakamura43642b32011-07-11 11:22:16 -0400908 nr_iov = argc - optind;
909 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100910 if (buf == NULL) {
911 return 0;
912 }
aliguorie3aff4f2009-04-05 19:14:04 +0000913
Devin Nakamura43642b32011-07-11 11:22:16 -0400914 gettimeofday(&t1, NULL);
915 cnt = do_aio_writev(&qiov, offset, &total);
916 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000917
Devin Nakamura43642b32011-07-11 11:22:16 -0400918 if (cnt < 0) {
919 printf("writev failed: %s\n", strerror(-cnt));
920 goto out;
921 }
aliguorie3aff4f2009-04-05 19:14:04 +0000922
Devin Nakamura43642b32011-07-11 11:22:16 -0400923 if (qflag) {
924 goto out;
925 }
aliguorie3aff4f2009-04-05 19:14:04 +0000926
Devin Nakamura43642b32011-07-11 11:22:16 -0400927 /* Finally, report back -- -C gives a parsable format */
928 t2 = tsub(t2, t1);
929 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200930out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400931 qemu_io_free(buf);
932 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000933}
934
Devin Nakamura43642b32011-07-11 11:22:16 -0400935static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200936{
Devin Nakamura43642b32011-07-11 11:22:16 -0400937 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200938"\n"
939" writes a range of bytes from the given offset source from multiple buffers,\n"
940" in a batch of requests that may be merged by qemu\n"
941"\n"
942" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200943" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200944" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
945"\n"
946" Writes into a segment of the currently open file, using a buffer\n"
947" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
948" by one for each request contained in the multiwrite command.\n"
949" -P, -- use different pattern to fill file\n"
950" -C, -- report statistics in a machine parsable format\n"
951" -q, -- quiet mode, do not show I/O statistics\n"
952"\n");
953}
954
955static int multiwrite_f(int argc, char **argv);
956
957static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400958 .name = "multiwrite",
959 .cfunc = multiwrite_f,
960 .argmin = 2,
961 .argmax = -1,
962 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
963 .oneline = "issues multiple write requests at once",
964 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200965};
966
Devin Nakamura43642b32011-07-11 11:22:16 -0400967static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200968{
Devin Nakamura43642b32011-07-11 11:22:16 -0400969 struct timeval t1, t2;
970 int Cflag = 0, qflag = 0;
971 int c, cnt;
972 char **buf;
973 int64_t offset, first_offset = 0;
974 /* Some compilers get confused and warn if this is not initialized. */
975 int total = 0;
976 int nr_iov;
977 int nr_reqs;
978 int pattern = 0xcd;
979 QEMUIOVector *qiovs;
980 int i;
981 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200982
Devin Nakamura43642b32011-07-11 11:22:16 -0400983 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
984 switch (c) {
985 case 'C':
986 Cflag = 1;
987 break;
988 case 'q':
989 qflag = 1;
990 break;
991 case 'P':
992 pattern = parse_pattern(optarg);
993 if (pattern < 0) {
994 return 0;
995 }
996 break;
997 default:
998 return command_usage(&writev_cmd);
999 }
1000 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001001
Devin Nakamura43642b32011-07-11 11:22:16 -04001002 if (optind > argc - 2) {
1003 return command_usage(&writev_cmd);
1004 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001005
Devin Nakamura43642b32011-07-11 11:22:16 -04001006 nr_reqs = 1;
1007 for (i = optind; i < argc; i++) {
1008 if (!strcmp(argv[i], ";")) {
1009 nr_reqs++;
1010 }
1011 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001012
Kevin Wolff2360622011-10-31 11:36:32 +01001013 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1014 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -05001015 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001016
Kevin Wolf67403db2011-10-31 11:49:21 +01001017 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001018 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001019
Devin Nakamura43642b32011-07-11 11:22:16 -04001020 /* Read the offset of the request */
1021 offset = cvtnum(argv[optind]);
1022 if (offset < 0) {
1023 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +01001024 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001025 }
1026 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001027
Devin Nakamura43642b32011-07-11 11:22:16 -04001028 if (offset & 0x1ff) {
1029 printf("offset %lld is not sector aligned\n",
1030 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +01001031 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001032 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001033
1034 if (i == 0) {
1035 first_offset = offset;
1036 }
1037
Devin Nakamura43642b32011-07-11 11:22:16 -04001038 /* Read lengths for qiov entries */
1039 for (j = optind; j < argc; j++) {
1040 if (!strcmp(argv[j], ";")) {
1041 break;
1042 }
1043 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001044
Devin Nakamura43642b32011-07-11 11:22:16 -04001045 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001046
Devin Nakamura43642b32011-07-11 11:22:16 -04001047 /* Build request */
Kevin Wolff2360622011-10-31 11:36:32 +01001048 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1049 if (buf[i] == NULL) {
1050 goto out;
1051 }
1052
Devin Nakamura43642b32011-07-11 11:22:16 -04001053 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001054 reqs[i].sector = offset >> 9;
1055 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001056
Devin Nakamura43642b32011-07-11 11:22:16 -04001057 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001058
Devin Nakamura43642b32011-07-11 11:22:16 -04001059 pattern++;
1060 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001061
Kevin Wolf67403db2011-10-31 11:49:21 +01001062 /* If there were empty requests at the end, ignore them */
1063 nr_reqs = i;
1064
Devin Nakamura43642b32011-07-11 11:22:16 -04001065 gettimeofday(&t1, NULL);
1066 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1067 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001068
Devin Nakamura43642b32011-07-11 11:22:16 -04001069 if (cnt < 0) {
1070 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1071 goto out;
1072 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001073
Devin Nakamura43642b32011-07-11 11:22:16 -04001074 if (qflag) {
1075 goto out;
1076 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001077
Devin Nakamura43642b32011-07-11 11:22:16 -04001078 /* Finally, report back -- -C gives a parsable format */
1079 t2 = tsub(t2, t1);
1080 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001081out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001082 for (i = 0; i < nr_reqs; i++) {
1083 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001084 if (reqs[i].qiov != NULL) {
1085 qemu_iovec_destroy(&qiovs[i]);
1086 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001087 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001088 g_free(buf);
1089 g_free(reqs);
1090 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001091 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001092}
1093
Christoph Hellwig95533d52009-06-20 21:10:44 +02001094struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001095 QEMUIOVector qiov;
1096 int64_t offset;
1097 char *buf;
1098 int qflag;
1099 int vflag;
1100 int Cflag;
1101 int Pflag;
1102 int pattern;
1103 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001104};
1105
Devin Nakamura43642b32011-07-11 11:22:16 -04001106static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001107{
Devin Nakamura43642b32011-07-11 11:22:16 -04001108 struct aio_ctx *ctx = opaque;
1109 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001110
Devin Nakamura43642b32011-07-11 11:22:16 -04001111 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001112
Christoph Hellwig95533d52009-06-20 21:10:44 +02001113
Devin Nakamura43642b32011-07-11 11:22:16 -04001114 if (ret < 0) {
1115 printf("aio_write failed: %s\n", strerror(-ret));
1116 goto out;
1117 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001118
Devin Nakamura43642b32011-07-11 11:22:16 -04001119 if (ctx->qflag) {
1120 goto out;
1121 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001122
Devin Nakamura43642b32011-07-11 11:22:16 -04001123 /* Finally, report back -- -C gives a parsable format */
1124 t2 = tsub(t2, ctx->t1);
1125 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1126 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001127out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001128 qemu_io_free(ctx->buf);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001129 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001130}
1131
Devin Nakamura43642b32011-07-11 11:22:16 -04001132static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001133{
Devin Nakamura43642b32011-07-11 11:22:16 -04001134 struct aio_ctx *ctx = opaque;
1135 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001136
Devin Nakamura43642b32011-07-11 11:22:16 -04001137 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001138
Devin Nakamura43642b32011-07-11 11:22:16 -04001139 if (ret < 0) {
1140 printf("readv failed: %s\n", strerror(-ret));
1141 goto out;
1142 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001143
Devin Nakamura43642b32011-07-11 11:22:16 -04001144 if (ctx->Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001145 void *cmp_buf = g_malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001146
Devin Nakamura43642b32011-07-11 11:22:16 -04001147 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1148 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1149 printf("Pattern verification failed at offset %"
1150 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1151 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001152 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -04001153 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001154
Devin Nakamura43642b32011-07-11 11:22:16 -04001155 if (ctx->qflag) {
1156 goto out;
1157 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001158
Devin Nakamura43642b32011-07-11 11:22:16 -04001159 if (ctx->vflag) {
1160 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1161 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001162
Devin Nakamura43642b32011-07-11 11:22:16 -04001163 /* Finally, report back -- -C gives a parsable format */
1164 t2 = tsub(t2, ctx->t1);
1165 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1166 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001167out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001168 qemu_io_free(ctx->buf);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001169 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001170}
1171
Devin Nakamura43642b32011-07-11 11:22:16 -04001172static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001173{
Devin Nakamura43642b32011-07-11 11:22:16 -04001174 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001175"\n"
1176" asynchronously reads a range of bytes from the given offset\n"
1177"\n"
1178" Example:\n"
1179" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1180"\n"
1181" Reads a segment of the currently open file, optionally dumping it to the\n"
1182" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001183" The read is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001184" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001185" -C, -- report statistics in a machine parsable format\n"
1186" -P, -- use a pattern to verify read data\n"
1187" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001188" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001189"\n");
1190}
1191
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001192static int aio_read_f(int argc, char **argv);
1193
1194static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001195 .name = "aio_read",
1196 .cfunc = aio_read_f,
1197 .argmin = 2,
1198 .argmax = -1,
1199 .args = "[-Cqv] [-P pattern ] off len [len..]",
1200 .oneline = "asynchronously reads a number of bytes",
1201 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001202};
1203
Devin Nakamura43642b32011-07-11 11:22:16 -04001204static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001205{
Devin Nakamura43642b32011-07-11 11:22:16 -04001206 int nr_iov, c;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001207 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001208
Devin Nakamura43642b32011-07-11 11:22:16 -04001209 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1210 switch (c) {
1211 case 'C':
1212 ctx->Cflag = 1;
1213 break;
1214 case 'P':
1215 ctx->Pflag = 1;
1216 ctx->pattern = parse_pattern(optarg);
1217 if (ctx->pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001218 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001219 return 0;
1220 }
1221 break;
1222 case 'q':
1223 ctx->qflag = 1;
1224 break;
1225 case 'v':
1226 ctx->vflag = 1;
1227 break;
1228 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001229 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001230 return command_usage(&aio_read_cmd);
1231 }
1232 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001233
Devin Nakamura43642b32011-07-11 11:22:16 -04001234 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001235 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001236 return command_usage(&aio_read_cmd);
1237 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001238
Devin Nakamura43642b32011-07-11 11:22:16 -04001239 ctx->offset = cvtnum(argv[optind]);
1240 if (ctx->offset < 0) {
1241 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001242 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001243 return 0;
1244 }
1245 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001246
Devin Nakamura43642b32011-07-11 11:22:16 -04001247 if (ctx->offset & 0x1ff) {
1248 printf("offset %" PRId64 " is not sector aligned\n",
1249 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001250 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001251 return 0;
1252 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001253
Devin Nakamura43642b32011-07-11 11:22:16 -04001254 nr_iov = argc - optind;
1255 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001256 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001257 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001258 return 0;
1259 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001260
Devin Nakamura43642b32011-07-11 11:22:16 -04001261 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001262 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1263 ctx->qiov.size >> 9, aio_read_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001264 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001265}
1266
Devin Nakamura43642b32011-07-11 11:22:16 -04001267static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001268{
Devin Nakamura43642b32011-07-11 11:22:16 -04001269 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001270"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001271" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001272" from multiple buffers\n"
1273"\n"
1274" Example:\n"
1275" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1276"\n"
1277" Writes into a segment of the currently open file, using a buffer\n"
1278" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001279" The write is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001280" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001281" -P, -- use different pattern to fill file\n"
1282" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001283" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001284"\n");
1285}
1286
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001287static int aio_write_f(int argc, char **argv);
1288
1289static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001290 .name = "aio_write",
1291 .cfunc = aio_write_f,
1292 .argmin = 2,
1293 .argmax = -1,
1294 .args = "[-Cq] [-P pattern ] off len [len..]",
1295 .oneline = "asynchronously writes a number of bytes",
1296 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001297};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001298
Devin Nakamura43642b32011-07-11 11:22:16 -04001299static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001300{
Devin Nakamura43642b32011-07-11 11:22:16 -04001301 int nr_iov, c;
1302 int pattern = 0xcd;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001303 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001304
Devin Nakamura43642b32011-07-11 11:22:16 -04001305 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1306 switch (c) {
1307 case 'C':
1308 ctx->Cflag = 1;
1309 break;
1310 case 'q':
1311 ctx->qflag = 1;
1312 break;
1313 case 'P':
1314 pattern = parse_pattern(optarg);
1315 if (pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001316 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001317 return 0;
1318 }
1319 break;
1320 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001321 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001322 return command_usage(&aio_write_cmd);
1323 }
1324 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001325
Devin Nakamura43642b32011-07-11 11:22:16 -04001326 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001327 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001328 return command_usage(&aio_write_cmd);
1329 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001330
Devin Nakamura43642b32011-07-11 11:22:16 -04001331 ctx->offset = cvtnum(argv[optind]);
1332 if (ctx->offset < 0) {
1333 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001334 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001335 return 0;
1336 }
1337 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001338
Devin Nakamura43642b32011-07-11 11:22:16 -04001339 if (ctx->offset & 0x1ff) {
1340 printf("offset %" PRId64 " is not sector aligned\n",
1341 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001342 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001343 return 0;
1344 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001345
Devin Nakamura43642b32011-07-11 11:22:16 -04001346 nr_iov = argc - optind;
1347 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001348 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001349 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001350 return 0;
1351 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001352
Devin Nakamura43642b32011-07-11 11:22:16 -04001353 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001354 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1355 ctx->qiov.size >> 9, aio_write_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001356 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001357}
1358
Devin Nakamura43642b32011-07-11 11:22:16 -04001359static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001360{
Devin Nakamura43642b32011-07-11 11:22:16 -04001361 qemu_aio_flush();
1362 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001363}
1364
1365static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001366 .name = "aio_flush",
1367 .cfunc = aio_flush_f,
1368 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001369};
1370
Devin Nakamura43642b32011-07-11 11:22:16 -04001371static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001372{
Devin Nakamura43642b32011-07-11 11:22:16 -04001373 bdrv_flush(bs);
1374 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001375}
1376
1377static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001378 .name = "flush",
1379 .altname = "f",
1380 .cfunc = flush_f,
1381 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001382};
1383
Devin Nakamura43642b32011-07-11 11:22:16 -04001384static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001385{
Devin Nakamura43642b32011-07-11 11:22:16 -04001386 int64_t offset;
1387 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001388
Devin Nakamura43642b32011-07-11 11:22:16 -04001389 offset = cvtnum(argv[1]);
1390 if (offset < 0) {
1391 printf("non-numeric truncate argument -- %s\n", argv[1]);
1392 return 0;
1393 }
aliguorie3aff4f2009-04-05 19:14:04 +00001394
Devin Nakamura43642b32011-07-11 11:22:16 -04001395 ret = bdrv_truncate(bs, offset);
1396 if (ret < 0) {
1397 printf("truncate: %s\n", strerror(-ret));
1398 return 0;
1399 }
aliguorie3aff4f2009-04-05 19:14:04 +00001400
Devin Nakamura43642b32011-07-11 11:22:16 -04001401 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001402}
1403
1404static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001405 .name = "truncate",
1406 .altname = "t",
1407 .cfunc = truncate_f,
1408 .argmin = 1,
1409 .argmax = 1,
1410 .args = "off",
1411 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001412};
1413
Devin Nakamura43642b32011-07-11 11:22:16 -04001414static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001415{
Devin Nakamura43642b32011-07-11 11:22:16 -04001416 int64_t size;
1417 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001418
Devin Nakamura43642b32011-07-11 11:22:16 -04001419 size = bdrv_getlength(bs);
1420 if (size < 0) {
1421 printf("getlength: %s\n", strerror(-size));
1422 return 0;
1423 }
aliguorie3aff4f2009-04-05 19:14:04 +00001424
Devin Nakamura43642b32011-07-11 11:22:16 -04001425 cvtstr(size, s1, sizeof(s1));
1426 printf("%s\n", s1);
1427 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001428}
1429
1430
1431static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001432 .name = "length",
1433 .altname = "l",
1434 .cfunc = length_f,
1435 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001436};
1437
1438
Devin Nakamura43642b32011-07-11 11:22:16 -04001439static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001440{
Devin Nakamura43642b32011-07-11 11:22:16 -04001441 BlockDriverInfo bdi;
1442 char s1[64], s2[64];
1443 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001444
Devin Nakamura43642b32011-07-11 11:22:16 -04001445 if (bs->drv && bs->drv->format_name) {
1446 printf("format name: %s\n", bs->drv->format_name);
1447 }
1448 if (bs->drv && bs->drv->protocol_name) {
1449 printf("format name: %s\n", bs->drv->protocol_name);
1450 }
aliguorie3aff4f2009-04-05 19:14:04 +00001451
Devin Nakamura43642b32011-07-11 11:22:16 -04001452 ret = bdrv_get_info(bs, &bdi);
1453 if (ret) {
1454 return 0;
1455 }
aliguorie3aff4f2009-04-05 19:14:04 +00001456
Devin Nakamura43642b32011-07-11 11:22:16 -04001457 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1458 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001459
Devin Nakamura43642b32011-07-11 11:22:16 -04001460 printf("cluster size: %s\n", s1);
1461 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001462
Devin Nakamura43642b32011-07-11 11:22:16 -04001463 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001464}
1465
1466
1467
1468static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001469 .name = "info",
1470 .altname = "i",
1471 .cfunc = info_f,
1472 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001473};
1474
Devin Nakamura43642b32011-07-11 11:22:16 -04001475static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001476{
Devin Nakamura43642b32011-07-11 11:22:16 -04001477 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001478"\n"
1479" discards a range of bytes from the given offset\n"
1480"\n"
1481" Example:\n"
1482" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1483"\n"
1484" Discards a segment of the currently open file.\n"
1485" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001486" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001487"\n");
1488}
1489
1490static int discard_f(int argc, char **argv);
1491
1492static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001493 .name = "discard",
1494 .altname = "d",
1495 .cfunc = discard_f,
1496 .argmin = 2,
1497 .argmax = -1,
1498 .args = "[-Cq] off len",
1499 .oneline = "discards a number of bytes at a specified offset",
1500 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001501};
1502
Devin Nakamura43642b32011-07-11 11:22:16 -04001503static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001504{
Devin Nakamura43642b32011-07-11 11:22:16 -04001505 struct timeval t1, t2;
1506 int Cflag = 0, qflag = 0;
1507 int c, ret;
1508 int64_t offset;
1509 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001510
Devin Nakamura43642b32011-07-11 11:22:16 -04001511 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1512 switch (c) {
1513 case 'C':
1514 Cflag = 1;
1515 break;
1516 case 'q':
1517 qflag = 1;
1518 break;
1519 default:
1520 return command_usage(&discard_cmd);
1521 }
1522 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001523
Devin Nakamura43642b32011-07-11 11:22:16 -04001524 if (optind != argc - 2) {
1525 return command_usage(&discard_cmd);
1526 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001527
Devin Nakamura43642b32011-07-11 11:22:16 -04001528 offset = cvtnum(argv[optind]);
1529 if (offset < 0) {
1530 printf("non-numeric length argument -- %s\n", argv[optind]);
1531 return 0;
1532 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001533
Devin Nakamura43642b32011-07-11 11:22:16 -04001534 optind++;
1535 count = cvtnum(argv[optind]);
1536 if (count < 0) {
1537 printf("non-numeric length argument -- %s\n", argv[optind]);
1538 return 0;
1539 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001540
Devin Nakamura43642b32011-07-11 11:22:16 -04001541 gettimeofday(&t1, NULL);
1542 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1543 count >> BDRV_SECTOR_BITS);
1544 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001545
Devin Nakamura43642b32011-07-11 11:22:16 -04001546 if (ret < 0) {
1547 printf("discard failed: %s\n", strerror(-ret));
1548 goto out;
1549 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001550
Devin Nakamura43642b32011-07-11 11:22:16 -04001551 /* Finally, report back -- -C gives a parsable format */
1552 if (!qflag) {
1553 t2 = tsub(t2, t1);
1554 print_report("discard", &t2, offset, count, count, 1, Cflag);
1555 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001556
1557out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001558 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001559}
1560
Devin Nakamura43642b32011-07-11 11:22:16 -04001561static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001562{
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001563 int64_t offset, sector_num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001564 int nb_sectors, remaining;
1565 char s1[64];
1566 int num, sum_alloc;
1567 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001568
Devin Nakamura43642b32011-07-11 11:22:16 -04001569 offset = cvtnum(argv[1]);
1570 if (offset & 0x1ff) {
1571 printf("offset %" PRId64 " is not sector aligned\n",
1572 offset);
1573 return 0;
1574 }
aliguorie3aff4f2009-04-05 19:14:04 +00001575
Devin Nakamura43642b32011-07-11 11:22:16 -04001576 if (argc == 3) {
1577 nb_sectors = cvtnum(argv[2]);
1578 } else {
1579 nb_sectors = 1;
1580 }
aliguorie3aff4f2009-04-05 19:14:04 +00001581
Devin Nakamura43642b32011-07-11 11:22:16 -04001582 remaining = nb_sectors;
1583 sum_alloc = 0;
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001584 sector_num = offset >> 9;
Devin Nakamura43642b32011-07-11 11:22:16 -04001585 while (remaining) {
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001586 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1587 sector_num += num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001588 remaining -= num;
1589 if (ret) {
1590 sum_alloc += num;
1591 }
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001592 if (num == 0) {
1593 nb_sectors -= remaining;
1594 remaining = 0;
1595 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001596 }
aliguorie3aff4f2009-04-05 19:14:04 +00001597
Devin Nakamura43642b32011-07-11 11:22:16 -04001598 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001599
Devin Nakamura43642b32011-07-11 11:22:16 -04001600 printf("%d/%d sectors allocated at offset %s\n",
1601 sum_alloc, nb_sectors, s1);
1602 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001603}
1604
1605static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001606 .name = "alloc",
1607 .altname = "a",
1608 .argmin = 1,
1609 .argmax = 2,
1610 .cfunc = alloc_f,
1611 .args = "off [sectors]",
1612 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001613};
1614
Devin Nakamura43642b32011-07-11 11:22:16 -04001615static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001616{
Devin Nakamura43642b32011-07-11 11:22:16 -04001617 int64_t offset;
1618 int64_t nb_sectors;
1619 char s1[64];
1620 int num, num_checked;
1621 int ret;
1622 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001623
Devin Nakamura43642b32011-07-11 11:22:16 -04001624 offset = 0;
1625 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001626
Devin Nakamura43642b32011-07-11 11:22:16 -04001627 do {
1628 num_checked = MIN(nb_sectors, INT_MAX);
1629 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1630 retstr = ret ? " allocated" : "not allocated";
1631 cvtstr(offset << 9ULL, s1, sizeof(s1));
1632 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1633 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001634
Devin Nakamura43642b32011-07-11 11:22:16 -04001635 offset += num;
1636 nb_sectors -= num;
1637 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001638
Devin Nakamura43642b32011-07-11 11:22:16 -04001639 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001640}
1641
1642static const cmdinfo_t map_cmd = {
1643 .name = "map",
1644 .argmin = 0,
1645 .argmax = 0,
1646 .cfunc = map_f,
1647 .args = "",
1648 .oneline = "prints the allocated areas of a file",
1649};
1650
1651
Devin Nakamura43642b32011-07-11 11:22:16 -04001652static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001653{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001654 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001655 bs = NULL;
1656 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001657}
1658
1659static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001660 .name = "close",
1661 .altname = "c",
1662 .cfunc = close_f,
1663 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001664};
1665
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001666static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001667{
Devin Nakamura43642b32011-07-11 11:22:16 -04001668 if (bs) {
1669 fprintf(stderr, "file open already, try 'help close'\n");
1670 return 1;
1671 }
aliguorie3aff4f2009-04-05 19:14:04 +00001672
Devin Nakamura43642b32011-07-11 11:22:16 -04001673 if (growable) {
1674 if (bdrv_file_open(&bs, name, flags)) {
1675 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1676 return 1;
1677 }
1678 } else {
1679 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001680
Devin Nakamura43642b32011-07-11 11:22:16 -04001681 if (bdrv_open(bs, name, flags, NULL) < 0) {
1682 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001683 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001684 bs = NULL;
1685 return 1;
1686 }
1687 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001688
Devin Nakamura43642b32011-07-11 11:22:16 -04001689 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001690}
1691
Devin Nakamura43642b32011-07-11 11:22:16 -04001692static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001693{
Devin Nakamura43642b32011-07-11 11:22:16 -04001694 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001695"\n"
1696" opens a new file in the requested mode\n"
1697"\n"
1698" Example:\n"
1699" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1700"\n"
1701" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001702" -r, -- open file read-only\n"
1703" -s, -- use snapshot file\n"
1704" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001705" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001706"\n");
1707}
1708
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001709static int open_f(int argc, char **argv);
1710
1711static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001712 .name = "open",
1713 .altname = "o",
1714 .cfunc = open_f,
1715 .argmin = 1,
1716 .argmax = -1,
1717 .flags = CMD_NOFILE_OK,
1718 .args = "[-Crsn] [path]",
1719 .oneline = "open the file specified by path",
1720 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001721};
aliguorie3aff4f2009-04-05 19:14:04 +00001722
Devin Nakamura43642b32011-07-11 11:22:16 -04001723static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001724{
Devin Nakamura43642b32011-07-11 11:22:16 -04001725 int flags = 0;
1726 int readonly = 0;
1727 int growable = 0;
1728 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001729
Devin Nakamura43642b32011-07-11 11:22:16 -04001730 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1731 switch (c) {
1732 case 's':
1733 flags |= BDRV_O_SNAPSHOT;
1734 break;
1735 case 'n':
1736 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1737 break;
1738 case 'r':
1739 readonly = 1;
1740 break;
1741 case 'g':
1742 growable = 1;
1743 break;
1744 default:
1745 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001746 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001747 }
aliguorie3aff4f2009-04-05 19:14:04 +00001748
Devin Nakamura43642b32011-07-11 11:22:16 -04001749 if (!readonly) {
1750 flags |= BDRV_O_RDWR;
1751 }
aliguorie3aff4f2009-04-05 19:14:04 +00001752
Devin Nakamura43642b32011-07-11 11:22:16 -04001753 if (optind != argc - 1) {
1754 return command_usage(&open_cmd);
1755 }
1756
1757 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001758}
1759
Devin Nakamura43642b32011-07-11 11:22:16 -04001760static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001761{
Devin Nakamura43642b32011-07-11 11:22:16 -04001762 /* only one device allowed so far */
1763 if (index >= 1) {
1764 return 0;
1765 }
1766 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001767}
1768
Devin Nakamura43642b32011-07-11 11:22:16 -04001769static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001770{
Devin Nakamura43642b32011-07-11 11:22:16 -04001771 if (ct->flags & CMD_FLAG_GLOBAL) {
1772 return 1;
1773 }
1774 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1775 fprintf(stderr, "no file open, try 'help open'\n");
1776 return 0;
1777 }
1778 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001779}
1780
1781static void usage(const char *name)
1782{
Devin Nakamura43642b32011-07-11 11:22:16 -04001783 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001784"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001785"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001786"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001787" -c, --cmd command to execute\n"
1788" -r, --read-only export read-only\n"
1789" -s, --snapshot use snapshot file\n"
1790" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001791" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001792" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001793" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +02001794" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001795" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001796" -h, --help display this help and exit\n"
1797" -V, --version output version information and exit\n"
1798"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001799 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001800}
1801
1802
1803int main(int argc, char **argv)
1804{
Devin Nakamura43642b32011-07-11 11:22:16 -04001805 int readonly = 0;
1806 int growable = 0;
Kevin Wolf592fa072012-04-18 12:07:39 +02001807 const char *sopt = "hVc:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -04001808 const struct option lopt[] = {
1809 { "help", 0, NULL, 'h' },
1810 { "version", 0, NULL, 'V' },
1811 { "offset", 1, NULL, 'o' },
1812 { "cmd", 1, NULL, 'c' },
1813 { "read-only", 0, NULL, 'r' },
1814 { "snapshot", 0, NULL, 's' },
1815 { "nocache", 0, NULL, 'n' },
1816 { "misalign", 0, NULL, 'm' },
1817 { "growable", 0, NULL, 'g' },
1818 { "native-aio", 0, NULL, 'k' },
Kevin Wolf592fa072012-04-18 12:07:39 +02001819 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001820 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -04001821 { NULL, 0, NULL, 0 }
1822 };
1823 int c;
1824 int opt_index = 0;
1825 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001826
Devin Nakamura43642b32011-07-11 11:22:16 -04001827 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001828
Devin Nakamura43642b32011-07-11 11:22:16 -04001829 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1830 switch (c) {
1831 case 's':
1832 flags |= BDRV_O_SNAPSHOT;
1833 break;
1834 case 'n':
1835 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1836 break;
1837 case 'c':
1838 add_user_command(optarg);
1839 break;
1840 case 'r':
1841 readonly = 1;
1842 break;
1843 case 'm':
1844 misalign = 1;
1845 break;
1846 case 'g':
1847 growable = 1;
1848 break;
1849 case 'k':
1850 flags |= BDRV_O_NATIVE_AIO;
1851 break;
Kevin Wolf592fa072012-04-18 12:07:39 +02001852 case 't':
1853 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
1854 error_report("Invalid cache option: %s", optarg);
1855 exit(1);
1856 }
1857 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001858 case 'T':
1859 if (!trace_backend_init(optarg, NULL)) {
1860 exit(1); /* error message will have been printed */
1861 }
1862 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04001863 case 'V':
1864 printf("%s version %s\n", progname, VERSION);
1865 exit(0);
1866 case 'h':
1867 usage(progname);
1868 exit(0);
1869 default:
1870 usage(progname);
1871 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001872 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001873 }
aliguorie3aff4f2009-04-05 19:14:04 +00001874
Devin Nakamura43642b32011-07-11 11:22:16 -04001875 if ((argc - optind) > 1) {
1876 usage(progname);
1877 exit(1);
1878 }
aliguorie3aff4f2009-04-05 19:14:04 +00001879
Devin Nakamura43642b32011-07-11 11:22:16 -04001880 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001881
Zhi Yong Wua57d1142012-02-19 22:24:59 +08001882 qemu_init_main_loop();
1883
Devin Nakamura43642b32011-07-11 11:22:16 -04001884 /* initialize commands */
1885 quit_init();
1886 help_init();
1887 add_command(&open_cmd);
1888 add_command(&close_cmd);
1889 add_command(&read_cmd);
1890 add_command(&readv_cmd);
1891 add_command(&write_cmd);
1892 add_command(&writev_cmd);
1893 add_command(&multiwrite_cmd);
1894 add_command(&aio_read_cmd);
1895 add_command(&aio_write_cmd);
1896 add_command(&aio_flush_cmd);
1897 add_command(&flush_cmd);
1898 add_command(&truncate_cmd);
1899 add_command(&length_cmd);
1900 add_command(&info_cmd);
1901 add_command(&discard_cmd);
1902 add_command(&alloc_cmd);
1903 add_command(&map_cmd);
1904
1905 add_args_command(init_args_command);
1906 add_check_command(init_check_command);
1907
1908 /* open the device */
1909 if (!readonly) {
1910 flags |= BDRV_O_RDWR;
1911 }
1912
1913 if ((argc - optind) == 1) {
1914 openfile(argv[optind], flags, growable);
1915 }
1916 command_loop();
1917
1918 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001919 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -04001920 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001921 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04001922
1923 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001924 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001925 }
1926 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001927}