File 0027-nvme-commonize-subsystems-info-in-a-helper.patch of Package nvme-cli.12152

From: Sagi Grimberg <sagi@grimberg.me>
Date: Fri, 31 Aug 2018 18:36:04 -0700
Subject: [PATCH] nvme: commonize subsystems info in a helper

Git-commit: 4dd389a505c23eec49615e70ef1070a9dfa32031
References: bsc#1111384

We will want that to reuse for other ops that
will require to scan subsystems or controllers

Signed-off-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Signed-off-by: Keith Busch <keith.busch@intel.com>
---
 nvme.c | 77 ++++++++++++++++++++++++++++++++++++------------------------------
 nvme.h |  2 ++
 2 files changed, 44 insertions(+), 35 deletions(-)

diff --git a/nvme.c b/nvme.c
index 22ef7d5..7d58500 100644
--- a/nvme.c
+++ b/nvme.c
@@ -1154,7 +1154,7 @@ static void free_subsys_list_item(struct subsys_list_item *item)
 	free(item->name);
 }
 
-static void free_subsys_list(struct subsys_list_item *slist, int n)
+void free_subsys_list(struct subsys_list_item *slist, int n)
 {
 	int i;
 
@@ -1164,13 +1164,50 @@ static void free_subsys_list(struct subsys_list_item *slist, int n)
 	free(slist);
 }
 
-static int list_subsys(int argc, char **argv, struct command *cmd,
-		       struct plugin *plugin)
+struct subsys_list_item *get_subsys_list(int *subcnt)
 {
 	char path[310];
 	struct dirent **subsys;
 	struct subsys_list_item *slist;
-	int fmt, n, i, ret = 0, subcnt = 0;
+	int n, i, ret = 0;
+
+	n = scandir(subsys_dir, &subsys, scan_subsys_filter, alphasort);
+	if (n < 0) {
+		fprintf(stderr, "no NVMe subsystem(s) detected.\n");
+		return NULL;
+	}
+
+	slist = calloc(n, sizeof(struct subsys_list_item));
+	if (!slist)
+		goto free_subsys;
+
+	for (i = 0; i < n; i++) {
+		snprintf(path, sizeof(path), "%s%s", subsys_dir,
+			subsys[i]->d_name);
+		ret = get_nvme_subsystem_info(subsys[i]->d_name, path,
+				&slist[*subcnt]);
+		if (ret) {
+			fprintf(stderr,
+				"%s: failed to get subsystem info: %s\n",
+				path, strerror(errno));
+			free_subsys_list_item(&slist[*subcnt]);
+		} else
+			(*subcnt)++;
+	}
+
+free_subsys:
+	for (i = 0; i < n; i++)
+		free(subsys[i]);
+	free(subsys);
+
+	return slist;
+}
+
+static int list_subsys(int argc, char **argv, struct command *cmd,
+		struct plugin *plugin)
+{
+	struct subsys_list_item *slist;
+	int fmt, ret, subcnt = 0;
 	const char *desc = "Retrieve information for subsystems";
 	struct config {
 		char *output_format;
@@ -1191,47 +1228,17 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
 		return ret;
 
 	fmt = validate_output_format(cfg.output_format);
-
 	if (fmt != JSON && fmt != NORMAL)
 		return -EINVAL;
-	n = scandir(subsys_dir, &subsys, scan_subsys_filter, alphasort);
-	if (n < 0) {
-		fprintf(stderr, "no NVMe subsystem(s) detected.\n");
-		return n;
-	}
 
-	slist = calloc(n, sizeof(struct subsys_list_item));
-	if (!slist) {
-		ret = ENOMEM;
-		goto free_subsys;
-	}
-
-	for (i = 0; i < n; i++) {
-		snprintf(path, sizeof(path), "%s%s", subsys_dir,
-			subsys[i]->d_name);
-		ret = get_nvme_subsystem_info(subsys[i]->d_name, path,
-				&slist[subcnt]);
-		if (ret) {
-			fprintf(stderr,
-				"%s: failed to get subsystem info: %s\n",
-				path, strerror(errno));
-			free_subsys_list_item(&slist[subcnt]);
-		} else
-			subcnt++;
-	}
+	slist = get_subsys_list(&subcnt);
 
 	if (fmt == JSON)
 		json_print_nvme_subsystem_list(slist, subcnt);
 	else
 		show_nvme_subsystem_list(slist, subcnt);
 
-free_subsys:
 	free_subsys_list(slist, subcnt);
-
-	for (i = 0; i < n; i++)
-		free(subsys[i]);
-	free(subsys);
-
 	return ret;
 }
 
diff --git a/nvme.h b/nvme.h
index df48175..f363155 100644
--- a/nvme.h
+++ b/nvme.h
@@ -148,4 +148,6 @@ extern const char *devicename;
 int __id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin, void (*vs)(__u8 *vs, struct json_object *root));
 int	validate_output_format(char *format);
 
+struct subsys_list_item *get_subsys_list(int *subcnt);
+void free_subsys_list(struct subsys_list_item *slist, int n);
 #endif /* _NVME_H */
-- 
2.12.3
openSUSE Build Service is sponsored by