carlwgeorge / rpms / qemu

Forked from rpms/qemu a year ago
Clone
45e84a0
From 5bb37d151b026759ee35f04212b11b4d625c7431 Mon Sep 17 00:00:00 2001
45e84a0
From: Kevin Wolf <kwolf@redhat.com>
45e84a0
Date: Wed, 7 Dec 2011 12:42:10 +0100
45e84a0
Subject: [PATCH 23/25] qemu-img rebase: Fix for undersized backing files
45e84a0
45e84a0
Backing files may be smaller than the corresponding COW file. When
45e84a0
reading directly from the backing file, qemu-img rebase must consider
45e84a0
this and assume zero sectors after the end of backing files.
45e84a0
45e84a0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
45e84a0
Reviewed-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
45e84a0
---
45e84a0
 qemu-img.c |   42 +++++++++++++++++++++++++++++++++---------
45e84a0
 1 files changed, 33 insertions(+), 9 deletions(-)
45e84a0
45e84a0
diff --git a/qemu-img.c b/qemu-img.c
45e84a0
index 8bdae66..01cc0d3 100644
45e84a0
--- a/qemu-img.c
45e84a0
+++ b/qemu-img.c
45e84a0
@@ -1420,6 +1420,8 @@ static int img_rebase(int argc, char **argv)
45e84a0
      */
45e84a0
     if (!unsafe) {
45e84a0
         uint64_t num_sectors;
45e84a0
+        uint64_t old_backing_num_sectors;
45e84a0
+        uint64_t new_backing_num_sectors;
45e84a0
         uint64_t sector;
45e84a0
         int n;
45e84a0
         uint8_t * buf_old;
45e84a0
@@ -1430,6 +1432,8 @@ static int img_rebase(int argc, char **argv)
45e84a0
         buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
45e84a0
45e84a0
         bdrv_get_geometry(bs, &num_sectors);
45e84a0
+        bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
45e84a0
+        bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
45e84a0
45e84a0
         local_progress = (float)100 /
45e84a0
             (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
45e84a0
@@ -1448,16 +1452,36 @@ static int img_rebase(int argc, char **argv)
45e84a0
                 continue;
45e84a0
             }
45e84a0
45e84a0
-            /* Read old and new backing file */
45e84a0
-            ret = bdrv_read(bs_old_backing, sector, buf_old, n);
45e84a0
-            if (ret < 0) {
45e84a0
-                error_report("error while reading from old backing file");
45e84a0
-                goto out;
45e84a0
+            /*
45e84a0
+             * Read old and new backing file and take into consideration that
45e84a0
+             * backing files may be smaller than the COW image.
45e84a0
+             */
45e84a0
+            if (sector >= old_backing_num_sectors) {
45e84a0
+                memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
45e84a0
+            } else {
45e84a0
+                if (sector + n > old_backing_num_sectors) {
45e84a0
+                    n = old_backing_num_sectors - sector;
45e84a0
+                }
45e84a0
+
45e84a0
+                ret = bdrv_read(bs_old_backing, sector, buf_old, n);
45e84a0
+                if (ret < 0) {
45e84a0
+                    error_report("error while reading from old backing file");
45e84a0
+                    goto out;
45e84a0
+                }
45e84a0
             }
45e84a0
-            ret = bdrv_read(bs_new_backing, sector, buf_new, n);
45e84a0
-            if (ret < 0) {
45e84a0
-                error_report("error while reading from new backing file");
45e84a0
-                goto out;
45e84a0
+
45e84a0
+            if (sector >= new_backing_num_sectors) {
45e84a0
+                memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
45e84a0
+            } else {
45e84a0
+                if (sector + n > new_backing_num_sectors) {
45e84a0
+                    n = new_backing_num_sectors - sector;
45e84a0
+                }
45e84a0
+
45e84a0
+                ret = bdrv_read(bs_new_backing, sector, buf_new, n);
45e84a0
+                if (ret < 0) {
45e84a0
+                    error_report("error while reading from new backing file");
45e84a0
+                    goto out;
45e84a0
+                }
45e84a0
             }
45e84a0
45e84a0
             /* If they differ, we need to write to the COW file */
45e84a0
-- 
45e84a0
1.7.7.5
45e84a0