Commit 7e764179 authored by Chaitanya Kulkarni's avatar Chaitanya Kulkarni Committed by Jens Axboe

nvmet: use type-name map for address family

Right now nvmet_addr_adrfam_[store|show]() uses switch and if else
ladder for address family to string and reverse mapping which also
repeats the strings in show and store function.

With addition of generic nvmet_type_name_map structure we can now get rid
of the switch and if else ladder and string duplication.

Also, we add a newline in before found label in nvmet_addr_trtype_store()
which keeps goto label code consistent with
nvmet_allowed_hosts_drop_link(), nvmet_port_subsys_drop_link() and
nvmet_ana_group_ana_state_store().
Signed-off-by: default avatarChaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 45e2f3c2
...@@ -32,31 +32,36 @@ static struct nvmet_type_name_map nvmet_transport[] = { ...@@ -32,31 +32,36 @@ static struct nvmet_type_name_map nvmet_transport[] = {
{ NVMF_TRTYPE_LOOP, "loop" }, { NVMF_TRTYPE_LOOP, "loop" },
}; };
static const struct nvmet_type_name_map nvmet_addr_family[] = {
{ NVMF_ADDR_FAMILY_PCI, "pcie" },
{ NVMF_ADDR_FAMILY_IP4, "ipv4" },
{ NVMF_ADDR_FAMILY_IP6, "ipv6" },
{ NVMF_ADDR_FAMILY_IB, "ib" },
{ NVMF_ADDR_FAMILY_FC, "fc" },
};
/* /*
* nvmet_port Generic ConfigFS definitions. * nvmet_port Generic ConfigFS definitions.
* Used in any place in the ConfigFS tree that refers to an address. * Used in any place in the ConfigFS tree that refers to an address.
*/ */
static ssize_t nvmet_addr_adrfam_show(struct config_item *item, static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
char *page)
{ {
switch (to_nvmet_port(item)->disc_addr.adrfam) { u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
case NVMF_ADDR_FAMILY_IP4: int i;
return sprintf(page, "ipv4\n");
case NVMF_ADDR_FAMILY_IP6: for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
return sprintf(page, "ipv6\n"); if (nvmet_addr_family[i].type == adrfam)
case NVMF_ADDR_FAMILY_IB: return sprintf(page, "%s\n", nvmet_addr_family[i].name);
return sprintf(page, "ib\n");
case NVMF_ADDR_FAMILY_FC:
return sprintf(page, "fc\n");
default:
return sprintf(page, "\n");
} }
return sprintf(page, "\n");
} }
static ssize_t nvmet_addr_adrfam_store(struct config_item *item, static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
const char *page, size_t count) const char *page, size_t count)
{ {
struct nvmet_port *port = to_nvmet_port(item); struct nvmet_port *port = to_nvmet_port(item);
int i;
if (port->enabled) { if (port->enabled) {
pr_err("Cannot modify address while enabled\n"); pr_err("Cannot modify address while enabled\n");
...@@ -64,19 +69,16 @@ static ssize_t nvmet_addr_adrfam_store(struct config_item *item, ...@@ -64,19 +69,16 @@ static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
return -EACCES; return -EACCES;
} }
if (sysfs_streq(page, "ipv4")) { for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP4; if (sysfs_streq(page, nvmet_addr_family[i].name))
} else if (sysfs_streq(page, "ipv6")) { goto found;
port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP6; }
} else if (sysfs_streq(page, "ib")) {
port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IB;
} else if (sysfs_streq(page, "fc")) {
port->disc_addr.adrfam = NVMF_ADDR_FAMILY_FC;
} else {
pr_err("Invalid value '%s' for adrfam\n", page); pr_err("Invalid value '%s' for adrfam\n", page);
return -EINVAL; return -EINVAL;
}
found:
port->disc_addr.adrfam = i;
return count; return count;
} }
...@@ -290,6 +292,7 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item, ...@@ -290,6 +292,7 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item,
pr_err("Invalid value '%s' for trtype\n", page); pr_err("Invalid value '%s' for trtype\n", page);
return -EINVAL; return -EINVAL;
found: found:
memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE); memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
port->disc_addr.trtype = nvmet_transport[i].type; port->disc_addr.trtype = nvmet_transport[i].type;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment