From nobody Thu Nov 13 21:53:48 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1582828568; cv=none; d=zohomail.com; s=zohoarc; b=CUGJaOt4sqjAeHlvAdMX+CFY/khx+QfYchJwnKZB0a8OReSBHbSwJIS9w8BiSy/GnM2+3fdSNtwRkLh+D0DbU6GbN71n3Ok3bb3qJen9ZSZgj/TA/ufqzNIGqj0hnxpsfKDCvd/a/TIUB/1laNbJOFs8kQs8Ddg3lvQcRjiI0SE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582828568; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HyikUCesDCH37bcDyzRmQu7fuOx8P2zF8fh19cRdJC0=; b=klJYcXlEHIScHjBe6kC4Wj+7eCmLSZe2/BjXDi5RYGb91tOr0wtamdJ+u9UCWtZhCpMAEzuqp7YpPobcGTT/Xq/s1tIlKLW+WyQ7yVNd0cKEa6VyBkLncm5BIpMttR9vioJvfs6vqGjozMqBkSTB9gmu+hP0/zPBkvMQmVKTuq0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582828568572428.51356021891957; Thu, 27 Feb 2020 10:36:08 -0800 (PST) Received: from localhost ([::1]:36486 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7O0x-0004bd-Vp for importer@patchew.org; Thu, 27 Feb 2020 13:36:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36243) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7Nzo-0003DI-8a for qemu-devel@nongnu.org; Thu, 27 Feb 2020 13:34:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7Nzm-0000gi-6Q for qemu-devel@nongnu.org; Thu, 27 Feb 2020 13:34:52 -0500 Received: from fanzine.igalia.com ([178.60.130.6]:33970) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j7Nzl-0000MN-Da; Thu, 27 Feb 2020 13:34:50 -0500 Received: from [192.168.12.213] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1j7NzT-0000H1-AZ; Thu, 27 Feb 2020 19:34:31 +0100 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1j7NzS-0000VV-OT; Thu, 27 Feb 2020 19:34:30 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=HyikUCesDCH37bcDyzRmQu7fuOx8P2zF8fh19cRdJC0=; b=DDpcReMooOXfGNSvKELJAPTnLiECwn3WQmApmSGyoOmuzAqO1Hz2EZn6VEho5kwaV2aKziATkMqEqM9ykpaOLM0iwtxNRLNh/SgSKQUeflT9WMf8nNghzqV8JfT1+6UuvvMVfoCMzjxQXz4duA99pO1mEkBugGZHaomUs9Ipcj4OXEKwuBN0602k7NSIALZJ4E/hPS1a+yQYaX0pdwvKxSwKJHYvs1BwCthnAtjtvsNah5zr6/fz48K1lLEWdR/PQkWuO94uaLFuR+2PKK2iPD4kOd6IV9n4cBNZ+BX/m+Ewdrm57y5t7goQnfCl/qaCLoza2n4VJ2aC57GjIMOW+Q==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH 1/2] qcow2: Make Qcow2AioTask store the full host offset Date: Thu, 27 Feb 2020 19:34:25 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The file_cluster_offset field of Qcow2AioTask stores a cluster-aligned host offset. In practice this is not very useful because all users(*) of this structure need the final host offset into the cluster, which they calculate using host_offset =3D file_cluster_offset + offset_into_cluster(s, offset) There is no reason why Qcow2AioTask cannot store host_offset directly and that is what this patch does. (*) compressed clusters are the exception: in this case what file_cluster_offset was storing was the full compressed cluster descriptor (offset + size). This does not change with this patch but it is documented now. Signed-off-by: Alberto Garcia --- block/qcow2.c | 68 +++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 3c754f616b..b2c7c8255e 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -74,7 +74,7 @@ typedef struct { =20 static int coroutine_fn qcow2_co_preadv_compressed(BlockDriverState *bs, - uint64_t file_cluster_offset, + uint64_t cluster_descriptor, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, @@ -2037,7 +2037,7 @@ out: =20 static coroutine_fn int qcow2_co_preadv_encrypted(BlockDriverState *bs, - uint64_t file_cluster_offset, + uint64_t host_offset, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, @@ -2064,16 +2064,12 @@ qcow2_co_preadv_encrypted(BlockDriverState *bs, } =20 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); - ret =3D bdrv_co_pread(s->data_file, - file_cluster_offset + offset_into_cluster(s, offse= t), - bytes, buf, 0); + ret =3D bdrv_co_pread(s->data_file, host_offset, bytes, buf, 0); if (ret < 0) { goto fail; } =20 - if (qcow2_co_decrypt(bs, - file_cluster_offset + offset_into_cluster(s, offs= et), - offset, buf, bytes) < 0) + if (qcow2_co_decrypt(bs, host_offset, offset, buf, bytes) < 0) { ret =3D -EIO; goto fail; @@ -2091,7 +2087,7 @@ typedef struct Qcow2AioTask { =20 BlockDriverState *bs; QCow2ClusterType cluster_type; /* only for read */ - uint64_t file_cluster_offset; + uint64_t host_offset; /* or full descriptor in compressed clusters */ uint64_t offset; uint64_t bytes; QEMUIOVector *qiov; @@ -2104,7 +2100,7 @@ static coroutine_fn int qcow2_add_task(BlockDriverSta= te *bs, AioTaskPool *pool, AioTaskFunc func, QCow2ClusterType cluster_type, - uint64_t file_cluster_offset, + uint64_t host_offset, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, @@ -2119,7 +2115,7 @@ static coroutine_fn int qcow2_add_task(BlockDriverSta= te *bs, .bs =3D bs, .cluster_type =3D cluster_type, .qiov =3D qiov, - .file_cluster_offset =3D file_cluster_offset, + .host_offset =3D host_offset, .offset =3D offset, .bytes =3D bytes, .qiov_offset =3D qiov_offset, @@ -2128,7 +2124,7 @@ static coroutine_fn int qcow2_add_task(BlockDriverSta= te *bs, =20 trace_qcow2_add_task(qemu_coroutine_self(), bs, pool, func =3D=3D qcow2_co_preadv_task_entry ? "read" := "write", - cluster_type, file_cluster_offset, offset, bytes, + cluster_type, host_offset, offset, bytes, qiov, qiov_offset); =20 if (!pool) { @@ -2142,13 +2138,12 @@ static coroutine_fn int qcow2_add_task(BlockDriverS= tate *bs, =20 static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs, QCow2ClusterType cluster_type, - uint64_t file_cluster_offset, + uint64_t host_offset, uint64_t offset, uint64_t byt= es, QEMUIOVector *qiov, size_t qiov_offset) { BDRVQcow2State *s =3D bs->opaque; - int offset_in_cluster =3D offset_into_cluster(s, offset); =20 switch (cluster_type) { case QCOW2_CLUSTER_ZERO_PLAIN: @@ -2164,19 +2159,17 @@ static coroutine_fn int qcow2_co_preadv_task(BlockD= riverState *bs, qiov, qiov_offset, 0); =20 case QCOW2_CLUSTER_COMPRESSED: - return qcow2_co_preadv_compressed(bs, file_cluster_offset, + return qcow2_co_preadv_compressed(bs, host_offset, offset, bytes, qiov, qiov_offset= ); =20 case QCOW2_CLUSTER_NORMAL: - assert(offset_into_cluster(s, file_cluster_offset) =3D=3D 0); if (bs->encrypted) { - return qcow2_co_preadv_encrypted(bs, file_cluster_offset, + return qcow2_co_preadv_encrypted(bs, host_offset, offset, bytes, qiov, qiov_off= set); } =20 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); - return bdrv_co_preadv_part(s->data_file, - file_cluster_offset + offset_in_cluster, + return bdrv_co_preadv_part(s->data_file, host_offset, bytes, qiov, qiov_offset, 0); =20 default: @@ -2192,7 +2185,7 @@ static coroutine_fn int qcow2_co_preadv_task_entry(Ai= oTask *task) =20 assert(!t->l2meta); =20 - return qcow2_co_preadv_task(t->bs, t->cluster_type, t->file_cluster_of= fset, + return qcow2_co_preadv_task(t->bs, t->cluster_type, t->host_offset, t->offset, t->bytes, t->qiov, t->qiov_offs= et); } =20 @@ -2228,11 +2221,20 @@ static coroutine_fn int qcow2_co_preadv_part(BlockD= riverState *bs, { qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes); } else { + /* + * For compressed clusters the variable cluster_offset + * does not actually store the offset but the full + * descriptor. We need to leave it unchanged because + * that's what qcow2_co_preadv_compressed() expects. + */ + uint64_t host_offset =3D (ret =3D=3D QCOW2_CLUSTER_COMPRESSED)= ? + cluster_offset : + cluster_offset + offset_into_cluster(s, offset); if (!aio && cur_bytes !=3D bytes) { aio =3D aio_task_pool_new(QCOW2_MAX_WORKERS); } ret =3D qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, re= t, - cluster_offset, offset, cur_bytes, + host_offset, offset, cur_bytes, qiov, qiov_offset, NULL); if (ret < 0) { goto out; @@ -2383,7 +2385,7 @@ static int handle_alloc_space(BlockDriverState *bs, Q= CowL2Meta *l2meta) * not use it somehow after qcow2_co_pwritev_task() call */ static coroutine_fn int qcow2_co_pwritev_task(BlockDriverState *bs, - uint64_t file_cluster_offset, + uint64_t host_offset, uint64_t offset, uint64_t by= tes, QEMUIOVector *qiov, uint64_t qiov_offset, @@ -2392,7 +2394,6 @@ static coroutine_fn int qcow2_co_pwritev_task(BlockDr= iverState *bs, int ret; BDRVQcow2State *s =3D bs->opaque; void *crypt_buf =3D NULL; - int offset_in_cluster =3D offset_into_cluster(s, offset); QEMUIOVector encrypted_qiov; =20 if (bs->encrypted) { @@ -2405,8 +2406,7 @@ static coroutine_fn int qcow2_co_pwritev_task(BlockDr= iverState *bs, } qemu_iovec_to_buf(qiov, qiov_offset, crypt_buf, bytes); =20 - if (qcow2_co_encrypt(bs, file_cluster_offset + offset_in_cluster, - offset, crypt_buf, bytes) < 0) + if (qcow2_co_encrypt(bs, host_offset, offset, crypt_buf, bytes) < = 0) { ret =3D -EIO; goto out_unlocked; @@ -2431,10 +2431,8 @@ static coroutine_fn int qcow2_co_pwritev_task(BlockD= riverState *bs, */ if (!merge_cow(offset, bytes, qiov, qiov_offset, l2meta)) { BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); - trace_qcow2_writev_data(qemu_coroutine_self(), - file_cluster_offset + offset_in_cluster); - ret =3D bdrv_co_pwritev_part(s->data_file, - file_cluster_offset + offset_in_cluster, + trace_qcow2_writev_data(qemu_coroutine_self(), host_offset); + ret =3D bdrv_co_pwritev_part(s->data_file, host_offset, bytes, qiov, qiov_offset, 0); if (ret < 0) { goto out_unlocked; @@ -2464,7 +2462,7 @@ static coroutine_fn int qcow2_co_pwritev_task_entry(A= ioTask *task) =20 assert(!t->cluster_type); =20 - return qcow2_co_pwritev_task(t->bs, t->file_cluster_offset, + return qcow2_co_pwritev_task(t->bs, t->host_offset, t->offset, t->bytes, t->qiov, t->qiov_off= set, t->l2meta); } @@ -2519,8 +2517,8 @@ static coroutine_fn int qcow2_co_pwritev_part( aio =3D aio_task_pool_new(QCOW2_MAX_WORKERS); } ret =3D qcow2_add_task(bs, aio, qcow2_co_pwritev_task_entry, 0, - cluster_offset, offset, cur_bytes, - qiov, qiov_offset, l2meta); + cluster_offset + offset_in_cluster, offset, + cur_bytes, qiov, qiov_offset, l2meta); l2meta =3D NULL; /* l2meta is consumed by qcow2_co_pwritev_task() = */ if (ret < 0) { goto fail_nometa; @@ -4353,7 +4351,7 @@ qcow2_co_pwritev_compressed_part(BlockDriverState *bs, =20 static int coroutine_fn qcow2_co_preadv_compressed(BlockDriverState *bs, - uint64_t file_cluster_offset, + uint64_t cluster_descriptor, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, @@ -4365,8 +4363,8 @@ qcow2_co_preadv_compressed(BlockDriverState *bs, uint8_t *buf, *out_buf; int offset_in_cluster =3D offset_into_cluster(s, offset); =20 - coffset =3D file_cluster_offset & s->cluster_offset_mask; - nb_csectors =3D ((file_cluster_offset >> s->csize_shift) & s->csize_ma= sk) + 1; + coffset =3D cluster_descriptor & s->cluster_offset_mask; + nb_csectors =3D ((cluster_descriptor >> s->csize_shift) & s->csize_mas= k) + 1; csize =3D nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE - (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK); =20 --=20 2.20.1