Skip to content

Commit b39e119

Browse files
committed
tree: add nvme_global_ctx to struct nvme_ns and struct nvme_ctrl
Add the global context to struct nvme_ns and struct nvme_ctrl, so it's possible to access the context within the tree operations. This avoids the need to add the global context pointer to every single function. Only those which create the objects need it. Signed-off-by: Daniel Wagner <[email protected]>
1 parent b004ae8 commit b39e119

6 files changed

Lines changed: 21 additions & 11 deletions

File tree

libnvme/src/nvme/private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ struct nvme_ns {
140140
struct nvme_ctrl *c;
141141
struct nvme_ns_head *head;
142142

143+
struct nvme_global_ctx *ctx;
143144
struct nvme_transport_handle *hdl;
144145
__u32 nsid;
145146
char *name;
@@ -164,6 +165,7 @@ struct nvme_ctrl {
164165
struct list_head namespaces;
165166
struct nvme_subsystem *s;
166167

168+
struct nvme_global_ctx *ctx;
167169
struct nvme_transport_handle *hdl;
168170
char *name;
169171
char *sysfs_dir;

libnvme/src/nvme/tree.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@ int nvme_create_ctrl(struct nvme_global_ctx *ctx,
14551455
if (!c)
14561456
return -ENOMEM;
14571457

1458+
c->ctx = ctx;
14581459
c->hdl = NULL;
14591460
nvmf_default_config(&c->cfg);
14601461
list_head_init(&c->namespaces);
@@ -2771,7 +2772,8 @@ static void nvme_ns_set_generic_name(struct nvme_ns *n, const char *name)
27712772
n->generic_name = strdup(generic_name);
27722773
}
27732774

2774-
int nvme_ns_open(const char *sys_path, const char *name, nvme_ns_t *ns)
2775+
static int nvme_ns_open(struct nvme_global_ctx *ctx, const char *sys_path,
2776+
const char *name, nvme_ns_t *ns)
27752777
{
27762778
int ret;
27772779
struct nvme_ns *n;
@@ -2808,6 +2810,7 @@ int nvme_ns_open(const char *sys_path, const char *name, nvme_ns_t *ns)
28082810
} else
28092811
head->sysfs_dir = NULL;
28102812

2813+
n->ctx = ctx;
28112814
n->head = head;
28122815
n->hdl = NULL;
28132816
n->name = strdup(name);
@@ -2858,7 +2861,8 @@ static char *nvme_ns_generic_to_blkdev(const char *generic)
28582861
return strdup(blkdev);
28592862
}
28602863

2861-
static int __nvme_scan_namespace(const char *sysfs_dir, const char *name, nvme_ns_t *ns)
2864+
static int __nvme_scan_namespace(struct nvme_global_ctx *ctx,
2865+
const char *sysfs_dir, const char *name, nvme_ns_t *ns)
28622866
{
28632867
_cleanup_free_ char *blkdev = NULL;
28642868
_cleanup_free_ char *path = NULL;
@@ -2873,7 +2877,7 @@ static int __nvme_scan_namespace(const char *sysfs_dir, const char *name, nvme_n
28732877
if (ret < 0)
28742878
return -ENOMEM;
28752879

2876-
ret = nvme_ns_open(path, blkdev, &n);
2880+
ret = nvme_ns_open(ctx, path, blkdev, &n);
28772881
if (ret)
28782882
return ret;
28792883

@@ -2884,9 +2888,10 @@ static int __nvme_scan_namespace(const char *sysfs_dir, const char *name, nvme_n
28842888
return 0;
28852889
}
28862890

2887-
int nvme_scan_namespace(const char *name, nvme_ns_t *ns)
2891+
int nvme_scan_namespace(struct nvme_global_ctx *ctx, const char *name,
2892+
nvme_ns_t *ns)
28882893
{
2889-
return __nvme_scan_namespace(nvme_ns_sysfs_dir(), name, ns);
2894+
return __nvme_scan_namespace(ctx, nvme_ns_sysfs_dir(), name, ns);
28902895
}
28912896

28922897

@@ -2966,7 +2971,7 @@ static int nvme_ctrl_scan_namespace(struct nvme_global_ctx *ctx, struct nvme_ctr
29662971
nvme_msg(ctx, LOG_DEBUG, "no subsystem for %s\n", name);
29672972
return -EINVAL;
29682973
}
2969-
ret = __nvme_scan_namespace(c->sysfs_dir, name, &n);
2974+
ret = __nvme_scan_namespace(ctx, c->sysfs_dir, name, &n);
29702975
if (ret) {
29712976
nvme_msg(ctx, LOG_DEBUG, "failed to scan namespace %s\n", name);
29722977
return ret;
@@ -2992,7 +2997,7 @@ static int nvme_subsystem_scan_namespace(struct nvme_global_ctx *ctx, nvme_subsy
29922997

29932998
nvme_msg(ctx, LOG_DEBUG, "scan subsystem %s namespace %s\n",
29942999
s->name, name);
2995-
ret = __nvme_scan_namespace(s->sysfs_dir, name, &n);
3000+
ret = __nvme_scan_namespace(ctx, s->sysfs_dir, name, &n);
29963001
if (ret) {
29973002
nvme_msg(ctx, LOG_DEBUG, "failed to scan namespace %s\n", name);
29983003
return ret;

libnvme/src/nvme/tree.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,12 +1493,14 @@ char *nvme_get_path_attr(nvme_path_t p, const char *attr);
14931493

14941494
/**
14951495
* nvme_scan_namespace() - scan namespace based on sysfs name
1496+
* @ctx: &struct nvme_global_ctx object
14961497
* @name: sysfs name of the namespace to scan
14971498
* @ns: &nvme_ns_t object to return
14981499
*
14991500
* Return: 0 on success or negative error code otherwise
15001501
*/
1501-
int nvme_scan_namespace(const char *name, nvme_ns_t *ns);
1502+
int nvme_scan_namespace(struct nvme_global_ctx *ctx, const char *name,
1503+
nvme_ns_t *ns);
15021504

15031505
/**
15041506
* 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ 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,
52+
nvme_transport_handle_get_name(hdl), &n);
5253
if (err) {
5354
nvme_show_error("solidigm-vs-drive-info: drive missing namespace");
5455
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)