[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 15/22] qcow2: Handle QCowL2Meta on error in preallocate_co()
From: |
Max Reitz |
Subject: |
[PULL 15/22] qcow2: Handle QCowL2Meta on error in preallocate_co() |
Date: |
Tue, 15 Sep 2020 12:46:20 +0200 |
From: Alberto Garcia <berto@igalia.com>
If qcow2_alloc_cluster_offset() or qcow2_alloc_cluster_link_l2() fail
then this function simply returns the error code, potentially leaking
the QCowL2Meta structure and leaving stale items in s->cluster_allocs.
A second problem is that this function calls qcow2_free_any_clusters()
on failure but passing a host cluster offset instead of an L2 entry.
Luckily for normal uncompressed clusters a raw offset also works like
a valid L2 entry so it works just the same, but we should be using
qcow2_free_clusters() instead.
This patch fixes both problems by using qcow2_handle_l2meta().
Signed-off-by: Alberto Garcia <berto@igalia.com>
Message-Id:
<cd3a6b9abd43f9c0b60be413d760f0cacc67eb66.1599573989.git.berto@igalia.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block/qcow2.c | 40 +++++++++++++++++-----------------------
1 file changed, 17 insertions(+), 23 deletions(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index 3e8114dcf8..d241fb734c 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2101,7 +2101,6 @@ static coroutine_fn int
qcow2_handle_l2meta(BlockDriverState *bs,
QCowL2Meta *next;
if (link_l2) {
- assert(!l2meta->prealloc);
ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
if (ret) {
goto out;
@@ -3123,7 +3122,7 @@ static int coroutine_fn preallocate_co(BlockDriverState
*bs, uint64_t offset,
int64_t file_length;
unsigned int cur_bytes;
int ret;
- QCowL2Meta *meta;
+ QCowL2Meta *meta = NULL, *m;
assert(offset <= new_length);
bytes = new_length - offset;
@@ -3134,27 +3133,17 @@ static int coroutine_fn preallocate_co(BlockDriverState
*bs, uint64_t offset,
&host_offset, &meta);
if (ret < 0) {
error_setg_errno(errp, -ret, "Allocating clusters failed");
- return ret;
+ goto out;
}
- while (meta) {
- QCowL2Meta *next = meta->next;
- meta->prealloc = true;
-
- ret = qcow2_alloc_cluster_link_l2(bs, meta);
- if (ret < 0) {
- error_setg_errno(errp, -ret, "Mapping clusters failed");
- qcow2_free_any_clusters(bs, meta->alloc_offset,
- meta->nb_clusters,
QCOW2_DISCARD_NEVER);
- return ret;
- }
-
- /* There are no dependent requests, but we need to remove our
- * request from the list of in-flight requests */
- QLIST_REMOVE(meta, next_in_flight);
+ for (m = meta; m != NULL; m = m->next) {
+ m->prealloc = true;
+ }
- g_free(meta);
- meta = next;
+ ret = qcow2_handle_l2meta(bs, &meta, true);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Mapping clusters failed");
+ goto out;
}
/* TODO Preallocate data if requested */
@@ -3171,7 +3160,8 @@ static int coroutine_fn preallocate_co(BlockDriverState
*bs, uint64_t offset,
file_length = bdrv_getlength(s->data_file->bs);
if (file_length < 0) {
error_setg_errno(errp, -file_length, "Could not get file size");
- return file_length;
+ ret = file_length;
+ goto out;
}
if (host_offset + cur_bytes > file_length) {
@@ -3181,11 +3171,15 @@ static int coroutine_fn preallocate_co(BlockDriverState
*bs, uint64_t offset,
ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false,
mode, 0, errp);
if (ret < 0) {
- return ret;
+ goto out;
}
}
- return 0;
+ ret = 0;
+
+out:
+ qcow2_handle_l2meta(bs, &meta, false);
+ return ret;
}
/* qcow2_refcount_metadata_size:
--
2.26.2
- [PULL 05/22] qemu-iotests: Simplify FilePath __init__, (continued)
- [PULL 05/22] qemu-iotests: Simplify FilePath __init__, Max Reitz, 2020/09/15
- [PULL 06/22] block/quorum.c: stable children names, Max Reitz, 2020/09/15
- [PULL 07/22] qemu-img: avoid unaligned read requests during convert, Max Reitz, 2020/09/15
- [PULL 08/22] qcow2: Use macros for the L1, refcount and bitmap table entry sizes, Max Reitz, 2020/09/15
- [PULL 10/22] qcow2: Don't check nb_clusters when removing l2meta from the list, Max Reitz, 2020/09/15
- [PULL 11/22] qcow2: Rewrite the documentation of qcow2_alloc_cluster_offset(), Max Reitz, 2020/09/15
- [PULL 12/22] qemu-img: Explicit number replaced by a constant, Max Reitz, 2020/09/15
- [PULL 09/22] qcow2: Fix removal of list members from BDRVQcow2State.cluster_allocs, Max Reitz, 2020/09/15
- [PULL 13/22] iotests: Skip test_stream_parallel in test 030 when doing "make check", Max Reitz, 2020/09/15
- [PULL 14/22] block/vhdx: Support vhdx image only with 512 bytes logical sector size, Max Reitz, 2020/09/15
- [PULL 15/22] qcow2: Handle QCowL2Meta on error in preallocate_co(),
Max Reitz <=
- [PULL 16/22] qcow2: Make qcow2_free_any_clusters() free only one cluster, Max Reitz, 2020/09/15
- [PULL 17/22] qcow2: Return the original error code in qcow2_co_pwrite_zeroes(), Max Reitz, 2020/09/15
- [PULL 18/22] block/rbd: remove runtime_opts, Max Reitz, 2020/09/15
- [PULL 19/22] block/qcow: remove runtime opts, Max Reitz, 2020/09/15
- [PULL 22/22] block/rbd: add 'namespace' to qemu_rbd_strong_runtime_opts[], Max Reitz, 2020/09/15
- [PULL 21/22] qcow2: Convert qcow2_alloc_cluster_offset() into qcow2_alloc_host_offset(), Max Reitz, 2020/09/15
- [PULL 20/22] qcow2: Make preallocate_co() resize the image to the correct size, Max Reitz, 2020/09/15
- Re: [PULL 00/22] Block patches, Peter Maydell, 2020/09/15