Enable power button even generation.
[qemu/hppa.git] / qemu-io.c
blob60464e6aba92f8c2b454b26e41a0ee0db3099f8e
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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>
15 #include "qemu-common.h"
16 #include "block_int.h"
17 #include "cmd.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
23 char *progname;
24 static BlockDriverState *bs;
26 static int misalign;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len, int pattern)
38 void *buf;
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;
49 static void qemu_io_free(void *p)
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
56 static void
57 dump_buffer(char *buffer, int64_t offset, int len)
59 int i, j;
60 char *p;
62 for (i = 0, p = buffer; i < len; i += 16) {
63 char *s = p;
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(".");
75 printf("\n");
79 static void
80 print_report(const char *op, struct timeval *t, int64_t offset,
81 int count, int total, int cnt, int Cflag)
83 char s1[64], s2[64], ts[64];
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));
101 static int do_read(char *buf, int64_t offset, int count, int *total)
103 int ret;
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;
112 static int do_write(char *buf, int64_t offset, int count, int *total)
114 int ret;
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;
123 static int do_pread(char *buf, int64_t offset, int count, int *total)
125 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
126 if (*total < 0)
127 return *total;
128 return 1;
131 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
133 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
134 if (*total < 0)
135 return *total;
136 return 1;
139 #define NOT_DONE 0x7fffffff
140 static void aio_rw_done(void *opaque, int ret)
142 *(int *)opaque = ret;
145 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
147 BlockDriverAIOCB *acb;
148 int async_ret = NOT_DONE;
150 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
151 aio_rw_done, &async_ret);
152 if (!acb)
153 return -EIO;
155 while (async_ret == NOT_DONE)
156 qemu_aio_wait();
158 *total = qiov->size;
159 return async_ret < 0 ? async_ret : 1;
162 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
164 BlockDriverAIOCB *acb;
165 int async_ret = NOT_DONE;
167 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
168 aio_rw_done, &async_ret);
169 if (!acb)
170 return -EIO;
172 while (async_ret == NOT_DONE)
173 qemu_aio_wait();
175 *total = qiov->size;
176 return async_ret < 0 ? async_ret : 1;
180 static const cmdinfo_t read_cmd;
182 static void
183 read_help(void)
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 " -C, -- report statistics in a machine parsable format\n"
195 " -l, -- length for pattern verification (only with -P)\n"
196 " -p, -- use bdrv_pread to read the file\n"
197 " -P, -- use a pattern to verify read data\n"
198 " -q, -- quite mode, do not show I/O statistics\n"
199 " -s, -- start offset for pattern verification (only with -P)\n"
200 " -v, -- dump buffer to standard output\n"
201 "\n");
204 static int
205 read_f(int argc, char **argv)
207 struct timeval t1, t2;
208 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
209 int Pflag = 0, sflag = 0, lflag = 0;
210 int c, cnt;
211 char *buf;
212 int64_t offset;
213 int count, total;
214 int pattern = 0, pattern_offset = 0, pattern_count = 0;
216 while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
217 switch (c) {
218 case 'C':
219 Cflag = 1;
220 break;
221 case 'l':
222 lflag = 1;
223 pattern_count = cvtnum(optarg);
224 if (pattern_count < 0) {
225 printf("non-numeric length argument -- %s\n", optarg);
226 return 0;
228 break;
229 case 'p':
230 pflag = 1;
231 break;
232 case 'P':
233 Pflag = 1;
234 pattern = atoi(optarg);
235 break;
236 case 'q':
237 qflag = 1;
238 break;
239 case 's':
240 sflag = 1;
241 pattern_offset = cvtnum(optarg);
242 if (pattern_offset < 0) {
243 printf("non-numeric length argument -- %s\n", optarg);
244 return 0;
246 break;
247 case 'v':
248 vflag = 1;
249 break;
250 default:
251 return command_usage(&read_cmd);
255 if (optind != argc - 2)
256 return command_usage(&read_cmd);
258 offset = cvtnum(argv[optind]);
259 if (offset < 0) {
260 printf("non-numeric length argument -- %s\n", argv[optind]);
261 return 0;
264 optind++;
265 count = cvtnum(argv[optind]);
266 if (count < 0) {
267 printf("non-numeric length argument -- %s\n", argv[optind]);
268 return 0;
271 if (!Pflag && (lflag || sflag)) {
272 return command_usage(&read_cmd);
275 if (!lflag) {
276 pattern_count = count - pattern_offset;
279 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
280 printf("pattern verfication range exceeds end of read data\n");
281 return 0;
284 if (!pflag)
285 if (offset & 0x1ff) {
286 printf("offset %lld is not sector aligned\n",
287 (long long)offset);
288 return 0;
290 if (count & 0x1ff) {
291 printf("count %d is not sector aligned\n",
292 count);
293 return 0;
297 buf = qemu_io_alloc(count, 0xab);
299 gettimeofday(&t1, NULL);
300 if (pflag)
301 cnt = do_pread(buf, offset, count, &total);
302 else
303 cnt = do_read(buf, offset, count, &total);
304 gettimeofday(&t2, NULL);
306 if (cnt < 0) {
307 printf("read failed: %s\n", strerror(-cnt));
308 return 0;
311 if (Pflag) {
312 void* cmp_buf = malloc(pattern_count);
313 memset(cmp_buf, pattern, pattern_count);
314 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
315 printf("Pattern verification failed at offset %lld, "
316 "%d bytes\n",
317 (long long) offset + pattern_offset, pattern_count);
319 free(cmp_buf);
322 if (qflag)
323 return 0;
325 if (vflag)
326 dump_buffer(buf, offset, count);
328 /* Finally, report back -- -C gives a parsable format */
329 t2 = tsub(t2, t1);
330 print_report("read", &t2, offset, count, total, cnt, Cflag);
332 qemu_io_free(buf);
334 return 0;
337 static const cmdinfo_t read_cmd = {
338 .name = "read",
339 .altname = "r",
340 .cfunc = read_f,
341 .argmin = 2,
342 .argmax = -1,
343 .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
344 .oneline = "reads a number of bytes at a specified offset",
345 .help = read_help,
348 static const cmdinfo_t readv_cmd;
350 static void
351 readv_help(void)
353 printf(
354 "\n"
355 " reads a range of bytes from the given offset into multiple buffers\n"
356 "\n"
357 " Example:\n"
358 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
359 "\n"
360 " Reads a segment of the currently open file, optionally dumping it to the\n"
361 " standard output stream (with -v option) for subsequent inspection.\n"
362 " Uses multiple iovec buffers if more than one byte range is specified.\n"
363 " -C, -- report statistics in a machine parsable format\n"
364 " -P, -- use a pattern to verify read data\n"
365 " -v, -- dump buffer to standard output\n"
366 " -q, -- quite mode, do not show I/O statistics\n"
367 "\n");
370 static int
371 readv_f(int argc, char **argv)
373 struct timeval t1, t2;
374 int Cflag = 0, qflag = 0, vflag = 0;
375 int c, cnt;
376 char *buf, *p;
377 int64_t offset;
378 int count = 0, total;
379 int nr_iov, i;
380 QEMUIOVector qiov;
381 int pattern = 0;
382 int Pflag = 0;
384 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
385 switch (c) {
386 case 'C':
387 Cflag = 1;
388 break;
389 case 'P':
390 Pflag = 1;
391 pattern = atoi(optarg);
392 break;
393 case 'q':
394 qflag = 1;
395 break;
396 case 'v':
397 vflag = 1;
398 break;
399 default:
400 return command_usage(&readv_cmd);
404 if (optind > argc - 2)
405 return command_usage(&readv_cmd);
408 offset = cvtnum(argv[optind]);
409 if (offset < 0) {
410 printf("non-numeric length argument -- %s\n", argv[optind]);
411 return 0;
413 optind++;
415 if (offset & 0x1ff) {
416 printf("offset %lld is not sector aligned\n",
417 (long long)offset);
418 return 0;
421 if (count & 0x1ff) {
422 printf("count %d is not sector aligned\n",
423 count);
424 return 0;
427 for (i = optind; i < argc; i++) {
428 size_t len;
430 len = cvtnum(argv[i]);
431 if (len < 0) {
432 printf("non-numeric length argument -- %s\n", argv[i]);
433 return 0;
435 count += len;
438 nr_iov = argc - optind;
439 qemu_iovec_init(&qiov, nr_iov);
440 buf = p = qemu_io_alloc(count, 0xab);
441 for (i = 0; i < nr_iov; i++) {
442 size_t len;
444 len = cvtnum(argv[optind]);
445 if (len < 0) {
446 printf("non-numeric length argument -- %s\n",
447 argv[optind]);
448 return 0;
451 qemu_iovec_add(&qiov, p, len);
452 p += len;
453 optind++;
456 gettimeofday(&t1, NULL);
457 cnt = do_aio_readv(&qiov, offset, &total);
458 gettimeofday(&t2, NULL);
460 if (cnt < 0) {
461 printf("readv failed: %s\n", strerror(-cnt));
462 return 0;
465 if (Pflag) {
466 void* cmp_buf = malloc(count);
467 memset(cmp_buf, pattern, count);
468 if (memcmp(buf, cmp_buf, count)) {
469 printf("Pattern verification failed at offset %lld, "
470 "%d bytes\n",
471 (long long) offset, count);
473 free(cmp_buf);
476 if (qflag)
477 return 0;
479 if (vflag)
480 dump_buffer(buf, offset, qiov.size);
482 /* Finally, report back -- -C gives a parsable format */
483 t2 = tsub(t2, t1);
484 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
486 qemu_io_free(buf);
488 return 0;
491 static const cmdinfo_t readv_cmd = {
492 .name = "readv",
493 .cfunc = readv_f,
494 .argmin = 2,
495 .argmax = -1,
496 .args = "[-Cqv] [-P pattern ] off len [len..]",
497 .oneline = "reads a number of bytes at a specified offset",
498 .help = readv_help,
501 static const cmdinfo_t write_cmd;
503 static void
504 write_help(void)
506 printf(
507 "\n"
508 " writes a range of bytes from the given offset\n"
509 "\n"
510 " Example:\n"
511 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
512 "\n"
513 " Writes into a segment of the currently open file, using a buffer\n"
514 " filled with a set pattern (0xcdcdcdcd).\n"
515 " -p, -- use bdrv_pwrite to write the file\n"
516 " -P, -- use different pattern to fill file\n"
517 " -C, -- report statistics in a machine parsable format\n"
518 " -q, -- quite mode, do not show I/O statistics\n"
519 "\n");
522 static int
523 write_f(int argc, char **argv)
525 struct timeval t1, t2;
526 int Cflag = 0, pflag = 0, qflag = 0;
527 int c, cnt;
528 char *buf;
529 int64_t offset;
530 int count, total;
531 int pattern = 0xcd;
533 while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
534 switch (c) {
535 case 'C':
536 Cflag = 1;
537 break;
538 case 'p':
539 pflag = 1;
540 break;
541 case 'P':
542 pattern = atoi(optarg);
543 break;
544 case 'q':
545 qflag = 1;
546 break;
547 default:
548 return command_usage(&write_cmd);
552 if (optind != argc - 2)
553 return command_usage(&write_cmd);
555 offset = cvtnum(argv[optind]);
556 if (offset < 0) {
557 printf("non-numeric length argument -- %s\n", argv[optind]);
558 return 0;
561 optind++;
562 count = cvtnum(argv[optind]);
563 if (count < 0) {
564 printf("non-numeric length argument -- %s\n", argv[optind]);
565 return 0;
568 if (!pflag) {
569 if (offset & 0x1ff) {
570 printf("offset %lld is not sector aligned\n",
571 (long long)offset);
572 return 0;
575 if (count & 0x1ff) {
576 printf("count %d is not sector aligned\n",
577 count);
578 return 0;
582 buf = qemu_io_alloc(count, pattern);
584 gettimeofday(&t1, NULL);
585 if (pflag)
586 cnt = do_pwrite(buf, offset, count, &total);
587 else
588 cnt = do_write(buf, offset, count, &total);
589 gettimeofday(&t2, NULL);
591 if (cnt < 0) {
592 printf("write failed: %s\n", strerror(-cnt));
593 return 0;
596 if (qflag)
597 return 0;
599 /* Finally, report back -- -C gives a parsable format */
600 t2 = tsub(t2, t1);
601 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
603 qemu_io_free(buf);
605 return 0;
608 static const cmdinfo_t write_cmd = {
609 .name = "write",
610 .altname = "w",
611 .cfunc = write_f,
612 .argmin = 2,
613 .argmax = -1,
614 .args = "[-aCpq] [-P pattern ] off len",
615 .oneline = "writes a number of bytes at a specified offset",
616 .help = write_help,
619 static const cmdinfo_t writev_cmd;
621 static void
622 writev_help(void)
624 printf(
625 "\n"
626 " writes a range of bytes from the given offset source from multiple buffers\n"
627 "\n"
628 " Example:\n"
629 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
630 "\n"
631 " Writes into a segment of the currently open file, using a buffer\n"
632 " filled with a set pattern (0xcdcdcdcd).\n"
633 " -P, -- use different pattern to fill file\n"
634 " -C, -- report statistics in a machine parsable format\n"
635 " -q, -- quite mode, do not show I/O statistics\n"
636 "\n");
639 static int
640 writev_f(int argc, char **argv)
642 struct timeval t1, t2;
643 int Cflag = 0, qflag = 0;
644 int c, cnt;
645 char *buf, *p;
646 int64_t offset;
647 int count = 0, total;
648 int nr_iov, i;
649 int pattern = 0xcd;
650 QEMUIOVector qiov;
652 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
653 switch (c) {
654 case 'C':
655 Cflag = 1;
656 break;
657 case 'q':
658 qflag = 1;
659 break;
660 case 'P':
661 pattern = atoi(optarg);
662 break;
663 default:
664 return command_usage(&writev_cmd);
668 if (optind > argc - 2)
669 return command_usage(&writev_cmd);
671 offset = cvtnum(argv[optind]);
672 if (offset < 0) {
673 printf("non-numeric length argument -- %s\n", argv[optind]);
674 return 0;
676 optind++;
678 if (offset & 0x1ff) {
679 printf("offset %lld is not sector aligned\n",
680 (long long)offset);
681 return 0;
684 if (count & 0x1ff) {
685 printf("count %d is not sector aligned\n",
686 count);
687 return 0;
691 for (i = optind; i < argc; i++) {
692 size_t len;
694 len = cvtnum(argv[optind]);
695 if (len < 0) {
696 printf("non-numeric length argument -- %s\n", argv[i]);
697 return 0;
699 count += len;
702 nr_iov = argc - optind;
703 qemu_iovec_init(&qiov, nr_iov);
704 buf = p = qemu_io_alloc(count, pattern);
705 for (i = 0; i < nr_iov; i++) {
706 size_t len;
708 len = cvtnum(argv[optind]);
709 if (len < 0) {
710 printf("non-numeric length argument -- %s\n",
711 argv[optind]);
712 return 0;
715 qemu_iovec_add(&qiov, p, len);
716 p += len;
717 optind++;
720 gettimeofday(&t1, NULL);
721 cnt = do_aio_writev(&qiov, offset, &total);
722 gettimeofday(&t2, NULL);
724 if (cnt < 0) {
725 printf("writev failed: %s\n", strerror(-cnt));
726 return 0;
729 if (qflag)
730 return 0;
732 /* Finally, report back -- -C gives a parsable format */
733 t2 = tsub(t2, t1);
734 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
736 qemu_io_free(buf);
738 return 0;
741 static const cmdinfo_t writev_cmd = {
742 .name = "writev",
743 .cfunc = writev_f,
744 .argmin = 2,
745 .argmax = -1,
746 .args = "[-Cq] [-P pattern ] off len [len..]",
747 .oneline = "writes a number of bytes at a specified offset",
748 .help = writev_help,
751 static int
752 flush_f(int argc, char **argv)
754 bdrv_flush(bs);
755 return 0;
758 static const cmdinfo_t flush_cmd = {
759 .name = "flush",
760 .altname = "f",
761 .cfunc = flush_f,
762 .oneline = "flush all in-core file state to disk",
765 static int
766 truncate_f(int argc, char **argv)
768 int64_t offset;
769 int ret;
771 offset = cvtnum(argv[1]);
772 if (offset < 0) {
773 printf("non-numeric truncate argument -- %s\n", argv[1]);
774 return 0;
777 ret = bdrv_truncate(bs, offset);
778 if (ret < 0) {
779 printf("truncate: %s", strerror(ret));
780 return 0;
783 return 0;
786 static const cmdinfo_t truncate_cmd = {
787 .name = "truncate",
788 .altname = "t",
789 .cfunc = truncate_f,
790 .argmin = 1,
791 .argmax = 1,
792 .args = "off",
793 .oneline = "truncates the current file at the given offset",
796 static int
797 length_f(int argc, char **argv)
799 int64_t size;
800 char s1[64];
802 size = bdrv_getlength(bs);
803 if (size < 0) {
804 printf("getlength: %s", strerror(size));
805 return 0;
808 cvtstr(size, s1, sizeof(s1));
809 printf("%s\n", s1);
810 return 0;
814 static const cmdinfo_t length_cmd = {
815 .name = "length",
816 .altname = "l",
817 .cfunc = length_f,
818 .oneline = "gets the length of the current file",
822 static int
823 info_f(int argc, char **argv)
825 BlockDriverInfo bdi;
826 char s1[64], s2[64];
827 int ret;
829 if (bs->drv && bs->drv->format_name)
830 printf("format name: %s\n", bs->drv->format_name);
831 if (bs->drv && bs->drv->protocol_name)
832 printf("format name: %s\n", bs->drv->protocol_name);
834 ret = bdrv_get_info(bs, &bdi);
835 if (ret)
836 return 0;
838 cvtstr(bdi.cluster_size, s1, sizeof(s1));
839 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
841 printf("cluster size: %s\n", s1);
842 printf("vm state offset: %s\n", s2);
844 return 0;
849 static const cmdinfo_t info_cmd = {
850 .name = "info",
851 .altname = "i",
852 .cfunc = info_f,
853 .oneline = "prints information about the current file",
856 static int
857 alloc_f(int argc, char **argv)
859 int64_t offset;
860 int nb_sectors;
861 char s1[64];
862 int num;
863 int ret;
864 const char *retstr;
866 offset = cvtnum(argv[1]);
867 if (offset & 0x1ff) {
868 printf("offset %lld is not sector aligned\n",
869 (long long)offset);
870 return 0;
873 if (argc == 3)
874 nb_sectors = cvtnum(argv[2]);
875 else
876 nb_sectors = 1;
878 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
880 cvtstr(offset, s1, sizeof(s1));
882 retstr = ret ? "allocated" : "not allocated";
883 if (nb_sectors == 1)
884 printf("sector %s at offset %s\n", retstr, s1);
885 else
886 printf("%d/%d sectors %s at offset %s\n",
887 num, nb_sectors, retstr, s1);
888 return 0;
891 static const cmdinfo_t alloc_cmd = {
892 .name = "alloc",
893 .altname = "a",
894 .argmin = 1,
895 .argmax = 2,
896 .cfunc = alloc_f,
897 .args = "off [sectors]",
898 .oneline = "checks if a sector is present in the file",
901 static int
902 close_f(int argc, char **argv)
904 bdrv_close(bs);
905 bs = NULL;
906 return 0;
909 static const cmdinfo_t close_cmd = {
910 .name = "close",
911 .altname = "c",
912 .cfunc = close_f,
913 .oneline = "close the current open file",
916 static int openfile(char *name, int flags)
918 if (bs) {
919 fprintf(stderr, "file open already, try 'help close'\n");
920 return 1;
923 bs = bdrv_new("hda");
924 if (!bs)
925 return 1;
927 if (bdrv_open(bs, name, flags) == -1) {
928 fprintf(stderr, "%s: can't open device %s\n", progname, name);
929 bs = NULL;
930 return 1;
933 return 0;
936 static void
937 open_help(void)
939 printf(
940 "\n"
941 " opens a new file in the requested mode\n"
942 "\n"
943 " Example:\n"
944 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
945 "\n"
946 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
947 " -C, -- create new file if it doesn't exist\n"
948 " -r, -- open file read-only\n"
949 " -s, -- use snapshot file\n"
950 " -n, -- disable host cache\n"
951 "\n");
954 static const cmdinfo_t open_cmd;
956 static int
957 open_f(int argc, char **argv)
959 int flags = 0;
960 int readonly = 0;
961 int c;
963 while ((c = getopt(argc, argv, "snCr")) != EOF) {
964 switch (c) {
965 case 's':
966 flags |= BDRV_O_SNAPSHOT;
967 break;
968 case 'n':
969 flags |= BDRV_O_NOCACHE;
970 break;
971 case 'C':
972 flags |= BDRV_O_CREAT;
973 break;
974 case 'r':
975 readonly = 1;
976 break;
977 default:
978 return command_usage(&open_cmd);
982 if (readonly)
983 flags |= BDRV_O_RDONLY;
984 else
985 flags |= BDRV_O_RDWR;
987 if (optind != argc - 1)
988 return command_usage(&open_cmd);
990 return openfile(argv[optind], flags);
993 static const cmdinfo_t open_cmd = {
994 .name = "open",
995 .altname = "o",
996 .cfunc = open_f,
997 .argmin = 1,
998 .argmax = -1,
999 .flags = CMD_NOFILE_OK,
1000 .args = "[-Crsn] [path]",
1001 .oneline = "open the file specified by path",
1002 .help = open_help,
1005 static int
1006 init_args_command(
1007 int index)
1009 /* only one device allowed so far */
1010 if (index >= 1)
1011 return 0;
1012 return ++index;
1015 static int
1016 init_check_command(
1017 const cmdinfo_t *ct)
1019 if (ct->flags & CMD_FLAG_GLOBAL)
1020 return 1;
1021 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1022 fprintf(stderr, "no file open, try 'help open'\n");
1023 return 0;
1025 return 1;
1028 static void usage(const char *name)
1030 printf(
1031 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1032 "QEMU Disk excerciser\n"
1033 "\n"
1034 " -C, --create create new file if it doesn't exist\n"
1035 " -c, --cmd command to execute\n"
1036 " -r, --read-only export read-only\n"
1037 " -s, --snapshot use snapshot file\n"
1038 " -n, --nocache disable host cache\n"
1039 " -m, --misalign misalign allocations for O_DIRECT\n"
1040 " -h, --help display this help and exit\n"
1041 " -V, --version output version information and exit\n"
1042 "\n",
1043 name);
1047 int main(int argc, char **argv)
1049 int readonly = 0;
1050 const char *sopt = "hVc:Crsnm";
1051 struct option lopt[] = {
1052 { "help", 0, 0, 'h' },
1053 { "version", 0, 0, 'V' },
1054 { "offset", 1, 0, 'o' },
1055 { "cmd", 1, 0, 'c' },
1056 { "create", 0, 0, 'C' },
1057 { "read-only", 0, 0, 'r' },
1058 { "snapshot", 0, 0, 's' },
1059 { "nocache", 0, 0, 'n' },
1060 { "misalign", 0, 0, 'm' },
1061 { NULL, 0, 0, 0 }
1063 int c;
1064 int opt_index = 0;
1065 int flags = 0;
1067 progname = basename(argv[0]);
1069 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1070 switch (c) {
1071 case 's':
1072 flags |= BDRV_O_SNAPSHOT;
1073 break;
1074 case 'n':
1075 flags |= BDRV_O_NOCACHE;
1076 break;
1077 case 'c':
1078 add_user_command(optarg);
1079 break;
1080 case 'C':
1081 flags |= BDRV_O_CREAT;
1082 break;
1083 case 'r':
1084 readonly = 1;
1085 break;
1086 case 'm':
1087 misalign = 1;
1088 break;
1089 case 'V':
1090 printf("%s version %s\n", progname, VERSION);
1091 exit(0);
1092 case 'h':
1093 usage(progname);
1094 exit(0);
1095 default:
1096 usage(progname);
1097 exit(1);
1101 if ((argc - optind) > 1) {
1102 usage(progname);
1103 exit(1);
1106 bdrv_init();
1108 /* initialize commands */
1109 quit_init();
1110 help_init();
1111 add_command(&open_cmd);
1112 add_command(&close_cmd);
1113 add_command(&read_cmd);
1114 add_command(&readv_cmd);
1115 add_command(&write_cmd);
1116 add_command(&writev_cmd);
1117 add_command(&flush_cmd);
1118 add_command(&truncate_cmd);
1119 add_command(&length_cmd);
1120 add_command(&info_cmd);
1121 add_command(&alloc_cmd);
1123 add_args_command(init_args_command);
1124 add_check_command(init_check_command);
1126 /* open the device */
1127 if (readonly)
1128 flags |= BDRV_O_RDONLY;
1129 else
1130 flags |= BDRV_O_RDWR;
1132 if ((argc - optind) == 1)
1133 openfile(argv[optind], flags);
1134 command_loop();
1136 if (bs)
1137 bdrv_close(bs);
1138 return 0;