All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org
Subject: [PULL 21/30] qcow2: Fix preallocation on block devices
Date: Fri,  8 May 2020 14:41:26 +0200	[thread overview]
Message-ID: <20200508124135.252565-22-kwolf@redhat.com> (raw)
In-Reply-To: <20200508124135.252565-1-kwolf@redhat.com>

From: Max Reitz <mreitz@redhat.com>

Calling bdrv_getlength() to get the pre-truncate file size will not
really work on block devices, because they have always the same length,
and trying to write beyond it will fail with a rather cryptic error
message.

Instead, we should use qcow2_get_last_cluster() and bdrv_getlength()
only as a fallback.

Before this patch:
$ truncate -s 1G test.img
$ sudo losetup -f --show test.img
/dev/loop0
$ sudo qemu-img create -f qcow2 -o preallocation=full /dev/loop0 64M
Formatting '/dev/loop0', fmt=qcow2 size=67108864 cluster_size=65536
preallocation=full lazy_refcounts=off refcount_bits=16
qemu-img: /dev/loop0: Could not resize image: Failed to resize refcount
structures: No space left on device

With this patch:
$ sudo qemu-img create -f qcow2 -o preallocation=full /dev/loop0 64M
Formatting '/dev/loop0', fmt=qcow2 size=67108864 cluster_size=65536
preallocation=full lazy_refcounts=off refcount_bits=16
qemu-img: /dev/loop0: Could not resize image: Failed to resize
underlying file: Preallocation mode 'full' unsupported for this
non-regular file

So as you can see, it still fails, but now the problem is missing
support on the block device level, so we at least get a better error
message.

Note that we cannot preallocate block devices on truncate by design,
because we do not know what area to preallocate.  Their length is always
the same, the truncate operation does not change it.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20200505141801.1096763-1-mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index fc7d4b185e..11903fb547 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -4107,7 +4107,7 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
     {
         int64_t allocation_start, host_offset, guest_offset;
         int64_t clusters_allocated;
-        int64_t old_file_size, new_file_size;
+        int64_t old_file_size, last_cluster, new_file_size;
         uint64_t nb_new_data_clusters, nb_new_l2_tables;
 
         /* With a data file, preallocation means just allocating the metadata
@@ -4127,7 +4127,13 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
             ret = old_file_size;
             goto fail;
         }
-        old_file_size = ROUND_UP(old_file_size, s->cluster_size);
+
+        last_cluster = qcow2_get_last_cluster(bs, old_file_size);
+        if (last_cluster >= 0) {
+            old_file_size = (last_cluster + 1) * s->cluster_size;
+        } else {
+            old_file_size = ROUND_UP(old_file_size, s->cluster_size);
+        }
 
         nb_new_data_clusters = DIV_ROUND_UP(offset - old_length,
                                             s->cluster_size);
-- 
2.25.3



  parent reply	other threads:[~2020-05-08 12:58 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-08 12:41 [PULL 00/30] Block layer patches Kevin Wolf
2020-05-08 12:41 ` [PULL 01/30] iotests: handle tmpfs Kevin Wolf
2020-05-08 12:41 ` [PULL 02/30] iotests/082: require bochs Kevin Wolf
2020-05-08 12:41 ` [PULL 03/30] iotests/148: use skip_if_unsupported Kevin Wolf
2020-05-08 12:41 ` [PULL 04/30] iotests/041: drop self.assert_no_active_block_jobs() Kevin Wolf
2020-05-08 12:41 ` [PULL 05/30] iotests/055: refactor compressed backup to vmdk Kevin Wolf
2020-05-08 12:41 ` [PULL 06/30] iotests/055: skip vmdk target tests if vmdk is not whitelisted Kevin Wolf
2020-05-08 12:41 ` [PULL 07/30] iotests/109: mark required formats as required to support whitelisting Kevin Wolf
2020-05-08 12:41 ` [PULL 08/30] iotests/113: mark bochs " Kevin Wolf
2020-05-08 12:41 ` [PULL 09/30] qcow2: Avoid integer wraparound in qcow2_co_truncate() Kevin Wolf
2020-05-08 12:41 ` [PULL 10/30] vmdk: Rename VmdkMetaData.valid to new_allocation Kevin Wolf
2020-05-08 12:41 ` [PULL 11/30] vmdk: Fix zero cluster allocation Kevin Wolf
2020-05-08 12:41 ` [PULL 12/30] vmdk: Fix partial overwrite of zero cluster Kevin Wolf
2020-05-08 12:41 ` [PULL 13/30] vmdk: Don't update L2 table for zero write on " Kevin Wolf
2020-05-08 12:41 ` [PULL 14/30] vmdk: Flush only once in vmdk_L2update() Kevin Wolf
2020-05-08 12:41 ` [PULL 15/30] iotests: vmdk: Enable zeroed_grained=on by default Kevin Wolf
2020-05-08 12:41 ` [PULL 16/30] iotests/283: Use consistent size for source and target Kevin Wolf
2020-05-08 12:41 ` [PULL 17/30] backup: Improve error for bdrv_getlength() failure Kevin Wolf
2020-05-08 12:41 ` [PULL 18/30] backup: Make sure that source and target size match Kevin Wolf
2020-05-08 12:41 ` [PULL 19/30] iotests: Backup with different source/target size Kevin Wolf
2020-05-08 12:41 ` [PULL 20/30] iotests/055: Use cache.no-flush for vmdk target Kevin Wolf
2020-05-08 12:41 ` Kevin Wolf [this message]
2020-05-08 12:41 ` [PULL 22/30] gluster: Drop useless has_zero_init callback Kevin Wolf
2020-05-08 12:41 ` [PULL 23/30] file-win32: Support BDRV_REQ_ZERO_WRITE for truncate Kevin Wolf
2020-05-08 12:41 ` [PULL 24/30] nfs: " Kevin Wolf
2020-05-08 12:41 ` [PULL 25/30] rbd: " Kevin Wolf
2020-05-08 12:41 ` [PULL 26/30] sheepdog: " Kevin Wolf
2020-05-08 12:41 ` [PULL 27/30] ssh: " Kevin Wolf
2020-05-08 12:41 ` [PULL 28/30] parallels: Rework truncation logic Kevin Wolf
2020-05-08 12:41 ` [PULL 29/30] vhdx: " Kevin Wolf
2020-05-08 12:41 ` [PULL 30/30] block: Drop unused .bdrv_has_zero_init_truncate Kevin Wolf
2020-05-08 15:10 ` [PULL 00/30] Block layer patches 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=20200508124135.252565-22-kwolf@redhat.com \
    --to=kwolf@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.