blob: 5af887e05796750dbb36675a27698eb642f833e3 [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{
Devin Nakamura43642b32011-07-11 11:22:16 -0400133 size_t *sizes = calloc(nr_iov, sizeof(size_t));
134 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:
Devin Nakamura43642b32011-07-11 11:22:16 -0400175 free(sizes);
176 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
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200221static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
222{
Devin Nakamura43642b32011-07-11 11:22:16 -0400223 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
224 if (*total < 0) {
225 return *total;
226 }
227 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200228}
229
230static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
231{
Devin Nakamura43642b32011-07-11 11:22:16 -0400232 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
233 if (*total < 0) {
234 return *total;
235 }
236 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200237}
238
aliguorie3aff4f2009-04-05 19:14:04 +0000239#define NOT_DONE 0x7fffffff
240static void aio_rw_done(void *opaque, int ret)
241{
Devin Nakamura43642b32011-07-11 11:22:16 -0400242 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000243}
244
245static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
246{
Devin Nakamura43642b32011-07-11 11:22:16 -0400247 BlockDriverAIOCB *acb;
248 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000249
Devin Nakamura43642b32011-07-11 11:22:16 -0400250 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
251 aio_rw_done, &async_ret);
252 if (!acb) {
253 return -EIO;
254 }
255 while (async_ret == NOT_DONE) {
256 qemu_aio_wait();
257 }
aliguorie3aff4f2009-04-05 19:14:04 +0000258
Devin Nakamura43642b32011-07-11 11:22:16 -0400259 *total = qiov->size;
260 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000261}
262
263static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
264{
Devin Nakamura43642b32011-07-11 11:22:16 -0400265 BlockDriverAIOCB *acb;
266 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000267
Devin Nakamura43642b32011-07-11 11:22:16 -0400268 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
269 aio_rw_done, &async_ret);
270 if (!acb) {
271 return -EIO;
272 }
aliguorie3aff4f2009-04-05 19:14:04 +0000273
Devin Nakamura43642b32011-07-11 11:22:16 -0400274 while (async_ret == NOT_DONE) {
275 qemu_aio_wait();
276 }
aliguorie3aff4f2009-04-05 19:14:04 +0000277
Devin Nakamura43642b32011-07-11 11:22:16 -0400278 *total = qiov->size;
279 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000280}
281
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200282struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400283 int num_done;
284 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200285};
286
287static void multiwrite_cb(void *opaque, int ret)
288{
Devin Nakamura43642b32011-07-11 11:22:16 -0400289 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200290
Devin Nakamura43642b32011-07-11 11:22:16 -0400291 async_ret->num_done++;
292 if (ret < 0) {
293 async_ret->error = ret;
294 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200295}
296
297static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
298{
Devin Nakamura43642b32011-07-11 11:22:16 -0400299 int i, ret;
300 struct multiwrite_async_ret async_ret = {
301 .num_done = 0,
302 .error = 0,
303 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200304
Devin Nakamura43642b32011-07-11 11:22:16 -0400305 *total = 0;
306 for (i = 0; i < num_reqs; i++) {
307 reqs[i].cb = multiwrite_cb;
308 reqs[i].opaque = &async_ret;
309 *total += reqs[i].qiov->size;
310 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200311
Devin Nakamura43642b32011-07-11 11:22:16 -0400312 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
313 if (ret < 0) {
314 return ret;
315 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200316
Devin Nakamura43642b32011-07-11 11:22:16 -0400317 while (async_ret.num_done < num_reqs) {
318 qemu_aio_wait();
319 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200320
Devin Nakamura43642b32011-07-11 11:22:16 -0400321 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200322}
aliguorie3aff4f2009-04-05 19:14:04 +0000323
Devin Nakamura43642b32011-07-11 11:22:16 -0400324static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000325{
Devin Nakamura43642b32011-07-11 11:22:16 -0400326 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000327"\n"
328" reads a range of bytes from the given offset\n"
329"\n"
330" Example:\n"
331" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
332"\n"
333" Reads a segment of the currently open file, optionally dumping it to the\n"
334" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200335" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200336" -C, -- report statistics in a machine parsable format\n"
337" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000338" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000339" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100340" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200341" -s, -- start offset for pattern verification (only with -P)\n"
342" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000343"\n");
344}
345
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000346static int read_f(int argc, char **argv);
347
348static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400349 .name = "read",
350 .altname = "r",
351 .cfunc = read_f,
352 .argmin = 2,
353 .argmax = -1,
354 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
355 .oneline = "reads a number of bytes at a specified offset",
356 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000357};
358
Devin Nakamura43642b32011-07-11 11:22:16 -0400359static int read_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000360{
Devin Nakamura43642b32011-07-11 11:22:16 -0400361 struct timeval t1, t2;
362 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
363 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
364 int c, cnt;
365 char *buf;
366 int64_t offset;
367 int count;
368 /* Some compilers get confused and warn if this is not initialized. */
369 int total = 0;
370 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000371
Devin Nakamura43642b32011-07-11 11:22:16 -0400372 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
373 switch (c) {
374 case 'b':
375 bflag = 1;
376 break;
377 case 'C':
378 Cflag = 1;
379 break;
380 case 'l':
381 lflag = 1;
382 pattern_count = cvtnum(optarg);
383 if (pattern_count < 0) {
384 printf("non-numeric length argument -- %s\n", optarg);
385 return 0;
386 }
387 break;
388 case 'p':
389 pflag = 1;
390 break;
391 case 'P':
392 Pflag = 1;
393 pattern = parse_pattern(optarg);
394 if (pattern < 0) {
395 return 0;
396 }
397 break;
398 case 'q':
399 qflag = 1;
400 break;
401 case 's':
402 sflag = 1;
403 pattern_offset = cvtnum(optarg);
404 if (pattern_offset < 0) {
405 printf("non-numeric length argument -- %s\n", optarg);
406 return 0;
407 }
408 break;
409 case 'v':
410 vflag = 1;
411 break;
412 default:
413 return command_usage(&read_cmd);
414 }
415 }
aliguorie3aff4f2009-04-05 19:14:04 +0000416
Devin Nakamura43642b32011-07-11 11:22:16 -0400417 if (optind != argc - 2) {
418 return command_usage(&read_cmd);
419 }
aliguorie3aff4f2009-04-05 19:14:04 +0000420
Devin Nakamura43642b32011-07-11 11:22:16 -0400421 if (bflag && pflag) {
422 printf("-b and -p cannot be specified at the same time\n");
423 return 0;
424 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200425
Devin Nakamura43642b32011-07-11 11:22:16 -0400426 offset = cvtnum(argv[optind]);
427 if (offset < 0) {
428 printf("non-numeric length argument -- %s\n", argv[optind]);
429 return 0;
430 }
aliguorie3aff4f2009-04-05 19:14:04 +0000431
Devin Nakamura43642b32011-07-11 11:22:16 -0400432 optind++;
433 count = cvtnum(argv[optind]);
434 if (count < 0) {
435 printf("non-numeric length argument -- %s\n", argv[optind]);
436 return 0;
437 }
aliguorie3aff4f2009-04-05 19:14:04 +0000438
Kevin Wolfd9654a52009-04-23 15:11:13 +0200439 if (!Pflag && (lflag || sflag)) {
440 return command_usage(&read_cmd);
441 }
442
443 if (!lflag) {
444 pattern_count = count - pattern_offset;
445 }
446
447 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
448 printf("pattern verfication range exceeds end of read data\n");
449 return 0;
450 }
451
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400452 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400453 if (offset & 0x1ff) {
454 printf("offset %" PRId64 " is not sector aligned\n",
455 offset);
456 return 0;
457 }
458 if (count & 0x1ff) {
459 printf("count %d is not sector aligned\n",
460 count);
461 return 0;
462 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400463 }
aliguorie3aff4f2009-04-05 19:14:04 +0000464
Devin Nakamura43642b32011-07-11 11:22:16 -0400465 buf = qemu_io_alloc(count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000466
Devin Nakamura43642b32011-07-11 11:22:16 -0400467 gettimeofday(&t1, NULL);
468 if (pflag) {
469 cnt = do_pread(buf, offset, count, &total);
470 } else if (bflag) {
471 cnt = do_load_vmstate(buf, offset, count, &total);
472 } else {
473 cnt = do_read(buf, offset, count, &total);
474 }
475 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000476
Devin Nakamura43642b32011-07-11 11:22:16 -0400477 if (cnt < 0) {
478 printf("read failed: %s\n", strerror(-cnt));
479 goto out;
480 }
aliguorie3aff4f2009-04-05 19:14:04 +0000481
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 if (Pflag) {
483 void *cmp_buf = malloc(pattern_count);
484 memset(cmp_buf, pattern, pattern_count);
485 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
486 printf("Pattern verification failed at offset %"
487 PRId64 ", %d bytes\n",
488 offset + pattern_offset, pattern_count);
489 }
490 free(cmp_buf);
491 }
aliguorie3aff4f2009-04-05 19:14:04 +0000492
Devin Nakamura43642b32011-07-11 11:22:16 -0400493 if (qflag) {
494 goto out;
495 }
aliguoric48101a2009-04-18 15:36:23 +0000496
Devin Nakamura43642b32011-07-11 11:22:16 -0400497 if (vflag) {
498 dump_buffer(buf, offset, count);
499 }
aliguorie3aff4f2009-04-05 19:14:04 +0000500
Devin Nakamura43642b32011-07-11 11:22:16 -0400501 /* Finally, report back -- -C gives a parsable format */
502 t2 = tsub(t2, t1);
503 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000504
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200505out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400506 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000507
Devin Nakamura43642b32011-07-11 11:22:16 -0400508 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000509}
510
Devin Nakamura43642b32011-07-11 11:22:16 -0400511static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000512{
Devin Nakamura43642b32011-07-11 11:22:16 -0400513 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000514"\n"
515" reads a range of bytes from the given offset into multiple buffers\n"
516"\n"
517" Example:\n"
518" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
519"\n"
520" Reads a segment of the currently open file, optionally dumping it to the\n"
521" standard output stream (with -v option) for subsequent inspection.\n"
522" Uses multiple iovec buffers if more than one byte range is specified.\n"
523" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000524" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000525" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100526" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000527"\n");
528}
529
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000530static int readv_f(int argc, char **argv);
531
532static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400533 .name = "readv",
534 .cfunc = readv_f,
535 .argmin = 2,
536 .argmax = -1,
537 .args = "[-Cqv] [-P pattern ] off len [len..]",
538 .oneline = "reads a number of bytes at a specified offset",
539 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000540};
541
Devin Nakamura43642b32011-07-11 11:22:16 -0400542static int readv_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000543{
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 struct timeval t1, t2;
545 int Cflag = 0, qflag = 0, vflag = 0;
546 int c, cnt;
547 char *buf;
548 int64_t offset;
549 /* Some compilers get confused and warn if this is not initialized. */
550 int total = 0;
551 int nr_iov;
552 QEMUIOVector qiov;
553 int pattern = 0;
554 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000555
Devin Nakamura43642b32011-07-11 11:22:16 -0400556 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
557 switch (c) {
558 case 'C':
559 Cflag = 1;
560 break;
561 case 'P':
562 Pflag = 1;
563 pattern = parse_pattern(optarg);
564 if (pattern < 0) {
565 return 0;
566 }
567 break;
568 case 'q':
569 qflag = 1;
570 break;
571 case 'v':
572 vflag = 1;
573 break;
574 default:
575 return command_usage(&readv_cmd);
576 }
577 }
aliguorie3aff4f2009-04-05 19:14:04 +0000578
Devin Nakamura43642b32011-07-11 11:22:16 -0400579 if (optind > argc - 2) {
580 return command_usage(&readv_cmd);
581 }
aliguorie3aff4f2009-04-05 19:14:04 +0000582
583
Devin Nakamura43642b32011-07-11 11:22:16 -0400584 offset = cvtnum(argv[optind]);
585 if (offset < 0) {
586 printf("non-numeric length argument -- %s\n", argv[optind]);
587 return 0;
588 }
589 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000590
Devin Nakamura43642b32011-07-11 11:22:16 -0400591 if (offset & 0x1ff) {
592 printf("offset %" PRId64 " is not sector aligned\n",
593 offset);
594 return 0;
595 }
aliguorie3aff4f2009-04-05 19:14:04 +0000596
Devin Nakamura43642b32011-07-11 11:22:16 -0400597 nr_iov = argc - optind;
598 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000599
Devin Nakamura43642b32011-07-11 11:22:16 -0400600 gettimeofday(&t1, NULL);
601 cnt = do_aio_readv(&qiov, offset, &total);
602 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000603
Devin Nakamura43642b32011-07-11 11:22:16 -0400604 if (cnt < 0) {
605 printf("readv failed: %s\n", strerror(-cnt));
606 goto out;
607 }
aliguorie3aff4f2009-04-05 19:14:04 +0000608
Devin Nakamura43642b32011-07-11 11:22:16 -0400609 if (Pflag) {
610 void *cmp_buf = malloc(qiov.size);
611 memset(cmp_buf, pattern, qiov.size);
612 if (memcmp(buf, cmp_buf, qiov.size)) {
613 printf("Pattern verification failed at offset %"
614 PRId64 ", %zd bytes\n", offset, qiov.size);
615 }
616 free(cmp_buf);
617 }
aliguoric48101a2009-04-18 15:36:23 +0000618
Devin Nakamura43642b32011-07-11 11:22:16 -0400619 if (qflag) {
620 goto out;
621 }
aliguorie3aff4f2009-04-05 19:14:04 +0000622
Devin Nakamura43642b32011-07-11 11:22:16 -0400623 if (vflag) {
624 dump_buffer(buf, offset, qiov.size);
625 }
aliguorie3aff4f2009-04-05 19:14:04 +0000626
Devin Nakamura43642b32011-07-11 11:22:16 -0400627 /* Finally, report back -- -C gives a parsable format */
628 t2 = tsub(t2, t1);
629 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000630
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200631out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400632 qemu_io_free(buf);
633 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000634}
635
Devin Nakamura43642b32011-07-11 11:22:16 -0400636static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000637{
Devin Nakamura43642b32011-07-11 11:22:16 -0400638 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000639"\n"
640" writes a range of bytes from the given offset\n"
641"\n"
642" Example:\n"
643" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
644"\n"
645" Writes into a segment of the currently open file, using a buffer\n"
646" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200647" -b, -- write to the VM state rather than the virtual disk\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000648" -p, -- use bdrv_pwrite to write the file\n"
649" -P, -- use different pattern to fill file\n"
650" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100651" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000652"\n");
653}
654
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000655static int write_f(int argc, char **argv);
656
657static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400658 .name = "write",
659 .altname = "w",
660 .cfunc = write_f,
661 .argmin = 2,
662 .argmax = -1,
663 .args = "[-abCpq] [-P pattern ] off len",
664 .oneline = "writes a number of bytes at a specified offset",
665 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000666};
667
Devin Nakamura43642b32011-07-11 11:22:16 -0400668static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000669{
Devin Nakamura43642b32011-07-11 11:22:16 -0400670 struct timeval t1, t2;
671 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
672 int c, cnt;
673 char *buf;
674 int64_t offset;
675 int count;
676 /* Some compilers get confused and warn if this is not initialized. */
677 int total = 0;
678 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000679
Devin Nakamura43642b32011-07-11 11:22:16 -0400680 while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
681 switch (c) {
682 case 'b':
683 bflag = 1;
684 break;
685 case 'C':
686 Cflag = 1;
687 break;
688 case 'p':
689 pflag = 1;
690 break;
691 case 'P':
692 pattern = parse_pattern(optarg);
693 if (pattern < 0) {
694 return 0;
695 }
696 break;
697 case 'q':
698 qflag = 1;
699 break;
700 default:
701 return command_usage(&write_cmd);
702 }
703 }
aliguorie3aff4f2009-04-05 19:14:04 +0000704
Devin Nakamura43642b32011-07-11 11:22:16 -0400705 if (optind != argc - 2) {
706 return command_usage(&write_cmd);
707 }
aliguorie3aff4f2009-04-05 19:14:04 +0000708
Devin Nakamura43642b32011-07-11 11:22:16 -0400709 if (bflag && pflag) {
710 printf("-b and -p cannot be specified at the same time\n");
711 return 0;
712 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200713
Devin Nakamura43642b32011-07-11 11:22:16 -0400714 offset = cvtnum(argv[optind]);
715 if (offset < 0) {
716 printf("non-numeric length argument -- %s\n", argv[optind]);
717 return 0;
718 }
aliguorie3aff4f2009-04-05 19:14:04 +0000719
Devin Nakamura43642b32011-07-11 11:22:16 -0400720 optind++;
721 count = cvtnum(argv[optind]);
722 if (count < 0) {
723 printf("non-numeric length argument -- %s\n", argv[optind]);
724 return 0;
725 }
aliguorie3aff4f2009-04-05 19:14:04 +0000726
Devin Nakamura43642b32011-07-11 11:22:16 -0400727 if (!pflag) {
728 if (offset & 0x1ff) {
729 printf("offset %" PRId64 " is not sector aligned\n",
730 offset);
731 return 0;
732 }
aliguorie3aff4f2009-04-05 19:14:04 +0000733
Devin Nakamura43642b32011-07-11 11:22:16 -0400734 if (count & 0x1ff) {
735 printf("count %d is not sector aligned\n",
736 count);
737 return 0;
738 }
739 }
aliguorie3aff4f2009-04-05 19:14:04 +0000740
Devin Nakamura43642b32011-07-11 11:22:16 -0400741 buf = qemu_io_alloc(count, pattern);
aliguorie3aff4f2009-04-05 19:14:04 +0000742
Devin Nakamura43642b32011-07-11 11:22:16 -0400743 gettimeofday(&t1, NULL);
744 if (pflag) {
745 cnt = do_pwrite(buf, offset, count, &total);
746 } else if (bflag) {
747 cnt = do_save_vmstate(buf, offset, count, &total);
748 } else {
749 cnt = do_write(buf, offset, count, &total);
750 }
751 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000752
Devin Nakamura43642b32011-07-11 11:22:16 -0400753 if (cnt < 0) {
754 printf("write failed: %s\n", strerror(-cnt));
755 goto out;
756 }
aliguorie3aff4f2009-04-05 19:14:04 +0000757
Devin Nakamura43642b32011-07-11 11:22:16 -0400758 if (qflag) {
759 goto out;
760 }
aliguorie3aff4f2009-04-05 19:14:04 +0000761
Devin Nakamura43642b32011-07-11 11:22:16 -0400762 /* Finally, report back -- -C gives a parsable format */
763 t2 = tsub(t2, t1);
764 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000765
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200766out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400767 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000768
Devin Nakamura43642b32011-07-11 11:22:16 -0400769 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000770}
771
aliguorie3aff4f2009-04-05 19:14:04 +0000772static void
773writev_help(void)
774{
Devin Nakamura43642b32011-07-11 11:22:16 -0400775 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000776"\n"
777" writes a range of bytes from the given offset source from multiple buffers\n"
778"\n"
779" Example:\n"
780" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
781"\n"
782" Writes into a segment of the currently open file, using a buffer\n"
783" filled with a set pattern (0xcdcdcdcd).\n"
784" -P, -- use different pattern to fill file\n"
785" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100786" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000787"\n");
788}
789
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000790static int writev_f(int argc, char **argv);
791
792static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400793 .name = "writev",
794 .cfunc = writev_f,
795 .argmin = 2,
796 .argmax = -1,
797 .args = "[-Cq] [-P pattern ] off len [len..]",
798 .oneline = "writes a number of bytes at a specified offset",
799 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000800};
801
Devin Nakamura43642b32011-07-11 11:22:16 -0400802static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000803{
Devin Nakamura43642b32011-07-11 11:22:16 -0400804 struct timeval t1, t2;
805 int Cflag = 0, qflag = 0;
806 int c, cnt;
807 char *buf;
808 int64_t offset;
809 /* Some compilers get confused and warn if this is not initialized. */
810 int total = 0;
811 int nr_iov;
812 int pattern = 0xcd;
813 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000814
Devin Nakamura43642b32011-07-11 11:22:16 -0400815 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
816 switch (c) {
817 case 'C':
818 Cflag = 1;
819 break;
820 case 'q':
821 qflag = 1;
822 break;
823 case 'P':
824 pattern = parse_pattern(optarg);
825 if (pattern < 0) {
826 return 0;
827 }
828 break;
829 default:
830 return command_usage(&writev_cmd);
831 }
832 }
aliguorie3aff4f2009-04-05 19:14:04 +0000833
Devin Nakamura43642b32011-07-11 11:22:16 -0400834 if (optind > argc - 2) {
835 return command_usage(&writev_cmd);
836 }
aliguorie3aff4f2009-04-05 19:14:04 +0000837
Devin Nakamura43642b32011-07-11 11:22:16 -0400838 offset = cvtnum(argv[optind]);
839 if (offset < 0) {
840 printf("non-numeric length argument -- %s\n", argv[optind]);
841 return 0;
842 }
843 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000844
Devin Nakamura43642b32011-07-11 11:22:16 -0400845 if (offset & 0x1ff) {
846 printf("offset %" PRId64 " is not sector aligned\n",
847 offset);
848 return 0;
849 }
aliguorie3aff4f2009-04-05 19:14:04 +0000850
Devin Nakamura43642b32011-07-11 11:22:16 -0400851 nr_iov = argc - optind;
852 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
aliguorie3aff4f2009-04-05 19:14:04 +0000853
Devin Nakamura43642b32011-07-11 11:22:16 -0400854 gettimeofday(&t1, NULL);
855 cnt = do_aio_writev(&qiov, offset, &total);
856 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000857
Devin Nakamura43642b32011-07-11 11:22:16 -0400858 if (cnt < 0) {
859 printf("writev failed: %s\n", strerror(-cnt));
860 goto out;
861 }
aliguorie3aff4f2009-04-05 19:14:04 +0000862
Devin Nakamura43642b32011-07-11 11:22:16 -0400863 if (qflag) {
864 goto out;
865 }
aliguorie3aff4f2009-04-05 19:14:04 +0000866
Devin Nakamura43642b32011-07-11 11:22:16 -0400867 /* Finally, report back -- -C gives a parsable format */
868 t2 = tsub(t2, t1);
869 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200870out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400871 qemu_io_free(buf);
872 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000873}
874
Devin Nakamura43642b32011-07-11 11:22:16 -0400875static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200876{
Devin Nakamura43642b32011-07-11 11:22:16 -0400877 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200878"\n"
879" writes a range of bytes from the given offset source from multiple buffers,\n"
880" in a batch of requests that may be merged by qemu\n"
881"\n"
882" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200883" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200884" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
885"\n"
886" Writes into a segment of the currently open file, using a buffer\n"
887" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
888" by one for each request contained in the multiwrite command.\n"
889" -P, -- use different pattern to fill file\n"
890" -C, -- report statistics in a machine parsable format\n"
891" -q, -- quiet mode, do not show I/O statistics\n"
892"\n");
893}
894
895static int multiwrite_f(int argc, char **argv);
896
897static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400898 .name = "multiwrite",
899 .cfunc = multiwrite_f,
900 .argmin = 2,
901 .argmax = -1,
902 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
903 .oneline = "issues multiple write requests at once",
904 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200905};
906
Devin Nakamura43642b32011-07-11 11:22:16 -0400907static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200908{
Devin Nakamura43642b32011-07-11 11:22:16 -0400909 struct timeval t1, t2;
910 int Cflag = 0, qflag = 0;
911 int c, cnt;
912 char **buf;
913 int64_t offset, first_offset = 0;
914 /* Some compilers get confused and warn if this is not initialized. */
915 int total = 0;
916 int nr_iov;
917 int nr_reqs;
918 int pattern = 0xcd;
919 QEMUIOVector *qiovs;
920 int i;
921 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200922
Devin Nakamura43642b32011-07-11 11:22:16 -0400923 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
924 switch (c) {
925 case 'C':
926 Cflag = 1;
927 break;
928 case 'q':
929 qflag = 1;
930 break;
931 case 'P':
932 pattern = parse_pattern(optarg);
933 if (pattern < 0) {
934 return 0;
935 }
936 break;
937 default:
938 return command_usage(&writev_cmd);
939 }
940 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200941
Devin Nakamura43642b32011-07-11 11:22:16 -0400942 if (optind > argc - 2) {
943 return command_usage(&writev_cmd);
944 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200945
Devin Nakamura43642b32011-07-11 11:22:16 -0400946 nr_reqs = 1;
947 for (i = optind; i < argc; i++) {
948 if (!strcmp(argv[i], ";")) {
949 nr_reqs++;
950 }
951 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200952
Anthony Liguori7267c092011-08-20 22:09:37 -0500953 reqs = g_malloc(nr_reqs * sizeof(*reqs));
954 buf = g_malloc(nr_reqs * sizeof(*buf));
955 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200956
Devin Nakamura43642b32011-07-11 11:22:16 -0400957 for (i = 0; i < nr_reqs; i++) {
958 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200959
Devin Nakamura43642b32011-07-11 11:22:16 -0400960 /* Read the offset of the request */
961 offset = cvtnum(argv[optind]);
962 if (offset < 0) {
963 printf("non-numeric offset argument -- %s\n", argv[optind]);
964 return 0;
965 }
966 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200967
Devin Nakamura43642b32011-07-11 11:22:16 -0400968 if (offset & 0x1ff) {
969 printf("offset %lld is not sector aligned\n",
970 (long long)offset);
971 return 0;
972 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200973
974 if (i == 0) {
975 first_offset = offset;
976 }
977
Devin Nakamura43642b32011-07-11 11:22:16 -0400978 /* Read lengths for qiov entries */
979 for (j = optind; j < argc; j++) {
980 if (!strcmp(argv[j], ";")) {
981 break;
982 }
983 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200984
Devin Nakamura43642b32011-07-11 11:22:16 -0400985 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200986
Devin Nakamura43642b32011-07-11 11:22:16 -0400987 /* Build request */
988 reqs[i].qiov = &qiovs[i];
989 buf[i] = create_iovec(reqs[i].qiov, &argv[optind], nr_iov, pattern);
990 reqs[i].sector = offset >> 9;
991 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200992
Devin Nakamura43642b32011-07-11 11:22:16 -0400993 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200994
Devin Nakamura43642b32011-07-11 11:22:16 -0400995 pattern++;
996 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200997
Devin Nakamura43642b32011-07-11 11:22:16 -0400998 gettimeofday(&t1, NULL);
999 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1000 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001001
Devin Nakamura43642b32011-07-11 11:22:16 -04001002 if (cnt < 0) {
1003 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1004 goto out;
1005 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001006
Devin Nakamura43642b32011-07-11 11:22:16 -04001007 if (qflag) {
1008 goto out;
1009 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001010
Devin Nakamura43642b32011-07-11 11:22:16 -04001011 /* Finally, report back -- -C gives a parsable format */
1012 t2 = tsub(t2, t1);
1013 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001014out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001015 for (i = 0; i < nr_reqs; i++) {
1016 qemu_io_free(buf[i]);
1017 qemu_iovec_destroy(&qiovs[i]);
1018 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001019 g_free(buf);
1020 g_free(reqs);
1021 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001022 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001023}
1024
Christoph Hellwig95533d52009-06-20 21:10:44 +02001025struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001026 QEMUIOVector qiov;
1027 int64_t offset;
1028 char *buf;
1029 int qflag;
1030 int vflag;
1031 int Cflag;
1032 int Pflag;
1033 int pattern;
1034 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001035};
1036
Devin Nakamura43642b32011-07-11 11:22:16 -04001037static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001038{
Devin Nakamura43642b32011-07-11 11:22:16 -04001039 struct aio_ctx *ctx = opaque;
1040 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001041
Devin Nakamura43642b32011-07-11 11:22:16 -04001042 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001043
Christoph Hellwig95533d52009-06-20 21:10:44 +02001044
Devin Nakamura43642b32011-07-11 11:22:16 -04001045 if (ret < 0) {
1046 printf("aio_write failed: %s\n", strerror(-ret));
1047 goto out;
1048 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001049
Devin Nakamura43642b32011-07-11 11:22:16 -04001050 if (ctx->qflag) {
1051 goto out;
1052 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001053
Devin Nakamura43642b32011-07-11 11:22:16 -04001054 /* Finally, report back -- -C gives a parsable format */
1055 t2 = tsub(t2, ctx->t1);
1056 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1057 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001058out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001059 qemu_io_free(ctx->buf);
1060 free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001061}
1062
Devin Nakamura43642b32011-07-11 11:22:16 -04001063static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001064{
Devin Nakamura43642b32011-07-11 11:22:16 -04001065 struct aio_ctx *ctx = opaque;
1066 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001067
Devin Nakamura43642b32011-07-11 11:22:16 -04001068 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001069
Devin Nakamura43642b32011-07-11 11:22:16 -04001070 if (ret < 0) {
1071 printf("readv failed: %s\n", strerror(-ret));
1072 goto out;
1073 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001074
Devin Nakamura43642b32011-07-11 11:22:16 -04001075 if (ctx->Pflag) {
1076 void *cmp_buf = malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001077
Devin Nakamura43642b32011-07-11 11:22:16 -04001078 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1079 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1080 printf("Pattern verification failed at offset %"
1081 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1082 }
1083 free(cmp_buf);
1084 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001085
Devin Nakamura43642b32011-07-11 11:22:16 -04001086 if (ctx->qflag) {
1087 goto out;
1088 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001089
Devin Nakamura43642b32011-07-11 11:22:16 -04001090 if (ctx->vflag) {
1091 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1092 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001093
Devin Nakamura43642b32011-07-11 11:22:16 -04001094 /* Finally, report back -- -C gives a parsable format */
1095 t2 = tsub(t2, ctx->t1);
1096 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1097 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001098out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001099 qemu_io_free(ctx->buf);
1100 free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001101}
1102
Devin Nakamura43642b32011-07-11 11:22:16 -04001103static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001104{
Devin Nakamura43642b32011-07-11 11:22:16 -04001105 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001106"\n"
1107" asynchronously reads a range of bytes from the given offset\n"
1108"\n"
1109" Example:\n"
1110" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1111"\n"
1112" Reads a segment of the currently open file, optionally dumping it to the\n"
1113" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001114" The read is performed asynchronously and the aio_flush command must be\n"
1115" used to ensure all outstanding aio requests have been completed\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001116" -C, -- report statistics in a machine parsable format\n"
1117" -P, -- use a pattern to verify read data\n"
1118" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001119" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001120"\n");
1121}
1122
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001123static int aio_read_f(int argc, char **argv);
1124
1125static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001126 .name = "aio_read",
1127 .cfunc = aio_read_f,
1128 .argmin = 2,
1129 .argmax = -1,
1130 .args = "[-Cqv] [-P pattern ] off len [len..]",
1131 .oneline = "asynchronously reads a number of bytes",
1132 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001133};
1134
Devin Nakamura43642b32011-07-11 11:22:16 -04001135static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001136{
Devin Nakamura43642b32011-07-11 11:22:16 -04001137 int nr_iov, c;
1138 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1139 BlockDriverAIOCB *acb;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001140
Devin Nakamura43642b32011-07-11 11:22:16 -04001141 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1142 switch (c) {
1143 case 'C':
1144 ctx->Cflag = 1;
1145 break;
1146 case 'P':
1147 ctx->Pflag = 1;
1148 ctx->pattern = parse_pattern(optarg);
1149 if (ctx->pattern < 0) {
1150 free(ctx);
1151 return 0;
1152 }
1153 break;
1154 case 'q':
1155 ctx->qflag = 1;
1156 break;
1157 case 'v':
1158 ctx->vflag = 1;
1159 break;
1160 default:
1161 free(ctx);
1162 return command_usage(&aio_read_cmd);
1163 }
1164 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001165
Devin Nakamura43642b32011-07-11 11:22:16 -04001166 if (optind > argc - 2) {
1167 free(ctx);
1168 return command_usage(&aio_read_cmd);
1169 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001170
Devin Nakamura43642b32011-07-11 11:22:16 -04001171 ctx->offset = cvtnum(argv[optind]);
1172 if (ctx->offset < 0) {
1173 printf("non-numeric length argument -- %s\n", argv[optind]);
1174 free(ctx);
1175 return 0;
1176 }
1177 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001178
Devin Nakamura43642b32011-07-11 11:22:16 -04001179 if (ctx->offset & 0x1ff) {
1180 printf("offset %" PRId64 " is not sector aligned\n",
1181 ctx->offset);
1182 free(ctx);
1183 return 0;
1184 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001185
Devin Nakamura43642b32011-07-11 11:22:16 -04001186 nr_iov = argc - optind;
1187 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001188
Devin Nakamura43642b32011-07-11 11:22:16 -04001189 gettimeofday(&ctx->t1, NULL);
1190 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1191 ctx->qiov.size >> 9, aio_read_done, ctx);
1192 if (!acb) {
1193 free(ctx->buf);
1194 free(ctx);
1195 return -EIO;
1196 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001197
Devin Nakamura43642b32011-07-11 11:22:16 -04001198 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001199}
1200
Devin Nakamura43642b32011-07-11 11:22:16 -04001201static void aio_write_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"
Devin Nakamura43642b32011-07-11 11:22:16 -04001205" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001206" from multiple buffers\n"
1207"\n"
1208" Example:\n"
1209" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1210"\n"
1211" Writes into a segment of the currently open file, using a buffer\n"
1212" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001213" The write is performed asynchronously and the aio_flush command must be\n"
1214" used to ensure all outstanding aio requests have been completed\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001215" -P, -- use different pattern to fill file\n"
1216" -C, -- report statistics in a machine parsable format\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_write_f(int argc, char **argv);
1222
1223static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001224 .name = "aio_write",
1225 .cfunc = aio_write_f,
1226 .argmin = 2,
1227 .argmax = -1,
1228 .args = "[-Cq] [-P pattern ] off len [len..]",
1229 .oneline = "asynchronously writes a number of bytes",
1230 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001231};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001232
Devin Nakamura43642b32011-07-11 11:22:16 -04001233static int aio_write_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;
1236 int pattern = 0xcd;
1237 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1238 BlockDriverAIOCB *acb;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001239
Devin Nakamura43642b32011-07-11 11:22:16 -04001240 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1241 switch (c) {
1242 case 'C':
1243 ctx->Cflag = 1;
1244 break;
1245 case 'q':
1246 ctx->qflag = 1;
1247 break;
1248 case 'P':
1249 pattern = parse_pattern(optarg);
1250 if (pattern < 0) {
Alex Jiab1b1dad2011-09-28 14:57:01 +08001251 free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001252 return 0;
1253 }
1254 break;
1255 default:
1256 free(ctx);
1257 return command_usage(&aio_write_cmd);
1258 }
1259 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001260
Devin Nakamura43642b32011-07-11 11:22:16 -04001261 if (optind > argc - 2) {
1262 free(ctx);
1263 return command_usage(&aio_write_cmd);
1264 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001265
Devin Nakamura43642b32011-07-11 11:22:16 -04001266 ctx->offset = cvtnum(argv[optind]);
1267 if (ctx->offset < 0) {
1268 printf("non-numeric length argument -- %s\n", argv[optind]);
1269 free(ctx);
1270 return 0;
1271 }
1272 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001273
Devin Nakamura43642b32011-07-11 11:22:16 -04001274 if (ctx->offset & 0x1ff) {
1275 printf("offset %" PRId64 " is not sector aligned\n",
1276 ctx->offset);
1277 free(ctx);
1278 return 0;
1279 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001280
Devin Nakamura43642b32011-07-11 11:22:16 -04001281 nr_iov = argc - optind;
1282 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001283
Devin Nakamura43642b32011-07-11 11:22:16 -04001284 gettimeofday(&ctx->t1, NULL);
1285 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1286 ctx->qiov.size >> 9, aio_write_done, ctx);
1287 if (!acb) {
1288 free(ctx->buf);
1289 free(ctx);
1290 return -EIO;
1291 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001292
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 int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001297{
Devin Nakamura43642b32011-07-11 11:22:16 -04001298 qemu_aio_flush();
1299 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001300}
1301
1302static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001303 .name = "aio_flush",
1304 .cfunc = aio_flush_f,
1305 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001306};
1307
Devin Nakamura43642b32011-07-11 11:22:16 -04001308static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001309{
Devin Nakamura43642b32011-07-11 11:22:16 -04001310 bdrv_flush(bs);
1311 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001312}
1313
1314static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001315 .name = "flush",
1316 .altname = "f",
1317 .cfunc = flush_f,
1318 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001319};
1320
Devin Nakamura43642b32011-07-11 11:22:16 -04001321static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001322{
Devin Nakamura43642b32011-07-11 11:22:16 -04001323 int64_t offset;
1324 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001325
Devin Nakamura43642b32011-07-11 11:22:16 -04001326 offset = cvtnum(argv[1]);
1327 if (offset < 0) {
1328 printf("non-numeric truncate argument -- %s\n", argv[1]);
1329 return 0;
1330 }
aliguorie3aff4f2009-04-05 19:14:04 +00001331
Devin Nakamura43642b32011-07-11 11:22:16 -04001332 ret = bdrv_truncate(bs, offset);
1333 if (ret < 0) {
1334 printf("truncate: %s\n", strerror(-ret));
1335 return 0;
1336 }
aliguorie3aff4f2009-04-05 19:14:04 +00001337
Devin Nakamura43642b32011-07-11 11:22:16 -04001338 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001339}
1340
1341static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001342 .name = "truncate",
1343 .altname = "t",
1344 .cfunc = truncate_f,
1345 .argmin = 1,
1346 .argmax = 1,
1347 .args = "off",
1348 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001349};
1350
Devin Nakamura43642b32011-07-11 11:22:16 -04001351static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001352{
Devin Nakamura43642b32011-07-11 11:22:16 -04001353 int64_t size;
1354 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001355
Devin Nakamura43642b32011-07-11 11:22:16 -04001356 size = bdrv_getlength(bs);
1357 if (size < 0) {
1358 printf("getlength: %s\n", strerror(-size));
1359 return 0;
1360 }
aliguorie3aff4f2009-04-05 19:14:04 +00001361
Devin Nakamura43642b32011-07-11 11:22:16 -04001362 cvtstr(size, s1, sizeof(s1));
1363 printf("%s\n", s1);
1364 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001365}
1366
1367
1368static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001369 .name = "length",
1370 .altname = "l",
1371 .cfunc = length_f,
1372 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001373};
1374
1375
Devin Nakamura43642b32011-07-11 11:22:16 -04001376static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001377{
Devin Nakamura43642b32011-07-11 11:22:16 -04001378 BlockDriverInfo bdi;
1379 char s1[64], s2[64];
1380 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001381
Devin Nakamura43642b32011-07-11 11:22:16 -04001382 if (bs->drv && bs->drv->format_name) {
1383 printf("format name: %s\n", bs->drv->format_name);
1384 }
1385 if (bs->drv && bs->drv->protocol_name) {
1386 printf("format name: %s\n", bs->drv->protocol_name);
1387 }
aliguorie3aff4f2009-04-05 19:14:04 +00001388
Devin Nakamura43642b32011-07-11 11:22:16 -04001389 ret = bdrv_get_info(bs, &bdi);
1390 if (ret) {
1391 return 0;
1392 }
aliguorie3aff4f2009-04-05 19:14:04 +00001393
Devin Nakamura43642b32011-07-11 11:22:16 -04001394 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1395 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001396
Devin Nakamura43642b32011-07-11 11:22:16 -04001397 printf("cluster size: %s\n", s1);
1398 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001399
Devin Nakamura43642b32011-07-11 11:22:16 -04001400 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001401}
1402
1403
1404
1405static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001406 .name = "info",
1407 .altname = "i",
1408 .cfunc = info_f,
1409 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001410};
1411
Devin Nakamura43642b32011-07-11 11:22:16 -04001412static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001413{
Devin Nakamura43642b32011-07-11 11:22:16 -04001414 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001415"\n"
1416" discards a range of bytes from the given offset\n"
1417"\n"
1418" Example:\n"
1419" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1420"\n"
1421" Discards a segment of the currently open file.\n"
1422" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001423" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001424"\n");
1425}
1426
1427static int discard_f(int argc, char **argv);
1428
1429static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001430 .name = "discard",
1431 .altname = "d",
1432 .cfunc = discard_f,
1433 .argmin = 2,
1434 .argmax = -1,
1435 .args = "[-Cq] off len",
1436 .oneline = "discards a number of bytes at a specified offset",
1437 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001438};
1439
Devin Nakamura43642b32011-07-11 11:22:16 -04001440static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001441{
Devin Nakamura43642b32011-07-11 11:22:16 -04001442 struct timeval t1, t2;
1443 int Cflag = 0, qflag = 0;
1444 int c, ret;
1445 int64_t offset;
1446 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001447
Devin Nakamura43642b32011-07-11 11:22:16 -04001448 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1449 switch (c) {
1450 case 'C':
1451 Cflag = 1;
1452 break;
1453 case 'q':
1454 qflag = 1;
1455 break;
1456 default:
1457 return command_usage(&discard_cmd);
1458 }
1459 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001460
Devin Nakamura43642b32011-07-11 11:22:16 -04001461 if (optind != argc - 2) {
1462 return command_usage(&discard_cmd);
1463 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001464
Devin Nakamura43642b32011-07-11 11:22:16 -04001465 offset = cvtnum(argv[optind]);
1466 if (offset < 0) {
1467 printf("non-numeric length argument -- %s\n", argv[optind]);
1468 return 0;
1469 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001470
Devin Nakamura43642b32011-07-11 11:22:16 -04001471 optind++;
1472 count = cvtnum(argv[optind]);
1473 if (count < 0) {
1474 printf("non-numeric length argument -- %s\n", argv[optind]);
1475 return 0;
1476 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001477
Devin Nakamura43642b32011-07-11 11:22:16 -04001478 gettimeofday(&t1, NULL);
1479 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1480 count >> BDRV_SECTOR_BITS);
1481 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001482
Devin Nakamura43642b32011-07-11 11:22:16 -04001483 if (ret < 0) {
1484 printf("discard failed: %s\n", strerror(-ret));
1485 goto out;
1486 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001487
Devin Nakamura43642b32011-07-11 11:22:16 -04001488 /* Finally, report back -- -C gives a parsable format */
1489 if (!qflag) {
1490 t2 = tsub(t2, t1);
1491 print_report("discard", &t2, offset, count, count, 1, Cflag);
1492 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001493
1494out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001495 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001496}
1497
Devin Nakamura43642b32011-07-11 11:22:16 -04001498static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001499{
Devin Nakamura43642b32011-07-11 11:22:16 -04001500 int64_t offset;
1501 int nb_sectors, remaining;
1502 char s1[64];
1503 int num, sum_alloc;
1504 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001505
Devin Nakamura43642b32011-07-11 11:22:16 -04001506 offset = cvtnum(argv[1]);
1507 if (offset & 0x1ff) {
1508 printf("offset %" PRId64 " is not sector aligned\n",
1509 offset);
1510 return 0;
1511 }
aliguorie3aff4f2009-04-05 19:14:04 +00001512
Devin Nakamura43642b32011-07-11 11:22:16 -04001513 if (argc == 3) {
1514 nb_sectors = cvtnum(argv[2]);
1515 } else {
1516 nb_sectors = 1;
1517 }
aliguorie3aff4f2009-04-05 19:14:04 +00001518
Devin Nakamura43642b32011-07-11 11:22:16 -04001519 remaining = nb_sectors;
1520 sum_alloc = 0;
1521 while (remaining) {
1522 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1523 remaining -= num;
1524 if (ret) {
1525 sum_alloc += num;
1526 }
1527 }
aliguorie3aff4f2009-04-05 19:14:04 +00001528
Devin Nakamura43642b32011-07-11 11:22:16 -04001529 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001530
Devin Nakamura43642b32011-07-11 11:22:16 -04001531 printf("%d/%d sectors allocated at offset %s\n",
1532 sum_alloc, nb_sectors, s1);
1533 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001534}
1535
1536static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001537 .name = "alloc",
1538 .altname = "a",
1539 .argmin = 1,
1540 .argmax = 2,
1541 .cfunc = alloc_f,
1542 .args = "off [sectors]",
1543 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001544};
1545
Devin Nakamura43642b32011-07-11 11:22:16 -04001546static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001547{
Devin Nakamura43642b32011-07-11 11:22:16 -04001548 int64_t offset;
1549 int64_t nb_sectors;
1550 char s1[64];
1551 int num, num_checked;
1552 int ret;
1553 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001554
Devin Nakamura43642b32011-07-11 11:22:16 -04001555 offset = 0;
1556 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001557
Devin Nakamura43642b32011-07-11 11:22:16 -04001558 do {
1559 num_checked = MIN(nb_sectors, INT_MAX);
1560 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1561 retstr = ret ? " allocated" : "not allocated";
1562 cvtstr(offset << 9ULL, s1, sizeof(s1));
1563 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1564 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001565
Devin Nakamura43642b32011-07-11 11:22:16 -04001566 offset += num;
1567 nb_sectors -= num;
1568 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001569
Devin Nakamura43642b32011-07-11 11:22:16 -04001570 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001571}
1572
1573static const cmdinfo_t map_cmd = {
1574 .name = "map",
1575 .argmin = 0,
1576 .argmax = 0,
1577 .cfunc = map_f,
1578 .args = "",
1579 .oneline = "prints the allocated areas of a file",
1580};
1581
1582
Devin Nakamura43642b32011-07-11 11:22:16 -04001583static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001584{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001585 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001586 bs = NULL;
1587 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001588}
1589
1590static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001591 .name = "close",
1592 .altname = "c",
1593 .cfunc = close_f,
1594 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001595};
1596
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001597static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001598{
Devin Nakamura43642b32011-07-11 11:22:16 -04001599 if (bs) {
1600 fprintf(stderr, "file open already, try 'help close'\n");
1601 return 1;
1602 }
aliguorie3aff4f2009-04-05 19:14:04 +00001603
Devin Nakamura43642b32011-07-11 11:22:16 -04001604 if (growable) {
1605 if (bdrv_file_open(&bs, name, flags)) {
1606 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1607 return 1;
1608 }
1609 } else {
1610 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001611
Devin Nakamura43642b32011-07-11 11:22:16 -04001612 if (bdrv_open(bs, name, flags, NULL) < 0) {
1613 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001614 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001615 bs = NULL;
1616 return 1;
1617 }
1618 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001619
Devin Nakamura43642b32011-07-11 11:22:16 -04001620 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001621}
1622
Devin Nakamura43642b32011-07-11 11:22:16 -04001623static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001624{
Devin Nakamura43642b32011-07-11 11:22:16 -04001625 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001626"\n"
1627" opens a new file in the requested mode\n"
1628"\n"
1629" Example:\n"
1630" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1631"\n"
1632" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001633" -r, -- open file read-only\n"
1634" -s, -- use snapshot file\n"
1635" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001636" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001637"\n");
1638}
1639
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001640static int open_f(int argc, char **argv);
1641
1642static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001643 .name = "open",
1644 .altname = "o",
1645 .cfunc = open_f,
1646 .argmin = 1,
1647 .argmax = -1,
1648 .flags = CMD_NOFILE_OK,
1649 .args = "[-Crsn] [path]",
1650 .oneline = "open the file specified by path",
1651 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001652};
aliguorie3aff4f2009-04-05 19:14:04 +00001653
Devin Nakamura43642b32011-07-11 11:22:16 -04001654static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001655{
Devin Nakamura43642b32011-07-11 11:22:16 -04001656 int flags = 0;
1657 int readonly = 0;
1658 int growable = 0;
1659 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001660
Devin Nakamura43642b32011-07-11 11:22:16 -04001661 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1662 switch (c) {
1663 case 's':
1664 flags |= BDRV_O_SNAPSHOT;
1665 break;
1666 case 'n':
1667 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1668 break;
1669 case 'r':
1670 readonly = 1;
1671 break;
1672 case 'g':
1673 growable = 1;
1674 break;
1675 default:
1676 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001677 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001678 }
aliguorie3aff4f2009-04-05 19:14:04 +00001679
Devin Nakamura43642b32011-07-11 11:22:16 -04001680 if (!readonly) {
1681 flags |= BDRV_O_RDWR;
1682 }
aliguorie3aff4f2009-04-05 19:14:04 +00001683
Devin Nakamura43642b32011-07-11 11:22:16 -04001684 if (optind != argc - 1) {
1685 return command_usage(&open_cmd);
1686 }
1687
1688 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001689}
1690
Devin Nakamura43642b32011-07-11 11:22:16 -04001691static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001692{
Devin Nakamura43642b32011-07-11 11:22:16 -04001693 /* only one device allowed so far */
1694 if (index >= 1) {
1695 return 0;
1696 }
1697 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001698}
1699
Devin Nakamura43642b32011-07-11 11:22:16 -04001700static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001701{
Devin Nakamura43642b32011-07-11 11:22:16 -04001702 if (ct->flags & CMD_FLAG_GLOBAL) {
1703 return 1;
1704 }
1705 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1706 fprintf(stderr, "no file open, try 'help open'\n");
1707 return 0;
1708 }
1709 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001710}
1711
1712static void usage(const char *name)
1713{
Devin Nakamura43642b32011-07-11 11:22:16 -04001714 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001715"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001716"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001717"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001718" -c, --cmd command to execute\n"
1719" -r, --read-only export read-only\n"
1720" -s, --snapshot use snapshot file\n"
1721" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001722" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001723" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001724" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001725" -h, --help display this help and exit\n"
1726" -V, --version output version information and exit\n"
1727"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001728 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001729}
1730
1731
1732int main(int argc, char **argv)
1733{
Devin Nakamura43642b32011-07-11 11:22:16 -04001734 int readonly = 0;
1735 int growable = 0;
1736 const char *sopt = "hVc:rsnmgk";
1737 const struct option lopt[] = {
1738 { "help", 0, NULL, 'h' },
1739 { "version", 0, NULL, 'V' },
1740 { "offset", 1, NULL, 'o' },
1741 { "cmd", 1, NULL, 'c' },
1742 { "read-only", 0, NULL, 'r' },
1743 { "snapshot", 0, NULL, 's' },
1744 { "nocache", 0, NULL, 'n' },
1745 { "misalign", 0, NULL, 'm' },
1746 { "growable", 0, NULL, 'g' },
1747 { "native-aio", 0, NULL, 'k' },
1748 { NULL, 0, NULL, 0 }
1749 };
1750 int c;
1751 int opt_index = 0;
1752 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001753
Devin Nakamura43642b32011-07-11 11:22:16 -04001754 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001755
Devin Nakamura43642b32011-07-11 11:22:16 -04001756 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1757 switch (c) {
1758 case 's':
1759 flags |= BDRV_O_SNAPSHOT;
1760 break;
1761 case 'n':
1762 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1763 break;
1764 case 'c':
1765 add_user_command(optarg);
1766 break;
1767 case 'r':
1768 readonly = 1;
1769 break;
1770 case 'm':
1771 misalign = 1;
1772 break;
1773 case 'g':
1774 growable = 1;
1775 break;
1776 case 'k':
1777 flags |= BDRV_O_NATIVE_AIO;
1778 break;
1779 case 'V':
1780 printf("%s version %s\n", progname, VERSION);
1781 exit(0);
1782 case 'h':
1783 usage(progname);
1784 exit(0);
1785 default:
1786 usage(progname);
1787 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001788 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001789 }
aliguorie3aff4f2009-04-05 19:14:04 +00001790
Devin Nakamura43642b32011-07-11 11:22:16 -04001791 if ((argc - optind) > 1) {
1792 usage(progname);
1793 exit(1);
1794 }
aliguorie3aff4f2009-04-05 19:14:04 +00001795
Devin Nakamura43642b32011-07-11 11:22:16 -04001796 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001797
Devin Nakamura43642b32011-07-11 11:22:16 -04001798 /* initialize commands */
1799 quit_init();
1800 help_init();
1801 add_command(&open_cmd);
1802 add_command(&close_cmd);
1803 add_command(&read_cmd);
1804 add_command(&readv_cmd);
1805 add_command(&write_cmd);
1806 add_command(&writev_cmd);
1807 add_command(&multiwrite_cmd);
1808 add_command(&aio_read_cmd);
1809 add_command(&aio_write_cmd);
1810 add_command(&aio_flush_cmd);
1811 add_command(&flush_cmd);
1812 add_command(&truncate_cmd);
1813 add_command(&length_cmd);
1814 add_command(&info_cmd);
1815 add_command(&discard_cmd);
1816 add_command(&alloc_cmd);
1817 add_command(&map_cmd);
1818
1819 add_args_command(init_args_command);
1820 add_check_command(init_check_command);
1821
1822 /* open the device */
1823 if (!readonly) {
1824 flags |= BDRV_O_RDWR;
1825 }
1826
1827 if ((argc - optind) == 1) {
1828 openfile(argv[optind], flags, growable);
1829 }
1830 command_loop();
1831
1832 /*
1833 * Make sure all outstanding requests get flushed the program exits.
1834 */
1835 qemu_aio_flush();
1836
1837 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001838 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001839 }
1840 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001841}