Blame SOURCES/kvm-nbd-client-Move-export-name-into-NBDExportInfo.patch

7711c0
From 0736b0b842bcd0acefed2523d2e0cda3876ca469 Mon Sep 17 00:00:00 2001
7711c0
From: John Snow <jsnow@redhat.com>
7711c0
Date: Wed, 27 Mar 2019 17:22:43 +0100
7711c0
Subject: [PATCH 105/163] nbd/client: Move export name into NBDExportInfo
7711c0
7711c0
RH-Author: John Snow <jsnow@redhat.com>
7711c0
Message-id: <20190327172308.31077-31-jsnow@redhat.com>
7711c0
Patchwork-id: 85203
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 30/55] nbd/client: Move export name into NBDExportInfo
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
Refactor the 'name' parameter of nbd_receive_negotiate() from
7711c0
being a separate parameter into being part of the in-out 'info'.
7711c0
This also spills over to a simplification of nbd_opt_go().
7711c0
7711c0
The main driver for this refactoring is that an upcoming patch
7711c0
would like to add support to qemu-nbd to list information about
7711c0
all exports available on a server, where the name(s) will be
7711c0
provided by the server instead of the client.  But another benefit
7711c0
is that we can now allow the client to explicitly specify the
7711c0
empty export name "" even when connecting to an oldstyle server
7711c0
(even if qemu is no longer such a server after commit 7f7dfe2a).
7711c0
7711c0
Signed-off-by: Eric Blake <eblake@redhat.com>
7711c0
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
7711c0
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7711c0
Message-Id: <20190117193658.16413-10-eblake@redhat.com>
7711c0
(cherry picked from commit 6dc1667d6881add34e9bad48ac2a848134ea8a6d)
7711c0
Signed-off-by: John Snow <jsnow@redhat.com>
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 block/nbd-client.c  |  5 +++--
7711c0
 include/block/nbd.h |  8 ++++----
7711c0
 nbd/client.c        | 39 ++++++++++++++++++---------------------
7711c0
 nbd/trace-events    |  2 +-
7711c0
 qemu-nbd.c          |  6 ++++--
7711c0
 5 files changed, 30 insertions(+), 30 deletions(-)
7711c0
7711c0
diff --git a/block/nbd-client.c b/block/nbd-client.c
7711c0
index ef32075..3309376 100644
7711c0
--- a/block/nbd-client.c
7711c0
+++ b/block/nbd-client.c
7711c0
@@ -999,10 +999,11 @@ int nbd_client_init(BlockDriverState *bs,
7711c0
     client->info.structured_reply = true;
7711c0
     client->info.base_allocation = true;
7711c0
     client->info.x_dirty_bitmap = g_strdup(x_dirty_bitmap);
7711c0
-    ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), export,
7711c0
-                                tlscreds, hostname,
7711c0
+    client->info.name = g_strdup(export ?: "");
7711c0
+    ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), tlscreds, hostname,
7711c0
                                 &client->ioc, &client->info, errp);
7711c0
     g_free(client->info.x_dirty_bitmap);
7711c0
+    g_free(client->info.name);
7711c0
     if (ret < 0) {
7711c0
         logout("Failed to negotiate with the NBD server\n");
7711c0
         return ret;
7711c0
diff --git a/include/block/nbd.h b/include/block/nbd.h
7711c0
index 24be957..00d3eb5 100644
7711c0
--- a/include/block/nbd.h
7711c0
+++ b/include/block/nbd.h
7711c0
@@ -262,6 +262,7 @@ struct NBDExportInfo {
7711c0
     /* Set by client before nbd_receive_negotiate() */
7711c0
     bool request_sizes;
7711c0
     char *x_dirty_bitmap;
7711c0
+    char *name; /* must be non-NULL */
7711c0
 
7711c0
     /* In-out fields, set by client before nbd_receive_negotiate() and
7711c0
      * updated by server results during nbd_receive_negotiate() */
7711c0
@@ -279,10 +280,9 @@ struct NBDExportInfo {
7711c0
 };
7711c0
 typedef struct NBDExportInfo NBDExportInfo;
7711c0
 
7711c0
-int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
-                          QCryptoTLSCreds *tlscreds, const char *hostname,
7711c0
-                          QIOChannel **outioc, NBDExportInfo *info,
7711c0
-                          Error **errp);
7711c0
+int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
7711c0
+                          const char *hostname, QIOChannel **outioc,
7711c0
+                          NBDExportInfo *info, Error **errp);
7711c0
 int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
7711c0
              Error **errp);
7711c0
 int nbd_send_request(QIOChannel *ioc, NBDRequest *request);
7711c0
diff --git a/nbd/client.c b/nbd/client.c
7711c0
index fd4ba8d..8227e69 100644
7711c0
--- a/nbd/client.c
7711c0
+++ b/nbd/client.c
7711c0
@@ -330,15 +330,14 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,
7711c0
 }
7711c0
 
7711c0
 
7711c0
-/* Returns -1 if NBD_OPT_GO proves the export @wantname cannot be
7711c0
+/* Returns -1 if NBD_OPT_GO proves the export @info->name cannot be
7711c0
  * used, 0 if NBD_OPT_GO is unsupported (fall back to NBD_OPT_LIST and
7711c0
  * NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to
7711c0
- * go (with @info populated). */
7711c0
-static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
7711c0
-                      NBDExportInfo *info, Error **errp)
7711c0
+ * go (with the rest of @info populated). */
7711c0
+static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
7711c0
 {
7711c0
     NBDOptionReply reply;
7711c0
-    uint32_t len = strlen(wantname);
7711c0
+    uint32_t len = strlen(info->name);
7711c0
     uint16_t type;
7711c0
     int error;
7711c0
     char *buf;
7711c0
@@ -348,10 +347,10 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
7711c0
      * flags still 0 is a witness of a broken server. */
7711c0
     info->flags = 0;
7711c0
 
7711c0
-    trace_nbd_opt_go_start(wantname);
7711c0
+    trace_nbd_opt_go_start(info->name);
7711c0
     buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1);
7711c0
     stl_be_p(buf, len);
7711c0
-    memcpy(buf + 4, wantname, len);
7711c0
+    memcpy(buf + 4, info->name, len);
7711c0
     /* At most one request, everything else up to server */
7711c0
     stw_be_p(buf + 4 + len, info->request_sizes);
7711c0
     if (info->request_sizes) {
7711c0
@@ -753,10 +752,9 @@ static int nbd_negotiate_simple_meta_context(QIOChannel *ioc,
7711c0
     return 0;
7711c0
 }
7711c0
 
7711c0
-int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
-                          QCryptoTLSCreds *tlscreds, const char *hostname,
7711c0
-                          QIOChannel **outioc, NBDExportInfo *info,
7711c0
-                          Error **errp)
7711c0
+int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
7711c0
+                          const char *hostname, QIOChannel **outioc,
7711c0
+                          NBDExportInfo *info, Error **errp)
7711c0
 {
7711c0
     uint64_t magic;
7711c0
     int rc;
7711c0
@@ -766,6 +764,8 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
 
7711c0
     trace_nbd_receive_negotiate(tlscreds, hostname ? hostname : "<null>");
7711c0
 
7711c0
+    assert(info->name);
7711c0
+    trace_nbd_receive_negotiate_name(info->name);
7711c0
     info->structured_reply = false;
7711c0
     info->base_allocation = false;
7711c0
     rc = -EINVAL;
7711c0
@@ -834,10 +834,6 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
                 goto fail;
7711c0
             }
7711c0
         }
7711c0
-        if (!name) {
7711c0
-            trace_nbd_receive_negotiate_default_name();
7711c0
-            name = "";
7711c0
-        }
7711c0
         if (fixedNewStyle) {
7711c0
             int result;
7711c0
 
7711c0
@@ -853,7 +849,8 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
 
7711c0
             if (info->structured_reply && base_allocation) {
7711c0
                 result = nbd_negotiate_simple_meta_context(
7711c0
-                        ioc, name, info->x_dirty_bitmap ?: "base:allocation",
7711c0
+                        ioc, info->name,
7711c0
+                        info->x_dirty_bitmap ?: "base:allocation",
7711c0
                         &info->meta_base_allocation_id, errp);
7711c0
                 if (result < 0) {
7711c0
                     goto fail;
7711c0
@@ -866,7 +863,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
              * TLS).  If it is not available, fall back to
7711c0
              * NBD_OPT_LIST for nicer error messages about a missing
7711c0
              * export, then use NBD_OPT_EXPORT_NAME.  */
7711c0
-            result = nbd_opt_go(ioc, name, info, errp);
7711c0
+            result = nbd_opt_go(ioc, info, errp);
7711c0
             if (result < 0) {
7711c0
                 goto fail;
7711c0
             }
7711c0
@@ -879,12 +876,12 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
              * query gives us better error reporting if the
7711c0
              * export name is not available.
7711c0
              */
7711c0
-            if (nbd_receive_query_exports(ioc, name, errp) < 0) {
7711c0
+            if (nbd_receive_query_exports(ioc, info->name, errp) < 0) {
7711c0
                 goto fail;
7711c0
             }
7711c0
         }
7711c0
         /* write the export name request */
7711c0
-        if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name,
7711c0
+        if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, info->name,
7711c0
                                     errp) < 0) {
7711c0
             goto fail;
7711c0
         }
7711c0
@@ -904,8 +901,8 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
     } else if (magic == NBD_CLIENT_MAGIC) {
7711c0
         uint32_t oldflags;
7711c0
 
7711c0
-        if (name) {
7711c0
-            error_setg(errp, "Server does not support export names");
7711c0
+        if (*info->name) {
7711c0
+            error_setg(errp, "Server does not support non-empty export names");
7711c0
             goto fail;
7711c0
         }
7711c0
         if (tlscreds) {
7711c0
diff --git a/nbd/trace-events b/nbd/trace-events
7711c0
index d1e1ca6..c3966d2 100644
7711c0
--- a/nbd/trace-events
7711c0
+++ b/nbd/trace-events
7711c0
@@ -17,7 +17,7 @@ nbd_opt_meta_reply(const char *context, uint32_t id) "Received mapping of contex
7711c0
 nbd_receive_negotiate(void *tlscreds, const char *hostname) "Receiving negotiation tlscreds=%p hostname=%s"
7711c0
 nbd_receive_negotiate_magic(uint64_t magic) "Magic is 0x%" PRIx64
7711c0
 nbd_receive_negotiate_server_flags(uint32_t globalflags) "Global flags are 0x%" PRIx32
7711c0
-nbd_receive_negotiate_default_name(void) "Using default NBD export name \"\""
7711c0
+nbd_receive_negotiate_name(const char *name) "Requesting NBD export name '%s'"
7711c0
 nbd_receive_negotiate_size_flags(uint64_t size, uint16_t flags) "Size is %" PRIu64 ", export flags 0x%" PRIx16
7711c0
 nbd_init_set_socket(void) "Setting NBD socket"
7711c0
 nbd_init_set_block_size(unsigned long block_size) "Setting block size to %lu"
7711c0
diff --git a/qemu-nbd.c b/qemu-nbd.c
7711c0
index efca0e4..3c53870 100644
7711c0
--- a/qemu-nbd.c
7711c0
+++ b/qemu-nbd.c
7711c0
@@ -264,7 +264,7 @@ static void *show_parts(void *arg)
7711c0
 static void *nbd_client_thread(void *arg)
7711c0
 {
7711c0
     char *device = arg;
7711c0
-    NBDExportInfo info = { .request_sizes = false, };
7711c0
+    NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
7711c0
     QIOChannelSocket *sioc;
7711c0
     int fd;
7711c0
     int ret;
7711c0
@@ -279,7 +279,7 @@ static void *nbd_client_thread(void *arg)
7711c0
         goto out;
7711c0
     }
7711c0
 
7711c0
-    ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL,
7711c0
+    ret = nbd_receive_negotiate(QIO_CHANNEL(sioc),
7711c0
                                 NULL, NULL, NULL, &info, &local_error);
7711c0
     if (ret < 0) {
7711c0
         if (local_error) {
7711c0
@@ -318,6 +318,7 @@ static void *nbd_client_thread(void *arg)
7711c0
     }
7711c0
     close(fd);
7711c0
     object_unref(OBJECT(sioc));
7711c0
+    g_free(info.name);
7711c0
     kill(getpid(), SIGTERM);
7711c0
     return (void *) EXIT_SUCCESS;
7711c0
 
7711c0
@@ -326,6 +327,7 @@ out_fd:
7711c0
 out_socket:
7711c0
     object_unref(OBJECT(sioc));
7711c0
 out:
7711c0
+    g_free(info.name);
7711c0
     kill(getpid(), SIGTERM);
7711c0
     return (void *) EXIT_FAILURE;
7711c0
 }
7711c0
-- 
7711c0
1.8.3.1
7711c0