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