Skip to content

Commit c167671

Browse files
committed
tree: open transport handle for the ns init fallback
Older kernels do not expose the sysfs entries the library needs. In this in this case it falls back to use a command to fetch the missing data. Though, the transport handle is not available yet. Open an temporary one. No need to optimize this code path, it is only for older kernels. Signed-off-by: Daniel Wagner <[email protected]>
1 parent d74e369 commit c167671

5 files changed

Lines changed: 36 additions & 19 deletions

File tree

libnvme/src/nvme/tree.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,8 @@ static int parse_attrs(const char *path, struct sysfs_attr_table *tbl, int size)
26772677
return 0;
26782678
}
26792679

2680-
static int nvme_ns_init(const char *path, struct nvme_ns *ns)
2680+
static int nvme_ns_init(struct nvme_global_ctx *ctx, const char *path,
2681+
struct nvme_ns *ns)
26812682
{
26822683
_cleanup_free_ char *attr = NULL;
26832684
struct stat sb;
@@ -2722,20 +2723,32 @@ static int nvme_ns_init(const char *path, struct nvme_ns *ns)
27222723
return ret;
27232724
} else {
27242725
_cleanup_free_ struct nvme_id_ns *id = NULL;
2726+
struct nvme_transport_handle *hdl = NULL;
2727+
struct nvme_passthru_cmd cmd;
2728+
27252729
uint8_t flbas;
27262730

27272731
id = __nvme_alloc(sizeof(*ns));
27282732
if (!id)
27292733
return -ENOMEM;
27302734

2731-
ret = nvme_ns_identify(ns, id);
2735+
ret = nvme_open(ctx, ns->name, &hdl);
2736+
if (ret) {
2737+
nvme_msg(ctx, LOG_ERR, "need root permissions");
2738+
goto close_hdl;
2739+
}
2740+
2741+
nvme_init_identify_ns(&cmd, nvme_ns_get_nsid(ns), id);
2742+
ret = nvme_submit_admin_passthru(hdl, &cmd);
27322743
if (ret)
2733-
return ret;
2744+
goto close_hdl;
27342745

27352746
nvme_id_ns_flbas_to_lbaf_inuse(id->flbas, &flbas);
27362747
ns->lba_count = le64_to_cpu(id->nsze);
27372748
ns->lba_util = le64_to_cpu(id->nuse);
27382749
ns->meta_size = le16_to_cpu(id->lbaf[flbas].ms);
2750+
close_hdl:
2751+
nvme_close(hdl);
27392752
}
27402753

27412754
return 0;
@@ -2755,7 +2768,8 @@ static void nvme_ns_set_generic_name(struct nvme_ns *n, const char *name)
27552768
n->generic_name = strdup(generic_name);
27562769
}
27572770

2758-
int nvme_ns_open(const char *sys_path, const char *name, nvme_ns_t *ns)
2771+
int nvme_ns_open(struct nvme_global_ctx *ctx, const char *sys_path,
2772+
const char *name, nvme_ns_t *ns)
27592773
{
27602774
int ret;
27612775
struct nvme_ns *n;
@@ -2798,7 +2812,7 @@ int nvme_ns_open(const char *sys_path, const char *name, nvme_ns_t *ns)
27982812

27992813
nvme_ns_set_generic_name(n, name);
28002814

2801-
ret = nvme_ns_init(sys_path, n);
2815+
ret = nvme_ns_init(ctx, sys_path, n);
28022816
if (ret)
28032817
goto free_ns;
28042818

@@ -2842,7 +2856,8 @@ static char *nvme_ns_generic_to_blkdev(const char *generic)
28422856
return strdup(blkdev);
28432857
}
28442858

2845-
static int __nvme_scan_namespace(const char *sysfs_dir, const char *name, nvme_ns_t *ns)
2859+
static int __nvme_scan_namespace(struct nvme_global_ctx *ctx,
2860+
const char *sysfs_dir, const char *name, nvme_ns_t *ns)
28462861
{
28472862
_cleanup_free_ char *blkdev = NULL;
28482863
_cleanup_free_ char *path = NULL;
@@ -2857,7 +2872,7 @@ static int __nvme_scan_namespace(const char *sysfs_dir, const char *name, nvme_n
28572872
if (ret < 0)
28582873
return -ENOMEM;
28592874

2860-
ret = nvme_ns_open(path, blkdev, &n);
2875+
ret = nvme_ns_open(ctx, path, blkdev, &n);
28612876
if (ret)
28622877
return ret;
28632878

@@ -2868,9 +2883,10 @@ static int __nvme_scan_namespace(const char *sysfs_dir, const char *name, nvme_n
28682883
return 0;
28692884
}
28702885

2871-
int nvme_scan_namespace(const char *name, nvme_ns_t *ns)
2886+
int nvme_scan_namespace(struct nvme_global_ctx *ctx,
2887+
const char *name, nvme_ns_t *ns)
28722888
{
2873-
return __nvme_scan_namespace(nvme_ns_sysfs_dir(), name, ns);
2889+
return __nvme_scan_namespace(ctx, nvme_ns_sysfs_dir(), name, ns);
28742890
}
28752891

28762892

@@ -2938,8 +2954,8 @@ static void nvme_subsystem_set_ns_path(nvme_subsystem_t s, nvme_ns_t n)
29382954
}
29392955
}
29402956

2941-
static int nvme_ctrl_scan_namespace(struct nvme_global_ctx *ctx, struct nvme_ctrl *c,
2942-
char *name)
2957+
static int nvme_ctrl_scan_namespace(struct nvme_global_ctx *ctx,
2958+
struct nvme_ctrl *c, char *name)
29432959
{
29442960
struct nvme_ns *n, *_n, *__n;
29452961
int ret;
@@ -2950,7 +2966,7 @@ static int nvme_ctrl_scan_namespace(struct nvme_global_ctx *ctx, struct nvme_ctr
29502966
nvme_msg(ctx, LOG_DEBUG, "no subsystem for %s\n", name);
29512967
return -EINVAL;
29522968
}
2953-
ret = __nvme_scan_namespace(c->sysfs_dir, name, &n);
2969+
ret = __nvme_scan_namespace(ctx, c->sysfs_dir, name, &n);
29542970
if (ret) {
29552971
nvme_msg(ctx, LOG_DEBUG, "failed to scan namespace %s\n", name);
29562972
return ret;
@@ -2968,15 +2984,15 @@ static int nvme_ctrl_scan_namespace(struct nvme_global_ctx *ctx, struct nvme_ctr
29682984
return 0;
29692985
}
29702986

2971-
static int nvme_subsystem_scan_namespace(struct nvme_global_ctx *ctx, nvme_subsystem_t s,
2972-
char *name)
2987+
static int nvme_subsystem_scan_namespace(struct nvme_global_ctx *ctx,
2988+
nvme_subsystem_t s, char *name)
29732989
{
29742990
struct nvme_ns *n, *_n, *__n;
29752991
int ret;
29762992

29772993
nvme_msg(ctx, LOG_DEBUG, "scan subsystem %s namespace %s\n",
29782994
s->name, name);
2979-
ret = __nvme_scan_namespace(s->sysfs_dir, name, &n);
2995+
ret = __nvme_scan_namespace(ctx, s->sysfs_dir, name, &n);
29802996
if (ret) {
29812997
nvme_msg(ctx, LOG_DEBUG, "failed to scan namespace %s\n", name);
29822998
return ret;

libnvme/src/nvme/tree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,8 @@ char *nvme_get_path_attr(nvme_path_t p, const char *attr);
15641564
*
15651565
* Return: 0 on success or negative error code otherwise
15661566
*/
1567-
int nvme_scan_namespace(const char *name, nvme_ns_t *ns);
1567+
int nvme_scan_namespace(struct nvme_global_ctx *ctx, const char *name,
1568+
nvme_ns_t *ns);
15681569

15691570
/**
15701571
* nvme_host_get_hostsymname() - Get the host's symbolic name

plugins/sandisk/sandisk-utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int sndk_get_pci_ids(struct nvme_global_ctx *ctx, struct nvme_transport_handle *
4848
nvme_ctrl_get_sysfs_dir(c));
4949
nvme_free_ctrl(c);
5050
} else {
51-
ret = nvme_scan_namespace(name, &n);
51+
ret = nvme_scan_namespace(ctx, name, &n);
5252
if (!ret) {
5353
fprintf(stderr, "Unable to find %s\n", name);
5454
return ret;

plugins/solidigm/solidigm-get-drive-info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int sldgm_get_drive_info(int argc, char **argv, struct command *acmd, struct plu
4848
if (!err)
4949
n = nvme_ctrl_first_ns(c);
5050
else {
51-
err = nvme_scan_namespace(nvme_transport_handle_get_name(hdl), &n);
51+
err = nvme_scan_namespace(ctx, nvme_transport_handle_get_name(hdl), &n);
5252
if (err) {
5353
nvme_show_error("solidigm-vs-drive-info: drive missing namespace");
5454
return err;

plugins/wdc/wdc-nvme.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ static int wdc_get_pci_ids(struct nvme_global_ctx *ctx, struct nvme_transport_ha
14901490
nvme_ctrl_get_sysfs_dir(c));
14911491
nvme_free_ctrl(c);
14921492
} else {
1493-
ret = nvme_scan_namespace(name, &n);
1493+
ret = nvme_scan_namespace(ctx, name, &n);
14941494
if (ret) {
14951495
fprintf(stderr, "Unable to find %s\n", name);
14961496
return ret;

0 commit comments

Comments
 (0)