cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qcow2-Zero-initialise-first-cluster-for-new-images.patch

0a122b
From a27b645ebf68deff6658389f34ee938ba042a2dd Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Tue, 7 Jan 2014 12:44:44 +0100
0a122b
Subject: [PATCH 8/8] qcow2: Zero-initialise first cluster for new images
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1389098684-1559-1-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: 56522
0a122b
O-Subject: [RHEL-7.0 qemu-kvm PATCH] qcow2: Zero-initialise first cluster for new images
0a122b
Bugzilla: 1032904
0a122b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
Bugzilla: 1032904
0a122b
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=6817817
0a122b
0a122b
Strictly speaking, this is only required for has_zero_init() == false,
0a122b
but it's easy enough to just do a cluster-aligned write that is padded
0a122b
with zeros after the header.
0a122b
0a122b
This fixes that after 'qemu-img create' header extensions are attempted
0a122b
to be parsed that are really just random leftover data.
0a122b
0a122b
Cc: qemu-stable@nongnu.org
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
Reviewed-by: Fam Zheng <famz@redhat.com>
0a122b
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
(cherry picked from commit f8413b3c23b08a547ce18609acc6fae5fd04ed5c)
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block/qcow2.c | 36 ++++++++++++++++++++----------------
0a122b
 1 file changed, 20 insertions(+), 16 deletions(-)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 block/qcow2.c |   36 ++++++++++++++++++++----------------
0a122b
 1 files changed, 20 insertions(+), 16 deletions(-)
0a122b
0a122b
diff --git a/block/qcow2.c b/block/qcow2.c
0a122b
index bf1f66f..f5f68f8 100644
0a122b
--- a/block/qcow2.c
0a122b
+++ b/block/qcow2.c
0a122b
@@ -1471,7 +1471,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
0a122b
      * size for any qcow2 image.
0a122b
      */
0a122b
     BlockDriverState* bs;
0a122b
-    QCowHeader header;
0a122b
+    QCowHeader *header;
0a122b
     uint8_t* refcount_table;
0a122b
     Error *local_err = NULL;
0a122b
     int ret;
0a122b
@@ -1489,30 +1489,34 @@ static int qcow2_create2(const char *filename, int64_t total_size,
0a122b
     }
0a122b
 
0a122b
     /* Write the header */
0a122b
-    memset(&header, 0, sizeof(header));
0a122b
-    header.magic = cpu_to_be32(QCOW_MAGIC);
0a122b
-    header.version = cpu_to_be32(version);
0a122b
-    header.cluster_bits = cpu_to_be32(cluster_bits);
0a122b
-    header.size = cpu_to_be64(0);
0a122b
-    header.l1_table_offset = cpu_to_be64(0);
0a122b
-    header.l1_size = cpu_to_be32(0);
0a122b
-    header.refcount_table_offset = cpu_to_be64(cluster_size);
0a122b
-    header.refcount_table_clusters = cpu_to_be32(1);
0a122b
-    header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT);
0a122b
-    header.header_length = cpu_to_be32(sizeof(header));
0a122b
+    QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
0a122b
+    header = g_malloc0(cluster_size);
0a122b
+    *header = (QCowHeader) {
0a122b
+        .magic                      = cpu_to_be32(QCOW_MAGIC),
0a122b
+        .version                    = cpu_to_be32(version),
0a122b
+        .cluster_bits               = cpu_to_be32(cluster_bits),
0a122b
+        .size                       = cpu_to_be64(0),
0a122b
+        .l1_table_offset            = cpu_to_be64(0),
0a122b
+        .l1_size                    = cpu_to_be32(0),
0a122b
+        .refcount_table_offset      = cpu_to_be64(cluster_size),
0a122b
+        .refcount_table_clusters    = cpu_to_be32(1),
0a122b
+        .refcount_order             = cpu_to_be32(3 + REFCOUNT_SHIFT),
0a122b
+        .header_length              = cpu_to_be32(sizeof(*header)),
0a122b
+    };
0a122b
 
0a122b
     if (flags & BLOCK_FLAG_ENCRYPT) {
0a122b
-        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
0a122b
+        header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
0a122b
     } else {
0a122b
-        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
0a122b
+        header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
0a122b
     }
0a122b
 
0a122b
     if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
0a122b
-        header.compatible_features |=
0a122b
+        header->compatible_features |=
0a122b
             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
0a122b
     }
0a122b
 
0a122b
-    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
0a122b
+    ret = bdrv_pwrite(bs, 0, header, cluster_size);
0a122b
+    g_free(header);
0a122b
     if (ret < 0) {
0a122b
         error_setg_errno(errp, -ret, "Could not write qcow2 header");
0a122b
         goto out;
0a122b
-- 
0a122b
1.7.1
0a122b