blob: 8a719a881cf1b6fe29c69cde63dc0c8dbaf14c7c [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 Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/block_int.h"
aliguorie3aff4f2009-04-05 19:14:04 +000020#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
Kevin Wolfb6e356a2013-06-05 14:19:28 +020032static int64_t cvtnum(const char *s)
33{
34 char *end;
35 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
36}
37
aliguorie3aff4f2009-04-05 19:14:04 +000038/*
Christoph Hellwigcf070d72009-07-20 01:19:25 +020039 * Parse the pattern argument to various sub-commands.
40 *
41 * Because the pattern is used as an argument to memset it must evaluate
42 * to an unsigned integer that fits into a single byte.
43 */
44static int parse_pattern(const char *arg)
45{
Devin Nakamura43642b32011-07-11 11:22:16 -040046 char *endptr = NULL;
47 long pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020048
Devin Nakamura43642b32011-07-11 11:22:16 -040049 pattern = strtol(arg, &endptr, 0);
50 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
51 printf("%s is not a valid pattern byte\n", arg);
52 return -1;
53 }
Christoph Hellwigcf070d72009-07-20 01:19:25 +020054
Devin Nakamura43642b32011-07-11 11:22:16 -040055 return pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020056}
57
58/*
aliguorie3aff4f2009-04-05 19:14:04 +000059 * Memory allocation helpers.
60 *
61 * Make sure memory is aligned by default, or purposefully misaligned if
62 * that is specified on the command line.
63 */
64
Devin Nakamura43642b32011-07-11 11:22:16 -040065#define MISALIGN_OFFSET 16
aliguorie3aff4f2009-04-05 19:14:04 +000066static void *qemu_io_alloc(size_t len, int pattern)
67{
Devin Nakamura43642b32011-07-11 11:22:16 -040068 void *buf;
aliguorie3aff4f2009-04-05 19:14:04 +000069
Devin Nakamura43642b32011-07-11 11:22:16 -040070 if (misalign) {
71 len += MISALIGN_OFFSET;
72 }
73 buf = qemu_blockalign(bs, len);
74 memset(buf, pattern, len);
75 if (misalign) {
76 buf += MISALIGN_OFFSET;
77 }
78 return buf;
aliguorie3aff4f2009-04-05 19:14:04 +000079}
80
81static void qemu_io_free(void *p)
82{
Devin Nakamura43642b32011-07-11 11:22:16 -040083 if (misalign) {
84 p -= MISALIGN_OFFSET;
85 }
86 qemu_vfree(p);
aliguorie3aff4f2009-04-05 19:14:04 +000087}
88
Devin Nakamura43642b32011-07-11 11:22:16 -040089static void dump_buffer(const void *buffer, int64_t offset, int len)
aliguorie3aff4f2009-04-05 19:14:04 +000090{
Devin Nakamura43642b32011-07-11 11:22:16 -040091 int i, j;
92 const uint8_t *p;
aliguorie3aff4f2009-04-05 19:14:04 +000093
Devin Nakamura43642b32011-07-11 11:22:16 -040094 for (i = 0, p = buffer; i < len; i += 16) {
95 const uint8_t *s = p;
aliguorie3aff4f2009-04-05 19:14:04 +000096
Devin Nakamura43642b32011-07-11 11:22:16 -040097 printf("%08" PRIx64 ": ", offset + i);
98 for (j = 0; j < 16 && i + j < len; j++, p++) {
99 printf("%02x ", *p);
100 }
101 printf(" ");
102 for (j = 0; j < 16 && i + j < len; j++, s++) {
103 if (isalnum(*s)) {
104 printf("%c", *s);
105 } else {
106 printf(".");
107 }
108 }
109 printf("\n");
110 }
aliguorie3aff4f2009-04-05 19:14:04 +0000111}
112
Devin Nakamura43642b32011-07-11 11:22:16 -0400113static void print_report(const char *op, struct timeval *t, int64_t offset,
114 int count, int total, int cnt, int Cflag)
aliguorie3aff4f2009-04-05 19:14:04 +0000115{
Devin Nakamura43642b32011-07-11 11:22:16 -0400116 char s1[64], s2[64], ts[64];
aliguorie3aff4f2009-04-05 19:14:04 +0000117
Devin Nakamura43642b32011-07-11 11:22:16 -0400118 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
119 if (!Cflag) {
120 cvtstr((double)total, s1, sizeof(s1));
121 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
122 printf("%s %d/%d bytes at offset %" PRId64 "\n",
123 op, total, count, offset);
124 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
125 s1, cnt, ts, s2, tdiv((double)cnt, *t));
126 } else {/* bytes,ops,time,bytes/sec,ops/sec */
127 printf("%d,%d,%s,%.3f,%.3f\n",
128 total, cnt, ts,
129 tdiv((double)total, *t),
130 tdiv((double)cnt, *t));
131 }
aliguorie3aff4f2009-04-05 19:14:04 +0000132}
133
Christoph Hellwigcf572982009-07-10 13:33:42 +0200134/*
135 * Parse multiple length statements for vectored I/O, and construct an I/O
136 * vector matching it.
137 */
138static void *
139create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
140{
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000141 size_t *sizes = g_new0(size_t, nr_iov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400142 size_t count = 0;
143 void *buf = NULL;
144 void *p;
145 int i;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200146
Devin Nakamura43642b32011-07-11 11:22:16 -0400147 for (i = 0; i < nr_iov; i++) {
148 char *arg = argv[i];
149 int64_t len;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200150
Devin Nakamura43642b32011-07-11 11:22:16 -0400151 len = cvtnum(arg);
152 if (len < 0) {
153 printf("non-numeric 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 /* should be SIZE_T_MAX, but that doesn't exist */
158 if (len > INT_MAX) {
159 printf("too large length argument -- %s\n", arg);
160 goto fail;
161 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200162
Devin Nakamura43642b32011-07-11 11:22:16 -0400163 if (len & 0x1ff) {
164 printf("length argument %" PRId64
165 " is not sector aligned\n", len);
166 goto fail;
167 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200168
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 sizes[i] = len;
170 count += len;
171 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200172
Devin Nakamura43642b32011-07-11 11:22:16 -0400173 qemu_iovec_init(qiov, nr_iov);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200174
Devin Nakamura43642b32011-07-11 11:22:16 -0400175 buf = p = qemu_io_alloc(count, pattern);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200176
Devin Nakamura43642b32011-07-11 11:22:16 -0400177 for (i = 0; i < nr_iov; i++) {
178 qemu_iovec_add(qiov, p, sizes[i]);
179 p += sizes[i];
180 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200181
Kevin Wolf40a0d7c2009-11-18 10:42:59 +0100182fail:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000183 g_free(sizes);
Devin Nakamura43642b32011-07-11 11:22:16 -0400184 return buf;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200185}
186
aliguorie3aff4f2009-04-05 19:14:04 +0000187static int do_read(char *buf, int64_t offset, int count, int *total)
188{
Devin Nakamura43642b32011-07-11 11:22:16 -0400189 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000190
Devin Nakamura43642b32011-07-11 11:22:16 -0400191 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
192 if (ret < 0) {
193 return ret;
194 }
195 *total = count;
196 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000197}
198
199static int do_write(char *buf, int64_t offset, int count, int *total)
200{
Devin Nakamura43642b32011-07-11 11:22:16 -0400201 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000202
Devin Nakamura43642b32011-07-11 11:22:16 -0400203 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
204 if (ret < 0) {
205 return ret;
206 }
207 *total = count;
208 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000209}
210
211static int do_pread(char *buf, int64_t offset, int count, int *total)
212{
Devin Nakamura43642b32011-07-11 11:22:16 -0400213 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
214 if (*total < 0) {
215 return *total;
216 }
217 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000218}
219
220static int do_pwrite(char *buf, int64_t offset, int count, int *total)
221{
Devin Nakamura43642b32011-07-11 11:22:16 -0400222 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
223 if (*total < 0) {
224 return *total;
225 }
226 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000227}
228
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000229typedef struct {
230 int64_t offset;
231 int count;
232 int *total;
233 int ret;
234 bool done;
235} CoWriteZeroes;
236
237static void coroutine_fn co_write_zeroes_entry(void *opaque)
238{
239 CoWriteZeroes *data = opaque;
240
241 data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE,
242 data->count / BDRV_SECTOR_SIZE);
243 data->done = true;
244 if (data->ret < 0) {
245 *data->total = data->ret;
246 return;
247 }
248
249 *data->total = data->count;
250}
251
252static int do_co_write_zeroes(int64_t offset, int count, int *total)
253{
254 Coroutine *co;
255 CoWriteZeroes data = {
256 .offset = offset,
257 .count = count,
258 .total = total,
259 .done = false,
260 };
261
262 co = qemu_coroutine_create(co_write_zeroes_entry);
263 qemu_coroutine_enter(co, &data);
264 while (!data.done) {
265 qemu_aio_wait();
266 }
267 if (data.ret < 0) {
268 return data.ret;
269 } else {
270 return 1;
271 }
272}
273
Kevin Wolf791bfa32012-12-04 16:35:12 +0100274static int do_write_compressed(char *buf, int64_t offset, int count, int *total)
275{
276 int ret;
277
278 ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
279 if (ret < 0) {
280 return ret;
281 }
282 *total = count;
283 return 1;
284}
285
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200286static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
287{
Devin Nakamura43642b32011-07-11 11:22:16 -0400288 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
289 if (*total < 0) {
290 return *total;
291 }
292 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200293}
294
295static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
296{
Devin Nakamura43642b32011-07-11 11:22:16 -0400297 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
298 if (*total < 0) {
299 return *total;
300 }
301 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200302}
303
aliguorie3aff4f2009-04-05 19:14:04 +0000304#define NOT_DONE 0x7fffffff
305static void aio_rw_done(void *opaque, int ret)
306{
Devin Nakamura43642b32011-07-11 11:22:16 -0400307 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000308}
309
310static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
311{
Devin Nakamura43642b32011-07-11 11:22:16 -0400312 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000313
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100314 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
315 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400316 while (async_ret == NOT_DONE) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200317 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400318 }
aliguorie3aff4f2009-04-05 19:14:04 +0000319
Devin Nakamura43642b32011-07-11 11:22:16 -0400320 *total = qiov->size;
321 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000322}
323
324static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
325{
Devin Nakamura43642b32011-07-11 11:22:16 -0400326 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000327
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100328 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
329 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400330 while (async_ret == NOT_DONE) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200331 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400332 }
aliguorie3aff4f2009-04-05 19:14:04 +0000333
Devin Nakamura43642b32011-07-11 11:22:16 -0400334 *total = qiov->size;
335 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000336}
337
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200338struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400339 int num_done;
340 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200341};
342
343static void multiwrite_cb(void *opaque, int ret)
344{
Devin Nakamura43642b32011-07-11 11:22:16 -0400345 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200346
Devin Nakamura43642b32011-07-11 11:22:16 -0400347 async_ret->num_done++;
348 if (ret < 0) {
349 async_ret->error = ret;
350 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200351}
352
353static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
354{
Devin Nakamura43642b32011-07-11 11:22:16 -0400355 int i, ret;
356 struct multiwrite_async_ret async_ret = {
357 .num_done = 0,
358 .error = 0,
359 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200360
Devin Nakamura43642b32011-07-11 11:22:16 -0400361 *total = 0;
362 for (i = 0; i < num_reqs; i++) {
363 reqs[i].cb = multiwrite_cb;
364 reqs[i].opaque = &async_ret;
365 *total += reqs[i].qiov->size;
366 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200367
Devin Nakamura43642b32011-07-11 11:22:16 -0400368 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
369 if (ret < 0) {
370 return ret;
371 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200372
Devin Nakamura43642b32011-07-11 11:22:16 -0400373 while (async_ret.num_done < num_reqs) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200374 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400375 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200376
Devin Nakamura43642b32011-07-11 11:22:16 -0400377 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200378}
aliguorie3aff4f2009-04-05 19:14:04 +0000379
Devin Nakamura43642b32011-07-11 11:22:16 -0400380static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000381{
Devin Nakamura43642b32011-07-11 11:22:16 -0400382 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000383"\n"
384" reads a range of bytes from the given offset\n"
385"\n"
386" Example:\n"
387" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
388"\n"
389" Reads a segment of the currently open file, optionally dumping it to the\n"
390" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200391" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200392" -C, -- report statistics in a machine parsable format\n"
393" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000394" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000395" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100396" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200397" -s, -- start offset for pattern verification (only with -P)\n"
398" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000399"\n");
400}
401
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000402static int read_f(int argc, char **argv);
403
404static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400405 .name = "read",
406 .altname = "r",
407 .cfunc = read_f,
408 .argmin = 2,
409 .argmax = -1,
410 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
411 .oneline = "reads a number of bytes at a specified offset",
412 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000413};
414
Devin Nakamura43642b32011-07-11 11:22:16 -0400415static int read_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000416{
Devin Nakamura43642b32011-07-11 11:22:16 -0400417 struct timeval t1, t2;
418 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
419 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
420 int c, cnt;
421 char *buf;
422 int64_t offset;
423 int count;
424 /* Some compilers get confused and warn if this is not initialized. */
425 int total = 0;
426 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000427
Devin Nakamura43642b32011-07-11 11:22:16 -0400428 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
429 switch (c) {
430 case 'b':
431 bflag = 1;
432 break;
433 case 'C':
434 Cflag = 1;
435 break;
436 case 'l':
437 lflag = 1;
438 pattern_count = cvtnum(optarg);
439 if (pattern_count < 0) {
440 printf("non-numeric length argument -- %s\n", optarg);
441 return 0;
442 }
443 break;
444 case 'p':
445 pflag = 1;
446 break;
447 case 'P':
448 Pflag = 1;
449 pattern = parse_pattern(optarg);
450 if (pattern < 0) {
451 return 0;
452 }
453 break;
454 case 'q':
455 qflag = 1;
456 break;
457 case 's':
458 sflag = 1;
459 pattern_offset = cvtnum(optarg);
460 if (pattern_offset < 0) {
461 printf("non-numeric length argument -- %s\n", optarg);
462 return 0;
463 }
464 break;
465 case 'v':
466 vflag = 1;
467 break;
468 default:
469 return command_usage(&read_cmd);
470 }
471 }
aliguorie3aff4f2009-04-05 19:14:04 +0000472
Devin Nakamura43642b32011-07-11 11:22:16 -0400473 if (optind != argc - 2) {
474 return command_usage(&read_cmd);
475 }
aliguorie3aff4f2009-04-05 19:14:04 +0000476
Devin Nakamura43642b32011-07-11 11:22:16 -0400477 if (bflag && pflag) {
478 printf("-b and -p cannot be specified at the same time\n");
479 return 0;
480 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200481
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 offset = cvtnum(argv[optind]);
483 if (offset < 0) {
484 printf("non-numeric length argument -- %s\n", argv[optind]);
485 return 0;
486 }
aliguorie3aff4f2009-04-05 19:14:04 +0000487
Devin Nakamura43642b32011-07-11 11:22:16 -0400488 optind++;
489 count = cvtnum(argv[optind]);
490 if (count < 0) {
491 printf("non-numeric length argument -- %s\n", argv[optind]);
492 return 0;
493 }
aliguorie3aff4f2009-04-05 19:14:04 +0000494
Kevin Wolfd9654a52009-04-23 15:11:13 +0200495 if (!Pflag && (lflag || sflag)) {
496 return command_usage(&read_cmd);
497 }
498
499 if (!lflag) {
500 pattern_count = count - pattern_offset;
501 }
502
503 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
Dong Xu Wang07f35072011-11-22 18:06:26 +0800504 printf("pattern verification range exceeds end of read data\n");
Kevin Wolfd9654a52009-04-23 15:11:13 +0200505 return 0;
506 }
507
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400508 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400509 if (offset & 0x1ff) {
510 printf("offset %" PRId64 " is not sector aligned\n",
511 offset);
512 return 0;
513 }
514 if (count & 0x1ff) {
515 printf("count %d is not sector aligned\n",
516 count);
517 return 0;
518 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400519 }
aliguorie3aff4f2009-04-05 19:14:04 +0000520
Devin Nakamura43642b32011-07-11 11:22:16 -0400521 buf = qemu_io_alloc(count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000522
Devin Nakamura43642b32011-07-11 11:22:16 -0400523 gettimeofday(&t1, NULL);
524 if (pflag) {
525 cnt = do_pread(buf, offset, count, &total);
526 } else if (bflag) {
527 cnt = do_load_vmstate(buf, offset, count, &total);
528 } else {
529 cnt = do_read(buf, offset, count, &total);
530 }
531 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000532
Devin Nakamura43642b32011-07-11 11:22:16 -0400533 if (cnt < 0) {
534 printf("read failed: %s\n", strerror(-cnt));
535 goto out;
536 }
aliguorie3aff4f2009-04-05 19:14:04 +0000537
Devin Nakamura43642b32011-07-11 11:22:16 -0400538 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000539 void *cmp_buf = g_malloc(pattern_count);
Devin Nakamura43642b32011-07-11 11:22:16 -0400540 memset(cmp_buf, pattern, pattern_count);
541 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
542 printf("Pattern verification failed at offset %"
543 PRId64 ", %d bytes\n",
544 offset + pattern_offset, pattern_count);
545 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000546 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400547 }
aliguorie3aff4f2009-04-05 19:14:04 +0000548
Devin Nakamura43642b32011-07-11 11:22:16 -0400549 if (qflag) {
550 goto out;
551 }
aliguoric48101a2009-04-18 15:36:23 +0000552
Devin Nakamura43642b32011-07-11 11:22:16 -0400553 if (vflag) {
554 dump_buffer(buf, offset, count);
555 }
aliguorie3aff4f2009-04-05 19:14:04 +0000556
Devin Nakamura43642b32011-07-11 11:22:16 -0400557 /* Finally, report back -- -C gives a parsable format */
558 t2 = tsub(t2, t1);
559 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000560
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200561out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400562 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000563
Devin Nakamura43642b32011-07-11 11:22:16 -0400564 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000565}
566
Devin Nakamura43642b32011-07-11 11:22:16 -0400567static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000568{
Devin Nakamura43642b32011-07-11 11:22:16 -0400569 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000570"\n"
571" reads a range of bytes from the given offset into multiple buffers\n"
572"\n"
573" Example:\n"
574" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
575"\n"
576" Reads a segment of the currently open file, optionally dumping it to the\n"
577" standard output stream (with -v option) for subsequent inspection.\n"
578" Uses multiple iovec buffers if more than one byte range is specified.\n"
579" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000580" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000581" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100582" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000583"\n");
584}
585
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000586static int readv_f(int argc, char **argv);
587
588static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400589 .name = "readv",
590 .cfunc = readv_f,
591 .argmin = 2,
592 .argmax = -1,
593 .args = "[-Cqv] [-P pattern ] off len [len..]",
594 .oneline = "reads a number of bytes at a specified offset",
595 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000596};
597
Devin Nakamura43642b32011-07-11 11:22:16 -0400598static int readv_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000599{
Devin Nakamura43642b32011-07-11 11:22:16 -0400600 struct timeval t1, t2;
601 int Cflag = 0, qflag = 0, vflag = 0;
602 int c, cnt;
603 char *buf;
604 int64_t offset;
605 /* Some compilers get confused and warn if this is not initialized. */
606 int total = 0;
607 int nr_iov;
608 QEMUIOVector qiov;
609 int pattern = 0;
610 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000611
Devin Nakamura43642b32011-07-11 11:22:16 -0400612 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
613 switch (c) {
614 case 'C':
615 Cflag = 1;
616 break;
617 case 'P':
618 Pflag = 1;
619 pattern = parse_pattern(optarg);
620 if (pattern < 0) {
621 return 0;
622 }
623 break;
624 case 'q':
625 qflag = 1;
626 break;
627 case 'v':
628 vflag = 1;
629 break;
630 default:
631 return command_usage(&readv_cmd);
632 }
633 }
aliguorie3aff4f2009-04-05 19:14:04 +0000634
Devin Nakamura43642b32011-07-11 11:22:16 -0400635 if (optind > argc - 2) {
636 return command_usage(&readv_cmd);
637 }
aliguorie3aff4f2009-04-05 19:14:04 +0000638
639
Devin Nakamura43642b32011-07-11 11:22:16 -0400640 offset = cvtnum(argv[optind]);
641 if (offset < 0) {
642 printf("non-numeric length argument -- %s\n", argv[optind]);
643 return 0;
644 }
645 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000646
Devin Nakamura43642b32011-07-11 11:22:16 -0400647 if (offset & 0x1ff) {
648 printf("offset %" PRId64 " is not sector aligned\n",
649 offset);
650 return 0;
651 }
aliguorie3aff4f2009-04-05 19:14:04 +0000652
Devin Nakamura43642b32011-07-11 11:22:16 -0400653 nr_iov = argc - optind;
654 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +0100655 if (buf == NULL) {
656 return 0;
657 }
aliguorie3aff4f2009-04-05 19:14:04 +0000658
Devin Nakamura43642b32011-07-11 11:22:16 -0400659 gettimeofday(&t1, NULL);
660 cnt = do_aio_readv(&qiov, offset, &total);
661 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000662
Devin Nakamura43642b32011-07-11 11:22:16 -0400663 if (cnt < 0) {
664 printf("readv failed: %s\n", strerror(-cnt));
665 goto out;
666 }
aliguorie3aff4f2009-04-05 19:14:04 +0000667
Devin Nakamura43642b32011-07-11 11:22:16 -0400668 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000669 void *cmp_buf = g_malloc(qiov.size);
Devin Nakamura43642b32011-07-11 11:22:16 -0400670 memset(cmp_buf, pattern, qiov.size);
671 if (memcmp(buf, cmp_buf, qiov.size)) {
672 printf("Pattern verification failed at offset %"
673 PRId64 ", %zd bytes\n", offset, qiov.size);
674 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000675 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400676 }
aliguoric48101a2009-04-18 15:36:23 +0000677
Devin Nakamura43642b32011-07-11 11:22:16 -0400678 if (qflag) {
679 goto out;
680 }
aliguorie3aff4f2009-04-05 19:14:04 +0000681
Devin Nakamura43642b32011-07-11 11:22:16 -0400682 if (vflag) {
683 dump_buffer(buf, offset, qiov.size);
684 }
aliguorie3aff4f2009-04-05 19:14:04 +0000685
Devin Nakamura43642b32011-07-11 11:22:16 -0400686 /* Finally, report back -- -C gives a parsable format */
687 t2 = tsub(t2, t1);
688 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000689
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200690out:
Kevin Wolf9e559532012-07-02 15:13:53 +0200691 qemu_iovec_destroy(&qiov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400692 qemu_io_free(buf);
693 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000694}
695
Devin Nakamura43642b32011-07-11 11:22:16 -0400696static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000697{
Devin Nakamura43642b32011-07-11 11:22:16 -0400698 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000699"\n"
700" writes a range of bytes from the given offset\n"
701"\n"
702" Example:\n"
703" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
704"\n"
705" Writes into a segment of the currently open file, using a buffer\n"
706" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200707" -b, -- write to the VM state rather than the virtual disk\n"
Kevin Wolf791bfa32012-12-04 16:35:12 +0100708" -c, -- write compressed data with bdrv_write_compressed\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000709" -p, -- use bdrv_pwrite to write the file\n"
710" -P, -- use different pattern to fill file\n"
711" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100712" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000713" -z, -- write zeroes using bdrv_co_write_zeroes\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000714"\n");
715}
716
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000717static int write_f(int argc, char **argv);
718
719static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400720 .name = "write",
721 .altname = "w",
722 .cfunc = write_f,
723 .argmin = 2,
724 .argmax = -1,
Kevin Wolf791bfa32012-12-04 16:35:12 +0100725 .args = "[-bcCpqz] [-P pattern ] off len",
Devin Nakamura43642b32011-07-11 11:22:16 -0400726 .oneline = "writes a number of bytes at a specified offset",
727 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000728};
729
Devin Nakamura43642b32011-07-11 11:22:16 -0400730static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000731{
Devin Nakamura43642b32011-07-11 11:22:16 -0400732 struct timeval t1, t2;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000733 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
Kevin Wolf791bfa32012-12-04 16:35:12 +0100734 int cflag = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400735 int c, cnt;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000736 char *buf = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400737 int64_t offset;
738 int count;
739 /* Some compilers get confused and warn if this is not initialized. */
740 int total = 0;
741 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000742
Kevin Wolf791bfa32012-12-04 16:35:12 +0100743 while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400744 switch (c) {
745 case 'b':
746 bflag = 1;
747 break;
Kevin Wolf791bfa32012-12-04 16:35:12 +0100748 case 'c':
749 cflag = 1;
750 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400751 case 'C':
752 Cflag = 1;
753 break;
754 case 'p':
755 pflag = 1;
756 break;
757 case 'P':
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000758 Pflag = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400759 pattern = parse_pattern(optarg);
760 if (pattern < 0) {
761 return 0;
762 }
763 break;
764 case 'q':
765 qflag = 1;
766 break;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000767 case 'z':
768 zflag = 1;
769 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400770 default:
771 return command_usage(&write_cmd);
772 }
773 }
aliguorie3aff4f2009-04-05 19:14:04 +0000774
Devin Nakamura43642b32011-07-11 11:22:16 -0400775 if (optind != argc - 2) {
776 return command_usage(&write_cmd);
777 }
aliguorie3aff4f2009-04-05 19:14:04 +0000778
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000779 if (bflag + pflag + zflag > 1) {
780 printf("-b, -p, or -z cannot be specified at the same time\n");
781 return 0;
782 }
783
784 if (zflag && Pflag) {
785 printf("-z and -P cannot be specified at the same time\n");
Devin Nakamura43642b32011-07-11 11:22:16 -0400786 return 0;
787 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200788
Devin Nakamura43642b32011-07-11 11:22:16 -0400789 offset = cvtnum(argv[optind]);
790 if (offset < 0) {
791 printf("non-numeric length argument -- %s\n", argv[optind]);
792 return 0;
793 }
aliguorie3aff4f2009-04-05 19:14:04 +0000794
Devin Nakamura43642b32011-07-11 11:22:16 -0400795 optind++;
796 count = cvtnum(argv[optind]);
797 if (count < 0) {
798 printf("non-numeric length argument -- %s\n", argv[optind]);
799 return 0;
800 }
aliguorie3aff4f2009-04-05 19:14:04 +0000801
Devin Nakamura43642b32011-07-11 11:22:16 -0400802 if (!pflag) {
803 if (offset & 0x1ff) {
804 printf("offset %" PRId64 " is not sector aligned\n",
805 offset);
806 return 0;
807 }
aliguorie3aff4f2009-04-05 19:14:04 +0000808
Devin Nakamura43642b32011-07-11 11:22:16 -0400809 if (count & 0x1ff) {
810 printf("count %d is not sector aligned\n",
811 count);
812 return 0;
813 }
814 }
aliguorie3aff4f2009-04-05 19:14:04 +0000815
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000816 if (!zflag) {
817 buf = qemu_io_alloc(count, pattern);
818 }
aliguorie3aff4f2009-04-05 19:14:04 +0000819
Devin Nakamura43642b32011-07-11 11:22:16 -0400820 gettimeofday(&t1, NULL);
821 if (pflag) {
822 cnt = do_pwrite(buf, offset, count, &total);
823 } else if (bflag) {
824 cnt = do_save_vmstate(buf, offset, count, &total);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000825 } else if (zflag) {
826 cnt = do_co_write_zeroes(offset, count, &total);
Kevin Wolf791bfa32012-12-04 16:35:12 +0100827 } else if (cflag) {
828 cnt = do_write_compressed(buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400829 } else {
830 cnt = do_write(buf, offset, count, &total);
831 }
832 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000833
Devin Nakamura43642b32011-07-11 11:22:16 -0400834 if (cnt < 0) {
835 printf("write failed: %s\n", strerror(-cnt));
836 goto out;
837 }
aliguorie3aff4f2009-04-05 19:14:04 +0000838
Devin Nakamura43642b32011-07-11 11:22:16 -0400839 if (qflag) {
840 goto out;
841 }
aliguorie3aff4f2009-04-05 19:14:04 +0000842
Devin Nakamura43642b32011-07-11 11:22:16 -0400843 /* Finally, report back -- -C gives a parsable format */
844 t2 = tsub(t2, t1);
845 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000846
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200847out:
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000848 if (!zflag) {
849 qemu_io_free(buf);
850 }
aliguorie3aff4f2009-04-05 19:14:04 +0000851
Devin Nakamura43642b32011-07-11 11:22:16 -0400852 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000853}
854
aliguorie3aff4f2009-04-05 19:14:04 +0000855static void
856writev_help(void)
857{
Devin Nakamura43642b32011-07-11 11:22:16 -0400858 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000859"\n"
860" writes a range of bytes from the given offset source from multiple buffers\n"
861"\n"
862" Example:\n"
863" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
864"\n"
865" Writes into a segment of the currently open file, using a buffer\n"
866" filled with a set pattern (0xcdcdcdcd).\n"
867" -P, -- use different pattern to fill file\n"
868" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100869" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000870"\n");
871}
872
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000873static int writev_f(int argc, char **argv);
874
875static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400876 .name = "writev",
877 .cfunc = writev_f,
878 .argmin = 2,
879 .argmax = -1,
880 .args = "[-Cq] [-P pattern ] off len [len..]",
881 .oneline = "writes a number of bytes at a specified offset",
882 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000883};
884
Devin Nakamura43642b32011-07-11 11:22:16 -0400885static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000886{
Devin Nakamura43642b32011-07-11 11:22:16 -0400887 struct timeval t1, t2;
888 int Cflag = 0, qflag = 0;
889 int c, cnt;
890 char *buf;
891 int64_t offset;
892 /* Some compilers get confused and warn if this is not initialized. */
893 int total = 0;
894 int nr_iov;
895 int pattern = 0xcd;
896 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000897
Devin Nakamura43642b32011-07-11 11:22:16 -0400898 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
899 switch (c) {
900 case 'C':
901 Cflag = 1;
902 break;
903 case 'q':
904 qflag = 1;
905 break;
906 case 'P':
907 pattern = parse_pattern(optarg);
908 if (pattern < 0) {
909 return 0;
910 }
911 break;
912 default:
913 return command_usage(&writev_cmd);
914 }
915 }
aliguorie3aff4f2009-04-05 19:14:04 +0000916
Devin Nakamura43642b32011-07-11 11:22:16 -0400917 if (optind > argc - 2) {
918 return command_usage(&writev_cmd);
919 }
aliguorie3aff4f2009-04-05 19:14:04 +0000920
Devin Nakamura43642b32011-07-11 11:22:16 -0400921 offset = cvtnum(argv[optind]);
922 if (offset < 0) {
923 printf("non-numeric length argument -- %s\n", argv[optind]);
924 return 0;
925 }
926 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000927
Devin Nakamura43642b32011-07-11 11:22:16 -0400928 if (offset & 0x1ff) {
929 printf("offset %" PRId64 " is not sector aligned\n",
930 offset);
931 return 0;
932 }
aliguorie3aff4f2009-04-05 19:14:04 +0000933
Devin Nakamura43642b32011-07-11 11:22:16 -0400934 nr_iov = argc - optind;
935 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100936 if (buf == NULL) {
937 return 0;
938 }
aliguorie3aff4f2009-04-05 19:14:04 +0000939
Devin Nakamura43642b32011-07-11 11:22:16 -0400940 gettimeofday(&t1, NULL);
941 cnt = do_aio_writev(&qiov, offset, &total);
942 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000943
Devin Nakamura43642b32011-07-11 11:22:16 -0400944 if (cnt < 0) {
945 printf("writev failed: %s\n", strerror(-cnt));
946 goto out;
947 }
aliguorie3aff4f2009-04-05 19:14:04 +0000948
Devin Nakamura43642b32011-07-11 11:22:16 -0400949 if (qflag) {
950 goto out;
951 }
aliguorie3aff4f2009-04-05 19:14:04 +0000952
Devin Nakamura43642b32011-07-11 11:22:16 -0400953 /* Finally, report back -- -C gives a parsable format */
954 t2 = tsub(t2, t1);
955 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200956out:
Kevin Wolf9e559532012-07-02 15:13:53 +0200957 qemu_iovec_destroy(&qiov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400958 qemu_io_free(buf);
959 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000960}
961
Devin Nakamura43642b32011-07-11 11:22:16 -0400962static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200963{
Devin Nakamura43642b32011-07-11 11:22:16 -0400964 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200965"\n"
966" writes a range of bytes from the given offset source from multiple buffers,\n"
967" in a batch of requests that may be merged by qemu\n"
968"\n"
969" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200970" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200971" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
972"\n"
973" Writes into a segment of the currently open file, using a buffer\n"
974" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
975" by one for each request contained in the multiwrite command.\n"
976" -P, -- use different pattern to fill file\n"
977" -C, -- report statistics in a machine parsable format\n"
978" -q, -- quiet mode, do not show I/O statistics\n"
979"\n");
980}
981
982static int multiwrite_f(int argc, char **argv);
983
984static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400985 .name = "multiwrite",
986 .cfunc = multiwrite_f,
987 .argmin = 2,
988 .argmax = -1,
989 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
990 .oneline = "issues multiple write requests at once",
991 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200992};
993
Devin Nakamura43642b32011-07-11 11:22:16 -0400994static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200995{
Devin Nakamura43642b32011-07-11 11:22:16 -0400996 struct timeval t1, t2;
997 int Cflag = 0, qflag = 0;
998 int c, cnt;
999 char **buf;
1000 int64_t offset, first_offset = 0;
1001 /* Some compilers get confused and warn if this is not initialized. */
1002 int total = 0;
1003 int nr_iov;
1004 int nr_reqs;
1005 int pattern = 0xcd;
1006 QEMUIOVector *qiovs;
1007 int i;
1008 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001009
Devin Nakamura43642b32011-07-11 11:22:16 -04001010 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1011 switch (c) {
1012 case 'C':
1013 Cflag = 1;
1014 break;
1015 case 'q':
1016 qflag = 1;
1017 break;
1018 case 'P':
1019 pattern = parse_pattern(optarg);
1020 if (pattern < 0) {
1021 return 0;
1022 }
1023 break;
1024 default:
1025 return command_usage(&writev_cmd);
1026 }
1027 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001028
Devin Nakamura43642b32011-07-11 11:22:16 -04001029 if (optind > argc - 2) {
1030 return command_usage(&writev_cmd);
1031 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001032
Devin Nakamura43642b32011-07-11 11:22:16 -04001033 nr_reqs = 1;
1034 for (i = optind; i < argc; i++) {
1035 if (!strcmp(argv[i], ";")) {
1036 nr_reqs++;
1037 }
1038 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001039
Kevin Wolff2360622011-10-31 11:36:32 +01001040 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1041 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -05001042 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001043
Kevin Wolf67403db2011-10-31 11:49:21 +01001044 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001045 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001046
Devin Nakamura43642b32011-07-11 11:22:16 -04001047 /* Read the offset of the request */
1048 offset = cvtnum(argv[optind]);
1049 if (offset < 0) {
1050 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +01001051 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001052 }
1053 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001054
Devin Nakamura43642b32011-07-11 11:22:16 -04001055 if (offset & 0x1ff) {
1056 printf("offset %lld is not sector aligned\n",
1057 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +01001058 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001059 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001060
1061 if (i == 0) {
1062 first_offset = offset;
1063 }
1064
Devin Nakamura43642b32011-07-11 11:22:16 -04001065 /* Read lengths for qiov entries */
1066 for (j = optind; j < argc; j++) {
1067 if (!strcmp(argv[j], ";")) {
1068 break;
1069 }
1070 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001071
Devin Nakamura43642b32011-07-11 11:22:16 -04001072 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001073
Devin Nakamura43642b32011-07-11 11:22:16 -04001074 /* Build request */
Kevin Wolff2360622011-10-31 11:36:32 +01001075 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1076 if (buf[i] == NULL) {
1077 goto out;
1078 }
1079
Devin Nakamura43642b32011-07-11 11:22:16 -04001080 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001081 reqs[i].sector = offset >> 9;
1082 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001083
Devin Nakamura43642b32011-07-11 11:22:16 -04001084 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001085
Devin Nakamura43642b32011-07-11 11:22:16 -04001086 pattern++;
1087 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001088
Kevin Wolf67403db2011-10-31 11:49:21 +01001089 /* If there were empty requests at the end, ignore them */
1090 nr_reqs = i;
1091
Devin Nakamura43642b32011-07-11 11:22:16 -04001092 gettimeofday(&t1, NULL);
1093 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1094 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001095
Devin Nakamura43642b32011-07-11 11:22:16 -04001096 if (cnt < 0) {
1097 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1098 goto out;
1099 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001100
Devin Nakamura43642b32011-07-11 11:22:16 -04001101 if (qflag) {
1102 goto out;
1103 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001104
Devin Nakamura43642b32011-07-11 11:22:16 -04001105 /* Finally, report back -- -C gives a parsable format */
1106 t2 = tsub(t2, t1);
1107 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001108out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001109 for (i = 0; i < nr_reqs; i++) {
1110 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001111 if (reqs[i].qiov != NULL) {
1112 qemu_iovec_destroy(&qiovs[i]);
1113 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001114 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001115 g_free(buf);
1116 g_free(reqs);
1117 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001118 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001119}
1120
Christoph Hellwig95533d52009-06-20 21:10:44 +02001121struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001122 QEMUIOVector qiov;
1123 int64_t offset;
1124 char *buf;
1125 int qflag;
1126 int vflag;
1127 int Cflag;
1128 int Pflag;
1129 int pattern;
1130 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001131};
1132
Devin Nakamura43642b32011-07-11 11:22:16 -04001133static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001134{
Devin Nakamura43642b32011-07-11 11:22:16 -04001135 struct aio_ctx *ctx = opaque;
1136 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001137
Devin Nakamura43642b32011-07-11 11:22:16 -04001138 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001139
Christoph Hellwig95533d52009-06-20 21:10:44 +02001140
Devin Nakamura43642b32011-07-11 11:22:16 -04001141 if (ret < 0) {
1142 printf("aio_write failed: %s\n", strerror(-ret));
1143 goto out;
1144 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001145
Devin Nakamura43642b32011-07-11 11:22:16 -04001146 if (ctx->qflag) {
1147 goto out;
1148 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001149
Devin Nakamura43642b32011-07-11 11:22:16 -04001150 /* Finally, report back -- -C gives a parsable format */
1151 t2 = tsub(t2, ctx->t1);
1152 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1153 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001154out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001155 qemu_io_free(ctx->buf);
Kevin Wolf9e559532012-07-02 15:13:53 +02001156 qemu_iovec_destroy(&ctx->qiov);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001157 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001158}
1159
Devin Nakamura43642b32011-07-11 11:22:16 -04001160static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001161{
Devin Nakamura43642b32011-07-11 11:22:16 -04001162 struct aio_ctx *ctx = opaque;
1163 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001164
Devin Nakamura43642b32011-07-11 11:22:16 -04001165 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001166
Devin Nakamura43642b32011-07-11 11:22:16 -04001167 if (ret < 0) {
1168 printf("readv failed: %s\n", strerror(-ret));
1169 goto out;
1170 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001171
Devin Nakamura43642b32011-07-11 11:22:16 -04001172 if (ctx->Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001173 void *cmp_buf = g_malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001174
Devin Nakamura43642b32011-07-11 11:22:16 -04001175 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1176 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1177 printf("Pattern verification failed at offset %"
1178 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1179 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001180 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -04001181 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001182
Devin Nakamura43642b32011-07-11 11:22:16 -04001183 if (ctx->qflag) {
1184 goto out;
1185 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001186
Devin Nakamura43642b32011-07-11 11:22:16 -04001187 if (ctx->vflag) {
1188 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1189 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001190
Devin Nakamura43642b32011-07-11 11:22:16 -04001191 /* Finally, report back -- -C gives a parsable format */
1192 t2 = tsub(t2, ctx->t1);
1193 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1194 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001195out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001196 qemu_io_free(ctx->buf);
Kevin Wolf9e559532012-07-02 15:13:53 +02001197 qemu_iovec_destroy(&ctx->qiov);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001198 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001199}
1200
Devin Nakamura43642b32011-07-11 11:22:16 -04001201static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001202{
Devin Nakamura43642b32011-07-11 11:22:16 -04001203 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001204"\n"
1205" asynchronously reads a range of bytes from the given offset\n"
1206"\n"
1207" Example:\n"
1208" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1209"\n"
1210" Reads a segment of the currently open file, optionally dumping it to the\n"
1211" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001212" The read is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001213" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001214" -C, -- report statistics in a machine parsable format\n"
1215" -P, -- use a pattern to verify read data\n"
1216" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001217" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001218"\n");
1219}
1220
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001221static int aio_read_f(int argc, char **argv);
1222
1223static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001224 .name = "aio_read",
1225 .cfunc = aio_read_f,
1226 .argmin = 2,
1227 .argmax = -1,
1228 .args = "[-Cqv] [-P pattern ] off len [len..]",
1229 .oneline = "asynchronously reads a number of bytes",
1230 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001231};
1232
Devin Nakamura43642b32011-07-11 11:22:16 -04001233static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001234{
Devin Nakamura43642b32011-07-11 11:22:16 -04001235 int nr_iov, c;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001236 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001237
Devin Nakamura43642b32011-07-11 11:22:16 -04001238 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1239 switch (c) {
1240 case 'C':
1241 ctx->Cflag = 1;
1242 break;
1243 case 'P':
1244 ctx->Pflag = 1;
1245 ctx->pattern = parse_pattern(optarg);
1246 if (ctx->pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001247 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001248 return 0;
1249 }
1250 break;
1251 case 'q':
1252 ctx->qflag = 1;
1253 break;
1254 case 'v':
1255 ctx->vflag = 1;
1256 break;
1257 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001258 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001259 return command_usage(&aio_read_cmd);
1260 }
1261 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001262
Devin Nakamura43642b32011-07-11 11:22:16 -04001263 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001264 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001265 return command_usage(&aio_read_cmd);
1266 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001267
Devin Nakamura43642b32011-07-11 11:22:16 -04001268 ctx->offset = cvtnum(argv[optind]);
1269 if (ctx->offset < 0) {
1270 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001271 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001272 return 0;
1273 }
1274 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001275
Devin Nakamura43642b32011-07-11 11:22:16 -04001276 if (ctx->offset & 0x1ff) {
1277 printf("offset %" PRId64 " is not sector aligned\n",
1278 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001279 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001280 return 0;
1281 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001282
Devin Nakamura43642b32011-07-11 11:22:16 -04001283 nr_iov = argc - optind;
1284 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001285 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001286 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001287 return 0;
1288 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001289
Devin Nakamura43642b32011-07-11 11:22:16 -04001290 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001291 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1292 ctx->qiov.size >> 9, aio_read_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001293 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001294}
1295
Devin Nakamura43642b32011-07-11 11:22:16 -04001296static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001297{
Devin Nakamura43642b32011-07-11 11:22:16 -04001298 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001299"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001300" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001301" from multiple buffers\n"
1302"\n"
1303" Example:\n"
1304" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1305"\n"
1306" Writes into a segment of the currently open file, using a buffer\n"
1307" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001308" The write is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001309" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001310" -P, -- use different pattern to fill file\n"
1311" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001312" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001313"\n");
1314}
1315
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001316static int aio_write_f(int argc, char **argv);
1317
1318static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001319 .name = "aio_write",
1320 .cfunc = aio_write_f,
1321 .argmin = 2,
1322 .argmax = -1,
1323 .args = "[-Cq] [-P pattern ] off len [len..]",
1324 .oneline = "asynchronously writes a number of bytes",
1325 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001326};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001327
Devin Nakamura43642b32011-07-11 11:22:16 -04001328static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001329{
Devin Nakamura43642b32011-07-11 11:22:16 -04001330 int nr_iov, c;
1331 int pattern = 0xcd;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001332 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001333
Devin Nakamura43642b32011-07-11 11:22:16 -04001334 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1335 switch (c) {
1336 case 'C':
1337 ctx->Cflag = 1;
1338 break;
1339 case 'q':
1340 ctx->qflag = 1;
1341 break;
1342 case 'P':
1343 pattern = parse_pattern(optarg);
1344 if (pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001345 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001346 return 0;
1347 }
1348 break;
1349 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001350 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001351 return command_usage(&aio_write_cmd);
1352 }
1353 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001354
Devin Nakamura43642b32011-07-11 11:22:16 -04001355 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001356 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001357 return command_usage(&aio_write_cmd);
1358 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001359
Devin Nakamura43642b32011-07-11 11:22:16 -04001360 ctx->offset = cvtnum(argv[optind]);
1361 if (ctx->offset < 0) {
1362 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001363 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001364 return 0;
1365 }
1366 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001367
Devin Nakamura43642b32011-07-11 11:22:16 -04001368 if (ctx->offset & 0x1ff) {
1369 printf("offset %" PRId64 " is not sector aligned\n",
1370 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001371 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001372 return 0;
1373 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001374
Devin Nakamura43642b32011-07-11 11:22:16 -04001375 nr_iov = argc - optind;
1376 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001377 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001378 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001379 return 0;
1380 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001381
Devin Nakamura43642b32011-07-11 11:22:16 -04001382 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001383 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1384 ctx->qiov.size >> 9, aio_write_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001385 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001386}
1387
Devin Nakamura43642b32011-07-11 11:22:16 -04001388static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001389{
Kevin Wolfe7c8b092012-11-13 16:41:22 +01001390 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04001391 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001392}
1393
1394static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001395 .name = "aio_flush",
1396 .cfunc = aio_flush_f,
1397 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001398};
1399
Devin Nakamura43642b32011-07-11 11:22:16 -04001400static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001401{
Devin Nakamura43642b32011-07-11 11:22:16 -04001402 bdrv_flush(bs);
1403 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001404}
1405
1406static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001407 .name = "flush",
1408 .altname = "f",
1409 .cfunc = flush_f,
1410 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001411};
1412
Devin Nakamura43642b32011-07-11 11:22:16 -04001413static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001414{
Devin Nakamura43642b32011-07-11 11:22:16 -04001415 int64_t offset;
1416 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001417
Devin Nakamura43642b32011-07-11 11:22:16 -04001418 offset = cvtnum(argv[1]);
1419 if (offset < 0) {
1420 printf("non-numeric truncate argument -- %s\n", argv[1]);
1421 return 0;
1422 }
aliguorie3aff4f2009-04-05 19:14:04 +00001423
Devin Nakamura43642b32011-07-11 11:22:16 -04001424 ret = bdrv_truncate(bs, offset);
1425 if (ret < 0) {
1426 printf("truncate: %s\n", strerror(-ret));
1427 return 0;
1428 }
aliguorie3aff4f2009-04-05 19:14:04 +00001429
Devin Nakamura43642b32011-07-11 11:22:16 -04001430 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001431}
1432
1433static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001434 .name = "truncate",
1435 .altname = "t",
1436 .cfunc = truncate_f,
1437 .argmin = 1,
1438 .argmax = 1,
1439 .args = "off",
1440 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001441};
1442
Devin Nakamura43642b32011-07-11 11:22:16 -04001443static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001444{
Devin Nakamura43642b32011-07-11 11:22:16 -04001445 int64_t size;
1446 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001447
Devin Nakamura43642b32011-07-11 11:22:16 -04001448 size = bdrv_getlength(bs);
1449 if (size < 0) {
1450 printf("getlength: %s\n", strerror(-size));
1451 return 0;
1452 }
aliguorie3aff4f2009-04-05 19:14:04 +00001453
Devin Nakamura43642b32011-07-11 11:22:16 -04001454 cvtstr(size, s1, sizeof(s1));
1455 printf("%s\n", s1);
1456 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001457}
1458
1459
1460static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001461 .name = "length",
1462 .altname = "l",
1463 .cfunc = length_f,
1464 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001465};
1466
1467
Devin Nakamura43642b32011-07-11 11:22:16 -04001468static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001469{
Devin Nakamura43642b32011-07-11 11:22:16 -04001470 BlockDriverInfo bdi;
1471 char s1[64], s2[64];
1472 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001473
Devin Nakamura43642b32011-07-11 11:22:16 -04001474 if (bs->drv && bs->drv->format_name) {
1475 printf("format name: %s\n", bs->drv->format_name);
1476 }
1477 if (bs->drv && bs->drv->protocol_name) {
1478 printf("format name: %s\n", bs->drv->protocol_name);
1479 }
aliguorie3aff4f2009-04-05 19:14:04 +00001480
Devin Nakamura43642b32011-07-11 11:22:16 -04001481 ret = bdrv_get_info(bs, &bdi);
1482 if (ret) {
1483 return 0;
1484 }
aliguorie3aff4f2009-04-05 19:14:04 +00001485
Devin Nakamura43642b32011-07-11 11:22:16 -04001486 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1487 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001488
Devin Nakamura43642b32011-07-11 11:22:16 -04001489 printf("cluster size: %s\n", s1);
1490 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001491
Devin Nakamura43642b32011-07-11 11:22:16 -04001492 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001493}
1494
1495
1496
1497static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001498 .name = "info",
1499 .altname = "i",
1500 .cfunc = info_f,
1501 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001502};
1503
Devin Nakamura43642b32011-07-11 11:22:16 -04001504static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001505{
Devin Nakamura43642b32011-07-11 11:22:16 -04001506 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001507"\n"
1508" discards a range of bytes from the given offset\n"
1509"\n"
1510" Example:\n"
1511" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1512"\n"
1513" Discards a segment of the currently open file.\n"
1514" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001515" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001516"\n");
1517}
1518
1519static int discard_f(int argc, char **argv);
1520
1521static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001522 .name = "discard",
1523 .altname = "d",
1524 .cfunc = discard_f,
1525 .argmin = 2,
1526 .argmax = -1,
1527 .args = "[-Cq] off len",
1528 .oneline = "discards a number of bytes at a specified offset",
1529 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001530};
1531
Devin Nakamura43642b32011-07-11 11:22:16 -04001532static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001533{
Devin Nakamura43642b32011-07-11 11:22:16 -04001534 struct timeval t1, t2;
1535 int Cflag = 0, qflag = 0;
1536 int c, ret;
1537 int64_t offset;
1538 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001539
Devin Nakamura43642b32011-07-11 11:22:16 -04001540 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1541 switch (c) {
1542 case 'C':
1543 Cflag = 1;
1544 break;
1545 case 'q':
1546 qflag = 1;
1547 break;
1548 default:
1549 return command_usage(&discard_cmd);
1550 }
1551 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001552
Devin Nakamura43642b32011-07-11 11:22:16 -04001553 if (optind != argc - 2) {
1554 return command_usage(&discard_cmd);
1555 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001556
Devin Nakamura43642b32011-07-11 11:22:16 -04001557 offset = cvtnum(argv[optind]);
1558 if (offset < 0) {
1559 printf("non-numeric length argument -- %s\n", argv[optind]);
1560 return 0;
1561 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001562
Devin Nakamura43642b32011-07-11 11:22:16 -04001563 optind++;
1564 count = cvtnum(argv[optind]);
1565 if (count < 0) {
1566 printf("non-numeric length argument -- %s\n", argv[optind]);
1567 return 0;
1568 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001569
Devin Nakamura43642b32011-07-11 11:22:16 -04001570 gettimeofday(&t1, NULL);
1571 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1572 count >> BDRV_SECTOR_BITS);
1573 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001574
Devin Nakamura43642b32011-07-11 11:22:16 -04001575 if (ret < 0) {
1576 printf("discard failed: %s\n", strerror(-ret));
1577 goto out;
1578 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001579
Devin Nakamura43642b32011-07-11 11:22:16 -04001580 /* Finally, report back -- -C gives a parsable format */
1581 if (!qflag) {
1582 t2 = tsub(t2, t1);
1583 print_report("discard", &t2, offset, count, count, 1, Cflag);
1584 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001585
1586out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001587 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001588}
1589
Devin Nakamura43642b32011-07-11 11:22:16 -04001590static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001591{
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001592 int64_t offset, sector_num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001593 int nb_sectors, remaining;
1594 char s1[64];
1595 int num, sum_alloc;
1596 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001597
Devin Nakamura43642b32011-07-11 11:22:16 -04001598 offset = cvtnum(argv[1]);
1599 if (offset & 0x1ff) {
1600 printf("offset %" PRId64 " is not sector aligned\n",
1601 offset);
1602 return 0;
1603 }
aliguorie3aff4f2009-04-05 19:14:04 +00001604
Devin Nakamura43642b32011-07-11 11:22:16 -04001605 if (argc == 3) {
1606 nb_sectors = cvtnum(argv[2]);
1607 } else {
1608 nb_sectors = 1;
1609 }
aliguorie3aff4f2009-04-05 19:14:04 +00001610
Devin Nakamura43642b32011-07-11 11:22:16 -04001611 remaining = nb_sectors;
1612 sum_alloc = 0;
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001613 sector_num = offset >> 9;
Devin Nakamura43642b32011-07-11 11:22:16 -04001614 while (remaining) {
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001615 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1616 sector_num += num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001617 remaining -= num;
1618 if (ret) {
1619 sum_alloc += num;
1620 }
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001621 if (num == 0) {
1622 nb_sectors -= remaining;
1623 remaining = 0;
1624 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001625 }
aliguorie3aff4f2009-04-05 19:14:04 +00001626
Devin Nakamura43642b32011-07-11 11:22:16 -04001627 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001628
Devin Nakamura43642b32011-07-11 11:22:16 -04001629 printf("%d/%d sectors allocated at offset %s\n",
1630 sum_alloc, nb_sectors, s1);
1631 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001632}
1633
1634static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001635 .name = "alloc",
1636 .altname = "a",
1637 .argmin = 1,
1638 .argmax = 2,
1639 .cfunc = alloc_f,
1640 .args = "off [sectors]",
1641 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001642};
1643
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001644
1645static int map_is_allocated(int64_t sector_num, int64_t nb_sectors, int64_t *pnum)
1646{
1647 int num, num_checked;
1648 int ret, firstret;
1649
1650 num_checked = MIN(nb_sectors, INT_MAX);
1651 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1652 if (ret < 0) {
1653 return ret;
1654 }
1655
1656 firstret = ret;
1657 *pnum = num;
1658
1659 while (nb_sectors > 0 && ret == firstret) {
1660 sector_num += num;
1661 nb_sectors -= num;
1662
1663 num_checked = MIN(nb_sectors, INT_MAX);
1664 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1665 if (ret == firstret) {
1666 *pnum += num;
1667 } else {
1668 break;
1669 }
1670 }
1671
1672 return firstret;
1673}
1674
Devin Nakamura43642b32011-07-11 11:22:16 -04001675static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001676{
Devin Nakamura43642b32011-07-11 11:22:16 -04001677 int64_t offset;
1678 int64_t nb_sectors;
1679 char s1[64];
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001680 int64_t num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001681 int ret;
1682 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001683
Devin Nakamura43642b32011-07-11 11:22:16 -04001684 offset = 0;
1685 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001686
Devin Nakamura43642b32011-07-11 11:22:16 -04001687 do {
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001688 ret = map_is_allocated(offset, nb_sectors, &num);
1689 if (ret < 0) {
1690 error_report("Failed to get allocation status: %s", strerror(-ret));
1691 return 0;
1692 }
1693
Devin Nakamura43642b32011-07-11 11:22:16 -04001694 retstr = ret ? " allocated" : "not allocated";
1695 cvtstr(offset << 9ULL, s1, sizeof(s1));
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001696 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1697 "at offset %s (%d)\n",
1698 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001699
Devin Nakamura43642b32011-07-11 11:22:16 -04001700 offset += num;
1701 nb_sectors -= num;
1702 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001703
Devin Nakamura43642b32011-07-11 11:22:16 -04001704 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001705}
1706
1707static const cmdinfo_t map_cmd = {
1708 .name = "map",
1709 .argmin = 0,
1710 .argmax = 0,
1711 .cfunc = map_f,
1712 .args = "",
1713 .oneline = "prints the allocated areas of a file",
1714};
1715
Kevin Wolf41c695c2012-12-06 14:32:58 +01001716static int break_f(int argc, char **argv)
1717{
1718 int ret;
1719
1720 ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1721 if (ret < 0) {
1722 printf("Could not set breakpoint: %s\n", strerror(-ret));
1723 }
1724
1725 return 0;
1726}
1727
1728static const cmdinfo_t break_cmd = {
1729 .name = "break",
1730 .argmin = 2,
1731 .argmax = 2,
1732 .cfunc = break_f,
1733 .args = "event tag",
1734 .oneline = "sets a breakpoint on event and tags the stopped "
1735 "request as tag",
1736};
1737
1738static int resume_f(int argc, char **argv)
1739{
1740 int ret;
1741
1742 ret = bdrv_debug_resume(bs, argv[1]);
1743 if (ret < 0) {
1744 printf("Could not resume request: %s\n", strerror(-ret));
1745 }
1746
1747 return 0;
1748}
1749
1750static const cmdinfo_t resume_cmd = {
1751 .name = "resume",
1752 .argmin = 1,
1753 .argmax = 1,
1754 .cfunc = resume_f,
1755 .args = "tag",
1756 .oneline = "resumes the request tagged as tag",
1757};
1758
1759static int wait_break_f(int argc, char **argv)
1760{
1761 while (!bdrv_debug_is_suspended(bs, argv[1])) {
1762 qemu_aio_wait();
1763 }
1764
1765 return 0;
1766}
1767
1768static const cmdinfo_t wait_break_cmd = {
1769 .name = "wait_break",
1770 .argmin = 1,
1771 .argmax = 1,
1772 .cfunc = wait_break_f,
1773 .args = "tag",
1774 .oneline = "waits for the suspension of a request",
1775};
1776
Stefan Hajnoczie01c30d2012-07-27 09:05:23 +01001777static int abort_f(int argc, char **argv)
1778{
1779 abort();
1780}
1781
1782static const cmdinfo_t abort_cmd = {
1783 .name = "abort",
1784 .cfunc = abort_f,
1785 .flags = CMD_NOFILE_OK,
1786 .oneline = "simulate a program crash using abort(3)",
1787};
Kevin Wolf191c2892010-09-16 13:18:08 +02001788
Devin Nakamura43642b32011-07-11 11:22:16 -04001789static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001790{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001791 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001792 bs = NULL;
1793 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001794}
1795
1796static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001797 .name = "close",
1798 .altname = "c",
1799 .cfunc = close_f,
1800 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001801};
1802
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001803static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001804{
Devin Nakamura43642b32011-07-11 11:22:16 -04001805 if (bs) {
1806 fprintf(stderr, "file open already, try 'help close'\n");
1807 return 1;
1808 }
aliguorie3aff4f2009-04-05 19:14:04 +00001809
Devin Nakamura43642b32011-07-11 11:22:16 -04001810 if (growable) {
Kevin Wolf787e4a82013-03-06 11:52:48 +01001811 if (bdrv_file_open(&bs, name, NULL, flags)) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001812 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1813 return 1;
1814 }
1815 } else {
1816 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001817
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001818 if (bdrv_open(bs, name, NULL, flags, NULL) < 0) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001819 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001820 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001821 bs = NULL;
1822 return 1;
1823 }
1824 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001825
Devin Nakamura43642b32011-07-11 11:22:16 -04001826 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001827}
1828
Devin Nakamura43642b32011-07-11 11:22:16 -04001829static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001830{
Devin Nakamura43642b32011-07-11 11:22:16 -04001831 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001832"\n"
1833" opens a new file in the requested mode\n"
1834"\n"
1835" Example:\n"
1836" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1837"\n"
1838" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001839" -r, -- open file read-only\n"
1840" -s, -- use snapshot file\n"
1841" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001842" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001843"\n");
1844}
1845
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001846static int open_f(int argc, char **argv);
1847
1848static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001849 .name = "open",
1850 .altname = "o",
1851 .cfunc = open_f,
1852 .argmin = 1,
1853 .argmax = -1,
1854 .flags = CMD_NOFILE_OK,
1855 .args = "[-Crsn] [path]",
1856 .oneline = "open the file specified by path",
1857 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001858};
aliguorie3aff4f2009-04-05 19:14:04 +00001859
Devin Nakamura43642b32011-07-11 11:22:16 -04001860static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001861{
Devin Nakamura43642b32011-07-11 11:22:16 -04001862 int flags = 0;
1863 int readonly = 0;
1864 int growable = 0;
1865 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001866
Devin Nakamura43642b32011-07-11 11:22:16 -04001867 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1868 switch (c) {
1869 case 's':
1870 flags |= BDRV_O_SNAPSHOT;
1871 break;
1872 case 'n':
1873 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1874 break;
1875 case 'r':
1876 readonly = 1;
1877 break;
1878 case 'g':
1879 growable = 1;
1880 break;
1881 default:
1882 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001883 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001884 }
aliguorie3aff4f2009-04-05 19:14:04 +00001885
Devin Nakamura43642b32011-07-11 11:22:16 -04001886 if (!readonly) {
1887 flags |= BDRV_O_RDWR;
1888 }
aliguorie3aff4f2009-04-05 19:14:04 +00001889
Devin Nakamura43642b32011-07-11 11:22:16 -04001890 if (optind != argc - 1) {
1891 return command_usage(&open_cmd);
1892 }
1893
1894 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001895}
1896
Devin Nakamura43642b32011-07-11 11:22:16 -04001897static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001898{
Devin Nakamura43642b32011-07-11 11:22:16 -04001899 if (ct->flags & CMD_FLAG_GLOBAL) {
1900 return 1;
1901 }
1902 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1903 fprintf(stderr, "no file open, try 'help open'\n");
1904 return 0;
1905 }
1906 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001907}
1908
1909static void usage(const char *name)
1910{
Devin Nakamura43642b32011-07-11 11:22:16 -04001911 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001912"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001913"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001914"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001915" -c, --cmd command to execute\n"
1916" -r, --read-only export read-only\n"
1917" -s, --snapshot use snapshot file\n"
1918" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001919" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001920" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001921" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +02001922" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001923" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001924" -h, --help display this help and exit\n"
1925" -V, --version output version information and exit\n"
1926"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001927 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001928}
1929
1930
1931int main(int argc, char **argv)
1932{
Devin Nakamura43642b32011-07-11 11:22:16 -04001933 int readonly = 0;
1934 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001935 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -04001936 const struct option lopt[] = {
1937 { "help", 0, NULL, 'h' },
1938 { "version", 0, NULL, 'V' },
1939 { "offset", 1, NULL, 'o' },
1940 { "cmd", 1, NULL, 'c' },
1941 { "read-only", 0, NULL, 'r' },
1942 { "snapshot", 0, NULL, 's' },
1943 { "nocache", 0, NULL, 'n' },
1944 { "misalign", 0, NULL, 'm' },
1945 { "growable", 0, NULL, 'g' },
1946 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001947 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +02001948 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001949 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -04001950 { NULL, 0, NULL, 0 }
1951 };
1952 int c;
1953 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001954 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +00001955
Devin Nakamura43642b32011-07-11 11:22:16 -04001956 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001957
Devin Nakamura43642b32011-07-11 11:22:16 -04001958 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1959 switch (c) {
1960 case 's':
1961 flags |= BDRV_O_SNAPSHOT;
1962 break;
1963 case 'n':
1964 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1965 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001966 case 'd':
1967 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
1968 error_report("Invalid discard option: %s", optarg);
1969 exit(1);
1970 }
1971 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04001972 case 'c':
1973 add_user_command(optarg);
1974 break;
1975 case 'r':
1976 readonly = 1;
1977 break;
1978 case 'm':
1979 misalign = 1;
1980 break;
1981 case 'g':
1982 growable = 1;
1983 break;
1984 case 'k':
1985 flags |= BDRV_O_NATIVE_AIO;
1986 break;
Kevin Wolf592fa072012-04-18 12:07:39 +02001987 case 't':
1988 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
1989 error_report("Invalid cache option: %s", optarg);
1990 exit(1);
1991 }
1992 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001993 case 'T':
1994 if (!trace_backend_init(optarg, NULL)) {
1995 exit(1); /* error message will have been printed */
1996 }
1997 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04001998 case 'V':
1999 printf("%s version %s\n", progname, VERSION);
2000 exit(0);
2001 case 'h':
2002 usage(progname);
2003 exit(0);
2004 default:
2005 usage(progname);
2006 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02002007 }
Devin Nakamura43642b32011-07-11 11:22:16 -04002008 }
aliguorie3aff4f2009-04-05 19:14:04 +00002009
Devin Nakamura43642b32011-07-11 11:22:16 -04002010 if ((argc - optind) > 1) {
2011 usage(progname);
2012 exit(1);
2013 }
aliguorie3aff4f2009-04-05 19:14:04 +00002014
Zhi Yong Wua57d1142012-02-19 22:24:59 +08002015 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +01002016 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +08002017
Devin Nakamura43642b32011-07-11 11:22:16 -04002018 /* initialize commands */
2019 quit_init();
2020 help_init();
2021 add_command(&open_cmd);
2022 add_command(&close_cmd);
2023 add_command(&read_cmd);
2024 add_command(&readv_cmd);
2025 add_command(&write_cmd);
2026 add_command(&writev_cmd);
2027 add_command(&multiwrite_cmd);
2028 add_command(&aio_read_cmd);
2029 add_command(&aio_write_cmd);
2030 add_command(&aio_flush_cmd);
2031 add_command(&flush_cmd);
2032 add_command(&truncate_cmd);
2033 add_command(&length_cmd);
2034 add_command(&info_cmd);
2035 add_command(&discard_cmd);
2036 add_command(&alloc_cmd);
2037 add_command(&map_cmd);
Kevin Wolf41c695c2012-12-06 14:32:58 +01002038 add_command(&break_cmd);
2039 add_command(&resume_cmd);
2040 add_command(&wait_break_cmd);
Stefan Hajnoczie01c30d2012-07-27 09:05:23 +01002041 add_command(&abort_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -04002042
Devin Nakamura43642b32011-07-11 11:22:16 -04002043 add_check_command(init_check_command);
2044
2045 /* open the device */
2046 if (!readonly) {
2047 flags |= BDRV_O_RDWR;
2048 }
2049
2050 if ((argc - optind) == 1) {
2051 openfile(argv[optind], flags, growable);
2052 }
2053 command_loop();
2054
2055 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002056 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -04002057 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002058 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04002059
2060 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01002061 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04002062 }
2063 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00002064}