From 2e26faa97553566aecc67e6ca58e80016016dd95 Mon Sep 17 00:00:00 2001 From: Jacek Kalwas Date: Tue, 22 Dec 2020 10:27:45 -0500 Subject: [PATCH] nvmf: add notifications about ns to the transport It is optional to register i.e. most transports will not need to take any action. It allows additional verification of ns/bdev capabilities on transport layer before actual attachment to the subsystem. Signed-off-by: Jacek Kalwas Change-Id: I07d96b1d33c2d5433b951cb418ae1a89bf9caea5 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5666 Community-CI: Broadcom CI Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Shuhei Matsumoto Reviewed-by: Aleksey Marchuk --- include/spdk/nvmf_transport.h | 16 ++++++++++++ lib/nvmf/subsystem.c | 26 +++++++++++++++++++ .../ctrlr_discovery.c/ctrlr_discovery_ut.c | 8 ++++++ test/unit/lib/nvmf/subsystem.c/subsystem_ut.c | 8 ++++++ 4 files changed, 58 insertions(+) diff --git a/include/spdk/nvmf_transport.h b/include/spdk/nvmf_transport.h index 580c2079f..313680b04 100644 --- a/include/spdk/nvmf_transport.h +++ b/include/spdk/nvmf_transport.h @@ -255,6 +255,22 @@ struct spdk_nvmf_transport_ops { const struct spdk_nvmf_subsystem *subsystem, const struct spdk_nvme_transport_id *trid); + /** + * It is a notification that a namespace is being added to the subsystem. + * Most transports will not need to take any action here. + * + * Returns a negated errno code to block the attachment. 0 to allow. + */ + int (*subsystem_add_ns)(struct spdk_nvmf_transport *transport, + const struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *ns); + + /** + * It is a notification that a namespace has been removed from the subsystem. + * Most transports will not need to take any action here. + */ + void (*subsystem_remove_ns)(struct spdk_nvmf_transport *transport, + const struct spdk_nvmf_subsystem *subsystem, uint32_t nsid); + /** * Check for new connections on the transport. */ diff --git a/lib/nvmf/subsystem.c b/lib/nvmf/subsystem.c index 97e2ec936..316530281 100644 --- a/lib/nvmf/subsystem.c +++ b/lib/nvmf/subsystem.c @@ -1152,6 +1152,7 @@ nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns); int spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) { + struct spdk_nvmf_transport *transport; struct spdk_nvmf_ns *ns; if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || @@ -1177,6 +1178,13 @@ spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t ns spdk_bdev_close(ns->desc); free(ns); + for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport; + transport = spdk_nvmf_transport_get_next(transport)) { + if (transport->ops->subsystem_remove_ns) { + transport->ops->subsystem_remove_ns(transport, subsystem, nsid); + } + } + nvmf_subsystem_ns_changed(subsystem, nsid); return 0; @@ -1343,6 +1351,7 @@ spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size, const char *ptpl_file) { + struct spdk_nvmf_transport *transport; struct spdk_nvmf_ns_opts opts; struct spdk_nvmf_ns *ns; struct spdk_nvmf_reservation_info info = {0}; @@ -1443,6 +1452,23 @@ spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char ns->ptpl_file = strdup(ptpl_file); } + for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport; + transport = spdk_nvmf_transport_get_next(transport)) { + if (transport->ops->subsystem_add_ns) { + rc = transport->ops->subsystem_add_ns(transport, subsystem, ns); + if (rc) { + SPDK_ERRLOG("Namespace attachment is not allowed by %s transport\n", transport->ops->name); + free(ns->ptpl_file); + nvmf_ns_reservation_clear_all_registrants(ns); + subsystem->ns[opts.nsid - 1] = NULL; + spdk_bdev_module_release_bdev(ns->bdev); + spdk_bdev_close(ns->desc); + free(ns); + return 0; + } + } + } + SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n", spdk_nvmf_subsystem_get_nqn(subsystem), bdev_name, diff --git a/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c b/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c index c916f531b..496c07fa6 100644 --- a/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c +++ b/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c @@ -53,6 +53,14 @@ DEFINE_STUB(spdk_nvmf_transport_stop_listen, (struct spdk_nvmf_transport *transport, const struct spdk_nvme_transport_id *trid), 0); +DEFINE_STUB(spdk_nvmf_transport_get_first, + struct spdk_nvmf_transport *, + (struct spdk_nvmf_tgt *tgt), NULL); + +DEFINE_STUB(spdk_nvmf_transport_get_next, + struct spdk_nvmf_transport *, + (struct spdk_nvmf_transport *transport), NULL); + DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc)); DEFINE_STUB(nvmf_ctrlr_async_event_discovery_log_change_notice, diff --git a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c index 69410cc0d..ac242b42b 100644 --- a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c +++ b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c @@ -76,6 +76,14 @@ DEFINE_STUB(nvmf_transport_find_listener, (struct spdk_nvmf_transport *transport, const struct spdk_nvme_transport_id *trid), NULL); +DEFINE_STUB(spdk_nvmf_transport_get_first, + struct spdk_nvmf_transport *, + (struct spdk_nvmf_tgt *tgt), NULL); + +DEFINE_STUB(spdk_nvmf_transport_get_next, + struct spdk_nvmf_transport *, + (struct spdk_nvmf_transport *transport), NULL); + DEFINE_STUB(spdk_nvmf_request_complete, int, (struct spdk_nvmf_request *req), 0);