{"id":813573,"url":"http://patchwork.ozlabs.org/api/patches/813573/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170913181910.29688-18-mreitz@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170913181910.29688-18-mreitz@redhat.com>","list_archive_url":null,"date":"2017-09-13T18:19:09","name":"[17/18] qemu-io: Add background write","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"be6836f91e9f1d200a75d34d07cd8b7df05ecbf1","submitter":{"id":36836,"url":"http://patchwork.ozlabs.org/api/people/36836/?format=json","name":"Max Reitz","email":"mreitz@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170913181910.29688-18-mreitz@redhat.com/mbox/","series":[{"id":2960,"url":"http://patchwork.ozlabs.org/api/series/2960/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=2960","date":"2017-09-13T18:18:52","name":"block/mirror: Add active-sync mirroring","version":1,"mbox":"http://patchwork.ozlabs.org/series/2960/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/813573/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/813573/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=mreitz@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xsr2j58kQz9rxl\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu, 14 Sep 2017 04:35:56 +1000 (AEST)","from localhost ([::1]:44082 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dsCVr-0005Jw-04\n\tfor incoming@patchwork.ozlabs.org; Wed, 13 Sep 2017 14:35:51 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:37949)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dsCId-0001JG-4v\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 14:22:12 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <mreitz@redhat.com>) id 1dsCIb-0006hg-UX\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 14:22:11 -0400","from mx1.redhat.com ([209.132.183.28]:47404)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <mreitz@redhat.com>)\n\tid 1dsCIV-0006cd-7K; Wed, 13 Sep 2017 14:22:03 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 61CF7356D9;\n\tWed, 13 Sep 2017 18:22:02 +0000 (UTC)","from localhost (ovpn-204-23.brq.redhat.com [10.40.204.23])\n\tby smtp.corp.redhat.com (Postfix) with ESMTPS id D05265D6AE;\n\tWed, 13 Sep 2017 18:22:01 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 61CF7356D9","From":"Max Reitz <mreitz@redhat.com>","To":"qemu-block@nongnu.org","Date":"Wed, 13 Sep 2017 20:19:09 +0200","Message-Id":"<20170913181910.29688-18-mreitz@redhat.com>","In-Reply-To":"<20170913181910.29688-1-mreitz@redhat.com>","References":"<20170913181910.29688-1-mreitz@redhat.com>","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.30]);\n\tWed, 13 Sep 2017 18:22:02 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PATCH 17/18] qemu-io: Add background write","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Kevin Wolf <kwolf@redhat.com>, Fam Zheng <famz@redhat.com>,\n\tqemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,\n\tStefan Hajnoczi <stefanha@redhat.com>, John Snow <jsnow@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"Add a new parameter -B to qemu-io's write command.  When used, qemu-io\nwill not wait for the result of the operation and instead execute it in\nthe background.\n\nSigned-off-by: Max Reitz <mreitz@redhat.com>\n---\n qemu-io-cmds.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----\n 1 file changed, 77 insertions(+), 6 deletions(-)","diff":"diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c\nindex 2811a89099..c635a248f5 100644\n--- a/qemu-io-cmds.c\n+++ b/qemu-io-cmds.c\n@@ -481,6 +481,62 @@ static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,\n typedef struct {\n     BlockBackend *blk;\n     int64_t offset;\n+    int bytes;\n+    char *buf;\n+    int flags;\n+} CoBackgroundWrite;\n+\n+static void coroutine_fn co_background_pwrite_entry(void *opaque)\n+{\n+    CoBackgroundWrite *data = opaque;\n+    QEMUIOVector qiov;\n+    int ret;\n+\n+    qemu_iovec_init(&qiov, 1);\n+    qemu_iovec_add(&qiov, data->buf, data->bytes);\n+\n+    ret = blk_co_pwritev(data->blk, data->offset, data->bytes, &qiov,\n+                         data->flags);\n+\n+    qemu_iovec_destroy(&qiov);\n+    g_free(data->buf);\n+\n+    if (ret < 0) {\n+        Error *err;\n+        error_setg_errno(&err, -ret, \"Background write failed\");\n+        error_report_err(err);\n+    }\n+}\n+\n+/* Takes ownership of @buf */\n+static int do_background_pwrite(BlockBackend *blk, char *buf, int64_t offset,\n+                                int64_t bytes, int flags)\n+{\n+    Coroutine *co;\n+    CoBackgroundWrite *data;\n+\n+    if (bytes > INT_MAX) {\n+        return -ERANGE;\n+    }\n+\n+    data = g_new(CoBackgroundWrite, 1);\n+    *data = (CoBackgroundWrite){\n+        .blk    = blk,\n+        .offset = offset,\n+        .bytes  = bytes,\n+        .buf    = buf,\n+        .flags  = flags,\n+    };\n+\n+    co = qemu_coroutine_create(co_background_pwrite_entry, data);\n+    bdrv_coroutine_enter(blk_bs(blk), co);\n+\n+    return bytes;\n+}\n+\n+typedef struct {\n+    BlockBackend *blk;\n+    int64_t offset;\n     int64_t bytes;\n     int64_t *total;\n     int flags;\n@@ -931,6 +987,7 @@ static void write_help(void)\n \" Writes into a segment of the currently open file, using a buffer\\n\"\n \" filled with a set pattern (0xcdcdcdcd).\\n\"\n \" -b, -- write to the VM state rather than the virtual disk\\n\"\n+\" -B, -- just start a background write, do not wait for the result\\n\"\n \" -c, -- write compressed data with blk_write_compressed\\n\"\n \" -f, -- use Force Unit Access semantics\\n\"\n \" -p, -- ignored for backwards compatibility\\n\"\n@@ -951,7 +1008,7 @@ static const cmdinfo_t write_cmd = {\n     .perm       = BLK_PERM_WRITE,\n     .argmin     = 2,\n     .argmax     = -1,\n-    .args       = \"[-bcCfquz] [-P pattern] off len\",\n+    .args       = \"[-bBcCfquz] [-P pattern] off len\",\n     .oneline    = \"writes a number of bytes at a specified offset\",\n     .help       = write_help,\n };\n@@ -961,6 +1018,7 @@ static int write_f(BlockBackend *blk, int argc, char **argv)\n     struct timeval t1, t2;\n     bool Cflag = false, qflag = false, bflag = false;\n     bool Pflag = false, zflag = false, cflag = false;\n+    bool background = false;\n     int flags = 0;\n     int c, cnt;\n     char *buf = NULL;\n@@ -970,11 +1028,14 @@ static int write_f(BlockBackend *blk, int argc, char **argv)\n     int64_t total = 0;\n     int pattern = 0xcd;\n \n-    while ((c = getopt(argc, argv, \"bcCfpP:quz\")) != -1) {\n+    while ((c = getopt(argc, argv, \"bBcCfpP:quz\")) != -1) {\n         switch (c) {\n         case 'b':\n             bflag = true;\n             break;\n+        case 'B':\n+            background = true;\n+            break;\n         case 'c':\n             cflag = true;\n             break;\n@@ -1032,6 +1093,11 @@ static int write_f(BlockBackend *blk, int argc, char **argv)\n         return 0;\n     }\n \n+    if (background && (bflag || cflag || zflag)) {\n+        printf(\"-B cannot be specified together with -b, -c, or -z\\n\");\n+        return 0;\n+    }\n+\n     offset = cvtnum(argv[optind]);\n     if (offset < 0) {\n         print_cvtnum_err(offset, argv[optind]);\n@@ -1074,6 +1140,8 @@ static int write_f(BlockBackend *blk, int argc, char **argv)\n         cnt = do_co_pwrite_zeroes(blk, offset, count, flags, &total);\n     } else if (cflag) {\n         cnt = do_write_compressed(blk, buf, offset, count, &total);\n+    } else if (background) {\n+        cnt = do_background_pwrite(blk, buf, offset, count, flags);\n     } else {\n         cnt = do_pwrite(blk, buf, offset, count, flags, &total);\n     }\n@@ -1088,12 +1156,15 @@ static int write_f(BlockBackend *blk, int argc, char **argv)\n         goto out;\n     }\n \n-    /* Finally, report back -- -C gives a parsable format */\n-    t2 = tsub(t2, t1);\n-    print_report(\"wrote\", &t2, offset, count, total, cnt, Cflag);\n+    if (!background) {\n+        /* Finally, report back -- -C gives a parsable format */\n+        t2 = tsub(t2, t1);\n+        print_report(\"wrote\", &t2, offset, count, total, cnt, Cflag);\n+    }\n \n out:\n-    if (!zflag) {\n+    /* do_background_pwrite() takes ownership of the buffer */\n+    if (!zflag && !background) {\n         qemu_io_free(buf);\n     }\n \n","prefixes":["17/18"]}