qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: qemu-block@nongnu.org, qemu-devel@nongnu.org
Cc: Max Reitz <mreitz@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Kevin Wolf <kwolf@redhat.com>, Eric Blake <eblake@redhat.com>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	John Snow <jsnow@redhat.com>
Subject: [Qemu-devel] [RFC PATCH 11/12] qemu-img: add bitmap dump
Date: Fri, 11 May 2018 21:25:36 -0400	[thread overview]
Message-ID: <20180512012537.22478-12-jsnow@redhat.com> (raw)
In-Reply-To: <20180512012537.22478-1-jsnow@redhat.com>

Signed-off-by: John Snow <jsnow@redhat.com>
---
 qemu-img-cmds.hx |   6 ++
 qemu-img.c       | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 191 insertions(+)

diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
index 2fe31893cf..d25f359f5a 100644
--- a/qemu-img-cmds.hx
+++ b/qemu-img-cmds.hx
@@ -22,6 +22,12 @@ STEXI
 @item bench [-c @var{count}] [-d @var{depth}] [-f @var{fmt}] [--flush-interval=@var{flush_interval}] [-n] [--no-drain] [-o @var{offset}] [--pattern=@var{pattern}] [-q] [-s @var{buffer_size}] [-S @var{step_size}] [-t @var{cache}] [-w] [-U] @var{filename}
 ETEXI
 
+DEF("bitmap", img_bitmap,
+    "bitmap dump [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [-U] filename [bitmap]")
+STEXI
+@item bitmap dump [--object @var{objectdef}] [--image-opts] [-f @var{fmt}] [--output=@var{ofmt}] [-U] @var{filename} [@var{bitmap}]
+ETEXI
+
 DEF("check", img_check,
     "check [-q] [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] [-U] filename")
 STEXI
diff --git a/qemu-img.c b/qemu-img.c
index e31e38f674..1141216617 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2881,6 +2881,191 @@ out1:
     return ret < 0;
 }
 
+static BitmapMapping *get_bitmap_mapping(BdrvDirtyBitmap *bitmap)
+{
+    BdrvDirtyBitmapIter *it = bdrv_dirty_iter_new(bitmap);
+    BitmapMapping *bm = g_new0(BitmapMapping, 1);
+    BitmapEntry *entry;
+    BitmapEntryList *tmp, **last;
+    int64_t offset;
+    int64_t zero_offset;
+
+    bm->name = g_strdup(bdrv_dirty_bitmap_name(bitmap));
+    bm->entries = NULL;
+    last = &bm->entries;
+
+    while (true) {
+        offset = bdrv_dirty_iter_next(it);
+        if (offset == -1) {
+            break;
+        }
+
+        zero_offset = bdrv_dirty_bitmap_next_zero(bitmap, offset);
+        if (zero_offset == -1) {
+            zero_offset = bdrv_dirty_bitmap_size(bitmap);
+        }
+
+        entry = g_new0(BitmapEntry, 1);
+        entry->offset = offset;
+        entry->length = zero_offset - offset;
+
+        tmp = g_new0(BitmapEntryList, 1);
+        tmp->value = entry;
+        *last = tmp;
+        last = &tmp->next;
+
+        if (zero_offset >= bdrv_dirty_bitmap_size(bitmap)) {
+            break;
+        }
+        bdrv_set_dirty_iter(it, zero_offset);
+    }
+
+    bdrv_dirty_iter_free(it);
+    return bm;
+}
+
+static void dump_bitmap_mapping_json(BitmapMapping *bm, int indent,
+                                     FILE *stream)
+{
+    QString *str;
+    QObject *obj;
+    Visitor *v = qobject_output_visitor_new(&obj);
+    const int pretty = 1;
+
+    visit_type_BitmapMapping(v, NULL, &bm, &error_abort);
+    visit_complete(v, &obj);
+    str = qobject_to_json_opt(obj, pretty, indent);
+    assert(str != NULL);
+    fprintf(stream, "%*s%s", 4 * indent, "", qstring_get_str(str));
+    qobject_unref(obj);
+    visit_free(v);
+    qobject_unref(str);
+}
+
+static void dump_bitmap_mapping_human(BitmapMapping *bm, FILE *stream)
+{
+    BitmapEntryList *bl = bm->entries;
+
+    fprintf(stream, "# %s\n", bm->name);
+    fprintf(stream, "%-16s%-16s\n", "Offset", "Length");
+    for ( ; bl; bl = bl->next) {
+        BitmapEntry *be = bl->value;
+        fprintf(stream, "%-16"PRId64"%-16"PRId64"\n", be->offset, be->length);
+    }
+}
+
+static void dump_bitmap_mapping(BitmapMapping *bm, OutputFormat output_format,
+                                int indent, FILE *stream)
+{
+    switch (output_format) {
+    case OFORMAT_JSON:
+        dump_bitmap_mapping_json(bm, indent, stream);
+        return;
+    case OFORMAT_HUMAN:
+        dump_bitmap_mapping_human(bm, stream);
+        return;
+    }
+}
+
+/* img_bitmap subcommands: dump */
+
+typedef struct BitmapOpts {
+    CommonOpts base;
+    const char *name;
+    uint32_t granularity;
+} BitmapOpts;
+
+static int bitmap_cmd_dump(BlockDriverState *bs, BitmapOpts *opts)
+{
+    BdrvDirtyBitmap *bitmap;
+    BitmapMapping *bm;
+    bool printed;
+    OutputFormat ofmt = opts->base.output_format;
+
+    if (opts->name) {
+        bitmap = bdrv_find_dirty_bitmap(bs, opts->name);
+        if (bitmap) {
+            bm = get_bitmap_mapping(bitmap);
+            dump_bitmap_mapping(bm, ofmt, 0, stdout);
+            qapi_free_BitmapMapping(bm);
+        } else {
+            if (ofmt == OFORMAT_HUMAN) {
+                fprintf(stdout, "Bitmap '%s' not found\n", opts->name);
+            }
+        }
+    } else {
+        if (ofmt == OFORMAT_JSON) {
+            fprintf(stdout, "[\n");
+        }
+        for (bitmap = NULL, printed = false;
+             (bitmap = bdrv_dirty_bitmap_next(bs, bitmap)); printed = true) {
+            if (printed) {
+                fprintf(stdout, ofmt == OFORMAT_JSON ? ",\n" : "\n");
+            }
+            bm = get_bitmap_mapping(bitmap);
+            dump_bitmap_mapping(bm, ofmt, 1, stdout);
+            qapi_free_BitmapMapping(bm);
+        }
+        if (ofmt == OFORMAT_JSON) {
+            fprintf(stdout, "%s%s", printed ? "\n" : "", "]\n");
+        }
+        if (ofmt == OFORMAT_HUMAN && printed == false) {
+            fprintf(stdout, "No bitmaps are present in this image.\n");
+        }
+    }
+
+    return 0;
+}
+
+static int img_bitmap(int argc, char **argv)
+{
+    const char *cmd;
+    BitmapOpts *opts;
+    BlockBackend *blk;
+    BlockDriverState *bs;
+    const char *bname = NULL;
+    int flags = 0;
+    int ret = EXIT_SUCCESS;
+    int (* handler)(BlockDriverState *b, BitmapOpts *opts);
+
+    if (argc < 2) {
+        error_report("Expected a bitmap subcommand: <dump>");
+        return EXIT_FAILURE;
+    }
+    cmd = argv[1];
+    if (strcmp(cmd, "dump") == 0) {
+        handler = bitmap_cmd_dump;
+    } else {
+        error_report("Unrecognized bitmap subcommand '%s'", cmd);
+        return EXIT_FAILURE;
+    }
+
+    opts = g_new0(BitmapOpts, 1);
+    if (parse_opts_common(&opts->base, --argc, ++argv)) {
+        return EXIT_FAILURE;
+    }
+    parse_positional(argc, argv, 0, &opts->base.filename, "filename");
+    parse_positional(argc, argv, 1, &bname, "bitmap");
+    parse_unexpected(argc, argv);
+
+    blk = img_open(opts->base.image_opts, opts->base.filename, opts->base.fmt,
+                   flags, false, false, opts->base.force_share);
+    if (!blk) {
+        ret = EXIT_FAILURE;
+        goto out;
+    }
+    bs = blk_bs(blk);
+
+    if (handler(bs, opts)) {
+        ret = EXIT_FAILURE;
+    }
+
+    blk_unref(blk);
+ out:
+    g_free(opts);
+    return ret;
+}
+
 #define SNAPSHOT_LIST   1
 #define SNAPSHOT_CREATE 2
 #define SNAPSHOT_APPLY  3
-- 
2.14.3

  parent reply	other threads:[~2018-05-12  1:25 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-12  1:25 [Qemu-devel] [RFC PATCH 00/12] qemu-img: add bitmap queries John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 01/12] qcow2-bitmap: cache bm_list John Snow
2018-05-14 11:55   ` Vladimir Sementsov-Ogievskiy
2018-05-14 12:15     ` Vladimir Sementsov-Ogievskiy
2018-05-15 20:38       ` John Snow
2018-05-15 20:27     ` John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 02/12] qcow2/dirty-bitmap: cache loaded bitmaps John Snow
2018-05-14 12:33   ` Vladimir Sementsov-Ogievskiy
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 03/12] block/qcow2-bitmap: avoid adjusting bm->flags for RO bitmaps John Snow
2018-05-14 12:44   ` Vladimir Sementsov-Ogievskiy
2018-05-15 20:59     ` John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 04/12] qcow2/dirty-bitmaps: load IN_USE bitmaps if disk is RO John Snow
2018-05-14 12:55   ` Vladimir Sementsov-Ogievskiy
2018-05-15 20:52     ` John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 05/12] qcow2-bitmap: track bitmap type John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 06/12] qapi: add bitmap info John Snow
2018-05-14 14:30   ` Vladimir Sementsov-Ogievskiy
2018-05-15 20:56     ` John Snow
2018-05-16 21:15     ` John Snow
2018-05-17 10:01       ` Vladimir Sementsov-Ogievskiy
2018-05-17 16:43         ` John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 07/12] qcow2-bitmap: add basic bitmaps info John Snow
2018-05-14 15:12   ` Vladimir Sementsov-Ogievskiy
2018-05-15 21:03     ` John Snow
2018-05-16 21:17     ` John Snow
2018-05-17 10:03       ` Vladimir Sementsov-Ogievskiy
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 08/12] qjson: allow caller to ask for arbitrary indent John Snow
2018-05-16 21:34   ` Eric Blake
2018-05-16 21:49     ` John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 09/12] qapi/block-core: add BitmapMapping and BitmapEntry structs John Snow
2018-05-16 21:37   ` Eric Blake
2018-05-16 21:55     ` John Snow
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 10/12] qemu-img: split off common chunk of map command John Snow
2018-05-12  1:25 ` John Snow [this message]
2018-05-12  1:25 ` [Qemu-devel] [RFC PATCH 12/12] qemu-img: add bitmap clear John Snow
2018-05-12  1:38 ` [Qemu-devel] [RFC PATCH 00/12] qemu-img: add bitmap queries no-reply
2018-05-14 11:32 ` Vladimir Sementsov-Ogievskiy

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=20180512012537.22478-12-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /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).