bdev_ut: test read-only bdev claim

While not documented as such, spdk_bdev_module_claim_bdev() has always
allowed a bdev that is opened read-only to remain read-only when
claimed. This occurs when NULL is passed in place of an spdk_bdev_desc.

This change updates the function's documentation to match the
implementation and adds a unit test to ensure the current behavior
remains.

Signed-off-by: Mike Gerdts <mgerdts@nvidia.com>
Change-Id: Ief26de60e4408bfe1aa60b7a4e1d8adf273470b6
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11267
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Paul Luse <paul.e.luse@intel.com>
This commit is contained in:
Mike Gerdts 2022-01-02 08:26:21 -06:00 committed by Tomasz Zawadzki
parent 79ba049a6f
commit 9f9c7161c9
2 changed files with 57 additions and 5 deletions

View File

@ -3,7 +3,7 @@
*
* Copyright (c) Intel Corporation.
* All rights reserved.
* Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -180,12 +180,13 @@ struct spdk_bdev_module {
};
/**
* Called by a bdev module to lay exclusive write claim to a bdev.
* Called by a bdev module to lay exclusive claim to a bdev.
*
* Also upgrades that bdev's descriptor to have write access.
* Also upgrades that bdev's descriptor to have write access if desc
* is not NULL.
*
* \param bdev Block device to be claimed.
* \param desc Descriptor for the above block device.
* \param desc Descriptor for the above block device or NULL.
* \param module Bdev module attempting to claim bdev.
*
* \return 0 on success

View File

@ -3,7 +3,7 @@
*
* Copyright (c) Intel Corporation. All rights reserved.
* Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
* Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -685,6 +685,56 @@ open_write_test(void)
free_bdev(bdev[3]);
}
static void
claim_test(void)
{
struct spdk_bdev *bdev;
struct spdk_bdev_desc *desc, *open_desc;
int rc;
uint32_t count;
/*
* A vbdev that uses a read-only bdev may need it to remain read-only.
* To do so, it opens the bdev read-only, then claims it without
* passing a spdk_bdev_desc.
*/
bdev = allocate_bdev("bdev0");
rc = spdk_bdev_open_ext("bdev0", false, bdev_ut_event_cb, NULL, &desc);
CU_ASSERT(rc == 0);
CU_ASSERT(desc->write == false);
rc = spdk_bdev_module_claim_bdev(bdev, NULL, &bdev_ut_if);
CU_ASSERT(rc == 0);
CU_ASSERT(bdev->internal.claim_module == &bdev_ut_if);
/* There should be only one open descriptor and it should still be ro */
count = 0;
TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
CU_ASSERT(open_desc == desc);
CU_ASSERT(!open_desc->write);
count++;
}
CU_ASSERT(count == 1);
/* A read-only bdev is upgraded to read-write if desc is passed. */
spdk_bdev_module_release_bdev(bdev);
rc = spdk_bdev_module_claim_bdev(bdev, desc, &bdev_ut_if);
CU_ASSERT(rc == 0);
CU_ASSERT(bdev->internal.claim_module == &bdev_ut_if);
/* There should be only one open descriptor and it should be rw */
count = 0;
TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
CU_ASSERT(open_desc == desc);
CU_ASSERT(open_desc->write);
count++;
}
CU_ASSERT(count == 1);
spdk_bdev_close(desc);
free_bdev(bdev);
}
static void
bytes_to_blocks_test(void)
{
@ -4915,6 +4965,7 @@ main(int argc, char **argv)
CU_ADD_TEST(suite, num_blocks_test);
CU_ADD_TEST(suite, io_valid_test);
CU_ADD_TEST(suite, open_write_test);
CU_ADD_TEST(suite, claim_test);
CU_ADD_TEST(suite, alias_add_del_test);
CU_ADD_TEST(suite, get_device_stat_test);
CU_ADD_TEST(suite, bdev_io_types_test);