blob: 31895305f107e224a8e5f86c0a6e2d5dfcababba [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"
20
21#define VERSION "0.0.1"
22
Devin Nakamura43642b32011-07-11 11:22:16 -040023#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000024
25char *progname;
26static BlockDriverState *bs;
27
28static int misalign;
29
30/*
Christoph Hellwigcf070d72009-07-20 01:19:25 +020031 * Parse the pattern argument to various sub-commands.
32 *
33 * Because the pattern is used as an argument to memset it must evaluate
34 * to an unsigned integer that fits into a single byte.
35 */
36static int parse_pattern(const char *arg)
37{
Devin Nakamura43642b32011-07-11 11:22:16 -040038 char *endptr = NULL;
39 long pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020040
Devin Nakamura43642b32011-07-11 11:22:16 -040041 pattern = strtol(arg, &endptr, 0);
42 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
43 printf("%s is not a valid pattern byte\n", arg);
44 return -1;
45 }
Christoph Hellwigcf070d72009-07-20 01:19:25 +020046
Devin Nakamura43642b32011-07-11 11:22:16 -040047 return pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020048}
49
50/*
aliguorie3aff4f2009-04-05 19:14:04 +000051 * Memory allocation helpers.
52 *
53 * Make sure memory is aligned by default, or purposefully misaligned if
54 * that is specified on the command line.
55 */
56
Devin Nakamura43642b32011-07-11 11:22:16 -040057#define MISALIGN_OFFSET 16
aliguorie3aff4f2009-04-05 19:14:04 +000058static void *qemu_io_alloc(size_t len, int pattern)
59{
Devin Nakamura43642b32011-07-11 11:22:16 -040060 void *buf;
aliguorie3aff4f2009-04-05 19:14:04 +000061
Devin Nakamura43642b32011-07-11 11:22:16 -040062 if (misalign) {
63 len += MISALIGN_OFFSET;
64 }
65 buf = qemu_blockalign(bs, len);
66 memset(buf, pattern, len);
67 if (misalign) {
68 buf += MISALIGN_OFFSET;
69 }
70 return buf;
aliguorie3aff4f2009-04-05 19:14:04 +000071}
72
73static void qemu_io_free(void *p)
74{
Devin Nakamura43642b32011-07-11 11:22:16 -040075 if (misalign) {
76 p -= MISALIGN_OFFSET;
77 }
78 qemu_vfree(p);
aliguorie3aff4f2009-04-05 19:14:04 +000079}
80
Devin Nakamura43642b32011-07-11 11:22:16 -040081static void dump_buffer(const void *buffer, int64_t offset, int len)
aliguorie3aff4f2009-04-05 19:14:04 +000082{
Devin Nakamura43642b32011-07-11 11:22:16 -040083 int i, j;
84 const uint8_t *p;
aliguorie3aff4f2009-04-05 19:14:04 +000085
Devin Nakamura43642b32011-07-11 11:22:16 -040086 for (i = 0, p = buffer; i < len; i += 16) {
87 const uint8_t *s = p;
aliguorie3aff4f2009-04-05 19:14:04 +000088
Devin Nakamura43642b32011-07-11 11:22:16 -040089 printf("%08" PRIx64 ": ", offset + i);
90 for (j = 0; j < 16 && i + j < len; j++, p++) {
91 printf("%02x ", *p);
92 }
93 printf(" ");
94 for (j = 0; j < 16 && i + j < len; j++, s++) {
95 if (isalnum(*s)) {
96 printf("%c", *s);
97 } else {
98 printf(".");
99 }
100 }
101 printf("\n");
102 }
aliguorie3aff4f2009-04-05 19:14:04 +0000103}
104
Devin Nakamura43642b32011-07-11 11:22:16 -0400105static void print_report(const char *op, struct timeval *t, int64_t offset,
106 int count, int total, int cnt, int Cflag)
aliguorie3aff4f2009-04-05 19:14:04 +0000107{
Devin Nakamura43642b32011-07-11 11:22:16 -0400108 char s1[64], s2[64], ts[64];
aliguorie3aff4f2009-04-05 19:14:04 +0000109
Devin Nakamura43642b32011-07-11 11:22:16 -0400110 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
111 if (!Cflag) {
112 cvtstr((double)total, s1, sizeof(s1));
113 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
114 printf("%s %d/%d bytes at offset %" PRId64 "\n",
115 op, total, count, offset);
116 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
117 s1, cnt, ts, s2, tdiv((double)cnt, *t));
118 } else {/* bytes,ops,time,bytes/sec,ops/sec */
119 printf("%d,%d,%s,%.3f,%.3f\n",
120 total, cnt, ts,
121 tdiv((double)total, *t),
122 tdiv((double)cnt, *t));
123 }
aliguorie3aff4f2009-04-05 19:14:04 +0000124}
125
Christoph Hellwigcf572982009-07-10 13:33:42 +0200126/*
127 * Parse multiple length statements for vectored I/O, and construct an I/O
128 * vector matching it.
129 */
130static void *
131create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
132{
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000133 size_t *sizes = g_new0(size_t, nr_iov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400134 size_t count = 0;
135 void *buf = NULL;
136 void *p;
137 int i;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200138
Devin Nakamura43642b32011-07-11 11:22:16 -0400139 for (i = 0; i < nr_iov; i++) {
140 char *arg = argv[i];
141 int64_t len;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200142
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 len = cvtnum(arg);
144 if (len < 0) {
145 printf("non-numeric length argument -- %s\n", arg);
146 goto fail;
147 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200148
Devin Nakamura43642b32011-07-11 11:22:16 -0400149 /* should be SIZE_T_MAX, but that doesn't exist */
150 if (len > INT_MAX) {
151 printf("too large length argument -- %s\n", arg);
152 goto fail;
153 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200154
Devin Nakamura43642b32011-07-11 11:22:16 -0400155 if (len & 0x1ff) {
156 printf("length argument %" PRId64
157 " is not sector aligned\n", len);
158 goto fail;
159 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200160
Devin Nakamura43642b32011-07-11 11:22:16 -0400161 sizes[i] = len;
162 count += len;
163 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200164
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 qemu_iovec_init(qiov, nr_iov);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200166
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 buf = p = qemu_io_alloc(count, pattern);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200168
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 for (i = 0; i < nr_iov; i++) {
170 qemu_iovec_add(qiov, p, sizes[i]);
171 p += sizes[i];
172 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200173
Kevin Wolf40a0d7c2009-11-18 10:42:59 +0100174fail:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000175 g_free(sizes);
Devin Nakamura43642b32011-07-11 11:22:16 -0400176 return buf;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200177}
178
aliguorie3aff4f2009-04-05 19:14:04 +0000179static int do_read(char *buf, int64_t offset, int count, int *total)
180{
Devin Nakamura43642b32011-07-11 11:22:16 -0400181 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000182
Devin Nakamura43642b32011-07-11 11:22:16 -0400183 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
184 if (ret < 0) {
185 return ret;
186 }
187 *total = count;
188 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000189}
190
191static int do_write(char *buf, int64_t offset, int count, int *total)
192{
Devin Nakamura43642b32011-07-11 11:22:16 -0400193 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000194
Devin Nakamura43642b32011-07-11 11:22:16 -0400195 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
196 if (ret < 0) {
197 return ret;
198 }
199 *total = count;
200 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000201}
202
203static int do_pread(char *buf, int64_t offset, int count, int *total)
204{
Devin Nakamura43642b32011-07-11 11:22:16 -0400205 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
206 if (*total < 0) {
207 return *total;
208 }
209 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000210}
211
212static int do_pwrite(char *buf, int64_t offset, int count, int *total)
213{
Devin Nakamura43642b32011-07-11 11:22:16 -0400214 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
215 if (*total < 0) {
216 return *total;
217 }
218 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000219}
220
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000221typedef struct {
222 int64_t offset;
223 int count;
224 int *total;
225 int ret;
226 bool done;
227} CoWriteZeroes;
228
229static void coroutine_fn co_write_zeroes_entry(void *opaque)
230{
231 CoWriteZeroes *data = opaque;
232
233 data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE,
234 data->count / BDRV_SECTOR_SIZE);
235 data->done = true;
236 if (data->ret < 0) {
237 *data->total = data->ret;
238 return;
239 }
240
241 *data->total = data->count;
242}
243
244static int do_co_write_zeroes(int64_t offset, int count, int *total)
245{
246 Coroutine *co;
247 CoWriteZeroes data = {
248 .offset = offset,
249 .count = count,
250 .total = total,
251 .done = false,
252 };
253
254 co = qemu_coroutine_create(co_write_zeroes_entry);
255 qemu_coroutine_enter(co, &data);
256 while (!data.done) {
257 qemu_aio_wait();
258 }
259 if (data.ret < 0) {
260 return data.ret;
261 } else {
262 return 1;
263 }
264}
265
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200266static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
267{
Devin Nakamura43642b32011-07-11 11:22:16 -0400268 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
269 if (*total < 0) {
270 return *total;
271 }
272 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200273}
274
275static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
276{
Devin Nakamura43642b32011-07-11 11:22:16 -0400277 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
278 if (*total < 0) {
279 return *total;
280 }
281 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200282}
283
aliguorie3aff4f2009-04-05 19:14:04 +0000284#define NOT_DONE 0x7fffffff
285static void aio_rw_done(void *opaque, int ret)
286{
Devin Nakamura43642b32011-07-11 11:22:16 -0400287 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000288}
289
290static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
291{
Devin Nakamura43642b32011-07-11 11:22:16 -0400292 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000293
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100294 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
295 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400296 while (async_ret == NOT_DONE) {
297 qemu_aio_wait();
298 }
aliguorie3aff4f2009-04-05 19:14:04 +0000299
Devin Nakamura43642b32011-07-11 11:22:16 -0400300 *total = qiov->size;
301 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000302}
303
304static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
305{
Devin Nakamura43642b32011-07-11 11:22:16 -0400306 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000307
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100308 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
309 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400310 while (async_ret == NOT_DONE) {
311 qemu_aio_wait();
312 }
aliguorie3aff4f2009-04-05 19:14:04 +0000313
Devin Nakamura43642b32011-07-11 11:22:16 -0400314 *total = qiov->size;
315 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000316}
317
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200318struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400319 int num_done;
320 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200321};
322
323static void multiwrite_cb(void *opaque, int ret)
324{
Devin Nakamura43642b32011-07-11 11:22:16 -0400325 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200326
Devin Nakamura43642b32011-07-11 11:22:16 -0400327 async_ret->num_done++;
328 if (ret < 0) {
329 async_ret->error = ret;
330 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200331}
332
333static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
334{
Devin Nakamura43642b32011-07-11 11:22:16 -0400335 int i, ret;
336 struct multiwrite_async_ret async_ret = {
337 .num_done = 0,
338 .error = 0,
339 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200340
Devin Nakamura43642b32011-07-11 11:22:16 -0400341 *total = 0;
342 for (i = 0; i < num_reqs; i++) {
343 reqs[i].cb = multiwrite_cb;
344 reqs[i].opaque = &async_ret;
345 *total += reqs[i].qiov->size;
346 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200347
Devin Nakamura43642b32011-07-11 11:22:16 -0400348 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
349 if (ret < 0) {
350 return ret;
351 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200352
Devin Nakamura43642b32011-07-11 11:22:16 -0400353 while (async_ret.num_done < num_reqs) {
354 qemu_aio_wait();
355 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200356
Devin Nakamura43642b32011-07-11 11:22:16 -0400357 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200358}
aliguorie3aff4f2009-04-05 19:14:04 +0000359
Devin Nakamura43642b32011-07-11 11:22:16 -0400360static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000361{
Devin Nakamura43642b32011-07-11 11:22:16 -0400362 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000363"\n"
364" reads a range of bytes from the given offset\n"
365"\n"
366" Example:\n"
367" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
368"\n"
369" Reads a segment of the currently open file, optionally dumping it to the\n"
370" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200371" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200372" -C, -- report statistics in a machine parsable format\n"
373" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000374" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000375" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100376" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200377" -s, -- start offset for pattern verification (only with -P)\n"
378" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000379"\n");
380}
381
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000382static int read_f(int argc, char **argv);
383
384static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400385 .name = "read",
386 .altname = "r",
387 .cfunc = read_f,
388 .argmin = 2,
389 .argmax = -1,
390 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
391 .oneline = "reads a number of bytes at a specified offset",
392 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000393};
394
Devin Nakamura43642b32011-07-11 11:22:16 -0400395static int read_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000396{
Devin Nakamura43642b32011-07-11 11:22:16 -0400397 struct timeval t1, t2;
398 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
399 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
400 int c, cnt;
401 char *buf;
402 int64_t offset;
403 int count;
404 /* Some compilers get confused and warn if this is not initialized. */
405 int total = 0;
406 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000407
Devin Nakamura43642b32011-07-11 11:22:16 -0400408 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
409 switch (c) {
410 case 'b':
411 bflag = 1;
412 break;
413 case 'C':
414 Cflag = 1;
415 break;
416 case 'l':
417 lflag = 1;
418 pattern_count = cvtnum(optarg);
419 if (pattern_count < 0) {
420 printf("non-numeric length argument -- %s\n", optarg);
421 return 0;
422 }
423 break;
424 case 'p':
425 pflag = 1;
426 break;
427 case 'P':
428 Pflag = 1;
429 pattern = parse_pattern(optarg);
430 if (pattern < 0) {
431 return 0;
432 }
433 break;
434 case 'q':
435 qflag = 1;
436 break;
437 case 's':
438 sflag = 1;
439 pattern_offset = cvtnum(optarg);
440 if (pattern_offset < 0) {
441 printf("non-numeric length argument -- %s\n", optarg);
442 return 0;
443 }
444 break;
445 case 'v':
446 vflag = 1;
447 break;
448 default:
449 return command_usage(&read_cmd);
450 }
451 }
aliguorie3aff4f2009-04-05 19:14:04 +0000452
Devin Nakamura43642b32011-07-11 11:22:16 -0400453 if (optind != argc - 2) {
454 return command_usage(&read_cmd);
455 }
aliguorie3aff4f2009-04-05 19:14:04 +0000456
Devin Nakamura43642b32011-07-11 11:22:16 -0400457 if (bflag && pflag) {
458 printf("-b and -p cannot be specified at the same time\n");
459 return 0;
460 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200461
Devin Nakamura43642b32011-07-11 11:22:16 -0400462 offset = cvtnum(argv[optind]);
463 if (offset < 0) {
464 printf("non-numeric length argument -- %s\n", argv[optind]);
465 return 0;
466 }
aliguorie3aff4f2009-04-05 19:14:04 +0000467
Devin Nakamura43642b32011-07-11 11:22:16 -0400468 optind++;
469 count = cvtnum(argv[optind]);
470 if (count < 0) {
471 printf("non-numeric length argument -- %s\n", argv[optind]);
472 return 0;
473 }
aliguorie3aff4f2009-04-05 19:14:04 +0000474
Kevin Wolfd9654a52009-04-23 15:11:13 +0200475 if (!Pflag && (lflag || sflag)) {
476 return command_usage(&read_cmd);
477 }
478
479 if (!lflag) {
480 pattern_count = count - pattern_offset;
481 }
482
483 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
Dong Xu Wang07f35072011-11-22 18:06:26 +0800484 printf("pattern verification range exceeds end of read data\n");
Kevin Wolfd9654a52009-04-23 15:11:13 +0200485 return 0;
486 }
487
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400488 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400489 if (offset & 0x1ff) {
490 printf("offset %" PRId64 " is not sector aligned\n",
491 offset);
492 return 0;
493 }
494 if (count & 0x1ff) {
495 printf("count %d is not sector aligned\n",
496 count);
497 return 0;
498 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400499 }
aliguorie3aff4f2009-04-05 19:14:04 +0000500
Devin Nakamura43642b32011-07-11 11:22:16 -0400501 buf = qemu_io_alloc(count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000502
Devin Nakamura43642b32011-07-11 11:22:16 -0400503 gettimeofday(&t1, NULL);
504 if (pflag) {
505 cnt = do_pread(buf, offset, count, &total);
506 } else if (bflag) {
507 cnt = do_load_vmstate(buf, offset, count, &total);
508 } else {
509 cnt = do_read(buf, offset, count, &total);
510 }
511 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000512
Devin Nakamura43642b32011-07-11 11:22:16 -0400513 if (cnt < 0) {
514 printf("read failed: %s\n", strerror(-cnt));
515 goto out;
516 }
aliguorie3aff4f2009-04-05 19:14:04 +0000517
Devin Nakamura43642b32011-07-11 11:22:16 -0400518 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000519 void *cmp_buf = g_malloc(pattern_count);
Devin Nakamura43642b32011-07-11 11:22:16 -0400520 memset(cmp_buf, pattern, pattern_count);
521 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
522 printf("Pattern verification failed at offset %"
523 PRId64 ", %d bytes\n",
524 offset + pattern_offset, pattern_count);
525 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000526 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400527 }
aliguorie3aff4f2009-04-05 19:14:04 +0000528
Devin Nakamura43642b32011-07-11 11:22:16 -0400529 if (qflag) {
530 goto out;
531 }
aliguoric48101a2009-04-18 15:36:23 +0000532
Devin Nakamura43642b32011-07-11 11:22:16 -0400533 if (vflag) {
534 dump_buffer(buf, offset, count);
535 }
aliguorie3aff4f2009-04-05 19:14:04 +0000536
Devin Nakamura43642b32011-07-11 11:22:16 -0400537 /* Finally, report back -- -C gives a parsable format */
538 t2 = tsub(t2, t1);
539 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000540
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200541out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400542 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000543
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000545}
546
Devin Nakamura43642b32011-07-11 11:22:16 -0400547static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000548{
Devin Nakamura43642b32011-07-11 11:22:16 -0400549 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000550"\n"
551" reads a range of bytes from the given offset into multiple buffers\n"
552"\n"
553" Example:\n"
554" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
555"\n"
556" Reads a segment of the currently open file, optionally dumping it to the\n"
557" standard output stream (with -v option) for subsequent inspection.\n"
558" Uses multiple iovec buffers if more than one byte range is specified.\n"
559" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000560" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000561" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100562" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000563"\n");
564}
565
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000566static int readv_f(int argc, char **argv);
567
568static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400569 .name = "readv",
570 .cfunc = readv_f,
571 .argmin = 2,
572 .argmax = -1,
573 .args = "[-Cqv] [-P pattern ] off len [len..]",
574 .oneline = "reads a number of bytes at a specified offset",
575 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000576};
577
Devin Nakamura43642b32011-07-11 11:22:16 -0400578static int readv_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000579{
Devin Nakamura43642b32011-07-11 11:22:16 -0400580 struct timeval t1, t2;
581 int Cflag = 0, qflag = 0, vflag = 0;
582 int c, cnt;
583 char *buf;
584 int64_t offset;
585 /* Some compilers get confused and warn if this is not initialized. */
586 int total = 0;
587 int nr_iov;
588 QEMUIOVector qiov;
589 int pattern = 0;
590 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000591
Devin Nakamura43642b32011-07-11 11:22:16 -0400592 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
593 switch (c) {
594 case 'C':
595 Cflag = 1;
596 break;
597 case 'P':
598 Pflag = 1;
599 pattern = parse_pattern(optarg);
600 if (pattern < 0) {
601 return 0;
602 }
603 break;
604 case 'q':
605 qflag = 1;
606 break;
607 case 'v':
608 vflag = 1;
609 break;
610 default:
611 return command_usage(&readv_cmd);
612 }
613 }
aliguorie3aff4f2009-04-05 19:14:04 +0000614
Devin Nakamura43642b32011-07-11 11:22:16 -0400615 if (optind > argc - 2) {
616 return command_usage(&readv_cmd);
617 }
aliguorie3aff4f2009-04-05 19:14:04 +0000618
619
Devin Nakamura43642b32011-07-11 11:22:16 -0400620 offset = cvtnum(argv[optind]);
621 if (offset < 0) {
622 printf("non-numeric length argument -- %s\n", argv[optind]);
623 return 0;
624 }
625 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000626
Devin Nakamura43642b32011-07-11 11:22:16 -0400627 if (offset & 0x1ff) {
628 printf("offset %" PRId64 " is not sector aligned\n",
629 offset);
630 return 0;
631 }
aliguorie3aff4f2009-04-05 19:14:04 +0000632
Devin Nakamura43642b32011-07-11 11:22:16 -0400633 nr_iov = argc - optind;
634 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +0100635 if (buf == NULL) {
636 return 0;
637 }
aliguorie3aff4f2009-04-05 19:14:04 +0000638
Devin Nakamura43642b32011-07-11 11:22:16 -0400639 gettimeofday(&t1, NULL);
640 cnt = do_aio_readv(&qiov, offset, &total);
641 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000642
Devin Nakamura43642b32011-07-11 11:22:16 -0400643 if (cnt < 0) {
644 printf("readv failed: %s\n", strerror(-cnt));
645 goto out;
646 }
aliguorie3aff4f2009-04-05 19:14:04 +0000647
Devin Nakamura43642b32011-07-11 11:22:16 -0400648 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000649 void *cmp_buf = g_malloc(qiov.size);
Devin Nakamura43642b32011-07-11 11:22:16 -0400650 memset(cmp_buf, pattern, qiov.size);
651 if (memcmp(buf, cmp_buf, qiov.size)) {
652 printf("Pattern verification failed at offset %"
653 PRId64 ", %zd bytes\n", offset, qiov.size);
654 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000655 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400656 }
aliguoric48101a2009-04-18 15:36:23 +0000657
Devin Nakamura43642b32011-07-11 11:22:16 -0400658 if (qflag) {
659 goto out;
660 }
aliguorie3aff4f2009-04-05 19:14:04 +0000661
Devin Nakamura43642b32011-07-11 11:22:16 -0400662 if (vflag) {
663 dump_buffer(buf, offset, qiov.size);
664 }
aliguorie3aff4f2009-04-05 19:14:04 +0000665
Devin Nakamura43642b32011-07-11 11:22:16 -0400666 /* Finally, report back -- -C gives a parsable format */
667 t2 = tsub(t2, t1);
668 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000669
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200670out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400671 qemu_io_free(buf);
672 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000673}
674
Devin Nakamura43642b32011-07-11 11:22:16 -0400675static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000676{
Devin Nakamura43642b32011-07-11 11:22:16 -0400677 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000678"\n"
679" writes a range of bytes from the given offset\n"
680"\n"
681" Example:\n"
682" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
683"\n"
684" Writes into a segment of the currently open file, using a buffer\n"
685" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200686" -b, -- write to the VM state rather than the virtual disk\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000687" -p, -- use bdrv_pwrite to write the file\n"
688" -P, -- use different pattern to fill file\n"
689" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100690" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000691" -z, -- write zeroes using bdrv_co_write_zeroes\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000692"\n");
693}
694
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000695static int write_f(int argc, char **argv);
696
697static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400698 .name = "write",
699 .altname = "w",
700 .cfunc = write_f,
701 .argmin = 2,
702 .argmax = -1,
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000703 .args = "[-bCpqz] [-P pattern ] off len",
Devin Nakamura43642b32011-07-11 11:22:16 -0400704 .oneline = "writes a number of bytes at a specified offset",
705 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000706};
707
Devin Nakamura43642b32011-07-11 11:22:16 -0400708static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000709{
Devin Nakamura43642b32011-07-11 11:22:16 -0400710 struct timeval t1, t2;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000711 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400712 int c, cnt;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000713 char *buf = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400714 int64_t offset;
715 int count;
716 /* Some compilers get confused and warn if this is not initialized. */
717 int total = 0;
718 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000719
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000720 while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400721 switch (c) {
722 case 'b':
723 bflag = 1;
724 break;
725 case 'C':
726 Cflag = 1;
727 break;
728 case 'p':
729 pflag = 1;
730 break;
731 case 'P':
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000732 Pflag = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400733 pattern = parse_pattern(optarg);
734 if (pattern < 0) {
735 return 0;
736 }
737 break;
738 case 'q':
739 qflag = 1;
740 break;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000741 case 'z':
742 zflag = 1;
743 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400744 default:
745 return command_usage(&write_cmd);
746 }
747 }
aliguorie3aff4f2009-04-05 19:14:04 +0000748
Devin Nakamura43642b32011-07-11 11:22:16 -0400749 if (optind != argc - 2) {
750 return command_usage(&write_cmd);
751 }
aliguorie3aff4f2009-04-05 19:14:04 +0000752
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000753 if (bflag + pflag + zflag > 1) {
754 printf("-b, -p, or -z cannot be specified at the same time\n");
755 return 0;
756 }
757
758 if (zflag && Pflag) {
759 printf("-z and -P cannot be specified at the same time\n");
Devin Nakamura43642b32011-07-11 11:22:16 -0400760 return 0;
761 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200762
Devin Nakamura43642b32011-07-11 11:22:16 -0400763 offset = cvtnum(argv[optind]);
764 if (offset < 0) {
765 printf("non-numeric length argument -- %s\n", argv[optind]);
766 return 0;
767 }
aliguorie3aff4f2009-04-05 19:14:04 +0000768
Devin Nakamura43642b32011-07-11 11:22:16 -0400769 optind++;
770 count = cvtnum(argv[optind]);
771 if (count < 0) {
772 printf("non-numeric length argument -- %s\n", argv[optind]);
773 return 0;
774 }
aliguorie3aff4f2009-04-05 19:14:04 +0000775
Devin Nakamura43642b32011-07-11 11:22:16 -0400776 if (!pflag) {
777 if (offset & 0x1ff) {
778 printf("offset %" PRId64 " is not sector aligned\n",
779 offset);
780 return 0;
781 }
aliguorie3aff4f2009-04-05 19:14:04 +0000782
Devin Nakamura43642b32011-07-11 11:22:16 -0400783 if (count & 0x1ff) {
784 printf("count %d is not sector aligned\n",
785 count);
786 return 0;
787 }
788 }
aliguorie3aff4f2009-04-05 19:14:04 +0000789
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000790 if (!zflag) {
791 buf = qemu_io_alloc(count, pattern);
792 }
aliguorie3aff4f2009-04-05 19:14:04 +0000793
Devin Nakamura43642b32011-07-11 11:22:16 -0400794 gettimeofday(&t1, NULL);
795 if (pflag) {
796 cnt = do_pwrite(buf, offset, count, &total);
797 } else if (bflag) {
798 cnt = do_save_vmstate(buf, offset, count, &total);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000799 } else if (zflag) {
800 cnt = do_co_write_zeroes(offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400801 } else {
802 cnt = do_write(buf, offset, count, &total);
803 }
804 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000805
Devin Nakamura43642b32011-07-11 11:22:16 -0400806 if (cnt < 0) {
807 printf("write failed: %s\n", strerror(-cnt));
808 goto out;
809 }
aliguorie3aff4f2009-04-05 19:14:04 +0000810
Devin Nakamura43642b32011-07-11 11:22:16 -0400811 if (qflag) {
812 goto out;
813 }
aliguorie3aff4f2009-04-05 19:14:04 +0000814
Devin Nakamura43642b32011-07-11 11:22:16 -0400815 /* Finally, report back -- -C gives a parsable format */
816 t2 = tsub(t2, t1);
817 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000818
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200819out:
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000820 if (!zflag) {
821 qemu_io_free(buf);
822 }
aliguorie3aff4f2009-04-05 19:14:04 +0000823
Devin Nakamura43642b32011-07-11 11:22:16 -0400824 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000825}
826
aliguorie3aff4f2009-04-05 19:14:04 +0000827static void
828writev_help(void)
829{
Devin Nakamura43642b32011-07-11 11:22:16 -0400830 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000831"\n"
832" writes a range of bytes from the given offset source from multiple buffers\n"
833"\n"
834" Example:\n"
835" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
836"\n"
837" Writes into a segment of the currently open file, using a buffer\n"
838" filled with a set pattern (0xcdcdcdcd).\n"
839" -P, -- use different pattern to fill file\n"
840" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100841" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000842"\n");
843}
844
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000845static int writev_f(int argc, char **argv);
846
847static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400848 .name = "writev",
849 .cfunc = writev_f,
850 .argmin = 2,
851 .argmax = -1,
852 .args = "[-Cq] [-P pattern ] off len [len..]",
853 .oneline = "writes a number of bytes at a specified offset",
854 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000855};
856
Devin Nakamura43642b32011-07-11 11:22:16 -0400857static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000858{
Devin Nakamura43642b32011-07-11 11:22:16 -0400859 struct timeval t1, t2;
860 int Cflag = 0, qflag = 0;
861 int c, cnt;
862 char *buf;
863 int64_t offset;
864 /* Some compilers get confused and warn if this is not initialized. */
865 int total = 0;
866 int nr_iov;
867 int pattern = 0xcd;
868 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000869
Devin Nakamura43642b32011-07-11 11:22:16 -0400870 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
871 switch (c) {
872 case 'C':
873 Cflag = 1;
874 break;
875 case 'q':
876 qflag = 1;
877 break;
878 case 'P':
879 pattern = parse_pattern(optarg);
880 if (pattern < 0) {
881 return 0;
882 }
883 break;
884 default:
885 return command_usage(&writev_cmd);
886 }
887 }
aliguorie3aff4f2009-04-05 19:14:04 +0000888
Devin Nakamura43642b32011-07-11 11:22:16 -0400889 if (optind > argc - 2) {
890 return command_usage(&writev_cmd);
891 }
aliguorie3aff4f2009-04-05 19:14:04 +0000892
Devin Nakamura43642b32011-07-11 11:22:16 -0400893 offset = cvtnum(argv[optind]);
894 if (offset < 0) {
895 printf("non-numeric length argument -- %s\n", argv[optind]);
896 return 0;
897 }
898 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000899
Devin Nakamura43642b32011-07-11 11:22:16 -0400900 if (offset & 0x1ff) {
901 printf("offset %" PRId64 " is not sector aligned\n",
902 offset);
903 return 0;
904 }
aliguorie3aff4f2009-04-05 19:14:04 +0000905
Devin Nakamura43642b32011-07-11 11:22:16 -0400906 nr_iov = argc - optind;
907 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100908 if (buf == NULL) {
909 return 0;
910 }
aliguorie3aff4f2009-04-05 19:14:04 +0000911
Devin Nakamura43642b32011-07-11 11:22:16 -0400912 gettimeofday(&t1, NULL);
913 cnt = do_aio_writev(&qiov, offset, &total);
914 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000915
Devin Nakamura43642b32011-07-11 11:22:16 -0400916 if (cnt < 0) {
917 printf("writev failed: %s\n", strerror(-cnt));
918 goto out;
919 }
aliguorie3aff4f2009-04-05 19:14:04 +0000920
Devin Nakamura43642b32011-07-11 11:22:16 -0400921 if (qflag) {
922 goto out;
923 }
aliguorie3aff4f2009-04-05 19:14:04 +0000924
Devin Nakamura43642b32011-07-11 11:22:16 -0400925 /* Finally, report back -- -C gives a parsable format */
926 t2 = tsub(t2, t1);
927 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200928out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400929 qemu_io_free(buf);
930 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000931}
932
Devin Nakamura43642b32011-07-11 11:22:16 -0400933static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200934{
Devin Nakamura43642b32011-07-11 11:22:16 -0400935 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200936"\n"
937" writes a range of bytes from the given offset source from multiple buffers,\n"
938" in a batch of requests that may be merged by qemu\n"
939"\n"
940" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200941" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200942" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
943"\n"
944" Writes into a segment of the currently open file, using a buffer\n"
945" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
946" by one for each request contained in the multiwrite command.\n"
947" -P, -- use different pattern to fill file\n"
948" -C, -- report statistics in a machine parsable format\n"
949" -q, -- quiet mode, do not show I/O statistics\n"
950"\n");
951}
952
953static int multiwrite_f(int argc, char **argv);
954
955static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400956 .name = "multiwrite",
957 .cfunc = multiwrite_f,
958 .argmin = 2,
959 .argmax = -1,
960 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
961 .oneline = "issues multiple write requests at once",
962 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200963};
964
Devin Nakamura43642b32011-07-11 11:22:16 -0400965static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200966{
Devin Nakamura43642b32011-07-11 11:22:16 -0400967 struct timeval t1, t2;
968 int Cflag = 0, qflag = 0;
969 int c, cnt;
970 char **buf;
971 int64_t offset, first_offset = 0;
972 /* Some compilers get confused and warn if this is not initialized. */
973 int total = 0;
974 int nr_iov;
975 int nr_reqs;
976 int pattern = 0xcd;
977 QEMUIOVector *qiovs;
978 int i;
979 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200980
Devin Nakamura43642b32011-07-11 11:22:16 -0400981 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
982 switch (c) {
983 case 'C':
984 Cflag = 1;
985 break;
986 case 'q':
987 qflag = 1;
988 break;
989 case 'P':
990 pattern = parse_pattern(optarg);
991 if (pattern < 0) {
992 return 0;
993 }
994 break;
995 default:
996 return command_usage(&writev_cmd);
997 }
998 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200999
Devin Nakamura43642b32011-07-11 11:22:16 -04001000 if (optind > argc - 2) {
1001 return command_usage(&writev_cmd);
1002 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001003
Devin Nakamura43642b32011-07-11 11:22:16 -04001004 nr_reqs = 1;
1005 for (i = optind; i < argc; i++) {
1006 if (!strcmp(argv[i], ";")) {
1007 nr_reqs++;
1008 }
1009 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001010
Kevin Wolff2360622011-10-31 11:36:32 +01001011 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1012 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -05001013 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001014
Kevin Wolf67403db2011-10-31 11:49:21 +01001015 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001016 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001017
Devin Nakamura43642b32011-07-11 11:22:16 -04001018 /* Read the offset of the request */
1019 offset = cvtnum(argv[optind]);
1020 if (offset < 0) {
1021 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +01001022 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001023 }
1024 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001025
Devin Nakamura43642b32011-07-11 11:22:16 -04001026 if (offset & 0x1ff) {
1027 printf("offset %lld is not sector aligned\n",
1028 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +01001029 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001030 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001031
1032 if (i == 0) {
1033 first_offset = offset;
1034 }
1035
Devin Nakamura43642b32011-07-11 11:22:16 -04001036 /* Read lengths for qiov entries */
1037 for (j = optind; j < argc; j++) {
1038 if (!strcmp(argv[j], ";")) {
1039 break;
1040 }
1041 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001042
Devin Nakamura43642b32011-07-11 11:22:16 -04001043 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001044
Devin Nakamura43642b32011-07-11 11:22:16 -04001045 /* Build request */
Kevin Wolff2360622011-10-31 11:36:32 +01001046 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1047 if (buf[i] == NULL) {
1048 goto out;
1049 }
1050
Devin Nakamura43642b32011-07-11 11:22:16 -04001051 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001052 reqs[i].sector = offset >> 9;
1053 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001054
Devin Nakamura43642b32011-07-11 11:22:16 -04001055 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001056
Devin Nakamura43642b32011-07-11 11:22:16 -04001057 pattern++;
1058 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001059
Kevin Wolf67403db2011-10-31 11:49:21 +01001060 /* If there were empty requests at the end, ignore them */
1061 nr_reqs = i;
1062
Devin Nakamura43642b32011-07-11 11:22:16 -04001063 gettimeofday(&t1, NULL);
1064 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1065 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001066
Devin Nakamura43642b32011-07-11 11:22:16 -04001067 if (cnt < 0) {
1068 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1069 goto out;
1070 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001071
Devin Nakamura43642b32011-07-11 11:22:16 -04001072 if (qflag) {
1073 goto out;
1074 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001075
Devin Nakamura43642b32011-07-11 11:22:16 -04001076 /* Finally, report back -- -C gives a parsable format */
1077 t2 = tsub(t2, t1);
1078 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001079out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001080 for (i = 0; i < nr_reqs; i++) {
1081 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001082 if (reqs[i].qiov != NULL) {
1083 qemu_iovec_destroy(&qiovs[i]);
1084 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001085 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001086 g_free(buf);
1087 g_free(reqs);
1088 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001089 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001090}
1091
Christoph Hellwig95533d52009-06-20 21:10:44 +02001092struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001093 QEMUIOVector qiov;
1094 int64_t offset;
1095 char *buf;
1096 int qflag;
1097 int vflag;
1098 int Cflag;
1099 int Pflag;
1100 int pattern;
1101 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001102};
1103
Devin Nakamura43642b32011-07-11 11:22:16 -04001104static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001105{
Devin Nakamura43642b32011-07-11 11:22:16 -04001106 struct aio_ctx *ctx = opaque;
1107 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001108
Devin Nakamura43642b32011-07-11 11:22:16 -04001109 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001110
Christoph Hellwig95533d52009-06-20 21:10:44 +02001111
Devin Nakamura43642b32011-07-11 11:22:16 -04001112 if (ret < 0) {
1113 printf("aio_write failed: %s\n", strerror(-ret));
1114 goto out;
1115 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001116
Devin Nakamura43642b32011-07-11 11:22:16 -04001117 if (ctx->qflag) {
1118 goto out;
1119 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001120
Devin Nakamura43642b32011-07-11 11:22:16 -04001121 /* Finally, report back -- -C gives a parsable format */
1122 t2 = tsub(t2, ctx->t1);
1123 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1124 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001125out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001126 qemu_io_free(ctx->buf);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001127 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001128}
1129
Devin Nakamura43642b32011-07-11 11:22:16 -04001130static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001131{
Devin Nakamura43642b32011-07-11 11:22:16 -04001132 struct aio_ctx *ctx = opaque;
1133 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001134
Devin Nakamura43642b32011-07-11 11:22:16 -04001135 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001136
Devin Nakamura43642b32011-07-11 11:22:16 -04001137 if (ret < 0) {
1138 printf("readv failed: %s\n", strerror(-ret));
1139 goto out;
1140 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001141
Devin Nakamura43642b32011-07-11 11:22:16 -04001142 if (ctx->Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001143 void *cmp_buf = g_malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001144
Devin Nakamura43642b32011-07-11 11:22:16 -04001145 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1146 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1147 printf("Pattern verification failed at offset %"
1148 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1149 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001150 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -04001151 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001152
Devin Nakamura43642b32011-07-11 11:22:16 -04001153 if (ctx->qflag) {
1154 goto out;
1155 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001156
Devin Nakamura43642b32011-07-11 11:22:16 -04001157 if (ctx->vflag) {
1158 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1159 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001160
Devin Nakamura43642b32011-07-11 11:22:16 -04001161 /* Finally, report back -- -C gives a parsable format */
1162 t2 = tsub(t2, ctx->t1);
1163 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1164 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001165out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001166 qemu_io_free(ctx->buf);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001167 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001168}
1169
Devin Nakamura43642b32011-07-11 11:22:16 -04001170static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001171{
Devin Nakamura43642b32011-07-11 11:22:16 -04001172 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001173"\n"
1174" asynchronously reads a range of bytes from the given offset\n"
1175"\n"
1176" Example:\n"
1177" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1178"\n"
1179" Reads a segment of the currently open file, optionally dumping it to the\n"
1180" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001181" The read is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001182" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001183" -C, -- report statistics in a machine parsable format\n"
1184" -P, -- use a pattern to verify read data\n"
1185" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001186" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001187"\n");
1188}
1189
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001190static int aio_read_f(int argc, char **argv);
1191
1192static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001193 .name = "aio_read",
1194 .cfunc = aio_read_f,
1195 .argmin = 2,
1196 .argmax = -1,
1197 .args = "[-Cqv] [-P pattern ] off len [len..]",
1198 .oneline = "asynchronously reads a number of bytes",
1199 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001200};
1201
Devin Nakamura43642b32011-07-11 11:22:16 -04001202static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001203{
Devin Nakamura43642b32011-07-11 11:22:16 -04001204 int nr_iov, c;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001205 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001206
Devin Nakamura43642b32011-07-11 11:22:16 -04001207 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1208 switch (c) {
1209 case 'C':
1210 ctx->Cflag = 1;
1211 break;
1212 case 'P':
1213 ctx->Pflag = 1;
1214 ctx->pattern = parse_pattern(optarg);
1215 if (ctx->pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001216 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001217 return 0;
1218 }
1219 break;
1220 case 'q':
1221 ctx->qflag = 1;
1222 break;
1223 case 'v':
1224 ctx->vflag = 1;
1225 break;
1226 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001227 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001228 return command_usage(&aio_read_cmd);
1229 }
1230 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001231
Devin Nakamura43642b32011-07-11 11:22:16 -04001232 if (optind > argc - 2) {
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 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001236
Devin Nakamura43642b32011-07-11 11:22:16 -04001237 ctx->offset = cvtnum(argv[optind]);
1238 if (ctx->offset < 0) {
1239 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001240 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001241 return 0;
1242 }
1243 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001244
Devin Nakamura43642b32011-07-11 11:22:16 -04001245 if (ctx->offset & 0x1ff) {
1246 printf("offset %" PRId64 " is not sector aligned\n",
1247 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001248 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001249 return 0;
1250 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001251
Devin Nakamura43642b32011-07-11 11:22:16 -04001252 nr_iov = argc - optind;
1253 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001254 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001255 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001256 return 0;
1257 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001258
Devin Nakamura43642b32011-07-11 11:22:16 -04001259 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001260 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1261 ctx->qiov.size >> 9, aio_read_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001262 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001263}
1264
Devin Nakamura43642b32011-07-11 11:22:16 -04001265static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001266{
Devin Nakamura43642b32011-07-11 11:22:16 -04001267 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001268"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001269" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001270" from multiple buffers\n"
1271"\n"
1272" Example:\n"
1273" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1274"\n"
1275" Writes into a segment of the currently open file, using a buffer\n"
1276" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001277" The write is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001278" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001279" -P, -- use different pattern to fill file\n"
1280" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001281" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001282"\n");
1283}
1284
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001285static int aio_write_f(int argc, char **argv);
1286
1287static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001288 .name = "aio_write",
1289 .cfunc = aio_write_f,
1290 .argmin = 2,
1291 .argmax = -1,
1292 .args = "[-Cq] [-P pattern ] off len [len..]",
1293 .oneline = "asynchronously writes a number of bytes",
1294 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001295};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001296
Devin Nakamura43642b32011-07-11 11:22:16 -04001297static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001298{
Devin Nakamura43642b32011-07-11 11:22:16 -04001299 int nr_iov, c;
1300 int pattern = 0xcd;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001301 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001302
Devin Nakamura43642b32011-07-11 11:22:16 -04001303 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1304 switch (c) {
1305 case 'C':
1306 ctx->Cflag = 1;
1307 break;
1308 case 'q':
1309 ctx->qflag = 1;
1310 break;
1311 case 'P':
1312 pattern = parse_pattern(optarg);
1313 if (pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001314 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001315 return 0;
1316 }
1317 break;
1318 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001319 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001320 return command_usage(&aio_write_cmd);
1321 }
1322 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001323
Devin Nakamura43642b32011-07-11 11:22:16 -04001324 if (optind > argc - 2) {
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 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001328
Devin Nakamura43642b32011-07-11 11:22:16 -04001329 ctx->offset = cvtnum(argv[optind]);
1330 if (ctx->offset < 0) {
1331 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001332 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001333 return 0;
1334 }
1335 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001336
Devin Nakamura43642b32011-07-11 11:22:16 -04001337 if (ctx->offset & 0x1ff) {
1338 printf("offset %" PRId64 " is not sector aligned\n",
1339 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001340 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001341 return 0;
1342 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001343
Devin Nakamura43642b32011-07-11 11:22:16 -04001344 nr_iov = argc - optind;
1345 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001346 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001347 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001348 return 0;
1349 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001350
Devin Nakamura43642b32011-07-11 11:22:16 -04001351 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001352 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1353 ctx->qiov.size >> 9, aio_write_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001354 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001355}
1356
Devin Nakamura43642b32011-07-11 11:22:16 -04001357static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001358{
Devin Nakamura43642b32011-07-11 11:22:16 -04001359 qemu_aio_flush();
1360 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001361}
1362
1363static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001364 .name = "aio_flush",
1365 .cfunc = aio_flush_f,
1366 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001367};
1368
Devin Nakamura43642b32011-07-11 11:22:16 -04001369static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001370{
Devin Nakamura43642b32011-07-11 11:22:16 -04001371 bdrv_flush(bs);
1372 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001373}
1374
1375static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001376 .name = "flush",
1377 .altname = "f",
1378 .cfunc = flush_f,
1379 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001380};
1381
Devin Nakamura43642b32011-07-11 11:22:16 -04001382static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001383{
Devin Nakamura43642b32011-07-11 11:22:16 -04001384 int64_t offset;
1385 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001386
Devin Nakamura43642b32011-07-11 11:22:16 -04001387 offset = cvtnum(argv[1]);
1388 if (offset < 0) {
1389 printf("non-numeric truncate argument -- %s\n", argv[1]);
1390 return 0;
1391 }
aliguorie3aff4f2009-04-05 19:14:04 +00001392
Devin Nakamura43642b32011-07-11 11:22:16 -04001393 ret = bdrv_truncate(bs, offset);
1394 if (ret < 0) {
1395 printf("truncate: %s\n", strerror(-ret));
1396 return 0;
1397 }
aliguorie3aff4f2009-04-05 19:14:04 +00001398
Devin Nakamura43642b32011-07-11 11:22:16 -04001399 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001400}
1401
1402static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001403 .name = "truncate",
1404 .altname = "t",
1405 .cfunc = truncate_f,
1406 .argmin = 1,
1407 .argmax = 1,
1408 .args = "off",
1409 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001410};
1411
Devin Nakamura43642b32011-07-11 11:22:16 -04001412static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001413{
Devin Nakamura43642b32011-07-11 11:22:16 -04001414 int64_t size;
1415 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001416
Devin Nakamura43642b32011-07-11 11:22:16 -04001417 size = bdrv_getlength(bs);
1418 if (size < 0) {
1419 printf("getlength: %s\n", strerror(-size));
1420 return 0;
1421 }
aliguorie3aff4f2009-04-05 19:14:04 +00001422
Devin Nakamura43642b32011-07-11 11:22:16 -04001423 cvtstr(size, s1, sizeof(s1));
1424 printf("%s\n", s1);
1425 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001426}
1427
1428
1429static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001430 .name = "length",
1431 .altname = "l",
1432 .cfunc = length_f,
1433 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001434};
1435
1436
Devin Nakamura43642b32011-07-11 11:22:16 -04001437static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001438{
Devin Nakamura43642b32011-07-11 11:22:16 -04001439 BlockDriverInfo bdi;
1440 char s1[64], s2[64];
1441 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001442
Devin Nakamura43642b32011-07-11 11:22:16 -04001443 if (bs->drv && bs->drv->format_name) {
1444 printf("format name: %s\n", bs->drv->format_name);
1445 }
1446 if (bs->drv && bs->drv->protocol_name) {
1447 printf("format name: %s\n", bs->drv->protocol_name);
1448 }
aliguorie3aff4f2009-04-05 19:14:04 +00001449
Devin Nakamura43642b32011-07-11 11:22:16 -04001450 ret = bdrv_get_info(bs, &bdi);
1451 if (ret) {
1452 return 0;
1453 }
aliguorie3aff4f2009-04-05 19:14:04 +00001454
Devin Nakamura43642b32011-07-11 11:22:16 -04001455 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1456 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001457
Devin Nakamura43642b32011-07-11 11:22:16 -04001458 printf("cluster size: %s\n", s1);
1459 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001460
Devin Nakamura43642b32011-07-11 11:22:16 -04001461 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001462}
1463
1464
1465
1466static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001467 .name = "info",
1468 .altname = "i",
1469 .cfunc = info_f,
1470 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001471};
1472
Devin Nakamura43642b32011-07-11 11:22:16 -04001473static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001474{
Devin Nakamura43642b32011-07-11 11:22:16 -04001475 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001476"\n"
1477" discards a range of bytes from the given offset\n"
1478"\n"
1479" Example:\n"
1480" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1481"\n"
1482" Discards a segment of the currently open file.\n"
1483" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001484" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001485"\n");
1486}
1487
1488static int discard_f(int argc, char **argv);
1489
1490static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001491 .name = "discard",
1492 .altname = "d",
1493 .cfunc = discard_f,
1494 .argmin = 2,
1495 .argmax = -1,
1496 .args = "[-Cq] off len",
1497 .oneline = "discards a number of bytes at a specified offset",
1498 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001499};
1500
Devin Nakamura43642b32011-07-11 11:22:16 -04001501static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001502{
Devin Nakamura43642b32011-07-11 11:22:16 -04001503 struct timeval t1, t2;
1504 int Cflag = 0, qflag = 0;
1505 int c, ret;
1506 int64_t offset;
1507 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001508
Devin Nakamura43642b32011-07-11 11:22:16 -04001509 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1510 switch (c) {
1511 case 'C':
1512 Cflag = 1;
1513 break;
1514 case 'q':
1515 qflag = 1;
1516 break;
1517 default:
1518 return command_usage(&discard_cmd);
1519 }
1520 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001521
Devin Nakamura43642b32011-07-11 11:22:16 -04001522 if (optind != argc - 2) {
1523 return command_usage(&discard_cmd);
1524 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001525
Devin Nakamura43642b32011-07-11 11:22:16 -04001526 offset = cvtnum(argv[optind]);
1527 if (offset < 0) {
1528 printf("non-numeric length argument -- %s\n", argv[optind]);
1529 return 0;
1530 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001531
Devin Nakamura43642b32011-07-11 11:22:16 -04001532 optind++;
1533 count = cvtnum(argv[optind]);
1534 if (count < 0) {
1535 printf("non-numeric length argument -- %s\n", argv[optind]);
1536 return 0;
1537 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001538
Devin Nakamura43642b32011-07-11 11:22:16 -04001539 gettimeofday(&t1, NULL);
1540 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1541 count >> BDRV_SECTOR_BITS);
1542 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001543
Devin Nakamura43642b32011-07-11 11:22:16 -04001544 if (ret < 0) {
1545 printf("discard failed: %s\n", strerror(-ret));
1546 goto out;
1547 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001548
Devin Nakamura43642b32011-07-11 11:22:16 -04001549 /* Finally, report back -- -C gives a parsable format */
1550 if (!qflag) {
1551 t2 = tsub(t2, t1);
1552 print_report("discard", &t2, offset, count, count, 1, Cflag);
1553 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001554
1555out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001556 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001557}
1558
Devin Nakamura43642b32011-07-11 11:22:16 -04001559static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001560{
Devin Nakamura43642b32011-07-11 11:22:16 -04001561 int64_t offset;
1562 int nb_sectors, remaining;
1563 char s1[64];
1564 int num, sum_alloc;
1565 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001566
Devin Nakamura43642b32011-07-11 11:22:16 -04001567 offset = cvtnum(argv[1]);
1568 if (offset & 0x1ff) {
1569 printf("offset %" PRId64 " is not sector aligned\n",
1570 offset);
1571 return 0;
1572 }
aliguorie3aff4f2009-04-05 19:14:04 +00001573
Devin Nakamura43642b32011-07-11 11:22:16 -04001574 if (argc == 3) {
1575 nb_sectors = cvtnum(argv[2]);
1576 } else {
1577 nb_sectors = 1;
1578 }
aliguorie3aff4f2009-04-05 19:14:04 +00001579
Devin Nakamura43642b32011-07-11 11:22:16 -04001580 remaining = nb_sectors;
1581 sum_alloc = 0;
1582 while (remaining) {
1583 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1584 remaining -= num;
1585 if (ret) {
1586 sum_alloc += num;
1587 }
1588 }
aliguorie3aff4f2009-04-05 19:14:04 +00001589
Devin Nakamura43642b32011-07-11 11:22:16 -04001590 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001591
Devin Nakamura43642b32011-07-11 11:22:16 -04001592 printf("%d/%d sectors allocated at offset %s\n",
1593 sum_alloc, nb_sectors, s1);
1594 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001595}
1596
1597static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001598 .name = "alloc",
1599 .altname = "a",
1600 .argmin = 1,
1601 .argmax = 2,
1602 .cfunc = alloc_f,
1603 .args = "off [sectors]",
1604 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001605};
1606
Devin Nakamura43642b32011-07-11 11:22:16 -04001607static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001608{
Devin Nakamura43642b32011-07-11 11:22:16 -04001609 int64_t offset;
1610 int64_t nb_sectors;
1611 char s1[64];
1612 int num, num_checked;
1613 int ret;
1614 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001615
Devin Nakamura43642b32011-07-11 11:22:16 -04001616 offset = 0;
1617 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001618
Devin Nakamura43642b32011-07-11 11:22:16 -04001619 do {
1620 num_checked = MIN(nb_sectors, INT_MAX);
1621 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1622 retstr = ret ? " allocated" : "not allocated";
1623 cvtstr(offset << 9ULL, s1, sizeof(s1));
1624 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1625 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001626
Devin Nakamura43642b32011-07-11 11:22:16 -04001627 offset += num;
1628 nb_sectors -= num;
1629 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001630
Devin Nakamura43642b32011-07-11 11:22:16 -04001631 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001632}
1633
1634static const cmdinfo_t map_cmd = {
1635 .name = "map",
1636 .argmin = 0,
1637 .argmax = 0,
1638 .cfunc = map_f,
1639 .args = "",
1640 .oneline = "prints the allocated areas of a file",
1641};
1642
1643
Devin Nakamura43642b32011-07-11 11:22:16 -04001644static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001645{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001646 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001647 bs = NULL;
1648 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001649}
1650
1651static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001652 .name = "close",
1653 .altname = "c",
1654 .cfunc = close_f,
1655 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001656};
1657
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001658static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001659{
Devin Nakamura43642b32011-07-11 11:22:16 -04001660 if (bs) {
1661 fprintf(stderr, "file open already, try 'help close'\n");
1662 return 1;
1663 }
aliguorie3aff4f2009-04-05 19:14:04 +00001664
Devin Nakamura43642b32011-07-11 11:22:16 -04001665 if (growable) {
1666 if (bdrv_file_open(&bs, name, flags)) {
1667 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1668 return 1;
1669 }
1670 } else {
1671 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001672
Devin Nakamura43642b32011-07-11 11:22:16 -04001673 if (bdrv_open(bs, name, flags, NULL) < 0) {
1674 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001675 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001676 bs = NULL;
1677 return 1;
1678 }
1679 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001680
Devin Nakamura43642b32011-07-11 11:22:16 -04001681 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001682}
1683
Devin Nakamura43642b32011-07-11 11:22:16 -04001684static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001685{
Devin Nakamura43642b32011-07-11 11:22:16 -04001686 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001687"\n"
1688" opens a new file in the requested mode\n"
1689"\n"
1690" Example:\n"
1691" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1692"\n"
1693" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001694" -r, -- open file read-only\n"
1695" -s, -- use snapshot file\n"
1696" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001697" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001698"\n");
1699}
1700
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001701static int open_f(int argc, char **argv);
1702
1703static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001704 .name = "open",
1705 .altname = "o",
1706 .cfunc = open_f,
1707 .argmin = 1,
1708 .argmax = -1,
1709 .flags = CMD_NOFILE_OK,
1710 .args = "[-Crsn] [path]",
1711 .oneline = "open the file specified by path",
1712 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001713};
aliguorie3aff4f2009-04-05 19:14:04 +00001714
Devin Nakamura43642b32011-07-11 11:22:16 -04001715static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001716{
Devin Nakamura43642b32011-07-11 11:22:16 -04001717 int flags = 0;
1718 int readonly = 0;
1719 int growable = 0;
1720 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001721
Devin Nakamura43642b32011-07-11 11:22:16 -04001722 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1723 switch (c) {
1724 case 's':
1725 flags |= BDRV_O_SNAPSHOT;
1726 break;
1727 case 'n':
1728 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1729 break;
1730 case 'r':
1731 readonly = 1;
1732 break;
1733 case 'g':
1734 growable = 1;
1735 break;
1736 default:
1737 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001738 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001739 }
aliguorie3aff4f2009-04-05 19:14:04 +00001740
Devin Nakamura43642b32011-07-11 11:22:16 -04001741 if (!readonly) {
1742 flags |= BDRV_O_RDWR;
1743 }
aliguorie3aff4f2009-04-05 19:14:04 +00001744
Devin Nakamura43642b32011-07-11 11:22:16 -04001745 if (optind != argc - 1) {
1746 return command_usage(&open_cmd);
1747 }
1748
1749 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001750}
1751
Devin Nakamura43642b32011-07-11 11:22:16 -04001752static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001753{
Devin Nakamura43642b32011-07-11 11:22:16 -04001754 /* only one device allowed so far */
1755 if (index >= 1) {
1756 return 0;
1757 }
1758 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001759}
1760
Devin Nakamura43642b32011-07-11 11:22:16 -04001761static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001762{
Devin Nakamura43642b32011-07-11 11:22:16 -04001763 if (ct->flags & CMD_FLAG_GLOBAL) {
1764 return 1;
1765 }
1766 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1767 fprintf(stderr, "no file open, try 'help open'\n");
1768 return 0;
1769 }
1770 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001771}
1772
1773static void usage(const char *name)
1774{
Devin Nakamura43642b32011-07-11 11:22:16 -04001775 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001776"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001777"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001778"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001779" -c, --cmd command to execute\n"
1780" -r, --read-only export read-only\n"
1781" -s, --snapshot use snapshot file\n"
1782" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001783" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001784" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001785" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001786" -h, --help display this help and exit\n"
1787" -V, --version output version information and exit\n"
1788"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001789 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001790}
1791
1792
1793int main(int argc, char **argv)
1794{
Devin Nakamura43642b32011-07-11 11:22:16 -04001795 int readonly = 0;
1796 int growable = 0;
1797 const char *sopt = "hVc:rsnmgk";
1798 const struct option lopt[] = {
1799 { "help", 0, NULL, 'h' },
1800 { "version", 0, NULL, 'V' },
1801 { "offset", 1, NULL, 'o' },
1802 { "cmd", 1, NULL, 'c' },
1803 { "read-only", 0, NULL, 'r' },
1804 { "snapshot", 0, NULL, 's' },
1805 { "nocache", 0, NULL, 'n' },
1806 { "misalign", 0, NULL, 'm' },
1807 { "growable", 0, NULL, 'g' },
1808 { "native-aio", 0, NULL, 'k' },
1809 { NULL, 0, NULL, 0 }
1810 };
1811 int c;
1812 int opt_index = 0;
1813 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001814
Devin Nakamura43642b32011-07-11 11:22:16 -04001815 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001816
Devin Nakamura43642b32011-07-11 11:22:16 -04001817 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1818 switch (c) {
1819 case 's':
1820 flags |= BDRV_O_SNAPSHOT;
1821 break;
1822 case 'n':
1823 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1824 break;
1825 case 'c':
1826 add_user_command(optarg);
1827 break;
1828 case 'r':
1829 readonly = 1;
1830 break;
1831 case 'm':
1832 misalign = 1;
1833 break;
1834 case 'g':
1835 growable = 1;
1836 break;
1837 case 'k':
1838 flags |= BDRV_O_NATIVE_AIO;
1839 break;
1840 case 'V':
1841 printf("%s version %s\n", progname, VERSION);
1842 exit(0);
1843 case 'h':
1844 usage(progname);
1845 exit(0);
1846 default:
1847 usage(progname);
1848 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001849 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001850 }
aliguorie3aff4f2009-04-05 19:14:04 +00001851
Devin Nakamura43642b32011-07-11 11:22:16 -04001852 if ((argc - optind) > 1) {
1853 usage(progname);
1854 exit(1);
1855 }
aliguorie3aff4f2009-04-05 19:14:04 +00001856
Devin Nakamura43642b32011-07-11 11:22:16 -04001857 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001858
Zhi Yong Wua57d1142012-02-19 22:24:59 +08001859 qemu_init_main_loop();
1860
Devin Nakamura43642b32011-07-11 11:22:16 -04001861 /* initialize commands */
1862 quit_init();
1863 help_init();
1864 add_command(&open_cmd);
1865 add_command(&close_cmd);
1866 add_command(&read_cmd);
1867 add_command(&readv_cmd);
1868 add_command(&write_cmd);
1869 add_command(&writev_cmd);
1870 add_command(&multiwrite_cmd);
1871 add_command(&aio_read_cmd);
1872 add_command(&aio_write_cmd);
1873 add_command(&aio_flush_cmd);
1874 add_command(&flush_cmd);
1875 add_command(&truncate_cmd);
1876 add_command(&length_cmd);
1877 add_command(&info_cmd);
1878 add_command(&discard_cmd);
1879 add_command(&alloc_cmd);
1880 add_command(&map_cmd);
1881
1882 add_args_command(init_args_command);
1883 add_check_command(init_check_command);
1884
1885 /* open the device */
1886 if (!readonly) {
1887 flags |= BDRV_O_RDWR;
1888 }
1889
1890 if ((argc - optind) == 1) {
1891 openfile(argv[optind], flags, growable);
1892 }
1893 command_loop();
1894
1895 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001896 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -04001897 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001898 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04001899
1900 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001901 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001902 }
1903 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001904}