Skip to contentSkip to conversation

qemu-img: check block status of backing file when converting.

PATCH Superseded Ren Kimura 2 participants 3 messages Apr 17, 2016 – Apr 21, 2016

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ren Kimura

author
#0 lore raw

PATCH v3 1/1 qemu-img: check block status of backing file when converting.

When converting images, check the block status of its backing file chain to avoid needlessly reading zeros.

---
 qemu-img.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
qemu-img.c +25 -2
index 06264d9..ad1df5f 100644
@@ -1451,6 +1451,22 @@ static void convert_select_part(ImgConvertState *s, int64_t sector_num)
     }
 }
 
+static int64_t get_backing_status(BlockDriverState *bs,
+                                  int64_t sector_num,
+                                  int nb_sectors, int *pnum)
+{
+    int64_t ret;
+    BlockDriverState *file;
+    ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum, &file);
+    if (!(ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO))) {
+        if (bs->backing) {
+            ret = get_backing_status(bs->backing->bs, sector_num,
+                                     nb_sectors, pnum);
+        }
+    }
+    return ret;
+}
+
 static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
 {
     int64_t ret;
@@ -1477,10 +1493,17 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
         } else if (!s->target_has_backing) {
             /* Without a target backing file we must copy over the contents of
              * the backing file as well. */
-            /* TODO Check block status of the backing file chain to avoid
+            /* Check block status of the backing file chain to avoid
              * needlessly reading zeroes and limiting the iteration to the
              * buffer size */
-            s->status = BLK_DATA;
+            ret = get_backing_status(blk_bs(s->src[s->src_cur]),
+                                     sector_num - s->src_cur_offset,
+                                     n, &n);
+            if (ret & BDRV_BLOCK_ZERO) {
+                s->status = BLK_ZERO;
+            } else {
+                s->status = BLK_DATA;
+            }
         } else {
             s->status = BLK_BACKING_FILE;
         }
Signature · 1 line
2.5.0