aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2024-11-05 01:28:26 +0000
committerJohn Baldwin <jhb@FreeBSD.org>2024-11-05 01:28:26 +0000
commit326e20fc1252577f96df0e53360507180cc9d153 (patch)
tree5305d62ef0348dfee9c1382d425af7d2f5b1701d
parent8922c5b8211e5b2f64b10791b51d08d5b7945f56 (diff)
-rw-r--r--sbin/nvmecontrol/devlist.c99
1 files changed, 59 insertions, 40 deletions
diff --git a/sbin/nvmecontrol/devlist.c b/sbin/nvmecontrol/devlist.c
index 2b34899d3aea..b2816339bc80 100644
--- a/sbin/nvmecontrol/devlist.c
+++ b/sbin/nvmecontrol/devlist.c
@@ -89,60 +89,79 @@ ns_get_sector_size(struct nvme_namespace_data *nsdata)
}
static void
-devlist(const struct cmd *f, int argc, char *argv[])
+scan_namespace(int fd, int ctrlr, uint32_t nsid)
{
- struct nvme_controller_data cdata;
- struct nvme_namespace_data nsdata;
+ struct nvme_namespace_data nsdata;
char name[64];
- uint8_t mn[64];
uint8_t buf[7];
- uint32_t i;
uint64_t size;
- int ctrlr, fd, found, ret;
- if (arg_parse(argc, argv, f))
+ if (read_namespace_data(fd, nsid, &nsdata) != 0)
return;
+ if (nsdata.nsze == 0)
+ return;
+ snprintf(name, sizeof(name), "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
+ NVME_NS_PREFIX, nsid);
+ size = nsdata.nsze * (uint64_t)ns_get_sector_size(&nsdata);
+ if (opt.human) {
+ humanize_number(buf, sizeof(buf), size, "B",
+ HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
+ printf(" %10s (%s)\n", name, buf);
+ } else {
+ printf(" %10s (%juMB)\n", name, (uintmax_t)size / 1024 / 1024);
+ }
+}
- ctrlr = -1;
- found = 0;
+static bool
+scan_controller(int ctrlr)
+{
+ struct nvme_controller_data cdata;
+ char name[64];
+ uint8_t mn[64];
+ uint32_t i;
+ int fd, ret;
- while (ctrlr < NVME_MAX_UNIT) {
- ctrlr++;
- sprintf(name, "%s%d", NVME_CTRLR_PREFIX, ctrlr);
+ snprintf(name, sizeof(name), "%s%d", NVME_CTRLR_PREFIX, ctrlr);
- ret = open_dev(name, &fd, 0, 0);
+ ret = open_dev(name, &fd, 0, 0);
- if (ret == EACCES) {
- warnx("could not open "_PATH_DEV"%s\n", name);
- continue;
- } else if (ret != 0)
- continue;
+ if (ret == EACCES) {
+ warnx("could not open "_PATH_DEV"%s\n", name);
+ return (false);
+ } else if (ret != 0)
+ return (false);
- found++;
- if (read_controller_data(fd, &cdata))
- continue;
- nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH);
- printf("%6s: %s\n", name, mn);
+ if (read_controller_data(fd, &cdata) != 0) {
+ close(fd);
+ return (true);
+ }
- for (i = 0; i < cdata.nn; i++) {
- if (read_namespace_data(fd, i + 1, &nsdata))
- continue;
- if (nsdata.nsze == 0)
- continue;
- sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
- NVME_NS_PREFIX, i + 1);
- size = nsdata.nsze * (uint64_t)ns_get_sector_size(&nsdata);
- if (opt.human) {
- humanize_number(buf, sizeof(buf), size, "B",
- HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
- printf(" %10s (%s)\n", name, buf);
+ nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH);
+ printf("%6s: %s\n", name, mn);
- } else {
- printf(" %10s (%juMB)\n", name, (uintmax_t)size / 1024 / 1024);
- }
- }
+ for (i = 0; i < cdata.nn; i++) {
+ scan_namespace(fd, ctrlr, i + 1);
+ }
- close(fd);
+ close(fd);
+ return (true);
+}
+
+static void
+devlist(const struct cmd *f, int argc, char *argv[])
+{
+ int ctrlr, found;
+
+ if (arg_parse(argc, argv, f))
+ return;
+
+ ctrlr = -1;
+ found = 0;
+
+ while (ctrlr < NVME_MAX_UNIT) {
+ ctrlr++;
+ if (scan_controller(ctrlr))
+ found++;
}
if (found == 0) {