From patchwork Mon Jan 16 16:15:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 9519043 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C9893600C5 for ; Mon, 16 Jan 2017 16:16:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B61B22830A for ; Mon, 16 Jan 2017 16:16:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AAEC4284DA; Mon, 16 Jan 2017 16:16:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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 mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 480372830A for ; Mon, 16 Jan 2017 16:16:32 +0000 (UTC) Received: from localhost ([::1]:57986 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cT9xP-0007yj-A3 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 16 Jan 2017 11:16:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49859) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cT9wt-0007wy-3k for qemu-devel@nongnu.org; Mon, 16 Jan 2017 11:16:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cT9ws-0002H0-1X for qemu-devel@nongnu.org; Mon, 16 Jan 2017 11:15:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56326) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cT9wl-0002Ch-L2; Mon, 16 Jan 2017 11:15:51 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D139637EE7; Mon, 16 Jan 2017 16:15:51 +0000 (UTC) Received: from localhost (ovpn-204-161.brq.redhat.com [10.40.204.161]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v0GGFn3C021143 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 16 Jan 2017 11:15:51 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 16 Jan 2017 17:15:47 +0100 Message-Id: <20170116161547.9005-1-mreitz@redhat.com> In-Reply-To: <20170113205237.30386-1-mreitz@redhat.com> References: <20170113205237.30386-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 16 Jan 2017 16:15:51 +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 v6 6/9] qemu-img: Use bdrv_filename() for map 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: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Replaces bs->filename by the result of bdrv_filename() in the qemu-img map subcommand. Since that value is queried relatively often, however, we should try to reuse the last result. Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- qemu-img.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 5df66fe661..6f14792025 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -2469,12 +2469,14 @@ 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) + int nb_sectors, MapEntry *e, + BlockDriverState **current_file) { int64_t ret; int depth; BlockDriverState *file; bool has_offset; + char *filename; /* As an optimization, we could cache the current range of unallocated * clusters in each file of the chain, and avoid querying the same @@ -2503,6 +2505,18 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num, has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); + if (!file || !has_offset) { + g_free(e->filename); + filename = NULL; + } else if (file == *current_file && e->has_filename) { + filename = e->filename; + } else { + g_free(e->filename); + filename = bdrv_filename(file, NULL, 0); + } + + *current_file = file; + *e = (MapEntry) { .start = sector_num * BDRV_SECTOR_SIZE, .length = nb_sectors * BDRV_SECTOR_SIZE, @@ -2511,8 +2525,8 @@ static int get_block_status(BlockDriverState *bs, int64_t sector_num, .offset = ret & BDRV_BLOCK_OFFSET_MASK, .has_offset = has_offset, .depth = depth, - .has_filename = file && has_offset, - .filename = file && has_offset ? file->filename : NULL, + .has_filename = filename, + .filename = filename, }; return 0; @@ -2544,10 +2558,10 @@ static int img_map(int argc, char **argv) int c; OutputFormat output_format = OFORMAT_HUMAN; BlockBackend *blk; - BlockDriverState *bs; + BlockDriverState *bs, *current_file = NULL; const char *filename, *fmt, *output; int64_t length; - MapEntry curr = { .length = 0 }, next; + MapEntry curr = { .length = 0 }, next = { 0 }; int ret = 0; bool image_opts = false; @@ -2633,7 +2647,7 @@ static int img_map(int argc, char **argv) /* 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); + ret = get_block_status(bs, sector_num, n, &next, ¤t_file); if (ret < 0) { error_report("Could not read file metadata: %s", strerror(-ret)); @@ -2648,12 +2662,17 @@ static int img_map(int argc, char **argv) if (curr.length > 0) { dump_map_entry(output_format, &curr, &next); } + + g_free(curr.filename); curr = next; + curr.filename = g_strdup(curr.filename); } dump_map_entry(output_format, &curr, NULL); out: + g_free(curr.filename); + g_free(next.filename); blk_unref(blk); return ret < 0; }