Use correct byteswap routine for elf_note
[qemu] / 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(const void *buffer, int64_t offset, int len)
58 {
59         int i, j;
60         const uint8_t *p;
61
62         for (i = 0, p = buffer; i < len; i += 16) {
63                 const uint8_t *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(*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 /*
102  * Parse multiple length statements for vectored I/O, and construct an I/O
103  * vector matching it.
104  */
105 static void *
106 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
107 {
108         size_t *sizes = calloc(nr_iov, sizeof(size_t));
109         size_t count = 0;
110         void *buf, *p;
111         int i;
112
113         for (i = 0; i < nr_iov; i++) {
114                 char *arg = argv[i];
115                 long long len;
116
117                 len = cvtnum(arg);
118                 if (len < 0) {
119                         printf("non-numeric length argument -- %s\n", arg);
120                         return NULL;
121                 }
122
123                 /* should be SIZE_T_MAX, but that doesn't exist */
124                 if (len > UINT_MAX) {
125                         printf("too large length argument -- %s\n", arg);
126                         return NULL;
127                 }
128
129                 if (len & 0x1ff) {
130                         printf("length argument %lld is not sector aligned\n",
131                                 len);
132                         return NULL;
133                 }
134
135                 sizes[i] = len;
136                 count += len;
137         }
138
139         qemu_iovec_init(qiov, nr_iov);
140
141         buf = p = qemu_io_alloc(count, pattern);
142
143         for (i = 0; i < nr_iov; i++) {
144                 qemu_iovec_add(qiov, p, sizes[i]);
145                 p += sizes[i];
146         }
147
148         free(sizes);
149         return buf;
150 }
151
152 static int do_read(char *buf, int64_t offset, int count, int *total)
153 {
154         int ret;
155
156         ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
157         if (ret < 0)
158                 return ret;
159         *total = count;
160         return 1;
161 }
162
163 static int do_write(char *buf, int64_t offset, int count, int *total)
164 {
165         int ret;
166
167         ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
168         if (ret < 0)
169                 return ret;
170         *total = count;
171         return 1;
172 }
173
174 static int do_pread(char *buf, int64_t offset, int count, int *total)
175 {
176         *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
177         if (*total < 0)
178                 return *total;
179         return 1;
180 }
181
182 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
183 {
184         *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
185         if (*total < 0)
186                 return *total;
187         return 1;
188 }
189
190 static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
191 {
192         *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
193         if (*total < 0)
194                 return *total;
195         return 1;
196 }
197
198 static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
199 {
200         *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
201         if (*total < 0)
202                 return *total;
203         return 1;
204 }
205
206 #define NOT_DONE 0x7fffffff
207 static void aio_rw_done(void *opaque, int ret)
208 {
209         *(int *)opaque = ret;
210 }
211
212 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
213 {
214         BlockDriverAIOCB *acb;
215         int async_ret = NOT_DONE;
216
217         acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
218                              aio_rw_done, &async_ret);
219         if (!acb)
220                 return -EIO;
221
222         while (async_ret == NOT_DONE)
223                 qemu_aio_wait();
224
225         *total = qiov->size;
226         return async_ret < 0 ? async_ret : 1;
227 }
228
229 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
230 {
231         BlockDriverAIOCB *acb;
232         int async_ret = NOT_DONE;
233
234         acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
235                               aio_rw_done, &async_ret);
236         if (!acb)
237                 return -EIO;
238
239         while (async_ret == NOT_DONE)
240                 qemu_aio_wait();
241
242         *total = qiov->size;
243         return async_ret < 0 ? async_ret : 1;
244 }
245
246
247 static const cmdinfo_t read_cmd;
248
249 static void
250 read_help(void)
251 {
252         printf(
253 "\n"
254 " reads a range of bytes from the given offset\n"
255 "\n"
256 " Example:\n"
257 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
258 "\n"
259 " Reads a segment of the currently open file, optionally dumping it to the\n"
260 " standard output stream (with -v option) for subsequent inspection.\n"
261 " -b, -- read from the VM state rather than the virtual disk\n"
262 " -C, -- report statistics in a machine parsable format\n"
263 " -l, -- length for pattern verification (only with -P)\n"
264 " -p, -- use bdrv_pread to read the file\n"
265 " -P, -- use a pattern to verify read data\n"
266 " -q, -- quite mode, do not show I/O statistics\n"
267 " -s, -- start offset for pattern verification (only with -P)\n"
268 " -v, -- dump buffer to standard output\n"
269 "\n");
270 }
271
272 static int
273 read_f(int argc, char **argv)
274 {
275         struct timeval t1, t2;
276         int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
277         int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
278         int c, cnt;
279         char *buf;
280         int64_t offset;
281         int count;
282         /* Some compilers get confused and warn if this is not initialized.  */
283         int total = 0;
284         int pattern = 0, pattern_offset = 0, pattern_count = 0;
285
286         while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
287                 switch (c) {
288                 case 'b':
289                         bflag = 1;
290                         break;
291                 case 'C':
292                         Cflag = 1;
293                         break;
294                 case 'l':
295                         lflag = 1;
296                         pattern_count = cvtnum(optarg);
297                         if (pattern_count < 0) {
298                                 printf("non-numeric length argument -- %s\n", optarg);
299                                 return 0;
300                         }
301                         break;
302                 case 'p':
303                         pflag = 1;
304                         break;
305                 case 'P':
306                         Pflag = 1;
307                         pattern = atoi(optarg);
308                         break;
309                 case 'q':
310                         qflag = 1;
311                         break;
312                 case 's':
313                         sflag = 1;
314                         pattern_offset = cvtnum(optarg);
315                         if (pattern_offset < 0) {
316                                 printf("non-numeric length argument -- %s\n", optarg);
317                                 return 0;
318                         }
319                         break;
320                 case 'v':
321                         vflag = 1;
322                         break;
323                 default:
324                         return command_usage(&read_cmd);
325                 }
326         }
327
328         if (optind != argc - 2)
329                 return command_usage(&read_cmd);
330
331         if (bflag && pflag) {
332                 printf("-b and -p cannot be specified at the same time\n");
333                 return 0;
334         }
335
336         offset = cvtnum(argv[optind]);
337         if (offset < 0) {
338                 printf("non-numeric length argument -- %s\n", argv[optind]);
339                 return 0;
340         }
341
342         optind++;
343         count = cvtnum(argv[optind]);
344         if (count < 0) {
345                 printf("non-numeric length argument -- %s\n", argv[optind]);
346                 return 0;
347         }
348
349     if (!Pflag && (lflag || sflag)) {
350         return command_usage(&read_cmd);
351     }
352
353     if (!lflag) {
354         pattern_count = count - pattern_offset;
355     }
356
357     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
358         printf("pattern verfication range exceeds end of read data\n");
359         return 0;
360     }
361
362         if (!pflag)
363                 if (offset & 0x1ff) {
364                         printf("offset %lld is not sector aligned\n",
365                                 (long long)offset);
366                         return 0;
367
368                 if (count & 0x1ff) {
369                         printf("count %d is not sector aligned\n",
370                                 count);
371                         return 0;
372                 }
373         }
374
375         buf = qemu_io_alloc(count, 0xab);
376
377         gettimeofday(&t1, NULL);
378         if (pflag)
379                 cnt = do_pread(buf, offset, count, &total);
380         else if (bflag)
381                 cnt = do_load_vmstate(buf, offset, count, &total);
382         else
383                 cnt = do_read(buf, offset, count, &total);
384         gettimeofday(&t2, NULL);
385
386         if (cnt < 0) {
387                 printf("read failed: %s\n", strerror(-cnt));
388                 goto out;
389         }
390
391         if (Pflag) {
392                 void* cmp_buf = malloc(pattern_count);
393                 memset(cmp_buf, pattern, pattern_count);
394                 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
395                         printf("Pattern verification failed at offset %lld, "
396                                 "%d bytes\n",
397                                 (long long) offset + pattern_offset, pattern_count);
398                 }
399                 free(cmp_buf);
400         }
401
402         if (qflag)
403                 goto out;
404
405         if (vflag)
406                 dump_buffer(buf, offset, count);
407
408         /* Finally, report back -- -C gives a parsable format */
409         t2 = tsub(t2, t1);
410         print_report("read", &t2, offset, count, total, cnt, Cflag);
411
412 out:
413         qemu_io_free(buf);
414
415         return 0;
416 }
417
418 static const cmdinfo_t read_cmd = {
419         .name           = "read",
420         .altname        = "r",
421         .cfunc          = read_f,
422         .argmin         = 2,
423         .argmax         = -1,
424         .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
425         .oneline        = "reads a number of bytes at a specified offset",
426         .help           = read_help,
427 };
428
429 static const cmdinfo_t readv_cmd;
430
431 static void
432 readv_help(void)
433 {
434         printf(
435 "\n"
436 " reads a range of bytes from the given offset into multiple buffers\n"
437 "\n"
438 " Example:\n"
439 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
440 "\n"
441 " Reads a segment of the currently open file, optionally dumping it to the\n"
442 " standard output stream (with -v option) for subsequent inspection.\n"
443 " Uses multiple iovec buffers if more than one byte range is specified.\n"
444 " -C, -- report statistics in a machine parsable format\n"
445 " -P, -- use a pattern to verify read data\n"
446 " -v, -- dump buffer to standard output\n"
447 " -q, -- quite mode, do not show I/O statistics\n"
448 "\n");
449 }
450
451 static int
452 readv_f(int argc, char **argv)
453 {
454         struct timeval t1, t2;
455         int Cflag = 0, qflag = 0, vflag = 0;
456         int c, cnt;
457         char *buf;
458         int64_t offset;
459         int total;
460         int nr_iov;
461         QEMUIOVector qiov;
462         int pattern = 0;
463         int Pflag = 0;
464
465         while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
466                 switch (c) {
467                 case 'C':
468                         Cflag = 1;
469                         break;
470                 case 'P':
471                         Pflag = 1;
472                         pattern = atoi(optarg);
473                         break;
474                 case 'q':
475                         qflag = 1;
476                         break;
477                 case 'v':
478                         vflag = 1;
479                         break;
480                 default:
481                         return command_usage(&readv_cmd);
482                 }
483         }
484
485         if (optind > argc - 2)
486                 return command_usage(&readv_cmd);
487
488
489         offset = cvtnum(argv[optind]);
490         if (offset < 0) {
491                 printf("non-numeric length argument -- %s\n", argv[optind]);
492                 return 0;
493         }
494         optind++;
495
496         if (offset & 0x1ff) {
497                 printf("offset %lld is not sector aligned\n",
498                         (long long)offset);
499                 return 0;
500         }
501
502         nr_iov = argc - optind;
503         buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
504
505         gettimeofday(&t1, NULL);
506         cnt = do_aio_readv(&qiov, offset, &total);
507         gettimeofday(&t2, NULL);
508
509         if (cnt < 0) {
510                 printf("readv failed: %s\n", strerror(-cnt));
511                 goto out;
512         }
513
514         if (Pflag) {
515                 void* cmp_buf = malloc(qiov.size);
516                 memset(cmp_buf, pattern, qiov.size);
517                 if (memcmp(buf, cmp_buf, qiov.size)) {
518                         printf("Pattern verification failed at offset %lld, "
519                                 "%zd bytes\n",
520                                 (long long) offset, qiov.size);
521                 }
522                 free(cmp_buf);
523         }
524
525         if (qflag)
526                 goto out;
527
528         if (vflag)
529                 dump_buffer(buf, offset, qiov.size);
530
531         /* Finally, report back -- -C gives a parsable format */
532         t2 = tsub(t2, t1);
533         print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
534
535 out:
536         qemu_io_free(buf);
537         return 0;
538 }
539
540 static const cmdinfo_t readv_cmd = {
541         .name           = "readv",
542         .cfunc          = readv_f,
543         .argmin         = 2,
544         .argmax         = -1,
545         .args           = "[-Cqv] [-P pattern ] off len [len..]",
546         .oneline        = "reads a number of bytes at a specified offset",
547         .help           = readv_help,
548 };
549
550 static const cmdinfo_t write_cmd;
551
552 static void
553 write_help(void)
554 {
555         printf(
556 "\n"
557 " writes a range of bytes from the given offset\n"
558 "\n"
559 " Example:\n"
560 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
561 "\n"
562 " Writes into a segment of the currently open file, using a buffer\n"
563 " filled with a set pattern (0xcdcdcdcd).\n"
564 " -b, -- write to the VM state rather than the virtual disk\n"
565 " -p, -- use bdrv_pwrite to write the file\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 write_f(int argc, char **argv)
574 {
575         struct timeval t1, t2;
576         int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
577         int c, cnt;
578         char *buf;
579         int64_t offset;
580         int count;
581         /* Some compilers get confused and warn if this is not initialized.  */
582         int total = 0;
583         int pattern = 0xcd;
584
585         while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
586                 switch (c) {
587                 case 'b':
588                         bflag = 1;
589                         break;
590                 case 'C':
591                         Cflag = 1;
592                         break;
593                 case 'p':
594                         pflag = 1;
595                         break;
596                 case 'P':
597                         pattern = atoi(optarg);
598                         break;
599                 case 'q':
600                         qflag = 1;
601                         break;
602                 default:
603                         return command_usage(&write_cmd);
604                 }
605         }
606
607         if (optind != argc - 2)
608                 return command_usage(&write_cmd);
609
610         if (bflag && pflag) {
611                 printf("-b and -p cannot be specified at the same time\n");
612                 return 0;
613         }
614
615         offset = cvtnum(argv[optind]);
616         if (offset < 0) {
617                 printf("non-numeric length argument -- %s\n", argv[optind]);
618                 return 0;
619         }
620
621         optind++;
622         count = cvtnum(argv[optind]);
623         if (count < 0) {
624                 printf("non-numeric length argument -- %s\n", argv[optind]);
625                 return 0;
626         }
627
628         if (!pflag) {
629                 if (offset & 0x1ff) {
630                         printf("offset %lld is not sector aligned\n",
631                                 (long long)offset);
632                         return 0;
633                 }
634
635                 if (count & 0x1ff) {
636                         printf("count %d is not sector aligned\n",
637                                 count);
638                         return 0;
639                 }
640         }
641
642         buf = qemu_io_alloc(count, pattern);
643
644         gettimeofday(&t1, NULL);
645         if (pflag)
646                 cnt = do_pwrite(buf, offset, count, &total);
647         else if (bflag)
648                 cnt = do_save_vmstate(buf, offset, count, &total);
649         else
650                 cnt = do_write(buf, offset, count, &total);
651         gettimeofday(&t2, NULL);
652
653         if (cnt < 0) {
654                 printf("write failed: %s\n", strerror(-cnt));
655                 goto out;
656         }
657
658         if (qflag)
659                 goto out;
660
661         /* Finally, report back -- -C gives a parsable format */
662         t2 = tsub(t2, t1);
663         print_report("wrote", &t2, offset, count, total, cnt, Cflag);
664
665 out:
666         qemu_io_free(buf);
667
668         return 0;
669 }
670
671 static const cmdinfo_t write_cmd = {
672         .name           = "write",
673         .altname        = "w",
674         .cfunc          = write_f,
675         .argmin         = 2,
676         .argmax         = -1,
677         .args           = "[-abCpq] [-P pattern ] off len",
678         .oneline        = "writes a number of bytes at a specified offset",
679         .help           = write_help,
680 };
681
682 static const cmdinfo_t writev_cmd;
683
684 static void
685 writev_help(void)
686 {
687         printf(
688 "\n"
689 " writes a range of bytes from the given offset source from multiple buffers\n"
690 "\n"
691 " Example:\n"
692 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
693 "\n"
694 " Writes into a segment of the currently open file, using a buffer\n"
695 " filled with a set pattern (0xcdcdcdcd).\n"
696 " -P, -- use different pattern to fill file\n"
697 " -C, -- report statistics in a machine parsable format\n"
698 " -q, -- quite mode, do not show I/O statistics\n"
699 "\n");
700 }
701
702 static int
703 writev_f(int argc, char **argv)
704 {
705         struct timeval t1, t2;
706         int Cflag = 0, qflag = 0;
707         int c, cnt;
708         char *buf;
709         int64_t offset;
710         int total;
711         int nr_iov;
712         int pattern = 0xcd;
713         QEMUIOVector qiov;
714
715         while ((c = getopt(argc, argv, "CqP:")) != EOF) {
716                 switch (c) {
717                 case 'C':
718                         Cflag = 1;
719                         break;
720                 case 'q':
721                         qflag = 1;
722                         break;
723                 case 'P':
724                         pattern = atoi(optarg);
725                         break;
726                 default:
727                         return command_usage(&writev_cmd);
728                 }
729         }
730
731         if (optind > argc - 2)
732                 return command_usage(&writev_cmd);
733
734         offset = cvtnum(argv[optind]);
735         if (offset < 0) {
736                 printf("non-numeric length argument -- %s\n", argv[optind]);
737                 return 0;
738         }
739         optind++;
740
741         if (offset & 0x1ff) {
742                 printf("offset %lld is not sector aligned\n",
743                         (long long)offset);
744                 return 0;
745         }
746
747         nr_iov = argc - optind;
748         buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
749
750         gettimeofday(&t1, NULL);
751         cnt = do_aio_writev(&qiov, offset, &total);
752         gettimeofday(&t2, NULL);
753
754         if (cnt < 0) {
755                 printf("writev failed: %s\n", strerror(-cnt));
756                 goto out;
757         }
758
759         if (qflag)
760                 goto out;
761
762         /* Finally, report back -- -C gives a parsable format */
763         t2 = tsub(t2, t1);
764         print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
765 out:
766         qemu_io_free(buf);
767         return 0;
768 }
769
770 static const cmdinfo_t writev_cmd = {
771         .name           = "writev",
772         .cfunc          = writev_f,
773         .argmin         = 2,
774         .argmax         = -1,
775         .args           = "[-Cq] [-P pattern ] off len [len..]",
776         .oneline        = "writes a number of bytes at a specified offset",
777         .help           = writev_help,
778 };
779
780 struct aio_ctx {
781         QEMUIOVector qiov;
782         int64_t offset;
783         char *buf;
784         int qflag;
785         int vflag;
786         int Cflag;
787         int Pflag;
788         int pattern;
789         struct timeval t1;
790 };
791
792 static void
793 aio_write_done(void *opaque, int ret)
794 {
795         struct aio_ctx *ctx = opaque;
796         struct timeval t2;
797
798         gettimeofday(&t2, NULL);
799
800
801         if (ret < 0) {
802                 printf("aio_write failed: %s\n", strerror(-ret));
803                 goto out;
804         }
805
806         if (ctx->qflag) {
807                 goto out;
808         }
809
810         /* Finally, report back -- -C gives a parsable format */
811         t2 = tsub(t2, ctx->t1);
812         print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
813                      ctx->qiov.size, 1, ctx->Cflag);
814 out:
815         qemu_io_free(ctx->buf);
816         free(ctx);
817 }
818
819 static const cmdinfo_t aio_read_cmd;
820
821 static void
822 aio_read_done(void *opaque, int ret)
823 {
824         struct aio_ctx *ctx = opaque;
825         struct timeval t2;
826
827         gettimeofday(&t2, NULL);
828
829         if (ret < 0) {
830                 printf("readv failed: %s\n", strerror(-ret));
831                 goto out;
832         }
833
834         if (ctx->Pflag) {
835                 void *cmp_buf = malloc(ctx->qiov.size);
836
837                 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
838                 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
839                         printf("Pattern verification failed at offset %lld, "
840                                 "%zd bytes\n",
841                                 (long long) ctx->offset, ctx->qiov.size);
842                 }
843                 free(cmp_buf);
844         }
845
846         if (ctx->qflag) {
847                 goto out;
848         }
849
850         if (ctx->vflag) {
851                 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
852         }
853
854         /* Finally, report back -- -C gives a parsable format */
855         t2 = tsub(t2, ctx->t1);
856         print_report("read", &t2, ctx->offset, ctx->qiov.size,
857                      ctx->qiov.size, 1, ctx->Cflag);
858 out:
859         qemu_io_free(ctx->buf);
860         free(ctx);
861 }
862
863 static void
864 aio_read_help(void)
865 {
866         printf(
867 "\n"
868 " asynchronously reads a range of bytes from the given offset\n"
869 "\n"
870 " Example:\n"
871 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
872 "\n"
873 " Reads a segment of the currently open file, optionally dumping it to the\n"
874 " standard output stream (with -v option) for subsequent inspection.\n"
875 " The read is performed asynchronously and should the aio_flush command \n"
876 " should be used to ensure all outstanding aio requests have been completed\n"
877 " -C, -- report statistics in a machine parsable format\n"
878 " -P, -- use a pattern to verify read data\n"
879 " -v, -- dump buffer to standard output\n"
880 " -q, -- quite mode, do not show I/O statistics\n"
881 "\n");
882 }
883
884 static int
885 aio_read_f(int argc, char **argv)
886 {
887         int nr_iov, c;
888         struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
889         BlockDriverAIOCB *acb;
890
891         while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
892                 switch (c) {
893                 case 'C':
894                         ctx->Cflag = 1;
895                         break;
896                 case 'P':
897                         ctx->Pflag = 1;
898                         ctx->pattern = atoi(optarg);
899                         break;
900                 case 'q':
901                         ctx->qflag = 1;
902                         break;
903                 case 'v':
904                         ctx->vflag = 1;
905                         break;
906                 default:
907                         free(ctx);
908                         return command_usage(&aio_read_cmd);
909                 }
910         }
911
912         if (optind > argc - 2) {
913                 free(ctx);
914                 return command_usage(&aio_read_cmd);
915         }
916
917         ctx->offset = cvtnum(argv[optind]);
918         if (ctx->offset < 0) {
919                 printf("non-numeric length argument -- %s\n", argv[optind]);
920                 free(ctx);
921                 return 0;
922         }
923         optind++;
924
925         if (ctx->offset & 0x1ff) {
926                 printf("offset %lld is not sector aligned\n",
927                         (long long)ctx->offset);
928                 free(ctx);
929                 return 0;
930         }
931
932         nr_iov = argc - optind;
933         ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
934
935         gettimeofday(&ctx->t1, NULL);
936         acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
937                               ctx->qiov.size >> 9, aio_read_done, ctx);
938         if (!acb) {
939                 free(ctx->buf);
940                 free(ctx);
941                 return -EIO;
942         }
943
944         return 0;
945 }
946
947 static const cmdinfo_t aio_read_cmd = {
948         .name           = "aio_read",
949         .cfunc          = aio_read_f,
950         .argmin         = 2,
951         .argmax         = -1,
952         .args           = "[-Cqv] [-P pattern ] off len [len..]",
953         .oneline        = "asynchronously reads a number of bytes",
954         .help           = aio_read_help,
955 };
956
957 static const cmdinfo_t aio_write_cmd;
958
959 static void
960 aio_write_help(void)
961 {
962         printf(
963 "\n"
964 " asynchronously writes a range of bytes from the given offset source \n"
965 " from multiple buffers\n"
966 "\n"
967 " Example:\n"
968 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
969 "\n"
970 " Writes into a segment of the currently open file, using a buffer\n"
971 " filled with a set pattern (0xcdcdcdcd).\n"
972 " The write is performed asynchronously and should the aio_flush command \n"
973 " should be used to ensure all outstanding aio requests have been completed\n"
974 " -P, -- use different pattern to fill file\n"
975 " -C, -- report statistics in a machine parsable format\n"
976 " -q, -- quite mode, do not show I/O statistics\n"
977 "\n");
978 }
979
980
981 static int
982 aio_write_f(int argc, char **argv)
983 {
984         int nr_iov, c;
985         int pattern = 0xcd;
986         struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
987         BlockDriverAIOCB *acb;
988
989         while ((c = getopt(argc, argv, "CqP:")) != EOF) {
990                 switch (c) {
991                 case 'C':
992                         ctx->Cflag = 1;
993                         break;
994                 case 'q':
995                         ctx->qflag = 1;
996                         break;
997                 case 'P':
998                         pattern = atoi(optarg);
999                         break;
1000                 default:
1001                         free(ctx);
1002                         return command_usage(&aio_write_cmd);
1003                 }
1004         }
1005
1006         if (optind > argc - 2) {
1007                 free(ctx);
1008                 return command_usage(&aio_write_cmd);
1009         }
1010
1011         ctx->offset = cvtnum(argv[optind]);
1012         if (ctx->offset < 0) {
1013                 printf("non-numeric length argument -- %s\n", argv[optind]);
1014                 free(ctx);
1015                 return 0;
1016         }
1017         optind++;
1018
1019         if (ctx->offset & 0x1ff) {
1020                 printf("offset %lld is not sector aligned\n",
1021                         (long long)ctx->offset);
1022                 free(ctx);
1023                 return 0;
1024         }
1025
1026         nr_iov = argc - optind;
1027         ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1028
1029         gettimeofday(&ctx->t1, NULL);
1030         acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1031                               ctx->qiov.size >> 9, aio_write_done, ctx);
1032         if (!acb) {
1033                 free(ctx->buf);
1034                 free(ctx);
1035                 return -EIO;
1036         }
1037
1038         return 0;
1039 }
1040
1041 static const cmdinfo_t aio_write_cmd = {
1042         .name           = "aio_write",
1043         .cfunc          = aio_write_f,
1044         .argmin         = 2,
1045         .argmax         = -1,
1046         .args           = "[-Cq] [-P pattern ] off len [len..]",
1047         .oneline        = "asynchronously writes a number of bytes",
1048         .help           = aio_write_help,
1049 };
1050
1051 static int
1052 aio_flush_f(int argc, char **argv)
1053 {
1054         qemu_aio_flush();
1055         return 0;
1056 }
1057
1058 static const cmdinfo_t aio_flush_cmd = {
1059         .name           = "aio_flush",
1060         .cfunc          = aio_flush_f,
1061         .oneline        = "completes all outstanding aio requets"
1062 };
1063
1064 static int
1065 flush_f(int argc, char **argv)
1066 {
1067         bdrv_flush(bs);
1068         return 0;
1069 }
1070
1071 static const cmdinfo_t flush_cmd = {
1072         .name           = "flush",
1073         .altname        = "f",
1074         .cfunc          = flush_f,
1075         .oneline        = "flush all in-core file state to disk",
1076 };
1077
1078 static int
1079 truncate_f(int argc, char **argv)
1080 {
1081         int64_t offset;
1082         int ret;
1083
1084         offset = cvtnum(argv[1]);
1085         if (offset < 0) {
1086                 printf("non-numeric truncate argument -- %s\n", argv[1]);
1087                 return 0;
1088         }
1089
1090         ret = bdrv_truncate(bs, offset);
1091         if (ret < 0) {
1092                 printf("truncate: %s", strerror(ret));
1093                 return 0;
1094         }
1095
1096         return 0;
1097 }
1098
1099 static const cmdinfo_t truncate_cmd = {
1100         .name           = "truncate",
1101         .altname        = "t",
1102         .cfunc          = truncate_f,
1103         .argmin         = 1,
1104         .argmax         = 1,
1105         .args           = "off",
1106         .oneline        = "truncates the current file at the given offset",
1107 };
1108
1109 static int
1110 length_f(int argc, char **argv)
1111 {
1112         int64_t size;
1113         char s1[64];
1114
1115         size = bdrv_getlength(bs);
1116         if (size < 0) {
1117                 printf("getlength: %s", strerror(size));
1118                 return 0;
1119         }
1120
1121         cvtstr(size, s1, sizeof(s1));
1122         printf("%s\n", s1);
1123         return 0;
1124 }
1125
1126
1127 static const cmdinfo_t length_cmd = {
1128         .name           = "length",
1129         .altname        = "l",
1130         .cfunc          = length_f,
1131         .oneline        = "gets the length of the current file",
1132 };
1133
1134
1135 static int
1136 info_f(int argc, char **argv)
1137 {
1138         BlockDriverInfo bdi;
1139         char s1[64], s2[64];
1140         int ret;
1141
1142         if (bs->drv && bs->drv->format_name)
1143                 printf("format name: %s\n", bs->drv->format_name);
1144         if (bs->drv && bs->drv->protocol_name)
1145                 printf("format name: %s\n", bs->drv->protocol_name);
1146
1147         ret = bdrv_get_info(bs, &bdi);
1148         if (ret)
1149                 return 0;
1150
1151         cvtstr(bdi.cluster_size, s1, sizeof(s1));
1152         cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1153
1154         printf("cluster size: %s\n", s1);
1155         printf("vm state offset: %s\n", s2);
1156
1157         return 0;
1158 }
1159
1160
1161
1162 static const cmdinfo_t info_cmd = {
1163         .name           = "info",
1164         .altname        = "i",
1165         .cfunc          = info_f,
1166         .oneline        = "prints information about the current file",
1167 };
1168
1169 static int
1170 alloc_f(int argc, char **argv)
1171 {
1172         int64_t offset;
1173         int nb_sectors;
1174         char s1[64];
1175         int num;
1176         int ret;
1177         const char *retstr;
1178
1179         offset = cvtnum(argv[1]);
1180         if (offset & 0x1ff) {
1181                 printf("offset %lld is not sector aligned\n",
1182                         (long long)offset);
1183                 return 0;
1184         }
1185
1186         if (argc == 3)
1187                 nb_sectors = cvtnum(argv[2]);
1188         else
1189                 nb_sectors = 1;
1190
1191         ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1192
1193         cvtstr(offset, s1, sizeof(s1));
1194
1195         retstr = ret ? "allocated" : "not allocated";
1196         if (nb_sectors == 1)
1197                 printf("sector %s at offset %s\n", retstr, s1);
1198         else
1199                 printf("%d/%d sectors %s at offset %s\n",
1200                         num, nb_sectors, retstr, s1);
1201         return 0;
1202 }
1203
1204 static const cmdinfo_t alloc_cmd = {
1205         .name           = "alloc",
1206         .altname        = "a",
1207         .argmin         = 1,
1208         .argmax         = 2,
1209         .cfunc          = alloc_f,
1210         .args           = "off [sectors]",
1211         .oneline        = "checks if a sector is present in the file",
1212 };
1213
1214 static int
1215 close_f(int argc, char **argv)
1216 {
1217         bdrv_close(bs);
1218         bs = NULL;
1219         return 0;
1220 }
1221
1222 static const cmdinfo_t close_cmd = {
1223         .name           = "close",
1224         .altname        = "c",
1225         .cfunc          = close_f,
1226         .oneline        = "close the current open file",
1227 };
1228
1229 static int openfile(char *name, int flags, int growable)
1230 {
1231         if (bs) {
1232                 fprintf(stderr, "file open already, try 'help close'\n");
1233                 return 1;
1234         }
1235
1236         bs = bdrv_new("hda");
1237         if (!bs)
1238                 return 1;
1239
1240         if (growable) {
1241                 flags |= BDRV_O_FILE;
1242         }
1243
1244         if (bdrv_open(bs, name, flags) == -1) {
1245                 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1246                 bs = NULL;
1247                 return 1;
1248         }
1249
1250         if (growable) {
1251                 bs->growable = 1;
1252         }
1253         return 0;
1254 }
1255
1256 static void
1257 open_help(void)
1258 {
1259         printf(
1260 "\n"
1261 " opens a new file in the requested mode\n"
1262 "\n"
1263 " Example:\n"
1264 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1265 "\n"
1266 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1267 " -C, -- create new file if it doesn't exist\n"
1268 " -r, -- open file read-only\n"
1269 " -s, -- use snapshot file\n"
1270 " -n, -- disable host cache\n"
1271 " -g, -- allow file to grow (only applies to protocols)"
1272 "\n");
1273 }
1274
1275 static const cmdinfo_t open_cmd;
1276
1277 static int
1278 open_f(int argc, char **argv)
1279 {
1280         int flags = 0;
1281         int readonly = 0;
1282         int growable = 0;
1283         int c;
1284
1285         while ((c = getopt(argc, argv, "snCrg")) != EOF) {
1286                 switch (c) {
1287                 case 's':
1288                         flags |= BDRV_O_SNAPSHOT;
1289                         break;
1290                 case 'n':
1291                         flags |= BDRV_O_NOCACHE;
1292                         break;
1293                 case 'C':
1294                         flags |= BDRV_O_CREAT;
1295                         break;
1296                 case 'r':
1297                         readonly = 1;
1298                         break;
1299                 case 'g':
1300                         growable = 1;
1301                         break;
1302                 default:
1303                         return command_usage(&open_cmd);
1304                 }
1305         }
1306
1307         if (readonly)
1308                 flags |= BDRV_O_RDONLY;
1309         else
1310                 flags |= BDRV_O_RDWR;
1311
1312         if (optind != argc - 1)
1313                 return command_usage(&open_cmd);
1314
1315         return openfile(argv[optind], flags, growable);
1316 }
1317
1318 static const cmdinfo_t open_cmd = {
1319         .name           = "open",
1320         .altname        = "o",
1321         .cfunc          = open_f,
1322         .argmin         = 1,
1323         .argmax         = -1,
1324         .flags          = CMD_NOFILE_OK,
1325         .args           = "[-Crsn] [path]",
1326         .oneline        = "open the file specified by path",
1327         .help           = open_help,
1328 };
1329
1330 static int
1331 init_args_command(
1332         int     index)
1333 {
1334         /* only one device allowed so far */
1335         if (index >= 1)
1336                 return 0;
1337         return ++index;
1338 }
1339
1340 static int
1341 init_check_command(
1342         const cmdinfo_t *ct)
1343 {
1344         if (ct->flags & CMD_FLAG_GLOBAL)
1345                 return 1;
1346         if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1347                 fprintf(stderr, "no file open, try 'help open'\n");
1348                 return 0;
1349         }
1350         return 1;
1351 }
1352
1353 static void usage(const char *name)
1354 {
1355         printf(
1356 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1357 "QEMU Disk exerciser\n"
1358 "\n"
1359 "  -C, --create         create new file if it doesn't exist\n"
1360 "  -c, --cmd            command to execute\n"
1361 "  -r, --read-only      export read-only\n"
1362 "  -s, --snapshot       use snapshot file\n"
1363 "  -n, --nocache        disable host cache\n"
1364 "  -g, --growable       allow file to grow (only applies to protocols)\n"
1365 "  -m, --misalign       misalign allocations for O_DIRECT\n"
1366 "  -h, --help           display this help and exit\n"
1367 "  -V, --version        output version information and exit\n"
1368 "\n",
1369         name);
1370 }
1371
1372
1373 int main(int argc, char **argv)
1374 {
1375         int readonly = 0;
1376         int growable = 0;
1377         const char *sopt = "hVc:Crsnmg";
1378         struct option lopt[] = {
1379                 { "help", 0, 0, 'h' },
1380                 { "version", 0, 0, 'V' },
1381                 { "offset", 1, 0, 'o' },
1382                 { "cmd", 1, 0, 'c' },
1383                 { "create", 0, 0, 'C' },
1384                 { "read-only", 0, 0, 'r' },
1385                 { "snapshot", 0, 0, 's' },
1386                 { "nocache", 0, 0, 'n' },
1387                 { "misalign", 0, 0, 'm' },
1388                 { "growable", 0, 0, 'g' },
1389                 { NULL, 0, 0, 0 }
1390         };
1391         int c;
1392         int opt_index = 0;
1393         int flags = 0;
1394
1395         progname = basename(argv[0]);
1396
1397         while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1398                 switch (c) {
1399                 case 's':
1400                         flags |= BDRV_O_SNAPSHOT;
1401                         break;
1402                 case 'n':
1403                         flags |= BDRV_O_NOCACHE;
1404                         break;
1405                 case 'c':
1406                         add_user_command(optarg);
1407                         break;
1408                 case 'C':
1409                         flags |= BDRV_O_CREAT;
1410                         break;
1411                 case 'r':
1412                         readonly = 1;
1413                         break;
1414                 case 'm':
1415                         misalign = 1;
1416                         break;
1417                 case 'g':
1418                         growable = 1;
1419                         break;
1420                 case 'V':
1421                         printf("%s version %s\n", progname, VERSION);
1422                         exit(0);
1423                 case 'h':
1424                         usage(progname);
1425                         exit(0);
1426                 default:
1427                         usage(progname);
1428                         exit(1);
1429                 }
1430         }
1431
1432         if ((argc - optind) > 1) {
1433                 usage(progname);
1434                 exit(1);
1435         }
1436
1437         bdrv_init();
1438
1439         /* initialize commands */
1440         quit_init();
1441         help_init();
1442         add_command(&open_cmd);
1443         add_command(&close_cmd);
1444         add_command(&read_cmd);
1445         add_command(&readv_cmd);
1446         add_command(&write_cmd);
1447         add_command(&writev_cmd);
1448         add_command(&aio_read_cmd);
1449         add_command(&aio_write_cmd);
1450         add_command(&aio_flush_cmd);
1451         add_command(&flush_cmd);
1452         add_command(&truncate_cmd);
1453         add_command(&length_cmd);
1454         add_command(&info_cmd);
1455         add_command(&alloc_cmd);
1456
1457         add_args_command(init_args_command);
1458         add_check_command(init_check_command);
1459
1460         /* open the device */
1461         if (readonly)
1462                 flags |= BDRV_O_RDONLY;
1463         else
1464                 flags |= BDRV_O_RDWR;
1465
1466         if ((argc - optind) == 1)
1467                 openfile(argv[optind], flags, growable);
1468         command_loop();
1469
1470         /*
1471          * Make sure all outstanding requests get flushed the program exits.
1472          */
1473         qemu_aio_flush();
1474
1475         if (bs)
1476                 bdrv_close(bs);
1477         return 0;
1478 }