From 9e7d75284473dcedaeab4b201a8709aa2f990ea8 Mon Sep 17 00:00:00 2001
Message-Id: <9e7d75284473dcedaeab4b201a8709aa2f990ea8.1334248851.git.minovotn@redhat.com>
From: Kevin Wolf <kwolf@redhat.com>
Date: Thu, 12 Apr 2012 12:40:21 +0200
Subject: [PATCH] block: Drain requests in bdrv_close

RH-Author: Kevin Wolf <kwolf@redhat.com>
Message-id: <1334234421-15298-1-git-send-email-kwolf@redhat.com>
Patchwork-id: 39202
O-Subject: [RHEL-6.3 qemu-kvm PATCH v2] block: Drain requests in bdrv_close
Bugzilla: 798857
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

Bugzilla: 798857
Upstream status: Submitted

If an AIO request is in flight that refers to a BlockDriverState that
has been closed and possibly even freed, more or less anything could
happen. I have seen segfaults, -EBADF return values and qcow2 sometimes
actually catches the situation in bdrv_close() and abort()s.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/block.c b/block.c
index 438449f..6c99220 100644
--- a/block.c
+++ b/block.c
@@ -687,6 +687,8 @@ void bdrv_close(BlockDriverState *bs)
         if (bs->job) {
             block_job_cancel_sync(bs->job);
         }
+        bdrv_drain_all();
+
         if (bs == bs_snapshots) {
             bs_snapshots = NULL;
         }
-- 
1.7.7.6