Skip to content

Commit 31c533d

Browse files
committed
log: Introduce libnvme error codes
Add libnvme specific error codes which map to a specific error message. The system error codes in errno are too overloaded to figure out what is going wrong. With this we can remove the nvme_log_message buffer. This avoids having a global library buffer. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 6b951c5 commit 31c533d

6 files changed

Lines changed: 89 additions & 37 deletions

File tree

libnvme/nvme.i

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,51 @@ static int discover_err = 0;
9494

9595
%exception nvme_ctrl::connect {
9696
connect_err = 0;
97+
errno = 0;
9798
$action /* $action sets connect_err to non-zero value on failure */
9899
if (connect_err == 1) {
99100
SWIG_exception(SWIG_AttributeError, "Existing controller connection");
100101
} else if (connect_err) {
101-
if (nvme_log_message)
102-
SWIG_exception(SWIG_RuntimeError, nvme_log_message);
103-
else
104-
SWIG_exception(SWIG_RuntimeError, "Connect failed");
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");
141+
}
105142
}
106143
}
107144

src/libnvme.map

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
LIBNVME_1_0 {
22
global:
33
__nvme_get_log_page;
4-
__nvme_msg;
54
nvme_admin_passthru64;
65
nvme_admin_passthru;
76
nvme_attach_ns;
@@ -174,7 +173,6 @@ LIBNVME_1_0 {
174173
nvme_io_passthru;
175174
nvme_lockdown;
176175
nvme_log_level;
177-
nvme_log_message;
178176
nvme_lookup_host;
179177
nvme_lookup_subsystem;
180178
nvme_namespace_attach_ctrls;

src/nvme/fabrics.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static int hostname2traddr(nvme_ctrl_t c)
361361
ret = getaddrinfo(c->traddr, NULL, &hints, &host_info);
362362
if (ret) {
363363
nvme_msg(LOG_ERR, "failed to resolve host %s info\n", c->traddr);
364-
return ret;
364+
return -ENVME_CONNECT_RESOLVE;
365365
}
366366

367367
switch (host_info->ai_family) {
@@ -378,13 +378,13 @@ static int hostname2traddr(nvme_ctrl_t c)
378378
default:
379379
nvme_msg(LOG_ERR, "unrecognized address family (%d) %s\n",
380380
host_info->ai_family, c->traddr);
381-
ret = -EINVAL;
381+
ret = -ENVME_CONNECT_ADDRFAM;
382382
goto free_addrinfo;
383383
}
384384

385385
if (!p) {
386386
nvme_msg(LOG_ERR, "failed to get traddr for %s\n", c->traddr);
387-
ret = -errno;
387+
ret = -ENVME_CONNECT_TRADDR;
388388
goto free_addrinfo;
389389
}
390390
c->traddr = strdup(addrstr);
@@ -403,15 +403,13 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
403403

404404
if (!transport) {
405405
nvme_msg(LOG_ERR, "need a transport (-t) argument\n");
406-
errno = EINVAL;
407-
return -1;
406+
return -ENVME_CONNECT_TARG;
408407
}
409408

410409
if (strncmp(transport, "loop", 4)) {
411410
if (!nvme_ctrl_get_traddr(c)) {
412411
nvme_msg(LOG_ERR, "need a address (-a) argument\n");
413-
errno = EINVAL;
414-
return -1;
412+
return -ENVME_CONNECT_AARG;
415413
}
416414
/* Use the default ctrl loss timeout if unset */
417415
if (cfg->ctrl_loss_tmo == -1)
@@ -421,8 +419,7 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
421419
/* always specify nqn as first arg - this will init the string */
422420
if (asprintf(argstr, "nqn=%s",
423421
nvme_ctrl_get_subsysnqn(c)) < 0) {
424-
errno = ENOMEM;
425-
return -1;
422+
return -ENOMEM;
426423
}
427424
if (!strcmp(nvme_ctrl_get_subsysnqn(c), NVME_DISC_SUBSYS_NAME)) {
428425
nvme_ctrl_set_discovery_ctrl(c, true);
@@ -499,7 +496,7 @@ static int __nvmf_add_ctrl(const char *argstr)
499496
if (fd < 0) {
500497
nvme_msg(LOG_ERR, "Failed to open %s: %s\n",
501498
nvmf_dev, strerror(errno));
502-
return -1;
499+
return -ENVME_CONNECT_OPEN;
503500
}
504501

505502
nvme_msg(LOG_DEBUG, "connect ctrl, '%.*s'\n",
@@ -508,15 +505,15 @@ static int __nvmf_add_ctrl(const char *argstr)
508505
if (ret != len) {
509506
nvme_msg(LOG_NOTICE, "Failed to write to %s: %s\n",
510507
nvmf_dev, strerror(errno));
511-
ret = -1;
508+
ret = -ENVME_CONNECT_WRITE;
512509
goto out_close;
513510
}
514511

515512
len = read(fd, buf, sizeof(buf));
516513
if (len < 0) {
517514
nvme_msg(LOG_ERR, "Failed to read from %s: %s\n",
518515
nvmf_dev, strerror(errno));
519-
ret = -1;
516+
ret = -ENVME_CONNECT_READ;
520517
goto out_close;
521518
}
522519
nvme_msg(LOG_DEBUG, "connect ctrl, response '%.*s'\n",
@@ -531,8 +528,7 @@ static int __nvmf_add_ctrl(const char *argstr)
531528
}
532529

533530
nvme_msg(LOG_ERR, "Failed to parse ctrl info for \"%s\"\n", argstr);
534-
errno = EINVAL;
535-
ret = -1;
531+
ret = -ENVME_CONNECT_PARSE;
536532
out_close:
537533
close(fd);
538534
return ret;
@@ -548,18 +544,26 @@ int nvmf_add_ctrl_opts(nvme_ctrl_t c, struct nvme_fabrics_config *cfg)
548544
cfg = merge_config(c, cfg);
549545
if (traddr_is_hostname(c)) {
550546
ret = hostname2traddr(c);
551-
if (ret)
552-
return ret;
547+
if (ret) {
548+
errno = -ret;
549+
return -1;
550+
}
553551
}
554552

555553
ret = build_options(h, c, &argstr);
556-
if (ret)
557-
return ret;
554+
if (ret) {
555+
errno = -ret;
556+
return -1;
557+
}
558558

559559
ret = __nvmf_add_ctrl(argstr);
560560
free(argstr);
561-
if (ret >= 0)
561+
if (ret < 0) {
562+
errno = -ret;
563+
ret = -1;
564+
} else {
562565
nvme_msg(LOG_INFO, "nvme%d: ctrl connected\n", ret);
566+
}
563567
return ret;
564568
}
565569

@@ -575,8 +579,10 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
575579
nvme_ctrl_set_discovered(c, true);
576580
if (traddr_is_hostname(c)) {
577581
ret = hostname2traddr(c);
578-
if (ret)
579-
return ret;
582+
if (ret) {
583+
errno = -ret;
584+
return -1;
585+
}
580586
}
581587

582588
ret = build_options(h, c, &argstr);
@@ -585,8 +591,10 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
585591

586592
ret = __nvmf_add_ctrl(argstr);
587593
free(argstr);
588-
if (ret < 0)
589-
return ret;
594+
if (ret < 0) {
595+
errno = -ret;
596+
return -1;
597+
}
590598

591599
nvme_msg(LOG_INFO, "nvme%d: ctrl connected\n", ret);
592600
return nvme_init_ctrl(h, c, ret);

src/nvme/ioctl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
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+
2741
/* '0' is interpreted by the kernel to mean 'apply the default timeout' */
2842
#define NVME_DEFAULT_IOCTL_TIMEOUT 0
2943

src/nvme/log.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
int nvme_log_level = DEFAULT_LOGLEVEL;
3636
bool nvme_log_timestamp;
3737
bool nvme_log_pid;
38-
char *nvme_log_message = NULL;
3938

4039
void __attribute__((format(printf, 3, 4)))
4140
__nvme_msg(int lvl, const char *func, const char *format, ...)
@@ -83,10 +82,6 @@ __nvme_msg(int lvl, const char *func, const char *format, ...)
8382
message = NULL;
8483
va_end(ap);
8584

86-
if (nvme_log_message)
87-
free(nvme_log_message);
88-
nvme_log_message = strdup(message);
89-
9085
if (lvl <= nvme_log_level)
9186
fprintf(stderr, "%s%s", header ? header : "<error>",
9287
message ? message : "<error>");

src/nvme/tree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
11561156
if (strcmp(c->transport, "loop")) {
11571157
c->address = nvme_get_attr(path, "address");
11581158
if (!c->address) {
1159-
errno = ENXIO;
1159+
errno = ENVME_CONNECT_INVAL_TR;
11601160
ret = -1;
11611161
goto out_free_name;
11621162
}
@@ -1166,13 +1166,13 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
11661166
if (!subsys_name) {
11671167
nvme_msg(LOG_ERR, "Failed to lookup subsystem name for %s\n",
11681168
c->name);
1169-
errno = ENXIO;
1169+
errno = ENVME_CONNECT_LOOKUP_SUBSYS_NAME;
11701170
ret = -1;
11711171
goto out_free_name;
11721172
}
11731173
s = nvme_lookup_subsystem(h, subsys_name, c->subsysnqn);
11741174
if (!s) {
1175-
errno = ENXIO;
1175+
errno = ENVME_CONNECT_LOOKUP_SUBSYS;
11761176
ret = -1;
11771177
goto out_free_subsys;
11781178
}

0 commit comments

Comments
 (0)