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