xref: /qemu/block/file-win32.c (revision 061ca8a368165fae300748c17971824a089f521f)
1223d4670Sths /*
2223d4670Sths  * Block driver for RAW files (win32)
3223d4670Sths  *
4223d4670Sths  * Copyright (c) 2006 Fabrice Bellard
5223d4670Sths  *
6223d4670Sths  * Permission is hereby granted, free of charge, to any person obtaining a copy
7223d4670Sths  * of this software and associated documentation files (the "Software"), to deal
8223d4670Sths  * in the Software without restriction, including without limitation the rights
9223d4670Sths  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10223d4670Sths  * copies of the Software, and to permit persons to whom the Software is
11223d4670Sths  * furnished to do so, subject to the following conditions:
12223d4670Sths  *
13223d4670Sths  * The above copyright notice and this permission notice shall be included in
14223d4670Sths  * all copies or substantial portions of the Software.
15223d4670Sths  *
16223d4670Sths  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17223d4670Sths  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18223d4670Sths  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19223d4670Sths  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20223d4670Sths  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21223d4670Sths  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22223d4670Sths  * THE SOFTWARE.
23223d4670Sths  */
24922a01a0SMarkus Armbruster 
2580c71a24SPeter Maydell #include "qemu/osdep.h"
26da34e65cSMarkus Armbruster #include "qapi/error.h"
27f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
28737e150eSPaolo Bonzini #include "block/block_int.h"
291de7afc9SPaolo Bonzini #include "qemu/module.h"
30922a01a0SMarkus Armbruster #include "qemu/option.h"
310187f5c9SPaolo Bonzini #include "block/raw-aio.h"
32fc4edb84SPaolo Bonzini #include "trace.h"
33737e150eSPaolo Bonzini #include "block/thread-pool.h"
341de7afc9SPaolo Bonzini #include "qemu/iov.h"
35452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
36d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h"
3749dc768dSaliguori #include <windows.h>
38223d4670Sths #include <winioctl.h>
39223d4670Sths 
40223d4670Sths #define FTYPE_FILE 0
41223d4670Sths #define FTYPE_CD     1
42223d4670Sths #define FTYPE_HARDDISK 2
43223d4670Sths 
44fc4edb84SPaolo Bonzini typedef struct RawWin32AIOData {
45fc4edb84SPaolo Bonzini     BlockDriverState *bs;
46fc4edb84SPaolo Bonzini     HANDLE hfile;
47fc4edb84SPaolo Bonzini     struct iovec *aio_iov;
48fc4edb84SPaolo Bonzini     int aio_niov;
49fc4edb84SPaolo Bonzini     size_t aio_nbytes;
50fc4edb84SPaolo Bonzini     off64_t aio_offset;
51fc4edb84SPaolo Bonzini     int aio_type;
52fc4edb84SPaolo Bonzini } RawWin32AIOData;
53fc4edb84SPaolo Bonzini 
54223d4670Sths typedef struct BDRVRawState {
55223d4670Sths     HANDLE hfile;
56223d4670Sths     int type;
57223d4670Sths     char drive_path[16]; /* format: "d:\" */
58a2736526SPaolo Bonzini     QEMUWin32AIOState *aio;
59223d4670Sths } BDRVRawState;
60223d4670Sths 
61fc4edb84SPaolo Bonzini /*
62fc4edb84SPaolo Bonzini  * Read/writes the data to/from a given linear buffer.
63fc4edb84SPaolo Bonzini  *
64fc4edb84SPaolo Bonzini  * Returns the number of bytes handles or -errno in case of an error. Short
65fc4edb84SPaolo Bonzini  * reads are only returned if the end of the file is reached.
66fc4edb84SPaolo Bonzini  */
67fc4edb84SPaolo Bonzini static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
68fc4edb84SPaolo Bonzini {
69fc4edb84SPaolo Bonzini     size_t offset = 0;
70fc4edb84SPaolo Bonzini     int i;
71fc4edb84SPaolo Bonzini 
72fc4edb84SPaolo Bonzini     for (i = 0; i < aiocb->aio_niov; i++) {
73fc4edb84SPaolo Bonzini         OVERLAPPED ov;
74fc4edb84SPaolo Bonzini         DWORD ret, ret_count, len;
75fc4edb84SPaolo Bonzini 
76fc4edb84SPaolo Bonzini         memset(&ov, 0, sizeof(ov));
77fc4edb84SPaolo Bonzini         ov.Offset = (aiocb->aio_offset + offset);
78fc4edb84SPaolo Bonzini         ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
79fc4edb84SPaolo Bonzini         len = aiocb->aio_iov[i].iov_len;
80fc4edb84SPaolo Bonzini         if (aiocb->aio_type & QEMU_AIO_WRITE) {
81fc4edb84SPaolo Bonzini             ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
82fc4edb84SPaolo Bonzini                             len, &ret_count, &ov);
83fc4edb84SPaolo Bonzini         } else {
84fc4edb84SPaolo Bonzini             ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
85fc4edb84SPaolo Bonzini                            len, &ret_count, &ov);
86fc4edb84SPaolo Bonzini         }
87fc4edb84SPaolo Bonzini         if (!ret) {
88fc4edb84SPaolo Bonzini             ret_count = 0;
89fc4edb84SPaolo Bonzini         }
90fc4edb84SPaolo Bonzini         if (ret_count != len) {
9156e023afSTal Kain             offset += ret_count;
92fc4edb84SPaolo Bonzini             break;
93fc4edb84SPaolo Bonzini         }
94fc4edb84SPaolo Bonzini         offset += len;
95fc4edb84SPaolo Bonzini     }
96fc4edb84SPaolo Bonzini 
97fc4edb84SPaolo Bonzini     return offset;
98fc4edb84SPaolo Bonzini }
99fc4edb84SPaolo Bonzini 
100fc4edb84SPaolo Bonzini static int aio_worker(void *arg)
101fc4edb84SPaolo Bonzini {
102fc4edb84SPaolo Bonzini     RawWin32AIOData *aiocb = arg;
103fc4edb84SPaolo Bonzini     ssize_t ret = 0;
104fc4edb84SPaolo Bonzini     size_t count;
105fc4edb84SPaolo Bonzini 
106fc4edb84SPaolo Bonzini     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
107fc4edb84SPaolo Bonzini     case QEMU_AIO_READ:
108fc4edb84SPaolo Bonzini         count = handle_aiocb_rw(aiocb);
109c0191e76SMax Reitz         if (count < aiocb->aio_nbytes) {
110fc4edb84SPaolo Bonzini             /* A short read means that we have reached EOF. Pad the buffer
111fc4edb84SPaolo Bonzini              * with zeros for bytes after EOF. */
112fc4edb84SPaolo Bonzini             iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
113fc4edb84SPaolo Bonzini                       0, aiocb->aio_nbytes - count);
114fc4edb84SPaolo Bonzini 
115fc4edb84SPaolo Bonzini             count = aiocb->aio_nbytes;
116fc4edb84SPaolo Bonzini         }
117fc4edb84SPaolo Bonzini         if (count == aiocb->aio_nbytes) {
118fc4edb84SPaolo Bonzini             ret = 0;
119fc4edb84SPaolo Bonzini         } else {
120fc4edb84SPaolo Bonzini             ret = -EINVAL;
121fc4edb84SPaolo Bonzini         }
122fc4edb84SPaolo Bonzini         break;
123fc4edb84SPaolo Bonzini     case QEMU_AIO_WRITE:
124fc4edb84SPaolo Bonzini         count = handle_aiocb_rw(aiocb);
125fc4edb84SPaolo Bonzini         if (count == aiocb->aio_nbytes) {
1265d555030SKevin Wolf             ret = 0;
127fc4edb84SPaolo Bonzini         } else {
1285d555030SKevin Wolf             ret = -EINVAL;
129fc4edb84SPaolo Bonzini         }
130fc4edb84SPaolo Bonzini         break;
131fc4edb84SPaolo Bonzini     case QEMU_AIO_FLUSH:
132fc4edb84SPaolo Bonzini         if (!FlushFileBuffers(aiocb->hfile)) {
133fc4edb84SPaolo Bonzini             return -EIO;
134fc4edb84SPaolo Bonzini         }
135fc4edb84SPaolo Bonzini         break;
136fc4edb84SPaolo Bonzini     default:
137fc4edb84SPaolo Bonzini         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
138fc4edb84SPaolo Bonzini         ret = -EINVAL;
139fc4edb84SPaolo Bonzini         break;
140fc4edb84SPaolo Bonzini     }
141fc4edb84SPaolo Bonzini 
142c84b3192SPaolo Bonzini     g_free(aiocb);
143fc4edb84SPaolo Bonzini     return ret;
144fc4edb84SPaolo Bonzini }
145fc4edb84SPaolo Bonzini 
1467c84b1b8SMarkus Armbruster static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
14736e3b2e7SEric Blake         int64_t offset, QEMUIOVector *qiov, int count,
148097310b5SMarkus Armbruster         BlockCompletionFunc *cb, void *opaque, int type)
149fc4edb84SPaolo Bonzini {
150c84b3192SPaolo Bonzini     RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
151c4d9d196SStefan Hajnoczi     ThreadPool *pool;
152fc4edb84SPaolo Bonzini 
153fc4edb84SPaolo Bonzini     acb->bs = bs;
154fc4edb84SPaolo Bonzini     acb->hfile = hfile;
155fc4edb84SPaolo Bonzini     acb->aio_type = type;
156fc4edb84SPaolo Bonzini 
157fc4edb84SPaolo Bonzini     if (qiov) {
158fc4edb84SPaolo Bonzini         acb->aio_iov = qiov->iov;
159fc4edb84SPaolo Bonzini         acb->aio_niov = qiov->niov;
16036e3b2e7SEric Blake         assert(qiov->size == count);
161fc4edb84SPaolo Bonzini     }
16236e3b2e7SEric Blake     acb->aio_nbytes = count;
16336e3b2e7SEric Blake     acb->aio_offset = offset;
164fc4edb84SPaolo Bonzini 
16536e3b2e7SEric Blake     trace_paio_submit(acb, opaque, offset, count, type);
166c4d9d196SStefan Hajnoczi     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
167c4d9d196SStefan Hajnoczi     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
168fc4edb84SPaolo Bonzini }
169fc4edb84SPaolo Bonzini 
170223d4670Sths int qemu_ftruncate64(int fd, int64_t length)
171223d4670Sths {
172223d4670Sths     LARGE_INTEGER li;
1732c993ec2SStefan Weil     DWORD dw;
174223d4670Sths     LONG high;
175223d4670Sths     HANDLE h;
176223d4670Sths     BOOL res;
177223d4670Sths 
178223d4670Sths     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
179223d4670Sths 	return -1;
180223d4670Sths 
181223d4670Sths     h = (HANDLE)_get_osfhandle(fd);
182223d4670Sths 
183223d4670Sths     /* get current position, ftruncate do not change position */
184223d4670Sths     li.HighPart = 0;
185223d4670Sths     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
1862c993ec2SStefan Weil     if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
187223d4670Sths 	return -1;
1882c993ec2SStefan Weil     }
189223d4670Sths 
190223d4670Sths     high = length >> 32;
1912c993ec2SStefan Weil     dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
1922c993ec2SStefan Weil     if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
193223d4670Sths 	return -1;
1942c993ec2SStefan Weil     }
195223d4670Sths     res = SetEndOfFile(h);
196223d4670Sths 
197223d4670Sths     /* back to old position */
198223d4670Sths     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
199223d4670Sths     return res ? 0 : -1;
200223d4670Sths }
201223d4670Sths 
202223d4670Sths static int set_sparse(int fd)
203223d4670Sths {
204223d4670Sths     DWORD returned;
205223d4670Sths     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
206223d4670Sths 				 NULL, 0, NULL, 0, &returned, NULL);
207223d4670Sths }
208223d4670Sths 
20985ebd381SStefan Hajnoczi static void raw_detach_aio_context(BlockDriverState *bs)
21085ebd381SStefan Hajnoczi {
21185ebd381SStefan Hajnoczi     BDRVRawState *s = bs->opaque;
21285ebd381SStefan Hajnoczi 
21385ebd381SStefan Hajnoczi     if (s->aio) {
21485ebd381SStefan Hajnoczi         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
21585ebd381SStefan Hajnoczi     }
21685ebd381SStefan Hajnoczi }
21785ebd381SStefan Hajnoczi 
21885ebd381SStefan Hajnoczi static void raw_attach_aio_context(BlockDriverState *bs,
21985ebd381SStefan Hajnoczi                                    AioContext *new_context)
22085ebd381SStefan Hajnoczi {
22185ebd381SStefan Hajnoczi     BDRVRawState *s = bs->opaque;
22285ebd381SStefan Hajnoczi 
22385ebd381SStefan Hajnoczi     if (s->aio) {
22485ebd381SStefan Hajnoczi         win32_aio_attach_aio_context(s->aio, new_context);
22585ebd381SStefan Hajnoczi     }
22685ebd381SStefan Hajnoczi }
22785ebd381SStefan Hajnoczi 
2282914a1deSEric Blake static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
229c25f53b0SPaolo Bonzini {
230c25f53b0SPaolo Bonzini     BDRVRawState *s = bs->opaque;
231c25f53b0SPaolo Bonzini     DWORD sectorsPerCluster, freeClusters, totalClusters, count;
232c25f53b0SPaolo Bonzini     DISK_GEOMETRY_EX dg;
233c25f53b0SPaolo Bonzini     BOOL status;
234c25f53b0SPaolo Bonzini 
235c25f53b0SPaolo Bonzini     if (s->type == FTYPE_CD) {
236a5b8dd2cSEric Blake         bs->bl.request_alignment = 2048;
237c25f53b0SPaolo Bonzini         return;
238c25f53b0SPaolo Bonzini     }
239c25f53b0SPaolo Bonzini     if (s->type == FTYPE_HARDDISK) {
240c25f53b0SPaolo Bonzini         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
241c25f53b0SPaolo Bonzini                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
242c25f53b0SPaolo Bonzini         if (status != 0) {
243a5b8dd2cSEric Blake             bs->bl.request_alignment = dg.Geometry.BytesPerSector;
244c25f53b0SPaolo Bonzini             return;
245c25f53b0SPaolo Bonzini         }
246c25f53b0SPaolo Bonzini         /* try GetDiskFreeSpace too */
247c25f53b0SPaolo Bonzini     }
248c25f53b0SPaolo Bonzini 
249c25f53b0SPaolo Bonzini     if (s->drive_path[0]) {
250c25f53b0SPaolo Bonzini         GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
251c25f53b0SPaolo Bonzini                          &dg.Geometry.BytesPerSector,
252c25f53b0SPaolo Bonzini                          &freeClusters, &totalClusters);
253a5b8dd2cSEric Blake         bs->bl.request_alignment = dg.Geometry.BytesPerSector;
254de7056a3SEric Blake         return;
255c25f53b0SPaolo Bonzini     }
256de7056a3SEric Blake 
257de7056a3SEric Blake     /* XXX Does Windows support AIO on less than 512-byte alignment? */
258de7056a3SEric Blake     bs->bl.request_alignment = 512;
259c25f53b0SPaolo Bonzini }
260c25f53b0SPaolo Bonzini 
2610a4279d9SKevin Wolf static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
2620a4279d9SKevin Wolf                             DWORD *overlapped)
2636a8dc042SJeff Cody {
2646a8dc042SJeff Cody     assert(access_flags != NULL);
2656a8dc042SJeff Cody     assert(overlapped != NULL);
2666a8dc042SJeff Cody 
2676a8dc042SJeff Cody     if (flags & BDRV_O_RDWR) {
2686a8dc042SJeff Cody         *access_flags = GENERIC_READ | GENERIC_WRITE;
2696a8dc042SJeff Cody     } else {
2706a8dc042SJeff Cody         *access_flags = GENERIC_READ;
2716a8dc042SJeff Cody     }
2726a8dc042SJeff Cody 
2736a8dc042SJeff Cody     *overlapped = FILE_ATTRIBUTE_NORMAL;
2740a4279d9SKevin Wolf     if (use_aio) {
275a2736526SPaolo Bonzini         *overlapped |= FILE_FLAG_OVERLAPPED;
276a2736526SPaolo Bonzini     }
2776a8dc042SJeff Cody     if (flags & BDRV_O_NOCACHE) {
2786a8dc042SJeff Cody         *overlapped |= FILE_FLAG_NO_BUFFERING;
2796a8dc042SJeff Cody     }
2806a8dc042SJeff Cody }
2816a8dc042SJeff Cody 
2827dc74db8SMax Reitz static void raw_parse_filename(const char *filename, QDict *options,
2837dc74db8SMax Reitz                                Error **errp)
2847dc74db8SMax Reitz {
28503c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "file:", options);
2867dc74db8SMax Reitz }
2877dc74db8SMax Reitz 
2888a79380bSKevin Wolf static QemuOptsList raw_runtime_opts = {
2898a79380bSKevin Wolf     .name = "raw",
2908a79380bSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
2918a79380bSKevin Wolf     .desc = {
2928a79380bSKevin Wolf         {
2938a79380bSKevin Wolf             .name = "filename",
2948a79380bSKevin Wolf             .type = QEMU_OPT_STRING,
2958a79380bSKevin Wolf             .help = "File name of the image",
2968a79380bSKevin Wolf         },
2970a4279d9SKevin Wolf         {
2980a4279d9SKevin Wolf             .name = "aio",
2990a4279d9SKevin Wolf             .type = QEMU_OPT_STRING,
3000a4279d9SKevin Wolf             .help = "host AIO implementation (threads, native)",
3010a4279d9SKevin Wolf         },
3028a79380bSKevin Wolf         { /* end of list */ }
3038a79380bSKevin Wolf     },
3048a79380bSKevin Wolf };
3058a79380bSKevin Wolf 
3060a4279d9SKevin Wolf static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
3070a4279d9SKevin Wolf {
3080a4279d9SKevin Wolf     BlockdevAioOptions aio, aio_default;
3090a4279d9SKevin Wolf 
3100a4279d9SKevin Wolf     aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
3110a4279d9SKevin Wolf                                               : BLOCKDEV_AIO_OPTIONS_THREADS;
312f7abe0ecSMarc-André Lureau     aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
31306c60b6cSMarkus Armbruster                           aio_default, errp);
3140a4279d9SKevin Wolf 
3150a4279d9SKevin Wolf     switch (aio) {
3160a4279d9SKevin Wolf     case BLOCKDEV_AIO_OPTIONS_NATIVE:
3170a4279d9SKevin Wolf         return true;
3180a4279d9SKevin Wolf     case BLOCKDEV_AIO_OPTIONS_THREADS:
3190a4279d9SKevin Wolf         return false;
3200a4279d9SKevin Wolf     default:
3210a4279d9SKevin Wolf         error_setg(errp, "Invalid AIO option");
3220a4279d9SKevin Wolf     }
3230a4279d9SKevin Wolf     return false;
3240a4279d9SKevin Wolf }
3250a4279d9SKevin Wolf 
326015a1036SMax Reitz static int raw_open(BlockDriverState *bs, QDict *options, int flags,
327015a1036SMax Reitz                     Error **errp)
328223d4670Sths {
329223d4670Sths     BDRVRawState *s = bs->opaque;
3309a2d77adSChristoph Hellwig     int access_flags;
331223d4670Sths     DWORD overlapped;
3328a79380bSKevin Wolf     QemuOpts *opts;
3338a79380bSKevin Wolf     Error *local_err = NULL;
3348a79380bSKevin Wolf     const char *filename;
3350a4279d9SKevin Wolf     bool use_aio;
3368a79380bSKevin Wolf     int ret;
337223d4670Sths 
338223d4670Sths     s->type = FTYPE_FILE;
339223d4670Sths 
34087ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
3418a79380bSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
34284d18f06SMarkus Armbruster     if (local_err) {
343c6252b7cSMax Reitz         error_propagate(errp, local_err);
3448a79380bSKevin Wolf         ret = -EINVAL;
3458a79380bSKevin Wolf         goto fail;
3468a79380bSKevin Wolf     }
3478a79380bSKevin Wolf 
3481c3a555cSFam Zheng     if (qdict_get_try_bool(options, "locking", false)) {
3491c3a555cSFam Zheng         error_setg(errp, "locking=on is not supported on Windows");
350cdece046SGerd Hoffmann         ret = -EINVAL;
3511c3a555cSFam Zheng         goto fail;
3521c3a555cSFam Zheng     }
3531c3a555cSFam Zheng 
3548a79380bSKevin Wolf     filename = qemu_opt_get(opts, "filename");
3558a79380bSKevin Wolf 
3560a4279d9SKevin Wolf     use_aio = get_aio_option(opts, flags, &local_err);
3570a4279d9SKevin Wolf     if (local_err) {
3580a4279d9SKevin Wolf         error_propagate(errp, local_err);
3590a4279d9SKevin Wolf         ret = -EINVAL;
3600a4279d9SKevin Wolf         goto fail;
3610a4279d9SKevin Wolf     }
3620a4279d9SKevin Wolf 
3630a4279d9SKevin Wolf     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
3649a2d77adSChristoph Hellwig 
365c25f53b0SPaolo Bonzini     if (filename[0] && filename[1] == ':') {
366c25f53b0SPaolo Bonzini         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
367c25f53b0SPaolo Bonzini     } else if (filename[0] == '\\' && filename[1] == '\\') {
368c25f53b0SPaolo Bonzini         s->drive_path[0] = 0;
369c25f53b0SPaolo Bonzini     } else {
370c25f53b0SPaolo Bonzini         /* Relative path.  */
371c25f53b0SPaolo Bonzini         char buf[MAX_PATH];
372c25f53b0SPaolo Bonzini         GetCurrentDirectory(MAX_PATH, buf);
373c25f53b0SPaolo Bonzini         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
374c25f53b0SPaolo Bonzini     }
375c25f53b0SPaolo Bonzini 
376223d4670Sths     s->hfile = CreateFile(filename, access_flags,
377223d4670Sths                           FILE_SHARE_READ, NULL,
3789a2d77adSChristoph Hellwig                           OPEN_EXISTING, overlapped, NULL);
379223d4670Sths     if (s->hfile == INVALID_HANDLE_VALUE) {
380223d4670Sths         int err = GetLastError();
381223d4670Sths 
38209237757SHalil Pasic         error_setg_win32(errp, err, "Could not open '%s'", filename);
3838a79380bSKevin Wolf         if (err == ERROR_ACCESS_DENIED) {
3848a79380bSKevin Wolf             ret = -EACCES;
3858a79380bSKevin Wolf         } else {
3868a79380bSKevin Wolf             ret = -EINVAL;
3878a79380bSKevin Wolf         }
3888a79380bSKevin Wolf         goto fail;
389a2736526SPaolo Bonzini     }
390a2736526SPaolo Bonzini 
3910a4279d9SKevin Wolf     if (use_aio) {
39299cc5989SStefan Hajnoczi         s->aio = win32_aio_init();
39399cc5989SStefan Hajnoczi         if (s->aio == NULL) {
39499cc5989SStefan Hajnoczi             CloseHandle(s->hfile);
39599cc5989SStefan Hajnoczi             error_setg(errp, "Could not initialize AIO");
39699cc5989SStefan Hajnoczi             ret = -EINVAL;
39799cc5989SStefan Hajnoczi             goto fail;
39899cc5989SStefan Hajnoczi         }
39999cc5989SStefan Hajnoczi 
40099cc5989SStefan Hajnoczi         ret = win32_aio_attach(s->aio, s->hfile);
401a2736526SPaolo Bonzini         if (ret < 0) {
40299cc5989SStefan Hajnoczi             win32_aio_cleanup(s->aio);
403a2736526SPaolo Bonzini             CloseHandle(s->hfile);
404c6252b7cSMax Reitz             error_setg_errno(errp, -ret, "Could not enable AIO");
4058a79380bSKevin Wolf             goto fail;
406a2736526SPaolo Bonzini         }
40785ebd381SStefan Hajnoczi 
40885ebd381SStefan Hajnoczi         win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
409223d4670Sths     }
4108a79380bSKevin Wolf 
4118a79380bSKevin Wolf     ret = 0;
4128a79380bSKevin Wolf fail:
4138a79380bSKevin Wolf     qemu_opts_del(opts);
4148a79380bSKevin Wolf     return ret;
415223d4670Sths }
416223d4670Sths 
417de7056a3SEric Blake static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
418de7056a3SEric Blake                                   uint64_t offset, uint64_t bytes,
419de7056a3SEric Blake                                   QEMUIOVector *qiov, int flags,
420097310b5SMarkus Armbruster                                   BlockCompletionFunc *cb, void *opaque)
421223d4670Sths {
422223d4670Sths     BDRVRawState *s = bs->opaque;
423a2736526SPaolo Bonzini     if (s->aio) {
424de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
425de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_READ);
426a2736526SPaolo Bonzini     } else {
427de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
428fc4edb84SPaolo Bonzini                            cb, opaque, QEMU_AIO_READ);
429223d4670Sths     }
430a2736526SPaolo Bonzini }
431223d4670Sths 
432de7056a3SEric Blake static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
433de7056a3SEric Blake                                    uint64_t offset, uint64_t bytes,
434de7056a3SEric Blake                                    QEMUIOVector *qiov, int flags,
435097310b5SMarkus Armbruster                                    BlockCompletionFunc *cb, void *opaque)
436223d4670Sths {
437223d4670Sths     BDRVRawState *s = bs->opaque;
438a2736526SPaolo Bonzini     if (s->aio) {
439de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
440de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_WRITE);
441a2736526SPaolo Bonzini     } else {
442de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
443fc4edb84SPaolo Bonzini                            cb, opaque, QEMU_AIO_WRITE);
444223d4670Sths     }
445a2736526SPaolo Bonzini }
446223d4670Sths 
4477c84b1b8SMarkus Armbruster static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
448097310b5SMarkus Armbruster                          BlockCompletionFunc *cb, void *opaque)
449223d4670Sths {
450223d4670Sths     BDRVRawState *s = bs->opaque;
451fc4edb84SPaolo Bonzini     return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
452223d4670Sths }
453223d4670Sths 
454223d4670Sths static void raw_close(BlockDriverState *bs)
455223d4670Sths {
456223d4670Sths     BDRVRawState *s = bs->opaque;
45799cc5989SStefan Hajnoczi 
45899cc5989SStefan Hajnoczi     if (s->aio) {
45985ebd381SStefan Hajnoczi         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
46099cc5989SStefan Hajnoczi         win32_aio_cleanup(s->aio);
46199cc5989SStefan Hajnoczi         s->aio = NULL;
46299cc5989SStefan Hajnoczi     }
46399cc5989SStefan Hajnoczi 
464223d4670Sths     CloseHandle(s->hfile);
4658bfea15dSKevin Wolf     if (bs->open_flags & BDRV_O_TEMPORARY) {
4668bfea15dSKevin Wolf         unlink(bs->filename);
4678bfea15dSKevin Wolf     }
468223d4670Sths }
469223d4670Sths 
470*061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
4718243ccb7SMax Reitz                                         PreallocMode prealloc, Error **errp)
472223d4670Sths {
473223d4670Sths     BDRVRawState *s = bs->opaque;
474b9e82a59Sblueswir1     LONG low, high;
475fbcad04dSFabien Chouteau     DWORD dwPtrLow;
476223d4670Sths 
4778243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
4788243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
479977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
4808243ccb7SMax Reitz         return -ENOTSUP;
4818243ccb7SMax Reitz     }
4828243ccb7SMax Reitz 
483223d4670Sths     low = offset;
484223d4670Sths     high = offset >> 32;
485fbcad04dSFabien Chouteau 
486fbcad04dSFabien Chouteau     /*
487fbcad04dSFabien Chouteau      * An error has occurred if the return value is INVALID_SET_FILE_POINTER
488fbcad04dSFabien Chouteau      * and GetLastError doesn't return NO_ERROR.
489fbcad04dSFabien Chouteau      */
490fbcad04dSFabien Chouteau     dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
491fbcad04dSFabien Chouteau     if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
4924bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetFilePointer error");
493223d4670Sths         return -EIO;
494fbcad04dSFabien Chouteau     }
495fbcad04dSFabien Chouteau     if (SetEndOfFile(s->hfile) == 0) {
4964bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
497223d4670Sths         return -EIO;
498fbcad04dSFabien Chouteau     }
499223d4670Sths     return 0;
500223d4670Sths }
501223d4670Sths 
502223d4670Sths static int64_t raw_getlength(BlockDriverState *bs)
503223d4670Sths {
504223d4670Sths     BDRVRawState *s = bs->opaque;
505223d4670Sths     LARGE_INTEGER l;
506223d4670Sths     ULARGE_INTEGER available, total, total_free;
507223d4670Sths     DISK_GEOMETRY_EX dg;
508223d4670Sths     DWORD count;
509223d4670Sths     BOOL status;
510223d4670Sths 
511223d4670Sths     switch(s->type) {
512223d4670Sths     case FTYPE_FILE:
513b9e82a59Sblueswir1         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
514223d4670Sths         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
515223d4670Sths             return -EIO;
516223d4670Sths         break;
517223d4670Sths     case FTYPE_CD:
518223d4670Sths         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
519223d4670Sths             return -EIO;
520223d4670Sths         l.QuadPart = total.QuadPart;
521223d4670Sths         break;
522223d4670Sths     case FTYPE_HARDDISK:
523223d4670Sths         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
524223d4670Sths                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
525223d4670Sths         if (status != 0) {
526223d4670Sths             l = dg.DiskSize;
527223d4670Sths         }
528223d4670Sths         break;
529223d4670Sths     default:
530223d4670Sths         return -EIO;
531223d4670Sths     }
532223d4670Sths     return l.QuadPart;
533223d4670Sths }
534223d4670Sths 
5354a1d5e1fSFam Zheng static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
5364a1d5e1fSFam Zheng {
5374a1d5e1fSFam Zheng     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
5384a1d5e1fSFam Zheng                                               DWORD * high);
5394a1d5e1fSFam Zheng     get_compressed_t get_compressed;
5404a1d5e1fSFam Zheng     struct _stati64 st;
5414a1d5e1fSFam Zheng     const char *filename = bs->filename;
5424a1d5e1fSFam Zheng     /* WinNT support GetCompressedFileSize to determine allocate size */
5434a1d5e1fSFam Zheng     get_compressed =
5444a1d5e1fSFam Zheng         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
5454a1d5e1fSFam Zheng                                             "GetCompressedFileSizeA");
5464a1d5e1fSFam Zheng     if (get_compressed) {
5474a1d5e1fSFam Zheng         DWORD high, low;
5484a1d5e1fSFam Zheng         low = get_compressed(filename, &high);
5494a1d5e1fSFam Zheng         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
5504a1d5e1fSFam Zheng             return (((int64_t) high) << 32) + low;
5514a1d5e1fSFam Zheng         }
5524a1d5e1fSFam Zheng     }
5534a1d5e1fSFam Zheng 
5544a1d5e1fSFam Zheng     if (_stati64(filename, &st) < 0) {
5554a1d5e1fSFam Zheng         return -1;
5564a1d5e1fSFam Zheng     }
5574a1d5e1fSFam Zheng     return st.st_size;
5584a1d5e1fSFam Zheng }
5594a1d5e1fSFam Zheng 
5603766ef57SKevin Wolf static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
5613766ef57SKevin Wolf {
5623766ef57SKevin Wolf     BlockdevCreateOptionsFile *file_opts;
5633766ef57SKevin Wolf     int fd;
5643766ef57SKevin Wolf 
5653766ef57SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_FILE);
5663766ef57SKevin Wolf     file_opts = &options->u.file;
5673766ef57SKevin Wolf 
5683766ef57SKevin Wolf     if (file_opts->has_preallocation) {
5693766ef57SKevin Wolf         error_setg(errp, "Preallocation is not supported on Windows");
5703766ef57SKevin Wolf         return -EINVAL;
5713766ef57SKevin Wolf     }
5723766ef57SKevin Wolf     if (file_opts->has_nocow) {
5733766ef57SKevin Wolf         error_setg(errp, "nocow is not supported on Windows");
5743766ef57SKevin Wolf         return -EINVAL;
5753766ef57SKevin Wolf     }
5763766ef57SKevin Wolf 
5773766ef57SKevin Wolf     fd = qemu_open(file_opts->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
5783766ef57SKevin Wolf                    0644);
5793766ef57SKevin Wolf     if (fd < 0) {
5803766ef57SKevin Wolf         error_setg_errno(errp, errno, "Could not create file");
5813766ef57SKevin Wolf         return -EIO;
5823766ef57SKevin Wolf     }
5833766ef57SKevin Wolf     set_sparse(fd);
5843766ef57SKevin Wolf     ftruncate(fd, file_opts->size);
5853766ef57SKevin Wolf     qemu_close(fd);
5863766ef57SKevin Wolf 
5873766ef57SKevin Wolf     return 0;
5883766ef57SKevin Wolf }
5893766ef57SKevin Wolf 
590efc75e2aSStefan Hajnoczi static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
591efc75e2aSStefan Hajnoczi                                            Error **errp)
592223d4670Sths {
5933766ef57SKevin Wolf     BlockdevCreateOptions options;
5940e7e1989SKevin Wolf     int64_t total_size = 0;
595223d4670Sths 
596d5546c5eSMax Reitz     strstart(filename, "file:", &filename);
597d5546c5eSMax Reitz 
5980e7e1989SKevin Wolf     /* Read out options */
599180e9526SHu Tao     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
600c2eb918eSHu Tao                           BDRV_SECTOR_SIZE);
601223d4670Sths 
6023766ef57SKevin Wolf     options = (BlockdevCreateOptions) {
6033766ef57SKevin Wolf         .driver     = BLOCKDEV_DRIVER_FILE,
6043766ef57SKevin Wolf         .u.file     = {
6053766ef57SKevin Wolf             .filename           = (char *) filename,
6063766ef57SKevin Wolf             .size               = total_size,
6073766ef57SKevin Wolf             .has_preallocation  = false,
6083766ef57SKevin Wolf             .has_nocow          = false,
6093766ef57SKevin Wolf         },
6103766ef57SKevin Wolf     };
6113766ef57SKevin Wolf     return raw_co_create(&options, errp);
612c6252b7cSMax Reitz }
613ddef7699SChunyan Liu 
614ddef7699SChunyan Liu static QemuOptsList raw_create_opts = {
615ddef7699SChunyan Liu     .name = "raw-create-opts",
616ddef7699SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
617ddef7699SChunyan Liu     .desc = {
618db08adf5SKevin Wolf         {
619db08adf5SKevin Wolf             .name = BLOCK_OPT_SIZE,
620ddef7699SChunyan Liu             .type = QEMU_OPT_SIZE,
621db08adf5SKevin Wolf             .help = "Virtual disk size"
622db08adf5SKevin Wolf         },
623ddef7699SChunyan Liu         { /* end of list */ }
624ddef7699SChunyan Liu     }
6250e7e1989SKevin Wolf };
6260e7e1989SKevin Wolf 
6275f535a94SMax Reitz BlockDriver bdrv_file = {
62884a12e66SChristoph Hellwig     .format_name	= "file",
62984a12e66SChristoph Hellwig     .protocol_name	= "file",
630f1b2f712Saliguori     .instance_size	= sizeof(BDRVRawState),
631030be321SBenoît Canet     .bdrv_needs_filename = true,
6327dc74db8SMax Reitz     .bdrv_parse_filename = raw_parse_filename,
63366f82ceeSKevin Wolf     .bdrv_file_open     = raw_open,
6342914a1deSEric Blake     .bdrv_refresh_limits = raw_probe_alignment,
635f1b2f712Saliguori     .bdrv_close         = raw_close,
636efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts = raw_co_create_opts,
6373ac21627SPeter Lieven     .bdrv_has_zero_init = bdrv_has_zero_init_1,
638c68b89acSKevin Wolf 
639de7056a3SEric Blake     .bdrv_aio_preadv    = raw_aio_preadv,
640de7056a3SEric Blake     .bdrv_aio_pwritev   = raw_aio_pwritev,
641fc4edb84SPaolo Bonzini     .bdrv_aio_flush     = raw_aio_flush,
642c68b89acSKevin Wolf 
643*061ca8a3SKevin Wolf     .bdrv_co_truncate   = raw_co_truncate,
644223d4670Sths     .bdrv_getlength	= raw_getlength,
6454a1d5e1fSFam Zheng     .bdrv_get_allocated_file_size
6464a1d5e1fSFam Zheng                         = raw_get_allocated_file_size,
6470e7e1989SKevin Wolf 
648ddef7699SChunyan Liu     .create_opts        = &raw_create_opts,
649223d4670Sths };
650223d4670Sths 
651223d4670Sths /***********************************************/
652223d4670Sths /* host device */
653223d4670Sths 
654223d4670Sths static int find_cdrom(char *cdrom_name, int cdrom_name_size)
655223d4670Sths {
656223d4670Sths     char drives[256], *pdrv = drives;
657223d4670Sths     UINT type;
658223d4670Sths 
659223d4670Sths     memset(drives, 0, sizeof(drives));
660223d4670Sths     GetLogicalDriveStrings(sizeof(drives), drives);
661223d4670Sths     while(pdrv[0] != '\0') {
662223d4670Sths         type = GetDriveType(pdrv);
663223d4670Sths         switch(type) {
664223d4670Sths         case DRIVE_CDROM:
665223d4670Sths             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
666223d4670Sths             return 0;
667223d4670Sths             break;
668223d4670Sths         }
669223d4670Sths         pdrv += lstrlen(pdrv) + 1;
670223d4670Sths     }
671223d4670Sths     return -1;
672223d4670Sths }
673223d4670Sths 
674223d4670Sths static int find_device_type(BlockDriverState *bs, const char *filename)
675223d4670Sths {
676223d4670Sths     BDRVRawState *s = bs->opaque;
677223d4670Sths     UINT type;
678223d4670Sths     const char *p;
679223d4670Sths 
680223d4670Sths     if (strstart(filename, "\\\\.\\", &p) ||
681223d4670Sths         strstart(filename, "//./", &p)) {
682223d4670Sths         if (stristart(p, "PhysicalDrive", NULL))
683223d4670Sths             return FTYPE_HARDDISK;
684223d4670Sths         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
685223d4670Sths         type = GetDriveType(s->drive_path);
6863060cd14Saliguori         switch (type) {
6873060cd14Saliguori         case DRIVE_REMOVABLE:
6883060cd14Saliguori         case DRIVE_FIXED:
6893060cd14Saliguori             return FTYPE_HARDDISK;
6903060cd14Saliguori         case DRIVE_CDROM:
691223d4670Sths             return FTYPE_CD;
6923060cd14Saliguori         default:
693223d4670Sths             return FTYPE_FILE;
6943060cd14Saliguori         }
695223d4670Sths     } else {
696223d4670Sths         return FTYPE_FILE;
697223d4670Sths     }
698223d4670Sths }
699223d4670Sths 
700508c7cb3SChristoph Hellwig static int hdev_probe_device(const char *filename)
701508c7cb3SChristoph Hellwig {
702508c7cb3SChristoph Hellwig     if (strstart(filename, "/dev/cdrom", NULL))
703508c7cb3SChristoph Hellwig         return 100;
704508c7cb3SChristoph Hellwig     if (is_windows_drive(filename))
705508c7cb3SChristoph Hellwig         return 100;
706508c7cb3SChristoph Hellwig     return 0;
707508c7cb3SChristoph Hellwig }
708508c7cb3SChristoph Hellwig 
70957ed25b1SMax Reitz static void hdev_parse_filename(const char *filename, QDict *options,
71057ed25b1SMax Reitz                                 Error **errp)
71157ed25b1SMax Reitz {
71203c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
71357ed25b1SMax Reitz }
71457ed25b1SMax Reitz 
715de7056a3SEric Blake static void hdev_refresh_limits(BlockDriverState *bs, Error **errp)
716de7056a3SEric Blake {
717de7056a3SEric Blake     /* XXX Does Windows support AIO on less than 512-byte alignment? */
718de7056a3SEric Blake     bs->bl.request_alignment = 512;
719de7056a3SEric Blake }
720de7056a3SEric Blake 
721015a1036SMax Reitz static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
722015a1036SMax Reitz                      Error **errp)
723223d4670Sths {
724223d4670Sths     BDRVRawState *s = bs->opaque;
725223d4670Sths     int access_flags, create_flags;
72668dc0364SStefan Weil     int ret = 0;
727223d4670Sths     DWORD overlapped;
728223d4670Sths     char device_name[64];
72968dc0364SStefan Weil 
73068dc0364SStefan Weil     Error *local_err = NULL;
73168dc0364SStefan Weil     const char *filename;
7320a4279d9SKevin Wolf     bool use_aio;
73368dc0364SStefan Weil 
73487ea75d5SPeter Crosthwaite     QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
73587ea75d5SPeter Crosthwaite                                       &error_abort);
73668dc0364SStefan Weil     qemu_opts_absorb_qdict(opts, options, &local_err);
73784d18f06SMarkus Armbruster     if (local_err) {
738c6252b7cSMax Reitz         error_propagate(errp, local_err);
73968dc0364SStefan Weil         ret = -EINVAL;
74068dc0364SStefan Weil         goto done;
74168dc0364SStefan Weil     }
74268dc0364SStefan Weil 
74368dc0364SStefan Weil     filename = qemu_opt_get(opts, "filename");
744223d4670Sths 
7450a4279d9SKevin Wolf     use_aio = get_aio_option(opts, flags, &local_err);
7460a4279d9SKevin Wolf     if (!local_err && use_aio) {
7470a4279d9SKevin Wolf         error_setg(&local_err, "AIO is not supported on Windows host devices");
7480a4279d9SKevin Wolf     }
7490a4279d9SKevin Wolf     if (local_err) {
7500a4279d9SKevin Wolf         error_propagate(errp, local_err);
7510a4279d9SKevin Wolf         ret = -EINVAL;
7520a4279d9SKevin Wolf         goto done;
7530a4279d9SKevin Wolf     }
7540a4279d9SKevin Wolf 
755223d4670Sths     if (strstart(filename, "/dev/cdrom", NULL)) {
75668dc0364SStefan Weil         if (find_cdrom(device_name, sizeof(device_name)) < 0) {
757c6252b7cSMax Reitz             error_setg(errp, "Could not open CD-ROM drive");
75868dc0364SStefan Weil             ret = -ENOENT;
75968dc0364SStefan Weil             goto done;
76068dc0364SStefan Weil         }
761223d4670Sths         filename = device_name;
762223d4670Sths     } else {
763223d4670Sths         /* transform drive letters into device name */
764223d4670Sths         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
765223d4670Sths              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
766223d4670Sths             filename[1] == ':' && filename[2] == '\0') {
767223d4670Sths             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
768223d4670Sths             filename = device_name;
769223d4670Sths         }
770223d4670Sths     }
771223d4670Sths     s->type = find_device_type(bs, filename);
772223d4670Sths 
7730a4279d9SKevin Wolf     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
7746a8dc042SJeff Cody 
775223d4670Sths     create_flags = OPEN_EXISTING;
776223d4670Sths 
777223d4670Sths     s->hfile = CreateFile(filename, access_flags,
778223d4670Sths                           FILE_SHARE_READ, NULL,
779223d4670Sths                           create_flags, overlapped, NULL);
780223d4670Sths     if (s->hfile == INVALID_HANDLE_VALUE) {
781223d4670Sths         int err = GetLastError();
782223d4670Sths 
78368dc0364SStefan Weil         if (err == ERROR_ACCESS_DENIED) {
78468dc0364SStefan Weil             ret = -EACCES;
78568dc0364SStefan Weil         } else {
78645d57f6eSMax Reitz             ret = -EINVAL;
787223d4670Sths         }
78845d57f6eSMax Reitz         error_setg_errno(errp, -ret, "Could not open device");
78968dc0364SStefan Weil         goto done;
79068dc0364SStefan Weil     }
79168dc0364SStefan Weil 
79268dc0364SStefan Weil done:
79368dc0364SStefan Weil     qemu_opts_del(opts);
79468dc0364SStefan Weil     return ret;
795223d4670Sths }
796223d4670Sths 
7975efa9d5aSAnthony Liguori static BlockDriver bdrv_host_device = {
798e60f469cSaurel32     .format_name	= "host_device",
79984a12e66SChristoph Hellwig     .protocol_name	= "host_device",
800e60f469cSaurel32     .instance_size	= sizeof(BDRVRawState),
801030be321SBenoît Canet     .bdrv_needs_filename = true,
80257ed25b1SMax Reitz     .bdrv_parse_filename = hdev_parse_filename,
803508c7cb3SChristoph Hellwig     .bdrv_probe_device	= hdev_probe_device,
80466f82ceeSKevin Wolf     .bdrv_file_open	= hdev_open,
805e60f469cSaurel32     .bdrv_close		= raw_close,
806de7056a3SEric Blake     .bdrv_refresh_limits = hdev_refresh_limits,
807223d4670Sths 
808de7056a3SEric Blake     .bdrv_aio_preadv    = raw_aio_preadv,
809de7056a3SEric Blake     .bdrv_aio_pwritev   = raw_aio_pwritev,
810fc4edb84SPaolo Bonzini     .bdrv_aio_flush     = raw_aio_flush,
811c68b89acSKevin Wolf 
81285ebd381SStefan Hajnoczi     .bdrv_detach_aio_context = raw_detach_aio_context,
81385ebd381SStefan Hajnoczi     .bdrv_attach_aio_context = raw_attach_aio_context,
81485ebd381SStefan Hajnoczi 
815223d4670Sths     .bdrv_getlength      = raw_getlength,
816b94a2610SKevin Wolf     .has_variable_length = true,
817b94a2610SKevin Wolf 
8184a1d5e1fSFam Zheng     .bdrv_get_allocated_file_size
8194a1d5e1fSFam Zheng                         = raw_get_allocated_file_size,
820223d4670Sths };
8215efa9d5aSAnthony Liguori 
82284a12e66SChristoph Hellwig static void bdrv_file_init(void)
8235efa9d5aSAnthony Liguori {
82484a12e66SChristoph Hellwig     bdrv_register(&bdrv_file);
8255efa9d5aSAnthony Liguori     bdrv_register(&bdrv_host_device);
8265efa9d5aSAnthony Liguori }
8275efa9d5aSAnthony Liguori 
82884a12e66SChristoph Hellwig block_init(bdrv_file_init);
829