From fb32fdeba1f428a3f6c30c8951e82f2a2eec23a8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 12 Aug 2021 14:01:09 +0200 Subject: Merging upstream version 1.15. Signed-off-by: Daniel Baumann --- nvme-topology.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'nvme-topology.c') diff --git a/nvme-topology.c b/nvme-topology.c index 31cf7f9..0a22f6b 100644 --- a/nvme-topology.c +++ b/nvme-topology.c @@ -155,23 +155,23 @@ static int scan_namespace(struct nvme_namespace *n) return ret; fd = open(path, O_RDONLY); - if (fd < 0) + if (fd < 0) { + ret = fd; goto free; - + } if (!n->nsid) { - n->nsid = nvme_get_nsid(fd); - if (n->nsid < 0) + ret = nvme_get_nsid(fd); + if (ret < 0) goto close_fd; + n->nsid = ret; } ret = nvme_identify_ns(fd, n->nsid, 0, &n->ns); - if (ret < 0) - goto close_fd; close_fd: close(fd); free: free(path); - return 0; + return ret; } static char *get_nvme_ctrl_path_ana_state(char *path, int nsid) @@ -319,10 +319,8 @@ static int scan_ctrl(struct nvme_ctrl *c, char *p, __u32 ns_instance) return ret; fd = open(path, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s\n", path); + if (fd < 0) goto free; - } ret = nvme_identify_ctrl(fd, &c->id); if (ret < 0) @@ -384,8 +382,12 @@ static int scan_subsystem(struct nvme_subsystem *s, __u32 ns_instance, int nsid) for (i = 0; i < s->nr_namespaces; i++) { n = &s->namespaces[i]; n->name = strdup(ns[i]->d_name); - n->ctrl = &s->ctrls[0]; - scan_namespace(n); + for (j = 0; j < s->nr_ctrls; j++) { + n->ctrl = &s->ctrls[j]; + if (!strcmp(n->ctrl->state, "live") && + !scan_namespace(n)) + break; + } } } else { i = s->nr_namespaces; @@ -666,6 +668,30 @@ char *nvme_char_from_block(char *dev) return path; } +int nvme_logical_block_size_from_ns_char(const char *dev) +{ + int ret; + int id, nsid; + char *path = NULL; + char *s; + + ret = sscanf(dev, "ng%dn%d", &id, &nsid); + if (ret != 2) + return -EINVAL; + + if (asprintf(&path, "/sys/block/nvme%dn%d/queue", id, nsid) < 0) + path = NULL; + + if (!path) + return -EINVAL; + + s = nvme_get_ctrl_attr(path, "logical_block_size"); + if (!s) + return -EINVAL; + + return atoi(s); +} + void *mmap_registers(const char *dev) { int fd; -- cgit v1.2.3