From patchwork Mon Jun 19 13:40:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 777772 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wrsgs6M40z9s7f for ; Mon, 19 Jun 2017 23:46:01 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="LmqekYzP"; dkim-atps=neutral Received: from localhost ([::1]:42745 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dMx0B-0004Et-Df for incoming@patchwork.ozlabs.org; Mon, 19 Jun 2017 09:45:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56872) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dMwuz-0008RQ-Or for qemu-devel@nongnu.org; Mon, 19 Jun 2017 09:40:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dMwuw-0001e6-OM for qemu-devel@nongnu.org; Mon, 19 Jun 2017 09:40:37 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:37103) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dMwuw-0001bc-4s; Mon, 19 Jun 2017 09:40:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=rSLajEuPBLk8R+h8ZfKQXYRC82zuOG88UbTyM1SDXu8=; b=LmqekYzPkI+KLlopqbTaSRWHpKNZnyYWuUCV/eb0mqaKcGYnQzA34ZT6uiGlM+DnEsAE93XL3Vy4XFg2QRLaBW0/q1V0aPyYzfuNY5rt0sosKRcwqfBLle5iTersPWhuHyyRBfxEZLGewj3/PJAxqOIc9l+VJdyfcwx84OpI7oQ//IX6iJsMgdRnUT4B9E57s2X3JjaNupmf6LIU0BDPYsbbs3PAm2tiRFe9/NnJuh/5TBxeQIvW1qHhk94PJBKjqEfOxiyHVqSv8otNwZ83/nqCEJJb4dsTcDFlMZntZVd2VC666zBGZCJbPVSUrnkpDnlx0HbTVD9C1k7gSWFxkw==; Received: from [194.100.51.2] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dMwur-0001EZ-38; Mon, 19 Jun 2017 15:40:29 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dMwuZ-00031j-4c; Mon, 19 Jun 2017 16:40:11 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Mon, 19 Jun 2017 16:40:05 +0300 Message-Id: <74ec5c59c8426ba26a3c2f6d2a40ce2cd6a75296.1497879398.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v4 4/7] qcow2: Split do_perform_cow() into _read(), _encrypt() and _write() 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 , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This patch splits do_perform_cow() into three separate functions to read, encrypt and write the COW regions. perform_cow() can now read both regions first, then encrypt them and finally write them to disk. The memory allocation is also done in this function now, using one single buffer large enough to hold both regions. Signed-off-by: Alberto Garcia Reviewed-by: Kevin Wolf --- block/qcow2-cluster.c | 117 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 30 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 4c03639a72..3c9ace8a96 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -403,34 +403,26 @@ int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num, return 0; } -static int coroutine_fn do_perform_cow(BlockDriverState *bs, - uint64_t src_cluster_offset, - uint64_t cluster_offset, - unsigned offset_in_cluster, - unsigned bytes) +static int coroutine_fn do_perform_cow_read(BlockDriverState *bs, + uint64_t src_cluster_offset, + unsigned offset_in_cluster, + uint8_t *buffer, + unsigned bytes) { - BDRVQcow2State *s = bs->opaque; QEMUIOVector qiov; - struct iovec iov; + struct iovec iov = { .iov_base = buffer, .iov_len = bytes }; int ret; if (bytes == 0) { return 0; } - iov.iov_len = bytes; - iov.iov_base = qemu_try_blockalign(bs, iov.iov_len); - if (iov.iov_base == NULL) { - return -ENOMEM; - } - qemu_iovec_init_external(&qiov, &iov, 1); BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); if (!bs->drv) { - ret = -ENOMEDIUM; - goto out; + return -ENOMEDIUM; } /* Call .bdrv_co_readv() directly instead of using the public block-layer @@ -440,39 +432,63 @@ static int coroutine_fn do_perform_cow(BlockDriverState *bs, ret = bs->drv->bdrv_co_preadv(bs, src_cluster_offset + offset_in_cluster, bytes, &qiov, 0); if (ret < 0) { - goto out; + return ret; } - if (bs->encrypted) { + return 0; +} + +static bool coroutine_fn do_perform_cow_encrypt(BlockDriverState *bs, + uint64_t src_cluster_offset, + unsigned offset_in_cluster, + uint8_t *buffer, + unsigned bytes) +{ + if (bytes && bs->encrypted) { + BDRVQcow2State *s = bs->opaque; int64_t sector = (src_cluster_offset + offset_in_cluster) >> BDRV_SECTOR_BITS; assert(s->cipher); assert((offset_in_cluster & ~BDRV_SECTOR_MASK) == 0); assert((bytes & ~BDRV_SECTOR_MASK) == 0); - if (qcow2_encrypt_sectors(s, sector, iov.iov_base, iov.iov_base, + if (qcow2_encrypt_sectors(s, sector, buffer, buffer, bytes >> BDRV_SECTOR_BITS, true, NULL) < 0) { - ret = -EIO; - goto out; + return false; } } + return true; +} + +static int coroutine_fn do_perform_cow_write(BlockDriverState *bs, + uint64_t cluster_offset, + unsigned offset_in_cluster, + uint8_t *buffer, + unsigned bytes) +{ + QEMUIOVector qiov; + struct iovec iov = { .iov_base = buffer, .iov_len = bytes }; + int ret; + + if (bytes == 0) { + return 0; + } + + qemu_iovec_init_external(&qiov, &iov, 1); ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset + offset_in_cluster, bytes); if (ret < 0) { - goto out; + return ret; } BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); ret = bdrv_co_pwritev(bs->file, cluster_offset + offset_in_cluster, bytes, &qiov, 0); if (ret < 0) { - goto out; + return ret; } - ret = 0; -out: - qemu_vfree(iov.iov_base); - return ret; + return 0; } @@ -760,22 +776,62 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m) BDRVQcow2State *s = bs->opaque; Qcow2COWRegion *start = &m->cow_start; Qcow2COWRegion *end = &m->cow_end; + unsigned buffer_size; + uint8_t *start_buffer, *end_buffer; int ret; + assert(start->nb_bytes <= UINT_MAX - end->nb_bytes); + if (start->nb_bytes == 0 && end->nb_bytes == 0) { return 0; } + /* Reserve a buffer large enough to store the data from both the + * start and end COW regions. Add some padding in the middle if + * necessary to make sure that the end region is optimally aligned */ + buffer_size = QEMU_ALIGN_UP(start->nb_bytes, bdrv_opt_mem_align(bs)) + + end->nb_bytes; + start_buffer = qemu_try_blockalign(bs, buffer_size); + if (start_buffer == NULL) { + return -ENOMEM; + } + /* The part of the buffer where the end region is located */ + end_buffer = start_buffer + buffer_size - end->nb_bytes; + qemu_co_mutex_unlock(&s->lock); - ret = do_perform_cow(bs, m->offset, m->alloc_offset, - start->offset, start->nb_bytes); + /* First we read the existing data from both COW regions */ + ret = do_perform_cow_read(bs, m->offset, start->offset, + start_buffer, start->nb_bytes); if (ret < 0) { goto fail; } - ret = do_perform_cow(bs, m->offset, m->alloc_offset, - end->offset, end->nb_bytes); + ret = do_perform_cow_read(bs, m->offset, end->offset, + end_buffer, end->nb_bytes); + if (ret < 0) { + goto fail; + } + + /* Encrypt the data if necessary before writing it */ + if (bs->encrypted) { + if (!do_perform_cow_encrypt(bs, m->offset, start->offset, + start_buffer, start->nb_bytes) || + !do_perform_cow_encrypt(bs, m->offset, end->offset, + end_buffer, end->nb_bytes)) { + ret = -EIO; + goto fail; + } + } + + /* And now we can write everything */ + ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, + start_buffer, start->nb_bytes); + if (ret < 0) { + goto fail; + } + ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, + end_buffer, end->nb_bytes); fail: qemu_co_mutex_lock(&s->lock); @@ -788,6 +844,7 @@ fail: qcow2_cache_depends_on_flush(s->l2_table_cache); } + qemu_vfree(start_buffer); return ret; }