diff --git a/include/spdk/bdev_zone.h b/include/spdk/bdev_zone.h index cbfef5bba6..a67945ebfd 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 fa41f979c8..676cb2af5d 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 7f3e648bc3..aec215d589 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 a5c053ca6b..01ed505450 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();