return NULL;
}
+static int scan_ctrl_paths_filter(const struct dirent *d)
+{
+ int id, cntlid, nsid;
+
+ if (d->d_name[0] == '.')
+ return 0;
+
+ if (strstr(d->d_name, "nvme")) {
+ if (sscanf(d->d_name, "nvme%dc%dn%d", &id, &cntlid, &nsid) == 3)
+ return 1;
+ if (sscanf(d->d_name, "nvme%dn%d", &id, &nsid) == 2)
+ return 1;
+ }
+
+ return 0;
+}
+
+static char *get_nvme_ctrl_path_ana_state(char *path, int nsid)
+{
+ struct dirent **paths;
+ char *ana_state;
+ int i, n;
+
+ ana_state = calloc(1, 16);
+ if (!ana_state)
+ return NULL;
+
+ n = scandir(path, &paths, scan_ctrl_paths_filter, alphasort);
+ if (n <= 0) {
+ free(ana_state);
+ return NULL;
+ }
+ for (i = 0; i < n; i++) {
+ int id, cntlid, ns, fd;
+ ssize_t ret;
+ char *ctrl_path;
+
+ if (sscanf(paths[i]->d_name, "nvme%dc%dn%d",
+ &id, &cntlid, &ns) != 3) {
+ if (sscanf(paths[i]->d_name, "nvme%dn%d",
+ &id, &ns) != 2) {
+ continue;
+ }
+ }
+ if (ns != nsid)
+ continue;
+
+ ret = asprintf(&ctrl_path, "%s/%s/ana_state",
+ path, paths[i]->d_name);
+ if (ret < 0) {
+ free(ana_state);
+ ana_state = NULL;
+ break;
+ }
+ fd = open(ctrl_path, O_RDONLY);
+ if (fd < 0) {
+ free(ctrl_path);
+ free(ana_state);
+ ana_state = NULL;
+ break;
+ }
+ ret = read(fd, ana_state, 16);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to read ANA state from %s\n",
+ ctrl_path);
+ free(ana_state);
+ ana_state = NULL;
+ } else if (ana_state[strlen(ana_state) - 1] == '\n')
+ ana_state[strlen(ana_state) - 1] = '\0';
+ close(fd);
+ free(ctrl_path);
+ break;
+ }
+ for (i = 0; i < n; i++)
+ free(paths[i]);
+ free(paths);
+ return ana_state;
+}
+
static int scan_ctrls_filter(const struct dirent *d)
{
int id, nsid;
free(ctrls->transport);
free(ctrls->address);
free(ctrls->state);
+ free(ctrls->ana_state);
}
static int get_nvme_subsystem_info(char *name, char *path,
- struct subsys_list_item *item)
+ struct subsys_list_item *item, __u32 nsid)
{
char ctrl_path[512];
struct dirent **ctrls;
continue;
}
+ if (nsid != NVME_NSID_ALL)
+ item->ctrls[ccnt].ana_state =
+ get_nvme_ctrl_path_ana_state(ctrl_path, nsid);
ccnt++;
}
free(slist);
}
-struct subsys_list_item *get_subsys_list(int *subcnt)
+struct subsys_list_item *get_subsys_list(int *subcnt, char *subsysnqn,
+ __u32 nsid)
{
char path[310];
struct dirent **subsys;
snprintf(path, sizeof(path), "%s%s", subsys_dir,
subsys[i]->d_name);
ret = get_nvme_subsystem_info(subsys[i]->d_name, path,
- &slist[*subcnt]);
+ &slist[*subcnt], nsid);
if (ret) {
fprintf(stderr,
"%s: failed to get subsystem info: %s\n",
path, strerror(errno));
free_subsys_list_item(&slist[*subcnt]);
- } else
+ } else if (subsysnqn &&
+ strncmp(slist[*subcnt].subsysnqn, subsysnqn, 255))
+ free_subsys_list_item(&slist[*subcnt]);
+ else
(*subcnt)++;
}
{
struct subsys_list_item *slist;
int fmt, ret, subcnt = 0;
+ char *subsysnqn = NULL;
const char *desc = "Retrieve information for subsystems";
struct config {
+ __u32 namespace_id;
char *output_format;
};
struct config cfg = {
+ .namespace_id = NVME_NSID_ALL,
.output_format = "normal",
};
if (ret < 0)
return ret;
+ devicename = NULL;
+ if (optind < argc) {
+ char path[512];
+ int id;
+
+ devicename = basename(argv[optind]);
+ if (sscanf(devicename, "nvme%dn%d", &id,
+ &cfg.namespace_id) != 2) {
+ fprintf(stderr, "%s is not a NVMe namespace device\n",
+ argv[optind]);
+ return -EINVAL;
+ }
+ sprintf(path, "/sys/block/%s/device", devicename);
+ subsysnqn = get_nvme_subsnqn(path);
+ if (!subsysnqn) {
+ fprintf(stderr, "Cannot read subsys NQN from %s\n",
+ devicename);
+ return -EINVAL;
+ }
+ optind++;
+ }
+
+ if (ret < 0) {
+ argconfig_print_help(desc, opts);
+ if (subsysnqn)
+ free(subsysnqn);
+ return ret;
+ }
fmt = validate_output_format(cfg.output_format);
- if (fmt != JSON && fmt != NORMAL)
+ if (fmt != JSON && fmt != NORMAL) {
+ if (subsysnqn)
+ free(subsysnqn);
return -EINVAL;
+ }
- slist = get_subsys_list(&subcnt);
+ slist = get_subsys_list(&subcnt, subsysnqn, cfg.namespace_id);
if (fmt == JSON)
json_print_nvme_subsystem_list(slist, subcnt);
show_nvme_subsystem_list(slist, subcnt);
free_subsys_list(slist, subcnt);
+ if (subsysnqn)
+ free(subsysnqn);
return ret;
}