[Libguestfs] [PATCH libnbd v2 2/6] api: Add nbd_aio_is_created test and use it in existing code.

Richard W.M. Jones rjones at redhat.com
Wed May 22 09:50:15 UTC 2019


Simple refactoring adding a new API.
---
 generator/generator | 11 +++++++++++
 lib/aio.c           |  6 ++++++
 lib/connect.c       |  8 ++++----
 lib/rw.c            |  4 ++--
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/generator/generator b/generator/generator
index 4492f9d..be37fd1 100755
--- a/generator/generator
+++ b/generator/generator
@@ -1537,6 +1537,17 @@ has detected that the file descriptor associated with this
 connection is writable.";
   };
 
+  "aio_is_created", {
+    default_call with
+    args = []; ret = RBool;
+    shortdesc = "check if the connection has just been created";
+    longdesc = "\
+Return true if this connection has just been created.  This
+is the state before the connection object has started
+connecting to a server.  In this state the handle can start
+to be connected by calling functions such as C<nbd_aio_connect>.";
+  };
+
   "aio_is_ready", {
     default_call with
     args = []; ret = RBool;
diff --git a/lib/aio.c b/lib/aio.c
index d8458ed..79c89d5 100644
--- a/lib/aio.c
+++ b/lib/aio.c
@@ -47,6 +47,12 @@ nbd_unlocked_aio_notify_write (struct nbd_connection *conn)
   return nbd_internal_run (conn->h, conn, notify_write);
 }
 
+int
+nbd_unlocked_aio_is_created (struct nbd_connection *conn)
+{
+  return conn->state == STATE_START;
+}
+
 int
 nbd_unlocked_aio_is_ready (struct nbd_connection *conn)
 {
diff --git a/lib/connect.c b/lib/connect.c
index d711fd7..a5a262d 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -68,7 +68,7 @@ wait_all_connected (struct nbd_handle *h)
   return 0;
 }
 
-/* For all connections in the CREATED state, start connecting them to
+/* For all connections in the initial state, start connecting them to
  * a Unix domain socket.  Wait until all connections are in the READY
  * state.
  */
@@ -87,7 +87,7 @@ nbd_unlocked_connect_unix (struct nbd_handle *h, const char *sockpath)
 
   started = false;
   for (i = 0; i < h->multi_conn; ++i) {
-    if (h->conns[i]->state == STATE_START) {
+    if (nbd_unlocked_aio_is_created (h->conns[i])) {
       if (nbd_unlocked_aio_connect (h->conns[i],
                                     (struct sockaddr *) &sun, len) == -1)
         return -1;
@@ -113,7 +113,7 @@ nbd_unlocked_connect_tcp (struct nbd_handle *h,
 
   started = false;
   for (i = 0; i < h->multi_conn; ++i) {
-    if (h->conns[i]->state == STATE_START) {
+    if (nbd_unlocked_aio_is_created (h->conns[i])) {
       if (nbd_unlocked_aio_connect_tcp (h->conns[i], hostname, port) == -1)
         return -1;
       started = true;
@@ -134,7 +134,7 @@ nbd_unlocked_connect_tcp (struct nbd_handle *h,
 int
 nbd_unlocked_connect_command (struct nbd_handle *h, char **argv)
 {
-  if (h->conns[0]->state != STATE_START) {
+  if (!nbd_unlocked_aio_is_created (h->conns[0])) {
     set_error (0, "first connection in this handle is not in the created state, this is likely to be caused by a programming error in the calling program");
     return -1;
   }
diff --git a/lib/rw.c b/lib/rw.c
index 9dfce97..861ab67 100644
--- a/lib/rw.c
+++ b/lib/rw.c
@@ -46,9 +46,9 @@ pick_connection (struct nbd_handle *h)
       break;
     }
     /* At least one connection is busy, not dead and not sitting in
-     * the initial CREATED state.
+     * the initial state.
      */
-    if (h->conns[i]->state != STATE_START &&
+    if (!nbd_unlocked_aio_is_created (h->conns[i]) &&
         !nbd_unlocked_aio_is_dead (h->conns[i]))
       error = EBUSY;
 
-- 
2.21.0




More information about the Libguestfs mailing list