Skip to contentSkip to conversation

block: fix bdrv_flush() ordering in bdrv_close()

PATCH Superseded Stefan Hajnoczi 2 participants 3 messages Jul 1, 2013 – Jul 2, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix bdrv_flush() ordering in bdrv_close()

Since 80ccf93b we flush the block device during close. The bdrv_drain_all() call should come before bdrv_flush() to ensure guest write requests have completed. Otherwise we may miss pending writes when flushing.

However, there is still a slight change that cancelling a blockjob or doing bdrv_flush() might leave an I/O request running, so call bdrv_drain_all() again for safety as the final step.

---
 block.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block.c +3 -2
index 6c493ad..fca55b1 100644
@@ -1358,11 +1358,12 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state)
 
 void bdrv_close(BlockDriverState *bs)
 {
-    bdrv_flush(bs);
+    bdrv_drain_all(); /* complete guest I/O */
     if (bs->job) {
         block_job_cancel_sync(bs->job);
     }
-    bdrv_drain_all();
+    bdrv_flush(bs);
+    bdrv_drain_all(); /* in case blockjob or flush started I/O */
     notifier_list_notify(&bs->close_notifiers, bs);
 
     if (bs->drv) {
Signature · 1 line
1.8.1.4
#1 lore raw

Am 01.07.2013 um 13:29 hat Stefan Hajnoczi geschrieben:

Quoted 22 lines

Since 80ccf93b we flush the block device during close. The bdrv_drain_all() call should come before bdrv_flush() to ensure guest write requests have completed. Otherwise we may miss pending writes when flushing.

However, there is still a slight change that cancelling a blockjob or doing bdrv_flush() might leave an I/O request running, so call bdrv_drain_all() again for safety as the final step.

---
 block.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block.c +0 -0
index 6c493ad..fca55b1 100644
@@ -1358,11 +1358,12 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state)
 
 void bdrv_close(BlockDriverState *bs)

block.c void bdrv_reopen_abort(BDRVReopenState *reopen_state) · line 1367

Quoted 10 lines
block.c +3 -2
 {
-    bdrv_flush(bs);
+    bdrv_drain_all(); /* complete guest I/O */
     if (bs->job) {
         block_job_cancel_sync(bs->job);
     }
-    bdrv_drain_all();
+    bdrv_flush(bs);
+    bdrv_drain_all(); /* in case blockjob or flush started I/O */
     notifier_list_notify(&bs->close_notifiers, bs);

I think we need the bdrv_drain_all() immediately before calling bdrv_flush(), so that block job writes are flushed as well. You can probably move the first one there, there doesn't seem to be a reason to drain guest requests and block job requests separately.

Kevin

#2 lore raw
Quoted 22 lines · Kevin Wolf

Am 01.07.2013 um 13:29 hat Stefan Hajnoczi geschrieben:

Quoted 21 lines

Since 80ccf93b we flush the block device during close. The bdrv_drain_all() call should come before bdrv_flush() to ensure guest write requests have completed. Otherwise we may miss pending writes when flushing.

However, there is still a slight change that cancelling a blockjob or doing bdrv_flush() might leave an I/O request running, so call bdrv_drain_all() again for safety as the final step.

---
 block.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block.c +0 -0
index 6c493ad..fca55b1 100644
@@ -1358,11 +1358,12 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state)
 
Quoted 16 lines

block.c void bdrv_reopen_abort(BDRVReopenState *reopen_state) · line 1367

Quoted 11 lines
block.c +3 -2
 void bdrv_close(BlockDriverState *bs)
 {
-    bdrv_flush(bs);
+    bdrv_drain_all(); /* complete guest I/O */
     if (bs->job) {
         block_job_cancel_sync(bs->job);
     }
-    bdrv_drain_all();
+    bdrv_flush(bs);
+    bdrv_drain_all(); /* in case blockjob or flush started I/O */
     notifier_list_notify(&bs->close_notifiers, bs);

I think we need the bdrv_drain_all() immediately before calling bdrv_flush(), so that block job writes are flushed as well. You can probably move the first one there, there doesn't seem to be a reason to drain guest requests and block job requests separately.

Good idea, will fix in v2.

Stefan