9ae3a8
From c659199f9bf37c798bd741ddd151db213d5d73f8 Mon Sep 17 00:00:00 2001
9ae3a8
From: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Date: Tue, 11 Feb 2014 16:14:25 +0100
9ae3a8
Subject: [PATCH 19/28] block: resize backing file image during offline commit, if necessary
9ae3a8
9ae3a8
RH-Author: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Message-id: <54cb47d8cd589585e9788df80b9612ea527e193d.1392134912.git.jcody@redhat.com>
9ae3a8
Patchwork-id: 57215
9ae3a8
O-Subject: [RHEL7 qemu-kvm PATCH 1/6] block: resize backing file image during offline commit, if necessary
9ae3a8
Bugzilla: 1047254
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Currently, if an image file is logically larger than its backing file,
9ae3a8
committing it via 'qemu-img commit' will fail.
9ae3a8
9ae3a8
For instance, if we have a base image with a virtual size 10G, and a
9ae3a8
snapshot image of size 20G, then committing the snapshot offline with
9ae3a8
'qemu-img commit' will likely fail.
9ae3a8
9ae3a8
This will automatically attempt to resize the base image, if the
9ae3a8
snapshot image to be committed is larger.
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
Reviewed-by: Fam Zheng <famz@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Reviewed-by: Benoit Canet <benoit@irqsave.net>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 72706ea4cd38bfcb151265df0178ba21863d7518)
9ae3a8
---
9ae3a8
 block.c | 28 +++++++++++++++++++++++++---
9ae3a8
 1 file changed, 25 insertions(+), 3 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block.c |   28 +++++++++++++++++++++++++---
9ae3a8
 1 files changed, 25 insertions(+), 3 deletions(-)
9ae3a8
9ae3a8
diff --git a/block.c b/block.c
9ae3a8
index 1b57236..c7aa285 100644
9ae3a8
--- a/block.c
9ae3a8
+++ b/block.c
9ae3a8
@@ -1947,10 +1947,10 @@ int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
9ae3a8
 int bdrv_commit(BlockDriverState *bs)
9ae3a8
 {
9ae3a8
     BlockDriver *drv = bs->drv;
9ae3a8
-    int64_t sector, total_sectors;
9ae3a8
+    int64_t sector, total_sectors, length, backing_length;
9ae3a8
     int n, ro, open_flags;
9ae3a8
     int ret = 0;
9ae3a8
-    uint8_t *buf;
9ae3a8
+    uint8_t *buf = NULL;
9ae3a8
     char filename[PATH_MAX];
9ae3a8
 
9ae3a8
     if (!drv)
9ae3a8
@@ -1975,7 +1975,29 @@ int bdrv_commit(BlockDriverState *bs)
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
-    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
9ae3a8
+    length = bdrv_getlength(bs);
9ae3a8
+    if (length < 0) {
9ae3a8
+        ret = length;
9ae3a8
+        goto ro_cleanup;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    backing_length = bdrv_getlength(bs->backing_hd);
9ae3a8
+    if (backing_length < 0) {
9ae3a8
+        ret = backing_length;
9ae3a8
+        goto ro_cleanup;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* If our top snapshot is larger than the backing file image,
9ae3a8
+     * grow the backing file image if possible.  If not possible,
9ae3a8
+     * we must return an error */
9ae3a8
+    if (length > backing_length) {
9ae3a8
+        ret = bdrv_truncate(bs->backing_hd, length);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            goto ro_cleanup;
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    total_sectors = length >> BDRV_SECTOR_BITS;
9ae3a8
     buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
9ae3a8
 
9ae3a8
     for (sector = 0; sector < total_sectors; sector += n) {
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8