Skip to content

Commit 16a38f7

Browse files
committed
fabrics: refactor error propagation in nvmf_connect
With the introduction of nvme_add_ctrl we can use the return code directly and don't rely on the errno being set. Also we can directly leave the function on error because we don't have to cleanup things explicitly anymore. Signed-off-by: Daniel Wagner <[email protected]>
1 parent f757c60 commit 16a38f7

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

fabrics.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ static int nvme_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
178178
int ret;
179179

180180
retry:
181+
/*
182+
* __create_discover_ctrl and callers depend on errno being set
183+
* in the error case.
184+
*/
185+
errno = 0;
181186
ret = nvmf_add_ctrl(h, c, cfg);
182187
if (!ret)
183188
return 0;
@@ -208,9 +213,7 @@ static nvme_ctrl_t __create_discover_ctrl(nvme_root_t r, nvme_host_t h,
208213
strcmp(trcfg->subsysnqn, NVME_DISC_SUBSYS_NAME));
209214
tmo = set_discovery_kato(cfg);
210215

211-
errno = 0;
212216
ret = nvme_add_ctrl(h, c, cfg);
213-
214217
cfg->keep_alive_tmo = tmo;
215218
if (ret) {
216219
nvme_free_ctrl(c);
@@ -1082,10 +1085,8 @@ int nvmf_connect(const char *desc, int argc, char **argv)
10821085
return -errno;
10831086

10841087
h = nvme_lookup_host(r, hnqn, hid);
1085-
if (!h) {
1086-
errno = ENOMEM;
1087-
goto out_free;
1088-
}
1088+
if (!h)
1089+
return -ENOMEM;
10891090
if (hostkey)
10901091
nvme_host_set_dhchap_key(h, hostkey);
10911092
if (!trsvcid)
@@ -1106,37 +1107,33 @@ int nvmf_connect(const char *desc, int argc, char **argv)
11061107
c = lookup_ctrl(h, &trcfg);
11071108
if (c && nvme_ctrl_get_name(c) && !cfg.duplicate_connect) {
11081109
fprintf(stderr, "already connected\n");
1109-
errno = EALREADY;
1110-
goto out_free;
1110+
return -EALREADY;
11111111
}
11121112

11131113
c = nvme_create_ctrl(r, subsysnqn, transport, traddr,
11141114
cfg.host_traddr, cfg.host_iface, trsvcid);
1115-
if (!c) {
1116-
errno = ENOMEM;
1117-
goto out_free;
1118-
}
1115+
if (!c)
1116+
return -ENOMEM;
11191117

11201118
if (ctrlkey)
11211119
nvme_ctrl_set_dhchap_key(c, ctrlkey);
11221120

11231121
nvme_parse_tls_args(keyring, tls_key, tls_key_identity, &cfg, c);
11241122

1125-
errno = 0;
11261123
ret = nvme_add_ctrl(h, c, &cfg);
1127-
if (ret)
1124+
if (ret) {
11281125
fprintf(stderr, "could not add new controller: %s\n",
1129-
nvme_strerror(errno));
1130-
else {
1131-
errno = 0;
1132-
if (flags != -EINVAL)
1133-
nvme_show_connect_msg(c, flags);
1126+
nvme_strerror(-ret));
1127+
return ret;
11341128
}
11351129

1136-
out_free:
1130+
/* always print connected device */
1131+
nvme_show_connect_msg(c, flags);
1132+
11371133
if (dump_config)
11381134
nvme_dump_config(r);
1139-
return -errno;
1135+
1136+
return 0;
11401137
}
11411138

11421139
static nvme_ctrl_t lookup_nvme_ctrl(nvme_root_t r, const char *name)

0 commit comments

Comments
 (0)