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
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "block_int.h"
28 #include "qemu-common.h"
31 #include "event_notifier.h"
37 #define FTYPE_HARDDISK 2
39 struct QEMUWin32AIOState
{
45 typedef struct QEMUWin32AIOCB
{
46 BlockDriverAIOCB common
;
47 struct QEMUWin32AIOState
*ctx
;
57 * Completes an AIO request (calls the callback and frees the ACB).
59 static void win32_aio_process_completion(QEMUWin32AIOState
*s
,
60 QEMUWin32AIOCB
*waiocb
, DWORD count
)
65 if (waiocb
->ov
.Internal
!= 0) {
69 if (count
< waiocb
->nbytes
) {
70 /* Short reads mean EOF, pad with zeros. */
71 if (waiocb
->is_read
) {
72 qemu_iovec_memset(waiocb
->qiov
, count
, 0,
73 waiocb
->qiov
->size
- count
);
80 if (!waiocb
->is_linear
) {
81 if (ret
== 0 && waiocb
->is_read
) {
82 QEMUIOVector
*qiov
= waiocb
->qiov
;
83 char *p
= waiocb
->buf
;
86 for (i
= 0; i
< qiov
->niov
; ++i
) {
87 memcpy(p
, qiov
->iov
[i
].iov_base
, qiov
->iov
[i
].iov_len
);
88 p
+= qiov
->iov
[i
].iov_len
;
95 waiocb
->common
.cb(waiocb
->common
.opaque
, ret
);
96 qemu_aio_release(waiocb
);
99 static void win32_aio_completion_cb(EventNotifier
*e
)
101 QEMUWin32AIOState
*s
= container_of(e
, QEMUWin32AIOState
, e
);
106 event_notifier_test_and_clear(&s
->e
);
107 while (GetQueuedCompletionStatus(s
->hIOCP
, &count
, &key
, &ov
, 0)) {
108 QEMUWin32AIOCB
*waiocb
= container_of(ov
, QEMUWin32AIOCB
, ov
);
110 win32_aio_process_completion(s
, waiocb
, count
);
114 static int win32_aio_flush_cb(EventNotifier
*e
)
116 QEMUWin32AIOState
*s
= container_of(e
, QEMUWin32AIOState
, e
);
118 return (s
->count
> 0) ? 1 : 0;
121 static void win32_aio_cancel(BlockDriverAIOCB
*blockacb
)
123 QEMUWin32AIOCB
*waiocb
= (QEMUWin32AIOCB
*)blockacb
;
126 * CancelIoEx is only supported in Vista and newer. For now, just
127 * wait for completion.
129 while (!HasOverlappedIoCompleted(&waiocb
->ov
)) {
134 static AIOPool win32_aio_pool
= {
135 .aiocb_size
= sizeof(QEMUWin32AIOCB
),
136 .cancel
= win32_aio_cancel
,
139 BlockDriverAIOCB
*win32_aio_submit(BlockDriverState
*bs
,
140 QEMUWin32AIOState
*aio
, HANDLE hfile
,
141 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
142 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
144 struct QEMUWin32AIOCB
*waiocb
;
145 uint64_t offset
= sector_num
* 512;
148 waiocb
= qemu_aio_get(&win32_aio_pool
, bs
, cb
, opaque
);
149 waiocb
->nbytes
= nb_sectors
* 512;
151 waiocb
->is_read
= (type
== QEMU_AIO_READ
);
153 if (qiov
->niov
> 1) {
154 waiocb
->buf
= qemu_blockalign(bs
, qiov
->size
);
155 if (type
& QEMU_AIO_WRITE
) {
156 char *p
= waiocb
->buf
;
159 for (i
= 0; i
< qiov
->niov
; ++i
) {
160 memcpy(p
, qiov
->iov
[i
].iov_base
, qiov
->iov
[i
].iov_len
);
161 p
+= qiov
->iov
[i
].iov_len
;
164 waiocb
->is_linear
= false;
166 waiocb
->buf
= qiov
->iov
[0].iov_base
;
167 waiocb
->is_linear
= true;
170 waiocb
->ov
= (OVERLAPPED
) {
171 .Offset
= (DWORD
) offset
,
172 .OffsetHigh
= (DWORD
) (offset
>> 32),
173 .hEvent
= event_notifier_get_handle(&aio
->e
)
177 if (type
& QEMU_AIO_READ
) {
178 rc
= ReadFile(hfile
, waiocb
->buf
, waiocb
->nbytes
, NULL
, &waiocb
->ov
);
180 rc
= WriteFile(hfile
, waiocb
->buf
, waiocb
->nbytes
, NULL
, &waiocb
->ov
);
182 if(rc
== 0 && GetLastError() != ERROR_IO_PENDING
) {
185 return &waiocb
->common
;
189 qemu_aio_release(waiocb
);
193 int win32_aio_attach(QEMUWin32AIOState
*aio
, HANDLE hfile
)
195 if (CreateIoCompletionPort(hfile
, aio
->hIOCP
, (ULONG_PTR
) 0, 0) == NULL
) {
202 QEMUWin32AIOState
*win32_aio_init(void)
204 QEMUWin32AIOState
*s
;
206 s
= g_malloc0(sizeof(*s
));
207 if (event_notifier_init(&s
->e
, false) < 0) {
211 s
->hIOCP
= CreateIoCompletionPort(INVALID_HANDLE_VALUE
, NULL
, 0, 0);
212 if (s
->hIOCP
== NULL
) {
216 qemu_aio_set_event_notifier(&s
->e
, win32_aio_completion_cb
,
222 event_notifier_cleanup(&s
->e
);