9ae3a8
From 11e8c9b1857d272cfe6f38e83ae70478c15214eb Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Sat, 15 Feb 2014 16:03:49 +0100
9ae3a8
Subject: [PATCH 4/5] qcow2: check for NULL l2meta
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1392480230-24011-4-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 57294
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 3/4] qcow2: check for NULL l2meta
9ae3a8
Bugzilla: 1055848
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
In the case of a metadata preallocation with a large cluster size,
9ae3a8
qcow2_alloc_cluster_offset() can allocate nothing and returns a
9ae3a8
NULL l2meta. This patch checks for it and link2 l2 with only valid
9ae3a8
l2meta.
9ae3a8
9ae3a8
Replace 9 and 512 with BDRV_SECTOR_BITS, BDRV_SECTOR_SIZE
9ae3a8
respectively while at the function.
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 7c2bbf4aa66ca5a9fc2ca147e0e6cb6f407a3aa2)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2.c | 31 ++++++++++++++++---------------
9ae3a8
 1 file changed, 16 insertions(+), 15 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2.c |   31 ++++++++++++++++---------------
9ae3a8
 1 files changed, 16 insertions(+), 15 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index fe950b4..daad932 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -1404,34 +1404,34 @@ static int preallocate(BlockDriverState *bs)
9ae3a8
     int ret;
9ae3a8
     QCowL2Meta *meta;
9ae3a8
 
9ae3a8
-    nb_sectors = bdrv_getlength(bs) >> 9;
9ae3a8
+    nb_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
9ae3a8
     offset = 0;
9ae3a8
 
9ae3a8
     while (nb_sectors) {
9ae3a8
-        num = MIN(nb_sectors, INT_MAX >> 9);
9ae3a8
+        num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
9ae3a8
         ret = qcow2_alloc_cluster_offset(bs, offset, &num,
9ae3a8
                                          &host_offset, &meta);
9ae3a8
         if (ret < 0) {
9ae3a8
             return ret;
9ae3a8
         }
9ae3a8
 
9ae3a8
-        ret = qcow2_alloc_cluster_link_l2(bs, meta);
9ae3a8
-        if (ret < 0) {
9ae3a8
-            qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters,
9ae3a8
-                                    QCOW2_DISCARD_NEVER);
9ae3a8
-            return ret;
9ae3a8
-        }
9ae3a8
-
9ae3a8
-        /* There are no dependent requests, but we need to remove our request
9ae3a8
-         * from the list of in-flight requests */
9ae3a8
         if (meta != NULL) {
9ae3a8
+            ret = qcow2_alloc_cluster_link_l2(bs, meta);
9ae3a8
+            if (ret < 0) {
9ae3a8
+                qcow2_free_any_clusters(bs, meta->alloc_offset,
9ae3a8
+                                        meta->nb_clusters, QCOW2_DISCARD_NEVER);
9ae3a8
+                return ret;
9ae3a8
+            }
9ae3a8
+
9ae3a8
+            /* There are no dependent requests, but we need to remove our
9ae3a8
+             * request from the list of in-flight requests */
9ae3a8
             QLIST_REMOVE(meta, next_in_flight);
9ae3a8
         }
9ae3a8
 
9ae3a8
         /* TODO Preallocate data if requested */
9ae3a8
 
9ae3a8
         nb_sectors -= num;
9ae3a8
-        offset += num << 9;
9ae3a8
+        offset += num << BDRV_SECTOR_BITS;
9ae3a8
     }
9ae3a8
 
9ae3a8
     /*
9ae3a8
@@ -1440,9 +1440,10 @@ static int preallocate(BlockDriverState *bs)
9ae3a8
      * EOF). Extend the image to the last allocated sector.
9ae3a8
      */
9ae3a8
     if (host_offset != 0) {
9ae3a8
-        uint8_t buf[512];
9ae3a8
-        memset(buf, 0, 512);
9ae3a8
-        ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1);
9ae3a8
+        uint8_t buf[BDRV_SECTOR_SIZE];
9ae3a8
+        memset(buf, 0, BDRV_SECTOR_SIZE);
9ae3a8
+        ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1,
9ae3a8
+                         buf, 1);
9ae3a8
         if (ret < 0) {
9ae3a8
             return ret;
9ae3a8
         }
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8