From nobody Thu Nov 6 16:27:19 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1489441563523683.8455328271124; Mon, 13 Mar 2017 14:46:03 -0700 (PDT) Received: from localhost ([::1]:54556 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnXn0-0002ln-6C for importer@patchew.org; Mon, 13 Mar 2017 17:46:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43911) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnXiW-0007pM-0g for qemu-devel@nongnu.org; Mon, 13 Mar 2017 17:41:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cnXiV-00025D-1t for qemu-devel@nongnu.org; Mon, 13 Mar 2017 17:41:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58438) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cnXiS-00023x-K6; Mon, 13 Mar 2017 17:41:20 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BB3B183F46; Mon, 13 Mar 2017 21:41:20 +0000 (UTC) Received: from localhost (ovpn-204-63.brq.redhat.com [10.40.204.63]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2DLfIH5016520 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 13 Mar 2017 17:41:20 -0400 From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 13 Mar 2017 22:41:11 +0100 Message-Id: <20170313214117.27350-1-mreitz@redhat.com> In-Reply-To: <20170313214001.26339-1-mreitz@redhat.com> References: <20170313214001.26339-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 13 Mar 2017 21:41:20 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.10 10/16] block/qcow2: Lock s->lock in preallocate() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" preallocate() is and will be called only from places that do not lock s->lock: Currently that is qcow2_create2(), as of a future patch it will be called from qcow2_truncate(), too. It therefore makes sense to move locking that mutex into preallocate() itself. Signed-off-by: Max Reitz Reviewed-by: Stefan Hajnoczi --- block/qcow2.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 9abdf9e0fb..5479fed938 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2036,12 +2036,15 @@ static int qcow2_change_backing_file(BlockDriverSta= te *bs, =20 static int preallocate(BlockDriverState *bs, int64_t offset, int64_t new_l= ength) { + BDRVQcow2State *s =3D bs->opaque; uint64_t bytes; uint64_t host_offset =3D 0; unsigned int cur_bytes; int ret; QCowL2Meta *meta; =20 + qemu_co_mutex_lock(&s->lock); + assert(offset <=3D new_length); bytes =3D new_length - offset; =20 @@ -2050,7 +2053,7 @@ static int preallocate(BlockDriverState *bs, int64_t = offset, int64_t new_length) ret =3D qcow2_alloc_cluster_offset(bs, offset, &cur_bytes, &host_offset, &meta); if (ret < 0) { - return ret; + goto done; } =20 while (meta) { @@ -2060,7 +2063,7 @@ static int preallocate(BlockDriverState *bs, int64_t = offset, int64_t new_length) if (ret < 0) { qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, QCOW2_DISCARD_N= EVER); - return ret; + goto done; } =20 /* There are no dependent requests, but we need to remove our @@ -2087,11 +2090,15 @@ static int preallocate(BlockDriverState *bs, int64_= t offset, int64_t new_length) ret =3D bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1, &data, 1); if (ret < 0) { - return ret; + goto done; } } =20 - return 0; + ret =3D 0; + +done: + qemu_co_mutex_unlock(&s->lock); + return ret; } =20 static int qcow2_create2(const char *filename, int64_t total_size, @@ -2310,10 +2317,7 @@ static int qcow2_create2(const char *filename, int64= _t total_size, =20 /* And if we're supposed to preallocate metadata, do that now */ if (prealloc !=3D PREALLOC_MODE_OFF) { - BDRVQcow2State *s =3D blk_bs(blk)->opaque; - qemu_co_mutex_lock(&s->lock); ret =3D preallocate(blk_bs(blk), 0, total_size); - qemu_co_mutex_unlock(&s->lock); if (ret < 0) { error_setg_errno(errp, -ret, "Could not preallocate metadata"); goto out; --=20 2.12.0