Skip to content

Commit e642498

Browse files
committed
tree: free ctrl attributes when (re)configure ctrl
The ctrl object can be reused, thus all resources should be freed first. While at it also reorder the declaration section. Reported-by: Martin George <marting@netapp.com> Signed-off-by: Daniel Wagner <wagi@kernel.org>
1 parent 2ca023a commit e642498

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

src/nvme/tree.c

Lines changed: 35 additions & 23 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,9 +2097,8 @@ 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);
2083-
c->sysfs_dir = (char *)path;
2101+
c->sysfs_dir = strdup(path);
20842102
c->firmware = nvme_get_ctrl_attr(c, "firmware_rev");
20852103
c->model = nvme_get_ctrl_attr(c, "model");
20862104
c->state = nvme_get_ctrl_attr(c, "state");
@@ -2101,10 +2119,8 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
21012119

21022120
int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
21032121
{
2104-
_cleanup_free_ char *subsys_name = NULL;
2105-
_cleanup_free_ char *name = NULL;
2122+
_cleanup_free_ char *subsys_name = NULL, *name = NULL, *path = NULL;
21062123
nvme_subsystem_t s;
2107-
char *path;
21082124
int ret;
21092125

21102126
ret = asprintf(&name, "nvme%d", instance);
@@ -2118,11 +2134,9 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
21182134
return ret;
21192135
}
21202136

2121-
ret = nvme_configure_ctrl(h->r, c, path, name);
2122-
if (ret < 0) {
2123-
free(path);
2137+
ret = nvme_reconfigure_ctrl(h->r, c, path, name);
2138+
if (ret < 0)
21242139
return ret;
2125-
}
21262140

21272141
c->address = nvme_get_attr(path, "address");
21282142
if (!c->address && strcmp(c->transport, "loop")) {
@@ -2153,12 +2167,11 @@ int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance)
21532167
static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s,
21542168
const char *path, const char *name)
21552169
{
2156-
nvme_ctrl_t c, p;
2157-
_cleanup_free_ char *addr = NULL, *address = NULL;
2158-
char *a = NULL, *e = NULL;
2159-
_cleanup_free_ char *transport = NULL;
2160-
char *traddr = NULL, *trsvcid = NULL;
2170+
_cleanup_free_ char *addr = NULL, *address = NULL, *transport = NULL;
21612171
char *host_traddr = NULL, *host_iface = NULL;
2172+
char *traddr = NULL, *trsvcid = NULL;
2173+
char *a = NULL, *e = NULL;
2174+
nvme_ctrl_t c, p;
21622175
int ret;
21632176

21642177
transport = nvme_get_attr(path, "transport");
@@ -2240,22 +2253,22 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s,
22402253
errno = ENODEV;
22412254
return NULL;
22422255
}
2243-
c->address = addr;
2244-
addr = NULL;
2256+
FREE_CTRL_ATTR(c->address);
2257+
c->address = strdup(addr);
22452258
if (s->subsystype && !strcmp(s->subsystype, "discovery"))
22462259
c->discovery_ctrl = true;
2247-
ret = nvme_configure_ctrl(r, c, path, name);
2260+
ret = nvme_reconfigure_ctrl(r, c, path, name);
22482261
return (ret < 0) ? NULL : c;
22492262
}
22502263

22512264
nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
22522265
{
2266+
_cleanup_free_ char *subsysnqn = NULL, *subsysname = NULL;
2267+
_cleanup_free_ char *hostnqn = NULL, *hostid = NULL;
2268+
_cleanup_free_ char *path = NULL;
22532269
nvme_host_t h;
22542270
nvme_subsystem_t s;
22552271
nvme_ctrl_t c;
2256-
_cleanup_free_ char *path = NULL;
2257-
_cleanup_free_ char *hostnqn = NULL, *hostid = NULL;
2258-
_cleanup_free_ char *subsysnqn = NULL, *subsysname = NULL;
22592272
int ret;
22602273

22612274
nvme_msg(r, LOG_DEBUG, "scan controller %s\n", name);
@@ -2309,7 +2322,6 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
23092322
if (!c)
23102323
return NULL;
23112324

2312-
path = NULL;
23132325
nvme_ctrl_scan_paths(r, c);
23142326
nvme_ctrl_scan_namespaces(r, c);
23152327
return c;

0 commit comments

Comments
 (0)