Skip to content

Commit 9354c61

Browse files
tbzatekigaw
authored andcommitted
nbft: Report proper error codes from discover_from_nbft()
In case of a connection failure of any SSNS records that are not marked as 'unavailable', report proper error code so that nvme-cli callers may retry the operation. The semantics of most functions is to return 0 zero for success and negative errno numbers in case of an error. Signed-off-by: Tomas Bzatek <[email protected]>
1 parent cbfd136 commit 9354c61

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

fabrics.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ static int nvme_read_config_checked(nvme_root_t r, const char *filename)
681681
return 0;
682682
}
683683

684+
/* returns negative errno values */
684685
int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
685686
{
686687
char *subsysnqn = NVME_DISC_SUBSYS_NAME;
@@ -757,7 +758,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
757758
if (ret < 0) {
758759
fprintf(stderr, "Failed to scan topology: %s\n",
759760
nvme_strerror(errno));
760-
return ret;
761+
return -errno;
761762
}
762763

763764
ret = nvme_host_get_ids(r, hostnqn, hostid, &hnqn, &hid);
@@ -766,7 +767,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
766767

767768
h = nvme_lookup_host(r, hnqn, hid);
768769
if (!h) {
769-
ret = ENOMEM;
770+
ret = -ENOMEM;
770771
goto out_free;
771772
}
772773

@@ -781,10 +782,9 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
781782

782783
if (!device && !transport && !traddr) {
783784
if (!nonbft)
784-
discover_from_nbft(r, hostnqn, hostid,
785-
hnqn, hid, desc, connect,
786-
&cfg, nbft_path, flags, verbose);
787-
785+
ret = discover_from_nbft(r, hostnqn, hostid,
786+
hnqn, hid, desc, connect,
787+
&cfg, nbft_path, flags, verbose);
788788
if (nbft)
789789
goto out_free;
790790

@@ -876,7 +876,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
876876
fprintf(stderr,
877877
"failed to add controller, error %s\n",
878878
nvme_strerror(errno));
879-
ret = errno;
879+
ret = -errno;
880880
goto out_free;
881881
}
882882
}

nbft.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ static int do_discover(struct nbft_info_discovery *dd,
282282
return 0;
283283
}
284284

285+
/* returns negative errno values */
285286
int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
286287
char *hostnqn_sys, char *hostid_sys,
287288
const char *desc, bool connect,
@@ -290,22 +291,24 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
290291
{
291292
char *hostnqn = NULL, *hostid = NULL, *host_traddr = NULL;
292293
nvme_host_t h;
293-
int ret, i;
294+
int ret, rr, i;
294295
struct list_head nbft_list;
295296
struct nbft_file_entry *entry = NULL;
296297
struct nbft_info_subsystem_ns **ss;
297298
struct nbft_info_hfi *hfi;
298299
struct nbft_info_discovery **dd;
299300

300301
if (!connect)
301-
/* to do: print discovery-type info from NBFT tables */
302+
/* TODO: print discovery-type info from NBFT tables */
302303
return 0;
303304

304305
list_head_init(&nbft_list);
305306
ret = read_nbft_files(&nbft_list, nbft_path);
306307
if (ret) {
307308
if (ret != -ENOENT)
308309
nvme_show_perror("Failed to access ACPI tables directory");
310+
else
311+
ret = 0; /* nothing to connect */
309312
goto out_free;
310313
}
311314

@@ -327,8 +330,10 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
327330
}
328331

329332
h = nvme_lookup_host(r, hostnqn, hostid);
330-
if (!h)
333+
if (!h) {
334+
ret = -ENOENT;
331335
goto out_free;
336+
}
332337

333338
/* Subsystem Namespace Descriptor List */
334339
for (ss = entry->nbft->subsystem_ns_list; ss && *ss; ss++)
@@ -358,34 +363,37 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
358363
.trsvcid = (*ss)->trsvcid,
359364
};
360365

361-
ret = do_connect(r, h, NULL, *ss, &trcfg,
362-
cfg, flags, verbose);
366+
rr = do_connect(r, h, NULL, *ss, &trcfg,
367+
cfg, flags, verbose);
363368

364369
/*
365370
* With TCP/DHCP, it can happen that the OS
366371
* obtains a different local IP address than the
367372
* firmware had. Retry without host_traddr.
368373
*/
369-
if (ret == -ENVME_CONNECT_ADDRNOTAVAIL &&
374+
if (rr == -ENVME_CONNECT_ADDRNOTAVAIL &&
370375
!strcmp(trcfg.transport, "tcp") &&
371376
strlen(hfi->tcp_info.dhcp_server_ipaddr) > 0) {
372377
trcfg.host_traddr = NULL;
373378

374-
ret = do_connect(r, h, NULL, *ss, &trcfg,
375-
cfg, flags, verbose);
379+
rr = do_connect(r, h, NULL, *ss, &trcfg,
380+
cfg, flags, verbose);
376381

377-
if (ret == 0 && verbose >= 1)
382+
if (rr == 0 && verbose >= 1)
378383
fprintf(stderr,
379384
"SSNS %d: connect with host_traddr=\"%s\" failed, success after omitting host_traddr\n",
380385
(*ss)->index,
381386
host_traddr);
382387
}
383388

384-
if (ret)
389+
if (rr) {
385390
fprintf(stderr, "SSNS %d: no controller found\n",
386391
(*ss)->index);
392+
/* report an error */
393+
ret = rr;
394+
}
387395

388-
if (ret == -ENOMEM)
396+
if (rr == -ENOMEM)
389397
goto out_free;
390398
}
391399

@@ -423,7 +431,7 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
423431
host_traddr = hfi->tcp_info.ipaddr;
424432
if (uri->port > 0) {
425433
if (asprintf(&trsvcid, "%d", uri->port) < 0) {
426-
errno = ENOMEM;
434+
ret = -ENOMEM;
427435
goto out_free;
428436
}
429437
} else
@@ -458,21 +466,25 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
458466
"Discovery Descriptor %d: failed to add discovery controller: %s\n",
459467
(*dd)->index,
460468
nvme_strerror(errno));
461-
if (errno == ENOMEM)
469+
if (errno == ENOMEM) {
470+
ret = -ENOMEM;
462471
goto out_free;
472+
}
463473
continue;
464474
}
465475

466-
ret = do_discover(*dd, r, h, c, cfg, &trcfg,
467-
flags, verbose);
476+
rr = do_discover(*dd, r, h, c, cfg, &trcfg,
477+
flags, verbose);
468478
if (!persistent)
469479
nvme_disconnect_ctrl(c);
470480
nvme_free_ctrl(c);
471-
if (ret == -ENOMEM)
481+
if (rr == -ENOMEM) {
482+
ret = rr;
472483
goto out_free;
484+
}
473485
}
474486
}
475487
out_free:
476488
free_nbfts(&nbft_list);
477-
return errno;
489+
return ret;
478490
}

0 commit comments

Comments
 (0)