Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 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 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 26 | #include "qemu/timer.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 27 | #include "block/block_int.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 28 | #include "block/aio.h" |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 29 | #include "block/raw-aio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/event_notifier.h" |
Michael Tokarev | 3249dbe | 2013-01-17 18:47:52 +0400 | [diff] [blame] | 31 | #include "qemu/iov.h" |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 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; |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 43 | AioContext *aio_ctx; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | typedef struct QEMUWin32AIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 47 | BlockAIOCB common; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 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; |
Michael Tokarev | 3249dbe | 2013-01-17 18:47:52 +0400 | [diff] [blame] | 84 | iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 85 | } |
Kevin Wolf | e8bccad | 2013-01-16 21:20:00 +0100 | [diff] [blame] | 86 | qemu_vfree(waiocb->buf); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 89 | waiocb->common.cb(waiocb->common.opaque, ret); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 90 | qemu_aio_unref(waiocb); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 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 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 108 | static const AIOCBInfo win32_aiocb_info = { |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 109 | .aiocb_size = sizeof(QEMUWin32AIOCB), |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 112 | BlockAIOCB *win32_aio_submit(BlockDriverState *bs, |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 113 | QEMUWin32AIOState *aio, HANDLE hfile, |
Eric Blake | de7056a | 2018-04-24 14:25:02 -0500 | [diff] [blame] | 114 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 115 | BlockCompletionFunc *cb, void *opaque, int type) |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 116 | { |
| 117 | struct QEMUWin32AIOCB *waiocb; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 118 | DWORD rc; |
| 119 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 120 | waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque); |
Eric Blake | de7056a | 2018-04-24 14:25:02 -0500 | [diff] [blame] | 121 | waiocb->nbytes = bytes; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 122 | waiocb->qiov = qiov; |
| 123 | waiocb->is_read = (type == QEMU_AIO_READ); |
| 124 | |
| 125 | if (qiov->niov > 1) { |
Kevin Wolf | 4b6af3d | 2014-05-21 18:05:47 +0200 | [diff] [blame] | 126 | waiocb->buf = qemu_try_blockalign(bs, qiov->size); |
| 127 | if (waiocb->buf == NULL) { |
| 128 | goto out; |
| 129 | } |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 130 | if (type & QEMU_AIO_WRITE) { |
Michael Tokarev | 3249dbe | 2013-01-17 18:47:52 +0400 | [diff] [blame] | 131 | iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 132 | } |
| 133 | waiocb->is_linear = false; |
| 134 | } else { |
| 135 | waiocb->buf = qiov->iov[0].iov_base; |
| 136 | waiocb->is_linear = true; |
| 137 | } |
| 138 | |
Stefan Weil | cee40d2 | 2012-11-04 12:09:34 +0100 | [diff] [blame] | 139 | memset(&waiocb->ov, 0, sizeof(waiocb->ov)); |
| 140 | waiocb->ov.Offset = (DWORD)offset; |
| 141 | waiocb->ov.OffsetHigh = (DWORD)(offset >> 32); |
| 142 | waiocb->ov.hEvent = event_notifier_get_handle(&aio->e); |
| 143 | |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 144 | aio->count++; |
| 145 | |
| 146 | if (type & QEMU_AIO_READ) { |
| 147 | rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov); |
| 148 | } else { |
| 149 | rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov); |
| 150 | } |
| 151 | if(rc == 0 && GetLastError() != ERROR_IO_PENDING) { |
| 152 | goto out_dec_count; |
| 153 | } |
| 154 | return &waiocb->common; |
| 155 | |
| 156 | out_dec_count: |
| 157 | aio->count--; |
Kevin Wolf | 4b6af3d | 2014-05-21 18:05:47 +0200 | [diff] [blame] | 158 | out: |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 159 | qemu_aio_unref(waiocb); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile) |
| 164 | { |
| 165 | if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) { |
| 166 | return -EINVAL; |
| 167 | } else { |
| 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 172 | void win32_aio_detach_aio_context(QEMUWin32AIOState *aio, |
| 173 | AioContext *old_context) |
| 174 | { |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 175 | aio_set_event_notifier(old_context, &aio->e, false, NULL, NULL); |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 176 | aio->aio_ctx = NULL; |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void win32_aio_attach_aio_context(QEMUWin32AIOState *aio, |
| 180 | AioContext *new_context) |
| 181 | { |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 182 | aio->aio_ctx = new_context; |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 183 | aio_set_event_notifier(new_context, &aio->e, false, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 184 | win32_aio_completion_cb, NULL); |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 187 | QEMUWin32AIOState *win32_aio_init(void) |
| 188 | { |
| 189 | QEMUWin32AIOState *s; |
| 190 | |
| 191 | s = g_malloc0(sizeof(*s)); |
| 192 | if (event_notifier_init(&s->e, false) < 0) { |
| 193 | goto out_free_state; |
| 194 | } |
| 195 | |
| 196 | s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); |
| 197 | if (s->hIOCP == NULL) { |
| 198 | goto out_close_efd; |
| 199 | } |
| 200 | |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 201 | return s; |
| 202 | |
| 203 | out_close_efd: |
| 204 | event_notifier_cleanup(&s->e); |
| 205 | out_free_state: |
| 206 | g_free(s); |
| 207 | return NULL; |
| 208 | } |
Stefan Hajnoczi | 99cc598 | 2014-05-08 16:34:49 +0200 | [diff] [blame] | 209 | |
| 210 | void win32_aio_cleanup(QEMUWin32AIOState *aio) |
| 211 | { |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 212 | assert(!aio->aio_ctx); |
Stefan Hajnoczi | 99cc598 | 2014-05-08 16:34:49 +0200 | [diff] [blame] | 213 | CloseHandle(aio->hIOCP); |
| 214 | event_notifier_cleanup(&aio->e); |
| 215 | g_free(aio); |
| 216 | } |