bdev/nvme: Add nvme_bdev_attach/detach_bdev_to/from_ctrlr functions

Some NVMe modes will be creating bdevs by their own.
For such case they have to have a way to add and remove
bdevs to/from controller.

Signed-off-by: Maciej Szwed <maciej.szwed@intel.com>
Change-Id: Ia188ad43695689358569cfd230b6bc39c15efce2
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/469980
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Konrad Sztyber <konrad.sztyber@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
Maciej Szwed 2019-10-01 10:18:44 +02:00 committed by Tomasz Zawadzki
parent b5b6a1ce81
commit d179541e0f
3 changed files with 32 additions and 14 deletions

View File

@ -251,20 +251,12 @@ static int
bdev_nvme_destruct(void *ctx)
{
struct nvme_bdev *nvme_disk = ctx;
struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = nvme_disk->nvme_bdev_ctrlr;
pthread_mutex_lock(&g_bdev_nvme_mutex);
nvme_bdev_ctrlr->ref--;
TAILQ_REMOVE(&nvme_disk->nvme_ns->bdevs, nvme_disk, tailq);
nvme_bdev_detach_bdev_from_ns(nvme_disk);
free(nvme_disk->disk.name);
free(nvme_disk);
if (nvme_bdev_ctrlr->ref == 0 && nvme_bdev_ctrlr->destruct) {
pthread_mutex_unlock(&g_bdev_nvme_mutex);
nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr);
return 0;
}
pthread_mutex_unlock(&g_bdev_nvme_mutex);
return 0;
}
@ -829,11 +821,9 @@ nvme_ctrlr_populate_standard_namespace(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr,
bdev->nvme_bdev_ctrlr = nvme_bdev_ctrlr;
nvme_ns->ns = ns;
bdev->nvme_ns = nvme_ns;
nvme_bdev_ctrlr->ref++;
bdev->disk.name = spdk_sprintf_alloc("%sn%d", nvme_bdev_ctrlr->name, spdk_nvme_ns_get_id(ns));
if (!bdev->disk.name) {
nvme_bdev_ctrlr->ref--;
free(bdev);
return -ENOMEM;
}
@ -870,12 +860,11 @@ nvme_ctrlr_populate_standard_namespace(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr,
rc = spdk_bdev_register(&bdev->disk);
if (rc) {
free(bdev->disk.name);
nvme_bdev_ctrlr->ref--;
free(bdev);
return rc;
}
TAILQ_INSERT_TAIL(&nvme_ns->bdevs, bdev, tailq);
nvme_bdev_attach_bdev_to_ns(nvme_ns, bdev);
return 0;
}

View File

@ -145,3 +145,30 @@ nvme_bdev_ctrlr_destruct(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr)
spdk_io_device_unregister(nvme_bdev_ctrlr, nvme_bdev_unregister_cb);
}
void
nvme_bdev_attach_bdev_to_ns(struct nvme_bdev_ns *nvme_ns, struct nvme_bdev *nvme_disk)
{
nvme_ns->ctrlr->ref++;
TAILQ_INSERT_TAIL(&nvme_ns->bdevs, nvme_disk, tailq);
}
void
nvme_bdev_detach_bdev_from_ns(struct nvme_bdev *nvme_disk)
{
struct nvme_bdev_ctrlr *ctrlr = nvme_disk->nvme_ns->ctrlr;
pthread_mutex_lock(&g_bdev_nvme_mutex);
ctrlr->ref--;
TAILQ_REMOVE(&nvme_disk->nvme_ns->bdevs, nvme_disk, tailq);
if (ctrlr->ref == 0 && ctrlr->destruct) {
pthread_mutex_unlock(&g_bdev_nvme_mutex);
nvme_bdev_ctrlr_destruct(ctrlr);
return;
}
pthread_mutex_unlock(&g_bdev_nvme_mutex);
}

View File

@ -121,5 +121,7 @@ void nvme_bdev_dump_trid_json(struct spdk_nvme_transport_id *trid,
struct spdk_json_write_ctx *w);
void nvme_bdev_ctrlr_destruct(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr);
void nvme_bdev_attach_bdev_to_ns(struct nvme_bdev_ns *nvme_ns, struct nvme_bdev *nvme_disk);
void nvme_bdev_detach_bdev_from_ns(struct nvme_bdev *nvme_disk);
#endif /* SPDK_COMMON_BDEV_NVME_H */