Skip to contentSkip to conversation

qemu-img rebase: Fix for undersized backing files

PATCH In review Kevin Wolf 2 participants 5 messages Dec 7, 2011

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 qemu-img rebase: Fix for undersized backing files

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.

---
 qemu-img.c |   42 +++++++++++++++++++++++++++++++++---------
 1 files changed, 33 insertions(+), 9 deletions(-)
qemu-img.c +33 -9
index 8bdae66..01cc0d3 100644
@@ -1420,6 +1420,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;
@@ -1430,6 +1432,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));
@@ -1448,16 +1452,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_report("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_report("error while reading from old backing file");
+                    goto out;
+                }
             }
-            ret = bdrv_read(bs_new_backing, sector, buf_new, n);
-            if (ret < 0) {
-                error_report("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_report("error while reading from new backing file");
+                    goto out;
+                }
             }
 
             /* If they differ, we need to write to the COW file */
Signature · 1 line
1.7.6.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Kevin Wolf

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.

---
 qemu-img.c |   42 +++++++++++++++++++++++++++++++++---------

 1 files changed, 33 insertions(+), 9 deletions(-)

It almost feels like we want a bdrv_read() variation that does the zeroing beyond end of image instead of duplicating this.

Stefan

author
#2 lore raw

Am 07.12.2011 12:50, schrieb Stefan Hajnoczi:

Quoted 10 lines · Kevin Wolf
Quoted 8 lines

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.

---
 qemu-img.c |   42 +++++++++++++++++++++++++++++++++---------
 1 files changed, 33 insertions(+), 9 deletions(-)
Quoted 4 lines

It almost feels like we want a bdrv_read() variation that does the zeroing beyond end of image instead of duplicating this.

Almost. If we have a third user, it's probably worth it.

Actually, if we make a bdrv_co_readv_backing() instead of a synchronous one, qcow2 would be the third user (and maybe VMDK could use it, too). That would require moving qemu-img into a coroutine first, which I think you wanted to do anyway?

Kevin

#3 lore raw
Quoted 16 lines · Kevin Wolf

Am 07.12.2011 12:50, schrieb Stefan Hajnoczi:

Quoted 10 lines · Kevin Wolf
Quoted 8 lines

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.

---
 qemu-img.c |   42 +++++++++++++++++++++++++++++++++---------

 1 files changed, 33 insertions(+), 9 deletions(-)
Quoted 4 lines

It almost feels like we want a bdrv_read() variation that does the zeroing beyond end of image instead of duplicating this.

Quoted 6 lines

Almost. If we have a third user, it's probably worth it.

Actually, if we make a bdrv_co_readv_backing() instead of a synchronous one, qcow2 would be the third user (and maybe VMDK could use it, too). That would require moving qemu-img into a coroutine first, which I think you wanted to do anyway?

If we can make qemu-img the last user of synchronous interfaces then it would be nice to convert it. Right now I'm happy that we're solidifying coroutines and converting synchronous hardware emulation, hopefully we'll get to the point where there is no real reason to keep the synchronous API around anymore.

Stefan

author
#4 lore raw

Am 07.12.2011 13:06, schrieb Stefan Hajnoczi:

Quoted 24 lines · Kevin Wolf
Quoted 16 lines

Am 07.12.2011 12:50, schrieb Stefan Hajnoczi:

Quoted 10 lines · Kevin Wolf
Quoted 8 lines

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.

---
 qemu-img.c |   42 +++++++++++++++++++++++++++++++++---------
 1 files changed, 33 insertions(+), 9 deletions(-)
Quoted 4 lines

It almost feels like we want a bdrv_read() variation that does the zeroing beyond end of image instead of duplicating this.

Quoted 6 lines

Almost. If we have a third user, it's probably worth it.

Actually, if we make a bdrv_co_readv_backing() instead of a synchronous one, qcow2 would be the third user (and maybe VMDK could use it, too). That would require moving qemu-img into a coroutine first, which I think you wanted to do anyway?

Quoted 5 lines

If we can make qemu-img the last user of synchronous interfaces then it would be nice to convert it. Right now I'm happy that we're solidifying coroutines and converting synchronous hardware emulation, hopefully we'll get to the point where there is no real reason to keep the synchronous API around anymore.

The point is, if I were to introduce a bdrv_co_readv_backing() function, I certainly wouldn't like to add all the bdrv_(aio_)read/write emulation code for it, too.

Kevin