Skip to contentSkip to conversation

qcow2: Flush image after creation

PATCH Superseded Max Reitz 3 participants 3 messages Oct 23, 2013 – Oct 24, 2013 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 qcow2: Flush image after creation

Opening the qcow2 image with BDRV_O_NO_FLUSH prevents any flushes during the image creation. This means that the image has not yet been flushed to disk when qemu-img create exits. This flush is delayed until the next operation on the image involving opening it without BDRV_O_NO_FLUSH and closing (or directly flushing) it. For large images and/or images with a small cluster size and preallocated metadata, this flush may take a significant amount of time and may occur unexpectedly.

Reopening the image without BDRV_O_NO_FLUSH right before the end of qcow2_create2() results in preponing the potentially costly flush into the image creation, which is expected to take some time (whereas successive image operations may be not).

---
 block/qcow2.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
block/qcow2.c +9 -1
index c1abaff..8b98c3a 100644
@@ -1584,7 +1584,15 @@ static int qcow2_create2(const char *filename, int64_t total_size,
         }
     }
 
-    ret = 0;
+    bdrv_close(bs);
+
+    /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
+    ret = bdrv_open(bs, filename, NULL,
+        BDRV_O_RDWR | BDRV_O_CACHE_WB, drv, &local_err);
+    if (error_is_set(&local_err)) {
+        error_propagate(errp, local_err);
+    }
+
 out:
     bdrv_unref(bs);
     return ret;
Signature · 1 line
1.8.3.1
#1 lore raw

Am 23.10.2013 um 21:40 hat Max Reitz geschrieben:

Quoted 23 lines

Opening the qcow2 image with BDRV_O_NO_FLUSH prevents any flushes during the image creation. This means that the image has not yet been flushed to disk when qemu-img create exits. This flush is delayed until the next operation on the image involving opening it without BDRV_O_NO_FLUSH and closing (or directly flushing) it. For large images and/or images with a small cluster size and preallocated metadata, this flush may take a significant amount of time and may occur unexpectedly.

Reopening the image without BDRV_O_NO_FLUSH right before the end of qcow2_create2() results in preponing the potentially costly flush into the image creation, which is expected to take some time (whereas successive image operations may be not).

---
 block/qcow2.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
block/qcow2.c +0 -0
index c1abaff..8b98c3a 100644

block/qcow2.c static int qcow2_create2(const char *filename, int64_t total_size, · line 1587

Quoted 4 lines
block/qcow2.c +0 -1
@@ -1584,7 +1584,15 @@ static int qcow2_create2(const char *filename, int64_t total_size,
         }
     }
 
-    ret = 0;

I would prefer to keep the explicit ret = 0 there (just like the unnecessary last 'goto out:', it just makes things more obvious and consistent)

block/qcow2.c static int qcow2_create2(const char *filename, int64_t total_size, · line 1593

Quoted 7 lines
block/qcow2.c +7 -0
+    bdrv_close(bs);
+
+    /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
+    ret = bdrv_open(bs, filename, NULL,
+        BDRV_O_RDWR | BDRV_O_CACHE_WB, drv, &local_err);
+    if (error_is_set(&local_err)) {
+        error_propagate(errp, local_err);

So a goto here wouldn't hurt either. Note how the unnecessary goto in the block before allowed you to just add your new code without modifying existing parts.

block/qcow2.c static int qcow2_create2(const char *filename, int64_t total_size, · line 1598

Quoted 5 lines
block/qcow2.c +2 -0
+    }
+
 out:
     bdrv_unref(bs);
     return ret;

Kevin

Reviewed-by on v1 1/1
#2 lore raw
In reply to Max Reitz
Quoted 10 lines · Max Reitz

Opening the qcow2 image with BDRV_O_NO_FLUSH prevents any flushes during the image creation. This means that the image has not yet been flushed to disk when qemu-img create exits. This flush is delayed until the next operation on the image involving opening it without BDRV_O_NO_FLUSH and closing (or directly flushing) it. For large images and/or images with a small cluster size and preallocated metadata, this flush may take a significant amount of time and may occur unexpectedly.

Reopening the image without BDRV_O_NO_FLUSH right before the end of qcow2_create2() results in preponing the potentially costly flush into

s/preponing/hoisting/

Quoted 7 lines

the image creation, which is expected to take some time (whereas successive image operations may be not).

---
 block/qcow2.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

block/qcow2.c static int qcow2_create2(const char *filename, int64_t total_size, · line 1591

Quoted 6 lines
block/qcow2.c +5 -1
-    ret = 0;
+    bdrv_close(bs);
+
+    /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
+    ret = bdrv_open(bs, filename, NULL,
+        BDRV_O_RDWR | BDRV_O_CACHE_WB, drv, &local_err);

I would probably have indented as:

ret = bdrv_open(bs, filename, NULL,
BDRV_O_RDWR | BDRV_O_CACHE_WB,
drv, &local_err);

but it's trivial enough that I'm also fine with your choice.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org