diff --git a/lib/nvme/nvme_ctrlr.c b/lib/nvme/nvme_ctrlr.c index 4088e8d6ba..7cb70c28fe 100644 --- a/lib/nvme/nvme_ctrlr.c +++ b/lib/nvme/nvme_ctrlr.c @@ -2138,6 +2138,23 @@ nvme_active_ns_ctx_destroy(struct nvme_active_ns_ctx *ctx) free(ctx); } +static int +nvme_ctrlr_destruct_namespace(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) +{ + struct spdk_nvme_ns *ns; + + assert(ctrlr != NULL); + + if (nsid < 1 || nsid > ctrlr->num_ns) { + return -EINVAL; + } + + ns = &ctrlr->ns[nsid - 1]; + nvme_ns_destruct(ns); + + return 0; +} + static void nvme_ctrlr_identify_active_ns_swap(struct spdk_nvme_ctrlr *ctrlr, uint32_t **new_ns_list, size_t max_entries) @@ -2848,8 +2865,8 @@ nvme_ctrlr_destruct_namespaces(struct spdk_nvme_ctrlr *ctrlr) if (ctrlr->ns) { uint32_t i, num_ns = ctrlr->num_ns; - for (i = 0; i < num_ns; i++) { - nvme_ns_destruct(&ctrlr->ns[i]); + for (i = 1; i <= num_ns; i++) { + nvme_ctrlr_destruct_namespace(ctrlr, i); } spdk_free(ctrlr->ns); @@ -2890,7 +2907,7 @@ nvme_ctrlr_update_namespaces(struct spdk_nvme_ctrlr *ctrlr) if (nsdata->ncap && !ns_is_active) { NVME_CTRLR_DEBUGLOG(ctrlr, "Namespace %u was removed\n", nsid); - nvme_ns_destruct(ns); + nvme_ctrlr_destruct_namespace(ctrlr, nsid); } } } @@ -4495,7 +4512,6 @@ spdk_nvme_ctrlr_detach_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid, { struct nvme_completion_poll_status *status; int res; - struct spdk_nvme_ns *ns; if (nsid == 0) { return -EINVAL; @@ -4527,12 +4543,7 @@ spdk_nvme_ctrlr_detach_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid, return res; } - ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid); - assert(ns != NULL); - /* Inactive NS */ - nvme_ns_destruct(ns); - - return 0; + return nvme_ctrlr_destruct_namespace(ctrlr, nsid); } uint32_t @@ -4584,7 +4595,6 @@ spdk_nvme_ctrlr_delete_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) { struct nvme_completion_poll_status *status; int res; - struct spdk_nvme_ns *ns; if (nsid == 0) { return -EINVAL; @@ -4615,11 +4625,7 @@ spdk_nvme_ctrlr_delete_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) return res; } - ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid); - assert(ns != NULL); - nvme_ns_destruct(ns); - - return 0; + return nvme_ctrlr_destruct_namespace(ctrlr, nsid); } int