Statistics
| Branch: | Revision:

root / block-raw-win32.c @ 1536ff64

History | View | Annotate | Download (13.5 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 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE))
108 33f00271 balrog
        overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
109 9f7965c7 aliguori
    else if (!(flags & BDRV_O_CACHE_WB))
110 9f7965c7 aliguori
        overlapped |= FILE_FLAG_WRITE_THROUGH;
111 223d4670 ths
    s->hfile = CreateFile(filename, access_flags,
112 223d4670 ths
                          FILE_SHARE_READ, NULL,
113 223d4670 ths
                          create_flags, overlapped, NULL);
114 223d4670 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
115 223d4670 ths
        int err = GetLastError();
116 223d4670 ths
117 223d4670 ths
        if (err == ERROR_ACCESS_DENIED)
118 223d4670 ths
            return -EACCES;
119 223d4670 ths
        return -1;
120 223d4670 ths
    }
121 223d4670 ths
    return 0;
122 223d4670 ths
}
123 223d4670 ths
124 223d4670 ths
static int raw_pread(BlockDriverState *bs, int64_t offset,
125 223d4670 ths
                     uint8_t *buf, int count)
126 223d4670 ths
{
127 223d4670 ths
    BDRVRawState *s = bs->opaque;
128 223d4670 ths
    OVERLAPPED ov;
129 223d4670 ths
    DWORD ret_count;
130 223d4670 ths
    int ret;
131 223d4670 ths
132 223d4670 ths
    memset(&ov, 0, sizeof(ov));
133 223d4670 ths
    ov.Offset = offset;
134 223d4670 ths
    ov.OffsetHigh = offset >> 32;
135 223d4670 ths
    ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
136 223d4670 ths
    if (!ret) {
137 03ff3ca3 aliguori
#ifdef WIN32_AIO
138 223d4670 ths
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
139 223d4670 ths
        if (!ret)
140 223d4670 ths
            return -EIO;
141 223d4670 ths
        else
142 03ff3ca3 aliguori
#endif
143 223d4670 ths
            return ret_count;
144 223d4670 ths
    }
145 223d4670 ths
    return ret_count;
146 223d4670 ths
}
147 223d4670 ths
148 223d4670 ths
static int raw_pwrite(BlockDriverState *bs, int64_t offset,
149 223d4670 ths
                      const uint8_t *buf, int count)
150 223d4670 ths
{
151 223d4670 ths
    BDRVRawState *s = bs->opaque;
152 223d4670 ths
    OVERLAPPED ov;
153 223d4670 ths
    DWORD ret_count;
154 223d4670 ths
    int ret;
155 223d4670 ths
156 223d4670 ths
    memset(&ov, 0, sizeof(ov));
157 223d4670 ths
    ov.Offset = offset;
158 223d4670 ths
    ov.OffsetHigh = offset >> 32;
159 223d4670 ths
    ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
160 223d4670 ths
    if (!ret) {
161 03ff3ca3 aliguori
#ifdef WIN32_AIO
162 223d4670 ths
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
163 223d4670 ths
        if (!ret)
164 223d4670 ths
            return -EIO;
165 223d4670 ths
        else
166 03ff3ca3 aliguori
#endif
167 223d4670 ths
            return ret_count;
168 223d4670 ths
    }
169 223d4670 ths
    return ret_count;
170 223d4670 ths
}
171 223d4670 ths
172 03ff3ca3 aliguori
#ifdef WIN32_AIO
173 223d4670 ths
static void raw_aio_cb(void *opaque)
174 223d4670 ths
{
175 223d4670 ths
    RawAIOCB *acb = opaque;
176 223d4670 ths
    BlockDriverState *bs = acb->common.bs;
177 223d4670 ths
    BDRVRawState *s = bs->opaque;
178 223d4670 ths
    DWORD ret_count;
179 223d4670 ths
    int ret;
180 223d4670 ths
181 223d4670 ths
    ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
182 223d4670 ths
    if (!ret || ret_count != acb->count) {
183 223d4670 ths
        acb->common.cb(acb->common.opaque, -EIO);
184 223d4670 ths
    } else {
185 223d4670 ths
        acb->common.cb(acb->common.opaque, 0);
186 223d4670 ths
    }
187 223d4670 ths
}
188 223d4670 ths
189 223d4670 ths
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
190 223d4670 ths
        int64_t sector_num, uint8_t *buf, int nb_sectors,
191 223d4670 ths
        BlockDriverCompletionFunc *cb, void *opaque)
192 223d4670 ths
{
193 223d4670 ths
    RawAIOCB *acb;
194 223d4670 ths
    int64_t offset;
195 223d4670 ths
196 223d4670 ths
    acb = qemu_aio_get(bs, cb, opaque);
197 223d4670 ths
    if (acb->hEvent) {
198 223d4670 ths
        acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
199 223d4670 ths
        if (!acb->hEvent) {
200 223d4670 ths
            qemu_aio_release(acb);
201 223d4670 ths
            return NULL;
202 223d4670 ths
        }
203 223d4670 ths
    }
204 223d4670 ths
    memset(&acb->ov, 0, sizeof(acb->ov));
205 223d4670 ths
    offset = sector_num * 512;
206 223d4670 ths
    acb->ov.Offset = offset;
207 223d4670 ths
    acb->ov.OffsetHigh = offset >> 32;
208 223d4670 ths
    acb->ov.hEvent = acb->hEvent;
209 223d4670 ths
    acb->count = nb_sectors * 512;
210 223d4670 ths
    qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
211 223d4670 ths
    return acb;
212 223d4670 ths
}
213 223d4670 ths
214 223d4670 ths
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
215 223d4670 ths
        int64_t sector_num, uint8_t *buf, int nb_sectors,
216 223d4670 ths
        BlockDriverCompletionFunc *cb, void *opaque)
217 223d4670 ths
{
218 223d4670 ths
    BDRVRawState *s = bs->opaque;
219 223d4670 ths
    RawAIOCB *acb;
220 223d4670 ths
    int ret;
221 223d4670 ths
222 223d4670 ths
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
223 223d4670 ths
    if (!acb)
224 223d4670 ths
        return NULL;
225 223d4670 ths
    ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
226 223d4670 ths
    if (!ret) {
227 223d4670 ths
        qemu_aio_release(acb);
228 223d4670 ths
        return NULL;
229 223d4670 ths
    }
230 223d4670 ths
    qemu_aio_release(acb);
231 223d4670 ths
    return (BlockDriverAIOCB *)acb;
232 223d4670 ths
}
233 223d4670 ths
234 223d4670 ths
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
235 223d4670 ths
        int64_t sector_num, uint8_t *buf, int nb_sectors,
236 223d4670 ths
        BlockDriverCompletionFunc *cb, void *opaque)
237 223d4670 ths
{
238 223d4670 ths
    BDRVRawState *s = bs->opaque;
239 223d4670 ths
    RawAIOCB *acb;
240 223d4670 ths
    int ret;
241 223d4670 ths
242 223d4670 ths
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
243 223d4670 ths
    if (!acb)
244 223d4670 ths
        return NULL;
245 223d4670 ths
    ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
246 223d4670 ths
    if (!ret) {
247 223d4670 ths
        qemu_aio_release(acb);
248 223d4670 ths
        return NULL;
249 223d4670 ths
    }
250 223d4670 ths
    qemu_aio_release(acb);
251 223d4670 ths
    return (BlockDriverAIOCB *)acb;
252 223d4670 ths
}
253 223d4670 ths
254 223d4670 ths
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
255 223d4670 ths
{
256 223d4670 ths
    RawAIOCB *acb = (RawAIOCB *)blockacb;
257 223d4670 ths
    BlockDriverState *bs = acb->common.bs;
258 223d4670 ths
    BDRVRawState *s = bs->opaque;
259 223d4670 ths
260 223d4670 ths
    qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
261 223d4670 ths
    /* XXX: if more than one async I/O it is not correct */
262 223d4670 ths
    CancelIo(s->hfile);
263 223d4670 ths
    qemu_aio_release(acb);
264 223d4670 ths
}
265 03ff3ca3 aliguori
#endif /* #if WIN32_AIO */
266 223d4670 ths
267 223d4670 ths
static void raw_flush(BlockDriverState *bs)
268 223d4670 ths
{
269 223d4670 ths
    BDRVRawState *s = bs->opaque;
270 223d4670 ths
    FlushFileBuffers(s->hfile);
271 223d4670 ths
}
272 223d4670 ths
273 223d4670 ths
static void raw_close(BlockDriverState *bs)
274 223d4670 ths
{
275 223d4670 ths
    BDRVRawState *s = bs->opaque;
276 223d4670 ths
    CloseHandle(s->hfile);
277 223d4670 ths
}
278 223d4670 ths
279 223d4670 ths
static int raw_truncate(BlockDriverState *bs, int64_t offset)
280 223d4670 ths
{
281 223d4670 ths
    BDRVRawState *s = bs->opaque;
282 223d4670 ths
    DWORD low, high;
283 223d4670 ths
284 223d4670 ths
    low = offset;
285 223d4670 ths
    high = offset >> 32;
286 223d4670 ths
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
287 223d4670 ths
        return -EIO;
288 223d4670 ths
    if (!SetEndOfFile(s->hfile))
289 223d4670 ths
        return -EIO;
290 223d4670 ths
    return 0;
291 223d4670 ths
}
292 223d4670 ths
293 223d4670 ths
static int64_t raw_getlength(BlockDriverState *bs)
294 223d4670 ths
{
295 223d4670 ths
    BDRVRawState *s = bs->opaque;
296 223d4670 ths
    LARGE_INTEGER l;
297 223d4670 ths
    ULARGE_INTEGER available, total, total_free;
298 223d4670 ths
    DISK_GEOMETRY_EX dg;
299 223d4670 ths
    DWORD count;
300 223d4670 ths
    BOOL status;
301 223d4670 ths
302 223d4670 ths
    switch(s->type) {
303 223d4670 ths
    case FTYPE_FILE:
304 223d4670 ths
        l.LowPart = GetFileSize(s->hfile, &l.HighPart);
305 223d4670 ths
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
306 223d4670 ths
            return -EIO;
307 223d4670 ths
        break;
308 223d4670 ths
    case FTYPE_CD:
309 223d4670 ths
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
310 223d4670 ths
            return -EIO;
311 223d4670 ths
        l.QuadPart = total.QuadPart;
312 223d4670 ths
        break;
313 223d4670 ths
    case FTYPE_HARDDISK:
314 223d4670 ths
        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
315 223d4670 ths
                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
316 223d4670 ths
        if (status != 0) {
317 223d4670 ths
            l = dg.DiskSize;
318 223d4670 ths
        }
319 223d4670 ths
        break;
320 223d4670 ths
    default:
321 223d4670 ths
        return -EIO;
322 223d4670 ths
    }
323 223d4670 ths
    return l.QuadPart;
324 223d4670 ths
}
325 223d4670 ths
326 223d4670 ths
static int raw_create(const char *filename, int64_t total_size,
327 223d4670 ths
                      const char *backing_file, int flags)
328 223d4670 ths
{
329 223d4670 ths
    int fd;
330 223d4670 ths
331 223d4670 ths
    if (flags || backing_file)
332 223d4670 ths
        return -ENOTSUP;
333 223d4670 ths
334 223d4670 ths
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
335 223d4670 ths
              0644);
336 223d4670 ths
    if (fd < 0)
337 223d4670 ths
        return -EIO;
338 223d4670 ths
    set_sparse(fd);
339 223d4670 ths
    ftruncate(fd, total_size * 512);
340 223d4670 ths
    close(fd);
341 223d4670 ths
    return 0;
342 223d4670 ths
}
343 223d4670 ths
344 223d4670 ths
BlockDriver bdrv_raw = {
345 223d4670 ths
    "raw",
346 223d4670 ths
    sizeof(BDRVRawState),
347 223d4670 ths
    NULL, /* no probe for protocols */
348 223d4670 ths
    raw_open,
349 223d4670 ths
    NULL,
350 223d4670 ths
    NULL,
351 223d4670 ths
    raw_close,
352 223d4670 ths
    raw_create,
353 223d4670 ths
    raw_flush,
354 223d4670 ths
355 03ff3ca3 aliguori
#ifdef WIN32_AIO
356 223d4670 ths
    .bdrv_aio_read = raw_aio_read,
357 223d4670 ths
    .bdrv_aio_write = raw_aio_write,
358 223d4670 ths
    .bdrv_aio_cancel = raw_aio_cancel,
359 223d4670 ths
    .aiocb_size = sizeof(RawAIOCB);
360 223d4670 ths
#endif
361 223d4670 ths
    .bdrv_pread = raw_pread,
362 223d4670 ths
    .bdrv_pwrite = raw_pwrite,
363 223d4670 ths
    .bdrv_truncate = raw_truncate,
364 223d4670 ths
    .bdrv_getlength = raw_getlength,
365 223d4670 ths
};
366 223d4670 ths
367 223d4670 ths
/***********************************************/
368 223d4670 ths
/* host device */
369 223d4670 ths
370 223d4670 ths
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
371 223d4670 ths
{
372 223d4670 ths
    char drives[256], *pdrv = drives;
373 223d4670 ths
    UINT type;
374 223d4670 ths
375 223d4670 ths
    memset(drives, 0, sizeof(drives));
376 223d4670 ths
    GetLogicalDriveStrings(sizeof(drives), drives);
377 223d4670 ths
    while(pdrv[0] != '\0') {
378 223d4670 ths
        type = GetDriveType(pdrv);
379 223d4670 ths
        switch(type) {
380 223d4670 ths
        case DRIVE_CDROM:
381 223d4670 ths
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
382 223d4670 ths
            return 0;
383 223d4670 ths
            break;
384 223d4670 ths
        }
385 223d4670 ths
        pdrv += lstrlen(pdrv) + 1;
386 223d4670 ths
    }
387 223d4670 ths
    return -1;
388 223d4670 ths
}
389 223d4670 ths
390 223d4670 ths
static int find_device_type(BlockDriverState *bs, const char *filename)
391 223d4670 ths
{
392 223d4670 ths
    BDRVRawState *s = bs->opaque;
393 223d4670 ths
    UINT type;
394 223d4670 ths
    const char *p;
395 223d4670 ths
396 223d4670 ths
    if (strstart(filename, "\\\\.\\", &p) ||
397 223d4670 ths
        strstart(filename, "//./", &p)) {
398 223d4670 ths
        if (stristart(p, "PhysicalDrive", NULL))
399 223d4670 ths
            return FTYPE_HARDDISK;
400 223d4670 ths
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
401 223d4670 ths
        type = GetDriveType(s->drive_path);
402 223d4670 ths
        if (type == DRIVE_CDROM)
403 223d4670 ths
            return FTYPE_CD;
404 223d4670 ths
        else
405 223d4670 ths
            return FTYPE_FILE;
406 223d4670 ths
    } else {
407 223d4670 ths
        return FTYPE_FILE;
408 223d4670 ths
    }
409 223d4670 ths
}
410 223d4670 ths
411 223d4670 ths
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
412 223d4670 ths
{
413 223d4670 ths
    BDRVRawState *s = bs->opaque;
414 223d4670 ths
    int access_flags, create_flags;
415 223d4670 ths
    DWORD overlapped;
416 223d4670 ths
    char device_name[64];
417 223d4670 ths
418 223d4670 ths
    if (strstart(filename, "/dev/cdrom", NULL)) {
419 223d4670 ths
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
420 223d4670 ths
            return -ENOENT;
421 223d4670 ths
        filename = device_name;
422 223d4670 ths
    } else {
423 223d4670 ths
        /* transform drive letters into device name */
424 223d4670 ths
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
425 223d4670 ths
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
426 223d4670 ths
            filename[1] == ':' && filename[2] == '\0') {
427 223d4670 ths
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
428 223d4670 ths
            filename = device_name;
429 223d4670 ths
        }
430 223d4670 ths
    }
431 223d4670 ths
    s->type = find_device_type(bs, filename);
432 223d4670 ths
433 223d4670 ths
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
434 223d4670 ths
        access_flags = GENERIC_READ | GENERIC_WRITE;
435 223d4670 ths
    } else {
436 223d4670 ths
        access_flags = GENERIC_READ;
437 223d4670 ths
    }
438 223d4670 ths
    create_flags = OPEN_EXISTING;
439 223d4670 ths
440 03ff3ca3 aliguori
#ifdef WIN32_AIO
441 223d4670 ths
    overlapped = FILE_FLAG_OVERLAPPED;
442 03ff3ca3 aliguori
#else
443 03ff3ca3 aliguori
    overlapped = FILE_ATTRIBUTE_NORMAL;
444 223d4670 ths
#endif
445 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE))
446 33f00271 balrog
        overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
447 9f7965c7 aliguori
    else if (!(flags & BDRV_O_CACHE_WB))
448 9f7965c7 aliguori
        overlapped |= FILE_FLAG_WRITE_THROUGH;
449 223d4670 ths
    s->hfile = CreateFile(filename, access_flags,
450 223d4670 ths
                          FILE_SHARE_READ, NULL,
451 223d4670 ths
                          create_flags, overlapped, NULL);
452 223d4670 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
453 223d4670 ths
        int err = GetLastError();
454 223d4670 ths
455 223d4670 ths
        if (err == ERROR_ACCESS_DENIED)
456 223d4670 ths
            return -EACCES;
457 223d4670 ths
        return -1;
458 223d4670 ths
    }
459 223d4670 ths
    return 0;
460 223d4670 ths
}
461 223d4670 ths
462 223d4670 ths
#if 0
463 223d4670 ths
/***********************************************/
464 223d4670 ths
/* removable device additional commands */
465 223d4670 ths

466 223d4670 ths
static int raw_is_inserted(BlockDriverState *bs)
467 223d4670 ths
{
468 223d4670 ths
    return 1;
469 223d4670 ths
}
470 223d4670 ths

471 223d4670 ths
static int raw_media_changed(BlockDriverState *bs)
472 223d4670 ths
{
473 223d4670 ths
    return -ENOTSUP;
474 223d4670 ths
}
475 223d4670 ths

476 223d4670 ths
static int raw_eject(BlockDriverState *bs, int eject_flag)
477 223d4670 ths
{
478 223d4670 ths
    DWORD ret_count;
479 223d4670 ths

480 223d4670 ths
    if (s->type == FTYPE_FILE)
481 223d4670 ths
        return -ENOTSUP;
482 223d4670 ths
    if (eject_flag) {
483 223d4670 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
484 223d4670 ths
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
485 223d4670 ths
    } else {
486 223d4670 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
487 223d4670 ths
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
488 223d4670 ths
    }
489 223d4670 ths
}
490 223d4670 ths

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