Statistics
| Branch: | Revision:

root / block-raw-win32.c @ 2acf5af0

History | View | Annotate | Download (13.4 kB)

1 223d4670 ths
/*
2 223d4670 ths
 * Block driver for RAW files (win32)
3 223d4670 ths
 *
4 223d4670 ths
 * Copyright (c) 2006 Fabrice Bellard
5 223d4670 ths
 *
6 223d4670 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 223d4670 ths
 * of this software and associated documentation files (the "Software"), to deal
8 223d4670 ths
 * in the Software without restriction, including without limitation the rights
9 223d4670 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 223d4670 ths
 * copies of the Software, and to permit persons to whom the Software is
11 223d4670 ths
 * furnished to do so, subject to the following conditions:
12 223d4670 ths
 *
13 223d4670 ths
 * The above copyright notice and this permission notice shall be included in
14 223d4670 ths
 * all copies or substantial portions of the Software.
15 223d4670 ths
 *
16 223d4670 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 223d4670 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 223d4670 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 223d4670 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 223d4670 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 223d4670 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 223d4670 ths
 * THE SOFTWARE.
23 223d4670 ths
 */
24 223d4670 ths
#include "qemu-common.h"
25 223d4670 ths
#include "qemu-timer.h"
26 223d4670 ths
#include "block_int.h"
27 223d4670 ths
#include <assert.h>
28 223d4670 ths
#include <winioctl.h>
29 223d4670 ths
30 03ff3ca3 aliguori
//#define WIN32_AIO
31 03ff3ca3 aliguori
32 223d4670 ths
#define FTYPE_FILE 0
33 223d4670 ths
#define FTYPE_CD     1
34 223d4670 ths
#define FTYPE_HARDDISK 2
35 223d4670 ths
36 223d4670 ths
typedef struct BDRVRawState {
37 223d4670 ths
    HANDLE hfile;
38 223d4670 ths
    int type;
39 223d4670 ths
    char drive_path[16]; /* format: "d:\" */
40 223d4670 ths
} BDRVRawState;
41 223d4670 ths
42 223d4670 ths
typedef struct RawAIOCB {
43 223d4670 ths
    BlockDriverAIOCB common;
44 223d4670 ths
    HANDLE hEvent;
45 223d4670 ths
    OVERLAPPED ov;
46 223d4670 ths
    int count;
47 223d4670 ths
} RawAIOCB;
48 223d4670 ths
49 223d4670 ths
int qemu_ftruncate64(int fd, int64_t length)
50 223d4670 ths
{
51 223d4670 ths
    LARGE_INTEGER li;
52 223d4670 ths
    LONG high;
53 223d4670 ths
    HANDLE h;
54 223d4670 ths
    BOOL res;
55 223d4670 ths
56 223d4670 ths
    if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
57 223d4670 ths
        return -1;
58 223d4670 ths
59 223d4670 ths
    h = (HANDLE)_get_osfhandle(fd);
60 223d4670 ths
61 223d4670 ths
    /* get current position, ftruncate do not change position */
62 223d4670 ths
    li.HighPart = 0;
63 223d4670 ths
    li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
64 223d4670 ths
    if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
65 223d4670 ths
        return -1;
66 223d4670 ths
67 223d4670 ths
    high = length >> 32;
68 223d4670 ths
    if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
69 223d4670 ths
        return -1;
70 223d4670 ths
    res = SetEndOfFile(h);
71 223d4670 ths
72 223d4670 ths
    /* back to old position */
73 223d4670 ths
    SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
74 223d4670 ths
    return res ? 0 : -1;
75 223d4670 ths
}
76 223d4670 ths
77 223d4670 ths
static int set_sparse(int fd)
78 223d4670 ths
{
79 223d4670 ths
    DWORD returned;
80 223d4670 ths
    return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
81 223d4670 ths
                                 NULL, 0, NULL, 0, &returned, NULL);
82 223d4670 ths
}
83 223d4670 ths
84 223d4670 ths
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
85 223d4670 ths
{
86 223d4670 ths
    BDRVRawState *s = bs->opaque;
87 223d4670 ths
    int access_flags, create_flags;
88 223d4670 ths
    DWORD overlapped;
89 223d4670 ths
90 223d4670 ths
    s->type = FTYPE_FILE;
91 223d4670 ths
92 223d4670 ths
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
93 223d4670 ths
        access_flags = GENERIC_READ | GENERIC_WRITE;
94 223d4670 ths
    } else {
95 223d4670 ths
        access_flags = GENERIC_READ;
96 223d4670 ths
    }
97 223d4670 ths
    if (flags & BDRV_O_CREAT) {
98 223d4670 ths
        create_flags = CREATE_ALWAYS;
99 223d4670 ths
    } else {
100 223d4670 ths
        create_flags = OPEN_EXISTING;
101 223d4670 ths
    }
102 03ff3ca3 aliguori
#ifdef WIN32_AIO
103 223d4670 ths
    overlapped = FILE_FLAG_OVERLAPPED;
104 03ff3ca3 aliguori
#else
105 03ff3ca3 aliguori
    overlapped = FILE_ATTRIBUTE_NORMAL;
106 223d4670 ths
#endif
107 33f00271 balrog
    if (flags & BDRV_O_DIRECT)
108 33f00271 balrog
        overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
109 223d4670 ths
    s->hfile = CreateFile(filename, access_flags,
110 223d4670 ths
                          FILE_SHARE_READ, NULL,
111 223d4670 ths
                          create_flags, overlapped, NULL);
112 223d4670 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
113 223d4670 ths
        int err = GetLastError();
114 223d4670 ths
115 223d4670 ths
        if (err == ERROR_ACCESS_DENIED)
116 223d4670 ths
            return -EACCES;
117 223d4670 ths
        return -1;
118 223d4670 ths
    }
119 223d4670 ths
    return 0;
120 223d4670 ths
}
121 223d4670 ths
122 223d4670 ths
static int raw_pread(BlockDriverState *bs, int64_t offset,
123 223d4670 ths
                     uint8_t *buf, int count)
124 223d4670 ths
{
125 223d4670 ths
    BDRVRawState *s = bs->opaque;
126 223d4670 ths
    OVERLAPPED ov;
127 223d4670 ths
    DWORD ret_count;
128 223d4670 ths
    int ret;
129 223d4670 ths
130 223d4670 ths
    memset(&ov, 0, sizeof(ov));
131 223d4670 ths
    ov.Offset = offset;
132 223d4670 ths
    ov.OffsetHigh = offset >> 32;
133 223d4670 ths
    ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
134 223d4670 ths
    if (!ret) {
135 03ff3ca3 aliguori
#ifdef WIN32_AIO
136 223d4670 ths
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
137 223d4670 ths
        if (!ret)
138 223d4670 ths
            return -EIO;
139 223d4670 ths
        else
140 03ff3ca3 aliguori
#endif
141 223d4670 ths
            return ret_count;
142 223d4670 ths
    }
143 223d4670 ths
    return ret_count;
144 223d4670 ths
}
145 223d4670 ths
146 223d4670 ths
static int raw_pwrite(BlockDriverState *bs, int64_t offset,
147 223d4670 ths
                      const uint8_t *buf, int count)
148 223d4670 ths
{
149 223d4670 ths
    BDRVRawState *s = bs->opaque;
150 223d4670 ths
    OVERLAPPED ov;
151 223d4670 ths
    DWORD ret_count;
152 223d4670 ths
    int ret;
153 223d4670 ths
154 223d4670 ths
    memset(&ov, 0, sizeof(ov));
155 223d4670 ths
    ov.Offset = offset;
156 223d4670 ths
    ov.OffsetHigh = offset >> 32;
157 223d4670 ths
    ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
158 223d4670 ths
    if (!ret) {
159 03ff3ca3 aliguori
#ifdef WIN32_AIO
160 223d4670 ths
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
161 223d4670 ths
        if (!ret)
162 223d4670 ths
            return -EIO;
163 223d4670 ths
        else
164 03ff3ca3 aliguori
#endif
165 223d4670 ths
            return ret_count;
166 223d4670 ths
    }
167 223d4670 ths
    return ret_count;
168 223d4670 ths
}
169 223d4670 ths
170 03ff3ca3 aliguori
#ifdef WIN32_AIO
171 223d4670 ths
static void raw_aio_cb(void *opaque)
172 223d4670 ths
{
173 223d4670 ths
    RawAIOCB *acb = opaque;
174 223d4670 ths
    BlockDriverState *bs = acb->common.bs;
175 223d4670 ths
    BDRVRawState *s = bs->opaque;
176 223d4670 ths
    DWORD ret_count;
177 223d4670 ths
    int ret;
178 223d4670 ths
179 223d4670 ths
    ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
180 223d4670 ths
    if (!ret || ret_count != acb->count) {
181 223d4670 ths
        acb->common.cb(acb->common.opaque, -EIO);
182 223d4670 ths
    } else {
183 223d4670 ths
        acb->common.cb(acb->common.opaque, 0);
184 223d4670 ths
    }
185 223d4670 ths
}
186 223d4670 ths
187 223d4670 ths
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
188 223d4670 ths
        int64_t sector_num, uint8_t *buf, int nb_sectors,
189 223d4670 ths
        BlockDriverCompletionFunc *cb, void *opaque)
190 223d4670 ths
{
191 223d4670 ths
    RawAIOCB *acb;
192 223d4670 ths
    int64_t offset;
193 223d4670 ths
194 223d4670 ths
    acb = qemu_aio_get(bs, cb, opaque);
195 223d4670 ths
    if (acb->hEvent) {
196 223d4670 ths
        acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
197 223d4670 ths
        if (!acb->hEvent) {
198 223d4670 ths
            qemu_aio_release(acb);
199 223d4670 ths
            return NULL;
200 223d4670 ths
        }
201 223d4670 ths
    }
202 223d4670 ths
    memset(&acb->ov, 0, sizeof(acb->ov));
203 223d4670 ths
    offset = sector_num * 512;
204 223d4670 ths
    acb->ov.Offset = offset;
205 223d4670 ths
    acb->ov.OffsetHigh = offset >> 32;
206 223d4670 ths
    acb->ov.hEvent = acb->hEvent;
207 223d4670 ths
    acb->count = nb_sectors * 512;
208 223d4670 ths
    qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
209 223d4670 ths
    return acb;
210 223d4670 ths
}
211 223d4670 ths
212 223d4670 ths
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
213 223d4670 ths
        int64_t sector_num, uint8_t *buf, int nb_sectors,
214 223d4670 ths
        BlockDriverCompletionFunc *cb, void *opaque)
215 223d4670 ths
{
216 223d4670 ths
    BDRVRawState *s = bs->opaque;
217 223d4670 ths
    RawAIOCB *acb;
218 223d4670 ths
    int ret;
219 223d4670 ths
220 223d4670 ths
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
221 223d4670 ths
    if (!acb)
222 223d4670 ths
        return NULL;
223 223d4670 ths
    ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
224 223d4670 ths
    if (!ret) {
225 223d4670 ths
        qemu_aio_release(acb);
226 223d4670 ths
        return NULL;
227 223d4670 ths
    }
228 223d4670 ths
    qemu_aio_release(acb);
229 223d4670 ths
    return (BlockDriverAIOCB *)acb;
230 223d4670 ths
}
231 223d4670 ths
232 223d4670 ths
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
233 223d4670 ths
        int64_t sector_num, uint8_t *buf, int nb_sectors,
234 223d4670 ths
        BlockDriverCompletionFunc *cb, void *opaque)
235 223d4670 ths
{
236 223d4670 ths
    BDRVRawState *s = bs->opaque;
237 223d4670 ths
    RawAIOCB *acb;
238 223d4670 ths
    int ret;
239 223d4670 ths
240 223d4670 ths
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
241 223d4670 ths
    if (!acb)
242 223d4670 ths
        return NULL;
243 223d4670 ths
    ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
244 223d4670 ths
    if (!ret) {
245 223d4670 ths
        qemu_aio_release(acb);
246 223d4670 ths
        return NULL;
247 223d4670 ths
    }
248 223d4670 ths
    qemu_aio_release(acb);
249 223d4670 ths
    return (BlockDriverAIOCB *)acb;
250 223d4670 ths
}
251 223d4670 ths
252 223d4670 ths
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
253 223d4670 ths
{
254 223d4670 ths
    RawAIOCB *acb = (RawAIOCB *)blockacb;
255 223d4670 ths
    BlockDriverState *bs = acb->common.bs;
256 223d4670 ths
    BDRVRawState *s = bs->opaque;
257 223d4670 ths
258 223d4670 ths
    qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
259 223d4670 ths
    /* XXX: if more than one async I/O it is not correct */
260 223d4670 ths
    CancelIo(s->hfile);
261 223d4670 ths
    qemu_aio_release(acb);
262 223d4670 ths
}
263 03ff3ca3 aliguori
#endif /* #if WIN32_AIO */
264 223d4670 ths
265 223d4670 ths
static void raw_flush(BlockDriverState *bs)
266 223d4670 ths
{
267 223d4670 ths
    BDRVRawState *s = bs->opaque;
268 223d4670 ths
    FlushFileBuffers(s->hfile);
269 223d4670 ths
}
270 223d4670 ths
271 223d4670 ths
static void raw_close(BlockDriverState *bs)
272 223d4670 ths
{
273 223d4670 ths
    BDRVRawState *s = bs->opaque;
274 223d4670 ths
    CloseHandle(s->hfile);
275 223d4670 ths
}
276 223d4670 ths
277 223d4670 ths
static int raw_truncate(BlockDriverState *bs, int64_t offset)
278 223d4670 ths
{
279 223d4670 ths
    BDRVRawState *s = bs->opaque;
280 223d4670 ths
    DWORD low, high;
281 223d4670 ths
282 223d4670 ths
    low = offset;
283 223d4670 ths
    high = offset >> 32;
284 223d4670 ths
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
285 223d4670 ths
        return -EIO;
286 223d4670 ths
    if (!SetEndOfFile(s->hfile))
287 223d4670 ths
        return -EIO;
288 223d4670 ths
    return 0;
289 223d4670 ths
}
290 223d4670 ths
291 223d4670 ths
static int64_t raw_getlength(BlockDriverState *bs)
292 223d4670 ths
{
293 223d4670 ths
    BDRVRawState *s = bs->opaque;
294 223d4670 ths
    LARGE_INTEGER l;
295 223d4670 ths
    ULARGE_INTEGER available, total, total_free;
296 223d4670 ths
    DISK_GEOMETRY_EX dg;
297 223d4670 ths
    DWORD count;
298 223d4670 ths
    BOOL status;
299 223d4670 ths
300 223d4670 ths
    switch(s->type) {
301 223d4670 ths
    case FTYPE_FILE:
302 223d4670 ths
        l.LowPart = GetFileSize(s->hfile, &l.HighPart);
303 223d4670 ths
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
304 223d4670 ths
            return -EIO;
305 223d4670 ths
        break;
306 223d4670 ths
    case FTYPE_CD:
307 223d4670 ths
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
308 223d4670 ths
            return -EIO;
309 223d4670 ths
        l.QuadPart = total.QuadPart;
310 223d4670 ths
        break;
311 223d4670 ths
    case FTYPE_HARDDISK:
312 223d4670 ths
        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
313 223d4670 ths
                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
314 223d4670 ths
        if (status != 0) {
315 223d4670 ths
            l = dg.DiskSize;
316 223d4670 ths
        }
317 223d4670 ths
        break;
318 223d4670 ths
    default:
319 223d4670 ths
        return -EIO;
320 223d4670 ths
    }
321 223d4670 ths
    return l.QuadPart;
322 223d4670 ths
}
323 223d4670 ths
324 223d4670 ths
static int raw_create(const char *filename, int64_t total_size,
325 223d4670 ths
                      const char *backing_file, int flags)
326 223d4670 ths
{
327 223d4670 ths
    int fd;
328 223d4670 ths
329 223d4670 ths
    if (flags || backing_file)
330 223d4670 ths
        return -ENOTSUP;
331 223d4670 ths
332 223d4670 ths
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
333 223d4670 ths
              0644);
334 223d4670 ths
    if (fd < 0)
335 223d4670 ths
        return -EIO;
336 223d4670 ths
    set_sparse(fd);
337 223d4670 ths
    ftruncate(fd, total_size * 512);
338 223d4670 ths
    close(fd);
339 223d4670 ths
    return 0;
340 223d4670 ths
}
341 223d4670 ths
342 223d4670 ths
void qemu_aio_init(void)
343 223d4670 ths
{
344 223d4670 ths
}
345 223d4670 ths
346 223d4670 ths
void qemu_aio_flush(void)
347 223d4670 ths
{
348 223d4670 ths
}
349 223d4670 ths
350 223d4670 ths
void qemu_aio_wait(void)
351 223d4670 ths
{
352 223d4670 ths
    qemu_bh_poll();
353 223d4670 ths
}
354 223d4670 ths
355 223d4670 ths
BlockDriver bdrv_raw = {
356 223d4670 ths
    "raw",
357 223d4670 ths
    sizeof(BDRVRawState),
358 223d4670 ths
    NULL, /* no probe for protocols */
359 223d4670 ths
    raw_open,
360 223d4670 ths
    NULL,
361 223d4670 ths
    NULL,
362 223d4670 ths
    raw_close,
363 223d4670 ths
    raw_create,
364 223d4670 ths
    raw_flush,
365 223d4670 ths
366 03ff3ca3 aliguori
#ifdef WIN32_AIO
367 223d4670 ths
    .bdrv_aio_read = raw_aio_read,
368 223d4670 ths
    .bdrv_aio_write = raw_aio_write,
369 223d4670 ths
    .bdrv_aio_cancel = raw_aio_cancel,
370 223d4670 ths
    .aiocb_size = sizeof(RawAIOCB);
371 223d4670 ths
#endif
372 223d4670 ths
    .bdrv_pread = raw_pread,
373 223d4670 ths
    .bdrv_pwrite = raw_pwrite,
374 223d4670 ths
    .bdrv_truncate = raw_truncate,
375 223d4670 ths
    .bdrv_getlength = raw_getlength,
376 223d4670 ths
};
377 223d4670 ths
378 223d4670 ths
/***********************************************/
379 223d4670 ths
/* host device */
380 223d4670 ths
381 223d4670 ths
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
382 223d4670 ths
{
383 223d4670 ths
    char drives[256], *pdrv = drives;
384 223d4670 ths
    UINT type;
385 223d4670 ths
386 223d4670 ths
    memset(drives, 0, sizeof(drives));
387 223d4670 ths
    GetLogicalDriveStrings(sizeof(drives), drives);
388 223d4670 ths
    while(pdrv[0] != '\0') {
389 223d4670 ths
        type = GetDriveType(pdrv);
390 223d4670 ths
        switch(type) {
391 223d4670 ths
        case DRIVE_CDROM:
392 223d4670 ths
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
393 223d4670 ths
            return 0;
394 223d4670 ths
            break;
395 223d4670 ths
        }
396 223d4670 ths
        pdrv += lstrlen(pdrv) + 1;
397 223d4670 ths
    }
398 223d4670 ths
    return -1;
399 223d4670 ths
}
400 223d4670 ths
401 223d4670 ths
static int find_device_type(BlockDriverState *bs, const char *filename)
402 223d4670 ths
{
403 223d4670 ths
    BDRVRawState *s = bs->opaque;
404 223d4670 ths
    UINT type;
405 223d4670 ths
    const char *p;
406 223d4670 ths
407 223d4670 ths
    if (strstart(filename, "\\\\.\\", &p) ||
408 223d4670 ths
        strstart(filename, "//./", &p)) {
409 223d4670 ths
        if (stristart(p, "PhysicalDrive", NULL))
410 223d4670 ths
            return FTYPE_HARDDISK;
411 223d4670 ths
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
412 223d4670 ths
        type = GetDriveType(s->drive_path);
413 223d4670 ths
        if (type == DRIVE_CDROM)
414 223d4670 ths
            return FTYPE_CD;
415 223d4670 ths
        else
416 223d4670 ths
            return FTYPE_FILE;
417 223d4670 ths
    } else {
418 223d4670 ths
        return FTYPE_FILE;
419 223d4670 ths
    }
420 223d4670 ths
}
421 223d4670 ths
422 223d4670 ths
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
423 223d4670 ths
{
424 223d4670 ths
    BDRVRawState *s = bs->opaque;
425 223d4670 ths
    int access_flags, create_flags;
426 223d4670 ths
    DWORD overlapped;
427 223d4670 ths
    char device_name[64];
428 223d4670 ths
429 223d4670 ths
    if (strstart(filename, "/dev/cdrom", NULL)) {
430 223d4670 ths
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
431 223d4670 ths
            return -ENOENT;
432 223d4670 ths
        filename = device_name;
433 223d4670 ths
    } else {
434 223d4670 ths
        /* transform drive letters into device name */
435 223d4670 ths
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
436 223d4670 ths
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
437 223d4670 ths
            filename[1] == ':' && filename[2] == '\0') {
438 223d4670 ths
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
439 223d4670 ths
            filename = device_name;
440 223d4670 ths
        }
441 223d4670 ths
    }
442 223d4670 ths
    s->type = find_device_type(bs, filename);
443 223d4670 ths
444 223d4670 ths
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
445 223d4670 ths
        access_flags = GENERIC_READ | GENERIC_WRITE;
446 223d4670 ths
    } else {
447 223d4670 ths
        access_flags = GENERIC_READ;
448 223d4670 ths
    }
449 223d4670 ths
    create_flags = OPEN_EXISTING;
450 223d4670 ths
451 03ff3ca3 aliguori
#ifdef WIN32_AIO
452 223d4670 ths
    overlapped = FILE_FLAG_OVERLAPPED;
453 03ff3ca3 aliguori
#else
454 03ff3ca3 aliguori
    overlapped = FILE_ATTRIBUTE_NORMAL;
455 223d4670 ths
#endif
456 33f00271 balrog
    if (flags & BDRV_O_DIRECT)
457 33f00271 balrog
        overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
458 223d4670 ths
    s->hfile = CreateFile(filename, access_flags,
459 223d4670 ths
                          FILE_SHARE_READ, NULL,
460 223d4670 ths
                          create_flags, overlapped, NULL);
461 223d4670 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
462 223d4670 ths
        int err = GetLastError();
463 223d4670 ths
464 223d4670 ths
        if (err == ERROR_ACCESS_DENIED)
465 223d4670 ths
            return -EACCES;
466 223d4670 ths
        return -1;
467 223d4670 ths
    }
468 223d4670 ths
    return 0;
469 223d4670 ths
}
470 223d4670 ths
471 223d4670 ths
#if 0
472 223d4670 ths
/***********************************************/
473 223d4670 ths
/* removable device additional commands */
474 223d4670 ths

475 223d4670 ths
static int raw_is_inserted(BlockDriverState *bs)
476 223d4670 ths
{
477 223d4670 ths
    return 1;
478 223d4670 ths
}
479 223d4670 ths

480 223d4670 ths
static int raw_media_changed(BlockDriverState *bs)
481 223d4670 ths
{
482 223d4670 ths
    return -ENOTSUP;
483 223d4670 ths
}
484 223d4670 ths

485 223d4670 ths
static int raw_eject(BlockDriverState *bs, int eject_flag)
486 223d4670 ths
{
487 223d4670 ths
    DWORD ret_count;
488 223d4670 ths

489 223d4670 ths
    if (s->type == FTYPE_FILE)
490 223d4670 ths
        return -ENOTSUP;
491 223d4670 ths
    if (eject_flag) {
492 223d4670 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
493 223d4670 ths
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
494 223d4670 ths
    } else {
495 223d4670 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
496 223d4670 ths
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
497 223d4670 ths
    }
498 223d4670 ths
}
499 223d4670 ths

500 223d4670 ths
static int raw_set_locked(BlockDriverState *bs, int locked)
501 223d4670 ths
{
502 223d4670 ths
    return -ENOTSUP;
503 223d4670 ths
}
504 223d4670 ths
#endif
505 223d4670 ths
506 223d4670 ths
BlockDriver bdrv_host_device = {
507 223d4670 ths
    "host_device",
508 223d4670 ths
    sizeof(BDRVRawState),
509 223d4670 ths
    NULL, /* no probe for protocols */
510 223d4670 ths
    hdev_open,
511 223d4670 ths
    NULL,
512 223d4670 ths
    NULL,
513 223d4670 ths
    raw_close,
514 223d4670 ths
    NULL,
515 223d4670 ths
    raw_flush,
516 223d4670 ths
517 03ff3ca3 aliguori
#ifdef WIN32_AIO
518 223d4670 ths
    .bdrv_aio_read = raw_aio_read,
519 223d4670 ths
    .bdrv_aio_write = raw_aio_write,
520 223d4670 ths
    .bdrv_aio_cancel = raw_aio_cancel,
521 223d4670 ths
    .aiocb_size = sizeof(RawAIOCB);
522 223d4670 ths
#endif
523 223d4670 ths
    .bdrv_pread = raw_pread,
524 223d4670 ths
    .bdrv_pwrite = raw_pwrite,
525 223d4670 ths
    .bdrv_getlength = raw_getlength,
526 223d4670 ths
};