Skip to content

Commit 4473661

Browse files
committed
mi: make return values + errno consistent across error paths
This change unifies and documents the error return values for the MI implementation. This gives us the following semantics: - zero on success - -1 for errors in the MI communication with an endpoint, with errno set accordingly - positive values where the MI communication succeeded, but we received an error response from the endpoint. The return value will be that from the MI response status field, and should correspond to one of the nvme_mi_resp_status values. We add these semantics to the file-level kdoc comments in mi.h. Most of the changes here are replacing the negative-errno returns: return -EIO; with: errno = EIO; return -1; but there are a few slightly-more-involved changes where we need to preserve errno across a cleanup/log that might clobber it. For the dbus code in mi-mctp.c, we need to convert the sd_bus convention of negative-errno values into errno; we can do most of these through the dbus_err() helper. Fixes: #417 Signed-off-by: Jeremy Kerr <[email protected]>
1 parent b4a9b52 commit 4473661

3 files changed

Lines changed: 193 additions & 84 deletions

File tree

src/nvme/mi-mctp.c

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,21 @@ static int nvme_mi_mctp_submit(struct nvme_mi_ep *ep,
210210
struct iovec req_iov[3], resp_iov[3];
211211
struct msghdr req_msg, resp_msg;
212212
struct sockaddr_mctp addr;
213+
int i, rc, errno_save;
213214
ssize_t len;
214215
__le32 mic;
215-
int i, rc;
216216
__u8 tag;
217217

218-
if (ep->transport != &nvme_mi_transport_mctp)
219-
return -EINVAL;
218+
if (ep->transport != &nvme_mi_transport_mctp) {
219+
errno = EINVAL;
220+
return -1;
221+
}
220222

221223
/* we need enough space for at least a generic (/error) response */
222-
if (resp->hdr_len < sizeof(struct nvme_mi_msg_resp))
223-
return -EINVAL;
224+
if (resp->hdr_len < sizeof(struct nvme_mi_msg_resp)) {
225+
errno = EINVAL;
226+
return -1;
227+
}
224228

225229
mctp = ep->transport_data;
226230
tag = nvme_mi_mctp_tag_alloc(ep);
@@ -256,9 +260,11 @@ static int nvme_mi_mctp_submit(struct nvme_mi_ep *ep,
256260

257261
len = ops.sendmsg(mctp->sd, &req_msg, 0);
258262
if (len < 0) {
263+
errno_save = errno;
259264
nvme_msg(ep->root, LOG_ERR,
260265
"Failure sending MCTP message: %m\n");
261-
rc = len;
266+
errno = errno_save;
267+
rc = -1;
262268
goto out;
263269
}
264270

@@ -282,14 +288,17 @@ static int nvme_mi_mctp_submit(struct nvme_mi_ep *ep,
282288
len = ops.recvmsg(mctp->sd, &resp_msg, 0);
283289

284290
if (len < 0) {
291+
errno_save = errno;
285292
nvme_msg(ep->root, LOG_ERR,
286293
"Failure receiving MCTP message: %m\n");
294+
errno = errno_save;
287295
goto out;
288296
}
289297

290298

291299
if (len == 0) {
292300
nvme_msg(ep->root, LOG_WARNING, "No data from MCTP endpoint\n");
301+
errno = EIO;
293302
goto out;
294303
}
295304

@@ -304,6 +313,7 @@ static int nvme_mi_mctp_submit(struct nvme_mi_ep *ep,
304313
nvme_msg(ep->root, LOG_ERR,
305314
"Invalid MCTP response: too short (%zd bytes, needed %zd)\n",
306315
len, 8 + sizeof(mic));
316+
errno = EPROTO;
307317
goto out;
308318
}
309319

@@ -312,6 +322,7 @@ static int nvme_mi_mctp_submit(struct nvme_mi_ep *ep,
312322
nvme_msg(ep->root, LOG_WARNING,
313323
"Response message has unaligned length (%zd)!\n",
314324
len);
325+
errno = EPROTO;
315326
goto out;
316327
}
317328

@@ -379,8 +390,10 @@ static int nvme_mi_mctp_desc_ep(struct nvme_mi_ep *ep, char *buf, size_t len)
379390
{
380391
struct nvme_mi_transport_mctp *mctp;
381392

382-
if (ep->transport != &nvme_mi_transport_mctp)
393+
if (ep->transport != &nvme_mi_transport_mctp) {
394+
errno = EINVAL;
383395
return -1;
396+
}
384397

385398
mctp = ep->transport_data;
386399

@@ -401,6 +414,7 @@ nvme_mi_ep_t nvme_mi_open_mctp(nvme_root_t root, unsigned int netid, __u8 eid)
401414
{
402415
struct nvme_mi_transport_mctp *mctp;
403416
struct nvme_mi_ep *ep;
417+
int errno_save;
404418

405419
ep = nvme_mi_init_ep(root);
406420
if (!ep)
@@ -423,15 +437,22 @@ nvme_mi_ep_t nvme_mi_open_mctp(nvme_root_t root, unsigned int netid, __u8 eid)
423437
return ep;
424438

425439
err_free_ep:
440+
errno_save = errno;
426441
free(ep);
442+
errno = errno_save;
427443
return NULL;
428444
}
429445

430446
#ifdef CONFIG_LIBSYSTEMD
431447

432-
static void _dbus_err(nvme_root_t root, int rc, int line) {
448+
/* helper for handling dbus errors: D-Bus API returns a negtive errno on
449+
* failure; set errno and log.
450+
*/
451+
static void _dbus_err(nvme_root_t root, int rc, int line)
452+
{
433453
nvme_msg(root, LOG_ERR, "MCTP D-Bus failed line %d: %s %d\n",
434454
line, strerror(-rc), rc);
455+
errno = -rc;
435456
}
436457

437458
#define dbus_err(r, rc) _dbus_err(r, rc, __LINE__)
@@ -452,9 +473,8 @@ static int nvme_mi_mctp_add(nvme_root_t root, unsigned int netid, __u8 eid)
452473
}
453474

454475
ep = nvme_mi_open_mctp(root, netid, eid);
455-
if (!ep) {
456-
return -ENOMEM;
457-
}
476+
if (!ep)
477+
return -1;
458478

459479
return 0;
460480
}
@@ -480,7 +500,7 @@ static int handle_mctp_endpoint(nvme_root_t root, const char* objpath,
480500
rc = sd_bus_message_enter_container(m, 'a', "{sv}");
481501
if (rc < 0) {
482502
dbus_err(root, rc);
483-
return rc;
503+
return -1;
484504
}
485505

486506
while (!container_end(m)) {
@@ -491,13 +511,13 @@ static int handle_mctp_endpoint(nvme_root_t root, const char* objpath,
491511
rc = sd_bus_message_enter_container(m, 'e', "sv");
492512
if (rc < 0) {
493513
dbus_err(root, rc);
494-
return rc;
514+
return -1;
495515
}
496516

497517
rc = sd_bus_message_read(m, "s", &propname);
498518
if (rc < 0) {
499519
dbus_err(root, rc);
500-
return rc;
520+
return -1;
501521
}
502522

503523
if (strcmp(propname, "EID") == 0) {
@@ -520,36 +540,38 @@ static int handle_mctp_endpoint(nvme_root_t root, const char* objpath,
520540

521541
if (rc < 0) {
522542
dbus_err(root, rc);
523-
return rc;
543+
return -1;
524544
}
525545

526546
/* Exit prop item */
527547
rc = sd_bus_message_exit_container(m);
528548
if (rc < 0) {
529549
dbus_err(root, rc);
530-
return rc;
550+
return -1;
531551
}
532552
}
533553

534554
/* Exit property dict */
535555
rc = sd_bus_message_exit_container(m);
536556
if (rc < 0) {
537557
dbus_err(root, rc);
538-
return rc;
558+
return -1;
539559
}
540560
}
541561

542562
if (have_nvmemi) {
543563
if (!(have_eid && have_net)) {
544564
nvme_msg(root, LOG_ERR,
545565
"Missing property for %s\n", objpath);
546-
return -ENOENT;
566+
errno = ENOENT;
567+
return -1;
547568
}
548569
rc = nvme_mi_mctp_add(root, net, eid);
549570
if (rc < 0) {
571+
int errno_save = errno;
550572
nvme_msg(root, LOG_ERR,
551-
"Error adding net %d eid %d: %s\n",
552-
net, eid, strerror(-rc));
573+
"Error adding net %d eid %d: %m\n", net, eid);
574+
errno = errno_save;
553575
}
554576
} else {
555577
/* Ignore other endpoints */
@@ -567,15 +589,15 @@ static int handle_mctp_obj(nvme_root_t root, sd_bus_message *m)
567589
rc = sd_bus_message_read(m, "o", &objpath);
568590
if (rc < 0) {
569591
dbus_err(root, rc);
570-
return rc;
592+
return -1;
571593
}
572594

573595
/* Enter response object: our array of (string, property dict)
574596
* values */
575597
rc = sd_bus_message_enter_container(m, 'a', "{sa{sv}}");
576598
if (rc < 0) {
577599
dbus_err(root, rc);
578-
return rc;
600+
return -1;
579601
}
580602

581603

@@ -585,13 +607,13 @@ static int handle_mctp_obj(nvme_root_t root, sd_bus_message *m)
585607
rc = sd_bus_message_enter_container(m, 'e', "sa{sv}");
586608
if (rc < 0) {
587609
dbus_err(root, rc);
588-
return rc;
610+
return -1;
589611
}
590612

591613
rc = sd_bus_message_read(m, "s", &ifname);
592614
if (rc < 0) {
593615
dbus_err(root, rc);
594-
return rc;
616+
return -1;
595617
}
596618

597619
if (!strcmp(ifname, MCTP_DBUS_IFACE_ENDPOINT)) {
@@ -605,23 +627,23 @@ static int handle_mctp_obj(nvme_root_t root, sd_bus_message *m)
605627
rc = sd_bus_message_skip(m, "a{sv}");
606628
if (rc < 0) {
607629
dbus_err(root, rc);
608-
return rc;
630+
return -1;
609631
}
610632
}
611633

612634
/* Exit interface item */
613635
rc = sd_bus_message_exit_container(m);
614636
if (rc < 0) {
615637
dbus_err(root, rc);
616-
return rc;
638+
return -1;
617639
}
618640
}
619641

620642
/* Exit response object */
621643
rc = sd_bus_message_exit_container(m);
622644
if (rc < 0) {
623645
dbus_err(root, rc);
624-
return rc;
646+
return -1;
625647
}
626648

627649
return 0;
@@ -632,19 +654,22 @@ nvme_root_t nvme_mi_scan_mctp(void)
632654
sd_bus *bus = NULL;
633655
sd_bus_message *resp = NULL;
634656
sd_bus_error berr = SD_BUS_ERROR_NULL;
657+
int rc, errno_save;
635658
nvme_root_t root;
636-
int rc;
637659

638660
root = nvme_mi_create_root(NULL, DEFAULT_LOGLEVEL);
639661
if (!root) {
640-
rc = -ENOMEM;
662+
errno = ENOMEM;
663+
rc = -1;
641664
goto out;
642665
}
643666

644667
rc = sd_bus_default_system(&bus);
645668
if (rc < 0) {
646669
nvme_msg(root, LOG_ERR, "Failed opening D-Bus: %s\n",
647670
strerror(-rc));
671+
errno = -rc;
672+
rc = -1;
648673
goto out;
649674
}
650675

@@ -659,14 +684,17 @@ nvme_root_t nvme_mi_scan_mctp(void)
659684
if (rc < 0) {
660685
nvme_msg(root, LOG_ERR, "Failed querying MCTP D-Bus: %s (%s)\n",
661686
berr.message, berr.name);
687+
errno = -rc;
688+
rc = -1;
662689
goto out;
663690
}
664691

665692
rc = sd_bus_message_enter_container(resp, 'a', "{oa{sa{sv}}}");
666693
if (rc != 1) {
667694
dbus_err(root, rc);
668695
if (rc == 0)
669-
rc = -EPROTO;
696+
errno = EPROTO;
697+
rc = -1;
670698
goto out;
671699
}
672700

@@ -675,6 +703,7 @@ nvme_root_t nvme_mi_scan_mctp(void)
675703
rc = sd_bus_message_enter_container(resp, 'e', "oa{sa{sv}}");
676704
if (rc < 0) {
677705
dbus_err(root, rc);
706+
rc = -1;
678707
goto out;
679708
}
680709

@@ -683,18 +712,21 @@ nvme_root_t nvme_mi_scan_mctp(void)
683712
rc = sd_bus_message_exit_container(resp);
684713
if (rc < 0) {
685714
dbus_err(root, rc);
715+
rc = -1;
686716
goto out;
687717
}
688718
}
689719

690720
rc = sd_bus_message_exit_container(resp);
691721
if (rc < 0) {
692722
dbus_err(root, rc);
723+
rc = -1;
693724
goto out;
694725
}
695726
rc = 0;
696727

697728
out:
729+
errno_save = errno;
698730
sd_bus_error_free(&berr);
699731
sd_bus_message_unref(resp);
700732
sd_bus_unref(bus);
@@ -703,6 +735,7 @@ nvme_root_t nvme_mi_scan_mctp(void)
703735
if (root) {
704736
nvme_mi_free_root(root);
705737
}
738+
errno = errno_save;
706739
root = NULL;
707740
}
708741
return root;

0 commit comments

Comments
 (0)