Blame SOURCES/kvm-qcow2-Implement-copy-offloading.patch

357786
From 3650b202ee9499c9c234eb05d296eb4d35f52f34 Mon Sep 17 00:00:00 2001
357786
From: Fam Zheng <famz@redhat.com>
357786
Date: Fri, 29 Jun 2018 06:11:44 +0200
357786
Subject: [PATCH 40/57] qcow2: Implement copy offloading
357786
357786
RH-Author: Fam Zheng <famz@redhat.com>
357786
Message-id: <20180629061153.12687-5-famz@redhat.com>
357786
Patchwork-id: 81154
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v2 04/13] qcow2: Implement copy offloading
357786
Bugzilla: 1482537
357786
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
357786
357786
The two callbacks are implemented quite similarly to the read/write
357786
functions: bdrv_co_copy_range_from maps for read and calls into bs->file
357786
or bs->backing depending on the allocation status; bdrv_co_copy_range_to
357786
maps for write and calls into bs->file.
357786
357786
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Message-id: 20180601092648.24614-5-famz@redhat.com
357786
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
(cherry picked from commit fd9fcd37a8645efe322956d94f76e90135522a16)
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/qcow2.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
357786
 1 file changed, 199 insertions(+), 30 deletions(-)
357786
357786
diff --git a/block/qcow2.c b/block/qcow2.c
357786
index 092db81..c85ebcb 100644
357786
--- a/block/qcow2.c
357786
+++ b/block/qcow2.c
357786
@@ -1756,6 +1756,39 @@ static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
357786
     return status;
357786
 }
357786
 
357786
+static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs,
357786
+                                            QCowL2Meta **pl2meta,
357786
+                                            bool link_l2)
357786
+{
357786
+    int ret = 0;
357786
+    QCowL2Meta *l2meta = *pl2meta;
357786
+
357786
+    while (l2meta != NULL) {
357786
+        QCowL2Meta *next;
357786
+
357786
+        if (!ret && link_l2) {
357786
+            ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
357786
+            if (ret) {
357786
+                goto out;
357786
+            }
357786
+        }
357786
+
357786
+        /* Take the request off the list of running requests */
357786
+        if (l2meta->nb_clusters != 0) {
357786
+            QLIST_REMOVE(l2meta, next_in_flight);
357786
+        }
357786
+
357786
+        qemu_co_queue_restart_all(&l2meta->dependent_requests);
357786
+
357786
+        next = l2meta->next;
357786
+        g_free(l2meta);
357786
+        l2meta = next;
357786
+    }
357786
+out:
357786
+    *pl2meta = l2meta;
357786
+    return ret;
357786
+}
357786
+
357786
 static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
357786
                                         uint64_t bytes, QEMUIOVector *qiov,
357786
                                         int flags)
357786
@@ -2042,24 +2075,9 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
357786
             }
357786
         }
357786
 
357786
-        while (l2meta != NULL) {
357786
-            QCowL2Meta *next;
357786
-
357786
-            ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
357786
-            if (ret < 0) {
357786
-                goto fail;
357786
-            }
357786
-
357786
-            /* Take the request off the list of running requests */
357786
-            if (l2meta->nb_clusters != 0) {
357786
-                QLIST_REMOVE(l2meta, next_in_flight);
357786
-            }
357786
-
357786
-            qemu_co_queue_restart_all(&l2meta->dependent_requests);
357786
-
357786
-            next = l2meta->next;
357786
-            g_free(l2meta);
357786
-            l2meta = next;
357786
+        ret = qcow2_handle_l2meta(bs, &l2meta, true);
357786
+        if (ret) {
357786
+            goto fail;
357786
         }
357786
 
357786
         bytes -= cur_bytes;
357786
@@ -2070,18 +2088,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
357786
     ret = 0;
357786
 
357786
 fail:
357786
-    while (l2meta != NULL) {
357786
-        QCowL2Meta *next;
357786
-
357786
-        if (l2meta->nb_clusters != 0) {
357786
-            QLIST_REMOVE(l2meta, next_in_flight);
357786
-        }
357786
-        qemu_co_queue_restart_all(&l2meta->dependent_requests);
357786
-
357786
-        next = l2meta->next;
357786
-        g_free(l2meta);
357786
-        l2meta = next;
357786
-    }
357786
+    qcow2_handle_l2meta(bs, &l2meta, false);
357786
 
357786
     qemu_co_mutex_unlock(&s->lock);
357786
 
357786
@@ -3264,6 +3271,166 @@ static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
357786
     return ret;
357786
 }
357786
 
357786
+static int coroutine_fn
357786
+qcow2_co_copy_range_from(BlockDriverState *bs,
357786
+                         BdrvChild *src, uint64_t src_offset,
357786
+                         BdrvChild *dst, uint64_t dst_offset,
357786
+                         uint64_t bytes, BdrvRequestFlags flags)
357786
+{
357786
+    BDRVQcow2State *s = bs->opaque;
357786
+    int ret;
357786
+    unsigned int cur_bytes; /* number of bytes in current iteration */
357786
+    BdrvChild *child = NULL;
357786
+    BdrvRequestFlags cur_flags;
357786
+
357786
+    assert(!bs->encrypted);
357786
+    qemu_co_mutex_lock(&s->lock);
357786
+
357786
+    while (bytes != 0) {
357786
+        uint64_t copy_offset = 0;
357786
+        /* prepare next request */
357786
+        cur_bytes = MIN(bytes, INT_MAX);
357786
+        cur_flags = flags;
357786
+
357786
+        ret = qcow2_get_cluster_offset(bs, src_offset, &cur_bytes, &copy_offset);
357786
+        if (ret < 0) {
357786
+            goto out;
357786
+        }
357786
+
357786
+        switch (ret) {
357786
+        case QCOW2_CLUSTER_UNALLOCATED:
357786
+            if (bs->backing && bs->backing->bs) {
357786
+                int64_t backing_length = bdrv_getlength(bs->backing->bs);
357786
+                if (src_offset >= backing_length) {
357786
+                    cur_flags |= BDRV_REQ_ZERO_WRITE;
357786
+                } else {
357786
+                    child = bs->backing;
357786
+                    cur_bytes = MIN(cur_bytes, backing_length - src_offset);
357786
+                    copy_offset = src_offset;
357786
+                }
357786
+            } else {
357786
+                cur_flags |= BDRV_REQ_ZERO_WRITE;
357786
+            }
357786
+            break;
357786
+
357786
+        case QCOW2_CLUSTER_ZERO_PLAIN:
357786
+        case QCOW2_CLUSTER_ZERO_ALLOC:
357786
+            cur_flags |= BDRV_REQ_ZERO_WRITE;
357786
+            break;
357786
+
357786
+        case QCOW2_CLUSTER_COMPRESSED:
357786
+            ret = -ENOTSUP;
357786
+            goto out;
357786
+            break;
357786
+
357786
+        case QCOW2_CLUSTER_NORMAL:
357786
+            child = bs->file;
357786
+            copy_offset += offset_into_cluster(s, src_offset);
357786
+            if ((copy_offset & 511) != 0) {
357786
+                ret = -EIO;
357786
+                goto out;
357786
+            }
357786
+            break;
357786
+
357786
+        default:
357786
+            abort();
357786
+        }
357786
+        qemu_co_mutex_unlock(&s->lock);
357786
+        ret = bdrv_co_copy_range_from(child,
357786
+                                      copy_offset,
357786
+                                      dst, dst_offset,
357786
+                                      cur_bytes, cur_flags);
357786
+        qemu_co_mutex_lock(&s->lock);
357786
+        if (ret < 0) {
357786
+            goto out;
357786
+        }
357786
+
357786
+        bytes -= cur_bytes;
357786
+        src_offset += cur_bytes;
357786
+        dst_offset += cur_bytes;
357786
+    }
357786
+    ret = 0;
357786
+
357786
+out:
357786
+    qemu_co_mutex_unlock(&s->lock);
357786
+    return ret;
357786
+}
357786
+
357786
+static int coroutine_fn
357786
+qcow2_co_copy_range_to(BlockDriverState *bs,
357786
+                       BdrvChild *src, uint64_t src_offset,
357786
+                       BdrvChild *dst, uint64_t dst_offset,
357786
+                       uint64_t bytes, BdrvRequestFlags flags)
357786
+{
357786
+    BDRVQcow2State *s = bs->opaque;
357786
+    int offset_in_cluster;
357786
+    int ret;
357786
+    unsigned int cur_bytes; /* number of sectors in current iteration */
357786
+    uint64_t cluster_offset;
357786
+    uint8_t *cluster_data = NULL;
357786
+    QCowL2Meta *l2meta = NULL;
357786
+
357786
+    assert(!bs->encrypted);
357786
+    s->cluster_cache_offset = -1; /* disable compressed cache */
357786
+
357786
+    qemu_co_mutex_lock(&s->lock);
357786
+
357786
+    while (bytes != 0) {
357786
+
357786
+        l2meta = NULL;
357786
+
357786
+        offset_in_cluster = offset_into_cluster(s, dst_offset);
357786
+        cur_bytes = MIN(bytes, INT_MAX);
357786
+
357786
+        /* TODO:
357786
+         * If src->bs == dst->bs, we could simply copy by incrementing
357786
+         * the refcnt, without copying user data.
357786
+         * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
357786
+        ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes,
357786
+                                         &cluster_offset, &l2meta);
357786
+        if (ret < 0) {
357786
+            goto fail;
357786
+        }
357786
+
357786
+        assert((cluster_offset & 511) == 0);
357786
+
357786
+        ret = qcow2_pre_write_overlap_check(bs, 0,
357786
+                cluster_offset + offset_in_cluster, cur_bytes);
357786
+        if (ret < 0) {
357786
+            goto fail;
357786
+        }
357786
+
357786
+        qemu_co_mutex_unlock(&s->lock);
357786
+        ret = bdrv_co_copy_range_to(src, src_offset,
357786
+                                    bs->file,
357786
+                                    cluster_offset + offset_in_cluster,
357786
+                                    cur_bytes, flags);
357786
+        qemu_co_mutex_lock(&s->lock);
357786
+        if (ret < 0) {
357786
+            goto fail;
357786
+        }
357786
+
357786
+        ret = qcow2_handle_l2meta(bs, &l2meta, true);
357786
+        if (ret) {
357786
+            goto fail;
357786
+        }
357786
+
357786
+        bytes -= cur_bytes;
357786
+        dst_offset += cur_bytes;
357786
+    }
357786
+    ret = 0;
357786
+
357786
+fail:
357786
+    qcow2_handle_l2meta(bs, &l2meta, false);
357786
+
357786
+    qemu_co_mutex_unlock(&s->lock);
357786
+
357786
+    qemu_vfree(cluster_data);
357786
+    trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
357786
+
357786
+    return ret;
357786
+}
357786
+
357786
 static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
357786
                           PreallocMode prealloc, Error **errp)
357786
 {
357786
@@ -4522,6 +4689,8 @@ BlockDriver bdrv_qcow2 = {
357786
 
357786
     .bdrv_co_pwrite_zeroes  = qcow2_co_pwrite_zeroes,
357786
     .bdrv_co_pdiscard       = qcow2_co_pdiscard,
357786
+    .bdrv_co_copy_range_from = qcow2_co_copy_range_from,
357786
+    .bdrv_co_copy_range_to  = qcow2_co_copy_range_to,
357786
     .bdrv_truncate          = qcow2_truncate,
357786
     .bdrv_co_pwritev_compressed = qcow2_co_pwritev_compressed,
357786
     .bdrv_make_empty        = qcow2_make_empty,
357786
-- 
357786
1.8.3.1
357786