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