From nobody Tue Apr 15 09:54:05 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1509024917049903.0479363777807; Thu, 26 Oct 2017 06:35:17 -0700 (PDT) Received: from localhost ([::1]:52944 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7iJT-0005mz-Cq for importer@patchew.org; Thu, 26 Oct 2017 09:35:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59058) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7i3f-0000qJ-30 for qemu-devel@nongnu.org; Thu, 26 Oct 2017 09:18:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e7i3d-0002Lr-Nz for qemu-devel@nongnu.org; Thu, 26 Oct 2017 09:18:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:21325) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e7i3R-0002Bi-T3; Thu, 26 Oct 2017 09:18:38 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 023EBD964A; Thu, 26 Oct 2017 13:18:37 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.118.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id F02A47F7ED; Thu, 26 Oct 2017 13:18:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 023EBD964A Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=kwolf@redhat.com From: Kevin Wolf To: qemu-block@nongnu.org Date: Thu, 26 Oct 2017 15:17:26 +0200 Message-Id: <20171026131741.5059-21-kwolf@redhat.com> In-Reply-To: <20171026131741.5059-1-kwolf@redhat.com> References: <20171026131741.5059-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 26 Oct 2017 13:18:37 +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 20/35] qemu-img: Change compare_sectors() to be 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, qemu-devel@nongnu.org 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" From: Eric Blake In the continuing quest to make more things byte-based, change compare_sectors(), renaming it to compare_buffers() in the process. Note that one caller (qemu-img compare) only cares about the first difference, while the other (qemu-img rebase) cares about how many consecutive sectors have the same equal/different status; however, this patch does not bother to micro-optimize the compare case to avoid the comparisons of sectors beyond the first mismatch. Both callers are always passing valid buffers in, so the initial check for buffer size can be turned into an assertion. Signed-off-by: Eric Blake Reviewed-by: John Snow Signed-off-by: Kevin Wolf --- qemu-img.c | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 66d595be3d..c6b6263853 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1156,31 +1156,28 @@ static int is_allocated_sectors_min(const uint8_t *= buf, int n, int *pnum, } =20 /* - * Compares two buffers sector by sector. Returns 0 if the first sector of= both - * buffers matches, non-zero otherwise. + * Compares two buffers sector by sector. Returns 0 if the first + * sector of each buffer matches, non-zero otherwise. * - * pnum is set to the number of sectors (including and immediately followi= ng - * the first one) that are known to have the same comparison result + * pnum is set to the sector-aligned size of the buffer prefix that + * has the same matching status as the first sector. */ -static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, - int *pnum) +static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, + int64_t bytes, int64_t *pnum) { bool res; - int i; + int64_t i =3D MIN(bytes, BDRV_SECTOR_SIZE); =20 - if (n <=3D 0) { - *pnum =3D 0; - return 0; - } + assert(bytes > 0); =20 - res =3D !!memcmp(buf1, buf2, 512); - for(i =3D 1; i < n; i++) { - buf1 +=3D 512; - buf2 +=3D 512; + res =3D !!memcmp(buf1, buf2, i); + while (i < bytes) { + int64_t len =3D MIN(bytes - i, BDRV_SECTOR_SIZE); =20 - if (!!memcmp(buf1, buf2, 512) !=3D res) { + if (!!memcmp(buf1 + i, buf2 + i, len) !=3D res) { break; } + i +=3D len; } =20 *pnum =3D i; @@ -1255,7 +1252,7 @@ static int img_compare(int argc, char **argv) int64_t total_sectors; int64_t sector_num =3D 0; int64_t nb_sectors; - int c, pnum; + int c; uint64_t progress_base; bool image_opts =3D false; bool force_share =3D false; @@ -1439,6 +1436,8 @@ static int img_compare(int argc, char **argv) /* nothing to do */ } else if (allocated1 =3D=3D allocated2) { if (allocated1) { + int64_t pnum; + nb_sectors =3D MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_= BITS); ret =3D blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, bu= f1, nb_sectors << BDRV_SECTOR_BITS); @@ -1458,11 +1457,11 @@ static int img_compare(int argc, char **argv) ret =3D 4; goto out; } - ret =3D compare_sectors(buf1, buf2, nb_sectors, &pnum); - if (ret || pnum !=3D nb_sectors) { + ret =3D compare_buffers(buf1, buf2, + nb_sectors * BDRV_SECTOR_SIZE, &pnum= ); + if (ret || pnum !=3D nb_sectors * BDRV_SECTOR_SIZE) { qprintf(quiet, "Content mismatch at offset %" PRId64 "= !\n", - sectors_to_bytes( - ret ? sector_num : sector_num + pnum)); + sectors_to_bytes(sector_num) + (ret ? 0 : pnum= )); ret =3D 1; goto out; } @@ -3354,16 +3353,16 @@ static int img_rebase(int argc, char **argv) /* If they differ, we need to write to the COW file */ uint64_t written =3D 0; =20 - while (written < n) { - int pnum; + while (written < n * BDRV_SECTOR_SIZE) { + int64_t pnum; =20 - if (compare_sectors(buf_old + written * 512, - buf_new + written * 512, n - written, &pnum)) + if (compare_buffers(buf_old + written, + buf_new + written, + n * BDRV_SECTOR_SIZE - written, &pnum)) { ret =3D blk_pwrite(blk, - (sector + written) << BDRV_SECTOR_BIT= S, - buf_old + written * 512, - pnum << BDRV_SECTOR_BITS, 0); + (sector << BDRV_SECTOR_BITS) + writte= n, + buf_old + written, pnum, 0); if (ret < 0) { error_report("Error while writing to COW image: %s= ", strerror(-ret)); --=20 2.13.6