summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMax Reitz <mreitz@redhat.com>2017-06-13 22:21:01 +0200
committerMax Reitz <mreitz@redhat.com>2017-07-11 17:45:02 +0200
commit652fecd0058de7d90609c347bfdbeff76ef817a6 (patch)
tree1052709f7b40a9f71edbf733acdce1e223ac0342
parent7bc45dc17265772abe5dd1d4649c7bb42e1c6df1 (diff)
downloadfocaccia-qemu-652fecd0058de7d90609c347bfdbeff76ef817a6.tar.gz
focaccia-qemu-652fecd0058de7d90609c347bfdbeff76ef817a6.zip
block/qcow2: Lock s->lock in preallocate()
preallocate() is and will be called only from places that do not
otherwise need to 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>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 20170613202107.10125-11-mreitz@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
-rw-r--r--block/qcow2.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/block/qcow2.c b/block/qcow2.c
index 1ee8452d80..d3fb2f3f79 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2486,12 +2486,17 @@ static int qcow2_set_up_encryption(BlockDriverState *bs, const char *encryptfmt,
 static int preallocate(BlockDriverState *bs,
                        uint64_t offset, uint64_t new_length)
 {
+    BDRVQcow2State *s = bs->opaque;
     uint64_t bytes;
     uint64_t host_offset = 0;
     unsigned int cur_bytes;
     int ret;
     QCowL2Meta *meta;
 
+    if (qemu_in_coroutine()) {
+        qemu_co_mutex_lock(&s->lock);
+    }
+
     assert(offset <= new_length);
     bytes = new_length - offset;
 
@@ -2500,7 +2505,7 @@ static int preallocate(BlockDriverState *bs,
         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
                                          &host_offset, &meta);
         if (ret < 0) {
-            return ret;
+            goto done;
         }
 
         while (meta) {
@@ -2510,7 +2515,7 @@ static int preallocate(BlockDriverState *bs,
             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
@@ -2537,11 +2542,17 @@ static int preallocate(BlockDriverState *bs,
         ret = bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1,
                           &data, 1);
         if (ret < 0) {
-            return ret;
+            goto done;
         }
     }
 
-    return 0;
+    ret = 0;
+
+done:
+    if (qemu_in_coroutine()) {
+        qemu_co_mutex_unlock(&s->lock);
+    }
+    return ret;
 }
 
 /* qcow2_refcount_metadata_size:
@@ -2835,10 +2846,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;