From 4dc86079b7ee5756392e7616a5bb55ac387bb1ed Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Tue, 8 Dec 2020 03:46:51 +0900 Subject: [PATCH] bdev/nvme: Factor out each RPC config from config_json() Factor out each RPC configuration from bdev_nvme_config_json() and bdev_ocssd_namespace_config_json() into a helper function, respectively. These changes will make us easier to introduce subsystem into the NVMe bdev module. Signed-off-by: Shuhei Matsumoto Change-Id: I297f3fb47893d45463e742834c58d2b1213a4b01 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5468 Community-CI: Broadcom CI Tested-by: SPDK CI Jenkins Reviewed-by: Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk --- module/bdev/nvme/bdev_nvme.c | 86 ++++++++++++++++++++++------------- module/bdev/nvme/bdev_ocssd.c | 55 ++++++++++++---------- 2 files changed, 85 insertions(+), 56 deletions(-) diff --git a/module/bdev/nvme/bdev_nvme.c b/module/bdev/nvme/bdev_nvme.c index 966ac354d..2d91ed490 100644 --- a/module/bdev/nvme/bdev_nvme.c +++ b/module/bdev/nvme/bdev_nvme.c @@ -2769,13 +2769,10 @@ nvme_namespace_config_json(struct spdk_json_write_ctx *w, struct nvme_bdev_ns *n g_config_json_namespace_fn[nvme_ns->type](w, nvme_ns); } -static int -bdev_nvme_config_json(struct spdk_json_write_ctx *w) +static void +bdev_nvme_opts_config_json(struct spdk_json_write_ctx *w) { - struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; - struct spdk_nvme_transport_id *trid; - const char *action; - uint32_t nsid; + const char *action; if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET) { action = "reset"; @@ -2805,26 +2802,59 @@ bdev_nvme_config_json(struct spdk_json_write_ctx *w) spdk_json_write_object_end(w); spdk_json_write_object_end(w); +} + +static void +nvme_bdev_ctrlr_config_json(struct spdk_json_write_ctx *w, + struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) +{ + struct spdk_nvme_transport_id *trid; + + trid = nvme_bdev_ctrlr->connected_trid; + + spdk_json_write_object_begin(w); + + spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller"); + + spdk_json_write_named_object_begin(w, "params"); + spdk_json_write_named_string(w, "name", nvme_bdev_ctrlr->name); + nvme_bdev_dump_trid_json(trid, w); + spdk_json_write_named_bool(w, "prchk_reftag", + (nvme_bdev_ctrlr->prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0); + spdk_json_write_named_bool(w, "prchk_guard", + (nvme_bdev_ctrlr->prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0); + + spdk_json_write_object_end(w); + + spdk_json_write_object_end(w); +} + +static void +bdev_nvme_hotplug_config_json(struct spdk_json_write_ctx *w) +{ + spdk_json_write_object_begin(w); + spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug"); + + spdk_json_write_named_object_begin(w, "params"); + spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us); + spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled); + spdk_json_write_object_end(w); + + spdk_json_write_object_end(w); +} + +static int +bdev_nvme_config_json(struct spdk_json_write_ctx *w) +{ + struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; + uint32_t nsid; + + bdev_nvme_opts_config_json(w); pthread_mutex_lock(&g_bdev_nvme_mutex); + TAILQ_FOREACH(nvme_bdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) { - trid = nvme_bdev_ctrlr->connected_trid; - - spdk_json_write_object_begin(w); - - spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller"); - - spdk_json_write_named_object_begin(w, "params"); - spdk_json_write_named_string(w, "name", nvme_bdev_ctrlr->name); - nvme_bdev_dump_trid_json(trid, w); - spdk_json_write_named_bool(w, "prchk_reftag", - (nvme_bdev_ctrlr->prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0); - spdk_json_write_named_bool(w, "prchk_guard", - (nvme_bdev_ctrlr->prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0); - - spdk_json_write_object_end(w); - - spdk_json_write_object_end(w); + nvme_bdev_ctrlr_config_json(w, nvme_bdev_ctrlr); for (nsid = 0; nsid < nvme_bdev_ctrlr->num_ns; ++nsid) { if (!nvme_bdev_ctrlr->namespaces[nsid]->populated) { @@ -2838,15 +2868,7 @@ bdev_nvme_config_json(struct spdk_json_write_ctx *w) /* Dump as last parameter to give all NVMe bdevs chance to be constructed * before enabling hotplug poller. */ - spdk_json_write_object_begin(w); - spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug"); - - spdk_json_write_named_object_begin(w, "params"); - spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us); - spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled); - spdk_json_write_object_end(w); - - spdk_json_write_object_end(w); + bdev_nvme_hotplug_config_json(w); pthread_mutex_unlock(&g_bdev_nvme_mutex); return 0; diff --git a/module/bdev/nvme/bdev_ocssd.c b/module/bdev/nvme/bdev_ocssd.c index 9543dec5c..4122d8bd9 100644 --- a/module/bdev/nvme/bdev_ocssd.c +++ b/module/bdev/nvme/bdev_ocssd.c @@ -129,37 +129,44 @@ bdev_ocssd_config_json(struct spdk_json_write_ctx *w) return 0; } -void -bdev_ocssd_namespace_config_json(struct spdk_json_write_ctx *w, struct nvme_bdev_ns *nvme_ns) +static void +ocssd_bdev_config_json(struct spdk_json_write_ctx *w, struct nvme_bdev *nvme_bdev) { struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; - struct nvme_bdev *nvme_bdev; struct ocssd_bdev *ocssd_bdev; char range_buf[128]; int rc; + nvme_bdev_ctrlr = nvme_bdev->nvme_ns->ctrlr; + ocssd_bdev = SPDK_CONTAINEROF(nvme_bdev, struct ocssd_bdev, nvme_bdev); + + rc = snprintf(range_buf, sizeof(range_buf), "%"PRIu64"-%"PRIu64, + ocssd_bdev->range.begin, ocssd_bdev->range.end); + if (rc < 0 || rc >= (int)sizeof(range_buf)) { + SPDK_ERRLOG("Failed to convert parallel unit range\n"); + return; + } + + spdk_json_write_object_begin(w); + spdk_json_write_named_string(w, "method", "bdev_ocssd_create"); + + spdk_json_write_named_object_begin(w, "params"); + spdk_json_write_named_string(w, "ctrlr_name", nvme_bdev_ctrlr->name); + spdk_json_write_named_string(w, "bdev_name", nvme_bdev->disk.name); + spdk_json_write_named_uint32(w, "nsid", nvme_bdev->nvme_ns->id); + spdk_json_write_named_string(w, "range", range_buf); + spdk_json_write_object_end(w); + + spdk_json_write_object_end(w); +} + +void +bdev_ocssd_namespace_config_json(struct spdk_json_write_ctx *w, struct nvme_bdev_ns *nvme_ns) +{ + struct nvme_bdev *nvme_bdev; + TAILQ_FOREACH(nvme_bdev, &nvme_ns->bdevs, tailq) { - nvme_bdev_ctrlr = nvme_bdev->nvme_ns->ctrlr; - ocssd_bdev = SPDK_CONTAINEROF(nvme_bdev, struct ocssd_bdev, nvme_bdev); - - rc = snprintf(range_buf, sizeof(range_buf), "%"PRIu64"-%"PRIu64, - ocssd_bdev->range.begin, ocssd_bdev->range.end); - if (rc < 0 || rc >= (int)sizeof(range_buf)) { - SPDK_ERRLOG("Failed to convert parallel unit range\n"); - continue; - } - - spdk_json_write_object_begin(w); - spdk_json_write_named_string(w, "method", "bdev_ocssd_create"); - - spdk_json_write_named_object_begin(w, "params"); - spdk_json_write_named_string(w, "ctrlr_name", nvme_bdev_ctrlr->name); - spdk_json_write_named_string(w, "bdev_name", nvme_bdev->disk.name); - spdk_json_write_named_uint32(w, "nsid", nvme_bdev->nvme_ns->id); - spdk_json_write_named_string(w, "range", range_buf); - spdk_json_write_object_end(w); - - spdk_json_write_object_end(w); + ocssd_bdev_config_json(w, nvme_bdev); } }