qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, mreitz@redhat.com
Subject: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire
Date: Thu,  7 May 2015 17:26:14 +0200	[thread overview]
Message-ID: <1431012374-14113-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1431012374-14113-1-git-send-email-pbonzini@redhat.com>

Right now, NBD includes potentially platform-specific error values in
the wire protocol.

Luckily, most common error values are more or less universal: in
particular, of all errno values <= 34 (up to ERANGE), they are all
the same on supported platforms except for 11 (which is EAGAIN on
Windows and Linux, but EDEADLK on Darwin and the *BSDs).

So, in order to guarantee some portability, only keep a dozen
possible error codes and squash everything else to EINVAL.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/nbd.c b/nbd.c
index eea8c51..1ad5b66 100644
--- a/nbd.c
+++ b/nbd.c
@@ -86,6 +86,37 @@
 #define NBD_OPT_ABORT           (2)
 #define NBD_OPT_LIST            (3)
 
+/* NBD errors are based on errno numbers, so there is a 1:1 mapping,
+ * but only a limited set of errno values is specified in the protocol.
+ * Everything else is squashed to EINVAL.
+ */
+static int system_errno_to_nbd_errno(int err)
+{
+    switch (err) {
+    case EPERM:
+        return 1;
+    case EIO:
+        return 5;
+    case ENXIO:
+        return 6;
+    case E2BIG:
+        return 7;
+    case ENOMEM:
+        return 12;
+    case EACCES:
+        return 13;
+    case EFBIG:
+        return 27;
+    case ENOSPC:
+        return 28;
+    case EROFS:
+        return 30;
+    case EINVAL:
+    default:
+        return 22;
+    }
+}
+
 /* Definitions for opaque data types */
 
 typedef struct NBDRequest NBDRequest;
@@ -856,6 +887,20 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
     reply->error  = be32_to_cpup((uint32_t*)(buf + 4));
     reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
 
+    /* NBD errors should be universally equal to the corresponding
+     * errno values, check it here.
+     */
+    QEMU_BUILD_BUG_ON(EPERM != 1);
+    QEMU_BUILD_BUG_ON(EIO != 5);
+    QEMU_BUILD_BUG_ON(ENXIO != 6);
+    QEMU_BUILD_BUG_ON(E2BIG != 7);
+    QEMU_BUILD_BUG_ON(ENOMEM != 12);
+    QEMU_BUILD_BUG_ON(EACCES != 13);
+    QEMU_BUILD_BUG_ON(EINVAL != 22);
+    QEMU_BUILD_BUG_ON(EFBIG != 27);
+    QEMU_BUILD_BUG_ON(ENOSPC != 28);
+    QEMU_BUILD_BUG_ON(EROFS != 30);
+
     TRACE("Got reply: "
           "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
           magic, reply->error, reply->handle);
@@ -872,6 +917,8 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
     uint8_t buf[NBD_REPLY_SIZE];
     ssize_t ret;
 
+    reply->error = system_errno_to_nbd_errno(reply->error);
+
     /* Reply
        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
        [ 4 ..  7]    error   (0 == no error)
-- 
2.3.5

  reply	other threads:[~2015-05-07 15:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-07 15:26 [Qemu-devel] [PATCH] qemu-nbd: return EROFS for discard on a read-only export Paolo Bonzini
2015-05-07 15:26 ` Paolo Bonzini [this message]
2015-05-07 17:01   ` [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire Eric Blake
2015-05-07 17:06     ` Paolo Bonzini
2015-05-08  6:45   ` Markus Armbruster
2015-05-08  8:30     ` Paolo Bonzini
2015-05-08  9:32       ` Markus Armbruster
2015-05-08  9:40         ` Paolo Bonzini
2015-05-08 10:04           ` Markus Armbruster
2015-05-08 12:45   ` Max Reitz
2015-05-08 13:01     ` Max Reitz
2015-05-08 12:43 ` [Qemu-devel] [PATCH] qemu-nbd: return EROFS for discard on a read-only export Max Reitz

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=1431012374-14113-2-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@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).