| 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 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qemu/timer.h" |
| 27 | #include "block/block-io.h" |
| 28 | #include "block/block_int.h" |
| 29 | #include "qemu/aio.h" |
| 30 | #include "block/raw-aio.h" |
| 31 | #include "qemu/aiocb.h" |
| 32 | #include "qemu/event_notifier.h" |
| 33 | #include "qemu/iov.h" |
| 34 | #include "qemu/memalign.h" |
| 35 | #include <windows.h> |
| 36 | #include <winioctl.h> |
| 37 | |
| 38 | #define FTYPE_FILE 0 |
| 39 | #define FTYPE_CD 1 |
| 40 | #define FTYPE_HARDDISK 2 |
| 41 | |
| 42 | struct QEMUWin32AIOState { |
| 43 | HANDLE hIOCP; |
| 44 | EventNotifier e; |
| 45 | int count; |
| 46 | AioContext *aio_ctx; |
| 47 | }; |
| 48 | |
| 49 | typedef struct QEMUWin32AIOCB { |
| 50 | BlockAIOCB common; |
| 51 | struct QEMUWin32AIOState *ctx; |
| 52 | AioContext *req_ctx; |
| 53 | int nbytes; |
| 54 | OVERLAPPED ov; |
| 55 | QEMUIOVector *qiov; |
| 56 | void *buf; |
| 57 | bool is_read; |
| 58 | bool is_linear; |
| 59 | int ret; |
| 60 | } QEMUWin32AIOCB; |
| 61 | |
| 62 | static void win32_aio_completion_cb_bh(void *opaque) |
| 63 | { |
| 64 | QEMUWin32AIOCB *waiocb = opaque; |
| 65 | |
| 66 | waiocb->common.cb(waiocb->common.opaque, waiocb->ret); |
| 67 | aio_context_unref(waiocb->req_ctx); |
| 68 | qemu_aio_unref(waiocb); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Completes an AIO request (calls the callback and frees the ACB). |
| 73 | */ |
| 74 | static void win32_aio_process_completion(QEMUWin32AIOState *s, |
| 75 | QEMUWin32AIOCB *waiocb, DWORD count) |
| 76 | { |
| 77 | s->count--; |
| 78 | |
| 79 | if (waiocb->ov.Internal != 0) { |
| 80 | waiocb->ret = -EIO; |
| 81 | } else { |
| 82 | waiocb->ret = 0; |
| 83 | if (count < waiocb->nbytes) { |
| 84 | /* Short reads mean EOF, pad with zeros. */ |
| 85 | if (waiocb->is_read) { |
| 86 | qemu_iovec_memset(waiocb->qiov, count, 0, |
| 87 | waiocb->qiov->size - count); |
| 88 | } else { |
| 89 | waiocb->ret = -EINVAL; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (!waiocb->is_linear) { |
| 95 | if (waiocb->ret == 0 && waiocb->is_read) { |
| 96 | QEMUIOVector *qiov = waiocb->qiov; |
| 97 | iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size); |
| 98 | } |
| 99 | qemu_vfree(waiocb->buf); |
| 100 | } |
| 101 | |
| 102 | if (waiocb->req_ctx == s->aio_ctx) { |
| 103 | win32_aio_completion_cb_bh(waiocb); |
| 104 | } else { |
| 105 | aio_bh_schedule_oneshot(waiocb->req_ctx, win32_aio_completion_cb_bh, |
| 106 | waiocb); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void win32_aio_completion_cb(EventNotifier *e) |
| 111 | { |
| 112 | QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e); |
| 113 | DWORD count; |
| 114 | ULONG_PTR key; |
| 115 | OVERLAPPED *ov; |
| 116 | |
| 117 | event_notifier_test_and_clear(&s->e); |
| 118 | while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) { |
| 119 | QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov); |
| 120 | |
| 121 | win32_aio_process_completion(s, waiocb, count); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static const AIOCBInfo win32_aiocb_info = { |
| 126 | .aiocb_size = sizeof(QEMUWin32AIOCB), |
| 127 | }; |
| 128 | |
| 129 | BlockAIOCB *win32_aio_submit(BlockDriverState *bs, |
| 130 | QEMUWin32AIOState *aio, HANDLE hfile, |
| 131 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, |
| 132 | BlockCompletionFunc *cb, void *opaque, int type) |
| 133 | { |
| 134 | struct QEMUWin32AIOCB *waiocb; |
| 135 | DWORD rc; |
| 136 | |
| 137 | waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque); |
| 138 | waiocb->req_ctx = qemu_get_current_aio_context(); |
| 139 | waiocb->nbytes = bytes; |
| 140 | waiocb->qiov = qiov; |
| 141 | waiocb->is_read = (type == QEMU_AIO_READ); |
| 142 | |
| 143 | aio_context_ref(waiocb->req_ctx); |
| 144 | |
| 145 | if (qiov->niov > 1) { |
| 146 | waiocb->buf = qemu_try_blockalign(bs, qiov->size); |
| 147 | if (waiocb->buf == NULL) { |
| 148 | goto out; |
| 149 | } |
| 150 | if (type & QEMU_AIO_WRITE) { |
| 151 | iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size); |
| 152 | } |
| 153 | waiocb->is_linear = false; |
| 154 | } else { |
| 155 | waiocb->buf = qiov->iov[0].iov_base; |
| 156 | waiocb->is_linear = true; |
| 157 | } |
| 158 | |
| 159 | memset(&waiocb->ov, 0, sizeof(waiocb->ov)); |
| 160 | waiocb->ov.Offset = (DWORD)offset; |
| 161 | waiocb->ov.OffsetHigh = (DWORD)(offset >> 32); |
| 162 | waiocb->ov.hEvent = event_notifier_get_handle(&aio->e); |
| 163 | |
| 164 | aio->count++; |
| 165 | |
| 166 | if (type & QEMU_AIO_READ) { |
| 167 | rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov); |
| 168 | } else { |
| 169 | rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov); |
| 170 | } |
| 171 | if(rc == 0 && GetLastError() != ERROR_IO_PENDING) { |
| 172 | goto out_dec_count; |
| 173 | } |
| 174 | return &waiocb->common; |
| 175 | |
| 176 | out_dec_count: |
| 177 | aio->count--; |
| 178 | out: |
| 179 | qemu_aio_unref(waiocb); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile) |
| 184 | { |
| 185 | if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) { |
| 186 | return -EINVAL; |
| 187 | } else { |
| 188 | return 0; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void win32_aio_detach_aio_context(QEMUWin32AIOState *aio, |
| 193 | AioContext *old_context) |
| 194 | { |
| 195 | aio_set_event_notifier(old_context, &aio->e, NULL, NULL, NULL); |
| 196 | aio->aio_ctx = NULL; |
| 197 | } |
| 198 | |
| 199 | void win32_aio_attach_aio_context(QEMUWin32AIOState *aio, |
| 200 | AioContext *new_context) |
| 201 | { |
| 202 | aio->aio_ctx = new_context; |
| 203 | aio_set_event_notifier(new_context, &aio->e, win32_aio_completion_cb, |
| 204 | NULL, NULL); |
| 205 | } |
| 206 | |
| 207 | QEMUWin32AIOState *win32_aio_init(void) |
| 208 | { |
| 209 | QEMUWin32AIOState *s; |
| 210 | |
| 211 | s = g_malloc0(sizeof(*s)); |
| 212 | if (event_notifier_init(&s->e, false) < 0) { |
| 213 | goto out_free_state; |
| 214 | } |
| 215 | |
| 216 | s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); |
| 217 | if (s->hIOCP == NULL) { |
| 218 | goto out_close_efd; |
| 219 | } |
| 220 | |
| 221 | return s; |
| 222 | |
| 223 | out_close_efd: |
| 224 | event_notifier_cleanup(&s->e); |
| 225 | out_free_state: |
| 226 | g_free(s); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | void win32_aio_cleanup(QEMUWin32AIOState *aio) |
| 231 | { |
| 232 | assert(!aio->aio_ctx); |
| 233 | CloseHandle(aio->hIOCP); |
| 234 | event_notifier_cleanup(&aio->e); |
| 235 | g_free(aio); |
| 236 | } |