From 28092d2feed22aae37a51ad521732de4fb0ee6d8 Mon Sep 17 00:00:00 2001 From: Dennis Maisenbacher Date: Thu, 15 Dec 2022 14:34:08 +0100 Subject: [PATCH] nvmf: Find a NS for an identify cmd through a helper function Refactoring to avoid code duplication in the following commits. Signed-off-by: Dennis Maisenbacher Change-Id: I5a597a02c810cfa1fad6dc397d012cf6a3f189ca Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16043 Community-CI: Mellanox Build Bot Reviewed-by: Ben Walker Reviewed-by: Jim Harris Tested-by: SPDK CI Jenkins --- lib/nvmf/ctrlr.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/nvmf/ctrlr.c b/lib/nvmf/ctrlr.c index d74c9da75..a650ef750 100644 --- a/lib/nvmf/ctrlr.c +++ b/lib/nvmf/ctrlr.c @@ -2591,6 +2591,34 @@ invalid_log_page: return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; } +static struct spdk_nvmf_ns * +_nvmf_subsystem_get_ns_safe(struct spdk_nvmf_subsystem *subsystem, + uint32_t nsid, + struct spdk_nvme_cpl *rsp) +{ + struct spdk_nvmf_ns *ns; + if (nsid == 0 || nsid > subsystem->max_nsid) { + SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", nsid); + rsp->status.sct = SPDK_NVME_SCT_GENERIC; + rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; + return NULL; + } + + ns = _nvmf_subsystem_get_ns(subsystem, nsid); + if (ns == NULL || ns->bdev == NULL) { + /* + * Inactive namespaces should return a zero filled data structure. + * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(), + * so we can just return early here. + */ + SPDK_DEBUGLOG(nvmf, "Identify Namespace for inactive NSID %u\n", nsid); + rsp->status.sct = SPDK_NVME_SCT_GENERIC; + rsp->status.sc = SPDK_NVME_SC_SUCCESS; + return NULL; + } + return ns; +} + int spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd, @@ -2602,23 +2630,8 @@ spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr, uint32_t max_num_blocks; enum spdk_nvme_ana_state ana_state; - if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) { - SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid); - rsp->status.sct = SPDK_NVME_SCT_GENERIC; - rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; - return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; - } - - ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid); - if (ns == NULL || ns->bdev == NULL) { - /* - * Inactive namespaces should return a zero filled data structure. - * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(), - * so we can just return early here. - */ - SPDK_DEBUGLOG(nvmf, "Identify Namespace for inactive NSID %u\n", cmd->nsid); - rsp->status.sct = SPDK_NVME_SCT_GENERIC; - rsp->status.sc = SPDK_NVME_SC_SUCCESS; + ns = _nvmf_subsystem_get_ns_safe(subsystem, cmd->nsid, rsp); + if (ns == NULL) { return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; }