Blame SOURCES/kvm-nbd-server-implement-dirty-bitmap-export.patch

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