Skip to content

Commit 0c4ea43

Browse files
committed
libnvme: Re-implement __nvme_lookup_ctrl()
Re-implement __nvme_lookup_ctrl() as equivalent to nvme_lookup_ctrl() but taking 'struct nvmf_context' as argument. Care needs to be taken to handle the 'subsysnqn' entry in 'struct nvmf_context'; __nvme_ctrl_find() needs to be called with a 'NULL' subsysnqn as the controller might be on any subsystem, and we need to set 'subsysnqn' to the NQN of the subsystem the controller should be created in rather than the subsysnqn from 'struct nvmf_context' (which might be empty). Signed-off-by: Hannes Reinecke <[email protected]>
1 parent d9bd6ec commit 0c4ea43

4 files changed

Lines changed: 59 additions & 36 deletions

File tree

libnvme/src/nvme/fabrics.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,9 +2425,7 @@ __public int nvmf_config_modify(struct nvme_global_ctx *ctx,
24252425
return -ENODEV;
24262426
}
24272427

2428-
c = nvme_lookup_ctrl(s, fctx->transport, fctx->traddr,
2429-
fctx->host_traddr, fctx->host_iface,
2430-
fctx->trsvcid, NULL);
2428+
c = __nvme_lookup_ctrl(s, fctx, NULL);
24312429
if (!c) {
24322430
nvme_msg(ctx, LOG_ERR, "Failed to lookup controller\n");
24332431
return -ENODEV;

libnvme/src/nvme/json.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,25 @@ static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj)
8383
{
8484
nvme_ctrl_t c;
8585
struct json_object *attr_obj;
86-
const char *transport, *traddr = NULL;
87-
const char *host_traddr = NULL, *host_iface = NULL, *trsvcid = NULL;
86+
struct nvmf_context fctx = {};
8887

8988
attr_obj = json_object_object_get(port_obj, "transport");
9089
if (!attr_obj)
9190
return;
92-
transport = json_object_get_string(attr_obj);
91+
fctx.transport = json_object_get_string(attr_obj);
9392
attr_obj = json_object_object_get(port_obj, "traddr");
9493
if (attr_obj)
95-
traddr = json_object_get_string(attr_obj);
94+
fctx.traddr = json_object_get_string(attr_obj);
9695
attr_obj = json_object_object_get(port_obj, "host_traddr");
9796
if (attr_obj)
98-
host_traddr = json_object_get_string(attr_obj);
97+
fctx.host_traddr = json_object_get_string(attr_obj);
9998
attr_obj = json_object_object_get(port_obj, "host_iface");
10099
if (attr_obj)
101-
host_iface = json_object_get_string(attr_obj);
100+
fctx.host_iface = json_object_get_string(attr_obj);
102101
attr_obj = json_object_object_get(port_obj, "trsvcid");
103102
if (attr_obj)
104-
trsvcid = json_object_get_string(attr_obj);
105-
c = nvme_lookup_ctrl(s, transport, traddr, host_traddr,
106-
host_iface, trsvcid, NULL);
103+
fctx.trsvcid = json_object_get_string(attr_obj);
104+
c = __nvme_lookup_ctrl(s, &fctx, NULL);
107105
if (!c)
108106
return;
109107
json_update_attributes(c, port_obj);

libnvme/src/nvme/private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ nvme_ctrl_t nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
399399
const char *traddr, const char *host_traddr,
400400
const char *host_iface, const char *trsvcid,
401401
nvme_ctrl_t p);
402+
nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s,
403+
struct nvmf_context *fctx,
404+
nvme_ctrl_t p);
402405
nvme_ctrl_t nvme_ctrl_find(nvme_subsystem_t s, struct nvmf_context *fctx);
403406

404407
void __nvme_free_host(nvme_host_t h);

libnvme/src/nvme/tree.c

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,9 +1451,9 @@ static ctrl_match_t _candidate_init(struct nvme_global_ctx *ctx,
14511451
return _match_ctrl;
14521452
}
14531453

1454-
static nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s,
1455-
struct nvmf_context *fctx,
1456-
nvme_ctrl_t p)
1454+
static nvme_ctrl_t __nvme_ctrl_find(nvme_subsystem_t s,
1455+
struct nvmf_context *fctx,
1456+
nvme_ctrl_t p)
14571457
{
14581458
struct candidate_args candidate = {};
14591459
struct nvme_ctrl *c, *matching_c = NULL;
@@ -1503,37 +1503,35 @@ __public bool nvme_ctrl_match_config(struct nvme_ctrl *c, const char *transport,
15031503

15041504
nvme_ctrl_t nvme_ctrl_find(nvme_subsystem_t s, struct nvmf_context *fctx)
15051505
{
1506-
return __nvme_lookup_ctrl(s, fctx, NULL/*p*/);
1506+
return __nvme_ctrl_find(s, fctx, NULL/*p*/);
15071507
}
15081508

1509-
nvme_ctrl_t nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
1510-
const char *traddr, const char *host_traddr,
1511-
const char *host_iface, const char *trsvcid,
1512-
nvme_ctrl_t p)
1509+
nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s,
1510+
struct nvmf_context *fctx,
1511+
nvme_ctrl_t p)
15131512
{
15141513
struct nvme_global_ctx *ctx;
15151514
struct nvme_ctrl *c;
1515+
const char *subsysnqn = fctx->subsysnqn;
15161516
int ret;
15171517

1518-
if (!s || !transport)
1518+
if (!s || !fctx->transport)
15191519
return NULL;
15201520

1521-
struct nvmf_context fctx = {
1522-
.transport = transport,
1523-
.traddr = traddr,
1524-
.host_traddr = host_traddr,
1525-
.host_iface = host_iface,
1526-
.trsvcid = trsvcid,
1527-
.subsysnqn = NULL,
1528-
};
1529-
1530-
c = __nvme_lookup_ctrl(s, &fctx, p);
1531-
if (c)
1521+
/* Clear out subsysnqn; might be different for discovery subsystems */
1522+
fctx->subsysnqn = NULL;
1523+
c = __nvme_ctrl_find(s, fctx, p);
1524+
if (c) {
1525+
fctx->subsysnqn = subsysnqn;
15321526
return c;
1527+
}
15331528

15341529
ctx = s->h ? s->h->ctx : NULL;
1535-
fctx.subsysnqn = s->subsysnqn;
1536-
ret = _nvme_create_ctrl(ctx, &fctx, &c);
1530+
/* Set the NQN to the subsystem the controller should be created in */
1531+
fctx->subsysnqn = s->subsysnqn;
1532+
ret = _nvme_create_ctrl(ctx, fctx, &c);
1533+
/* And restore NQN to avoid issues with repetitive calls */
1534+
fctx->subsysnqn = subsysnqn;
15371535
if (ret)
15381536
return NULL;
15391537

@@ -1543,6 +1541,26 @@ nvme_ctrl_t nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
15431541
return c;
15441542
}
15451543

1544+
nvme_ctrl_t nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
1545+
const char *traddr, const char *host_traddr,
1546+
const char *host_iface, const char *trsvcid,
1547+
nvme_ctrl_t p)
1548+
{
1549+
if (!s || !transport)
1550+
return NULL;
1551+
1552+
struct nvmf_context fctx = {
1553+
.transport = transport,
1554+
.traddr = traddr,
1555+
.host_traddr = host_traddr,
1556+
.host_iface = host_iface,
1557+
.trsvcid = trsvcid,
1558+
.subsysnqn = NULL,
1559+
};
1560+
1561+
return __nvme_lookup_ctrl(s, &fctx, p);
1562+
}
1563+
15461564
static int nvme_ctrl_scan_paths(struct nvme_global_ctx *ctx, struct nvme_ctrl *c)
15471565
{
15481566
_cleanup_dirents_ struct dirents paths = {};
@@ -1893,8 +1911,14 @@ int nvme_ctrl_alloc(struct nvme_global_ctx *ctx, nvme_subsystem_t s,
18931911
skip_address:
18941912
p = NULL;
18951913
do {
1896-
c = nvme_lookup_ctrl(s, transport, traddr,
1897-
host_traddr, host_iface, trsvcid, p);
1914+
struct nvmf_context fctx = {
1915+
.transport = transport,
1916+
.traddr = traddr,
1917+
.host_traddr = host_traddr,
1918+
.host_iface = host_iface,
1919+
.trsvcid = trsvcid,
1920+
};
1921+
c = __nvme_lookup_ctrl(s, &fctx, p);
18981922
if (c) {
18991923
if (!c->name)
19001924
break;

0 commit comments

Comments
 (0)