|
|
26ba25 |
From e058bf5e47af745997f99aa21c003e40f50bddcc Mon Sep 17 00:00:00 2001
|
|
|
26ba25 |
From: John Snow <jsnow@redhat.com>
|
|
|
26ba25 |
Date: Wed, 18 Jul 2018 22:55:03 +0200
|
|
|
26ba25 |
Subject: [PATCH 245/268] nbd/server: implement dirty bitmap export
|
|
|
26ba25 |
|
|
|
26ba25 |
RH-Author: John Snow <jsnow@redhat.com>
|
|
|
26ba25 |
Message-id: <20180718225511.14878-28-jsnow@redhat.com>
|
|
|
26ba25 |
Patchwork-id: 81412
|
|
|
26ba25 |
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 27/35] nbd/server: implement dirty bitmap export
|
|
|
26ba25 |
Bugzilla: 1207657
|
|
|
26ba25 |
RH-Acked-by: Eric Blake <eblake@redhat.com>
|
|
|
26ba25 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
26ba25 |
RH-Acked-by: Fam Zheng <famz@redhat.com>
|
|
|
26ba25 |
|
|
|
26ba25 |
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
|
|
|
26ba25 |
|
|
|
26ba25 |
Handle a new NBD meta namespace: "qemu", and corresponding queries:
|
|
|
26ba25 |
"qemu:dirty-bitmap:<export bitmap name>".
|
|
|
26ba25 |
|
|
|
26ba25 |
With the new metadata context negotiated, BLOCK_STATUS query will reply
|
|
|
26ba25 |
with dirty-bitmap data, converted to extents. The new public function
|
|
|
26ba25 |
nbd_export_bitmap selects which bitmap to export. For now, only one bitmap
|
|
|
26ba25 |
may be exported.
|
|
|
26ba25 |
|
|
|
26ba25 |
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
|
|
|
26ba25 |
Message-Id: <20180609151758.17343-5-vsementsov@virtuozzo.com>
|
|
|
26ba25 |
Reviewed-by: Eric Blake <eblake@redhat.com>
|
|
|
26ba25 |
[eblake: wording tweaks, minor cleanups, additional tracing]
|
|
|
26ba25 |
Signed-off-by: Eric Blake <eblake@redhat.com>
|
|
|
26ba25 |
(cherry picked from commit 3d068aff16d6dbf066328977c5152847a62f2a0a)
|
|
|
26ba25 |
Signed-off-by: John Snow <jsnow@redhat.com>
|
|
|
26ba25 |
|
|
|
26ba25 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
26ba25 |
---
|
|
|
26ba25 |
include/block/nbd.h | 8 +-
|
|
|
26ba25 |
nbd/server.c | 278 +++++++++++++++++++++++++++++++++++++++++++++++-----
|
|
|
26ba25 |
nbd/trace-events | 1 +
|
|
|
26ba25 |
3 files changed, 262 insertions(+), 25 deletions(-)
|
|
|
26ba25 |
|
|
|
26ba25 |
diff --git a/include/block/nbd.h b/include/block/nbd.h
|
|
|
26ba25 |
index fcdcd54..8bb9606 100644
|
|
|
26ba25 |
--- a/include/block/nbd.h
|
|
|
26ba25 |
+++ b/include/block/nbd.h
|
|
|
26ba25 |
@@ -229,11 +229,13 @@ enum {
|
|
|
26ba25 |
#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)
|
|
|
26ba25 |
#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2)
|
|
|
26ba25 |
|
|
|
26ba25 |
-/* Flags for extents (NBDExtent.flags) of NBD_REPLY_TYPE_BLOCK_STATUS,
|
|
|
26ba25 |
- * for base:allocation meta context */
|
|
|
26ba25 |
+/* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */
|
|
|
26ba25 |
#define NBD_STATE_HOLE (1 << 0)
|
|
|
26ba25 |
#define NBD_STATE_ZERO (1 << 1)
|
|
|
26ba25 |
|
|
|
26ba25 |
+/* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */
|
|
|
26ba25 |
+#define NBD_STATE_DIRTY (1 << 0)
|
|
|
26ba25 |
+
|
|
|
26ba25 |
static inline bool nbd_reply_type_is_error(int type)
|
|
|
26ba25 |
{
|
|
|
26ba25 |
return type & (1 << 15);
|
|
|
26ba25 |
@@ -315,6 +317,8 @@ void nbd_client_put(NBDClient *client);
|
|
|
26ba25 |
void nbd_server_start(SocketAddress *addr, const char *tls_creds,
|
|
|
26ba25 |
Error **errp);
|
|
|
26ba25 |
|
|
|
26ba25 |
+void nbd_export_bitmap(NBDExport *exp, const char *bitmap,
|
|
|
26ba25 |
+ const char *bitmap_export_name, Error **errp);
|
|
|
26ba25 |
|
|
|
26ba25 |
/* nbd_read
|
|
|
26ba25 |
* Reads @size bytes from @ioc. Returns 0 on success.
|
|
|
26ba25 |
diff --git a/nbd/server.c b/nbd/server.c
|
|
|
26ba25 |
index 9171cd4..2c2d62c 100644
|
|
|
26ba25 |
--- a/nbd/server.c
|
|
|
26ba25 |
+++ b/nbd/server.c
|
|
|
26ba25 |
@@ -23,6 +23,13 @@
|
|
|
26ba25 |
#include "nbd-internal.h"
|
|
|
26ba25 |
|
|
|
26ba25 |
#define NBD_META_ID_BASE_ALLOCATION 0
|
|
|
26ba25 |
+#define NBD_META_ID_DIRTY_BITMAP 1
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+/* NBD_MAX_BITMAP_EXTENTS: 1 mb of extents data. An empirical
|
|
|
26ba25 |
+ * constant. If an increase is needed, note that the NBD protocol
|
|
|
26ba25 |
+ * recommends no larger than 32 mb, so that the client won't consider
|
|
|
26ba25 |
+ * the reply as a denial of service attack. */
|
|
|
26ba25 |
+#define NBD_MAX_BITMAP_EXTENTS (0x100000 / 8)
|
|
|
26ba25 |
|
|
|
26ba25 |
static int system_errno_to_nbd_errno(int err)
|
|
|
26ba25 |
{
|
|
|
26ba25 |
@@ -80,6 +87,9 @@ struct NBDExport {
|
|
|
26ba25 |
|
|
|
26ba25 |
BlockBackend *eject_notifier_blk;
|
|
|
26ba25 |
Notifier eject_notifier;
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ BdrvDirtyBitmap *export_bitmap;
|
|
|
26ba25 |
+ char *export_bitmap_context;
|
|
|
26ba25 |
};
|
|
|
26ba25 |
|
|
|
26ba25 |
static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
|
|
|
26ba25 |
@@ -92,6 +102,7 @@ typedef struct NBDExportMetaContexts {
|
|
|
26ba25 |
bool valid; /* means that negotiation of the option finished without
|
|
|
26ba25 |
errors */
|
|
|
26ba25 |
bool base_allocation; /* export base:allocation context (block status) */
|
|
|
26ba25 |
+ bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
|
|
|
26ba25 |
} NBDExportMetaContexts;
|
|
|
26ba25 |
|
|
|
26ba25 |
struct NBDClient {
|
|
|
26ba25 |
@@ -814,6 +825,56 @@ static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
|
|
|
26ba25 |
&meta->base_allocation, errp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
+/* nbd_meta_bitmap_query
|
|
|
26ba25 |
+ *
|
|
|
26ba25 |
+ * Handle query to 'qemu:' namespace.
|
|
|
26ba25 |
+ * @len is the amount of text remaining to be read from the current name, after
|
|
|
26ba25 |
+ * the 'qemu:' portion has been stripped.
|
|
|
26ba25 |
+ *
|
|
|
26ba25 |
+ * Return -errno on I/O error, 0 if option was completely handled by
|
|
|
26ba25 |
+ * sending a reply about inconsistent lengths, or 1 on success. */
|
|
|
26ba25 |
+static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
|
|
|
26ba25 |
+ uint32_t len, Error **errp)
|
|
|
26ba25 |
+{
|
|
|
26ba25 |
+ bool dirty_bitmap = false;
|
|
|
26ba25 |
+ size_t dirty_bitmap_len = strlen("dirty-bitmap:");
|
|
|
26ba25 |
+ int ret;
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (!meta->exp->export_bitmap) {
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
|
|
|
26ba25 |
+ return nbd_opt_skip(client, len, errp);
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (len == 0) {
|
|
|
26ba25 |
+ if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
|
|
|
26ba25 |
+ meta->bitmap = true;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_parse("empty");
|
|
|
26ba25 |
+ return 1;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (len < dirty_bitmap_len) {
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
|
|
|
26ba25 |
+ return nbd_opt_skip(client, len, errp);
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ len -= dirty_bitmap_len;
|
|
|
26ba25 |
+ ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
|
|
|
26ba25 |
+ if (ret <= 0) {
|
|
|
26ba25 |
+ return ret;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ if (!dirty_bitmap) {
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
|
|
|
26ba25 |
+ return nbd_opt_skip(client, len, errp);
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ return nbd_meta_empty_or_pattern(
|
|
|
26ba25 |
+ client, meta->exp->export_bitmap_context +
|
|
|
26ba25 |
+ strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
|
|
|
26ba25 |
+}
|
|
|
26ba25 |
+
|
|
|
26ba25 |
/* nbd_negotiate_meta_query
|
|
|
26ba25 |
*
|
|
|
26ba25 |
* Parse namespace name and call corresponding function to parse body of the
|
|
|
26ba25 |
@@ -829,9 +890,14 @@ static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
|
|
|
26ba25 |
static int nbd_negotiate_meta_query(NBDClient *client,
|
|
|
26ba25 |
NBDExportMetaContexts *meta, Error **errp)
|
|
|
26ba25 |
{
|
|
|
26ba25 |
+ /*
|
|
|
26ba25 |
+ * Both 'qemu' and 'base' namespaces have length = 5 including a
|
|
|
26ba25 |
+ * colon. If another length namespace is later introduced, this
|
|
|
26ba25 |
+ * should certainly be refactored.
|
|
|
26ba25 |
+ */
|
|
|
26ba25 |
int ret;
|
|
|
26ba25 |
- char query[sizeof("base:") - 1];
|
|
|
26ba25 |
- size_t baselen = strlen("base:");
|
|
|
26ba25 |
+ size_t ns_len = 5;
|
|
|
26ba25 |
+ char ns[5];
|
|
|
26ba25 |
uint32_t len;
|
|
|
26ba25 |
|
|
|
26ba25 |
ret = nbd_opt_read(client, &len, sizeof(len), errp);
|
|
|
26ba25 |
@@ -840,25 +906,27 @@ static int nbd_negotiate_meta_query(NBDClient *client,
|
|
|
26ba25 |
}
|
|
|
26ba25 |
cpu_to_be32s(&len;;
|
|
|
26ba25 |
|
|
|
26ba25 |
- /* The only supported namespace for now is 'base'. So query should start
|
|
|
26ba25 |
- * with 'base:'. Otherwise, we can ignore it and skip the remainder. */
|
|
|
26ba25 |
- if (len < baselen) {
|
|
|
26ba25 |
+ if (len < ns_len) {
|
|
|
26ba25 |
trace_nbd_negotiate_meta_query_skip("length too short");
|
|
|
26ba25 |
return nbd_opt_skip(client, len, errp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
- len -= baselen;
|
|
|
26ba25 |
- ret = nbd_opt_read(client, query, baselen, errp);
|
|
|
26ba25 |
+ len -= ns_len;
|
|
|
26ba25 |
+ ret = nbd_opt_read(client, ns, ns_len, errp);
|
|
|
26ba25 |
if (ret <= 0) {
|
|
|
26ba25 |
return ret;
|
|
|
26ba25 |
}
|
|
|
26ba25 |
- if (strncmp(query, "base:", baselen) != 0) {
|
|
|
26ba25 |
- trace_nbd_negotiate_meta_query_skip("not for base: namespace");
|
|
|
26ba25 |
- return nbd_opt_skip(client, len, errp);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (!strncmp(ns, "base:", ns_len)) {
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_parse("base:");
|
|
|
26ba25 |
+ return nbd_meta_base_query(client, meta, len, errp);
|
|
|
26ba25 |
+ } else if (!strncmp(ns, "qemu:", ns_len)) {
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_parse("qemu:");
|
|
|
26ba25 |
+ return nbd_meta_qemu_query(client, meta, len, errp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
- trace_nbd_negotiate_meta_query_parse("base:");
|
|
|
26ba25 |
- return nbd_meta_base_query(client, meta, len, errp);
|
|
|
26ba25 |
+ trace_nbd_negotiate_meta_query_skip("unknown namespace");
|
|
|
26ba25 |
+ return nbd_opt_skip(client, len, errp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
/* nbd_negotiate_meta_queries
|
|
|
26ba25 |
@@ -928,6 +996,16 @@ static int nbd_negotiate_meta_queries(NBDClient *client,
|
|
|
26ba25 |
}
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
+ if (meta->bitmap) {
|
|
|
26ba25 |
+ ret = nbd_negotiate_send_meta_context(client,
|
|
|
26ba25 |
+ meta->exp->export_bitmap_context,
|
|
|
26ba25 |
+ NBD_META_ID_DIRTY_BITMAP,
|
|
|
26ba25 |
+ errp);
|
|
|
26ba25 |
+ if (ret < 0) {
|
|
|
26ba25 |
+ return ret;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
|
|
|
26ba25 |
if (ret == 0) {
|
|
|
26ba25 |
meta->valid = true;
|
|
|
26ba25 |
@@ -1556,6 +1634,11 @@ void nbd_export_put(NBDExport *exp)
|
|
|
26ba25 |
exp->blk = NULL;
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
+ if (exp->export_bitmap) {
|
|
|
26ba25 |
+ bdrv_dirty_bitmap_set_qmp_locked(exp->export_bitmap, false);
|
|
|
26ba25 |
+ g_free(exp->export_bitmap_context);
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
g_free(exp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
}
|
|
|
26ba25 |
@@ -1797,9 +1880,15 @@ static int blockstatus_to_extent_be(BlockDriverState *bs, uint64_t offset,
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
/* nbd_co_send_extents
|
|
|
26ba25 |
- * @extents should be in big-endian */
|
|
|
26ba25 |
+ *
|
|
|
26ba25 |
+ * @length is only for tracing purposes (and may be smaller or larger
|
|
|
26ba25 |
+ * than the client's original request). @last controls whether
|
|
|
26ba25 |
+ * NBD_REPLY_FLAG_DONE is sent. @extents should already be in
|
|
|
26ba25 |
+ * big-endian format.
|
|
|
26ba25 |
+ */
|
|
|
26ba25 |
static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
|
|
|
26ba25 |
- NBDExtent *extents, unsigned nb_extents,
|
|
|
26ba25 |
+ NBDExtent *extents, unsigned int nb_extents,
|
|
|
26ba25 |
+ uint64_t length, bool last,
|
|
|
26ba25 |
uint32_t context_id, Error **errp)
|
|
|
26ba25 |
{
|
|
|
26ba25 |
NBDStructuredMeta chunk;
|
|
|
26ba25 |
@@ -1809,7 +1898,9 @@ static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
|
|
|
26ba25 |
{.iov_base = extents, .iov_len = nb_extents * sizeof(extents[0])}
|
|
|
26ba25 |
};
|
|
|
26ba25 |
|
|
|
26ba25 |
- set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_BLOCK_STATUS,
|
|
|
26ba25 |
+ trace_nbd_co_send_extents(handle, nb_extents, context_id, length, last);
|
|
|
26ba25 |
+ set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
|
|
|
26ba25 |
+ NBD_REPLY_TYPE_BLOCK_STATUS,
|
|
|
26ba25 |
handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
|
|
|
26ba25 |
stl_be_p(&chunk.context_id, context_id);
|
|
|
26ba25 |
|
|
|
26ba25 |
@@ -1819,8 +1910,8 @@ static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
|
|
|
26ba25 |
/* Get block status from the exported device and send it to the client */
|
|
|
26ba25 |
static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
|
|
|
26ba25 |
BlockDriverState *bs, uint64_t offset,
|
|
|
26ba25 |
- uint64_t length, uint32_t context_id,
|
|
|
26ba25 |
- Error **errp)
|
|
|
26ba25 |
+ uint64_t length, bool last,
|
|
|
26ba25 |
+ uint32_t context_id, Error **errp)
|
|
|
26ba25 |
{
|
|
|
26ba25 |
int ret;
|
|
|
26ba25 |
NBDExtent extent;
|
|
|
26ba25 |
@@ -1831,7 +1922,84 @@ static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
|
|
|
26ba25 |
client, handle, -ret, "can't get block status", errp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
- return nbd_co_send_extents(client, handle, &extent, 1, context_id, errp);
|
|
|
26ba25 |
+ return nbd_co_send_extents(client, handle, &extent, 1, length, last,
|
|
|
26ba25 |
+ context_id, errp);
|
|
|
26ba25 |
+}
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+/*
|
|
|
26ba25 |
+ * Populate @extents from a dirty bitmap. Unless @dont_fragment, the
|
|
|
26ba25 |
+ * final extent may exceed the original @length. Store in @length the
|
|
|
26ba25 |
+ * byte length encoded (which may be smaller or larger than the
|
|
|
26ba25 |
+ * original), and return the number of extents used.
|
|
|
26ba25 |
+ */
|
|
|
26ba25 |
+static unsigned int bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t offset,
|
|
|
26ba25 |
+ uint64_t *length, NBDExtent *extents,
|
|
|
26ba25 |
+ unsigned int nb_extents,
|
|
|
26ba25 |
+ bool dont_fragment)
|
|
|
26ba25 |
+{
|
|
|
26ba25 |
+ uint64_t begin = offset, end;
|
|
|
26ba25 |
+ uint64_t overall_end = offset + *length;
|
|
|
26ba25 |
+ unsigned int i = 0;
|
|
|
26ba25 |
+ BdrvDirtyBitmapIter *it;
|
|
|
26ba25 |
+ bool dirty;
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ bdrv_dirty_bitmap_lock(bitmap);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ it = bdrv_dirty_iter_new(bitmap);
|
|
|
26ba25 |
+ dirty = bdrv_get_dirty_locked(NULL, bitmap, offset);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ assert(begin < overall_end && nb_extents);
|
|
|
26ba25 |
+ while (begin < overall_end && i < nb_extents) {
|
|
|
26ba25 |
+ if (dirty) {
|
|
|
26ba25 |
+ end = bdrv_dirty_bitmap_next_zero(bitmap, begin);
|
|
|
26ba25 |
+ } else {
|
|
|
26ba25 |
+ bdrv_set_dirty_iter(it, begin);
|
|
|
26ba25 |
+ end = bdrv_dirty_iter_next(it);
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ if (end == -1 || end - begin > UINT32_MAX) {
|
|
|
26ba25 |
+ /* Cap to an aligned value < 4G beyond begin. */
|
|
|
26ba25 |
+ end = MIN(bdrv_dirty_bitmap_size(bitmap),
|
|
|
26ba25 |
+ begin + UINT32_MAX + 1 -
|
|
|
26ba25 |
+ bdrv_dirty_bitmap_granularity(bitmap));
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ if (dont_fragment && end > overall_end) {
|
|
|
26ba25 |
+ end = overall_end;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ extents[i].length = cpu_to_be32(end - begin);
|
|
|
26ba25 |
+ extents[i].flags = cpu_to_be32(dirty ? NBD_STATE_DIRTY : 0);
|
|
|
26ba25 |
+ i++;
|
|
|
26ba25 |
+ begin = end;
|
|
|
26ba25 |
+ dirty = !dirty;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ bdrv_dirty_iter_free(it);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ bdrv_dirty_bitmap_unlock(bitmap);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ *length = end - offset;
|
|
|
26ba25 |
+ return i;
|
|
|
26ba25 |
+}
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
|
|
|
26ba25 |
+ BdrvDirtyBitmap *bitmap, uint64_t offset,
|
|
|
26ba25 |
+ uint32_t length, bool dont_fragment, bool last,
|
|
|
26ba25 |
+ uint32_t context_id, Error **errp)
|
|
|
26ba25 |
+{
|
|
|
26ba25 |
+ int ret;
|
|
|
26ba25 |
+ unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BITMAP_EXTENTS;
|
|
|
26ba25 |
+ NBDExtent *extents = g_new(NBDExtent, nb_extents);
|
|
|
26ba25 |
+ uint64_t final_length = length;
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ nb_extents = bitmap_to_extents(bitmap, offset, &final_length, extents,
|
|
|
26ba25 |
+ nb_extents, dont_fragment);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ ret = nbd_co_send_extents(client, handle, extents, nb_extents,
|
|
|
26ba25 |
+ final_length, last, context_id, errp);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ g_free(extents);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ return ret;
|
|
|
26ba25 |
}
|
|
|
26ba25 |
|
|
|
26ba25 |
/* nbd_co_receive_request
|
|
|
26ba25 |
@@ -2051,11 +2219,34 @@ static coroutine_fn int nbd_handle_request(NBDClient *client,
|
|
|
26ba25 |
return nbd_send_generic_reply(client, request->handle, -EINVAL,
|
|
|
26ba25 |
"need non-zero length", errp);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
- if (client->export_meta.valid && client->export_meta.base_allocation) {
|
|
|
26ba25 |
- return nbd_co_send_block_status(client, request->handle,
|
|
|
26ba25 |
- blk_bs(exp->blk), request->from,
|
|
|
26ba25 |
- request->len,
|
|
|
26ba25 |
- NBD_META_ID_BASE_ALLOCATION, errp);
|
|
|
26ba25 |
+ if (client->export_meta.valid &&
|
|
|
26ba25 |
+ (client->export_meta.base_allocation ||
|
|
|
26ba25 |
+ client->export_meta.bitmap))
|
|
|
26ba25 |
+ {
|
|
|
26ba25 |
+ if (client->export_meta.base_allocation) {
|
|
|
26ba25 |
+ ret = nbd_co_send_block_status(client, request->handle,
|
|
|
26ba25 |
+ blk_bs(exp->blk), request->from,
|
|
|
26ba25 |
+ request->len,
|
|
|
26ba25 |
+ !client->export_meta.bitmap,
|
|
|
26ba25 |
+ NBD_META_ID_BASE_ALLOCATION,
|
|
|
26ba25 |
+ errp);
|
|
|
26ba25 |
+ if (ret < 0) {
|
|
|
26ba25 |
+ return ret;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (client->export_meta.bitmap) {
|
|
|
26ba25 |
+ ret = nbd_co_send_bitmap(client, request->handle,
|
|
|
26ba25 |
+ client->exp->export_bitmap,
|
|
|
26ba25 |
+ request->from, request->len,
|
|
|
26ba25 |
+ request->flags & NBD_CMD_FLAG_REQ_ONE,
|
|
|
26ba25 |
+ true, NBD_META_ID_DIRTY_BITMAP, errp);
|
|
|
26ba25 |
+ if (ret < 0) {
|
|
|
26ba25 |
+ return ret;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ return ret;
|
|
|
26ba25 |
} else {
|
|
|
26ba25 |
return nbd_send_generic_reply(client, request->handle, -EINVAL,
|
|
|
26ba25 |
"CMD_BLOCK_STATUS not negotiated",
|
|
|
26ba25 |
@@ -2207,3 +2398,44 @@ void nbd_client_new(NBDExport *exp,
|
|
|
26ba25 |
co = qemu_coroutine_create(nbd_co_client_start, client);
|
|
|
26ba25 |
qemu_coroutine_enter(co);
|
|
|
26ba25 |
}
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+void nbd_export_bitmap(NBDExport *exp, const char *bitmap,
|
|
|
26ba25 |
+ const char *bitmap_export_name, Error **errp)
|
|
|
26ba25 |
+{
|
|
|
26ba25 |
+ BdrvDirtyBitmap *bm = NULL;
|
|
|
26ba25 |
+ BlockDriverState *bs = blk_bs(exp->blk);
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (exp->export_bitmap) {
|
|
|
26ba25 |
+ error_setg(errp, "Export bitmap is already set");
|
|
|
26ba25 |
+ return;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ while (true) {
|
|
|
26ba25 |
+ bm = bdrv_find_dirty_bitmap(bs, bitmap);
|
|
|
26ba25 |
+ if (bm != NULL || bs->backing == NULL) {
|
|
|
26ba25 |
+ break;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ bs = bs->backing->bs;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (bm == NULL) {
|
|
|
26ba25 |
+ error_setg(errp, "Bitmap '%s' is not found", bitmap);
|
|
|
26ba25 |
+ return;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (bdrv_dirty_bitmap_enabled(bm)) {
|
|
|
26ba25 |
+ error_setg(errp, "Bitmap '%s' is enabled", bitmap);
|
|
|
26ba25 |
+ return;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ if (bdrv_dirty_bitmap_qmp_locked(bm)) {
|
|
|
26ba25 |
+ error_setg(errp, "Bitmap '%s' is locked", bitmap);
|
|
|
26ba25 |
+ return;
|
|
|
26ba25 |
+ }
|
|
|
26ba25 |
+
|
|
|
26ba25 |
+ bdrv_dirty_bitmap_set_qmp_locked(bm, true);
|
|
|
26ba25 |
+ exp->export_bitmap = bm;
|
|
|
26ba25 |
+ exp->export_bitmap_context =
|
|
|
26ba25 |
+ g_strdup_printf("qemu:dirty-bitmap:%s", bitmap_export_name);
|
|
|
26ba25 |
+}
|
|
|
26ba25 |
diff --git a/nbd/trace-events b/nbd/trace-events
|
|
|
26ba25 |
index dee081e..5e1d4af 100644
|
|
|
26ba25 |
--- a/nbd/trace-events
|
|
|
26ba25 |
+++ b/nbd/trace-events
|
|
|
26ba25 |
@@ -64,6 +64,7 @@ nbd_co_send_simple_reply(uint64_t handle, uint32_t error, const char *errname, i
|
|
|
26ba25 |
nbd_co_send_structured_done(uint64_t handle) "Send structured reply done: handle = %" PRIu64
|
|
|
26ba25 |
nbd_co_send_structured_read(uint64_t handle, uint64_t offset, void *data, size_t size) "Send structured read data reply: handle = %" PRIu64 ", offset = %" PRIu64 ", data = %p, len = %zu"
|
|
|
26ba25 |
nbd_co_send_structured_read_hole(uint64_t handle, uint64_t offset, size_t size) "Send structured read hole reply: handle = %" PRIu64 ", offset = %" PRIu64 ", len = %zu"
|
|
|
26ba25 |
+nbd_co_send_extents(uint64_t handle, unsigned int extents, uint32_t id, uint64_t length, int last) "Send block status reply: handle = %" PRIu64 ", extents = %u, context = %d (extents cover %" PRIu64 " bytes, last chunk = %d)"
|
|
|
26ba25 |
nbd_co_send_structured_error(uint64_t handle, int err, const char *errname, const char *msg) "Send structured error reply: handle = %" PRIu64 ", error = %d (%s), msg = '%s'"
|
|
|
26ba25 |
nbd_co_receive_request_decode_type(uint64_t handle, uint16_t type, const char *name) "Decoding type: handle = %" PRIu64 ", type = %" PRIu16 " (%s)"
|
|
|
26ba25 |
nbd_co_receive_request_payload_received(uint64_t handle, uint32_t len) "Payload received: handle = %" PRIu64 ", len = %" PRIu32
|
|
|
26ba25 |
--
|
|
|
26ba25 |
1.8.3.1
|
|
|
26ba25 |
|