Revision a2736526

b/block/Makefile.objs
3 3
block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
4 4
block-obj-y += qed-check.o
5 5
block-obj-y += parallels.o blkdebug.o blkverify.o
6
block-obj-$(CONFIG_WIN32) += raw-win32.o
6
block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
7 7
block-obj-$(CONFIG_POSIX) += raw-posix.o
8 8
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
9 9

  
b/block/raw-aio.h
35 35
        BlockDriverCompletionFunc *cb, void *opaque, int type);
36 36
#endif
37 37

  
38
#ifdef _WIN32
39
typedef struct QEMUWin32AIOState QEMUWin32AIOState;
40
QEMUWin32AIOState *win32_aio_init(void);
41
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile);
42
BlockDriverAIOCB *win32_aio_submit(BlockDriverState *bs,
43
        QEMUWin32AIOState *aio, HANDLE hfile,
44
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
45
        BlockDriverCompletionFunc *cb, void *opaque, int type);
46
#endif
47

  
38 48
#endif /* QEMU_RAW_AIO_H */
b/block/raw-win32.c
36 36
#define FTYPE_CD     1
37 37
#define FTYPE_HARDDISK 2
38 38

  
39
static QEMUWin32AIOState *aio;
40

  
39 41
typedef struct RawWin32AIOData {
40 42
    BlockDriverState *bs;
41 43
    HANDLE hfile;
......
50 52
    HANDLE hfile;
51 53
    int type;
52 54
    char drive_path[16]; /* format: "d:\" */
55
    QEMUWin32AIOState *aio;
53 56
} BDRVRawState;
54 57

  
55 58
/*
......
208 211
    }
209 212

  
210 213
    *overlapped = FILE_ATTRIBUTE_NORMAL;
214
    if (flags & BDRV_O_NATIVE_AIO) {
215
        *overlapped |= FILE_FLAG_OVERLAPPED;
216
    }
211 217
    if (flags & BDRV_O_NOCACHE) {
212 218
        *overlapped |= FILE_FLAG_NO_BUFFERING;
213 219
    }
......
222 228
    s->type = FTYPE_FILE;
223 229

  
224 230
    raw_parse_flags(flags, &access_flags, &overlapped);
231
    
232
    if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) {
233
        aio = win32_aio_init();
234
        if (aio == NULL) {
235
            return -EINVAL;
236
        }
237
    }
225 238

  
226 239
    s->hfile = CreateFile(filename, access_flags,
227 240
                          FILE_SHARE_READ, NULL,
......
231 244

  
232 245
        if (err == ERROR_ACCESS_DENIED)
233 246
            return -EACCES;
234
        return -1;
247
        return -EINVAL;
248
    }
249

  
250
    if (flags & BDRV_O_NATIVE_AIO) {
251
        int ret = win32_aio_attach(aio, s->hfile);
252
        if (ret < 0) {
253
            CloseHandle(s->hfile);
254
            return ret;
255
        }
256
        s->aio = aio;
235 257
    }
236 258
    return 0;
237 259
}
......
241 263
                         BlockDriverCompletionFunc *cb, void *opaque)
242 264
{
243 265
    BDRVRawState *s = bs->opaque;
244
    return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
245
                       cb, opaque, QEMU_AIO_READ);
266
    if (s->aio) {
267
        return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
268
                                nb_sectors, cb, opaque, QEMU_AIO_READ); 
269
    } else {
270
        return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
271
                           cb, opaque, QEMU_AIO_READ);
272
    }
246 273
}
247 274

  
248 275
static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
......
250 277
                          BlockDriverCompletionFunc *cb, void *opaque)
251 278
{
252 279
    BDRVRawState *s = bs->opaque;
253
    return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
254
                       cb, opaque, QEMU_AIO_WRITE);
280
    if (s->aio) {
281
        return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
282
                                nb_sectors, cb, opaque, QEMU_AIO_WRITE); 
283
    } else {
284
        return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
285
                           cb, opaque, QEMU_AIO_WRITE);
286
    }
255 287
}
256 288

  
257 289
static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
b/block/win32-aio.c
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"
25
#include "qemu-timer.h"
26
#include "block_int.h"
27
#include "module.h"
28
#include "qemu-common.h"
29
#include "qemu-aio.h"
30
#include "raw-aio.h"
31
#include "event_notifier.h"
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;
83
            char *p = waiocb->buf;
84
            int i;
85

  
86
            for (i = 0; i < qiov->niov; ++i) {
87
                memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
88
                p += qiov->iov[i].iov_len;
89
            }
90
            g_free(waiocb->buf);
91
        }
92
    }
93

  
94

  
95
    waiocb->common.cb(waiocb->common.opaque, ret);
96
    qemu_aio_release(waiocb);
97
}
98

  
99
static void win32_aio_completion_cb(EventNotifier *e)
100
{
101
    QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e);
102
    DWORD count;
103
    ULONG_PTR key;
104
    OVERLAPPED *ov;
105

  
106
    event_notifier_test_and_clear(&s->e);
107
    while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) {
108
        QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov);
109

  
110
        win32_aio_process_completion(s, waiocb, count);
111
    }
112
}
113

  
114
static int win32_aio_flush_cb(EventNotifier *e)
115
{
116
    QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e);
117

  
118
    return (s->count > 0) ? 1 : 0;
119
}
120

  
121
static void win32_aio_cancel(BlockDriverAIOCB *blockacb)
122
{
123
    QEMUWin32AIOCB *waiocb = (QEMUWin32AIOCB *)blockacb;
124

  
125
    /*
126
     * CancelIoEx is only supported in Vista and newer.  For now, just
127
     * wait for completion.
128
     */
129
    while (!HasOverlappedIoCompleted(&waiocb->ov)) {
130
        qemu_aio_wait();
131
    }
132
}
133

  
134
static AIOPool win32_aio_pool = {
135
    .aiocb_size         = sizeof(QEMUWin32AIOCB),
136
    .cancel             = win32_aio_cancel,
137
};
138

  
139
BlockDriverAIOCB *win32_aio_submit(BlockDriverState *bs,
140
        QEMUWin32AIOState *aio, HANDLE hfile,
141
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
142
        BlockDriverCompletionFunc *cb, void *opaque, int type)
143
{
144
    struct QEMUWin32AIOCB *waiocb;
145
    uint64_t offset = sector_num * 512;
146
    DWORD rc;
147

  
148
    waiocb = qemu_aio_get(&win32_aio_pool, bs, cb, opaque);
149
    waiocb->nbytes = nb_sectors * 512;
150
    waiocb->qiov = qiov;
151
    waiocb->is_read = (type == QEMU_AIO_READ);
152

  
153
    if (qiov->niov > 1) {
154
        waiocb->buf = qemu_blockalign(bs, qiov->size);
155
        if (type & QEMU_AIO_WRITE) {
156
            char *p = waiocb->buf;
157
            int i;
158

  
159
            for (i = 0; i < qiov->niov; ++i) {
160
                memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
161
                p += qiov->iov[i].iov_len;
162
            }
163
        }
164
        waiocb->is_linear = false;
165
    } else {
166
        waiocb->buf = qiov->iov[0].iov_base;
167
        waiocb->is_linear = true;
168
    }
169

  
170
    waiocb->ov = (OVERLAPPED) {
171
        .Offset = (DWORD) offset,
172
        .OffsetHigh = (DWORD) (offset >> 32),
173
        .hEvent = event_notifier_get_handle(&aio->e)
174
    };
175
    aio->count++;
176

  
177
    if (type & QEMU_AIO_READ) {
178
        rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
179
    } else {
180
        rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
181
    }
182
    if(rc == 0 && GetLastError() != ERROR_IO_PENDING) {
183
        goto out_dec_count;
184
    }
185
    return &waiocb->common;
186

  
187
out_dec_count:
188
    aio->count--;
189
    qemu_aio_release(waiocb);
190
    return NULL;
191
}
192

  
193
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
194
{
195
    if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) {
196
        return -EINVAL;
197
    } else {
198
        return 0;
199
    }
200
}
201

  
202
QEMUWin32AIOState *win32_aio_init(void)
203
{
204
    QEMUWin32AIOState *s;
205

  
206
    s = g_malloc0(sizeof(*s));
207
    if (event_notifier_init(&s->e, false) < 0) {
208
        goto out_free_state;
209
    }
210

  
211
    s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
212
    if (s->hIOCP == NULL) {
213
        goto out_close_efd;
214
    }
215

  
216
    qemu_aio_set_event_notifier(&s->e, win32_aio_completion_cb,
217
                                win32_aio_flush_cb);
218

  
219
    return s;
220

  
221
out_close_efd:
222
    event_notifier_cleanup(&s->e);
223
out_free_state:
224
    g_free(s);
225
    return NULL;
226
}

Also available in: Unified diff