From 6deac3e66093631f438e8920227f9a7b427ae35b Mon Sep 17 00:00:00 2001 From: wuzhouhui Date: Tue, 14 Aug 2018 16:39:27 +0800 Subject: [PATCH] bdev/lvol: using spdk_bdev_alias_del_all() to delete all alias on destroy So we don't need to allocate memory (maybe failed) just for free other memory. Change-Id: I2c83f6acc2aa6ed79455bff90f952a2e70b44d59 Signed-off-by: wuzhouhui Reviewed-on: https://review.gerrithub.io/422203 Chandler-Test-Pool: SPDK Automated Test System Reviewed-by: Jim Harris Reviewed-by: Ben Walker Reviewed-by: Shuhei Matsumoto Reviewed-by: Changpeng Liu Tested-by: SPDK CI Jenkins --- CHANGELOG.md | 4 ++++ include/spdk/bdev_module.h | 7 +++++++ lib/bdev/bdev.c | 12 ++++++++++++ lib/bdev/lvol/vbdev_lvol.c | 9 +-------- test/unit/lib/bdev/bdev.c/bdev_ut.c | 19 ++++++++++++++++++- .../lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c | 12 ++++++++++++ 6 files changed, 54 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18cef14ebe..2520453fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ each of the SPDK static ones. Although the production of the shared libs conforms with conventional version naming practices, such naming does not at this time confer any SPDK ABI compatibility claims. +### bdev + +spdk_bdev_alias_del_all() was added to delete all alias from block device. + ## v18.07: ### bdev diff --git a/include/spdk/bdev_module.h b/include/spdk/bdev_module.h index 2a15e65518..ece0d07c80 100644 --- a/include/spdk/bdev_module.h +++ b/include/spdk/bdev_module.h @@ -590,6 +590,13 @@ int spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias); */ int spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias); +/** + * Removes all alias from block device alias list. + * + * \param bdev Block device to operate. + */ +void spdk_bdev_alias_del_all(struct spdk_bdev *bdev); + /** * Get pointer to block device aliases list. * diff --git a/lib/bdev/bdev.c b/lib/bdev/bdev.c index 39d336cca2..daf5808d66 100644 --- a/lib/bdev/bdev.c +++ b/lib/bdev/bdev.c @@ -1570,6 +1570,18 @@ spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias) return -ENOENT; } +void +spdk_bdev_alias_del_all(struct spdk_bdev *bdev) +{ + struct spdk_bdev_alias *p, *tmp; + + TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) { + TAILQ_REMOVE(&bdev->aliases, p, tailq); + free(p->alias); + free(p); + } +} + struct spdk_io_channel * spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc) { diff --git a/lib/bdev/lvol/vbdev_lvol.c b/lib/bdev/lvol/vbdev_lvol.c index ec682a1d60..dbe86f4556 100644 --- a/lib/bdev/lvol/vbdev_lvol.c +++ b/lib/bdev/lvol/vbdev_lvol.c @@ -541,17 +541,10 @@ static int vbdev_lvol_unregister(void *ctx) { struct spdk_lvol *lvol = ctx; - char *alias; assert(lvol != NULL); - alias = spdk_sprintf_alloc("%s/%s", lvol->lvol_store->name, lvol->name); - if (alias != NULL) { - spdk_bdev_alias_del(lvol->bdev, alias); - free(alias); - } else { - SPDK_ERRLOG("Cannot alloc memory for alias\n"); - } + spdk_bdev_alias_del_all(lvol->bdev); spdk_lvol_close(lvol, _vbdev_lvol_unregister_cb, lvol->bdev); /* return 1 to indicate we have an operation that must finish asynchronously before the diff --git a/test/unit/lib/bdev/bdev.c/bdev_ut.c b/test/unit/lib/bdev/bdev.c/bdev_ut.c index 422fb25d30..6c190f566a 100644 --- a/test/unit/lib/bdev/bdev.c/bdev_ut.c +++ b/test/unit/lib/bdev/bdev.c/bdev_ut.c @@ -502,7 +502,7 @@ io_valid_test(void) static void alias_add_del_test(void) { - struct spdk_bdev *bdev[2]; + struct spdk_bdev *bdev[3]; int rc; /* Creating and registering bdevs */ @@ -512,6 +512,9 @@ alias_add_del_test(void) bdev[1] = allocate_bdev("bdev1"); SPDK_CU_ASSERT_FATAL(bdev[1] != 0); + bdev[2] = allocate_bdev("bdev2"); + SPDK_CU_ASSERT_FATAL(bdev[2] != 0); + /* * Trying adding an alias identical to name. * Alias is identical to name, so it can not be added to aliases list @@ -558,12 +561,26 @@ alias_add_del_test(void) rc = spdk_bdev_alias_del(bdev[0], bdev[0]->name); CU_ASSERT(rc != 0); + /* Trying to del all alias from empty alias list */ + spdk_bdev_alias_del_all(bdev[2]); + CU_ASSERT(TAILQ_EMPTY(&bdev[2]->aliases)); + + /* Trying to del all alias from non-empty alias list */ + rc = spdk_bdev_alias_add(bdev[2], "alias0"); + CU_ASSERT(rc == 0); + rc = spdk_bdev_alias_add(bdev[2], "alias1"); + CU_ASSERT(rc == 0); + spdk_bdev_alias_del_all(bdev[2]); + CU_ASSERT(TAILQ_EMPTY(&bdev[2]->aliases)); + /* Unregister and free bdevs */ spdk_bdev_unregister(bdev[0], NULL, NULL); spdk_bdev_unregister(bdev[1], NULL, NULL); + spdk_bdev_unregister(bdev[2], NULL, NULL); free(bdev[0]); free(bdev[1]); + free(bdev[2]); } static void diff --git a/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c b/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c index 2d41380eab..484fed841a 100644 --- a/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c +++ b/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c @@ -103,6 +103,18 @@ spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias) return -ENOENT; } +void +spdk_bdev_alias_del_all(struct spdk_bdev *bdev) +{ + struct spdk_bdev_alias *p, *tmp; + + TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) { + TAILQ_REMOVE(&bdev->aliases, p, tailq); + free(p->alias); + free(p); + } +} + void spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno) {