From patchwork Thu Oct 26 13:17:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 830672 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yN7Mr31Dwz9t6D for ; Fri, 27 Oct 2017 00:36:56 +1100 (AEDT) Received: from localhost ([::1]:52959 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7iL8-00073K-Ek for incoming@patchwork.ozlabs.org; Thu, 26 Oct 2017 09:36:54 -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) 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 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) 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 = 0; - uint64_t sector; - int n; - int64_t count; + int64_t size; + int64_t old_backing_size; + int64_t new_backing_size = 0; + uint64_t offset; + int64_t n; float local_progress = 0; buf_old = blk_blockalign(blk, IO_BUF_SIZE); buf_new = blk_blockalign(blk, IO_BUF_SIZE); - num_sectors = blk_nb_sectors(blk); - if (num_sectors < 0) { + size = blk_getlength(blk); + if (size < 0) { error_report("Could not get size of '%s': %s", - filename, strerror(-num_sectors)); + filename, strerror(-size)); ret = -1; goto out; } - old_backing_num_sectors = blk_nb_sectors(blk_old_backing); - if (old_backing_num_sectors < 0) { + old_backing_size = blk_getlength(blk_old_backing); + if (old_backing_size < 0) { char backing_name[PATH_MAX]; bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); error_report("Could not get size of '%s': %s", - backing_name, strerror(-old_backing_num_sectors)); + backing_name, strerror(-old_backing_size)); ret = -1; goto out; } if (blk_new_backing) { - new_backing_num_sectors = blk_nb_sectors(blk_new_backing); - if (new_backing_num_sectors < 0) { + new_backing_size = 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_sectors)); + out_baseimg, strerror(-new_backing_size)); ret = -1; goto out; } } - if (num_sectors != 0) { - local_progress = (float)100 / - (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); + if (size != 0) { + local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); } - for (sector = 0; sector < num_sectors; sector += n) { - - /* How many sectors can we handle with the next read? */ - if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { - n = (IO_BUF_SIZE / 512); - } else { - n = num_sectors - sector; - } + for (offset = 0; offset < size; offset += n) { + /* How many bytes can we handle with the next read? */ + n = MIN(IO_BUF_SIZE, size - offset); /* If the cluster is allocated, we don't need to take action */ - ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS, - n << BDRV_SECTOR_BITS, &count); + ret = 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 = 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 that * backing files may be smaller than the COW image. */ - if (sector >= old_backing_num_sectors) { - memset(buf_old, 0, n * BDRV_SECTOR_SIZE); + if (offset >= old_backing_size) { + memset(buf_old, 0, n); } else { - if (sector + n > old_backing_num_sectors) { - n = old_backing_num_sectors - sector; + if (offset + n > old_backing_size) { + n = old_backing_size - offset; } - ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS, - buf_old, n << BDRV_SECTOR_BITS); + ret = blk_pread(blk_old_backing, offset, buf_old, n); if (ret < 0) { error_report("error while reading from old backing file"); goto out; } } - if (sector >= new_backing_num_sectors || !blk_new_backing) { - memset(buf_new, 0, n * BDRV_SECTOR_SIZE); + if (offset >= new_backing_size || !blk_new_backing) { + memset(buf_new, 0, n); } else { - if (sector + n > new_backing_num_sectors) { - n = new_backing_num_sectors - sector; + if (offset + n > new_backing_size) { + n = new_backing_size - offset; } - ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS, - buf_new, n << BDRV_SECTOR_BITS); + ret = blk_pread(blk_new_backing, offset, buf_new, n); if (ret < 0) { error_report("error while reading from new backing file"); 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 = 0; - while (written < n * BDRV_SECTOR_SIZE) { + while (written < n) { int64_t pnum; - 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 = blk_pwrite(blk, - (sector << BDRV_SECTOR_BITS) + written, + ret = blk_pwrite(blk, offset + written, buf_old + written, pnum, 0); if (ret < 0) { error_report("Error while writing to COW image: %s",