xref: /openbmc/qemu/include/block/aio.h (revision dcc772e2f2b7c2a68644133fea2b874f6751a57b)
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"
181de7afc9SPaolo Bonzini #include "qemu/queue.h"
191de7afc9SPaolo Bonzini #include "qemu/event_notifier.h"
20*dcc772e2SLiu Ping Fan #include "qemu/thread.h"
21737e150eSPaolo Bonzini 
22737e150eSPaolo Bonzini typedef struct BlockDriverAIOCB BlockDriverAIOCB;
23737e150eSPaolo Bonzini typedef void BlockDriverCompletionFunc(void *opaque, int ret);
24737e150eSPaolo Bonzini 
25737e150eSPaolo Bonzini typedef struct AIOCBInfo {
26737e150eSPaolo Bonzini     void (*cancel)(BlockDriverAIOCB *acb);
27737e150eSPaolo Bonzini     size_t aiocb_size;
28737e150eSPaolo Bonzini } AIOCBInfo;
29737e150eSPaolo Bonzini 
30737e150eSPaolo Bonzini struct BlockDriverAIOCB {
31737e150eSPaolo Bonzini     const AIOCBInfo *aiocb_info;
32737e150eSPaolo Bonzini     BlockDriverState *bs;
33737e150eSPaolo Bonzini     BlockDriverCompletionFunc *cb;
34737e150eSPaolo Bonzini     void *opaque;
35737e150eSPaolo Bonzini };
36737e150eSPaolo Bonzini 
37737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
38737e150eSPaolo Bonzini                    BlockDriverCompletionFunc *cb, void *opaque);
39737e150eSPaolo Bonzini void qemu_aio_release(void *p);
40737e150eSPaolo Bonzini 
41737e150eSPaolo Bonzini typedef struct AioHandler AioHandler;
42737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
43737e150eSPaolo Bonzini typedef void IOHandler(void *opaque);
44737e150eSPaolo Bonzini 
45737e150eSPaolo Bonzini typedef struct AioContext {
46737e150eSPaolo Bonzini     GSource source;
47737e150eSPaolo Bonzini 
48737e150eSPaolo Bonzini     /* The list of registered AIO handlers */
49737e150eSPaolo Bonzini     QLIST_HEAD(, AioHandler) aio_handlers;
50737e150eSPaolo Bonzini 
51737e150eSPaolo Bonzini     /* This is a simple lock used to protect the aio_handlers list.
52737e150eSPaolo Bonzini      * Specifically, it's used to ensure that no callbacks are removed while
53737e150eSPaolo Bonzini      * we're walking and dispatching callbacks.
54737e150eSPaolo Bonzini      */
55737e150eSPaolo Bonzini     int walking_handlers;
56737e150eSPaolo Bonzini 
57*dcc772e2SLiu Ping Fan     /* lock to protect between bh's adders and deleter */
58*dcc772e2SLiu Ping Fan     QemuMutex bh_lock;
59737e150eSPaolo Bonzini     /* Anchor of the list of Bottom Halves belonging to the context */
60737e150eSPaolo Bonzini     struct QEMUBH *first_bh;
61737e150eSPaolo Bonzini 
62737e150eSPaolo Bonzini     /* A simple lock used to protect the first_bh list, and ensure that
63737e150eSPaolo Bonzini      * no callbacks are removed while we're walking and dispatching callbacks.
64737e150eSPaolo Bonzini      */
65737e150eSPaolo Bonzini     int walking_bh;
66737e150eSPaolo Bonzini 
67737e150eSPaolo Bonzini     /* Used for aio_notify.  */
68737e150eSPaolo Bonzini     EventNotifier notifier;
696b5f8762SStefan Hajnoczi 
706b5f8762SStefan Hajnoczi     /* GPollFDs for aio_poll() */
716b5f8762SStefan Hajnoczi     GArray *pollfds;
729b34277dSStefan Hajnoczi 
739b34277dSStefan Hajnoczi     /* Thread pool for performing work and receiving completion callbacks */
749b34277dSStefan Hajnoczi     struct ThreadPool *thread_pool;
75737e150eSPaolo Bonzini } AioContext;
76737e150eSPaolo Bonzini 
77737e150eSPaolo Bonzini /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
78737e150eSPaolo Bonzini typedef int (AioFlushEventNotifierHandler)(EventNotifier *e);
79737e150eSPaolo Bonzini 
80737e150eSPaolo Bonzini /**
81737e150eSPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
82737e150eSPaolo Bonzini  *
83737e150eSPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
84737e150eSPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
85737e150eSPaolo Bonzini  * as soon as possible.
86737e150eSPaolo Bonzini  */
87737e150eSPaolo Bonzini AioContext *aio_context_new(void);
88737e150eSPaolo Bonzini 
89737e150eSPaolo Bonzini /**
90737e150eSPaolo Bonzini  * aio_context_ref:
91737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
92737e150eSPaolo Bonzini  *
93737e150eSPaolo Bonzini  * Add a reference to an AioContext.
94737e150eSPaolo Bonzini  */
95737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx);
96737e150eSPaolo Bonzini 
97737e150eSPaolo Bonzini /**
98737e150eSPaolo Bonzini  * aio_context_unref:
99737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
100737e150eSPaolo Bonzini  *
101737e150eSPaolo Bonzini  * Drop a reference to an AioContext.
102737e150eSPaolo Bonzini  */
103737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx);
104737e150eSPaolo Bonzini 
105737e150eSPaolo Bonzini /**
106737e150eSPaolo Bonzini  * aio_bh_new: Allocate a new bottom half structure.
107737e150eSPaolo Bonzini  *
108737e150eSPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
109737e150eSPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
110737e150eSPaolo Bonzini  * is opaque and must be allocated prior to its use.
111737e150eSPaolo Bonzini  */
112737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
113737e150eSPaolo Bonzini 
114737e150eSPaolo Bonzini /**
115737e150eSPaolo Bonzini  * aio_notify: Force processing of pending events.
116737e150eSPaolo Bonzini  *
117737e150eSPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
118737e150eSPaolo Bonzini  * aio_wait to exit, so that the next call will re-examine pending events.
119737e150eSPaolo Bonzini  * The caller of aio_notify will usually call aio_wait again very soon,
120737e150eSPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
121737e150eSPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
122737e150eSPaolo Bonzini  * that the main loop waits for.
123737e150eSPaolo Bonzini  *
124737e150eSPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
125737e150eSPaolo Bonzini  * a bottom half calls it already.
126737e150eSPaolo Bonzini  */
127737e150eSPaolo Bonzini void aio_notify(AioContext *ctx);
128737e150eSPaolo Bonzini 
129737e150eSPaolo Bonzini /**
130737e150eSPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
131737e150eSPaolo Bonzini  *
132737e150eSPaolo Bonzini  * These are internal functions used by the QEMU main loop.
133*dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
134*dcc772e2SLiu Ping Fan  * be called concurrently
135737e150eSPaolo Bonzini  */
136737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx);
137737e150eSPaolo Bonzini 
138737e150eSPaolo Bonzini /**
139737e150eSPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
140737e150eSPaolo Bonzini  *
141737e150eSPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
142737e150eSPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
143737e150eSPaolo Bonzini  *
144737e150eSPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
145737e150eSPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
146737e150eSPaolo Bonzini  * schedules itself.
147737e150eSPaolo Bonzini  *
148737e150eSPaolo Bonzini  * @bh: The bottom half to be scheduled.
149737e150eSPaolo Bonzini  */
150737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
151737e150eSPaolo Bonzini 
152737e150eSPaolo Bonzini /**
153737e150eSPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
154737e150eSPaolo Bonzini  *
155737e150eSPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
156737e150eSPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
157737e150eSPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
158737e150eSPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
159737e150eSPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
160737e150eSPaolo Bonzini  *
161737e150eSPaolo Bonzini  * @bh: The bottom half to be canceled.
162737e150eSPaolo Bonzini  */
163737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
164737e150eSPaolo Bonzini 
165737e150eSPaolo Bonzini /**
166737e150eSPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
167737e150eSPaolo Bonzini  *
168737e150eSPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
169737e150eSPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
170737e150eSPaolo Bonzini  * scheduled.
171*dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
172*dcc772e2SLiu Ping Fan  * end.
173737e150eSPaolo Bonzini  *
174737e150eSPaolo Bonzini  * @bh: The bottom half to be deleted.
175737e150eSPaolo Bonzini  */
176737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
177737e150eSPaolo Bonzini 
178737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
179737e150eSPaolo Bonzini  * attached to the AioContext.
180737e150eSPaolo Bonzini  *
181737e150eSPaolo Bonzini  * This is used internally in the implementation of the GSource.
182737e150eSPaolo Bonzini  */
183737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx);
184737e150eSPaolo Bonzini 
185737e150eSPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
186737e150eSPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
187737e150eSPaolo Bonzini  *
188737e150eSPaolo Bonzini  * If there is no pending AIO operation or completion (bottom half),
1892ea9b58fSKevin Wolf  * return false.  If there are pending AIO operations of bottom halves,
1902ea9b58fSKevin Wolf  * return true.
191737e150eSPaolo Bonzini  *
192737e150eSPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
193737e150eSPaolo Bonzini  * operations, it may not be possible to make any progress without
194737e150eSPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
195737e150eSPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
196737e150eSPaolo Bonzini  * before returning.
197737e150eSPaolo Bonzini  */
198737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking);
199737e150eSPaolo Bonzini 
200737e150eSPaolo Bonzini #ifdef CONFIG_POSIX
201737e150eSPaolo Bonzini /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
202737e150eSPaolo Bonzini typedef int (AioFlushHandler)(void *opaque);
203737e150eSPaolo Bonzini 
204737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks.  Behaves very similarly
205737e150eSPaolo Bonzini  * to qemu_set_fd_handler2.  Unlike qemu_set_fd_handler2, these callbacks will
206737e150eSPaolo Bonzini  * be invoked when using qemu_aio_wait().
207737e150eSPaolo Bonzini  *
208737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
209737e150eSPaolo Bonzini  * instead of qemu_set_fd_handler[2].
210737e150eSPaolo Bonzini  */
211737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
212737e150eSPaolo Bonzini                         int fd,
213737e150eSPaolo Bonzini                         IOHandler *io_read,
214737e150eSPaolo Bonzini                         IOHandler *io_write,
215737e150eSPaolo Bonzini                         AioFlushHandler *io_flush,
216737e150eSPaolo Bonzini                         void *opaque);
217737e150eSPaolo Bonzini #endif
218737e150eSPaolo Bonzini 
219737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
220737e150eSPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
221737e150eSPaolo Bonzini  * will be invoked when using qemu_aio_wait().
222737e150eSPaolo Bonzini  *
223737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
224737e150eSPaolo Bonzini  * instead of event_notifier_set_handler.
225737e150eSPaolo Bonzini  */
226737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
227737e150eSPaolo Bonzini                             EventNotifier *notifier,
228737e150eSPaolo Bonzini                             EventNotifierHandler *io_read,
229737e150eSPaolo Bonzini                             AioFlushEventNotifierHandler *io_flush);
230737e150eSPaolo Bonzini 
231737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
232737e150eSPaolo Bonzini  * to this AioContext.
233737e150eSPaolo Bonzini  */
234737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
235737e150eSPaolo Bonzini 
2369b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */
2379b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
2389b34277dSStefan Hajnoczi 
239737e150eSPaolo Bonzini /* Functions to operate on the main QEMU AioContext.  */
240737e150eSPaolo Bonzini 
241737e150eSPaolo Bonzini bool qemu_aio_wait(void);
242737e150eSPaolo Bonzini void qemu_aio_set_event_notifier(EventNotifier *notifier,
243737e150eSPaolo Bonzini                                  EventNotifierHandler *io_read,
244737e150eSPaolo Bonzini                                  AioFlushEventNotifierHandler *io_flush);
245737e150eSPaolo Bonzini 
246737e150eSPaolo Bonzini #ifdef CONFIG_POSIX
247737e150eSPaolo Bonzini void qemu_aio_set_fd_handler(int fd,
248737e150eSPaolo Bonzini                              IOHandler *io_read,
249737e150eSPaolo Bonzini                              IOHandler *io_write,
250737e150eSPaolo Bonzini                              AioFlushHandler *io_flush,
251737e150eSPaolo Bonzini                              void *opaque);
252737e150eSPaolo Bonzini #endif
253737e150eSPaolo Bonzini 
254737e150eSPaolo Bonzini #endif
255