From nobody Wed Nov 5 10:26:02 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; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1533664056134880.3291937392764; Tue, 7 Aug 2018 10:47:36 -0700 (PDT) Received: from localhost ([::1]:40236 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fn64s-0002P0-Uh for importer@patchew.org; Tue, 07 Aug 2018 13:47:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42942) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fn60u-0007Tm-7G for qemu-devel@nongnu.org; Tue, 07 Aug 2018 13:43:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fn60r-00021K-3s for qemu-devel@nongnu.org; Tue, 07 Aug 2018 13:43:20 -0400 Received: from relay.sw.ru ([185.231.240.75]:44992) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fn60q-0001xR-QU; Tue, 07 Aug 2018 13:43:17 -0400 Received: from vz-out.virtuozzo.com ([185.231.240.5] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fn60m-0003lt-Hl; Tue, 07 Aug 2018 20:43:12 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 7 Aug 2018 20:43:07 +0300 Message-Id: <20180807174311.32454-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180807174311.32454-1-vsementsov@virtuozzo.com> References: <20180807174311.32454-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 3/7] qcow2: split out reading normal clusters from qcow2_co_preadv 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: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Memory allocation may become less efficient for encrypted case. It's a payment for further asynchronous scheme. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2.c | 114 ++++++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 64 insertions(+), 50 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 65e3ca00e2..5e7f2ee318 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1808,6 +1808,67 @@ out: return ret; } =20 +static coroutine_fn int qcow2_co_preadv_normal(BlockDriverState *bs, + uint64_t file_cluster_offse= t, + uint64_t offset, + uint64_t bytes, + QEMUIOVector *qiov, + uint64_t qiov_offset) +{ + int ret; + BDRVQcow2State *s =3D bs->opaque; + void *crypt_buf =3D NULL; + QEMUIOVector hd_qiov; + int offset_in_cluster =3D offset_into_cluster(s, offset); + + if ((file_cluster_offset & 511) !=3D 0) { + return -EIO; + } + + qemu_iovec_init(&hd_qiov, qiov->niov); + + if (bs->encrypted) { + assert(s->crypto); + assert(bytes <=3D QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); + + crypt_buf =3D qemu_try_blockalign(bs->file->bs, bytes); + qemu_iovec_add(&hd_qiov, crypt_buf, bytes); + } else { + qemu_iovec_concat(&hd_qiov, qiov, qiov_offset, bytes); + } + + BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); + ret =3D bdrv_co_preadv(bs->file, + file_cluster_offset + offset_in_cluster, + bytes, &hd_qiov, 0); + if (ret < 0) { + goto out; + } + + if (bs->encrypted) { + assert(s->crypto); + assert((offset & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); + assert((bytes & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); + if (qcrypto_block_decrypt(s->crypto, + (s->crypt_physical_offset ? + file_cluster_offset + offset_in_cluster= : + offset), + crypt_buf, + bytes, + NULL) < 0) { + ret =3D -EIO; + goto out; + } + qemu_iovec_from_buf(qiov, qiov_offset, crypt_buf, bytes); + } + +out: + qemu_vfree(crypt_buf); + qemu_iovec_destroy(&hd_qiov); + + return ret; +} + static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t off= set, uint64_t bytes, QEMUIOVector *qiov, int flags) @@ -1819,7 +1880,6 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverSt= ate *bs, uint64_t offset, uint64_t cluster_offset =3D 0; uint64_t bytes_done =3D 0; QEMUIOVector hd_qiov; - uint8_t *cluster_data =3D NULL; =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -1882,57 +1942,12 @@ static coroutine_fn int qcow2_co_preadv(BlockDriver= State *bs, uint64_t offset, case QCOW2_CLUSTER_NORMAL: qemu_co_mutex_unlock(&s->lock); =20 - if ((cluster_offset & 511) !=3D 0) { - ret =3D -EIO; - goto fail_nolock; - } - - if (bs->encrypted) { - assert(s->crypto); - - /* - * For encrypted images, read everything into a temporary - * contiguous buffer on which the AES functions can work. - */ - if (!cluster_data) { - cluster_data =3D - qemu_try_blockalign(bs->file->bs, - QCOW_MAX_CRYPT_CLUSTERS - * s->cluster_size); - if (cluster_data =3D=3D NULL) { - ret =3D -ENOMEM; - goto fail_nolock; - } - } - - assert(cur_bytes <=3D QCOW_MAX_CRYPT_CLUSTERS * s->cluster= _size); - qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes); - } - - BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); - ret =3D bdrv_co_preadv(bs->file, - cluster_offset + offset_in_cluster, - cur_bytes, &hd_qiov, 0); + ret =3D qcow2_co_preadv_normal(bs, cluster_offset, + offset, cur_bytes, qiov, bytes_do= ne); if (ret < 0) { goto fail_nolock; } - if (bs->encrypted) { - assert(s->crypto); - assert((offset & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); - assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); - if (qcrypto_block_decrypt(s->crypto, - (s->crypt_physical_offset ? - cluster_offset + offset_in_clus= ter : - offset), - cluster_data, - cur_bytes, - NULL) < 0) { - ret =3D -EIO; - goto fail_nolock; - } - qemu_iovec_from_buf(qiov, bytes_done, cluster_data, cur_by= tes); - } + qemu_co_mutex_lock(&s->lock); break; =20 @@ -1953,7 +1968,6 @@ fail: =20 fail_nolock: qemu_iovec_destroy(&hd_qiov); - qemu_vfree(cluster_data); =20 return ret; } --=20 2.11.1