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 <tomasz.zawadzki@intel.com>
Change-Id: I25ada843a59c632fe330263a65456d25c5ccf4cc
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/1918
Community-CI: Mellanox Build Bot
Community-CI: Broadcom CI
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Tomasz Zawadzki 2020-04-16 10:03:00 -04:00 committed by Jim Harris
parent d8db56228f
commit 15a5018067

View File

@ -871,10 +871,10 @@ err2:
return rv; return rv;
} }
static void static struct cuse_device *
nvme_cuse_stop(struct spdk_nvme_ctrlr *ctrlr) 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) { TAILQ_FOREACH(ctrlr_device, &g_ctrlr_ctx_head, tailq) {
if (ctrlr_device->ctrlr == ctrlr) { if (ctrlr_device->ctrlr == ctrlr) {
@ -882,6 +882,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) { if (!ctrlr_device) {
SPDK_ERRLOG("Cannot find associated CUSE device\n"); SPDK_ERRLOG("Cannot find associated CUSE device\n");
return; return;
@ -926,16 +955,7 @@ spdk_nvme_cuse_get_ctrlr_name(struct spdk_nvme_ctrlr *ctrlr)
{ {
struct cuse_device *ctrlr_device; struct cuse_device *ctrlr_device;
if (TAILQ_EMPTY(&g_ctrlr_ctx_head)) { ctrlr_device = nvme_cuse_get_cuse_ctrlr_device(ctrlr);
return NULL;
}
TAILQ_FOREACH(ctrlr_device, &g_ctrlr_ctx_head, tailq) {
if (ctrlr_device->ctrlr == ctrlr) {
break;
}
}
if (!ctrlr_device) { if (!ctrlr_device) {
return NULL; return NULL;
} }
@ -947,28 +967,8 @@ char *
spdk_nvme_cuse_get_ns_name(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) spdk_nvme_cuse_get_ns_name(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid)
{ {
struct cuse_device *ns_device; 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) { if (!ns_device) {
return NULL; return NULL;
} }