<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 7d1eb32b705304a4230726638c5cd5ddd3969f91 Mon Sep 17 00:00:00 2001
From: Jeffrey Cody &lt;jcody@redhat.com&gt;
Date: Wed, 21 Mar 2012 21:55:06 +0100
Subject: [PATCH 39/55] block: add image streaming block job

RH-Author: Jeffrey Cody &lt;jcody@redhat.com&gt;
Message-id: &lt;1b61917a4947dde01736a89b84292b27d9efe66a.1332362400.git.jcody@redhat.com&gt;
Patchwork-id: 38889
O-Subject: [RHEL6.3 qemu-kvm PATCH v8 39/54] block: add image streaming block job
Bugzilla: 582475
RH-Acked-by: Paolo Bonzini &lt;pbonzini@redhat.com&gt;
RH-Acked-by: Marcelo Tosatti &lt;mtosatti@redhat.com&gt;
RH-Acked-by: Kevin Wolf &lt;kwolf@redhat.com&gt;

From: Stefan Hajnoczi &lt;stefanha@linux.vnet.ibm.com&gt;

Signed-off-by: Stefan Hajnoczi &lt;stefanha@linux.vnet.ibm.com&gt;
Signed-off-by: Kevin Wolf &lt;kwolf@redhat.com&gt;

(cherry picked from commit 4f1043b4ffdd6f130cab7c1d8d59d833a475adac)

Signed-off-by: Stefan Hajnoczi &lt;stefanha@linux.vnet.ibm.com&gt;
Signed-off-by: Anthony Liguori &lt;aliguori@us.ibm.com&gt;
Signed-off-by: Jeff Cody &lt;jcody@redhat.com&gt;
---
 Makefile.objs  |    1 +
 block/stream.c |  133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block_int.h    |    3 +
 trace-events   |    4 ++
 4 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 block/stream.c

Signed-off-by: Michal Novotny &lt;minovotn@redhat.com&gt;
---
 Makefile.objs  |    1 +
 block/stream.c |  133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block_int.h    |    3 +
 trace-events   |    4 ++
 4 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 block/stream.c

diff --git a/Makefile.objs b/Makefile.objs
index 716b346..d21d747 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -26,6 +26,7 @@ block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow
 block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-nested-y += qed-check.o
 block-nested-y += parallels.o nbd.o blkdebug.o
+block-nested-y += stream.o
 block-nested-$(CONFIG_WIN32) += raw-win32.o
 block-nested-$(CONFIG_POSIX) += raw-posix.o
 block-nested-$(CONFIG_CURL) += curl.o
diff --git a/block/stream.c b/block/stream.c
new file mode 100644
index 0000000..886f704
--- /dev/null
+++ b/block/stream.c
@@ -0,0 +1,133 @@
+/*
+ * Image streaming
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Stefan Hajnoczi   &lt;stefanha@linux.vnet.ibm.com&gt;
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include "trace.h"
+#include "block_int.h"
+
+enum {
+    /*
+     * Size of data buffer for populating the image file.  This should be large
+     * enough to process multiple clusters in a single call, so that populating
+     * contiguous regions of the image is efficient.
+     */
+    STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
+};
+
+typedef struct StreamBlockJob {
+    BlockJob common;
+    BlockDriverState *base;
+} StreamBlockJob;
+
+static int coroutine_fn stream_populate(BlockDriverState *bs,
+                                        int64_t sector_num, int nb_sectors,
+                                        void *buf)
+{
+    struct iovec iov = {
+        .iov_base = buf,
+        .iov_len  = nb_sectors * BDRV_SECTOR_SIZE,
+    };
+    QEMUIOVector qiov;
+
+    qemu_iovec_init_external(&amp;qiov, &amp;iov, 1);
+
+    /* Copy-on-read the unallocated clusters */
+    return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &amp;qiov);
+}
+
+static void coroutine_fn stream_run(void *opaque)
+{
+    StreamBlockJob *s = opaque;
+    BlockDriverState *bs = s-&gt;common.bs;
+    int64_t sector_num, end;
+    int ret = 0;
+    int n;
+    void *buf;
+
+    s-&gt;common.len = bdrv_getlength(bs);
+    if (s-&gt;common.len &lt; 0) {
+        block_job_complete(&amp;s-&gt;common, s-&gt;common.len);
+        return;
+    }
+
+    end = s-&gt;common.len &gt;&gt; BDRV_SECTOR_BITS;
+    buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
+
+    /* Turn on copy-on-read for the whole block device so that guest read
+     * requests help us make progress.  Only do this when copying the entire
+     * backing chain since the copy-on-read operation does not take base into
+     * account.
+     */
+    if (!s-&gt;base) {
+        bdrv_enable_copy_on_read(bs);
+    }
+
+    for (sector_num = 0; sector_num &lt; end; sector_num += n) {
+        if (block_job_is_cancelled(&amp;s-&gt;common)) {
+            break;
+        }
+
+        /* TODO rate-limit */
+        /* Note that even when no rate limit is applied we need to yield with
+         * no pending I/O here so that qemu_aio_flush() is able to return.
+         */
+        co_sleep(rt_clock, 0);
+
+        ret = bdrv_co_is_allocated(bs, sector_num,
+                                   STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &amp;n);
+        trace_stream_one_iteration(s, sector_num, n, ret);
+        if (ret == 0) {
+            ret = stream_populate(bs, sector_num, n, buf);
+        }
+        if (ret &lt; 0) {
+            break;
+        }
+
+        /* Publish progress */
+        s-&gt;common.offset += n * BDRV_SECTOR_SIZE;
+    }
+
+    if (!s-&gt;base) {
+        bdrv_disable_copy_on_read(bs);
+    }
+
+    if (sector_num == end &amp;&amp; ret == 0) {
+        ret = bdrv_change_backing_file(bs, NULL, NULL);
+    }
+
+    qemu_vfree(buf);
+    block_job_complete(&amp;s-&gt;common, ret);
+}
+
+static BlockJobType stream_job_type = {
+    .instance_size = sizeof(StreamBlockJob),
+    .job_type      = "stream",
+};
+
+int stream_start(BlockDriverState *bs, BlockDriverState *base,
+                 BlockDriverCompletionFunc *cb, void *opaque)
+{
+    StreamBlockJob *s;
+    Coroutine *co;
+
+    s = block_job_create(&amp;stream_job_type, bs, cb, opaque);
+    if (!s) {
+        return -EBUSY; /* bs must already be in use */
+    }
+
+    s-&gt;base = base;
+
+    co = qemu_coroutine_create(stream_run);
+    trace_stream_start(bs, base, s, co, opaque);
+    qemu_coroutine_enter(co, s);
+    return 0;
+}
diff --git a/block_int.h b/block_int.h
index a99ddc2..c61604c 100644
--- a/block_int.h
+++ b/block_int.h
@@ -282,6 +282,9 @@ int block_job_set_speed(BlockJob *job, int64_t value);
 void block_job_cancel(BlockJob *job);
 bool block_job_is_cancelled(BlockJob *job);
 
+int stream_start(BlockDriverState *bs, BlockDriverState *base,
+                 BlockDriverCompletionFunc *cb, void *opaque);
+
 typedef struct BlockConf {
     BlockDriverState *bs;
     uint16_t physical_block_size;
diff --git a/trace-events b/trace-events
index 6725413..bf83d7a 100644
--- a/trace-events
+++ b/trace-events
@@ -62,6 +62,10 @@ disable bdrv_co_io_em(void *bs, int64_t sector_num, int nb_sectors, int is_write
 disable bdrv_co_copy_on_readv(void *bs, int64_t sector_num, int nb_sector) "bs %p sector_num %"PRId64" nb_sectors %d"
 disable bdrv_co_do_copy_on_readv(void *bs, int64_t sector_num, int nb_sectors, int64_t cluster_sector_num, int cluster_nb_sectors) "bs %p sector_num %"PRId64" nb_sectors %d cluster_sector_num %"PRId64" cluster_nb_sectors %d"
 
+# block/stream.c
+disable stream_one_iteration(void *s, int64_t sector_num, int nb_sectors, int is_allocated) "s %p sector_num %"PRId64" nb_sectors %d is_allocated %d"
+disable stream_start(void *bs, void *base, void *s, void *co, void *opaque) "bs %p base %p s %p co %p opaque %p"
+
 # hw/virtio-blk.c
 disable virtio_blk_req_complete(void *req, int status) "req %p status %d"
 disable virtio_blk_rw_complete(void *req, int ret) "req %p ret %d"
-- 
1.7.7.6

</pre></body></html>