blob: 8f3b94b838c75d0fb206ac1e3a5e5d3093c05d2d [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:
Kevin Wolf9e559532012-07-02 15:13:53 +0200673 qemu_iovec_destroy(&qiov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400674 qemu_io_free(buf);
675 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000676}
677
Devin Nakamura43642b32011-07-11 11:22:16 -0400678static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000679{
Devin Nakamura43642b32011-07-11 11:22:16 -0400680 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000681"\n"
682" writes a range of bytes from the given offset\n"
683"\n"
684" Example:\n"
685" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
686"\n"
687" Writes into a segment of the currently open file, using a buffer\n"
688" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200689" -b, -- write to the VM state rather than the virtual disk\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000690" -p, -- use bdrv_pwrite to write the file\n"
691" -P, -- use different pattern to fill file\n"
692" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100693" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000694" -z, -- write zeroes using bdrv_co_write_zeroes\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000695"\n");
696}
697
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000698static int write_f(int argc, char **argv);
699
700static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400701 .name = "write",
702 .altname = "w",
703 .cfunc = write_f,
704 .argmin = 2,
705 .argmax = -1,
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000706 .args = "[-bCpqz] [-P pattern ] off len",
Devin Nakamura43642b32011-07-11 11:22:16 -0400707 .oneline = "writes a number of bytes at a specified offset",
708 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000709};
710
Devin Nakamura43642b32011-07-11 11:22:16 -0400711static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000712{
Devin Nakamura43642b32011-07-11 11:22:16 -0400713 struct timeval t1, t2;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000714 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400715 int c, cnt;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000716 char *buf = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400717 int64_t offset;
718 int count;
719 /* Some compilers get confused and warn if this is not initialized. */
720 int total = 0;
721 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000722
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000723 while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400724 switch (c) {
725 case 'b':
726 bflag = 1;
727 break;
728 case 'C':
729 Cflag = 1;
730 break;
731 case 'p':
732 pflag = 1;
733 break;
734 case 'P':
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000735 Pflag = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400736 pattern = parse_pattern(optarg);
737 if (pattern < 0) {
738 return 0;
739 }
740 break;
741 case 'q':
742 qflag = 1;
743 break;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000744 case 'z':
745 zflag = 1;
746 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400747 default:
748 return command_usage(&write_cmd);
749 }
750 }
aliguorie3aff4f2009-04-05 19:14:04 +0000751
Devin Nakamura43642b32011-07-11 11:22:16 -0400752 if (optind != argc - 2) {
753 return command_usage(&write_cmd);
754 }
aliguorie3aff4f2009-04-05 19:14:04 +0000755
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000756 if (bflag + pflag + zflag > 1) {
757 printf("-b, -p, or -z cannot be specified at the same time\n");
758 return 0;
759 }
760
761 if (zflag && Pflag) {
762 printf("-z and -P cannot be specified at the same time\n");
Devin Nakamura43642b32011-07-11 11:22:16 -0400763 return 0;
764 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200765
Devin Nakamura43642b32011-07-11 11:22:16 -0400766 offset = cvtnum(argv[optind]);
767 if (offset < 0) {
768 printf("non-numeric length argument -- %s\n", argv[optind]);
769 return 0;
770 }
aliguorie3aff4f2009-04-05 19:14:04 +0000771
Devin Nakamura43642b32011-07-11 11:22:16 -0400772 optind++;
773 count = cvtnum(argv[optind]);
774 if (count < 0) {
775 printf("non-numeric length argument -- %s\n", argv[optind]);
776 return 0;
777 }
aliguorie3aff4f2009-04-05 19:14:04 +0000778
Devin Nakamura43642b32011-07-11 11:22:16 -0400779 if (!pflag) {
780 if (offset & 0x1ff) {
781 printf("offset %" PRId64 " is not sector aligned\n",
782 offset);
783 return 0;
784 }
aliguorie3aff4f2009-04-05 19:14:04 +0000785
Devin Nakamura43642b32011-07-11 11:22:16 -0400786 if (count & 0x1ff) {
787 printf("count %d is not sector aligned\n",
788 count);
789 return 0;
790 }
791 }
aliguorie3aff4f2009-04-05 19:14:04 +0000792
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000793 if (!zflag) {
794 buf = qemu_io_alloc(count, pattern);
795 }
aliguorie3aff4f2009-04-05 19:14:04 +0000796
Devin Nakamura43642b32011-07-11 11:22:16 -0400797 gettimeofday(&t1, NULL);
798 if (pflag) {
799 cnt = do_pwrite(buf, offset, count, &total);
800 } else if (bflag) {
801 cnt = do_save_vmstate(buf, offset, count, &total);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000802 } else if (zflag) {
803 cnt = do_co_write_zeroes(offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400804 } else {
805 cnt = do_write(buf, offset, count, &total);
806 }
807 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000808
Devin Nakamura43642b32011-07-11 11:22:16 -0400809 if (cnt < 0) {
810 printf("write failed: %s\n", strerror(-cnt));
811 goto out;
812 }
aliguorie3aff4f2009-04-05 19:14:04 +0000813
Devin Nakamura43642b32011-07-11 11:22:16 -0400814 if (qflag) {
815 goto out;
816 }
aliguorie3aff4f2009-04-05 19:14:04 +0000817
Devin Nakamura43642b32011-07-11 11:22:16 -0400818 /* Finally, report back -- -C gives a parsable format */
819 t2 = tsub(t2, t1);
820 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000821
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200822out:
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000823 if (!zflag) {
824 qemu_io_free(buf);
825 }
aliguorie3aff4f2009-04-05 19:14:04 +0000826
Devin Nakamura43642b32011-07-11 11:22:16 -0400827 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000828}
829
aliguorie3aff4f2009-04-05 19:14:04 +0000830static void
831writev_help(void)
832{
Devin Nakamura43642b32011-07-11 11:22:16 -0400833 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000834"\n"
835" writes a range of bytes from the given offset source from multiple buffers\n"
836"\n"
837" Example:\n"
838" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
839"\n"
840" Writes into a segment of the currently open file, using a buffer\n"
841" filled with a set pattern (0xcdcdcdcd).\n"
842" -P, -- use different pattern to fill file\n"
843" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100844" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000845"\n");
846}
847
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000848static int writev_f(int argc, char **argv);
849
850static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400851 .name = "writev",
852 .cfunc = writev_f,
853 .argmin = 2,
854 .argmax = -1,
855 .args = "[-Cq] [-P pattern ] off len [len..]",
856 .oneline = "writes a number of bytes at a specified offset",
857 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000858};
859
Devin Nakamura43642b32011-07-11 11:22:16 -0400860static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000861{
Devin Nakamura43642b32011-07-11 11:22:16 -0400862 struct timeval t1, t2;
863 int Cflag = 0, qflag = 0;
864 int c, cnt;
865 char *buf;
866 int64_t offset;
867 /* Some compilers get confused and warn if this is not initialized. */
868 int total = 0;
869 int nr_iov;
870 int pattern = 0xcd;
871 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000872
Devin Nakamura43642b32011-07-11 11:22:16 -0400873 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
874 switch (c) {
875 case 'C':
876 Cflag = 1;
877 break;
878 case 'q':
879 qflag = 1;
880 break;
881 case 'P':
882 pattern = parse_pattern(optarg);
883 if (pattern < 0) {
884 return 0;
885 }
886 break;
887 default:
888 return command_usage(&writev_cmd);
889 }
890 }
aliguorie3aff4f2009-04-05 19:14:04 +0000891
Devin Nakamura43642b32011-07-11 11:22:16 -0400892 if (optind > argc - 2) {
893 return command_usage(&writev_cmd);
894 }
aliguorie3aff4f2009-04-05 19:14:04 +0000895
Devin Nakamura43642b32011-07-11 11:22:16 -0400896 offset = cvtnum(argv[optind]);
897 if (offset < 0) {
898 printf("non-numeric length argument -- %s\n", argv[optind]);
899 return 0;
900 }
901 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000902
Devin Nakamura43642b32011-07-11 11:22:16 -0400903 if (offset & 0x1ff) {
904 printf("offset %" PRId64 " is not sector aligned\n",
905 offset);
906 return 0;
907 }
aliguorie3aff4f2009-04-05 19:14:04 +0000908
Devin Nakamura43642b32011-07-11 11:22:16 -0400909 nr_iov = argc - optind;
910 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100911 if (buf == NULL) {
912 return 0;
913 }
aliguorie3aff4f2009-04-05 19:14:04 +0000914
Devin Nakamura43642b32011-07-11 11:22:16 -0400915 gettimeofday(&t1, NULL);
916 cnt = do_aio_writev(&qiov, offset, &total);
917 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000918
Devin Nakamura43642b32011-07-11 11:22:16 -0400919 if (cnt < 0) {
920 printf("writev failed: %s\n", strerror(-cnt));
921 goto out;
922 }
aliguorie3aff4f2009-04-05 19:14:04 +0000923
Devin Nakamura43642b32011-07-11 11:22:16 -0400924 if (qflag) {
925 goto out;
926 }
aliguorie3aff4f2009-04-05 19:14:04 +0000927
Devin Nakamura43642b32011-07-11 11:22:16 -0400928 /* Finally, report back -- -C gives a parsable format */
929 t2 = tsub(t2, t1);
930 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200931out:
Kevin Wolf9e559532012-07-02 15:13:53 +0200932 qemu_iovec_destroy(&qiov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400933 qemu_io_free(buf);
934 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000935}
936
Devin Nakamura43642b32011-07-11 11:22:16 -0400937static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200938{
Devin Nakamura43642b32011-07-11 11:22:16 -0400939 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200940"\n"
941" writes a range of bytes from the given offset source from multiple buffers,\n"
942" in a batch of requests that may be merged by qemu\n"
943"\n"
944" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200945" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200946" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
947"\n"
948" Writes into a segment of the currently open file, using a buffer\n"
949" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
950" by one for each request contained in the multiwrite command.\n"
951" -P, -- use different pattern to fill file\n"
952" -C, -- report statistics in a machine parsable format\n"
953" -q, -- quiet mode, do not show I/O statistics\n"
954"\n");
955}
956
957static int multiwrite_f(int argc, char **argv);
958
959static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400960 .name = "multiwrite",
961 .cfunc = multiwrite_f,
962 .argmin = 2,
963 .argmax = -1,
964 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
965 .oneline = "issues multiple write requests at once",
966 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200967};
968
Devin Nakamura43642b32011-07-11 11:22:16 -0400969static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200970{
Devin Nakamura43642b32011-07-11 11:22:16 -0400971 struct timeval t1, t2;
972 int Cflag = 0, qflag = 0;
973 int c, cnt;
974 char **buf;
975 int64_t offset, first_offset = 0;
976 /* Some compilers get confused and warn if this is not initialized. */
977 int total = 0;
978 int nr_iov;
979 int nr_reqs;
980 int pattern = 0xcd;
981 QEMUIOVector *qiovs;
982 int i;
983 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200984
Devin Nakamura43642b32011-07-11 11:22:16 -0400985 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
986 switch (c) {
987 case 'C':
988 Cflag = 1;
989 break;
990 case 'q':
991 qflag = 1;
992 break;
993 case 'P':
994 pattern = parse_pattern(optarg);
995 if (pattern < 0) {
996 return 0;
997 }
998 break;
999 default:
1000 return command_usage(&writev_cmd);
1001 }
1002 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001003
Devin Nakamura43642b32011-07-11 11:22:16 -04001004 if (optind > argc - 2) {
1005 return command_usage(&writev_cmd);
1006 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001007
Devin Nakamura43642b32011-07-11 11:22:16 -04001008 nr_reqs = 1;
1009 for (i = optind; i < argc; i++) {
1010 if (!strcmp(argv[i], ";")) {
1011 nr_reqs++;
1012 }
1013 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001014
Kevin Wolff2360622011-10-31 11:36:32 +01001015 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1016 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -05001017 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001018
Kevin Wolf67403db2011-10-31 11:49:21 +01001019 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001020 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001021
Devin Nakamura43642b32011-07-11 11:22:16 -04001022 /* Read the offset of the request */
1023 offset = cvtnum(argv[optind]);
1024 if (offset < 0) {
1025 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +01001026 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001027 }
1028 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001029
Devin Nakamura43642b32011-07-11 11:22:16 -04001030 if (offset & 0x1ff) {
1031 printf("offset %lld is not sector aligned\n",
1032 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +01001033 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001034 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001035
1036 if (i == 0) {
1037 first_offset = offset;
1038 }
1039
Devin Nakamura43642b32011-07-11 11:22:16 -04001040 /* Read lengths for qiov entries */
1041 for (j = optind; j < argc; j++) {
1042 if (!strcmp(argv[j], ";")) {
1043 break;
1044 }
1045 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001046
Devin Nakamura43642b32011-07-11 11:22:16 -04001047 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001048
Devin Nakamura43642b32011-07-11 11:22:16 -04001049 /* Build request */
Kevin Wolff2360622011-10-31 11:36:32 +01001050 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1051 if (buf[i] == NULL) {
1052 goto out;
1053 }
1054
Devin Nakamura43642b32011-07-11 11:22:16 -04001055 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001056 reqs[i].sector = offset >> 9;
1057 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001058
Devin Nakamura43642b32011-07-11 11:22:16 -04001059 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001060
Devin Nakamura43642b32011-07-11 11:22:16 -04001061 pattern++;
1062 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001063
Kevin Wolf67403db2011-10-31 11:49:21 +01001064 /* If there were empty requests at the end, ignore them */
1065 nr_reqs = i;
1066
Devin Nakamura43642b32011-07-11 11:22:16 -04001067 gettimeofday(&t1, NULL);
1068 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1069 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001070
Devin Nakamura43642b32011-07-11 11:22:16 -04001071 if (cnt < 0) {
1072 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1073 goto out;
1074 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001075
Devin Nakamura43642b32011-07-11 11:22:16 -04001076 if (qflag) {
1077 goto out;
1078 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001079
Devin Nakamura43642b32011-07-11 11:22:16 -04001080 /* Finally, report back -- -C gives a parsable format */
1081 t2 = tsub(t2, t1);
1082 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001083out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001084 for (i = 0; i < nr_reqs; i++) {
1085 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001086 if (reqs[i].qiov != NULL) {
1087 qemu_iovec_destroy(&qiovs[i]);
1088 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001089 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001090 g_free(buf);
1091 g_free(reqs);
1092 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001093 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001094}
1095
Christoph Hellwig95533d52009-06-20 21:10:44 +02001096struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001097 QEMUIOVector qiov;
1098 int64_t offset;
1099 char *buf;
1100 int qflag;
1101 int vflag;
1102 int Cflag;
1103 int Pflag;
1104 int pattern;
1105 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001106};
1107
Devin Nakamura43642b32011-07-11 11:22:16 -04001108static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001109{
Devin Nakamura43642b32011-07-11 11:22:16 -04001110 struct aio_ctx *ctx = opaque;
1111 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001112
Devin Nakamura43642b32011-07-11 11:22:16 -04001113 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001114
Christoph Hellwig95533d52009-06-20 21:10:44 +02001115
Devin Nakamura43642b32011-07-11 11:22:16 -04001116 if (ret < 0) {
1117 printf("aio_write failed: %s\n", strerror(-ret));
1118 goto out;
1119 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001120
Devin Nakamura43642b32011-07-11 11:22:16 -04001121 if (ctx->qflag) {
1122 goto out;
1123 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001124
Devin Nakamura43642b32011-07-11 11:22:16 -04001125 /* Finally, report back -- -C gives a parsable format */
1126 t2 = tsub(t2, ctx->t1);
1127 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1128 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001129out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001130 qemu_io_free(ctx->buf);
Kevin Wolf9e559532012-07-02 15:13:53 +02001131 qemu_iovec_destroy(&ctx->qiov);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001132 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001133}
1134
Devin Nakamura43642b32011-07-11 11:22:16 -04001135static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001136{
Devin Nakamura43642b32011-07-11 11:22:16 -04001137 struct aio_ctx *ctx = opaque;
1138 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001139
Devin Nakamura43642b32011-07-11 11:22:16 -04001140 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001141
Devin Nakamura43642b32011-07-11 11:22:16 -04001142 if (ret < 0) {
1143 printf("readv failed: %s\n", strerror(-ret));
1144 goto out;
1145 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001146
Devin Nakamura43642b32011-07-11 11:22:16 -04001147 if (ctx->Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001148 void *cmp_buf = g_malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001149
Devin Nakamura43642b32011-07-11 11:22:16 -04001150 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1151 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1152 printf("Pattern verification failed at offset %"
1153 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1154 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001155 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -04001156 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001157
Devin Nakamura43642b32011-07-11 11:22:16 -04001158 if (ctx->qflag) {
1159 goto out;
1160 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001161
Devin Nakamura43642b32011-07-11 11:22:16 -04001162 if (ctx->vflag) {
1163 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1164 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001165
Devin Nakamura43642b32011-07-11 11:22:16 -04001166 /* Finally, report back -- -C gives a parsable format */
1167 t2 = tsub(t2, ctx->t1);
1168 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1169 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001170out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001171 qemu_io_free(ctx->buf);
Kevin Wolf9e559532012-07-02 15:13:53 +02001172 qemu_iovec_destroy(&ctx->qiov);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001173 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001174}
1175
Devin Nakamura43642b32011-07-11 11:22:16 -04001176static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001177{
Devin Nakamura43642b32011-07-11 11:22:16 -04001178 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001179"\n"
1180" asynchronously reads a range of bytes from the given offset\n"
1181"\n"
1182" Example:\n"
1183" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1184"\n"
1185" Reads a segment of the currently open file, optionally dumping it to the\n"
1186" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001187" The read is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001188" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001189" -C, -- report statistics in a machine parsable format\n"
1190" -P, -- use a pattern to verify read data\n"
1191" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001192" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001193"\n");
1194}
1195
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001196static int aio_read_f(int argc, char **argv);
1197
1198static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001199 .name = "aio_read",
1200 .cfunc = aio_read_f,
1201 .argmin = 2,
1202 .argmax = -1,
1203 .args = "[-Cqv] [-P pattern ] off len [len..]",
1204 .oneline = "asynchronously reads a number of bytes",
1205 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001206};
1207
Devin Nakamura43642b32011-07-11 11:22:16 -04001208static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001209{
Devin Nakamura43642b32011-07-11 11:22:16 -04001210 int nr_iov, c;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001211 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001212
Devin Nakamura43642b32011-07-11 11:22:16 -04001213 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1214 switch (c) {
1215 case 'C':
1216 ctx->Cflag = 1;
1217 break;
1218 case 'P':
1219 ctx->Pflag = 1;
1220 ctx->pattern = parse_pattern(optarg);
1221 if (ctx->pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001222 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001223 return 0;
1224 }
1225 break;
1226 case 'q':
1227 ctx->qflag = 1;
1228 break;
1229 case 'v':
1230 ctx->vflag = 1;
1231 break;
1232 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001233 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001234 return command_usage(&aio_read_cmd);
1235 }
1236 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001237
Devin Nakamura43642b32011-07-11 11:22:16 -04001238 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001239 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001240 return command_usage(&aio_read_cmd);
1241 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001242
Devin Nakamura43642b32011-07-11 11:22:16 -04001243 ctx->offset = cvtnum(argv[optind]);
1244 if (ctx->offset < 0) {
1245 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001246 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001247 return 0;
1248 }
1249 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001250
Devin Nakamura43642b32011-07-11 11:22:16 -04001251 if (ctx->offset & 0x1ff) {
1252 printf("offset %" PRId64 " is not sector aligned\n",
1253 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001254 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001255 return 0;
1256 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001257
Devin Nakamura43642b32011-07-11 11:22:16 -04001258 nr_iov = argc - optind;
1259 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001260 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001261 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001262 return 0;
1263 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001264
Devin Nakamura43642b32011-07-11 11:22:16 -04001265 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001266 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1267 ctx->qiov.size >> 9, aio_read_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001268 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001269}
1270
Devin Nakamura43642b32011-07-11 11:22:16 -04001271static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001272{
Devin Nakamura43642b32011-07-11 11:22:16 -04001273 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001274"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001275" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001276" from multiple buffers\n"
1277"\n"
1278" Example:\n"
1279" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1280"\n"
1281" Writes into a segment of the currently open file, using a buffer\n"
1282" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001283" The write is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001284" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001285" -P, -- use different pattern to fill file\n"
1286" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001287" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001288"\n");
1289}
1290
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001291static int aio_write_f(int argc, char **argv);
1292
1293static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001294 .name = "aio_write",
1295 .cfunc = aio_write_f,
1296 .argmin = 2,
1297 .argmax = -1,
1298 .args = "[-Cq] [-P pattern ] off len [len..]",
1299 .oneline = "asynchronously writes a number of bytes",
1300 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001301};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001302
Devin Nakamura43642b32011-07-11 11:22:16 -04001303static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001304{
Devin Nakamura43642b32011-07-11 11:22:16 -04001305 int nr_iov, c;
1306 int pattern = 0xcd;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001307 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001308
Devin Nakamura43642b32011-07-11 11:22:16 -04001309 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1310 switch (c) {
1311 case 'C':
1312 ctx->Cflag = 1;
1313 break;
1314 case 'q':
1315 ctx->qflag = 1;
1316 break;
1317 case 'P':
1318 pattern = parse_pattern(optarg);
1319 if (pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001320 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001321 return 0;
1322 }
1323 break;
1324 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001325 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001326 return command_usage(&aio_write_cmd);
1327 }
1328 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001329
Devin Nakamura43642b32011-07-11 11:22:16 -04001330 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001331 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001332 return command_usage(&aio_write_cmd);
1333 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001334
Devin Nakamura43642b32011-07-11 11:22:16 -04001335 ctx->offset = cvtnum(argv[optind]);
1336 if (ctx->offset < 0) {
1337 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001338 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001339 return 0;
1340 }
1341 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001342
Devin Nakamura43642b32011-07-11 11:22:16 -04001343 if (ctx->offset & 0x1ff) {
1344 printf("offset %" PRId64 " is not sector aligned\n",
1345 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001346 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001347 return 0;
1348 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001349
Devin Nakamura43642b32011-07-11 11:22:16 -04001350 nr_iov = argc - optind;
1351 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001352 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001353 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001354 return 0;
1355 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001356
Devin Nakamura43642b32011-07-11 11:22:16 -04001357 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001358 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1359 ctx->qiov.size >> 9, aio_write_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001360 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001361}
1362
Devin Nakamura43642b32011-07-11 11:22:16 -04001363static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001364{
Devin Nakamura43642b32011-07-11 11:22:16 -04001365 qemu_aio_flush();
1366 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001367}
1368
1369static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001370 .name = "aio_flush",
1371 .cfunc = aio_flush_f,
1372 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001373};
1374
Devin Nakamura43642b32011-07-11 11:22:16 -04001375static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001376{
Devin Nakamura43642b32011-07-11 11:22:16 -04001377 bdrv_flush(bs);
1378 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001379}
1380
1381static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001382 .name = "flush",
1383 .altname = "f",
1384 .cfunc = flush_f,
1385 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001386};
1387
Devin Nakamura43642b32011-07-11 11:22:16 -04001388static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001389{
Devin Nakamura43642b32011-07-11 11:22:16 -04001390 int64_t offset;
1391 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001392
Devin Nakamura43642b32011-07-11 11:22:16 -04001393 offset = cvtnum(argv[1]);
1394 if (offset < 0) {
1395 printf("non-numeric truncate argument -- %s\n", argv[1]);
1396 return 0;
1397 }
aliguorie3aff4f2009-04-05 19:14:04 +00001398
Devin Nakamura43642b32011-07-11 11:22:16 -04001399 ret = bdrv_truncate(bs, offset);
1400 if (ret < 0) {
1401 printf("truncate: %s\n", strerror(-ret));
1402 return 0;
1403 }
aliguorie3aff4f2009-04-05 19:14:04 +00001404
Devin Nakamura43642b32011-07-11 11:22:16 -04001405 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001406}
1407
1408static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001409 .name = "truncate",
1410 .altname = "t",
1411 .cfunc = truncate_f,
1412 .argmin = 1,
1413 .argmax = 1,
1414 .args = "off",
1415 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001416};
1417
Devin Nakamura43642b32011-07-11 11:22:16 -04001418static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001419{
Devin Nakamura43642b32011-07-11 11:22:16 -04001420 int64_t size;
1421 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001422
Devin Nakamura43642b32011-07-11 11:22:16 -04001423 size = bdrv_getlength(bs);
1424 if (size < 0) {
1425 printf("getlength: %s\n", strerror(-size));
1426 return 0;
1427 }
aliguorie3aff4f2009-04-05 19:14:04 +00001428
Devin Nakamura43642b32011-07-11 11:22:16 -04001429 cvtstr(size, s1, sizeof(s1));
1430 printf("%s\n", s1);
1431 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001432}
1433
1434
1435static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001436 .name = "length",
1437 .altname = "l",
1438 .cfunc = length_f,
1439 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001440};
1441
1442
Devin Nakamura43642b32011-07-11 11:22:16 -04001443static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001444{
Devin Nakamura43642b32011-07-11 11:22:16 -04001445 BlockDriverInfo bdi;
1446 char s1[64], s2[64];
1447 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001448
Devin Nakamura43642b32011-07-11 11:22:16 -04001449 if (bs->drv && bs->drv->format_name) {
1450 printf("format name: %s\n", bs->drv->format_name);
1451 }
1452 if (bs->drv && bs->drv->protocol_name) {
1453 printf("format name: %s\n", bs->drv->protocol_name);
1454 }
aliguorie3aff4f2009-04-05 19:14:04 +00001455
Devin Nakamura43642b32011-07-11 11:22:16 -04001456 ret = bdrv_get_info(bs, &bdi);
1457 if (ret) {
1458 return 0;
1459 }
aliguorie3aff4f2009-04-05 19:14:04 +00001460
Devin Nakamura43642b32011-07-11 11:22:16 -04001461 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1462 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001463
Devin Nakamura43642b32011-07-11 11:22:16 -04001464 printf("cluster size: %s\n", s1);
1465 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001466
Devin Nakamura43642b32011-07-11 11:22:16 -04001467 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001468}
1469
1470
1471
1472static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001473 .name = "info",
1474 .altname = "i",
1475 .cfunc = info_f,
1476 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001477};
1478
Devin Nakamura43642b32011-07-11 11:22:16 -04001479static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001480{
Devin Nakamura43642b32011-07-11 11:22:16 -04001481 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001482"\n"
1483" discards a range of bytes from the given offset\n"
1484"\n"
1485" Example:\n"
1486" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1487"\n"
1488" Discards a segment of the currently open file.\n"
1489" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001490" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001491"\n");
1492}
1493
1494static int discard_f(int argc, char **argv);
1495
1496static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001497 .name = "discard",
1498 .altname = "d",
1499 .cfunc = discard_f,
1500 .argmin = 2,
1501 .argmax = -1,
1502 .args = "[-Cq] off len",
1503 .oneline = "discards a number of bytes at a specified offset",
1504 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001505};
1506
Devin Nakamura43642b32011-07-11 11:22:16 -04001507static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001508{
Devin Nakamura43642b32011-07-11 11:22:16 -04001509 struct timeval t1, t2;
1510 int Cflag = 0, qflag = 0;
1511 int c, ret;
1512 int64_t offset;
1513 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001514
Devin Nakamura43642b32011-07-11 11:22:16 -04001515 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1516 switch (c) {
1517 case 'C':
1518 Cflag = 1;
1519 break;
1520 case 'q':
1521 qflag = 1;
1522 break;
1523 default:
1524 return command_usage(&discard_cmd);
1525 }
1526 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001527
Devin Nakamura43642b32011-07-11 11:22:16 -04001528 if (optind != argc - 2) {
1529 return command_usage(&discard_cmd);
1530 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001531
Devin Nakamura43642b32011-07-11 11:22:16 -04001532 offset = cvtnum(argv[optind]);
1533 if (offset < 0) {
1534 printf("non-numeric length argument -- %s\n", argv[optind]);
1535 return 0;
1536 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001537
Devin Nakamura43642b32011-07-11 11:22:16 -04001538 optind++;
1539 count = cvtnum(argv[optind]);
1540 if (count < 0) {
1541 printf("non-numeric length argument -- %s\n", argv[optind]);
1542 return 0;
1543 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001544
Devin Nakamura43642b32011-07-11 11:22:16 -04001545 gettimeofday(&t1, NULL);
1546 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1547 count >> BDRV_SECTOR_BITS);
1548 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001549
Devin Nakamura43642b32011-07-11 11:22:16 -04001550 if (ret < 0) {
1551 printf("discard failed: %s\n", strerror(-ret));
1552 goto out;
1553 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001554
Devin Nakamura43642b32011-07-11 11:22:16 -04001555 /* Finally, report back -- -C gives a parsable format */
1556 if (!qflag) {
1557 t2 = tsub(t2, t1);
1558 print_report("discard", &t2, offset, count, count, 1, Cflag);
1559 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001560
1561out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001562 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001563}
1564
Devin Nakamura43642b32011-07-11 11:22:16 -04001565static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001566{
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001567 int64_t offset, sector_num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001568 int nb_sectors, remaining;
1569 char s1[64];
1570 int num, sum_alloc;
1571 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001572
Devin Nakamura43642b32011-07-11 11:22:16 -04001573 offset = cvtnum(argv[1]);
1574 if (offset & 0x1ff) {
1575 printf("offset %" PRId64 " is not sector aligned\n",
1576 offset);
1577 return 0;
1578 }
aliguorie3aff4f2009-04-05 19:14:04 +00001579
Devin Nakamura43642b32011-07-11 11:22:16 -04001580 if (argc == 3) {
1581 nb_sectors = cvtnum(argv[2]);
1582 } else {
1583 nb_sectors = 1;
1584 }
aliguorie3aff4f2009-04-05 19:14:04 +00001585
Devin Nakamura43642b32011-07-11 11:22:16 -04001586 remaining = nb_sectors;
1587 sum_alloc = 0;
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001588 sector_num = offset >> 9;
Devin Nakamura43642b32011-07-11 11:22:16 -04001589 while (remaining) {
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001590 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1591 sector_num += num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001592 remaining -= num;
1593 if (ret) {
1594 sum_alloc += num;
1595 }
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001596 if (num == 0) {
1597 nb_sectors -= remaining;
1598 remaining = 0;
1599 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001600 }
aliguorie3aff4f2009-04-05 19:14:04 +00001601
Devin Nakamura43642b32011-07-11 11:22:16 -04001602 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001603
Devin Nakamura43642b32011-07-11 11:22:16 -04001604 printf("%d/%d sectors allocated at offset %s\n",
1605 sum_alloc, nb_sectors, s1);
1606 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001607}
1608
1609static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001610 .name = "alloc",
1611 .altname = "a",
1612 .argmin = 1,
1613 .argmax = 2,
1614 .cfunc = alloc_f,
1615 .args = "off [sectors]",
1616 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001617};
1618
Devin Nakamura43642b32011-07-11 11:22:16 -04001619static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001620{
Devin Nakamura43642b32011-07-11 11:22:16 -04001621 int64_t offset;
1622 int64_t nb_sectors;
1623 char s1[64];
1624 int num, num_checked;
1625 int ret;
1626 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001627
Devin Nakamura43642b32011-07-11 11:22:16 -04001628 offset = 0;
1629 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001630
Devin Nakamura43642b32011-07-11 11:22:16 -04001631 do {
1632 num_checked = MIN(nb_sectors, INT_MAX);
1633 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1634 retstr = ret ? " allocated" : "not allocated";
1635 cvtstr(offset << 9ULL, s1, sizeof(s1));
1636 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1637 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001638
Devin Nakamura43642b32011-07-11 11:22:16 -04001639 offset += num;
1640 nb_sectors -= num;
1641 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001642
Devin Nakamura43642b32011-07-11 11:22:16 -04001643 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001644}
1645
1646static const cmdinfo_t map_cmd = {
1647 .name = "map",
1648 .argmin = 0,
1649 .argmax = 0,
1650 .cfunc = map_f,
1651 .args = "",
1652 .oneline = "prints the allocated areas of a file",
1653};
1654
1655
Devin Nakamura43642b32011-07-11 11:22:16 -04001656static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001657{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001658 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001659 bs = NULL;
1660 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001661}
1662
1663static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001664 .name = "close",
1665 .altname = "c",
1666 .cfunc = close_f,
1667 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001668};
1669
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001670static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001671{
Devin Nakamura43642b32011-07-11 11:22:16 -04001672 if (bs) {
1673 fprintf(stderr, "file open already, try 'help close'\n");
1674 return 1;
1675 }
aliguorie3aff4f2009-04-05 19:14:04 +00001676
Devin Nakamura43642b32011-07-11 11:22:16 -04001677 if (growable) {
1678 if (bdrv_file_open(&bs, name, flags)) {
1679 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1680 return 1;
1681 }
1682 } else {
1683 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001684
Devin Nakamura43642b32011-07-11 11:22:16 -04001685 if (bdrv_open(bs, name, flags, NULL) < 0) {
1686 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001687 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001688 bs = NULL;
1689 return 1;
1690 }
1691 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001692
Devin Nakamura43642b32011-07-11 11:22:16 -04001693 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001694}
1695
Devin Nakamura43642b32011-07-11 11:22:16 -04001696static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001697{
Devin Nakamura43642b32011-07-11 11:22:16 -04001698 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001699"\n"
1700" opens a new file in the requested mode\n"
1701"\n"
1702" Example:\n"
1703" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1704"\n"
1705" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001706" -r, -- open file read-only\n"
1707" -s, -- use snapshot file\n"
1708" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001709" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001710"\n");
1711}
1712
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001713static int open_f(int argc, char **argv);
1714
1715static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001716 .name = "open",
1717 .altname = "o",
1718 .cfunc = open_f,
1719 .argmin = 1,
1720 .argmax = -1,
1721 .flags = CMD_NOFILE_OK,
1722 .args = "[-Crsn] [path]",
1723 .oneline = "open the file specified by path",
1724 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001725};
aliguorie3aff4f2009-04-05 19:14:04 +00001726
Devin Nakamura43642b32011-07-11 11:22:16 -04001727static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001728{
Devin Nakamura43642b32011-07-11 11:22:16 -04001729 int flags = 0;
1730 int readonly = 0;
1731 int growable = 0;
1732 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001733
Devin Nakamura43642b32011-07-11 11:22:16 -04001734 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1735 switch (c) {
1736 case 's':
1737 flags |= BDRV_O_SNAPSHOT;
1738 break;
1739 case 'n':
1740 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1741 break;
1742 case 'r':
1743 readonly = 1;
1744 break;
1745 case 'g':
1746 growable = 1;
1747 break;
1748 default:
1749 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001750 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001751 }
aliguorie3aff4f2009-04-05 19:14:04 +00001752
Devin Nakamura43642b32011-07-11 11:22:16 -04001753 if (!readonly) {
1754 flags |= BDRV_O_RDWR;
1755 }
aliguorie3aff4f2009-04-05 19:14:04 +00001756
Devin Nakamura43642b32011-07-11 11:22:16 -04001757 if (optind != argc - 1) {
1758 return command_usage(&open_cmd);
1759 }
1760
1761 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001762}
1763
Devin Nakamura43642b32011-07-11 11:22:16 -04001764static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001765{
Devin Nakamura43642b32011-07-11 11:22:16 -04001766 /* only one device allowed so far */
1767 if (index >= 1) {
1768 return 0;
1769 }
1770 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001771}
1772
Devin Nakamura43642b32011-07-11 11:22:16 -04001773static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001774{
Devin Nakamura43642b32011-07-11 11:22:16 -04001775 if (ct->flags & CMD_FLAG_GLOBAL) {
1776 return 1;
1777 }
1778 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1779 fprintf(stderr, "no file open, try 'help open'\n");
1780 return 0;
1781 }
1782 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001783}
1784
1785static void usage(const char *name)
1786{
Devin Nakamura43642b32011-07-11 11:22:16 -04001787 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001788"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001789"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001790"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001791" -c, --cmd command to execute\n"
1792" -r, --read-only export read-only\n"
1793" -s, --snapshot use snapshot file\n"
1794" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001795" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001796" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001797" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +02001798" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001799" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001800" -h, --help display this help and exit\n"
1801" -V, --version output version information and exit\n"
1802"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001803 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001804}
1805
1806
1807int main(int argc, char **argv)
1808{
Devin Nakamura43642b32011-07-11 11:22:16 -04001809 int readonly = 0;
1810 int growable = 0;
Kevin Wolf592fa072012-04-18 12:07:39 +02001811 const char *sopt = "hVc:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -04001812 const struct option lopt[] = {
1813 { "help", 0, NULL, 'h' },
1814 { "version", 0, NULL, 'V' },
1815 { "offset", 1, NULL, 'o' },
1816 { "cmd", 1, NULL, 'c' },
1817 { "read-only", 0, NULL, 'r' },
1818 { "snapshot", 0, NULL, 's' },
1819 { "nocache", 0, NULL, 'n' },
1820 { "misalign", 0, NULL, 'm' },
1821 { "growable", 0, NULL, 'g' },
1822 { "native-aio", 0, NULL, 'k' },
Kevin Wolf592fa072012-04-18 12:07:39 +02001823 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001824 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -04001825 { NULL, 0, NULL, 0 }
1826 };
1827 int c;
1828 int opt_index = 0;
1829 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001830
Devin Nakamura43642b32011-07-11 11:22:16 -04001831 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001832
Devin Nakamura43642b32011-07-11 11:22:16 -04001833 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1834 switch (c) {
1835 case 's':
1836 flags |= BDRV_O_SNAPSHOT;
1837 break;
1838 case 'n':
1839 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1840 break;
1841 case 'c':
1842 add_user_command(optarg);
1843 break;
1844 case 'r':
1845 readonly = 1;
1846 break;
1847 case 'm':
1848 misalign = 1;
1849 break;
1850 case 'g':
1851 growable = 1;
1852 break;
1853 case 'k':
1854 flags |= BDRV_O_NATIVE_AIO;
1855 break;
Kevin Wolf592fa072012-04-18 12:07:39 +02001856 case 't':
1857 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
1858 error_report("Invalid cache option: %s", optarg);
1859 exit(1);
1860 }
1861 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001862 case 'T':
1863 if (!trace_backend_init(optarg, NULL)) {
1864 exit(1); /* error message will have been printed */
1865 }
1866 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04001867 case 'V':
1868 printf("%s version %s\n", progname, VERSION);
1869 exit(0);
1870 case 'h':
1871 usage(progname);
1872 exit(0);
1873 default:
1874 usage(progname);
1875 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001876 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001877 }
aliguorie3aff4f2009-04-05 19:14:04 +00001878
Devin Nakamura43642b32011-07-11 11:22:16 -04001879 if ((argc - optind) > 1) {
1880 usage(progname);
1881 exit(1);
1882 }
aliguorie3aff4f2009-04-05 19:14:04 +00001883
Devin Nakamura43642b32011-07-11 11:22:16 -04001884 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001885
Zhi Yong Wua57d1142012-02-19 22:24:59 +08001886 qemu_init_main_loop();
1887
Devin Nakamura43642b32011-07-11 11:22:16 -04001888 /* initialize commands */
1889 quit_init();
1890 help_init();
1891 add_command(&open_cmd);
1892 add_command(&close_cmd);
1893 add_command(&read_cmd);
1894 add_command(&readv_cmd);
1895 add_command(&write_cmd);
1896 add_command(&writev_cmd);
1897 add_command(&multiwrite_cmd);
1898 add_command(&aio_read_cmd);
1899 add_command(&aio_write_cmd);
1900 add_command(&aio_flush_cmd);
1901 add_command(&flush_cmd);
1902 add_command(&truncate_cmd);
1903 add_command(&length_cmd);
1904 add_command(&info_cmd);
1905 add_command(&discard_cmd);
1906 add_command(&alloc_cmd);
1907 add_command(&map_cmd);
1908
1909 add_args_command(init_args_command);
1910 add_check_command(init_check_command);
1911
1912 /* open the device */
1913 if (!readonly) {
1914 flags |= BDRV_O_RDWR;
1915 }
1916
1917 if ((argc - optind) == 1) {
1918 openfile(argv[optind], flags, growable);
1919 }
1920 command_loop();
1921
1922 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001923 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -04001924 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001925 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04001926
1927 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001928 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001929 }
1930 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001931}