]> git.proxmox.com Git - mirror_qemu.git/blame - block/win32-aio.c
CODING_STYLE: specify the indent rule for multiline code
[mirror_qemu.git] / block / win32-aio.c
CommitLineData
a2736526
PB
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 */
80c71a24 24#include "qemu/osdep.h"
a2736526 25#include "qemu-common.h"
1de7afc9 26#include "qemu/timer.h"
737e150e 27#include "block/block_int.h"
1de7afc9 28#include "qemu/module.h"
737e150e 29#include "block/aio.h"
0187f5c9 30#include "block/raw-aio.h"
1de7afc9 31#include "qemu/event_notifier.h"
3249dbe6 32#include "qemu/iov.h"
a2736526
PB
33#include <windows.h>
34#include <winioctl.h>
35
36#define FTYPE_FILE 0
37#define FTYPE_CD 1
38#define FTYPE_HARDDISK 2
39
40struct QEMUWin32AIOState {
41 HANDLE hIOCP;
42 EventNotifier e;
43 int count;
9d456654 44 AioContext *aio_ctx;
a2736526
PB
45};
46
47typedef struct QEMUWin32AIOCB {
7c84b1b8 48 BlockAIOCB common;
a2736526
PB
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;
57
58/*
59 * Completes an AIO request (calls the callback and frees the ACB).
60 */
61static void win32_aio_process_completion(QEMUWin32AIOState *s,
62 QEMUWin32AIOCB *waiocb, DWORD count)
63{
64 int ret;
65 s->count--;
66
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;
78 }
79 }
80 }
81
82 if (!waiocb->is_linear) {
83 if (ret == 0 && waiocb->is_read) {
84 QEMUIOVector *qiov = waiocb->qiov;
3249dbe6 85 iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
a2736526 86 }
e8bccad5 87 qemu_vfree(waiocb->buf);
a2736526
PB
88 }
89
a2736526 90 waiocb->common.cb(waiocb->common.opaque, ret);
8007429a 91 qemu_aio_unref(waiocb);
a2736526
PB
92}
93
94static 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
d7331bed 109static const AIOCBInfo win32_aiocb_info = {
a2736526 110 .aiocb_size = sizeof(QEMUWin32AIOCB),
a2736526
PB
111};
112
7c84b1b8 113BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
a2736526 114 QEMUWin32AIOState *aio, HANDLE hfile,
de7056a3 115 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
097310b5 116 BlockCompletionFunc *cb, void *opaque, int type)
a2736526
PB
117{
118 struct QEMUWin32AIOCB *waiocb;
a2736526
PB
119 DWORD rc;
120
d7331bed 121 waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
de7056a3 122 waiocb->nbytes = bytes;
a2736526
PB
123 waiocb->qiov = qiov;
124 waiocb->is_read = (type == QEMU_AIO_READ);
125
126 if (qiov->niov > 1) {
4b6af3d5
KW
127 waiocb->buf = qemu_try_blockalign(bs, qiov->size);
128 if (waiocb->buf == NULL) {
129 goto out;
130 }
a2736526 131 if (type & QEMU_AIO_WRITE) {
3249dbe6 132 iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
a2736526
PB
133 }
134 waiocb->is_linear = false;
135 } else {
136 waiocb->buf = qiov->iov[0].iov_base;
137 waiocb->is_linear = true;
138 }
139
cee40d2d
SW
140 memset(&waiocb->ov, 0, sizeof(waiocb->ov));
141 waiocb->ov.Offset = (DWORD)offset;
142 waiocb->ov.OffsetHigh = (DWORD)(offset >> 32);
143 waiocb->ov.hEvent = event_notifier_get_handle(&aio->e);
144
a2736526
PB
145 aio->count++;
146
147 if (type & QEMU_AIO_READ) {
148 rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
149 } else {
150 rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
151 }
152 if(rc == 0 && GetLastError() != ERROR_IO_PENDING) {
153 goto out_dec_count;
154 }
155 return &waiocb->common;
156
157out_dec_count:
158 aio->count--;
4b6af3d5 159out:
8007429a 160 qemu_aio_unref(waiocb);
a2736526
PB
161 return NULL;
162}
163
164int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
165{
166 if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) {
167 return -EINVAL;
168 } else {
169 return 0;
170 }
171}
172
85ebd381
SH
173void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
174 AioContext *old_context)
175{
f6a51c84 176 aio_set_event_notifier(old_context, &aio->e, false, NULL, NULL);
9d456654 177 aio->aio_ctx = NULL;
85ebd381
SH
178}
179
180void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
181 AioContext *new_context)
182{
9d456654 183 aio->aio_ctx = new_context;
dca21ef2 184 aio_set_event_notifier(new_context, &aio->e, false,
f6a51c84 185 win32_aio_completion_cb, NULL);
85ebd381
SH
186}
187
a2736526
PB
188QEMUWin32AIOState *win32_aio_init(void)
189{
190 QEMUWin32AIOState *s;
191
192 s = g_malloc0(sizeof(*s));
193 if (event_notifier_init(&s->e, false) < 0) {
194 goto out_free_state;
195 }
196
197 s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
198 if (s->hIOCP == NULL) {
199 goto out_close_efd;
200 }
201
a2736526
PB
202 return s;
203
204out_close_efd:
205 event_notifier_cleanup(&s->e);
206out_free_state:
207 g_free(s);
208 return NULL;
209}
99cc5989
SH
210
211void win32_aio_cleanup(QEMUWin32AIOState *aio)
212{
9d456654 213 assert(!aio->aio_ctx);
99cc5989
SH
214 CloseHandle(aio->hIOCP);
215 event_notifier_cleanup(&aio->e);
216 g_free(aio);
217}