From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43184) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VmUs4-0004W7-En for qemu-devel@nongnu.org; Fri, 29 Nov 2013 15:41:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VmUry-00047n-8V for qemu-devel@nongnu.org; Fri, 29 Nov 2013 15:41:04 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53408) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VmUrx-00047Z-WC for qemu-devel@nongnu.org; Fri, 29 Nov 2013 15:40:58 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rATKeuJt012537 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 29 Nov 2013 15:40:56 -0500 From: Max Reitz Date: Fri, 29 Nov 2013 21:41:29 +0100 Message-Id: <1385757689-23524-1-git-send-email-mreitz@redhat.com> Subject: [Qemu-devel] [PATCH v2] block: Close backing file early in bdrv_img_create List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz Leaving the backing file open although it is not needed anymore can cause problems if it is opened through a block driver which allows exclusive access only and if the create function of the block driver used for the top image (the one being created) tries to close and reopen the image file (which will include opening the backing file a second time). In particular, this will happen with a backing file opened through qemu-nbd and using qcow2 as the top image file format (which reopens the image to flush it to disk). Signed-off-by: Max Reitz --- v2: - Minimizing the changes prevents introducing a leak of the BlockDriverState in case of an error in bdrv_open() (thanks, Kevin). --- block.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/block.c b/block.c index 382ea71..6d571ad 100644 --- a/block.c +++ b/block.c @@ -4608,6 +4608,9 @@ void bdrv_img_create(const char *filename, const char *fmt, snprintf(buf, sizeof(buf), "%" PRId64, size); set_option_parameter(param, BLOCK_OPT_SIZE, buf); + + bdrv_unref(bs); + bs = NULL; } else { error_setg(errp, "Image creation needs a size parameter"); goto out; -- 1.8.4.2