]> git.proxmox.com Git - qemu.git/blame - qemu-io.c
msi: Add msi_get_message()
[qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
1/*
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 */
c32d766a 10#include <sys/time.h>
e3aff4f6
AL
11#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
c32d766a 15#include <libgen.h>
e3aff4f6
AL
16
17#include "qemu-common.h"
a5a5238e 18#include "main-loop.h"
e3aff4f6
AL
19#include "block_int.h"
20#include "cmd.h"
d7bb72c8 21#include "trace/control.h"
e3aff4f6
AL
22
23#define VERSION "0.0.1"
24
43642b38 25#define CMD_NOFILE_OK 0x01
e3aff4f6
AL
26
27char *progname;
28static BlockDriverState *bs;
29
30static int misalign;
31
cf070d7e
CH
32/*
33 * Parse the pattern argument to various sub-commands.
34 *
35 * Because the pattern is used as an argument to memset it must evaluate
36 * to an unsigned integer that fits into a single byte.
37 */
38static int parse_pattern(const char *arg)
39{
43642b38
DN
40 char *endptr = NULL;
41 long pattern;
cf070d7e 42
43642b38
DN
43 pattern = strtol(arg, &endptr, 0);
44 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
45 printf("%s is not a valid pattern byte\n", arg);
46 return -1;
47 }
cf070d7e 48
43642b38 49 return pattern;
cf070d7e
CH
50}
51
e3aff4f6
AL
52/*
53 * Memory allocation helpers.
54 *
55 * Make sure memory is aligned by default, or purposefully misaligned if
56 * that is specified on the command line.
57 */
58
43642b38 59#define MISALIGN_OFFSET 16
e3aff4f6
AL
60static void *qemu_io_alloc(size_t len, int pattern)
61{
43642b38
DN
62 void *buf;
63
64 if (misalign) {
65 len += MISALIGN_OFFSET;
66 }
67 buf = qemu_blockalign(bs, len);
68 memset(buf, pattern, len);
69 if (misalign) {
70 buf += MISALIGN_OFFSET;
71 }
72 return buf;
e3aff4f6
AL
73}
74
75static void qemu_io_free(void *p)
76{
43642b38
DN
77 if (misalign) {
78 p -= MISALIGN_OFFSET;
79 }
80 qemu_vfree(p);
e3aff4f6
AL
81}
82
43642b38 83static void dump_buffer(const void *buffer, int64_t offset, int len)
e3aff4f6 84{
43642b38
DN
85 int i, j;
86 const uint8_t *p;
87
88 for (i = 0, p = buffer; i < len; i += 16) {
89 const uint8_t *s = p;
90
91 printf("%08" PRIx64 ": ", offset + i);
92 for (j = 0; j < 16 && i + j < len; j++, p++) {
93 printf("%02x ", *p);
94 }
95 printf(" ");
96 for (j = 0; j < 16 && i + j < len; j++, s++) {
97 if (isalnum(*s)) {
98 printf("%c", *s);
99 } else {
100 printf(".");
101 }
102 }
103 printf("\n");
104 }
e3aff4f6
AL
105}
106
43642b38
DN
107static void print_report(const char *op, struct timeval *t, int64_t offset,
108 int count, int total, int cnt, int Cflag)
e3aff4f6 109{
43642b38
DN
110 char s1[64], s2[64], ts[64];
111
112 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
113 if (!Cflag) {
114 cvtstr((double)total, s1, sizeof(s1));
115 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
116 printf("%s %d/%d bytes at offset %" PRId64 "\n",
117 op, total, count, offset);
118 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
119 s1, cnt, ts, s2, tdiv((double)cnt, *t));
120 } else {/* bytes,ops,time,bytes/sec,ops/sec */
121 printf("%d,%d,%s,%.3f,%.3f\n",
122 total, cnt, ts,
123 tdiv((double)total, *t),
124 tdiv((double)cnt, *t));
125 }
e3aff4f6
AL
126}
127
cf57298a
CH
128/*
129 * Parse multiple length statements for vectored I/O, and construct an I/O
130 * vector matching it.
131 */
132static void *
133create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
134{
031380d8 135 size_t *sizes = g_new0(size_t, nr_iov);
43642b38
DN
136 size_t count = 0;
137 void *buf = NULL;
138 void *p;
139 int i;
140
141 for (i = 0; i < nr_iov; i++) {
142 char *arg = argv[i];
143 int64_t len;
144
145 len = cvtnum(arg);
146 if (len < 0) {
147 printf("non-numeric length argument -- %s\n", arg);
148 goto fail;
149 }
150
151 /* should be SIZE_T_MAX, but that doesn't exist */
152 if (len > INT_MAX) {
153 printf("too large length argument -- %s\n", arg);
154 goto fail;
155 }
156
157 if (len & 0x1ff) {
158 printf("length argument %" PRId64
159 " is not sector aligned\n", len);
160 goto fail;
161 }
162
163 sizes[i] = len;
164 count += len;
165 }
166
167 qemu_iovec_init(qiov, nr_iov);
168
169 buf = p = qemu_io_alloc(count, pattern);
170
171 for (i = 0; i < nr_iov; i++) {
172 qemu_iovec_add(qiov, p, sizes[i]);
173 p += sizes[i];
174 }
cf57298a 175
40a0d7c3 176fail:
031380d8 177 g_free(sizes);
43642b38 178 return buf;
cf57298a
CH
179}
180
e3aff4f6
AL
181static int do_read(char *buf, int64_t offset, int count, int *total)
182{
43642b38 183 int ret;
e3aff4f6 184
43642b38
DN
185 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
186 if (ret < 0) {
187 return ret;
188 }
189 *total = count;
190 return 1;
e3aff4f6
AL
191}
192
193static int do_write(char *buf, int64_t offset, int count, int *total)
194{
43642b38 195 int ret;
e3aff4f6 196
43642b38
DN
197 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
198 if (ret < 0) {
199 return ret;
200 }
201 *total = count;
202 return 1;
e3aff4f6
AL
203}
204
205static int do_pread(char *buf, int64_t offset, int count, int *total)
206{
43642b38
DN
207 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
208 if (*total < 0) {
209 return *total;
210 }
211 return 1;
e3aff4f6
AL
212}
213
214static int do_pwrite(char *buf, int64_t offset, int count, int *total)
215{
43642b38
DN
216 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
217 if (*total < 0) {
218 return *total;
219 }
220 return 1;
e3aff4f6
AL
221}
222
71b58b82
SH
223typedef struct {
224 int64_t offset;
225 int count;
226 int *total;
227 int ret;
228 bool done;
229} CoWriteZeroes;
230
231static void coroutine_fn co_write_zeroes_entry(void *opaque)
232{
233 CoWriteZeroes *data = opaque;
234
235 data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE,
236 data->count / BDRV_SECTOR_SIZE);
237 data->done = true;
238 if (data->ret < 0) {
239 *data->total = data->ret;
240 return;
241 }
242
243 *data->total = data->count;
244}
245
246static int do_co_write_zeroes(int64_t offset, int count, int *total)
247{
248 Coroutine *co;
249 CoWriteZeroes data = {
250 .offset = offset,
251 .count = count,
252 .total = total,
253 .done = false,
254 };
255
256 co = qemu_coroutine_create(co_write_zeroes_entry);
257 qemu_coroutine_enter(co, &data);
258 while (!data.done) {
259 qemu_aio_wait();
260 }
261 if (data.ret < 0) {
262 return data.ret;
263 } else {
264 return 1;
265 }
266}
267
ca94dbc7
KW
268static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
269{
43642b38
DN
270 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
271 if (*total < 0) {
272 return *total;
273 }
274 return 1;
ca94dbc7
KW
275}
276
277static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
278{
43642b38
DN
279 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
280 if (*total < 0) {
281 return *total;
282 }
283 return 1;
ca94dbc7
KW
284}
285
e3aff4f6
AL
286#define NOT_DONE 0x7fffffff
287static void aio_rw_done(void *opaque, int ret)
288{
43642b38 289 *(int *)opaque = ret;
e3aff4f6
AL
290}
291
292static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
293{
43642b38 294 int async_ret = NOT_DONE;
e3aff4f6 295
ad54ae80
PB
296 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
297 aio_rw_done, &async_ret);
43642b38 298 while (async_ret == NOT_DONE) {
a5a5238e 299 main_loop_wait(false);
43642b38 300 }
e3aff4f6 301
43642b38
DN
302 *total = qiov->size;
303 return async_ret < 0 ? async_ret : 1;
e3aff4f6
AL
304}
305
306static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
307{
43642b38 308 int async_ret = NOT_DONE;
e3aff4f6 309
ad54ae80
PB
310 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
311 aio_rw_done, &async_ret);
43642b38 312 while (async_ret == NOT_DONE) {
a5a5238e 313 main_loop_wait(false);
43642b38 314 }
e3aff4f6 315
43642b38
DN
316 *total = qiov->size;
317 return async_ret < 0 ? async_ret : 1;
e3aff4f6
AL
318}
319
776cbbbd 320struct multiwrite_async_ret {
43642b38
DN
321 int num_done;
322 int error;
776cbbbd
KW
323};
324
325static void multiwrite_cb(void *opaque, int ret)
326{
43642b38 327 struct multiwrite_async_ret *async_ret = opaque;
776cbbbd 328
43642b38
DN
329 async_ret->num_done++;
330 if (ret < 0) {
331 async_ret->error = ret;
332 }
776cbbbd
KW
333}
334
335static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
336{
43642b38
DN
337 int i, ret;
338 struct multiwrite_async_ret async_ret = {
339 .num_done = 0,
340 .error = 0,
341 };
342
343 *total = 0;
344 for (i = 0; i < num_reqs; i++) {
345 reqs[i].cb = multiwrite_cb;
346 reqs[i].opaque = &async_ret;
347 *total += reqs[i].qiov->size;
348 }
349
350 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
351 if (ret < 0) {
352 return ret;
353 }
354
355 while (async_ret.num_done < num_reqs) {
a5a5238e 356 main_loop_wait(false);
43642b38
DN
357 }
358
359 return async_ret.error < 0 ? async_ret.error : 1;
776cbbbd 360}
e3aff4f6 361
43642b38 362static void read_help(void)
e3aff4f6 363{
43642b38 364 printf(
e3aff4f6
AL
365"\n"
366" reads a range of bytes from the given offset\n"
367"\n"
368" Example:\n"
369" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
370"\n"
371" Reads a segment of the currently open file, optionally dumping it to the\n"
372" standard output stream (with -v option) for subsequent inspection.\n"
ca94dbc7 373" -b, -- read from the VM state rather than the virtual disk\n"
d9654a58
KW
374" -C, -- report statistics in a machine parsable format\n"
375" -l, -- length for pattern verification (only with -P)\n"
e3aff4f6 376" -p, -- use bdrv_pread to read the file\n"
c48101ae 377" -P, -- use a pattern to verify read data\n"
095343ad 378" -q, -- quiet mode, do not show I/O statistics\n"
d9654a58
KW
379" -s, -- start offset for pattern verification (only with -P)\n"
380" -v, -- dump buffer to standard output\n"
e3aff4f6
AL
381"\n");
382}
383
22a2bdcb
BS
384static int read_f(int argc, char **argv);
385
386static const cmdinfo_t read_cmd = {
43642b38
DN
387 .name = "read",
388 .altname = "r",
389 .cfunc = read_f,
390 .argmin = 2,
391 .argmax = -1,
392 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
393 .oneline = "reads a number of bytes at a specified offset",
394 .help = read_help,
22a2bdcb
BS
395};
396
43642b38 397static int read_f(int argc, char **argv)
e3aff4f6 398{
43642b38
DN
399 struct timeval t1, t2;
400 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
401 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
402 int c, cnt;
403 char *buf;
404 int64_t offset;
405 int count;
406 /* Some compilers get confused and warn if this is not initialized. */
407 int total = 0;
408 int pattern = 0, pattern_offset = 0, pattern_count = 0;
409
410 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
411 switch (c) {
412 case 'b':
413 bflag = 1;
414 break;
415 case 'C':
416 Cflag = 1;
417 break;
418 case 'l':
419 lflag = 1;
420 pattern_count = cvtnum(optarg);
421 if (pattern_count < 0) {
422 printf("non-numeric length argument -- %s\n", optarg);
423 return 0;
424 }
425 break;
426 case 'p':
427 pflag = 1;
428 break;
429 case 'P':
430 Pflag = 1;
431 pattern = parse_pattern(optarg);
432 if (pattern < 0) {
433 return 0;
434 }
435 break;
436 case 'q':
437 qflag = 1;
438 break;
439 case 's':
440 sflag = 1;
441 pattern_offset = cvtnum(optarg);
442 if (pattern_offset < 0) {
443 printf("non-numeric length argument -- %s\n", optarg);
444 return 0;
445 }
446 break;
447 case 'v':
448 vflag = 1;
449 break;
450 default:
451 return command_usage(&read_cmd);
452 }
453 }
454
455 if (optind != argc - 2) {
456 return command_usage(&read_cmd);
457 }
458
459 if (bflag && pflag) {
460 printf("-b and -p cannot be specified at the same time\n");
461 return 0;
462 }
463
464 offset = cvtnum(argv[optind]);
465 if (offset < 0) {
466 printf("non-numeric length argument -- %s\n", argv[optind]);
467 return 0;
468 }
469
470 optind++;
471 count = cvtnum(argv[optind]);
472 if (count < 0) {
473 printf("non-numeric length argument -- %s\n", argv[optind]);
474 return 0;
475 }
e3aff4f6 476
d9654a58
KW
477 if (!Pflag && (lflag || sflag)) {
478 return command_usage(&read_cmd);
479 }
480
481 if (!lflag) {
482 pattern_count = count - pattern_offset;
483 }
484
485 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
07f35073 486 printf("pattern verification range exceeds end of read data\n");
d9654a58
KW
487 return 0;
488 }
489
5afc8b3d 490 if (!pflag) {
43642b38
DN
491 if (offset & 0x1ff) {
492 printf("offset %" PRId64 " is not sector aligned\n",
493 offset);
494 return 0;
495 }
496 if (count & 0x1ff) {
497 printf("count %d is not sector aligned\n",
498 count);
499 return 0;
500 }
5afc8b3d 501 }
43642b38
DN
502
503 buf = qemu_io_alloc(count, 0xab);
504
505 gettimeofday(&t1, NULL);
506 if (pflag) {
507 cnt = do_pread(buf, offset, count, &total);
508 } else if (bflag) {
509 cnt = do_load_vmstate(buf, offset, count, &total);
510 } else {
511 cnt = do_read(buf, offset, count, &total);
512 }
513 gettimeofday(&t2, NULL);
514
515 if (cnt < 0) {
516 printf("read failed: %s\n", strerror(-cnt));
517 goto out;
518 }
519
520 if (Pflag) {
031380d8 521 void *cmp_buf = g_malloc(pattern_count);
43642b38
DN
522 memset(cmp_buf, pattern, pattern_count);
523 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
524 printf("Pattern verification failed at offset %"
525 PRId64 ", %d bytes\n",
526 offset + pattern_offset, pattern_count);
527 }
031380d8 528 g_free(cmp_buf);
43642b38
DN
529 }
530
531 if (qflag) {
532 goto out;
533 }
534
535 if (vflag) {
536 dump_buffer(buf, offset, count);
537 }
538
539 /* Finally, report back -- -C gives a parsable format */
540 t2 = tsub(t2, t1);
541 print_report("read", &t2, offset, count, total, cnt, Cflag);
e3aff4f6 542
7d8abfcb 543out:
43642b38 544 qemu_io_free(buf);
e3aff4f6 545
43642b38 546 return 0;
e3aff4f6
AL
547}
548
43642b38 549static void readv_help(void)
e3aff4f6 550{
43642b38 551 printf(
e3aff4f6
AL
552"\n"
553" reads a range of bytes from the given offset into multiple buffers\n"
554"\n"
555" Example:\n"
556" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
557"\n"
558" Reads a segment of the currently open file, optionally dumping it to the\n"
559" standard output stream (with -v option) for subsequent inspection.\n"
560" Uses multiple iovec buffers if more than one byte range is specified.\n"
561" -C, -- report statistics in a machine parsable format\n"
c48101ae 562" -P, -- use a pattern to verify read data\n"
e3aff4f6 563" -v, -- dump buffer to standard output\n"
095343ad 564" -q, -- quiet mode, do not show I/O statistics\n"
e3aff4f6
AL
565"\n");
566}
567
22a2bdcb
BS
568static int readv_f(int argc, char **argv);
569
570static const cmdinfo_t readv_cmd = {
43642b38
DN
571 .name = "readv",
572 .cfunc = readv_f,
573 .argmin = 2,
574 .argmax = -1,
575 .args = "[-Cqv] [-P pattern ] off len [len..]",
576 .oneline = "reads a number of bytes at a specified offset",
577 .help = readv_help,
22a2bdcb
BS
578};
579
43642b38 580static int readv_f(int argc, char **argv)
e3aff4f6 581{
43642b38
DN
582 struct timeval t1, t2;
583 int Cflag = 0, qflag = 0, vflag = 0;
584 int c, cnt;
585 char *buf;
586 int64_t offset;
587 /* Some compilers get confused and warn if this is not initialized. */
588 int total = 0;
589 int nr_iov;
590 QEMUIOVector qiov;
591 int pattern = 0;
592 int Pflag = 0;
593
594 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
595 switch (c) {
596 case 'C':
597 Cflag = 1;
598 break;
599 case 'P':
600 Pflag = 1;
601 pattern = parse_pattern(optarg);
602 if (pattern < 0) {
603 return 0;
604 }
605 break;
606 case 'q':
607 qflag = 1;
608 break;
609 case 'v':
610 vflag = 1;
611 break;
612 default:
613 return command_usage(&readv_cmd);
614 }
615 }
616
617 if (optind > argc - 2) {
618 return command_usage(&readv_cmd);
619 }
620
621
622 offset = cvtnum(argv[optind]);
623 if (offset < 0) {
624 printf("non-numeric length argument -- %s\n", argv[optind]);
625 return 0;
626 }
627 optind++;
628
629 if (offset & 0x1ff) {
630 printf("offset %" PRId64 " is not sector aligned\n",
631 offset);
632 return 0;
633 }
634
635 nr_iov = argc - optind;
636 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
f2360620
KW
637 if (buf == NULL) {
638 return 0;
639 }
43642b38
DN
640
641 gettimeofday(&t1, NULL);
642 cnt = do_aio_readv(&qiov, offset, &total);
643 gettimeofday(&t2, NULL);
644
645 if (cnt < 0) {
646 printf("readv failed: %s\n", strerror(-cnt));
647 goto out;
648 }
649
650 if (Pflag) {
031380d8 651 void *cmp_buf = g_malloc(qiov.size);
43642b38
DN
652 memset(cmp_buf, pattern, qiov.size);
653 if (memcmp(buf, cmp_buf, qiov.size)) {
654 printf("Pattern verification failed at offset %"
655 PRId64 ", %zd bytes\n", offset, qiov.size);
656 }
031380d8 657 g_free(cmp_buf);
43642b38
DN
658 }
659
660 if (qflag) {
661 goto out;
662 }
663
664 if (vflag) {
665 dump_buffer(buf, offset, qiov.size);
666 }
667
668 /* Finally, report back -- -C gives a parsable format */
669 t2 = tsub(t2, t1);
670 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
e3aff4f6 671
7d8abfcb 672out:
9e559533 673 qemu_iovec_destroy(&qiov);
43642b38
DN
674 qemu_io_free(buf);
675 return 0;
e3aff4f6
AL
676}
677
43642b38 678static void write_help(void)
e3aff4f6 679{
43642b38 680 printf(
e3aff4f6
AL
681"\n"
682" writes a range of bytes from the given offset\n"
683"\n"
684" Example:\n"
685" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
686"\n"
687" Writes into a segment of the currently open file, using a buffer\n"
688" filled with a set pattern (0xcdcdcdcd).\n"
ca94dbc7 689" -b, -- write to the VM state rather than the virtual disk\n"
e3aff4f6
AL
690" -p, -- use bdrv_pwrite to write the file\n"
691" -P, -- use different pattern to fill file\n"
692" -C, -- report statistics in a machine parsable format\n"
095343ad 693" -q, -- quiet mode, do not show I/O statistics\n"
71b58b82 694" -z, -- write zeroes using bdrv_co_write_zeroes\n"
e3aff4f6
AL
695"\n");
696}
697
22a2bdcb
BS
698static int write_f(int argc, char **argv);
699
700static const cmdinfo_t write_cmd = {
43642b38
DN
701 .name = "write",
702 .altname = "w",
703 .cfunc = write_f,
704 .argmin = 2,
705 .argmax = -1,
71b58b82 706 .args = "[-bCpqz] [-P pattern ] off len",
43642b38
DN
707 .oneline = "writes a number of bytes at a specified offset",
708 .help = write_help,
22a2bdcb
BS
709};
710
43642b38 711static int write_f(int argc, char **argv)
e3aff4f6 712{
43642b38 713 struct timeval t1, t2;
71b58b82 714 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
43642b38 715 int c, cnt;
71b58b82 716 char *buf = NULL;
43642b38
DN
717 int64_t offset;
718 int count;
719 /* Some compilers get confused and warn if this is not initialized. */
720 int total = 0;
721 int pattern = 0xcd;
722
71b58b82 723 while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) {
43642b38
DN
724 switch (c) {
725 case 'b':
726 bflag = 1;
727 break;
728 case 'C':
729 Cflag = 1;
730 break;
731 case 'p':
732 pflag = 1;
733 break;
734 case 'P':
71b58b82 735 Pflag = 1;
43642b38
DN
736 pattern = parse_pattern(optarg);
737 if (pattern < 0) {
738 return 0;
739 }
740 break;
741 case 'q':
742 qflag = 1;
743 break;
71b58b82
SH
744 case 'z':
745 zflag = 1;
746 break;
43642b38
DN
747 default:
748 return command_usage(&write_cmd);
749 }
750 }
751
752 if (optind != argc - 2) {
753 return command_usage(&write_cmd);
754 }
755
71b58b82
SH
756 if (bflag + pflag + zflag > 1) {
757 printf("-b, -p, or -z cannot be specified at the same time\n");
758 return 0;
759 }
760
761 if (zflag && Pflag) {
762 printf("-z and -P cannot be specified at the same time\n");
43642b38
DN
763 return 0;
764 }
765
766 offset = cvtnum(argv[optind]);
767 if (offset < 0) {
768 printf("non-numeric length argument -- %s\n", argv[optind]);
769 return 0;
770 }
771
772 optind++;
773 count = cvtnum(argv[optind]);
774 if (count < 0) {
775 printf("non-numeric length argument -- %s\n", argv[optind]);
776 return 0;
777 }
778
779 if (!pflag) {
780 if (offset & 0x1ff) {
781 printf("offset %" PRId64 " is not sector aligned\n",
782 offset);
783 return 0;
784 }
785
786 if (count & 0x1ff) {
787 printf("count %d is not sector aligned\n",
788 count);
789 return 0;
790 }
791 }
792
71b58b82
SH
793 if (!zflag) {
794 buf = qemu_io_alloc(count, pattern);
795 }
43642b38
DN
796
797 gettimeofday(&t1, NULL);
798 if (pflag) {
799 cnt = do_pwrite(buf, offset, count, &total);
800 } else if (bflag) {
801 cnt = do_save_vmstate(buf, offset, count, &total);
71b58b82
SH
802 } else if (zflag) {
803 cnt = do_co_write_zeroes(offset, count, &total);
43642b38
DN
804 } else {
805 cnt = do_write(buf, offset, count, &total);
806 }
807 gettimeofday(&t2, NULL);
808
809 if (cnt < 0) {
810 printf("write failed: %s\n", strerror(-cnt));
811 goto out;
812 }
813
814 if (qflag) {
815 goto out;
816 }
817
818 /* Finally, report back -- -C gives a parsable format */
819 t2 = tsub(t2, t1);
820 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
e3aff4f6 821
7d8abfcb 822out:
71b58b82
SH
823 if (!zflag) {
824 qemu_io_free(buf);
825 }
e3aff4f6 826
43642b38 827 return 0;
e3aff4f6
AL
828}
829
e3aff4f6
AL
830static void
831writev_help(void)
832{
43642b38 833 printf(
e3aff4f6
AL
834"\n"
835" writes a range of bytes from the given offset source from multiple buffers\n"
836"\n"
837" Example:\n"
838" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
839"\n"
840" Writes into a segment of the currently open file, using a buffer\n"
841" filled with a set pattern (0xcdcdcdcd).\n"
842" -P, -- use different pattern to fill file\n"
843" -C, -- report statistics in a machine parsable format\n"
095343ad 844" -q, -- quiet mode, do not show I/O statistics\n"
e3aff4f6
AL
845"\n");
846}
847
22a2bdcb
BS
848static int writev_f(int argc, char **argv);
849
850static const cmdinfo_t writev_cmd = {
43642b38
DN
851 .name = "writev",
852 .cfunc = writev_f,
853 .argmin = 2,
854 .argmax = -1,
855 .args = "[-Cq] [-P pattern ] off len [len..]",
856 .oneline = "writes a number of bytes at a specified offset",
857 .help = writev_help,
22a2bdcb
BS
858};
859
43642b38 860static int writev_f(int argc, char **argv)
e3aff4f6 861{
43642b38
DN
862 struct timeval t1, t2;
863 int Cflag = 0, qflag = 0;
864 int c, cnt;
865 char *buf;
866 int64_t offset;
867 /* Some compilers get confused and warn if this is not initialized. */
868 int total = 0;
869 int nr_iov;
870 int pattern = 0xcd;
871 QEMUIOVector qiov;
872
873 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
874 switch (c) {
875 case 'C':
876 Cflag = 1;
877 break;
878 case 'q':
879 qflag = 1;
880 break;
881 case 'P':
882 pattern = parse_pattern(optarg);
883 if (pattern < 0) {
884 return 0;
885 }
886 break;
887 default:
888 return command_usage(&writev_cmd);
889 }
890 }
891
892 if (optind > argc - 2) {
893 return command_usage(&writev_cmd);
894 }
895
896 offset = cvtnum(argv[optind]);
897 if (offset < 0) {
898 printf("non-numeric length argument -- %s\n", argv[optind]);
899 return 0;
900 }
901 optind++;
902
903 if (offset & 0x1ff) {
904 printf("offset %" PRId64 " is not sector aligned\n",
905 offset);
906 return 0;
907 }
908
909 nr_iov = argc - optind;
910 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
f2360620
KW
911 if (buf == NULL) {
912 return 0;
913 }
43642b38
DN
914
915 gettimeofday(&t1, NULL);
916 cnt = do_aio_writev(&qiov, offset, &total);
917 gettimeofday(&t2, NULL);
918
919 if (cnt < 0) {
920 printf("writev failed: %s\n", strerror(-cnt));
921 goto out;
922 }
923
924 if (qflag) {
925 goto out;
926 }
927
928 /* Finally, report back -- -C gives a parsable format */
929 t2 = tsub(t2, t1);
930 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
7d8abfcb 931out:
9e559533 932 qemu_iovec_destroy(&qiov);
43642b38
DN
933 qemu_io_free(buf);
934 return 0;
e3aff4f6
AL
935}
936
43642b38 937static void multiwrite_help(void)
776cbbbd 938{
43642b38 939 printf(
776cbbbd
KW
940"\n"
941" writes a range of bytes from the given offset source from multiple buffers,\n"
942" in a batch of requests that may be merged by qemu\n"
943"\n"
944" Example:\n"
b2bedb21 945" 'multiwrite 512 1k 1k ; 4k 1k'\n"
776cbbbd
KW
946" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
947"\n"
948" Writes into a segment of the currently open file, using a buffer\n"
949" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
950" by one for each request contained in the multiwrite command.\n"
951" -P, -- use different pattern to fill file\n"
952" -C, -- report statistics in a machine parsable format\n"
953" -q, -- quiet mode, do not show I/O statistics\n"
954"\n");
955}
956
957static int multiwrite_f(int argc, char **argv);
958
959static const cmdinfo_t multiwrite_cmd = {
43642b38
DN
960 .name = "multiwrite",
961 .cfunc = multiwrite_f,
962 .argmin = 2,
963 .argmax = -1,
964 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
965 .oneline = "issues multiple write requests at once",
966 .help = multiwrite_help,
776cbbbd
KW
967};
968
43642b38 969static int multiwrite_f(int argc, char **argv)
776cbbbd 970{
43642b38
DN
971 struct timeval t1, t2;
972 int Cflag = 0, qflag = 0;
973 int c, cnt;
974 char **buf;
975 int64_t offset, first_offset = 0;
976 /* Some compilers get confused and warn if this is not initialized. */
977 int total = 0;
978 int nr_iov;
979 int nr_reqs;
980 int pattern = 0xcd;
981 QEMUIOVector *qiovs;
982 int i;
983 BlockRequest *reqs;
984
985 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
986 switch (c) {
987 case 'C':
988 Cflag = 1;
989 break;
990 case 'q':
991 qflag = 1;
992 break;
993 case 'P':
994 pattern = parse_pattern(optarg);
995 if (pattern < 0) {
996 return 0;
997 }
998 break;
999 default:
1000 return command_usage(&writev_cmd);
1001 }
1002 }
1003
1004 if (optind > argc - 2) {
1005 return command_usage(&writev_cmd);
1006 }
1007
1008 nr_reqs = 1;
1009 for (i = optind; i < argc; i++) {
1010 if (!strcmp(argv[i], ";")) {
1011 nr_reqs++;
1012 }
1013 }
1014
f2360620
KW
1015 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1016 buf = g_malloc0(nr_reqs * sizeof(*buf));
7267c094 1017 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
43642b38 1018
67403dbb 1019 for (i = 0; i < nr_reqs && optind < argc; i++) {
43642b38
DN
1020 int j;
1021
1022 /* Read the offset of the request */
1023 offset = cvtnum(argv[optind]);
1024 if (offset < 0) {
1025 printf("non-numeric offset argument -- %s\n", argv[optind]);
67403dbb 1026 goto out;
43642b38
DN
1027 }
1028 optind++;
1029
1030 if (offset & 0x1ff) {
1031 printf("offset %lld is not sector aligned\n",
1032 (long long)offset);
67403dbb 1033 goto out;
43642b38 1034 }
776cbbbd
KW
1035
1036 if (i == 0) {
1037 first_offset = offset;
1038 }
1039
43642b38
DN
1040 /* Read lengths for qiov entries */
1041 for (j = optind; j < argc; j++) {
1042 if (!strcmp(argv[j], ";")) {
1043 break;
1044 }
1045 }
776cbbbd 1046
43642b38 1047 nr_iov = j - optind;
776cbbbd 1048
43642b38 1049 /* Build request */
f2360620
KW
1050 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1051 if (buf[i] == NULL) {
1052 goto out;
1053 }
1054
43642b38 1055 reqs[i].qiov = &qiovs[i];
43642b38
DN
1056 reqs[i].sector = offset >> 9;
1057 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
776cbbbd 1058
43642b38 1059 optind = j + 1;
776cbbbd 1060
43642b38
DN
1061 pattern++;
1062 }
776cbbbd 1063
67403dbb
KW
1064 /* If there were empty requests at the end, ignore them */
1065 nr_reqs = i;
1066
43642b38
DN
1067 gettimeofday(&t1, NULL);
1068 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1069 gettimeofday(&t2, NULL);
776cbbbd 1070
43642b38
DN
1071 if (cnt < 0) {
1072 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1073 goto out;
1074 }
776cbbbd 1075
43642b38
DN
1076 if (qflag) {
1077 goto out;
1078 }
776cbbbd 1079
43642b38
DN
1080 /* Finally, report back -- -C gives a parsable format */
1081 t2 = tsub(t2, t1);
1082 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
776cbbbd 1083out:
43642b38
DN
1084 for (i = 0; i < nr_reqs; i++) {
1085 qemu_io_free(buf[i]);
f2360620
KW
1086 if (reqs[i].qiov != NULL) {
1087 qemu_iovec_destroy(&qiovs[i]);
1088 }
43642b38 1089 }
7267c094
AL
1090 g_free(buf);
1091 g_free(reqs);
1092 g_free(qiovs);
43642b38 1093 return 0;
776cbbbd
KW
1094}
1095
95533d5f 1096struct aio_ctx {
43642b38
DN
1097 QEMUIOVector qiov;
1098 int64_t offset;
1099 char *buf;
1100 int qflag;
1101 int vflag;
1102 int Cflag;
1103 int Pflag;
1104 int pattern;
1105 struct timeval t1;
95533d5f
CH
1106};
1107
43642b38 1108static void aio_write_done(void *opaque, int ret)
95533d5f 1109{
43642b38
DN
1110 struct aio_ctx *ctx = opaque;
1111 struct timeval t2;
95533d5f 1112
43642b38 1113 gettimeofday(&t2, NULL);
95533d5f 1114
95533d5f 1115
43642b38
DN
1116 if (ret < 0) {
1117 printf("aio_write failed: %s\n", strerror(-ret));
1118 goto out;
1119 }
95533d5f 1120
43642b38
DN
1121 if (ctx->qflag) {
1122 goto out;
1123 }
95533d5f 1124
43642b38
DN
1125 /* Finally, report back -- -C gives a parsable format */
1126 t2 = tsub(t2, ctx->t1);
1127 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1128 ctx->qiov.size, 1, ctx->Cflag);
7d8abfcb 1129out:
43642b38 1130 qemu_io_free(ctx->buf);
9e559533 1131 qemu_iovec_destroy(&ctx->qiov);
031380d8 1132 g_free(ctx);
95533d5f
CH
1133}
1134
43642b38 1135static void aio_read_done(void *opaque, int ret)
95533d5f 1136{
43642b38
DN
1137 struct aio_ctx *ctx = opaque;
1138 struct timeval t2;
1139
1140 gettimeofday(&t2, NULL);
1141
1142 if (ret < 0) {
1143 printf("readv failed: %s\n", strerror(-ret));
1144 goto out;
1145 }
1146
1147 if (ctx->Pflag) {
031380d8 1148 void *cmp_buf = g_malloc(ctx->qiov.size);
43642b38
DN
1149
1150 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1151 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1152 printf("Pattern verification failed at offset %"
1153 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1154 }
031380d8 1155 g_free(cmp_buf);
43642b38
DN
1156 }
1157
1158 if (ctx->qflag) {
1159 goto out;
1160 }
1161
1162 if (ctx->vflag) {
1163 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1164 }
1165
1166 /* Finally, report back -- -C gives a parsable format */
1167 t2 = tsub(t2, ctx->t1);
1168 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1169 ctx->qiov.size, 1, ctx->Cflag);
7d8abfcb 1170out:
43642b38 1171 qemu_io_free(ctx->buf);
9e559533 1172 qemu_iovec_destroy(&ctx->qiov);
031380d8 1173 g_free(ctx);
95533d5f
CH
1174}
1175
43642b38 1176static void aio_read_help(void)
95533d5f 1177{
43642b38 1178 printf(
95533d5f
CH
1179"\n"
1180" asynchronously reads a range of bytes from the given offset\n"
1181"\n"
1182" Example:\n"
1183" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1184"\n"
1185" Reads a segment of the currently open file, optionally dumping it to the\n"
1186" standard output stream (with -v option) for subsequent inspection.\n"
e432cef9 1187" The read is performed asynchronously and the aio_flush command must be\n"
96bab41d 1188" used to ensure all outstanding aio requests have been completed.\n"
95533d5f
CH
1189" -C, -- report statistics in a machine parsable format\n"
1190" -P, -- use a pattern to verify read data\n"
1191" -v, -- dump buffer to standard output\n"
095343ad 1192" -q, -- quiet mode, do not show I/O statistics\n"
95533d5f
CH
1193"\n");
1194}
1195
22a2bdcb
BS
1196static int aio_read_f(int argc, char **argv);
1197
1198static const cmdinfo_t aio_read_cmd = {
43642b38
DN
1199 .name = "aio_read",
1200 .cfunc = aio_read_f,
1201 .argmin = 2,
1202 .argmax = -1,
1203 .args = "[-Cqv] [-P pattern ] off len [len..]",
1204 .oneline = "asynchronously reads a number of bytes",
1205 .help = aio_read_help,
22a2bdcb
BS
1206};
1207
43642b38 1208static int aio_read_f(int argc, char **argv)
95533d5f 1209{
43642b38 1210 int nr_iov, c;
031380d8 1211 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
43642b38
DN
1212
1213 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1214 switch (c) {
1215 case 'C':
1216 ctx->Cflag = 1;
1217 break;
1218 case 'P':
1219 ctx->Pflag = 1;
1220 ctx->pattern = parse_pattern(optarg);
1221 if (ctx->pattern < 0) {
031380d8 1222 g_free(ctx);
43642b38
DN
1223 return 0;
1224 }
1225 break;
1226 case 'q':
1227 ctx->qflag = 1;
1228 break;
1229 case 'v':
1230 ctx->vflag = 1;
1231 break;
1232 default:
031380d8 1233 g_free(ctx);
43642b38
DN
1234 return command_usage(&aio_read_cmd);
1235 }
1236 }
1237
1238 if (optind > argc - 2) {
031380d8 1239 g_free(ctx);
43642b38
DN
1240 return command_usage(&aio_read_cmd);
1241 }
1242
1243 ctx->offset = cvtnum(argv[optind]);
1244 if (ctx->offset < 0) {
1245 printf("non-numeric length argument -- %s\n", argv[optind]);
031380d8 1246 g_free(ctx);
43642b38
DN
1247 return 0;
1248 }
1249 optind++;
1250
1251 if (ctx->offset & 0x1ff) {
1252 printf("offset %" PRId64 " is not sector aligned\n",
1253 ctx->offset);
031380d8 1254 g_free(ctx);
43642b38
DN
1255 return 0;
1256 }
1257
1258 nr_iov = argc - optind;
1259 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
f2360620 1260 if (ctx->buf == NULL) {
031380d8 1261 g_free(ctx);
f2360620
KW
1262 return 0;
1263 }
43642b38
DN
1264
1265 gettimeofday(&ctx->t1, NULL);
ad54ae80
PB
1266 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1267 ctx->qiov.size >> 9, aio_read_done, ctx);
43642b38 1268 return 0;
95533d5f
CH
1269}
1270
43642b38 1271static void aio_write_help(void)
95533d5f 1272{
43642b38 1273 printf(
95533d5f 1274"\n"
43642b38 1275" asynchronously writes a range of bytes from the given offset source\n"
95533d5f
CH
1276" from multiple buffers\n"
1277"\n"
1278" Example:\n"
1279" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1280"\n"
1281" Writes into a segment of the currently open file, using a buffer\n"
1282" filled with a set pattern (0xcdcdcdcd).\n"
e432cef9 1283" The write is performed asynchronously and the aio_flush command must be\n"
96bab41d 1284" used to ensure all outstanding aio requests have been completed.\n"
95533d5f
CH
1285" -P, -- use different pattern to fill file\n"
1286" -C, -- report statistics in a machine parsable format\n"
095343ad 1287" -q, -- quiet mode, do not show I/O statistics\n"
95533d5f
CH
1288"\n");
1289}
1290
22a2bdcb
BS
1291static int aio_write_f(int argc, char **argv);
1292
1293static const cmdinfo_t aio_write_cmd = {
43642b38
DN
1294 .name = "aio_write",
1295 .cfunc = aio_write_f,
1296 .argmin = 2,
1297 .argmax = -1,
1298 .args = "[-Cq] [-P pattern ] off len [len..]",
1299 .oneline = "asynchronously writes a number of bytes",
1300 .help = aio_write_help,
22a2bdcb 1301};
95533d5f 1302
43642b38 1303static int aio_write_f(int argc, char **argv)
95533d5f 1304{
43642b38
DN
1305 int nr_iov, c;
1306 int pattern = 0xcd;
031380d8 1307 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
43642b38
DN
1308
1309 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1310 switch (c) {
1311 case 'C':
1312 ctx->Cflag = 1;
1313 break;
1314 case 'q':
1315 ctx->qflag = 1;
1316 break;
1317 case 'P':
1318 pattern = parse_pattern(optarg);
1319 if (pattern < 0) {
031380d8 1320 g_free(ctx);
43642b38
DN
1321 return 0;
1322 }
1323 break;
1324 default:
031380d8 1325 g_free(ctx);
43642b38
DN
1326 return command_usage(&aio_write_cmd);
1327 }
1328 }
1329
1330 if (optind > argc - 2) {
031380d8 1331 g_free(ctx);
43642b38
DN
1332 return command_usage(&aio_write_cmd);
1333 }
1334
1335 ctx->offset = cvtnum(argv[optind]);
1336 if (ctx->offset < 0) {
1337 printf("non-numeric length argument -- %s\n", argv[optind]);
031380d8 1338 g_free(ctx);
43642b38
DN
1339 return 0;
1340 }
1341 optind++;
1342
1343 if (ctx->offset & 0x1ff) {
1344 printf("offset %" PRId64 " is not sector aligned\n",
1345 ctx->offset);
031380d8 1346 g_free(ctx);
43642b38
DN
1347 return 0;
1348 }
1349
1350 nr_iov = argc - optind;
1351 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
f2360620 1352 if (ctx->buf == NULL) {
031380d8 1353 g_free(ctx);
f2360620
KW
1354 return 0;
1355 }
43642b38
DN
1356
1357 gettimeofday(&ctx->t1, NULL);
ad54ae80
PB
1358 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1359 ctx->qiov.size >> 9, aio_write_done, ctx);
43642b38 1360 return 0;
95533d5f
CH
1361}
1362
43642b38 1363static int aio_flush_f(int argc, char **argv)
95533d5f 1364{
43642b38
DN
1365 qemu_aio_flush();
1366 return 0;
95533d5f
CH
1367}
1368
1369static const cmdinfo_t aio_flush_cmd = {
43642b38
DN
1370 .name = "aio_flush",
1371 .cfunc = aio_flush_f,
1372 .oneline = "completes all outstanding aio requests"
95533d5f
CH
1373};
1374
43642b38 1375static int flush_f(int argc, char **argv)
e3aff4f6 1376{
43642b38
DN
1377 bdrv_flush(bs);
1378 return 0;
e3aff4f6
AL
1379}
1380
1381static const cmdinfo_t flush_cmd = {
43642b38
DN
1382 .name = "flush",
1383 .altname = "f",
1384 .cfunc = flush_f,
1385 .oneline = "flush all in-core file state to disk",
e3aff4f6
AL
1386};
1387
43642b38 1388static int truncate_f(int argc, char **argv)
e3aff4f6 1389{
43642b38
DN
1390 int64_t offset;
1391 int ret;
1392
1393 offset = cvtnum(argv[1]);
1394 if (offset < 0) {
1395 printf("non-numeric truncate argument -- %s\n", argv[1]);
1396 return 0;
1397 }
1398
1399 ret = bdrv_truncate(bs, offset);
1400 if (ret < 0) {
1401 printf("truncate: %s\n", strerror(-ret));
1402 return 0;
1403 }
1404
1405 return 0;
e3aff4f6
AL
1406}
1407
1408static const cmdinfo_t truncate_cmd = {
43642b38
DN
1409 .name = "truncate",
1410 .altname = "t",
1411 .cfunc = truncate_f,
1412 .argmin = 1,
1413 .argmax = 1,
1414 .args = "off",
1415 .oneline = "truncates the current file at the given offset",
e3aff4f6
AL
1416};
1417
43642b38 1418static int length_f(int argc, char **argv)
e3aff4f6 1419{
43642b38
DN
1420 int64_t size;
1421 char s1[64];
1422
1423 size = bdrv_getlength(bs);
1424 if (size < 0) {
1425 printf("getlength: %s\n", strerror(-size));
1426 return 0;
1427 }
1428
1429 cvtstr(size, s1, sizeof(s1));
1430 printf("%s\n", s1);
1431 return 0;
e3aff4f6
AL
1432}
1433
1434
1435static const cmdinfo_t length_cmd = {
43642b38
DN
1436 .name = "length",
1437 .altname = "l",
1438 .cfunc = length_f,
1439 .oneline = "gets the length of the current file",
e3aff4f6
AL
1440};
1441
1442
43642b38 1443static int info_f(int argc, char **argv)
e3aff4f6 1444{
43642b38
DN
1445 BlockDriverInfo bdi;
1446 char s1[64], s2[64];
1447 int ret;
e3aff4f6 1448
43642b38
DN
1449 if (bs->drv && bs->drv->format_name) {
1450 printf("format name: %s\n", bs->drv->format_name);
1451 }
1452 if (bs->drv && bs->drv->protocol_name) {
1453 printf("format name: %s\n", bs->drv->protocol_name);
1454 }
e3aff4f6 1455
43642b38
DN
1456 ret = bdrv_get_info(bs, &bdi);
1457 if (ret) {
1458 return 0;
1459 }
e3aff4f6 1460
43642b38
DN
1461 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1462 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
e3aff4f6 1463
43642b38
DN
1464 printf("cluster size: %s\n", s1);
1465 printf("vm state offset: %s\n", s2);
e3aff4f6 1466
43642b38 1467 return 0;
e3aff4f6
AL
1468}
1469
1470
1471
1472static const cmdinfo_t info_cmd = {
43642b38
DN
1473 .name = "info",
1474 .altname = "i",
1475 .cfunc = info_f,
1476 .oneline = "prints information about the current file",
e3aff4f6
AL
1477};
1478
43642b38 1479static void discard_help(void)
edff5db1 1480{
43642b38 1481 printf(
edff5db1
SH
1482"\n"
1483" discards a range of bytes from the given offset\n"
1484"\n"
1485" Example:\n"
1486" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1487"\n"
1488" Discards a segment of the currently open file.\n"
1489" -C, -- report statistics in a machine parsable format\n"
095343ad 1490" -q, -- quiet mode, do not show I/O statistics\n"
edff5db1
SH
1491"\n");
1492}
1493
1494static int discard_f(int argc, char **argv);
1495
1496static const cmdinfo_t discard_cmd = {
43642b38
DN
1497 .name = "discard",
1498 .altname = "d",
1499 .cfunc = discard_f,
1500 .argmin = 2,
1501 .argmax = -1,
1502 .args = "[-Cq] off len",
1503 .oneline = "discards a number of bytes at a specified offset",
1504 .help = discard_help,
edff5db1
SH
1505};
1506
43642b38 1507static int discard_f(int argc, char **argv)
edff5db1 1508{
43642b38
DN
1509 struct timeval t1, t2;
1510 int Cflag = 0, qflag = 0;
1511 int c, ret;
1512 int64_t offset;
1513 int count;
1514
1515 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1516 switch (c) {
1517 case 'C':
1518 Cflag = 1;
1519 break;
1520 case 'q':
1521 qflag = 1;
1522 break;
1523 default:
1524 return command_usage(&discard_cmd);
1525 }
1526 }
1527
1528 if (optind != argc - 2) {
1529 return command_usage(&discard_cmd);
1530 }
1531
1532 offset = cvtnum(argv[optind]);
1533 if (offset < 0) {
1534 printf("non-numeric length argument -- %s\n", argv[optind]);
1535 return 0;
1536 }
1537
1538 optind++;
1539 count = cvtnum(argv[optind]);
1540 if (count < 0) {
1541 printf("non-numeric length argument -- %s\n", argv[optind]);
1542 return 0;
1543 }
1544
1545 gettimeofday(&t1, NULL);
1546 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1547 count >> BDRV_SECTOR_BITS);
1548 gettimeofday(&t2, NULL);
1549
1550 if (ret < 0) {
1551 printf("discard failed: %s\n", strerror(-ret));
1552 goto out;
1553 }
1554
1555 /* Finally, report back -- -C gives a parsable format */
1556 if (!qflag) {
1557 t2 = tsub(t2, t1);
1558 print_report("discard", &t2, offset, count, count, 1, Cflag);
1559 }
edff5db1
SH
1560
1561out:
43642b38 1562 return 0;
edff5db1
SH
1563}
1564
43642b38 1565static int alloc_f(int argc, char **argv)
e3aff4f6 1566{
cc785c34 1567 int64_t offset, sector_num;
43642b38
DN
1568 int nb_sectors, remaining;
1569 char s1[64];
1570 int num, sum_alloc;
1571 int ret;
1572
1573 offset = cvtnum(argv[1]);
1574 if (offset & 0x1ff) {
1575 printf("offset %" PRId64 " is not sector aligned\n",
1576 offset);
1577 return 0;
1578 }
1579
1580 if (argc == 3) {
1581 nb_sectors = cvtnum(argv[2]);
1582 } else {
1583 nb_sectors = 1;
1584 }
1585
1586 remaining = nb_sectors;
1587 sum_alloc = 0;
cc785c34 1588 sector_num = offset >> 9;
43642b38 1589 while (remaining) {
cc785c34
PB
1590 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1591 sector_num += num;
43642b38
DN
1592 remaining -= num;
1593 if (ret) {
1594 sum_alloc += num;
1595 }
cc785c34
PB
1596 if (num == 0) {
1597 nb_sectors -= remaining;
1598 remaining = 0;
1599 }
43642b38
DN
1600 }
1601
1602 cvtstr(offset, s1, sizeof(s1));
1603
1604 printf("%d/%d sectors allocated at offset %s\n",
1605 sum_alloc, nb_sectors, s1);
1606 return 0;
e3aff4f6
AL
1607}
1608
1609static const cmdinfo_t alloc_cmd = {
43642b38
DN
1610 .name = "alloc",
1611 .altname = "a",
1612 .argmin = 1,
1613 .argmax = 2,
1614 .cfunc = alloc_f,
1615 .args = "off [sectors]",
1616 .oneline = "checks if a sector is present in the file",
e3aff4f6
AL
1617};
1618
43642b38 1619static int map_f(int argc, char **argv)
191c2890 1620{
43642b38
DN
1621 int64_t offset;
1622 int64_t nb_sectors;
1623 char s1[64];
1624 int num, num_checked;
1625 int ret;
1626 const char *retstr;
1627
1628 offset = 0;
1629 nb_sectors = bs->total_sectors;
1630
1631 do {
1632 num_checked = MIN(nb_sectors, INT_MAX);
1633 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1634 retstr = ret ? " allocated" : "not allocated";
1635 cvtstr(offset << 9ULL, s1, sizeof(s1));
1636 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1637 offset << 9ULL, num, num_checked, retstr, s1, ret);
1638
1639 offset += num;
1640 nb_sectors -= num;
1641 } while (offset < bs->total_sectors);
1642
1643 return 0;
191c2890
KW
1644}
1645
1646static const cmdinfo_t map_cmd = {
1647 .name = "map",
1648 .argmin = 0,
1649 .argmax = 0,
1650 .cfunc = map_f,
1651 .args = "",
1652 .oneline = "prints the allocated areas of a file",
1653};
1654
e01c30d3
SH
1655static int abort_f(int argc, char **argv)
1656{
1657 abort();
1658}
1659
1660static const cmdinfo_t abort_cmd = {
1661 .name = "abort",
1662 .cfunc = abort_f,
1663 .flags = CMD_NOFILE_OK,
1664 .oneline = "simulate a program crash using abort(3)",
1665};
191c2890 1666
43642b38 1667static int close_f(int argc, char **argv)
e3aff4f6 1668{
b4657855 1669 bdrv_delete(bs);
43642b38
DN
1670 bs = NULL;
1671 return 0;
e3aff4f6
AL
1672}
1673
1674static const cmdinfo_t close_cmd = {
43642b38
DN
1675 .name = "close",
1676 .altname = "c",
1677 .cfunc = close_f,
1678 .oneline = "close the current open file",
e3aff4f6
AL
1679};
1680
9c4bab26 1681static int openfile(char *name, int flags, int growable)
e3aff4f6 1682{
43642b38
DN
1683 if (bs) {
1684 fprintf(stderr, "file open already, try 'help close'\n");
1685 return 1;
1686 }
1687
1688 if (growable) {
1689 if (bdrv_file_open(&bs, name, flags)) {
1690 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1691 return 1;
1692 }
1693 } else {
1694 bs = bdrv_new("hda");
1695
1696 if (bdrv_open(bs, name, flags, NULL) < 0) {
1697 fprintf(stderr, "%s: can't open device %s\n", progname, name);
b4657855 1698 bdrv_delete(bs);
43642b38
DN
1699 bs = NULL;
1700 return 1;
1701 }
1702 }
1703
1704 return 0;
e3aff4f6
AL
1705}
1706
43642b38 1707static void open_help(void)
e3aff4f6 1708{
43642b38 1709 printf(
e3aff4f6
AL
1710"\n"
1711" opens a new file in the requested mode\n"
1712"\n"
1713" Example:\n"
1714" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1715"\n"
1716" Opens a file for subsequent use by all of the other qemu-io commands.\n"
e3aff4f6
AL
1717" -r, -- open file read-only\n"
1718" -s, -- use snapshot file\n"
1719" -n, -- disable host cache\n"
9c4bab26 1720" -g, -- allow file to grow (only applies to protocols)"
e3aff4f6
AL
1721"\n");
1722}
1723
22a2bdcb
BS
1724static int open_f(int argc, char **argv);
1725
1726static const cmdinfo_t open_cmd = {
43642b38
DN
1727 .name = "open",
1728 .altname = "o",
1729 .cfunc = open_f,
1730 .argmin = 1,
1731 .argmax = -1,
1732 .flags = CMD_NOFILE_OK,
1733 .args = "[-Crsn] [path]",
1734 .oneline = "open the file specified by path",
1735 .help = open_help,
22a2bdcb 1736};
e3aff4f6 1737
43642b38 1738static int open_f(int argc, char **argv)
e3aff4f6 1739{
43642b38
DN
1740 int flags = 0;
1741 int readonly = 0;
1742 int growable = 0;
1743 int c;
1744
1745 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1746 switch (c) {
1747 case 's':
1748 flags |= BDRV_O_SNAPSHOT;
1749 break;
1750 case 'n':
1751 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1752 break;
1753 case 'r':
1754 readonly = 1;
1755 break;
1756 case 'g':
1757 growable = 1;
1758 break;
1759 default:
1760 return command_usage(&open_cmd);
f5edb014 1761 }
43642b38
DN
1762 }
1763
1764 if (!readonly) {
1765 flags |= BDRV_O_RDWR;
1766 }
e3aff4f6 1767
43642b38
DN
1768 if (optind != argc - 1) {
1769 return command_usage(&open_cmd);
1770 }
e3aff4f6 1771
43642b38 1772 return openfile(argv[optind], flags, growable);
e3aff4f6
AL
1773}
1774
43642b38 1775static int init_args_command(int index)
e3aff4f6 1776{
43642b38
DN
1777 /* only one device allowed so far */
1778 if (index >= 1) {
1779 return 0;
1780 }
1781 return ++index;
e3aff4f6
AL
1782}
1783
43642b38 1784static int init_check_command(const cmdinfo_t *ct)
e3aff4f6 1785{
43642b38
DN
1786 if (ct->flags & CMD_FLAG_GLOBAL) {
1787 return 1;
1788 }
1789 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1790 fprintf(stderr, "no file open, try 'help open'\n");
1791 return 0;
1792 }
1793 return 1;
e3aff4f6
AL
1794}
1795
1796static void usage(const char *name)
1797{
43642b38 1798 printf(
9a2d77ad 1799"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
84844a20 1800"QEMU Disk exerciser\n"
e3aff4f6 1801"\n"
e3aff4f6
AL
1802" -c, --cmd command to execute\n"
1803" -r, --read-only export read-only\n"
1804" -s, --snapshot use snapshot file\n"
1805" -n, --nocache disable host cache\n"
1db6947d 1806" -g, --growable allow file to grow (only applies to protocols)\n"
e3aff4f6 1807" -m, --misalign misalign allocations for O_DIRECT\n"
5c6c3a6c 1808" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
592fa070 1809" -t, --cache=MODE use the given cache mode for the image\n"
d7bb72c8 1810" -T, --trace FILE enable trace events listed in the given file\n"
e3aff4f6
AL
1811" -h, --help display this help and exit\n"
1812" -V, --version output version information and exit\n"
1813"\n",
43642b38 1814 name);
e3aff4f6
AL
1815}
1816
1817
1818int main(int argc, char **argv)
1819{
43642b38
DN
1820 int readonly = 0;
1821 int growable = 0;
592fa070 1822 const char *sopt = "hVc:rsnmgkt:T:";
43642b38
DN
1823 const struct option lopt[] = {
1824 { "help", 0, NULL, 'h' },
1825 { "version", 0, NULL, 'V' },
1826 { "offset", 1, NULL, 'o' },
1827 { "cmd", 1, NULL, 'c' },
1828 { "read-only", 0, NULL, 'r' },
1829 { "snapshot", 0, NULL, 's' },
1830 { "nocache", 0, NULL, 'n' },
1831 { "misalign", 0, NULL, 'm' },
1832 { "growable", 0, NULL, 'g' },
1833 { "native-aio", 0, NULL, 'k' },
592fa070 1834 { "cache", 1, NULL, 't' },
d7bb72c8 1835 { "trace", 1, NULL, 'T' },
43642b38
DN
1836 { NULL, 0, NULL, 0 }
1837 };
1838 int c;
1839 int opt_index = 0;
1840 int flags = 0;
1841
1842 progname = basename(argv[0]);
1843
1844 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1845 switch (c) {
1846 case 's':
1847 flags |= BDRV_O_SNAPSHOT;
1848 break;
1849 case 'n':
1850 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1851 break;
1852 case 'c':
1853 add_user_command(optarg);
1854 break;
1855 case 'r':
1856 readonly = 1;
1857 break;
1858 case 'm':
1859 misalign = 1;
1860 break;
1861 case 'g':
1862 growable = 1;
1863 break;
1864 case 'k':
1865 flags |= BDRV_O_NATIVE_AIO;
1866 break;
592fa070
KW
1867 case 't':
1868 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
1869 error_report("Invalid cache option: %s", optarg);
1870 exit(1);
1871 }
1872 break;
d7bb72c8
SH
1873 case 'T':
1874 if (!trace_backend_init(optarg, NULL)) {
1875 exit(1); /* error message will have been printed */
1876 }
1877 break;
43642b38
DN
1878 case 'V':
1879 printf("%s version %s\n", progname, VERSION);
1880 exit(0);
1881 case 'h':
1882 usage(progname);
1883 exit(0);
1884 default:
1885 usage(progname);
1886 exit(1);
f5edb014 1887 }
43642b38
DN
1888 }
1889
1890 if ((argc - optind) > 1) {
1891 usage(progname);
1892 exit(1);
1893 }
e3aff4f6 1894
43642b38
DN
1895 bdrv_init();
1896
a57d1143
ZYW
1897 qemu_init_main_loop();
1898
43642b38
DN
1899 /* initialize commands */
1900 quit_init();
1901 help_init();
1902 add_command(&open_cmd);
1903 add_command(&close_cmd);
1904 add_command(&read_cmd);
1905 add_command(&readv_cmd);
1906 add_command(&write_cmd);
1907 add_command(&writev_cmd);
1908 add_command(&multiwrite_cmd);
1909 add_command(&aio_read_cmd);
1910 add_command(&aio_write_cmd);
1911 add_command(&aio_flush_cmd);
1912 add_command(&flush_cmd);
1913 add_command(&truncate_cmd);
1914 add_command(&length_cmd);
1915 add_command(&info_cmd);
1916 add_command(&discard_cmd);
1917 add_command(&alloc_cmd);
1918 add_command(&map_cmd);
e01c30d3 1919 add_command(&abort_cmd);
43642b38
DN
1920
1921 add_args_command(init_args_command);
1922 add_check_command(init_check_command);
1923
1924 /* open the device */
1925 if (!readonly) {
1926 flags |= BDRV_O_RDWR;
1927 }
1928
1929 if ((argc - optind) == 1) {
1930 openfile(argv[optind], flags, growable);
1931 }
1932 command_loop();
e3aff4f6 1933
43642b38 1934 /*
922453bc 1935 * Make sure all outstanding requests complete before the program exits.
43642b38 1936 */
922453bc 1937 bdrv_drain_all();
95533d5f 1938
43642b38 1939 if (bs) {
b4657855 1940 bdrv_delete(bs);
43642b38
DN
1941 }
1942 return 0;
e3aff4f6 1943}