bdev/nvme: Add UUID and EUI64 comparison to check if two namespaces are identical

Following Linux NVMe host, add UUID and EUI64 comparison to
bdev_nvme_compare_ns().

Besides, previously the return value of memcmp() had been used as
the return value of bdev_nvme_compare_ns() and this was wrong.
Fix it in this patch together.

Add unit test cases for bdev_nvme_compare_ns().

Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Change-Id: I069ab53e77741d6348b847d51e84a9338e2f3787
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/7755
Reviewed-by: Paul Luse <paul.e.luse@intel.com>
Reviewed-by: Ziye Yang <ziye.yang@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: <dongx.yi@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
Shuhei Matsumoto 2021-05-17 06:47:51 +09:00 committed by Tomasz Zawadzki
parent eabe783cc4
commit 8c2738a8fe
2 changed files with 65 additions and 5 deletions

View File

@ -1422,11 +1422,16 @@ static bool
bdev_nvme_compare_ns(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
{
const struct spdk_nvme_ns_data *nsdata1, *nsdata2;
const struct spdk_uuid *uuid1, *uuid2;
nsdata1 = spdk_nvme_ns_get_data(ns1);
nsdata2 = spdk_nvme_ns_get_data(ns2);
uuid1 = spdk_nvme_ns_get_uuid(ns1);
uuid2 = spdk_nvme_ns_get_uuid(ns2);
return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid));
return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid)) == 0 &&
nsdata1->eui64 == nsdata2->eui64 &&
uuid1 != NULL && uuid2 != NULL && spdk_uuid_compare(uuid1, uuid2) == 0;
}
static void
@ -2168,7 +2173,7 @@ bdev_nvme_compare_namespaces(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr,
new_ns = spdk_nvme_ctrlr_get_ns(new_ctrlr, nsid);
assert(new_ns != NULL);
if (bdev_nvme_compare_ns(nvme_ns->ns, new_ns) != 0) {
if (!bdev_nvme_compare_ns(nvme_ns->ns, new_ns)) {
return -EINVAL;
}
}

View File

@ -127,9 +127,6 @@ DEFINE_STUB(spdk_nvme_ns_get_dealloc_logical_block_read_value,
DEFINE_STUB(spdk_nvme_ns_get_optimal_io_boundary, uint32_t, (struct spdk_nvme_ns *ns), 0);
DEFINE_STUB(spdk_nvme_ns_get_uuid, const struct spdk_uuid *,
(const struct spdk_nvme_ns *ns), NULL);
DEFINE_STUB(spdk_nvme_ns_get_csi, enum spdk_nvme_csi,
(const struct spdk_nvme_ns *ns), 0);
@ -231,6 +228,7 @@ struct spdk_nvme_ns {
struct spdk_nvme_ctrlr *ctrlr;
uint32_t id;
bool is_active;
struct spdk_uuid uuid;
};
struct spdk_nvme_qpair {
@ -742,6 +740,13 @@ spdk_nvme_ns_get_num_sectors(struct spdk_nvme_ns *ns)
{
return _nvme_ns_get_data(ns)->nsze;
}
const struct spdk_uuid *
spdk_nvme_ns_get_uuid(const struct spdk_nvme_ns *ns)
{
return &ns->uuid;
}
int
spdk_nvme_ns_cmd_read_with_md(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, void *buffer,
void *metadata, uint64_t lba, uint32_t lba_count,
@ -2251,6 +2256,55 @@ test_bdev_unregister(void)
CU_ASSERT(nvme_bdev_ctrlr_get_by_name("nvme0") == NULL);
}
static void
test_compare_ns(void)
{
struct spdk_nvme_ns_data nsdata1 = {}, nsdata2 = {};
struct spdk_nvme_ctrlr ctrlr1 = { .nsdata = &nsdata1, }, ctrlr2 = { .nsdata = &nsdata2, };
struct spdk_nvme_ns ns1 = { .id = 1, .ctrlr = &ctrlr1, }, ns2 = { .id = 1, .ctrlr = &ctrlr2, };
/* No IDs are defined. */
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == true);
/* Only EUI64 are defined and not matched. */
nsdata1.eui64 = 0xABCDEF0123456789;
nsdata2.eui64 = 0xBBCDEF0123456789;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == false);
/* Only EUI64 are defined and matched. */
nsdata2.eui64 = 0xABCDEF0123456789;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == true);
/* Only NGUID are defined and not matched. */
nsdata1.eui64 = 0x0;
nsdata2.eui64 = 0x0;
nsdata1.nguid[0] = 0x12;
nsdata2.nguid[0] = 0x10;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == false);
/* Only NGUID are defined and matched. */
nsdata2.nguid[0] = 0x12;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == true);
/* Only UUID are defined and not matched. */
nsdata1.nguid[0] = 0x0;
nsdata2.nguid[0] = 0x0;
ns1.uuid.u.raw[0] = 0xAA;
ns2.uuid.u.raw[0] = 0xAB;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == false);
/* Only UUID are defined and matched. */
ns1.uuid.u.raw[0] = 0xAB;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == true);
/* All EUI64, NGUID, and UUID are defined and matched. */
nsdata1.eui64 = 0x123456789ABCDEF;
nsdata2.eui64 = 0x123456789ABCDEF;
nsdata1.nguid[15] = 0x34;
nsdata2.nguid[15] = 0x34;
CU_ASSERT(bdev_nvme_compare_ns(&ns1, &ns2) == true);
}
static void
init_accel(void)
{
@ -2288,6 +2342,7 @@ main(int argc, const char **argv)
CU_ADD_TEST(suite, test_abort);
CU_ADD_TEST(suite, test_get_io_qpair);
CU_ADD_TEST(suite, test_bdev_unregister);
CU_ADD_TEST(suite, test_compare_ns);
CU_basic_set_mode(CU_BRM_VERBOSE);