From nobody Sat Feb 7 07:09:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1568111661; cv=none; d=zoho.com; s=zohoarc; b=KBfe+8cB4gmo10OxhS6oEmzsw92d9UxHhD3vYdhKT0qVpyO22ywgULKNzJ2mQoMmg4ivfTWbsF5R8RAUgB34F18Gyd0bmVFaXL7kM7PQEiAdNwxE9ihLxbP9lPrWNUdJNAVA2Euk3ZVlW8RpREO7pcfvZ0aUwhT0z7klIj9UdGY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568111661; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=OrqP3kZgpM/WiS6DC072haQOCkeF0YFugiFqzSgyKsw=; b=AugXKJ8fhexFwLHD7OKzb3PGwAv42MksgbR2FSfNmYhiDAli/gsxLmrWIEBGjUvDXmJhb2uTUYyZfnulWF0SniGAFq1s5d8jcilpEBjQzoIGRGRRZkqxoG5sNF4ErxeDVn9Ce/mEJXDDxAmaAm1wf43YbTsmfCikuLHLfRi2Z34= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568111661231902.9426028072203; Tue, 10 Sep 2019 03:34:21 -0700 (PDT) Received: from localhost ([::1]:37638 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i7dTX-0002am-27 for importer@patchew.org; Tue, 10 Sep 2019 06:34:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56598) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i7dJK-0000S7-Ha for qemu-devel@nongnu.org; Tue, 10 Sep 2019 06:23:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i7dJI-0001ul-Cc for qemu-devel@nongnu.org; Tue, 10 Sep 2019 06:23:45 -0400 Received: from relay.sw.ru ([185.231.240.75]:60074) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i7dJB-0001oL-CN; Tue, 10 Sep 2019 06:23:37 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92) (envelope-from ) id 1i7dJ8-0007it-2d; Tue, 10 Sep 2019 13:23:34 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Tue, 10 Sep 2019 13:23:30 +0300 Message-Id: <20190910102332.20560-13-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20190910102332.20560-1-vsementsov@virtuozzo.com> References: <20190910102332.20560-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v11 12/14] block: add lock/unlock range functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, vsementsov@virtuozzo.com, wencongyang2@huawei.com, xiechanglong.d@gmail.com, qemu-devel@nongnu.org, armbru@redhat.com, jsnow@redhat.com, stefanha@redhat.com, den@openvz.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Introduce lock/unlock range functionality, based on serialized requests. This is needed to refactor backup, dropping local tracked-request-like synchronization. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/block/block_int.h | 4 ++++ block/io.c | 44 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/include/block/block_int.h b/include/block/block_int.h index 0422acdf1c..b59011ca97 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -65,6 +65,7 @@ enum BdrvTrackedRequestType { BDRV_TRACKED_WRITE, BDRV_TRACKED_DISCARD, BDRV_TRACKED_TRUNCATE, + BDRV_TRACKED_LOCK, }; =20 typedef struct BdrvTrackedRequest { @@ -968,6 +969,9 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child, int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child, int64_t offset, unsigned int bytes, QEMUIOVector *qiov, size_t qiov_offset, BdrvRequestFlags flags); +void *coroutine_fn bdrv_co_try_lock(BlockDriverState *bs, + int64_t offset, unsigned int bytes); +void coroutine_fn bdrv_co_unlock(void *opaque); =20 static inline int coroutine_fn bdrv_co_pread(BdrvChild *child, int64_t offset, unsigned int bytes, void *buf, BdrvRequestFlags flags) diff --git a/block/io.c b/block/io.c index e909736349..b9dd578d56 100644 --- a/block/io.c +++ b/block/io.c @@ -786,6 +786,15 @@ void bdrv_dec_in_flight(BlockDriverState *bs) bdrv_wakeup(bs); } =20 +static bool ignore_intersection(BdrvTrackedRequest *a, BdrvTrackedRequest = *b) +{ + return a =3D=3D b || (!a->serialising && !b->serialising) || + (a->type =3D=3D BDRV_TRACKED_LOCK && b->type =3D=3D BDRV_TRACKED_R= EAD && + !b->serialising) || + (b->type =3D=3D BDRV_TRACKED_LOCK && a->type =3D=3D BDRV_TRACKED_R= EAD && + !a->serialising); +} + static bool coroutine_fn do_wait_serialising_requests(BdrvTrackedRequest *= self, bool wait) { @@ -802,7 +811,7 @@ static bool coroutine_fn do_wait_serialising_requests(B= drvTrackedRequest *self, retry =3D false; qemu_co_mutex_lock(&bs->reqs_lock); QLIST_FOREACH(req, &bs->tracked_requests, list) { - if (req =3D=3D self || (!req->serialising && !self->serialisin= g)) { + if (ignore_intersection(self, req)) { continue; } if (tracked_request_overlaps(req, self->overlap_offset, @@ -840,6 +849,12 @@ static bool coroutine_fn wait_serialising_requests(Bdr= vTrackedRequest *self) return do_wait_serialising_requests(self, true); } =20 +static bool coroutine_fn should_wait_serialising_requests( + BdrvTrackedRequest *self) +{ + return do_wait_serialising_requests(self, false); +} + static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, size_t size) { @@ -3378,3 +3393,30 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, = PreallocMode prealloc, =20 return tco.ret; } + +void *coroutine_fn bdrv_co_try_lock(BlockDriverState *bs, + int64_t offset, unsigned int bytes) +{ + BdrvTrackedRequest *req =3D g_new(BdrvTrackedRequest, 1); + + tracked_request_begin(req, bs, offset, bytes, BDRV_TRACKED_LOCK); + mark_request_serialising(req, bdrv_get_cluster_size(bs)); + + if (should_wait_serialising_requests(req)) { + tracked_request_end(req); + g_free(req); + return NULL; + } + + return req; +} + +void coroutine_fn bdrv_co_unlock(void *opaque) +{ + BdrvTrackedRequest *req =3D opaque; + + assert(req->type =3D=3D BDRV_TRACKED_LOCK); + + tracked_request_end(req); + g_free(req); +} --=20 2.18.0