qemu-img: check block status of backing file when converting.
cover qemu-img: check block status of backing file when converting.
When converting images, check the block status of it's backing file chain to avoid needlessly reading zeros.
PATCH 1/1 qemu-img: check block status of backing file when converting.
--- qemu-img.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
qemu-img.c +24 -3
@@ -1451,6 +1451,21 @@ 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, +BlockDriverState **file) +{ +int64_t ret; +if (bs->backing) { + ret =get_backing_status (bs->backing->bs, sector_num, + nb_sectors, pnum, file); + }else { + ret =bdrv_get_block_status (bs, sector_num, nb_sectors, pnum, file); + } +return ret; +} +static int convert_iteration_sectors (ImgConvertState *s,int64_t sector_num) {int64_t ret;
@@ -1469,7 +1484,6 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)if (ret <0 ) {return ret; } -if (ret &BDRV_BLOCK_ZERO ) { s->status =BLK_ZERO ; }else if (ret &BDRV_BLOCK_DATA ) {
@@ -1477,10 +1491,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, &file); +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
Quoted 1 line · Ren Kimura
Signed-off-by Ren Kimura <[email protected]>
How about you put what you wrote in the 0/1 email here?
(But with s/it's/its/ :-))
Quoted 11 lines
--- qemu-img.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)qemu-img.c +0
-0@@ -1451,6 +1451,21 @@ static void convert_select_part(ImgConvertState *s, int64_t sector_num) } }
qemu-img.c static void convert_select_part(ImgConvertState *s, int64_t sector_num) · line 1462
Quoted 10 lines
qemu-img.c +9
-0+static int64_t get_backing_status (BlockDriverState *bs, +int64_t sector_num, +int nb_sectors,int *pnum, +BlockDriverState **file) +{ +int64_t ret; +if (bs->backing) { + ret =get_backing_status (bs->backing->bs, sector_num, + nb_sectors, pnum, file);
This won't really work. You may not fall through to the backing file if the overlay file has an allocated range here.
So you'll have to call bdrv_get_block_status() unconditionally first, and only if its return value has neither the BDRV_BLOCK_DATA nor the BDRV_BLOCK_ZERO flag set you can (and then have to) fall through to its backing file.
qemu-img.c static void convert_select_part(ImgConvertState *s, int64_t sector_num) · line 1468
Quoted 6 lines
qemu-img.c +6
-0+ }else { + ret =bdrv_get_block_status (bs, sector_num, nb_sectors, pnum, file); + } +return ret; +} +
Apart from that, I'd suggest to drop the "file" parameter. We don't care about whatever is returned in it anyway. Just declare a local BlockDriverState pointer in this function which will receive this value.
The reason for why I'm suggesting this is because this would make it clearer to the reader that this value is discarded.
Feel free to follow or ignore this suggestion, based on whether you consider it useful or not.
Max
Quoted 32 lines
qemu-img.c +9
-3static int convert_iteration_sectors (ImgConvertState *s,int64_t sector_num) {int64_t ret;@@ -1469,7 +1484,6 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)if (ret <0 ) {return ret; } -if (ret &BDRV_BLOCK_ZERO ) { s->status =BLK_ZERO ; }else if (ret &BDRV_BLOCK_DATA ) {@@ -1477,10 +1491,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, &file); +if (ret &BDRV_BLOCK_ZERO ) { + s->status =BLK_ZERO ; + }else { + s->status =BLK_DATA ; + } }else { s->status =BLK_BACKING_FILE ; }
Max
Thank you for reviewing.
OK. I'll fix these and send version 2 later.
Thanks