From af5e3e5ad9183d4a78bd4a2d287c872aabecbf8c Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Fri, 17 Feb 2012 14:20:00 +0100 Subject: [PATCH 2/4] qemu-img rebase: Fix for undersized backing files RH-Author: Kevin Wolf Message-id: <1329488400-14832-1-git-send-email-kwolf@redhat.com> Patchwork-id: 37415 O-Subject: [RHEL-6.3 qemu-kvm PATCH] qemu-img rebase: Fix for undersized backing files Bugzilla: 638055 RH-Acked-by: Markus Armbruster RH-Acked-by: Paolo Bonzini RH-Acked-by: Laszlo Ersek Bugzilla: 638055 Backing files may be smaller than the corresponding COW file. When reading directly from the backing file, qemu-img rebase must consider this and assume zero sectors after the end of backing files. Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi (cherry picked from commit 87a1b3e381c282b143023c411df284175f0b656b) Conflicts: qemu-img.c Signed-off-by: Kevin Wolf --- qemu-img.c | 42 +++++++++++++++++++++++++++++++++--------- 1 files changed, 33 insertions(+), 9 deletions(-) Signed-off-by: Michal Novotny --- qemu-img.c | 42 +++++++++++++++++++++++++++++++++--------- 1 files changed, 33 insertions(+), 9 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 94cd684..38de348 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1405,6 +1405,8 @@ static int img_rebase(int argc, char **argv) */ if (!unsafe) { uint64_t num_sectors; + uint64_t old_backing_num_sectors; + uint64_t new_backing_num_sectors; uint64_t sector; int n; uint8_t * buf_old; @@ -1415,6 +1417,8 @@ static int img_rebase(int argc, char **argv) buf_new = qemu_blockalign(bs, IO_BUF_SIZE); bdrv_get_geometry(bs, &num_sectors); + bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors); + bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors); local_progress = (float)100 / (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); @@ -1433,16 +1437,36 @@ static int img_rebase(int argc, char **argv) continue; } - /* Read old and new backing file */ - ret = bdrv_read(bs_old_backing, sector, buf_old, n); - if (ret < 0) { - error("error while reading from old backing file"); - goto out; + /* + * Read old and new backing file and take into consideration that + * backing files may be smaller than the COW image. + */ + if (sector >= old_backing_num_sectors) { + memset(buf_old, 0, n * BDRV_SECTOR_SIZE); + } else { + if (sector + n > old_backing_num_sectors) { + n = old_backing_num_sectors - sector; + } + + ret = bdrv_read(bs_old_backing, sector, buf_old, n); + if (ret < 0) { + error("error while reading from old backing file"); + goto out; + } } - ret = bdrv_read(bs_new_backing, sector, buf_new, n); - if (ret < 0) { - error("error while reading from new backing file"); - goto out; + + if (sector >= new_backing_num_sectors) { + memset(buf_new, 0, n * BDRV_SECTOR_SIZE); + } else { + if (sector + n > new_backing_num_sectors) { + n = new_backing_num_sectors - sector; + } + + ret = bdrv_read(bs_new_backing, sector, buf_new, n); + if (ret < 0) { + error("error while reading from new backing file"); + goto out; + } } /* If they differ, we need to write to the COW file */ -- 1.7.7.6