From patchwork Fri Nov 29 20:41:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 295519 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 51B472C00C3 for ; Sat, 30 Nov 2013 07:41:26 +1100 (EST) Received: from localhost ([::1]:49366 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VmUsN-0004Wg-3p for incoming@patchwork.ozlabs.org; Fri, 29 Nov 2013 15:41:23 -0500 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 Received: from localhost (ovpn-116-54.ams2.redhat.com [10.36.116.54]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id rATKerhx032036 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 29 Nov 2013 15:40:55 -0500 From: Max Reitz To: qemu-devel@nongnu.org Date: Fri, 29 Nov 2013 21:41:29 +0100 Message-Id: <1385757689-23524-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH v2] block: Close backing file early in bdrv_img_create X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: Benoit Canet Reviewed-by: Kevin Wolf --- 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;