Skip to content

Commit

Permalink
nbd: Move nbd_errno_to_system_errno() to public header
Browse files Browse the repository at this point in the history
This is needed in preparation for structured reply handling,
as we will be performing the translation from NBD error to
system errno value higher in the stack at block/nbd-client.c.

Signed-off-by: Eric Blake <[email protected]>
Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
ebblake committed Oct 30, 2017
1 parent e7a78d0 commit dd68944
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 45 deletions.
13 changes: 13 additions & 0 deletions include/block/nbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ enum {
* aren't overflowing some other buffer. */
#define NBD_MAX_NAME_SIZE 256

/* 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.
*/
#define NBD_SUCCESS 0
#define NBD_EPERM 1
#define NBD_EIO 5
#define NBD_ENOMEM 12
#define NBD_EINVAL 22
#define NBD_ENOSPC 28
#define NBD_ESHUTDOWN 108

/* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */
struct NBDExportInfo {
/* Set by client before nbd_receive_negotiate() */
Expand All @@ -172,6 +184,7 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *request);
int nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp);
int nbd_client(int fd);
int nbd_disconnect(int fd);
int nbd_errno_to_system_errno(int err);

typedef struct NBDExport NBDExport;
typedef struct NBDClient NBDClient;
Expand Down
32 changes: 0 additions & 32 deletions nbd/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,6 @@
#include "trace.h"
#include "nbd-internal.h"

static int nbd_errno_to_system_errno(int err)
{
int ret;
switch (err) {
case NBD_SUCCESS:
ret = 0;
break;
case NBD_EPERM:
ret = EPERM;
break;
case NBD_EIO:
ret = EIO;
break;
case NBD_ENOMEM:
ret = ENOMEM;
break;
case NBD_ENOSPC:
ret = ENOSPC;
break;
case NBD_ESHUTDOWN:
ret = ESHUTDOWN;
break;
default:
trace_nbd_unknown_error(err);
/* fallthrough */
case NBD_EINVAL:
ret = EINVAL;
break;
}
return ret;
}

/* Definitions for opaque data types */

static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
Expand Down
34 changes: 34 additions & 0 deletions nbd/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "qemu/osdep.h"
#include "qapi/error.h"
#include "trace.h"
#include "nbd-internal.h"

/* Discard length bytes from channel. Return -errno on failure and 0 on
Expand Down Expand Up @@ -171,3 +172,36 @@ const char *nbd_err_lookup(int err)
return "<unknown>";
}
}


int nbd_errno_to_system_errno(int err)
{
int ret;
switch (err) {
case NBD_SUCCESS:
ret = 0;
break;
case NBD_EPERM:
ret = EPERM;
break;
case NBD_EIO:
ret = EIO;
break;
case NBD_ENOMEM:
ret = ENOMEM;
break;
case NBD_ENOSPC:
ret = ENOSPC;
break;
case NBD_ESHUTDOWN:
ret = ESHUTDOWN;
break;
default:
trace_nbd_unknown_error(err);
/* fallthrough */
case NBD_EINVAL:
ret = EINVAL;
break;
}
return ret;
}
12 changes: 0 additions & 12 deletions nbd/nbd-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@
#define NBD_SET_TIMEOUT _IO(0xab, 9)
#define NBD_SET_FLAGS _IO(0xab, 10)

/* 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.
*/
#define NBD_SUCCESS 0
#define NBD_EPERM 1
#define NBD_EIO 5
#define NBD_ENOMEM 12
#define NBD_EINVAL 22
#define NBD_ENOSPC 28
#define NBD_ESHUTDOWN 108

/* nbd_read_eof
* Tries to read @size bytes from @ioc.
* Returns 1 on success
Expand Down
4 changes: 3 additions & 1 deletion nbd/trace-events
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# nbd/client.c
nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL"
nbd_send_option_request(uint32_t opt, const char *name, uint32_t len) "Sending option request %" PRIu32" (%s), len %" PRIu32
nbd_receive_option_reply(uint32_t option, const char *optname, uint32_t type, const char *typename, uint32_t length) "Received option reply 0x%" PRIx32" (%s), type 0x%" PRIx32" (%s), len %" PRIu32
nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't understand request 0x%" PRIx32 " (%s), attempting fallback"
Expand Down Expand Up @@ -31,6 +30,9 @@ nbd_client_clear_socket(void) "Clearing NBD socket"
nbd_send_request(uint64_t from, uint32_t len, uint64_t handle, uint16_t flags, uint16_t type, const char *name) "Sending request to server: { .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64 ", .flags = 0x%" PRIx16 ", .type = %" PRIu16 " (%s) }"
nbd_receive_reply(uint32_t magic, int32_t error, const char *errname, uint64_t handle) "Got reply: { magic = 0x%" PRIx32 ", .error = %" PRId32 " (%s), handle = %" PRIu64" }"

# nbd/common.c
nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL"

# nbd/server.c
nbd_negotiate_send_rep_len(uint32_t opt, const char *optname, uint32_t type, const char *typename, uint32_t len) "Reply opt=0x%" PRIx32 " (%s), type=0x%" PRIx32 " (%s), len=%" PRIu32
nbd_negotiate_send_rep_err(const char *msg) "sending error message \"%s\""
Expand Down

0 comments on commit dd68944

Please sign in to comment.