]>
Commit | Line | Data |
---|---|---|
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 | */ | |
24 | #include "qemu-common.h" | |
1de7afc9 | 25 | #include "qemu/timer.h" |
737e150e | 26 | #include "block/block_int.h" |
1de7afc9 | 27 | #include "qemu/module.h" |
737e150e | 28 | #include "block/aio.h" |
a2736526 | 29 | #include "raw-aio.h" |
1de7afc9 | 30 | #include "qemu/event_notifier.h" |
3249dbe6 | 31 | #include "qemu/iov.h" |
a2736526 PB |
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 | }; | |
44 | ||
45 | typedef struct QEMUWin32AIOCB { | |
46 | BlockDriverAIOCB common; | |
47 | struct QEMUWin32AIOState *ctx; | |
48 | int nbytes; | |
49 | OVERLAPPED ov; | |
50 | QEMUIOVector *qiov; | |
51 | void *buf; | |
52 | bool is_read; | |
53 | bool is_linear; | |
54 | } QEMUWin32AIOCB; | |
55 | ||
56 | /* | |
57 | * Completes an AIO request (calls the callback and frees the ACB). | |
58 | */ | |
59 | static void win32_aio_process_completion(QEMUWin32AIOState *s, | |
60 | QEMUWin32AIOCB *waiocb, DWORD count) | |
61 | { | |
62 | int ret; | |
63 | s->count--; | |
64 | ||
65 | if (waiocb->ov.Internal != 0) { | |
66 | ret = -EIO; | |
67 | } else { | |
68 | ret = 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); | |
74 | } else { | |
75 | ret = -EINVAL; | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | if (!waiocb->is_linear) { | |
81 | if (ret == 0 && waiocb->is_read) { | |
82 | QEMUIOVector *qiov = waiocb->qiov; | |
3249dbe6 | 83 | iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size); |
a2736526 | 84 | } |
e8bccad5 | 85 | qemu_vfree(waiocb->buf); |
a2736526 PB |
86 | } |
87 | ||
88 | ||
89 | waiocb->common.cb(waiocb->common.opaque, ret); | |
90 | qemu_aio_release(waiocb); | |
91 | } | |
92 | ||
93 | static void win32_aio_completion_cb(EventNotifier *e) | |
94 | { | |
95 | QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e); | |
96 | DWORD count; | |
97 | ULONG_PTR key; | |
98 | OVERLAPPED *ov; | |
99 | ||
100 | event_notifier_test_and_clear(&s->e); | |
101 | while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) { | |
102 | QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov); | |
103 | ||
104 | win32_aio_process_completion(s, waiocb, count); | |
105 | } | |
106 | } | |
107 | ||
a2736526 PB |
108 | static void win32_aio_cancel(BlockDriverAIOCB *blockacb) |
109 | { | |
110 | QEMUWin32AIOCB *waiocb = (QEMUWin32AIOCB *)blockacb; | |
111 | ||
112 | /* | |
113 | * CancelIoEx is only supported in Vista and newer. For now, just | |
114 | * wait for completion. | |
115 | */ | |
116 | while (!HasOverlappedIoCompleted(&waiocb->ov)) { | |
117 | qemu_aio_wait(); | |
118 | } | |
119 | } | |
120 | ||
d7331bed | 121 | static const AIOCBInfo win32_aiocb_info = { |
a2736526 PB |
122 | .aiocb_size = sizeof(QEMUWin32AIOCB), |
123 | .cancel = win32_aio_cancel, | |
124 | }; | |
125 | ||
126 | BlockDriverAIOCB *win32_aio_submit(BlockDriverState *bs, | |
127 | QEMUWin32AIOState *aio, HANDLE hfile, | |
128 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
129 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
130 | { | |
131 | struct QEMUWin32AIOCB *waiocb; | |
132 | uint64_t offset = sector_num * 512; | |
133 | DWORD rc; | |
134 | ||
d7331bed | 135 | waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque); |
a2736526 PB |
136 | waiocb->nbytes = nb_sectors * 512; |
137 | waiocb->qiov = qiov; | |
138 | waiocb->is_read = (type == QEMU_AIO_READ); | |
139 | ||
140 | if (qiov->niov > 1) { | |
141 | waiocb->buf = qemu_blockalign(bs, qiov->size); | |
142 | if (type & QEMU_AIO_WRITE) { | |
3249dbe6 | 143 | iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size); |
a2736526 PB |
144 | } |
145 | waiocb->is_linear = false; | |
146 | } else { | |
147 | waiocb->buf = qiov->iov[0].iov_base; | |
148 | waiocb->is_linear = true; | |
149 | } | |
150 | ||
cee40d2d SW |
151 | memset(&waiocb->ov, 0, sizeof(waiocb->ov)); |
152 | waiocb->ov.Offset = (DWORD)offset; | |
153 | waiocb->ov.OffsetHigh = (DWORD)(offset >> 32); | |
154 | waiocb->ov.hEvent = event_notifier_get_handle(&aio->e); | |
155 | ||
a2736526 PB |
156 | aio->count++; |
157 | ||
158 | if (type & QEMU_AIO_READ) { | |
159 | rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov); | |
160 | } else { | |
161 | rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov); | |
162 | } | |
163 | if(rc == 0 && GetLastError() != ERROR_IO_PENDING) { | |
164 | goto out_dec_count; | |
165 | } | |
166 | return &waiocb->common; | |
167 | ||
168 | out_dec_count: | |
169 | aio->count--; | |
170 | qemu_aio_release(waiocb); | |
171 | return NULL; | |
172 | } | |
173 | ||
174 | int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile) | |
175 | { | |
176 | if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) { | |
177 | return -EINVAL; | |
178 | } else { | |
179 | return 0; | |
180 | } | |
181 | } | |
182 | ||
183 | QEMUWin32AIOState *win32_aio_init(void) | |
184 | { | |
185 | QEMUWin32AIOState *s; | |
186 | ||
187 | s = g_malloc0(sizeof(*s)); | |
188 | if (event_notifier_init(&s->e, false) < 0) { | |
189 | goto out_free_state; | |
190 | } | |
191 | ||
192 | s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); | |
193 | if (s->hIOCP == NULL) { | |
194 | goto out_close_efd; | |
195 | } | |
196 | ||
b10577df | 197 | qemu_aio_set_event_notifier(&s->e, win32_aio_completion_cb); |
a2736526 PB |
198 | |
199 | return s; | |
200 | ||
201 | out_close_efd: | |
202 | event_notifier_cleanup(&s->e); | |
203 | out_free_state: | |
204 | g_free(s); | |
205 | return NULL; | |
206 | } |