bdev: bdev_name_add() checks if the name exists in the global name tree

If the specified name already exists in the global bdev name tree,
RB_INSERT() returns a pointer to it. Hence we do not have to call
bdev_get_by_name() when using bdev_name_add().

Hence update bdev_name_add() to return -EEXIST if RB_INSERT() returns
a non-NULL pointer, and then remove the bdev_get_by_name() calls.

Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Change-Id: I2d4554ef7e5286270417def64b638b803eecfca2
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/8573
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
Shuhei Matsumoto 2021-06-30 14:06:14 +09:00 committed by Tomasz Zawadzki
parent 36b5a69bb0
commit 20ba4a0dbe

View File

@ -3245,9 +3245,15 @@ bdev_channel_destroy(void *io_device, void *ctx_buf)
bdev_channel_destroy_resource(ch);
}
/*
* If the name already exists in the global bdev name tree, RB_INSERT() returns a pointer
* to it. Hence we do not have to call bdev_get_by_name() when using this function.
*/
static int
bdev_name_add(struct spdk_bdev_name *bdev_name, struct spdk_bdev *bdev, const char *name)
{
struct spdk_bdev_name *tmp;
bdev_name->name = strdup(name);
if (bdev_name->name == NULL) {
SPDK_ERRLOG("Unable to allocate bdev name\n");
@ -3255,7 +3261,14 @@ bdev_name_add(struct spdk_bdev_name *bdev_name, struct spdk_bdev *bdev, const ch
}
bdev_name->bdev = bdev;
RB_INSERT(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
tmp = RB_INSERT(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
if (tmp != NULL) {
SPDK_ERRLOG("Bdev name %s already exists\n", name);
free(bdev_name->name);
return -EEXIST;
}
return 0;
}
@ -3277,11 +3290,6 @@ spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
return -EINVAL;
}
if (bdev_get_by_name(alias)) {
SPDK_ERRLOG("Bdev name/alias: %s already exists\n", alias);
return -EEXIST;
}
tmp = calloc(1, sizeof(*tmp));
if (tmp == NULL) {
SPDK_ERRLOG("Unable to allocate alias\n");
@ -5569,11 +5577,6 @@ bdev_register(struct spdk_bdev *bdev)
return -EINVAL;
}
if (bdev_get_by_name(bdev->name)) {
SPDK_ERRLOG("Bdev name:%s already exists\n", bdev->name);
return -EEXIST;
}
/* Users often register their own I/O devices using the bdev name. In
* order to avoid conflicts, prepend bdev_. */
bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name);