From patchwork Fri Jun 3 17:21:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 9153797 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6D95E6074E for ; Fri, 3 Jun 2016 17:28:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5DB5C26E39 for ; Fri, 3 Jun 2016 17:28:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 528A528304; Fri, 3 Jun 2016 17:28:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id F21E626E39 for ; Fri, 3 Jun 2016 17:28:24 +0000 (UTC) Received: from localhost ([::1]:56886 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8stU-0007k2-37 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 03 Jun 2016 13:28:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8sn0-0002Q4-9P for qemu-devel@nongnu.org; Fri, 03 Jun 2016 13:21:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8smt-0003HD-4H for qemu-devel@nongnu.org; Fri, 03 Jun 2016 13:21:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37029) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8sml-0003BR-Qg; Fri, 03 Jun 2016 13:21:28 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7AE2CC01605A; Fri, 3 Jun 2016 17:21:27 +0000 (UTC) Received: from noname.redhat.com (ovpn-116-70.ams2.redhat.com [10.36.116.70]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u53HLK0I020146; Fri, 3 Jun 2016 13:21:26 -0400 From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 3 Jun 2016 19:21:16 +0200 Message-Id: <1464974478-23598-4-git-send-email-kwolf@redhat.com> In-Reply-To: <1464974478-23598-1-git-send-email-kwolf@redhat.com> References: <1464974478-23598-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 03 Jun 2016 17:21:27 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/5] qcow2: Make copy_sectors() byte based 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, jsnow@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This will allow copy on write operations where the overwritten part of the cluster is not aligned to sector boundaries. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- block/qcow2-cluster.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 9fb7f9f..5d04eb7 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -391,21 +391,17 @@ int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num, } static int coroutine_fn copy_sectors(BlockDriverState *bs, - uint64_t start_sect, + uint64_t src_cluster_offset, uint64_t cluster_offset, - int n_start, int n_end) + int offset_in_cluster, + int bytes) { BDRVQcow2State *s = bs->opaque; QEMUIOVector qiov; struct iovec iov; - int n, ret; - - n = n_end - n_start; - if (n <= 0) { - return 0; - } + int ret; - iov.iov_len = n * BDRV_SECTOR_SIZE; + iov.iov_len = bytes; iov.iov_base = qemu_try_blockalign(bs, iov.iov_len); if (iov.iov_base == NULL) { return -ENOMEM; @@ -424,18 +420,20 @@ static int coroutine_fn copy_sectors(BlockDriverState *bs, * interface. This avoids double I/O throttling and request tracking, * which can lead to deadlock when block layer copy-on-read is enabled. */ - ret = bs->drv->bdrv_co_preadv(bs, (start_sect + n_start) * BDRV_SECTOR_SIZE, - n * BDRV_SECTOR_SIZE, &qiov, 0); + ret = bs->drv->bdrv_co_preadv(bs, src_cluster_offset + offset_in_cluster, + bytes, &qiov, 0); if (ret < 0) { goto out; } if (bs->encrypted) { Error *err = NULL; + int sector = (cluster_offset + offset_in_cluster) >> BDRV_SECTOR_BITS; assert(s->cipher); - if (qcow2_encrypt_sectors(s, start_sect + n_start, - iov.iov_base, iov.iov_base, n, - true, &err) < 0) { + 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, + bytes >> BDRV_SECTOR_BITS, true, &err) < 0) { ret = -EIO; error_free(err); goto out; @@ -443,14 +441,14 @@ static int coroutine_fn copy_sectors(BlockDriverState *bs, } ret = qcow2_pre_write_overlap_check(bs, 0, - cluster_offset + n_start * BDRV_SECTOR_SIZE, n * BDRV_SECTOR_SIZE); + cluster_offset + offset_in_cluster, bytes); if (ret < 0) { goto out; } BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); - ret = bdrv_co_writev(bs->file->bs, (cluster_offset >> 9) + n_start, n, - &qiov); + ret = bdrv_co_pwritev(bs->file->bs, cluster_offset + offset_in_cluster, + bytes, &qiov, 0); if (ret < 0) { goto out; } @@ -743,9 +741,8 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m, Qcow2COWRegion *r) } qemu_co_mutex_unlock(&s->lock); - ret = copy_sectors(bs, m->offset / BDRV_SECTOR_SIZE, m->alloc_offset, - r->offset / BDRV_SECTOR_SIZE, - r->offset / BDRV_SECTOR_SIZE + r->nb_sectors); + ret = copy_sectors(bs, m->offset, m->alloc_offset, + r->offset, r->nb_sectors * BDRV_SECTOR_SIZE); qemu_co_mutex_lock(&s->lock); if (ret < 0) {