nvmet: refactor configfs transport type handling
authorChristoph Hellwig <hch@lst.de>
Tue, 20 Mar 2018 19:41:34 +0000 (20:41 +0100)
committerJens Axboe <axboe@kernel.dk>
Mon, 26 Mar 2018 14:53:43 +0000 (08:53 -0600)
Have a common table of mappings from numerical transport ids to names, and
zero the transport specific area in common code in nvmet_addr_trtype_store.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <keith.busch@intel.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/nvme/target/configfs.c

index 7780a7210acb98a1c04d2c4d044acc0d4e158e40..ad9ff27234b52dabc617dacdf4dbb4c01d98f385 100644 (file)
 static const struct config_item_type nvmet_host_type;
 static const struct config_item_type nvmet_subsys_type;
 
+static const struct nvmet_transport_name {
+       u8              type;
+       const char      *name;
+} nvmet_transport_names[] = {
+       { NVMF_TRTYPE_RDMA,     "rdma" },
+       { NVMF_TRTYPE_FC,       "fc" },
+       { NVMF_TRTYPE_LOOP,     "loop" },
+};
+
 /*
  * nvmet_port Generic ConfigFS definitions.
  * Used in any place in the ConfigFS tree that refers to an address.
@@ -208,43 +217,30 @@ CONFIGFS_ATTR(nvmet_, addr_trsvcid);
 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
                char *page)
 {
-       switch (to_nvmet_port(item)->disc_addr.trtype) {
-       case NVMF_TRTYPE_RDMA:
-               return sprintf(page, "rdma\n");
-       case NVMF_TRTYPE_LOOP:
-               return sprintf(page, "loop\n");
-       case NVMF_TRTYPE_FC:
-               return sprintf(page, "fc\n");
-       default:
-               return sprintf(page, "\n");
+       struct nvmet_port *port = to_nvmet_port(item);
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
+               if (port->disc_addr.trtype != nvmet_transport_names[i].type)
+                       continue;
+               return sprintf(page, "%s\n", nvmet_transport_names[i].name);
        }
+
+       return sprintf(page, "\n");
 }
 
 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
 {
-       port->disc_addr.trtype = NVMF_TRTYPE_RDMA;
-       memset(&port->disc_addr.tsas.rdma, 0, NVMF_TSAS_SIZE);
        port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
        port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
        port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
 }
 
-static void nvmet_port_init_tsas_loop(struct nvmet_port *port)
-{
-       port->disc_addr.trtype = NVMF_TRTYPE_LOOP;
-       memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
-}
-
-static void nvmet_port_init_tsas_fc(struct nvmet_port *port)
-{
-       port->disc_addr.trtype = NVMF_TRTYPE_FC;
-       memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
-}
-
 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
                const char *page, size_t count)
 {
        struct nvmet_port *port = to_nvmet_port(item);
+       int i;
 
        if (port->enabled) {
                pr_err("Cannot modify address while enabled\n");
@@ -252,17 +248,18 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item,
                return -EACCES;
        }
 
-       if (sysfs_streq(page, "rdma")) {
-               nvmet_port_init_tsas_rdma(port);
-       } else if (sysfs_streq(page, "loop")) {
-               nvmet_port_init_tsas_loop(port);
-       } else if (sysfs_streq(page, "fc")) {
-               nvmet_port_init_tsas_fc(port);
-       } else {
-               pr_err("Invalid value '%s' for trtype\n", page);
-               return -EINVAL;
+       for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
+               if (sysfs_streq(page, nvmet_transport_names[i].name))
+                       goto found;
        }
 
+       pr_err("Invalid value '%s' for trtype\n", page);
+       return -EINVAL;
+found:
+       memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
+       port->disc_addr.trtype = nvmet_transport_names[i].type;
+       if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
+               nvmet_port_init_tsas_rdma(port);
        return count;
 }