From nobody Tue Nov 4 13:04:20 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 1504177987065334.89495612264375; Thu, 31 Aug 2017 04:13:07 -0700 (PDT) Received: from localhost ([::1]:55068 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dnNPE-0003U4-VF for importer@patchew.org; Thu, 31 Aug 2017 07:13:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50146) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dnNI9-0006mg-PM for qemu-devel@nongnu.org; Thu, 31 Aug 2017 07:05:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dnNI2-0007lF-UP for qemu-devel@nongnu.org; Thu, 31 Aug 2017 07:05:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49462) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dnNHx-0007d5-Pn; Thu, 31 Aug 2017 07:05:33 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CEBFD404312; Thu, 31 Aug 2017 11:05:32 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id E45739ABBC; Thu, 31 Aug 2017 11:05:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CEBFD404312 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Thu, 31 Aug 2017 12:05:17 +0100 Message-Id: <20170831110518.10741-4-berrange@redhat.com> In-Reply-To: <20170831110518.10741-1-berrange@redhat.com> References: <20170831110518.10741-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 31 Aug 2017 11:05:32 +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 v2 3/4] block: convert crypto driver to bdrv_co_preadv|pwritev 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-block@nongnu.org, Stefan Hajnoczi , 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" Make the crypto driver implement the bdrv_co_preadv|pwritev callbacks, and also use bdrv_co_preadv|pwritev for I/O with the protocol driver beneath. Signed-off-by: Daniel P. Berrange Reviewed-by: Eric Blake --- block/crypto.c | 103 +++++++++++++++++++++++++++++------------------------= ---- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index e63f094379..40daa77188 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -380,19 +380,23 @@ static void block_crypto_close(BlockDriverState *bs) =20 =20 #define BLOCK_CRYPTO_MAX_SECTORS 2048 +#define BLOCK_CRYPTO_MAX_LENGTH (BLOCK_CRYPTO_MAX_SECTORS * BDRV_SECTOR_SI= ZE) =20 static coroutine_fn int -block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num, - int remaining_sectors, QEMUIOVector *qiov) +block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t byt= es, + QEMUIOVector *qiov, int flags) { BlockCrypto *crypto =3D bs->opaque; - int cur_nr_sectors; /* number of sectors in current iteration */ + uint64_t cur_bytes; /* number of bytes in current iteration */ uint64_t bytes_done =3D 0; uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; - size_t payload_offset =3D - qcrypto_block_get_payload_offset(crypto->block) / BDRV_SECTOR_SIZE; + size_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->blo= ck); + uint64_t sector_num =3D offset / BDRV_SECTOR_SIZE; + + assert((offset % BDRV_SECTOR_SIZE) =3D=3D 0); + assert((bytes % BDRV_SECTOR_SIZE) =3D=3D 0); =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -400,44 +404,39 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t s= ector_num, * in qiov which points to guest memory. */ cipher_data =3D qemu_try_blockalign( - bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * BDRV_SECTOR_SIZE, - qiov->size)); + bs->file->bs, MIN(BLOCK_CRYPTO_MAX_LENGTH, qiov->size)); if (cipher_data =3D=3D NULL) { ret =3D -ENOMEM; goto cleanup; } =20 - while (remaining_sectors) { - cur_nr_sectors =3D remaining_sectors; + while (bytes) { + cur_bytes =3D bytes; =20 - if (cur_nr_sectors > BLOCK_CRYPTO_MAX_SECTORS) { - cur_nr_sectors =3D BLOCK_CRYPTO_MAX_SECTORS; + if (cur_bytes > BLOCK_CRYPTO_MAX_LENGTH) { + cur_bytes =3D BLOCK_CRYPTO_MAX_LENGTH; } =20 qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * BDRV_SECTOR= _SIZE); + qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); =20 - ret =3D bdrv_co_readv(bs->file, - payload_offset + sector_num, - cur_nr_sectors, &hd_qiov); + ret =3D bdrv_co_preadv(bs->file, payload_offset + offset + bytes_d= one, + cur_bytes, &hd_qiov, 0); if (ret < 0) { goto cleanup; } =20 - if (qcrypto_block_decrypt(crypto->block, - sector_num, - cipher_data, cur_nr_sectors * BDRV_SECTO= R_SIZE, - NULL) < 0) { + if (qcrypto_block_decrypt(crypto->block, sector_num, cipher_data, + cur_bytes, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 - qemu_iovec_from_buf(qiov, bytes_done, - cipher_data, cur_nr_sectors * BDRV_SECTOR_SIZE= ); + qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes); =20 - remaining_sectors -=3D cur_nr_sectors; - sector_num +=3D cur_nr_sectors; - bytes_done +=3D cur_nr_sectors * BDRV_SECTOR_SIZE; + sector_num +=3D cur_bytes / BDRV_SECTOR_SIZE; + bytes -=3D cur_bytes; + bytes_done +=3D cur_bytes; } =20 cleanup: @@ -449,17 +448,20 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t s= ector_num, =20 =20 static coroutine_fn int -block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num, - int remaining_sectors, QEMUIOVector *qiov) +block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t by= tes, + QEMUIOVector *qiov, int flags) { BlockCrypto *crypto =3D bs->opaque; - int cur_nr_sectors; /* number of sectors in current iteration */ + uint64_t cur_bytes; /* number of bytes in current iteration */ uint64_t bytes_done =3D 0; uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; - size_t payload_offset =3D - qcrypto_block_get_payload_offset(crypto->block) / BDRV_SECTOR_SIZE; + size_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->blo= ck); + uint64_t sector_num =3D offset / BDRV_SECTOR_SIZE; + + assert((offset % BDRV_SECTOR_SIZE) =3D=3D 0); + assert((bytes % BDRV_SECTOR_SIZE) =3D=3D 0); =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -467,44 +469,39 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t = sector_num, * contents of qiov - it points to guest memory. */ cipher_data =3D qemu_try_blockalign( - bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * BDRV_SECTOR_SIZE, - qiov->size)); + bs->file->bs, MIN(BLOCK_CRYPTO_MAX_LENGTH, qiov->size)); if (cipher_data =3D=3D NULL) { ret =3D -ENOMEM; goto cleanup; } =20 - while (remaining_sectors) { - cur_nr_sectors =3D remaining_sectors; + while (bytes) { + cur_bytes =3D bytes; =20 - if (cur_nr_sectors > BLOCK_CRYPTO_MAX_SECTORS) { - cur_nr_sectors =3D BLOCK_CRYPTO_MAX_SECTORS; + if (cur_bytes > BLOCK_CRYPTO_MAX_LENGTH) { + cur_bytes =3D BLOCK_CRYPTO_MAX_LENGTH; } =20 - qemu_iovec_to_buf(qiov, bytes_done, - cipher_data, cur_nr_sectors * BDRV_SECTOR_SIZE); + qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes); =20 - if (qcrypto_block_encrypt(crypto->block, - sector_num, - cipher_data, cur_nr_sectors * BDRV_SECTO= R_SIZE, - NULL) < 0) { + if (qcrypto_block_encrypt(crypto->block, sector_num, cipher_data, + cur_bytes, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * BDRV_SECTOR= _SIZE); + qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); =20 - ret =3D bdrv_co_writev(bs->file, - payload_offset + sector_num, - cur_nr_sectors, &hd_qiov); + ret =3D bdrv_co_pwritev(bs->file, payload_offset + offset + bytes_= done, + cur_bytes, &hd_qiov, 0); if (ret < 0) { goto cleanup; } =20 - remaining_sectors -=3D cur_nr_sectors; - sector_num +=3D cur_nr_sectors; - bytes_done +=3D cur_nr_sectors * BDRV_SECTOR_SIZE; + sector_num +=3D cur_bytes / BDRV_SECTOR_SIZE; + bytes -=3D cur_bytes; + bytes_done +=3D cur_bytes; } =20 cleanup: @@ -514,6 +511,11 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t s= ector_num, return ret; } =20 +static void block_crypto_refresh_limits(BlockDriverState *bs, Error **errp) +{ + bs->bl.request_alignment =3D BDRV_SECTOR_SIZE; /* No sub-sector I/O */ +} + =20 static int64_t block_crypto_getlength(BlockDriverState *bs) { @@ -611,8 +613,9 @@ BlockDriver bdrv_crypto_luks =3D { .bdrv_truncate =3D block_crypto_truncate, .create_opts =3D &block_crypto_create_opts_luks, =20 - .bdrv_co_readv =3D block_crypto_co_readv, - .bdrv_co_writev =3D block_crypto_co_writev, + .bdrv_refresh_limits =3D block_crypto_refresh_limits, + .bdrv_co_preadv =3D block_crypto_co_preadv, + .bdrv_co_pwritev =3D block_crypto_co_pwritev, .bdrv_getlength =3D block_crypto_getlength, .bdrv_get_info =3D block_crypto_get_info_luks, .bdrv_get_specific_info =3D block_crypto_get_specific_info_luks, --=20 2.13.5