qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Peter Maydell <peter.maydell@linaro.org>
Subject: [Qemu-devel] [PULL 13/15] qcow2: Discard unaligned tail when wiping image
Date: Mon,  3 Apr 2017 17:33:53 +0200	[thread overview]
Message-ID: <20170403153355.19722-14-mreitz@redhat.com> (raw)
In-Reply-To: <20170403153355.19722-1-mreitz@redhat.com>

From: Eric Blake <eblake@redhat.com>

There is a subtle difference between the fast (qcow2v3 with no
extra data) and slow path (qcow2v2 format [aka 0.10], or when a
snapshot is present) of qcow2_make_empty().  The slow path fails
to discard the final (partial) cluster of an unaligned image.

The problem stems from the fact that qcow2_discard_clusters() was
silently ignoring sub-cluster head and tail on unaligned requests.
A quick audit of all callers shows that qcow2_snapshot_create() has
always passed a cluster-aligned request since the call was added
in commit 1ebf561; qcow2_co_pdiscard() has passed a cluster-aligned
request since commit ecdbead taught the block layer about preferred
discard alignment; and qcow2_make_empty() was fixed to pass an
aligned start (but not necessarily end) in commit a3e1505.

Asserting that the start is always aligned also points out that we
now have a dead check: rounding the end offset down can never result
in a value less than the aligned start offset (the check was rendered
dead with commit ecdbead).  Meanwhile, we do not want to round the
end cluster down in the one case of the end offset matching the
(unaligned) file size - that final partial cluster should still be
discarded.

With those fixes in place, the fast and slow paths are back in sync
at discarding an entire image; the next patch will update
qemu-iotests to ensure we don't regress.

Note that bdrv_co_pdiscard ignores ALL partial cluster requests,
including the partial cluster at the end of an image; it can be
argued that the partial cluster at the end should be special-cased
so that a guest issuing discard requests at proper alignments
everywhere else can likewise empty the entire image.  But that
optimization is left for another day.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-id: 20170331185356.2479-3-eblake@redhat.com
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/qcow2-cluster.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 78c11d4948..100398c565 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1519,12 +1519,10 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
 
     end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS);
 
-    /* Round start up and end down */
-    offset = align_offset(offset, s->cluster_size);
-    end_offset = start_of_cluster(s, end_offset);
-
-    if (offset > end_offset) {
-        return 0;
+    /* The caller must cluster-align start; round end down except at EOF */
+    assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
+    if (end_offset != bs->total_sectors * BDRV_SECTOR_SIZE) {
+        end_offset = start_of_cluster(s, end_offset);
     }
 
     nb_clusters = size_to_clusters(s, end_offset - offset);
-- 
2.12.1

  parent reply	other threads:[~2017-04-03 15:34 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-03 15:33 [Qemu-devel] [PULL 00/15] Block patches for rc3 Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 01/15] block: add missed aio_context_acquire into release_drive Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 02/15] nbd sockets vnc: Mark problematic address family tests TODO Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 03/15] char: Fix socket with "type": "vsock" address Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 04/15] io vnc sockets: Clean up SocketAddressKind switches Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 05/15] block: Document -drive problematic code and bugs Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 06/15] gluster: Prepare for SocketAddressFlat extension Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 07/15] qapi-schema: SocketAddressFlat variants 'vsock' and 'fd' Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 08/15] sockets: New helper socket_address_crumple() Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 09/15] nbd: Tidy up blockdev-add interface Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 10/15] sheepdog: Fix blockdev-add Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 11/15] qemu-io-cmds: Assert that global and nofile commands don't use ct->perms Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 12/15] iotests: fix 097 when run with qcow Max Reitz
2017-04-03 15:33 ` Max Reitz [this message]
2017-04-03 15:33 ` [Qemu-devel] [PULL 14/15] iotests: Improve image-clear tests on non-aligned image Max Reitz
2017-04-03 15:33 ` [Qemu-devel] [PULL 15/15] block/parallels: Avoid overflows Max Reitz
2017-04-03 16:48 ` [Qemu-devel] [PULL 00/15] Block patches for rc3 Peter Maydell

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=20170403153355.19722-14-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=peter.maydell@linaro.org \
    --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).