From 8c21fd00c98582703f1d0f701cd5bbfca2644b40 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 19 Feb 2024 11:37:58 +0100 Subject: Adding upstream version 1.8. Signed-off-by: Daniel Baumann --- src/nvme/tree.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'src/nvme/tree.c') diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 07a3c53..344f8bc 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -61,7 +61,21 @@ struct candidate_args { }; typedef bool (*ctrl_match_t)(struct nvme_ctrl *c, struct candidate_args *candidate); -const char *nvme_slots_sysfs_dir = "/sys/bus/pci/slots"; +#define PATH_SYSFS_SLOTS "/sys/bus/pci/slots" + +static char *nvme_slots_sysfs_dir(void) +{ + char *basepath = getenv("LIBNVME_SYSFS_PATH"); + char *str; + + if (!basepath) + return strdup(PATH_SYSFS_SLOTS); + + if (!asprintf(&str, "%s" PATH_SYSFS_SLOTS, basepath)) + return NULL; + + return str; +} static struct nvme_host *default_host; @@ -122,7 +136,8 @@ static void cleanup_dirents(struct dirents *ents) nvme_host_t nvme_default_host(nvme_root_t r) { struct nvme_host *h; - _cleanup_free_ char *hostnqn, *hostid; + _cleanup_free_ char *hostnqn = NULL; + _cleanup_free_ char *hostid = NULL; hostnqn = nvmf_hostnqn_from_file(); if (!hostnqn) @@ -657,9 +672,10 @@ static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s, static int nvme_init_subsystem(nvme_subsystem_t s, const char *name) { + _cleanup_free_ char *subsys_dir = nvme_subsys_sysfs_dir(); char *path; - if (asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir, name) < 0) + if (asprintf(&path, "%s/%s", subsys_dir, name) < 0) return -1; s->model = nvme_get_attr(path, "model"); @@ -700,11 +716,12 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name, { struct nvme_subsystem *s = NULL, *_s; _cleanup_free_ char *path = NULL, *subsysnqn = NULL; + _cleanup_free_ char *subsys_dir = nvme_subsys_sysfs_dir(); nvme_host_t h = NULL; int ret; nvme_msg(r, LOG_DEBUG, "scan subsystem %s\n", name); - ret = asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir, name); + ret = asprintf(&path, "%s/%s", subsys_dir, name); if (ret < 0) return ret; @@ -1686,6 +1703,7 @@ static int nvme_ctrl_scan_namespaces(nvme_root_t r, struct nvme_ctrl *c) static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r, const char *ctrl_name) { + _cleanup_free_ char *subsys_dir = nvme_subsys_sysfs_dir(); _cleanup_dirents_ struct dirents subsys = {}; int i; @@ -1696,7 +1714,7 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r, struct stat st; _cleanup_free_ char *path = NULL; - if (asprintf(&path, "%s/%s/%s", nvme_subsys_sysfs_dir, + if (asprintf(&path, "%s/%s/%s", subsys_dir, subsys.ents[i]->d_name, ctrl_name) < 0) { errno = ENOMEM; return NULL; @@ -1712,18 +1730,19 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r, static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address) { + _cleanup_free_ char *slots_sysfs_dir = nvme_slots_sysfs_dir(); _cleanup_free_ char *target_addr = NULL; - int ret; _cleanup_dir_ DIR *slots_dir = NULL; + int ret; struct dirent *entry; if (!address) return NULL; - slots_dir = opendir(nvme_slots_sysfs_dir); + slots_dir = opendir(slots_sysfs_dir); if (!slots_dir) { nvme_msg(r, LOG_WARNING, "failed to open slots dir %s\n", - nvme_slots_sysfs_dir); + slots_sysfs_dir); return NULL; } @@ -1736,7 +1755,7 @@ static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address) _cleanup_free_ char *addr = NULL; ret = asprintf(&path, "%s/%s", - nvme_slots_sysfs_dir, entry->d_name); + slots_sysfs_dir, entry->d_name); if (ret < 0) { errno = ENOMEM; return NULL; @@ -1798,10 +1817,11 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance) { - nvme_subsystem_t s; + _cleanup_free_ char *ctrl_dir = nvme_ctrl_sysfs_dir(); _cleanup_free_ char *subsys_name = NULL; - char *path; _cleanup_free_ char *name = NULL; + nvme_subsystem_t s; + char *path; int ret; ret = asprintf(&name, "nvme%d", instance); @@ -1809,7 +1829,7 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance) errno = ENOMEM; return -1; } - ret = asprintf(&path, "%s/nvme%d", nvme_ctrl_sysfs_dir, instance); + ret = asprintf(&path, "%s/nvme%d", ctrl_dir, instance); if (ret < 0) { errno = ENOMEM; return ret; @@ -1853,7 +1873,7 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s, nvme_ctrl_t c, p; _cleanup_free_ char *addr = NULL, *address = NULL; char *a, *e; - _cleanup_free_ char *transport; + _cleanup_free_ char *transport = NULL; char *traddr = NULL, *trsvcid = NULL; char *host_traddr = NULL, *host_iface = NULL; int ret; @@ -1953,10 +1973,11 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name) _cleanup_free_ char *path = NULL; _cleanup_free_ char *hostnqn = NULL, *hostid = NULL; _cleanup_free_ char *subsysnqn = NULL, *subsysname = NULL; + _cleanup_free_ char *ctrl_dir = nvme_ctrl_sysfs_dir(); int ret; nvme_msg(r, LOG_DEBUG, "scan controller %s\n", name); - ret = asprintf(&path, "%s/%s", nvme_ctrl_sysfs_dir, name); + ret = asprintf(&path, "%s/%s", ctrl_dir, name); if (ret < 0) { errno = ENOMEM; return NULL; @@ -1989,7 +2010,7 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name) } subsysname = nvme_ctrl_lookup_subsystem_name(r, name); if (!subsysname) { - nvme_msg(r, LOG_ERR, + nvme_msg(r, LOG_DEBUG, "failed to lookup subsystem for controller %s\n", name); errno = ENXIO; @@ -2436,7 +2457,7 @@ static int nvme_ns_init(const char *path, struct nvme_ns *ns) struct sysfs_attr_table base[] = { { &ns->nsid, nvme_strtou32, true, "nsid" }, { &ns->lba_count, nvme_strtou64, true, "size" }, - { &ns->lba_size, nvme_strtou64, true, "queue/physical_block_size" }, + { &ns->lba_size, nvme_strtou64, true, "queue/logical_block_size" }, { ns->eui64, nvme_strtoeuid, false, "eui" }, { ns->nguid, nvme_strtouuid, false, "nguid" }, { ns->uuid, nvme_strtouuid, false, "uuid" } @@ -2584,7 +2605,9 @@ static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char * nvme_ns_t nvme_scan_namespace(const char *name) { - return __nvme_scan_namespace(nvme_ns_sysfs_dir, name); + _cleanup_free_ char *ns_dir = nvme_ns_sysfs_dir(); + + return __nvme_scan_namespace(ns_dir, name); } static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c, -- cgit v1.2.3