From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
Kevin Wolf <kwolf@redhat.com>, Eric Blake <eblake@redhat.com>
Subject: [Qemu-devel] [PATCH v6 6/9] qemu-img: Use bdrv_filename() for map
Date: Mon, 16 Jan 2017 17:15:47 +0100 [thread overview]
Message-ID: <20170116161547.9005-1-mreitz@redhat.com> (raw)
In-Reply-To: <20170113205237.30386-1-mreitz@redhat.com>
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 <mreitz@redhat.com>
---
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;
}
--
2.11.0
next prev parent reply other threads:[~2017-01-16 16:16 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-13 20:52 [Qemu-devel] [PATCH v6 0/9] block: Drop BDS.filename Max Reitz
2017-01-13 20:52 ` [Qemu-devel] [PATCH v6 1/9] block: Always set *file in get_block_status Max Reitz
2017-01-16 20:44 ` Eric Blake
2017-01-13 20:52 ` [Qemu-devel] [PATCH v6 2/9] block: Change bdrv_get_encrypted_filename() Max Reitz
2017-01-13 20:52 ` [Qemu-devel] [PATCH v6 3/9] block: Avoid BlockDriverState.filename Max Reitz
2017-01-16 20:46 ` Eric Blake
2017-01-13 20:52 ` [Qemu-devel] [PATCH v6 4/9] block: Do not blindly copy filename from file Max Reitz
2017-01-16 20:48 ` Eric Blake
2017-01-13 20:52 ` [Qemu-devel] [PATCH v6 5/9] block: Add bdrv_filename() Max Reitz
2017-01-16 21:33 ` Eric Blake
2017-01-16 16:13 ` [Qemu-devel] [PATCH v6 7/9] block: Drop BlockDriverState.filename Max Reitz
2017-01-16 16:13 ` [Qemu-devel] [PATCH v6 8/9] block: Complete move to pull filename updates Max Reitz
2017-01-16 16:13 ` [Qemu-devel] [PATCH v6 9/9] iotests: Test changed Quorum filename Max Reitz
2017-01-16 16:15 ` Max Reitz [this message]
2017-01-16 21:45 ` [Qemu-devel] [PATCH v6 6/9] qemu-img: Use bdrv_filename() for map Eric Blake
2017-01-16 16:17 ` [Qemu-devel] [PATCH v6 0/9] block: Drop BDS.filename Max Reitz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170116161547.9005-1-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).