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