]> git.proxmox.com Git - qemu.git/blobdiff - qemu-io.c
virtio-blk: Avoid zeroing every request structure
[qemu.git] / qemu-io.c
index 1bf94e1cda2544eceb082c387dc9a20cbe034dba..8517b909c7a0374e5c6e3060d016ea82eadaeacf 100644 (file)
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -7,10 +7,12 @@
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
  */
+#include <sys/time.h>
 #include <sys/types.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <getopt.h>
+#include <libgen.h>
 
 #include "qemu-common.h"
 #include "block_int.h"
@@ -25,6 +27,26 @@ static BlockDriverState *bs;
 
 static int misalign;
 
+/*
+ * Parse the pattern argument to various sub-commands.
+ *
+ * Because the pattern is used as an argument to memset it must evaluate
+ * to an unsigned integer that fits into a single byte.
+ */
+static int parse_pattern(const char *arg)
+{
+       char *endptr = NULL;
+       long pattern;
+
+       pattern = strtol(arg, &endptr, 0);
+       if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
+               printf("%s is not a valid pattern byte\n", arg);
+               return -1;
+       }
+
+       return pattern;
+}
+
 /*
  * Memory allocation helpers.
  *
@@ -107,7 +129,8 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
 {
        size_t *sizes = calloc(nr_iov, sizeof(size_t));
        size_t count = 0;
-       void *buf, *p;
+       void *buf = NULL;
+       void *p;
        int i;
 
        for (i = 0; i < nr_iov; i++) {
@@ -117,19 +140,19 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
                len = cvtnum(arg);
                if (len < 0) {
                        printf("non-numeric length argument -- %s\n", arg);
-                       return NULL;
+                       goto fail;
                }
 
                /* should be SIZE_T_MAX, but that doesn't exist */
                if (len > UINT_MAX) {
                        printf("too large length argument -- %s\n", arg);
-                       return NULL;
+                       goto fail;
                }
 
                if (len & 0x1ff) {
                        printf("length argument %lld is not sector aligned\n",
                                len);
-                       return NULL;
+                       goto fail;
                }
 
                sizes[i] = len;
@@ -145,6 +168,7 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
                p += sizes[i];
        }
 
+fail:
        free(sizes);
        return buf;
 }
@@ -244,8 +268,6 @@ static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
 }
 
 
-static const cmdinfo_t read_cmd;
-
 static void
 read_help(void)
 {
@@ -269,6 +291,19 @@ read_help(void)
 "\n");
 }
 
+static int read_f(int argc, char **argv);
+
+static const cmdinfo_t read_cmd = {
+       .name           = "read",
+       .altname        = "r",
+       .cfunc          = read_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
+       .oneline        = "reads a number of bytes at a specified offset",
+       .help           = read_help,
+};
+
 static int
 read_f(int argc, char **argv)
 {
@@ -304,7 +339,9 @@ read_f(int argc, char **argv)
                        break;
                case 'P':
                        Pflag = 1;
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                case 'q':
                        qflag = 1;
@@ -415,19 +452,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t read_cmd = {
-       .name           = "read",
-       .altname        = "r",
-       .cfunc          = read_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
-       .oneline        = "reads a number of bytes at a specified offset",
-       .help           = read_help,
-};
-
-static const cmdinfo_t readv_cmd;
-
 static void
 readv_help(void)
 {
@@ -448,6 +472,18 @@ readv_help(void)
 "\n");
 }
 
+static int readv_f(int argc, char **argv);
+
+static const cmdinfo_t readv_cmd = {
+       .name           = "readv",
+       .cfunc          = readv_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cqv] [-P pattern ] off len [len..]",
+       .oneline        = "reads a number of bytes at a specified offset",
+       .help           = readv_help,
+};
+
 static int
 readv_f(int argc, char **argv)
 {
@@ -456,7 +492,8 @@ readv_f(int argc, char **argv)
        int c, cnt;
        char *buf;
        int64_t offset;
-       int total;
+        /* Some compilers get confused and warn if this is not initialized.  */
+        int total = 0;
        int nr_iov;
        QEMUIOVector qiov;
        int pattern = 0;
@@ -469,7 +506,9 @@ readv_f(int argc, char **argv)
                        break;
                case 'P':
                        Pflag = 1;
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                case 'q':
                        qflag = 1;
@@ -537,18 +576,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t readv_cmd = {
-       .name           = "readv",
-       .cfunc          = readv_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cqv] [-P pattern ] off len [len..]",
-       .oneline        = "reads a number of bytes at a specified offset",
-       .help           = readv_help,
-};
-
-static const cmdinfo_t write_cmd;
-
 static void
 write_help(void)
 {
@@ -569,6 +596,19 @@ write_help(void)
 "\n");
 }
 
+static int write_f(int argc, char **argv);
+
+static const cmdinfo_t write_cmd = {
+       .name           = "write",
+       .altname        = "w",
+       .cfunc          = write_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-abCpq] [-P pattern ] off len",
+       .oneline        = "writes a number of bytes at a specified offset",
+       .help           = write_help,
+};
+
 static int
 write_f(int argc, char **argv)
 {
@@ -594,7 +634,9 @@ write_f(int argc, char **argv)
                        pflag = 1;
                        break;
                case 'P':
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                case 'q':
                        qflag = 1;
@@ -668,19 +710,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t write_cmd = {
-       .name           = "write",
-       .altname        = "w",
-       .cfunc          = write_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-abCpq] [-P pattern ] off len",
-       .oneline        = "writes a number of bytes at a specified offset",
-       .help           = write_help,
-};
-
-static const cmdinfo_t writev_cmd;
-
 static void
 writev_help(void)
 {
@@ -699,6 +728,18 @@ writev_help(void)
 "\n");
 }
 
+static int writev_f(int argc, char **argv);
+
+static const cmdinfo_t writev_cmd = {
+       .name           = "writev",
+       .cfunc          = writev_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cq] [-P pattern ] off len [len..]",
+       .oneline        = "writes a number of bytes at a specified offset",
+       .help           = writev_help,
+};
+
 static int
 writev_f(int argc, char **argv)
 {
@@ -707,7 +748,8 @@ writev_f(int argc, char **argv)
        int c, cnt;
        char *buf;
        int64_t offset;
-       int total;
+        /* Some compilers get confused and warn if this is not initialized.  */
+        int total = 0;
        int nr_iov;
        int pattern = 0xcd;
        QEMUIOVector qiov;
@@ -721,7 +763,9 @@ writev_f(int argc, char **argv)
                        qflag = 1;
                        break;
                case 'P':
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                default:
                        return command_usage(&writev_cmd);
@@ -767,16 +811,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t writev_cmd = {
-       .name           = "writev",
-       .cfunc          = writev_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cq] [-P pattern ] off len [len..]",
-       .oneline        = "writes a number of bytes at a specified offset",
-       .help           = writev_help,
-};
-
 struct aio_ctx {
        QEMUIOVector qiov;
        int64_t offset;
@@ -816,8 +850,6 @@ out:
        free(ctx);
 }
 
-static const cmdinfo_t aio_read_cmd;
-
 static void
 aio_read_done(void *opaque, int ret)
 {
@@ -872,8 +904,8 @@ aio_read_help(void)
 "\n"
 " Reads a segment of the currently open file, optionally dumping it to the\n"
 " standard output stream (with -v option) for subsequent inspection.\n"
-" The read is performed asynchronously and should the aio_flush command \n"
-" should be used to ensure all outstanding aio requests have been completed\n"
+" The read is performed asynchronously and the aio_flush command must be\n"
+" used to ensure all outstanding aio requests have been completed\n"
 " -C, -- report statistics in a machine parsable format\n"
 " -P, -- use a pattern to verify read data\n"
 " -v, -- dump buffer to standard output\n"
@@ -881,6 +913,18 @@ aio_read_help(void)
 "\n");
 }
 
+static int aio_read_f(int argc, char **argv);
+
+static const cmdinfo_t aio_read_cmd = {
+       .name           = "aio_read",
+       .cfunc          = aio_read_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cqv] [-P pattern ] off len [len..]",
+       .oneline        = "asynchronously reads a number of bytes",
+       .help           = aio_read_help,
+};
+
 static int
 aio_read_f(int argc, char **argv)
 {
@@ -895,7 +939,9 @@ aio_read_f(int argc, char **argv)
                        break;
                case 'P':
                        ctx->Pflag = 1;
-                       ctx->pattern = atoi(optarg);
+                       ctx->pattern = parse_pattern(optarg);
+                       if (ctx->pattern < 0)
+                               return 0;
                        break;
                case 'q':
                        ctx->qflag = 1;
@@ -944,18 +990,6 @@ aio_read_f(int argc, char **argv)
        return 0;
 }
 
-static const cmdinfo_t aio_read_cmd = {
-       .name           = "aio_read",
-       .cfunc          = aio_read_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cqv] [-P pattern ] off len [len..]",
-       .oneline        = "asynchronously reads a number of bytes",
-       .help           = aio_read_help,
-};
-
-static const cmdinfo_t aio_write_cmd;
-
 static void
 aio_write_help(void)
 {
@@ -969,14 +1003,25 @@ aio_write_help(void)
 "\n"
 " Writes into a segment of the currently open file, using a buffer\n"
 " filled with a set pattern (0xcdcdcdcd).\n"
-" The write is performed asynchronously and should the aio_flush command \n"
-" should be used to ensure all outstanding aio requests have been completed\n"
+" The write is performed asynchronously and the aio_flush command must be\n"
+" used to ensure all outstanding aio requests have been completed\n"
 " -P, -- use different pattern to fill file\n"
 " -C, -- report statistics in a machine parsable format\n"
 " -q, -- quite mode, do not show I/O statistics\n"
 "\n");
 }
 
+static int aio_write_f(int argc, char **argv);
+
+static const cmdinfo_t aio_write_cmd = {
+       .name           = "aio_write",
+       .cfunc          = aio_write_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cq] [-P pattern ] off len [len..]",
+       .oneline        = "asynchronously writes a number of bytes",
+       .help           = aio_write_help,
+};
 
 static int
 aio_write_f(int argc, char **argv)
@@ -995,7 +1040,9 @@ aio_write_f(int argc, char **argv)
                        ctx->qflag = 1;
                        break;
                case 'P':
-                       pattern = atoi(optarg);
+                       pattern = parse_pattern(optarg);
+                       if (pattern < 0)
+                               return 0;
                        break;
                default:
                        free(ctx);
@@ -1038,16 +1085,6 @@ aio_write_f(int argc, char **argv)
        return 0;
 }
 
-static const cmdinfo_t aio_write_cmd = {
-       .name           = "aio_write",
-       .cfunc          = aio_write_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cq] [-P pattern ] off len [len..]",
-       .oneline        = "asynchronously writes a number of bytes",
-       .help           = aio_write_help,
-};
-
 static int
 aio_flush_f(int argc, char **argv)
 {
@@ -1058,7 +1095,7 @@ aio_flush_f(int argc, char **argv)
 static const cmdinfo_t aio_flush_cmd = {
        .name           = "aio_flush",
        .cfunc          = aio_flush_f,
-       .oneline        = "completes all outstanding aio requets"
+       .oneline        = "completes all outstanding aio requests"
 };
 
 static int
@@ -1170,11 +1207,10 @@ static int
 alloc_f(int argc, char **argv)
 {
        int64_t offset;
-       int nb_sectors;
+       int nb_sectors, remaining;
        char s1[64];
-       int num;
+       int num, sum_alloc;
        int ret;
-       const char *retstr;
 
        offset = cvtnum(argv[1]);
        if (offset & 0x1ff) {
@@ -1188,16 +1224,23 @@ alloc_f(int argc, char **argv)
        else
                nb_sectors = 1;
 
-       ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
+       remaining = nb_sectors;
+       sum_alloc = 0;
+       while (remaining) {
+               ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
+               remaining -= num;
+               if (ret) {
+                       sum_alloc += num;
+               }
+       }
 
        cvtstr(offset, s1, sizeof(s1));
 
-       retstr = ret ? "allocated" : "not allocated";
        if (nb_sectors == 1)
-               printf("sector %s at offset %s\n", retstr, s1);
+               printf("sector allocated at offset %s\n", s1);
        else
-               printf("%d/%d sectors %s at offset %s\n",
-                       num, nb_sectors, retstr, s1);
+               printf("%d/%d sectors allocated at offset %s\n",
+                       sum_alloc, nb_sectors, s1);
        return 0;
 }
 
@@ -1233,25 +1276,21 @@ static int openfile(char *name, int flags, int growable)
                return 1;
        }
 
-       bs = bdrv_new("hda");
-       if (!bs)
-               return 1;
-
-       if (bdrv_open(bs, name, flags) == -1) {
-               fprintf(stderr, "%s: can't open device %s\n", progname, name);
-               bs = NULL;
-               return 1;
-       }
-
-
        if (growable) {
-               if (!bs->drv || !bs->drv->protocol_name) {
-                       fprintf(stderr,
-                               "%s: only protocols can be opened growable\n",
-                               progname);
+               if (bdrv_file_open(&bs, name, flags)) {
+                       fprintf(stderr, "%s: can't open device %s\n", progname, name);
+                       return 1;
+               }
+       } else {
+               bs = bdrv_new("hda");
+               if (!bs)
+                       return 1;
+
+               if (bdrv_open(bs, name, flags, NULL) < 0) {
+                       fprintf(stderr, "%s: can't open device %s\n", progname, name);
+                       bs = NULL;
                        return 1;
                }
-               bs->growable = 1;
        }
 
        return 0;
@@ -1268,7 +1307,6 @@ open_help(void)
 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
 "\n"
 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
-" -C, -- create new file if it doesn't exist\n"
 " -r, -- open file read-only\n"
 " -s, -- use snapshot file\n"
 " -n, -- disable host cache\n"
@@ -1276,7 +1314,19 @@ open_help(void)
 "\n");
 }
 
-static const cmdinfo_t open_cmd;
+static int open_f(int argc, char **argv);
+
+static const cmdinfo_t open_cmd = {
+       .name           = "open",
+       .altname        = "o",
+       .cfunc          = open_f,
+       .argmin         = 1,
+       .argmax         = -1,
+       .flags          = CMD_NOFILE_OK,
+       .args           = "[-Crsn] [path]",
+       .oneline        = "open the file specified by path",
+       .help           = open_help,
+};
 
 static int
 open_f(int argc, char **argv)
@@ -1286,7 +1336,7 @@ open_f(int argc, char **argv)
        int growable = 0;
        int c;
 
-       while ((c = getopt(argc, argv, "snCrg")) != EOF) {
+       while ((c = getopt(argc, argv, "snrg")) != EOF) {
                switch (c) {
                case 's':
                        flags |= BDRV_O_SNAPSHOT;
@@ -1294,9 +1344,6 @@ open_f(int argc, char **argv)
                case 'n':
                        flags |= BDRV_O_NOCACHE;
                        break;
-               case 'C':
-                       flags |= BDRV_O_CREAT;
-                       break;
                case 'r':
                        readonly = 1;
                        break;
@@ -1308,10 +1355,9 @@ open_f(int argc, char **argv)
                }
        }
 
-       if (readonly)
-               flags |= BDRV_O_RDONLY;
-       else
-               flags |= BDRV_O_RDWR;
+       if (!readonly) {
+            flags |= BDRV_O_RDWR;
+        }
 
        if (optind != argc - 1)
                return command_usage(&open_cmd);
@@ -1319,18 +1365,6 @@ open_f(int argc, char **argv)
        return openfile(argv[optind], flags, growable);
 }
 
-static const cmdinfo_t open_cmd = {
-       .name           = "open",
-       .altname        = "o",
-       .cfunc          = open_f,
-       .argmin         = 1,
-       .argmax         = -1,
-       .flags          = CMD_NOFILE_OK,
-       .args           = "[-Crsn] [path]",
-       .oneline        = "open the file specified by path",
-       .help           = open_help,
-};
-
 static int
 init_args_command(
         int     index)
@@ -1357,15 +1391,16 @@ init_check_command(
 static void usage(const char *name)
 {
        printf(
-"Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
+"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
 "QEMU Disk exerciser\n"
 "\n"
-"  -C, --create         create new file if it doesn't exist\n"
 "  -c, --cmd            command to execute\n"
 "  -r, --read-only      export read-only\n"
 "  -s, --snapshot       use snapshot file\n"
 "  -n, --nocache        disable host cache\n"
+"  -g, --growable       allow file to grow (only applies to protocols)\n"
 "  -m, --misalign       misalign allocations for O_DIRECT\n"
+"  -k, --native-aio     use kernel AIO implementation (on Linux only)\n"
 "  -h, --help           display this help and exit\n"
 "  -V, --version        output version information and exit\n"
 "\n",
@@ -1377,19 +1412,19 @@ int main(int argc, char **argv)
 {
        int readonly = 0;
        int growable = 0;
-       const char *sopt = "hVc:Crsnmg";
-       struct option lopt[] = {
-               { "help", 0, 0, 'h' },
-               { "version", 0, 0, 'V' },
-               { "offset", 1, 0, 'o' },
-               { "cmd", 1, 0, 'c' },
-               { "create", 0, 0, 'C' },
-               { "read-only", 0, 0, 'r' },
-               { "snapshot", 0, 0, 's' },
-               { "nocache", 0, 0, 'n' },
-               { "misalign", 0, 0, 'm' },
-               { "growable", 0, 0, 'g' },
-               { NULL, 0, 0, 0 }
+       const char *sopt = "hVc:rsnmgk";
+        const struct option lopt[] = {
+               { "help", 0, NULL, 'h' },
+               { "version", 0, NULL, 'V' },
+               { "offset", 1, NULL, 'o' },
+               { "cmd", 1, NULL, 'c' },
+               { "read-only", 0, NULL, 'r' },
+               { "snapshot", 0, NULL, 's' },
+               { "nocache", 0, NULL, 'n' },
+               { "misalign", 0, NULL, 'm' },
+               { "growable", 0, NULL, 'g' },
+               { "native-aio", 0, NULL, 'k' },
+               { NULL, 0, NULL, 0 }
        };
        int c;
        int opt_index = 0;
@@ -1408,9 +1443,6 @@ int main(int argc, char **argv)
                case 'c':
                        add_user_command(optarg);
                        break;
-               case 'C':
-                       flags |= BDRV_O_CREAT;
-                       break;
                case 'r':
                        readonly = 1;
                        break;
@@ -1420,6 +1452,9 @@ int main(int argc, char **argv)
                case 'g':
                        growable = 1;
                        break;
+               case 'k':
+                       flags |= BDRV_O_NATIVE_AIO;
+                       break;
                case 'V':
                        printf("%s version %s\n", progname, VERSION);
                        exit(0);
@@ -1461,10 +1496,9 @@ int main(int argc, char **argv)
        add_check_command(init_check_command);
 
        /* open the device */
-       if (readonly)
-               flags |= BDRV_O_RDONLY;
-       else
-               flags |= BDRV_O_RDWR;
+       if (!readonly) {
+            flags |= BDRV_O_RDWR;
+        }
 
        if ((argc - optind) == 1)
                openfile(argv[optind], flags, growable);