blob: e6fcd7719ef75f03f4358c6f38386274b0f44404 [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"
18#include "block_int.h"
19#include "cmd.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000020#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000021
22#define VERSION "0.0.1"
23
Devin Nakamura43642b32011-07-11 11:22:16 -040024#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000025
26char *progname;
27static BlockDriverState *bs;
28
29static int misalign;
30
31/*
Christoph Hellwigcf070d72009-07-20 01:19:25 +020032 * Parse the pattern argument to various sub-commands.
33 *
34 * Because the pattern is used as an argument to memset it must evaluate
35 * to an unsigned integer that fits into a single byte.
36 */
37static int parse_pattern(const char *arg)
38{
Devin Nakamura43642b32011-07-11 11:22:16 -040039 char *endptr = NULL;
40 long pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020041
Devin Nakamura43642b32011-07-11 11:22:16 -040042 pattern = strtol(arg, &endptr, 0);
43 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
44 printf("%s is not a valid pattern byte\n", arg);
45 return -1;
46 }
Christoph Hellwigcf070d72009-07-20 01:19:25 +020047
Devin Nakamura43642b32011-07-11 11:22:16 -040048 return pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020049}
50
51/*
aliguorie3aff4f2009-04-05 19:14:04 +000052 * Memory allocation helpers.
53 *
54 * Make sure memory is aligned by default, or purposefully misaligned if
55 * that is specified on the command line.
56 */
57
Devin Nakamura43642b32011-07-11 11:22:16 -040058#define MISALIGN_OFFSET 16
aliguorie3aff4f2009-04-05 19:14:04 +000059static void *qemu_io_alloc(size_t len, int pattern)
60{
Devin Nakamura43642b32011-07-11 11:22:16 -040061 void *buf;
aliguorie3aff4f2009-04-05 19:14:04 +000062
Devin Nakamura43642b32011-07-11 11:22:16 -040063 if (misalign) {
64 len += MISALIGN_OFFSET;
65 }
66 buf = qemu_blockalign(bs, len);
67 memset(buf, pattern, len);
68 if (misalign) {
69 buf += MISALIGN_OFFSET;
70 }
71 return buf;
aliguorie3aff4f2009-04-05 19:14:04 +000072}
73
74static void qemu_io_free(void *p)
75{
Devin Nakamura43642b32011-07-11 11:22:16 -040076 if (misalign) {
77 p -= MISALIGN_OFFSET;
78 }
79 qemu_vfree(p);
aliguorie3aff4f2009-04-05 19:14:04 +000080}
81
Devin Nakamura43642b32011-07-11 11:22:16 -040082static void dump_buffer(const void *buffer, int64_t offset, int len)
aliguorie3aff4f2009-04-05 19:14:04 +000083{
Devin Nakamura43642b32011-07-11 11:22:16 -040084 int i, j;
85 const uint8_t *p;
aliguorie3aff4f2009-04-05 19:14:04 +000086
Devin Nakamura43642b32011-07-11 11:22:16 -040087 for (i = 0, p = buffer; i < len; i += 16) {
88 const uint8_t *s = p;
aliguorie3aff4f2009-04-05 19:14:04 +000089
Devin Nakamura43642b32011-07-11 11:22:16 -040090 printf("%08" PRIx64 ": ", offset + i);
91 for (j = 0; j < 16 && i + j < len; j++, p++) {
92 printf("%02x ", *p);
93 }
94 printf(" ");
95 for (j = 0; j < 16 && i + j < len; j++, s++) {
96 if (isalnum(*s)) {
97 printf("%c", *s);
98 } else {
99 printf(".");
100 }
101 }
102 printf("\n");
103 }
aliguorie3aff4f2009-04-05 19:14:04 +0000104}
105
Devin Nakamura43642b32011-07-11 11:22:16 -0400106static void print_report(const char *op, struct timeval *t, int64_t offset,
107 int count, int total, int cnt, int Cflag)
aliguorie3aff4f2009-04-05 19:14:04 +0000108{
Devin Nakamura43642b32011-07-11 11:22:16 -0400109 char s1[64], s2[64], ts[64];
aliguorie3aff4f2009-04-05 19:14:04 +0000110
Devin Nakamura43642b32011-07-11 11:22:16 -0400111 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
112 if (!Cflag) {
113 cvtstr((double)total, s1, sizeof(s1));
114 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
115 printf("%s %d/%d bytes at offset %" PRId64 "\n",
116 op, total, count, offset);
117 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
118 s1, cnt, ts, s2, tdiv((double)cnt, *t));
119 } else {/* bytes,ops,time,bytes/sec,ops/sec */
120 printf("%d,%d,%s,%.3f,%.3f\n",
121 total, cnt, ts,
122 tdiv((double)total, *t),
123 tdiv((double)cnt, *t));
124 }
aliguorie3aff4f2009-04-05 19:14:04 +0000125}
126
Christoph Hellwigcf572982009-07-10 13:33:42 +0200127/*
128 * Parse multiple length statements for vectored I/O, and construct an I/O
129 * vector matching it.
130 */
131static void *
132create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
133{
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000134 size_t *sizes = g_new0(size_t, nr_iov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400135 size_t count = 0;
136 void *buf = NULL;
137 void *p;
138 int i;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200139
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 for (i = 0; i < nr_iov; i++) {
141 char *arg = argv[i];
142 int64_t len;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200143
Devin Nakamura43642b32011-07-11 11:22:16 -0400144 len = cvtnum(arg);
145 if (len < 0) {
146 printf("non-numeric length argument -- %s\n", arg);
147 goto fail;
148 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200149
Devin Nakamura43642b32011-07-11 11:22:16 -0400150 /* should be SIZE_T_MAX, but that doesn't exist */
151 if (len > INT_MAX) {
152 printf("too large length argument -- %s\n", arg);
153 goto fail;
154 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200155
Devin Nakamura43642b32011-07-11 11:22:16 -0400156 if (len & 0x1ff) {
157 printf("length argument %" PRId64
158 " is not sector aligned\n", len);
159 goto fail;
160 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200161
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 sizes[i] = len;
163 count += len;
164 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200165
Devin Nakamura43642b32011-07-11 11:22:16 -0400166 qemu_iovec_init(qiov, nr_iov);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200167
Devin Nakamura43642b32011-07-11 11:22:16 -0400168 buf = p = qemu_io_alloc(count, pattern);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200169
Devin Nakamura43642b32011-07-11 11:22:16 -0400170 for (i = 0; i < nr_iov; i++) {
171 qemu_iovec_add(qiov, p, sizes[i]);
172 p += sizes[i];
173 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200174
Kevin Wolf40a0d7c2009-11-18 10:42:59 +0100175fail:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000176 g_free(sizes);
Devin Nakamura43642b32011-07-11 11:22:16 -0400177 return buf;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200178}
179
aliguorie3aff4f2009-04-05 19:14:04 +0000180static int do_read(char *buf, int64_t offset, int count, int *total)
181{
Devin Nakamura43642b32011-07-11 11:22:16 -0400182 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000183
Devin Nakamura43642b32011-07-11 11:22:16 -0400184 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
185 if (ret < 0) {
186 return ret;
187 }
188 *total = count;
189 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000190}
191
192static int do_write(char *buf, int64_t offset, int count, int *total)
193{
Devin Nakamura43642b32011-07-11 11:22:16 -0400194 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000195
Devin Nakamura43642b32011-07-11 11:22:16 -0400196 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
197 if (ret < 0) {
198 return ret;
199 }
200 *total = count;
201 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000202}
203
204static int do_pread(char *buf, int64_t offset, int count, int *total)
205{
Devin Nakamura43642b32011-07-11 11:22:16 -0400206 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
207 if (*total < 0) {
208 return *total;
209 }
210 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000211}
212
213static int do_pwrite(char *buf, int64_t offset, int count, int *total)
214{
Devin Nakamura43642b32011-07-11 11:22:16 -0400215 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
216 if (*total < 0) {
217 return *total;
218 }
219 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000220}
221
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000222typedef struct {
223 int64_t offset;
224 int count;
225 int *total;
226 int ret;
227 bool done;
228} CoWriteZeroes;
229
230static void coroutine_fn co_write_zeroes_entry(void *opaque)
231{
232 CoWriteZeroes *data = opaque;
233
234 data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE,
235 data->count / BDRV_SECTOR_SIZE);
236 data->done = true;
237 if (data->ret < 0) {
238 *data->total = data->ret;
239 return;
240 }
241
242 *data->total = data->count;
243}
244
245static int do_co_write_zeroes(int64_t offset, int count, int *total)
246{
247 Coroutine *co;
248 CoWriteZeroes data = {
249 .offset = offset,
250 .count = count,
251 .total = total,
252 .done = false,
253 };
254
255 co = qemu_coroutine_create(co_write_zeroes_entry);
256 qemu_coroutine_enter(co, &data);
257 while (!data.done) {
258 qemu_aio_wait();
259 }
260 if (data.ret < 0) {
261 return data.ret;
262 } else {
263 return 1;
264 }
265}
266
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200267static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
268{
Devin Nakamura43642b32011-07-11 11:22:16 -0400269 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
270 if (*total < 0) {
271 return *total;
272 }
273 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200274}
275
276static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
277{
Devin Nakamura43642b32011-07-11 11:22:16 -0400278 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
279 if (*total < 0) {
280 return *total;
281 }
282 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200283}
284
aliguorie3aff4f2009-04-05 19:14:04 +0000285#define NOT_DONE 0x7fffffff
286static void aio_rw_done(void *opaque, int ret)
287{
Devin Nakamura43642b32011-07-11 11:22:16 -0400288 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000289}
290
291static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
292{
Devin Nakamura43642b32011-07-11 11:22:16 -0400293 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000294
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100295 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
296 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400297 while (async_ret == NOT_DONE) {
298 qemu_aio_wait();
299 }
aliguorie3aff4f2009-04-05 19:14:04 +0000300
Devin Nakamura43642b32011-07-11 11:22:16 -0400301 *total = qiov->size;
302 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000303}
304
305static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
306{
Devin Nakamura43642b32011-07-11 11:22:16 -0400307 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000308
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100309 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
310 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400311 while (async_ret == NOT_DONE) {
312 qemu_aio_wait();
313 }
aliguorie3aff4f2009-04-05 19:14:04 +0000314
Devin Nakamura43642b32011-07-11 11:22:16 -0400315 *total = qiov->size;
316 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000317}
318
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200319struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400320 int num_done;
321 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200322};
323
324static void multiwrite_cb(void *opaque, int ret)
325{
Devin Nakamura43642b32011-07-11 11:22:16 -0400326 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200327
Devin Nakamura43642b32011-07-11 11:22:16 -0400328 async_ret->num_done++;
329 if (ret < 0) {
330 async_ret->error = ret;
331 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200332}
333
334static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
335{
Devin Nakamura43642b32011-07-11 11:22:16 -0400336 int i, ret;
337 struct multiwrite_async_ret async_ret = {
338 .num_done = 0,
339 .error = 0,
340 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200341
Devin Nakamura43642b32011-07-11 11:22:16 -0400342 *total = 0;
343 for (i = 0; i < num_reqs; i++) {
344 reqs[i].cb = multiwrite_cb;
345 reqs[i].opaque = &async_ret;
346 *total += reqs[i].qiov->size;
347 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200348
Devin Nakamura43642b32011-07-11 11:22:16 -0400349 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
350 if (ret < 0) {
351 return ret;
352 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200353
Devin Nakamura43642b32011-07-11 11:22:16 -0400354 while (async_ret.num_done < num_reqs) {
355 qemu_aio_wait();
356 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200357
Devin Nakamura43642b32011-07-11 11:22:16 -0400358 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200359}
aliguorie3aff4f2009-04-05 19:14:04 +0000360
Devin Nakamura43642b32011-07-11 11:22:16 -0400361static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000362{
Devin Nakamura43642b32011-07-11 11:22:16 -0400363 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000364"\n"
365" reads a range of bytes from the given offset\n"
366"\n"
367" Example:\n"
368" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
369"\n"
370" Reads a segment of the currently open file, optionally dumping it to the\n"
371" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200372" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200373" -C, -- report statistics in a machine parsable format\n"
374" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000375" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000376" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100377" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200378" -s, -- start offset for pattern verification (only with -P)\n"
379" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000380"\n");
381}
382
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000383static int read_f(int argc, char **argv);
384
385static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400386 .name = "read",
387 .altname = "r",
388 .cfunc = read_f,
389 .argmin = 2,
390 .argmax = -1,
391 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
392 .oneline = "reads a number of bytes at a specified offset",
393 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000394};
395
Devin Nakamura43642b32011-07-11 11:22:16 -0400396static int read_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000397{
Devin Nakamura43642b32011-07-11 11:22:16 -0400398 struct timeval t1, t2;
399 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
400 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
401 int c, cnt;
402 char *buf;
403 int64_t offset;
404 int count;
405 /* Some compilers get confused and warn if this is not initialized. */
406 int total = 0;
407 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000408
Devin Nakamura43642b32011-07-11 11:22:16 -0400409 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
410 switch (c) {
411 case 'b':
412 bflag = 1;
413 break;
414 case 'C':
415 Cflag = 1;
416 break;
417 case 'l':
418 lflag = 1;
419 pattern_count = cvtnum(optarg);
420 if (pattern_count < 0) {
421 printf("non-numeric length argument -- %s\n", optarg);
422 return 0;
423 }
424 break;
425 case 'p':
426 pflag = 1;
427 break;
428 case 'P':
429 Pflag = 1;
430 pattern = parse_pattern(optarg);
431 if (pattern < 0) {
432 return 0;
433 }
434 break;
435 case 'q':
436 qflag = 1;
437 break;
438 case 's':
439 sflag = 1;
440 pattern_offset = cvtnum(optarg);
441 if (pattern_offset < 0) {
442 printf("non-numeric length argument -- %s\n", optarg);
443 return 0;
444 }
445 break;
446 case 'v':
447 vflag = 1;
448 break;
449 default:
450 return command_usage(&read_cmd);
451 }
452 }
aliguorie3aff4f2009-04-05 19:14:04 +0000453
Devin Nakamura43642b32011-07-11 11:22:16 -0400454 if (optind != argc - 2) {
455 return command_usage(&read_cmd);
456 }
aliguorie3aff4f2009-04-05 19:14:04 +0000457
Devin Nakamura43642b32011-07-11 11:22:16 -0400458 if (bflag && pflag) {
459 printf("-b and -p cannot be specified at the same time\n");
460 return 0;
461 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200462
Devin Nakamura43642b32011-07-11 11:22:16 -0400463 offset = cvtnum(argv[optind]);
464 if (offset < 0) {
465 printf("non-numeric length argument -- %s\n", argv[optind]);
466 return 0;
467 }
aliguorie3aff4f2009-04-05 19:14:04 +0000468
Devin Nakamura43642b32011-07-11 11:22:16 -0400469 optind++;
470 count = cvtnum(argv[optind]);
471 if (count < 0) {
472 printf("non-numeric length argument -- %s\n", argv[optind]);
473 return 0;
474 }
aliguorie3aff4f2009-04-05 19:14:04 +0000475
Kevin Wolfd9654a52009-04-23 15:11:13 +0200476 if (!Pflag && (lflag || sflag)) {
477 return command_usage(&read_cmd);
478 }
479
480 if (!lflag) {
481 pattern_count = count - pattern_offset;
482 }
483
484 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
Dong Xu Wang07f35072011-11-22 18:06:26 +0800485 printf("pattern verification range exceeds end of read data\n");
Kevin Wolfd9654a52009-04-23 15:11:13 +0200486 return 0;
487 }
488
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400489 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400490 if (offset & 0x1ff) {
491 printf("offset %" PRId64 " is not sector aligned\n",
492 offset);
493 return 0;
494 }
495 if (count & 0x1ff) {
496 printf("count %d is not sector aligned\n",
497 count);
498 return 0;
499 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400500 }
aliguorie3aff4f2009-04-05 19:14:04 +0000501
Devin Nakamura43642b32011-07-11 11:22:16 -0400502 buf = qemu_io_alloc(count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000503
Devin Nakamura43642b32011-07-11 11:22:16 -0400504 gettimeofday(&t1, NULL);
505 if (pflag) {
506 cnt = do_pread(buf, offset, count, &total);
507 } else if (bflag) {
508 cnt = do_load_vmstate(buf, offset, count, &total);
509 } else {
510 cnt = do_read(buf, offset, count, &total);
511 }
512 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000513
Devin Nakamura43642b32011-07-11 11:22:16 -0400514 if (cnt < 0) {
515 printf("read failed: %s\n", strerror(-cnt));
516 goto out;
517 }
aliguorie3aff4f2009-04-05 19:14:04 +0000518
Devin Nakamura43642b32011-07-11 11:22:16 -0400519 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000520 void *cmp_buf = g_malloc(pattern_count);
Devin Nakamura43642b32011-07-11 11:22:16 -0400521 memset(cmp_buf, pattern, pattern_count);
522 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
523 printf("Pattern verification failed at offset %"
524 PRId64 ", %d bytes\n",
525 offset + pattern_offset, pattern_count);
526 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000527 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400528 }
aliguorie3aff4f2009-04-05 19:14:04 +0000529
Devin Nakamura43642b32011-07-11 11:22:16 -0400530 if (qflag) {
531 goto out;
532 }
aliguoric48101a2009-04-18 15:36:23 +0000533
Devin Nakamura43642b32011-07-11 11:22:16 -0400534 if (vflag) {
535 dump_buffer(buf, offset, count);
536 }
aliguorie3aff4f2009-04-05 19:14:04 +0000537
Devin Nakamura43642b32011-07-11 11:22:16 -0400538 /* Finally, report back -- -C gives a parsable format */
539 t2 = tsub(t2, t1);
540 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000541
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200542out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400543 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000544
Devin Nakamura43642b32011-07-11 11:22:16 -0400545 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000546}
547
Devin Nakamura43642b32011-07-11 11:22:16 -0400548static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000549{
Devin Nakamura43642b32011-07-11 11:22:16 -0400550 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000551"\n"
552" reads a range of bytes from the given offset into multiple buffers\n"
553"\n"
554" Example:\n"
555" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
556"\n"
557" Reads a segment of the currently open file, optionally dumping it to the\n"
558" standard output stream (with -v option) for subsequent inspection.\n"
559" Uses multiple iovec buffers if more than one byte range is specified.\n"
560" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000561" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000562" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100563" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000564"\n");
565}
566
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000567static int readv_f(int argc, char **argv);
568
569static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400570 .name = "readv",
571 .cfunc = readv_f,
572 .argmin = 2,
573 .argmax = -1,
574 .args = "[-Cqv] [-P pattern ] off len [len..]",
575 .oneline = "reads a number of bytes at a specified offset",
576 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000577};
578
Devin Nakamura43642b32011-07-11 11:22:16 -0400579static int readv_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000580{
Devin Nakamura43642b32011-07-11 11:22:16 -0400581 struct timeval t1, t2;
582 int Cflag = 0, qflag = 0, vflag = 0;
583 int c, cnt;
584 char *buf;
585 int64_t offset;
586 /* Some compilers get confused and warn if this is not initialized. */
587 int total = 0;
588 int nr_iov;
589 QEMUIOVector qiov;
590 int pattern = 0;
591 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000592
Devin Nakamura43642b32011-07-11 11:22:16 -0400593 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
594 switch (c) {
595 case 'C':
596 Cflag = 1;
597 break;
598 case 'P':
599 Pflag = 1;
600 pattern = parse_pattern(optarg);
601 if (pattern < 0) {
602 return 0;
603 }
604 break;
605 case 'q':
606 qflag = 1;
607 break;
608 case 'v':
609 vflag = 1;
610 break;
611 default:
612 return command_usage(&readv_cmd);
613 }
614 }
aliguorie3aff4f2009-04-05 19:14:04 +0000615
Devin Nakamura43642b32011-07-11 11:22:16 -0400616 if (optind > argc - 2) {
617 return command_usage(&readv_cmd);
618 }
aliguorie3aff4f2009-04-05 19:14:04 +0000619
620
Devin Nakamura43642b32011-07-11 11:22:16 -0400621 offset = cvtnum(argv[optind]);
622 if (offset < 0) {
623 printf("non-numeric length argument -- %s\n", argv[optind]);
624 return 0;
625 }
626 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000627
Devin Nakamura43642b32011-07-11 11:22:16 -0400628 if (offset & 0x1ff) {
629 printf("offset %" PRId64 " is not sector aligned\n",
630 offset);
631 return 0;
632 }
aliguorie3aff4f2009-04-05 19:14:04 +0000633
Devin Nakamura43642b32011-07-11 11:22:16 -0400634 nr_iov = argc - optind;
635 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +0100636 if (buf == NULL) {
637 return 0;
638 }
aliguorie3aff4f2009-04-05 19:14:04 +0000639
Devin Nakamura43642b32011-07-11 11:22:16 -0400640 gettimeofday(&t1, NULL);
641 cnt = do_aio_readv(&qiov, offset, &total);
642 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000643
Devin Nakamura43642b32011-07-11 11:22:16 -0400644 if (cnt < 0) {
645 printf("readv failed: %s\n", strerror(-cnt));
646 goto out;
647 }
aliguorie3aff4f2009-04-05 19:14:04 +0000648
Devin Nakamura43642b32011-07-11 11:22:16 -0400649 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000650 void *cmp_buf = g_malloc(qiov.size);
Devin Nakamura43642b32011-07-11 11:22:16 -0400651 memset(cmp_buf, pattern, qiov.size);
652 if (memcmp(buf, cmp_buf, qiov.size)) {
653 printf("Pattern verification failed at offset %"
654 PRId64 ", %zd bytes\n", offset, qiov.size);
655 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000656 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400657 }
aliguoric48101a2009-04-18 15:36:23 +0000658
Devin Nakamura43642b32011-07-11 11:22:16 -0400659 if (qflag) {
660 goto out;
661 }
aliguorie3aff4f2009-04-05 19:14:04 +0000662
Devin Nakamura43642b32011-07-11 11:22:16 -0400663 if (vflag) {
664 dump_buffer(buf, offset, qiov.size);
665 }
aliguorie3aff4f2009-04-05 19:14:04 +0000666
Devin Nakamura43642b32011-07-11 11:22:16 -0400667 /* Finally, report back -- -C gives a parsable format */
668 t2 = tsub(t2, t1);
669 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000670
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200671out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400672 qemu_io_free(buf);
673 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000674}
675
Devin Nakamura43642b32011-07-11 11:22:16 -0400676static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000677{
Devin Nakamura43642b32011-07-11 11:22:16 -0400678 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000679"\n"
680" writes a range of bytes from the given offset\n"
681"\n"
682" Example:\n"
683" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
684"\n"
685" Writes into a segment of the currently open file, using a buffer\n"
686" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200687" -b, -- write to the VM state rather than the virtual disk\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000688" -p, -- use bdrv_pwrite to write the file\n"
689" -P, -- use different pattern to fill file\n"
690" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100691" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000692" -z, -- write zeroes using bdrv_co_write_zeroes\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000693"\n");
694}
695
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000696static int write_f(int argc, char **argv);
697
698static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400699 .name = "write",
700 .altname = "w",
701 .cfunc = write_f,
702 .argmin = 2,
703 .argmax = -1,
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000704 .args = "[-bCpqz] [-P pattern ] off len",
Devin Nakamura43642b32011-07-11 11:22:16 -0400705 .oneline = "writes a number of bytes at a specified offset",
706 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000707};
708
Devin Nakamura43642b32011-07-11 11:22:16 -0400709static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000710{
Devin Nakamura43642b32011-07-11 11:22:16 -0400711 struct timeval t1, t2;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000712 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400713 int c, cnt;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000714 char *buf = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400715 int64_t offset;
716 int count;
717 /* Some compilers get confused and warn if this is not initialized. */
718 int total = 0;
719 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000720
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000721 while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400722 switch (c) {
723 case 'b':
724 bflag = 1;
725 break;
726 case 'C':
727 Cflag = 1;
728 break;
729 case 'p':
730 pflag = 1;
731 break;
732 case 'P':
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000733 Pflag = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400734 pattern = parse_pattern(optarg);
735 if (pattern < 0) {
736 return 0;
737 }
738 break;
739 case 'q':
740 qflag = 1;
741 break;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000742 case 'z':
743 zflag = 1;
744 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400745 default:
746 return command_usage(&write_cmd);
747 }
748 }
aliguorie3aff4f2009-04-05 19:14:04 +0000749
Devin Nakamura43642b32011-07-11 11:22:16 -0400750 if (optind != argc - 2) {
751 return command_usage(&write_cmd);
752 }
aliguorie3aff4f2009-04-05 19:14:04 +0000753
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000754 if (bflag + pflag + zflag > 1) {
755 printf("-b, -p, or -z cannot be specified at the same time\n");
756 return 0;
757 }
758
759 if (zflag && Pflag) {
760 printf("-z and -P cannot be specified at the same time\n");
Devin Nakamura43642b32011-07-11 11:22:16 -0400761 return 0;
762 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200763
Devin Nakamura43642b32011-07-11 11:22:16 -0400764 offset = cvtnum(argv[optind]);
765 if (offset < 0) {
766 printf("non-numeric length argument -- %s\n", argv[optind]);
767 return 0;
768 }
aliguorie3aff4f2009-04-05 19:14:04 +0000769
Devin Nakamura43642b32011-07-11 11:22:16 -0400770 optind++;
771 count = cvtnum(argv[optind]);
772 if (count < 0) {
773 printf("non-numeric length argument -- %s\n", argv[optind]);
774 return 0;
775 }
aliguorie3aff4f2009-04-05 19:14:04 +0000776
Devin Nakamura43642b32011-07-11 11:22:16 -0400777 if (!pflag) {
778 if (offset & 0x1ff) {
779 printf("offset %" PRId64 " is not sector aligned\n",
780 offset);
781 return 0;
782 }
aliguorie3aff4f2009-04-05 19:14:04 +0000783
Devin Nakamura43642b32011-07-11 11:22:16 -0400784 if (count & 0x1ff) {
785 printf("count %d is not sector aligned\n",
786 count);
787 return 0;
788 }
789 }
aliguorie3aff4f2009-04-05 19:14:04 +0000790
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000791 if (!zflag) {
792 buf = qemu_io_alloc(count, pattern);
793 }
aliguorie3aff4f2009-04-05 19:14:04 +0000794
Devin Nakamura43642b32011-07-11 11:22:16 -0400795 gettimeofday(&t1, NULL);
796 if (pflag) {
797 cnt = do_pwrite(buf, offset, count, &total);
798 } else if (bflag) {
799 cnt = do_save_vmstate(buf, offset, count, &total);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000800 } else if (zflag) {
801 cnt = do_co_write_zeroes(offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400802 } else {
803 cnt = do_write(buf, offset, count, &total);
804 }
805 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000806
Devin Nakamura43642b32011-07-11 11:22:16 -0400807 if (cnt < 0) {
808 printf("write failed: %s\n", strerror(-cnt));
809 goto out;
810 }
aliguorie3aff4f2009-04-05 19:14:04 +0000811
Devin Nakamura43642b32011-07-11 11:22:16 -0400812 if (qflag) {
813 goto out;
814 }
aliguorie3aff4f2009-04-05 19:14:04 +0000815
Devin Nakamura43642b32011-07-11 11:22:16 -0400816 /* Finally, report back -- -C gives a parsable format */
817 t2 = tsub(t2, t1);
818 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000819
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200820out:
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000821 if (!zflag) {
822 qemu_io_free(buf);
823 }
aliguorie3aff4f2009-04-05 19:14:04 +0000824
Devin Nakamura43642b32011-07-11 11:22:16 -0400825 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000826}
827
aliguorie3aff4f2009-04-05 19:14:04 +0000828static void
829writev_help(void)
830{
Devin Nakamura43642b32011-07-11 11:22:16 -0400831 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000832"\n"
833" writes a range of bytes from the given offset source from multiple buffers\n"
834"\n"
835" Example:\n"
836" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
837"\n"
838" Writes into a segment of the currently open file, using a buffer\n"
839" filled with a set pattern (0xcdcdcdcd).\n"
840" -P, -- use different pattern to fill file\n"
841" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100842" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000843"\n");
844}
845
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000846static int writev_f(int argc, char **argv);
847
848static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400849 .name = "writev",
850 .cfunc = writev_f,
851 .argmin = 2,
852 .argmax = -1,
853 .args = "[-Cq] [-P pattern ] off len [len..]",
854 .oneline = "writes a number of bytes at a specified offset",
855 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000856};
857
Devin Nakamura43642b32011-07-11 11:22:16 -0400858static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000859{
Devin Nakamura43642b32011-07-11 11:22:16 -0400860 struct timeval t1, t2;
861 int Cflag = 0, qflag = 0;
862 int c, cnt;
863 char *buf;
864 int64_t offset;
865 /* Some compilers get confused and warn if this is not initialized. */
866 int total = 0;
867 int nr_iov;
868 int pattern = 0xcd;
869 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000870
Devin Nakamura43642b32011-07-11 11:22:16 -0400871 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
872 switch (c) {
873 case 'C':
874 Cflag = 1;
875 break;
876 case 'q':
877 qflag = 1;
878 break;
879 case 'P':
880 pattern = parse_pattern(optarg);
881 if (pattern < 0) {
882 return 0;
883 }
884 break;
885 default:
886 return command_usage(&writev_cmd);
887 }
888 }
aliguorie3aff4f2009-04-05 19:14:04 +0000889
Devin Nakamura43642b32011-07-11 11:22:16 -0400890 if (optind > argc - 2) {
891 return command_usage(&writev_cmd);
892 }
aliguorie3aff4f2009-04-05 19:14:04 +0000893
Devin Nakamura43642b32011-07-11 11:22:16 -0400894 offset = cvtnum(argv[optind]);
895 if (offset < 0) {
896 printf("non-numeric length argument -- %s\n", argv[optind]);
897 return 0;
898 }
899 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000900
Devin Nakamura43642b32011-07-11 11:22:16 -0400901 if (offset & 0x1ff) {
902 printf("offset %" PRId64 " is not sector aligned\n",
903 offset);
904 return 0;
905 }
aliguorie3aff4f2009-04-05 19:14:04 +0000906
Devin Nakamura43642b32011-07-11 11:22:16 -0400907 nr_iov = argc - optind;
908 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100909 if (buf == NULL) {
910 return 0;
911 }
aliguorie3aff4f2009-04-05 19:14:04 +0000912
Devin Nakamura43642b32011-07-11 11:22:16 -0400913 gettimeofday(&t1, NULL);
914 cnt = do_aio_writev(&qiov, offset, &total);
915 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000916
Devin Nakamura43642b32011-07-11 11:22:16 -0400917 if (cnt < 0) {
918 printf("writev failed: %s\n", strerror(-cnt));
919 goto out;
920 }
aliguorie3aff4f2009-04-05 19:14:04 +0000921
Devin Nakamura43642b32011-07-11 11:22:16 -0400922 if (qflag) {
923 goto out;
924 }
aliguorie3aff4f2009-04-05 19:14:04 +0000925
Devin Nakamura43642b32011-07-11 11:22:16 -0400926 /* Finally, report back -- -C gives a parsable format */
927 t2 = tsub(t2, t1);
928 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200929out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400930 qemu_io_free(buf);
931 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000932}
933
Devin Nakamura43642b32011-07-11 11:22:16 -0400934static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200935{
Devin Nakamura43642b32011-07-11 11:22:16 -0400936 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200937"\n"
938" writes a range of bytes from the given offset source from multiple buffers,\n"
939" in a batch of requests that may be merged by qemu\n"
940"\n"
941" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200942" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200943" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
944"\n"
945" Writes into a segment of the currently open file, using a buffer\n"
946" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
947" by one for each request contained in the multiwrite command.\n"
948" -P, -- use different pattern to fill file\n"
949" -C, -- report statistics in a machine parsable format\n"
950" -q, -- quiet mode, do not show I/O statistics\n"
951"\n");
952}
953
954static int multiwrite_f(int argc, char **argv);
955
956static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400957 .name = "multiwrite",
958 .cfunc = multiwrite_f,
959 .argmin = 2,
960 .argmax = -1,
961 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
962 .oneline = "issues multiple write requests at once",
963 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200964};
965
Devin Nakamura43642b32011-07-11 11:22:16 -0400966static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200967{
Devin Nakamura43642b32011-07-11 11:22:16 -0400968 struct timeval t1, t2;
969 int Cflag = 0, qflag = 0;
970 int c, cnt;
971 char **buf;
972 int64_t offset, first_offset = 0;
973 /* Some compilers get confused and warn if this is not initialized. */
974 int total = 0;
975 int nr_iov;
976 int nr_reqs;
977 int pattern = 0xcd;
978 QEMUIOVector *qiovs;
979 int i;
980 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200981
Devin Nakamura43642b32011-07-11 11:22:16 -0400982 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
983 switch (c) {
984 case 'C':
985 Cflag = 1;
986 break;
987 case 'q':
988 qflag = 1;
989 break;
990 case 'P':
991 pattern = parse_pattern(optarg);
992 if (pattern < 0) {
993 return 0;
994 }
995 break;
996 default:
997 return command_usage(&writev_cmd);
998 }
999 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001000
Devin Nakamura43642b32011-07-11 11:22:16 -04001001 if (optind > argc - 2) {
1002 return command_usage(&writev_cmd);
1003 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001004
Devin Nakamura43642b32011-07-11 11:22:16 -04001005 nr_reqs = 1;
1006 for (i = optind; i < argc; i++) {
1007 if (!strcmp(argv[i], ";")) {
1008 nr_reqs++;
1009 }
1010 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001011
Kevin Wolff2360622011-10-31 11:36:32 +01001012 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1013 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -05001014 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001015
Kevin Wolf67403db2011-10-31 11:49:21 +01001016 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001017 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001018
Devin Nakamura43642b32011-07-11 11:22:16 -04001019 /* Read the offset of the request */
1020 offset = cvtnum(argv[optind]);
1021 if (offset < 0) {
1022 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +01001023 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001024 }
1025 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001026
Devin Nakamura43642b32011-07-11 11:22:16 -04001027 if (offset & 0x1ff) {
1028 printf("offset %lld is not sector aligned\n",
1029 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +01001030 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001031 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001032
1033 if (i == 0) {
1034 first_offset = offset;
1035 }
1036
Devin Nakamura43642b32011-07-11 11:22:16 -04001037 /* Read lengths for qiov entries */
1038 for (j = optind; j < argc; j++) {
1039 if (!strcmp(argv[j], ";")) {
1040 break;
1041 }
1042 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001043
Devin Nakamura43642b32011-07-11 11:22:16 -04001044 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001045
Devin Nakamura43642b32011-07-11 11:22:16 -04001046 /* Build request */
Kevin Wolff2360622011-10-31 11:36:32 +01001047 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1048 if (buf[i] == NULL) {
1049 goto out;
1050 }
1051
Devin Nakamura43642b32011-07-11 11:22:16 -04001052 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001053 reqs[i].sector = offset >> 9;
1054 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001055
Devin Nakamura43642b32011-07-11 11:22:16 -04001056 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001057
Devin Nakamura43642b32011-07-11 11:22:16 -04001058 pattern++;
1059 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001060
Kevin Wolf67403db2011-10-31 11:49:21 +01001061 /* If there were empty requests at the end, ignore them */
1062 nr_reqs = i;
1063
Devin Nakamura43642b32011-07-11 11:22:16 -04001064 gettimeofday(&t1, NULL);
1065 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1066 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001067
Devin Nakamura43642b32011-07-11 11:22:16 -04001068 if (cnt < 0) {
1069 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1070 goto out;
1071 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001072
Devin Nakamura43642b32011-07-11 11:22:16 -04001073 if (qflag) {
1074 goto out;
1075 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001076
Devin Nakamura43642b32011-07-11 11:22:16 -04001077 /* Finally, report back -- -C gives a parsable format */
1078 t2 = tsub(t2, t1);
1079 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001080out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001081 for (i = 0; i < nr_reqs; i++) {
1082 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001083 if (reqs[i].qiov != NULL) {
1084 qemu_iovec_destroy(&qiovs[i]);
1085 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001086 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001087 g_free(buf);
1088 g_free(reqs);
1089 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001090 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001091}
1092
Christoph Hellwig95533d52009-06-20 21:10:44 +02001093struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001094 QEMUIOVector qiov;
1095 int64_t offset;
1096 char *buf;
1097 int qflag;
1098 int vflag;
1099 int Cflag;
1100 int Pflag;
1101 int pattern;
1102 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001103};
1104
Devin Nakamura43642b32011-07-11 11:22:16 -04001105static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001106{
Devin Nakamura43642b32011-07-11 11:22:16 -04001107 struct aio_ctx *ctx = opaque;
1108 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001109
Devin Nakamura43642b32011-07-11 11:22:16 -04001110 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001111
Christoph Hellwig95533d52009-06-20 21:10:44 +02001112
Devin Nakamura43642b32011-07-11 11:22:16 -04001113 if (ret < 0) {
1114 printf("aio_write failed: %s\n", strerror(-ret));
1115 goto out;
1116 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001117
Devin Nakamura43642b32011-07-11 11:22:16 -04001118 if (ctx->qflag) {
1119 goto out;
1120 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001121
Devin Nakamura43642b32011-07-11 11:22:16 -04001122 /* Finally, report back -- -C gives a parsable format */
1123 t2 = tsub(t2, ctx->t1);
1124 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1125 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001126out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001127 qemu_io_free(ctx->buf);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001128 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001129}
1130
Devin Nakamura43642b32011-07-11 11:22:16 -04001131static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001132{
Devin Nakamura43642b32011-07-11 11:22:16 -04001133 struct aio_ctx *ctx = opaque;
1134 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001135
Devin Nakamura43642b32011-07-11 11:22:16 -04001136 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001137
Devin Nakamura43642b32011-07-11 11:22:16 -04001138 if (ret < 0) {
1139 printf("readv failed: %s\n", strerror(-ret));
1140 goto out;
1141 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001142
Devin Nakamura43642b32011-07-11 11:22:16 -04001143 if (ctx->Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001144 void *cmp_buf = g_malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001145
Devin Nakamura43642b32011-07-11 11:22:16 -04001146 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1147 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1148 printf("Pattern verification failed at offset %"
1149 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1150 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001151 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -04001152 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001153
Devin Nakamura43642b32011-07-11 11:22:16 -04001154 if (ctx->qflag) {
1155 goto out;
1156 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001157
Devin Nakamura43642b32011-07-11 11:22:16 -04001158 if (ctx->vflag) {
1159 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1160 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001161
Devin Nakamura43642b32011-07-11 11:22:16 -04001162 /* Finally, report back -- -C gives a parsable format */
1163 t2 = tsub(t2, ctx->t1);
1164 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1165 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001166out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001167 qemu_io_free(ctx->buf);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001168 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001169}
1170
Devin Nakamura43642b32011-07-11 11:22:16 -04001171static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001172{
Devin Nakamura43642b32011-07-11 11:22:16 -04001173 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001174"\n"
1175" asynchronously reads a range of bytes from the given offset\n"
1176"\n"
1177" Example:\n"
1178" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1179"\n"
1180" Reads a segment of the currently open file, optionally dumping it to the\n"
1181" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001182" The read is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001183" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001184" -C, -- report statistics in a machine parsable format\n"
1185" -P, -- use a pattern to verify read data\n"
1186" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001187" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001188"\n");
1189}
1190
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001191static int aio_read_f(int argc, char **argv);
1192
1193static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001194 .name = "aio_read",
1195 .cfunc = aio_read_f,
1196 .argmin = 2,
1197 .argmax = -1,
1198 .args = "[-Cqv] [-P pattern ] off len [len..]",
1199 .oneline = "asynchronously reads a number of bytes",
1200 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001201};
1202
Devin Nakamura43642b32011-07-11 11:22:16 -04001203static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001204{
Devin Nakamura43642b32011-07-11 11:22:16 -04001205 int nr_iov, c;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001206 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001207
Devin Nakamura43642b32011-07-11 11:22:16 -04001208 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1209 switch (c) {
1210 case 'C':
1211 ctx->Cflag = 1;
1212 break;
1213 case 'P':
1214 ctx->Pflag = 1;
1215 ctx->pattern = parse_pattern(optarg);
1216 if (ctx->pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001217 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001218 return 0;
1219 }
1220 break;
1221 case 'q':
1222 ctx->qflag = 1;
1223 break;
1224 case 'v':
1225 ctx->vflag = 1;
1226 break;
1227 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001228 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001229 return command_usage(&aio_read_cmd);
1230 }
1231 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001232
Devin Nakamura43642b32011-07-11 11:22:16 -04001233 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001234 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001235 return command_usage(&aio_read_cmd);
1236 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001237
Devin Nakamura43642b32011-07-11 11:22:16 -04001238 ctx->offset = cvtnum(argv[optind]);
1239 if (ctx->offset < 0) {
1240 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001241 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001242 return 0;
1243 }
1244 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001245
Devin Nakamura43642b32011-07-11 11:22:16 -04001246 if (ctx->offset & 0x1ff) {
1247 printf("offset %" PRId64 " is not sector aligned\n",
1248 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001249 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001250 return 0;
1251 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001252
Devin Nakamura43642b32011-07-11 11:22:16 -04001253 nr_iov = argc - optind;
1254 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001255 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001256 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001257 return 0;
1258 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001259
Devin Nakamura43642b32011-07-11 11:22:16 -04001260 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001261 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1262 ctx->qiov.size >> 9, aio_read_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001263 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001264}
1265
Devin Nakamura43642b32011-07-11 11:22:16 -04001266static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001267{
Devin Nakamura43642b32011-07-11 11:22:16 -04001268 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001269"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001270" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001271" from multiple buffers\n"
1272"\n"
1273" Example:\n"
1274" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1275"\n"
1276" Writes into a segment of the currently open file, using a buffer\n"
1277" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001278" The write is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001279" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001280" -P, -- use different pattern to fill file\n"
1281" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001282" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001283"\n");
1284}
1285
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001286static int aio_write_f(int argc, char **argv);
1287
1288static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001289 .name = "aio_write",
1290 .cfunc = aio_write_f,
1291 .argmin = 2,
1292 .argmax = -1,
1293 .args = "[-Cq] [-P pattern ] off len [len..]",
1294 .oneline = "asynchronously writes a number of bytes",
1295 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001296};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001297
Devin Nakamura43642b32011-07-11 11:22:16 -04001298static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001299{
Devin Nakamura43642b32011-07-11 11:22:16 -04001300 int nr_iov, c;
1301 int pattern = 0xcd;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001302 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001303
Devin Nakamura43642b32011-07-11 11:22:16 -04001304 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1305 switch (c) {
1306 case 'C':
1307 ctx->Cflag = 1;
1308 break;
1309 case 'q':
1310 ctx->qflag = 1;
1311 break;
1312 case 'P':
1313 pattern = parse_pattern(optarg);
1314 if (pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001315 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001316 return 0;
1317 }
1318 break;
1319 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001320 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001321 return command_usage(&aio_write_cmd);
1322 }
1323 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001324
Devin Nakamura43642b32011-07-11 11:22:16 -04001325 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001326 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001327 return command_usage(&aio_write_cmd);
1328 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001329
Devin Nakamura43642b32011-07-11 11:22:16 -04001330 ctx->offset = cvtnum(argv[optind]);
1331 if (ctx->offset < 0) {
1332 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001333 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001334 return 0;
1335 }
1336 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001337
Devin Nakamura43642b32011-07-11 11:22:16 -04001338 if (ctx->offset & 0x1ff) {
1339 printf("offset %" PRId64 " is not sector aligned\n",
1340 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001341 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001342 return 0;
1343 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001344
Devin Nakamura43642b32011-07-11 11:22:16 -04001345 nr_iov = argc - optind;
1346 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001347 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001348 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001349 return 0;
1350 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001351
Devin Nakamura43642b32011-07-11 11:22:16 -04001352 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001353 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1354 ctx->qiov.size >> 9, aio_write_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001355 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001356}
1357
Devin Nakamura43642b32011-07-11 11:22:16 -04001358static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001359{
Devin Nakamura43642b32011-07-11 11:22:16 -04001360 qemu_aio_flush();
1361 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001362}
1363
1364static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001365 .name = "aio_flush",
1366 .cfunc = aio_flush_f,
1367 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001368};
1369
Devin Nakamura43642b32011-07-11 11:22:16 -04001370static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001371{
Devin Nakamura43642b32011-07-11 11:22:16 -04001372 bdrv_flush(bs);
1373 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001374}
1375
1376static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001377 .name = "flush",
1378 .altname = "f",
1379 .cfunc = flush_f,
1380 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001381};
1382
Devin Nakamura43642b32011-07-11 11:22:16 -04001383static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001384{
Devin Nakamura43642b32011-07-11 11:22:16 -04001385 int64_t offset;
1386 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001387
Devin Nakamura43642b32011-07-11 11:22:16 -04001388 offset = cvtnum(argv[1]);
1389 if (offset < 0) {
1390 printf("non-numeric truncate argument -- %s\n", argv[1]);
1391 return 0;
1392 }
aliguorie3aff4f2009-04-05 19:14:04 +00001393
Devin Nakamura43642b32011-07-11 11:22:16 -04001394 ret = bdrv_truncate(bs, offset);
1395 if (ret < 0) {
1396 printf("truncate: %s\n", strerror(-ret));
1397 return 0;
1398 }
aliguorie3aff4f2009-04-05 19:14:04 +00001399
Devin Nakamura43642b32011-07-11 11:22:16 -04001400 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001401}
1402
1403static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001404 .name = "truncate",
1405 .altname = "t",
1406 .cfunc = truncate_f,
1407 .argmin = 1,
1408 .argmax = 1,
1409 .args = "off",
1410 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001411};
1412
Devin Nakamura43642b32011-07-11 11:22:16 -04001413static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001414{
Devin Nakamura43642b32011-07-11 11:22:16 -04001415 int64_t size;
1416 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001417
Devin Nakamura43642b32011-07-11 11:22:16 -04001418 size = bdrv_getlength(bs);
1419 if (size < 0) {
1420 printf("getlength: %s\n", strerror(-size));
1421 return 0;
1422 }
aliguorie3aff4f2009-04-05 19:14:04 +00001423
Devin Nakamura43642b32011-07-11 11:22:16 -04001424 cvtstr(size, s1, sizeof(s1));
1425 printf("%s\n", s1);
1426 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001427}
1428
1429
1430static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001431 .name = "length",
1432 .altname = "l",
1433 .cfunc = length_f,
1434 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001435};
1436
1437
Devin Nakamura43642b32011-07-11 11:22:16 -04001438static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001439{
Devin Nakamura43642b32011-07-11 11:22:16 -04001440 BlockDriverInfo bdi;
1441 char s1[64], s2[64];
1442 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001443
Devin Nakamura43642b32011-07-11 11:22:16 -04001444 if (bs->drv && bs->drv->format_name) {
1445 printf("format name: %s\n", bs->drv->format_name);
1446 }
1447 if (bs->drv && bs->drv->protocol_name) {
1448 printf("format name: %s\n", bs->drv->protocol_name);
1449 }
aliguorie3aff4f2009-04-05 19:14:04 +00001450
Devin Nakamura43642b32011-07-11 11:22:16 -04001451 ret = bdrv_get_info(bs, &bdi);
1452 if (ret) {
1453 return 0;
1454 }
aliguorie3aff4f2009-04-05 19:14:04 +00001455
Devin Nakamura43642b32011-07-11 11:22:16 -04001456 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1457 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001458
Devin Nakamura43642b32011-07-11 11:22:16 -04001459 printf("cluster size: %s\n", s1);
1460 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001461
Devin Nakamura43642b32011-07-11 11:22:16 -04001462 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001463}
1464
1465
1466
1467static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001468 .name = "info",
1469 .altname = "i",
1470 .cfunc = info_f,
1471 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001472};
1473
Devin Nakamura43642b32011-07-11 11:22:16 -04001474static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001475{
Devin Nakamura43642b32011-07-11 11:22:16 -04001476 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001477"\n"
1478" discards a range of bytes from the given offset\n"
1479"\n"
1480" Example:\n"
1481" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1482"\n"
1483" Discards a segment of the currently open file.\n"
1484" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001485" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001486"\n");
1487}
1488
1489static int discard_f(int argc, char **argv);
1490
1491static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001492 .name = "discard",
1493 .altname = "d",
1494 .cfunc = discard_f,
1495 .argmin = 2,
1496 .argmax = -1,
1497 .args = "[-Cq] off len",
1498 .oneline = "discards a number of bytes at a specified offset",
1499 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001500};
1501
Devin Nakamura43642b32011-07-11 11:22:16 -04001502static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001503{
Devin Nakamura43642b32011-07-11 11:22:16 -04001504 struct timeval t1, t2;
1505 int Cflag = 0, qflag = 0;
1506 int c, ret;
1507 int64_t offset;
1508 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001509
Devin Nakamura43642b32011-07-11 11:22:16 -04001510 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1511 switch (c) {
1512 case 'C':
1513 Cflag = 1;
1514 break;
1515 case 'q':
1516 qflag = 1;
1517 break;
1518 default:
1519 return command_usage(&discard_cmd);
1520 }
1521 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001522
Devin Nakamura43642b32011-07-11 11:22:16 -04001523 if (optind != argc - 2) {
1524 return command_usage(&discard_cmd);
1525 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001526
Devin Nakamura43642b32011-07-11 11:22:16 -04001527 offset = cvtnum(argv[optind]);
1528 if (offset < 0) {
1529 printf("non-numeric length argument -- %s\n", argv[optind]);
1530 return 0;
1531 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001532
Devin Nakamura43642b32011-07-11 11:22:16 -04001533 optind++;
1534 count = cvtnum(argv[optind]);
1535 if (count < 0) {
1536 printf("non-numeric length argument -- %s\n", argv[optind]);
1537 return 0;
1538 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001539
Devin Nakamura43642b32011-07-11 11:22:16 -04001540 gettimeofday(&t1, NULL);
1541 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1542 count >> BDRV_SECTOR_BITS);
1543 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001544
Devin Nakamura43642b32011-07-11 11:22:16 -04001545 if (ret < 0) {
1546 printf("discard failed: %s\n", strerror(-ret));
1547 goto out;
1548 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001549
Devin Nakamura43642b32011-07-11 11:22:16 -04001550 /* Finally, report back -- -C gives a parsable format */
1551 if (!qflag) {
1552 t2 = tsub(t2, t1);
1553 print_report("discard", &t2, offset, count, count, 1, Cflag);
1554 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001555
1556out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001557 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001558}
1559
Devin Nakamura43642b32011-07-11 11:22:16 -04001560static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001561{
Devin Nakamura43642b32011-07-11 11:22:16 -04001562 int64_t offset;
1563 int nb_sectors, remaining;
1564 char s1[64];
1565 int num, sum_alloc;
1566 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001567
Devin Nakamura43642b32011-07-11 11:22:16 -04001568 offset = cvtnum(argv[1]);
1569 if (offset & 0x1ff) {
1570 printf("offset %" PRId64 " is not sector aligned\n",
1571 offset);
1572 return 0;
1573 }
aliguorie3aff4f2009-04-05 19:14:04 +00001574
Devin Nakamura43642b32011-07-11 11:22:16 -04001575 if (argc == 3) {
1576 nb_sectors = cvtnum(argv[2]);
1577 } else {
1578 nb_sectors = 1;
1579 }
aliguorie3aff4f2009-04-05 19:14:04 +00001580
Devin Nakamura43642b32011-07-11 11:22:16 -04001581 remaining = nb_sectors;
1582 sum_alloc = 0;
1583 while (remaining) {
1584 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1585 remaining -= num;
1586 if (ret) {
1587 sum_alloc += num;
1588 }
1589 }
aliguorie3aff4f2009-04-05 19:14:04 +00001590
Devin Nakamura43642b32011-07-11 11:22:16 -04001591 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001592
Devin Nakamura43642b32011-07-11 11:22:16 -04001593 printf("%d/%d sectors allocated at offset %s\n",
1594 sum_alloc, nb_sectors, s1);
1595 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001596}
1597
1598static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001599 .name = "alloc",
1600 .altname = "a",
1601 .argmin = 1,
1602 .argmax = 2,
1603 .cfunc = alloc_f,
1604 .args = "off [sectors]",
1605 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001606};
1607
Devin Nakamura43642b32011-07-11 11:22:16 -04001608static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001609{
Devin Nakamura43642b32011-07-11 11:22:16 -04001610 int64_t offset;
1611 int64_t nb_sectors;
1612 char s1[64];
1613 int num, num_checked;
1614 int ret;
1615 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001616
Devin Nakamura43642b32011-07-11 11:22:16 -04001617 offset = 0;
1618 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001619
Devin Nakamura43642b32011-07-11 11:22:16 -04001620 do {
1621 num_checked = MIN(nb_sectors, INT_MAX);
1622 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1623 retstr = ret ? " allocated" : "not allocated";
1624 cvtstr(offset << 9ULL, s1, sizeof(s1));
1625 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1626 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001627
Devin Nakamura43642b32011-07-11 11:22:16 -04001628 offset += num;
1629 nb_sectors -= num;
1630 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001631
Devin Nakamura43642b32011-07-11 11:22:16 -04001632 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001633}
1634
1635static const cmdinfo_t map_cmd = {
1636 .name = "map",
1637 .argmin = 0,
1638 .argmax = 0,
1639 .cfunc = map_f,
1640 .args = "",
1641 .oneline = "prints the allocated areas of a file",
1642};
1643
1644
Devin Nakamura43642b32011-07-11 11:22:16 -04001645static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001646{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001647 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001648 bs = NULL;
1649 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001650}
1651
1652static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001653 .name = "close",
1654 .altname = "c",
1655 .cfunc = close_f,
1656 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001657};
1658
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001659static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001660{
Devin Nakamura43642b32011-07-11 11:22:16 -04001661 if (bs) {
1662 fprintf(stderr, "file open already, try 'help close'\n");
1663 return 1;
1664 }
aliguorie3aff4f2009-04-05 19:14:04 +00001665
Devin Nakamura43642b32011-07-11 11:22:16 -04001666 if (growable) {
1667 if (bdrv_file_open(&bs, name, flags)) {
1668 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1669 return 1;
1670 }
1671 } else {
1672 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001673
Devin Nakamura43642b32011-07-11 11:22:16 -04001674 if (bdrv_open(bs, name, flags, NULL) < 0) {
1675 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001676 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001677 bs = NULL;
1678 return 1;
1679 }
1680 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001681
Devin Nakamura43642b32011-07-11 11:22:16 -04001682 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001683}
1684
Devin Nakamura43642b32011-07-11 11:22:16 -04001685static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001686{
Devin Nakamura43642b32011-07-11 11:22:16 -04001687 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001688"\n"
1689" opens a new file in the requested mode\n"
1690"\n"
1691" Example:\n"
1692" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1693"\n"
1694" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001695" -r, -- open file read-only\n"
1696" -s, -- use snapshot file\n"
1697" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001698" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001699"\n");
1700}
1701
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001702static int open_f(int argc, char **argv);
1703
1704static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001705 .name = "open",
1706 .altname = "o",
1707 .cfunc = open_f,
1708 .argmin = 1,
1709 .argmax = -1,
1710 .flags = CMD_NOFILE_OK,
1711 .args = "[-Crsn] [path]",
1712 .oneline = "open the file specified by path",
1713 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001714};
aliguorie3aff4f2009-04-05 19:14:04 +00001715
Devin Nakamura43642b32011-07-11 11:22:16 -04001716static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001717{
Devin Nakamura43642b32011-07-11 11:22:16 -04001718 int flags = 0;
1719 int readonly = 0;
1720 int growable = 0;
1721 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001722
Devin Nakamura43642b32011-07-11 11:22:16 -04001723 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1724 switch (c) {
1725 case 's':
1726 flags |= BDRV_O_SNAPSHOT;
1727 break;
1728 case 'n':
1729 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1730 break;
1731 case 'r':
1732 readonly = 1;
1733 break;
1734 case 'g':
1735 growable = 1;
1736 break;
1737 default:
1738 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001739 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001740 }
aliguorie3aff4f2009-04-05 19:14:04 +00001741
Devin Nakamura43642b32011-07-11 11:22:16 -04001742 if (!readonly) {
1743 flags |= BDRV_O_RDWR;
1744 }
aliguorie3aff4f2009-04-05 19:14:04 +00001745
Devin Nakamura43642b32011-07-11 11:22:16 -04001746 if (optind != argc - 1) {
1747 return command_usage(&open_cmd);
1748 }
1749
1750 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001751}
1752
Devin Nakamura43642b32011-07-11 11:22:16 -04001753static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001754{
Devin Nakamura43642b32011-07-11 11:22:16 -04001755 /* only one device allowed so far */
1756 if (index >= 1) {
1757 return 0;
1758 }
1759 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001760}
1761
Devin Nakamura43642b32011-07-11 11:22:16 -04001762static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001763{
Devin Nakamura43642b32011-07-11 11:22:16 -04001764 if (ct->flags & CMD_FLAG_GLOBAL) {
1765 return 1;
1766 }
1767 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1768 fprintf(stderr, "no file open, try 'help open'\n");
1769 return 0;
1770 }
1771 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001772}
1773
1774static void usage(const char *name)
1775{
Devin Nakamura43642b32011-07-11 11:22:16 -04001776 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001777"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001778"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001779"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001780" -c, --cmd command to execute\n"
1781" -r, --read-only export read-only\n"
1782" -s, --snapshot use snapshot file\n"
1783" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001784" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001785" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001786" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001787" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001788" -h, --help display this help and exit\n"
1789" -V, --version output version information and exit\n"
1790"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001791 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001792}
1793
1794
1795int main(int argc, char **argv)
1796{
Devin Nakamura43642b32011-07-11 11:22:16 -04001797 int readonly = 0;
1798 int growable = 0;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001799 const char *sopt = "hVc:rsnmgkT:";
Devin Nakamura43642b32011-07-11 11:22:16 -04001800 const struct option lopt[] = {
1801 { "help", 0, NULL, 'h' },
1802 { "version", 0, NULL, 'V' },
1803 { "offset", 1, NULL, 'o' },
1804 { "cmd", 1, NULL, 'c' },
1805 { "read-only", 0, NULL, 'r' },
1806 { "snapshot", 0, NULL, 's' },
1807 { "nocache", 0, NULL, 'n' },
1808 { "misalign", 0, NULL, 'm' },
1809 { "growable", 0, NULL, 'g' },
1810 { "native-aio", 0, NULL, 'k' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001811 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -04001812 { NULL, 0, NULL, 0 }
1813 };
1814 int c;
1815 int opt_index = 0;
1816 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001817
Devin Nakamura43642b32011-07-11 11:22:16 -04001818 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001819
Devin Nakamura43642b32011-07-11 11:22:16 -04001820 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1821 switch (c) {
1822 case 's':
1823 flags |= BDRV_O_SNAPSHOT;
1824 break;
1825 case 'n':
1826 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1827 break;
1828 case 'c':
1829 add_user_command(optarg);
1830 break;
1831 case 'r':
1832 readonly = 1;
1833 break;
1834 case 'm':
1835 misalign = 1;
1836 break;
1837 case 'g':
1838 growable = 1;
1839 break;
1840 case 'k':
1841 flags |= BDRV_O_NATIVE_AIO;
1842 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001843 case 'T':
1844 if (!trace_backend_init(optarg, NULL)) {
1845 exit(1); /* error message will have been printed */
1846 }
1847 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04001848 case 'V':
1849 printf("%s version %s\n", progname, VERSION);
1850 exit(0);
1851 case 'h':
1852 usage(progname);
1853 exit(0);
1854 default:
1855 usage(progname);
1856 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001857 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001858 }
aliguorie3aff4f2009-04-05 19:14:04 +00001859
Devin Nakamura43642b32011-07-11 11:22:16 -04001860 if ((argc - optind) > 1) {
1861 usage(progname);
1862 exit(1);
1863 }
aliguorie3aff4f2009-04-05 19:14:04 +00001864
Devin Nakamura43642b32011-07-11 11:22:16 -04001865 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001866
Zhi Yong Wua57d1142012-02-19 22:24:59 +08001867 qemu_init_main_loop();
1868
Devin Nakamura43642b32011-07-11 11:22:16 -04001869 /* initialize commands */
1870 quit_init();
1871 help_init();
1872 add_command(&open_cmd);
1873 add_command(&close_cmd);
1874 add_command(&read_cmd);
1875 add_command(&readv_cmd);
1876 add_command(&write_cmd);
1877 add_command(&writev_cmd);
1878 add_command(&multiwrite_cmd);
1879 add_command(&aio_read_cmd);
1880 add_command(&aio_write_cmd);
1881 add_command(&aio_flush_cmd);
1882 add_command(&flush_cmd);
1883 add_command(&truncate_cmd);
1884 add_command(&length_cmd);
1885 add_command(&info_cmd);
1886 add_command(&discard_cmd);
1887 add_command(&alloc_cmd);
1888 add_command(&map_cmd);
1889
1890 add_args_command(init_args_command);
1891 add_check_command(init_check_command);
1892
1893 /* open the device */
1894 if (!readonly) {
1895 flags |= BDRV_O_RDWR;
1896 }
1897
1898 if ((argc - optind) == 1) {
1899 openfile(argv[optind], flags, growable);
1900 }
1901 command_loop();
1902
1903 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001904 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -04001905 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001906 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04001907
1908 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001909 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001910 }
1911 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001912}