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 <niklas.cassel@wdc.com>
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 <sys_sgci@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Niklas Cassel 2021-03-18 17:33:52 +00:00 committed by Tomasz Zawadzki
parent 346c43edd3
commit f6bbec8ba9

View File

@ -38,6 +38,7 @@
#include "spdk/event.h" #include "spdk/event.h"
#include "spdk/log.h" #include "spdk/log.h"
#include "spdk/string.h" #include "spdk/string.h"
#include "spdk/bdev_zone.h"
static char *g_bdev_name = "Malloc0"; 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); 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(). * 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"); 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); hello_write(hello_context);
} }