Skip to content

Commit cde70c4

Browse files
committed
Implement nvme_errno_to_string()
Add a function nvme_errno_to_string() to provide a string describing the error from nvme_add_ctrl(). Signed-off-by: Hannes Reinecke <[email protected]>
1 parent c66a5a7 commit cde70c4

5 files changed

Lines changed: 50 additions & 53 deletions

File tree

libnvme/nvme.i

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -99,45 +99,11 @@ static int discover_err = 0;
9999
if (connect_err == 1) {
100100
SWIG_exception(SWIG_AttributeError, "Existing controller connection");
101101
} else if (connect_err) {
102-
switch (errno) {
103-
case ENVME_CONNECT_RESOLVE:
104-
SWIG_exception(SWIG_RuntimeError, "failed to resolve host");
105-
break;
106-
case ENVME_CONNECT_ADDRFAM:
107-
SWIG_exception(SWIG_RuntimeError, "unrecognized address family");
108-
break;
109-
case ENVME_CONNECT_TRADDR:
110-
SWIG_exception(SWIG_RuntimeError, "failed to get traddr");
111-
break;
112-
case ENVME_CONNECT_TARG:
113-
SWIG_exception(SWIG_RuntimeError, "need a transport (-t) argument");
114-
break;
115-
case ENVME_CONNECT_AARG:
116-
SWIG_exception(SWIG_RuntimeError, "need a address (-a) argument\n");
117-
break;
118-
case ENVME_CONNECT_OPEN:
119-
SWIG_exception(SWIG_RuntimeError, "failed to open nvme-fabrics device");
120-
break;
121-
case ENVME_CONNECT_WRITE:
122-
SWIG_exception(SWIG_RuntimeError, "failed to write to nvme-fabrics device");
123-
break;
124-
case ENVME_CONNECT_READ:
125-
SWIG_exception(SWIG_RuntimeError, "failed to read from nvme-fabrics device");
126-
break;
127-
case ENVME_CONNECT_PARSE:
128-
SWIG_exception(SWIG_RuntimeError, "failed to parse ctrl info");
129-
break;
130-
case ENVME_CONNECT_INVAL_TR:
131-
SWIG_exception(SWIG_RuntimeError, "invalid transport type");
132-
break;
133-
case ENVME_CONNECT_LOOKUP_SUBSYS_NAME:
134-
SWIG_exception(SWIG_RuntimeError, "failed to lookup subsystem name");
135-
break;
136-
case ENVME_CONNECT_LOOKUP_SUBSYS:
137-
SWIG_exception(SWIG_RuntimeError, "failed to lookup subsystem");
138-
break;
139-
default:
140-
SWIG_exception(SWIG_RuntimeError, "Connect failed");
102+
const char *errstr = nvme_errno_to_string(errno);
103+
if (errstr) {
104+
SWIG_exception(SWIG_RuntimeError, errstr);
105+
} else {
106+
SWIG_exception(SWIG_RuntimeError, "Connect failed");
141107
}
142108
}
143109
}

src/libnvme.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ LIBNVME_1_0 {
5656
nvme_dsm;
5757
nvme_dsm_range;
5858
nvme_dump_config;
59+
nvme_errno_to_string;
5960
nvme_first_host;
6061
nvme_first_subsystem;
6162
nvme_flush;

src/nvme/ioctl.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@
2424
#ifndef _LINUX_NVME_IOCTL_H
2525
#define _LINUX_NVME_IOCTL_H
2626

27-
/* libnvme errno error codes */
28-
#define ENVME_CONNECT_RESOLVE 1000 /* "failed to resolve host" */
29-
#define ENVME_CONNECT_ADDRFAM 1001 /* "unrecognized address family" */
30-
#define ENVME_CONNECT_TRADDR 1002 /* "failed to get traddr" */
31-
#define ENVME_CONNECT_TARG 1003 /* "need a transport (-t) argument" */
32-
#define ENVME_CONNECT_AARG 1004 /* "need a address (-a) argument\n" */
33-
#define ENVME_CONNECT_OPEN 1005 /* "failed to open nvme-fabrics device" */
34-
#define ENVME_CONNECT_WRITE 1006 /* "failed to write to nvme-fabrics device" */
35-
#define ENVME_CONNECT_READ 1007 /* "failed to read from nvme-fabrics device" */
36-
#define ENVME_CONNECT_PARSE 1008 /* "failed to parse ctrl info" */
37-
#define ENVME_CONNECT_INVAL_TR 1009 /* "invalid transport type" */
38-
#define ENVME_CONNECT_LOOKUP_SUBSYS_NAME 1010 /* "failed to lookup subsystem name" */
39-
#define ENVME_CONNECT_LOOKUP_SUBSYS 1011 /* "failed to lookup subsystem */
40-
4127
/* '0' is interpreted by the kernel to mean 'apply the default timeout' */
4228
#define NVME_DEFAULT_IOCTL_TIMEOUT 0
4329

src/nvme/util.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,25 @@ int nvme_get_directive_receive_length(enum nvme_directive_dtype dtype,
476476
return -EINVAL;
477477
}
478478
}
479+
480+
static const char * const libnvme_status[] = {
481+
[ENVME_CONNECT_RESOLVE] = "failed to resolve host",
482+
[ENVME_CONNECT_ADDRFAM] = "unrecognized address family",
483+
[ENVME_CONNECT_TRADDR] = "failed to get transport address",
484+
[ENVME_CONNECT_TARG] = "no transport specified",
485+
[ENVME_CONNECT_AARG] = "no transport address specified",
486+
[ENVME_CONNECT_OPEN] = "failed to open nvme-fabrics device",
487+
[ENVME_CONNECT_WRITE] = "failed to write to nvme-fabrics device",
488+
[ENVME_CONNECT_READ] = "failed to read from nvme-fabrics device",
489+
[ENVME_CONNECT_PARSE] = "failed to parse ctrl info",
490+
[ENVME_CONNECT_INVAL_TR] = "invalid transport type",
491+
[ENVME_CONNECT_LOOKUP_SUBSYS_NAME] = "failed to lookup subsystem name",
492+
[ENVME_CONNECT_LOOKUP_SUBSYS] = "failed to lookup subsystem",
493+
};
494+
495+
const char *nvme_errno_to_string(int status)
496+
{
497+
const char *s = ARGSTR(libnvme_status, status);
498+
499+
return s;
500+
}

src/nvme/util.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111

1212
#include "types.h"
1313

14+
/* nvme connect error codes */
15+
#define ENVME_CONNECT_RESOLVE 1000 /* "failed to resolve host" */
16+
#define ENVME_CONNECT_ADDRFAM 1001 /* "unrecognized address family" */
17+
#define ENVME_CONNECT_TRADDR 1002 /* "failed to get traddr" */
18+
#define ENVME_CONNECT_TARG 1003 /* "need a transport (-t) argument" */
19+
#define ENVME_CONNECT_AARG 1004 /* "need a address (-a) argument\n" */
20+
#define ENVME_CONNECT_OPEN 1005 /* "failed to open nvme-fabrics device" */
21+
#define ENVME_CONNECT_WRITE 1006 /* "failed to write to nvme-fabrics device" */
22+
#define ENVME_CONNECT_READ 1007 /* "failed to read from nvme-fabrics device" */
23+
#define ENVME_CONNECT_PARSE 1008 /* "failed to parse ctrl info" */
24+
#define ENVME_CONNECT_INVAL_TR 1009 /* "invalid transport type" */
25+
#define ENVME_CONNECT_LOOKUP_SUBSYS_NAME 1010 /* "failed to lookup subsystem name" */
26+
#define ENVME_CONNECT_LOOKUP_SUBSYS 1011 /* "failed to lookup subsystem */
27+
1428
/**
1529
* nvme_status_to_errno() - Converts nvme return status to errno
1630
* @status: Return status from an nvme passthrough commmand
@@ -31,6 +45,14 @@ __u8 nvme_status_to_errno(int status, bool fabrics);
3145
*/
3246
const char *nvme_status_to_string(int status, bool fabrics);
3347

48+
/**
49+
* nvme_errno_to_string() - Returns string describing nvme connect failures
50+
* @err: Returned error code from nvme_add_ctrl()
51+
*
52+
* Return: String representation of the nvme connect error codes
53+
*/
54+
const char *nvme_errno_to_string(int err);
55+
3456
/**
3557
* nvme_init_id_ns() - Initialize an Identify Namepsace structure for creation.
3658
* @ns: Address of the Identify Namespace structure to initialize

0 commit comments

Comments
 (0)