Skip to contentSkip to conversation

qemu-nbd: return EROFS for discard on a read-only export

PATCH In review Paolo Bonzini 4 participants 12 messages May 7, 2015 – May 8, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-nbd: return EROFS for discard on a read-only export

This is consistent with the handling of writes.

---
 nbd.c | 6 ++++++
 1 file changed, 6 insertions(+)
nbd.c +6 -0
index cb1b9bb..eea8c51 100644
@@ -1325,6 +1325,12 @@ static void nbd_trip(void *opaque)
         break;
     case NBD_CMD_TRIM:
         TRACE("Request type is TRIM");
+        if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
+            TRACE("Server is read-only, return error");
+            reply.error = EROFS;
+            goto error_reply;
+        }
+
         ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset)
                                        / BDRV_SECTOR_SIZE,
                              request.len / BDRV_SECTOR_SIZE);
Signature · 1 line
2.3.5
#1 lore raw

PATCH 1/1 qemu-nbd: only send a limited number of errno codes on the wire

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.

---
 nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
nbd.c +47 -0
index eea8c51..1ad5b66 100644
@@ -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)
Signature · 1 line
2.3.5
#2 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 7 lines · Paolo Bonzini

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).

Well, this is not true on GNU Hurd, but to my knowledge no one has tried to compile qemu on Hurd.

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 893

Quoted 5 lines
nbd.c +4 -0
+    /* NBD errors should be universally equal to the corresponding
+     * errno values, check it here.
+     */
+    QEMU_BUILD_BUG_ON(EPERM != 1);

And this is (probably not the first place) where qemu compilation would fail on Hurd. At any rate, I see no reason to hold up the patch on that ground.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 8 lines · Eric Blake
Quoted 5 lines

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 893

Quoted 5 lines
nbd.c +4 -0
+    /* NBD errors should be universally equal to the corresponding
+     * errno values, check it here.
+     */
+    QEMU_BUILD_BUG_ON(EPERM != 1);

And this is (probably not the first place) where qemu compilation would fail on Hurd. At any rate, I see no reason to hold up the patch on that ground.

Well, I can certainly add a backwards mapping.

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

In reply to Paolo Bonzini
Quoted 2 lines · Paolo Bonzini

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

Design flaw.

Quoted 4 lines

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).

Can EAGAIN or EDEADLK happen? "I don't know" is an acceptable answer :)

Quoted 2 lines

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

Ugh. I guess it'll do.

Cleaner solution: Fix the protocol to transmit "EPERM", "EIO", ... in addition to 1, 5, ...

If backward compatibility is not an issue: s/in addition to/instead of/.

Quoted 10 lines
---
 nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
nbd.c +0 -0
index eea8c51..1ad5b66 100644

nbd.c line 92

Quoted 7 lines
nbd.c +4 -0
@@ -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.
+ */

Is the protocol defined anywhere?

Quoted 17 lines
nbd.c +17 -0
+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;

nbd.c line 119

Quoted 10 lines
nbd.c +10 -0
+    case ENOSPC:
+        return 28;
+    case EROFS:
+        return 30;
+    case EINVAL:
+    default:
+        return 22;
+    }
+}
+

This maps recognized OS errnos to NBD errnos. The latter are literals.

Quoted 11 lines
nbd.c +4 -0
 /* 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);

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 10 lines
nbd.c +10 -0
+    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);
+

This checks that the mapping above is the identify function for all the recognized NBD errnos. Why is that necessary?

Same literals as above. Violates DRY. I don't mind all that much, but wonder whether we could at least do the checking next to system_errno_to_nbd_errno().

Quoted 12 lines
nbd.c +2 -0
     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)
#5 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 6 lines · Markus Armbruster
Quoted 4 lines

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).

Can EAGAIN or EDEADLK happen? "I don't know" is an acceptable answer :)

No. Even stuff like EFAULT would be a bug.

Quoted 7 lines
Quoted 2 lines

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

Ugh. I guess it'll do.

Cleaner solution: Fix the protocol to transmit "EPERM", "EIO", ... in addition to 1, 5, ...

Why? It's a binary protocol after all. But I agree that the "right" fix without backwards-compatibility would be to make the errors something like 0x80000000 to 0x80000004.

Paolo

Quoted 93 lines

If backward compatibility is not an issue: s/in addition to/instead of/.

Quoted 10 lines
---
 nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
nbd.c +0 -0
index eea8c51..1ad5b66 100644

nbd.c line 92

Quoted 7 lines
nbd.c +4 -0
@@ -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.
+ */

Is the protocol defined anywhere?

Quoted 17 lines
nbd.c +17 -0
+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;

nbd.c line 119

Quoted 10 lines
nbd.c +10 -0
+    case ENOSPC:
+        return 28;
+    case EROFS:
+        return 30;
+    case EINVAL:
+    default:
+        return 22;
+    }
+}
+

This maps recognized OS errnos to NBD errnos. The latter are literals.

Quoted 11 lines
nbd.c +4 -0
 /* 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);

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 10 lines
nbd.c +10 -0
+    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);
+

This checks that the mapping above is the identify function for all the recognized NBD errnos. Why is that necessary?

Same literals as above. Violates DRY. I don't mind all that much, but wonder whether we could at least do the checking next to system_errno_to_nbd_errno().

Quoted 12 lines
nbd.c +2 -0
     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)
#6 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Markus Armbruster
Quoted 4 lines

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).

Can EAGAIN or EDEADLK happen? "I don't know" is an acceptable answer :)

No. Even stuff like EFAULT would be a bug.

Good.

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

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

Ugh. I guess it'll do.

Cleaner solution: Fix the protocol to transmit "EPERM", "EIO", ... in addition to 1, 5, ...

Why? It's a binary protocol after all.

Okay, I just succeeded in demonstrating my ignorance %-)

Quoted 5 lines

But I agree that the "right"
fix without backwards-compatibility would be to make the errors something like 0x80000000 to 0x80000004.

Paolo

Two more remarks below.

Quoted 15 lines
Quoted 15 lines

If backward compatibility is not an issue: s/in addition to/instead of/.

Quoted 13 lines
---
 nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
nbd.c +0 -0
index eea8c51..1ad5b66 100644
@@ -86,6 +86,37 @@
 #define NBD_OPT_ABORT           (2)
 #define NBD_OPT_LIST            (3)
 
Quoted 6 lines
Quoted 6 lines

nbd.c line 92

Quoted 4 lines
nbd.c +4 -0
+/* 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.
+ */

Is the protocol defined anywhere?

https://github.com/yoe/nbd/blob/master/doc/proto.txt

All it has on the error code is this paragraph:

The reply contains three fields: a 32 bit magic number ('magic'), a 32 bit error code ('error'; 0, unless an error occurred in which case it is the errno of the error on the server side), and the same 64 bit handle that the corresponding request had in its 'handle' field. In case the reply is sent in response to a read request and the error field is 0 (zero), the reply header is immediately followed by request.len bytes of data.

Could you update it to document the errno compatibility issues, and recommended practice (i.e. this patch's)?

Quoted 46 lines
Quoted 46 lines
Quoted 17 lines
nbd.c +17 -0
+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;

nbd.c line 119

Quoted 10 lines
nbd.c +10 -0
+    case ENOSPC:
+        return 28;
+    case EROFS:
+        return 30;
+    case EINVAL:
+    default:
+        return 22;
+    }
+}
+

This maps recognized OS errnos to NBD errnos. The latter are literals.

Quoted 16 lines
nbd.c +9 -0
 /* 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);
Quoted 8 lines
Quoted 8 lines

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 5 lines
nbd.c +5 -0
+    QEMU_BUILD_BUG_ON(EINVAL != 22);
+    QEMU_BUILD_BUG_ON(EFBIG != 27);
+    QEMU_BUILD_BUG_ON(ENOSPC != 28);
+    QEMU_BUILD_BUG_ON(EROFS != 30);
+

This checks that the mapping above is the identify function for all the recognized NBD errnos. Why is that necessary?

Still curious. Explain, and earn my R-by :)

Quoted 3 lines
Quoted 3 lines

Same literals as above. Violates DRY. I don't mind all that much, but wonder whether we could at least do the checking next to system_errno_to_nbd_errno().

Could we?

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
nbd.c +2 -0
     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)
#7 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 19 lines · Markus Armbruster
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

+/* 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.
+ */

Is the protocol defined anywhere?

https://github.com/yoe/nbd/blob/master/doc/proto.txt

All it has on the error code is this paragraph:

The reply contains three fields: a 32 bit magic number ('magic'), a 32 bit error code ('error'; 0, unless an error occurred in which case it is the errno of the error on the server side), and the same 64 bit handle that the corresponding request had in its 'handle' field. In case the reply is sent in response to a read request and the error field is 0 (zero), the reply header is immediately followed by request.len bytes of data.

Quoted 2 lines

Could you update it to document the errno compatibility issues, and recommended practice (i.e. this patch's)?

Yes, I've sent a patch yesterday.

Quoted 55 lines
Quoted 46 lines
Quoted 46 lines
Quoted 17 lines
nbd.c +17 -0
+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;

nbd.c line 119

Quoted 10 lines
nbd.c +10 -0
+    case ENOSPC:
+        return 28;
+    case EROFS:
+        return 30;
+    case EINVAL:
+    default:
+        return 22;
+    }
+}
+

This maps recognized OS errnos to NBD errnos. The latter are literals.

Quoted 16 lines

/* Definitions for opaque data types */

typedef struct NBDRequest NBDRequest;

nbd.c +9 -0
@@ -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);
Quoted 8 lines
Quoted 8 lines

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 5 lines
nbd.c +5 -0
+    QEMU_BUILD_BUG_ON(EINVAL != 22);
+    QEMU_BUILD_BUG_ON(EFBIG != 27);
+    QEMU_BUILD_BUG_ON(ENOSPC != 28);
+    QEMU_BUILD_BUG_ON(EROFS != 30);
+

This checks that the mapping above is the identify function for all the recognized NBD errnos. Why is that necessary?

Quoted 1 line

Still curious. Explain, and earn my R-by :)

Because block/nbd.c expects host errnos, and I was too lazy to add a switch and a nbd_errno_to_system_errno function. But Eric pointed out that Hurd has weird errnos (the low bits match, but there's a 0x10 subsystem code in bits 24-31) so I'll add it.

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Same literals as above. Violates DRY. I don't mind all that much, but wonder whether we could at least do the checking next to system_errno_to_nbd_errno().

Could we?

Yes, s/could/should/. Also, should have added RFC to the patch.

Paolo

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
nbd.c +2 -0
     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)
#8 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · Markus Armbruster
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

+/* 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.
+ */

Is the protocol defined anywhere?

https://github.com/yoe/nbd/blob/master/doc/proto.txt

All it has on the error code is this paragraph:

The reply contains three fields: a 32 bit magic number ('magic'), a 32 bit error code ('error'; 0, unless an error occurred in which case it is the errno of the error on the server side), and the same 64 bit handle that the corresponding request had in its 'handle' field. In case the reply is sent in response to a read request and the error field is 0 (zero), the reply header is immediately followed by request.len bytes of data.

Quoted 4 lines
Quoted 2 lines

Could you update it to document the errno compatibility issues, and recommended practice (i.e. this patch's)?

Yes, I've sent a patch yesterday.

Excellent!

Quoted 55 lines
Quoted 55 lines
Quoted 54 lines
Quoted 54 lines
Quoted 17 lines
nbd.c +17 -0
+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;

nbd.c line 119

Quoted 10 lines
nbd.c +10 -0
+    case ENOSPC:
+        return 28;
+    case EROFS:
+        return 30;
+    case EINVAL:
+    default:
+        return 22;
+    }
+}
+

This maps recognized OS errnos to NBD errnos. The latter are literals.

Quoted 11 lines

/* Definitions for opaque data types */

typedef struct NBDRequest NBDRequest;

nbd.c +4 -0
@@ -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);

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 10 lines
nbd.c +10 -0
+    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);
+

This checks that the mapping above is the identify function for all the recognized NBD errnos. Why is that necessary?

Quoted 6 lines
Quoted 1 line

Still curious. Explain, and earn my R-by :)

Because block/nbd.c expects host errnos, and I was too lazy to add a switch and a nbd_errno_to_system_errno function. But Eric pointed out that Hurd has weird errnos (the low bits match, but there's a 0x10 subsystem code in bits 24-31) so I'll add it.

Oww! That's going to hurd...

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Same literals as above. Violates DRY. I don't mind all that much, but wonder whether we could at least do the checking next to system_errno_to_nbd_errno().

Could we?

Yes, s/could/should/. Also, should have added RFC to the patch.

Thanks!

#9 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

In reply to Paolo Bonzini
Quoted 66 lines · Paolo Bonzini

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.

---
  nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 47 insertions(+)
nbd.c +35 -0
index eea8c51..1ad5b66 100644
@@ -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);

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 10 lines
nbd.c +10 -0
+    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);
+

Why no nbd_errno_to_system_errno() function?

Max

Quoted 12 lines
nbd.c +2 -0
      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)
#10 lore raw

Re: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire

Quoted 69 lines · Max Reitz
Quoted 68 lines · Paolo Bonzini

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.

---
  nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 47 insertions(+)
nbd.c +37 -0
index eea8c51..1ad5b66 100644
@@ -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);
Quoted 8 lines

nbd.c ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) · line 903

Quoted 6 lines
nbd.c +6 -0
+    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);
+

Why no nbd_errno_to_system_errno() function?

Oops, I missed v2. Sorry.

Max