thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-qcow2-remove-n_start-and-n_end-of-qcow2_alloc_cluste.patch

9ae3a8
From 68066d2b824a88a45b3263dd1ceb18307c92581f Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Sat, 15 Feb 2014 16:03:47 +0100
9ae3a8
Subject: [PATCH 2/5] qcow2: remove n_start and n_end of qcow2_alloc_cluster_offset()
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1392480230-24011-2-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 57292
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 1/4] qcow2: remove n_start and n_end of qcow2_alloc_cluster_offset()
9ae3a8
Bugzilla: 1049176
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
9ae3a8
From: Hu Tao <hutao@cn.fujitsu.com>
9ae3a8
9ae3a8
BZ: 1049176
9ae3a8
BZ: 1055848
9ae3a8
9ae3a8
n_start can be actually calculated from offset. The number of
9ae3a8
sectors to be allocated(n_end - n_start) can be passed in in
9ae3a8
num. By removing n_start and n_end, we can save two parameters.
9ae3a8
9ae3a8
The side effect is there is a bug in qcow2.c:preallocate() that
9ae3a8
passes incorrect n_start to qcow2_alloc_cluster_offset() is
9ae3a8
fixed. The bug can be triggerred by a larger cluster size than
9ae3a8
the default value(65536), for example:
9ae3a8
9ae3a8
./qemu-img create -f qcow2 \
9ae3a8
  -o 'cluster_size=131072,preallocation=metadata' file.img 4G
9ae3a8
9ae3a8
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Benoit Canet <benoit@irqsave.net>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 16f0587e0a5da5b1ad76cb7c3739491bc042201c)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-cluster.c | 14 ++++++--------
9ae3a8
 block/qcow2.c         | 13 +++++++------
9ae3a8
 block/qcow2.h         |  2 +-
9ae3a8
 trace-events          |  2 +-
9ae3a8
 4 files changed, 15 insertions(+), 16 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-cluster.c |   14 ++++++--------
9ae3a8
 block/qcow2.c         |   13 +++++++------
9ae3a8
 block/qcow2.h         |    2 +-
9ae3a8
 trace-events          |    2 +-
9ae3a8
 4 files changed, 15 insertions(+), 16 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
9ae3a8
index 79c3ae0..862df0f 100644
9ae3a8
--- a/block/qcow2-cluster.c
9ae3a8
+++ b/block/qcow2-cluster.c
9ae3a8
@@ -1173,7 +1173,7 @@ fail:
9ae3a8
  * Return 0 on success and -errno in error cases
9ae3a8
  */
9ae3a8
 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
9ae3a8
-    int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m)
9ae3a8
+    int *num, uint64_t *host_offset, QCowL2Meta **m)
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
     uint64_t start, remaining;
9ae3a8
@@ -1181,15 +1181,13 @@ int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
9ae3a8
     uint64_t cur_bytes;
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
-    trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset,
9ae3a8
-                                      n_start, n_end);
9ae3a8
+    trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *num);
9ae3a8
 
9ae3a8
-    assert(n_start * BDRV_SECTOR_SIZE == offset_into_cluster(s, offset));
9ae3a8
-    offset = start_of_cluster(s, offset);
9ae3a8
+    assert((offset & ~BDRV_SECTOR_MASK) == 0);
9ae3a8
 
9ae3a8
 again:
9ae3a8
-    start = offset + (n_start << BDRV_SECTOR_BITS);
9ae3a8
-    remaining = (n_end - n_start) << BDRV_SECTOR_BITS;
9ae3a8
+    start = offset;
9ae3a8
+    remaining = *num << BDRV_SECTOR_BITS;
9ae3a8
     cluster_offset = 0;
9ae3a8
     *host_offset = 0;
9ae3a8
     cur_bytes = 0;
9ae3a8
@@ -1275,7 +1273,7 @@ again:
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
-    *num = (n_end - n_start) - (remaining >> BDRV_SECTOR_BITS);
9ae3a8
+    *num -= remaining >> BDRV_SECTOR_BITS;
9ae3a8
     assert(*num > 0);
9ae3a8
     assert(*host_offset != 0);
9ae3a8
 
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index 25a5b5e..fe950b4 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -1000,7 +1000,6 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
     int index_in_cluster;
9ae3a8
-    int n_end;
9ae3a8
     int ret;
9ae3a8
     int cur_nr_sectors; /* number of sectors in current iteration */
9ae3a8
     uint64_t cluster_offset;
9ae3a8
@@ -1024,14 +1023,16 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
9ae3a8
 
9ae3a8
         trace_qcow2_writev_start_part(qemu_coroutine_self());
9ae3a8
         index_in_cluster = sector_num & (s->cluster_sectors - 1);
9ae3a8
-        n_end = index_in_cluster + remaining_sectors;
9ae3a8
+        cur_nr_sectors = remaining_sectors;
9ae3a8
         if (s->crypt_method &&
9ae3a8
-            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) {
9ae3a8
-            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
9ae3a8
+            cur_nr_sectors >
9ae3a8
+            QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
9ae3a8
+            cur_nr_sectors =
9ae3a8
+                QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
9ae3a8
         }
9ae3a8
 
9ae3a8
         ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
9ae3a8
-            index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta);
9ae3a8
+            &cur_nr_sectors, &cluster_offset, &l2meta);
9ae3a8
         if (ret < 0) {
9ae3a8
             goto fail;
9ae3a8
         }
9ae3a8
@@ -1408,7 +1409,7 @@ static int preallocate(BlockDriverState *bs)
9ae3a8
 
9ae3a8
     while (nb_sectors) {
9ae3a8
         num = MIN(nb_sectors, INT_MAX >> 9);
9ae3a8
-        ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
9ae3a8
+        ret = qcow2_alloc_cluster_offset(bs, offset, &num,
9ae3a8
                                          &host_offset, &meta);
9ae3a8
         if (ret < 0) {
9ae3a8
             return ret;
9ae3a8
diff --git a/block/qcow2.h b/block/qcow2.h
9ae3a8
index 9749f03..4a653c5 100644
9ae3a8
--- a/block/qcow2.h
9ae3a8
+++ b/block/qcow2.h
9ae3a8
@@ -463,7 +463,7 @@ void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
9ae3a8
 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
9ae3a8
     int *num, uint64_t *cluster_offset);
9ae3a8
 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
9ae3a8
-    int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m);
9ae3a8
+    int *num, uint64_t *host_offset, QCowL2Meta **m);
9ae3a8
 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
9ae3a8
                                          uint64_t offset,
9ae3a8
                                          int compressed_size);
9ae3a8
diff --git a/trace-events b/trace-events
9ae3a8
index 5d86cf3..b9144b2 100644
9ae3a8
--- a/trace-events
9ae3a8
+++ b/trace-events
9ae3a8
@@ -485,7 +485,7 @@ qcow2_writev_done_part(void *co, int cur_nr_sectors) "co %p cur_nr_sectors %d"
9ae3a8
 qcow2_writev_data(void *co, uint64_t offset) "co %p offset %" PRIx64
9ae3a8
 
9ae3a8
 # block/qcow2-cluster.c
9ae3a8
-qcow2_alloc_clusters_offset(void *co, uint64_t offset, int n_start, int n_end) "co %p offet %" PRIx64 " n_start %d n_end %d"
9ae3a8
+qcow2_alloc_clusters_offset(void *co, uint64_t offset, int num) "co %p offet %" PRIx64 " num %d"
9ae3a8
 qcow2_handle_copied(void *co, uint64_t guest_offset, uint64_t host_offset, uint64_t bytes) "co %p guest_offet %" PRIx64 " host_offset %" PRIx64 " bytes %" PRIx64
9ae3a8
 qcow2_handle_alloc(void *co, uint64_t guest_offset, uint64_t host_offset, uint64_t bytes) "co %p guest_offet %" PRIx64 " host_offset %" PRIx64 " bytes %" PRIx64
9ae3a8
 qcow2_do_alloc_clusters_offset(void *co, uint64_t guest_offset, uint64_t host_offset, int nb_clusters) "co %p guest_offet %" PRIx64 " host_offset %" PRIx64 " nb_clusters %d"
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8