From: sagi@grimberg.me (Sagi Grimberg)
Subject: [PATCH nvme-cli 3/4] nvme: commonize subsystems info in a helper
Date: Fri, 31 Aug 2018 18:36:04 -0700 [thread overview]
Message-ID: <20180901013605.10658-4-sagi@grimberg.me> (raw)
In-Reply-To: <20180901013605.10658-1-sagi@grimberg.me>
We will want that to reuse for other ops that
will require to scan subsystems or controllers
Signed-off-by: Sagi Grimberg <sagi at grimberg.me>
---
nvme.c | 77 ++++++++++++++++++++++++++++++++--------------------------
nvme.h | 2 ++
2 files changed, 44 insertions(+), 35 deletions(-)
diff --git a/nvme.c b/nvme.c
index d7be2e04c9f4..80422516e6ad 100644
--- a/nvme.c
+++ b/nvme.c
@@ -1498,7 +1498,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;
@@ -1508,13 +1508,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;
@@ -1535,47 +1572,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 5098b0ef9c7c..e509e9c64f05 100644
--- a/nvme.h
+++ b/nvme.h
@@ -155,4 +155,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.17.1
next prev parent reply other threads:[~2018-09-01 1:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-01 1:36 [PATCH nvme-cli 0/4] Some useful fabrics patches Sagi Grimberg
2018-09-01 1:36 ` [PATCH nvme-cli 1/4] fabrics: make some arguments integers Sagi Grimberg
2018-09-01 20:39 ` Chaitanya Kulkarni
2018-09-01 1:36 ` [PATCH nvme-cli 2/4] fabrics: don't fail empty discovery log page Sagi Grimberg
2018-09-01 20:40 ` Chaitanya Kulkarni
2018-09-01 1:36 ` Sagi Grimberg [this message]
2018-09-01 20:40 ` [PATCH nvme-cli 3/4] nvme: commonize subsystems info in a helper Chaitanya Kulkarni
2018-09-01 1:36 ` [PATCH nvme-cli 4/4] fabrics: add disconnect-all command Sagi Grimberg
2018-09-01 20:40 ` Chaitanya Kulkarni
2018-09-06 0:20 ` [PATCH nvme-cli 0/4] Some useful fabrics patches Keith Busch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180901013605.10658-4-sagi@grimberg.me \
--to=sagi@grimberg.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).