Blob Blame History Raw
From ce9d818f420af6be0801004a77e91915587fc02f Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Tue, 22 Jun 2021 13:48:36 +0200
Subject: [PATCH] nvme-topology: scan all controllers in scan_subsystem()

When a controller is unavailable or resetting during scan_subsystem()
we should be checking all available controllers for this namespace
to avoid a spurious failure.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 nvme-topology.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/nvme-topology.c b/nvme-topology.c
index 47121e4..6d2edaa 100644
--- a/nvme-topology.c
+++ b/nvme-topology.c
@@ -155,23 +155,23 @@ static int scan_namespace(struct nvme_namespace *n)
 		return ret;
 
 	fd = open(path, O_RDONLY);
-	if (fd < 0)
+	if (fd < 0) {
+		ret = fd;
 		goto free;
-
+	}
 	if (!n->nsid) {
-		n->nsid = nvme_get_nsid(fd);
-		if (n->nsid < 0)
+		ret = nvme_get_nsid(fd);
+		if (ret < 0)
 			goto close_fd;
+		n->nsid = ret;
 	}
 
 	ret = nvme_identify_ns(fd, n->nsid, 0, &n->ns);
-	if (ret < 0)
-		goto close_fd;
 close_fd:
 	close(fd);
 free:
 	free(path);
-	return 0;
+	return ret;
 }
 
 static char *get_nvme_ctrl_path_ana_state(char *path, int nsid)
@@ -382,8 +382,11 @@ static int scan_subsystem(struct nvme_subsystem *s, __u32 ns_instance, int nsid)
 		for (i = 0; i < s->nr_namespaces; i++) {
 			n = &s->namespaces[i];
 			n->name = strdup(ns[i]->d_name);
-			n->ctrl = &s->ctrls[0];
-			scan_namespace(n);
+			for (j = 0; j < s->nr_ctrls; j++) {
+				n->ctrl = &s->ctrls[j];
+				if (!scan_namespace(n))
+					break;
+			}
 		}
 	} else {
 		i = s->nr_namespaces;
-- 
2.27.0