]> git.proxmox.com Git - qemu.git/blob - qemu-io.c
qemu-io: Handle create_iovec errors
[qemu.git] / qemu-io.c
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 */
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
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
25 char *progname;
26 static BlockDriverState *bs;
27
28 static int misalign;
29
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 */
36 static 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
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
58 static void *qemu_io_alloc(size_t len, int pattern)
59 {
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;
71 }
72
73 static void qemu_io_free(void *p)
74 {
75 if (misalign) {
76 p -= MISALIGN_OFFSET;
77 }
78 qemu_vfree(p);
79 }
80
81 static void dump_buffer(const void *buffer, int64_t offset, int len)
82 {
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 }
103 }
104
105 static void print_report(const char *op, struct timeval *t, int64_t offset,
106 int count, int total, int cnt, int Cflag)
107 {
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 }
124 }
125
126 /*
127 * Parse multiple length statements for vectored I/O, and construct an I/O
128 * vector matching it.
129 */
130 static void *
131 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
132 {
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 }
173
174 fail:
175 free(sizes);
176 return buf;
177 }
178
179 static int do_read(char *buf, int64_t offset, int count, int *total)
180 {
181 int ret;
182
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;
189 }
190
191 static int do_write(char *buf, int64_t offset, int count, int *total)
192 {
193 int ret;
194
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;
201 }
202
203 static int do_pread(char *buf, int64_t offset, int count, int *total)
204 {
205 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
206 if (*total < 0) {
207 return *total;
208 }
209 return 1;
210 }
211
212 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
213 {
214 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
215 if (*total < 0) {
216 return *total;
217 }
218 return 1;
219 }
220
221 static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
222 {
223 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
224 if (*total < 0) {
225 return *total;
226 }
227 return 1;
228 }
229
230 static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
231 {
232 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
233 if (*total < 0) {
234 return *total;
235 }
236 return 1;
237 }
238
239 #define NOT_DONE 0x7fffffff
240 static void aio_rw_done(void *opaque, int ret)
241 {
242 *(int *)opaque = ret;
243 }
244
245 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
246 {
247 BlockDriverAIOCB *acb;
248 int async_ret = NOT_DONE;
249
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 }
258
259 *total = qiov->size;
260 return async_ret < 0 ? async_ret : 1;
261 }
262
263 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
264 {
265 BlockDriverAIOCB *acb;
266 int async_ret = NOT_DONE;
267
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 }
273
274 while (async_ret == NOT_DONE) {
275 qemu_aio_wait();
276 }
277
278 *total = qiov->size;
279 return async_ret < 0 ? async_ret : 1;
280 }
281
282 struct multiwrite_async_ret {
283 int num_done;
284 int error;
285 };
286
287 static void multiwrite_cb(void *opaque, int ret)
288 {
289 struct multiwrite_async_ret *async_ret = opaque;
290
291 async_ret->num_done++;
292 if (ret < 0) {
293 async_ret->error = ret;
294 }
295 }
296
297 static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
298 {
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;
322 }
323
324 static void read_help(void)
325 {
326 printf(
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"
335 " -b, -- read from the VM state rather than the virtual disk\n"
336 " -C, -- report statistics in a machine parsable format\n"
337 " -l, -- length for pattern verification (only with -P)\n"
338 " -p, -- use bdrv_pread to read the file\n"
339 " -P, -- use a pattern to verify read data\n"
340 " -q, -- quiet mode, do not show I/O statistics\n"
341 " -s, -- start offset for pattern verification (only with -P)\n"
342 " -v, -- dump buffer to standard output\n"
343 "\n");
344 }
345
346 static int read_f(int argc, char **argv);
347
348 static const cmdinfo_t read_cmd = {
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,
357 };
358
359 static int read_f(int argc, char **argv)
360 {
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 }
438
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
452 if (!pflag) {
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 }
463 }
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);
504
505 out:
506 qemu_io_free(buf);
507
508 return 0;
509 }
510
511 static void readv_help(void)
512 {
513 printf(
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"
524 " -P, -- use a pattern to verify read data\n"
525 " -v, -- dump buffer to standard output\n"
526 " -q, -- quiet mode, do not show I/O statistics\n"
527 "\n");
528 }
529
530 static int readv_f(int argc, char **argv);
531
532 static const cmdinfo_t readv_cmd = {
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,
540 };
541
542 static int readv_f(int argc, char **argv)
543 {
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);
599 if (buf == NULL) {
600 return 0;
601 }
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);
633
634 out:
635 qemu_io_free(buf);
636 return 0;
637 }
638
639 static void write_help(void)
640 {
641 printf(
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"
650 " -b, -- write to the VM state rather than the virtual disk\n"
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"
654 " -q, -- quiet mode, do not show I/O statistics\n"
655 "\n");
656 }
657
658 static int write_f(int argc, char **argv);
659
660 static const cmdinfo_t write_cmd = {
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,
669 };
670
671 static int write_f(int argc, char **argv)
672 {
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);
768
769 out:
770 qemu_io_free(buf);
771
772 return 0;
773 }
774
775 static void
776 writev_help(void)
777 {
778 printf(
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"
789 " -q, -- quiet mode, do not show I/O statistics\n"
790 "\n");
791 }
792
793 static int writev_f(int argc, char **argv);
794
795 static const cmdinfo_t writev_cmd = {
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,
803 };
804
805 static int writev_f(int argc, char **argv)
806 {
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);
856 if (buf == NULL) {
857 return 0;
858 }
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);
876 out:
877 qemu_io_free(buf);
878 return 0;
879 }
880
881 static void multiwrite_help(void)
882 {
883 printf(
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"
889 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
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
901 static int multiwrite_f(int argc, char **argv);
902
903 static const cmdinfo_t multiwrite_cmd = {
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,
911 };
912
913 static int multiwrite_f(int argc, char **argv)
914 {
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
959 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
960 buf = g_malloc0(nr_reqs * sizeof(*buf));
961 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
962
963 for (i = 0; i < nr_reqs; i++) {
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]);
970 return 0;
971 }
972 optind++;
973
974 if (offset & 0x1ff) {
975 printf("offset %lld is not sector aligned\n",
976 (long long)offset);
977 return 0;
978 }
979
980 if (i == 0) {
981 first_offset = offset;
982 }
983
984 /* Read lengths for qiov entries */
985 for (j = optind; j < argc; j++) {
986 if (!strcmp(argv[j], ";")) {
987 break;
988 }
989 }
990
991 nr_iov = j - optind;
992
993 /* Build request */
994 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
995 if (buf[i] == NULL) {
996 goto out;
997 }
998
999 reqs[i].qiov = &qiovs[i];
1000 reqs[i].sector = offset >> 9;
1001 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1002
1003 optind = j + 1;
1004
1005 pattern++;
1006 }
1007
1008 gettimeofday(&t1, NULL);
1009 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1010 gettimeofday(&t2, NULL);
1011
1012 if (cnt < 0) {
1013 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1014 goto out;
1015 }
1016
1017 if (qflag) {
1018 goto out;
1019 }
1020
1021 /* Finally, report back -- -C gives a parsable format */
1022 t2 = tsub(t2, t1);
1023 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1024 out:
1025 for (i = 0; i < nr_reqs; i++) {
1026 qemu_io_free(buf[i]);
1027 if (reqs[i].qiov != NULL) {
1028 qemu_iovec_destroy(&qiovs[i]);
1029 }
1030 }
1031 g_free(buf);
1032 g_free(reqs);
1033 g_free(qiovs);
1034 return 0;
1035 }
1036
1037 struct aio_ctx {
1038 QEMUIOVector qiov;
1039 int64_t offset;
1040 char *buf;
1041 int qflag;
1042 int vflag;
1043 int Cflag;
1044 int Pflag;
1045 int pattern;
1046 struct timeval t1;
1047 };
1048
1049 static void aio_write_done(void *opaque, int ret)
1050 {
1051 struct aio_ctx *ctx = opaque;
1052 struct timeval t2;
1053
1054 gettimeofday(&t2, NULL);
1055
1056
1057 if (ret < 0) {
1058 printf("aio_write failed: %s\n", strerror(-ret));
1059 goto out;
1060 }
1061
1062 if (ctx->qflag) {
1063 goto out;
1064 }
1065
1066 /* Finally, report back -- -C gives a parsable format */
1067 t2 = tsub(t2, ctx->t1);
1068 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1069 ctx->qiov.size, 1, ctx->Cflag);
1070 out:
1071 qemu_io_free(ctx->buf);
1072 free(ctx);
1073 }
1074
1075 static void aio_read_done(void *opaque, int ret)
1076 {
1077 struct aio_ctx *ctx = opaque;
1078 struct timeval t2;
1079
1080 gettimeofday(&t2, NULL);
1081
1082 if (ret < 0) {
1083 printf("readv failed: %s\n", strerror(-ret));
1084 goto out;
1085 }
1086
1087 if (ctx->Pflag) {
1088 void *cmp_buf = malloc(ctx->qiov.size);
1089
1090 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1091 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1092 printf("Pattern verification failed at offset %"
1093 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1094 }
1095 free(cmp_buf);
1096 }
1097
1098 if (ctx->qflag) {
1099 goto out;
1100 }
1101
1102 if (ctx->vflag) {
1103 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1104 }
1105
1106 /* Finally, report back -- -C gives a parsable format */
1107 t2 = tsub(t2, ctx->t1);
1108 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1109 ctx->qiov.size, 1, ctx->Cflag);
1110 out:
1111 qemu_io_free(ctx->buf);
1112 free(ctx);
1113 }
1114
1115 static void aio_read_help(void)
1116 {
1117 printf(
1118 "\n"
1119 " asynchronously reads a range of bytes from the given offset\n"
1120 "\n"
1121 " Example:\n"
1122 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1123 "\n"
1124 " Reads a segment of the currently open file, optionally dumping it to the\n"
1125 " standard output stream (with -v option) for subsequent inspection.\n"
1126 " The read is performed asynchronously and the aio_flush command must be\n"
1127 " used to ensure all outstanding aio requests have been completed\n"
1128 " -C, -- report statistics in a machine parsable format\n"
1129 " -P, -- use a pattern to verify read data\n"
1130 " -v, -- dump buffer to standard output\n"
1131 " -q, -- quiet mode, do not show I/O statistics\n"
1132 "\n");
1133 }
1134
1135 static int aio_read_f(int argc, char **argv);
1136
1137 static const cmdinfo_t aio_read_cmd = {
1138 .name = "aio_read",
1139 .cfunc = aio_read_f,
1140 .argmin = 2,
1141 .argmax = -1,
1142 .args = "[-Cqv] [-P pattern ] off len [len..]",
1143 .oneline = "asynchronously reads a number of bytes",
1144 .help = aio_read_help,
1145 };
1146
1147 static int aio_read_f(int argc, char **argv)
1148 {
1149 int nr_iov, c;
1150 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1151 BlockDriverAIOCB *acb;
1152
1153 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1154 switch (c) {
1155 case 'C':
1156 ctx->Cflag = 1;
1157 break;
1158 case 'P':
1159 ctx->Pflag = 1;
1160 ctx->pattern = parse_pattern(optarg);
1161 if (ctx->pattern < 0) {
1162 free(ctx);
1163 return 0;
1164 }
1165 break;
1166 case 'q':
1167 ctx->qflag = 1;
1168 break;
1169 case 'v':
1170 ctx->vflag = 1;
1171 break;
1172 default:
1173 free(ctx);
1174 return command_usage(&aio_read_cmd);
1175 }
1176 }
1177
1178 if (optind > argc - 2) {
1179 free(ctx);
1180 return command_usage(&aio_read_cmd);
1181 }
1182
1183 ctx->offset = cvtnum(argv[optind]);
1184 if (ctx->offset < 0) {
1185 printf("non-numeric length argument -- %s\n", argv[optind]);
1186 free(ctx);
1187 return 0;
1188 }
1189 optind++;
1190
1191 if (ctx->offset & 0x1ff) {
1192 printf("offset %" PRId64 " is not sector aligned\n",
1193 ctx->offset);
1194 free(ctx);
1195 return 0;
1196 }
1197
1198 nr_iov = argc - optind;
1199 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
1200 if (ctx->buf == NULL) {
1201 free(ctx);
1202 return 0;
1203 }
1204
1205 gettimeofday(&ctx->t1, NULL);
1206 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1207 ctx->qiov.size >> 9, aio_read_done, ctx);
1208 if (!acb) {
1209 free(ctx->buf);
1210 free(ctx);
1211 return -EIO;
1212 }
1213
1214 return 0;
1215 }
1216
1217 static void aio_write_help(void)
1218 {
1219 printf(
1220 "\n"
1221 " asynchronously writes a range of bytes from the given offset source\n"
1222 " from multiple buffers\n"
1223 "\n"
1224 " Example:\n"
1225 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1226 "\n"
1227 " Writes into a segment of the currently open file, using a buffer\n"
1228 " filled with a set pattern (0xcdcdcdcd).\n"
1229 " The write is performed asynchronously and the aio_flush command must be\n"
1230 " used to ensure all outstanding aio requests have been completed\n"
1231 " -P, -- use different pattern to fill file\n"
1232 " -C, -- report statistics in a machine parsable format\n"
1233 " -q, -- quiet mode, do not show I/O statistics\n"
1234 "\n");
1235 }
1236
1237 static int aio_write_f(int argc, char **argv);
1238
1239 static const cmdinfo_t aio_write_cmd = {
1240 .name = "aio_write",
1241 .cfunc = aio_write_f,
1242 .argmin = 2,
1243 .argmax = -1,
1244 .args = "[-Cq] [-P pattern ] off len [len..]",
1245 .oneline = "asynchronously writes a number of bytes",
1246 .help = aio_write_help,
1247 };
1248
1249 static int aio_write_f(int argc, char **argv)
1250 {
1251 int nr_iov, c;
1252 int pattern = 0xcd;
1253 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1254 BlockDriverAIOCB *acb;
1255
1256 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1257 switch (c) {
1258 case 'C':
1259 ctx->Cflag = 1;
1260 break;
1261 case 'q':
1262 ctx->qflag = 1;
1263 break;
1264 case 'P':
1265 pattern = parse_pattern(optarg);
1266 if (pattern < 0) {
1267 free(ctx);
1268 return 0;
1269 }
1270 break;
1271 default:
1272 free(ctx);
1273 return command_usage(&aio_write_cmd);
1274 }
1275 }
1276
1277 if (optind > argc - 2) {
1278 free(ctx);
1279 return command_usage(&aio_write_cmd);
1280 }
1281
1282 ctx->offset = cvtnum(argv[optind]);
1283 if (ctx->offset < 0) {
1284 printf("non-numeric length argument -- %s\n", argv[optind]);
1285 free(ctx);
1286 return 0;
1287 }
1288 optind++;
1289
1290 if (ctx->offset & 0x1ff) {
1291 printf("offset %" PRId64 " is not sector aligned\n",
1292 ctx->offset);
1293 free(ctx);
1294 return 0;
1295 }
1296
1297 nr_iov = argc - optind;
1298 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1299 if (ctx->buf == NULL) {
1300 free(ctx);
1301 return 0;
1302 }
1303
1304 gettimeofday(&ctx->t1, NULL);
1305 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1306 ctx->qiov.size >> 9, aio_write_done, ctx);
1307 if (!acb) {
1308 free(ctx->buf);
1309 free(ctx);
1310 return -EIO;
1311 }
1312
1313 return 0;
1314 }
1315
1316 static int aio_flush_f(int argc, char **argv)
1317 {
1318 qemu_aio_flush();
1319 return 0;
1320 }
1321
1322 static const cmdinfo_t aio_flush_cmd = {
1323 .name = "aio_flush",
1324 .cfunc = aio_flush_f,
1325 .oneline = "completes all outstanding aio requests"
1326 };
1327
1328 static int flush_f(int argc, char **argv)
1329 {
1330 bdrv_flush(bs);
1331 return 0;
1332 }
1333
1334 static const cmdinfo_t flush_cmd = {
1335 .name = "flush",
1336 .altname = "f",
1337 .cfunc = flush_f,
1338 .oneline = "flush all in-core file state to disk",
1339 };
1340
1341 static int truncate_f(int argc, char **argv)
1342 {
1343 int64_t offset;
1344 int ret;
1345
1346 offset = cvtnum(argv[1]);
1347 if (offset < 0) {
1348 printf("non-numeric truncate argument -- %s\n", argv[1]);
1349 return 0;
1350 }
1351
1352 ret = bdrv_truncate(bs, offset);
1353 if (ret < 0) {
1354 printf("truncate: %s\n", strerror(-ret));
1355 return 0;
1356 }
1357
1358 return 0;
1359 }
1360
1361 static const cmdinfo_t truncate_cmd = {
1362 .name = "truncate",
1363 .altname = "t",
1364 .cfunc = truncate_f,
1365 .argmin = 1,
1366 .argmax = 1,
1367 .args = "off",
1368 .oneline = "truncates the current file at the given offset",
1369 };
1370
1371 static int length_f(int argc, char **argv)
1372 {
1373 int64_t size;
1374 char s1[64];
1375
1376 size = bdrv_getlength(bs);
1377 if (size < 0) {
1378 printf("getlength: %s\n", strerror(-size));
1379 return 0;
1380 }
1381
1382 cvtstr(size, s1, sizeof(s1));
1383 printf("%s\n", s1);
1384 return 0;
1385 }
1386
1387
1388 static const cmdinfo_t length_cmd = {
1389 .name = "length",
1390 .altname = "l",
1391 .cfunc = length_f,
1392 .oneline = "gets the length of the current file",
1393 };
1394
1395
1396 static int info_f(int argc, char **argv)
1397 {
1398 BlockDriverInfo bdi;
1399 char s1[64], s2[64];
1400 int ret;
1401
1402 if (bs->drv && bs->drv->format_name) {
1403 printf("format name: %s\n", bs->drv->format_name);
1404 }
1405 if (bs->drv && bs->drv->protocol_name) {
1406 printf("format name: %s\n", bs->drv->protocol_name);
1407 }
1408
1409 ret = bdrv_get_info(bs, &bdi);
1410 if (ret) {
1411 return 0;
1412 }
1413
1414 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1415 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1416
1417 printf("cluster size: %s\n", s1);
1418 printf("vm state offset: %s\n", s2);
1419
1420 return 0;
1421 }
1422
1423
1424
1425 static const cmdinfo_t info_cmd = {
1426 .name = "info",
1427 .altname = "i",
1428 .cfunc = info_f,
1429 .oneline = "prints information about the current file",
1430 };
1431
1432 static void discard_help(void)
1433 {
1434 printf(
1435 "\n"
1436 " discards a range of bytes from the given offset\n"
1437 "\n"
1438 " Example:\n"
1439 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1440 "\n"
1441 " Discards a segment of the currently open file.\n"
1442 " -C, -- report statistics in a machine parsable format\n"
1443 " -q, -- quiet mode, do not show I/O statistics\n"
1444 "\n");
1445 }
1446
1447 static int discard_f(int argc, char **argv);
1448
1449 static const cmdinfo_t discard_cmd = {
1450 .name = "discard",
1451 .altname = "d",
1452 .cfunc = discard_f,
1453 .argmin = 2,
1454 .argmax = -1,
1455 .args = "[-Cq] off len",
1456 .oneline = "discards a number of bytes at a specified offset",
1457 .help = discard_help,
1458 };
1459
1460 static int discard_f(int argc, char **argv)
1461 {
1462 struct timeval t1, t2;
1463 int Cflag = 0, qflag = 0;
1464 int c, ret;
1465 int64_t offset;
1466 int count;
1467
1468 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1469 switch (c) {
1470 case 'C':
1471 Cflag = 1;
1472 break;
1473 case 'q':
1474 qflag = 1;
1475 break;
1476 default:
1477 return command_usage(&discard_cmd);
1478 }
1479 }
1480
1481 if (optind != argc - 2) {
1482 return command_usage(&discard_cmd);
1483 }
1484
1485 offset = cvtnum(argv[optind]);
1486 if (offset < 0) {
1487 printf("non-numeric length argument -- %s\n", argv[optind]);
1488 return 0;
1489 }
1490
1491 optind++;
1492 count = cvtnum(argv[optind]);
1493 if (count < 0) {
1494 printf("non-numeric length argument -- %s\n", argv[optind]);
1495 return 0;
1496 }
1497
1498 gettimeofday(&t1, NULL);
1499 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1500 count >> BDRV_SECTOR_BITS);
1501 gettimeofday(&t2, NULL);
1502
1503 if (ret < 0) {
1504 printf("discard failed: %s\n", strerror(-ret));
1505 goto out;
1506 }
1507
1508 /* Finally, report back -- -C gives a parsable format */
1509 if (!qflag) {
1510 t2 = tsub(t2, t1);
1511 print_report("discard", &t2, offset, count, count, 1, Cflag);
1512 }
1513
1514 out:
1515 return 0;
1516 }
1517
1518 static int alloc_f(int argc, char **argv)
1519 {
1520 int64_t offset;
1521 int nb_sectors, remaining;
1522 char s1[64];
1523 int num, sum_alloc;
1524 int ret;
1525
1526 offset = cvtnum(argv[1]);
1527 if (offset & 0x1ff) {
1528 printf("offset %" PRId64 " is not sector aligned\n",
1529 offset);
1530 return 0;
1531 }
1532
1533 if (argc == 3) {
1534 nb_sectors = cvtnum(argv[2]);
1535 } else {
1536 nb_sectors = 1;
1537 }
1538
1539 remaining = nb_sectors;
1540 sum_alloc = 0;
1541 while (remaining) {
1542 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1543 remaining -= num;
1544 if (ret) {
1545 sum_alloc += num;
1546 }
1547 }
1548
1549 cvtstr(offset, s1, sizeof(s1));
1550
1551 printf("%d/%d sectors allocated at offset %s\n",
1552 sum_alloc, nb_sectors, s1);
1553 return 0;
1554 }
1555
1556 static const cmdinfo_t alloc_cmd = {
1557 .name = "alloc",
1558 .altname = "a",
1559 .argmin = 1,
1560 .argmax = 2,
1561 .cfunc = alloc_f,
1562 .args = "off [sectors]",
1563 .oneline = "checks if a sector is present in the file",
1564 };
1565
1566 static int map_f(int argc, char **argv)
1567 {
1568 int64_t offset;
1569 int64_t nb_sectors;
1570 char s1[64];
1571 int num, num_checked;
1572 int ret;
1573 const char *retstr;
1574
1575 offset = 0;
1576 nb_sectors = bs->total_sectors;
1577
1578 do {
1579 num_checked = MIN(nb_sectors, INT_MAX);
1580 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1581 retstr = ret ? " allocated" : "not allocated";
1582 cvtstr(offset << 9ULL, s1, sizeof(s1));
1583 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1584 offset << 9ULL, num, num_checked, retstr, s1, ret);
1585
1586 offset += num;
1587 nb_sectors -= num;
1588 } while (offset < bs->total_sectors);
1589
1590 return 0;
1591 }
1592
1593 static const cmdinfo_t map_cmd = {
1594 .name = "map",
1595 .argmin = 0,
1596 .argmax = 0,
1597 .cfunc = map_f,
1598 .args = "",
1599 .oneline = "prints the allocated areas of a file",
1600 };
1601
1602
1603 static int close_f(int argc, char **argv)
1604 {
1605 bdrv_delete(bs);
1606 bs = NULL;
1607 return 0;
1608 }
1609
1610 static const cmdinfo_t close_cmd = {
1611 .name = "close",
1612 .altname = "c",
1613 .cfunc = close_f,
1614 .oneline = "close the current open file",
1615 };
1616
1617 static int openfile(char *name, int flags, int growable)
1618 {
1619 if (bs) {
1620 fprintf(stderr, "file open already, try 'help close'\n");
1621 return 1;
1622 }
1623
1624 if (growable) {
1625 if (bdrv_file_open(&bs, name, flags)) {
1626 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1627 return 1;
1628 }
1629 } else {
1630 bs = bdrv_new("hda");
1631
1632 if (bdrv_open(bs, name, flags, NULL) < 0) {
1633 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1634 bdrv_delete(bs);
1635 bs = NULL;
1636 return 1;
1637 }
1638 }
1639
1640 return 0;
1641 }
1642
1643 static void open_help(void)
1644 {
1645 printf(
1646 "\n"
1647 " opens a new file in the requested mode\n"
1648 "\n"
1649 " Example:\n"
1650 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1651 "\n"
1652 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1653 " -r, -- open file read-only\n"
1654 " -s, -- use snapshot file\n"
1655 " -n, -- disable host cache\n"
1656 " -g, -- allow file to grow (only applies to protocols)"
1657 "\n");
1658 }
1659
1660 static int open_f(int argc, char **argv);
1661
1662 static const cmdinfo_t open_cmd = {
1663 .name = "open",
1664 .altname = "o",
1665 .cfunc = open_f,
1666 .argmin = 1,
1667 .argmax = -1,
1668 .flags = CMD_NOFILE_OK,
1669 .args = "[-Crsn] [path]",
1670 .oneline = "open the file specified by path",
1671 .help = open_help,
1672 };
1673
1674 static int open_f(int argc, char **argv)
1675 {
1676 int flags = 0;
1677 int readonly = 0;
1678 int growable = 0;
1679 int c;
1680
1681 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1682 switch (c) {
1683 case 's':
1684 flags |= BDRV_O_SNAPSHOT;
1685 break;
1686 case 'n':
1687 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1688 break;
1689 case 'r':
1690 readonly = 1;
1691 break;
1692 case 'g':
1693 growable = 1;
1694 break;
1695 default:
1696 return command_usage(&open_cmd);
1697 }
1698 }
1699
1700 if (!readonly) {
1701 flags |= BDRV_O_RDWR;
1702 }
1703
1704 if (optind != argc - 1) {
1705 return command_usage(&open_cmd);
1706 }
1707
1708 return openfile(argv[optind], flags, growable);
1709 }
1710
1711 static int init_args_command(int index)
1712 {
1713 /* only one device allowed so far */
1714 if (index >= 1) {
1715 return 0;
1716 }
1717 return ++index;
1718 }
1719
1720 static int init_check_command(const cmdinfo_t *ct)
1721 {
1722 if (ct->flags & CMD_FLAG_GLOBAL) {
1723 return 1;
1724 }
1725 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1726 fprintf(stderr, "no file open, try 'help open'\n");
1727 return 0;
1728 }
1729 return 1;
1730 }
1731
1732 static void usage(const char *name)
1733 {
1734 printf(
1735 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1736 "QEMU Disk exerciser\n"
1737 "\n"
1738 " -c, --cmd command to execute\n"
1739 " -r, --read-only export read-only\n"
1740 " -s, --snapshot use snapshot file\n"
1741 " -n, --nocache disable host cache\n"
1742 " -g, --growable allow file to grow (only applies to protocols)\n"
1743 " -m, --misalign misalign allocations for O_DIRECT\n"
1744 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1745 " -h, --help display this help and exit\n"
1746 " -V, --version output version information and exit\n"
1747 "\n",
1748 name);
1749 }
1750
1751
1752 int main(int argc, char **argv)
1753 {
1754 int readonly = 0;
1755 int growable = 0;
1756 const char *sopt = "hVc:rsnmgk";
1757 const struct option lopt[] = {
1758 { "help", 0, NULL, 'h' },
1759 { "version", 0, NULL, 'V' },
1760 { "offset", 1, NULL, 'o' },
1761 { "cmd", 1, NULL, 'c' },
1762 { "read-only", 0, NULL, 'r' },
1763 { "snapshot", 0, NULL, 's' },
1764 { "nocache", 0, NULL, 'n' },
1765 { "misalign", 0, NULL, 'm' },
1766 { "growable", 0, NULL, 'g' },
1767 { "native-aio", 0, NULL, 'k' },
1768 { NULL, 0, NULL, 0 }
1769 };
1770 int c;
1771 int opt_index = 0;
1772 int flags = 0;
1773
1774 progname = basename(argv[0]);
1775
1776 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1777 switch (c) {
1778 case 's':
1779 flags |= BDRV_O_SNAPSHOT;
1780 break;
1781 case 'n':
1782 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1783 break;
1784 case 'c':
1785 add_user_command(optarg);
1786 break;
1787 case 'r':
1788 readonly = 1;
1789 break;
1790 case 'm':
1791 misalign = 1;
1792 break;
1793 case 'g':
1794 growable = 1;
1795 break;
1796 case 'k':
1797 flags |= BDRV_O_NATIVE_AIO;
1798 break;
1799 case 'V':
1800 printf("%s version %s\n", progname, VERSION);
1801 exit(0);
1802 case 'h':
1803 usage(progname);
1804 exit(0);
1805 default:
1806 usage(progname);
1807 exit(1);
1808 }
1809 }
1810
1811 if ((argc - optind) > 1) {
1812 usage(progname);
1813 exit(1);
1814 }
1815
1816 bdrv_init();
1817
1818 /* initialize commands */
1819 quit_init();
1820 help_init();
1821 add_command(&open_cmd);
1822 add_command(&close_cmd);
1823 add_command(&read_cmd);
1824 add_command(&readv_cmd);
1825 add_command(&write_cmd);
1826 add_command(&writev_cmd);
1827 add_command(&multiwrite_cmd);
1828 add_command(&aio_read_cmd);
1829 add_command(&aio_write_cmd);
1830 add_command(&aio_flush_cmd);
1831 add_command(&flush_cmd);
1832 add_command(&truncate_cmd);
1833 add_command(&length_cmd);
1834 add_command(&info_cmd);
1835 add_command(&discard_cmd);
1836 add_command(&alloc_cmd);
1837 add_command(&map_cmd);
1838
1839 add_args_command(init_args_command);
1840 add_check_command(init_check_command);
1841
1842 /* open the device */
1843 if (!readonly) {
1844 flags |= BDRV_O_RDWR;
1845 }
1846
1847 if ((argc - optind) == 1) {
1848 openfile(argv[optind], flags, growable);
1849 }
1850 command_loop();
1851
1852 /*
1853 * Make sure all outstanding requests get flushed the program exits.
1854 */
1855 qemu_aio_flush();
1856
1857 if (bs) {
1858 bdrv_delete(bs);
1859 }
1860 return 0;
1861 }