thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
34b321
From 9bd448e0f256ac9327363a0f370b9616d967e986 Mon Sep 17 00:00:00 2001
34b321
From: John Snow <jsnow@redhat.com>
34b321
Date: Mon, 23 Nov 2015 17:38:25 +0100
34b321
Subject: [PATCH 06/27] qemu-io: Split off commands to qemu-io-cmds.c
34b321
34b321
RH-Author: John Snow <jsnow@redhat.com>
34b321
Message-id: <1448300320-7772-7-git-send-email-jsnow@redhat.com>
34b321
Patchwork-id: 68434
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 06/21] qemu-io: Split off commands to qemu-io-cmds.c
34b321
Bugzilla: 1272523
34b321
RH-Acked-by: Thomas Huth <thuth@redhat.com>
34b321
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
34b321
RH-Acked-by: Max Reitz <mreitz@redhat.com>
34b321
34b321
From: Kevin Wolf <kwolf@redhat.com>
34b321
34b321
This is the implementation of all qemu-io commands that make sense to be
34b321
called from the qemu monitor, i.e. everything except open, close and
34b321
quit.
34b321
34b321
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
34b321
Reviewed-by: Eric Blake <eblake@redhat.com>
34b321
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
34b321
(cherry picked from commit 797ac58cb2093ab9192d8998a1fef85d87cc8661)
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
34b321
Conflicts:
34b321
  qemu-io.c: Minor differences, but as this is code motion,
34b321
             The code motion had to be re-done to preserve
34b321
             any backported changes.
34b321
  qemu-io-cmds.c: re-created from downstream qemu-io.c.
34b321
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
---
34b321
 Makefile       |    2 +-
34b321
 qemu-io-cmds.c | 1853 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34b321
 qemu-io.c      | 1831 +------------------------------------------------------
34b321
 3 files changed, 1856 insertions(+), 1830 deletions(-)
34b321
 create mode 100644 qemu-io-cmds.c
34b321
34b321
diff --git a/Makefile b/Makefile
34b321
index 8407945..f28ce29 100644
34b321
--- a/Makefile
34b321
+++ b/Makefile
34b321
@@ -205,7 +205,7 @@ qemu-img.o: qemu-img-cmds.h
34b321
 
34b321
 qemu-img$(EXESUF): qemu-img.o $(block-obj-y) libqemuutil.a libqemustub.a
34b321
 qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) libqemuutil.a libqemustub.a
34b321
-qemu-io$(EXESUF): qemu-io.o cmd.o $(block-obj-y) libqemuutil.a libqemustub.a
34b321
+qemu-io$(EXESUF): qemu-io.o qemu-io-cmds.o cmd.o $(block-obj-y) libqemuutil.a libqemustub.a
34b321
 
34b321
 qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
34b321
 
34b321
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
34b321
new file mode 100644
34b321
index 0000000..efa6070
34b321
--- /dev/null
34b321
+++ b/qemu-io-cmds.c
34b321
@@ -0,0 +1,1853 @@
34b321
+/*
34b321
+ * Command line utility to exercise the QEMU I/O path.
34b321
+ *
34b321
+ * Copyright (C) 2009 Red Hat, Inc.
34b321
+ * Copyright (c) 2003-2005 Silicon Graphics, Inc.
34b321
+ *
34b321
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
34b321
+ * See the COPYING file in the top-level directory.
34b321
+ */
34b321
+
34b321
+#include "qemu-common.h"
34b321
+#include "block/block_int.h"
34b321
+#include "block/qapi.h"
34b321
+#include "cmd.h"
34b321
+
34b321
+#define CMD_NOFILE_OK   0x01
34b321
+
34b321
+int qemuio_misalign;
34b321
+
34b321
+static int64_t cvtnum(const char *s)
34b321
+{
34b321
+    char *end;
34b321
+    return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
34b321
+}
34b321
+
34b321
+/*
34b321
+ * Parse the pattern argument to various sub-commands.
34b321
+ *
34b321
+ * Because the pattern is used as an argument to memset it must evaluate
34b321
+ * to an unsigned integer that fits into a single byte.
34b321
+ */
34b321
+static int parse_pattern(const char *arg)
34b321
+{
34b321
+    char *endptr = NULL;
34b321
+    long pattern;
34b321
+
34b321
+    pattern = strtol(arg, &endptr, 0);
34b321
+    if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
34b321
+        printf("%s is not a valid pattern byte\n", arg);
34b321
+        return -1;
34b321
+    }
34b321
+
34b321
+    return pattern;
34b321
+}
34b321
+
34b321
+/*
34b321
+ * Memory allocation helpers.
34b321
+ *
34b321
+ * Make sure memory is aligned by default, or purposefully misaligned if
34b321
+ * that is specified on the command line.
34b321
+ */
34b321
+
34b321
+#define MISALIGN_OFFSET     16
34b321
+static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
34b321
+{
34b321
+    void *buf;
34b321
+
34b321
+    if (qemuio_misalign) {
34b321
+        len += MISALIGN_OFFSET;
34b321
+    }
34b321
+    buf = qemu_blockalign(bs, len);
34b321
+    memset(buf, pattern, len);
34b321
+    if (qemuio_misalign) {
34b321
+        buf += MISALIGN_OFFSET;
34b321
+    }
34b321
+    return buf;
34b321
+}
34b321
+
34b321
+static void qemu_io_free(void *p)
34b321
+{
34b321
+    if (qemuio_misalign) {
34b321
+        p -= MISALIGN_OFFSET;
34b321
+    }
34b321
+    qemu_vfree(p);
34b321
+}
34b321
+
34b321
+static void dump_buffer(const void *buffer, int64_t offset, int len)
34b321
+{
34b321
+    int i, j;
34b321
+    const uint8_t *p;
34b321
+
34b321
+    for (i = 0, p = buffer; i < len; i += 16) {
34b321
+        const uint8_t *s = p;
34b321
+
34b321
+        printf("%08" PRIx64 ":  ", offset + i);
34b321
+        for (j = 0; j < 16 && i + j < len; j++, p++) {
34b321
+            printf("%02x ", *p);
34b321
+        }
34b321
+        printf(" ");
34b321
+        for (j = 0; j < 16 && i + j < len; j++, s++) {
34b321
+            if (isalnum(*s)) {
34b321
+                printf("%c", *s);
34b321
+            } else {
34b321
+                printf(".");
34b321
+            }
34b321
+        }
34b321
+        printf("\n");
34b321
+    }
34b321
+}
34b321
+
34b321
+static void print_report(const char *op, struct timeval *t, int64_t offset,
34b321
+                         int count, int total, int cnt, int Cflag)
34b321
+{
34b321
+    char s1[64], s2[64], ts[64];
34b321
+
34b321
+    timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
34b321
+    if (!Cflag) {
34b321
+        cvtstr((double)total, s1, sizeof(s1));
34b321
+        cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
34b321
+        printf("%s %d/%d bytes at offset %" PRId64 "\n",
34b321
+               op, total, count, offset);
34b321
+        printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
34b321
+               s1, cnt, ts, s2, tdiv((double)cnt, *t));
34b321
+    } else {/* bytes,ops,time,bytes/sec,ops/sec */
34b321
+        printf("%d,%d,%s,%.3f,%.3f\n",
34b321
+            total, cnt, ts,
34b321
+            tdiv((double)total, *t),
34b321
+            tdiv((double)cnt, *t));
34b321
+    }
34b321
+}
34b321
+
34b321
+/*
34b321
+ * Parse multiple length statements for vectored I/O, and construct an I/O
34b321
+ * vector matching it.
34b321
+ */
34b321
+static void *
34b321
+create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
34b321
+             int pattern)
34b321
+{
34b321
+    size_t *sizes = g_new0(size_t, nr_iov);
34b321
+    size_t count = 0;
34b321
+    void *buf = NULL;
34b321
+    void *p;
34b321
+    int i;
34b321
+
34b321
+    for (i = 0; i < nr_iov; i++) {
34b321
+        char *arg = argv[i];
34b321
+        int64_t len;
34b321
+
34b321
+        len = cvtnum(arg);
34b321
+        if (len < 0) {
34b321
+            printf("non-numeric length argument -- %s\n", arg);
34b321
+            goto fail;
34b321
+        }
34b321
+
34b321
+        /* should be SIZE_T_MAX, but that doesn't exist */
34b321
+        if (len > INT_MAX) {
34b321
+            printf("too large length argument -- %s\n", arg);
34b321
+            goto fail;
34b321
+        }
34b321
+
34b321
+        if (len & 0x1ff) {
34b321
+            printf("length argument %" PRId64
34b321
+                   " is not sector aligned\n", len);
34b321
+            goto fail;
34b321
+        }
34b321
+
34b321
+        sizes[i] = len;
34b321
+        count += len;
34b321
+    }
34b321
+
34b321
+    qemu_iovec_init(qiov, nr_iov);
34b321
+
34b321
+    buf = p = qemu_io_alloc(bs, count, pattern);
34b321
+
34b321
+    for (i = 0; i < nr_iov; i++) {
34b321
+        qemu_iovec_add(qiov, p, sizes[i]);
34b321
+        p += sizes[i];
34b321
+    }
34b321
+
34b321
+fail:
34b321
+    g_free(sizes);
34b321
+    return buf;
34b321
+}
34b321
+
34b321
+static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
+                   int *total)
34b321
+{
34b321
+    int ret;
34b321
+
34b321
+    ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
+    if (ret < 0) {
34b321
+        return ret;
34b321
+    }
34b321
+    *total = count;
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
+                    int *total)
34b321
+{
34b321
+    int ret;
34b321
+
34b321
+    ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
+    if (ret < 0) {
34b321
+        return ret;
34b321
+    }
34b321
+    *total = count;
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
+                    int *total)
34b321
+{
34b321
+    *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
34b321
+    if (*total < 0) {
34b321
+        return *total;
34b321
+    }
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
+                     int *total)
34b321
+{
34b321
+    *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
34b321
+    if (*total < 0) {
34b321
+        return *total;
34b321
+    }
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+typedef struct {
34b321
+    BlockDriverState *bs;
34b321
+    int64_t offset;
34b321
+    int count;
34b321
+    int *total;
34b321
+    int ret;
34b321
+    bool done;
34b321
+} CoWriteZeroes;
34b321
+
34b321
+static void coroutine_fn co_write_zeroes_entry(void *opaque)
34b321
+{
34b321
+    CoWriteZeroes *data = opaque;
34b321
+
34b321
+    data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
34b321
+                                     data->count / BDRV_SECTOR_SIZE, 0);
34b321
+    data->done = true;
34b321
+    if (data->ret < 0) {
34b321
+        *data->total = data->ret;
34b321
+        return;
34b321
+    }
34b321
+
34b321
+    *data->total = data->count;
34b321
+}
34b321
+
34b321
+static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
34b321
+                              int *total)
34b321
+{
34b321
+    Coroutine *co;
34b321
+    CoWriteZeroes data = {
34b321
+        .bs     = bs,
34b321
+        .offset = offset,
34b321
+        .count  = count,
34b321
+        .total  = total,
34b321
+        .done   = false,
34b321
+    };
34b321
+
34b321
+    co = qemu_coroutine_create(co_write_zeroes_entry);
34b321
+    qemu_coroutine_enter(co, &data);
34b321
+    while (!data.done) {
34b321
+        qemu_aio_wait();
34b321
+    }
34b321
+    if (data.ret < 0) {
34b321
+        return data.ret;
34b321
+    } else {
34b321
+        return 1;
34b321
+    }
34b321
+}
34b321
+
34b321
+static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                               int count, int *total)
34b321
+{
34b321
+    int ret;
34b321
+
34b321
+    ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
+    if (ret < 0) {
34b321
+        return ret;
34b321
+    }
34b321
+    *total = count;
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                           int count, int *total)
34b321
+{
34b321
+    *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
34b321
+    if (*total < 0) {
34b321
+        return *total;
34b321
+    }
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
+                           int count, int *total)
34b321
+{
34b321
+    *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
34b321
+    if (*total < 0) {
34b321
+        return *total;
34b321
+    }
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+#define NOT_DONE 0x7fffffff
34b321
+static void aio_rw_done(void *opaque, int ret)
34b321
+{
34b321
+    *(int *)opaque = ret;
34b321
+}
34b321
+
34b321
+static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
34b321
+                        int64_t offset, int *total)
34b321
+{
34b321
+    int async_ret = NOT_DONE;
34b321
+
34b321
+    bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
34b321
+                   aio_rw_done, &async_ret);
34b321
+    while (async_ret == NOT_DONE) {
34b321
+        main_loop_wait(false);
34b321
+    }
34b321
+
34b321
+    *total = qiov->size;
34b321
+    return async_ret < 0 ? async_ret : 1;
34b321
+}
34b321
+
34b321
+static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
34b321
+                         int64_t offset, int *total)
34b321
+{
34b321
+    int async_ret = NOT_DONE;
34b321
+
34b321
+    bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
34b321
+                    aio_rw_done, &async_ret);
34b321
+    while (async_ret == NOT_DONE) {
34b321
+        main_loop_wait(false);
34b321
+    }
34b321
+
34b321
+    *total = qiov->size;
34b321
+    return async_ret < 0 ? async_ret : 1;
34b321
+}
34b321
+
34b321
+struct multiwrite_async_ret {
34b321
+    int num_done;
34b321
+    int error;
34b321
+};
34b321
+
34b321
+static void multiwrite_cb(void *opaque, int ret)
34b321
+{
34b321
+    struct multiwrite_async_ret *async_ret = opaque;
34b321
+
34b321
+    async_ret->num_done++;
34b321
+    if (ret < 0) {
34b321
+        async_ret->error = ret;
34b321
+    }
34b321
+}
34b321
+
34b321
+static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
34b321
+                             int num_reqs, int *total)
34b321
+{
34b321
+    int i, ret;
34b321
+    struct multiwrite_async_ret async_ret = {
34b321
+        .num_done = 0,
34b321
+        .error = 0,
34b321
+    };
34b321
+
34b321
+    *total = 0;
34b321
+    for (i = 0; i < num_reqs; i++) {
34b321
+        reqs[i].cb = multiwrite_cb;
34b321
+        reqs[i].opaque = &async_ret;
34b321
+        *total += reqs[i].qiov->size;
34b321
+    }
34b321
+
34b321
+    ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
34b321
+    if (ret < 0) {
34b321
+        return ret;
34b321
+    }
34b321
+
34b321
+    while (async_ret.num_done < num_reqs) {
34b321
+        main_loop_wait(false);
34b321
+    }
34b321
+
34b321
+    return async_ret.error < 0 ? async_ret.error : 1;
34b321
+}
34b321
+
34b321
+static void read_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" reads a range of bytes from the given offset\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
34b321
+"\n"
34b321
+" Reads a segment of the currently open file, optionally dumping it to the\n"
34b321
+" standard output stream (with -v option) for subsequent inspection.\n"
34b321
+" -b, -- read from the VM state rather than the virtual disk\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -l, -- length for pattern verification (only with -P)\n"
34b321
+" -p, -- use bdrv_pread to read the file\n"
34b321
+" -P, -- use a pattern to verify read data\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+" -s, -- start offset for pattern verification (only with -P)\n"
34b321
+" -v, -- dump buffer to standard output\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int read_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t read_cmd = {
34b321
+    .name       = "read",
34b321
+    .altname    = "r",
34b321
+    .cfunc      = read_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
34b321
+    .oneline    = "reads a number of bytes at a specified offset",
34b321
+    .help       = read_help,
34b321
+};
34b321
+
34b321
+static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    struct timeval t1, t2;
34b321
+    int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
34b321
+    int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
34b321
+    int c, cnt;
34b321
+    char *buf;
34b321
+    int64_t offset;
34b321
+    int count;
34b321
+    /* Some compilers get confused and warn if this is not initialized.  */
34b321
+    int total = 0;
34b321
+    int pattern = 0, pattern_offset = 0, pattern_count = 0;
34b321
+
34b321
+    while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'b':
34b321
+            bflag = 1;
34b321
+            break;
34b321
+        case 'C':
34b321
+            Cflag = 1;
34b321
+            break;
34b321
+        case 'l':
34b321
+            lflag = 1;
34b321
+            pattern_count = cvtnum(optarg);
34b321
+            if (pattern_count < 0) {
34b321
+                printf("non-numeric length argument -- %s\n", optarg);
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        case 'p':
34b321
+            pflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            Pflag = 1;
34b321
+            pattern = parse_pattern(optarg);
34b321
+            if (pattern < 0) {
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        case 'q':
34b321
+            qflag = 1;
34b321
+            break;
34b321
+        case 's':
34b321
+            sflag = 1;
34b321
+            pattern_offset = cvtnum(optarg);
34b321
+            if (pattern_offset < 0) {
34b321
+                printf("non-numeric length argument -- %s\n", optarg);
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        case 'v':
34b321
+            vflag = 1;
34b321
+            break;
34b321
+        default:
34b321
+            return command_usage(&read_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind != argc - 2) {
34b321
+        return command_usage(&read_cmd);
34b321
+    }
34b321
+
34b321
+    if (bflag && pflag) {
34b321
+        printf("-b and -p cannot be specified at the same time\n");
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    offset = cvtnum(argv[optind]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    optind++;
34b321
+    count = cvtnum(argv[optind]);
34b321
+    if (count < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    if (!Pflag && (lflag || sflag)) {
34b321
+        return command_usage(&read_cmd);
34b321
+    }
34b321
+
34b321
+    if (!lflag) {
34b321
+        pattern_count = count - pattern_offset;
34b321
+    }
34b321
+
34b321
+    if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
34b321
+        printf("pattern verification range exceeds end of read data\n");
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    if (!pflag) {
34b321
+        if (offset & 0x1ff) {
34b321
+            printf("offset %" PRId64 " is not sector aligned\n",
34b321
+                   offset);
34b321
+            return 0;
34b321
+        }
34b321
+        if (count & 0x1ff) {
34b321
+            printf("count %d is not sector aligned\n",
34b321
+                   count);
34b321
+            return 0;
34b321
+        }
34b321
+    }
34b321
+
34b321
+    buf = qemu_io_alloc(bs, count, 0xab);
34b321
+
34b321
+    gettimeofday(&t1, NULL);
34b321
+    if (pflag) {
34b321
+        cnt = do_pread(bs, buf, offset, count, &total);
34b321
+    } else if (bflag) {
34b321
+        cnt = do_load_vmstate(bs, buf, offset, count, &total);
34b321
+    } else {
34b321
+        cnt = do_read(bs, buf, offset, count, &total);
34b321
+    }
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (cnt < 0) {
34b321
+        printf("read failed: %s\n", strerror(-cnt));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (Pflag) {
34b321
+        void *cmp_buf = g_malloc(pattern_count);
34b321
+        memset(cmp_buf, pattern, pattern_count);
34b321
+        if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
34b321
+            printf("Pattern verification failed at offset %"
34b321
+                   PRId64 ", %d bytes\n",
34b321
+                   offset + pattern_offset, pattern_count);
34b321
+        }
34b321
+        g_free(cmp_buf);
34b321
+    }
34b321
+
34b321
+    if (qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (vflag) {
34b321
+        dump_buffer(buf, offset, count);
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, t1);
34b321
+    print_report("read", &t2, offset, count, total, cnt, Cflag);
34b321
+
34b321
+out:
34b321
+    qemu_io_free(buf);
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static void readv_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" reads a range of bytes from the given offset into multiple buffers\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
34b321
+"\n"
34b321
+" Reads a segment of the currently open file, optionally dumping it to the\n"
34b321
+" standard output stream (with -v option) for subsequent inspection.\n"
34b321
+" Uses multiple iovec buffers if more than one byte range is specified.\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -P, -- use a pattern to verify read data\n"
34b321
+" -v, -- dump buffer to standard output\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int readv_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t readv_cmd = {
34b321
+    .name       = "readv",
34b321
+    .cfunc      = readv_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-Cqv] [-P pattern ] off len [len..]",
34b321
+    .oneline    = "reads a number of bytes at a specified offset",
34b321
+    .help       = readv_help,
34b321
+};
34b321
+
34b321
+static int readv_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    struct timeval t1, t2;
34b321
+    int Cflag = 0, qflag = 0, vflag = 0;
34b321
+    int c, cnt;
34b321
+    char *buf;
34b321
+    int64_t offset;
34b321
+    /* Some compilers get confused and warn if this is not initialized.  */
34b321
+    int total = 0;
34b321
+    int nr_iov;
34b321
+    QEMUIOVector qiov;
34b321
+    int pattern = 0;
34b321
+    int Pflag = 0;
34b321
+
34b321
+    while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'C':
34b321
+            Cflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            Pflag = 1;
34b321
+            pattern = parse_pattern(optarg);
34b321
+            if (pattern < 0) {
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        case 'q':
34b321
+            qflag = 1;
34b321
+            break;
34b321
+        case 'v':
34b321
+            vflag = 1;
34b321
+            break;
34b321
+        default:
34b321
+            return command_usage(&readv_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind > argc - 2) {
34b321
+        return command_usage(&readv_cmd);
34b321
+    }
34b321
+
34b321
+
34b321
+    offset = cvtnum(argv[optind]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+    optind++;
34b321
+
34b321
+    if (offset & 0x1ff) {
34b321
+        printf("offset %" PRId64 " is not sector aligned\n",
34b321
+               offset);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    nr_iov = argc - optind;
34b321
+    buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
34b321
+    if (buf == NULL) {
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    gettimeofday(&t1, NULL);
34b321
+    cnt = do_aio_readv(bs, &qiov, offset, &total);
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (cnt < 0) {
34b321
+        printf("readv failed: %s\n", strerror(-cnt));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (Pflag) {
34b321
+        void *cmp_buf = g_malloc(qiov.size);
34b321
+        memset(cmp_buf, pattern, qiov.size);
34b321
+        if (memcmp(buf, cmp_buf, qiov.size)) {
34b321
+            printf("Pattern verification failed at offset %"
34b321
+                   PRId64 ", %zd bytes\n", offset, qiov.size);
34b321
+        }
34b321
+        g_free(cmp_buf);
34b321
+    }
34b321
+
34b321
+    if (qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (vflag) {
34b321
+        dump_buffer(buf, offset, qiov.size);
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, t1);
34b321
+    print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
34b321
+
34b321
+out:
34b321
+    qemu_iovec_destroy(&qiov);
34b321
+    qemu_io_free(buf);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static void write_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" writes a range of bytes from the given offset\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
34b321
+"\n"
34b321
+" Writes into a segment of the currently open file, using a buffer\n"
34b321
+" filled with a set pattern (0xcdcdcdcd).\n"
34b321
+" -b, -- write to the VM state rather than the virtual disk\n"
34b321
+" -c, -- write compressed data with bdrv_write_compressed\n"
34b321
+" -p, -- use bdrv_pwrite to write the file\n"
34b321
+" -P, -- use different pattern to fill file\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+" -z, -- write zeroes using bdrv_co_write_zeroes\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int write_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t write_cmd = {
34b321
+    .name       = "write",
34b321
+    .altname    = "w",
34b321
+    .cfunc      = write_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-bcCpqz] [-P pattern ] off len",
34b321
+    .oneline    = "writes a number of bytes at a specified offset",
34b321
+    .help       = write_help,
34b321
+};
34b321
+
34b321
+static int write_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    struct timeval t1, t2;
34b321
+    int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
34b321
+    int cflag = 0;
34b321
+    int c, cnt;
34b321
+    char *buf = NULL;
34b321
+    int64_t offset;
34b321
+    int count;
34b321
+    /* Some compilers get confused and warn if this is not initialized.  */
34b321
+    int total = 0;
34b321
+    int pattern = 0xcd;
34b321
+
34b321
+    while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'b':
34b321
+            bflag = 1;
34b321
+            break;
34b321
+        case 'c':
34b321
+            cflag = 1;
34b321
+            break;
34b321
+        case 'C':
34b321
+            Cflag = 1;
34b321
+            break;
34b321
+        case 'p':
34b321
+            pflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            Pflag = 1;
34b321
+            pattern = parse_pattern(optarg);
34b321
+            if (pattern < 0) {
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        case 'q':
34b321
+            qflag = 1;
34b321
+            break;
34b321
+        case 'z':
34b321
+            zflag = 1;
34b321
+            break;
34b321
+        default:
34b321
+            return command_usage(&write_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind != argc - 2) {
34b321
+        return command_usage(&write_cmd);
34b321
+    }
34b321
+
34b321
+    if (bflag + pflag + zflag > 1) {
34b321
+        printf("-b, -p, or -z cannot be specified at the same time\n");
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    if (zflag && Pflag) {
34b321
+        printf("-z and -P cannot be specified at the same time\n");
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    offset = cvtnum(argv[optind]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    optind++;
34b321
+    count = cvtnum(argv[optind]);
34b321
+    if (count < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    if (!pflag) {
34b321
+        if (offset & 0x1ff) {
34b321
+            printf("offset %" PRId64 " is not sector aligned\n",
34b321
+                   offset);
34b321
+            return 0;
34b321
+        }
34b321
+
34b321
+        if (count & 0x1ff) {
34b321
+            printf("count %d is not sector aligned\n",
34b321
+                   count);
34b321
+            return 0;
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (!zflag) {
34b321
+        buf = qemu_io_alloc(bs, count, pattern);
34b321
+    }
34b321
+
34b321
+    gettimeofday(&t1, NULL);
34b321
+    if (pflag) {
34b321
+        cnt = do_pwrite(bs, buf, offset, count, &total);
34b321
+    } else if (bflag) {
34b321
+        cnt = do_save_vmstate(bs, buf, offset, count, &total);
34b321
+    } else if (zflag) {
34b321
+        cnt = do_co_write_zeroes(bs, offset, count, &total);
34b321
+    } else if (cflag) {
34b321
+        cnt = do_write_compressed(bs, buf, offset, count, &total);
34b321
+    } else {
34b321
+        cnt = do_write(bs, buf, offset, count, &total);
34b321
+    }
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (cnt < 0) {
34b321
+        printf("write failed: %s\n", strerror(-cnt));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, t1);
34b321
+    print_report("wrote", &t2, offset, count, total, cnt, Cflag);
34b321
+
34b321
+out:
34b321
+    if (!zflag) {
34b321
+        qemu_io_free(buf);
34b321
+    }
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static void
34b321
+writev_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" writes a range of bytes from the given offset source from multiple buffers\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
34b321
+"\n"
34b321
+" Writes into a segment of the currently open file, using a buffer\n"
34b321
+" filled with a set pattern (0xcdcdcdcd).\n"
34b321
+" -P, -- use different pattern to fill file\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int writev_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t writev_cmd = {
34b321
+    .name       = "writev",
34b321
+    .cfunc      = writev_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-Cq] [-P pattern ] off len [len..]",
34b321
+    .oneline    = "writes a number of bytes at a specified offset",
34b321
+    .help       = writev_help,
34b321
+};
34b321
+
34b321
+static int writev_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    struct timeval t1, t2;
34b321
+    int Cflag = 0, qflag = 0;
34b321
+    int c, cnt;
34b321
+    char *buf;
34b321
+    int64_t offset;
34b321
+    /* Some compilers get confused and warn if this is not initialized.  */
34b321
+    int total = 0;
34b321
+    int nr_iov;
34b321
+    int pattern = 0xcd;
34b321
+    QEMUIOVector qiov;
34b321
+
34b321
+    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'C':
34b321
+            Cflag = 1;
34b321
+            break;
34b321
+        case 'q':
34b321
+            qflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            pattern = parse_pattern(optarg);
34b321
+            if (pattern < 0) {
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        default:
34b321
+            return command_usage(&writev_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind > argc - 2) {
34b321
+        return command_usage(&writev_cmd);
34b321
+    }
34b321
+
34b321
+    offset = cvtnum(argv[optind]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+    optind++;
34b321
+
34b321
+    if (offset & 0x1ff) {
34b321
+        printf("offset %" PRId64 " is not sector aligned\n",
34b321
+               offset);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    nr_iov = argc - optind;
34b321
+    buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
34b321
+    if (buf == NULL) {
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    gettimeofday(&t1, NULL);
34b321
+    cnt = do_aio_writev(bs, &qiov, offset, &total);
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (cnt < 0) {
34b321
+        printf("writev failed: %s\n", strerror(-cnt));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, t1);
34b321
+    print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
34b321
+out:
34b321
+    qemu_iovec_destroy(&qiov);
34b321
+    qemu_io_free(buf);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static void multiwrite_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" writes a range of bytes from the given offset source from multiple buffers,\n"
34b321
+" in a batch of requests that may be merged by qemu\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'multiwrite 512 1k 1k ; 4k 1k'\n"
34b321
+"  writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
34b321
+"\n"
34b321
+" Writes into a segment of the currently open file, using a buffer\n"
34b321
+" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
34b321
+" by one for each request contained in the multiwrite command.\n"
34b321
+" -P, -- use different pattern to fill file\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t multiwrite_cmd = {
34b321
+    .name       = "multiwrite",
34b321
+    .cfunc      = multiwrite_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
34b321
+    .oneline    = "issues multiple write requests at once",
34b321
+    .help       = multiwrite_help,
34b321
+};
34b321
+
34b321
+static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    struct timeval t1, t2;
34b321
+    int Cflag = 0, qflag = 0;
34b321
+    int c, cnt;
34b321
+    char **buf;
34b321
+    int64_t offset, first_offset = 0;
34b321
+    /* Some compilers get confused and warn if this is not initialized.  */
34b321
+    int total = 0;
34b321
+    int nr_iov;
34b321
+    int nr_reqs;
34b321
+    int pattern = 0xcd;
34b321
+    QEMUIOVector *qiovs;
34b321
+    int i;
34b321
+    BlockRequest *reqs;
34b321
+
34b321
+    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'C':
34b321
+            Cflag = 1;
34b321
+            break;
34b321
+        case 'q':
34b321
+            qflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            pattern = parse_pattern(optarg);
34b321
+            if (pattern < 0) {
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        default:
34b321
+            return command_usage(&writev_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind > argc - 2) {
34b321
+        return command_usage(&writev_cmd);
34b321
+    }
34b321
+
34b321
+    nr_reqs = 1;
34b321
+    for (i = optind; i < argc; i++) {
34b321
+        if (!strcmp(argv[i], ";")) {
34b321
+            nr_reqs++;
34b321
+        }
34b321
+    }
34b321
+
34b321
+    reqs = g_malloc0(nr_reqs * sizeof(*reqs));
34b321
+    buf = g_malloc0(nr_reqs * sizeof(*buf));
34b321
+    qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
34b321
+
34b321
+    for (i = 0; i < nr_reqs && optind < argc; i++) {
34b321
+        int j;
34b321
+
34b321
+        /* Read the offset of the request */
34b321
+        offset = cvtnum(argv[optind]);
34b321
+        if (offset < 0) {
34b321
+            printf("non-numeric offset argument -- %s\n", argv[optind]);
34b321
+            goto out;
34b321
+        }
34b321
+        optind++;
34b321
+
34b321
+        if (offset & 0x1ff) {
34b321
+            printf("offset %lld is not sector aligned\n",
34b321
+                   (long long)offset);
34b321
+            goto out;
34b321
+        }
34b321
+
34b321
+        if (i == 0) {
34b321
+            first_offset = offset;
34b321
+        }
34b321
+
34b321
+        /* Read lengths for qiov entries */
34b321
+        for (j = optind; j < argc; j++) {
34b321
+            if (!strcmp(argv[j], ";")) {
34b321
+                break;
34b321
+            }
34b321
+        }
34b321
+
34b321
+        nr_iov = j - optind;
34b321
+
34b321
+        /* Build request */
34b321
+        buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
34b321
+        if (buf[i] == NULL) {
34b321
+            goto out;
34b321
+        }
34b321
+
34b321
+        reqs[i].qiov = &qiovs[i];
34b321
+        reqs[i].sector = offset >> 9;
34b321
+        reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
34b321
+
34b321
+        optind = j + 1;
34b321
+
34b321
+        pattern++;
34b321
+    }
34b321
+
34b321
+    /* If there were empty requests at the end, ignore them */
34b321
+    nr_reqs = i;
34b321
+
34b321
+    gettimeofday(&t1, NULL);
34b321
+    cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (cnt < 0) {
34b321
+        printf("aio_multiwrite failed: %s\n", strerror(-cnt));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, t1);
34b321
+    print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
34b321
+out:
34b321
+    for (i = 0; i < nr_reqs; i++) {
34b321
+        qemu_io_free(buf[i]);
34b321
+        if (reqs[i].qiov != NULL) {
34b321
+            qemu_iovec_destroy(&qiovs[i]);
34b321
+        }
34b321
+    }
34b321
+    g_free(buf);
34b321
+    g_free(reqs);
34b321
+    g_free(qiovs);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+struct aio_ctx {
34b321
+    QEMUIOVector qiov;
34b321
+    int64_t offset;
34b321
+    char *buf;
34b321
+    int qflag;
34b321
+    int vflag;
34b321
+    int Cflag;
34b321
+    int Pflag;
34b321
+    int pattern;
34b321
+    struct timeval t1;
34b321
+};
34b321
+
34b321
+static void aio_write_done(void *opaque, int ret)
34b321
+{
34b321
+    struct aio_ctx *ctx = opaque;
34b321
+    struct timeval t2;
34b321
+
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+
34b321
+    if (ret < 0) {
34b321
+        printf("aio_write failed: %s\n", strerror(-ret));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (ctx->qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, ctx->t1);
34b321
+    print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
34b321
+                 ctx->qiov.size, 1, ctx->Cflag);
34b321
+out:
34b321
+    qemu_io_free(ctx->buf);
34b321
+    qemu_iovec_destroy(&ctx->qiov);
34b321
+    g_free(ctx);
34b321
+}
34b321
+
34b321
+static void aio_read_done(void *opaque, int ret)
34b321
+{
34b321
+    struct aio_ctx *ctx = opaque;
34b321
+    struct timeval t2;
34b321
+
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (ret < 0) {
34b321
+        printf("readv failed: %s\n", strerror(-ret));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (ctx->Pflag) {
34b321
+        void *cmp_buf = g_malloc(ctx->qiov.size);
34b321
+
34b321
+        memset(cmp_buf, ctx->pattern, ctx->qiov.size);
34b321
+        if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
34b321
+            printf("Pattern verification failed at offset %"
34b321
+                   PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
34b321
+        }
34b321
+        g_free(cmp_buf);
34b321
+    }
34b321
+
34b321
+    if (ctx->qflag) {
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    if (ctx->vflag) {
34b321
+        dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    t2 = tsub(t2, ctx->t1);
34b321
+    print_report("read", &t2, ctx->offset, ctx->qiov.size,
34b321
+                 ctx->qiov.size, 1, ctx->Cflag);
34b321
+out:
34b321
+    qemu_io_free(ctx->buf);
34b321
+    qemu_iovec_destroy(&ctx->qiov);
34b321
+    g_free(ctx);
34b321
+}
34b321
+
34b321
+static void aio_read_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" asynchronously reads a range of bytes from the given offset\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
34b321
+"\n"
34b321
+" Reads a segment of the currently open file, optionally dumping it to the\n"
34b321
+" standard output stream (with -v option) for subsequent inspection.\n"
34b321
+" The read is performed asynchronously and the aio_flush command must be\n"
34b321
+" used to ensure all outstanding aio requests have been completed.\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -P, -- use a pattern to verify read data\n"
34b321
+" -v, -- dump buffer to standard output\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t aio_read_cmd = {
34b321
+    .name       = "aio_read",
34b321
+    .cfunc      = aio_read_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-Cqv] [-P pattern ] off len [len..]",
34b321
+    .oneline    = "asynchronously reads a number of bytes",
34b321
+    .help       = aio_read_help,
34b321
+};
34b321
+
34b321
+static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int nr_iov, c;
34b321
+    struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
34b321
+
34b321
+    while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'C':
34b321
+            ctx->Cflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            ctx->Pflag = 1;
34b321
+            ctx->pattern = parse_pattern(optarg);
34b321
+            if (ctx->pattern < 0) {
34b321
+                g_free(ctx);
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        case 'q':
34b321
+            ctx->qflag = 1;
34b321
+            break;
34b321
+        case 'v':
34b321
+            ctx->vflag = 1;
34b321
+            break;
34b321
+        default:
34b321
+            g_free(ctx);
34b321
+            return command_usage(&aio_read_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind > argc - 2) {
34b321
+        g_free(ctx);
34b321
+        return command_usage(&aio_read_cmd);
34b321
+    }
34b321
+
34b321
+    ctx->offset = cvtnum(argv[optind]);
34b321
+    if (ctx->offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        g_free(ctx);
34b321
+        return 0;
34b321
+    }
34b321
+    optind++;
34b321
+
34b321
+    if (ctx->offset & 0x1ff) {
34b321
+        printf("offset %" PRId64 " is not sector aligned\n",
34b321
+               ctx->offset);
34b321
+        g_free(ctx);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    nr_iov = argc - optind;
34b321
+    ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
34b321
+    if (ctx->buf == NULL) {
34b321
+        g_free(ctx);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    gettimeofday(&ctx->t1, NULL);
34b321
+    bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
34b321
+                   ctx->qiov.size >> 9, aio_read_done, ctx);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static void aio_write_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" asynchronously writes a range of bytes from the given offset source\n"
34b321
+" from multiple buffers\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
34b321
+"\n"
34b321
+" Writes into a segment of the currently open file, using a buffer\n"
34b321
+" filled with a set pattern (0xcdcdcdcd).\n"
34b321
+" The write is performed asynchronously and the aio_flush command must be\n"
34b321
+" used to ensure all outstanding aio requests have been completed.\n"
34b321
+" -P, -- use different pattern to fill file\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t aio_write_cmd = {
34b321
+    .name       = "aio_write",
34b321
+    .cfunc      = aio_write_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-Cq] [-P pattern ] off len [len..]",
34b321
+    .oneline    = "asynchronously writes a number of bytes",
34b321
+    .help       = aio_write_help,
34b321
+};
34b321
+
34b321
+static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int nr_iov, c;
34b321
+    int pattern = 0xcd;
34b321
+    struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
34b321
+
34b321
+    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'C':
34b321
+            ctx->Cflag = 1;
34b321
+            break;
34b321
+        case 'q':
34b321
+            ctx->qflag = 1;
34b321
+            break;
34b321
+        case 'P':
34b321
+            pattern = parse_pattern(optarg);
34b321
+            if (pattern < 0) {
34b321
+                g_free(ctx);
34b321
+                return 0;
34b321
+            }
34b321
+            break;
34b321
+        default:
34b321
+            g_free(ctx);
34b321
+            return command_usage(&aio_write_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind > argc - 2) {
34b321
+        g_free(ctx);
34b321
+        return command_usage(&aio_write_cmd);
34b321
+    }
34b321
+
34b321
+    ctx->offset = cvtnum(argv[optind]);
34b321
+    if (ctx->offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        g_free(ctx);
34b321
+        return 0;
34b321
+    }
34b321
+    optind++;
34b321
+
34b321
+    if (ctx->offset & 0x1ff) {
34b321
+        printf("offset %" PRId64 " is not sector aligned\n",
34b321
+               ctx->offset);
34b321
+        g_free(ctx);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    nr_iov = argc - optind;
34b321
+    ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
34b321
+    if (ctx->buf == NULL) {
34b321
+        g_free(ctx);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    gettimeofday(&ctx->t1, NULL);
34b321
+    bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
34b321
+                    ctx->qiov.size >> 9, aio_write_done, ctx);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    bdrv_drain_all();
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t aio_flush_cmd = {
34b321
+    .name       = "aio_flush",
34b321
+    .cfunc      = aio_flush_f,
34b321
+    .oneline    = "completes all outstanding aio requests"
34b321
+};
34b321
+
34b321
+static int flush_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    bdrv_flush(bs);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t flush_cmd = {
34b321
+    .name       = "flush",
34b321
+    .altname    = "f",
34b321
+    .cfunc      = flush_f,
34b321
+    .oneline    = "flush all in-core file state to disk",
34b321
+};
34b321
+
34b321
+static int truncate_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int64_t offset;
34b321
+    int ret;
34b321
+
34b321
+    offset = cvtnum(argv[1]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric truncate argument -- %s\n", argv[1]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    ret = bdrv_truncate(bs, offset);
34b321
+    if (ret < 0) {
34b321
+        printf("truncate: %s\n", strerror(-ret));
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t truncate_cmd = {
34b321
+    .name       = "truncate",
34b321
+    .altname    = "t",
34b321
+    .cfunc      = truncate_f,
34b321
+    .argmin     = 1,
34b321
+    .argmax     = 1,
34b321
+    .args       = "off",
34b321
+    .oneline    = "truncates the current file at the given offset",
34b321
+};
34b321
+
34b321
+static int length_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int64_t size;
34b321
+    char s1[64];
34b321
+
34b321
+    size = bdrv_getlength(bs);
34b321
+    if (size < 0) {
34b321
+        printf("getlength: %s\n", strerror(-size));
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    cvtstr(size, s1, sizeof(s1));
34b321
+    printf("%s\n", s1);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+
34b321
+static const cmdinfo_t length_cmd = {
34b321
+    .name   = "length",
34b321
+    .altname    = "l",
34b321
+    .cfunc      = length_f,
34b321
+    .oneline    = "gets the length of the current file",
34b321
+};
34b321
+
34b321
+
34b321
+static int info_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    BlockDriverInfo bdi;
34b321
+    ImageInfoSpecific *spec_info;
34b321
+    char s1[64], s2[64];
34b321
+    int ret;
34b321
+
34b321
+    if (bs->drv && bs->drv->format_name) {
34b321
+        printf("format name: %s\n", bs->drv->format_name);
34b321
+    }
34b321
+    if (bs->drv && bs->drv->protocol_name) {
34b321
+        printf("format name: %s\n", bs->drv->protocol_name);
34b321
+    }
34b321
+
34b321
+    ret = bdrv_get_info(bs, &bdi;;
34b321
+    if (ret) {
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    cvtstr(bdi.cluster_size, s1, sizeof(s1));
34b321
+    cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
34b321
+
34b321
+    printf("cluster size: %s\n", s1);
34b321
+    printf("vm state offset: %s\n", s2);
34b321
+
34b321
+    spec_info = bdrv_get_specific_info(bs);
34b321
+    if (spec_info) {
34b321
+        printf("Format specific information:\n");
34b321
+        bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
34b321
+        qapi_free_ImageInfoSpecific(spec_info);
34b321
+    }
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+
34b321
+
34b321
+static const cmdinfo_t info_cmd = {
34b321
+    .name       = "info",
34b321
+    .altname    = "i",
34b321
+    .cfunc      = info_f,
34b321
+    .oneline    = "prints information about the current file",
34b321
+};
34b321
+
34b321
+static void discard_help(void)
34b321
+{
34b321
+    printf(
34b321
+"\n"
34b321
+" discards a range of bytes from the given offset\n"
34b321
+"\n"
34b321
+" Example:\n"
34b321
+" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
34b321
+"\n"
34b321
+" Discards a segment of the currently open file.\n"
34b321
+" -C, -- report statistics in a machine parsable format\n"
34b321
+" -q, -- quiet mode, do not show I/O statistics\n"
34b321
+"\n");
34b321
+}
34b321
+
34b321
+static int discard_f(BlockDriverState *bs, int argc, char **argv);
34b321
+
34b321
+static const cmdinfo_t discard_cmd = {
34b321
+    .name       = "discard",
34b321
+    .altname    = "d",
34b321
+    .cfunc      = discard_f,
34b321
+    .argmin     = 2,
34b321
+    .argmax     = -1,
34b321
+    .args       = "[-Cq] off len",
34b321
+    .oneline    = "discards a number of bytes at a specified offset",
34b321
+    .help       = discard_help,
34b321
+};
34b321
+
34b321
+static int discard_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    struct timeval t1, t2;
34b321
+    int Cflag = 0, qflag = 0;
34b321
+    int c, ret;
34b321
+    int64_t offset;
34b321
+    int count;
34b321
+
34b321
+    while ((c = getopt(argc, argv, "Cq")) != EOF) {
34b321
+        switch (c) {
34b321
+        case 'C':
34b321
+            Cflag = 1;
34b321
+            break;
34b321
+        case 'q':
34b321
+            qflag = 1;
34b321
+            break;
34b321
+        default:
34b321
+            return command_usage(&discard_cmd);
34b321
+        }
34b321
+    }
34b321
+
34b321
+    if (optind != argc - 2) {
34b321
+        return command_usage(&discard_cmd);
34b321
+    }
34b321
+
34b321
+    offset = cvtnum(argv[optind]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    optind++;
34b321
+    count = cvtnum(argv[optind]);
34b321
+    if (count < 0) {
34b321
+        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    gettimeofday(&t1, NULL);
34b321
+    ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
34b321
+                       count >> BDRV_SECTOR_BITS);
34b321
+    gettimeofday(&t2, NULL);
34b321
+
34b321
+    if (ret < 0) {
34b321
+        printf("discard failed: %s\n", strerror(-ret));
34b321
+        goto out;
34b321
+    }
34b321
+
34b321
+    /* Finally, report back -- -C gives a parsable format */
34b321
+    if (!qflag) {
34b321
+        t2 = tsub(t2, t1);
34b321
+        print_report("discard", &t2, offset, count, count, 1, Cflag);
34b321
+    }
34b321
+
34b321
+out:
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static int alloc_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int64_t offset, sector_num;
34b321
+    int nb_sectors, remaining;
34b321
+    char s1[64];
34b321
+    int num, sum_alloc;
34b321
+    int ret;
34b321
+
34b321
+    offset = cvtnum(argv[1]);
34b321
+    if (offset < 0) {
34b321
+        printf("non-numeric offset argument -- %s\n", argv[1]);
34b321
+        return 0;
34b321
+    } else if (offset & 0x1ff) {
34b321
+        printf("offset %" PRId64 " is not sector aligned\n",
34b321
+               offset);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    if (argc == 3) {
34b321
+        nb_sectors = cvtnum(argv[2]);
34b321
+        if (nb_sectors < 0) {
34b321
+            printf("non-numeric length argument -- %s\n", argv[2]);
34b321
+            return 0;
34b321
+        }
34b321
+    } else {
34b321
+        nb_sectors = 1;
34b321
+    }
34b321
+
34b321
+    remaining = nb_sectors;
34b321
+    sum_alloc = 0;
34b321
+    sector_num = offset >> 9;
34b321
+    while (remaining) {
34b321
+        ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
34b321
+        if (ret < 0) {
34b321
+            printf("is_allocated failed: %s\n", strerror(-ret));
34b321
+            return 0;
34b321
+        }
34b321
+        sector_num += num;
34b321
+        remaining -= num;
34b321
+        if (ret) {
34b321
+            sum_alloc += num;
34b321
+        }
34b321
+        if (num == 0) {
34b321
+            nb_sectors -= remaining;
34b321
+            remaining = 0;
34b321
+        }
34b321
+    }
34b321
+
34b321
+    cvtstr(offset, s1, sizeof(s1));
34b321
+
34b321
+    printf("%d/%d sectors allocated at offset %s\n",
34b321
+           sum_alloc, nb_sectors, s1);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t alloc_cmd = {
34b321
+    .name       = "alloc",
34b321
+    .altname    = "a",
34b321
+    .argmin     = 1,
34b321
+    .argmax     = 2,
34b321
+    .cfunc      = alloc_f,
34b321
+    .args       = "off [sectors]",
34b321
+    .oneline    = "checks if a sector is present in the file",
34b321
+};
34b321
+
34b321
+static int map_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int64_t offset;
34b321
+    int64_t nb_sectors;
34b321
+    char s1[64];
34b321
+    int num, num_checked;
34b321
+    int ret;
34b321
+    const char *retstr;
34b321
+
34b321
+    offset = 0;
34b321
+    nb_sectors = bs->total_sectors;
34b321
+
34b321
+    do {
34b321
+        num_checked = MIN(nb_sectors, INT_MAX);
34b321
+        ret = bdrv_is_allocated(bs, offset, num_checked, &num);
34b321
+        retstr = ret ? "    allocated" : "not allocated";
34b321
+        cvtstr(offset << 9ULL, s1, sizeof(s1));
34b321
+        printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
34b321
+               offset << 9ULL, num, num_checked, retstr, s1, ret);
34b321
+
34b321
+        offset += num;
34b321
+        nb_sectors -= num;
34b321
+    } while (offset < bs->total_sectors);
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t map_cmd = {
34b321
+       .name           = "map",
34b321
+       .argmin         = 0,
34b321
+       .argmax         = 0,
34b321
+       .cfunc          = map_f,
34b321
+       .args           = "",
34b321
+       .oneline        = "prints the allocated areas of a file",
34b321
+};
34b321
+
34b321
+static int break_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int ret;
34b321
+
34b321
+    ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
34b321
+    if (ret < 0) {
34b321
+        printf("Could not set breakpoint: %s\n", strerror(-ret));
34b321
+    }
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t break_cmd = {
34b321
+       .name           = "break",
34b321
+       .argmin         = 2,
34b321
+       .argmax         = 2,
34b321
+       .cfunc          = break_f,
34b321
+       .args           = "event tag",
34b321
+       .oneline        = "sets a breakpoint on event and tags the stopped "
34b321
+                         "request as tag",
34b321
+};
34b321
+
34b321
+static int resume_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    int ret;
34b321
+
34b321
+    ret = bdrv_debug_resume(bs, argv[1]);
34b321
+    if (ret < 0) {
34b321
+        printf("Could not resume request: %s\n", strerror(-ret));
34b321
+    }
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t resume_cmd = {
34b321
+       .name           = "resume",
34b321
+       .argmin         = 1,
34b321
+       .argmax         = 1,
34b321
+       .cfunc          = resume_f,
34b321
+       .args           = "tag",
34b321
+       .oneline        = "resumes the request tagged as tag",
34b321
+};
34b321
+
34b321
+static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    while (!bdrv_debug_is_suspended(bs, argv[1])) {
34b321
+        qemu_aio_wait();
34b321
+    }
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t wait_break_cmd = {
34b321
+       .name           = "wait_break",
34b321
+       .argmin         = 1,
34b321
+       .argmax         = 1,
34b321
+       .cfunc          = wait_break_f,
34b321
+       .args           = "tag",
34b321
+       .oneline        = "waits for the suspension of a request",
34b321
+};
34b321
+
34b321
+static int abort_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    abort();
34b321
+}
34b321
+
34b321
+static const cmdinfo_t abort_cmd = {
34b321
+       .name           = "abort",
34b321
+       .cfunc          = abort_f,
34b321
+       .flags          = CMD_NOFILE_OK,
34b321
+       .oneline        = "simulate a program crash using abort(3)",
34b321
+};
34b321
+
34b321
+static void sleep_cb(void *opaque)
34b321
+{
34b321
+    bool *expired = opaque;
34b321
+    *expired = true;
34b321
+}
34b321
+
34b321
+static int sleep_f(BlockDriverState *bs, int argc, char **argv)
34b321
+{
34b321
+    char *endptr;
34b321
+    long ms;
34b321
+    struct QEMUTimer *timer;
34b321
+    bool expired = false;
34b321
+
34b321
+    ms = strtol(argv[1], &endptr, 0);
34b321
+    if (ms < 0 || *endptr != '\0') {
34b321
+        printf("%s is not a valid number\n", argv[1]);
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    timer = qemu_new_timer_ns(host_clock, sleep_cb, &expired);
34b321
+    qemu_mod_timer(timer, qemu_get_clock_ns(host_clock) + SCALE_MS * ms);
34b321
+
34b321
+    while (!expired) {
34b321
+        main_loop_wait(false);
34b321
+    }
34b321
+
34b321
+    qemu_free_timer(timer);
34b321
+
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static const cmdinfo_t sleep_cmd = {
34b321
+       .name           = "sleep",
34b321
+       .argmin         = 1,
34b321
+       .argmax         = 1,
34b321
+       .cfunc          = sleep_f,
34b321
+       .flags          = CMD_NOFILE_OK,
34b321
+       .oneline        = "waits for the given value in milliseconds",
34b321
+};
34b321
+
34b321
+
34b321
+static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
34b321
+{
34b321
+    if (ct->flags & CMD_FLAG_GLOBAL) {
34b321
+        return 1;
34b321
+    }
34b321
+    if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
34b321
+        fprintf(stderr, "no file open, try 'help open'\n");
34b321
+        return 0;
34b321
+    }
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static void __attribute((constructor)) init_qemuio_commands(void)
34b321
+{
34b321
+    /* initialize commands */
34b321
+    help_init();
34b321
+    add_command(&read_cmd);
34b321
+    add_command(&readv_cmd);
34b321
+    add_command(&write_cmd);
34b321
+    add_command(&writev_cmd);
34b321
+    add_command(&multiwrite_cmd);
34b321
+    add_command(&aio_read_cmd);
34b321
+    add_command(&aio_write_cmd);
34b321
+    add_command(&aio_flush_cmd);
34b321
+    add_command(&flush_cmd);
34b321
+    add_command(&truncate_cmd);
34b321
+    add_command(&length_cmd);
34b321
+    add_command(&info_cmd);
34b321
+    add_command(&discard_cmd);
34b321
+    add_command(&alloc_cmd);
34b321
+    add_command(&map_cmd);
34b321
+    add_command(&break_cmd);
34b321
+    add_command(&resume_cmd);
34b321
+    add_command(&wait_break_cmd);
34b321
+    add_command(&abort_cmd);
34b321
+    add_command(&sleep_cmd);
34b321
+
34b321
+    add_check_command(init_check_command);
34b321
+}
34b321
diff --git a/qemu-io.c b/qemu-io.c
34b321
index c3cc4f3..4f1c808 100644
34b321
--- a/qemu-io.c
34b321
+++ b/qemu-io.c
34b321
@@ -31,1761 +31,8 @@
34b321
 char *progname;
34b321
 
34b321
 BlockDriverState *qemuio_bs;
34b321
-static int misalign;
34b321
 
34b321
-static int64_t cvtnum(const char *s)
34b321
-{
34b321
-    char *end;
34b321
-    return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
34b321
-}
34b321
-
34b321
-/*
34b321
- * Parse the pattern argument to various sub-commands.
34b321
- *
34b321
- * Because the pattern is used as an argument to memset it must evaluate
34b321
- * to an unsigned integer that fits into a single byte.
34b321
- */
34b321
-static int parse_pattern(const char *arg)
34b321
-{
34b321
-    char *endptr = NULL;
34b321
-    long pattern;
34b321
-
34b321
-    pattern = strtol(arg, &endptr, 0);
34b321
-    if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
34b321
-        printf("%s is not a valid pattern byte\n", arg);
34b321
-        return -1;
34b321
-    }
34b321
-
34b321
-    return pattern;
34b321
-}
34b321
-
34b321
-/*
34b321
- * Memory allocation helpers.
34b321
- *
34b321
- * Make sure memory is aligned by default, or purposefully misaligned if
34b321
- * that is specified on the command line.
34b321
- */
34b321
-
34b321
-#define MISALIGN_OFFSET     16
34b321
-static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
34b321
-{
34b321
-    void *buf;
34b321
-
34b321
-    if (misalign) {
34b321
-        len += MISALIGN_OFFSET;
34b321
-    }
34b321
-    buf = qemu_blockalign(bs, len);
34b321
-    memset(buf, pattern, len);
34b321
-    if (misalign) {
34b321
-        buf += MISALIGN_OFFSET;
34b321
-    }
34b321
-    return buf;
34b321
-}
34b321
-
34b321
-static void qemu_io_free(void *p)
34b321
-{
34b321
-    if (misalign) {
34b321
-        p -= MISALIGN_OFFSET;
34b321
-    }
34b321
-    qemu_vfree(p);
34b321
-}
34b321
-
34b321
-static void dump_buffer(const void *buffer, int64_t offset, int len)
34b321
-{
34b321
-    int i, j;
34b321
-    const uint8_t *p;
34b321
-
34b321
-    for (i = 0, p = buffer; i < len; i += 16) {
34b321
-        const uint8_t *s = p;
34b321
-
34b321
-        printf("%08" PRIx64 ":  ", offset + i);
34b321
-        for (j = 0; j < 16 && i + j < len; j++, p++) {
34b321
-            printf("%02x ", *p);
34b321
-        }
34b321
-        printf(" ");
34b321
-        for (j = 0; j < 16 && i + j < len; j++, s++) {
34b321
-            if (isalnum(*s)) {
34b321
-                printf("%c", *s);
34b321
-            } else {
34b321
-                printf(".");
34b321
-            }
34b321
-        }
34b321
-        printf("\n");
34b321
-    }
34b321
-}
34b321
-
34b321
-static void print_report(const char *op, struct timeval *t, int64_t offset,
34b321
-                         int count, int total, int cnt, int Cflag)
34b321
-{
34b321
-    char s1[64], s2[64], ts[64];
34b321
-
34b321
-    timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
34b321
-    if (!Cflag) {
34b321
-        cvtstr((double)total, s1, sizeof(s1));
34b321
-        cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
34b321
-        printf("%s %d/%d bytes at offset %" PRId64 "\n",
34b321
-               op, total, count, offset);
34b321
-        printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
34b321
-               s1, cnt, ts, s2, tdiv((double)cnt, *t));
34b321
-    } else {/* bytes,ops,time,bytes/sec,ops/sec */
34b321
-        printf("%d,%d,%s,%.3f,%.3f\n",
34b321
-            total, cnt, ts,
34b321
-            tdiv((double)total, *t),
34b321
-            tdiv((double)cnt, *t));
34b321
-    }
34b321
-}
34b321
-
34b321
-/*
34b321
- * Parse multiple length statements for vectored I/O, and construct an I/O
34b321
- * vector matching it.
34b321
- */
34b321
-static void *
34b321
-create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
34b321
-             int pattern)
34b321
-{
34b321
-    size_t *sizes = g_new0(size_t, nr_iov);
34b321
-    size_t count = 0;
34b321
-    void *buf = NULL;
34b321
-    void *p;
34b321
-    int i;
34b321
-
34b321
-    for (i = 0; i < nr_iov; i++) {
34b321
-        char *arg = argv[i];
34b321
-        int64_t len;
34b321
-
34b321
-        len = cvtnum(arg);
34b321
-        if (len < 0) {
34b321
-            printf("non-numeric length argument -- %s\n", arg);
34b321
-            goto fail;
34b321
-        }
34b321
-
34b321
-        /* should be SIZE_T_MAX, but that doesn't exist */
34b321
-        if (len > INT_MAX) {
34b321
-            printf("too large length argument -- %s\n", arg);
34b321
-            goto fail;
34b321
-        }
34b321
-
34b321
-        if (len & 0x1ff) {
34b321
-            printf("length argument %" PRId64
34b321
-                   " is not sector aligned\n", len);
34b321
-            goto fail;
34b321
-        }
34b321
-
34b321
-        sizes[i] = len;
34b321
-        count += len;
34b321
-    }
34b321
-
34b321
-    qemu_iovec_init(qiov, nr_iov);
34b321
-
34b321
-    buf = p = qemu_io_alloc(bs, count, pattern);
34b321
-
34b321
-    for (i = 0; i < nr_iov; i++) {
34b321
-        qemu_iovec_add(qiov, p, sizes[i]);
34b321
-        p += sizes[i];
34b321
-    }
34b321
-
34b321
-fail:
34b321
-    g_free(sizes);
34b321
-    return buf;
34b321
-}
34b321
-
34b321
-static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                   int *total)
34b321
-{
34b321
-    int ret;
34b321
-
34b321
-    ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
-    if (ret < 0) {
34b321
-        return ret;
34b321
-    }
34b321
-    *total = count;
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                    int *total)
34b321
-{
34b321
-    int ret;
34b321
-
34b321
-    ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
-    if (ret < 0) {
34b321
-        return ret;
34b321
-    }
34b321
-    *total = count;
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                    int *total)
34b321
-{
34b321
-    *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
34b321
-    if (*total < 0) {
34b321
-        return *total;
34b321
-    }
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
34b321
-                     int *total)
34b321
-{
34b321
-    *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
34b321
-    if (*total < 0) {
34b321
-        return *total;
34b321
-    }
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-typedef struct {
34b321
-    BlockDriverState *bs;
34b321
-    int64_t offset;
34b321
-    int count;
34b321
-    int *total;
34b321
-    int ret;
34b321
-    bool done;
34b321
-} CoWriteZeroes;
34b321
-
34b321
-static void coroutine_fn co_write_zeroes_entry(void *opaque)
34b321
-{
34b321
-    CoWriteZeroes *data = opaque;
34b321
-
34b321
-    data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
34b321
-                                     data->count / BDRV_SECTOR_SIZE, 0);
34b321
-    data->done = true;
34b321
-    if (data->ret < 0) {
34b321
-        *data->total = data->ret;
34b321
-        return;
34b321
-    }
34b321
-
34b321
-    *data->total = data->count;
34b321
-}
34b321
-
34b321
-static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
34b321
-                              int *total)
34b321
-{
34b321
-    Coroutine *co;
34b321
-    CoWriteZeroes data = {
34b321
-        .bs     = bs,
34b321
-        .offset = offset,
34b321
-        .count  = count,
34b321
-        .total  = total,
34b321
-        .done   = false,
34b321
-    };
34b321
-
34b321
-    co = qemu_coroutine_create(co_write_zeroes_entry);
34b321
-    qemu_coroutine_enter(co, &data);
34b321
-    while (!data.done) {
34b321
-        qemu_aio_wait();
34b321
-    }
34b321
-    if (data.ret < 0) {
34b321
-        return data.ret;
34b321
-    } else {
34b321
-        return 1;
34b321
-    }
34b321
-}
34b321
-
34b321
-static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
34b321
-                               int count, int *total)
34b321
-{
34b321
-    int ret;
34b321
-
34b321
-    ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
34b321
-    if (ret < 0) {
34b321
-        return ret;
34b321
-    }
34b321
-    *total = count;
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
-                           int count, int *total)
34b321
-{
34b321
-    *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
34b321
-    if (*total < 0) {
34b321
-        return *total;
34b321
-    }
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
34b321
-                           int count, int *total)
34b321
-{
34b321
-    *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
34b321
-    if (*total < 0) {
34b321
-        return *total;
34b321
-    }
34b321
-    return 1;
34b321
-}
34b321
-
34b321
-#define NOT_DONE 0x7fffffff
34b321
-static void aio_rw_done(void *opaque, int ret)
34b321
-{
34b321
-    *(int *)opaque = ret;
34b321
-}
34b321
-
34b321
-static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
34b321
-                        int64_t offset, int *total)
34b321
-{
34b321
-    int async_ret = NOT_DONE;
34b321
-
34b321
-    bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
34b321
-                   aio_rw_done, &async_ret);
34b321
-    while (async_ret == NOT_DONE) {
34b321
-        main_loop_wait(false);
34b321
-    }
34b321
-
34b321
-    *total = qiov->size;
34b321
-    return async_ret < 0 ? async_ret : 1;
34b321
-}
34b321
-
34b321
-static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
34b321
-                         int64_t offset, int *total)
34b321
-{
34b321
-    int async_ret = NOT_DONE;
34b321
-
34b321
-    bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
34b321
-                    aio_rw_done, &async_ret);
34b321
-    while (async_ret == NOT_DONE) {
34b321
-        main_loop_wait(false);
34b321
-    }
34b321
-
34b321
-    *total = qiov->size;
34b321
-    return async_ret < 0 ? async_ret : 1;
34b321
-}
34b321
-
34b321
-struct multiwrite_async_ret {
34b321
-    int num_done;
34b321
-    int error;
34b321
-};
34b321
-
34b321
-static void multiwrite_cb(void *opaque, int ret)
34b321
-{
34b321
-    struct multiwrite_async_ret *async_ret = opaque;
34b321
-
34b321
-    async_ret->num_done++;
34b321
-    if (ret < 0) {
34b321
-        async_ret->error = ret;
34b321
-    }
34b321
-}
34b321
-
34b321
-static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
34b321
-                             int num_reqs, int *total)
34b321
-{
34b321
-    int i, ret;
34b321
-    struct multiwrite_async_ret async_ret = {
34b321
-        .num_done = 0,
34b321
-        .error = 0,
34b321
-    };
34b321
-
34b321
-    *total = 0;
34b321
-    for (i = 0; i < num_reqs; i++) {
34b321
-        reqs[i].cb = multiwrite_cb;
34b321
-        reqs[i].opaque = &async_ret;
34b321
-        *total += reqs[i].qiov->size;
34b321
-    }
34b321
-
34b321
-    ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
34b321
-    if (ret < 0) {
34b321
-        return ret;
34b321
-    }
34b321
-
34b321
-    while (async_ret.num_done < num_reqs) {
34b321
-        main_loop_wait(false);
34b321
-    }
34b321
-
34b321
-    return async_ret.error < 0 ? async_ret.error : 1;
34b321
-}
34b321
-
34b321
-static void read_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" reads a range of bytes from the given offset\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
34b321
-"\n"
34b321
-" Reads a segment of the currently open file, optionally dumping it to the\n"
34b321
-" standard output stream (with -v option) for subsequent inspection.\n"
34b321
-" -b, -- read from the VM state rather than the virtual disk\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -l, -- length for pattern verification (only with -P)\n"
34b321
-" -p, -- use bdrv_pread to read the file\n"
34b321
-" -P, -- use a pattern to verify read data\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-" -s, -- start offset for pattern verification (only with -P)\n"
34b321
-" -v, -- dump buffer to standard output\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int read_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t read_cmd = {
34b321
-    .name       = "read",
34b321
-    .altname    = "r",
34b321
-    .cfunc      = read_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
34b321
-    .oneline    = "reads a number of bytes at a specified offset",
34b321
-    .help       = read_help,
34b321
-};
34b321
-
34b321
-static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    struct timeval t1, t2;
34b321
-    int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
34b321
-    int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
34b321
-    int c, cnt;
34b321
-    char *buf;
34b321
-    int64_t offset;
34b321
-    int count;
34b321
-    /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
-    int pattern = 0, pattern_offset = 0, pattern_count = 0;
34b321
-
34b321
-    while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'b':
34b321
-            bflag = 1;
34b321
-            break;
34b321
-        case 'C':
34b321
-            Cflag = 1;
34b321
-            break;
34b321
-        case 'l':
34b321
-            lflag = 1;
34b321
-            pattern_count = cvtnum(optarg);
34b321
-            if (pattern_count < 0) {
34b321
-                printf("non-numeric length argument -- %s\n", optarg);
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        case 'p':
34b321
-            pflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            Pflag = 1;
34b321
-            pattern = parse_pattern(optarg);
34b321
-            if (pattern < 0) {
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        case 'q':
34b321
-            qflag = 1;
34b321
-            break;
34b321
-        case 's':
34b321
-            sflag = 1;
34b321
-            pattern_offset = cvtnum(optarg);
34b321
-            if (pattern_offset < 0) {
34b321
-                printf("non-numeric length argument -- %s\n", optarg);
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        case 'v':
34b321
-            vflag = 1;
34b321
-            break;
34b321
-        default:
34b321
-            return command_usage(&read_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind != argc - 2) {
34b321
-        return command_usage(&read_cmd);
34b321
-    }
34b321
-
34b321
-    if (bflag && pflag) {
34b321
-        printf("-b and -p cannot be specified at the same time\n");
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    offset = cvtnum(argv[optind]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    optind++;
34b321
-    count = cvtnum(argv[optind]);
34b321
-    if (count < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    if (!Pflag && (lflag || sflag)) {
34b321
-        return command_usage(&read_cmd);
34b321
-    }
34b321
-
34b321
-    if (!lflag) {
34b321
-        pattern_count = count - pattern_offset;
34b321
-    }
34b321
-
34b321
-    if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
34b321
-        printf("pattern verification range exceeds end of read data\n");
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    if (!pflag) {
34b321
-        if (offset & 0x1ff) {
34b321
-            printf("offset %" PRId64 " is not sector aligned\n",
34b321
-                   offset);
34b321
-            return 0;
34b321
-        }
34b321
-        if (count & 0x1ff) {
34b321
-            printf("count %d is not sector aligned\n",
34b321
-                   count);
34b321
-            return 0;
34b321
-        }
34b321
-    }
34b321
-
34b321
-    buf = qemu_io_alloc(bs, count, 0xab);
34b321
-
34b321
-    gettimeofday(&t1, NULL);
34b321
-    if (pflag) {
34b321
-        cnt = do_pread(bs, buf, offset, count, &total);
34b321
-    } else if (bflag) {
34b321
-        cnt = do_load_vmstate(bs, buf, offset, count, &total);
34b321
-    } else {
34b321
-        cnt = do_read(bs, buf, offset, count, &total);
34b321
-    }
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (cnt < 0) {
34b321
-        printf("read failed: %s\n", strerror(-cnt));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (Pflag) {
34b321
-        void *cmp_buf = g_malloc(pattern_count);
34b321
-        memset(cmp_buf, pattern, pattern_count);
34b321
-        if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
34b321
-            printf("Pattern verification failed at offset %"
34b321
-                   PRId64 ", %d bytes\n",
34b321
-                   offset + pattern_offset, pattern_count);
34b321
-        }
34b321
-        g_free(cmp_buf);
34b321
-    }
34b321
-
34b321
-    if (qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (vflag) {
34b321
-        dump_buffer(buf, offset, count);
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, t1);
34b321
-    print_report("read", &t2, offset, count, total, cnt, Cflag);
34b321
-
34b321
-out:
34b321
-    qemu_io_free(buf);
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static void readv_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" reads a range of bytes from the given offset into multiple buffers\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
34b321
-"\n"
34b321
-" Reads a segment of the currently open file, optionally dumping it to the\n"
34b321
-" standard output stream (with -v option) for subsequent inspection.\n"
34b321
-" Uses multiple iovec buffers if more than one byte range is specified.\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -P, -- use a pattern to verify read data\n"
34b321
-" -v, -- dump buffer to standard output\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int readv_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t readv_cmd = {
34b321
-    .name       = "readv",
34b321
-    .cfunc      = readv_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-Cqv] [-P pattern ] off len [len..]",
34b321
-    .oneline    = "reads a number of bytes at a specified offset",
34b321
-    .help       = readv_help,
34b321
-};
34b321
-
34b321
-static int readv_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    struct timeval t1, t2;
34b321
-    int Cflag = 0, qflag = 0, vflag = 0;
34b321
-    int c, cnt;
34b321
-    char *buf;
34b321
-    int64_t offset;
34b321
-    /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
-    int nr_iov;
34b321
-    QEMUIOVector qiov;
34b321
-    int pattern = 0;
34b321
-    int Pflag = 0;
34b321
-
34b321
-    while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'C':
34b321
-            Cflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            Pflag = 1;
34b321
-            pattern = parse_pattern(optarg);
34b321
-            if (pattern < 0) {
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        case 'q':
34b321
-            qflag = 1;
34b321
-            break;
34b321
-        case 'v':
34b321
-            vflag = 1;
34b321
-            break;
34b321
-        default:
34b321
-            return command_usage(&readv_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind > argc - 2) {
34b321
-        return command_usage(&readv_cmd);
34b321
-    }
34b321
-
34b321
-
34b321
-    offset = cvtnum(argv[optind]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-    optind++;
34b321
-
34b321
-    if (offset & 0x1ff) {
34b321
-        printf("offset %" PRId64 " is not sector aligned\n",
34b321
-               offset);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    nr_iov = argc - optind;
34b321
-    buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
34b321
-    if (buf == NULL) {
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    gettimeofday(&t1, NULL);
34b321
-    cnt = do_aio_readv(bs, &qiov, offset, &total);
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (cnt < 0) {
34b321
-        printf("readv failed: %s\n", strerror(-cnt));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (Pflag) {
34b321
-        void *cmp_buf = g_malloc(qiov.size);
34b321
-        memset(cmp_buf, pattern, qiov.size);
34b321
-        if (memcmp(buf, cmp_buf, qiov.size)) {
34b321
-            printf("Pattern verification failed at offset %"
34b321
-                   PRId64 ", %zd bytes\n", offset, qiov.size);
34b321
-        }
34b321
-        g_free(cmp_buf);
34b321
-    }
34b321
-
34b321
-    if (qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (vflag) {
34b321
-        dump_buffer(buf, offset, qiov.size);
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, t1);
34b321
-    print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
34b321
-
34b321
-out:
34b321
-    qemu_iovec_destroy(&qiov);
34b321
-    qemu_io_free(buf);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static void write_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" writes a range of bytes from the given offset\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
34b321
-"\n"
34b321
-" Writes into a segment of the currently open file, using a buffer\n"
34b321
-" filled with a set pattern (0xcdcdcdcd).\n"
34b321
-" -b, -- write to the VM state rather than the virtual disk\n"
34b321
-" -c, -- write compressed data with bdrv_write_compressed\n"
34b321
-" -p, -- use bdrv_pwrite to write the file\n"
34b321
-" -P, -- use different pattern to fill file\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-" -z, -- write zeroes using bdrv_co_write_zeroes\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int write_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t write_cmd = {
34b321
-    .name       = "write",
34b321
-    .altname    = "w",
34b321
-    .cfunc      = write_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-bcCpqz] [-P pattern ] off len",
34b321
-    .oneline    = "writes a number of bytes at a specified offset",
34b321
-    .help       = write_help,
34b321
-};
34b321
-
34b321
-static int write_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    struct timeval t1, t2;
34b321
-    int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
34b321
-    int cflag = 0;
34b321
-    int c, cnt;
34b321
-    char *buf = NULL;
34b321
-    int64_t offset;
34b321
-    int count;
34b321
-    /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
-    int pattern = 0xcd;
34b321
-
34b321
-    while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'b':
34b321
-            bflag = 1;
34b321
-            break;
34b321
-        case 'c':
34b321
-            cflag = 1;
34b321
-            break;
34b321
-        case 'C':
34b321
-            Cflag = 1;
34b321
-            break;
34b321
-        case 'p':
34b321
-            pflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            Pflag = 1;
34b321
-            pattern = parse_pattern(optarg);
34b321
-            if (pattern < 0) {
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        case 'q':
34b321
-            qflag = 1;
34b321
-            break;
34b321
-        case 'z':
34b321
-            zflag = 1;
34b321
-            break;
34b321
-        default:
34b321
-            return command_usage(&write_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind != argc - 2) {
34b321
-        return command_usage(&write_cmd);
34b321
-    }
34b321
-
34b321
-    if (bflag + pflag + zflag > 1) {
34b321
-        printf("-b, -p, or -z cannot be specified at the same time\n");
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    if (zflag && Pflag) {
34b321
-        printf("-z and -P cannot be specified at the same time\n");
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    offset = cvtnum(argv[optind]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    optind++;
34b321
-    count = cvtnum(argv[optind]);
34b321
-    if (count < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    if (!pflag) {
34b321
-        if (offset & 0x1ff) {
34b321
-            printf("offset %" PRId64 " is not sector aligned\n",
34b321
-                   offset);
34b321
-            return 0;
34b321
-        }
34b321
-
34b321
-        if (count & 0x1ff) {
34b321
-            printf("count %d is not sector aligned\n",
34b321
-                   count);
34b321
-            return 0;
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (!zflag) {
34b321
-        buf = qemu_io_alloc(bs, count, pattern);
34b321
-    }
34b321
-
34b321
-    gettimeofday(&t1, NULL);
34b321
-    if (pflag) {
34b321
-        cnt = do_pwrite(bs, buf, offset, count, &total);
34b321
-    } else if (bflag) {
34b321
-        cnt = do_save_vmstate(bs, buf, offset, count, &total);
34b321
-    } else if (zflag) {
34b321
-        cnt = do_co_write_zeroes(bs, offset, count, &total);
34b321
-    } else if (cflag) {
34b321
-        cnt = do_write_compressed(bs, buf, offset, count, &total);
34b321
-    } else {
34b321
-        cnt = do_write(bs, buf, offset, count, &total);
34b321
-    }
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (cnt < 0) {
34b321
-        printf("write failed: %s\n", strerror(-cnt));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, t1);
34b321
-    print_report("wrote", &t2, offset, count, total, cnt, Cflag);
34b321
-
34b321
-out:
34b321
-    if (!zflag) {
34b321
-        qemu_io_free(buf);
34b321
-    }
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static void
34b321
-writev_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" writes a range of bytes from the given offset source from multiple buffers\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
34b321
-"\n"
34b321
-" Writes into a segment of the currently open file, using a buffer\n"
34b321
-" filled with a set pattern (0xcdcdcdcd).\n"
34b321
-" -P, -- use different pattern to fill file\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int writev_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t writev_cmd = {
34b321
-    .name       = "writev",
34b321
-    .cfunc      = writev_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-Cq] [-P pattern ] off len [len..]",
34b321
-    .oneline    = "writes a number of bytes at a specified offset",
34b321
-    .help       = writev_help,
34b321
-};
34b321
-
34b321
-static int writev_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    struct timeval t1, t2;
34b321
-    int Cflag = 0, qflag = 0;
34b321
-    int c, cnt;
34b321
-    char *buf;
34b321
-    int64_t offset;
34b321
-    /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
-    int nr_iov;
34b321
-    int pattern = 0xcd;
34b321
-    QEMUIOVector qiov;
34b321
-
34b321
-    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'C':
34b321
-            Cflag = 1;
34b321
-            break;
34b321
-        case 'q':
34b321
-            qflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            pattern = parse_pattern(optarg);
34b321
-            if (pattern < 0) {
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        default:
34b321
-            return command_usage(&writev_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind > argc - 2) {
34b321
-        return command_usage(&writev_cmd);
34b321
-    }
34b321
-
34b321
-    offset = cvtnum(argv[optind]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-    optind++;
34b321
-
34b321
-    if (offset & 0x1ff) {
34b321
-        printf("offset %" PRId64 " is not sector aligned\n",
34b321
-               offset);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    nr_iov = argc - optind;
34b321
-    buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
34b321
-    if (buf == NULL) {
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    gettimeofday(&t1, NULL);
34b321
-    cnt = do_aio_writev(bs, &qiov, offset, &total);
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (cnt < 0) {
34b321
-        printf("writev failed: %s\n", strerror(-cnt));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, t1);
34b321
-    print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
34b321
-out:
34b321
-    qemu_iovec_destroy(&qiov);
34b321
-    qemu_io_free(buf);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static void multiwrite_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" writes a range of bytes from the given offset source from multiple buffers,\n"
34b321
-" in a batch of requests that may be merged by qemu\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'multiwrite 512 1k 1k ; 4k 1k'\n"
34b321
-"  writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
34b321
-"\n"
34b321
-" Writes into a segment of the currently open file, using a buffer\n"
34b321
-" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
34b321
-" by one for each request contained in the multiwrite command.\n"
34b321
-" -P, -- use different pattern to fill file\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t multiwrite_cmd = {
34b321
-    .name       = "multiwrite",
34b321
-    .cfunc      = multiwrite_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
34b321
-    .oneline    = "issues multiple write requests at once",
34b321
-    .help       = multiwrite_help,
34b321
-};
34b321
-
34b321
-static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    struct timeval t1, t2;
34b321
-    int Cflag = 0, qflag = 0;
34b321
-    int c, cnt;
34b321
-    char **buf;
34b321
-    int64_t offset, first_offset = 0;
34b321
-    /* Some compilers get confused and warn if this is not initialized.  */
34b321
-    int total = 0;
34b321
-    int nr_iov;
34b321
-    int nr_reqs;
34b321
-    int pattern = 0xcd;
34b321
-    QEMUIOVector *qiovs;
34b321
-    int i;
34b321
-    BlockRequest *reqs;
34b321
-
34b321
-    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'C':
34b321
-            Cflag = 1;
34b321
-            break;
34b321
-        case 'q':
34b321
-            qflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            pattern = parse_pattern(optarg);
34b321
-            if (pattern < 0) {
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        default:
34b321
-            return command_usage(&writev_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind > argc - 2) {
34b321
-        return command_usage(&writev_cmd);
34b321
-    }
34b321
-
34b321
-    nr_reqs = 1;
34b321
-    for (i = optind; i < argc; i++) {
34b321
-        if (!strcmp(argv[i], ";")) {
34b321
-            nr_reqs++;
34b321
-        }
34b321
-    }
34b321
-
34b321
-    reqs = g_malloc0(nr_reqs * sizeof(*reqs));
34b321
-    buf = g_malloc0(nr_reqs * sizeof(*buf));
34b321
-    qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
34b321
-
34b321
-    for (i = 0; i < nr_reqs && optind < argc; i++) {
34b321
-        int j;
34b321
-
34b321
-        /* Read the offset of the request */
34b321
-        offset = cvtnum(argv[optind]);
34b321
-        if (offset < 0) {
34b321
-            printf("non-numeric offset argument -- %s\n", argv[optind]);
34b321
-            goto out;
34b321
-        }
34b321
-        optind++;
34b321
-
34b321
-        if (offset & 0x1ff) {
34b321
-            printf("offset %lld is not sector aligned\n",
34b321
-                   (long long)offset);
34b321
-            goto out;
34b321
-        }
34b321
-
34b321
-        if (i == 0) {
34b321
-            first_offset = offset;
34b321
-        }
34b321
-
34b321
-        /* Read lengths for qiov entries */
34b321
-        for (j = optind; j < argc; j++) {
34b321
-            if (!strcmp(argv[j], ";")) {
34b321
-                break;
34b321
-            }
34b321
-        }
34b321
-
34b321
-        nr_iov = j - optind;
34b321
-
34b321
-        /* Build request */
34b321
-        buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
34b321
-        if (buf[i] == NULL) {
34b321
-            goto out;
34b321
-        }
34b321
-
34b321
-        reqs[i].qiov = &qiovs[i];
34b321
-        reqs[i].sector = offset >> 9;
34b321
-        reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
34b321
-
34b321
-        optind = j + 1;
34b321
-
34b321
-        pattern++;
34b321
-    }
34b321
-
34b321
-    /* If there were empty requests at the end, ignore them */
34b321
-    nr_reqs = i;
34b321
-
34b321
-    gettimeofday(&t1, NULL);
34b321
-    cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (cnt < 0) {
34b321
-        printf("aio_multiwrite failed: %s\n", strerror(-cnt));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, t1);
34b321
-    print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
34b321
-out:
34b321
-    for (i = 0; i < nr_reqs; i++) {
34b321
-        qemu_io_free(buf[i]);
34b321
-        if (reqs[i].qiov != NULL) {
34b321
-            qemu_iovec_destroy(&qiovs[i]);
34b321
-        }
34b321
-    }
34b321
-    g_free(buf);
34b321
-    g_free(reqs);
34b321
-    g_free(qiovs);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-struct aio_ctx {
34b321
-    QEMUIOVector qiov;
34b321
-    int64_t offset;
34b321
-    char *buf;
34b321
-    int qflag;
34b321
-    int vflag;
34b321
-    int Cflag;
34b321
-    int Pflag;
34b321
-    int pattern;
34b321
-    struct timeval t1;
34b321
-};
34b321
-
34b321
-static void aio_write_done(void *opaque, int ret)
34b321
-{
34b321
-    struct aio_ctx *ctx = opaque;
34b321
-    struct timeval t2;
34b321
-
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-
34b321
-    if (ret < 0) {
34b321
-        printf("aio_write failed: %s\n", strerror(-ret));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (ctx->qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, ctx->t1);
34b321
-    print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
34b321
-                 ctx->qiov.size, 1, ctx->Cflag);
34b321
-out:
34b321
-    qemu_io_free(ctx->buf);
34b321
-    qemu_iovec_destroy(&ctx->qiov);
34b321
-    g_free(ctx);
34b321
-}
34b321
-
34b321
-static void aio_read_done(void *opaque, int ret)
34b321
-{
34b321
-    struct aio_ctx *ctx = opaque;
34b321
-    struct timeval t2;
34b321
-
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (ret < 0) {
34b321
-        printf("readv failed: %s\n", strerror(-ret));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (ctx->Pflag) {
34b321
-        void *cmp_buf = g_malloc(ctx->qiov.size);
34b321
-
34b321
-        memset(cmp_buf, ctx->pattern, ctx->qiov.size);
34b321
-        if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
34b321
-            printf("Pattern verification failed at offset %"
34b321
-                   PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
34b321
-        }
34b321
-        g_free(cmp_buf);
34b321
-    }
34b321
-
34b321
-    if (ctx->qflag) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (ctx->vflag) {
34b321
-        dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    t2 = tsub(t2, ctx->t1);
34b321
-    print_report("read", &t2, ctx->offset, ctx->qiov.size,
34b321
-                 ctx->qiov.size, 1, ctx->Cflag);
34b321
-out:
34b321
-    qemu_io_free(ctx->buf);
34b321
-    qemu_iovec_destroy(&ctx->qiov);
34b321
-    g_free(ctx);
34b321
-}
34b321
-
34b321
-static void aio_read_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" asynchronously reads a range of bytes from the given offset\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
34b321
-"\n"
34b321
-" Reads a segment of the currently open file, optionally dumping it to the\n"
34b321
-" standard output stream (with -v option) for subsequent inspection.\n"
34b321
-" The read is performed asynchronously and the aio_flush command must be\n"
34b321
-" used to ensure all outstanding aio requests have been completed.\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -P, -- use a pattern to verify read data\n"
34b321
-" -v, -- dump buffer to standard output\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t aio_read_cmd = {
34b321
-    .name       = "aio_read",
34b321
-    .cfunc      = aio_read_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-Cqv] [-P pattern ] off len [len..]",
34b321
-    .oneline    = "asynchronously reads a number of bytes",
34b321
-    .help       = aio_read_help,
34b321
-};
34b321
-
34b321
-static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int nr_iov, c;
34b321
-    struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
34b321
-
34b321
-    while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'C':
34b321
-            ctx->Cflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            ctx->Pflag = 1;
34b321
-            ctx->pattern = parse_pattern(optarg);
34b321
-            if (ctx->pattern < 0) {
34b321
-                g_free(ctx);
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        case 'q':
34b321
-            ctx->qflag = 1;
34b321
-            break;
34b321
-        case 'v':
34b321
-            ctx->vflag = 1;
34b321
-            break;
34b321
-        default:
34b321
-            g_free(ctx);
34b321
-            return command_usage(&aio_read_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind > argc - 2) {
34b321
-        g_free(ctx);
34b321
-        return command_usage(&aio_read_cmd);
34b321
-    }
34b321
-
34b321
-    ctx->offset = cvtnum(argv[optind]);
34b321
-    if (ctx->offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        g_free(ctx);
34b321
-        return 0;
34b321
-    }
34b321
-    optind++;
34b321
-
34b321
-    if (ctx->offset & 0x1ff) {
34b321
-        printf("offset %" PRId64 " is not sector aligned\n",
34b321
-               ctx->offset);
34b321
-        g_free(ctx);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    nr_iov = argc - optind;
34b321
-    ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
34b321
-    if (ctx->buf == NULL) {
34b321
-        g_free(ctx);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    gettimeofday(&ctx->t1, NULL);
34b321
-    bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
34b321
-                   ctx->qiov.size >> 9, aio_read_done, ctx);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static void aio_write_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" asynchronously writes a range of bytes from the given offset source\n"
34b321
-" from multiple buffers\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
34b321
-"\n"
34b321
-" Writes into a segment of the currently open file, using a buffer\n"
34b321
-" filled with a set pattern (0xcdcdcdcd).\n"
34b321
-" The write is performed asynchronously and the aio_flush command must be\n"
34b321
-" used to ensure all outstanding aio requests have been completed.\n"
34b321
-" -P, -- use different pattern to fill file\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t aio_write_cmd = {
34b321
-    .name       = "aio_write",
34b321
-    .cfunc      = aio_write_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-Cq] [-P pattern ] off len [len..]",
34b321
-    .oneline    = "asynchronously writes a number of bytes",
34b321
-    .help       = aio_write_help,
34b321
-};
34b321
-
34b321
-static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int nr_iov, c;
34b321
-    int pattern = 0xcd;
34b321
-    struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
34b321
-
34b321
-    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'C':
34b321
-            ctx->Cflag = 1;
34b321
-            break;
34b321
-        case 'q':
34b321
-            ctx->qflag = 1;
34b321
-            break;
34b321
-        case 'P':
34b321
-            pattern = parse_pattern(optarg);
34b321
-            if (pattern < 0) {
34b321
-                g_free(ctx);
34b321
-                return 0;
34b321
-            }
34b321
-            break;
34b321
-        default:
34b321
-            g_free(ctx);
34b321
-            return command_usage(&aio_write_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind > argc - 2) {
34b321
-        g_free(ctx);
34b321
-        return command_usage(&aio_write_cmd);
34b321
-    }
34b321
-
34b321
-    ctx->offset = cvtnum(argv[optind]);
34b321
-    if (ctx->offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        g_free(ctx);
34b321
-        return 0;
34b321
-    }
34b321
-    optind++;
34b321
-
34b321
-    if (ctx->offset & 0x1ff) {
34b321
-        printf("offset %" PRId64 " is not sector aligned\n",
34b321
-               ctx->offset);
34b321
-        g_free(ctx);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    nr_iov = argc - optind;
34b321
-    ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
34b321
-    if (ctx->buf == NULL) {
34b321
-        g_free(ctx);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    gettimeofday(&ctx->t1, NULL);
34b321
-    bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
34b321
-                    ctx->qiov.size >> 9, aio_write_done, ctx);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    bdrv_drain_all();
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t aio_flush_cmd = {
34b321
-    .name       = "aio_flush",
34b321
-    .cfunc      = aio_flush_f,
34b321
-    .oneline    = "completes all outstanding aio requests"
34b321
-};
34b321
-
34b321
-static int flush_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    bdrv_flush(bs);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t flush_cmd = {
34b321
-    .name       = "flush",
34b321
-    .altname    = "f",
34b321
-    .cfunc      = flush_f,
34b321
-    .oneline    = "flush all in-core file state to disk",
34b321
-};
34b321
-
34b321
-static int truncate_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int64_t offset;
34b321
-    int ret;
34b321
-
34b321
-    offset = cvtnum(argv[1]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric truncate argument -- %s\n", argv[1]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    ret = bdrv_truncate(bs, offset);
34b321
-    if (ret < 0) {
34b321
-        printf("truncate: %s\n", strerror(-ret));
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t truncate_cmd = {
34b321
-    .name       = "truncate",
34b321
-    .altname    = "t",
34b321
-    .cfunc      = truncate_f,
34b321
-    .argmin     = 1,
34b321
-    .argmax     = 1,
34b321
-    .args       = "off",
34b321
-    .oneline    = "truncates the current file at the given offset",
34b321
-};
34b321
-
34b321
-static int length_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int64_t size;
34b321
-    char s1[64];
34b321
-
34b321
-    size = bdrv_getlength(bs);
34b321
-    if (size < 0) {
34b321
-        printf("getlength: %s\n", strerror(-size));
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    cvtstr(size, s1, sizeof(s1));
34b321
-    printf("%s\n", s1);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-
34b321
-static const cmdinfo_t length_cmd = {
34b321
-    .name   = "length",
34b321
-    .altname    = "l",
34b321
-    .cfunc      = length_f,
34b321
-    .oneline    = "gets the length of the current file",
34b321
-};
34b321
-
34b321
-
34b321
-static int info_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    BlockDriverInfo bdi;
34b321
-    ImageInfoSpecific *spec_info;
34b321
-    char s1[64], s2[64];
34b321
-    int ret;
34b321
-
34b321
-    if (bs->drv && bs->drv->format_name) {
34b321
-        printf("format name: %s\n", bs->drv->format_name);
34b321
-    }
34b321
-    if (bs->drv && bs->drv->protocol_name) {
34b321
-        printf("format name: %s\n", bs->drv->protocol_name);
34b321
-    }
34b321
-
34b321
-    ret = bdrv_get_info(bs, &bdi;;
34b321
-    if (ret) {
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    cvtstr(bdi.cluster_size, s1, sizeof(s1));
34b321
-    cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
34b321
-
34b321
-    printf("cluster size: %s\n", s1);
34b321
-    printf("vm state offset: %s\n", s2);
34b321
-
34b321
-    spec_info = bdrv_get_specific_info(bs);
34b321
-    if (spec_info) {
34b321
-        printf("Format specific information:\n");
34b321
-        bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
34b321
-        qapi_free_ImageInfoSpecific(spec_info);
34b321
-    }
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-
34b321
-
34b321
-static const cmdinfo_t info_cmd = {
34b321
-    .name       = "info",
34b321
-    .altname    = "i",
34b321
-    .cfunc      = info_f,
34b321
-    .oneline    = "prints information about the current file",
34b321
-};
34b321
-
34b321
-static void discard_help(void)
34b321
-{
34b321
-    printf(
34b321
-"\n"
34b321
-" discards a range of bytes from the given offset\n"
34b321
-"\n"
34b321
-" Example:\n"
34b321
-" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
34b321
-"\n"
34b321
-" Discards a segment of the currently open file.\n"
34b321
-" -C, -- report statistics in a machine parsable format\n"
34b321
-" -q, -- quiet mode, do not show I/O statistics\n"
34b321
-"\n");
34b321
-}
34b321
-
34b321
-static int discard_f(BlockDriverState *bs, int argc, char **argv);
34b321
-
34b321
-static const cmdinfo_t discard_cmd = {
34b321
-    .name       = "discard",
34b321
-    .altname    = "d",
34b321
-    .cfunc      = discard_f,
34b321
-    .argmin     = 2,
34b321
-    .argmax     = -1,
34b321
-    .args       = "[-Cq] off len",
34b321
-    .oneline    = "discards a number of bytes at a specified offset",
34b321
-    .help       = discard_help,
34b321
-};
34b321
-
34b321
-static int discard_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    struct timeval t1, t2;
34b321
-    int Cflag = 0, qflag = 0;
34b321
-    int c, ret;
34b321
-    int64_t offset;
34b321
-    int count;
34b321
-
34b321
-    while ((c = getopt(argc, argv, "Cq")) != EOF) {
34b321
-        switch (c) {
34b321
-        case 'C':
34b321
-            Cflag = 1;
34b321
-            break;
34b321
-        case 'q':
34b321
-            qflag = 1;
34b321
-            break;
34b321
-        default:
34b321
-            return command_usage(&discard_cmd);
34b321
-        }
34b321
-    }
34b321
-
34b321
-    if (optind != argc - 2) {
34b321
-        return command_usage(&discard_cmd);
34b321
-    }
34b321
-
34b321
-    offset = cvtnum(argv[optind]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    optind++;
34b321
-    count = cvtnum(argv[optind]);
34b321
-    if (count < 0) {
34b321
-        printf("non-numeric length argument -- %s\n", argv[optind]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    gettimeofday(&t1, NULL);
34b321
-    ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
34b321
-                       count >> BDRV_SECTOR_BITS);
34b321
-    gettimeofday(&t2, NULL);
34b321
-
34b321
-    if (ret < 0) {
34b321
-        printf("discard failed: %s\n", strerror(-ret));
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    /* Finally, report back -- -C gives a parsable format */
34b321
-    if (!qflag) {
34b321
-        t2 = tsub(t2, t1);
34b321
-        print_report("discard", &t2, offset, count, count, 1, Cflag);
34b321
-    }
34b321
-
34b321
-out:
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static int alloc_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int64_t offset, sector_num;
34b321
-    int nb_sectors, remaining;
34b321
-    char s1[64];
34b321
-    int num, sum_alloc;
34b321
-    int ret;
34b321
-
34b321
-    offset = cvtnum(argv[1]);
34b321
-    if (offset < 0) {
34b321
-        printf("non-numeric offset argument -- %s\n", argv[1]);
34b321
-        return 0;
34b321
-    } else if (offset & 0x1ff) {
34b321
-        printf("offset %" PRId64 " is not sector aligned\n",
34b321
-               offset);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    if (argc == 3) {
34b321
-        nb_sectors = cvtnum(argv[2]);
34b321
-        if (nb_sectors < 0) {
34b321
-            printf("non-numeric length argument -- %s\n", argv[2]);
34b321
-            return 0;
34b321
-        }
34b321
-    } else {
34b321
-        nb_sectors = 1;
34b321
-    }
34b321
-
34b321
-    remaining = nb_sectors;
34b321
-    sum_alloc = 0;
34b321
-    sector_num = offset >> 9;
34b321
-    while (remaining) {
34b321
-        ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
34b321
-        if (ret < 0) {
34b321
-            printf("is_allocated failed: %s\n", strerror(-ret));
34b321
-            return 0;
34b321
-        }
34b321
-        sector_num += num;
34b321
-        remaining -= num;
34b321
-        if (ret) {
34b321
-            sum_alloc += num;
34b321
-        }
34b321
-        if (num == 0) {
34b321
-            nb_sectors -= remaining;
34b321
-            remaining = 0;
34b321
-        }
34b321
-    }
34b321
-
34b321
-    cvtstr(offset, s1, sizeof(s1));
34b321
-
34b321
-    printf("%d/%d sectors allocated at offset %s\n",
34b321
-           sum_alloc, nb_sectors, s1);
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t alloc_cmd = {
34b321
-    .name       = "alloc",
34b321
-    .altname    = "a",
34b321
-    .argmin     = 1,
34b321
-    .argmax     = 2,
34b321
-    .cfunc      = alloc_f,
34b321
-    .args       = "off [sectors]",
34b321
-    .oneline    = "checks if a sector is present in the file",
34b321
-};
34b321
-
34b321
-static int map_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int64_t offset;
34b321
-    int64_t nb_sectors;
34b321
-    char s1[64];
34b321
-    int num, num_checked;
34b321
-    int ret;
34b321
-    const char *retstr;
34b321
-
34b321
-    offset = 0;
34b321
-    nb_sectors = bs->total_sectors;
34b321
-
34b321
-    do {
34b321
-        num_checked = MIN(nb_sectors, INT_MAX);
34b321
-        ret = bdrv_is_allocated(bs, offset, num_checked, &num);
34b321
-        retstr = ret ? "    allocated" : "not allocated";
34b321
-        cvtstr(offset << 9ULL, s1, sizeof(s1));
34b321
-        printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
34b321
-               offset << 9ULL, num, num_checked, retstr, s1, ret);
34b321
-
34b321
-        offset += num;
34b321
-        nb_sectors -= num;
34b321
-    } while (offset < bs->total_sectors);
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t map_cmd = {
34b321
-       .name           = "map",
34b321
-       .argmin         = 0,
34b321
-       .argmax         = 0,
34b321
-       .cfunc          = map_f,
34b321
-       .args           = "",
34b321
-       .oneline        = "prints the allocated areas of a file",
34b321
-};
34b321
-
34b321
-static int break_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int ret;
34b321
-
34b321
-    ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
34b321
-    if (ret < 0) {
34b321
-        printf("Could not set breakpoint: %s\n", strerror(-ret));
34b321
-    }
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t break_cmd = {
34b321
-       .name           = "break",
34b321
-       .argmin         = 2,
34b321
-       .argmax         = 2,
34b321
-       .cfunc          = break_f,
34b321
-       .args           = "event tag",
34b321
-       .oneline        = "sets a breakpoint on event and tags the stopped "
34b321
-                         "request as tag",
34b321
-};
34b321
-
34b321
-static int resume_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    int ret;
34b321
-
34b321
-    ret = bdrv_debug_resume(bs, argv[1]);
34b321
-    if (ret < 0) {
34b321
-        printf("Could not resume request: %s\n", strerror(-ret));
34b321
-    }
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t resume_cmd = {
34b321
-       .name           = "resume",
34b321
-       .argmin         = 1,
34b321
-       .argmax         = 1,
34b321
-       .cfunc          = resume_f,
34b321
-       .args           = "tag",
34b321
-       .oneline        = "resumes the request tagged as tag",
34b321
-};
34b321
-
34b321
-static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    while (!bdrv_debug_is_suspended(bs, argv[1])) {
34b321
-        qemu_aio_wait();
34b321
-    }
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t wait_break_cmd = {
34b321
-       .name           = "wait_break",
34b321
-       .argmin         = 1,
34b321
-       .argmax         = 1,
34b321
-       .cfunc          = wait_break_f,
34b321
-       .args           = "tag",
34b321
-       .oneline        = "waits for the suspension of a request",
34b321
-};
34b321
-
34b321
-static int abort_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    abort();
34b321
-}
34b321
-
34b321
-static const cmdinfo_t abort_cmd = {
34b321
-       .name           = "abort",
34b321
-       .cfunc          = abort_f,
34b321
-       .flags          = CMD_NOFILE_OK,
34b321
-       .oneline        = "simulate a program crash using abort(3)",
34b321
-};
34b321
+extern int qemuio_misalign;
34b321
 
34b321
 static int close_f(BlockDriverState *bs, int argc, char **argv)
34b321
 {
34b321
@@ -1801,45 +48,6 @@ static const cmdinfo_t close_cmd = {
34b321
     .oneline    = "close the current open file",
34b321
 };
34b321
 
34b321
-static void sleep_cb(void *opaque)
34b321
-{
34b321
-    bool *expired = opaque;
34b321
-    *expired = true;
34b321
-}
34b321
-
34b321
-static int sleep_f(BlockDriverState *bs, int argc, char **argv)
34b321
-{
34b321
-    char *endptr;
34b321
-    long ms;
34b321
-    struct QEMUTimer *timer;
34b321
-    bool expired = false;
34b321
-
34b321
-    ms = strtol(argv[1], &endptr, 0);
34b321
-    if (ms < 0 || *endptr != '\0') {
34b321
-        printf("%s is not a valid number\n", argv[1]);
34b321
-        return 0;
34b321
-    }
34b321
-
34b321
-    timer = qemu_new_timer_ns(host_clock, sleep_cb, &expired);
34b321
-    qemu_mod_timer(timer, qemu_get_clock_ns(host_clock) + SCALE_MS * ms);
34b321
-
34b321
-    while (!expired) {
34b321
-        main_loop_wait(false);
34b321
-    }
34b321
-
34b321
-    qemu_free_timer(timer);
34b321
-
34b321
-    return 0;
34b321
-}
34b321
-
34b321
-static const cmdinfo_t sleep_cmd = {
34b321
-       .name           = "sleep",
34b321
-       .argmin         = 1,
34b321
-       .argmax         = 1,
34b321
-       .cfunc          = sleep_f,
34b321
-       .flags          = CMD_NOFILE_OK,
34b321
-       .oneline        = "waits for the given value in milliseconds",
34b321
-};
34b321
 
34b321
 static int openfile(char *name, int flags, int growable, QDict *opts)
34b321
 {
34b321
@@ -1962,18 +170,6 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
34b321
     return openfile(argv[optind], flags, growable, opts);
34b321
 }
34b321
 
34b321
-static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
34b321
-{
34b321
-    if (ct->flags & CMD_FLAG_GLOBAL) {
34b321
-        return 1;
34b321
-    }
34b321
-    if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
34b321
-        fprintf(stderr, "no file open, try 'help open'\n");
34b321
-        return 0;
34b321
-    }
34b321
-    return 1;
34b321
-}
34b321
-
34b321
 static void usage(const char *name)
34b321
 {
34b321
     printf(
34b321
@@ -2049,7 +245,7 @@ int main(int argc, char **argv)
34b321
             readonly = 1;
34b321
             break;
34b321
         case 'm':
34b321
-            misalign = 1;
34b321
+            qemuio_misalign = 1;
34b321
             break;
34b321
         case 'g':
34b321
             growable = 1;
34b321
@@ -2090,31 +286,8 @@ int main(int argc, char **argv)
34b321
 
34b321
     /* initialize commands */
34b321
     quit_init();
34b321
-    help_init();
34b321
     add_command(&open_cmd);
34b321
     add_command(&close_cmd);
34b321
-    add_command(&read_cmd);
34b321
-    add_command(&readv_cmd);
34b321
-    add_command(&write_cmd);
34b321
-    add_command(&writev_cmd);
34b321
-    add_command(&multiwrite_cmd);
34b321
-    add_command(&aio_read_cmd);
34b321
-    add_command(&aio_write_cmd);
34b321
-    add_command(&aio_flush_cmd);
34b321
-    add_command(&flush_cmd);
34b321
-    add_command(&truncate_cmd);
34b321
-    add_command(&length_cmd);
34b321
-    add_command(&info_cmd);
34b321
-    add_command(&discard_cmd);
34b321
-    add_command(&alloc_cmd);
34b321
-    add_command(&map_cmd);
34b321
-    add_command(&break_cmd);
34b321
-    add_command(&resume_cmd);
34b321
-    add_command(&wait_break_cmd);
34b321
-    add_command(&abort_cmd);
34b321
-    add_command(&sleep_cmd);
34b321
-
34b321
-    add_check_command(init_check_command);
34b321
 
34b321
     /* open the device */
34b321
     if (!readonly) {
34b321
-- 
34b321
1.8.3.1
34b321