]> git.proxmox.com Git - mirror_qemu.git/blob - block/win32-aio.c
Merge remote-tracking branch 'remotes/stefanha/tags/net-pull-request' into staging
[mirror_qemu.git] / block / win32-aio.c
1 /*
2 * Block driver for RAW files (win32)
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #include "qemu-common.h"
25 #include "qemu/timer.h"
26 #include "block/block_int.h"
27 #include "qemu/module.h"
28 #include "block/aio.h"
29 #include "raw-aio.h"
30 #include "qemu/event_notifier.h"
31 #include "qemu/iov.h"
32 #include <windows.h>
33 #include <winioctl.h>
34
35 #define FTYPE_FILE 0
36 #define FTYPE_CD 1
37 #define FTYPE_HARDDISK 2
38
39 struct QEMUWin32AIOState {
40 HANDLE hIOCP;
41 EventNotifier e;
42 int count;
43 bool is_aio_context_attached;
44 };
45
46 typedef struct QEMUWin32AIOCB {
47 BlockDriverAIOCB common;
48 struct QEMUWin32AIOState *ctx;
49 int nbytes;
50 OVERLAPPED ov;
51 QEMUIOVector *qiov;
52 void *buf;
53 bool is_read;
54 bool is_linear;
55 } QEMUWin32AIOCB;
56
57 /*
58 * Completes an AIO request (calls the callback and frees the ACB).
59 */
60 static void win32_aio_process_completion(QEMUWin32AIOState *s,
61 QEMUWin32AIOCB *waiocb, DWORD count)
62 {
63 int ret;
64 s->count--;
65
66 if (waiocb->ov.Internal != 0) {
67 ret = -EIO;
68 } else {
69 ret = 0;
70 if (count < waiocb->nbytes) {
71 /* Short reads mean EOF, pad with zeros. */
72 if (waiocb->is_read) {
73 qemu_iovec_memset(waiocb->qiov, count, 0,
74 waiocb->qiov->size - count);
75 } else {
76 ret = -EINVAL;
77 }
78 }
79 }
80
81 if (!waiocb->is_linear) {
82 if (ret == 0 && waiocb->is_read) {
83 QEMUIOVector *qiov = waiocb->qiov;
84 iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
85 }
86 qemu_vfree(waiocb->buf);
87 }
88
89
90 waiocb->common.cb(waiocb->common.opaque, ret);
91 qemu_aio_release(waiocb);
92 }
93
94 static void win32_aio_completion_cb(EventNotifier *e)
95 {
96 QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e);
97 DWORD count;
98 ULONG_PTR key;
99 OVERLAPPED *ov;
100
101 event_notifier_test_and_clear(&s->e);
102 while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) {
103 QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov);
104
105 win32_aio_process_completion(s, waiocb, count);
106 }
107 }
108
109 static void win32_aio_cancel(BlockDriverAIOCB *blockacb)
110 {
111 QEMUWin32AIOCB *waiocb = (QEMUWin32AIOCB *)blockacb;
112
113 /*
114 * CancelIoEx is only supported in Vista and newer. For now, just
115 * wait for completion.
116 */
117 while (!HasOverlappedIoCompleted(&waiocb->ov)) {
118 aio_poll(bdrv_get_aio_context(blockacb->bs), true);
119 }
120 }
121
122 static const AIOCBInfo win32_aiocb_info = {
123 .aiocb_size = sizeof(QEMUWin32AIOCB),
124 .cancel = win32_aio_cancel,
125 };
126
127 BlockDriverAIOCB *win32_aio_submit(BlockDriverState *bs,
128 QEMUWin32AIOState *aio, HANDLE hfile,
129 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
130 BlockDriverCompletionFunc *cb, void *opaque, int type)
131 {
132 struct QEMUWin32AIOCB *waiocb;
133 uint64_t offset = sector_num * 512;
134 DWORD rc;
135
136 waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
137 waiocb->nbytes = nb_sectors * 512;
138 waiocb->qiov = qiov;
139 waiocb->is_read = (type == QEMU_AIO_READ);
140
141 if (qiov->niov > 1) {
142 waiocb->buf = qemu_blockalign(bs, qiov->size);
143 if (type & QEMU_AIO_WRITE) {
144 iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
145 }
146 waiocb->is_linear = false;
147 } else {
148 waiocb->buf = qiov->iov[0].iov_base;
149 waiocb->is_linear = true;
150 }
151
152 memset(&waiocb->ov, 0, sizeof(waiocb->ov));
153 waiocb->ov.Offset = (DWORD)offset;
154 waiocb->ov.OffsetHigh = (DWORD)(offset >> 32);
155 waiocb->ov.hEvent = event_notifier_get_handle(&aio->e);
156
157 aio->count++;
158
159 if (type & QEMU_AIO_READ) {
160 rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
161 } else {
162 rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
163 }
164 if(rc == 0 && GetLastError() != ERROR_IO_PENDING) {
165 goto out_dec_count;
166 }
167 return &waiocb->common;
168
169 out_dec_count:
170 aio->count--;
171 qemu_aio_release(waiocb);
172 return NULL;
173 }
174
175 int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
176 {
177 if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) {
178 return -EINVAL;
179 } else {
180 return 0;
181 }
182 }
183
184 void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
185 AioContext *old_context)
186 {
187 aio_set_event_notifier(old_context, &aio->e, NULL);
188 aio->is_aio_context_attached = false;
189 }
190
191 void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
192 AioContext *new_context)
193 {
194 aio->is_aio_context_attached = true;
195 aio_set_event_notifier(new_context, &aio->e, win32_aio_completion_cb);
196 }
197
198 QEMUWin32AIOState *win32_aio_init(void)
199 {
200 QEMUWin32AIOState *s;
201
202 s = g_malloc0(sizeof(*s));
203 if (event_notifier_init(&s->e, false) < 0) {
204 goto out_free_state;
205 }
206
207 s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
208 if (s->hIOCP == NULL) {
209 goto out_close_efd;
210 }
211
212 return s;
213
214 out_close_efd:
215 event_notifier_cleanup(&s->e);
216 out_free_state:
217 g_free(s);
218 return NULL;
219 }
220
221 void win32_aio_cleanup(QEMUWin32AIOState *aio)
222 {
223 assert(!aio->is_aio_context_attached);
224 CloseHandle(aio->hIOCP);
225 event_notifier_cleanup(&aio->e);
226 g_free(aio);
227 }