From a3ed1795cd1736ad3a37e2616ef26054468c176f Mon Sep 17 00:00:00 2001 From: Changpeng Liu Date: Fri, 18 Nov 2016 16:16:37 +0800 Subject: [PATCH] nvmf: add subsystem check on creation and cleanup for nvmf rpc Change-Id: I85826c99c450426c26870ae261a7b7c8daeba031 Signed-off-by: Changpeng Liu --- app/nvmf_tgt/conf.c | 23 ++++++++++++++--------- app/nvmf_tgt/nvmf_tgt.c | 5 +++++ include/spdk/nvmf.h | 1 + lib/nvmf/subsystem.c | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/nvmf_tgt/conf.c b/app/nvmf_tgt/conf.c index d36649ea8..1bab8c866 100644 --- a/app/nvmf_tgt/conf.c +++ b/app/nvmf_tgt/conf.c @@ -719,12 +719,12 @@ spdk_nvmf_parse_subsystem_for_rpc(const char *name, if (bdf == NULL) { SPDK_ERRLOG("Subsystem %s: missing NVMe directive\n", name); - return -1; + goto error; } if (num_devs != 0) { SPDK_ERRLOG("Subsystem %s: Namespaces not allowed for Direct mode\n", name); - return -1; + goto error; } ctx.app_subsystem = app_subsys; @@ -734,7 +734,7 @@ spdk_nvmf_parse_subsystem_for_rpc(const char *name, } else { if (spdk_pci_addr_parse(&ctx.pci_addr, bdf) < 0) { SPDK_ERRLOG("Invalid format for NVMe BDF: %s\n", bdf); - return -1; + goto error; } ctx.any = false; } @@ -746,7 +746,7 @@ spdk_nvmf_parse_subsystem_for_rpc(const char *name, if (!ctx.found) { SPDK_ERRLOG("Could not find NVMe controller at PCI address %04x:%02x:%02x.%x\n", ctx.pci_addr.domain, ctx.pci_addr.bus, ctx.pci_addr.dev, ctx.pci_addr.func); - return -1; + goto error; } } else { struct spdk_bdev *bdev; @@ -754,14 +754,14 @@ spdk_nvmf_parse_subsystem_for_rpc(const char *name, if (sn == NULL) { SPDK_ERRLOG("Subsystem %s: missing serial number\n", name); - return -1; + goto error; } if (spdk_nvmf_validate_sn(sn) != 0) { - return -1; + goto error; } if (num_devs > MAX_VIRTUAL_NAMESPACE) { - return -1; + goto error; } subsystem->dev.virt.ns_count = 0; @@ -771,11 +771,11 @@ spdk_nvmf_parse_subsystem_for_rpc(const char *name, namespace = dev_list[i]; if (!namespace) { SPDK_ERRLOG("Namespace %d: missing block device\n", i); - return -1; + goto error; } bdev = spdk_bdev_get_by_name(namespace); if (spdk_nvmf_subsystem_add_ns(subsystem, bdev)) { - return -1; + goto error; } SPDK_NOTICELOG("Attaching block device %s to subsystem %s\n", @@ -787,4 +787,9 @@ spdk_nvmf_parse_subsystem_for_rpc(const char *name, nvmf_tgt_start_subsystem(app_subsys); return 0; + +error: + spdk_nvmf_delete_subsystem(app_subsys->subsystem); + app_subsys->subsystem = NULL; + return -1; } diff --git a/app/nvmf_tgt/nvmf_tgt.c b/app/nvmf_tgt/nvmf_tgt.c index 88b742242..976bb380b 100644 --- a/app/nvmf_tgt/nvmf_tgt.c +++ b/app/nvmf_tgt/nvmf_tgt.c @@ -222,6 +222,11 @@ nvmf_tgt_create_subsystem(const char *name, enum spdk_nvmf_subtype subtype, struct spdk_nvmf_subsystem *subsystem; struct nvmf_tgt_subsystem *app_subsys; + if (spdk_nvmf_subsystem_exists(name)) { + SPDK_ERRLOG("Subsystem already exist\n"); + return NULL; + } + app_subsys = calloc(1, sizeof(*app_subsys)); if (app_subsys == NULL) { SPDK_ERRLOG("Subsystem allocation failed\n"); diff --git a/include/spdk/nvmf.h b/include/spdk/nvmf.h index 0fea9b7d5..32eda8967 100644 --- a/include/spdk/nvmf.h +++ b/include/spdk/nvmf.h @@ -168,6 +168,7 @@ void spdk_nvmf_delete_subsystem(struct spdk_nvmf_subsystem *subsystem); struct spdk_nvmf_subsystem * nvmf_find_subsystem(const char *subnqn, const char *hostnqn); +bool spdk_nvmf_subsystem_exists(const char *subnqn); int spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem, diff --git a/lib/nvmf/subsystem.c b/lib/nvmf/subsystem.c index 13905dfbc..41ea4930a 100644 --- a/lib/nvmf/subsystem.c +++ b/lib/nvmf/subsystem.c @@ -47,6 +47,24 @@ static TAILQ_HEAD(, spdk_nvmf_subsystem) g_subsystems = TAILQ_HEAD_INITIALIZER(g_subsystems); +bool +spdk_nvmf_subsystem_exists(const char *subnqn) +{ + struct spdk_nvmf_subsystem *subsystem; + + if (!subnqn) { + return false; + } + + TAILQ_FOREACH(subsystem, &g_subsystems, entries) { + if (strcmp(subnqn, subsystem->subnqn) == 0) { + return true; + } + } + + return false; +} + struct spdk_nvmf_subsystem * nvmf_find_subsystem(const char *subnqn, const char *hostnqn) {