yeahuh / rpms / qemu-kvm

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