qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, eblake@redhat.com, den@openvz.org,
	vsementsov@virtuozzo.com
Subject: [Qemu-devel] [PATCH v2 12/12] nbd/server: refactor nbd_trip
Date: Fri,  2 Jun 2017 18:01:50 +0300	[thread overview]
Message-ID: <20170602150150.258222-13-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20170602150150.258222-1-vsementsov@virtuozzo.com>

- do not use 'goto error_reply' outside a switch to jump into the
  middle of the switch's default case label
- reduce code duplication

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 nbd/server.c | 53 ++++++++++++++++++++---------------------------------
 1 file changed, 20 insertions(+), 33 deletions(-)

diff --git a/nbd/server.c b/nbd/server.c
index 209a3d4e1e..198eabe338 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1078,6 +1078,7 @@ static coroutine_fn void nbd_trip(void *opaque)
     NBDReply reply;
     int ret;
     int flags;
+    int reply_data_len = 0;
 
     TRACE("Reading request.");
     if (client->closing) {
@@ -1090,7 +1091,7 @@ static coroutine_fn void nbd_trip(void *opaque)
     client->recv_coroutine = NULL;
     nbd_client_receive_next_request(client);
     if (ret == -EIO) {
-        goto out;
+        goto disconnect;
     }
 
     reply.handle = request.handle;
@@ -1098,7 +1099,7 @@ static coroutine_fn void nbd_trip(void *opaque)
 
     if (ret < 0) {
         reply.error = -ret;
-        goto error_reply;
+        goto reply;
     }
 
     if (client->closing) {
@@ -1119,7 +1120,7 @@ static coroutine_fn void nbd_trip(void *opaque)
             if (ret < 0) {
                 LOG("flush failed");
                 reply.error = -ret;
-                goto error_reply;
+                break;
             }
         }
 
@@ -1128,12 +1129,12 @@ static coroutine_fn void nbd_trip(void *opaque)
         if (ret < 0) {
             LOG("reading from file failed");
             reply.error = -ret;
-            goto error_reply;
+            break;
         }
 
+        reply_data_len = request.len;
         TRACE("Read %" PRIu32" byte(s)", request.len);
-        if (nbd_co_send_reply(req, &reply, request.len) < 0)
-            goto out;
+
         break;
     case NBD_CMD_WRITE:
         TRACE("Request type is WRITE");
@@ -1141,7 +1142,7 @@ static coroutine_fn void nbd_trip(void *opaque)
         if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
             TRACE("Server is read-only, return error");
             reply.error = EROFS;
-            goto error_reply;
+            break;
         }
 
         TRACE("Writing to device");
@@ -1155,21 +1156,16 @@ static coroutine_fn void nbd_trip(void *opaque)
         if (ret < 0) {
             LOG("writing to file failed");
             reply.error = -ret;
-            goto error_reply;
         }
 
-        if (nbd_co_send_reply(req, &reply, 0) < 0) {
-            goto out;
-        }
         break;
-
     case NBD_CMD_WRITE_ZEROES:
         TRACE("Request type is WRITE_ZEROES");
 
         if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
             TRACE("Server is read-only, return error");
             reply.error = EROFS;
-            goto error_reply;
+            break;
         }
 
         TRACE("Writing to device");
@@ -1186,14 +1182,9 @@ static coroutine_fn void nbd_trip(void *opaque)
         if (ret < 0) {
             LOG("writing to file failed");
             reply.error = -ret;
-            goto error_reply;
         }
 
-        if (nbd_co_send_reply(req, &reply, 0) < 0) {
-            goto out;
-        }
         break;
-
     case NBD_CMD_DISC:
         /* unreachable, thanks to special case in nbd_co_receive_request() */
         abort();
@@ -1206,9 +1197,7 @@ static coroutine_fn void nbd_trip(void *opaque)
             LOG("flush failed");
             reply.error = -ret;
         }
-        if (nbd_co_send_reply(req, &reply, 0) < 0) {
-            goto out;
-        }
+
         break;
     case NBD_CMD_TRIM:
         TRACE("Request type is TRIM");
@@ -1218,21 +1207,19 @@ static coroutine_fn void nbd_trip(void *opaque)
             LOG("discard failed");
             reply.error = -ret;
         }
-        if (nbd_co_send_reply(req, &reply, 0) < 0) {
-            goto out;
-        }
+
         break;
     default:
         LOG("invalid request type (%" PRIu32 ") received", request.type);
         reply.error = EINVAL;
-    error_reply:
-        /* We must disconnect after NBD_CMD_WRITE if we did not
-         * read the payload.
-         */
-        if (nbd_co_send_reply(req, &reply, 0) < 0 || !req->complete) {
-            goto out;
-        }
-        break;
+    }
+
+reply:
+    /* We must disconnect after NBD_CMD_WRITE if we did not
+     * read the payload.
+     */
+    if (nbd_co_send_reply(req, &reply, reply_data_len) < 0 || !req->complete) {
+        goto disconnect;
     }
 
     TRACE("Request/Reply complete");
@@ -1242,7 +1229,7 @@ done:
     nbd_client_put(client);
     return;
 
-out:
+disconnect:
     nbd_request_put(req);
     client_close(client);
     nbd_client_put(client);
-- 
2.11.1

  parent reply	other threads:[~2017-06-02 15:02 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-02 15:01 [Qemu-devel] [PATCH v2 00/12] nbd refactoring part 1 Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 01/12] nbd: rename read_sync and friends Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 02/12] nbd: make nbd_drop public Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 03/12] nbd/server: get rid of nbd_negotiate_read and friends Vladimir Sementsov-Ogievskiy
2017-07-18 12:04   ` Eric Blake
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 04/12] nbd/server: get rid of ssize_t Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 05/12] nbd/server: refactor nbd_co_send_reply Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 06/12] nbd/server: get rid of EAGAIN dead code Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 07/12] nbd/server: refactor nbd_co_receive_request Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 08/12] nbd/server: remove NBDClientNewData Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 09/12] nbd/server: nbd_negotiate: fix error path Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 10/12] nbd/server: get rid of fail: return rc Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` [Qemu-devel] [PATCH v2 11/12] nbd/server: rename rc to ret Vladimir Sementsov-Ogievskiy
2017-06-02 15:01 ` Vladimir Sementsov-Ogievskiy [this message]
2017-06-13 18:04   ` [Qemu-devel] [PATCH v2 12/12] nbd/server: refactor nbd_trip Paolo Bonzini
2017-06-13 14:10 ` [Qemu-devel] ping Re: [PATCH v2 00/12] nbd refactoring part 1 Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170602150150.258222-13-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).