On 25.06.2020 18:22, Max Reitz wrote:
> This changes iotest 204's output, because blkdebug on top of a COW node
> used to make qemu-img map disregard the rest of the backing chain (the
> backing chain was broken by the filter). With this patch, the
> allocation in the base image is reported correctly.
>
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
> qemu-img.c | 36 ++++++++++++++++++++++--------------
> tests/qemu-iotests/204.out | 1 +
> 2 files changed, 23 insertions(+), 14 deletions(-)
>
> diff --git a/qemu-img.c b/qemu-img.c
> index 381271a74e..947be6ffac 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -1089,7 +1089,7 @@ static int img_commit(int argc, char **argv)
> /* This is different from QMP, which by default uses the deepest file in
> * the backing chain (i.e., the very base); however, the traditional
> * behavior of qemu-img commit is using the immediate backing file. */
> - base_bs = backing_bs(bs);
> + base_bs = bdrv_backing_chain_next(bs);
> if (!base_bs) {
> error_setg(&local_err, "Image does not have a backing file");
> goto done;
> @@ -1737,18 +1737,20 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
> if (s->sector_next_status <= sector_num) {
> uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
> int64_t count;
> + BlockDriverState *src_bs = blk_bs(s->src[src_cur]);
> + BlockDriverState *base;
> +
> + if (s->target_has_backing) {
> + base = bdrv_cow_bs(bdrv_skip_filters(src_bs));
> + } else {
> + base = NULL;
> + }
>
> do {
> count = n * BDRV_SECTOR_SIZE;
>
> - if (s->target_has_backing) {
> - ret = bdrv_block_status(blk_bs(s->src[src_cur]), offset,
> - count, &count, NULL, NULL);
> - } else {
> - ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
> - offset, count, &count, NULL,
> - NULL);
> - }
> + ret = bdrv_block_status_above(src_bs, base, offset, count, &count,
> + NULL, NULL);
>
> if (ret < 0) {
> if (s->salvage) {
> @@ -2673,7 +2675,8 @@ static int img_convert(int argc, char **argv)
> * s.target_backing_sectors has to be negative, which it will
> * be automatically). The backing file length is used only
> * for optimizations, so such a case is not fatal. */
> - s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs);
> + s.target_backing_sectors =
> + bdrv_nb_sectors(bdrv_backing_chain_next(out_bs));
> } else {
> s.target_backing_sectors = -1;
> }
> @@ -3044,6 +3047,7 @@ static int get_block_status(BlockDriverState *bs, int64_t offset,
>
> depth = 0;
> for (;;) {
> + bs = bdrv_skip_filters(bs);
> ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
> if (ret < 0) {
> return ret;
> @@ -3052,7 +3056,7 @@ static int get_block_status(BlockDriverState *bs, int64_t offset,
> if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
> break;
> }
> - bs = backing_bs(bs);
> + bs = bdrv_cow_bs(bs);
> if (bs == NULL) {
> ret = 0;
> break;
> @@ -3437,6 +3441,7 @@ static int img_rebase(int argc, char **argv)
> uint8_t *buf_old = NULL;
> uint8_t *buf_new = NULL;
> BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
> + BlockDriverState *unfiltered_bs;
> char *filename;
> const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
> int c, flags, src_flags, ret;
> @@ -3571,6 +3576,8 @@ static int img_rebase(int argc, char **argv)
> }
> bs = blk_bs(blk);
>
> + unfiltered_bs = bdrv_skip_filters(bs);
> +
> if (out_basefmt != NULL) {
> if (bdrv_find_format(out_basefmt) == NULL) {
> error_report("Invalid format name: '%s'", out_basefmt);
> @@ -3582,7 +3589,7 @@ static int img_rebase(int argc, char **argv)
> /* For safe rebasing we need to compare old and new backing file */
> if (!unsafe) {
> QDict *options = NULL;
> - BlockDriverState *base_bs = backing_bs(bs);
> + BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs);
>
> if (base_bs) {
> blk_old_backing = blk_new(qemu_get_aio_context(),
> @@ -3738,8 +3745,9 @@ static int img_rebase(int argc, char **argv)
> * If cluster wasn't changed since prefix_chain, we don't need
> * to take action
> */
> - ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs,
> - false, offset, n, &n);
> + ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
> + prefix_chain_bs, false,
> + offset, n, &n);
> if (ret < 0) {
> error_report("error while reading image metadata: %s",
> strerror(-ret));
> diff --git a/tests/qemu-iotests/204.out b/tests/qemu-iotests/204.out
> index f3a10fbe90..684774d763 100644
> --- a/tests/qemu-iotests/204.out
> +++ b/tests/qemu-iotests/204.out
> @@ -59,5 +59,6 @@ Offset Length File
> 0x900000 0x2400000 TEST_DIR/t.IMGFMT
> 0x3c00000 0x1100000 TEST_DIR/t.IMGFMT
> 0x6a00000 0x400000 TEST_DIR/t.IMGFMT
> +0x6e00000 0x1200000 TEST_DIR/t.IMGFMT.base
> No errors were found on the image.
> *** done
Reviewed-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>