From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55233) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZCv9U-000661-R9 for qemu-devel@nongnu.org; Wed, 08 Jul 2015 15:37:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZCv9S-00032R-Cc for qemu-devel@nongnu.org; Wed, 08 Jul 2015 15:37:04 -0400 From: Kevin Wolf Date: Wed, 8 Jul 2015 21:36:42 +0200 Message-Id: <1436384203-10576-5-git-send-email-kwolf@redhat.com> In-Reply-To: <1436384203-10576-1-git-send-email-kwolf@redhat.com> References: <1436384203-10576-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH for-2.4 4/5] block: Reorder cleanups in bdrv_close() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: kwolf@redhat.com, berto@igalia.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com Block drivers may still want to access their child nodes in their .bdrv_close handler. If they unref and/or detach a child by themselves, this should not result in a double free. There is additional code for backing files, which are just a special case of child nodes. The same applies for them. Signed-off-by: Kevin Wolf --- block.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/block.c b/block.c index b723cf2..d5c9f03 100644 --- a/block.c +++ b/block.c @@ -1901,6 +1901,14 @@ void bdrv_close(BlockDriverState *bs) if (bs->drv) { BdrvChild *child, *next; + bs->drv->bdrv_close(bs); + + if (bs->backing_hd) { + BlockDriverState *backing_hd = bs->backing_hd; + bdrv_set_backing_hd(bs, NULL); + bdrv_unref(backing_hd); + } + QLIST_FOREACH_SAFE(child, &bs->children, next, next) { /* TODO Remove bdrv_unref() from drivers' close function and use * bdrv_unref_child() here */ @@ -1910,12 +1918,6 @@ void bdrv_close(BlockDriverState *bs) bdrv_detach_child(child); } - if (bs->backing_hd) { - BlockDriverState *backing_hd = bs->backing_hd; - bdrv_set_backing_hd(bs, NULL); - bdrv_unref(backing_hd); - } - bs->drv->bdrv_close(bs); g_free(bs->opaque); bs->opaque = NULL; bs->drv = NULL; -- 1.8.3.1