From c0c333e2ed4533ffde6086df87534bea4ab6759c Mon Sep 17 00:00:00 2001 From: yupeng Date: Tue, 8 Nov 2022 05:07:02 +0000 Subject: [PATCH] bdev: provide all available bdevs when loop bdevs The bdev hot remove might be an async process. The bdev_open will return an error during the hot remove process. If someone invoke the bdev_get_bdevs API when a bdev is in the middle of a hot remove process, the spdk_for_each_bdev function will stop its loop when a bdev_open return an error. Thus the bdev_get_bdevs will only return partual bdevs or even return an empty list if the hot remove bdev is the first bdev in the loop. When spdk_for_each_bdev and spdk_for_each_bdev_leaf loop for each bdevs, if a bdev returns an error, we skip that bdev instead of stop the whole loop. Signed-off-by: Peng Yu Change-Id: Ib35b817e23e47569fc5762a883b4ff8e322ae173 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15322 Tested-by: SPDK CI Jenkins Community-CI: Mellanox Build Bot Reviewed-by: Shuhei Matsumoto Reviewed-by: Mike Gerdts Reviewed-by: Xiaodong Liu Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk --- lib/bdev/bdev.c | 12 ++++++++++++ test/unit/lib/bdev/bdev.c/bdev_ut.c | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/bdev/bdev.c b/lib/bdev/bdev.c index 06fc93262..1a4aa589b 100644 --- a/lib/bdev/bdev.c +++ b/lib/bdev/bdev.c @@ -6997,6 +6997,12 @@ spdk_for_each_bdev(void *ctx, spdk_for_each_bdev_fn fn) rc = bdev_open(bdev, false, desc); if (rc != 0) { bdev_desc_free(desc); + if (rc == -ENODEV) { + /* Ignore the error and move to the next bdev. */ + rc = 0; + bdev = spdk_bdev_next(bdev); + continue; + } break; } pthread_mutex_unlock(&g_bdev_mgr.mutex); @@ -7035,6 +7041,12 @@ spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn) rc = bdev_open(bdev, false, desc); if (rc != 0) { bdev_desc_free(desc); + if (rc == -ENODEV) { + /* Ignore the error and move to the next bdev. */ + rc = 0; + bdev = spdk_bdev_next_leaf(bdev); + continue; + } break; } pthread_mutex_unlock(&g_bdev_mgr.mutex); diff --git a/test/unit/lib/bdev/bdev.c/bdev_ut.c b/test/unit/lib/bdev/bdev.c/bdev_ut.c index 952df403a..c546d81da 100644 --- a/test/unit/lib/bdev/bdev.c/bdev_ut.c +++ b/test/unit/lib/bdev/bdev.c/bdev_ut.c @@ -5939,6 +5939,7 @@ for_each_bdev_test(void) int rc, count; bdev[0] = allocate_bdev("bdev0"); + bdev[0]->internal.status = SPDK_BDEV_STATUS_REMOVING; bdev[1] = allocate_bdev("bdev1"); rc = spdk_bdev_module_claim_bdev(bdev[1], NULL, &bdev_ut_if); @@ -5963,13 +5964,14 @@ for_each_bdev_test(void) count = 0; rc = spdk_for_each_bdev(&count, count_bdevs); CU_ASSERT(rc == 0); - CU_ASSERT(count == 8); + CU_ASSERT(count == 7); count = 0; rc = spdk_for_each_bdev_leaf(&count, count_bdevs); CU_ASSERT(rc == 0); - CU_ASSERT(count == 5); + CU_ASSERT(count == 4); + bdev[0]->internal.status = SPDK_BDEV_STATUS_READY; free_bdev(bdev[0]); free_bdev(bdev[1]); free_bdev(bdev[2]);