From patchwork Tue Apr 11 22:29:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9676151 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 82983600CB for ; Tue, 11 Apr 2017 22:36:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DCB728531 for ; Tue, 11 Apr 2017 22:36:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F47728589; Tue, 11 Apr 2017 22:36:04 +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 A8C7F28531 for ; Tue, 11 Apr 2017 22:36:03 +0000 (UTC) Received: from localhost ([::1]:41350 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cy4OI-0001QR-OX for patchwork-qemu-devel@patchwork.kernel.org; Tue, 11 Apr 2017 18:36:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40272) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cy4Ic-0006Fe-Bi for qemu-devel@nongnu.org; Tue, 11 Apr 2017 18:30:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cy4Ib-0004jK-C9 for qemu-devel@nongnu.org; Tue, 11 Apr 2017 18:30:10 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46992) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cy4IU-0004cg-IG; Tue, 11 Apr 2017 18:30:02 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 874C881252; Tue, 11 Apr 2017 22:30:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 874C881252 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 874C881252 Received: from red.redhat.com (ovpn-122-61.rdu2.redhat.com [10.10.122.61]) by smtp.corp.redhat.com (Postfix) with ESMTP id B039818EEA; Tue, 11 Apr 2017 22:30:00 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 11 Apr 2017 17:29:37 -0500 Message-Id: <20170411222945.11741-10-eblake@redhat.com> In-Reply-To: <20170411222945.11741-1-eblake@redhat.com> References: <20170411222945.11741-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 11 Apr 2017 22:30:01 +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 09/17] mirror: Switch mirror_cow_align() to 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, Jeff Cody , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Convert another internal function (no semantic change), and add mirror_clip_bytes() as a counterpart to mirror_clip_sectors(). Some of the conversion is a bit tricky, requiring temporaries to convert between units; it will be cleared up in a following patch. Signed-off-by: Eric Blake --- block/mirror.c | 63 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 9a6f474..84b8cca 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -176,6 +176,15 @@ static void mirror_read_complete(void *opaque, int ret) aio_context_release(blk_get_aio_context(s->common.blk)); } +/* Clip bytes relative to offset to not exceed end-of-file */ +static inline void mirror_clip_bytes(MirrorBlockJob *s, + int64_t offset, + unsigned int *bytes) +{ + *bytes = MIN(*bytes, s->bdev_length - offset); +} + +/* Clip nb_sectors relative to sector_num to not exceed end-of-file */ static inline void mirror_clip_sectors(MirrorBlockJob *s, int64_t sector_num, int *nb_sectors) @@ -184,43 +193,39 @@ static inline void mirror_clip_sectors(MirrorBlockJob *s, s->bdev_length / BDRV_SECTOR_SIZE - sector_num); } -/* Round sector_num and/or nb_sectors to target cluster if COW is needed, and - * return the offset of the adjusted tail sector against original. */ -static int mirror_cow_align(MirrorBlockJob *s, - int64_t *sector_num, - int *nb_sectors) +/* Round offset and/or bytes to target cluster if COW is needed, and + * return the offset of the adjusted tail against original. */ +static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, + unsigned int *bytes) { bool need_cow; int ret = 0; - int chunk_sectors = s->granularity >> BDRV_SECTOR_BITS; - int64_t align_sector_num = *sector_num; - int align_nb_sectors = *nb_sectors; - int max_sectors = chunk_sectors * s->max_iov; + int64_t align_offset = *offset; + unsigned int align_bytes = *bytes; + int max_bytes = s->granularity * s->max_iov; - need_cow = !test_bit(*sector_num / chunk_sectors, s->cow_bitmap); - need_cow |= !test_bit((*sector_num + *nb_sectors - 1) / chunk_sectors, + need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); + need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, s->cow_bitmap); if (need_cow) { - bdrv_round_sectors_to_clusters(blk_bs(s->target), *sector_num, - *nb_sectors, &align_sector_num, - &align_nb_sectors); + bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, + &align_offset, &align_bytes); } - if (align_nb_sectors > max_sectors) { - align_nb_sectors = max_sectors; + if (align_bytes > max_bytes) { + align_bytes = max_bytes; if (need_cow) { - align_nb_sectors = QEMU_ALIGN_DOWN(align_nb_sectors, - s->target_cluster_size >> - BDRV_SECTOR_BITS); + align_bytes = QEMU_ALIGN_DOWN(align_bytes, + s->target_cluster_size); } } - /* Clipping may result in align_nb_sectors unaligned to chunk boundary, but + /* Clipping may result in align_bytes unaligned to chunk boundary, but * that doesn't matter because it's already the end of source image. */ - mirror_clip_sectors(s, align_sector_num, &align_nb_sectors); + mirror_clip_bytes(s, align_offset, &align_bytes); - ret = align_sector_num + align_nb_sectors - (*sector_num + *nb_sectors); - *sector_num = align_sector_num; - *nb_sectors = align_nb_sectors; + ret = align_offset + align_bytes - (*offset + *bytes); + *offset = align_offset; + *bytes = align_bytes; assert(ret >= 0); return ret; } @@ -256,10 +261,18 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num, nb_sectors = MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors); nb_sectors = MIN(max_sectors, nb_sectors); assert(nb_sectors); + assert(nb_sectors < BDRV_REQUEST_MAX_SECTORS); ret = nb_sectors; if (s->cow_bitmap) { - ret += mirror_cow_align(s, §or_num, &nb_sectors); + int64_t offset = sector_num * BDRV_SECTOR_SIZE; + unsigned int bytes = nb_sectors * BDRV_SECTOR_SIZE; + int gap; + + gap = mirror_cow_align(s, &offset, &bytes); + sector_num = offset / BDRV_SECTOR_SIZE; + nb_sectors = bytes / BDRV_SECTOR_SIZE; + ret += gap / BDRV_SECTOR_SIZE; } assert(nb_sectors << BDRV_SECTOR_BITS <= s->buf_size); /* The sector range must meet granularity because: