From nobody Tue Apr 22 23:54:46 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: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by
 mx.zohomail.com
	with SMTPS id 1499448954902169.92973409188187;
 Fri, 7 Jul 2017 10:35:54 -0700 (PDT)
Received: from localhost ([::1]:57838 helo=lists.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <qemu-devel-bounces+importer=patchew.org@nongnu.org>)
	id 1dTXAX-0003N4-G7
	for importer@patchew.org; Fri, 07 Jul 2017 13:35:53 -0400
Received: from eggs.gnu.org ([2001:4830:134:3::10]:41875)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <kwolf@redhat.com>) id 1dTWlL-0005AC-1F
	for qemu-devel@nongnu.org; Fri, 07 Jul 2017 13:09:52 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <kwolf@redhat.com>) id 1dTWlJ-000854-L9
	for qemu-devel@nongnu.org; Fri, 07 Jul 2017 13:09:51 -0400
Received: from mx1.redhat.com ([209.132.183.28]:52814)
	by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)
	(Exim 4.71) (envelope-from <kwolf@redhat.com>)
	id 1dTWlG-0007zZ-Fg; Fri, 07 Jul 2017 13:09:46 -0400
Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com
	[10.5.11.15])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.redhat.com (Postfix) with ESMTPS id 8135D8123F;
	Fri,  7 Jul 2017 17:09:45 +0000 (UTC)
Received: from noname.redhat.com (ovpn-117-34.ams2.redhat.com [10.36.117.34])
	by smtp.corp.redhat.com (Postfix) with ESMTP id 9AD3161F23;
	Fri,  7 Jul 2017 17:09:44 +0000 (UTC)
DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8135D8123F
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=kwolf@redhat.com
DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8135D8123F
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Date: Fri,  7 Jul 2017 19:07:45 +0200
Message-Id: <1499447335-6125-31-git-send-email-kwolf@redhat.com>
In-Reply-To: <1499447335-6125-1-git-send-email-kwolf@redhat.com>
References: <1499447335-6125-1-git-send-email-kwolf@redhat.com>
X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15
X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16
	(mx1.redhat.com [10.5.110.25]);
	Fri, 07 Jul 2017 17:09:45 +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] [PULL 030/100] mirror: Switch mirror_cow_align() to
 byte-based
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.21
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
	<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <http://lists.nongnu.org/archive/html/qemu-devel/>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
	<mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: "Qemu-devel" <qemu-devel-bounces+importer=patchew.org@nongnu.org>
X-ZohoMail: RSF_0  Z_629925259 SPT_0
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"

From: Eric Blake <eblake@redhat.com>

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 <eblake@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/mirror.c | 63 ++++++++++++++++++++++++++++++++++--------------------=
----
 1 file changed, 37 insertions(+), 26 deletions(-)

diff --git a/block/mirror.c b/block/mirror.c
index a41ef25..0378bd2 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));
 }
=20
+/* 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,38 @@ static inline int mirror_clip_sectors(MirrorBlockJob =
*s,
                s->bdev_length / BDRV_SECTOR_SIZE - sector_num);
 }
=20
-/* 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;
=20
-    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);
     }
=20
-    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);
=20
-    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 +260,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;
=20
     if (s->cow_bitmap) {
-        ret +=3D mirror_cow_align(s, &sector_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
1.8.3.1