From nobody Wed Apr 16 04:32:52 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.zohomail.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 1499793378762739.6801596587388; Tue, 11 Jul 2017 10:16:18 -0700 (PDT) Received: from localhost ([::1]:47722 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUylj-0002zH-Fj for importer@patchew.org; Tue, 11 Jul 2017 13:16:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47258) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUxmc-0002SX-HB for qemu-devel@nongnu.org; Tue, 11 Jul 2017 12:13:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dUxmZ-0007Tb-Ez for qemu-devel@nongnu.org; Tue, 11 Jul 2017 12:13:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52964) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dUxmU-0007RM-3w; Tue, 11 Jul 2017 12:12:58 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2F910C054C5F; Tue, 11 Jul 2017 16:12:57 +0000 (UTC) Received: from localhost (ovpn-204-123.brq.redhat.com [10.40.204.123]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 79D277DE29; Tue, 11 Jul 2017 16:12:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2F910C054C5F Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2F910C054C5F From: Max Reitz To: qemu-block@nongnu.org Date: Tue, 11 Jul 2017 18:08:08 +0200 Message-Id: <20170711160814.20941-80-mreitz@redhat.com> In-Reply-To: <20170711160814.20941-1-mreitz@redhat.com> References: <20170711160814.20941-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Tue, 11 Jul 2017 16:12:57 +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] [PULL 79/85] 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: 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 otherwise need to 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: Eric Blake Reviewed-by: Stefan Hajnoczi Message-id: 20170613202107.10125-11-mreitz@redhat.com Signed-off-by: Max Reitz --- block/qcow2.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 1ee8452..d3fb2f3 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2486,12 +2486,17 @@ static int qcow2_set_up_encryption(BlockDriverState= *bs, const char *encryptfmt, static int preallocate(BlockDriverState *bs, uint64_t offset, uint64_t new_length) { + BDRVQcow2State *s =3D bs->opaque; uint64_t bytes; uint64_t host_offset =3D 0; unsigned int cur_bytes; int ret; QCowL2Meta *meta; =20 + if (qemu_in_coroutine()) { + qemu_co_mutex_lock(&s->lock); + } + assert(offset <=3D new_length); bytes =3D new_length - offset; =20 @@ -2500,7 +2505,7 @@ static int preallocate(BlockDriverState *bs, ret =3D qcow2_alloc_cluster_offset(bs, offset, &cur_bytes, &host_offset, &meta); if (ret < 0) { - return ret; + goto done; } =20 while (meta) { @@ -2510,7 +2515,7 @@ static int preallocate(BlockDriverState *bs, 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 @@ -2537,11 +2542,17 @@ static int preallocate(BlockDriverState *bs, 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: + if (qemu_in_coroutine()) { + qemu_co_mutex_unlock(&s->lock); + } + return ret; } =20 /* qcow2_refcount_metadata_size: @@ -2835,10 +2846,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.9.4