From nobody Tue Apr 15 09:53: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 1509025096805675.8725123781949; Thu, 26 Oct 2017 06:38:16 -0700 (PDT) Received: from localhost ([::1]:52964 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7iMH-0008BV-Qn for importer@patchew.org; Thu, 26 Oct 2017 09:38:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59066) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7i3f-0000qf-6Y 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-0002MB-Px for qemu-devel@nongnu.org; Thu, 26 Oct 2017 09:18:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58718) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e7i3T-0002D7-Ic; Thu, 26 Oct 2017 09:18:39 -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 B8352DA300; Thu, 26 Oct 2017 13:18:38 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.118.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A25D80368; Thu, 26 Oct 2017 13:18:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B8352DA300 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:27 +0200 Message-Id: <20171026131741.5059-22-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:38 +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 21/35] qemu-img: Change img_rebase() 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 the internal iteration of img_rebase(). We can finally drop the TODO assertion added earlier, now that the entire algorithm is byte-based and no longer has to shift from bytes to sectors. Most of the change is mechanical ('num_sectors' becomes 'size', 'sector' becomes 'offset', 'n' goes from sectors to bytes); some of it is also a cleanup (use of MIN() instead of open-coding, loss of variable 'count' added earlier in commit d6a644bb). Signed-off-by: Eric Blake Reviewed-by: John Snow Signed-off-by: Kevin Wolf --- qemu-img.c | 84 +++++++++++++++++++++++++---------------------------------= ---- 1 file changed, 34 insertions(+), 50 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index c6b6263853..994365c6a4 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3248,70 +3248,58 @@ static int img_rebase(int argc, char **argv) * the image is the same as the original one at any time. */ if (!unsafe) { - int64_t num_sectors; - int64_t old_backing_num_sectors; - int64_t new_backing_num_sectors =3D 0; - uint64_t sector; - int n; - int64_t count; + int64_t size; + int64_t old_backing_size; + int64_t new_backing_size =3D 0; + uint64_t offset; + int64_t n; float local_progress =3D 0; =20 buf_old =3D blk_blockalign(blk, IO_BUF_SIZE); buf_new =3D blk_blockalign(blk, IO_BUF_SIZE); =20 - num_sectors =3D blk_nb_sectors(blk); - if (num_sectors < 0) { + size =3D blk_getlength(blk); + if (size < 0) { error_report("Could not get size of '%s': %s", - filename, strerror(-num_sectors)); + filename, strerror(-size)); ret =3D -1; goto out; } - old_backing_num_sectors =3D blk_nb_sectors(blk_old_backing); - if (old_backing_num_sectors < 0) { + old_backing_size =3D blk_getlength(blk_old_backing); + if (old_backing_size < 0) { char backing_name[PATH_MAX]; =20 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_nam= e)); error_report("Could not get size of '%s': %s", - backing_name, strerror(-old_backing_num_sectors)); + backing_name, strerror(-old_backing_size)); ret =3D -1; goto out; } if (blk_new_backing) { - new_backing_num_sectors =3D blk_nb_sectors(blk_new_backing); - if (new_backing_num_sectors < 0) { + new_backing_size =3D blk_getlength(blk_new_backing); + if (new_backing_size < 0) { error_report("Could not get size of '%s': %s", - out_baseimg, strerror(-new_backing_num_sector= s)); + out_baseimg, strerror(-new_backing_size)); ret =3D -1; goto out; } } =20 - if (num_sectors !=3D 0) { - local_progress =3D (float)100 / - (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); + if (size !=3D 0) { + local_progress =3D (float)100 / (size / MIN(size, IO_BUF_SIZE)= ); } =20 - for (sector =3D 0; sector < num_sectors; sector +=3D n) { - - /* How many sectors can we handle with the next read? */ - if (sector + (IO_BUF_SIZE / 512) <=3D num_sectors) { - n =3D (IO_BUF_SIZE / 512); - } else { - n =3D num_sectors - sector; - } + for (offset =3D 0; offset < size; offset +=3D n) { + /* How many bytes can we handle with the next read? */ + n =3D MIN(IO_BUF_SIZE, size - offset); =20 /* If the cluster is allocated, we don't need to take action */ - ret =3D bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS, - n << BDRV_SECTOR_BITS, &count); + ret =3D bdrv_is_allocated(bs, offset, n, &n); if (ret < 0) { error_report("error while reading image metadata: %s", strerror(-ret)); goto out; } - /* TODO relax this once bdrv_is_allocated does not enforce - * sector alignment */ - assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)); - n =3D count >> BDRV_SECTOR_BITS; if (ret) { continue; } @@ -3320,30 +3308,28 @@ static int img_rebase(int argc, char **argv) * Read old and new backing file and take into consideration t= hat * backing files may be smaller than the COW image. */ - if (sector >=3D old_backing_num_sectors) { - memset(buf_old, 0, n * BDRV_SECTOR_SIZE); + if (offset >=3D old_backing_size) { + memset(buf_old, 0, n); } else { - if (sector + n > old_backing_num_sectors) { - n =3D old_backing_num_sectors - sector; + if (offset + n > old_backing_size) { + n =3D old_backing_size - offset; } =20 - ret =3D blk_pread(blk_old_backing, sector << BDRV_SECTOR_B= ITS, - buf_old, n << BDRV_SECTOR_BITS); + ret =3D blk_pread(blk_old_backing, offset, buf_old, n); if (ret < 0) { error_report("error while reading from old backing fil= e"); goto out; } } =20 - if (sector >=3D new_backing_num_sectors || !blk_new_backing) { - memset(buf_new, 0, n * BDRV_SECTOR_SIZE); + if (offset >=3D new_backing_size || !blk_new_backing) { + memset(buf_new, 0, n); } else { - if (sector + n > new_backing_num_sectors) { - n =3D new_backing_num_sectors - sector; + if (offset + n > new_backing_size) { + n =3D new_backing_size - offset; } =20 - ret =3D blk_pread(blk_new_backing, sector << BDRV_SECTOR_B= ITS, - buf_new, n << BDRV_SECTOR_BITS); + ret =3D blk_pread(blk_new_backing, offset, buf_new, n); if (ret < 0) { error_report("error while reading from new backing fil= e"); goto out; @@ -3353,15 +3339,13 @@ 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 * BDRV_SECTOR_SIZE) { + while (written < n) { int64_t pnum; =20 - if (compare_buffers(buf_old + written, - buf_new + written, - n * BDRV_SECTOR_SIZE - written, &pnum)) + if (compare_buffers(buf_old + written, buf_new + written, + n - written, &pnum)) { - ret =3D blk_pwrite(blk, - (sector << BDRV_SECTOR_BITS) + writte= n, + ret =3D blk_pwrite(blk, offset + written, buf_old + written, pnum, 0); if (ret < 0) { error_report("Error while writing to COW image: %s= ", --=20 2.13.6