]> www.infradead.org Git - users/hch/nvme-cli.git/commitdiff
Remove code duplication, use library
authorKeith Busch <kbusch@kernel.org>
Fri, 29 May 2020 19:10:09 +0000 (12:10 -0700)
committerKeith Busch <kbusch@kernel.org>
Fri, 29 May 2020 20:48:09 +0000 (13:48 -0700)
This is an ongoing effort, but just making a commit now as it's passing
more regressed tests.

Signed-off-by: Keith Busch <kbusch@kernel.org>
libnvme
nvme.c
plugins/wdc/wdc-nvme.c
print.c [deleted file]
util/user-types.h

diff --git a/libnvme b/libnvme
index a2e424322385bedfefbcf6620711083c7ff4f6c3..5fca2499aa7342ab7b57067982e54822409c8561 160000 (submodule)
--- a/libnvme
+++ b/libnvme
@@ -1 +1 @@
-Subproject commit a2e424322385bedfefbcf6620711083c7ff4f6c3
+Subproject commit 5fca2499aa7342ab7b57067982e54822409c8561
diff --git a/nvme.c b/nvme.c
index 3f20c671ae5ddcc978bdebb4ff5a77debc244990..5d361996e2b815d3b65a73bbe16b4f1db9db9b04 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -243,115 +243,115 @@ static void nvme_print_object(struct json_object *j)
        }
 }
 
-void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags)
+static void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_props_to_json(bar, flags));
 }
 
-void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
+static void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
                enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_ns_to_json(ns, flags));
 }
 
-void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags)
+static void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_ns_desc_list_to_json(data, flags));
 }
 
-void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, enum nvme_print_flags flags)
+static void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_ctrl_to_json(ctrl, flags));
 }
 
-void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
+static void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
        enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_nvm_set_list_to_json(nvmset, flags));
 }
 
-void nvme_show_list_secondary_ctrl(
+static void nvme_show_list_secondary_ctrl(
        struct nvme_secondary_ctrl_list *sc_list,
        __u32 count, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_secondary_ctrl_list_to_json(sc_list, flags));
 }
 
-void nvme_show_id_ns_granularity_list(struct nvme_id_ns_granularity_list *glist,
+static void nvme_show_id_ns_granularity_list(struct nvme_id_ns_granularity_list *glist,
        enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_ns_granularity_list_to_json(glist, flags));
 }
 
-void nvme_show_id_uuid_list(struct nvme_id_uuid_list *uuid_list,
+static void nvme_show_id_uuid_list(struct nvme_id_uuid_list *uuid_list,
                                enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_id_uuid_list_to_json(uuid_list, flags));
 }
 
-void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
+static void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
                        const char *devname, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_error_log_to_json(err_log, entries, flags));
 }
 
-void nvme_show_resv_report(struct nvme_resv_status *status, int bytes,
+static void nvme_show_resv_report(struct nvme_resv_status *status, int bytes,
        __u32 cdw11, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_resv_report_to_json(status, cdw11 & 1, flags));
 }
 
-void nvme_show_fw_log(struct nvme_firmware_slot *fw_log,
+static void nvme_show_fw_log(struct nvme_firmware_slot *fw_log,
        const char *devname, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_fw_slot_log_to_json(fw_log, flags));
 }
 
-void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
+static void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
                                   const char *devname,
                                   enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_ns_list_to_json(log, flags));
 }
 
-void nvme_show_effects_log(struct nvme_cmd_effects_log *effects,
+static void nvme_show_effects_log(struct nvme_cmd_effects_log *effects,
                           unsigned int flags)
 {
        nvme_print_object(nvme_cmd_effects_log_to_json(effects, flags));
 }
 
-void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
+static void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
                             __u16 group_id, const char *devname,
                             enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_endurance_group_log_to_json(endurance_log, flags));
 }
 
-void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
+static void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
                         const char *devname, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_smart_log_to_json(smart, flags));
 }
 
-void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
+static void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
                        enum nvme_print_flags flags, size_t len)
 {
        nvme_print_object(nvme_ana_log_to_json(ana_log, flags));
 }
 
-void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
+static void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
                             enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_dev_self_test_log_to_json(self_test, flags));
 }
 
-void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
+static void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
                            const char *devname, enum nvme_print_flags flags)
 {
        nvme_print_object(nvme_sanitize_log_to_json(sanitize, flags));
 }
 
-void nvme_directive_show(__u8 dtype, __u8 doper, __u16 spec, __u32 nsid, __u32 result,
+static void nvme_directive_show(__u8 dtype, __u8 doper, __u16 spec, __u32 nsid, __u32 result,
        void *buf, __u32 len, enum nvme_print_flags flags)
 {
        struct json_object *j = NULL;
@@ -391,17 +391,18 @@ void nvme_directive_show(__u8 dtype, __u8 doper, __u16 spec, __u32 nsid, __u32 r
                fprintf(stderr, "Unrecognized dtype:%d doper:%d\n", dtype, doper);
 }
 
-void nvme_feature_show_fields(__u32 fid, unsigned int result, void **buf,
+static void nvme_feature_show_fields(__u32 fid, unsigned int result, void **buf,
        unsigned long flags)
 {
        nvme_print_object(nvme_feature_to_json(fid, result, 0, buf, flags));
 }
 
-void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
+static void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
                        enum nvme_print_flags flags)
 {
-       nvme_lba_status_desc_list_to_json(list, flags);
+       nvme_print_object(nvme_lba_status_desc_list_to_json(list, flags));
 }
+
 static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
 {
        struct nvme_smart_log log;
@@ -5027,7 +5028,7 @@ static int connect_cmd(int argc, char **argv, struct command *command, struct pl
        };
 
        OPT_ARGS(opts) = {
-               OPT_LIST("nqn", 'n', &cfg.nqn, nvmf_nqn),
+               OPT_STRING("nqn", 'n', "NAME", &cfg.nqn, nvmf_nqn),
                NVMF_OPTS(cfg),
                OPT_END()
        };
@@ -5080,8 +5081,8 @@ static int disconnect_cmd(int argc, char **argv, struct command *command, struct
        struct config cfg = { 0 };
 
        OPT_ARGS(opts) = {
-               OPT_LIST("nqn",    'n', &cfg.nqn,    nqn),
-               OPT_LIST("device", 'd', &cfg.device, device),
+               OPT_STRING("nqn",    'n', "NAME", &cfg.nqn,    nqn),
+               OPT_STRING("device", 'd', "DEV",  &cfg.device, device),
                OPT_END()
        };
 
index a805b9c35a77490241406ba8717154e349c9333c..3274b247c6c970cc021cf4d99e8fcf954bde5160 100644 (file)
@@ -755,78 +755,71 @@ static long double int128_to_double(__u8 *data)
 
 static int wdc_get_pci_ids(uint32_t *device_id, uint32_t *vendor_id)
 {
-       int fd, ret = -1;
-       char *block, path[512], *id;
-
-       id = calloc(1, 32);
-       if (!id) {
-               fprintf(stderr, "ERROR : WDC : %s : calloc failed\n", __func__);
-               return -1;
-       }
-
-       block = (char *)devicename;
+       char vid[256], did[256], id[32];
+       nvme_ctrl_t c = NULL;
+       nvme_ns_t n = NULL;
+       int fd, ret;
 
-       /* read the vendor ID from sys fs  */
-       sprintf(path, "/sys/class/nvme/%s/device/vendor", block);
+       c = nvme_scan_ctrl(devicename);
+       if (c) {
+               snprintf(vid, sizeof(vid), "%s/device/vendor",
+                       nvme_ctrl_get_sysfs_dir(c));
+               snprintf(did, sizeof(did), "%s/device/device",
+                       nvme_ctrl_get_sysfs_dir(c));
+               nvme_free_ctrl(c);
+       } else {
+               n = nvme_scan_namespace(devicename);
+               if (!n) {
+                       fprintf(stderr, "Unable to find %s\n", devicename);
+                       return -1;
+               }
 
-       fd = open(path, O_RDONLY);
-       if (fd < 0) {
-               sprintf(path, "/sys/class/misc/%s/device/vendor", block);
-               fd = open(path, O_RDONLY);
+               snprintf(vid, sizeof(vid), "%s/device/device/vendor",
+                       nvme_ns_get_sysfs_dir(n));
+               snprintf(did, sizeof(did), "%s/device/device/device",
+                       nvme_ns_get_sysfs_dir(n));
+               nvme_free_ns(n);
        }
+
+       fd = open(vid, O_RDONLY);
        if (fd < 0) {
                fprintf(stderr, "ERROR : WDC : %s : Open vendor file failed\n", __func__);
-               ret = -1;
-               goto free_id;
+               return -1;
        }
 
        ret = read(fd, id, 32);
+       close(fd);
+
        if (ret < 0) {
                fprintf(stderr, "%s: Read of pci vendor id failed\n", __func__);
-               ret = -1;
-               goto close_fd;
-       } else {
-               if (id[strlen(id) - 1] == '\n')
-                       id[strlen(id) - 1] = '\0';
-
-               /* convert the device id string to an int  */
-               *vendor_id = (int)strtol(&id[2], NULL, 16);
-               ret = 0;
+               return -1;
        }
 
-       /* read the device ID from sys fs */
-       sprintf(path, "/sys/class/nvme/%s/device/device", block);
+       if (id[strlen(id) - 1] == '\n')
+               id[strlen(id) - 1] = '\0';
 
-       fd = open(path, O_RDONLY);
-       if (fd < 0) {
-               sprintf(path, "/sys/class/misc/%s/device/device", block);
-               fd = open(path, O_RDONLY);
-       }
+       *vendor_id = strtol(id, NULL, 0);
+       ret = 0;
+
+       fd = open(did, O_RDONLY);
        if (fd < 0) {
                fprintf(stderr, "ERROR : WDC : %s : Open device file failed\n", __func__);
-               ret = -1;
-               goto close_fd;
+               return -1;
        }
 
        ret = read(fd, id, 32);
+       close(fd);
+
        if (ret < 0) {
                fprintf(stderr, "%s: Read of pci device id failed\n", __func__);
-               ret = -1;
-       } else {
-               if (id[strlen(id) - 1] == '\n')
-                       id[strlen(id) - 1] = '\0';
-
-               /* convert the device id string to an int  */
-               *device_id = strtol(&id[2], NULL, 16);
-               ret = 0;
+               return -1;
        }
 
-close_fd:
-       close(fd);
-free_id:
-       free(block);
-       free(id);
-       return ret;
+       if (id[strlen(id) - 1] == '\n')
+               id[strlen(id) - 1] = '\0';
+
+       *device_id = strtol(id, NULL, 0);
+       return 0;
 }
 
 static bool wdc_check_device(int fd)
diff --git a/print.c b/print.c
deleted file mode 100644 (file)
index e57d3e2..0000000
--- a/print.c
+++ /dev/null
@@ -1,192 +0,0 @@
-#include <assert.h>
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-
-#include <uuid/uuid.h>
-
-#include "print.h"
-#include "models.h"
-#include "util/json.h"
-#include "util/suffix.h"
-#include "common.h"
-
-
-static const uint8_t zero_uuid[16] = { 0 };
-static const uint8_t invalid_uuid[16] = {[0 ... 15] = 0xff };
-
-static void nvme_print_object(struct json_object *j)
-{
-       const unsigned long jflags =
-               JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY;
-
-       if (j) {
-               nvme_json_object_print(stdout, j, jflags);
-               json_object_put(j);
-       }
-}
-
-void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_props_to_json(bar, flags));
-}
-
-void nvme_show_single_property(int offset, uint64_t value64, int human)
-{
-}
-
-void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
-               enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_id_ns_to_json(ns, mode));
-}
-
-void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_id_ns_desc_list_to_json(data, flags));
-}
-
-void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, unsigned int mode)
-{
-       nvme_print_object(nvme_id_ctrl_to_json(ctrl, mode));
-}
-
-void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
-       enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_id_nvm_set_list_to_json(nvmset, flags));
-}
-
-void nvme_show_list_secondary_ctrl(
-       struct nvme_secondary_ctrl_list *sc_list,
-       __u32 count, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_id_secondary_ctrl_list_to_json(sc_list, flags));
-}
-
-void nvme_show_id_ns_granularity_list(struct nvme_id_ns_granularity_list *glist,
-       enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_id_ns_granularity_list_to_json(glist, flags));
-}
-
-void nvme_show_id_uuid_list(const struct nvme_id_uuid_list *uuid_list,
-                               enum nvme_print_flags flags)
-{
-       nnvme_print_object(vme_id_uuid_list_to_json(uuid_list, flags));
-}
-
-void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
-                       const char *devname, enum nvme_print_flags flags)
-{
-       nnvme_print_object(vme_error_log_to_json(err_log, entries, flags));
-}
-
-void nvme_show_resv_report(struct nvme_reservation_status *status, int bytes,
-       __u32 cdw11, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_resv_report_to_json(status, cdw11 & 1, flags));
-}
-
-void nvme_show_fw_log(struct nvme_firmware_slot *fw_log,
-       const char *devname, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_fw_slot_log_to_json(fw_log, flags));
-}
-
-void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
-                                  const char *devname,
-                                  enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_ns_list_to_json(log, flags));
-}
-
-void nvme_show_effects_log(struct nvme_cmd_effects_log *effects,
-                          unsigned int flags)
-{
-       nvme_print_object(nvme_cmd_effects_log_to_json(effects, flags));
-}
-
-void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
-                            __u16 group_id, const char *devname,
-                            enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_endurance_group_log_to_json(endurance_log, flags));
-}
-
-void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
-                        const char *devname, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_smart_log_to_json(smart, flags));
-}
-
-void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
-                       enum nvme_print_flags flags, size_t len)
-{
-       nvme_print_object(nvme_ana_log_to_json(ana_log, flags));
-}
-
-void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
-                            enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_dev_self_test_log_to_json(self_test, flags));
-}
-
-void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
-                           const char *devname, enum nvme_print_flags flags)
-{
-       nvme_print_object(nvme_sanitize_log_to_json(sanitize, flags));
-}
-
-void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result,
-       void *buf, __u32 len, enum nvme_print_flags flags)
-{
-       struct json_object *j;
-
-       switch (dtype) {
-       case NVME_DIRECTIVE_DTYPE_IDENTIFY:
-               switch (doper) {
-               case NVME_DIRECTIVE_RECEIVE_IDENTIFY_DOPER_PARAM:
-                       j = nvme_identify_directives_to_json(buf, flags);
-                       break;
-               default:
-                       break;
-               }
-               break;
-       case NVME_DIRECTIVE_DTYPE_STREAMS:
-               switch (doper) {
-               case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_PARAM:
-                       j = nvme_streams_directive_params (buf, flags);
-                       break;
-               case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_STATUS:
-                       j = nvme_streams_status_to_json(buf, flags);
-                       break;
-               case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_RESOURCE:
-                       break;
-               default:
-                       break;
-               }
-               break;
-       default:
-               break;
-       }
-
-       if (j)
-               nvme_print_object(j);
-       else
-               fprintf(stderr, "Unrecognized dtype:%d doper:%d\n", dtype, doper);
-}
-
-void nvme_feature_show_fields(__u32 fid, unsigned int result, void **buf,
-       unsigned long flags)
-{
-       nvme_print_object(nvme_feature_to_json(fid, result, 0, buf, flags));
-}
-
-void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
-                       enum nvme_print_flags flags)
-{
-       nvme_lba_status_desc_list_to_json(list, flags);
-}
index dad5f5b6dcbfb80e4732daa8ed57454b76a368ff..7268bc0dd2a58af46eba51b14b33df4b15bd0fc1 100644 (file)
@@ -7,7 +7,7 @@
 
 #include <json-c/json.h>
 
-#include "libnvme.h"
+#include <libnvme.h>
 #include "nvme.h"
 
 enum nvme_print_flags {
@@ -178,18 +178,6 @@ static inline uint64_t int48_to_long(__u8 *data)
        return (uint64_t)unalign_int((uint8_t *)data, 6);
 }
 
-static inline __u64 read64(void *addr)
-{
-       __le32 *p = addr;
-       return le32_to_cpu(*p) | ((__u64)le32_to_cpu(*(p + 1)) << 32);
-}
-
-static inline __u32 read32(void *addr)
-{
-       __le32 *p = addr;
-       return le32_to_cpu(*p);
-}
-
 static inline void nvme_json_add_str_len(struct json_object *j, const char *n,
                                         const char *v, int l, unsigned long flags)
 {
@@ -235,14 +223,14 @@ static inline void nvme_json_add_flag(struct json_object *j, const char *n,
 static inline void nvme_json_add_le64_ptr(struct json_object *j, const char *n,
                                          void *addr)
 {
-       __u64 v = read64(addr);
+       __u64 v = nvme_mmio_read64(addr);
        nvme_json_add_int64(j, n, v);
 }
 
 static inline void nvme_json_add_le32_ptr(struct json_object *j, const char *n,
                                          void *addr)
 {
-       __u32 v = read32(addr);
+       __u32 v = nvme_mmio_read32(addr);
        nvme_json_add_int(j, n, v);
 }