From nobody Thu Oct 30 08:09:57 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526088926550476.95690450266636; Fri, 11 May 2018 18:35:26 -0700 (PDT) Received: from localhost ([::1]:33856 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHJRV-0001vZ-Gz for importer@patchew.org; Fri, 11 May 2018 21:35:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40342) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHJIE-0006SQ-Ae for qemu-devel@nongnu.org; Fri, 11 May 2018 21:25:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fHJIB-0006Ym-PB for qemu-devel@nongnu.org; Fri, 11 May 2018 21:25:50 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:46944 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fHJI8-0006UJ-CA; Fri, 11 May 2018 21:25:44 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D255D4077B9C; Sat, 12 May 2018 01:25:43 +0000 (UTC) Received: from probe.bos.redhat.com (dhcp-17-231.bos.redhat.com [10.18.17.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id 79AE7215CDA7; Sat, 12 May 2018 01:25:43 +0000 (UTC) From: John Snow To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Fri, 11 May 2018 21:25:36 -0400 Message-Id: <20180512012537.22478-12-jsnow@redhat.com> In-Reply-To: <20180512012537.22478-1-jsnow@redhat.com> References: <20180512012537.22478-1-jsnow@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Sat, 12 May 2018 01:25:43 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Sat, 12 May 2018 01:25:43 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jsnow@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 11/12] qemu-img: add bitmap dump 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 , Vladimir Sementsov-Ogievskiy , Markus Armbruster , Max Reitz , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: John Snow --- 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-inte= rval=3D@var{flush_interval}] [-n] [--no-drain] [-o @var{offset}] [--pattern= =3D@var{pattern}] [-q] [-s @var{buffer_size}] [-S @var{step_size}] [-t @var= {cache}] [-w] [-U] @var{filename} ETEXI =20 +DEF("bitmap", img_bitmap, + "bitmap dump [--object objectdef] [--image-opts] [-f fmt] [--output=3D= ofmt] [-U] filename [bitmap]") +STEXI +@item bitmap dump [--object @var{objectdef}] [--image-opts] [-f @var{fmt}]= [--output=3D@var{ofmt}] [-U] @var{filename} [@var{bitmap}] +ETEXI + DEF("check", img_check, "check [-q] [--object objectdef] [--image-opts] [-f fmt] [--output=3Do= fmt] [-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; } =20 +static BitmapMapping *get_bitmap_mapping(BdrvDirtyBitmap *bitmap) +{ + BdrvDirtyBitmapIter *it =3D bdrv_dirty_iter_new(bitmap); + BitmapMapping *bm =3D g_new0(BitmapMapping, 1); + BitmapEntry *entry; + BitmapEntryList *tmp, **last; + int64_t offset; + int64_t zero_offset; + + bm->name =3D g_strdup(bdrv_dirty_bitmap_name(bitmap)); + bm->entries =3D NULL; + last =3D &bm->entries; + + while (true) { + offset =3D bdrv_dirty_iter_next(it); + if (offset =3D=3D -1) { + break; + } + + zero_offset =3D bdrv_dirty_bitmap_next_zero(bitmap, offset); + if (zero_offset =3D=3D -1) { + zero_offset =3D bdrv_dirty_bitmap_size(bitmap); + } + + entry =3D g_new0(BitmapEntry, 1); + entry->offset =3D offset; + entry->length =3D zero_offset - offset; + + tmp =3D g_new0(BitmapEntryList, 1); + tmp->value =3D entry; + *last =3D tmp; + last =3D &tmp->next; + + if (zero_offset >=3D 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 =3D qobject_output_visitor_new(&obj); + const int pretty =3D 1; + + visit_type_BitmapMapping(v, NULL, &bm, &error_abort); + visit_complete(v, &obj); + str =3D qobject_to_json_opt(obj, pretty, indent); + assert(str !=3D 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 =3D bm->entries; + + fprintf(stream, "# %s\n", bm->name); + fprintf(stream, "%-16s%-16s\n", "Offset", "Length"); + for ( ; bl; bl =3D bl->next) { + BitmapEntry *be =3D bl->value; + fprintf(stream, "%-16"PRId64"%-16"PRId64"\n", be->offset, be->leng= th); + } +} + +static void dump_bitmap_mapping(BitmapMapping *bm, OutputFormat output_for= mat, + 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 =3D opts->base.output_format; + + if (opts->name) { + bitmap =3D bdrv_find_dirty_bitmap(bs, opts->name); + if (bitmap) { + bm =3D get_bitmap_mapping(bitmap); + dump_bitmap_mapping(bm, ofmt, 0, stdout); + qapi_free_BitmapMapping(bm); + } else { + if (ofmt =3D=3D OFORMAT_HUMAN) { + fprintf(stdout, "Bitmap '%s' not found\n", opts->name); + } + } + } else { + if (ofmt =3D=3D OFORMAT_JSON) { + fprintf(stdout, "[\n"); + } + for (bitmap =3D NULL, printed =3D false; + (bitmap =3D bdrv_dirty_bitmap_next(bs, bitmap)); printed =3D = true) { + if (printed) { + fprintf(stdout, ofmt =3D=3D OFORMAT_JSON ? ",\n" : "\n"); + } + bm =3D get_bitmap_mapping(bitmap); + dump_bitmap_mapping(bm, ofmt, 1, stdout); + qapi_free_BitmapMapping(bm); + } + if (ofmt =3D=3D OFORMAT_JSON) { + fprintf(stdout, "%s%s", printed ? "\n" : "", "]\n"); + } + if (ofmt =3D=3D OFORMAT_HUMAN && printed =3D=3D 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 =3D NULL; + int flags =3D 0; + int ret =3D EXIT_SUCCESS; + int (* handler)(BlockDriverState *b, BitmapOpts *opts); + + if (argc < 2) { + error_report("Expected a bitmap subcommand: "); + return EXIT_FAILURE; + } + cmd =3D argv[1]; + if (strcmp(cmd, "dump") =3D=3D 0) { + handler =3D bitmap_cmd_dump; + } else { + error_report("Unrecognized bitmap subcommand '%s'", cmd); + return EXIT_FAILURE; + } + + opts =3D 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 =3D img_open(opts->base.image_opts, opts->base.filename, opts->bas= e.fmt, + flags, false, false, opts->base.force_share); + if (!blk) { + ret =3D EXIT_FAILURE; + goto out; + } + bs =3D blk_bs(blk); + + if (handler(bs, opts)) { + ret =3D EXIT_FAILURE; + } + + blk_unref(blk); + out: + g_free(opts); + return ret; +} + #define SNAPSHOT_LIST 1 #define SNAPSHOT_CREATE 2 #define SNAPSHOT_APPLY 3 --=20 2.14.3