Blame SOURCES/kvm-nbd-Merge-nbd_export_bitmap-into-nbd_export_new.patch

383d26
From 13ac133e5fbde2559899dc3717b2a2c20ce9aeb1 Mon Sep 17 00:00:00 2001
383d26
From: John Snow <jsnow@redhat.com>
383d26
Date: Wed, 27 Mar 2019 17:22:32 +0100
383d26
Subject: [PATCH 093/163] nbd: Merge nbd_export_bitmap into nbd_export_new
383d26
383d26
RH-Author: John Snow <jsnow@redhat.com>
383d26
Message-id: <20190327172308.31077-20-jsnow@redhat.com>
383d26
Patchwork-id: 85190
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 19/55] nbd: Merge nbd_export_bitmap into nbd_export_new
383d26
Bugzilla: 1691009
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
From: Eric Blake <eblake@redhat.com>
383d26
383d26
We only have one caller that wants to export a bitmap name,
383d26
which it does right after creation of the export. But there is
383d26
still a brief window of time where an NBD client could see the
383d26
export but not the dirty bitmap, which a robust client would
383d26
have to interpret as meaning the entire image should be treated
383d26
as dirty.  Better is to eliminate the window entirely, by
383d26
inlining nbd_export_bitmap() into nbd_export_new(), and refusing
383d26
to create the bitmap in the first place if the requested bitmap
383d26
can't be located.
383d26
383d26
We also no longer need logic for setting a different bitmap
383d26
name compared to the bitmap being exported.
383d26
383d26
Signed-off-by: Eric Blake <eblake@redhat.com>
383d26
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
383d26
Message-Id: <20190111194720.15671-8-eblake@redhat.com>
383d26
(cherry picked from commit 678ba275c77b5b12f3bc9bb369a1b824fc9f679f)
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 blockdev-nbd.c      | 11 +------
383d26
 include/block/nbd.h |  9 ++----
383d26
 nbd/server.c        | 87 ++++++++++++++++++++++++-----------------------------
383d26
 qemu-nbd.c          |  5 +--
383d26
 4 files changed, 47 insertions(+), 65 deletions(-)
383d26
383d26
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
383d26
index cd86b38..c76d541 100644
383d26
--- a/blockdev-nbd.c
383d26
+++ b/blockdev-nbd.c
383d26
@@ -175,7 +175,7 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
383d26
         writable = false;
383d26
     }
383d26
 
383d26
-    exp = nbd_export_new(bs, 0, -1, name, NULL,
383d26
+    exp = nbd_export_new(bs, 0, -1, name, NULL, bitmap,
383d26
                          writable ? 0 : NBD_FLAG_READ_ONLY,
383d26
                          NULL, false, on_eject_blk, errp);
383d26
     if (!exp) {
383d26
@@ -186,15 +186,6 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
383d26
      * our only way of accessing it is through nbd_export_find(), so we can drop
383d26
      * the strong reference that is @exp. */
383d26
     nbd_export_put(exp);
383d26
-
383d26
-    if (has_bitmap) {
383d26
-        Error *err = NULL;
383d26
-        nbd_export_bitmap(exp, bitmap, bitmap, &err;;
383d26
-        if (err) {
383d26
-            error_propagate(errp, err);
383d26
-            nbd_export_remove(exp, NBD_SERVER_REMOVE_MODE_HARD, NULL);
383d26
-        }
383d26
-    }
383d26
 }
383d26
 
383d26
 void qmp_nbd_server_remove(const char *name,
383d26
diff --git a/include/block/nbd.h b/include/block/nbd.h
383d26
index 2f9a2ae..1971b55 100644
383d26
--- a/include/block/nbd.h
383d26
+++ b/include/block/nbd.h
383d26
@@ -296,9 +296,9 @@ typedef struct NBDClient NBDClient;
383d26
 
383d26
 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
383d26
                           const char *name, const char *description,
383d26
-                          uint16_t nbdflags, void (*close)(NBDExport *),
383d26
-                          bool writethrough, BlockBackend *on_eject_blk,
383d26
-                          Error **errp);
383d26
+                          const char *bitmap, uint16_t nbdflags,
383d26
+                          void (*close)(NBDExport *), bool writethrough,
383d26
+                          BlockBackend *on_eject_blk, Error **errp);
383d26
 void nbd_export_close(NBDExport *exp);
383d26
 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp);
383d26
 void nbd_export_get(NBDExport *exp);
383d26
@@ -319,9 +319,6 @@ void nbd_client_put(NBDClient *client);
383d26
 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
383d26
                       Error **errp);
383d26
 
383d26
-void nbd_export_bitmap(NBDExport *exp, const char *bitmap,
383d26
-                       const char *bitmap_export_name, Error **errp);
383d26
-
383d26
 /* nbd_read
383d26
  * Reads @size bytes from @ioc. Returns 0 on success.
383d26
  */
383d26
diff --git a/nbd/server.c b/nbd/server.c
383d26
index 6c02b57..6b13601 100644
383d26
--- a/nbd/server.c
383d26
+++ b/nbd/server.c
383d26
@@ -1457,9 +1457,9 @@ static void nbd_eject_notifier(Notifier *n, void *data)
383d26
 
383d26
 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
383d26
                           const char *name, const char *description,
383d26
-                          uint16_t nbdflags, void (*close)(NBDExport *),
383d26
-                          bool writethrough, BlockBackend *on_eject_blk,
383d26
-                          Error **errp)
383d26
+                          const char *bitmap, uint16_t nbdflags,
383d26
+                          void (*close)(NBDExport *), bool writethrough,
383d26
+                          BlockBackend *on_eject_blk, Error **errp)
383d26
 {
383d26
     AioContext *ctx;
383d26
     BlockBackend *blk;
383d26
@@ -1507,6 +1507,43 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
383d26
     }
383d26
     exp->size -= exp->size % BDRV_SECTOR_SIZE;
383d26
 
383d26
+    if (bitmap) {
383d26
+        BdrvDirtyBitmap *bm = NULL;
383d26
+        BlockDriverState *bs = blk_bs(blk);
383d26
+
383d26
+        while (true) {
383d26
+            bm = bdrv_find_dirty_bitmap(bs, bitmap);
383d26
+            if (bm != NULL || bs->backing == NULL) {
383d26
+                break;
383d26
+            }
383d26
+
383d26
+            bs = bs->backing->bs;
383d26
+        }
383d26
+
383d26
+        if (bm == NULL) {
383d26
+            error_setg(errp, "Bitmap '%s' is not found", bitmap);
383d26
+            goto fail;
383d26
+        }
383d26
+
383d26
+        if ((nbdflags & NBD_FLAG_READ_ONLY) && bdrv_is_writable(bs) &&
383d26
+            bdrv_dirty_bitmap_enabled(bm)) {
383d26
+            error_setg(errp,
383d26
+                       "Enabled bitmap '%s' incompatible with readonly export",
383d26
+                       bitmap);
383d26
+            goto fail;
383d26
+        }
383d26
+
383d26
+        if (bdrv_dirty_bitmap_user_locked(bm)) {
383d26
+            error_setg(errp, "Bitmap '%s' is in use", bitmap);
383d26
+            goto fail;
383d26
+        }
383d26
+
383d26
+        bdrv_dirty_bitmap_set_qmp_locked(bm, true);
383d26
+        exp->export_bitmap = bm;
383d26
+        exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
383d26
+                                                     bitmap);
383d26
+    }
383d26
+
383d26
     exp->close = close;
383d26
     exp->ctx = blk_get_aio_context(blk);
383d26
     blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
383d26
@@ -2424,47 +2461,3 @@ void nbd_client_new(QIOChannelSocket *sioc,
383d26
     co = qemu_coroutine_create(nbd_co_client_start, client);
383d26
     qemu_coroutine_enter(co);
383d26
 }
383d26
-
383d26
-void nbd_export_bitmap(NBDExport *exp, const char *bitmap,
383d26
-                       const char *bitmap_export_name, Error **errp)
383d26
-{
383d26
-    BdrvDirtyBitmap *bm = NULL;
383d26
-    BlockDriverState *bs = blk_bs(exp->blk);
383d26
-
383d26
-    if (exp->export_bitmap) {
383d26
-        error_setg(errp, "Export bitmap is already set");
383d26
-        return;
383d26
-    }
383d26
-
383d26
-    while (true) {
383d26
-        bm = bdrv_find_dirty_bitmap(bs, bitmap);
383d26
-        if (bm != NULL || bs->backing == NULL) {
383d26
-            break;
383d26
-        }
383d26
-
383d26
-        bs = bs->backing->bs;
383d26
-    }
383d26
-
383d26
-    if (bm == NULL) {
383d26
-        error_setg(errp, "Bitmap '%s' is not found", bitmap);
383d26
-        return;
383d26
-    }
383d26
-
383d26
-    if ((exp->nbdflags & NBD_FLAG_READ_ONLY) && bdrv_is_writable(bs) &&
383d26
-        bdrv_dirty_bitmap_enabled(bm)) {
383d26
-        error_setg(errp,
383d26
-                   "Enabled bitmap '%s' incompatible with readonly export",
383d26
-                   bitmap);
383d26
-        return;
383d26
-    }
383d26
-
383d26
-    if (bdrv_dirty_bitmap_user_locked(bm)) {
383d26
-        error_setg(errp, "Bitmap '%s' is in use", bitmap);
383d26
-        return;
383d26
-    }
383d26
-
383d26
-    bdrv_dirty_bitmap_set_qmp_locked(bm, true);
383d26
-    exp->export_bitmap = bm;
383d26
-    exp->export_bitmap_context =
383d26
-            g_strdup_printf("qemu:dirty-bitmap:%s", bitmap_export_name);
383d26
-}
383d26
diff --git a/qemu-nbd.c b/qemu-nbd.c
383d26
index c85aee4..ac4c958 100644
383d26
--- a/qemu-nbd.c
383d26
+++ b/qemu-nbd.c
383d26
@@ -1018,8 +1018,9 @@ int main(int argc, char **argv)
383d26
     }
383d26
 
383d26
     export = nbd_export_new(bs, dev_offset, fd_size, export_name,
383d26
-                            export_description, nbdflags, nbd_export_closed,
383d26
-                            writethrough, NULL, &error_fatal);
383d26
+                            export_description, NULL, nbdflags,
383d26
+                            nbd_export_closed, writethrough, NULL,
383d26
+                            &error_fatal);
383d26
 
383d26
     if (device) {
383d26
 #if HAVE_NBD_DEVICE
383d26
-- 
383d26
1.8.3.1
383d26