From aad15de4275d2fc90acdf6101493dfee4e39b803 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Thu, 24 Mar 2016 23:33:57 +0100 Subject: qemu-img: Fix preallocation with -S 0 for convert When passing -S 0 to qemu-img convert, the target image is supposed to be fully allocated. Right now, this is not the case if the source image contains areas which bdrv_get_block_status() reports as being zero. This patch changes a zeroed area's status from BLK_ZERO to BLK_DATA before invoking convert_write() if -S 0 has been specified. In addition, the check whether convert_read() actually needs to do anything (basically only if the current area is a BLK_DATA area) is pulled out of that function to the caller. If -S 0 has been specified, zeroed areas need to be written as data to the output, thus they then have to be accounted when calculating the progress made. This patch changes the reference output for iotest 122; contrary to what it assumed, -S 0 really should allocate everything in the output, not just areas that are filled with zeros (as opposed to being zeroed). Signed-off-by: Max Reitz Reviewed-by: Fam Zheng Signed-off-by: Kevin Wolf --- qemu-img.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 55f76e7..06264d9 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1514,10 +1514,6 @@ static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors, int n; int ret; - if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) { - return 0; - } - assert(nb_sectors <= s->buf_sectors); while (nb_sectors > 0) { BlockBackend *blk; @@ -1655,7 +1651,8 @@ static int convert_do_copy(ImgConvertState *s) ret = n; goto fail; } - if (s->status == BLK_DATA) { + if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) + { s->allocated_sectors += n; } sector_num += n; @@ -1675,17 +1672,24 @@ static int convert_do_copy(ImgConvertState *s) ret = n; goto fail; } - if (s->status == BLK_DATA) { + if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) + { allocated_done += n; qemu_progress_print(100.0 * allocated_done / s->allocated_sectors, 0); } - ret = convert_read(s, sector_num, n, buf); - if (ret < 0) { - error_report("error while reading sector %" PRId64 - ": %s", sector_num, strerror(-ret)); - goto fail; + if (s->status == BLK_DATA) { + ret = convert_read(s, sector_num, n, buf); + if (ret < 0) { + error_report("error while reading sector %" PRId64 + ": %s", sector_num, strerror(-ret)); + goto fail; + } + } else if (!s->min_sparse && s->status == BLK_ZERO) { + n = MIN(n, s->buf_sectors); + memset(buf, 0, n * BDRV_SECTOR_SIZE); + s->status = BLK_DATA; } ret = convert_write(s, sector_num, n, buf); -- cgit v1.1