lib/vhost_blk: Open bdev by bdev_name first in spdk_vhost_blk_construct()

Open a bdev whose name matches bdev_name first in spdk_vhost_blk_construct()
to avoid using spdk_bdev_get_by_name() to fix the race condition
due to the time gap between spdk_bdev_get_by_name() and spdk_bdev_open_ext().
A pointer to a bdev is valid only while the bdev is opened.

Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Change-Id: I99fedd696aa44bb46c1fcc4f49ca37069f7adfef
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4502
Community-CI: Broadcom CI
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
This commit is contained in:
Shuhei Matsumoto 2020-10-01 13:47:53 +09:00 committed by Tomasz Zawadzki
parent a22d8a530f
commit 0238bb734a

View File

@ -1250,13 +1250,6 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_
int ret = 0;
spdk_vhost_lock();
bdev = spdk_bdev_get_by_name(dev_name);
if (bdev == NULL) {
SPDK_ERRLOG("%s: bdev '%s' not found\n",
name, dev_name);
ret = -ENODEV;
goto out;
}
bvdev = calloc(1, sizeof(*bvdev));
if (bvdev == NULL) {
@ -1264,6 +1257,14 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_
goto out;
}
ret = spdk_bdev_open_ext(dev_name, true, bdev_event_cb, bvdev, &bvdev->bdev_desc);
if (ret != 0) {
SPDK_ERRLOG("%s: could not open bdev '%s', error=%d\n",
name, dev_name, ret);
goto out;
}
bdev = spdk_bdev_desc_get_bdev(bvdev->bdev_desc);
vdev = &bvdev->vdev;
vdev->virtio_features = SPDK_VHOST_BLK_FEATURES_BASE;
vdev->disabled_features = SPDK_VHOST_BLK_DISABLED_FEATURES;
@ -1284,13 +1285,6 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_
vdev->virtio_features |= (1ULL << VIRTIO_BLK_F_FLUSH);
}
ret = spdk_bdev_open_ext(dev_name, true, bdev_event_cb, bvdev, &bvdev->bdev_desc);
if (ret != 0) {
SPDK_ERRLOG("%s: could not open bdev '%s', error=%d\n",
name, dev_name, ret);
goto out;
}
/*
* When starting qemu with vhost-user-blk multiqueue, the vhost device will
* be started/stopped many times, related to the queues num, as the