preallocate() is and will be called only from places that do not lock
s->lock: Currently that is qcow2_create2(), as of a future patch it will
be called from qcow2_truncate(), too.
It therefore makes sense to move locking that mutex into preallocate()
itself.
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block/qcow2.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index 9abdf9e0fb..5479fed938 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2036,12 +2036,15 @@ static int qcow2_change_backing_file(BlockDriverState *bs,
static int preallocate(BlockDriverState *bs, int64_t offset, int64_t new_length)
{
+ BDRVQcow2State *s = bs->opaque;
uint64_t bytes;
uint64_t host_offset = 0;
unsigned int cur_bytes;
int ret;
QCowL2Meta *meta;
+ qemu_co_mutex_lock(&s->lock);
+
assert(offset <= new_length);
bytes = new_length - offset;
@@ -2050,7 +2053,7 @@ static int preallocate(BlockDriverState *bs, int64_t offset, int64_t new_length)
ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
&host_offset, &meta);
if (ret < 0) {
- return ret;
+ goto done;
}
while (meta) {
@@ -2060,7 +2063,7 @@ static int preallocate(BlockDriverState *bs, int64_t offset, int64_t new_length)
if (ret < 0) {
qcow2_free_any_clusters(bs, meta->alloc_offset,
meta->nb_clusters, QCOW2_DISCARD_NEVER);
- return ret;
+ goto done;
}
/* There are no dependent requests, but we need to remove our
@@ -2087,11 +2090,15 @@ static int preallocate(BlockDriverState *bs, int64_t offset, int64_t new_length)
ret = bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1,
&data, 1);
if (ret < 0) {
- return ret;
+ goto done;
}
}
- return 0;
+ ret = 0;
+
+done:
+ qemu_co_mutex_unlock(&s->lock);
+ return ret;
}
static int qcow2_create2(const char *filename, int64_t total_size,
@@ -2310,10 +2317,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
/* And if we're supposed to preallocate metadata, do that now */
if (prealloc != PREALLOC_MODE_OFF) {
- BDRVQcow2State *s = blk_bs(blk)->opaque;
- qemu_co_mutex_lock(&s->lock);
ret = preallocate(blk_bs(blk), 0, total_size);
- qemu_co_mutex_unlock(&s->lock);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not preallocate metadata");
goto out;
--
2.12.0