xref: /spdk/module/bdev/aio/bdev_aio.c (revision 88e3ffd7b6c5ec1ea1a660354d25f02c766092e1)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bdev_aio.h"
35 
36 #include "spdk/stdinc.h"
37 
38 #include "spdk/barrier.h"
39 #include "spdk/bdev.h"
40 #include "spdk/bdev_module.h"
41 #include "spdk/env.h"
42 #include "spdk/fd.h"
43 #include "spdk/likely.h"
44 #include "spdk/thread.h"
45 #include "spdk/json.h"
46 #include "spdk/util.h"
47 #include "spdk/string.h"
48 
49 #include "spdk/log.h"
50 
51 #include <sys/eventfd.h>
52 #include <libaio.h>
53 
54 struct bdev_aio_io_channel {
55 	uint64_t				io_inflight;
56 	io_context_t				io_ctx;
57 	struct bdev_aio_group_channel		*group_ch;
58 	TAILQ_ENTRY(bdev_aio_io_channel)	link;
59 };
60 
61 struct bdev_aio_group_channel {
62 	/* eventfd for io completion notification in interrupt mode.
63 	 * Negative value like '-1' indicates it is invalid or unused.
64 	 */
65 	int					efd;
66 	struct spdk_interrupt			*intr;
67 	struct spdk_poller			*poller;
68 	TAILQ_HEAD(, bdev_aio_io_channel)	io_ch_head;
69 };
70 
71 struct bdev_aio_task {
72 	struct iocb			iocb;
73 	uint64_t			len;
74 	struct bdev_aio_io_channel	*ch;
75 	TAILQ_ENTRY(bdev_aio_task)	link;
76 };
77 
78 struct file_disk {
79 	struct bdev_aio_task	*reset_task;
80 	struct spdk_poller	*reset_retry_timer;
81 	struct spdk_bdev	disk;
82 	char			*filename;
83 	int			fd;
84 	TAILQ_ENTRY(file_disk)  link;
85 	bool			block_size_override;
86 };
87 
88 /* For user space reaping of completions */
89 struct spdk_aio_ring {
90 	uint32_t id;
91 	uint32_t size;
92 	uint32_t head;
93 	uint32_t tail;
94 
95 	uint32_t version;
96 	uint32_t compat_features;
97 	uint32_t incompat_features;
98 	uint32_t header_length;
99 };
100 
101 #define SPDK_AIO_RING_VERSION	0xa10a10a1
102 
103 static int bdev_aio_initialize(void);
104 static void bdev_aio_fini(void);
105 static void aio_free_disk(struct file_disk *fdisk);
106 static TAILQ_HEAD(, file_disk) g_aio_disk_head = TAILQ_HEAD_INITIALIZER(g_aio_disk_head);
107 
108 #define SPDK_AIO_QUEUE_DEPTH 128
109 #define MAX_EVENTS_PER_POLL 32
110 
111 static int
112 bdev_aio_get_ctx_size(void)
113 {
114 	return sizeof(struct bdev_aio_task);
115 }
116 
117 static struct spdk_bdev_module aio_if = {
118 	.name		= "aio",
119 	.module_init	= bdev_aio_initialize,
120 	.module_fini	= bdev_aio_fini,
121 	.get_ctx_size	= bdev_aio_get_ctx_size,
122 };
123 
124 SPDK_BDEV_MODULE_REGISTER(aio, &aio_if)
125 
126 static int
127 bdev_aio_open(struct file_disk *disk)
128 {
129 	int fd;
130 
131 	fd = open(disk->filename, O_RDWR | O_DIRECT);
132 	if (fd < 0) {
133 		/* Try without O_DIRECT for non-disk files */
134 		fd = open(disk->filename, O_RDWR);
135 		if (fd < 0) {
136 			SPDK_ERRLOG("open() failed (file:%s), errno %d: %s\n",
137 				    disk->filename, errno, spdk_strerror(errno));
138 			disk->fd = -1;
139 			return -1;
140 		}
141 	}
142 
143 	disk->fd = fd;
144 
145 	return 0;
146 }
147 
148 static int
149 bdev_aio_close(struct file_disk *disk)
150 {
151 	int rc;
152 
153 	if (disk->fd == -1) {
154 		return 0;
155 	}
156 
157 	rc = close(disk->fd);
158 	if (rc < 0) {
159 		SPDK_ERRLOG("close() failed (fd=%d), errno %d: %s\n",
160 			    disk->fd, errno, spdk_strerror(errno));
161 		return -1;
162 	}
163 
164 	disk->fd = -1;
165 
166 	return 0;
167 }
168 
169 static int64_t
170 bdev_aio_readv(struct file_disk *fdisk, struct spdk_io_channel *ch,
171 	       struct bdev_aio_task *aio_task,
172 	       struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t offset)
173 {
174 	struct iocb *iocb = &aio_task->iocb;
175 	struct bdev_aio_io_channel *aio_ch = spdk_io_channel_get_ctx(ch);
176 	int rc;
177 
178 	io_prep_preadv(iocb, fdisk->fd, iov, iovcnt, offset);
179 	if (aio_ch->group_ch->efd >= 0) {
180 		io_set_eventfd(iocb, aio_ch->group_ch->efd);
181 	}
182 	iocb->data = aio_task;
183 	aio_task->len = nbytes;
184 	aio_task->ch = aio_ch;
185 
186 	SPDK_DEBUGLOG(aio, "read %d iovs size %lu to off: %#lx\n",
187 		      iovcnt, nbytes, offset);
188 
189 	rc = io_submit(aio_ch->io_ctx, 1, &iocb);
190 	if (rc < 0) {
191 		if (rc == -EAGAIN) {
192 			spdk_bdev_io_complete(spdk_bdev_io_from_ctx(aio_task), SPDK_BDEV_IO_STATUS_NOMEM);
193 		} else {
194 			spdk_bdev_io_complete_aio_status(spdk_bdev_io_from_ctx(aio_task), rc);
195 			SPDK_ERRLOG("%s: io_submit returned %d\n", __func__, rc);
196 		}
197 		return -1;
198 	}
199 	aio_ch->io_inflight++;
200 	return nbytes;
201 }
202 
203 static int64_t
204 bdev_aio_writev(struct file_disk *fdisk, struct spdk_io_channel *ch,
205 		struct bdev_aio_task *aio_task,
206 		struct iovec *iov, int iovcnt, size_t len, uint64_t offset)
207 {
208 	struct iocb *iocb = &aio_task->iocb;
209 	struct bdev_aio_io_channel *aio_ch = spdk_io_channel_get_ctx(ch);
210 	int rc;
211 
212 	io_prep_pwritev(iocb, fdisk->fd, iov, iovcnt, offset);
213 	if (aio_ch->group_ch->efd >= 0) {
214 		io_set_eventfd(iocb, aio_ch->group_ch->efd);
215 	}
216 	iocb->data = aio_task;
217 	aio_task->len = len;
218 	aio_task->ch = aio_ch;
219 
220 	SPDK_DEBUGLOG(aio, "write %d iovs size %lu from off: %#lx\n",
221 		      iovcnt, len, offset);
222 
223 	rc = io_submit(aio_ch->io_ctx, 1, &iocb);
224 	if (rc < 0) {
225 		if (rc == -EAGAIN) {
226 			spdk_bdev_io_complete(spdk_bdev_io_from_ctx(aio_task), SPDK_BDEV_IO_STATUS_NOMEM);
227 		} else {
228 			spdk_bdev_io_complete_aio_status(spdk_bdev_io_from_ctx(aio_task), rc);
229 			SPDK_ERRLOG("%s: io_submit returned %d\n", __func__, rc);
230 		}
231 		return -1;
232 	}
233 	aio_ch->io_inflight++;
234 	return len;
235 }
236 
237 static void
238 bdev_aio_flush(struct file_disk *fdisk, struct bdev_aio_task *aio_task)
239 {
240 	int rc = fsync(fdisk->fd);
241 
242 	if (rc == 0) {
243 		spdk_bdev_io_complete(spdk_bdev_io_from_ctx(aio_task), SPDK_BDEV_IO_STATUS_SUCCESS);
244 	} else {
245 		spdk_bdev_io_complete_aio_status(spdk_bdev_io_from_ctx(aio_task), -errno);
246 	}
247 }
248 
249 static int
250 bdev_aio_destruct(void *ctx)
251 {
252 	struct file_disk *fdisk = ctx;
253 	int rc = 0;
254 
255 	TAILQ_REMOVE(&g_aio_disk_head, fdisk, link);
256 	rc = bdev_aio_close(fdisk);
257 	if (rc < 0) {
258 		SPDK_ERRLOG("bdev_aio_close() failed\n");
259 	}
260 	spdk_io_device_unregister(fdisk, NULL);
261 	aio_free_disk(fdisk);
262 	return rc;
263 }
264 
265 static int
266 bdev_user_io_getevents(io_context_t io_ctx, unsigned int max, struct io_event *uevents)
267 {
268 	uint32_t head, tail, count;
269 	struct spdk_aio_ring *ring;
270 	struct timespec timeout;
271 	struct io_event *kevents;
272 
273 	ring = (struct spdk_aio_ring *)io_ctx;
274 
275 	if (spdk_unlikely(ring->version != SPDK_AIO_RING_VERSION || ring->incompat_features != 0)) {
276 		timeout.tv_sec = 0;
277 		timeout.tv_nsec = 0;
278 
279 		return io_getevents(io_ctx, 0, max, uevents, &timeout);
280 	}
281 
282 	/* Read the current state out of the ring */
283 	head = ring->head;
284 	tail = ring->tail;
285 
286 	/* This memory barrier is required to prevent the loads above
287 	 * from being re-ordered with stores to the events array
288 	 * potentially occurring on other threads. */
289 	spdk_smp_rmb();
290 
291 	/* Calculate how many items are in the circular ring */
292 	count = tail - head;
293 	if (tail < head) {
294 		count += ring->size;
295 	}
296 
297 	/* Reduce the count to the limit provided by the user */
298 	count = spdk_min(max, count);
299 
300 	/* Grab the memory location of the event array */
301 	kevents = (struct io_event *)((uintptr_t)ring + ring->header_length);
302 
303 	/* Copy the events out of the ring. */
304 	if ((head + count) <= ring->size) {
305 		/* Only one copy is required */
306 		memcpy(uevents, &kevents[head], count * sizeof(struct io_event));
307 	} else {
308 		uint32_t first_part = ring->size - head;
309 		/* Two copies are required */
310 		memcpy(uevents, &kevents[head], first_part * sizeof(struct io_event));
311 		memcpy(&uevents[first_part], &kevents[0], (count - first_part) * sizeof(struct io_event));
312 	}
313 
314 	/* Update the head pointer. On x86, stores will not be reordered with older loads,
315 	 * so the copies out of the event array will always be complete prior to this
316 	 * update becoming visible. On other architectures this is not guaranteed, so
317 	 * add a barrier. */
318 #if defined(__i386__) || defined(__x86_64__)
319 	spdk_compiler_barrier();
320 #else
321 	spdk_smp_mb();
322 #endif
323 	ring->head = (head + count) % ring->size;
324 
325 	return count;
326 }
327 
328 static int
329 bdev_aio_io_channel_poll(struct bdev_aio_io_channel *io_ch)
330 {
331 	int nr, i = 0;
332 	struct bdev_aio_task *aio_task;
333 	struct io_event events[SPDK_AIO_QUEUE_DEPTH];
334 	uint64_t io_result;
335 
336 	nr = bdev_user_io_getevents(io_ch->io_ctx, SPDK_AIO_QUEUE_DEPTH, events);
337 
338 	if (nr < 0) {
339 		return 0;
340 	}
341 
342 #define MAX_AIO_ERRNO 256
343 	for (i = 0; i < nr; i++) {
344 		aio_task = events[i].data;
345 		aio_task->ch->io_inflight--;
346 		io_result = events[i].res;
347 		if (io_result == aio_task->len) {
348 			spdk_bdev_io_complete(spdk_bdev_io_from_ctx(aio_task), SPDK_BDEV_IO_STATUS_SUCCESS);
349 		} else if (io_result < MAX_AIO_ERRNO) {
350 			/* Linux AIO will return its errno to io_event.res */
351 			int aio_errno = io_result;
352 
353 			spdk_bdev_io_complete_aio_status(spdk_bdev_io_from_ctx(aio_task), -aio_errno);
354 		} else {
355 			SPDK_ERRLOG("failed to complete aio: requested len is %lu, but completed len is %lu.\n",
356 				    aio_task->len, io_result);
357 			spdk_bdev_io_complete(spdk_bdev_io_from_ctx(aio_task), SPDK_BDEV_IO_STATUS_FAILED);
358 		}
359 	}
360 
361 	return nr;
362 }
363 
364 static int
365 bdev_aio_group_poll(void *arg)
366 {
367 	struct bdev_aio_group_channel *group_ch = arg;
368 	struct bdev_aio_io_channel *io_ch;
369 	int nr = 0;
370 
371 	TAILQ_FOREACH(io_ch, &group_ch->io_ch_head, link) {
372 		nr += bdev_aio_io_channel_poll(io_ch);
373 	}
374 
375 	return nr > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
376 }
377 
378 static int
379 bdev_aio_group_interrupt(void *arg)
380 {
381 	struct bdev_aio_group_channel *group_ch = arg;
382 	int rc;
383 	uint64_t num_events;
384 
385 	assert(group_ch->efd >= 0);
386 
387 	/* if completed IO number is larger than SPDK_AIO_QUEUE_DEPTH,
388 	 * io_getevent should be called again to ensure all completed IO are processed.
389 	 */
390 	rc = read(group_ch->efd, &num_events, sizeof(num_events));
391 	if (rc < 0) {
392 		SPDK_ERRLOG("failed to acknowledge aio group: %s.\n", spdk_strerror(errno));
393 		return -errno;
394 	}
395 
396 	if (num_events > SPDK_AIO_QUEUE_DEPTH) {
397 		num_events -= SPDK_AIO_QUEUE_DEPTH;
398 		rc = write(group_ch->efd, &num_events, sizeof(num_events));
399 		if (rc < 0) {
400 			SPDK_ERRLOG("failed to notify aio group: %s.\n", spdk_strerror(errno));
401 		}
402 	}
403 
404 	return bdev_aio_group_poll(group_ch);
405 }
406 
407 static void
408 _bdev_aio_get_io_inflight(struct spdk_io_channel_iter *i)
409 {
410 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
411 	struct bdev_aio_io_channel *aio_ch = spdk_io_channel_get_ctx(ch);
412 
413 	if (aio_ch->io_inflight) {
414 		spdk_for_each_channel_continue(i, -1);
415 		return;
416 	}
417 
418 	spdk_for_each_channel_continue(i, 0);
419 }
420 
421 static int bdev_aio_reset_retry_timer(void *arg);
422 
423 static void
424 _bdev_aio_get_io_inflight_done(struct spdk_io_channel_iter *i, int status)
425 {
426 	struct file_disk *fdisk = spdk_io_channel_iter_get_ctx(i);
427 
428 	if (status == -1) {
429 		fdisk->reset_retry_timer = SPDK_POLLER_REGISTER(bdev_aio_reset_retry_timer, fdisk, 500);
430 		return;
431 	}
432 
433 	spdk_bdev_io_complete(spdk_bdev_io_from_ctx(fdisk->reset_task), SPDK_BDEV_IO_STATUS_SUCCESS);
434 }
435 
436 static int
437 bdev_aio_reset_retry_timer(void *arg)
438 {
439 	struct file_disk *fdisk = arg;
440 
441 	if (fdisk->reset_retry_timer) {
442 		spdk_poller_unregister(&fdisk->reset_retry_timer);
443 	}
444 
445 	spdk_for_each_channel(fdisk,
446 			      _bdev_aio_get_io_inflight,
447 			      fdisk,
448 			      _bdev_aio_get_io_inflight_done);
449 
450 	return SPDK_POLLER_BUSY;
451 }
452 
453 static void
454 bdev_aio_reset(struct file_disk *fdisk, struct bdev_aio_task *aio_task)
455 {
456 	fdisk->reset_task = aio_task;
457 
458 	bdev_aio_reset_retry_timer(fdisk);
459 }
460 
461 static void
462 bdev_aio_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
463 		    bool success)
464 {
465 	if (!success) {
466 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
467 		return;
468 	}
469 
470 	switch (bdev_io->type) {
471 	case SPDK_BDEV_IO_TYPE_READ:
472 		bdev_aio_readv((struct file_disk *)bdev_io->bdev->ctxt,
473 			       ch,
474 			       (struct bdev_aio_task *)bdev_io->driver_ctx,
475 			       bdev_io->u.bdev.iovs,
476 			       bdev_io->u.bdev.iovcnt,
477 			       bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
478 			       bdev_io->u.bdev.offset_blocks * bdev_io->bdev->blocklen);
479 		break;
480 	case SPDK_BDEV_IO_TYPE_WRITE:
481 		bdev_aio_writev((struct file_disk *)bdev_io->bdev->ctxt,
482 				ch,
483 				(struct bdev_aio_task *)bdev_io->driver_ctx,
484 				bdev_io->u.bdev.iovs,
485 				bdev_io->u.bdev.iovcnt,
486 				bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
487 				bdev_io->u.bdev.offset_blocks * bdev_io->bdev->blocklen);
488 		break;
489 	default:
490 		SPDK_ERRLOG("Wrong io type\n");
491 		break;
492 	}
493 }
494 
495 static int _bdev_aio_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
496 {
497 	switch (bdev_io->type) {
498 	/* Read and write operations must be performed on buffers aligned to
499 	 * bdev->required_alignment. If user specified unaligned buffers,
500 	 * get the aligned buffer from the pool by calling spdk_bdev_io_get_buf. */
501 	case SPDK_BDEV_IO_TYPE_READ:
502 	case SPDK_BDEV_IO_TYPE_WRITE:
503 		spdk_bdev_io_get_buf(bdev_io, bdev_aio_get_buf_cb,
504 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
505 		return 0;
506 	case SPDK_BDEV_IO_TYPE_FLUSH:
507 		bdev_aio_flush((struct file_disk *)bdev_io->bdev->ctxt,
508 			       (struct bdev_aio_task *)bdev_io->driver_ctx);
509 		return 0;
510 
511 	case SPDK_BDEV_IO_TYPE_RESET:
512 		bdev_aio_reset((struct file_disk *)bdev_io->bdev->ctxt,
513 			       (struct bdev_aio_task *)bdev_io->driver_ctx);
514 		return 0;
515 	default:
516 		return -1;
517 	}
518 }
519 
520 static void bdev_aio_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
521 {
522 	if (_bdev_aio_submit_request(ch, bdev_io) < 0) {
523 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
524 	}
525 }
526 
527 static bool
528 bdev_aio_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
529 {
530 	switch (io_type) {
531 	case SPDK_BDEV_IO_TYPE_READ:
532 	case SPDK_BDEV_IO_TYPE_WRITE:
533 	case SPDK_BDEV_IO_TYPE_FLUSH:
534 	case SPDK_BDEV_IO_TYPE_RESET:
535 		return true;
536 
537 	default:
538 		return false;
539 	}
540 }
541 
542 static int
543 bdev_aio_create_cb(void *io_device, void *ctx_buf)
544 {
545 	struct bdev_aio_io_channel *ch = ctx_buf;
546 
547 	if (io_setup(SPDK_AIO_QUEUE_DEPTH, &ch->io_ctx) < 0) {
548 		SPDK_ERRLOG("async I/O context setup failure\n");
549 		return -1;
550 	}
551 
552 	ch->group_ch = spdk_io_channel_get_ctx(spdk_get_io_channel(&aio_if));
553 	TAILQ_INSERT_TAIL(&ch->group_ch->io_ch_head, ch, link);
554 
555 	return 0;
556 }
557 
558 static void
559 bdev_aio_destroy_cb(void *io_device, void *ctx_buf)
560 {
561 	struct bdev_aio_io_channel *ch = ctx_buf;
562 
563 	io_destroy(ch->io_ctx);
564 
565 	assert(ch->group_ch);
566 	TAILQ_REMOVE(&ch->group_ch->io_ch_head, ch, link);
567 
568 	spdk_put_io_channel(spdk_io_channel_from_ctx(ch->group_ch));
569 }
570 
571 static struct spdk_io_channel *
572 bdev_aio_get_io_channel(void *ctx)
573 {
574 	struct file_disk *fdisk = ctx;
575 
576 	return spdk_get_io_channel(fdisk);
577 }
578 
579 
580 static int
581 bdev_aio_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
582 {
583 	struct file_disk *fdisk = ctx;
584 
585 	spdk_json_write_named_object_begin(w, "aio");
586 
587 	spdk_json_write_named_string(w, "filename", fdisk->filename);
588 
589 	spdk_json_write_object_end(w);
590 
591 	return 0;
592 }
593 
594 static void
595 bdev_aio_write_json_config(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
596 {
597 	struct file_disk *fdisk = bdev->ctxt;
598 
599 	spdk_json_write_object_begin(w);
600 
601 	spdk_json_write_named_string(w, "method", "bdev_aio_create");
602 
603 	spdk_json_write_named_object_begin(w, "params");
604 	spdk_json_write_named_string(w, "name", bdev->name);
605 	if (fdisk->block_size_override) {
606 		spdk_json_write_named_uint32(w, "block_size", bdev->blocklen);
607 	}
608 	spdk_json_write_named_string(w, "filename", fdisk->filename);
609 	spdk_json_write_object_end(w);
610 
611 	spdk_json_write_object_end(w);
612 }
613 
614 static const struct spdk_bdev_fn_table aio_fn_table = {
615 	.destruct		= bdev_aio_destruct,
616 	.submit_request		= bdev_aio_submit_request,
617 	.io_type_supported	= bdev_aio_io_type_supported,
618 	.get_io_channel		= bdev_aio_get_io_channel,
619 	.dump_info_json		= bdev_aio_dump_info_json,
620 	.write_config_json	= bdev_aio_write_json_config,
621 };
622 
623 static void aio_free_disk(struct file_disk *fdisk)
624 {
625 	if (fdisk == NULL) {
626 		return;
627 	}
628 	free(fdisk->filename);
629 	free(fdisk->disk.name);
630 	free(fdisk);
631 }
632 
633 static int
634 bdev_aio_register_interrupt(struct bdev_aio_group_channel *ch)
635 {
636 	int efd;
637 
638 	efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
639 	if (efd < 0) {
640 		return -1;
641 	}
642 
643 	ch->intr = SPDK_INTERRUPT_REGISTER(efd, bdev_aio_group_interrupt, ch);
644 	if (ch->intr == NULL) {
645 		close(efd);
646 		return -1;
647 	}
648 	ch->efd = efd;
649 
650 	return 0;
651 }
652 
653 static void
654 bdev_aio_unregister_interrupt(struct bdev_aio_group_channel *ch)
655 {
656 	spdk_interrupt_unregister(&ch->intr);
657 	close(ch->efd);
658 	ch->efd = -1;
659 }
660 
661 static int
662 bdev_aio_group_create_cb(void *io_device, void *ctx_buf)
663 {
664 	struct bdev_aio_group_channel *ch = ctx_buf;
665 
666 	TAILQ_INIT(&ch->io_ch_head);
667 	/* Initialize ch->efd to be invalid and unused. */
668 	ch->efd = -1;
669 
670 	if (spdk_interrupt_mode_is_enabled()) {
671 		return bdev_aio_register_interrupt(ch);
672 	}
673 
674 	ch->poller = SPDK_POLLER_REGISTER(bdev_aio_group_poll, ch, 0);
675 
676 	return 0;
677 }
678 
679 static void
680 bdev_aio_group_destroy_cb(void *io_device, void *ctx_buf)
681 {
682 	struct bdev_aio_group_channel *ch = ctx_buf;
683 
684 	if (!TAILQ_EMPTY(&ch->io_ch_head)) {
685 		SPDK_ERRLOG("Group channel of bdev aio has uncleared io channel\n");
686 	}
687 
688 	if (ch->intr) {
689 		bdev_aio_unregister_interrupt(ch);
690 		return;
691 	}
692 
693 	spdk_poller_unregister(&ch->poller);
694 }
695 
696 int
697 create_aio_bdev(const char *name, const char *filename, uint32_t block_size)
698 {
699 	struct file_disk *fdisk;
700 	uint32_t detected_block_size;
701 	uint64_t disk_size;
702 	int rc;
703 
704 	fdisk = calloc(1, sizeof(*fdisk));
705 	if (!fdisk) {
706 		SPDK_ERRLOG("Unable to allocate enough memory for aio backend\n");
707 		return -ENOMEM;
708 	}
709 
710 	fdisk->filename = strdup(filename);
711 	if (!fdisk->filename) {
712 		rc = -ENOMEM;
713 		goto error_return;
714 	}
715 
716 	if (bdev_aio_open(fdisk)) {
717 		SPDK_ERRLOG("Unable to open file %s. fd: %d errno: %d\n", filename, fdisk->fd, errno);
718 		rc = -errno;
719 		goto error_return;
720 	}
721 
722 	disk_size = spdk_fd_get_size(fdisk->fd);
723 
724 	fdisk->disk.name = strdup(name);
725 	if (!fdisk->disk.name) {
726 		rc = -ENOMEM;
727 		goto error_return;
728 	}
729 	fdisk->disk.product_name = "AIO disk";
730 	fdisk->disk.module = &aio_if;
731 
732 	fdisk->disk.write_cache = 1;
733 
734 	detected_block_size = spdk_fd_get_blocklen(fdisk->fd);
735 	if (block_size == 0) {
736 		/* User did not specify block size - use autodetected block size. */
737 		if (detected_block_size == 0) {
738 			SPDK_ERRLOG("Block size could not be auto-detected\n");
739 			rc = -EINVAL;
740 			goto error_return;
741 		}
742 		fdisk->block_size_override = false;
743 		block_size = detected_block_size;
744 	} else {
745 		if (block_size < detected_block_size) {
746 			SPDK_ERRLOG("Specified block size %" PRIu32 " is smaller than "
747 				    "auto-detected block size %" PRIu32 "\n",
748 				    block_size, detected_block_size);
749 			rc = -EINVAL;
750 			goto error_return;
751 		} else if (detected_block_size != 0 && block_size != detected_block_size) {
752 			SPDK_WARNLOG("Specified block size %" PRIu32 " does not match "
753 				     "auto-detected block size %" PRIu32 "\n",
754 				     block_size, detected_block_size);
755 		}
756 		fdisk->block_size_override = true;
757 	}
758 
759 	if (block_size < 512) {
760 		SPDK_ERRLOG("Invalid block size %" PRIu32 " (must be at least 512).\n", block_size);
761 		rc = -EINVAL;
762 		goto error_return;
763 	}
764 
765 	if (!spdk_u32_is_pow2(block_size)) {
766 		SPDK_ERRLOG("Invalid block size %" PRIu32 " (must be a power of 2.)\n", block_size);
767 		rc = -EINVAL;
768 		goto error_return;
769 	}
770 
771 	fdisk->disk.blocklen = block_size;
772 	if (fdisk->block_size_override && detected_block_size) {
773 		fdisk->disk.required_alignment = spdk_u32log2(detected_block_size);
774 	} else {
775 		fdisk->disk.required_alignment = spdk_u32log2(block_size);
776 	}
777 
778 	if (disk_size % fdisk->disk.blocklen != 0) {
779 		SPDK_ERRLOG("Disk size %" PRIu64 " is not a multiple of block size %" PRIu32 "\n",
780 			    disk_size, fdisk->disk.blocklen);
781 		rc = -EINVAL;
782 		goto error_return;
783 	}
784 
785 	fdisk->disk.blockcnt = disk_size / fdisk->disk.blocklen;
786 	fdisk->disk.ctxt = fdisk;
787 
788 	fdisk->disk.fn_table = &aio_fn_table;
789 
790 	spdk_io_device_register(fdisk, bdev_aio_create_cb, bdev_aio_destroy_cb,
791 				sizeof(struct bdev_aio_io_channel),
792 				fdisk->disk.name);
793 	rc = spdk_bdev_register(&fdisk->disk);
794 	if (rc) {
795 		spdk_io_device_unregister(fdisk, NULL);
796 		goto error_return;
797 	}
798 
799 	TAILQ_INSERT_TAIL(&g_aio_disk_head, fdisk, link);
800 	return 0;
801 
802 error_return:
803 	bdev_aio_close(fdisk);
804 	aio_free_disk(fdisk);
805 	return rc;
806 }
807 
808 struct delete_aio_bdev_ctx {
809 	delete_aio_bdev_complete cb_fn;
810 	void *cb_arg;
811 };
812 
813 static void
814 aio_bdev_unregister_cb(void *arg, int bdeverrno)
815 {
816 	struct delete_aio_bdev_ctx *ctx = arg;
817 
818 	ctx->cb_fn(ctx->cb_arg, bdeverrno);
819 	free(ctx);
820 }
821 
822 void
823 bdev_aio_delete(struct spdk_bdev *bdev, delete_aio_bdev_complete cb_fn, void *cb_arg)
824 {
825 	struct delete_aio_bdev_ctx *ctx;
826 
827 	if (!bdev || bdev->module != &aio_if) {
828 		cb_fn(cb_arg, -ENODEV);
829 		return;
830 	}
831 
832 	ctx = calloc(1, sizeof(*ctx));
833 	if (ctx == NULL) {
834 		cb_fn(cb_arg, -ENOMEM);
835 		return;
836 	}
837 
838 	ctx->cb_fn = cb_fn;
839 	ctx->cb_arg = cb_arg;
840 	spdk_bdev_unregister(bdev, aio_bdev_unregister_cb, ctx);
841 }
842 
843 static int
844 bdev_aio_initialize(void)
845 {
846 	spdk_io_device_register(&aio_if, bdev_aio_group_create_cb, bdev_aio_group_destroy_cb,
847 				sizeof(struct bdev_aio_group_channel), "aio_module");
848 
849 	return 0;
850 }
851 
852 static void
853 bdev_aio_fini(void)
854 {
855 	spdk_io_device_unregister(&aio_if, NULL);
856 }
857 
858 SPDK_LOG_REGISTER_COMPONENT(aio)
859