Skip to content

Commit 7c39cf1

Browse files
authored
Merge pull request #173 from igaw/add-error-codes
log: Introduce libnvme error codes
2 parents 209c5ab + 31c533d commit 7c39cf1

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
@@ -363,7 +363,7 @@ static int hostname2traddr(nvme_ctrl_t c)
363363
ret = getaddrinfo(c->traddr, NULL, &hints, &host_info);
364364
if (ret) {
365365
nvme_msg(LOG_ERR, "failed to resolve host %s info\n", c->traddr);
366-
return ret;
366+
return -ENVME_CONNECT_RESOLVE;
367367
}
368368

369369
switch (host_info->ai_family) {
@@ -380,13 +380,13 @@ static int hostname2traddr(nvme_ctrl_t c)
380380
default:
381381
nvme_msg(LOG_ERR, "unrecognized address family (%d) %s\n",
382382
host_info->ai_family, c->traddr);
383-
ret = -EINVAL;
383+
ret = -ENVME_CONNECT_ADDRFAM;
384384
goto free_addrinfo;
385385
}
386386

387387
if (!p) {
388388
nvme_msg(LOG_ERR, "failed to get traddr for %s\n", c->traddr);
389-
ret = -errno;
389+
ret = -ENVME_CONNECT_TRADDR;
390390
goto free_addrinfo;
391391
}
392392
c->traddr = strdup(addrstr);
@@ -405,15 +405,13 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
405405

406406
if (!transport) {
407407
nvme_msg(LOG_ERR, "need a transport (-t) argument\n");
408-
errno = EINVAL;
409-
return -1;
408+
return -ENVME_CONNECT_TARG;
410409
}
411410

412411
if (strncmp(transport, "loop", 4)) {
413412
if (!nvme_ctrl_get_traddr(c)) {
414413
nvme_msg(LOG_ERR, "need a address (-a) argument\n");
415-
errno = EINVAL;
416-
return -1;
414+
return -ENVME_CONNECT_AARG;
417415
}
418416
/* Use the default ctrl loss timeout if unset */
419417
if (cfg->ctrl_loss_tmo == -1)
@@ -423,8 +421,7 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
423421
/* always specify nqn as first arg - this will init the string */
424422
if (asprintf(argstr, "nqn=%s",
425423
nvme_ctrl_get_subsysnqn(c)) < 0) {
426-
errno = ENOMEM;
427-
return -1;
424+
return -ENOMEM;
428425
}
429426
if (!strcmp(nvme_ctrl_get_subsysnqn(c), NVME_DISC_SUBSYS_NAME)) {
430427
nvme_ctrl_set_discovery_ctrl(c, true);
@@ -501,7 +498,7 @@ static int __nvmf_add_ctrl(const char *argstr)
501498
if (fd < 0) {
502499
nvme_msg(LOG_ERR, "Failed to open %s: %s\n",
503500
nvmf_dev, strerror(errno));
504-
return -1;
501+
return -ENVME_CONNECT_OPEN;
505502
}
506503

507504
nvme_msg(LOG_DEBUG, "connect ctrl, '%.*s'\n",
@@ -510,15 +507,15 @@ static int __nvmf_add_ctrl(const char *argstr)
510507
if (ret != len) {
511508
nvme_msg(LOG_NOTICE, "Failed to write to %s: %s\n",
512509
nvmf_dev, strerror(errno));
513-
ret = -1;
510+
ret = -ENVME_CONNECT_WRITE;
514511
goto out_close;
515512
}
516513

517514
len = read(fd, buf, sizeof(buf));
518515
if (len < 0) {
519516
nvme_msg(LOG_ERR, "Failed to read from %s: %s\n",
520517
nvmf_dev, strerror(errno));
521-
ret = -1;
518+
ret = -ENVME_CONNECT_READ;
522519
goto out_close;
523520
}
524521
nvme_msg(LOG_DEBUG, "connect ctrl, response '%.*s'\n",
@@ -533,8 +530,7 @@ static int __nvmf_add_ctrl(const char *argstr)
533530
}
534531

535532
nvme_msg(LOG_ERR, "Failed to parse ctrl info for \"%s\"\n", argstr);
536-
errno = EINVAL;
537-
ret = -1;
533+
ret = -ENVME_CONNECT_PARSE;
538534
out_close:
539535
close(fd);
540536
return ret;
@@ -550,18 +546,26 @@ int nvmf_add_ctrl_opts(nvme_ctrl_t c, struct nvme_fabrics_config *cfg)
550546
cfg = merge_config(c, cfg);
551547
if (traddr_is_hostname(c)) {
552548
ret = hostname2traddr(c);
553-
if (ret)
554-
return ret;
549+
if (ret) {
550+
errno = -ret;
551+
return -1;
552+
}
555553
}
556554

557555
ret = build_options(h, c, &argstr);
558-
if (ret)
559-
return ret;
556+
if (ret) {
557+
errno = -ret;
558+
return -1;
559+
}
560560

561561
ret = __nvmf_add_ctrl(argstr);
562562
free(argstr);
563-
if (ret >= 0)
563+
if (ret < 0) {
564+
errno = -ret;
565+
ret = -1;
566+
} else {
564567
nvme_msg(LOG_INFO, "nvme%d: ctrl connected\n", ret);
568+
}
565569
return ret;
566570
}
567571

@@ -577,8 +581,10 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
577581
nvme_ctrl_set_discovered(c, true);
578582
if (traddr_is_hostname(c)) {
579583
ret = hostname2traddr(c);
580-
if (ret)
581-
return ret;
584+
if (ret) {
585+
errno = -ret;
586+
return -1;
587+
}
582588
}
583589

584590
ret = build_options(h, c, &argstr);
@@ -587,8 +593,10 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
587593

588594
ret = __nvmf_add_ctrl(argstr);
589595
free(argstr);
590-
if (ret < 0)
591-
return ret;
596+
if (ret < 0) {
597+
errno = -ret;
598+
return -1;
599+
}
592600

593601
nvme_msg(LOG_INFO, "nvme%d: ctrl connected\n", ret);
594602
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)