From patchwork Wed Sep 13 16:03:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 813512 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=208.118.235.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xsmrk5mQ0z9sPk for ; Thu, 14 Sep 2017 02:12:06 +1000 (AEST) Received: from localhost ([::1]:43351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsACu-0005Kh-Tv for incoming@patchwork.ozlabs.org; Wed, 13 Sep 2017 12:08:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsA99-0001Lk-Pb for qemu-devel@nongnu.org; Wed, 13 Sep 2017 12:04:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsA98-00062A-Nw for qemu-devel@nongnu.org; Wed, 13 Sep 2017 12:04:15 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56164) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsA94-0005x0-59; Wed, 13 Sep 2017 12:04: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 3C92CC047B8C; Wed, 13 Sep 2017 16:04:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3C92CC047B8C Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eblake@redhat.com Received: from red.redhat.com (ovpn-120-201.rdu2.redhat.com [10.10.120.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A9365C47C; Wed, 13 Sep 2017 16:04:03 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 11:03:16 -0500 Message-Id: <20170913160333.23622-7-eblake@redhat.com> In-Reply-To: <20170913160333.23622-1-eblake@redhat.com> References: <20170913160333.23622-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.31]); Wed, 13 Sep 2017 16:04: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 06/23] qemu-img: Switch get_block_status() 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, jsnow@redhat.com, famz@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Continue by converting an internal function (no semantic change), and simplifying its caller accordingly. Signed-off-by: Eric Blake Reviewed-by: Fam Zheng Reviewed-by: John Snow --- v2-v3: no change --- qemu-img.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 0c12e1c240..54f7682069 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -2670,14 +2670,16 @@ static void dump_map_entry(OutputFormat output_format, MapEntry *e, } } -static int get_block_status(BlockDriverState *bs, int64_t sector_num, - int nb_sectors, MapEntry *e) +static int get_block_status(BlockDriverState *bs, int64_t offset, + int64_t bytes, MapEntry *e) { int64_t ret; int depth; BlockDriverState *file; bool has_offset; + int nb_sectors = bytes >> BDRV_SECTOR_BITS; + assert(bytes < INT_MAX); /* As an optimization, we could cache the current range of unallocated * clusters in each file of the chain, and avoid querying the same * range repeatedly. @@ -2685,8 +2687,8 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num, depth = 0; for (;;) { - ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors, - &file); + ret = bdrv_get_block_status(bs, offset >> BDRV_SECTOR_BITS, nb_sectors, + &nb_sectors, &file); if (ret < 0) { return ret; } @@ -2706,7 +2708,7 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num, has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); *e = (MapEntry) { - .start = sector_num * BDRV_SECTOR_SIZE, + .start = offset, .length = nb_sectors * BDRV_SECTOR_SIZE, .data = !!(ret & BDRV_BLOCK_DATA), .zero = !!(ret & BDRV_BLOCK_ZERO), @@ -2836,16 +2838,12 @@ static int img_map(int argc, char **argv) length = blk_getlength(blk); while (curr.start + curr.length < length) { - int64_t nsectors_left; - int64_t sector_num; - int n; - - sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; + int64_t offset = curr.start + curr.length; + int64_t n; /* Probe up to 1 GiB at a time. */ - nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; - n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); - ret = get_block_status(bs, sector_num, n, &next); + n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE); + ret = get_block_status(bs, offset, n, &next); if (ret < 0) { error_report("Could not read file metadata: %s", strerror(-ret));