i386: populate floppy drive information in DSDT
[qemu.git] / block / win32-aio.c
blob2d509a9a7bbfe38d99afd448c2b074e3e8b98718
1 /*
2 * Block driver for RAW files (win32)
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "qemu/timer.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "block/aio.h"
30 #include "raw-aio.h"
31 #include "qemu/event_notifier.h"
32 #include "qemu/iov.h"
33 #include <windows.h>
34 #include <winioctl.h>
36 #define FTYPE_FILE 0
37 #define FTYPE_CD 1
38 #define FTYPE_HARDDISK 2
40 struct QEMUWin32AIOState {
41 HANDLE hIOCP;
42 EventNotifier e;
43 int count;
44 bool is_aio_context_attached;
47 typedef struct QEMUWin32AIOCB {
48 BlockAIOCB common;
49 struct QEMUWin32AIOState *ctx;
50 int nbytes;
51 OVERLAPPED ov;
52 QEMUIOVector *qiov;
53 void *buf;
54 bool is_read;
55 bool is_linear;
56 } QEMUWin32AIOCB;
59 * Completes an AIO request (calls the callback and frees the ACB).
61 static void win32_aio_process_completion(QEMUWin32AIOState *s,
62 QEMUWin32AIOCB *waiocb, DWORD count)
64 int ret;
65 s->count--;
67 if (waiocb->ov.Internal != 0) {
68 ret = -EIO;
69 } else {
70 ret = 0;
71 if (count < waiocb->nbytes) {
72 /* Short reads mean EOF, pad with zeros. */
73 if (waiocb->is_read) {
74 qemu_iovec_memset(waiocb->qiov, count, 0,
75 waiocb->qiov->size - count);
76 } else {
77 ret = -EINVAL;
82 if (!waiocb->is_linear) {
83 if (ret == 0 && waiocb->is_read) {
84 QEMUIOVector *qiov = waiocb->qiov;
85 iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
87 qemu_vfree(waiocb->buf);
91 waiocb->common.cb(waiocb->common.opaque, ret);
92 qemu_aio_unref(waiocb);
95 static void win32_aio_completion_cb(EventNotifier *e)
97 QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e);
98 DWORD count;
99 ULONG_PTR key;
100 OVERLAPPED *ov;
102 event_notifier_test_and_clear(&s->e);
103 while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) {
104 QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov);
106 win32_aio_process_completion(s, waiocb, count);
110 static const AIOCBInfo win32_aiocb_info = {
111 .aiocb_size = sizeof(QEMUWin32AIOCB),
114 BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
115 QEMUWin32AIOState *aio, HANDLE hfile,
116 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
117 BlockCompletionFunc *cb, void *opaque, int type)
119 struct QEMUWin32AIOCB *waiocb;
120 uint64_t offset = sector_num * 512;
121 DWORD rc;
123 waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
124 waiocb->nbytes = nb_sectors * 512;
125 waiocb->qiov = qiov;
126 waiocb->is_read = (type == QEMU_AIO_READ);
128 if (qiov->niov > 1) {
129 waiocb->buf = qemu_try_blockalign(bs, qiov->size);
130 if (waiocb->buf == NULL) {
131 goto out;
133 if (type & QEMU_AIO_WRITE) {
134 iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
136 waiocb->is_linear = false;
137 } else {
138 waiocb->buf = qiov->iov[0].iov_base;
139 waiocb->is_linear = true;
142 memset(&waiocb->ov, 0, sizeof(waiocb->ov));
143 waiocb->ov.Offset = (DWORD)offset;
144 waiocb->ov.OffsetHigh = (DWORD)(offset >> 32);
145 waiocb->ov.hEvent = event_notifier_get_handle(&aio->e);
147 aio->count++;
149 if (type & QEMU_AIO_READ) {
150 rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
151 } else {
152 rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
154 if(rc == 0 && GetLastError() != ERROR_IO_PENDING) {
155 goto out_dec_count;
157 return &waiocb->common;
159 out_dec_count:
160 aio->count--;
161 out:
162 qemu_aio_unref(waiocb);
163 return NULL;
166 int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
168 if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) {
169 return -EINVAL;
170 } else {
171 return 0;
175 void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
176 AioContext *old_context)
178 aio_set_event_notifier(old_context, &aio->e, false, NULL);
179 aio->is_aio_context_attached = false;
182 void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
183 AioContext *new_context)
185 aio->is_aio_context_attached = true;
186 aio_set_event_notifier(new_context, &aio->e, false,
187 win32_aio_completion_cb);
190 QEMUWin32AIOState *win32_aio_init(void)
192 QEMUWin32AIOState *s;
194 s = g_malloc0(sizeof(*s));
195 if (event_notifier_init(&s->e, false) < 0) {
196 goto out_free_state;
199 s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
200 if (s->hIOCP == NULL) {
201 goto out_close_efd;
204 return s;
206 out_close_efd:
207 event_notifier_cleanup(&s->e);
208 out_free_state:
209 g_free(s);
210 return NULL;
213 void win32_aio_cleanup(QEMUWin32AIOState *aio)
215 assert(!aio->is_aio_context_attached);
216 CloseHandle(aio->hIOCP);
217 event_notifier_cleanup(&aio->e);
218 g_free(aio);