Received: from eggs.gnu.org ([2001:4830:134:3::10]:46863) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atr1h-0006rk-Ul for qemu-devel@nongnu.org; Sat, 23 Apr 2016 02:26:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1atr1h-0003TG-3b for qemu-devel@nongnu.org; Sat, 23 Apr 2016 02:26:45 -0400 From: Ren Kimura Date: Sat, 23 Apr 2016 15:26:27 +0900 Message-Id: <1461392787-26897-2-git-send-email-rkx1209dev@gmail.com> In-Reply-To: <1461392787-26897-1-git-send-email-rkx1209dev@gmail.com> References: <1461392787-26897-1-git-send-email-rkx1209dev@gmail.com> Subject: [Qemu-devel] [PATCH v4 1/1] qemu-img: check block status of backing file when converting. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: mreitz@redhat.com Cc: kwolf@redhat.com, qemu-block@nongnu.org, qemu-devel@nongnu.org When converting images, check the block status of its backing file chain to avoid needlessly reading zeros. Signed-off-by: Ren Kimura --- qemu-img.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 06264d9..b771227 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -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) +{ + while (bs->backing) { + int64_t ret; + BlockDriverState *file; + bs = bs->backing->bs; + ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum, &file); + if (ret < 0 || ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) { + return ret; + } + } + return -1; +} + static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) { int64_t ret; @@ -1477,10 +1493,21 @@ 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 < 0) { + return ret; + } + + if (ret & BDRV_BLOCK_ZERO) { + s->status = BLK_ZERO; + } else { + s->status = BLK_DATA; + } } else { s->status = BLK_BACKING_FILE; } -- 2.5.0