Skip to content

Commit

Permalink
nvme: use cleanup helper for nvme_root_t objects
Browse files Browse the repository at this point in the history
Use a cleanup helper for the nvme_root_t objects which simplifies the
error paths and ensures we are always removing all the allocated
resources.

Signed-off-by: Daniel Wagner <[email protected]>
  • Loading branch information
igaw committed May 10, 2024
1 parent 3dcd180 commit aa594c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
24 changes: 8 additions & 16 deletions nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -3269,7 +3269,7 @@ static bool nvme_match_device_filter(nvme_subsystem_t s,
static int list_subsys(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
nvme_root_t r = NULL;
_cleanup_nvme_root_ nvme_root_t r = NULL;
enum nvme_print_flags flags;
const char *desc = "Retrieve information for subsystems";
nvme_scan_filter_t filter = NULL;
Expand All @@ -3281,7 +3281,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,

err = argconfig_parse(argc, argv, desc, opts);
if (err < 0)
goto ret;
return err;

devname = NULL;
if (optind < argc)
Expand All @@ -3303,17 +3303,15 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
nvme_show_error("Failed to scan nvme subsystem for %s", devname);
else
nvme_show_error("Failed to scan nvme subsystem");
err = -errno;
goto ret;
return -errno;
}

if (devname) {
int subsys_num;

if (sscanf(devname, "nvme%dn%d", &subsys_num, &nsid) != 2) {
nvme_show_error("Invalid device name %s", devname);
err = -EINVAL;
goto ret;
return -EINVAL;
}
filter = nvme_match_device_filter;
}
Expand All @@ -3322,22 +3320,19 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
if (err) {
if (errno != ENOENT)
nvme_show_error("Failed to scan topology: %s", nvme_strerror(errno));
goto ret;
return -errno;
}

nvme_show_subsystem_list(r, nsid != NVME_NSID_ALL, flags);

ret:
if (r)
nvme_free_tree(r);
return err;
return 0;
}

static int list(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve basic information for all NVMe namespaces";
enum nvme_print_flags flags;
nvme_root_t r;
_cleanup_nvme_root_ nvme_root_t r = NULL;
int err = 0;

NVME_ARGS(opts);
Expand All @@ -3364,12 +3359,10 @@ static int list(int argc, char **argv, struct command *cmd, struct plugin *plugi
if (err < 0) {
if (errno != ENOENT)
nvme_show_error("Failed to scan topology: %s", nvme_strerror(errno));
nvme_free_tree(r);
return err;
}

nvme_show_list_items(r, flags);
nvme_free_tree(r);

return err;
}
Expand Down Expand Up @@ -9508,7 +9501,7 @@ static int show_topology_cmd(int argc, char **argv, struct command *command, str
const char *desc = "Show the topology\n";
const char *ranking = "Ranking order: namespace|ctrl";
enum nvme_print_flags flags;
nvme_root_t r;
_cleanup_nvme_root_ nvme_root_t r = NULL;
enum nvme_cli_topo_ranking rank;
int err;

Expand Down Expand Up @@ -9560,7 +9553,6 @@ static int show_topology_cmd(int argc, char **argv, struct command *command, str
}

nvme_show_topology(r, rank, flags);
nvme_free_tree(r);

return err;
}
Expand Down
9 changes: 9 additions & 0 deletions util/cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <unistd.h>
#include <stdlib.h>

#include <libnvme.h>

#include "util/mem.h"

#define __cleanup__(fn) __attribute__((cleanup(fn)))
Expand Down Expand Up @@ -34,4 +36,11 @@ static inline void close_file(int *f)
}
#define _cleanup_file_ __cleanup__(close_file)

static inline void cleanup_nvme_root(nvme_root_t *r)
{
if (r)
nvme_free_tree(*r);
}
#define _cleanup_nvme_root_ __cleanup__(cleanup_nvme_root)

#endif

0 comments on commit aa594c4

Please sign in to comment.