1488570ebSJim Harris /* SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse * Copyright (C) 2018 Intel Corporation.
38b75f85fSJohn Kariuki * All rights reserved.
48b75f85fSJohn Kariuki */
58b75f85fSJohn Kariuki
68b75f85fSJohn Kariuki #include "spdk/stdinc.h"
7a83f91c2SBen Walker #include "spdk/thread.h"
88b75f85fSJohn Kariuki #include "spdk/bdev.h"
98b75f85fSJohn Kariuki #include "spdk/env.h"
108b75f85fSJohn Kariuki #include "spdk/event.h"
118b75f85fSJohn Kariuki #include "spdk/log.h"
128b75f85fSJohn Kariuki #include "spdk/string.h"
13f6bbec8bSNiklas Cassel #include "spdk/bdev_zone.h"
148b75f85fSJohn Kariuki
158b75f85fSJohn Kariuki static char *g_bdev_name = "Malloc0";
168b75f85fSJohn Kariuki
178b75f85fSJohn Kariuki /*
188b75f85fSJohn Kariuki * We'll use this struct to gather housekeeping hello_context to pass between
198b75f85fSJohn Kariuki * our events and callbacks.
208b75f85fSJohn Kariuki */
218b75f85fSJohn Kariuki struct hello_context_t {
228b75f85fSJohn Kariuki struct spdk_bdev *bdev;
238b75f85fSJohn Kariuki struct spdk_bdev_desc *bdev_desc;
248b75f85fSJohn Kariuki struct spdk_io_channel *bdev_io_channel;
258b75f85fSJohn Kariuki char *buff;
2665e08d12SArtur Paszkiewicz uint32_t buff_size;
278b75f85fSJohn Kariuki char *bdev_name;
28f26b1fcaSPiotr Pelplinski struct spdk_bdev_io_wait_entry bdev_io_wait;
298b75f85fSJohn Kariuki };
308b75f85fSJohn Kariuki
318b75f85fSJohn Kariuki /*
328b75f85fSJohn Kariuki * Usage function for printing parameters that are specific to this application
338b75f85fSJohn Kariuki */
348b75f85fSJohn Kariuki static void
hello_bdev_usage(void)358b75f85fSJohn Kariuki hello_bdev_usage(void)
368b75f85fSJohn Kariuki {
37c2dc15a0SDariusz Stojaczyk printf(" -b <bdev> name of the bdev to use\n");
388b75f85fSJohn Kariuki }
398b75f85fSJohn Kariuki
408b75f85fSJohn Kariuki /*
418b75f85fSJohn Kariuki * This function is called to parse the parameters that are specific to this application
428b75f85fSJohn Kariuki */
438dd1cd21SBen Walker static int
hello_bdev_parse_arg(int ch,char * arg)448dd1cd21SBen Walker hello_bdev_parse_arg(int ch, char *arg)
458b75f85fSJohn Kariuki {
468b75f85fSJohn Kariuki switch (ch) {
478b75f85fSJohn Kariuki case 'b':
488b75f85fSJohn Kariuki g_bdev_name = arg;
498b75f85fSJohn Kariuki break;
5026979c50SChunyang Hui default:
5126979c50SChunyang Hui return -EINVAL;
528b75f85fSJohn Kariuki }
5301e5610dSChunyang Hui return 0;
548b75f85fSJohn Kariuki }
558b75f85fSJohn Kariuki
568b75f85fSJohn Kariuki /*
578b75f85fSJohn Kariuki * Callback function for read io completion.
588b75f85fSJohn Kariuki */
598b75f85fSJohn Kariuki static void
read_complete(struct spdk_bdev_io * bdev_io,bool success,void * cb_arg)608b75f85fSJohn Kariuki read_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
618b75f85fSJohn Kariuki {
628b75f85fSJohn Kariuki struct hello_context_t *hello_context = cb_arg;
638b75f85fSJohn Kariuki
648b75f85fSJohn Kariuki if (success) {
658b75f85fSJohn Kariuki SPDK_NOTICELOG("Read string from bdev : %s\n", hello_context->buff);
668b75f85fSJohn Kariuki } else {
678b75f85fSJohn Kariuki SPDK_ERRLOG("bdev io read error\n");
688b75f85fSJohn Kariuki }
698b75f85fSJohn Kariuki
708b75f85fSJohn Kariuki /* Complete the bdev io and close the channel */
718b75f85fSJohn Kariuki spdk_bdev_free_io(bdev_io);
728b75f85fSJohn Kariuki spdk_put_io_channel(hello_context->bdev_io_channel);
738b75f85fSJohn Kariuki spdk_bdev_close(hello_context->bdev_desc);
748b75f85fSJohn Kariuki SPDK_NOTICELOG("Stopping app\n");
758b75f85fSJohn Kariuki spdk_app_stop(success ? 0 : -1);
768b75f85fSJohn Kariuki }
778b75f85fSJohn Kariuki
78f26b1fcaSPiotr Pelplinski static void
hello_read(void * arg)79f26b1fcaSPiotr Pelplinski hello_read(void *arg)
80f26b1fcaSPiotr Pelplinski {
81f26b1fcaSPiotr Pelplinski struct hello_context_t *hello_context = arg;
82f26b1fcaSPiotr Pelplinski int rc = 0;
83f26b1fcaSPiotr Pelplinski
84f26b1fcaSPiotr Pelplinski SPDK_NOTICELOG("Reading io\n");
85f26b1fcaSPiotr Pelplinski rc = spdk_bdev_read(hello_context->bdev_desc, hello_context->bdev_io_channel,
8665e08d12SArtur Paszkiewicz hello_context->buff, 0, hello_context->buff_size, read_complete,
8765e08d12SArtur Paszkiewicz hello_context);
88f26b1fcaSPiotr Pelplinski
89f26b1fcaSPiotr Pelplinski if (rc == -ENOMEM) {
90f26b1fcaSPiotr Pelplinski SPDK_NOTICELOG("Queueing io\n");
91f26b1fcaSPiotr Pelplinski /* In case we cannot perform I/O now, queue I/O */
92f26b1fcaSPiotr Pelplinski hello_context->bdev_io_wait.bdev = hello_context->bdev;
93f26b1fcaSPiotr Pelplinski hello_context->bdev_io_wait.cb_fn = hello_read;
94f26b1fcaSPiotr Pelplinski hello_context->bdev_io_wait.cb_arg = hello_context;
95f26b1fcaSPiotr Pelplinski spdk_bdev_queue_io_wait(hello_context->bdev, hello_context->bdev_io_channel,
96f26b1fcaSPiotr Pelplinski &hello_context->bdev_io_wait);
97f26b1fcaSPiotr Pelplinski } else if (rc) {
98f26b1fcaSPiotr Pelplinski SPDK_ERRLOG("%s error while reading from bdev: %d\n", spdk_strerror(-rc), rc);
99f26b1fcaSPiotr Pelplinski spdk_put_io_channel(hello_context->bdev_io_channel);
100f26b1fcaSPiotr Pelplinski spdk_bdev_close(hello_context->bdev_desc);
101f26b1fcaSPiotr Pelplinski spdk_app_stop(-1);
102f26b1fcaSPiotr Pelplinski }
103f26b1fcaSPiotr Pelplinski }
104f26b1fcaSPiotr Pelplinski
1058b75f85fSJohn Kariuki /*
1068b75f85fSJohn Kariuki * Callback function for write io completion.
1078b75f85fSJohn Kariuki */
1088b75f85fSJohn Kariuki static void
write_complete(struct spdk_bdev_io * bdev_io,bool success,void * cb_arg)1098b75f85fSJohn Kariuki write_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1108b75f85fSJohn Kariuki {
1118b75f85fSJohn Kariuki struct hello_context_t *hello_context = cb_arg;
1128b75f85fSJohn Kariuki
1138b75f85fSJohn Kariuki /* Complete the I/O */
1148b75f85fSJohn Kariuki spdk_bdev_free_io(bdev_io);
1158b75f85fSJohn Kariuki
1168b75f85fSJohn Kariuki if (success) {
1178b75f85fSJohn Kariuki SPDK_NOTICELOG("bdev io write completed successfully\n");
1188b75f85fSJohn Kariuki } else {
1198b75f85fSJohn Kariuki SPDK_ERRLOG("bdev io write error: %d\n", EIO);
1208b75f85fSJohn Kariuki spdk_put_io_channel(hello_context->bdev_io_channel);
1218b75f85fSJohn Kariuki spdk_bdev_close(hello_context->bdev_desc);
1228b75f85fSJohn Kariuki spdk_app_stop(-1);
1238b75f85fSJohn Kariuki return;
1248b75f85fSJohn Kariuki }
1258b75f85fSJohn Kariuki
1268b75f85fSJohn Kariuki /* Zero the buffer so that we can use it for reading */
12765e08d12SArtur Paszkiewicz memset(hello_context->buff, 0, hello_context->buff_size);
1288b75f85fSJohn Kariuki
129f26b1fcaSPiotr Pelplinski hello_read(hello_context);
130f26b1fcaSPiotr Pelplinski }
1318b75f85fSJohn Kariuki
132f26b1fcaSPiotr Pelplinski static void
hello_write(void * arg)133f26b1fcaSPiotr Pelplinski hello_write(void *arg)
134f26b1fcaSPiotr Pelplinski {
135f26b1fcaSPiotr Pelplinski struct hello_context_t *hello_context = arg;
136f26b1fcaSPiotr Pelplinski int rc = 0;
137f26b1fcaSPiotr Pelplinski
138f26b1fcaSPiotr Pelplinski SPDK_NOTICELOG("Writing to the bdev\n");
139f26b1fcaSPiotr Pelplinski rc = spdk_bdev_write(hello_context->bdev_desc, hello_context->bdev_io_channel,
14065e08d12SArtur Paszkiewicz hello_context->buff, 0, hello_context->buff_size, write_complete,
14165e08d12SArtur Paszkiewicz hello_context);
142f26b1fcaSPiotr Pelplinski
143f26b1fcaSPiotr Pelplinski if (rc == -ENOMEM) {
144f26b1fcaSPiotr Pelplinski SPDK_NOTICELOG("Queueing io\n");
145f26b1fcaSPiotr Pelplinski /* In case we cannot perform I/O now, queue I/O */
146f26b1fcaSPiotr Pelplinski hello_context->bdev_io_wait.bdev = hello_context->bdev;
147f26b1fcaSPiotr Pelplinski hello_context->bdev_io_wait.cb_fn = hello_write;
148f26b1fcaSPiotr Pelplinski hello_context->bdev_io_wait.cb_arg = hello_context;
149f26b1fcaSPiotr Pelplinski spdk_bdev_queue_io_wait(hello_context->bdev, hello_context->bdev_io_channel,
150f26b1fcaSPiotr Pelplinski &hello_context->bdev_io_wait);
151f26b1fcaSPiotr Pelplinski } else if (rc) {
152f26b1fcaSPiotr Pelplinski SPDK_ERRLOG("%s error while writing to bdev: %d\n", spdk_strerror(-rc), rc);
1538b75f85fSJohn Kariuki spdk_put_io_channel(hello_context->bdev_io_channel);
1548b75f85fSJohn Kariuki spdk_bdev_close(hello_context->bdev_desc);
1558b75f85fSJohn Kariuki spdk_app_stop(-1);
1568b75f85fSJohn Kariuki }
1578b75f85fSJohn Kariuki }
1588b75f85fSJohn Kariuki
15983b57c54SShuhei Matsumoto static void
hello_bdev_event_cb(enum spdk_bdev_event_type type,struct spdk_bdev * bdev,void * event_ctx)16083b57c54SShuhei Matsumoto hello_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
16183b57c54SShuhei Matsumoto void *event_ctx)
16283b57c54SShuhei Matsumoto {
16383b57c54SShuhei Matsumoto SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
16483b57c54SShuhei Matsumoto }
16583b57c54SShuhei Matsumoto
166f6bbec8bSNiklas Cassel static void
reset_zone_complete(struct spdk_bdev_io * bdev_io,bool success,void * cb_arg)167f6bbec8bSNiklas Cassel reset_zone_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
168f6bbec8bSNiklas Cassel {
169f6bbec8bSNiklas Cassel struct hello_context_t *hello_context = cb_arg;
170f6bbec8bSNiklas Cassel
171f6bbec8bSNiklas Cassel /* Complete the I/O */
172f6bbec8bSNiklas Cassel spdk_bdev_free_io(bdev_io);
173f6bbec8bSNiklas Cassel
174f6bbec8bSNiklas Cassel if (!success) {
175f6bbec8bSNiklas Cassel SPDK_ERRLOG("bdev io reset zone error: %d\n", EIO);
176f6bbec8bSNiklas Cassel spdk_put_io_channel(hello_context->bdev_io_channel);
177f6bbec8bSNiklas Cassel spdk_bdev_close(hello_context->bdev_desc);
178f6bbec8bSNiklas Cassel spdk_app_stop(-1);
179f6bbec8bSNiklas Cassel return;
180f6bbec8bSNiklas Cassel }
181f6bbec8bSNiklas Cassel
182f6bbec8bSNiklas Cassel hello_write(hello_context);
183f6bbec8bSNiklas Cassel }
184f6bbec8bSNiklas Cassel
185f6bbec8bSNiklas Cassel static void
hello_reset_zone(void * arg)186f6bbec8bSNiklas Cassel hello_reset_zone(void *arg)
187f6bbec8bSNiklas Cassel {
188f6bbec8bSNiklas Cassel struct hello_context_t *hello_context = arg;
189f6bbec8bSNiklas Cassel int rc = 0;
190f6bbec8bSNiklas Cassel
191f6bbec8bSNiklas Cassel rc = spdk_bdev_zone_management(hello_context->bdev_desc, hello_context->bdev_io_channel,
192f6bbec8bSNiklas Cassel 0, SPDK_BDEV_ZONE_RESET, reset_zone_complete, hello_context);
193f6bbec8bSNiklas Cassel
194f6bbec8bSNiklas Cassel if (rc == -ENOMEM) {
195f6bbec8bSNiklas Cassel SPDK_NOTICELOG("Queueing io\n");
196f6bbec8bSNiklas Cassel /* In case we cannot perform I/O now, queue I/O */
197f6bbec8bSNiklas Cassel hello_context->bdev_io_wait.bdev = hello_context->bdev;
198f6bbec8bSNiklas Cassel hello_context->bdev_io_wait.cb_fn = hello_reset_zone;
199f6bbec8bSNiklas Cassel hello_context->bdev_io_wait.cb_arg = hello_context;
200f6bbec8bSNiklas Cassel spdk_bdev_queue_io_wait(hello_context->bdev, hello_context->bdev_io_channel,
201f6bbec8bSNiklas Cassel &hello_context->bdev_io_wait);
202f6bbec8bSNiklas Cassel } else if (rc) {
203f6bbec8bSNiklas Cassel SPDK_ERRLOG("%s error while resetting zone: %d\n", spdk_strerror(-rc), rc);
204f6bbec8bSNiklas Cassel spdk_put_io_channel(hello_context->bdev_io_channel);
205f6bbec8bSNiklas Cassel spdk_bdev_close(hello_context->bdev_desc);
206f6bbec8bSNiklas Cassel spdk_app_stop(-1);
207f6bbec8bSNiklas Cassel }
208f6bbec8bSNiklas Cassel }
209f6bbec8bSNiklas Cassel
2108b75f85fSJohn Kariuki /*
2118b75f85fSJohn Kariuki * Our initial event that kicks off everything from main().
2128b75f85fSJohn Kariuki */
2138b75f85fSJohn Kariuki static void
hello_start(void * arg1)214deb8ee5cSBen Walker hello_start(void *arg1)
2158b75f85fSJohn Kariuki {
2168b75f85fSJohn Kariuki struct hello_context_t *hello_context = arg1;
21765e08d12SArtur Paszkiewicz uint32_t buf_align;
2188b75f85fSJohn Kariuki int rc = 0;
2198b75f85fSJohn Kariuki hello_context->bdev = NULL;
2208b75f85fSJohn Kariuki hello_context->bdev_desc = NULL;
2218b75f85fSJohn Kariuki
2228b75f85fSJohn Kariuki SPDK_NOTICELOG("Successfully started the application\n");
2238b75f85fSJohn Kariuki
2248b75f85fSJohn Kariuki /*
22583b57c54SShuhei Matsumoto * There can be many bdevs configured, but this application will only use
22683b57c54SShuhei Matsumoto * the one input by the user at runtime.
22783b57c54SShuhei Matsumoto *
22883b57c54SShuhei Matsumoto * Open the bdev by calling spdk_bdev_open_ext() with its name.
2298b75f85fSJohn Kariuki * The function will return a descriptor
2308b75f85fSJohn Kariuki */
2318b75f85fSJohn Kariuki SPDK_NOTICELOG("Opening the bdev %s\n", hello_context->bdev_name);
23283b57c54SShuhei Matsumoto rc = spdk_bdev_open_ext(hello_context->bdev_name, true, hello_bdev_event_cb, NULL,
23383b57c54SShuhei Matsumoto &hello_context->bdev_desc);
2348b75f85fSJohn Kariuki if (rc) {
2358b75f85fSJohn Kariuki SPDK_ERRLOG("Could not open bdev: %s\n", hello_context->bdev_name);
2368b75f85fSJohn Kariuki spdk_app_stop(-1);
2378b75f85fSJohn Kariuki return;
2388b75f85fSJohn Kariuki }
2398b75f85fSJohn Kariuki
24083b57c54SShuhei Matsumoto /* A bdev pointer is valid while the bdev is opened. */
24183b57c54SShuhei Matsumoto hello_context->bdev = spdk_bdev_desc_get_bdev(hello_context->bdev_desc);
24283b57c54SShuhei Matsumoto
24383b57c54SShuhei Matsumoto
2448b75f85fSJohn Kariuki SPDK_NOTICELOG("Opening io channel\n");
2458b75f85fSJohn Kariuki /* Open I/O channel */
2468b75f85fSJohn Kariuki hello_context->bdev_io_channel = spdk_bdev_get_io_channel(hello_context->bdev_desc);
2478b75f85fSJohn Kariuki if (hello_context->bdev_io_channel == NULL) {
2488b75f85fSJohn Kariuki SPDK_ERRLOG("Could not create bdev I/O channel!!\n");
2498b75f85fSJohn Kariuki spdk_bdev_close(hello_context->bdev_desc);
2508b75f85fSJohn Kariuki spdk_app_stop(-1);
2518b75f85fSJohn Kariuki return;
2528b75f85fSJohn Kariuki }
2538b75f85fSJohn Kariuki
2548b75f85fSJohn Kariuki /* Allocate memory for the write buffer.
2558b75f85fSJohn Kariuki * Initialize the write buffer with the string "Hello World!"
2568b75f85fSJohn Kariuki */
25765e08d12SArtur Paszkiewicz hello_context->buff_size = spdk_bdev_get_block_size(hello_context->bdev) *
25865e08d12SArtur Paszkiewicz spdk_bdev_get_write_unit_size(hello_context->bdev);
2598b75f85fSJohn Kariuki buf_align = spdk_bdev_get_buf_align(hello_context->bdev);
26065e08d12SArtur Paszkiewicz hello_context->buff = spdk_dma_zmalloc(hello_context->buff_size, buf_align, NULL);
2618b75f85fSJohn Kariuki if (!hello_context->buff) {
2628b75f85fSJohn Kariuki SPDK_ERRLOG("Failed to allocate buffer\n");
2638b75f85fSJohn Kariuki spdk_put_io_channel(hello_context->bdev_io_channel);
2648b75f85fSJohn Kariuki spdk_bdev_close(hello_context->bdev_desc);
2658b75f85fSJohn Kariuki spdk_app_stop(-1);
2668b75f85fSJohn Kariuki return;
2678b75f85fSJohn Kariuki }
26865e08d12SArtur Paszkiewicz snprintf(hello_context->buff, hello_context->buff_size, "%s", "Hello World!\n");
2698b75f85fSJohn Kariuki
270f6bbec8bSNiklas Cassel if (spdk_bdev_is_zoned(hello_context->bdev)) {
271f6bbec8bSNiklas Cassel hello_reset_zone(hello_context);
272f6bbec8bSNiklas Cassel /* If bdev is zoned, the callback, reset_zone_complete, will call hello_write() */
273f6bbec8bSNiklas Cassel return;
274f6bbec8bSNiklas Cassel }
275f6bbec8bSNiklas Cassel
276f26b1fcaSPiotr Pelplinski hello_write(hello_context);
2778b75f85fSJohn Kariuki }
2788b75f85fSJohn Kariuki
2798b75f85fSJohn Kariuki int
main(int argc,char ** argv)2808b75f85fSJohn Kariuki main(int argc, char **argv)
2818b75f85fSJohn Kariuki {
2828b75f85fSJohn Kariuki struct spdk_app_opts opts = {};
2838b75f85fSJohn Kariuki int rc = 0;
2848b75f85fSJohn Kariuki struct hello_context_t hello_context = {};
2858b75f85fSJohn Kariuki
2868b75f85fSJohn Kariuki /* Set default values in opts structure. */
28748701bd9SZiye Yang spdk_app_opts_init(&opts, sizeof(opts));
2888b75f85fSJohn Kariuki opts.name = "hello_bdev";
289*5db859daSKrzysztof Karas opts.rpc_addr = NULL;
2908b75f85fSJohn Kariuki
2918b75f85fSJohn Kariuki /*
29215d92f6aSDarek Stojaczyk * Parse built-in SPDK command line parameters as well
29315d92f6aSDarek Stojaczyk * as our custom one(s).
2948b75f85fSJohn Kariuki */
295c2dc15a0SDariusz Stojaczyk if ((rc = spdk_app_parse_args(argc, argv, &opts, "b:", NULL, hello_bdev_parse_arg,
2968b75f85fSJohn Kariuki hello_bdev_usage)) != SPDK_APP_PARSE_ARGS_SUCCESS) {
2978b75f85fSJohn Kariuki exit(rc);
2988b75f85fSJohn Kariuki }
2998b75f85fSJohn Kariuki hello_context.bdev_name = g_bdev_name;
3008b75f85fSJohn Kariuki
3018b75f85fSJohn Kariuki /*
30215d92f6aSDarek Stojaczyk * spdk_app_start() will initialize the SPDK framework, call hello_start(),
30315d92f6aSDarek Stojaczyk * and then block until spdk_app_stop() is called (or if an initialization
30415d92f6aSDarek Stojaczyk * error occurs, spdk_app_start() will return with rc even without calling
30515d92f6aSDarek Stojaczyk * hello_start().
3068b75f85fSJohn Kariuki */
30736287957SBen Walker rc = spdk_app_start(&opts, hello_start, &hello_context);
3088b75f85fSJohn Kariuki if (rc) {
3098b75f85fSJohn Kariuki SPDK_ERRLOG("ERROR starting application\n");
3108b75f85fSJohn Kariuki }
3118b75f85fSJohn Kariuki
31215d92f6aSDarek Stojaczyk /* At this point either spdk_app_stop() was called, or spdk_app_start()
31315d92f6aSDarek Stojaczyk * failed because of internal error.
31415d92f6aSDarek Stojaczyk */
31515d92f6aSDarek Stojaczyk
3168b75f85fSJohn Kariuki /* When the app stops, free up memory that we allocated. */
3178b75f85fSJohn Kariuki spdk_dma_free(hello_context.buff);
3188b75f85fSJohn Kariuki
3198b75f85fSJohn Kariuki /* Gracefully close out all of the SPDK subsystems. */
3208b75f85fSJohn Kariuki spdk_app_fini();
3218b75f85fSJohn Kariuki return rc;
3228b75f85fSJohn Kariuki }
323