From 159fa94ad85984ed3e8eb63cd975d6b24f49ac12 Mon Sep 17 00:00:00 2001 From: Mao Jiang Date: Wed, 8 Sep 2021 10:05:42 +0000 Subject: [PATCH] test/nvmf/subsystem: cases for restoring ns reservation Add rkey checking to enhance nvmf_ns_reservation_restore(). Change-Id: I6d557adcba9bf81f954c118aa09452642318bc98 Signed-off-by: Mao Jiang Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/9427 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Jim Harris --- lib/nvmf/subsystem.c | 11 +++ test/unit/lib/nvmf/subsystem.c/subsystem_ut.c | 67 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/lib/nvmf/subsystem.c b/lib/nvmf/subsystem.c index 894171e88..873b639b8 100644 --- a/lib/nvmf/subsystem.c +++ b/lib/nvmf/subsystem.c @@ -2089,6 +2089,7 @@ nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservatio uint32_t i; struct spdk_nvmf_registrant *reg, *holder = NULL; struct spdk_uuid bdev_uuid, holder_uuid; + bool rkey_flag = false; SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n", ns->nsid, info->ptpl_activated, info->num_regs); @@ -2098,6 +2099,16 @@ nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservatio return 0; } + /* Check info->crkey exist or not in info->registrants[i].rkey */ + for (i = 0; i < info->num_regs; i++) { + if (info->crkey == info->registrants[i].rkey) { + rkey_flag = true; + } + } + if (!rkey_flag) { + return -EINVAL; + } + spdk_uuid_parse(&bdev_uuid, info->bdev_uuid); if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) { SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n"); diff --git a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c index 8a5465c07..d55a27fdb 100644 --- a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c +++ b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c @@ -1651,6 +1651,72 @@ test_nvmf_valid_nqn(void) CU_ASSERT(rc == false); } +static void +test_nvmf_ns_reservation_restore(void) +{ + struct spdk_nvmf_ns ns = {}; + struct spdk_nvmf_reservation_info info = {}; + struct spdk_bdev bdev = {}; + struct spdk_uuid s_uuid = {}; + struct spdk_nvmf_registrant *reg0, *reg1; + char uuid[SPDK_UUID_STRING_LEN] = {}; + int rc; + + ns.bdev = &bdev; + TAILQ_INIT(&ns.registrants); + info.ptpl_activated = true; + info.num_regs = 2; + info.rtype = SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS; + info.registrants[0].rkey = 0xb; + info.registrants[1].rkey = 0xc; + + /* Generate and prepare uuids, make sure bdev and info uuid are the same */ + spdk_uuid_generate(&s_uuid); + uuid_unparse((void *)&s_uuid, uuid); + snprintf(info.holder_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); + snprintf(info.bdev_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); + snprintf(info.registrants[0].host_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); + spdk_uuid_copy(&bdev.uuid, &s_uuid); + spdk_uuid_generate(&s_uuid); + uuid_unparse((void *)&s_uuid, uuid); + snprintf(info.registrants[1].host_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); + + /* info->rkey not exist in registrants */ + info.crkey = 0xa; + + rc = nvmf_ns_reservation_restore(&ns, &info); + CU_ASSERT(rc == -EINVAL); + + /* info->rkey exists in registrants */ + info.crkey = 0xb; + + rc = nvmf_ns_reservation_restore(&ns, &info); + CU_ASSERT(rc == 0); + CU_ASSERT(ns.crkey == 0xb); + CU_ASSERT(ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS); + CU_ASSERT(ns.ptpl_activated == true); + /* Check two registrant`s rkey */ + reg0 = TAILQ_FIRST(&ns.registrants); + reg1 = TAILQ_NEXT(reg0, link); + CU_ASSERT(ns.holder == reg0); + CU_ASSERT(reg0->rkey = 0xb); + CU_ASSERT(reg1->rkey = 0xc); + + rc = nvmf_ns_reservation_clear_all_registrants(&ns); + CU_ASSERT(rc == 2); + CU_ASSERT(TAILQ_EMPTY(&ns.registrants)); + + /* Existing bdev UUID is different with configuration */ + spdk_uuid_generate(&s_uuid); + uuid_unparse((void *)&s_uuid, uuid); + snprintf(info.bdev_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); + spdk_uuid_generate(&s_uuid); + spdk_uuid_copy(&bdev.uuid, &s_uuid); + + rc = nvmf_ns_reservation_restore(&ns, &info); + CU_ASSERT(rc == -EINVAL); +} + int main(int argc, char **argv) { CU_pSuite suite = NULL; @@ -1680,6 +1746,7 @@ int main(int argc, char **argv) CU_ADD_TEST(suite, test_spdk_nvmf_subsystem_add_host); CU_ADD_TEST(suite, test_nvmf_ns_reservation_report); CU_ADD_TEST(suite, test_nvmf_valid_nqn); + CU_ADD_TEST(suite, test_nvmf_ns_reservation_restore); allocate_threads(1); set_thread(0);