diff mbox

[13/19] nbd/server: return original error codes

Message ID 20170530143052.165002-14-vsementsov@virtuozzo.com
State New
Headers show

Commit Message

Vladimir Sementsov-Ogievskiy May 30, 2017, 2:30 p.m. UTC
The code in many cases return -EINVAL or -EIO instead of original error
code from, for example, write_sync(). Following patch will need EPIPE
handling, so, let's refactor this where possible (the only exclusion
is nbd_co_receive_request, with own return-code convention)

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 nbd/server.c | 124 +++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 77 insertions(+), 47 deletions(-)

Comments

Eric Blake June 1, 2017, 10:29 p.m. UTC | #1
On 05/30/2017 09:30 AM, Vladimir Sementsov-Ogievskiy wrote:
> The code in many cases return -EINVAL or -EIO instead of original error
> code from, for example, write_sync(). Following patch will need EPIPE
> handling, so, let's refactor this where possible (the only exclusion
> is nbd_co_receive_request, with own return-code convention)

Do we still want/need EPIPE handling, given the discussion on the
previous two patches?

> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>  nbd/server.c | 124 +++++++++++++++++++++++++++++++++++++----------------------
>  1 file changed, 77 insertions(+), 47 deletions(-)
> 

Feels weird to have a net gain in code, but maybe worthwhile.

> diff --git a/nbd/server.c b/nbd/server.c
> index a47f13e4fb..30dfb81a5c 100644
> --- a/nbd/server.c
> +++ b/nbd/server.c
> @@ -136,30 +136,38 @@ static void nbd_client_receive_next_request(NBDClient *client);
>  static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
>                                        uint32_t opt, uint32_t len)
>  {
> +    int ret;
>      uint64_t magic;
>  
>      TRACE("Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32,
>            type, opt, len);
>  
>      magic = cpu_to_be64(NBD_REP_MAGIC);
> -    if (write_sync(ioc, &magic, sizeof(magic), NULL) < 0) {
> +    ret = write_sync(ioc, &magic, sizeof(magic), NULL);
> +    if (ret < 0) {
>          LOG("write failed (rep magic)");
> -        return -EINVAL;
> +        return ret;
>      }

Constructs like this should get shorter once we plumb errp all the way
through.  Okay, I can live with the temporary verbosity.

You may still have to make changes due to rebasing (in which case I'll
definitely want to review again); but if this patch doesn't need further
rework, you can add:
Reviewed-by: Eric Blake <eblake@redhat.com>
Vladimir Sementsov-Ogievskiy June 2, 2017, 10 a.m. UTC | #2
02.06.2017 01:29, Eric Blake wrote:
> On 05/30/2017 09:30 AM, Vladimir Sementsov-Ogievskiy wrote:
>> The code in many cases return -EINVAL or -EIO instead of original error
>> code from, for example, write_sync(). Following patch will need EPIPE
>> handling, so, let's refactor this where possible (the only exclusion
>> is nbd_co_receive_request, with own return-code convention)
> Do we still want/need EPIPE handling, given the discussion on the
> previous two patches?

Looks like this patch should be dropped. If EPIPE is not accepted 
(previous discussion), so error code is always EIO, no needs to save it 
into a variable..

>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>> ---
>>   nbd/server.c | 124 +++++++++++++++++++++++++++++++++++++----------------------
>>   1 file changed, 77 insertions(+), 47 deletions(-)
>>
> Feels weird to have a net gain in code, but maybe worthwhile.
>
>> diff --git a/nbd/server.c b/nbd/server.c
>> index a47f13e4fb..30dfb81a5c 100644
>> --- a/nbd/server.c
>> +++ b/nbd/server.c
>> @@ -136,30 +136,38 @@ static void nbd_client_receive_next_request(NBDClient *client);
>>   static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
>>                                         uint32_t opt, uint32_t len)
>>   {
>> +    int ret;
>>       uint64_t magic;
>>   
>>       TRACE("Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32,
>>             type, opt, len);
>>   
>>       magic = cpu_to_be64(NBD_REP_MAGIC);
>> -    if (write_sync(ioc, &magic, sizeof(magic), NULL) < 0) {
>> +    ret = write_sync(ioc, &magic, sizeof(magic), NULL);
>> +    if (ret < 0) {
>>           LOG("write failed (rep magic)");
>> -        return -EINVAL;
>> +        return ret;
>>       }
> Constructs like this should get shorter once we plumb errp all the way
> through.  Okay, I can live with the temporary verbosity.
>
> You may still have to make changes due to rebasing (in which case I'll
> definitely want to review again); but if this patch doesn't need further
> rework, you can add:
> Reviewed-by: Eric Blake <eblake@redhat.com>
>
diff mbox

Patch

diff --git a/nbd/server.c b/nbd/server.c
index a47f13e4fb..30dfb81a5c 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -136,30 +136,38 @@  static void nbd_client_receive_next_request(NBDClient *client);
 static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
                                       uint32_t opt, uint32_t len)
 {
+    int ret;
     uint64_t magic;
 
     TRACE("Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32,
           type, opt, len);
 
     magic = cpu_to_be64(NBD_REP_MAGIC);
-    if (write_sync(ioc, &magic, sizeof(magic), NULL) < 0) {
+    ret = write_sync(ioc, &magic, sizeof(magic), NULL);
+    if (ret < 0) {
         LOG("write failed (rep magic)");
-        return -EINVAL;
+        return ret;
     }
+
     opt = cpu_to_be32(opt);
-    if (write_sync(ioc, &opt, sizeof(opt), NULL) < 0) {
+    ret = write_sync(ioc, &opt, sizeof(opt), NULL);
+    if (ret < 0) {
         LOG("write failed (rep opt)");
-        return -EINVAL;
+        return ret;
     }
+
     type = cpu_to_be32(type);
-    if (write_sync(ioc, &type, sizeof(type), NULL) < 0) {
+    ret = write_sync(ioc, &type, sizeof(type), NULL);
+    if (ret < 0) {
         LOG("write failed (rep type)");
-        return -EINVAL;
+        return ret;
     }
+
     len = cpu_to_be32(len);
-    if (write_sync(ioc, &len, sizeof(len), NULL) < 0) {
+    ret = write_sync(ioc, &len, sizeof(len), NULL);
+    if (ret < 0) {
         LOG("write failed (rep data length)");
-        return -EINVAL;
+        return ret;
     }
     return 0;
 }
@@ -192,12 +200,12 @@  nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
     if (ret < 0) {
         goto out;
     }
-    if (write_sync(ioc, msg, len, NULL) < 0) {
+
+    ret = write_sync(ioc, msg, len, NULL);
+    if (ret < 0) {
         LOG("write failed (error message)");
-        ret = -EIO;
-    } else {
-        ret = 0;
     }
+
 out:
     g_free(msg);
     return ret;
@@ -223,18 +231,24 @@  static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp)
     }
 
     len = cpu_to_be32(name_len);
-    if (write_sync(ioc, &len, sizeof(len), NULL) < 0) {
+    ret = write_sync(ioc, &len, sizeof(len), NULL);
+    if (ret < 0) {
         LOG("write failed (name length)");
-        return -EINVAL;
+        return ret;
     }
-    if (write_sync(ioc, name, name_len, NULL) < 0) {
+
+    ret = write_sync(ioc, name, name_len, NULL);
+    if (ret < 0) {
         LOG("write failed (name buffer)");
-        return -EINVAL;
+        return ret;
     }
-    if (write_sync(ioc, desc, desc_len, NULL) < 0) {
+
+    ret = write_sync(ioc, desc, desc_len, NULL);
+    if (ret < 0) {
         LOG("write failed (description buffer)");
-        return -EINVAL;
+        return ret;
     }
+
     return 0;
 }
 
@@ -242,11 +256,13 @@  static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp)
  * Return -errno on error, 0 on success. */
 static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length)
 {
+    int ret;
     NBDExport *exp;
 
     if (length) {
-        if (drop_sync(client->ioc, length, NULL) < 0) {
-            return -EIO;
+        ret = drop_sync(client->ioc, length, NULL);
+        if (ret < 0) {
+            return ret;
         }
         return nbd_negotiate_send_rep_err(client->ioc,
                                           NBD_REP_ERR_INVALID, NBD_OPT_LIST,
@@ -255,8 +271,9 @@  static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length)
 
     /* For each export, send a NBD_REP_SERVER reply. */
     QTAILQ_FOREACH(exp, &exports, next) {
-        if (nbd_negotiate_send_rep_list(client->ioc, exp)) {
-            return -EINVAL;
+        ret = nbd_negotiate_send_rep_list(client->ioc, exp);
+        if (ret < 0) {
+            return ret;
         }
     }
     /* Finish with a NBD_REP_ACK. */
@@ -265,6 +282,7 @@  static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length)
 
 static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length)
 {
+    int ret;
     char name[NBD_MAX_NAME_SIZE + 1];
 
     /* Client sends:
@@ -275,9 +293,11 @@  static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length)
         LOG("Bad length received");
         return -EINVAL;
     }
-    if (read_sync(client->ioc, name, length, NULL) < 0) {
+
+    ret = read_sync(client->ioc, name, length, NULL);
+    if (ret < 0) {
         LOG("read failed");
-        return -EINVAL;
+        return ret;
     }
     name[length] = '\0';
 
@@ -354,6 +374,7 @@  static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
  * Return -errno on error, 0 on success. */
 static int nbd_negotiate_options(NBDClient *client)
 {
+    int ret;
     uint32_t flags;
     bool fixedNewstyle = false;
 
@@ -371,9 +392,10 @@  static int nbd_negotiate_options(NBDClient *client)
         ...           Rest of request
     */
 
-    if (read_sync(client->ioc, &flags, sizeof(flags), NULL) < 0) {
+    ret = read_sync(client->ioc, &flags, sizeof(flags), NULL);
+    if (ret < 0) {
         LOG("read failed");
-        return -EIO;
+        return ret;
     }
     TRACE("Checking client flags");
     be32_to_cpus(&flags);
@@ -397,9 +419,10 @@  static int nbd_negotiate_options(NBDClient *client)
         uint32_t clientflags, length;
         uint64_t magic;
 
-        if (read_sync(client->ioc, &magic, sizeof(magic), NULL) < 0) {
+        ret = read_sync(client->ioc, &magic, sizeof(magic), NULL);
+        if (ret < 0) {
             LOG("read failed");
-            return -EINVAL;
+            return ret;
         }
         TRACE("Checking opts magic");
         if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
@@ -407,17 +430,17 @@  static int nbd_negotiate_options(NBDClient *client)
             return -EINVAL;
         }
 
-        if (read_sync(client->ioc, &clientflags,
-                      sizeof(clientflags), NULL) < 0)
-        {
+        ret = read_sync(client->ioc, &clientflags, sizeof(clientflags), NULL);
+        if (ret < 0) {
             LOG("read failed");
-            return -EINVAL;
+            return ret;
         }
         clientflags = be32_to_cpu(clientflags);
 
-        if (read_sync(client->ioc, &length, sizeof(length), NULL) < 0) {
+        ret = read_sync(client->ioc, &length, sizeof(length), NULL);
+        if (ret < 0) {
             LOG("read failed");
-            return -EINVAL;
+            return ret;
         }
         length = be32_to_cpu(length);
 
@@ -445,8 +468,9 @@  static int nbd_negotiate_options(NBDClient *client)
                 return -EINVAL;
 
             default:
-                if (drop_sync(client->ioc, length, NULL) < 0) {
-                    return -EIO;
+                ret = drop_sync(client->ioc, length, NULL);
+                if (ret < 0) {
+                    return ret;
                 }
                 ret = nbd_negotiate_send_rep_err(client->ioc,
                                                  NBD_REP_ERR_TLS_REQD,
@@ -476,15 +500,17 @@  static int nbd_negotiate_options(NBDClient *client)
                 /* NBD spec says we must try to reply before
                  * disconnecting, but that we must also tolerate
                  * guests that don't wait for our reply. */
-                nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, clientflags);
-                return -EINVAL;
+                ret = nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
+                                             clientflags);
+                return ret < 0 ? ret : -EINVAL;
 
             case NBD_OPT_EXPORT_NAME:
                 return nbd_negotiate_handle_export_name(client, length);
 
             case NBD_OPT_STARTTLS:
-                if (drop_sync(client->ioc, length, NULL) < 0) {
-                    return -EIO;
+                ret = drop_sync(client->ioc, length, NULL);
+                if (ret < 0) {
+                    return ret;
                 }
                 if (client->tlscreds) {
                     ret = nbd_negotiate_send_rep_err(client->ioc,
@@ -502,8 +528,9 @@  static int nbd_negotiate_options(NBDClient *client)
                 }
                 break;
             default:
-                if (drop_sync(client->ioc, length, NULL) < 0) {
-                    return -EIO;
+                ret = drop_sync(client->ioc, length, NULL);
+                if (ret < 0) {
+                    return ret;
                 }
                 ret = nbd_negotiate_send_rep_err(client->ioc,
                                                  NBD_REP_ERR_UNSUP,
@@ -584,14 +611,17 @@  static coroutine_fn int nbd_negotiate(NBDClient *client)
             TRACE("TLS cannot be enabled with oldstyle protocol");
             return -EINVAL;
         }
-        if (write_sync(client->ioc, buf, sizeof(buf), NULL) < 0) {
+
+        ret = write_sync(client->ioc, buf, sizeof(buf), NULL);
+        if (ret < 0) {
             LOG("write failed");
-            return -EINVAL;
+            return ret;
         }
     } else {
-        if (write_sync(client->ioc, buf, 18, NULL) < 0) {
+        ret = write_sync(client->ioc, buf, 18, NULL);
+        if (ret < 0) {
             LOG("write failed");
-            return -EINVAL;
+            return ret;
         }
         ret = nbd_negotiate_options(client);
         if (ret != 0) {
@@ -977,7 +1007,7 @@  static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len)
         if (ret == 0) {
             ret = write_sync(client->ioc, req->data, len, NULL);
             if (ret < 0) {
-                ret = -EIO;
+                ret = ret;
             }
         }
         qio_channel_set_cork(client->ioc, false);