From f6bbec8ba9cd64f959eb8590b1f89ca70a5a5da2 Mon Sep 17 00:00:00 2001 From: Niklas Cassel Date: Thu, 18 Mar 2021 17:33:52 +0000 Subject: [PATCH] bdev/hello_world: for zoned bdevs, reset the zone before write If the bdev is a zoned bdev, reset zone 0 (containing offset 0), before doing the "Hello world!" write to offset 0. This is done to ensure that the write pointer for the first zone is at offset 0. If we don't do a zone reset before doing the write, the write would fail if there already were data written to the first zone. (E.g. if the user ran the hello_bdev example twice.) Signed-off-by: Niklas Cassel Change-Id: I52b6fc9cf6e86fef9aeb19482eafd1f857ba1478 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/6943 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Jim Harris --- examples/bdev/hello_world/hello_bdev.c | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/examples/bdev/hello_world/hello_bdev.c b/examples/bdev/hello_world/hello_bdev.c index 0738fe4b36..9bfa6ee789 100644 --- a/examples/bdev/hello_world/hello_bdev.c +++ b/examples/bdev/hello_world/hello_bdev.c @@ -38,6 +38,7 @@ #include "spdk/event.h" #include "spdk/log.h" #include "spdk/string.h" +#include "spdk/bdev_zone.h" static char *g_bdev_name = "Malloc0"; @@ -190,6 +191,50 @@ hello_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); } +static void +reset_zone_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) +{ + struct hello_context_t *hello_context = cb_arg; + + /* Complete the I/O */ + spdk_bdev_free_io(bdev_io); + + if (!success) { + SPDK_ERRLOG("bdev io reset zone error: %d\n", EIO); + spdk_put_io_channel(hello_context->bdev_io_channel); + spdk_bdev_close(hello_context->bdev_desc); + spdk_app_stop(-1); + return; + } + + hello_write(hello_context); +} + +static void +hello_reset_zone(void *arg) +{ + struct hello_context_t *hello_context = arg; + int rc = 0; + + rc = spdk_bdev_zone_management(hello_context->bdev_desc, hello_context->bdev_io_channel, + 0, SPDK_BDEV_ZONE_RESET, reset_zone_complete, hello_context); + + if (rc == -ENOMEM) { + SPDK_NOTICELOG("Queueing io\n"); + /* In case we cannot perform I/O now, queue I/O */ + hello_context->bdev_io_wait.bdev = hello_context->bdev; + hello_context->bdev_io_wait.cb_fn = hello_reset_zone; + hello_context->bdev_io_wait.cb_arg = hello_context; + spdk_bdev_queue_io_wait(hello_context->bdev, hello_context->bdev_io_channel, + &hello_context->bdev_io_wait); + } else if (rc) { + SPDK_ERRLOG("%s error while resetting zone: %d\n", spdk_strerror(-rc), rc); + spdk_put_io_channel(hello_context->bdev_io_channel); + spdk_bdev_close(hello_context->bdev_desc); + spdk_app_stop(-1); + } +} + /* * Our initial event that kicks off everything from main(). */ @@ -249,6 +294,12 @@ hello_start(void *arg1) } snprintf(hello_context->buff, blk_size, "%s", "Hello World!\n"); + if (spdk_bdev_is_zoned(hello_context->bdev)) { + hello_reset_zone(hello_context); + /* If bdev is zoned, the callback, reset_zone_complete, will call hello_write() */ + return; + } + hello_write(hello_context); }