Skip to content

Commit

Permalink
plugins/memblaze: code refactor on using cleanup_nvme_dev
Browse files Browse the repository at this point in the history
using cleanup_nvme_dev to open/close device

Signed-off-by: jinhua.huang <[email protected]>
  • Loading branch information
jinhuahuang2021 authored and igaw committed Mar 27, 2024
1 parent a49d11a commit 71ed2d1
Showing 1 changed file with 12 additions and 39 deletions.
51 changes: 12 additions & 39 deletions plugins/memblaze/memblaze-nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static int mb_get_additional_smart_log(int argc, char **argv, struct command *cm
"Get Memblaze vendor specific additional smart log, and show it.";
const char *namespace = "(optional) desired namespace";
const char *raw = "dump output in binary format";
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
struct config {
__u32 namespace_id;
bool raw_binary;
Expand Down Expand Up @@ -455,7 +455,6 @@ static int mb_get_additional_smart_log(int argc, char **argv, struct command *cm
if (err > 0)
nvme_show_status(err);

dev_close(dev);
return err;
}

Expand All @@ -479,7 +478,7 @@ static int mb_get_powermanager_status(int argc, char **argv, struct command *cmd
const char *desc = "Get Memblaze power management ststus\n (value 0 - 25w, 1 - 20w, 2 - 15w)";
__u32 result;
__u32 feature_id = MB_FEAT_POWER_MGMT;
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
int err;

OPT_ARGS(opts) = {
Expand Down Expand Up @@ -511,7 +510,6 @@ static int mb_get_powermanager_status(int argc, char **argv, struct command *cmd
mb_feature_to_string(feature_id), nvme_select_to_string(0), result);
else if (err > 0)
nvme_show_status(err);
dev_close(dev);
return err;
}

Expand All @@ -521,7 +519,7 @@ static int mb_set_powermanager_status(int argc, char **argv, struct command *cmd
const char *desc = "Set Memblaze power management status\n (value 0 - 25w, 1 - 20w, 2 - 15w)";
const char *value = "new value of feature (required)";
const char *save = "specifies that the controller shall save the attribute";
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
__u32 result;
int err;

Expand Down Expand Up @@ -571,7 +569,6 @@ static int mb_set_powermanager_status(int argc, char **argv, struct command *cmd
else if (err > 0)
nvme_show_status(err);

dev_close(dev);
return err;
}

Expand All @@ -587,7 +584,7 @@ static int mb_set_high_latency_log(int argc, char **argv, struct command *cmd,
" p2 value: 1 .. 5000 ms";
const char *param = "input parameters";
int param1 = 0, param2 = 0;
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
__u32 result;
int err;

Expand All @@ -614,12 +611,10 @@ static int mb_set_high_latency_log(int argc, char **argv, struct command *cmd,

if (parse_params(cfg.param, 2, &param1, &param2)) {
printf("setfeature: invalid formats %s\n", cfg.param);
dev_close(dev);
return -EINVAL;
}
if ((param1 == 1) && (param2 < P2MIN || param2 > P2MAX)) {
printf("setfeature: invalid high io latency threshold %d\n", param2);
dev_close(dev);
return -EINVAL;
}
cfg.value = (param1 << MB_FEAT_HIGH_LATENCY_VALUE_SHIFT) | param2;
Expand Down Expand Up @@ -648,7 +643,6 @@ static int mb_set_high_latency_log(int argc, char **argv, struct command *cmd,
else if (err > 0)
nvme_show_status(err);

dev_close(dev);
return err;
}

Expand Down Expand Up @@ -744,7 +738,7 @@ static int mb_high_latency_log_print(int argc, char **argv, struct command *cmd,
{
const char *desc = "Get Memblaze high latency log";
char buf[LOG_PAGE_SIZE];
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
FILE *fdi = NULL;
int err;

Expand Down Expand Up @@ -774,7 +768,6 @@ static int mb_high_latency_log_print(int argc, char **argv, struct command *cmd,

if (fdi)
fclose(fdi);
dev_close(dev);
return err;
}

Expand Down Expand Up @@ -803,7 +796,7 @@ static int mb_selective_download(int argc, char **argv, struct command *cmd, str
int xfer = 4096;
void *fw_buf;
int selectNo, fw_fd, fw_size, err, offset = 0;
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
struct stat sb;
int i;

Expand Down Expand Up @@ -917,7 +910,6 @@ static int mb_selective_download(int argc, char **argv, struct command *cmd, str
out_close:
close(fw_fd);
out:
dev_close(dev);
return err;
}

Expand Down Expand Up @@ -1011,7 +1003,7 @@ static int mb_lat_stats_log_print(int argc, char **argv, struct command *cmd, st
char stats[LOG_PAGE_SIZE];
char f1[] = FID_C1_LOG_FILENAME;
char f2[] = FID_C2_LOG_FILENAME;
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
int err;

const char *desc = "Get Latency Statistics log and show it.";
Expand Down Expand Up @@ -1041,15 +1033,14 @@ static int mb_lat_stats_log_print(int argc, char **argv, struct command *cmd, st
else
nvme_show_status(err);

dev_close(dev);
return err;
}

static int memblaze_clear_error_log(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
char *desc = "Clear Memblaze devices error log.";
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
int err;

__u32 result;
Expand Down Expand Up @@ -1098,7 +1089,6 @@ static int memblaze_clear_error_log(int argc, char **argv, struct command *cmd,
else if (err > 0)
nvme_show_status(err);

dev_close(dev);
return err;
}

Expand All @@ -1116,7 +1106,7 @@ static int mb_set_lat_stats(int argc, char **argv, struct command *command, stru
const __u32 cdw12 = 0x0;
const __u32 data_len = 32;
const __u32 save = 0;
struct nvme_dev *dev;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
void *buf = NULL;
__u32 result;
int err;
Expand Down Expand Up @@ -1190,7 +1180,6 @@ static int mb_set_lat_stats(int argc, char **argv, struct command *command, stru
result);
} else {
printf("Could not read feature id 0xE2.\n");
dev_close(dev);
return err;
}
break;
Expand All @@ -1210,7 +1199,6 @@ static int mb_set_lat_stats(int argc, char **argv, struct command *command, stru
printf("%d not supported.\n", option);
err = EINVAL;
}
dev_close(dev);
return err;
}

Expand Down Expand Up @@ -1620,9 +1608,7 @@ static int mb_get_smart_log_add(int argc, char **argv, struct command *cmd, stru
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, "dump the whole log buffer in binary format"),
OPT_END()};

// Open device

struct nvme_dev *dev = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;

err = parse_and_open(&dev, argc, argv, cmd->help, opts);
if (err)
Expand All @@ -1645,9 +1631,6 @@ static int mb_get_smart_log_add(int argc, char **argv, struct command *cmd, stru
nvme_show_error("%s: %s", cmd->name, nvme_strerror(errno));
}

// Close device

dev_close(dev);
return err;
}

Expand Down Expand Up @@ -1761,9 +1744,7 @@ static int mb_set_latency_feature(int argc, char **argv, struct command *cmd, st
"set trim high latency log threshold, it's a 0-based value and unit is 10ms"),
OPT_END()};

// Open device

struct nvme_dev *dev = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;

err = parse_and_open(&dev, argc, argv, cmd->help, opts);
if (err)
Expand Down Expand Up @@ -1802,9 +1783,6 @@ static int mb_set_latency_feature(int argc, char **argv, struct command *cmd, st
else
nvme_show_error("%s: %s", cmd->name, nvme_strerror(errno));

// Close device

dev_close(dev);
return err;
}

Expand All @@ -1817,9 +1795,7 @@ static int mb_get_latency_feature(int argc, char **argv, struct command *cmd, st
OPT_ARGS(opts) = {
OPT_END()};

// Open device

struct nvme_dev *dev = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;

err = parse_and_open(&dev, argc, argv, cmd->help, opts);
if (err)
Expand Down Expand Up @@ -1850,8 +1826,5 @@ static int mb_get_latency_feature(int argc, char **argv, struct command *cmd, st
nvme_show_error("%s: %s", cmd->name, nvme_strerror(errno));
}

// Close device

dev_close(dev);
return err;
}

0 comments on commit 71ed2d1

Please sign in to comment.