Skip to contentSkip to conversation

block: persistent dirty bitmaps (RFC)

PATCH In review Vladimir Sementsov-Ogievskiy 4 participants 18 messages Jan 13, 2015 – Feb 4, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

cover block: persistent dirty bitmaps (RFC)

The bitmaps are saved into qcow2 file format. It provides both 'internal' and 'external' dirty bitmaps feature:
- for qcow2 drives we can store bitmaps in the same file
- for other formats we can store bitmaps in the separate qcow2 file

QCow2 header is extended by fields 'nb_dirty_bitmaps' and 'dirty_bitmaps_offset' like with snapshots.

Proposed command line syntax is the following:

-dirty-bitmap [option1=val1][,option2=val2]...
Available options are:
name The name for the bitmap (necessary).

file The file to load the bitmap from.

file_id When specified with 'file' option, then this file will
be available through this id for other -dirty-bitmap options when specified without 'file' option, then it is a reference to 'file', specified with another -dirty-bitmap option, and it will be used to load the bitmap from.

drive The drive to bind the bitmap to. It should be specified
as 'id' suboption of one of -drive options. If nor 'file' neither 'file_id' are specified, then the bitmap will be loaded from that drive (internal dirty bitmap).

granularity The granularity for the bitmap. Not necessary, the
default value may be used.

enabled on|off. Default is 'on'. Disabled bitmaps are not
changing regardless of writes to corresponding drive.

Examples:

qemu -drive file=a.qcow2,id=disk -dirty-bitmap name=b,drive=disk qemu -drive file=a.raw,id=disk \
-dirty-bitmap name=b,drive=disk,file=b.qcow2,enabled=off

Vladimir Sementsov-Ogievskiy (8):
spec: add qcow2-dirty-bitmaps specification
hbitmap: store / restore
qcow2: add dirty-bitmaps feature
block: store persistent dirty bitmaps
block: add bdrv_load_dirty_bitmap
qemu: command line option for dirty bitmaps
qmp: print dirty bitmap
iotests: test internal persistent dirty bitmap

 block.c                    | 113 ++++++++++
 block/Makefile.objs        |   2 +-
 block/qcow2-dirty-bitmap.c | 514 +++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c              |  26 +++
 block/qcow2.h              |  48 +++++
 blockdev.c                 |  51 +++++
 docs/specs/qcow2.txt       |  59 ++++++
 hmp-commands.hx            |  15 ++
 hmp.c                      |   8 +
 hmp.h                      |   1 +
 include/block/block.h      |   9 +
 include/block/block_int.h  |  10 +
 include/qemu/hbitmap.h     |  49 +++++
 include/sysemu/blockdev.h  |   1 +
 include/sysemu/sysemu.h    |   1 +
 qapi-schema.json           |   3 +-
 qapi/block-core.json       |   3 +
 qemu-options.hx            |  37 ++++
 qmp-commands.hx            |   5 +
 tests/qemu-iotests/115     |  96 +++++++++
 tests/qemu-iotests/115.out |  64 ++++++
 tests/qemu-iotests/group   |   1 +
 util/hbitmap.c             |  87 ++++++++
 vl.c                       | 100 +++++++++
 24 files changed, 1301 insertions(+), 2 deletions(-)
 create mode 100644 block/qcow2-dirty-bitmap.c
 create mode 100755 tests/qemu-iotests/115
 create mode 100644 tests/qemu-iotests/115.out
Signature · 1 line
1.9.1
#3 lore raw

PATCH 2/8 hbitmap: store / restore

Functions to store / restore HBitmap. HBitmap should be saved to linear bitmap format independently of endianess.

These functions are appropriate for dirty bitmap migration, retoring the bitmap in several steps is available. To save performance, every step writes only the last level of the bitmap. All other levels are restored by hbitmap_restore_finish as a last step of restoring. So, HBitmap is inconsistent while restoring.

---
 include/qemu/hbitmap.h | 49 ++++++++++++++++++++++++++++
 util/hbitmap.c         | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+)
include/qemu/hbitmap.h +49 -0
index c48c50a..f432f7f 100644
@@ -137,6 +137,55 @@ void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count);
 bool hbitmap_get(const HBitmap *hb, uint64_t item);
 
 /**
+ * hbitmap_data_size:
+ * @hb: HBitmap to operate on.
+ * @count: Number of bits
+ *
+ * Return amount of bytes hbitmap_store_data needs
+ */
+uint64_t hbitmap_data_size(const HBitmap *hb, uint64_t count);
+
+/**
+ * hbitmap_store_data
+ * @hb: HBitmap to oprate on.
+ * @buf: Buffer to store bitmap data.
+ * @start: First bit to store.
+ * @count: Number of bits to store.
+ *
+ * Stores HBitmap data corresponding to given region. The format of saved data
+ * is linear sequence of bits, so it can be used by hbitmap_restore_data
+ * independently of endianess
+ */
+void hbitmap_store_data(const HBitmap *hb, uint8_t *buf,
+                        uint64_t start, uint64_t count);
+
+/**
+ * hbitmap_restore_data
+ * @hb: HBitmap to oprate on.
+ * @buf: Buffer to restore bitmap data from.
+ * @start: First bit to restore.
+ * @count: Number of bits to restore.
+ *
+ * Retores HBitmap data corresponding to given region. The format is the same
+ * as for hbitmap_store_data.
+ *
+ * ! The bitmap becomes inconsistent after this operation.
+ * hbitmap_restore_finish should be called before using the bitmap after
+ * data restoring.
+ */
+void hbitmap_restore_data(HBitmap *hb, uint8_t *buf,
+                          uint64_t start, uint64_t count);
+
+/**
+ * hbitmap_restore_finish
+ * @hb: HBitmap to operate on.
+ *
+ * Repair HBitmap after calling hbitmap_restore_data. Actuall all HBitmap
+ * layers are restore here.
+ */
+void hbitmap_restore_finish(HBitmap *hb);
+
+/**
  * hbitmap_free:
  * @hb: HBitmap to operate on.
  *
util/hbitmap.c +87 -0

93 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/8 spec: add qcow2-dirty-bitmaps specification

In reply to Vladimir Sementsov-Ogievskiy

Persistent dirty bitmaps will be saved into qcow2 files. It may be used as 'internal' bitmaps (for qcow2 drives) or as 'external' bitmaps for other drives (there may be qcow2 file with zero disk size but with several dirty bitmaps for other drives).

---
 docs/specs/qcow2.txt | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
docs/specs/qcow2.txt +59 -0
index 121dfc8..b29de40 100644
@@ -116,6 +116,13 @@ in the description of a field.
                     Length of the header structure in bytes. For version 2
                     images, the length is always assumed to be 72 bytes.
 
+        104 - 107:  nb_dirty_bitmaps
+                    Number of dirty bitmaps contained in the image
+
+        108 - 115:  dirty_bitmaps_offset
+                    Offset into the image file at which the dirty bitmaps table
+                    starts. Must be aligned to a cluster boundary.
+
 Directly after the image header, optional sections called header extensions can
 be stored. Each extension has a structure like the following:
 
@@ -360,3 +367,55 @@ Snapshot table entry:
 
         variable:   Padding to round up the snapshot table entry size to the
                     next multiple of 8.
+
+
+== Dirty bitmaps ==
+
+The feature supports storing several dirty bitmaps in the qcow2 file.
+
+=== Cluster mapping ===
+
+Dirty bitmaps are stored using a ONE-level structure for the mapping of
+bitmaps to host clusters. There only an L1 table.
+
+The L1 table has a variable size (stored in the Bitmap table entry) and may
+use multiple clusters, however it must be contiguous in the image file.
+
+Given an offset into the bitmap, the offset into the image file can be
+obtained as follows:
+
+    offset = l1_table[offset / cluster_size] + (offset % cluster_size)
+
+L1 table entry:
+
+    Bit  0 -  61:   Standard cluster descriptor
+
+        62 -  63:   Reserved
+
+=== Bitmap table ===
+
+A directory of all bitmaps is stored in the bitmap table, a contiguous area in
+the image file, whose starting offset and length are given by the header fields
+dirty_bitmaps_offset and nb_dirty_bitmaps. The entries of the bitmap table have
+variable length, depending on the length of name and extra data.
+
+Bitmap table entry:
+
+    Byte 0 -  7:    Offset into the image file at which the L1 table for the
+                    bitmap starts. Must be aligned to a cluster boundary.
+
+         8 - 11:    Number of entries in the L1 table of the bitmap
+
+        12 - 15:    Bitmap granularity in bytes
+
+        16 - 23:    Bitmap size in sectors
+
+        24 - 25:    Size of the bitmap name
+
+        variable:   The name of the bitmap (not null terminated)
+
+        variable:   Padding to round up the bitmap table entry size to the
+                    next multiple of 8.
+
+The fields "size", "granularity" and "name" are corresponding with the fields
+in struct BdrvDirtyBitmap.
Signature · 1 line
1.9.1
#5 lore raw

PATCH 4/8 block: store persistent dirty bitmaps

In reply to Vladimir Sementsov-Ogievskiy

Persistent dirty bitmaps are the bitmaps, for which the new field BdrvDirtyBitmap.file is not NULL. We save all persistent dirty bitmaps owned by BlockDriverState in corresponding bdrv_close(). BdrvDirtyBitmap.file is a BlockDriverState, where we want to save the bitmap. It may be set in bdrv_dirty_bitmap_set_file() only once. bdrv_ref/bdrv_unref are used for BdrvDirtyBitmap.file to be sure that files will be closed and resources will be freed.

---
 block.c               | 43 +++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |  3 +++
 2 files changed, 46 insertions(+)
block.c +43 -0
index 2466ba8..7237b95 100644
@@ -54,6 +54,7 @@
 struct BdrvDirtyBitmap {
     HBitmap *bitmap;
     BdrvDirtyBitmap *originator;
+    BlockDriverState *file;
     int64_t size;
     int64_t granularity;
     char *name;
@@ -1840,6 +1841,7 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state)
 void bdrv_close(BlockDriverState *bs)
 {
     BdrvAioNotifier *ban, *ban_next;
+    BdrvDirtyBitmap *bm, *bm_next;
 
     if (bs->job) {
         block_job_cancel_sync(bs->job);
@@ -1849,6 +1851,15 @@ void bdrv_close(BlockDriverState *bs)
     bdrv_drain_all(); /* in case flush left pending I/O */
     notifier_list_notify(&bs->close_notifiers, bs);
 
+    /* save and release persistent dirty bitmaps */
+    QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, bm_next) {
+        if (bm->file) {
+            bdrv_store_dirty_bitmap(bm);
+            bdrv_unref(bm->file);
+            bdrv_release_dirty_bitmap(bs, bm);
+        }
+    }
+
     if (bs->drv) {
         if (bs->backing_hd) {
             BlockDriverState *backing_hd = bs->backing_hd;
@@ -5373,6 +5384,29 @@ BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
     return originator;
 }
 
+int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap)
+{
+    BlockDriverState *bs = bitmap->file;
+    uint8_t *buf;
+    uint64_t size;
+    assert(bs);
+    assert(bs->drv);
+    assert(bs->drv->bdrv_dirty_bitmap_store);
+
+    size = hbitmap_data_size(bitmap->bitmap, bitmap->size);
+    size = (size + 3) & ~3;
+    buf = g_malloc(size);
+
+    hbitmap_store_data(bitmap->bitmap, buf, 0, bitmap->size);
+
+    int res = bs->drv->bdrv_dirty_bitmap_store(bs, buf,
+                                               bitmap->name,
+                                               bitmap->size,
+                                               bitmap->granularity);
+
+    g_free(buf);
+    return res;
+}
 
 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
                                           int granularity,
@@ -5421,6 +5455,15 @@ void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
     }
 }
 
+void bdrv_dirty_bitmap_set_file(BdrvDirtyBitmap *bitmap, BlockDriverState *file)
+{
+    assert(bitmap->file == NULL);
+    bitmap->file = file;
+    if (file != NULL) {
+        bdrv_ref(file);
+    }
+}
+
 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
 {
     bitmap->enabled = false;
include/block/block.h +3 -0
index cb1f28d..0dfefe3 100644
@@ -442,6 +442,8 @@ BdrvDirtyBitmap *bdrv_copy_dirty_bitmap(BlockDriverState *bs,
 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
                                            BdrvDirtyBitmap *failed);
 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap);
+void bdrv_dirty_bitmap_set_file(BdrvDirtyBitmap *bitmap,
+                                BlockDriverState *file);
 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap);
 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap);
 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs);
@@ -458,6 +460,7 @@ void bdrv_dirty_iter_init(BlockDriverState *bs,
                           BdrvDirtyBitmap *bitmap, struct HBitmapIter *hbi);
 void bdrv_set_dirty_iter(struct HBitmapIter *hbi, int64_t offset);
 int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap);
+int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap);
 
 void bdrv_enable_copy_on_read(BlockDriverState *bs);
 void bdrv_disable_copy_on_read(BlockDriverState *bs);
Signature · 1 line
1.9.1
#4 lore raw

PATCH 3/8 qcow2: add dirty-bitmaps feature

In reply to Vladimir Sementsov-Ogievskiy

Adds dirty-bitmaps feature to qcow2 format as specified in docs/specs/qcow2.txt

---
 block/Makefile.objs        |   2 +-
 block/qcow2-dirty-bitmap.c | 514 +++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c              |  26 +++
 block/qcow2.h              |  48 +++++
 include/block/block_int.h  |  10 +
 5 files changed, 599 insertions(+), 1 deletion(-)
 create mode 100644 block/qcow2-dirty-bitmap.c
block/Makefile.objs +1 -1
index 04b0e43..eebd1c9 100644
@@ -1,5 +1,5 @@
 block-obj-y += raw_bsd.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
-block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
+block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o qcow2-dirty-bitmap.o
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o vhdx-log.o
block/qcow2-dirty-bitmap.c new file +514 -0

514 lines not on this page · raw message

block/qcow2.c +26 -0
index e4e690a..6512788 100644
@@ -570,6 +570,8 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     be32_to_cpus(&header.refcount_table_clusters);
     be64_to_cpus(&header.snapshots_offset);
     be32_to_cpus(&header.nb_snapshots);
+    be64_to_cpus(&header.dirty_bitmaps_offset);
+    be32_to_cpus(&header.nb_dirty_bitmaps);
 
     if (header.magic != QCOW_MAGIC) {
         error_setg(errp, "Image is not in qcow2 format");
@@ -892,6 +894,16 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    /* Internal bitmaps */
+    s->dirty_bitmaps_offset = header.dirty_bitmaps_offset;
+    s->nb_dirty_bitmaps = header.nb_dirty_bitmaps;
+
+    ret = qcow2_read_dirty_bitmaps(bs);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "Could not read dirty bitmaps");
+        goto fail;
+    }
+
     /* Clear unknown autoclear feature bits */
     if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) {
         s->autoclear_features = 0;
@@ -994,6 +1006,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     g_free(s->unknown_header_fields);
     cleanup_unknown_header_ext(bs);
     qcow2_free_snapshots(bs);
+    qcow2_free_dirty_bitmaps(bs);
     qcow2_refcount_close(bs);
     qemu_vfree(s->l1_table);
     /* else pre-write overlap checks in cache_destroy may crash */
@@ -1457,6 +1470,7 @@ static void qcow2_close(BlockDriverState *bs)
     qemu_vfree(s->cluster_data);
     qcow2_refcount_close(bs);
     qcow2_free_snapshots(bs);
+    qcow2_free_dirty_bitmaps(bs);
 }
 
 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
@@ -1579,6 +1593,8 @@ int qcow2_update_header(BlockDriverState *bs)
         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
+        .nb_dirty_bitmaps           = cpu_to_be32(s->nb_dirty_bitmaps),
+        .dirty_bitmaps_offset       = cpu_to_be64(s->dirty_bitmaps_offset),
 
         /* Version 3 fields */
         .incompatible_features  = cpu_to_be64(s->incompatible_features),
@@ -2123,6 +2139,12 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
         return -ENOTSUP;
     }
 
+    /* cannot proceed if image has dirty_bitmaps */
+    if (s->nb_dirty_bitmaps) {
+        error_report("Can't resize an image which has dirty bitmaps");
+        return -ENOTSUP;
+    }
+
     /* shrinking is currently not supported */
     if (offset < bs->total_sectors * 512) {
         error_report("qcow2 doesn't support shrinking images yet");
@@ -2888,6 +2910,10 @@ BlockDriver bdrv_qcow2 = {
     .bdrv_get_info          = qcow2_get_info,
     .bdrv_get_specific_info = qcow2_get_specific_info,
 
+    .bdrv_dirty_bitmap_load = qcow2_dirty_bitmap_load,
+    .bdrv_dirty_bitmap_store = qcow2_dirty_bitmap_store,
+    .bdrv_dirty_bitmap_delete = qcow2_dirty_bitmap_delete,
+
     .bdrv_save_vmstate    = qcow2_save_vmstate,
     .bdrv_load_vmstate    = qcow2_load_vmstate,
 
block/qcow2.h +48 -0

90 lines not on this page · raw message

include/block/block_int.h +10 -0
index cb1e4a1..0e3b5b3 100644
@@ -204,6 +204,16 @@ struct BlockDriver {
     int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
     ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
 
+    int (*bdrv_dirty_bitmap_store)(BlockDriverState *bs, uint8_t *buf,
+                                   const char *name, uint64_t size,
+                                   int granularity);
+    uint8_t *(*bdrv_dirty_bitmap_load)(BlockDriverState *bs,
+                                       const char *name, uint64_t size,
+                                       int granularity);
+    int (*bdrv_dirty_bitmap_delete)(BlockDriverState *bs,
+                                    const char *name,
+                                    Error **errp);
+
     int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov,
                              int64_t pos);
     int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
Signature · 1 line
1.9.1
#6 lore raw

PATCH 5/8 block: add bdrv_load_dirty_bitmap

In reply to Vladimir Sementsov-Ogievskiy

The funcion loads dirty bitmap from file, using underlying driver function.

Note: the function doesn't change BdrvDirtyBitmap.file field. This field is only used by bdrv_store_dirty_bitmap() function and is ONLY written by bdrv_dirty_bitmap_set_file() function.

---
 block.c               | 37 +++++++++++++++++++++++++++++++++++++
 include/block/block.h |  5 +++++
 2 files changed, 42 insertions(+)
block.c +37 -0
index 7237b95..77419e9 100644
@@ -5384,6 +5384,43 @@ BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
     return originator;
 }
 
+BdrvDirtyBitmap *bdrv_load_dirty_bitmap(BlockDriverState *bs,
+                                        BlockDriverState *file,
+                                        int granularity,
+                                        const char *name,
+                                        Error **errp)
+{
+    BlockDriver *drv = file->drv;
+    if (!drv) {
+        return NULL;
+    }
+    if (drv->bdrv_dirty_bitmap_load) {
+        BdrvDirtyBitmap *bitmap;
+        uint64_t bitmap_size = bdrv_nb_sectors(bs);
+        uint8_t *buf = drv->bdrv_dirty_bitmap_load(file, name, bitmap_size,
+                                                   granularity);
+        if (buf == NULL) {
+            return NULL;
+        }
+
+        bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
+        if (bitmap == NULL) {
+            g_free(buf);
+            return NULL;
+        }
+
+        hbitmap_restore_data(bitmap->bitmap, buf, 0, bitmap_size);
+        hbitmap_restore_finish(bitmap->bitmap);
+
+        return bitmap;
+    }
+    if (file->file)  {
+        return bdrv_load_dirty_bitmap(bs, file->file, granularity, name,
+                                      errp);
+    }
+    return NULL;
+}
+
 int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap)
 {
     BlockDriverState *bs = bitmap->file;
include/block/block.h +5 -0
index 0dfefe3..f36557f 100644
@@ -460,6 +460,11 @@ void bdrv_dirty_iter_init(BlockDriverState *bs,
                           BdrvDirtyBitmap *bitmap, struct HBitmapIter *hbi);
 void bdrv_set_dirty_iter(struct HBitmapIter *hbi, int64_t offset);
 int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap);
+BdrvDirtyBitmap *bdrv_load_dirty_bitmap(BlockDriverState *bs,
+                                        BlockDriverState *file,
+                                        int granularity,
+                                        const char *name,
+                                        Error **errp);
 int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap);
 
 void bdrv_enable_copy_on_read(BlockDriverState *bs);
Signature · 1 line
1.9.1
#7 lore raw

PATCH 6/8 qemu: command line option for dirty bitmaps

In reply to Vladimir Sementsov-Ogievskiy

The patch adds the following command line option:

-dirty-bitmap [option1=val1][,option2=val2]...
Available options are:
name The name for the bitmap (necessary).

file The file to load the bitmap from.

file_id When specified with 'file' option, then this file will
be available through this id for other -dirty-bitmap options when specified without 'file' option, then it is a reference to 'file', specified with another -dirty-bitmap option, and it will be used to load the bitmap from.

drive The drive to bind the bitmap to. It should be specified
as 'id' suboption of one of -drive options. If nor 'file' neither 'file_id' are specified, then the bitmap will be loaded from that drive (internal dirty bitmap).

granularity The granularity for the bitmap. Not necessary, the
default value may be used.

enabled on|off. Default is 'on'. Disabled bitmaps are not
changing regardless of writes to corresponding drive.

---
 blockdev.c                |  38 ++++++++++++++++++
 include/sysemu/blockdev.h |   1 +
 include/sysemu/sysemu.h   |   1 +
 qemu-options.hx           |  37 +++++++++++++++++
 vl.c                      | 100 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 177 insertions(+)
blockdev.c +38 -0
index 209fedd..8a9be08 100644
@@ -176,6 +176,11 @@ QemuOpts *drive_def(const char *optstr)
     return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
 }
 
+QemuOpts *dirty_bitmap_def(const char *optstr)
+{
+    return qemu_opts_parse(qemu_find_opts("dirty-bitmap"), optstr, 0);
+}
+
 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
                     const char *optstr)
 {
@@ -3044,6 +3049,39 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)
     return head;
 }
 
+QemuOptsList qemu_dirty_bitmap_opts = {
+    .name = "dirty-bitmap",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_dirty_bitmap_opts.head),
+    .desc = {
+        {
+            .name = "name",
+            .type = QEMU_OPT_STRING,
+            .help = "Name of the dirty bitmap",
+        },{
+            .name = "file",
+            .type = QEMU_OPT_STRING,
+            .help = "file name to load the bitmap from",
+        },{
+            .name = "file_id",
+            .type = QEMU_OPT_STRING,
+            .help = "node name to load the bitmap from (or to set id for"
+                    " for file, opened by previous option)",
+        },{
+            .name = "drive",
+            .type = QEMU_OPT_STRING,
+            .help = "drive id to bind the bitmap to",
+        },{
+            .name = "granularity",
+            .type = QEMU_OPT_NUMBER,
+            .help = "granularity",
+        },{
+            .name = "enabled",
+            .type = QEMU_OPT_BOOL,
+            .help = "enabled flag (default is 'on')",
+        }
+    }
+};
+
 QemuOptsList qemu_common_drive_opts = {
     .name = "drive",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
include/sysemu/blockdev.h +1 -0
index 09d1e30..48cce5c 100644
@@ -57,6 +57,7 @@ int drive_get_max_devs(BlockInterfaceType type);
 DriveInfo *drive_get_next(BlockInterfaceType type);
 
 QemuOpts *drive_def(const char *optstr);
+QemuOpts *dirty_bitmap_def(const char *optstr);
 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
                     const char *optstr);
 DriveInfo *drive_new(QemuOpts *arg, BlockInterfaceType block_default_type);
include/sysemu/sysemu.h +1 -0
index 503e5a4..c984a52 100644
@@ -231,6 +231,7 @@ bool usb_enabled(bool default_usb);
 
 extern QemuOptsList qemu_legacy_drive_opts;
 extern QemuOptsList qemu_common_drive_opts;
+extern QemuOptsList qemu_dirty_bitmap_opts;
 extern QemuOptsList qemu_drive_opts;
 extern QemuOptsList qemu_chardev_opts;
 extern QemuOptsList qemu_device_opts;
qemu-options.hx +37 -0
index 10b9568..3a5bfde 100644
@@ -592,6 +592,43 @@ qemu-system-i386 -hda a -hdb b
 @end example
 ETEXI
 
+DEF("dirty-bitmap", HAS_ARG, QEMU_OPTION_dirty_bitmap,
+    "-dirty-bitmap name=name[,file=file][,file_id=file_id][,drive=@var{id}]\n"
+    "              [,granularity=granularity][,enabled=on|off]\n",
+    QEMU_ARCH_ALL)
+STEXI
+@item -dirty-bitmap @var{option}[,@var{option}[,@var{option}[,...]]]
+@findex -dirty-bitmap
+
+Define a dirty-bitmap. Valid options are:
+
+@table @option
+@item name=@var{name}
+The name of the bitmap. Should be unique per @var{file}/@var{drive} and per
+@var{for_drive}.
+@item file=@var{file}
+The separate qcow2 file for loading the bitmap @var{name} from it.
+@item file_id=@var{file_id}
+When specified with @var{file} option, then this @var{file} will be available
+through this @var{file_id} for other @option{-dirty-bitmap} options.
+When specified without @var{file} option, then it is a reference to @var{file},
+specified with another @option{-dirty-bitmap} option, and it will be used to
+load the bitmap from.
+@item drive=@var{drive}
+The drive to bind the bitmap to. It should be specified as @var{id} suboption
+of one of @option{-drive} options.
+If nor @var{file} neither @var{file_id} are specified, then the bitmap will be
+loaded from that drive (internal dirty bitmap).
+@item granularity=@var{granularity}
+Granularity (in bytes) for created dirty bitmap. If the bitmap is already
+exists in specified @var{file}/@var{file_id}/@var{device} it's granularity will
+not be changed but only checked (an error will be generated if this check
+fails).
+@item enabled=@var{enabled}
+Enabled flag for the bitmap. By default the bitmap will be enabled.
+@end table
+ETEXI
+
 DEF("mtdblock", HAS_ARG, QEMU_OPTION_mtdblock,
     "-mtdblock file  use 'file' as on-board Flash memory image\n",
     QEMU_ARCH_ALL)
vl.c +100 -0

124 lines not on this page · raw message

Signature · 1 line
1.9.1
#8 lore raw

PATCH 7/8 qmp: print dirty bitmap

In reply to Vladimir Sementsov-Ogievskiy

Adds qmp and hmp commands to print dirty bitmap. This is needed only for testing persistent dirty bitmap feature.

---
 block.c               | 33 +++++++++++++++++++++++++++++++++
 blockdev.c            | 13 +++++++++++++
 hmp-commands.hx       | 15 +++++++++++++++
 hmp.c                 |  8 ++++++++
 hmp.h                 |  1 +
 include/block/block.h |  1 +
 qapi-schema.json      |  3 ++-
 qapi/block-core.json  |  3 +++
 qmp-commands.hx       |  5 +++++
 9 files changed, 81 insertions(+), 1 deletion(-)
block.c +33 -0
index 77419e9..3e6dedf 100644
@@ -5445,6 +5445,39 @@ int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap)
     return res;
 }
 
+void bdrv_print_dirty_bitmap(BdrvDirtyBitmap *bitmap)
+{
+    unsigned long a = 0, b = 0;
+
+    printf("bitmap '%s'\n", bitmap->name ? bitmap->name : "no name");
+    printf("enabled: %s\n", bitmap->enabled ? "true" : "false");
+    printf("size: %" PRId64 "\n", bitmap->size);
+    printf("granularity: %" PRId64 "\n", bitmap->granularity);
+    printf("dirty regions begin:\n");
+
+    while (true) {
+        for (a = b; a < bitmap->size && !hbitmap_get(bitmap->bitmap, a); ++a) {
+            ;
+        }
+        if (a >= bitmap->size) {
+            break;
+        }
+
+        for (b = a + 1;
+             b < bitmap->size && hbitmap_get(bitmap->bitmap, b);
+             ++b) {
+            ;
+        }
+
+        printf("%ld -> %ld\n", a, b - 1);
+        if (b >= bitmap->size) {
+            break;
+        }
+    }
+
+    printf("dirty regions end\n");
+}
+
 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
                                           int granularity,
                                           const char *name,
blockdev.c +13 -0
index 8a9be08..8b58c2e 100644
@@ -2079,6 +2079,19 @@ void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name,
     aio_context_release(aio_context);
 }
 
+void qmp_block_dirty_bitmap_print(const char *node_ref, const char *name,
+                                  Error **errp)
+{
+    BdrvDirtyBitmap *bitmap;
+
+    bitmap = block_dirty_bitmap_lookup(node_ref, name, NULL, errp);
+    if (!bitmap) {
+        return;
+    }
+
+    bdrv_print_dirty_bitmap(bitmap);
+}
+
 void qmp_block_dirty_bitmap_remove(const char *node_ref, const char *name,
                                    Error **errp)
 {
hmp-commands.hx +15 -0
index e37bc8b..a9be506 100644
@@ -58,6 +58,21 @@ Quit the emulator.
 ETEXI
 
     {
+        .name       = "print_dirty_bitmap",
+        .args_type  = "device:B,bitmap:s",
+        .params     = "device bitmap",
+        .help       = "print dirty bitmap",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd = hmp_print_dirty_bitmap,
+    },
+
+STEXI
+@item print_dirty_bitmap device_id bitmap_name
+@findex print_dirty_bitmap
+Print dirty bitmap meta information and dirty regions.
+ETEXI
+
+    {
         .name       = "block_resize",
         .args_type  = "device:B,size:o",
         .params     = "device size",
hmp.c +8 -0
index 63b19c7..a269145 100644
@@ -782,6 +782,14 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
     qapi_free_TPMInfoList(info_list);
 }
 
+void hmp_print_dirty_bitmap(Monitor *mon, const QDict *qdict)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    const char *name = qdict_get_str(qdict, "bitmap");
+
+    qmp_block_dirty_bitmap_print(device, name, NULL);
+}
+
 void hmp_quit(Monitor *mon, const QDict *qdict)
 {
     monitor_suspend(mon);
hmp.h +1 -0
index 4bb5dca..6bbbc33 100644
@@ -19,6 +19,7 @@
 #include "qapi-types.h"
 #include "qapi/qmp/qdict.h"
 
+void hmp_print_dirty_bitmap(Monitor *mon, const QDict *qdict);
 void hmp_info_name(Monitor *mon, const QDict *qdict);
 void hmp_info_version(Monitor *mon, const QDict *qdict);
 void hmp_info_kvm(Monitor *mon, const QDict *qdict);
include/block/block.h +1 -0
index f36557f..7188791 100644
@@ -466,6 +466,7 @@ BdrvDirtyBitmap *bdrv_load_dirty_bitmap(BlockDriverState *bs,
                                         const char *name,
                                         Error **errp);
 int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap);
+void bdrv_print_dirty_bitmap(BdrvDirtyBitmap *bitmap);
 
 void bdrv_enable_copy_on_read(BlockDriverState *bs);
 void bdrv_disable_copy_on_read(BlockDriverState *bs);
qapi-schema.json +2 -1
index 85f55d9..1475f69 100644
@@ -1263,7 +1263,8 @@
        'blockdev-snapshot-internal-sync': 'BlockdevSnapshotInternal',
        'block-dirty-bitmap-add': 'BlockDirtyBitmapAdd',
        'block-dirty-bitmap-enable': 'BlockDirtyBitmap',
-       'block-dirty-bitmap-disable': 'BlockDirtyBitmap'
+       'block-dirty-bitmap-disable': 'BlockDirtyBitmap',
+       'block-dirty-bitmap-print': 'BlockDirtyBitmap'
    } }
 
 ##
qapi/block-core.json +3 -0
index 1892b50..3e1edb1 100644
@@ -982,6 +982,9 @@
 {'command': 'block-dirty-bitmap-disable',
   'data': 'BlockDirtyBitmap' }
 
+{'command': 'block-dirty-bitmap-print',
+  'data': 'BlockDirtyBitmap' }
+
 ##
 # @block_set_io_throttle:
 #
qmp-commands.hx +5 -0
index 479d4f5..8065715 100644
@@ -1222,6 +1222,11 @@ EQMP
         .args_type  = "node-ref:B,name:s",
         .mhandler.cmd_new = qmp_marshal_input_block_dirty_bitmap_disable,
     },
+    {
+        .name       = "block-dirty-bitmap-print",
+        .args_type  = "node-ref:B,name:s",
+        .mhandler.cmd_new = qmp_marshal_input_block_dirty_bitmap_print,
+    },
 
 SQMP
 
Signature · 1 line
1.9.1
#13 lore raw

PATCH 8/8 iotests: test internal persistent dirty bitmap

In reply to Vladimir Sementsov-Ogievskiy

The test performs several vm reloads with checking and updating dirty bitmap.

---
 tests/qemu-iotests/115     | 96 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/115.out | 64 +++++++++++++++++++++++++++++++
 tests/qemu-iotests/group   |  1 +
 3 files changed, 161 insertions(+)
 create mode 100755 tests/qemu-iotests/115
 create mode 100644 tests/qemu-iotests/115.out
tests/qemu-iotests/115 new file +96 -0
new file mode 100755
index 0000000..ef853b1
@@ -0,0 +1,96 @@
+#!/bin/bash
+#
+# Persistent dirty bitmap test
+#
+# Performs several vm reloads with checking and updating dirty bitmap.
+#
+# Copyright (C) 2014 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+status=1    # failure is the default!
+
+_cleanup()
+{
+    _cleanup_qemu
+    _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+. ./common.qemu
+
+_supported_fmt qcow2
+_supported_os Linux
+
+size=1G
+_make_test_img $size
+
+qemu_comm_method="monitor"
+
+launch() {
+    _launch_qemu -drive file="${TEST_IMG}",id=disk\
+        -dirty-bitmap name=bitmap,drive=disk
+    echo
+    echo START VM
+}
+
+print_bitmap() {
+    _send_qemu_cmd $QEMU_HANDLE 'print_dirty_bitmap disk bitmap'\
+        "dirty regions end" | sed 's/^/    /'
+}
+
+write() {
+    _send_qemu_cmd $QEMU_HANDLE 'qemu-io disk "write ' $@ '"' "(qemu)"
+    echo
+    echo write $@
+}
+
+quit() {
+    _send_qemu_cmd $QEMU_HANDLE 'qemu-io disk flush' "(qemu)"
+    _send_qemu_cmd $QEMU_HANDLE 'quit' ""
+    echo QUIT
+    wait ${QEMU_PID[$QEMU_HANDLE]} 2>/dev/null
+}
+
+{
+    launch
+    print_bitmap
+    write 50m 1m
+    print_bitmap
+    quit
+
+    launch
+    print_bitmap
+    write 0m 10m
+    write 700m 200m
+    print_bitmap
+    quit
+
+    launch
+    print_bitmap
+    quit
+} | sed '/(qemu)/d'
+
+status=0
tests/qemu-iotests/115.out new file +64 -0

64 lines not on this page · raw message

tests/qemu-iotests/group +1 -0
index a4742c6..77377b3 100644
@@ -115,3 +115,4 @@
 111 rw auto quick
 113 rw auto quick
 114 rw auto quick
+115 rw auto quick
Signature · 1 line
1.9.1
#14 lore raw
In reply to Vladimir Sementsov-Ogievskiy

ping

Best regards,
Vladimir

Quoted 79 lines · Vladimir Sementsov-Ogievskiy

The bitmaps are saved into qcow2 file format. It provides both 'internal' and 'external' dirty bitmaps feature:
- for qcow2 drives we can store bitmaps in the same file
- for other formats we can store bitmaps in the separate qcow2 file

QCow2 header is extended by fields 'nb_dirty_bitmaps' and 'dirty_bitmaps_offset' like with snapshots.

Proposed command line syntax is the following:

-dirty-bitmap [option1=val1][,option2=val2]...
Available options are:
name The name for the bitmap (necessary).

file The file to load the bitmap from.

file_id When specified with 'file' option, then this file will
be available through this id for other -dirty-bitmap options when specified without 'file' option, then it is a reference to 'file', specified with another -dirty-bitmap option, and it will be used to load the bitmap from.

drive The drive to bind the bitmap to. It should be specified
as 'id' suboption of one of -drive options. If nor 'file' neither 'file_id' are specified, then the bitmap will be loaded from that drive (internal dirty bitmap).

granularity The granularity for the bitmap. Not necessary, the
default value may be used.

enabled on|off. Default is 'on'. Disabled bitmaps are not
changing regardless of writes to corresponding drive.

Examples:

qemu -drive file=a.qcow2,id=disk -dirty-bitmap name=b,drive=disk qemu -drive file=a.raw,id=disk \
-dirty-bitmap name=b,drive=disk,file=b.qcow2,enabled=off

Vladimir Sementsov-Ogievskiy (8):
spec: add qcow2-dirty-bitmaps specification
hbitmap: store / restore
qcow2: add dirty-bitmaps feature
block: store persistent dirty bitmaps
block: add bdrv_load_dirty_bitmap
qemu: command line option for dirty bitmaps
qmp: print dirty bitmap
iotests: test internal persistent dirty bitmap

  block.c                    | 113 ++++++++++
  block/Makefile.objs        |   2 +-
  block/qcow2-dirty-bitmap.c | 514 +++++++++++++++++++++++++++++++++++++++++++++
  block/qcow2.c              |  26 +++
  block/qcow2.h              |  48 +++++
  blockdev.c                 |  51 +++++
  docs/specs/qcow2.txt       |  59 ++++++
  hmp-commands.hx            |  15 ++
  hmp.c                      |   8 +
  hmp.h                      |   1 +
  include/block/block.h      |   9 +
  include/block/block_int.h  |  10 +
  include/qemu/hbitmap.h     |  49 +++++
  include/sysemu/blockdev.h  |   1 +
  include/sysemu/sysemu.h    |   1 +
  qapi-schema.json           |   3 +-
  qapi/block-core.json       |   3 +
  qemu-options.hx            |  37 ++++
  qmp-commands.hx            |   5 +
  tests/qemu-iotests/115     |  96 +++++++++
  tests/qemu-iotests/115.out |  64 ++++++
  tests/qemu-iotests/group   |   1 +
  util/hbitmap.c             |  87 ++++++++
  vl.c                       | 100 +++++++++
  24 files changed, 1301 insertions(+), 2 deletions(-)

create mode 100644 block/qcow2-dirty-bitmap.c
create mode 100755 tests/qemu-iotests/115
create mode 100644 tests/qemu-iotests/115.out

#2 lore raw

Re: [Qemu-devel] [PATCH 1/8] spec: add qcow2-dirty-bitmaps specification

In reply to Vladimir Sementsov-Ogievskiy
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

Persistent dirty bitmaps will be saved into qcow2 files. It may be used as 'internal' bitmaps (for qcow2 drives) or as 'external' bitmaps for other drives (there may be qcow2 file with zero disk size but with several dirty bitmaps for other drives).

---
 docs/specs/qcow2.txt | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
docs/specs/qcow2.txt +0 -0
index 121dfc8..b29de40 100644

docs/specs/qcow2.txt in the description of a field. · line 124

Quoted 9 lines
docs/specs/qcow2.txt +6 -0
@@ -116,6 +116,13 @@ in the description of a field.
                     Length of the header structure in bytes. For version 2
                     images, the length is always assumed to be 72 bytes.
 
+        104 - 107:  nb_dirty_bitmaps
+                    Number of dirty bitmaps contained in the image
+
+        108 - 115:  dirty_bitmaps_offset
+                    Offset into the image file at which the dirty bitmaps table
+                    starts. Must be aligned to a cluster boundary.

NACK. You cannot add new fields directly into the header without bumping the version number; but that is overkill. Instead, stick the dirty bitmaps into a new header extension. The existing fields of the header are sufficient to record that a new extension is present. Also, you need to decide whether dirty bitmaps are an ignorable feature or a mandatory feature, and set the appropriate feature bit in the header flag fields (thus when an older qemu that doesn't know about dirty bitmaps opens the file, it will either reject the open due to not knowing how to keep dirty bitmaps sane, or will accept the open but clear the ignorable bit so that the next time a new qemu opens the file it will know that the dirty bitmap is no longer reliable).

docs/specs/qcow2.txt Snapshot table entry: · line 379

Quoted 4 lines
docs/specs/qcow2.txt +4 -0
+=== Cluster mapping ===
+
+Dirty bitmaps are stored using a ONE-level structure for the mapping of
+bitmaps to host clusters. There only an L1 table.

s/There/There is/

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#9 lore raw

Re: [Qemu-devel] [PATCH 7/8] qmp: print dirty bitmap

In reply to Vladimir Sementsov-Ogievskiy
Quoted 27 lines · Vladimir Sementsov-Ogievskiy

Adds qmp and hmp commands to print dirty bitmap. This is needed only for testing persistent dirty bitmap feature.

---

+++ b/block.c

block.c +11 -0
@@ -5445,6 +5445,39 @@ int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap)
     return res;
 }
 
+void bdrv_print_dirty_bitmap(BdrvDirtyBitmap *bitmap)
+{
+    unsigned long a = 0, b = 0;
+
+    printf("bitmap '%s'\n", bitmap->name ? bitmap->name : "no name");
+    printf("enabled: %s\n", bitmap->enabled ? "true" : "false");
+    printf("size: %" PRId64 "\n", bitmap->size);
+    printf("granularity: %" PRId64 "\n", bitmap->granularity);
+    printf("dirty regions begin:\n");
 
+++ b/blockdev.c
@@ -2079,6 +2079,19 @@ void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name,
     aio_context_release(aio_context);
 }
 
+void qmp_block_dirty_bitmap_print(const char *node_ref, const char *name,

block.c void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name, · line 2092

Quoted 10 lines
block.c +10 -0
+                                  Error **errp)
+{
+    BdrvDirtyBitmap *bitmap;
+
+    bitmap = block_dirty_bitmap_lookup(node_ref, name, NULL, errp);
+    if (!bitmap) {
+        return;
+    }
+
+    bdrv_print_dirty_bitmap(bitmap);

Eww. This assumes that stdout is usable. But that is not the case for QMP commands. It would be better to package up the output in a structured format and return it to the caller, and let the caller decide how to print it.

qapi/block-core.json line 986

Quoted 7 lines
qapi/block-core.json +3 -0
+++ b/qapi/block-core.json
@@ -982,6 +982,9 @@
 {'command': 'block-dirty-bitmap-disable',
   'data': 'BlockDirtyBitmap' }
 
+{'command': 'block-dirty-bitmap-print',
+  'data': 'BlockDirtyBitmap' }

Missing documentation, if we even want this command. As mentioned above, this should return ALL of the information necessary for the client to then decide how to print the information, rather than directly printing information to stdout itself. And it might be better to name this command in the 'query-' namespace.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#10 lore raw

Re: [Qemu-devel] [PATCH 7/8] qmp: print dirty bitmap

Quoted 27 lines · Eric Blake
Quoted 26 lines · Vladimir Sementsov-Ogievskiy

Adds qmp and hmp commands to print dirty bitmap. This is needed only for testing persistent dirty bitmap feature.

---

+++ b/block.c

block.c +10 -0
@@ -5445,6 +5445,39 @@ int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap)
     return res;
 }
 
+void bdrv_print_dirty_bitmap(BdrvDirtyBitmap *bitmap)
+{
+    unsigned long a = 0, b = 0;
+
+    printf("bitmap '%s'\n", bitmap->name ? bitmap->name : "no name");
+    printf("enabled: %s\n", bitmap->enabled ? "true" : "false");
+    printf("size: %" PRId64 "\n", bitmap->size);
+    printf("granularity: %" PRId64 "\n", bitmap->granularity);
+    printf("dirty regions begin:\n");
 
+++ b/blockdev.c
@@ -2079,6 +2079,19 @@ void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name,
     aio_context_release(aio_context);
 }
 
Quoted 16 lines

blockdev.c void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name, · line 2092

Quoted 11 lines
blockdev.c +11 -0
+void qmp_block_dirty_bitmap_print(const char *node_ref, const char *name,
+                                  Error **errp)
+{
+    BdrvDirtyBitmap *bitmap;
+
+    bitmap = block_dirty_bitmap_lookup(node_ref, name, NULL, errp);
+    if (!bitmap) {
+        return;
+    }
+
+    bdrv_print_dirty_bitmap(bitmap);

Eww. This assumes that stdout is usable. But that is not the case for QMP commands. It would be better to package up the output in a structured format and return it to the caller, and let the caller decide how to print it.

This isn't a case of "would be better", actually.

HMP commands may print to the *monitor*. Printing to stdout is wrong.

QMP commands return their result as a JSON object. Printing results anywhere is wrong.

We commonly implement the HMP command handler on top of the QMP command handler. The QMP command handler returns an object. The QMP core sends this object in JSON format to the QMP client. The HMP command prints it to the monitor in a suitable human-readable format.

Quoted 5 lines
Quoted 5 lines
qapi/block-core.json +1 -0
+++ b/qapi/block-core.json
@@ -982,6 +982,9 @@
 {'command': 'block-dirty-bitmap-disable',
   'data': 'BlockDirtyBitmap' }
 
Quoted 8 lines

qapi/block-core.json line 986

Quoted 2 lines
qapi/block-core.json +2 -0
+{'command': 'block-dirty-bitmap-print',
+  'data': 'BlockDirtyBitmap' }

Missing documentation, if we even want this command. As mentioned above, this should return ALL of the information necessary for the client to then decide how to print the information, rather than directly printing information to stdout itself. And it might be better to name this command in the 'query-' namespace.

Yup.

#11 lore raw

Re: [Qemu-devel] [PATCH 7/8] qmp: print dirty bitmap

In reply to Eric Blake

is it better to add qmp_query_dirty_bitmap with underlying bdrv_query_dirty_bitmap, or to modify (add dirty regions information) existing qmp_query_block/qmp_query_dirty_bitmapS?

Best regards,
Vladimir

Quoted 54 lines · Eric Blake
Quoted 25 lines · Vladimir Sementsov-Ogievskiy

Adds qmp and hmp commands to print dirty bitmap. This is needed only for testing persistent dirty bitmap feature.

---
+++ b/block.c

block.c +11 -0
@@ -5445,6 +5445,39 @@ int bdrv_store_dirty_bitmap(BdrvDirtyBitmap *bitmap)
      return res;
  }
 
+void bdrv_print_dirty_bitmap(BdrvDirtyBitmap *bitmap)
+{
+    unsigned long a = 0, b = 0;
+
+    printf("bitmap '%s'\n", bitmap->name ? bitmap->name : "no name");
+    printf("enabled: %s\n", bitmap->enabled ? "true" : "false");
+    printf("size: %" PRId64 "\n", bitmap->size);
+    printf("granularity: %" PRId64 "\n", bitmap->granularity);
+    printf("dirty regions begin:\n");
+++ b/blockdev.c
@@ -2079,6 +2079,19 @@ void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name,
      aio_context_release(aio_context);
  }
 
+void qmp_block_dirty_bitmap_print(const char *node_ref, const char *name,

block.c void qmp_block_dirty_bitmap_add(const char *node_ref, const char *name, · line 2092

Quoted 10 lines
block.c +10 -0
+                                  Error **errp)
+{
+    BdrvDirtyBitmap *bitmap;
+
+    bitmap = block_dirty_bitmap_lookup(node_ref, name, NULL, errp);
+    if (!bitmap) {
+        return;
+    }
+
+    bdrv_print_dirty_bitmap(bitmap);

Eww. This assumes that stdout is usable. But that is not the case for QMP commands. It would be better to package up the output in a structured format and return it to the caller, and let the caller decide how to print it.

qapi/block-core.json line 986

Quoted 7 lines
qapi/block-core.json +3 -0
+++ b/qapi/block-core.json
@@ -982,6 +982,9 @@
  {'command': 'block-dirty-bitmap-disable',
    'data': 'BlockDirtyBitmap' }
 
+{'command': 'block-dirty-bitmap-print',
+  'data': 'BlockDirtyBitmap' }

Missing documentation, if we even want this command. As mentioned above, this should return ALL of the information necessary for the client to then decide how to print the information, rather than directly printing information to stdout itself. And it might be better to name this command in the 'query-' namespace.

#12 lore raw

Re: [Qemu-devel] [PATCH 7/8] qmp: print dirty bitmap

Quoted 3 lines · Vladimir Sementsov-Ogievskiy

is it better to add qmp_query_dirty_bitmap with underlying bdrv_query_dirty_bitmap, or to modify (add dirty regions information) existing qmp_query_block/qmp_query_dirty_bitmapS?

[please don't top-post on technical lists]

Extending an existing command may be reasonable, if the amount of information being added is compact (if you are adding several kilobytes of information, that might justify a new command, if only so that old callers don't waste time receiving large amounts of data on the wire just to sift through and discard it all to get at the older content they cared about).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#15 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 75 lines · Vladimir Sementsov-Ogievskiy

The bitmaps are saved into qcow2 file format. It provides both 'internal' and 'external' dirty bitmaps feature:
- for qcow2 drives we can store bitmaps in the same file
- for other formats we can store bitmaps in the separate qcow2 file

QCow2 header is extended by fields 'nb_dirty_bitmaps' and 'dirty_bitmaps_offset' like with snapshots.

Proposed command line syntax is the following:

-dirty-bitmap [option1=val1][,option2=val2]...
Available options are:
name The name for the bitmap (necessary).

file The file to load the bitmap from.

file_id When specified with 'file' option, then this file will
be available through this id for other -dirty-bitmap options when specified without 'file' option, then it is a reference to 'file', specified with another -dirty-bitmap option, and it will be used to load the bitmap from.

drive The drive to bind the bitmap to. It should be specified
as 'id' suboption of one of -drive options. If nor 'file' neither 'file_id' are specified, then the bitmap will be loaded from that drive (internal dirty bitmap).

granularity The granularity for the bitmap. Not necessary, the
default value may be used.

enabled on|off. Default is 'on'. Disabled bitmaps are not
changing regardless of writes to corresponding drive.

Examples:

qemu -drive file=a.qcow2,id=disk -dirty-bitmap name=b,drive=disk qemu -drive file=a.raw,id=disk \
-dirty-bitmap name=b,drive=disk,file=b.qcow2,enabled=off

Vladimir Sementsov-Ogievskiy (8):
spec: add qcow2-dirty-bitmaps specification
hbitmap: store / restore
qcow2: add dirty-bitmaps feature
block: store persistent dirty bitmaps
block: add bdrv_load_dirty_bitmap
qemu: command line option for dirty bitmaps
qmp: print dirty bitmap
iotests: test internal persistent dirty bitmap

  block.c                    | 113 ++++++++++
  block/Makefile.objs        |   2 +-
  block/qcow2-dirty-bitmap.c | 514 +++++++++++++++++++++++++++++++++++++++++++++
  block/qcow2.c              |  26 +++
  block/qcow2.h              |  48 +++++
  blockdev.c                 |  51 +++++
  docs/specs/qcow2.txt       |  59 ++++++
  hmp-commands.hx            |  15 ++
  hmp.c                      |   8 +
  hmp.h                      |   1 +
  include/block/block.h      |   9 +
  include/block/block_int.h  |  10 +
  include/qemu/hbitmap.h     |  49 +++++
  include/sysemu/blockdev.h  |   1 +
  include/sysemu/sysemu.h    |   1 +
  qapi-schema.json           |   3 +-
  qapi/block-core.json       |   3 +
  qemu-options.hx            |  37 ++++
  qmp-commands.hx            |   5 +
  tests/qemu-iotests/115     |  96 +++++++++
  tests/qemu-iotests/115.out |  64 ++++++
  tests/qemu-iotests/group   |   1 +
  util/hbitmap.c             |  87 ++++++++
  vl.c                       | 100 +++++++++
  24 files changed, 1301 insertions(+), 2 deletions(-)
Quoted 4 lines

create mode 100644 block/qcow2-dirty-bitmap.c
create mode 100755 tests/qemu-iotests/115
create mode 100644 tests/qemu-iotests/115.out

Ping. I've already done (locally):
1) using qcow2 header extension instead of changing the header
2) normal qmp query request instead of "print dirty bitmap"
- thanks to Eric and Markus

Now I'm waiting for some comments on the concept and it's realization to roll v3.

Signature · 2 lines
Best regards,
Vladimir
#16 lore raw
Quoted 82 lines · Vladimir Sementsov-Ogievskiy
Quoted 76 lines · Vladimir Sementsov-Ogievskiy

The bitmaps are saved into qcow2 file format. It provides both 'internal' and 'external' dirty bitmaps feature:
- for qcow2 drives we can store bitmaps in the same file
- for other formats we can store bitmaps in the separate qcow2 file

QCow2 header is extended by fields 'nb_dirty_bitmaps' and 'dirty_bitmaps_offset' like with snapshots.

Proposed command line syntax is the following:

-dirty-bitmap [option1=val1][,option2=val2]...
Available options are:
name The name for the bitmap (necessary).

file The file to load the bitmap from.

file_id When specified with 'file' option, then this file will
be available through this id for other -dirty-bitmap options when specified without 'file' option, then it is a reference to 'file', specified with another -dirty-bitmap option, and it will be used to load the bitmap from.

drive The drive to bind the bitmap to. It should be specified
as 'id' suboption of one of -drive options. If nor 'file' neither 'file_id' are specified, then the bitmap will be loaded from that drive (internal dirty bitmap).

granularity The granularity for the bitmap. Not necessary, the
default value may be used.

enabled on|off. Default is 'on'. Disabled bitmaps are not
changing regardless of writes to corresponding drive.

Examples:

qemu -drive file=a.qcow2,id=disk -dirty-bitmap name=b,drive=disk qemu -drive file=a.raw,id=disk \
-dirty-bitmap name=b,drive=disk,file=b.qcow2,enabled=off

Vladimir Sementsov-Ogievskiy (8):
spec: add qcow2-dirty-bitmaps specification
hbitmap: store / restore
qcow2: add dirty-bitmaps feature
block: store persistent dirty bitmaps
block: add bdrv_load_dirty_bitmap
qemu: command line option for dirty bitmaps
qmp: print dirty bitmap
iotests: test internal persistent dirty bitmap

  block.c                    | 113 ++++++++++
  block/Makefile.objs        |   2 +-
  block/qcow2-dirty-bitmap.c | 514
+++++++++++++++++++++++++++++++++++++++++++++
  block/qcow2.c              |  26 +++
  block/qcow2.h              |  48 +++++
  blockdev.c                 |  51 +++++
  docs/specs/qcow2.txt       |  59 ++++++
  hmp-commands.hx            |  15 ++
  hmp.c                      |   8 +
  hmp.h                      |   1 +
  include/block/block.h      |   9 +
  include/block/block_int.h  |  10 +
  include/qemu/hbitmap.h     |  49 +++++
  include/sysemu/blockdev.h  |   1 +
  include/sysemu/sysemu.h    |   1 +
  qapi-schema.json           |   3 +-
  qapi/block-core.json       |   3 +
  qemu-options.hx            |  37 ++++
  qmp-commands.hx            |   5 +
  tests/qemu-iotests/115     |  96 +++++++++
  tests/qemu-iotests/115.out |  64 ++++++
  tests/qemu-iotests/group   |   1 +
  util/hbitmap.c             |  87 ++++++++
  vl.c                       | 100 +++++++++
  24 files changed, 1301 insertions(+), 2 deletions(-)
Quoted 4 lines

create mode 100644 block/qcow2-dirty-bitmap.c
create mode 100755 tests/qemu-iotests/115
create mode 100644 tests/qemu-iotests/115.out

Quoted 8 lines

Ping. I've already done (locally):
1) using qcow2 header extension instead of changing the header
2) normal qmp query request instead of "print dirty bitmap"
- thanks to Eric and Markus

Now I'm waiting for some comments on the concept and it's realization to roll v3.

My apologies, I've been sick for a while. I'll get going on finishing review of these two series.

I assume you'd like to merge the bitmaps migration first?

--js

#17 lore raw
Quoted 98 lines · John Snow
Quoted 86 lines · Vladimir Sementsov-Ogievskiy
Quoted 80 lines · Vladimir Sementsov-Ogievskiy

The bitmaps are saved into qcow2 file format. It provides both 'internal' and 'external' dirty bitmaps feature:
- for qcow2 drives we can store bitmaps in the same file
- for other formats we can store bitmaps in the separate qcow2 file

QCow2 header is extended by fields 'nb_dirty_bitmaps' and 'dirty_bitmaps_offset' like with snapshots.

Proposed command line syntax is the following:

-dirty-bitmap [option1=val1][,option2=val2]...
Available options are:
name The name for the bitmap (necessary).

file The file to load the bitmap from.

file_id When specified with 'file' option, then this file
will
be available through this id for other -dirty-bitmap options when specified without 'file' option, then it is a reference to 'file', specified with another -dirty-bitmap option, and it will be used to load the bitmap from.

drive The drive to bind the bitmap to. It should be
specified
as 'id' suboption of one of -drive options. If nor 'file' neither 'file_id' are specified, then the
bitmap
will be loaded from that drive (internal dirty
bitmap).

granularity The granularity for the bitmap. Not necessary, the
default value may be used.

enabled on|off. Default is 'on'. Disabled bitmaps are not
changing regardless of writes to corresponding drive.

Examples:

qemu -drive file=a.qcow2,id=disk -dirty-bitmap name=b,drive=disk qemu -drive file=a.raw,id=disk \
-dirty-bitmap name=b,drive=disk,file=b.qcow2,enabled=off

Vladimir Sementsov-Ogievskiy (8):
spec: add qcow2-dirty-bitmaps specification
hbitmap: store / restore
qcow2: add dirty-bitmaps feature
block: store persistent dirty bitmaps
block: add bdrv_load_dirty_bitmap
qemu: command line option for dirty bitmaps
qmp: print dirty bitmap
iotests: test internal persistent dirty bitmap

  block.c                    | 113 ++++++++++
  block/Makefile.objs        |   2 +-
  block/qcow2-dirty-bitmap.c | 514
+++++++++++++++++++++++++++++++++++++++++++++
  block/qcow2.c              |  26 +++
  block/qcow2.h              |  48 +++++
  blockdev.c                 |  51 +++++
  docs/specs/qcow2.txt       |  59 ++++++
  hmp-commands.hx            |  15 ++
  hmp.c                      |   8 +
  hmp.h                      |   1 +
  include/block/block.h      |   9 +
  include/block/block_int.h  |  10 +
  include/qemu/hbitmap.h     |  49 +++++
  include/sysemu/blockdev.h  |   1 +
  include/sysemu/sysemu.h    |   1 +
  qapi-schema.json           |   3 +-
  qapi/block-core.json       |   3 +
  qemu-options.hx            |  37 ++++
  qmp-commands.hx            |   5 +
  tests/qemu-iotests/115     |  96 +++++++++
  tests/qemu-iotests/115.out |  64 ++++++
  tests/qemu-iotests/group   |   1 +
  util/hbitmap.c             |  87 ++++++++
  vl.c                       | 100 +++++++++
  24 files changed, 1301 insertions(+), 2 deletions(-)
Quoted 4 lines

create mode 100644 block/qcow2-dirty-bitmap.c
create mode 100755 tests/qemu-iotests/115
create mode 100644 tests/qemu-iotests/115.out

Quoted 8 lines

Ping. I've already done (locally):
1) using qcow2 header extension instead of changing the header
2) normal qmp query request instead of "print dirty bitmap"
- thanks to Eric and Markus

Now I'm waiting for some comments on the concept and it's realization to roll v3.

Quoted 5 lines

My apologies, I've been sick for a while. I'll get going on finishing review of these two series.

I assume you'd like to merge the bitmaps migration first?

--js

The sequence is not important. These series are necessary for making a complete backup solution - don't lose named dirty bitmaps on shutdown and migration. Shutdown is more often task, and dirty bitmaps migration was appeared as a subtask when we were discussed the format for saving dirty bitmaps. But they both are needed, then, ok, let's start with migration.

Signature · 2 lines
Best regards,
Vladimir