qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Max Reitz <mreitz@redhat.com>, qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH 2/2] iotests: Test large write request to qcow2 file
Date: Thu, 10 Oct 2019 11:25:02 -0500	[thread overview]
Message-ID: <462986b3-2014-a611-5bd4-e7fdf8fef6d5@redhat.com> (raw)
In-Reply-To: <20191010100858.1261-3-mreitz@redhat.com>

On 10/10/19 5:08 AM, Max Reitz wrote:
> Without HEAD^, the following happens when you attempt a large write
> request to a qcow2 file such that the number of bytes covered by all
> clusters involved in a single allocation will exceed INT_MAX:
> 
> (A) handle_alloc_space() decides to fill the whole area with zeroes and
>      fails because bdrv_co_pwrite_zeroes() fails (the request is too
>      large).
> 
> (B) If handle_alloc_space() does not do anything, but merge_cow()
>      decides that the requests can be merged, it will create a too long
>      IOV that later cannot be written.
> 
> (C) Otherwise, all parts will be written separately, so those requests
>      will work.
> 
> In either B or C, though, qcow2_alloc_cluster_link_l2() will have an
> overflow: We use an int (i) to iterate over nb_clusters, and then
> calculate the L2 entry based on "i << s->cluster_bits" -- which will
> overflow if the range covers more than INT_MAX bytes.  This then leads
> to image corruption because the L2 entry will be wrong (it will be
> recognized as a compressed cluster).
> 
> Even if that were not the case, the .cow_end area would be empty
> (because handle_alloc() will cap avail_bytes and nb_bytes at INT_MAX, so
> their difference (which is the .cow_end size) will be 0).
> 
> So this test checks that on such large requests, the image will not be
> corrupted.  Unfortunately, we cannot check whether COW will be handled
> correctly, because that data is discarded when it is written to null-co
> (but we have to use null-co, because writing 2 GB of data in a test is
> not quite reasonable).
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>   tests/qemu-iotests/268     | 83 ++++++++++++++++++++++++++++++++++++++
>   tests/qemu-iotests/268.out |  9 +++++
>   tests/qemu-iotests/group   |  1 +
>   3 files changed, 93 insertions(+)
>   create mode 100755 tests/qemu-iotests/268
>   create mode 100644 tests/qemu-iotests/268.out
> 
> diff --git a/tests/qemu-iotests/268 b/tests/qemu-iotests/268
> new file mode 100755
> index 0000000000..b9a12b908c
> --- /dev/null
> +++ b/tests/qemu-iotests/268

> +# We want a null-co as the data file, because it allows us to quickly
> +# "write" 2G of data without using any space.
> +# (qemu-img create does not like it, though, because null-co does not
> +# support image creation.)
> +$QEMU_IMG amend -o data_file="json:{'driver':'null-co',,'size':'4294967296'}" \
> +    "$TEST_IMG"
> +

A bit awkward, but works.

> +# This gives us a range of:
> +#   2^31 - 512 + 768 - 1 = 2^31 + 255 > 2^31
> +# until the beginning of the end COW block.  (The total allocation
> +# size depends on the cluster size, but all that is important is that
> +# it exceeds INT_MAX.)
> +#
> +# 2^31 - 512 is the maximum request size.  We want this to result in a
> +# single allocation, and because the qcow2 driver splits allocations
> +# on L2 boundaries, we need large L2 tables; hence the cluster size of
> +# 2 MB.  (Anything from 256 kB should work, though, because then one L2
> +# table covers 8 GB.)
> +$QEMU_IO -c "write 768 $((2 ** 31 - 512))" "$TEST_IMG" | _filter_qemu_io

Yep, that causes the rounding issue that requires being able to handle > 
2G gracefully.

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


  reply	other threads:[~2019-10-10 16:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-10 10:08 [PATCH 0/2] qcow2: Limit total allocation range to INT_MAX Max Reitz
2019-10-10 10:08 ` [PATCH 1/2] " Max Reitz
2019-10-10 15:56   ` Eric Blake
2019-10-11 10:18   ` Philippe Mathieu-Daudé
2019-10-10 10:08 ` [PATCH 2/2] iotests: Test large write request to qcow2 file Max Reitz
2019-10-10 16:25   ` Eric Blake [this message]
2019-10-14 15:12 ` [PATCH 0/2] qcow2: Limit total allocation range to INT_MAX Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=462986b3-2014-a611-5bd4-e7fdf8fef6d5@redhat.com \
    --to=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).