[PATCH V4 nvme-cli 2/3] get access state of Native NVME Multipath device

chengjike chengjike.cheng at huawei.com
Fri Oct 8 05:25:52 PDT 2021


(This is a prep patch, which provides functions for the next "patch 3/3").
Achieve nvme_ns_get_access function: If all paths state of namespace are "live" and ana_state
of all paths are "optimized" or "non-optimized", access state of ns is "full";
If ana_state of partial paths are "optimized" or "non-optimized" and their state is "live",
access state of ns is "partial"; Otherwise, access state of ns is "faulty".

Signed-off-by: chengjike <chengjike.cheng at huawei.com>
---
 src/nvme/tree.c | 33 +++++++++++++++++++++++++++++++++
 src/nvme/tree.h | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/src/nvme/tree.c b/src/nvme/tree.c
index 07a687a..78d709f 100644
--- a/src/nvme/tree.c
+++ b/src/nvme/tree.c
@@ -747,6 +747,16 @@ nvme_path_t nvme_ctrl_next_path(nvme_ctrl_t c, nvme_path_t p)
 	return p ? list_next(&c->paths, p, entry) : NULL;
 }
 
+nvme_path_t nvme_ns_first_path(nvme_ns_t n)
+{
+	return list_top(&n->paths, struct nvme_path, nentry);
+}
+
+nvme_path_t nvme_ns_next_path(nvme_ns_t n, nvme_path_t p)
+{
+	return p ? list_next(&n->paths, p, nentry) : NULL;
+}
+
 #define FREE_CTRL_ATTR(a) \
 	do { if (a) { free(a); (a) = NULL; } } while (0)
 void nvme_deconfigure_ctrl(nvme_ctrl_t c)
@@ -1335,6 +1345,29 @@ const char *nvme_ns_get_name(nvme_ns_t n)
 	return n->name;
 }
 
+const char *nvme_ns_get_access(nvme_ns_t n)
+{
+	int total = 0;
+	int invalid = 0;
+	nvme_path_t p;
+
+	nvme_ns_for_each_path(n, p) {
+		if (p->c && p->c->state && strcmp(p->c->state, "live"))
+			invalid++;
+		else if (p->ana_state && strcmp(p->ana_state, "optimized") && 
+			strcmp(p->ana_state, "non-optimized"))
+			invalid++;
+		total++;
+	}
+
+	if (total == invalid) 
+		return "faulty";
+	else if (invalid == 0)
+		return "full";
+	else
+		return "partial";
+}
+
 const char *nvme_ns_get_model(nvme_ns_t n)
 {
 	return n->c ? n->c->model : n->s->model;
diff --git a/src/nvme/tree.h b/src/nvme/tree.h
index 68f5cbf..0af5b9b 100644
--- a/src/nvme/tree.h
+++ b/src/nvme/tree.h
@@ -190,6 +190,23 @@ nvme_path_t nvme_ctrl_first_path(nvme_ctrl_t c);
  */
 nvme_path_t nvme_ctrl_next_path(nvme_ctrl_t c, nvme_path_t p);
 
+/**
+ * nvme_ns_first_path() -
+ * @n: Address of the nvme namespace structure
+ *
+ * Return: a pointer of first nvme namespace structure in the linked list
+ */
+nvme_path_t nvme_ns_first_path(nvme_ns_t n);
+
+/**
+ * nvme_ns_next_path() -
+ * @n: Address of the nvme namespace structure
+ * @p: Address of the nvme path structure
+ *
+ * Return: a pointer of next nvme path structure in the linked list
+ */
+nvme_path_t nvme_ns_next_path(nvme_ns_t n, nvme_path_t p);
+
 /**
  * nvme_subsystem_first_ctrl() -
  * @s:
@@ -336,6 +353,13 @@ nvme_ns_t nvme_subsystem_next_ns(nvme_subsystem_t s, nvme_ns_t n);
 	for (p = nvme_ctrl_first_path(c); p != NULL; 		\
 		p = nvme_ctrl_next_path(c, p))
 
+/**
+ * nvme_ns_for_each_path()
+ */
+#define nvme_ns_for_each_path(n, p)			\
+	for (p = nvme_ns_first_path(n); p != NULL; 	\
+		p = nvme_ns_next_path(n, p))
+
 /**
  * nvme_subsystem_for_each_ns_safe()
  */
@@ -452,6 +476,19 @@ const char *nvme_ns_get_sysfs_dir(nvme_ns_t n);
  */
 const char *nvme_ns_get_name(nvme_ns_t n);
 
+/**
+ * nvme_ns_get_access() - 
+ * @n: Address of the nvme namespace structure
+ *
+ * Get the access status of Native NVME Multipath device
+ *
+ * Return: If all paths state of namespace are "live" and ana_state of 
+ * all paths are "optimized" or "non-optimized", "full" is returned; 
+ * If ana_state of partial paths are "optimized" or "non-optimized" and 
+ * their state is "live", "partial" is returned; Otherwise, "faulty" is returned. 
+ */
+const char *nvme_ns_get_access(nvme_ns_t n);
+
 /**
  * nvme_ns_get_firmware() -
  * @n:
-- 
2.21.0.windows.1




More information about the Linux-nvme mailing list