From 8afb3d003798965618a8f018bac1644204f4b6e2 Mon Sep 17 00:00:00 2001
From: GangCao <gang.cao@intel.com>
Date: Thu, 29 Sep 2022 03:03:22 -0400
Subject: [PATCH] lib/bdev: return error when failing to get resource

To fix issue: 2719

Change-Id: I983ef607fad154608fff9bb9355645968caf0c5a
Signed-off-by: GangCao <gang.cao@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14746
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@nvidia.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
---
 lib/bdev/bdev.c | 55 +++++++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/lib/bdev/bdev.c b/lib/bdev/bdev.c
index 122f3668a..1642289ff 100644
--- a/lib/bdev/bdev.c
+++ b/lib/bdev/bdev.c
@@ -1382,6 +1382,30 @@ spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w)
 	spdk_json_write_array_end(w);
 }
 
+static void
+bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf)
+{
+	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
+	struct spdk_bdev_io *bdev_io;
+
+	if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) {
+		SPDK_ERRLOG("Pending I/O list wasn't empty on mgmt channel free\n");
+	}
+
+	if (!TAILQ_EMPTY(&ch->shared_resources)) {
+		SPDK_ERRLOG("Module channel list wasn't empty on mgmt channel free\n");
+	}
+
+	while (!STAILQ_EMPTY(&ch->per_thread_cache)) {
+		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
+		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
+		ch->per_thread_cache_count--;
+		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
+	}
+
+	assert(ch->per_thread_cache_count == 0);
+}
+
 static int
 bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
 {
@@ -1399,7 +1423,12 @@ bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
 	ch->per_thread_cache_count = 0;
 	for (i = 0; i < ch->bdev_io_cache_size; i++) {
 		bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
-		assert(bdev_io != NULL);
+		if (bdev_io == NULL) {
+			SPDK_ERRLOG("You need to increase bdev_io_pool_size using bdev_set_options RPC.\n");
+			assert(false);
+			bdev_mgmt_channel_destroy(io_device, ctx_buf);
+			return -1;
+		}
 		ch->per_thread_cache_count++;
 		STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
 	}
@@ -1410,30 +1439,6 @@ bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
 	return 0;
 }
 
-static void
-bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf)
-{
-	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
-	struct spdk_bdev_io *bdev_io;
-
-	if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) {
-		SPDK_ERRLOG("Pending I/O list wasn't empty on mgmt channel free\n");
-	}
-
-	if (!TAILQ_EMPTY(&ch->shared_resources)) {
-		SPDK_ERRLOG("Module channel list wasn't empty on mgmt channel free\n");
-	}
-
-	while (!STAILQ_EMPTY(&ch->per_thread_cache)) {
-		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
-		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
-		ch->per_thread_cache_count--;
-		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
-	}
-
-	assert(ch->per_thread_cache_count == 0);
-}
-
 static void
 bdev_init_complete(int rc)
 {
-- 
2.23.0

