]> git.proxmox.com Git - qemu.git/blob - qemu-io.c
qemu-io: Fix handling of bdrv_is_allocated() return value (Kevin Wolf)
[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/types.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <getopt.h>
14
15 #include "qemu-common.h"
16 #include "block_int.h"
17 #include "cmd.h"
18
19 #define VERSION "0.0.1"
20
21 #define CMD_NOFILE_OK 0x01
22
23 char *progname;
24 static BlockDriverState *bs;
25
26 static int misalign;
27
28 /*
29 * Memory allocation helpers.
30 *
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
33 */
34
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len, int pattern)
37 {
38 void *buf;
39
40 if (misalign)
41 len += MISALIGN_OFFSET;
42 buf = qemu_memalign(512, len);
43 memset(buf, pattern, len);
44 if (misalign)
45 buf += MISALIGN_OFFSET;
46 return buf;
47 }
48
49 static void qemu_io_free(void *p)
50 {
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
54 }
55
56 static void
57 dump_buffer(char *buffer, int64_t offset, int len)
58 {
59 int i, j;
60 char *p;
61
62 for (i = 0, p = buffer; i < len; i += 16) {
63 char *s = p;
64
65 printf("%08llx: ", (unsigned long long)offset + i);
66 for (j = 0; j < 16 && i + j < len; j++, p++)
67 printf("%02x ", *p);
68 printf(" ");
69 for (j = 0; j < 16 && i + j < len; j++, s++) {
70 if (isalnum((int)*s))
71 printf("%c", *s);
72 else
73 printf(".");
74 }
75 printf("\n");
76 }
77 }
78
79 static void
80 print_report(const char *op, struct timeval *t, int64_t offset,
81 int count, int total, int cnt, int Cflag)
82 {
83 char s1[64], s2[64], ts[64];
84
85 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
86 if (!Cflag) {
87 cvtstr((double)total, s1, sizeof(s1));
88 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op, total, count, (long long)offset);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1, cnt, ts, s2, tdiv((double)cnt, *t));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
95 total, cnt, ts,
96 tdiv((double)total, *t),
97 tdiv((double)cnt, *t));
98 }
99 }
100
101 static int do_read(char *buf, int64_t offset, int count, int *total)
102 {
103 int ret;
104
105 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
106 if (ret < 0)
107 return ret;
108 *total = count;
109 return 1;
110 }
111
112 static int do_write(char *buf, int64_t offset, int count, int *total)
113 {
114 int ret;
115
116 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
117 if (ret < 0)
118 return ret;
119 *total = count;
120 return 1;
121 }
122
123 static int do_pread(char *buf, int64_t offset, int count, int *total)
124 {
125 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
126 if (*total < 0)
127 return *total;
128 return 1;
129 }
130
131 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
132 {
133 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
134 if (*total < 0)
135 return *total;
136 return 1;
137 }
138
139 #define NOT_DONE 0x7fffffff
140 static void aio_rw_done(void *opaque, int ret)
141 {
142 *(int *)opaque = ret;
143 }
144
145 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
146 {
147 BlockDriverAIOCB *acb;
148 int async_ret = NOT_DONE;
149
150 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
151 aio_rw_done, &async_ret);
152 if (!acb)
153 return -EIO;
154
155 while (async_ret == NOT_DONE)
156 qemu_aio_wait();
157
158 *total = qiov->size;
159 return async_ret < 0 ? async_ret : 1;
160 }
161
162 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
163 {
164 BlockDriverAIOCB *acb;
165 int async_ret = NOT_DONE;
166
167 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
168 aio_rw_done, &async_ret);
169 if (!acb)
170 return -EIO;
171
172 while (async_ret == NOT_DONE)
173 qemu_aio_wait();
174
175 *total = qiov->size;
176 return async_ret < 0 ? async_ret : 1;
177 }
178
179
180 static const cmdinfo_t read_cmd;
181
182 static void
183 read_help(void)
184 {
185 printf(
186 "\n"
187 " reads a range of bytes from the given offset\n"
188 "\n"
189 " Example:\n"
190 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
191 "\n"
192 " Reads a segment of the currently open file, optionally dumping it to the\n"
193 " standard output stream (with -v option) for subsequent inspection.\n"
194 " -p, -- use bdrv_pread to read the file\n"
195 " -C, -- report statistics in a machine parsable format\n"
196 " -v, -- dump buffer to standard output\n"
197 " -q, -- quite mode, do not show I/O statistics\n"
198 "\n");
199 }
200
201 static int
202 read_f(int argc, char **argv)
203 {
204 struct timeval t1, t2;
205 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
206 int c, cnt;
207 char *buf;
208 int64_t offset;
209 int count, total;
210
211 while ((c = getopt(argc, argv, "Cpqv")) != EOF) {
212 switch (c) {
213 case 'C':
214 Cflag = 1;
215 break;
216 case 'p':
217 pflag = 1;
218 break;
219 case 'q':
220 qflag = 1;
221 break;
222 case 'v':
223 vflag = 1;
224 break;
225 default:
226 return command_usage(&read_cmd);
227 }
228 }
229
230 if (optind != argc - 2)
231 return command_usage(&read_cmd);
232
233 offset = cvtnum(argv[optind]);
234 if (offset < 0) {
235 printf("non-numeric length argument -- %s\n", argv[optind]);
236 return 0;
237 }
238
239 optind++;
240 count = cvtnum(argv[optind]);
241 if (count < 0) {
242 printf("non-numeric length argument -- %s\n", argv[optind]);
243 return 0;
244 }
245
246 if (!pflag)
247 if (offset & 0x1ff) {
248 printf("offset %lld is not sector aligned\n",
249 (long long)offset);
250 return 0;
251
252 if (count & 0x1ff) {
253 printf("count %d is not sector aligned\n",
254 count);
255 return 0;
256 }
257 }
258
259 buf = qemu_io_alloc(count, 0xab);
260
261 gettimeofday(&t1, NULL);
262 if (pflag)
263 cnt = do_pread(buf, offset, count, &total);
264 else
265 cnt = do_read(buf, offset, count, &total);
266 gettimeofday(&t2, NULL);
267
268 if (cnt < 0) {
269 printf("read failed: %s\n", strerror(-cnt));
270 return 0;
271 }
272
273 if (qflag)
274 return 0;
275
276 if (vflag)
277 dump_buffer(buf, offset, count);
278
279 /* Finally, report back -- -C gives a parsable format */
280 t2 = tsub(t2, t1);
281 print_report("read", &t2, offset, count, total, cnt, Cflag);
282
283 qemu_io_free(buf);
284
285 return 0;
286 }
287
288 static const cmdinfo_t read_cmd = {
289 .name = "read",
290 .altname = "r",
291 .cfunc = read_f,
292 .argmin = 2,
293 .argmax = -1,
294 .args = "[-aCpqv] off len",
295 .oneline = "reads a number of bytes at a specified offset",
296 .help = read_help,
297 };
298
299 static const cmdinfo_t readv_cmd;
300
301 static void
302 readv_help(void)
303 {
304 printf(
305 "\n"
306 " reads a range of bytes from the given offset into multiple buffers\n"
307 "\n"
308 " Example:\n"
309 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
310 "\n"
311 " Reads a segment of the currently open file, optionally dumping it to the\n"
312 " standard output stream (with -v option) for subsequent inspection.\n"
313 " Uses multiple iovec buffers if more than one byte range is specified.\n"
314 " -C, -- report statistics in a machine parsable format\n"
315 " -v, -- dump buffer to standard output\n"
316 " -q, -- quite mode, do not show I/O statistics\n"
317 "\n");
318 }
319
320 static int
321 readv_f(int argc, char **argv)
322 {
323 struct timeval t1, t2;
324 int Cflag = 0, qflag = 0, vflag = 0;
325 int c, cnt;
326 char *buf, *p;
327 int64_t offset;
328 int count = 0, total;
329 int nr_iov, i;
330 QEMUIOVector qiov;
331
332 while ((c = getopt(argc, argv, "Cqv")) != EOF) {
333 switch (c) {
334 case 'C':
335 Cflag = 1;
336 break;
337 case 'q':
338 qflag = 1;
339 break;
340 case 'v':
341 vflag = 1;
342 break;
343 default:
344 return command_usage(&readv_cmd);
345 }
346 }
347
348 if (optind > argc - 2)
349 return command_usage(&readv_cmd);
350
351
352 offset = cvtnum(argv[optind]);
353 if (offset < 0) {
354 printf("non-numeric length argument -- %s\n", argv[optind]);
355 return 0;
356 }
357 optind++;
358
359 if (offset & 0x1ff) {
360 printf("offset %lld is not sector aligned\n",
361 (long long)offset);
362 return 0;
363 }
364
365 if (count & 0x1ff) {
366 printf("count %d is not sector aligned\n",
367 count);
368 return 0;
369 }
370
371 for (i = optind; i < argc; i++) {
372 size_t len;
373
374 len = cvtnum(argv[i]);
375 if (len < 0) {
376 printf("non-numeric length argument -- %s\n", argv[i]);
377 return 0;
378 }
379 count += len;
380 }
381
382 nr_iov = argc - optind;
383 qemu_iovec_init(&qiov, nr_iov);
384 buf = p = qemu_io_alloc(count, 0xab);
385 for (i = 0; i < nr_iov; i++) {
386 size_t len;
387
388 len = cvtnum(argv[optind]);
389 if (len < 0) {
390 printf("non-numeric length argument -- %s\n",
391 argv[optind]);
392 return 0;
393 }
394
395 qemu_iovec_add(&qiov, p, len);
396 p += len;
397 optind++;
398 }
399
400 gettimeofday(&t1, NULL);
401 cnt = do_aio_readv(&qiov, offset, &total);
402 gettimeofday(&t2, NULL);
403
404 if (cnt < 0) {
405 printf("readv failed: %s\n", strerror(-cnt));
406 return 0;
407 }
408
409 if (qflag)
410 return 0;
411
412 if (vflag)
413 dump_buffer(buf, offset, qiov.size);
414
415 /* Finally, report back -- -C gives a parsable format */
416 t2 = tsub(t2, t1);
417 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
418
419 qemu_io_free(buf);
420
421 return 0;
422 }
423
424 static const cmdinfo_t readv_cmd = {
425 .name = "readv",
426 .cfunc = readv_f,
427 .argmin = 2,
428 .argmax = -1,
429 .args = "[-Cqv] off len [len..]",
430 .oneline = "reads a number of bytes at a specified offset",
431 .help = readv_help,
432 };
433
434 static const cmdinfo_t write_cmd;
435
436 static void
437 write_help(void)
438 {
439 printf(
440 "\n"
441 " writes a range of bytes from the given offset\n"
442 "\n"
443 " Example:\n"
444 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
445 "\n"
446 " Writes into a segment of the currently open file, using a buffer\n"
447 " filled with a set pattern (0xcdcdcdcd).\n"
448 " -p, -- use bdrv_pwrite to write the file\n"
449 " -P, -- use different pattern to fill file\n"
450 " -C, -- report statistics in a machine parsable format\n"
451 " -q, -- quite mode, do not show I/O statistics\n"
452 "\n");
453 }
454
455 static int
456 write_f(int argc, char **argv)
457 {
458 struct timeval t1, t2;
459 int Cflag = 0, pflag = 0, qflag = 0;
460 int c, cnt;
461 char *buf;
462 int64_t offset;
463 int count, total;
464 int pattern = 0xcd;
465
466 while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
467 switch (c) {
468 case 'C':
469 Cflag = 1;
470 break;
471 case 'p':
472 pflag = 1;
473 break;
474 case 'P':
475 pattern = atoi(optarg);
476 break;
477 case 'q':
478 qflag = 1;
479 break;
480 default:
481 return command_usage(&write_cmd);
482 }
483 }
484
485 if (optind != argc - 2)
486 return command_usage(&write_cmd);
487
488 offset = cvtnum(argv[optind]);
489 if (offset < 0) {
490 printf("non-numeric length argument -- %s\n", argv[optind]);
491 return 0;
492 }
493
494 optind++;
495 count = cvtnum(argv[optind]);
496 if (count < 0) {
497 printf("non-numeric length argument -- %s\n", argv[optind]);
498 return 0;
499 }
500
501 if (!pflag) {
502 if (offset & 0x1ff) {
503 printf("offset %lld is not sector aligned\n",
504 (long long)offset);
505 return 0;
506 }
507
508 if (count & 0x1ff) {
509 printf("count %d is not sector aligned\n",
510 count);
511 return 0;
512 }
513 }
514
515 buf = qemu_io_alloc(count, pattern);
516
517 gettimeofday(&t1, NULL);
518 if (pflag)
519 cnt = do_pwrite(buf, offset, count, &total);
520 else
521 cnt = do_write(buf, offset, count, &total);
522 gettimeofday(&t2, NULL);
523
524 if (cnt < 0) {
525 printf("write failed: %s\n", strerror(-cnt));
526 return 0;
527 }
528
529 if (qflag)
530 return 0;
531
532 /* Finally, report back -- -C gives a parsable format */
533 t2 = tsub(t2, t1);
534 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
535
536 qemu_io_free(buf);
537
538 return 0;
539 }
540
541 static const cmdinfo_t write_cmd = {
542 .name = "write",
543 .altname = "w",
544 .cfunc = write_f,
545 .argmin = 2,
546 .argmax = -1,
547 .args = "[-aCpq] [-P pattern ] off len",
548 .oneline = "writes a number of bytes at a specified offset",
549 .help = write_help,
550 };
551
552 static const cmdinfo_t writev_cmd;
553
554 static void
555 writev_help(void)
556 {
557 printf(
558 "\n"
559 " writes a range of bytes from the given offset source from multiple buffers\n"
560 "\n"
561 " Example:\n"
562 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
563 "\n"
564 " Writes into a segment of the currently open file, using a buffer\n"
565 " filled with a set pattern (0xcdcdcdcd).\n"
566 " -P, -- use different pattern to fill file\n"
567 " -C, -- report statistics in a machine parsable format\n"
568 " -q, -- quite mode, do not show I/O statistics\n"
569 "\n");
570 }
571
572 static int
573 writev_f(int argc, char **argv)
574 {
575 struct timeval t1, t2;
576 int Cflag = 0, qflag = 0;
577 int c, cnt;
578 char *buf, *p;
579 int64_t offset;
580 int count = 0, total;
581 int nr_iov, i;
582 int pattern = 0xcd;
583 QEMUIOVector qiov;
584
585 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
586 switch (c) {
587 case 'C':
588 Cflag = 1;
589 break;
590 case 'q':
591 qflag = 1;
592 break;
593 case 'P':
594 pattern = atoi(optarg);
595 break;
596 default:
597 return command_usage(&writev_cmd);
598 }
599 }
600
601 if (optind > argc - 2)
602 return command_usage(&writev_cmd);
603
604 offset = cvtnum(argv[optind]);
605 if (offset < 0) {
606 printf("non-numeric length argument -- %s\n", argv[optind]);
607 return 0;
608 }
609 optind++;
610
611 if (offset & 0x1ff) {
612 printf("offset %lld is not sector aligned\n",
613 (long long)offset);
614 return 0;
615 }
616
617 if (count & 0x1ff) {
618 printf("count %d is not sector aligned\n",
619 count);
620 return 0;
621 }
622
623
624 for (i = optind; i < argc; i++) {
625 size_t len;
626
627 len = cvtnum(argv[optind]);
628 if (len < 0) {
629 printf("non-numeric length argument -- %s\n", argv[i]);
630 return 0;
631 }
632 count += len;
633 }
634
635 nr_iov = argc - optind;
636 qemu_iovec_init(&qiov, nr_iov);
637 buf = p = qemu_io_alloc(count, pattern);
638 for (i = 0; i < nr_iov; i++) {
639 size_t len;
640
641 len = cvtnum(argv[optind]);
642 if (len < 0) {
643 printf("non-numeric length argument -- %s\n",
644 argv[optind]);
645 return 0;
646 }
647
648 qemu_iovec_add(&qiov, p, len);
649 p += len;
650 optind++;
651 }
652
653 gettimeofday(&t1, NULL);
654 cnt = do_aio_writev(&qiov, offset, &total);
655 gettimeofday(&t2, NULL);
656
657 if (cnt < 0) {
658 printf("writev failed: %s\n", strerror(-cnt));
659 return 0;
660 }
661
662 if (qflag)
663 return 0;
664
665 /* Finally, report back -- -C gives a parsable format */
666 t2 = tsub(t2, t1);
667 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
668
669 qemu_io_free(buf);
670
671 return 0;
672 }
673
674 static const cmdinfo_t writev_cmd = {
675 .name = "writev",
676 .cfunc = writev_f,
677 .argmin = 2,
678 .argmax = -1,
679 .args = "[-Cq] [-P pattern ] off len [len..]",
680 .oneline = "writes a number of bytes at a specified offset",
681 .help = writev_help,
682 };
683
684 static int
685 flush_f(int argc, char **argv)
686 {
687 bdrv_flush(bs);
688 return 0;
689 }
690
691 static const cmdinfo_t flush_cmd = {
692 .name = "flush",
693 .altname = "f",
694 .cfunc = flush_f,
695 .oneline = "flush all in-core file state to disk",
696 };
697
698 static int
699 truncate_f(int argc, char **argv)
700 {
701 int64_t offset;
702 int ret;
703
704 offset = cvtnum(argv[1]);
705 if (offset < 0) {
706 printf("non-numeric truncate argument -- %s\n", argv[1]);
707 return 0;
708 }
709
710 ret = bdrv_truncate(bs, offset);
711 if (ret < 0) {
712 printf("truncate: %s", strerror(ret));
713 return 0;
714 }
715
716 return 0;
717 }
718
719 static const cmdinfo_t truncate_cmd = {
720 .name = "truncate",
721 .altname = "t",
722 .cfunc = truncate_f,
723 .argmin = 1,
724 .argmax = 1,
725 .args = "off",
726 .oneline = "truncates the current file at the given offset",
727 };
728
729 static int
730 length_f(int argc, char **argv)
731 {
732 int64_t size;
733 char s1[64];
734
735 size = bdrv_getlength(bs);
736 if (size < 0) {
737 printf("getlength: %s", strerror(size));
738 return 0;
739 }
740
741 cvtstr(size, s1, sizeof(s1));
742 printf("%s\n", s1);
743 return 0;
744 }
745
746
747 static const cmdinfo_t length_cmd = {
748 .name = "length",
749 .altname = "l",
750 .cfunc = length_f,
751 .oneline = "gets the length of the current file",
752 };
753
754
755 static int
756 info_f(int argc, char **argv)
757 {
758 BlockDriverInfo bdi;
759 char s1[64], s2[64];
760 int ret;
761
762 if (bs->drv && bs->drv->format_name)
763 printf("format name: %s\n", bs->drv->format_name);
764 if (bs->drv && bs->drv->protocol_name)
765 printf("format name: %s\n", bs->drv->protocol_name);
766
767 ret = bdrv_get_info(bs, &bdi);
768 if (ret)
769 return 0;
770
771 cvtstr(bdi.cluster_size, s1, sizeof(s1));
772 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
773
774 printf("cluster size: %s\n", s1);
775 printf("vm state offset: %s\n", s2);
776
777 return 0;
778 }
779
780
781
782 static const cmdinfo_t info_cmd = {
783 .name = "info",
784 .altname = "i",
785 .cfunc = info_f,
786 .oneline = "prints information about the current file",
787 };
788
789 static int
790 alloc_f(int argc, char **argv)
791 {
792 int64_t offset;
793 int nb_sectors;
794 char s1[64];
795 int num;
796 int ret;
797 const char *retstr;
798
799 offset = cvtnum(argv[1]);
800 if (offset & 0x1ff) {
801 printf("offset %lld is not sector aligned\n",
802 (long long)offset);
803 return 0;
804 }
805
806 if (argc == 3)
807 nb_sectors = cvtnum(argv[2]);
808 else
809 nb_sectors = 1;
810
811 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
812
813 cvtstr(offset, s1, sizeof(s1));
814
815 retstr = ret ? "allocated" : "not allocated";
816 if (nb_sectors == 1)
817 printf("sector %s at offset %s\n", retstr, s1);
818 else
819 printf("%d/%d sectors %s at offset %s\n",
820 num, nb_sectors, retstr, s1);
821 return 0;
822 }
823
824 static const cmdinfo_t alloc_cmd = {
825 .name = "alloc",
826 .altname = "a",
827 .argmin = 1,
828 .argmax = 2,
829 .cfunc = alloc_f,
830 .args = "off [sectors]",
831 .oneline = "checks if a sector is present in the file",
832 };
833
834 static int
835 close_f(int argc, char **argv)
836 {
837 bdrv_close(bs);
838 bs = NULL;
839 return 0;
840 }
841
842 static const cmdinfo_t close_cmd = {
843 .name = "close",
844 .altname = "c",
845 .cfunc = close_f,
846 .oneline = "close the current open file",
847 };
848
849 static int openfile(char *name, int flags)
850 {
851 if (bs) {
852 fprintf(stderr, "file open already, try 'help close'\n");
853 return 1;
854 }
855
856 bs = bdrv_new("hda");
857 if (!bs)
858 return 1;
859
860 if (bdrv_open(bs, name, flags) == -1) {
861 fprintf(stderr, "%s: can't open device %s\n", progname, name);
862 bs = NULL;
863 return 1;
864 }
865
866 return 0;
867 }
868
869 static void
870 open_help(void)
871 {
872 printf(
873 "\n"
874 " opens a new file in the requested mode\n"
875 "\n"
876 " Example:\n"
877 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
878 "\n"
879 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
880 " -C, -- create new file if it doesn't exist\n"
881 " -r, -- open file read-only\n"
882 " -s, -- use snapshot file\n"
883 " -n, -- disable host cache\n"
884 "\n");
885 }
886
887 static const cmdinfo_t open_cmd;
888
889 static int
890 open_f(int argc, char **argv)
891 {
892 int flags = 0;
893 int readonly = 0;
894 int c;
895
896 while ((c = getopt(argc, argv, "snCr")) != EOF) {
897 switch (c) {
898 case 's':
899 flags |= BDRV_O_SNAPSHOT;
900 break;
901 case 'n':
902 flags |= BDRV_O_NOCACHE;
903 break;
904 case 'C':
905 flags |= BDRV_O_CREAT;
906 break;
907 case 'r':
908 readonly = 1;
909 break;
910 default:
911 return command_usage(&open_cmd);
912 }
913 }
914
915 if (readonly)
916 flags |= BDRV_O_RDONLY;
917 else
918 flags |= BDRV_O_RDWR;
919
920 if (optind != argc - 1)
921 return command_usage(&open_cmd);
922
923 return openfile(argv[optind], flags);
924 }
925
926 static const cmdinfo_t open_cmd = {
927 .name = "open",
928 .altname = "o",
929 .cfunc = open_f,
930 .argmin = 1,
931 .argmax = -1,
932 .flags = CMD_NOFILE_OK,
933 .args = "[-Crsn] [path]",
934 .oneline = "open the file specified by path",
935 .help = open_help,
936 };
937
938 static int
939 init_args_command(
940 int index)
941 {
942 /* only one device allowed so far */
943 if (index >= 1)
944 return 0;
945 return ++index;
946 }
947
948 static int
949 init_check_command(
950 const cmdinfo_t *ct)
951 {
952 if (ct->flags & CMD_FLAG_GLOBAL)
953 return 1;
954 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
955 fprintf(stderr, "no file open, try 'help open'\n");
956 return 0;
957 }
958 return 1;
959 }
960
961 static void usage(const char *name)
962 {
963 printf(
964 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
965 "QEMU Disk excerciser\n"
966 "\n"
967 " -C, --create create new file if it doesn't exist\n"
968 " -c, --cmd command to execute\n"
969 " -r, --read-only export read-only\n"
970 " -s, --snapshot use snapshot file\n"
971 " -n, --nocache disable host cache\n"
972 " -m, --misalign misalign allocations for O_DIRECT\n"
973 " -h, --help display this help and exit\n"
974 " -V, --version output version information and exit\n"
975 "\n",
976 name);
977 }
978
979
980 int main(int argc, char **argv)
981 {
982 int readonly = 0;
983 const char *sopt = "hVc:Crsnm";
984 struct option lopt[] = {
985 { "help", 0, 0, 'h' },
986 { "version", 0, 0, 'V' },
987 { "offset", 1, 0, 'o' },
988 { "cmd", 1, 0, 'c' },
989 { "create", 0, 0, 'C' },
990 { "read-only", 0, 0, 'r' },
991 { "snapshot", 0, 0, 's' },
992 { "nocache", 0, 0, 'n' },
993 { "misalign", 0, 0, 'm' },
994 { NULL, 0, 0, 0 }
995 };
996 int c;
997 int opt_index = 0;
998 int flags = 0;
999
1000 progname = basename(argv[0]);
1001
1002 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1003 switch (c) {
1004 case 's':
1005 flags |= BDRV_O_SNAPSHOT;
1006 break;
1007 case 'n':
1008 flags |= BDRV_O_NOCACHE;
1009 break;
1010 case 'c':
1011 add_user_command(optarg);
1012 break;
1013 case 'C':
1014 flags |= BDRV_O_CREAT;
1015 break;
1016 case 'r':
1017 readonly = 1;
1018 break;
1019 case 'm':
1020 misalign = 1;
1021 break;
1022 case 'V':
1023 printf("%s version %s\n", progname, VERSION);
1024 exit(0);
1025 case 'h':
1026 usage(progname);
1027 exit(0);
1028 default:
1029 usage(progname);
1030 exit(1);
1031 }
1032 }
1033
1034 if ((argc - optind) > 1) {
1035 usage(progname);
1036 exit(1);
1037 }
1038
1039 bdrv_init();
1040
1041 /* initialize commands */
1042 quit_init();
1043 help_init();
1044 add_command(&open_cmd);
1045 add_command(&close_cmd);
1046 add_command(&read_cmd);
1047 add_command(&readv_cmd);
1048 add_command(&write_cmd);
1049 add_command(&writev_cmd);
1050 add_command(&flush_cmd);
1051 add_command(&truncate_cmd);
1052 add_command(&length_cmd);
1053 add_command(&info_cmd);
1054 add_command(&alloc_cmd);
1055
1056 add_args_command(init_args_command);
1057 add_check_command(init_check_command);
1058
1059 /* open the device */
1060 if (readonly)
1061 flags |= BDRV_O_RDONLY;
1062 else
1063 flags |= BDRV_O_RDWR;
1064
1065 if ((argc - optind) == 1)
1066 openfile(argv[optind], flags);
1067 command_loop();
1068
1069 if (bs)
1070 bdrv_close(bs);
1071 return 0;
1072 }