1737e150eSPaolo Bonzini /* 2737e150eSPaolo Bonzini * QEMU aio implementation 3737e150eSPaolo Bonzini * 4737e150eSPaolo Bonzini * Copyright IBM, Corp. 2008 5737e150eSPaolo Bonzini * 6737e150eSPaolo Bonzini * Authors: 7737e150eSPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 8737e150eSPaolo Bonzini * 9737e150eSPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 10737e150eSPaolo Bonzini * the COPYING file in the top-level directory. 11737e150eSPaolo Bonzini * 12737e150eSPaolo Bonzini */ 13737e150eSPaolo Bonzini 14737e150eSPaolo Bonzini #ifndef QEMU_AIO_H 15737e150eSPaolo Bonzini #define QEMU_AIO_H 16737e150eSPaolo Bonzini 17737e150eSPaolo Bonzini #include "qemu-common.h" 18*1de7afc9SPaolo Bonzini #include "qemu/queue.h" 19*1de7afc9SPaolo Bonzini #include "qemu/event_notifier.h" 20737e150eSPaolo Bonzini 21737e150eSPaolo Bonzini typedef struct BlockDriverAIOCB BlockDriverAIOCB; 22737e150eSPaolo Bonzini typedef void BlockDriverCompletionFunc(void *opaque, int ret); 23737e150eSPaolo Bonzini 24737e150eSPaolo Bonzini typedef struct AIOCBInfo { 25737e150eSPaolo Bonzini void (*cancel)(BlockDriverAIOCB *acb); 26737e150eSPaolo Bonzini size_t aiocb_size; 27737e150eSPaolo Bonzini } AIOCBInfo; 28737e150eSPaolo Bonzini 29737e150eSPaolo Bonzini struct BlockDriverAIOCB { 30737e150eSPaolo Bonzini const AIOCBInfo *aiocb_info; 31737e150eSPaolo Bonzini BlockDriverState *bs; 32737e150eSPaolo Bonzini BlockDriverCompletionFunc *cb; 33737e150eSPaolo Bonzini void *opaque; 34737e150eSPaolo Bonzini }; 35737e150eSPaolo Bonzini 36737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 37737e150eSPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque); 38737e150eSPaolo Bonzini void qemu_aio_release(void *p); 39737e150eSPaolo Bonzini 40737e150eSPaolo Bonzini typedef struct AioHandler AioHandler; 41737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque); 42737e150eSPaolo Bonzini typedef void IOHandler(void *opaque); 43737e150eSPaolo Bonzini 44737e150eSPaolo Bonzini typedef struct AioContext { 45737e150eSPaolo Bonzini GSource source; 46737e150eSPaolo Bonzini 47737e150eSPaolo Bonzini /* The list of registered AIO handlers */ 48737e150eSPaolo Bonzini QLIST_HEAD(, AioHandler) aio_handlers; 49737e150eSPaolo Bonzini 50737e150eSPaolo Bonzini /* This is a simple lock used to protect the aio_handlers list. 51737e150eSPaolo Bonzini * Specifically, it's used to ensure that no callbacks are removed while 52737e150eSPaolo Bonzini * we're walking and dispatching callbacks. 53737e150eSPaolo Bonzini */ 54737e150eSPaolo Bonzini int walking_handlers; 55737e150eSPaolo Bonzini 56737e150eSPaolo Bonzini /* Anchor of the list of Bottom Halves belonging to the context */ 57737e150eSPaolo Bonzini struct QEMUBH *first_bh; 58737e150eSPaolo Bonzini 59737e150eSPaolo Bonzini /* A simple lock used to protect the first_bh list, and ensure that 60737e150eSPaolo Bonzini * no callbacks are removed while we're walking and dispatching callbacks. 61737e150eSPaolo Bonzini */ 62737e150eSPaolo Bonzini int walking_bh; 63737e150eSPaolo Bonzini 64737e150eSPaolo Bonzini /* Used for aio_notify. */ 65737e150eSPaolo Bonzini EventNotifier notifier; 66737e150eSPaolo Bonzini } AioContext; 67737e150eSPaolo Bonzini 68737e150eSPaolo Bonzini /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */ 69737e150eSPaolo Bonzini typedef int (AioFlushEventNotifierHandler)(EventNotifier *e); 70737e150eSPaolo Bonzini 71737e150eSPaolo Bonzini /** 72737e150eSPaolo Bonzini * aio_context_new: Allocate a new AioContext. 73737e150eSPaolo Bonzini * 74737e150eSPaolo Bonzini * AioContext provide a mini event-loop that can be waited on synchronously. 75737e150eSPaolo Bonzini * They also provide bottom halves, a service to execute a piece of code 76737e150eSPaolo Bonzini * as soon as possible. 77737e150eSPaolo Bonzini */ 78737e150eSPaolo Bonzini AioContext *aio_context_new(void); 79737e150eSPaolo Bonzini 80737e150eSPaolo Bonzini /** 81737e150eSPaolo Bonzini * aio_context_ref: 82737e150eSPaolo Bonzini * @ctx: The AioContext to operate on. 83737e150eSPaolo Bonzini * 84737e150eSPaolo Bonzini * Add a reference to an AioContext. 85737e150eSPaolo Bonzini */ 86737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx); 87737e150eSPaolo Bonzini 88737e150eSPaolo Bonzini /** 89737e150eSPaolo Bonzini * aio_context_unref: 90737e150eSPaolo Bonzini * @ctx: The AioContext to operate on. 91737e150eSPaolo Bonzini * 92737e150eSPaolo Bonzini * Drop a reference to an AioContext. 93737e150eSPaolo Bonzini */ 94737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx); 95737e150eSPaolo Bonzini 96737e150eSPaolo Bonzini /** 97737e150eSPaolo Bonzini * aio_bh_new: Allocate a new bottom half structure. 98737e150eSPaolo Bonzini * 99737e150eSPaolo Bonzini * Bottom halves are lightweight callbacks whose invocation is guaranteed 100737e150eSPaolo Bonzini * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure 101737e150eSPaolo Bonzini * is opaque and must be allocated prior to its use. 102737e150eSPaolo Bonzini */ 103737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque); 104737e150eSPaolo Bonzini 105737e150eSPaolo Bonzini /** 106737e150eSPaolo Bonzini * aio_notify: Force processing of pending events. 107737e150eSPaolo Bonzini * 108737e150eSPaolo Bonzini * Similar to signaling a condition variable, aio_notify forces 109737e150eSPaolo Bonzini * aio_wait to exit, so that the next call will re-examine pending events. 110737e150eSPaolo Bonzini * The caller of aio_notify will usually call aio_wait again very soon, 111737e150eSPaolo Bonzini * or go through another iteration of the GLib main loop. Hence, aio_notify 112737e150eSPaolo Bonzini * also has the side effect of recalculating the sets of file descriptors 113737e150eSPaolo Bonzini * that the main loop waits for. 114737e150eSPaolo Bonzini * 115737e150eSPaolo Bonzini * Calling aio_notify is rarely necessary, because for example scheduling 116737e150eSPaolo Bonzini * a bottom half calls it already. 117737e150eSPaolo Bonzini */ 118737e150eSPaolo Bonzini void aio_notify(AioContext *ctx); 119737e150eSPaolo Bonzini 120737e150eSPaolo Bonzini /** 121737e150eSPaolo Bonzini * aio_bh_poll: Poll bottom halves for an AioContext. 122737e150eSPaolo Bonzini * 123737e150eSPaolo Bonzini * These are internal functions used by the QEMU main loop. 124737e150eSPaolo Bonzini */ 125737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx); 126737e150eSPaolo Bonzini 127737e150eSPaolo Bonzini /** 128737e150eSPaolo Bonzini * qemu_bh_schedule: Schedule a bottom half. 129737e150eSPaolo Bonzini * 130737e150eSPaolo Bonzini * Scheduling a bottom half interrupts the main loop and causes the 131737e150eSPaolo Bonzini * execution of the callback that was passed to qemu_bh_new. 132737e150eSPaolo Bonzini * 133737e150eSPaolo Bonzini * Bottom halves that are scheduled from a bottom half handler are instantly 134737e150eSPaolo Bonzini * invoked. This can create an infinite loop if a bottom half handler 135737e150eSPaolo Bonzini * schedules itself. 136737e150eSPaolo Bonzini * 137737e150eSPaolo Bonzini * @bh: The bottom half to be scheduled. 138737e150eSPaolo Bonzini */ 139737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh); 140737e150eSPaolo Bonzini 141737e150eSPaolo Bonzini /** 142737e150eSPaolo Bonzini * qemu_bh_cancel: Cancel execution of a bottom half. 143737e150eSPaolo Bonzini * 144737e150eSPaolo Bonzini * Canceling execution of a bottom half undoes the effect of calls to 145737e150eSPaolo Bonzini * qemu_bh_schedule without freeing its resources yet. While cancellation 146737e150eSPaolo Bonzini * itself is also wait-free and thread-safe, it can of course race with the 147737e150eSPaolo Bonzini * loop that executes bottom halves unless you are holding the iothread 148737e150eSPaolo Bonzini * mutex. This makes it mostly useless if you are not holding the mutex. 149737e150eSPaolo Bonzini * 150737e150eSPaolo Bonzini * @bh: The bottom half to be canceled. 151737e150eSPaolo Bonzini */ 152737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh); 153737e150eSPaolo Bonzini 154737e150eSPaolo Bonzini /** 155737e150eSPaolo Bonzini *qemu_bh_delete: Cancel execution of a bottom half and free its resources. 156737e150eSPaolo Bonzini * 157737e150eSPaolo Bonzini * Deleting a bottom half frees the memory that was allocated for it by 158737e150eSPaolo Bonzini * qemu_bh_new. It also implies canceling the bottom half if it was 159737e150eSPaolo Bonzini * scheduled. 160737e150eSPaolo Bonzini * 161737e150eSPaolo Bonzini * @bh: The bottom half to be deleted. 162737e150eSPaolo Bonzini */ 163737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh); 164737e150eSPaolo Bonzini 165737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource 166737e150eSPaolo Bonzini * attached to the AioContext. 167737e150eSPaolo Bonzini * 168737e150eSPaolo Bonzini * This is used internally in the implementation of the GSource. 169737e150eSPaolo Bonzini */ 170737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx); 171737e150eSPaolo Bonzini 172737e150eSPaolo Bonzini /* Progress in completing AIO work to occur. This can issue new pending 173737e150eSPaolo Bonzini * aio as a result of executing I/O completion or bh callbacks. 174737e150eSPaolo Bonzini * 175737e150eSPaolo Bonzini * If there is no pending AIO operation or completion (bottom half), 176737e150eSPaolo Bonzini * return false. If there are pending bottom halves, return true. 177737e150eSPaolo Bonzini * 178737e150eSPaolo Bonzini * If there are no pending bottom halves, but there are pending AIO 179737e150eSPaolo Bonzini * operations, it may not be possible to make any progress without 180737e150eSPaolo Bonzini * blocking. If @blocking is true, this function will wait until one 181737e150eSPaolo Bonzini * or more AIO events have completed, to ensure something has moved 182737e150eSPaolo Bonzini * before returning. 183737e150eSPaolo Bonzini * 184737e150eSPaolo Bonzini * If @blocking is false, this function will also return false if the 185737e150eSPaolo Bonzini * function cannot make any progress without blocking. 186737e150eSPaolo Bonzini */ 187737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking); 188737e150eSPaolo Bonzini 189737e150eSPaolo Bonzini #ifdef CONFIG_POSIX 190737e150eSPaolo Bonzini /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */ 191737e150eSPaolo Bonzini typedef int (AioFlushHandler)(void *opaque); 192737e150eSPaolo Bonzini 193737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks. Behaves very similarly 194737e150eSPaolo Bonzini * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will 195737e150eSPaolo Bonzini * be invoked when using qemu_aio_wait(). 196737e150eSPaolo Bonzini * 197737e150eSPaolo Bonzini * Code that invokes AIO completion functions should rely on this function 198737e150eSPaolo Bonzini * instead of qemu_set_fd_handler[2]. 199737e150eSPaolo Bonzini */ 200737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx, 201737e150eSPaolo Bonzini int fd, 202737e150eSPaolo Bonzini IOHandler *io_read, 203737e150eSPaolo Bonzini IOHandler *io_write, 204737e150eSPaolo Bonzini AioFlushHandler *io_flush, 205737e150eSPaolo Bonzini void *opaque); 206737e150eSPaolo Bonzini #endif 207737e150eSPaolo Bonzini 208737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks. Behaves very similarly 209737e150eSPaolo Bonzini * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks 210737e150eSPaolo Bonzini * will be invoked when using qemu_aio_wait(). 211737e150eSPaolo Bonzini * 212737e150eSPaolo Bonzini * Code that invokes AIO completion functions should rely on this function 213737e150eSPaolo Bonzini * instead of event_notifier_set_handler. 214737e150eSPaolo Bonzini */ 215737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx, 216737e150eSPaolo Bonzini EventNotifier *notifier, 217737e150eSPaolo Bonzini EventNotifierHandler *io_read, 218737e150eSPaolo Bonzini AioFlushEventNotifierHandler *io_flush); 219737e150eSPaolo Bonzini 220737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached 221737e150eSPaolo Bonzini * to this AioContext. 222737e150eSPaolo Bonzini */ 223737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx); 224737e150eSPaolo Bonzini 225737e150eSPaolo Bonzini /* Functions to operate on the main QEMU AioContext. */ 226737e150eSPaolo Bonzini 227737e150eSPaolo Bonzini bool qemu_aio_wait(void); 228737e150eSPaolo Bonzini void qemu_aio_set_event_notifier(EventNotifier *notifier, 229737e150eSPaolo Bonzini EventNotifierHandler *io_read, 230737e150eSPaolo Bonzini AioFlushEventNotifierHandler *io_flush); 231737e150eSPaolo Bonzini 232737e150eSPaolo Bonzini #ifdef CONFIG_POSIX 233737e150eSPaolo Bonzini void qemu_aio_set_fd_handler(int fd, 234737e150eSPaolo Bonzini IOHandler *io_read, 235737e150eSPaolo Bonzini IOHandler *io_write, 236737e150eSPaolo Bonzini AioFlushHandler *io_flush, 237737e150eSPaolo Bonzini void *opaque); 238737e150eSPaolo Bonzini #endif 239737e150eSPaolo Bonzini 240737e150eSPaolo Bonzini #endif 241