From f26b1fca5f14388764f648c3fe1e2f79d1aed98d Mon Sep 17 00:00:00 2001 From: Piotr Pelplinski Date: Tue, 16 Oct 2018 09:27:30 +0200 Subject: [PATCH] hello_bdev: add spdk_bdev_queue_io_wait to hello_bdev example Signed-off-by: Piotr Pelplinski Change-Id: I358c7bd567166ffaf26b526bd69d030485611fe5 Reviewed-on: https://review.gerrithub.io/429855 Chandler-Test-Pool: SPDK Automated Test System Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Jim Harris --- examples/bdev/hello_world/hello_bdev.c | 75 +++++++++++++++++++------- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/examples/bdev/hello_world/hello_bdev.c b/examples/bdev/hello_world/hello_bdev.c index 8581a3c73..cc65b0514 100644 --- a/examples/bdev/hello_world/hello_bdev.c +++ b/examples/bdev/hello_world/hello_bdev.c @@ -52,6 +52,7 @@ struct hello_context_t { struct spdk_io_channel *bdev_io_channel; char *buff; char *bdev_name; + struct spdk_bdev_io_wait_entry bdev_io_wait; }; /* @@ -97,6 +98,33 @@ read_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) spdk_app_stop(success ? 0 : -1); } +static void +hello_read(void *arg) +{ + struct hello_context_t *hello_context = arg; + int rc = 0; + uint32_t length = spdk_bdev_get_block_size(hello_context->bdev); + + SPDK_NOTICELOG("Reading io\n"); + rc = spdk_bdev_read(hello_context->bdev_desc, hello_context->bdev_io_channel, + hello_context->buff, 0, length, read_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_read; + 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 reading from bdev: %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); + } +} + /* * Callback function for write io completion. */ @@ -104,8 +132,7 @@ static void write_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) { struct hello_context_t *hello_context = cb_arg; - int rc; - uint32_t blk_size; + uint32_t length; /* Complete the I/O */ spdk_bdev_free_io(bdev_io); @@ -121,19 +148,36 @@ write_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) } /* Zero the buffer so that we can use it for reading */ - blk_size = spdk_bdev_get_block_size(hello_context->bdev); - memset(hello_context->buff, 0, blk_size); + length = spdk_bdev_get_block_size(hello_context->bdev); + memset(hello_context->buff, 0, length); - SPDK_NOTICELOG("Reading io\n"); - rc = spdk_bdev_read(hello_context->bdev_desc, hello_context->bdev_io_channel, - hello_context->buff, 0, blk_size, read_complete, hello_context); + hello_read(hello_context); +} - if (rc) { - SPDK_ERRLOG("%s error while reading from bdev: %d\n", spdk_strerror(-rc), rc); +static void +hello_write(void *arg) +{ + struct hello_context_t *hello_context = arg; + int rc = 0; + uint32_t length = spdk_bdev_get_block_size(hello_context->bdev); + + SPDK_NOTICELOG("Writing to the bdev\n"); + rc = spdk_bdev_write(hello_context->bdev_desc, hello_context->bdev_io_channel, + hello_context->buff, 0, length, write_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_write; + 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 writing to bdev: %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); - return; } } @@ -200,16 +244,7 @@ hello_start(void *arg1, void *arg2) } snprintf(hello_context->buff, blk_size, "%s", "Hello World!\n"); - SPDK_NOTICELOG("Writing to the bdev\n"); - rc = spdk_bdev_write(hello_context->bdev_desc, hello_context->bdev_io_channel, - hello_context->buff, 0, blk_size, write_complete, hello_context); - if (rc) { - SPDK_ERRLOG("%s error while writing to bdev: %d\n", spdk_strerror(-rc), rc); - spdk_bdev_close(hello_context->bdev_desc); - spdk_put_io_channel(hello_context->bdev_io_channel); - spdk_app_stop(-1); - return; - } + hello_write(hello_context); } int