[Libguestfs] [libnbd PATCH] API: Add nbd_set_opt_mode to expose NEGOTIATING state

Eric Blake eblake at redhat.com
Tue Aug 11 02:13:09 UTC 2020


On 8/10/20 9:09 PM, Eric Blake wrote:
> This is the bare minimum needed to allow the user to take control over
> the rest of option negotiating.  This patch adds several new API:
> 

> ---
>   lib/internal.h                                |  4 +
>   generator/API.ml                              | 93 +++++++++++++++++--
>   generator/API.mli                             |  1 +
>   generator/C.ml                                |  4 +-
>   generator/state_machine.ml                    | 26 ++++++
>   generator/states-magic.c                      |  6 +-
>   generator/states-newstyle-opt-go.c            |  4 +
>   .../states-newstyle-opt-structured-reply.c    |  7 +-
>   generator/states-newstyle.c                   | 47 ++++++++--
>   lib/connect.c                                 |  5 +-
>   lib/is-state.c                                | 14 ++-
>   lib/Makefile.am                               |  3 +-
>   examples/list-exports.c                       | 64 +++++++++----
>   13 files changed, 235 insertions(+), 43 deletions(-)

It helps if I add my new file before making the commit:

diff --git c/lib/opt.c i/lib/opt.c
new file mode 100644
index 0000000..5d74a4f
--- /dev/null
+++ i/lib/opt.c
@@ -0,0 +1,85 @@
+/* NBD client library in userspace
+ * Copyright (C) 2020 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
02110-1301 USA
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <assert.h>
+#include <limits.h>
+
+#include "internal.h"
+
+int
+nbd_unlocked_set_opt_mode (struct nbd_handle *h, bool value)
+{
+  h->opt_mode = value;
+  return 0;
+}
+
+/* NB: may_set_error = false. */
+int
+nbd_unlocked_get_opt_mode (struct nbd_handle *h)
+{
+  return h->opt_mode;
+}
+
+static int
+wait_for_option (struct nbd_handle *h)
+{
+  while (nbd_internal_is_state_connecting (get_next_state (h))) {
+    if (nbd_unlocked_poll (h, -1) == -1)
+      return -1;
+  }
+
+  /* Should we do further decoding, like connect.c error_unless_ready? */
+  return 0;
+}
+
+/* Issue NBD_OPT_GO (or NBD_OPT_EXPORT_NAME) and wait for the reply. */
+int
+nbd_unlocked_opt_go (struct nbd_handle *h)
+{
+  int r;
+
+  h->sbuf.option.option = NBD_OPT_GO;
+
+  if (nbd_internal_run (h, cmd_issue) == -1)
+    debug (h, "option queued, ignoring state machine failure");
+
+  r = wait_for_option (h);
+  if (r == 0 && nbd_internal_is_state_negotiating (get_next_state (h)))
+    return -1; /* NBD_OPT_GO failed, but can be attempted again */
+  return r;
+}
+
+/* Issue NBD_OPT_ABORT and wait for the state change. */
+int
+nbd_unlocked_opt_abort (struct nbd_handle *h)
+{
+  h->sbuf.option.option = NBD_OPT_ABORT;
+
+  if (nbd_internal_run (h, cmd_issue) == -1)
+    debug (h, "option queued, ignoring state machine failure");
+
+  return wait_for_option (h);
+}


-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




More information about the Libguestfs mailing list