vmstate: add support for arrays of uint16_t
[qemu] / qemu-io.c
index 703326d..cac72e9 100644 (file)
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -7,10 +7,12 @@
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
  */
+#include <sys/time.h>
 #include <sys/types.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <getopt.h>
+#include <libgen.h>
 
 #include "qemu-common.h"
 #include "block_int.h"
@@ -26,6 +28,26 @@ static BlockDriverState *bs;
 static int misalign;
 
 /*
+ * Parse the pattern argument to various sub-commands.
+ *
+ * Because the pattern is used as an argument to memset it must evaluate
+ * to an unsigned integer that fits into a single byte.
+ */
+static int parse_pattern(const char *arg)
+{
+       char *endptr = NULL;
+       long pattern;
+
+       pattern = strtol(arg, &endptr, 0);
+       if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
+               printf("%s is not a valid pattern byte\n", arg);
+               return -1;
+       }
+
+       return pattern;
+}
+
+/*
  * Memory allocation helpers.
  *
  * Make sure memory is aligned by default, or purposefully misaligned if
@@ -54,20 +76,20 @@ static void qemu_io_free(void *p)
 }
 
 static void
-dump_buffer(char *buffer, int64_t offset, int len)
+dump_buffer(const void *buffer, int64_t offset, int len)
 {
        int i, j;
-       char *p;
+       const uint8_t *p;
 
        for (i = 0, p = buffer; i < len; i += 16) {
-               char    *s = p;
+               const uint8_t *s = p;
 
                printf("%08llx:  ", (unsigned long long)offset + i);
                for (j = 0; j < 16 && i + j < len; j++, p++)
                        printf("%02x ", *p);
                printf(" ");
                for (j = 0; j < 16 && i + j < len; j++, s++) {
-                       if (isalnum((int)*s))
+                       if (isalnum(*s))
                                printf("%c", *s);
                        else
                                printf(".");
@@ -98,6 +120,57 @@ print_report(const char *op, struct timeval *t, int64_t offset,
        }
 }
 
+/*
+ * Parse multiple length statements for vectored I/O, and construct an I/O
+ * vector matching it.
+ */
+static void *
+create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
+{
+       size_t *sizes = calloc(nr_iov, sizeof(size_t));
+       size_t count = 0;
+       void *buf, *p;
+       int i;
+
+       for (i = 0; i < nr_iov; i++) {
+               char *arg = argv[i];
+               long long len;
+
+               len = cvtnum(arg);
+               if (len < 0) {
+                       printf("non-numeric length argument -- %s\n", arg);
+                       return NULL;
+               }
+
+               /* should be SIZE_T_MAX, but that doesn't exist */
+               if (len > UINT_MAX) {
+                       printf("too large length argument -- %s\n", arg);
+                       return NULL;
+               }
+
+               if (len & 0x1ff) {
+                       printf("length argument %lld is not sector aligned\n",
+                               len);
+                       return NULL;
+               }
+
+               sizes[i] = len;
+               count += len;
+       }
+
+       qemu_iovec_init(qiov, nr_iov);
+
+       buf = p = qemu_io_alloc(count, pattern);
+
+       for (i = 0; i < nr_iov; i++) {
+               qemu_iovec_add(qiov, p, sizes[i]);
+               p += sizes[i];
+       }
+
+       free(sizes);
+       return buf;
+}
+
 static int do_read(char *buf, int64_t offset, int count, int *total)
 {
        int ret;
@@ -136,6 +209,22 @@ static int do_pwrite(char *buf, int64_t offset, int count, int *total)
        return 1;
 }
 
+static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
+{
+       *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
+       if (*total < 0)
+               return *total;
+       return 1;
+}
+
+static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
+{
+       *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
+       if (*total < 0)
+               return *total;
+       return 1;
+}
+
 #define NOT_DONE 0x7fffffff
 static void aio_rw_done(void *opaque, int ret)
 {
@@ -191,10 +280,14 @@ read_help(void)
 "\n"
 " Reads a segment of the currently open file, optionally dumping it to the\n"
 " standard output stream (with -v option) for subsequent inspection.\n"
-" -p, -- use bdrv_pread to read the file\n"
+" -b, -- read from the VM state rather than the virtual disk\n"
 " -C, -- report statistics in a machine parsable format\n"
-" -v, -- dump buffer to standard output\n"
+" -l, -- length for pattern verification (only with -P)\n"
+" -p, -- use bdrv_pread to read the file\n"
+" -P, -- use a pattern to verify read data\n"
 " -q, -- quite mode, do not show I/O statistics\n"
+" -s, -- start offset for pattern verification (only with -P)\n"
+" -v, -- dump buffer to standard output\n"
 "\n");
 }
 
@@ -203,22 +296,51 @@ read_f(int argc, char **argv)
 {
        struct timeval t1, t2;
        int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
+       int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
        int c, cnt;
        char *buf;
        int64_t offset;
-       int count, total;
+       int count;
+        /* Some compilers get confused and warn if this is not initialized.  */
+        int total = 0;
+       int pattern = 0, pattern_offset = 0, pattern_count = 0;
 
-       while ((c = getopt(argc, argv, "Cpqv")) != EOF) {
+       while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
                switch (c) {
+               case 'b':
+                       bflag = 1;
+                       break;
                case 'C':
                        Cflag = 1;
                        break;
+               case 'l':
+                       lflag = 1;
+                       pattern_count = cvtnum(optarg);
+                       if (pattern_count < 0) {
+                               printf("non-numeric length argument -- %s\n", optarg);
+                               return 0;
+                       }
+                       break;
                case 'p':
                        pflag = 1;
                        break;
+               case 'P':
+                       Pflag = 1;
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
+                       break;
                case 'q':
                        qflag = 1;
                        break;
+               case 's':
+                       sflag = 1;
+                       pattern_offset = cvtnum(optarg);
+                       if (pattern_offset < 0) {
+                               printf("non-numeric length argument -- %s\n", optarg);
+                               return 0;
+                       }
+                       break;
                case 'v':
                        vflag = 1;
                        break;
@@ -230,6 +352,11 @@ read_f(int argc, char **argv)
        if (optind != argc - 2)
                return command_usage(&read_cmd);
 
+       if (bflag && pflag) {
+               printf("-b and -p cannot be specified at the same time\n");
+               return 0;
+       }
+
        offset = cvtnum(argv[optind]);
        if (offset < 0) {
                printf("non-numeric length argument -- %s\n", argv[optind]);
@@ -243,6 +370,19 @@ read_f(int argc, char **argv)
                return 0;
        }
 
+    if (!Pflag && (lflag || sflag)) {
+        return command_usage(&read_cmd);
+    }
+
+    if (!lflag) {
+        pattern_count = count - pattern_offset;
+    }
+
+    if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
+        printf("pattern verfication range exceeds end of read data\n");
+        return 0;
+    }
+
        if (!pflag)
                if (offset & 0x1ff) {
                        printf("offset %lld is not sector aligned\n",
@@ -261,17 +401,30 @@ read_f(int argc, char **argv)
        gettimeofday(&t1, NULL);
        if (pflag)
                cnt = do_pread(buf, offset, count, &total);
+       else if (bflag)
+               cnt = do_load_vmstate(buf, offset, count, &total);
        else
                cnt = do_read(buf, offset, count, &total);
        gettimeofday(&t2, NULL);
 
        if (cnt < 0) {
                printf("read failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
+       }
+
+       if (Pflag) {
+               void* cmp_buf = malloc(pattern_count);
+               memset(cmp_buf, pattern, pattern_count);
+               if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
+                       printf("Pattern verification failed at offset %lld, "
+                               "%d bytes\n",
+                               (long long) offset + pattern_offset, pattern_count);
+               }
+               free(cmp_buf);
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
         if (vflag)
                dump_buffer(buf, offset, count);
@@ -280,6 +433,7 @@ read_f(int argc, char **argv)
        t2 = tsub(t2, t1);
        print_report("read", &t2, offset, count, total, cnt, Cflag);
 
+out:
        qemu_io_free(buf);
 
        return 0;
@@ -291,7 +445,7 @@ static const cmdinfo_t read_cmd = {
        .cfunc          = read_f,
        .argmin         = 2,
        .argmax         = -1,
-       .args           = "[-aCpqv] off len",
+       .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
        .oneline        = "reads a number of bytes at a specified offset",
        .help           = read_help,
 };
@@ -312,6 +466,7 @@ readv_help(void)
 " standard output stream (with -v option) for subsequent inspection.\n"
 " Uses multiple iovec buffers if more than one byte range is specified.\n"
 " -C, -- report statistics in a machine parsable format\n"
+" -P, -- use a pattern to verify read data\n"
 " -v, -- dump buffer to standard output\n"
 " -q, -- quite mode, do not show I/O statistics\n"
 "\n");
@@ -323,17 +478,25 @@ readv_f(int argc, char **argv)
        struct timeval t1, t2;
        int Cflag = 0, qflag = 0, vflag = 0;
        int c, cnt;
-       char *buf, *p;
+       char *buf;
        int64_t offset;
-       int count = 0, total;
-       int nr_iov, i;
+       int total;
+       int nr_iov;
        QEMUIOVector qiov;
+       int pattern = 0;
+       int Pflag = 0;
 
-       while ((c = getopt(argc, argv, "Cqv")) != EOF) {
+       while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
                switch (c) {
                case 'C':
                        Cflag = 1;
                        break;
+               case 'P':
+                       Pflag = 1;
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
+                       break;
                case 'q':
                        qflag = 1;
                        break;
@@ -362,40 +525,8 @@ readv_f(int argc, char **argv)
                return 0;
        }
 
-       if (count & 0x1ff) {
-               printf("count %d is not sector aligned\n",
-                       count);
-               return 0;
-       }
-
-       for (i = optind; i < argc; i++) {
-               size_t len;
-
-               len = cvtnum(argv[i]);
-               if (len < 0) {
-                       printf("non-numeric length argument -- %s\n", argv[i]);
-                       return 0;
-               }
-               count += len;
-       }
-
        nr_iov = argc - optind;
-       qemu_iovec_init(&qiov, nr_iov);
-       buf = p = qemu_io_alloc(count, 0xab);
-       for (i = 0; i < nr_iov; i++) {
-               size_t len;
-
-               len = cvtnum(argv[optind]);
-               if (len < 0) {
-                       printf("non-numeric length argument -- %s\n",
-                               argv[optind]);
-                       return 0;
-               }
-
-               qemu_iovec_add(&qiov, p, len);
-               p += len;
-               optind++;
-       }
+       buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
 
        gettimeofday(&t1, NULL);
        cnt = do_aio_readv(&qiov, offset, &total);
@@ -403,11 +534,22 @@ readv_f(int argc, char **argv)
 
        if (cnt < 0) {
                printf("readv failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
+       }
+
+       if (Pflag) {
+               void* cmp_buf = malloc(qiov.size);
+               memset(cmp_buf, pattern, qiov.size);
+               if (memcmp(buf, cmp_buf, qiov.size)) {
+                       printf("Pattern verification failed at offset %lld, "
+                               "%zd bytes\n",
+                               (long long) offset, qiov.size);
+               }
+               free(cmp_buf);
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
         if (vflag)
                dump_buffer(buf, offset, qiov.size);
@@ -416,8 +558,8 @@ readv_f(int argc, char **argv)
        t2 = tsub(t2, t1);
        print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
 
+out:
        qemu_io_free(buf);
-
        return 0;
 }
 
@@ -426,7 +568,7 @@ static const cmdinfo_t readv_cmd = {
        .cfunc          = readv_f,
        .argmin         = 2,
        .argmax         = -1,
-       .args           = "[-Cqv] off len [len..]",
+       .args           = "[-Cqv] [-P pattern ] off len [len..]",
        .oneline        = "reads a number of bytes at a specified offset",
        .help           = readv_help,
 };
@@ -445,6 +587,7 @@ write_help(void)
 "\n"
 " Writes into a segment of the currently open file, using a buffer\n"
 " filled with a set pattern (0xcdcdcdcd).\n"
+" -b, -- write to the VM state rather than the virtual disk\n"
 " -p, -- use bdrv_pwrite to write the file\n"
 " -P, -- use different pattern to fill file\n"
 " -C, -- report statistics in a machine parsable format\n"
@@ -456,15 +599,20 @@ static int
 write_f(int argc, char **argv)
 {
        struct timeval t1, t2;
-       int Cflag = 0, pflag = 0, qflag = 0;
+       int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
        int c, cnt;
        char *buf;
        int64_t offset;
-       int count, total;
+       int count;
+        /* Some compilers get confused and warn if this is not initialized.  */
+        int total = 0;
        int pattern = 0xcd;
 
-       while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
+       while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
                switch (c) {
+               case 'b':
+                       bflag = 1;
+                       break;
                case 'C':
                        Cflag = 1;
                        break;
@@ -472,7 +620,9 @@ write_f(int argc, char **argv)
                        pflag = 1;
                        break;
                case 'P':
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                case 'q':
                        qflag = 1;
@@ -485,6 +635,11 @@ write_f(int argc, char **argv)
        if (optind != argc - 2)
                return command_usage(&write_cmd);
 
+       if (bflag && pflag) {
+               printf("-b and -p cannot be specified at the same time\n");
+               return 0;
+       }
+
        offset = cvtnum(argv[optind]);
        if (offset < 0) {
                printf("non-numeric length argument -- %s\n", argv[optind]);
@@ -517,22 +672,25 @@ write_f(int argc, char **argv)
        gettimeofday(&t1, NULL);
        if (pflag)
                cnt = do_pwrite(buf, offset, count, &total);
+       else if (bflag)
+               cnt = do_save_vmstate(buf, offset, count, &total);
        else
                cnt = do_write(buf, offset, count, &total);
        gettimeofday(&t2, NULL);
 
        if (cnt < 0) {
                printf("write failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
        /* Finally, report back -- -C gives a parsable format */
        t2 = tsub(t2, t1);
        print_report("wrote", &t2, offset, count, total, cnt, Cflag);
 
+out:
        qemu_io_free(buf);
 
        return 0;
@@ -544,7 +702,7 @@ static const cmdinfo_t write_cmd = {
        .cfunc          = write_f,
        .argmin         = 2,
        .argmax         = -1,
-       .args           = "[-aCpq] [-P pattern ] off len",
+       .args           = "[-abCpq] [-P pattern ] off len",
        .oneline        = "writes a number of bytes at a specified offset",
        .help           = write_help,
 };
@@ -575,10 +733,10 @@ writev_f(int argc, char **argv)
        struct timeval t1, t2;
        int Cflag = 0, qflag = 0;
        int c, cnt;
-       char *buf, *p;
+       char *buf;
        int64_t offset;
-       int count = 0, total;
-       int nr_iov, i;
+       int total;
+       int nr_iov;
        int pattern = 0xcd;
        QEMUIOVector qiov;
 
@@ -591,7 +749,9 @@ writev_f(int argc, char **argv)
                        qflag = 1;
                        break;
                case 'P':
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                default:
                        return command_usage(&writev_cmd);
@@ -614,41 +774,8 @@ writev_f(int argc, char **argv)
                return 0;
        }
 
-       if (count & 0x1ff) {
-               printf("count %d is not sector aligned\n",
-                       count);
-               return 0;
-       }
-
-
-       for (i = optind; i < argc; i++) {
-               size_t len;
-
-               len = cvtnum(argv[optind]);
-               if (len < 0) {
-                       printf("non-numeric length argument -- %s\n", argv[i]);
-                       return 0;
-               }
-               count += len;
-       }
-
        nr_iov = argc - optind;
-       qemu_iovec_init(&qiov, nr_iov);
-       buf = p = qemu_io_alloc(count, pattern);
-       for (i = 0; i < nr_iov; i++) {
-               size_t len;
-
-               len = cvtnum(argv[optind]);
-               if (len < 0) {
-                       printf("non-numeric length argument -- %s\n",
-                               argv[optind]);
-                       return 0;
-               }
-
-               qemu_iovec_add(&qiov, p, len);
-               p += len;
-               optind++;
-       }
+       buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
 
        gettimeofday(&t1, NULL);
        cnt = do_aio_writev(&qiov, offset, &total);
@@ -656,18 +783,17 @@ writev_f(int argc, char **argv)
 
        if (cnt < 0) {
                printf("writev failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
        /* Finally, report back -- -C gives a parsable format */
        t2 = tsub(t2, t1);
        print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
-
+out:
        qemu_io_free(buf);
-
        return 0;
 }
 
@@ -681,6 +807,294 @@ static const cmdinfo_t writev_cmd = {
        .help           = writev_help,
 };
 
+struct aio_ctx {
+       QEMUIOVector qiov;
+       int64_t offset;
+       char *buf;
+       int qflag;
+       int vflag;
+       int Cflag;
+       int Pflag;
+       int pattern;
+       struct timeval t1;
+};
+
+static void
+aio_write_done(void *opaque, int ret)
+{
+       struct aio_ctx *ctx = opaque;
+       struct timeval t2;
+
+       gettimeofday(&t2, NULL);
+
+
+       if (ret < 0) {
+               printf("aio_write failed: %s\n", strerror(-ret));
+               goto out;
+       }
+
+       if (ctx->qflag) {
+               goto out;
+       }
+
+       /* Finally, report back -- -C gives a parsable format */
+       t2 = tsub(t2, ctx->t1);
+       print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
+                    ctx->qiov.size, 1, ctx->Cflag);
+out:
+       qemu_io_free(ctx->buf);
+       free(ctx);
+}
+
+static const cmdinfo_t aio_read_cmd;
+
+static void
+aio_read_done(void *opaque, int ret)
+{
+       struct aio_ctx *ctx = opaque;
+       struct timeval t2;
+
+       gettimeofday(&t2, NULL);
+
+       if (ret < 0) {
+               printf("readv failed: %s\n", strerror(-ret));
+               goto out;
+       }
+
+       if (ctx->Pflag) {
+               void *cmp_buf = malloc(ctx->qiov.size);
+
+               memset(cmp_buf, ctx->pattern, ctx->qiov.size);
+               if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
+                       printf("Pattern verification failed at offset %lld, "
+                               "%zd bytes\n",
+                               (long long) ctx->offset, ctx->qiov.size);
+               }
+               free(cmp_buf);
+       }
+
+       if (ctx->qflag) {
+               goto out;
+       }
+
+       if (ctx->vflag) {
+               dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
+       }
+
+       /* Finally, report back -- -C gives a parsable format */
+       t2 = tsub(t2, ctx->t1);
+       print_report("read", &t2, ctx->offset, ctx->qiov.size,
+                    ctx->qiov.size, 1, ctx->Cflag);
+out:
+       qemu_io_free(ctx->buf);
+       free(ctx);
+}
+
+static void
+aio_read_help(void)
+{
+       printf(
+"\n"
+" asynchronously reads a range of bytes from the given offset\n"
+"\n"
+" Example:\n"
+" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
+"\n"
+" Reads a segment of the currently open file, optionally dumping it to the\n"
+" standard output stream (with -v option) for subsequent inspection.\n"
+" The read is performed asynchronously and should the aio_flush command \n"
+" should be used to ensure all outstanding aio requests have been completed\n"
+" -C, -- report statistics in a machine parsable format\n"
+" -P, -- use a pattern to verify read data\n"
+" -v, -- dump buffer to standard output\n"
+" -q, -- quite mode, do not show I/O statistics\n"
+"\n");
+}
+
+static int
+aio_read_f(int argc, char **argv)
+{
+       int nr_iov, c;
+       struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
+       BlockDriverAIOCB *acb;
+
+       while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
+               switch (c) {
+               case 'C':
+                       ctx->Cflag = 1;
+                       break;
+               case 'P':
+                       ctx->Pflag = 1;
+                       ctx->pattern = parse_pattern(optarg);
+                       if (ctx->pattern < 0)
+                               return 0;
+                       break;
+               case 'q':
+                       ctx->qflag = 1;
+                       break;
+               case 'v':
+                       ctx->vflag = 1;
+                       break;
+               default:
+                       free(ctx);
+                       return command_usage(&aio_read_cmd);
+               }
+       }
+
+       if (optind > argc - 2) {
+               free(ctx);
+               return command_usage(&aio_read_cmd);
+       }
+
+       ctx->offset = cvtnum(argv[optind]);
+       if (ctx->offset < 0) {
+               printf("non-numeric length argument -- %s\n", argv[optind]);
+               free(ctx);
+               return 0;
+       }
+       optind++;
+
+       if (ctx->offset & 0x1ff) {
+               printf("offset %lld is not sector aligned\n",
+                       (long long)ctx->offset);
+               free(ctx);
+               return 0;
+       }
+
+       nr_iov = argc - optind;
+       ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
+
+       gettimeofday(&ctx->t1, NULL);
+       acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
+                             ctx->qiov.size >> 9, aio_read_done, ctx);
+       if (!acb) {
+               free(ctx->buf);
+               free(ctx);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+static const cmdinfo_t aio_read_cmd = {
+       .name           = "aio_read",
+       .cfunc          = aio_read_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cqv] [-P pattern ] off len [len..]",
+       .oneline        = "asynchronously reads a number of bytes",
+       .help           = aio_read_help,
+};
+
+static const cmdinfo_t aio_write_cmd;
+
+static void
+aio_write_help(void)
+{
+       printf(
+"\n"
+" asynchronously writes a range of bytes from the given offset source \n"
+" from multiple buffers\n"
+"\n"
+" Example:\n"
+" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
+"\n"
+" Writes into a segment of the currently open file, using a buffer\n"
+" filled with a set pattern (0xcdcdcdcd).\n"
+" The write is performed asynchronously and should the aio_flush command \n"
+" should be used to ensure all outstanding aio requests have been completed\n"
+" -P, -- use different pattern to fill file\n"
+" -C, -- report statistics in a machine parsable format\n"
+" -q, -- quite mode, do not show I/O statistics\n"
+"\n");
+}
+
+
+static int
+aio_write_f(int argc, char **argv)
+{
+       int nr_iov, c;
+       int pattern = 0xcd;
+       struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
+       BlockDriverAIOCB *acb;
+
+       while ((c = getopt(argc, argv, "CqP:")) != EOF) {
+               switch (c) {
+               case 'C':
+                       ctx->Cflag = 1;
+                       break;
+               case 'q':
+                       ctx->qflag = 1;
+                       break;
+               case 'P':
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
+                       break;
+               default:
+                       free(ctx);
+                       return command_usage(&aio_write_cmd);
+               }
+       }
+
+       if (optind > argc - 2) {
+               free(ctx);
+               return command_usage(&aio_write_cmd);
+       }
+
+       ctx->offset = cvtnum(argv[optind]);
+       if (ctx->offset < 0) {
+               printf("non-numeric length argument -- %s\n", argv[optind]);
+               free(ctx);
+               return 0;
+       }
+       optind++;
+
+       if (ctx->offset & 0x1ff) {
+               printf("offset %lld is not sector aligned\n",
+                       (long long)ctx->offset);
+               free(ctx);
+               return 0;
+       }
+
+       nr_iov = argc - optind;
+       ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
+
+       gettimeofday(&ctx->t1, NULL);
+       acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
+                             ctx->qiov.size >> 9, aio_write_done, ctx);
+       if (!acb) {
+               free(ctx->buf);
+               free(ctx);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+static const cmdinfo_t aio_write_cmd = {
+       .name           = "aio_write",
+       .cfunc          = aio_write_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cq] [-P pattern ] off len [len..]",
+       .oneline        = "asynchronously writes a number of bytes",
+       .help           = aio_write_help,
+};
+
+static int
+aio_flush_f(int argc, char **argv)
+{
+       qemu_aio_flush();
+       return 0;
+}
+
+static const cmdinfo_t aio_flush_cmd = {
+       .name           = "aio_flush",
+       .cfunc          = aio_flush_f,
+       .oneline        = "completes all outstanding aio requets"
+};
+
 static int
 flush_f(int argc, char **argv)
 {
@@ -790,9 +1204,9 @@ static int
 alloc_f(int argc, char **argv)
 {
        int64_t offset;
-       int nb_sectors;
+       int nb_sectors, remaining;
        char s1[64];
-       int num;
+       int num, sum_alloc;
        int ret;
 
        offset = cvtnum(argv[1]);
@@ -807,10 +1221,14 @@ alloc_f(int argc, char **argv)
        else
                nb_sectors = 1;
 
-       ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
-       if (ret) {
-               printf("is_allocated: %s", strerror(ret));
-               return 0;
+       remaining = nb_sectors;
+       sum_alloc = 0;
+       while (remaining) {
+               ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
+               remaining -= num;
+               if (ret) {
+                       sum_alloc += num;
+               }
        }
 
        cvtstr(offset, s1, sizeof(s1));
@@ -819,7 +1237,7 @@ alloc_f(int argc, char **argv)
                printf("sector allocated at offset %s\n", s1);
        else
                printf("%d/%d sectors allocated at offset %s\n",
-                       num, nb_sectors, s1);
+                       sum_alloc, nb_sectors, s1);
        return 0;
 }
 
@@ -848,7 +1266,7 @@ static const cmdinfo_t close_cmd = {
        .oneline        = "close the current open file",
 };
 
-static int openfile(char *name, int flags)
+static int openfile(char *name, int flags, int growable)
 {
        if (bs) {
                fprintf(stderr, "file open already, try 'help close'\n");
@@ -859,12 +1277,19 @@ static int openfile(char *name, int flags)
        if (!bs)
                return 1;
 
+       if (growable) {
+               flags |= BDRV_O_FILE;
+       }
+
        if (bdrv_open(bs, name, flags) == -1) {
                fprintf(stderr, "%s: can't open device %s\n", progname, name);
                bs = NULL;
                return 1;
        }
 
+       if (growable) {
+               bs->growable = 1;
+       }
        return 0;
 }
 
@@ -883,6 +1308,7 @@ open_help(void)
 " -r, -- open file read-only\n"
 " -s, -- use snapshot file\n"
 " -n, -- disable host cache\n"
+" -g, -- allow file to grow (only applies to protocols)"
 "\n");
 }
 
@@ -893,9 +1319,10 @@ open_f(int argc, char **argv)
 {
        int flags = 0;
        int readonly = 0;
+       int growable = 0;
        int c;
 
-       while ((c = getopt(argc, argv, "snCr")) != EOF) {
+       while ((c = getopt(argc, argv, "snCrg")) != EOF) {
                switch (c) {
                case 's':
                        flags |= BDRV_O_SNAPSHOT;
@@ -909,6 +1336,9 @@ open_f(int argc, char **argv)
                case 'r':
                        readonly = 1;
                        break;
+               case 'g':
+                       growable = 1;
+                       break;
                default:
                        return command_usage(&open_cmd);
                }
@@ -922,7 +1352,7 @@ open_f(int argc, char **argv)
        if (optind != argc - 1)
                return command_usage(&open_cmd);
 
-       return openfile(argv[optind], flags);
+       return openfile(argv[optind], flags, growable);
 }
 
 static const cmdinfo_t open_cmd = {
@@ -964,14 +1394,16 @@ static void usage(const char *name)
 {
        printf(
 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
-"QEMU Disk excerciser\n"
+"QEMU Disk exerciser\n"
 "\n"
 "  -C, --create         create new file if it doesn't exist\n"
 "  -c, --cmd            command to execute\n"
 "  -r, --read-only      export read-only\n"
 "  -s, --snapshot       use snapshot file\n"
 "  -n, --nocache        disable host cache\n"
+"  -g, --growable       allow file to grow (only applies to protocols)\n"
 "  -m, --misalign       misalign allocations for O_DIRECT\n"
+"  -k, --native-aio     use kernel AIO implementation (on Linux only)\n"
 "  -h, --help           display this help and exit\n"
 "  -V, --version        output version information and exit\n"
 "\n",
@@ -982,18 +1414,21 @@ static void usage(const char *name)
 int main(int argc, char **argv)
 {
        int readonly = 0;
-       const char *sopt = "hVc:Crsnm";
+       int growable = 0;
+       const char *sopt = "hVc:Crsnmgk";
        struct option lopt[] = {
-               { "help", 0, 0, 'h' },
-               { "version", 0, 0, 'V' },
-               { "offset", 1, 0, 'o' },
-               { "cmd", 1, 0, 'c' },
-               { "create", 0, 0, 'C' },
-               { "read-only", 0, 0, 'r' },
-               { "snapshot", 0, 0, 's' },
-               { "nocache", 0, 0, 'n' },
-               { "misalign", 0, 0, 'm' },
-               { NULL, 0, 0, 0 }
+               { "help", 0, NULL, 'h' },
+               { "version", 0, NULL, 'V' },
+               { "offset", 1, NULL, 'o' },
+               { "cmd", 1, NULL, 'c' },
+               { "create", 0, NULL, 'C' },
+               { "read-only", 0, NULL, 'r' },
+               { "snapshot", 0, NULL, 's' },
+               { "nocache", 0, NULL, 'n' },
+               { "misalign", 0, NULL, 'm' },
+               { "growable", 0, NULL, 'g' },
+               { "native-aio", 0, NULL, 'k' },
+               { NULL, 0, NULL, 0 }
        };
        int c;
        int opt_index = 0;
@@ -1021,6 +1456,12 @@ int main(int argc, char **argv)
                case 'm':
                        misalign = 1;
                        break;
+               case 'g':
+                       growable = 1;
+                       break;
+               case 'k':
+                       flags |= BDRV_O_NATIVE_AIO;
+                       break;
                case 'V':
                        printf("%s version %s\n", progname, VERSION);
                        exit(0);
@@ -1049,6 +1490,9 @@ int main(int argc, char **argv)
        add_command(&readv_cmd);
        add_command(&write_cmd);
        add_command(&writev_cmd);
+       add_command(&aio_read_cmd);
+       add_command(&aio_write_cmd);
+       add_command(&aio_flush_cmd);
        add_command(&flush_cmd);
        add_command(&truncate_cmd);
        add_command(&length_cmd);
@@ -1065,9 +1509,14 @@ int main(int argc, char **argv)
                flags |= BDRV_O_RDWR;
 
        if ((argc - optind) == 1)
-               openfile(argv[optind], flags);
+               openfile(argv[optind], flags, growable);
        command_loop();
 
+       /*
+        * Make sure all outstanding requests get flushed the program exits.
+        */
+       qemu_aio_flush();
+
        if (bs)
                bdrv_close(bs);
        return 0;