From d42b332ae64d7b5a52bc5e52258123ce0352f3b9 Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Thu, 16 Apr 2020 10:03:00 -0400 Subject: [PATCH] cuse: refactor retrieving cuse_device to separate function This patch adds nvme_cuse_get_cuse_ctrlr_device() and nvme_cuse_get_cuse_ns_device that returns struct cuse_device of a given nvme controller or namespace. Similar iteration was used in two places so they were replaced accordingly. Next patch will add third. Signed-off-by: Tomasz Zawadzki Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/1918 (master) (cherry picked from commit 15a50180674127f7492834b311192ab021485ae2) Change-Id: I25ada843a59c632fe330263a65456d25c5ccf4cc Signed-off-by: Tomasz Zawadzki Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2155 Reviewed-by: Jim Harris Reviewed-by: Ben Walker --- lib/nvme/nvme_cuse.c | 68 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/nvme/nvme_cuse.c b/lib/nvme/nvme_cuse.c index 9e3ae0227..404c4b7f0 100644 --- a/lib/nvme/nvme_cuse.c +++ b/lib/nvme/nvme_cuse.c @@ -877,10 +877,10 @@ err2: return rv; } -static void -nvme_cuse_stop(struct spdk_nvme_ctrlr *ctrlr) +static struct cuse_device * +nvme_cuse_get_cuse_ctrlr_device(struct spdk_nvme_ctrlr *ctrlr) { - struct cuse_device *ctrlr_device; + struct cuse_device *ctrlr_device = NULL; TAILQ_FOREACH(ctrlr_device, &g_ctrlr_ctx_head, tailq) { if (ctrlr_device->ctrlr == ctrlr) { @@ -888,6 +888,35 @@ nvme_cuse_stop(struct spdk_nvme_ctrlr *ctrlr) } } + return ctrlr_device; +} + +static struct cuse_device * +nvme_cuse_get_cuse_ns_device(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) +{ + struct cuse_device *ctrlr_device = NULL; + struct cuse_device *ns_device = NULL; + + ctrlr_device = nvme_cuse_get_cuse_ctrlr_device(ctrlr); + if (!ctrlr_device) { + return NULL; + } + + TAILQ_FOREACH(ns_device, &ctrlr_device->ns_devices, tailq) { + if (ns_device->nsid == nsid) { + break; + } + } + + return ns_device; +} + +static void +nvme_cuse_stop(struct spdk_nvme_ctrlr *ctrlr) +{ + struct cuse_device *ctrlr_device; + + ctrlr_device = nvme_cuse_get_cuse_ctrlr_device(ctrlr); if (!ctrlr_device) { SPDK_ERRLOG("Cannot find associated CUSE device\n"); return; @@ -932,16 +961,7 @@ spdk_nvme_cuse_get_ctrlr_name(struct spdk_nvme_ctrlr *ctrlr) { struct cuse_device *ctrlr_device; - if (TAILQ_EMPTY(&g_ctrlr_ctx_head)) { - return NULL; - } - - TAILQ_FOREACH(ctrlr_device, &g_ctrlr_ctx_head, tailq) { - if (ctrlr_device->ctrlr == ctrlr) { - break; - } - } - + ctrlr_device = nvme_cuse_get_cuse_ctrlr_device(ctrlr); if (!ctrlr_device) { return NULL; } @@ -953,28 +973,8 @@ char * spdk_nvme_cuse_get_ns_name(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) { struct cuse_device *ns_device; - struct cuse_device *ctrlr_device; - - if (TAILQ_EMPTY(&g_ctrlr_ctx_head)) { - return NULL; - } - - TAILQ_FOREACH(ctrlr_device, &g_ctrlr_ctx_head, tailq) { - if (ctrlr_device->ctrlr == ctrlr) { - break; - } - } - - if (!ctrlr_device) { - return NULL; - } - - TAILQ_FOREACH(ns_device, &ctrlr_device->ns_devices, tailq) { - if (ns_device->nsid == nsid) { - break; - } - } + ns_device = nvme_cuse_get_cuse_ns_device(ctrlr, nsid); if (!ns_device) { return NULL; }