blob: 6327861e1d22995f1efe68c746bfde8c0a8601e7 [file] [log] [blame]
Paolo Bonzinia2736522012-10-26 11:43:58 +02001/*
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 Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/timer.h"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010027#include "block/block-io.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010029#include "block/aio.h"
Paolo Bonzini0187f5c2016-07-04 18:33:20 +020030#include "block/raw-aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/event_notifier.h"
Michael Tokarev3249dbe2013-01-17 18:47:52 +040032#include "qemu/iov.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000033#include "qemu/memalign.h"
Paolo Bonzinia2736522012-10-26 11:43:58 +020034#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;
Paolo Bonzini9d456652017-02-13 14:52:30 +010045 AioContext *aio_ctx;
Paolo Bonzinia2736522012-10-26 11:43:58 +020046};
47
48typedef struct QEMUWin32AIOCB {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020049 BlockAIOCB common;
Paolo Bonzinia2736522012-10-26 11:43:58 +020050 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;
Michael Tokarev3249dbe2013-01-17 18:47:52 +040086 iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
Paolo Bonzinia2736522012-10-26 11:43:58 +020087 }
Kevin Wolfe8bccad2013-01-16 21:20:00 +010088 qemu_vfree(waiocb->buf);
Paolo Bonzinia2736522012-10-26 11:43:58 +020089 }
90
Paolo Bonzinia2736522012-10-26 11:43:58 +020091 waiocb->common.cb(waiocb->common.opaque, ret);
Fam Zheng80074292014-09-11 13:41:28 +080092 qemu_aio_unref(waiocb);
Paolo Bonzinia2736522012-10-26 11:43:58 +020093}
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
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100110static const AIOCBInfo win32_aiocb_info = {
Paolo Bonzinia2736522012-10-26 11:43:58 +0200111 .aiocb_size = sizeof(QEMUWin32AIOCB),
Paolo Bonzinia2736522012-10-26 11:43:58 +0200112};
113
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200114BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
Paolo Bonzinia2736522012-10-26 11:43:58 +0200115 QEMUWin32AIOState *aio, HANDLE hfile,
Eric Blakede7056a2018-04-24 14:25:02 -0500116 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
Markus Armbruster097310b2014-10-07 13:59:15 +0200117 BlockCompletionFunc *cb, void *opaque, int type)
Paolo Bonzinia2736522012-10-26 11:43:58 +0200118{
119 struct QEMUWin32AIOCB *waiocb;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200120 DWORD rc;
121
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100122 waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
Eric Blakede7056a2018-04-24 14:25:02 -0500123 waiocb->nbytes = bytes;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200124 waiocb->qiov = qiov;
125 waiocb->is_read = (type == QEMU_AIO_READ);
126
127 if (qiov->niov > 1) {
Kevin Wolf4b6af3d2014-05-21 18:05:47 +0200128 waiocb->buf = qemu_try_blockalign(bs, qiov->size);
129 if (waiocb->buf == NULL) {
130 goto out;
131 }
Paolo Bonzinia2736522012-10-26 11:43:58 +0200132 if (type & QEMU_AIO_WRITE) {
Michael Tokarev3249dbe2013-01-17 18:47:52 +0400133 iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200134 }
135 waiocb->is_linear = false;
136 } else {
137 waiocb->buf = qiov->iov[0].iov_base;
138 waiocb->is_linear = true;
139 }
140
Stefan Weilcee40d22012-11-04 12:09:34 +0100141 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
Paolo Bonzinia2736522012-10-26 11:43:58 +0200146 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--;
Kevin Wolf4b6af3d2014-05-21 18:05:47 +0200160out:
Fam Zheng80074292014-09-11 13:41:28 +0800161 qemu_aio_unref(waiocb);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200162 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
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200174void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
175 AioContext *old_context)
176{
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400177 aio_set_event_notifier(old_context, &aio->e, NULL, NULL, NULL);
Paolo Bonzini9d456652017-02-13 14:52:30 +0100178 aio->aio_ctx = NULL;
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200179}
180
181void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
182 AioContext *new_context)
183{
Paolo Bonzini9d456652017-02-13 14:52:30 +0100184 aio->aio_ctx = new_context;
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400185 aio_set_event_notifier(new_context, &aio->e, win32_aio_completion_cb,
186 NULL, NULL);
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200187}
188
Paolo Bonzinia2736522012-10-26 11:43:58 +0200189QEMUWin32AIOState *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
Paolo Bonzinia2736522012-10-26 11:43:58 +0200203 return s;
204
205out_close_efd:
206 event_notifier_cleanup(&s->e);
207out_free_state:
208 g_free(s);
209 return NULL;
210}
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200211
212void win32_aio_cleanup(QEMUWin32AIOState *aio)
213{
Paolo Bonzini9d456652017-02-13 14:52:30 +0100214 assert(!aio->aio_ctx);
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200215 CloseHandle(aio->hIOCP);
216 event_notifier_cleanup(&aio->e);
217 g_free(aio);
218}