From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48465) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXrdq-0008BD-3P for qemu-devel@nongnu.org; Mon, 22 Feb 2016 09:39:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXrdp-0007WE-3U for qemu-devel@nongnu.org; Mon, 22 Feb 2016 09:39:14 -0500 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]:38241) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXrdo-0007W0-SS for qemu-devel@nongnu.org; Mon, 22 Feb 2016 09:39:13 -0500 Received: by mail-wm0-x229.google.com with SMTP id a4so165734302wme.1 for ; Mon, 22 Feb 2016 06:39:12 -0800 (PST) Sender: Paolo Bonzini From: Paolo Bonzini Date: Mon, 22 Feb 2016 15:39:04 +0100 Message-Id: <1456151945-11225-3-git-send-email-pbonzini@redhat.com> In-Reply-To: <1456151945-11225-1-git-send-email-pbonzini@redhat.com> References: <1456151945-11225-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 2/3] block: keep BlockBackend alive until device finalize time List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Markus Armbruster , Max Reitz While the next patch will anticipate the death of the DriveInfo data structure, the BlockBackend must survive after unrealize, for example in case there are outstanding operations on it. The good thing is that we can just use reference counting to do it. Signed-off-by: Paolo Bonzini --- hw/core/qdev-properties-system.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c index 469ba8a..5e84b55 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -93,6 +93,7 @@ static void parse_drive(DeviceState *dev, const char *str, void **ptr, return; } *ptr = blk; + blk_ref(blk); } static void release_drive(Object *obj, const char *name, void *opaque) @@ -101,13 +102,17 @@ static void release_drive(Object *obj, const char *name, void *opaque) Property *prop = opaque; BlockBackend **ptr = qdev_get_prop_ptr(dev, prop); - if (*ptr && blk_get_attached_dev(*ptr) != NULL) { - /* Unrealize has already called blk_detach_dev and blockdev_del_drive - * if the device has been realized; in that case, blk_get_attached_dev - * returns NULL. Thus, we get here if the device failed to realize, - * and the -drive must not be released. - */ - blk_detach_dev(*ptr, dev); + if (*ptr) { + if (blk_get_attached_dev(*ptr) != NULL) { + /* Unrealize has already called blk_detach_dev and + * blockdev_del_drive if the device has been realized; + * in that case, blk_get_attached_dev returns NULL. Thus, + * we get here if the device failed to realize, and the + * -drive must not be released. + */ + blk_detach_dev(*ptr, dev); + } + blk_unref(*ptr); } } -- 2.5.0