Blame SOURCES/kvm-blockdev-Split-off-basic-bitmap-operations-for-qemu-.patch

ddf19c
From 2afa718d59ef86879a9e34b4601a1f2658afa9ba Mon Sep 17 00:00:00 2001
ddf19c
From: Eric Blake <eblake@redhat.com>
ddf19c
Date: Tue, 2 Jun 2020 02:34:14 +0100
ddf19c
Subject: [PATCH 09/26] blockdev: Split off basic bitmap operations for
ddf19c
 qemu-img
ddf19c
ddf19c
RH-Author: Eric Blake <eblake@redhat.com>
ddf19c
Message-id: <20200602023420.2133649-7-eblake@redhat.com>
ddf19c
Patchwork-id: 97073
ddf19c
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 06/12] blockdev: Split off basic bitmap operations for qemu-img
ddf19c
Bugzilla: 1779893 1779904
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ddf19c
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ddf19c
ddf19c
Upcoming patches want to add some basic bitmap manipulation abilities
ddf19c
to qemu-img.  But blockdev.o is too heavyweight to link into qemu-img
ddf19c
(among other things, it would drag in block jobs and transaction
ddf19c
support - qemu-img does offline manipulation, where atomicity is less
ddf19c
important because there are no concurrent modifications to compete
ddf19c
with), so it's time to split off the bare bones of what we will need
ddf19c
into a new file block/monitor/bitmap-qmp-cmds.o.
ddf19c
ddf19c
This is sufficient to expose 6 QMP commands for use by qemu-img (add,
ddf19c
remove, clear, enable, disable, merge), as well as move the three
ddf19c
helper functions touched in the previous patch.  Regarding
ddf19c
MAINTAINERS, the new file is automatically part of block core, but
ddf19c
also makes sense as related to other dirty bitmap files.
ddf19c
ddf19c
Signed-off-by: Eric Blake <eblake@redhat.com>
ddf19c
Reviewed-by: Max Reitz <mreitz@redhat.com>
ddf19c
Message-Id: <20200513011648.166876-6-eblake@redhat.com>
ddf19c
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
ddf19c
(cherry picked from commit bb4e58c6137e80129b955789dd4b66c1504f20dc)
ddf19c
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
ddf19c
Conflicts:
ddf19c
	Makefile.objs - comment context
ddf19c
	block/monitor/Makefile.objs - context: a2dde2f2 not backported
ddf19c
	blockdev.c - context
ddf19c
Signed-off-by: Eric Blake <eblake@redhat.com>
ddf19c
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 MAINTAINERS                     |   1 +
ddf19c
 Makefile.objs                   |   3 +-
ddf19c
 block/monitor/Makefile.objs     |   1 +
ddf19c
 block/monitor/bitmap-qmp-cmds.c | 321 ++++++++++++++++++++++++++++++++++++++++
ddf19c
 blockdev.c                      | 284 -----------------------------------
ddf19c
 5 files changed, 324 insertions(+), 286 deletions(-)
ddf19c
 create mode 100644 block/monitor/Makefile.objs
ddf19c
 create mode 100644 block/monitor/bitmap-qmp-cmds.c
ddf19c
ddf19c
diff --git a/MAINTAINERS b/MAINTAINERS
ddf19c
index 3a81ac9..49d5d44 100644
ddf19c
--- a/MAINTAINERS
ddf19c
+++ b/MAINTAINERS
ddf19c
@@ -1875,6 +1875,7 @@ L: qemu-block@nongnu.org
ddf19c
 S: Supported
ddf19c
 F: include/qemu/hbitmap.h
ddf19c
 F: include/block/dirty-bitmap.h
ddf19c
+F: block/monitor/bitmap-qmp-cmds.c
ddf19c
 F: block/dirty-bitmap.c
ddf19c
 F: block/qcow2-bitmap.c
ddf19c
 F: migration/block-dirty-bitmap.c
ddf19c
diff --git a/Makefile.objs b/Makefile.objs
ddf19c
index 1a8f288..7404ef0 100644
ddf19c
--- a/Makefile.objs
ddf19c
+++ b/Makefile.objs
ddf19c
@@ -13,9 +13,8 @@ authz-obj-y = authz/
ddf19c
 #######################################################################
ddf19c
 # block-obj-y is code used by both qemu system emulation and qemu-img
ddf19c
 
ddf19c
-block-obj-y = nbd/
ddf19c
+block-obj-y = block/ block/monitor/ nbd/ scsi/
ddf19c
 block-obj-y += block.o blockjob.o job.o
ddf19c
-block-obj-y += block/ scsi/
ddf19c
 block-obj-y += qemu-io-cmds.o
ddf19c
 block-obj-$(CONFIG_REPLICATION) += replication.o
ddf19c
 
ddf19c
diff --git a/block/monitor/Makefile.objs b/block/monitor/Makefile.objs
ddf19c
new file mode 100644
ddf19c
index 0000000..f0c7642
ddf19c
--- /dev/null
ddf19c
+++ b/block/monitor/Makefile.objs
ddf19c
@@ -0,0 +1 @@
ddf19c
+block-obj-y += bitmap-qmp-cmds.o
ddf19c
diff --git a/block/monitor/bitmap-qmp-cmds.c b/block/monitor/bitmap-qmp-cmds.c
ddf19c
new file mode 100644
ddf19c
index 0000000..9f11dee
ddf19c
--- /dev/null
ddf19c
+++ b/block/monitor/bitmap-qmp-cmds.c
ddf19c
@@ -0,0 +1,321 @@
ddf19c
+/*
ddf19c
+ * QEMU block dirty bitmap QMP commands
ddf19c
+ *
ddf19c
+ * Copyright (c) 2003-2008 Fabrice Bellard
ddf19c
+ *
ddf19c
+ * This work is licensed under the terms of the GNU GPL, version 2 or
ddf19c
+ * later.  See the COPYING file in the top-level directory.
ddf19c
+ *
ddf19c
+ * This file incorporates work covered by the following copyright and
ddf19c
+ * permission notice:
ddf19c
+ *
ddf19c
+ * Copyright (c) 2003-2008 Fabrice Bellard
ddf19c
+ *
ddf19c
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
ddf19c
+ * of this software and associated documentation files (the "Software"), to deal
ddf19c
+ * in the Software without restriction, including without limitation the rights
ddf19c
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ddf19c
+ * copies of the Software, and to permit persons to whom the Software is
ddf19c
+ * furnished to do so, subject to the following conditions:
ddf19c
+ *
ddf19c
+ * The above copyright notice and this permission notice shall be included in
ddf19c
+ * all copies or substantial portions of the Software.
ddf19c
+ *
ddf19c
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ddf19c
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ddf19c
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ddf19c
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ddf19c
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ddf19c
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ddf19c
+ * THE SOFTWARE.
ddf19c
+ */
ddf19c
+
ddf19c
+#include "qemu/osdep.h"
ddf19c
+
ddf19c
+#include "block/block_int.h"
ddf19c
+#include "qapi/qapi-commands-block.h"
ddf19c
+#include "qapi/error.h"
ddf19c
+
ddf19c
+/**
ddf19c
+ * block_dirty_bitmap_lookup:
ddf19c
+ * Return a dirty bitmap (if present), after validating
ddf19c
+ * the node reference and bitmap names.
ddf19c
+ *
ddf19c
+ * @node: The name of the BDS node to search for bitmaps
ddf19c
+ * @name: The name of the bitmap to search for
ddf19c
+ * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
ddf19c
+ * @errp: Output pointer for error information. Can be NULL.
ddf19c
+ *
ddf19c
+ * @return: A bitmap object on success, or NULL on failure.
ddf19c
+ */
ddf19c
+BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
ddf19c
+                                           const char *name,
ddf19c
+                                           BlockDriverState **pbs,
ddf19c
+                                           Error **errp)
ddf19c
+{
ddf19c
+    BlockDriverState *bs;
ddf19c
+    BdrvDirtyBitmap *bitmap;
ddf19c
+
ddf19c
+    if (!node) {
ddf19c
+        error_setg(errp, "Node cannot be NULL");
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+    if (!name) {
ddf19c
+        error_setg(errp, "Bitmap name cannot be NULL");
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+    bs = bdrv_lookup_bs(node, node, NULL);
ddf19c
+    if (!bs) {
ddf19c
+        error_setg(errp, "Node '%s' not found", node);
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    bitmap = bdrv_find_dirty_bitmap(bs, name);
ddf19c
+    if (!bitmap) {
ddf19c
+        error_setg(errp, "Dirty bitmap '%s' not found", name);
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (pbs) {
ddf19c
+        *pbs = bs;
ddf19c
+    }
ddf19c
+
ddf19c
+    return bitmap;
ddf19c
+}
ddf19c
+
ddf19c
+void qmp_block_dirty_bitmap_add(const char *node, const char *name,
ddf19c
+                                bool has_granularity, uint32_t granularity,
ddf19c
+                                bool has_persistent, bool persistent,
ddf19c
+                                bool has_disabled, bool disabled,
ddf19c
+                                Error **errp)
ddf19c
+{
ddf19c
+    BlockDriverState *bs;
ddf19c
+    BdrvDirtyBitmap *bitmap;
ddf19c
+    AioContext *aio_context;
ddf19c
+
ddf19c
+    if (!name || name[0] == '\0') {
ddf19c
+        error_setg(errp, "Bitmap name cannot be empty");
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    bs = bdrv_lookup_bs(node, node, errp);
ddf19c
+    if (!bs) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    aio_context = bdrv_get_aio_context(bs);
ddf19c
+    aio_context_acquire(aio_context);
ddf19c
+
ddf19c
+    if (has_granularity) {
ddf19c
+        if (granularity < 512 || !is_power_of_2(granularity)) {
ddf19c
+            error_setg(errp, "Granularity must be power of 2 "
ddf19c
+                             "and at least 512");
ddf19c
+            goto out;
ddf19c
+        }
ddf19c
+    } else {
ddf19c
+        /* Default to cluster size, if available: */
ddf19c
+        granularity = bdrv_get_default_bitmap_granularity(bs);
ddf19c
+    }
ddf19c
+
ddf19c
+    if (!has_persistent) {
ddf19c
+        persistent = false;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (!has_disabled) {
ddf19c
+        disabled = false;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (persistent &&
ddf19c
+        !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
ddf19c
+    {
ddf19c
+        goto out;
ddf19c
+    }
ddf19c
+
ddf19c
+    bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
ddf19c
+    if (bitmap == NULL) {
ddf19c
+        goto out;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (disabled) {
ddf19c
+        bdrv_disable_dirty_bitmap(bitmap);
ddf19c
+    }
ddf19c
+
ddf19c
+    bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
ddf19c
+
ddf19c
+out:
ddf19c
+    aio_context_release(aio_context);
ddf19c
+}
ddf19c
+
ddf19c
+BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,
ddf19c
+                                           bool release,
ddf19c
+                                           BlockDriverState **bitmap_bs,
ddf19c
+                                           Error **errp)
ddf19c
+{
ddf19c
+    BlockDriverState *bs;
ddf19c
+    BdrvDirtyBitmap *bitmap;
ddf19c
+    AioContext *aio_context;
ddf19c
+
ddf19c
+    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
+    if (!bitmap || !bs) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    aio_context = bdrv_get_aio_context(bs);
ddf19c
+    aio_context_acquire(aio_context);
ddf19c
+
ddf19c
+    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO,
ddf19c
+                                errp)) {
ddf19c
+        aio_context_release(aio_context);
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
ddf19c
+        bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
ddf19c
+    {
ddf19c
+        aio_context_release(aio_context);
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (release) {
ddf19c
+        bdrv_release_dirty_bitmap(bitmap);
ddf19c
+    }
ddf19c
+
ddf19c
+    if (bitmap_bs) {
ddf19c
+        *bitmap_bs = bs;
ddf19c
+    }
ddf19c
+
ddf19c
+    aio_context_release(aio_context);
ddf19c
+    return release ? NULL : bitmap;
ddf19c
+}
ddf19c
+
ddf19c
+void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
ddf19c
+                                   Error **errp)
ddf19c
+{
ddf19c
+    block_dirty_bitmap_remove(node, name, true, NULL, errp);
ddf19c
+}
ddf19c
+
ddf19c
+/**
ddf19c
+ * Completely clear a bitmap, for the purposes of synchronizing a bitmap
ddf19c
+ * immediately after a full backup operation.
ddf19c
+ */
ddf19c
+void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
ddf19c
+                                  Error **errp)
ddf19c
+{
ddf19c
+    BdrvDirtyBitmap *bitmap;
ddf19c
+    BlockDriverState *bs;
ddf19c
+
ddf19c
+    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
+    if (!bitmap || !bs) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    bdrv_clear_dirty_bitmap(bitmap, NULL);
ddf19c
+}
ddf19c
+
ddf19c
+void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
ddf19c
+                                   Error **errp)
ddf19c
+{
ddf19c
+    BlockDriverState *bs;
ddf19c
+    BdrvDirtyBitmap *bitmap;
ddf19c
+
ddf19c
+    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
+    if (!bitmap) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    bdrv_enable_dirty_bitmap(bitmap);
ddf19c
+}
ddf19c
+
ddf19c
+void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
ddf19c
+                                    Error **errp)
ddf19c
+{
ddf19c
+    BlockDriverState *bs;
ddf19c
+    BdrvDirtyBitmap *bitmap;
ddf19c
+
ddf19c
+    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
+    if (!bitmap) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    bdrv_disable_dirty_bitmap(bitmap);
ddf19c
+}
ddf19c
+
ddf19c
+BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,
ddf19c
+                                          BlockDirtyBitmapMergeSourceList *bms,
ddf19c
+                                          HBitmap **backup, Error **errp)
ddf19c
+{
ddf19c
+    BlockDriverState *bs;
ddf19c
+    BdrvDirtyBitmap *dst, *src, *anon;
ddf19c
+    BlockDirtyBitmapMergeSourceList *lst;
ddf19c
+    Error *local_err = NULL;
ddf19c
+
ddf19c
+    dst = block_dirty_bitmap_lookup(node, target, &bs, errp);
ddf19c
+    if (!dst) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst),
ddf19c
+                                    NULL, errp);
ddf19c
+    if (!anon) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    for (lst = bms; lst; lst = lst->next) {
ddf19c
+        switch (lst->value->type) {
ddf19c
+            const char *name, *node;
ddf19c
+        case QTYPE_QSTRING:
ddf19c
+            name = lst->value->u.local;
ddf19c
+            src = bdrv_find_dirty_bitmap(bs, name);
ddf19c
+            if (!src) {
ddf19c
+                error_setg(errp, "Dirty bitmap '%s' not found", name);
ddf19c
+                dst = NULL;
ddf19c
+                goto out;
ddf19c
+            }
ddf19c
+            break;
ddf19c
+        case QTYPE_QDICT:
ddf19c
+            node = lst->value->u.external.node;
ddf19c
+            name = lst->value->u.external.name;
ddf19c
+            src = block_dirty_bitmap_lookup(node, name, NULL, errp);
ddf19c
+            if (!src) {
ddf19c
+                dst = NULL;
ddf19c
+                goto out;
ddf19c
+            }
ddf19c
+            break;
ddf19c
+        default:
ddf19c
+            abort();
ddf19c
+        }
ddf19c
+
ddf19c
+        bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err);
ddf19c
+        if (local_err) {
ddf19c
+            error_propagate(errp, local_err);
ddf19c
+            dst = NULL;
ddf19c
+            goto out;
ddf19c
+        }
ddf19c
+    }
ddf19c
+
ddf19c
+    /* Merge into dst; dst is unchanged on failure. */
ddf19c
+    bdrv_merge_dirty_bitmap(dst, anon, backup, errp);
ddf19c
+
ddf19c
+ out:
ddf19c
+    bdrv_release_dirty_bitmap(anon);
ddf19c
+    return dst;
ddf19c
+}
ddf19c
+
ddf19c
+void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
ddf19c
+                                  BlockDirtyBitmapMergeSourceList *bitmaps,
ddf19c
+                                  Error **errp)
ddf19c
+{
ddf19c
+    block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
ddf19c
+}
ddf19c
diff --git a/blockdev.c b/blockdev.c
ddf19c
index 3958058..5128c9b 100644
ddf19c
--- a/blockdev.c
ddf19c
+++ b/blockdev.c
ddf19c
@@ -1250,53 +1250,6 @@ out_aio_context:
ddf19c
     return NULL;
ddf19c
 }
ddf19c
 
ddf19c
-/**
ddf19c
- * block_dirty_bitmap_lookup:
ddf19c
- * Return a dirty bitmap (if present), after validating
ddf19c
- * the node reference and bitmap names.
ddf19c
- *
ddf19c
- * @node: The name of the BDS node to search for bitmaps
ddf19c
- * @name: The name of the bitmap to search for
ddf19c
- * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
ddf19c
- * @errp: Output pointer for error information. Can be NULL.
ddf19c
- *
ddf19c
- * @return: A bitmap object on success, or NULL on failure.
ddf19c
- */
ddf19c
-BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
ddf19c
-                                           const char *name,
ddf19c
-                                           BlockDriverState **pbs,
ddf19c
-                                           Error **errp)
ddf19c
-{
ddf19c
-    BlockDriverState *bs;
ddf19c
-    BdrvDirtyBitmap *bitmap;
ddf19c
-
ddf19c
-    if (!node) {
ddf19c
-        error_setg(errp, "Node cannot be NULL");
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-    if (!name) {
ddf19c
-        error_setg(errp, "Bitmap name cannot be NULL");
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-    bs = bdrv_lookup_bs(node, node, NULL);
ddf19c
-    if (!bs) {
ddf19c
-        error_setg(errp, "Node '%s' not found", node);
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    bitmap = bdrv_find_dirty_bitmap(bs, name);
ddf19c
-    if (!bitmap) {
ddf19c
-        error_setg(errp, "Dirty bitmap '%s' not found", name);
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (pbs) {
ddf19c
-        *pbs = bs;
ddf19c
-    }
ddf19c
-
ddf19c
-    return bitmap;
ddf19c
-}
ddf19c
-
ddf19c
 /* New and old BlockDriverState structs for atomic group operations */
ddf19c
 
ddf19c
 typedef struct BlkActionState BlkActionState;
ddf19c
@@ -2974,243 +2927,6 @@ out:
ddf19c
     aio_context_release(aio_context);
ddf19c
 }
ddf19c
 
ddf19c
-void qmp_block_dirty_bitmap_add(const char *node, const char *name,
ddf19c
-                                bool has_granularity, uint32_t granularity,
ddf19c
-                                bool has_persistent, bool persistent,
ddf19c
-                                bool has_disabled, bool disabled,
ddf19c
-                                Error **errp)
ddf19c
-{
ddf19c
-    BlockDriverState *bs;
ddf19c
-    BdrvDirtyBitmap *bitmap;
ddf19c
-    AioContext *aio_context;
ddf19c
-
ddf19c
-    if (!name || name[0] == '\0') {
ddf19c
-        error_setg(errp, "Bitmap name cannot be empty");
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    bs = bdrv_lookup_bs(node, node, errp);
ddf19c
-    if (!bs) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    aio_context = bdrv_get_aio_context(bs);
ddf19c
-    aio_context_acquire(aio_context);
ddf19c
-
ddf19c
-    if (has_granularity) {
ddf19c
-        if (granularity < 512 || !is_power_of_2(granularity)) {
ddf19c
-            error_setg(errp, "Granularity must be power of 2 "
ddf19c
-                             "and at least 512");
ddf19c
-            goto out;
ddf19c
-        }
ddf19c
-    } else {
ddf19c
-        /* Default to cluster size, if available: */
ddf19c
-        granularity = bdrv_get_default_bitmap_granularity(bs);
ddf19c
-    }
ddf19c
-
ddf19c
-    if (!has_persistent) {
ddf19c
-        persistent = false;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (!has_disabled) {
ddf19c
-        disabled = false;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (persistent &&
ddf19c
-        !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
ddf19c
-    {
ddf19c
-        goto out;
ddf19c
-    }
ddf19c
-
ddf19c
-    bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
ddf19c
-    if (bitmap == NULL) {
ddf19c
-        goto out;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (disabled) {
ddf19c
-        bdrv_disable_dirty_bitmap(bitmap);
ddf19c
-    }
ddf19c
-
ddf19c
-    bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
ddf19c
-
ddf19c
-out:
ddf19c
-    aio_context_release(aio_context);
ddf19c
-}
ddf19c
-
ddf19c
-BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,
ddf19c
-                                           bool release,
ddf19c
-                                           BlockDriverState **bitmap_bs,
ddf19c
-                                           Error **errp)
ddf19c
-{
ddf19c
-    BlockDriverState *bs;
ddf19c
-    BdrvDirtyBitmap *bitmap;
ddf19c
-    AioContext *aio_context;
ddf19c
-
ddf19c
-    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
-    if (!bitmap || !bs) {
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    aio_context = bdrv_get_aio_context(bs);
ddf19c
-    aio_context_acquire(aio_context);
ddf19c
-
ddf19c
-    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO,
ddf19c
-                                errp)) {
ddf19c
-        aio_context_release(aio_context);
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
ddf19c
-        bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
ddf19c
-    {
ddf19c
-        aio_context_release(aio_context);
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (release) {
ddf19c
-        bdrv_release_dirty_bitmap(bitmap);
ddf19c
-    }
ddf19c
-
ddf19c
-    if (bitmap_bs) {
ddf19c
-        *bitmap_bs = bs;
ddf19c
-    }
ddf19c
-
ddf19c
-    aio_context_release(aio_context);
ddf19c
-    return release ? NULL : bitmap;
ddf19c
-}
ddf19c
-
ddf19c
-void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
ddf19c
-                                   Error **errp)
ddf19c
-{
ddf19c
-    block_dirty_bitmap_remove(node, name, true, NULL, errp);
ddf19c
-}
ddf19c
-
ddf19c
-/**
ddf19c
- * Completely clear a bitmap, for the purposes of synchronizing a bitmap
ddf19c
- * immediately after a full backup operation.
ddf19c
- */
ddf19c
-void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
ddf19c
-                                  Error **errp)
ddf19c
-{
ddf19c
-    BdrvDirtyBitmap *bitmap;
ddf19c
-    BlockDriverState *bs;
ddf19c
-
ddf19c
-    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
-    if (!bitmap || !bs) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    bdrv_clear_dirty_bitmap(bitmap, NULL);
ddf19c
-}
ddf19c
-
ddf19c
-void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
ddf19c
-                                   Error **errp)
ddf19c
-{
ddf19c
-    BlockDriverState *bs;
ddf19c
-    BdrvDirtyBitmap *bitmap;
ddf19c
-
ddf19c
-    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
-    if (!bitmap) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    bdrv_enable_dirty_bitmap(bitmap);
ddf19c
-}
ddf19c
-
ddf19c
-void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
ddf19c
-                                    Error **errp)
ddf19c
-{
ddf19c
-    BlockDriverState *bs;
ddf19c
-    BdrvDirtyBitmap *bitmap;
ddf19c
-
ddf19c
-    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
ddf19c
-    if (!bitmap) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    bdrv_disable_dirty_bitmap(bitmap);
ddf19c
-}
ddf19c
-
ddf19c
-BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,
ddf19c
-                                          BlockDirtyBitmapMergeSourceList *bms,
ddf19c
-                                          HBitmap **backup, Error **errp)
ddf19c
-{
ddf19c
-    BlockDriverState *bs;
ddf19c
-    BdrvDirtyBitmap *dst, *src, *anon;
ddf19c
-    BlockDirtyBitmapMergeSourceList *lst;
ddf19c
-    Error *local_err = NULL;
ddf19c
-
ddf19c
-    dst = block_dirty_bitmap_lookup(node, target, &bs, errp);
ddf19c
-    if (!dst) {
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst),
ddf19c
-                                    NULL, errp);
ddf19c
-    if (!anon) {
ddf19c
-        return NULL;
ddf19c
-    }
ddf19c
-
ddf19c
-    for (lst = bms; lst; lst = lst->next) {
ddf19c
-        switch (lst->value->type) {
ddf19c
-            const char *name, *node;
ddf19c
-        case QTYPE_QSTRING:
ddf19c
-            name = lst->value->u.local;
ddf19c
-            src = bdrv_find_dirty_bitmap(bs, name);
ddf19c
-            if (!src) {
ddf19c
-                error_setg(errp, "Dirty bitmap '%s' not found", name);
ddf19c
-                dst = NULL;
ddf19c
-                goto out;
ddf19c
-            }
ddf19c
-            break;
ddf19c
-        case QTYPE_QDICT:
ddf19c
-            node = lst->value->u.external.node;
ddf19c
-            name = lst->value->u.external.name;
ddf19c
-            src = block_dirty_bitmap_lookup(node, name, NULL, errp);
ddf19c
-            if (!src) {
ddf19c
-                dst = NULL;
ddf19c
-                goto out;
ddf19c
-            }
ddf19c
-            break;
ddf19c
-        default:
ddf19c
-            abort();
ddf19c
-        }
ddf19c
-
ddf19c
-        bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err);
ddf19c
-        if (local_err) {
ddf19c
-            error_propagate(errp, local_err);
ddf19c
-            dst = NULL;
ddf19c
-            goto out;
ddf19c
-        }
ddf19c
-    }
ddf19c
-
ddf19c
-    /* Merge into dst; dst is unchanged on failure. */
ddf19c
-    bdrv_merge_dirty_bitmap(dst, anon, backup, errp);
ddf19c
-
ddf19c
- out:
ddf19c
-    bdrv_release_dirty_bitmap(anon);
ddf19c
-    return dst;
ddf19c
-}
ddf19c
-
ddf19c
-void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
ddf19c
-                                  BlockDirtyBitmapMergeSourceList *bitmaps,
ddf19c
-                                  Error **errp)
ddf19c
-{
ddf19c
-    block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
ddf19c
-}
ddf19c
-
ddf19c
 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
ddf19c
                                                               const char *name,
ddf19c
                                                               Error **errp)
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c