@@ -515,6 +515,7 @@ static int qcow2_open(BlockDriverState *bs, int flags)
/* Initialise locks */
qemu_co_mutex_init(&s->lock);
+ qemu_co_mutex_init(&s->dedup_lock);
/* Repair image if dirty */
if (!(flags & BDRV_O_CHECK) && !bs->read_only &&
@@ -805,9 +806,19 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
s->cluster_cache_offset = -1; /* disable compressed cache */
+ atomic_dedup_is_running = qcow2_dedup_is_running(bs);
+
+ if (atomic_dedup_is_running) {
+ /* This mutex is used to serialize the write requests in the dedup case.
+ * The goal is to avoid that the dedup process concurrents requests to
+ * the same clusters and corrupt data.
+ * With qcow2_dedup_read_missing_and_concatenate that would not work.
+ */
+ qemu_co_mutex_lock(&s->dedup_lock);
+ }
+
qemu_co_mutex_lock(&s->lock);
- atomic_dedup_is_running = qcow2_dedup_is_running(bs);
if (atomic_dedup_is_running) {
QTAILQ_INIT(&ds.undedupables);
ds.phash.reuse = false;
@@ -977,6 +988,10 @@ fail:
g_free(l2meta);
}
+ if (atomic_dedup_is_running) {
+ qemu_co_mutex_unlock(&s->dedup_lock);
+ }
+
qemu_iovec_destroy(&hd_qiov);
qemu_vfree(cluster_data);
qemu_vfree(dedup_cluster_data);
@@ -236,6 +236,7 @@ typedef struct BDRVQcowState {
GTree *dedup_tree_by_hash;
CoMutex lock;
+ CoMutex dedup_lock;
uint32_t crypt_method; /* current crypt method, 0 if no key yet */
uint32_t crypt_method_header;
This fixes the sub cluster sized writes race conditions while waiting for a faster solution. Signed-off-by: Benoit Canet <benoit@irqsave.net> --- block/qcow2.c | 17 ++++++++++++++++- block/qcow2.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-)