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