]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
plugins/memblaze: code refactor on using cleanup_nvme_dev
authorjinhua.huang <jinhua.huang@memblaze.com>
Wed, 27 Mar 2024 02:08:55 +0000 (10:08 +0800)
committerDaniel Wagner <wagi@monom.org>
Wed, 27 Mar 2024 08:13:56 +0000 (09:13 +0100)
using cleanup_nvme_dev to open/close device

Signed-off-by: jinhua.huang <jinhua.huang@memblaze.com>
plugins/memblaze/memblaze-nvme.c

index a1888f46a005e821765d35fb9dc96a36baba420e..88df87e97aa758c4586ebd629ecb6e827792e6e8 100644 (file)
@@ -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;
@@ -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;
 }
 
@@ -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) = {
@@ -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;
 }
 
@@ -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;
 
@@ -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;
 }
 
@@ -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;
 
@@ -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;
@@ -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;
 }
 
@@ -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;
 
@@ -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;
 }
 
@@ -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;
 
@@ -917,7 +910,6 @@ out_free:
 out_close:
        close(fw_fd);
 out:
-       dev_close(dev);
        return err;
 }
 
@@ -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.";
@@ -1041,7 +1033,6 @@ 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;
 }
 
@@ -1049,7 +1040,7 @@ 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;
@@ -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;
 }
 
@@ -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;
@@ -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;
@@ -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;
 }
 
@@ -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)
@@ -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;
 }
 
@@ -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)
@@ -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;
 }
 
@@ -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)
@@ -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;
 }