[Libguestfs] [nbdkit PATCH 1/6] connections: Simplify handle_request()

Eric Blake eblake at redhat.com
Fri Nov 17 03:26:52 UTC 2017


_handle_request() was never returning anything but 0; and had to
communicate the error value through a parameter by reference.
As there is no return value that a plugin can use to declare the
connection useless, we can consolidate this into just returning
the error value.  Meanwhile, we don't need a wrapper layer of
handle_request() just to grab locks; so shuffle the naming a
bit to reduce the lines of code.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/connections.c | 55 ++++++++++++++++---------------------------------------
 1 file changed, 16 insertions(+), 39 deletions(-)

diff --git a/src/connections.c b/src/connections.c
index f3be63d..0ad252c 100644
--- a/src/connections.c
+++ b/src/connections.c
@@ -749,16 +749,14 @@ get_error (struct connection *conn)
  * check them again.  'buf' is either the data to be written or the
  * data to be returned, and points to a buffer of size 'count' bytes.
  *
- * Only returns -1 if there is a fatal error and the connection cannot
- * continue.
- *
- * On read/write errors, sets *error appropriately and returns 0.
+ * In all cases, the return value is the system errno value that will
+ * later be converted to the nbd error to send back to the client (0
+ * for success).
  */
-static int
-_handle_request (struct connection *conn,
-                 uint32_t cmd, uint32_t flags, uint64_t offset, uint32_t count,
-                 void *buf,
-                 uint32_t *error)
+static uint32_t
+handle_request (struct connection *conn,
+                uint32_t cmd, uint32_t flags, uint64_t offset, uint32_t count,
+                void *buf)
 {
   bool flush_after_command;
   int r;
@@ -776,40 +774,35 @@ _handle_request (struct connection *conn,
   case NBD_CMD_READ:
     r = plugin_pread (conn, buf, count, offset);
     if (r == -1) {
-      *error = get_error (conn);
-      return 0;
+      return get_error (conn);
     }
     break;

   case NBD_CMD_WRITE:
     r = plugin_pwrite (conn, buf, count, offset);
     if (r == -1) {
-      *error = get_error (conn);
-      return 0;
+      return get_error (conn);
     }
     break;

   case NBD_CMD_FLUSH:
     r = plugin_flush (conn);
     if (r == -1) {
-      *error = get_error (conn);
-      return 0;
+      return get_error (conn);
     }
     break;

   case NBD_CMD_TRIM:
     r = plugin_trim (conn, count, offset);
     if (r == -1) {
-      *error = get_error (conn);
-      return 0;
+      return get_error (conn);
     }
     break;

   case NBD_CMD_WRITE_ZEROES:
     r = plugin_zero (conn, count, offset, !(flags & NBD_CMD_FLAG_NO_HOLE));
     if (r == -1) {
-      *error = get_error (conn);
-      return 0;
+      return get_error (conn);
     }
     break;

@@ -820,8 +813,7 @@ _handle_request (struct connection *conn,
   if (flush_after_command) {
     r = plugin_flush (conn);
     if (r == -1) {
-      *error = get_error (conn);
-      return 0;
+      return get_error (conn);
     }
   }

@@ -829,21 +821,6 @@ _handle_request (struct connection *conn,
 }

 static int
-handle_request (struct connection *conn,
-                uint32_t cmd, uint32_t flags, uint64_t offset, uint32_t count,
-                void *buf,
-                uint32_t *error)
-{
-  int r;
-
-  plugin_lock_request (conn);
-  r = _handle_request (conn, cmd, flags, offset, count, buf, error);
-  plugin_unlock_request (conn);
-
-  return r;
-}
-
-static int
 skip_over_write_buffer (int sock, size_t count)
 {
   char buf[BUFSIZ];
@@ -974,9 +951,9 @@ recv_request_send_reply (struct connection *conn)
   }

   /* Perform the request.  Only this part happens inside the request lock. */
-  r = handle_request (conn, cmd, flags, offset, count, buf, &error);
-  if (r == -1)
-    return -1;
+  plugin_lock_request (conn);
+  error = handle_request (conn, cmd, flags, offset, count, buf);
+  plugin_unlock_request (conn);

   /* Send the reply packet. */
  send_reply:
-- 
2.13.6




More information about the Libguestfs mailing list