Skip to content

Commit

Permalink
tree: Add nvme_root identify flag to scan NVMe topology
Browse files Browse the repository at this point in the history
This is to avoid nvme identify namespace to scan NVMe topology.

Signed-off-by: Tokunori Ikegami <[email protected]>
  • Loading branch information
ikegami-t committed Mar 16, 2024
1 parent d96e92a commit ae5ed59
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/libnvme.map
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ LIBNVME_1_0 {
nvme_resv_report;
nvme_sanitize_nvm;
nvme_scan;
nvme_scan_no_id;
nvme_scan_ctrl;
nvme_scan_ctrls;
nvme_scan_ctrl_namespace_paths;
Expand Down
1 change: 1 addition & 0 deletions src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ struct nvme_root {
bool modified;
bool mi_probe_enabled;
struct nvme_fabric_options *options;
bool identify;
};

int nvme_set_attr(const char *dir, const char *attr, const char *value);
Expand Down
45 changes: 30 additions & 15 deletions src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ nvme_root_t nvme_create_root(FILE *fp, int log_level)
r->fp = fp;
list_head_init(&r->hosts);
list_head_init(&r->endpoints);
r->identify = true;
nvme_set_root(r);
return r;
}
Expand Down Expand Up @@ -244,15 +245,27 @@ int nvme_read_config(nvme_root_t r, const char *config_file)
return err;
}

nvme_root_t nvme_scan(const char *config_file)
static nvme_root_t nvme_scan_with_id_flag(const char *config_file, bool identify)
{
nvme_root_t r = nvme_create_root(NULL, DEFAULT_LOGLEVEL);

r->identify = identify;

nvme_scan_topology(r, NULL, NULL);
nvme_read_config(r, config_file);
return r;
}

nvme_root_t nvme_scan(const char *config_file)
{
return nvme_scan_with_id_flag(config_file, true);
}

nvme_root_t nvme_scan_no_id(const char *config_file)
{
return nvme_scan_with_id_flag(config_file, false);
}

int nvme_update_config(nvme_root_t r)
{
if (!r->modified || !r->config_file)
Expand Down Expand Up @@ -2430,19 +2443,22 @@ struct sysfs_attr_table {
int (*parse)(const char *str, void *res);
bool mandatory;
const char *name;
bool identify;
};

#define GETSHIFT(x) (__builtin_ffsll(x) - 1)
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

static int parse_attrs(const char *path, struct sysfs_attr_table *tbl, int size)
static int parse_attrs(const char *path, struct sysfs_attr_table *tbl, int size, bool identify)
{
char *str;
int ret, i;

for (i = 0; i < size; i++) {
struct sysfs_attr_table *e = &tbl[i];

if (!identify && e->identify)
continue;
str = nvme_get_attr(path, e->name);
if (!str) {
if (!e->mandatory)
Expand All @@ -2458,7 +2474,7 @@ static int parse_attrs(const char *path, struct sysfs_attr_table *tbl, int size)
return 0;
}

static int nvme_ns_init(const char *path, struct nvme_ns *ns)
static int nvme_ns_init(const char *path, struct nvme_ns *ns, bool identify)
{
_cleanup_free_ char *attr = NULL;
struct stat sb;
Expand All @@ -2473,7 +2489,7 @@ static int nvme_ns_init(const char *path, struct nvme_ns *ns)
{ ns->uuid, nvme_strtouuid, false, "uuid" }
};

ret = parse_attrs(path, base, ARRAY_SIZE(base));
ret = parse_attrs(path, base, ARRAY_SIZE(base), identify);
if (ret)
return ret;

Expand All @@ -2486,15 +2502,14 @@ static int nvme_ns_init(const char *path, struct nvme_ns *ns)
/* only available on kernels >= 6.8 */
struct sysfs_attr_table ext[] = {
{ &ns->csi, nvme_strtoi, true, "csi" },
{ &ns->lba_util, nvme_strtou64, true, "nuse" },
{ &ns->lba_util, nvme_strtou64, true, "nuse", true },
{ &ns->meta_size, nvme_strtoi, true, "metadata_bytes"},

};

ret = parse_attrs(path, ext, ARRAY_SIZE(ext));
ret = parse_attrs(path, ext, ARRAY_SIZE(ext), identify);
if (ret)
return ret;
} else {
} else if (identify) {
struct nvme_id_ns *id;
uint8_t flbas;

Expand Down Expand Up @@ -2529,7 +2544,7 @@ static void nvme_ns_set_generic_name(struct nvme_ns *n, const char *name)
n->generic_name = strdup(generic_name);
}

static nvme_ns_t nvme_ns_open(const char *sys_path, const char *name)
static nvme_ns_t nvme_ns_open(const char *sys_path, const char *name, bool identify)
{
struct nvme_ns *n;

Expand All @@ -2544,7 +2559,7 @@ static nvme_ns_t nvme_ns_open(const char *sys_path, const char *name)

nvme_ns_set_generic_name(n, name);

if (nvme_ns_init(sys_path, n) != 0)
if (nvme_ns_init(sys_path, n, identify) != 0)
goto free_ns;

list_head_init(&n->paths);
Expand Down Expand Up @@ -2584,7 +2599,7 @@ static char *nvme_ns_generic_to_blkdev(const char *generic)
return strdup(blkdev);
}

static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *name)
static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *name, bool identify)
{
struct nvme_ns *n;
_cleanup_free_ char *path = NULL;
Expand All @@ -2603,7 +2618,7 @@ static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *
return NULL;
}

n = nvme_ns_open(path, blkdev);
n = nvme_ns_open(path, blkdev, identify);
if (!n)
return NULL;

Expand All @@ -2617,7 +2632,7 @@ nvme_ns_t nvme_scan_namespace(const char *name)
{
_cleanup_free_ char *ns_dir = nvme_ns_sysfs_dir();

return __nvme_scan_namespace(ns_dir, name);
return __nvme_scan_namespace(ns_dir, name, true);
}

static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c,
Expand All @@ -2632,7 +2647,7 @@ static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c,
errno = EINVAL;
return -1;
}
n = __nvme_scan_namespace(c->sysfs_dir, name);
n = __nvme_scan_namespace(c->sysfs_dir, name, r->identify);
if (!n) {
nvme_msg(r, LOG_DEBUG, "failed to scan namespace %s\n", name);
return -1;
Expand Down Expand Up @@ -2681,7 +2696,7 @@ static int nvme_subsystem_scan_namespace(nvme_root_t r, nvme_subsystem_t s,

nvme_msg(r, LOG_DEBUG, "scan subsystem %s namespace %s\n",
s->name, name);
n = __nvme_scan_namespace(s->sysfs_dir, name);
n = __nvme_scan_namespace(s->sysfs_dir, name, r->identify);
if (!n) {
nvme_msg(r, LOG_DEBUG, "failed to scan namespace %s\n", name);
return -1;
Expand Down
8 changes: 8 additions & 0 deletions src/nvme/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,14 @@ void nvme_free_host(nvme_host_t h);
*/
nvme_root_t nvme_scan(const char *config_file);

/**
* nvme_scan_no_id() - Scan NVMe topology without nvme identify namespace
* @config_file: Configuration file
*
* Return: nvme_root_t object of found elements
*/
nvme_root_t nvme_scan_no_id(const char *config_file);

/**
* nvme_read_config() - Read NVMe JSON configuration file
* @r: nvme_root_t object
Expand Down

0 comments on commit ae5ed59

Please sign in to comment.