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

7711c0
From 629f726f14a40cfe4edf74ec0d213eed935e5a79 Mon Sep 17 00:00:00 2001
7711c0
From: John Snow <jsnow@redhat.com>
7711c0
Date: Wed, 27 Mar 2019 17:22:29 +0100
7711c0
Subject: [PATCH 090/163] nbd: Merge nbd_export_set_name into nbd_export_new
7711c0
7711c0
RH-Author: John Snow <jsnow@redhat.com>
7711c0
Message-id: <20190327172308.31077-17-jsnow@redhat.com>
7711c0
Patchwork-id: 85171
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 16/55] nbd: Merge nbd_export_set_name into nbd_export_new
7711c0
Bugzilla: 1691009
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
7711c0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
7711c0
From: Eric Blake <eblake@redhat.com>
7711c0
7711c0
The existing NBD code had a weird split where nbd_export_new()
7711c0
created an export but did not add it to the list of exported
7711c0
names until a later nbd_export_set_name() came along and grabbed
7711c0
a second reference on the object; later, the first call to
7711c0
nbd_export_close() drops the second reference while removing
7711c0
the export from the list.  This is in part because the QAPI
7711c0
NbdServerRemoveNode enum documents the possibility of adding a
7711c0
mode where we could do a soft disconnect: preventing new clients,
7711c0
but waiting for existing clients to gracefully quit, based on
7711c0
the mode used when calling nbd_export_close().
7711c0
7711c0
But in spite of all that, note that we never change the name of
7711c0
an NBD export while it is exposed, which means it is easier to
7711c0
just inline the process of setting the name as part of creating
7711c0
the export.
7711c0
7711c0
Inline the contents of nbd_export_set_name() and
7711c0
nbd_export_set_description() into the two points in an export
7711c0
lifecycle where they matter, then adjust both callers to pass
7711c0
the name up front.  Note that for creation, all callers pass a
7711c0
non-NULL name, (passing NULL at creation was for old style
7711c0
servers, but we removed support for that in commit 7f7dfe2a),
7711c0
so we can add an assert and do things unconditionally; but for
7711c0
cleanup, because of the dual nature of nbd_export_close(), we
7711c0
still have to be careful to avoid use-after-free.  Along the
7711c0
way, add a comment reminding ourselves of the potential of
7711c0
adding a middle mode disconnect.
7711c0
7711c0
Signed-off-by: Eric Blake <eblake@redhat.com>
7711c0
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7711c0
Message-Id: <20190111194720.15671-5-eblake@redhat.com>
7711c0
(cherry picked from commit 3fa4c76590569f9dc128beb3eee65aaefcb6321e)
7711c0
Signed-off-by: John Snow <jsnow@redhat.com>
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 blockdev-nbd.c      |  5 ++---
7711c0
 include/block/nbd.h |  3 +--
7711c0
 nbd/server.c        | 52 +++++++++++++++++++++++-----------------------------
7711c0
 qemu-nbd.c          |  8 +++-----
7711c0
 4 files changed, 29 insertions(+), 39 deletions(-)
7711c0
7711c0
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
7711c0
index ca58491..582ffde 100644
7711c0
--- a/blockdev-nbd.c
7711c0
+++ b/blockdev-nbd.c
7711c0
@@ -174,14 +174,13 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
7711c0
         writable = false;
7711c0
     }
7711c0
 
7711c0
-    exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY,
7711c0
+    exp = nbd_export_new(bs, 0, -1, name, NULL,
7711c0
+                         writable ? 0 : NBD_FLAG_READ_ONLY,
7711c0
                          NULL, false, on_eject_blk, errp);
7711c0
     if (!exp) {
7711c0
         return;
7711c0
     }
7711c0
 
7711c0
-    nbd_export_set_name(exp, name);
7711c0
-
7711c0
     /* The list of named exports has a strong reference to this export now and
7711c0
      * our only way of accessing it is through nbd_export_find(), so we can drop
7711c0
      * the strong reference that is @exp. */
7711c0
diff --git a/include/block/nbd.h b/include/block/nbd.h
7711c0
index 65402d3..2f9a2ae 100644
7711c0
--- a/include/block/nbd.h
7711c0
+++ b/include/block/nbd.h
7711c0
@@ -295,6 +295,7 @@ typedef struct NBDExport NBDExport;
7711c0
 typedef struct NBDClient NBDClient;
7711c0
 
7711c0
 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7711c0
+                          const char *name, const char *description,
7711c0
                           uint16_t nbdflags, void (*close)(NBDExport *),
7711c0
                           bool writethrough, BlockBackend *on_eject_blk,
7711c0
                           Error **errp);
7711c0
@@ -306,8 +307,6 @@ void nbd_export_put(NBDExport *exp);
7711c0
 BlockBackend *nbd_export_get_blockdev(NBDExport *exp);
7711c0
 
7711c0
 NBDExport *nbd_export_find(const char *name);
7711c0
-void nbd_export_set_name(NBDExport *exp, const char *name);
7711c0
-void nbd_export_set_description(NBDExport *exp, const char *description);
7711c0
 void nbd_export_close_all(void);
7711c0
 
7711c0
 void nbd_client_new(QIOChannelSocket *sioc,
7711c0
diff --git a/nbd/server.c b/nbd/server.c
7711c0
index c0f2e85..6c02b57 100644
7711c0
--- a/nbd/server.c
7711c0
+++ b/nbd/server.c
7711c0
@@ -1456,6 +1456,7 @@ static void nbd_eject_notifier(Notifier *n, void *data)
7711c0
 }
7711c0
 
7711c0
 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7711c0
+                          const char *name, const char *description,
7711c0
                           uint16_t nbdflags, void (*close)(NBDExport *),
7711c0
                           bool writethrough, BlockBackend *on_eject_blk,
7711c0
                           Error **errp)
7711c0
@@ -1471,6 +1472,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7711c0
      * that BDRV_O_INACTIVE is cleared and the image is ready for write
7711c0
      * access since the export could be available before migration handover.
7711c0
      */
7711c0
+    assert(name);
7711c0
     ctx = bdrv_get_aio_context(bs);
7711c0
     aio_context_acquire(ctx);
7711c0
     bdrv_invalidate_cache(bs, NULL);
7711c0
@@ -1494,6 +1496,8 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7711c0
     QTAILQ_INIT(&exp->clients);
7711c0
     exp->blk = blk;
7711c0
     exp->dev_offset = dev_offset;
7711c0
+    exp->name = g_strdup(name);
7711c0
+    exp->description = g_strdup(description);
7711c0
     exp->nbdflags = nbdflags;
7711c0
     exp->size = size < 0 ? blk_getlength(blk) : size;
7711c0
     if (exp->size < 0) {
7711c0
@@ -1513,10 +1517,14 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7711c0
         exp->eject_notifier.notify = nbd_eject_notifier;
7711c0
         blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
7711c0
     }
7711c0
+    QTAILQ_INSERT_TAIL(&exports, exp, next);
7711c0
+    nbd_export_get(exp);
7711c0
     return exp;
7711c0
 
7711c0
 fail:
7711c0
     blk_unref(blk);
7711c0
+    g_free(exp->name);
7711c0
+    g_free(exp->description);
7711c0
     g_free(exp);
7711c0
     return NULL;
7711c0
 }
7711c0
@@ -1533,43 +1541,29 @@ NBDExport *nbd_export_find(const char *name)
7711c0
     return NULL;
7711c0
 }
7711c0
 
7711c0
-void nbd_export_set_name(NBDExport *exp, const char *name)
7711c0
-{
7711c0
-    if (exp->name == name) {
7711c0
-        return;
7711c0
-    }
7711c0
-
7711c0
-    nbd_export_get(exp);
7711c0
-    if (exp->name != NULL) {
7711c0
-        g_free(exp->name);
7711c0
-        exp->name = NULL;
7711c0
-        QTAILQ_REMOVE(&exports, exp, next);
7711c0
-        nbd_export_put(exp);
7711c0
-    }
7711c0
-    if (name != NULL) {
7711c0
-        nbd_export_get(exp);
7711c0
-        exp->name = g_strdup(name);
7711c0
-        QTAILQ_INSERT_TAIL(&exports, exp, next);
7711c0
-    }
7711c0
-    nbd_export_put(exp);
7711c0
-}
7711c0
-
7711c0
-void nbd_export_set_description(NBDExport *exp, const char *description)
7711c0
-{
7711c0
-    g_free(exp->description);
7711c0
-    exp->description = g_strdup(description);
7711c0
-}
7711c0
-
7711c0
 void nbd_export_close(NBDExport *exp)
7711c0
 {
7711c0
     NBDClient *client, *next;
7711c0
 
7711c0
     nbd_export_get(exp);
7711c0
+    /*
7711c0
+     * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
7711c0
+     * close mode that stops advertising the export to new clients but
7711c0
+     * still permits existing clients to run to completion? Because of
7711c0
+     * that possibility, nbd_export_close() can be called more than
7711c0
+     * once on an export.
7711c0
+     */
7711c0
     QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
7711c0
         client_close(client, true);
7711c0
     }
7711c0
-    nbd_export_set_name(exp, NULL);
7711c0
-    nbd_export_set_description(exp, NULL);
7711c0
+    if (exp->name) {
7711c0
+        nbd_export_put(exp);
7711c0
+        g_free(exp->name);
7711c0
+        exp->name = NULL;
7711c0
+        QTAILQ_REMOVE(&exports, exp, next);
7711c0
+    }
7711c0
+    g_free(exp->description);
7711c0
+    exp->description = NULL;
7711c0
     nbd_export_put(exp);
7711c0
 }
7711c0
 
7711c0
diff --git a/qemu-nbd.c b/qemu-nbd.c
7711c0
index c37defb..c85aee4 100644
7711c0
--- a/qemu-nbd.c
7711c0
+++ b/qemu-nbd.c
7711c0
@@ -1017,11 +1017,9 @@ int main(int argc, char **argv)
7711c0
         }
7711c0
     }
7711c0
 
7711c0
-    export = nbd_export_new(bs, dev_offset, fd_size, nbdflags,
7711c0
-                            nbd_export_closed, writethrough,
7711c0
-                            NULL, &error_fatal);
7711c0
-    nbd_export_set_name(export, export_name);
7711c0
-    nbd_export_set_description(export, export_description);
7711c0
+    export = nbd_export_new(bs, dev_offset, fd_size, export_name,
7711c0
+                            export_description, nbdflags, nbd_export_closed,
7711c0
+                            writethrough, NULL, &error_fatal);
7711c0
 
7711c0
     if (device) {
7711c0
 #if HAVE_NBD_DEVICE
7711c0
-- 
7711c0
1.8.3.1
7711c0