From 0859db6b69a85ec3ca734db9d4a436396f93ab37 Mon Sep 17 00:00:00 2001 From: Niklas Cassel Date: Fri, 16 Apr 2021 11:22:07 +0000 Subject: [PATCH] bdev/zone: add support for get num zones There are three modules implementing the bdev-zone API: bdev_nvme, bdev_ocssd, and vbdev_zone_block. For all three modules, the number of zones can be calculated using: block_count / zone_size. To avoid this calculation being performed everywhere, create a helper function in bdev_zone.h, together with the other zone APIs, such that a user can easily get the number of zones. Signed-off-by: Niklas Cassel Change-Id: I2967b15a604ab8bf4420588e7510b9820762f925 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/7451 Reviewed-by: Konrad Sztyber Reviewed-by: Jim Harris Reviewed-by: Ben Walker Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins --- include/spdk/bdev_zone.h | 8 ++++++++ lib/bdev/bdev_zone.c | 6 ++++++ lib/bdev/spdk_bdev.map | 1 + test/unit/lib/bdev/bdev_zone.c/bdev_zone_ut.c | 14 ++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/include/spdk/bdev_zone.h b/include/spdk/bdev_zone.h index cbfef5bba6d..a67945ebfd7 100644 --- a/include/spdk/bdev_zone.h +++ b/include/spdk/bdev_zone.h @@ -84,6 +84,14 @@ struct spdk_bdev_zone_info { */ uint64_t spdk_bdev_get_zone_size(const struct spdk_bdev *bdev); +/** + * Get the number of zones for the given device. + * + * \param bdev Block device to query. + * \return The number of zones. + */ +uint64_t spdk_bdev_get_num_zones(const struct spdk_bdev *bdev); + /** * Get device maximum zone append data transfer size in logical blocks. * diff --git a/lib/bdev/bdev_zone.c b/lib/bdev/bdev_zone.c index fa41f979c82..676cb2af5da 100644 --- a/lib/bdev/bdev_zone.c +++ b/lib/bdev/bdev_zone.c @@ -44,6 +44,12 @@ spdk_bdev_get_zone_size(const struct spdk_bdev *bdev) return bdev->zone_size; } +uint64_t +spdk_bdev_get_num_zones(const struct spdk_bdev *bdev) +{ + return bdev->zone_size ? bdev->blockcnt / bdev->zone_size : 0; +} + uint32_t spdk_bdev_get_max_zone_append_size(const struct spdk_bdev *bdev) { diff --git a/lib/bdev/spdk_bdev.map b/lib/bdev/spdk_bdev.map index 7f3e648bc3c..aec215d589f 100644 --- a/lib/bdev/spdk_bdev.map +++ b/lib/bdev/spdk_bdev.map @@ -143,6 +143,7 @@ # Public functions in bdev_zone.h spdk_bdev_get_zone_size; + spdk_bdev_get_num_zones; spdk_bdev_get_max_zone_append_size; spdk_bdev_get_max_open_zones; spdk_bdev_get_max_active_zones; diff --git a/test/unit/lib/bdev/bdev_zone.c/bdev_zone_ut.c b/test/unit/lib/bdev/bdev_zone.c/bdev_zone_ut.c index a5c053ca6bd..01ed5054509 100644 --- a/test/unit/lib/bdev/bdev_zone.c/bdev_zone_ut.c +++ b/test/unit/lib/bdev/bdev_zone.c/bdev_zone_ut.c @@ -254,6 +254,19 @@ test_get_zone_size(void) CU_ASSERT(get_zone_size == 1024 * 4096); } +static void +test_get_num_zones(void) +{ + struct spdk_bdev bdev = {}; + uint64_t get_num_zones; + + bdev.blockcnt = 1024 * 1024 * 1024; + bdev.zone_size = 1024 * 4096; + + get_num_zones = spdk_bdev_get_num_zones(&bdev); + CU_ASSERT(get_num_zones == 256); +} + static void test_get_max_zone_append_size(void) { @@ -318,6 +331,7 @@ static void test_zone_get_operation(void) { test_get_zone_size(); + test_get_num_zones(); test_get_max_zone_append_size(); test_get_max_open_zones(); test_get_max_active_zones();