Skip to content

Commit a6ea317

Browse files
committed
tree: free ctrl attributes when (re)configure ctrl
The trl object can be reused, thus all resources should be freed first. Suggested-by: Martin George <[email protected]> Signed-off-by: Daniel Wagner <[email protected]>
1 parent 778aaf1 commit a6ea317

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

src/nvme/tree.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,11 +2064,30 @@ static void nvme_read_sysfs_tls(nvme_root_t r, nvme_ctrl_t c)
20642064
free(key);
20652065
}
20662066

2067-
static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
2068-
const char *name)
2067+
static int nvme_reconfigure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
2068+
const char *name)
20692069
{
20702070
DIR *d;
20712071

2072+
/*
2073+
* It's necesssary to release any resources first because a ctrl
2074+
* can be reused.
2075+
*/
2076+
nvme_ctrl_release_fd(c);
2077+
FREE_CTRL_ATTR(c->name);
2078+
FREE_CTRL_ATTR(c->sysfs_dir);
2079+
FREE_CTRL_ATTR(c->firmware);
2080+
FREE_CTRL_ATTR(c->model);
2081+
FREE_CTRL_ATTR(c->state);
2082+
FREE_CTRL_ATTR(c->numa_node);
2083+
FREE_CTRL_ATTR(c->queue_count);
2084+
FREE_CTRL_ATTR(c->serial);
2085+
FREE_CTRL_ATTR(c->sqsize);
2086+
FREE_CTRL_ATTR(c->cntrltype);
2087+
FREE_CTRL_ATTR(c->cntlid);
2088+
FREE_CTRL_ATTR(c->dctype);
2089+
FREE_CTRL_ATTR(c->phy_slot);
2090+
20722091
d = opendir(path);
20732092
if (!d) {
20742093
nvme_msg(r, LOG_ERR, "Failed to open ctrl dir %s, error %d\n",
@@ -2078,7 +2097,6 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
20782097
}
20792098
closedir(d);
20802099

2081-
c->fd = -1;
20822100
c->name = strdup(name);
20832101
c->sysfs_dir = (char *)path;
20842102
c->firmware = nvme_get_ctrl_attr(c, "firmware_rev");
@@ -2118,7 +2136,7 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
21182136
return ret;
21192137
}
21202138

2121-
ret = nvme_configure_ctrl(h->r, c, path, name);
2139+
ret = nvme_reconfigure_ctrl(h->r, c, path, name);
21222140
if (ret < 0) {
21232141
free(path);
21242142
return ret;
@@ -2240,11 +2258,12 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s,
22402258
errno = ENODEV;
22412259
return NULL;
22422260
}
2261+
FREE_CTRL_ATTR(c->address);
22432262
c->address = addr;
22442263
addr = NULL;
22452264
if (s->subsystype && !strcmp(s->subsystype, "discovery"))
22462265
c->discovery_ctrl = true;
2247-
ret = nvme_configure_ctrl(r, c, path, name);
2266+
ret = nvme_reconfigure_ctrl(r, c, path, name);
22482267
return (ret < 0) ? NULL : c;
22492268
}
22502269

0 commit comments

Comments
 (0)