218e99
From 43c680e82e94061bf1b522466054730d8f3d8c22 Mon Sep 17 00:00:00 2001
218e99
From: Paolo Bonzini <pbonzini@redhat.com>
218e99
Date: Fri, 18 Oct 2013 08:14:42 +0200
218e99
Subject: [PATCH 17/81] qemu-img: add a "map" subcommand
218e99
218e99
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
218e99
Message-id: <1382084091-16636-18-git-send-email-pbonzini@redhat.com>
218e99
Patchwork-id: 55000
218e99
O-Subject: [RHEL 7.0 qemu-kvm PATCH 17/26] qemu-img: add a "map" subcommand
218e99
Bugzilla: 989646
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
RH-Acked-by: Max Reitz <mreitz@redhat.com>
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
218e99
This command dumps the metadata of an entire chain, in either tabular or JSON
218e99
format.
218e99
218e99
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
(cherry picked from commit 4c93a13b5daf9bd5fca1a547661b0fb9a2f0ca52)
218e99
---
218e99
 qemu-img-cmds.hx |   6 ++
218e99
 qemu-img.c       | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
218e99
 2 files changed, 198 insertions(+)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 qemu-img-cmds.hx |    6 ++
218e99
 qemu-img.c       |  192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
218e99
 2 files changed, 198 insertions(+), 0 deletions(-)
218e99
218e99
diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
218e99
index 4ca7e95..c97a1f4 100644
218e99
--- a/qemu-img-cmds.hx
218e99
+++ b/qemu-img-cmds.hx
218e99
@@ -45,6 +45,12 @@ STEXI
218e99
 @item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename}
218e99
 ETEXI
218e99
 
218e99
+DEF("map", img_map,
218e99
+    "map [-f fmt] [--output=ofmt] filename")
218e99
+STEXI
218e99
+@item map [-f @var{fmt}] [--output=@var{ofmt}] @var{filename}
218e99
+ETEXI
218e99
+
218e99
 DEF("snapshot", img_snapshot,
218e99
     "snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename")
218e99
 STEXI
218e99
diff --git a/qemu-img.c b/qemu-img.c
218e99
index a9aabd5..e58c052 100644
218e99
--- a/qemu-img.c
218e99
+++ b/qemu-img.c
218e99
@@ -1919,6 +1919,198 @@ static int img_info(int argc, char **argv)
218e99
     return 0;
218e99
 }
218e99
 
218e99
+
218e99
+typedef struct MapEntry {
218e99
+    int flags;
218e99
+    int depth;
218e99
+    int64_t start;
218e99
+    int64_t length;
218e99
+    int64_t offset;
218e99
+    BlockDriverState *bs;
218e99
+} MapEntry;
218e99
+
218e99
+static void dump_map_entry(OutputFormat output_format, MapEntry *e,
218e99
+                           MapEntry *next)
218e99
+{
218e99
+    switch (output_format) {
218e99
+    case OFORMAT_HUMAN:
218e99
+        if ((e->flags & BDRV_BLOCK_DATA) &&
218e99
+            !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
218e99
+            error_report("File contains external, encrypted or compressed clusters.");
218e99
+            exit(1);
218e99
+        }
218e99
+        if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
218e99
+            printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
218e99
+                   e->start, e->length, e->offset, e->bs->filename);
218e99
+        }
218e99
+        /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
218e99
+         * Modify the flags here to allow more coalescing.
218e99
+         */
218e99
+        if (next &&
218e99
+            (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
218e99
+            next->flags &= ~BDRV_BLOCK_DATA;
218e99
+            next->flags |= BDRV_BLOCK_ZERO;
218e99
+        }
218e99
+        break;
218e99
+    case OFORMAT_JSON:
218e99
+        printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
218e99
+               " \"zero\": %s, \"data\": %s",
218e99
+               (e->start == 0 ? "[" : ",\n"),
218e99
+               e->start, e->length, e->depth,
218e99
+               (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
218e99
+               (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
218e99
+        if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
218e99
+            printf(", 'offset': %"PRId64"", e->offset);
218e99
+        }
218e99
+        putchar('}');
218e99
+
218e99
+        if (!next) {
218e99
+            printf("]\n");
218e99
+        }
218e99
+        break;
218e99
+    }
218e99
+}
218e99
+
218e99
+static int get_block_status(BlockDriverState *bs, int64_t sector_num,
218e99
+                            int nb_sectors, MapEntry *e)
218e99
+{
218e99
+    int64_t ret;
218e99
+    int depth;
218e99
+
218e99
+    /* As an optimization, we could cache the current range of unallocated
218e99
+     * clusters in each file of the chain, and avoid querying the same
218e99
+     * range repeatedly.
218e99
+     */
218e99
+
218e99
+    depth = 0;
218e99
+    for (;;) {
218e99
+        ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
218e99
+        if (ret < 0) {
218e99
+            return ret;
218e99
+        }
218e99
+        assert(nb_sectors);
218e99
+        if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
218e99
+            break;
218e99
+        }
218e99
+        bs = bs->backing_hd;
218e99
+        if (bs == NULL) {
218e99
+            ret = 0;
218e99
+            break;
218e99
+        }
218e99
+
218e99
+        depth++;
218e99
+    }
218e99
+
218e99
+    e->start = sector_num * BDRV_SECTOR_SIZE;
218e99
+    e->length = nb_sectors * BDRV_SECTOR_SIZE;
218e99
+    e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
218e99
+    e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
218e99
+    e->depth = depth;
218e99
+    e->bs = bs;
218e99
+    return 0;
218e99
+}
218e99
+
218e99
+static int img_map(int argc, char **argv)
218e99
+{
218e99
+    int c;
218e99
+    OutputFormat output_format = OFORMAT_HUMAN;
218e99
+    BlockDriverState *bs;
218e99
+    const char *filename, *fmt, *output;
218e99
+    int64_t length;
218e99
+    MapEntry curr = { .length = 0 }, next;
218e99
+    int ret = 0;
218e99
+
218e99
+    fmt = NULL;
218e99
+    output = NULL;
218e99
+    for (;;) {
218e99
+        int option_index = 0;
218e99
+        static const struct option long_options[] = {
218e99
+            {"help", no_argument, 0, 'h'},
218e99
+            {"format", required_argument, 0, 'f'},
218e99
+            {"output", required_argument, 0, OPTION_OUTPUT},
218e99
+            {0, 0, 0, 0}
218e99
+        };
218e99
+        c = getopt_long(argc, argv, "f:h",
218e99
+                        long_options, &option_index);
218e99
+        if (c == -1) {
218e99
+            break;
218e99
+        }
218e99
+        switch (c) {
218e99
+        case '?':
218e99
+        case 'h':
218e99
+            help();
218e99
+            break;
218e99
+        case 'f':
218e99
+            fmt = optarg;
218e99
+            break;
218e99
+        case OPTION_OUTPUT:
218e99
+            output = optarg;
218e99
+            break;
218e99
+        }
218e99
+    }
218e99
+    if (optind >= argc) {
218e99
+        help();
218e99
+    }
218e99
+    filename = argv[optind++];
218e99
+
218e99
+    if (output && !strcmp(output, "json")) {
218e99
+        output_format = OFORMAT_JSON;
218e99
+    } else if (output && !strcmp(output, "human")) {
218e99
+        output_format = OFORMAT_HUMAN;
218e99
+    } else if (output) {
218e99
+        error_report("--output must be used with human or json as argument.");
218e99
+        return 1;
218e99
+    }
218e99
+
218e99
+    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false);
218e99
+    if (!bs) {
218e99
+        return 1;
218e99
+    }
218e99
+
218e99
+    if (output_format == OFORMAT_HUMAN) {
218e99
+        printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
218e99
+    }
218e99
+
218e99
+    length = bdrv_getlength(bs);
218e99
+    while (curr.start + curr.length < length) {
218e99
+        int64_t nsectors_left;
218e99
+        int64_t sector_num;
218e99
+        int n;
218e99
+
218e99
+        sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
218e99
+
218e99
+        /* Probe up to 1 GiB at a time.  */
218e99
+        nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
218e99
+        n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
218e99
+        ret = get_block_status(bs, sector_num, n, &next;;
218e99
+
218e99
+        if (ret < 0) {
218e99
+            error_report("Could not read file metadata: %s", strerror(-ret));
218e99
+            goto out;
218e99
+        }
218e99
+
218e99
+        if (curr.length != 0 && curr.flags == next.flags &&
218e99
+            curr.depth == next.depth &&
218e99
+            ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
218e99
+             curr.offset + curr.length == next.offset)) {
218e99
+            curr.length += next.length;
218e99
+            continue;
218e99
+        }
218e99
+
218e99
+        if (curr.length > 0) {
218e99
+            dump_map_entry(output_format, &curr, &next;;
218e99
+        }
218e99
+        curr = next;
218e99
+    }
218e99
+
218e99
+    dump_map_entry(output_format, &curr, NULL);
218e99
+
218e99
+out:
218e99
+    bdrv_close(bs);
218e99
+    bdrv_delete(bs);
218e99
+    return ret < 0;
218e99
+}
218e99
+
218e99
 #define SNAPSHOT_LIST   1
218e99
 #define SNAPSHOT_CREATE 2
218e99
 #define SNAPSHOT_APPLY  3
218e99
-- 
218e99
1.7.1
218e99