From nobody Sun Oct 5 20:54:08 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 1499289693679233.73409004235714; Wed, 5 Jul 2017 14:21:33 -0700 (PDT) Received: from localhost ([::1]:48122 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrjn-0007Zp-Au for importer@patchew.org; Wed, 05 Jul 2017 17:21:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37980) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXu-0004yz-W3 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXt-0002iq-SZ for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33332) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXq-0002gQ-1B; Wed, 05 Jul 2017 17:09:10 -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 180C7C0587D8; Wed, 5 Jul 2017 21:09:09 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id D0326600CC; Wed, 5 Jul 2017 21:09:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 180C7C0587D8 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 180C7C0587D8 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:32 -0500 Message-Id: <20170705210842.960-12-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-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.32]); Wed, 05 Jul 2017 21:09:09 +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 v4 11/21] 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, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, 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" 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 Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v3-v4: no change v2: tweak mirror_clip_bytes() signature to match previous patch --- block/mirror.c | 64 ++++++++++++++++++++++++++++++++++--------------------= ---- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 70682b6..374cefd 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 int64_t mirror_clip_bytes(MirrorBlockJob *s, + int64_t offset, + int64_t bytes) +{ + return MIN(bytes, s->bdev_length - offset); +} + +/* Clip nb_sectors relative to sector_num to not exceed end-of-file */ static inline int mirror_clip_sectors(MirrorBlockJob *s, int64_t sector_num, int nb_sectors) @@ -184,44 +193,39 @@ static inline int 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 =3D 0; - int chunk_sectors =3D s->granularity >> BDRV_SECTOR_BITS; - int64_t align_sector_num =3D *sector_num; - int align_nb_sectors =3D *nb_sectors; - int max_sectors =3D chunk_sectors * s->max_iov; + int64_t align_offset =3D *offset; + unsigned int align_bytes =3D *bytes; + int max_bytes =3D s->granularity * s->max_iov; - need_cow =3D !test_bit(*sector_num / chunk_sectors, s->cow_bitmap); - need_cow |=3D !test_bit((*sector_num + *nb_sectors - 1) / chunk_sector= s, + need_cow =3D !test_bit(*offset / s->granularity, s->cow_bitmap); + need_cow |=3D !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 =3D max_sectors; + if (align_bytes > max_bytes) { + align_bytes =3D max_bytes; if (need_cow) { - align_nb_sectors =3D QEMU_ALIGN_DOWN(align_nb_sectors, - s->target_cluster_size >> - BDRV_SECTOR_BITS); + align_bytes =3D 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. */ - align_nb_sectors =3D mirror_clip_sectors(s, align_sector_num, - align_nb_sectors); + align_bytes =3D mirror_clip_bytes(s, align_offset, align_bytes); - ret =3D align_sector_num + align_nb_sectors - (*sector_num + *nb_secto= rs); - *sector_num =3D align_sector_num; - *nb_sectors =3D align_nb_sectors; + ret =3D align_offset + align_bytes - (*offset + *bytes); + *offset =3D align_offset; + *bytes =3D align_bytes; assert(ret >=3D 0); return ret; } @@ -257,10 +261,18 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t = sector_num, nb_sectors =3D MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors); nb_sectors =3D MIN(max_sectors, nb_sectors); assert(nb_sectors); + assert(nb_sectors < BDRV_REQUEST_MAX_SECTORS); ret =3D nb_sectors; if (s->cow_bitmap) { - ret +=3D mirror_cow_align(s, §or_num, &nb_sectors); + int64_t offset =3D sector_num * BDRV_SECTOR_SIZE; + unsigned int bytes =3D nb_sectors * BDRV_SECTOR_SIZE; + int gap; + + gap =3D mirror_cow_align(s, &offset, &bytes); + sector_num =3D offset / BDRV_SECTOR_SIZE; + nb_sectors =3D bytes / BDRV_SECTOR_SIZE; + ret +=3D gap / BDRV_SECTOR_SIZE; } assert(nb_sectors << BDRV_SECTOR_BITS <=3D s->buf_size); /* The sector range must meet granularity because: --=20 2.9.4