bdev/nvme: add a intermediate function to wrapper create/get block devices

When users call the RPC to add a NVMe controller, block NVMe driver
will create controller and block devices based on Namespaces, so
wrapper the two actions together with a intermediate function,
this will make the code looks more clean for the coming patch.

Change-Id: Iddc072042eb2df90662c42e04427307ccf5d5633
Signed-off-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/445658
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: wuzhouhui <wuzhouhui@kingsoft.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
This commit is contained in:
Changpeng Liu 2019-03-05 01:13:03 -05:00 committed by Ben Walker
parent 33668b2254
commit 8ca0fbcec0

View File

@ -1124,6 +1124,56 @@ spdk_bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, voi
return 0; return 0;
} }
static int
bdev_nvme_create_and_get_bdev_names(struct spdk_nvme_ctrlr *ctrlr,
const char *base_name,
const char **names, size_t *count,
const struct spdk_nvme_transport_id *trid,
uint32_t prchk_flags)
{
struct nvme_bdev_ctrlr *nvme_bdev_ctrlr;
struct nvme_bdev *nvme_bdev;
uint32_t i, nsid;
size_t j;
if (create_ctrlr(ctrlr, base_name, trid, prchk_flags)) {
SPDK_ERRLOG("Failed to create new device\n");
return -1;
}
nvme_bdev_ctrlr = nvme_bdev_ctrlr_get(trid);
if (!nvme_bdev_ctrlr) {
SPDK_ERRLOG("Failed to find new NVMe controller\n");
return -1;
}
/*
* Report the new bdevs that were created in this call.
* There can be more than one bdev per NVMe controller since one bdev is created per namespace.
*/
j = 0;
for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) {
nsid = i + 1;
nvme_bdev = &nvme_bdev_ctrlr->bdevs[nsid - 1];
if (!nvme_bdev->active) {
continue;
}
assert(nvme_bdev->id == nsid);
if (j < *count) {
names[j] = nvme_bdev->disk.name;
j++;
} else {
SPDK_ERRLOG("Maximum number of namespaces supported per NVMe controller is %zu. Unable to return all names of created bdevs\n",
*count);
return -1;
}
}
*count = j;
return 0;
}
int int
spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid, spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid,
struct spdk_nvme_host_id *hostid, struct spdk_nvme_host_id *hostid,
@ -1134,10 +1184,6 @@ spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid,
{ {
struct spdk_nvme_ctrlr_opts opts; struct spdk_nvme_ctrlr_opts opts;
struct spdk_nvme_ctrlr *ctrlr; struct spdk_nvme_ctrlr *ctrlr;
struct nvme_bdev_ctrlr *nvme_bdev_ctrlr;
struct nvme_bdev *nvme_bdev;
uint32_t i, nsid;
size_t j;
struct nvme_probe_skip_entry *entry, *tmp; struct nvme_probe_skip_entry *entry, *tmp;
if (nvme_bdev_ctrlr_get(trid) != NULL) { if (nvme_bdev_ctrlr_get(trid) != NULL) {
@ -1180,42 +1226,8 @@ spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid,
return -1; return -1;
} }
if (create_ctrlr(ctrlr, base_name, trid, prchk_flags)) { return bdev_nvme_create_and_get_bdev_names(ctrlr, base_name, names,
SPDK_ERRLOG("Failed to create new device\n"); count, trid, prchk_flags);
return -1;
}
nvme_bdev_ctrlr = nvme_bdev_ctrlr_get(trid);
if (!nvme_bdev_ctrlr) {
SPDK_ERRLOG("Failed to find new NVMe controller\n");
return -1;
}
/*
* Report the new bdevs that were created in this call.
* There can be more than one bdev per NVMe controller since one bdev is created per namespace.
*/
j = 0;
for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) {
nsid = i + 1;
nvme_bdev = &nvme_bdev_ctrlr->bdevs[nsid - 1];
if (!nvme_bdev->active) {
continue;
}
assert(nvme_bdev->id == nsid);
if (j < *count) {
names[j] = nvme_bdev->disk.name;
j++;
} else {
SPDK_ERRLOG("Maximum number of namespaces supported per NVMe controller is %zu. Unable to return all names of created bdevs\n",
*count);
return -1;
}
}
*count = j;
return 0;
} }
int int