xref: /openbmc/qemu/block/file-win32.c (revision 061ca8a368165fae300748c17971824a089f521f)
1c1bb86cdSEric Blake /*
2c1bb86cdSEric Blake  * Block driver for RAW files (win32)
3c1bb86cdSEric Blake  *
4c1bb86cdSEric Blake  * Copyright (c) 2006 Fabrice Bellard
5c1bb86cdSEric Blake  *
6c1bb86cdSEric Blake  * Permission is hereby granted, free of charge, to any person obtaining a copy
7c1bb86cdSEric Blake  * of this software and associated documentation files (the "Software"), to deal
8c1bb86cdSEric Blake  * in the Software without restriction, including without limitation the rights
9c1bb86cdSEric Blake  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10c1bb86cdSEric Blake  * copies of the Software, and to permit persons to whom the Software is
11c1bb86cdSEric Blake  * furnished to do so, subject to the following conditions:
12c1bb86cdSEric Blake  *
13c1bb86cdSEric Blake  * The above copyright notice and this permission notice shall be included in
14c1bb86cdSEric Blake  * all copies or substantial portions of the Software.
15c1bb86cdSEric Blake  *
16c1bb86cdSEric Blake  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17c1bb86cdSEric Blake  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18c1bb86cdSEric Blake  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19c1bb86cdSEric Blake  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20c1bb86cdSEric Blake  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21c1bb86cdSEric Blake  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22c1bb86cdSEric Blake  * THE SOFTWARE.
23c1bb86cdSEric Blake  */
24922a01a0SMarkus Armbruster 
25c1bb86cdSEric Blake #include "qemu/osdep.h"
26c1bb86cdSEric Blake #include "qapi/error.h"
27c1bb86cdSEric Blake #include "qemu/cutils.h"
28c1bb86cdSEric Blake #include "block/block_int.h"
29c1bb86cdSEric Blake #include "qemu/module.h"
30922a01a0SMarkus Armbruster #include "qemu/option.h"
31c1bb86cdSEric Blake #include "block/raw-aio.h"
32c1bb86cdSEric Blake #include "trace.h"
33c1bb86cdSEric Blake #include "block/thread-pool.h"
34c1bb86cdSEric Blake #include "qemu/iov.h"
35452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
36c1bb86cdSEric Blake #include "qapi/qmp/qstring.h"
37c1bb86cdSEric Blake #include <windows.h>
38c1bb86cdSEric Blake #include <winioctl.h>
39c1bb86cdSEric Blake 
40c1bb86cdSEric Blake #define FTYPE_FILE 0
41c1bb86cdSEric Blake #define FTYPE_CD     1
42c1bb86cdSEric Blake #define FTYPE_HARDDISK 2
43c1bb86cdSEric Blake 
44c1bb86cdSEric Blake typedef struct RawWin32AIOData {
45c1bb86cdSEric Blake     BlockDriverState *bs;
46c1bb86cdSEric Blake     HANDLE hfile;
47c1bb86cdSEric Blake     struct iovec *aio_iov;
48c1bb86cdSEric Blake     int aio_niov;
49c1bb86cdSEric Blake     size_t aio_nbytes;
50c1bb86cdSEric Blake     off64_t aio_offset;
51c1bb86cdSEric Blake     int aio_type;
52c1bb86cdSEric Blake } RawWin32AIOData;
53c1bb86cdSEric Blake 
54c1bb86cdSEric Blake typedef struct BDRVRawState {
55c1bb86cdSEric Blake     HANDLE hfile;
56c1bb86cdSEric Blake     int type;
57c1bb86cdSEric Blake     char drive_path[16]; /* format: "d:\" */
58c1bb86cdSEric Blake     QEMUWin32AIOState *aio;
59c1bb86cdSEric Blake } BDRVRawState;
60c1bb86cdSEric Blake 
61c1bb86cdSEric Blake /*
62c1bb86cdSEric Blake  * Read/writes the data to/from a given linear buffer.
63c1bb86cdSEric Blake  *
64c1bb86cdSEric Blake  * Returns the number of bytes handles or -errno in case of an error. Short
65c1bb86cdSEric Blake  * reads are only returned if the end of the file is reached.
66c1bb86cdSEric Blake  */
67c1bb86cdSEric Blake static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
68c1bb86cdSEric Blake {
69c1bb86cdSEric Blake     size_t offset = 0;
70c1bb86cdSEric Blake     int i;
71c1bb86cdSEric Blake 
72c1bb86cdSEric Blake     for (i = 0; i < aiocb->aio_niov; i++) {
73c1bb86cdSEric Blake         OVERLAPPED ov;
74c1bb86cdSEric Blake         DWORD ret, ret_count, len;
75c1bb86cdSEric Blake 
76c1bb86cdSEric Blake         memset(&ov, 0, sizeof(ov));
77c1bb86cdSEric Blake         ov.Offset = (aiocb->aio_offset + offset);
78c1bb86cdSEric Blake         ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
79c1bb86cdSEric Blake         len = aiocb->aio_iov[i].iov_len;
80c1bb86cdSEric Blake         if (aiocb->aio_type & QEMU_AIO_WRITE) {
81c1bb86cdSEric Blake             ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
82c1bb86cdSEric Blake                             len, &ret_count, &ov);
83c1bb86cdSEric Blake         } else {
84c1bb86cdSEric Blake             ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
85c1bb86cdSEric Blake                            len, &ret_count, &ov);
86c1bb86cdSEric Blake         }
87c1bb86cdSEric Blake         if (!ret) {
88c1bb86cdSEric Blake             ret_count = 0;
89c1bb86cdSEric Blake         }
90c1bb86cdSEric Blake         if (ret_count != len) {
91c1bb86cdSEric Blake             offset += ret_count;
92c1bb86cdSEric Blake             break;
93c1bb86cdSEric Blake         }
94c1bb86cdSEric Blake         offset += len;
95c1bb86cdSEric Blake     }
96c1bb86cdSEric Blake 
97c1bb86cdSEric Blake     return offset;
98c1bb86cdSEric Blake }
99c1bb86cdSEric Blake 
100c1bb86cdSEric Blake static int aio_worker(void *arg)
101c1bb86cdSEric Blake {
102c1bb86cdSEric Blake     RawWin32AIOData *aiocb = arg;
103c1bb86cdSEric Blake     ssize_t ret = 0;
104c1bb86cdSEric Blake     size_t count;
105c1bb86cdSEric Blake 
106c1bb86cdSEric Blake     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
107c1bb86cdSEric Blake     case QEMU_AIO_READ:
108c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
109c1bb86cdSEric Blake         if (count < aiocb->aio_nbytes) {
110c1bb86cdSEric Blake             /* A short read means that we have reached EOF. Pad the buffer
111c1bb86cdSEric Blake              * with zeros for bytes after EOF. */
112c1bb86cdSEric Blake             iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
113c1bb86cdSEric Blake                       0, aiocb->aio_nbytes - count);
114c1bb86cdSEric Blake 
115c1bb86cdSEric Blake             count = aiocb->aio_nbytes;
116c1bb86cdSEric Blake         }
117c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
118c1bb86cdSEric Blake             ret = 0;
119c1bb86cdSEric Blake         } else {
120c1bb86cdSEric Blake             ret = -EINVAL;
121c1bb86cdSEric Blake         }
122c1bb86cdSEric Blake         break;
123c1bb86cdSEric Blake     case QEMU_AIO_WRITE:
124c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
125c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
126c1bb86cdSEric Blake             ret = 0;
127c1bb86cdSEric Blake         } else {
128c1bb86cdSEric Blake             ret = -EINVAL;
129c1bb86cdSEric Blake         }
130c1bb86cdSEric Blake         break;
131c1bb86cdSEric Blake     case QEMU_AIO_FLUSH:
132c1bb86cdSEric Blake         if (!FlushFileBuffers(aiocb->hfile)) {
133c1bb86cdSEric Blake             return -EIO;
134c1bb86cdSEric Blake         }
135c1bb86cdSEric Blake         break;
136c1bb86cdSEric Blake     default:
137c1bb86cdSEric Blake         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
138c1bb86cdSEric Blake         ret = -EINVAL;
139c1bb86cdSEric Blake         break;
140c1bb86cdSEric Blake     }
141c1bb86cdSEric Blake 
142c1bb86cdSEric Blake     g_free(aiocb);
143c1bb86cdSEric Blake     return ret;
144c1bb86cdSEric Blake }
145c1bb86cdSEric Blake 
146c1bb86cdSEric Blake static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
147c1bb86cdSEric Blake         int64_t offset, QEMUIOVector *qiov, int count,
148c1bb86cdSEric Blake         BlockCompletionFunc *cb, void *opaque, int type)
149c1bb86cdSEric Blake {
150c1bb86cdSEric Blake     RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
151c1bb86cdSEric Blake     ThreadPool *pool;
152c1bb86cdSEric Blake 
153c1bb86cdSEric Blake     acb->bs = bs;
154c1bb86cdSEric Blake     acb->hfile = hfile;
155c1bb86cdSEric Blake     acb->aio_type = type;
156c1bb86cdSEric Blake 
157c1bb86cdSEric Blake     if (qiov) {
158c1bb86cdSEric Blake         acb->aio_iov = qiov->iov;
159c1bb86cdSEric Blake         acb->aio_niov = qiov->niov;
160c1bb86cdSEric Blake         assert(qiov->size == count);
161c1bb86cdSEric Blake     }
162c1bb86cdSEric Blake     acb->aio_nbytes = count;
163c1bb86cdSEric Blake     acb->aio_offset = offset;
164c1bb86cdSEric Blake 
165c1bb86cdSEric Blake     trace_paio_submit(acb, opaque, offset, count, type);
166c1bb86cdSEric Blake     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
167c1bb86cdSEric Blake     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
168c1bb86cdSEric Blake }
169c1bb86cdSEric Blake 
170c1bb86cdSEric Blake int qemu_ftruncate64(int fd, int64_t length)
171c1bb86cdSEric Blake {
172c1bb86cdSEric Blake     LARGE_INTEGER li;
173c1bb86cdSEric Blake     DWORD dw;
174c1bb86cdSEric Blake     LONG high;
175c1bb86cdSEric Blake     HANDLE h;
176c1bb86cdSEric Blake     BOOL res;
177c1bb86cdSEric Blake 
178c1bb86cdSEric Blake     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
179c1bb86cdSEric Blake 	return -1;
180c1bb86cdSEric Blake 
181c1bb86cdSEric Blake     h = (HANDLE)_get_osfhandle(fd);
182c1bb86cdSEric Blake 
183c1bb86cdSEric Blake     /* get current position, ftruncate do not change position */
184c1bb86cdSEric Blake     li.HighPart = 0;
185c1bb86cdSEric Blake     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
186c1bb86cdSEric Blake     if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
187c1bb86cdSEric Blake 	return -1;
188c1bb86cdSEric Blake     }
189c1bb86cdSEric Blake 
190c1bb86cdSEric Blake     high = length >> 32;
191c1bb86cdSEric Blake     dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
192c1bb86cdSEric Blake     if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
193c1bb86cdSEric Blake 	return -1;
194c1bb86cdSEric Blake     }
195c1bb86cdSEric Blake     res = SetEndOfFile(h);
196c1bb86cdSEric Blake 
197c1bb86cdSEric Blake     /* back to old position */
198c1bb86cdSEric Blake     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
199c1bb86cdSEric Blake     return res ? 0 : -1;
200c1bb86cdSEric Blake }
201c1bb86cdSEric Blake 
202c1bb86cdSEric Blake static int set_sparse(int fd)
203c1bb86cdSEric Blake {
204c1bb86cdSEric Blake     DWORD returned;
205c1bb86cdSEric Blake     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
206c1bb86cdSEric Blake 				 NULL, 0, NULL, 0, &returned, NULL);
207c1bb86cdSEric Blake }
208c1bb86cdSEric Blake 
209c1bb86cdSEric Blake static void raw_detach_aio_context(BlockDriverState *bs)
210c1bb86cdSEric Blake {
211c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
212c1bb86cdSEric Blake 
213c1bb86cdSEric Blake     if (s->aio) {
214c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
215c1bb86cdSEric Blake     }
216c1bb86cdSEric Blake }
217c1bb86cdSEric Blake 
218c1bb86cdSEric Blake static void raw_attach_aio_context(BlockDriverState *bs,
219c1bb86cdSEric Blake                                    AioContext *new_context)
220c1bb86cdSEric Blake {
221c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
222c1bb86cdSEric Blake 
223c1bb86cdSEric Blake     if (s->aio) {
224c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, new_context);
225c1bb86cdSEric Blake     }
226c1bb86cdSEric Blake }
227c1bb86cdSEric Blake 
228c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
229c1bb86cdSEric Blake {
230c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
231c1bb86cdSEric Blake     DWORD sectorsPerCluster, freeClusters, totalClusters, count;
232c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
233c1bb86cdSEric Blake     BOOL status;
234c1bb86cdSEric Blake 
235c1bb86cdSEric Blake     if (s->type == FTYPE_CD) {
236c1bb86cdSEric Blake         bs->bl.request_alignment = 2048;
237c1bb86cdSEric Blake         return;
238c1bb86cdSEric Blake     }
239c1bb86cdSEric Blake     if (s->type == FTYPE_HARDDISK) {
240c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
241c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
242c1bb86cdSEric Blake         if (status != 0) {
243c1bb86cdSEric Blake             bs->bl.request_alignment = dg.Geometry.BytesPerSector;
244c1bb86cdSEric Blake             return;
245c1bb86cdSEric Blake         }
246c1bb86cdSEric Blake         /* try GetDiskFreeSpace too */
247c1bb86cdSEric Blake     }
248c1bb86cdSEric Blake 
249c1bb86cdSEric Blake     if (s->drive_path[0]) {
250c1bb86cdSEric Blake         GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
251c1bb86cdSEric Blake                          &dg.Geometry.BytesPerSector,
252c1bb86cdSEric Blake                          &freeClusters, &totalClusters);
253c1bb86cdSEric Blake         bs->bl.request_alignment = dg.Geometry.BytesPerSector;
254de7056a3SEric Blake         return;
255c1bb86cdSEric Blake     }
256de7056a3SEric Blake 
257de7056a3SEric Blake     /* XXX Does Windows support AIO on less than 512-byte alignment? */
258de7056a3SEric Blake     bs->bl.request_alignment = 512;
259c1bb86cdSEric Blake }
260c1bb86cdSEric Blake 
261c1bb86cdSEric Blake static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
262c1bb86cdSEric Blake                             DWORD *overlapped)
263c1bb86cdSEric Blake {
264c1bb86cdSEric Blake     assert(access_flags != NULL);
265c1bb86cdSEric Blake     assert(overlapped != NULL);
266c1bb86cdSEric Blake 
267c1bb86cdSEric Blake     if (flags & BDRV_O_RDWR) {
268c1bb86cdSEric Blake         *access_flags = GENERIC_READ | GENERIC_WRITE;
269c1bb86cdSEric Blake     } else {
270c1bb86cdSEric Blake         *access_flags = GENERIC_READ;
271c1bb86cdSEric Blake     }
272c1bb86cdSEric Blake 
273c1bb86cdSEric Blake     *overlapped = FILE_ATTRIBUTE_NORMAL;
274c1bb86cdSEric Blake     if (use_aio) {
275c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_OVERLAPPED;
276c1bb86cdSEric Blake     }
277c1bb86cdSEric Blake     if (flags & BDRV_O_NOCACHE) {
278c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_NO_BUFFERING;
279c1bb86cdSEric Blake     }
280c1bb86cdSEric Blake }
281c1bb86cdSEric Blake 
282c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options,
283c1bb86cdSEric Blake                                Error **errp)
284c1bb86cdSEric Blake {
28503c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "file:", options);
286c1bb86cdSEric Blake }
287c1bb86cdSEric Blake 
288c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = {
289c1bb86cdSEric Blake     .name = "raw",
290c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
291c1bb86cdSEric Blake     .desc = {
292c1bb86cdSEric Blake         {
293c1bb86cdSEric Blake             .name = "filename",
294c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
295c1bb86cdSEric Blake             .help = "File name of the image",
296c1bb86cdSEric Blake         },
297c1bb86cdSEric Blake         {
298c1bb86cdSEric Blake             .name = "aio",
299c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
300c1bb86cdSEric Blake             .help = "host AIO implementation (threads, native)",
301c1bb86cdSEric Blake         },
302c1bb86cdSEric Blake         { /* end of list */ }
303c1bb86cdSEric Blake     },
304c1bb86cdSEric Blake };
305c1bb86cdSEric Blake 
306c1bb86cdSEric Blake static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
307c1bb86cdSEric Blake {
308c1bb86cdSEric Blake     BlockdevAioOptions aio, aio_default;
309c1bb86cdSEric Blake 
310c1bb86cdSEric Blake     aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
311c1bb86cdSEric Blake                                               : BLOCKDEV_AIO_OPTIONS_THREADS;
312f7abe0ecSMarc-André Lureau     aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
31306c60b6cSMarkus Armbruster                           aio_default, errp);
314c1bb86cdSEric Blake 
315c1bb86cdSEric Blake     switch (aio) {
316c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_NATIVE:
317c1bb86cdSEric Blake         return true;
318c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_THREADS:
319c1bb86cdSEric Blake         return false;
320c1bb86cdSEric Blake     default:
321c1bb86cdSEric Blake         error_setg(errp, "Invalid AIO option");
322c1bb86cdSEric Blake     }
323c1bb86cdSEric Blake     return false;
324c1bb86cdSEric Blake }
325c1bb86cdSEric Blake 
326c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags,
327c1bb86cdSEric Blake                     Error **errp)
328c1bb86cdSEric Blake {
329c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
330c1bb86cdSEric Blake     int access_flags;
331c1bb86cdSEric Blake     DWORD overlapped;
332c1bb86cdSEric Blake     QemuOpts *opts;
333c1bb86cdSEric Blake     Error *local_err = NULL;
334c1bb86cdSEric Blake     const char *filename;
335c1bb86cdSEric Blake     bool use_aio;
336c1bb86cdSEric Blake     int ret;
337c1bb86cdSEric Blake 
338c1bb86cdSEric Blake     s->type = FTYPE_FILE;
339c1bb86cdSEric Blake 
340c1bb86cdSEric Blake     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
341c1bb86cdSEric Blake     qemu_opts_absorb_qdict(opts, options, &local_err);
342c1bb86cdSEric Blake     if (local_err) {
343c1bb86cdSEric Blake         error_propagate(errp, local_err);
344c1bb86cdSEric Blake         ret = -EINVAL;
345c1bb86cdSEric Blake         goto fail;
346c1bb86cdSEric Blake     }
347c1bb86cdSEric Blake 
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 
354c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
355c1bb86cdSEric Blake 
356c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
357c1bb86cdSEric Blake     if (local_err) {
358c1bb86cdSEric Blake         error_propagate(errp, local_err);
359c1bb86cdSEric Blake         ret = -EINVAL;
360c1bb86cdSEric Blake         goto fail;
361c1bb86cdSEric Blake     }
362c1bb86cdSEric Blake 
363c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
364c1bb86cdSEric Blake 
365c1bb86cdSEric Blake     if (filename[0] && filename[1] == ':') {
366c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
367c1bb86cdSEric Blake     } else if (filename[0] == '\\' && filename[1] == '\\') {
368c1bb86cdSEric Blake         s->drive_path[0] = 0;
369c1bb86cdSEric Blake     } else {
370c1bb86cdSEric Blake         /* Relative path.  */
371c1bb86cdSEric Blake         char buf[MAX_PATH];
372c1bb86cdSEric Blake         GetCurrentDirectory(MAX_PATH, buf);
373c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
374c1bb86cdSEric Blake     }
375c1bb86cdSEric Blake 
376c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
377c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
378c1bb86cdSEric Blake                           OPEN_EXISTING, overlapped, NULL);
379c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
380c1bb86cdSEric Blake         int err = GetLastError();
381c1bb86cdSEric Blake 
382c1bb86cdSEric Blake         error_setg_win32(errp, err, "Could not open '%s'", filename);
383c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
384c1bb86cdSEric Blake             ret = -EACCES;
385c1bb86cdSEric Blake         } else {
386c1bb86cdSEric Blake             ret = -EINVAL;
387c1bb86cdSEric Blake         }
388c1bb86cdSEric Blake         goto fail;
389c1bb86cdSEric Blake     }
390c1bb86cdSEric Blake 
391c1bb86cdSEric Blake     if (use_aio) {
392c1bb86cdSEric Blake         s->aio = win32_aio_init();
393c1bb86cdSEric Blake         if (s->aio == NULL) {
394c1bb86cdSEric Blake             CloseHandle(s->hfile);
395c1bb86cdSEric Blake             error_setg(errp, "Could not initialize AIO");
396c1bb86cdSEric Blake             ret = -EINVAL;
397c1bb86cdSEric Blake             goto fail;
398c1bb86cdSEric Blake         }
399c1bb86cdSEric Blake 
400c1bb86cdSEric Blake         ret = win32_aio_attach(s->aio, s->hfile);
401c1bb86cdSEric Blake         if (ret < 0) {
402c1bb86cdSEric Blake             win32_aio_cleanup(s->aio);
403c1bb86cdSEric Blake             CloseHandle(s->hfile);
404c1bb86cdSEric Blake             error_setg_errno(errp, -ret, "Could not enable AIO");
405c1bb86cdSEric Blake             goto fail;
406c1bb86cdSEric Blake         }
407c1bb86cdSEric Blake 
408c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
409c1bb86cdSEric Blake     }
410c1bb86cdSEric Blake 
411c1bb86cdSEric Blake     ret = 0;
412c1bb86cdSEric Blake fail:
413c1bb86cdSEric Blake     qemu_opts_del(opts);
414c1bb86cdSEric Blake     return ret;
415c1bb86cdSEric Blake }
416c1bb86cdSEric Blake 
417de7056a3SEric Blake static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
418de7056a3SEric Blake                                   uint64_t offset, uint64_t bytes,
419de7056a3SEric Blake                                   QEMUIOVector *qiov, int flags,
420c1bb86cdSEric Blake                                   BlockCompletionFunc *cb, void *opaque)
421c1bb86cdSEric Blake {
422c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
423c1bb86cdSEric Blake     if (s->aio) {
424de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
425de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_READ);
426c1bb86cdSEric Blake     } else {
427de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
428c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_READ);
429c1bb86cdSEric Blake     }
430c1bb86cdSEric Blake }
431c1bb86cdSEric Blake 
432de7056a3SEric Blake static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
433de7056a3SEric Blake                                    uint64_t offset, uint64_t bytes,
434de7056a3SEric Blake                                    QEMUIOVector *qiov, int flags,
435c1bb86cdSEric Blake                                    BlockCompletionFunc *cb, void *opaque)
436c1bb86cdSEric Blake {
437c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
438c1bb86cdSEric Blake     if (s->aio) {
439de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
440de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_WRITE);
441c1bb86cdSEric Blake     } else {
442de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
443c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_WRITE);
444c1bb86cdSEric Blake     }
445c1bb86cdSEric Blake }
446c1bb86cdSEric Blake 
447c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
448c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
449c1bb86cdSEric Blake {
450c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
451c1bb86cdSEric Blake     return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
452c1bb86cdSEric Blake }
453c1bb86cdSEric Blake 
454c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs)
455c1bb86cdSEric Blake {
456c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
457c1bb86cdSEric Blake 
458c1bb86cdSEric Blake     if (s->aio) {
459c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
460c1bb86cdSEric Blake         win32_aio_cleanup(s->aio);
461c1bb86cdSEric Blake         s->aio = NULL;
462c1bb86cdSEric Blake     }
463c1bb86cdSEric Blake 
464c1bb86cdSEric Blake     CloseHandle(s->hfile);
465c1bb86cdSEric Blake     if (bs->open_flags & BDRV_O_TEMPORARY) {
466c1bb86cdSEric Blake         unlink(bs->filename);
467c1bb86cdSEric Blake     }
468c1bb86cdSEric Blake }
469c1bb86cdSEric Blake 
470*061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
4718243ccb7SMax Reitz                                         PreallocMode prealloc, Error **errp)
472c1bb86cdSEric Blake {
473c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
474c1bb86cdSEric Blake     LONG low, high;
475c1bb86cdSEric Blake     DWORD dwPtrLow;
476c1bb86cdSEric Blake 
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 
483c1bb86cdSEric Blake     low = offset;
484c1bb86cdSEric Blake     high = offset >> 32;
485c1bb86cdSEric Blake 
486c1bb86cdSEric Blake     /*
487c1bb86cdSEric Blake      * An error has occurred if the return value is INVALID_SET_FILE_POINTER
488c1bb86cdSEric Blake      * and GetLastError doesn't return NO_ERROR.
489c1bb86cdSEric Blake      */
490c1bb86cdSEric Blake     dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
491c1bb86cdSEric Blake     if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
4924bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetFilePointer error");
493c1bb86cdSEric Blake         return -EIO;
494c1bb86cdSEric Blake     }
495c1bb86cdSEric Blake     if (SetEndOfFile(s->hfile) == 0) {
4964bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
497c1bb86cdSEric Blake         return -EIO;
498c1bb86cdSEric Blake     }
499c1bb86cdSEric Blake     return 0;
500c1bb86cdSEric Blake }
501c1bb86cdSEric Blake 
502c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs)
503c1bb86cdSEric Blake {
504c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
505c1bb86cdSEric Blake     LARGE_INTEGER l;
506c1bb86cdSEric Blake     ULARGE_INTEGER available, total, total_free;
507c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
508c1bb86cdSEric Blake     DWORD count;
509c1bb86cdSEric Blake     BOOL status;
510c1bb86cdSEric Blake 
511c1bb86cdSEric Blake     switch(s->type) {
512c1bb86cdSEric Blake     case FTYPE_FILE:
513c1bb86cdSEric Blake         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
514c1bb86cdSEric Blake         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
515c1bb86cdSEric Blake             return -EIO;
516c1bb86cdSEric Blake         break;
517c1bb86cdSEric Blake     case FTYPE_CD:
518c1bb86cdSEric Blake         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
519c1bb86cdSEric Blake             return -EIO;
520c1bb86cdSEric Blake         l.QuadPart = total.QuadPart;
521c1bb86cdSEric Blake         break;
522c1bb86cdSEric Blake     case FTYPE_HARDDISK:
523c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
524c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
525c1bb86cdSEric Blake         if (status != 0) {
526c1bb86cdSEric Blake             l = dg.DiskSize;
527c1bb86cdSEric Blake         }
528c1bb86cdSEric Blake         break;
529c1bb86cdSEric Blake     default:
530c1bb86cdSEric Blake         return -EIO;
531c1bb86cdSEric Blake     }
532c1bb86cdSEric Blake     return l.QuadPart;
533c1bb86cdSEric Blake }
534c1bb86cdSEric Blake 
535c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
536c1bb86cdSEric Blake {
537c1bb86cdSEric Blake     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
538c1bb86cdSEric Blake                                               DWORD * high);
539c1bb86cdSEric Blake     get_compressed_t get_compressed;
540c1bb86cdSEric Blake     struct _stati64 st;
541c1bb86cdSEric Blake     const char *filename = bs->filename;
542c1bb86cdSEric Blake     /* WinNT support GetCompressedFileSize to determine allocate size */
543c1bb86cdSEric Blake     get_compressed =
544c1bb86cdSEric Blake         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
545c1bb86cdSEric Blake                                             "GetCompressedFileSizeA");
546c1bb86cdSEric Blake     if (get_compressed) {
547c1bb86cdSEric Blake         DWORD high, low;
548c1bb86cdSEric Blake         low = get_compressed(filename, &high);
549c1bb86cdSEric Blake         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
550c1bb86cdSEric Blake             return (((int64_t) high) << 32) + low;
551c1bb86cdSEric Blake         }
552c1bb86cdSEric Blake     }
553c1bb86cdSEric Blake 
554c1bb86cdSEric Blake     if (_stati64(filename, &st) < 0) {
555c1bb86cdSEric Blake         return -1;
556c1bb86cdSEric Blake     }
557c1bb86cdSEric Blake     return st.st_size;
558c1bb86cdSEric Blake }
559c1bb86cdSEric Blake 
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)
592c1bb86cdSEric Blake {
5933766ef57SKevin Wolf     BlockdevCreateOptions options;
594c1bb86cdSEric Blake     int64_t total_size = 0;
595c1bb86cdSEric Blake 
596c1bb86cdSEric Blake     strstart(filename, "file:", &filename);
597c1bb86cdSEric Blake 
598c1bb86cdSEric Blake     /* Read out options */
599c1bb86cdSEric Blake     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
600c1bb86cdSEric Blake                           BDRV_SECTOR_SIZE);
601c1bb86cdSEric Blake 
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);
612c1bb86cdSEric Blake }
613c1bb86cdSEric Blake 
614c1bb86cdSEric Blake static QemuOptsList raw_create_opts = {
615c1bb86cdSEric Blake     .name = "raw-create-opts",
616c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
617c1bb86cdSEric Blake     .desc = {
618c1bb86cdSEric Blake         {
619c1bb86cdSEric Blake             .name = BLOCK_OPT_SIZE,
620c1bb86cdSEric Blake             .type = QEMU_OPT_SIZE,
621c1bb86cdSEric Blake             .help = "Virtual disk size"
622c1bb86cdSEric Blake         },
623c1bb86cdSEric Blake         { /* end of list */ }
624c1bb86cdSEric Blake     }
625c1bb86cdSEric Blake };
626c1bb86cdSEric Blake 
627c1bb86cdSEric Blake BlockDriver bdrv_file = {
628c1bb86cdSEric Blake     .format_name	= "file",
629c1bb86cdSEric Blake     .protocol_name	= "file",
630c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
631c1bb86cdSEric Blake     .bdrv_needs_filename = true,
632c1bb86cdSEric Blake     .bdrv_parse_filename = raw_parse_filename,
633c1bb86cdSEric Blake     .bdrv_file_open     = raw_open,
634c1bb86cdSEric Blake     .bdrv_refresh_limits = raw_probe_alignment,
635c1bb86cdSEric Blake     .bdrv_close         = raw_close,
636efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts = raw_co_create_opts,
637c1bb86cdSEric Blake     .bdrv_has_zero_init = bdrv_has_zero_init_1,
638c1bb86cdSEric Blake 
639de7056a3SEric Blake     .bdrv_aio_preadv    = raw_aio_preadv,
640de7056a3SEric Blake     .bdrv_aio_pwritev   = raw_aio_pwritev,
641c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
642c1bb86cdSEric Blake 
643*061ca8a3SKevin Wolf     .bdrv_co_truncate   = raw_co_truncate,
644c1bb86cdSEric Blake     .bdrv_getlength	= raw_getlength,
645c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
646c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
647c1bb86cdSEric Blake 
648c1bb86cdSEric Blake     .create_opts        = &raw_create_opts,
649c1bb86cdSEric Blake };
650c1bb86cdSEric Blake 
651c1bb86cdSEric Blake /***********************************************/
652c1bb86cdSEric Blake /* host device */
653c1bb86cdSEric Blake 
654c1bb86cdSEric Blake static int find_cdrom(char *cdrom_name, int cdrom_name_size)
655c1bb86cdSEric Blake {
656c1bb86cdSEric Blake     char drives[256], *pdrv = drives;
657c1bb86cdSEric Blake     UINT type;
658c1bb86cdSEric Blake 
659c1bb86cdSEric Blake     memset(drives, 0, sizeof(drives));
660c1bb86cdSEric Blake     GetLogicalDriveStrings(sizeof(drives), drives);
661c1bb86cdSEric Blake     while(pdrv[0] != '\0') {
662c1bb86cdSEric Blake         type = GetDriveType(pdrv);
663c1bb86cdSEric Blake         switch(type) {
664c1bb86cdSEric Blake         case DRIVE_CDROM:
665c1bb86cdSEric Blake             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
666c1bb86cdSEric Blake             return 0;
667c1bb86cdSEric Blake             break;
668c1bb86cdSEric Blake         }
669c1bb86cdSEric Blake         pdrv += lstrlen(pdrv) + 1;
670c1bb86cdSEric Blake     }
671c1bb86cdSEric Blake     return -1;
672c1bb86cdSEric Blake }
673c1bb86cdSEric Blake 
674c1bb86cdSEric Blake static int find_device_type(BlockDriverState *bs, const char *filename)
675c1bb86cdSEric Blake {
676c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
677c1bb86cdSEric Blake     UINT type;
678c1bb86cdSEric Blake     const char *p;
679c1bb86cdSEric Blake 
680c1bb86cdSEric Blake     if (strstart(filename, "\\\\.\\", &p) ||
681c1bb86cdSEric Blake         strstart(filename, "//./", &p)) {
682c1bb86cdSEric Blake         if (stristart(p, "PhysicalDrive", NULL))
683c1bb86cdSEric Blake             return FTYPE_HARDDISK;
684c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
685c1bb86cdSEric Blake         type = GetDriveType(s->drive_path);
686c1bb86cdSEric Blake         switch (type) {
687c1bb86cdSEric Blake         case DRIVE_REMOVABLE:
688c1bb86cdSEric Blake         case DRIVE_FIXED:
689c1bb86cdSEric Blake             return FTYPE_HARDDISK;
690c1bb86cdSEric Blake         case DRIVE_CDROM:
691c1bb86cdSEric Blake             return FTYPE_CD;
692c1bb86cdSEric Blake         default:
693c1bb86cdSEric Blake             return FTYPE_FILE;
694c1bb86cdSEric Blake         }
695c1bb86cdSEric Blake     } else {
696c1bb86cdSEric Blake         return FTYPE_FILE;
697c1bb86cdSEric Blake     }
698c1bb86cdSEric Blake }
699c1bb86cdSEric Blake 
700c1bb86cdSEric Blake static int hdev_probe_device(const char *filename)
701c1bb86cdSEric Blake {
702c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL))
703c1bb86cdSEric Blake         return 100;
704c1bb86cdSEric Blake     if (is_windows_drive(filename))
705c1bb86cdSEric Blake         return 100;
706c1bb86cdSEric Blake     return 0;
707c1bb86cdSEric Blake }
708c1bb86cdSEric Blake 
709c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options,
710c1bb86cdSEric Blake                                 Error **errp)
711c1bb86cdSEric Blake {
71203c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
713c1bb86cdSEric Blake }
714c1bb86cdSEric Blake 
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 
721c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
722c1bb86cdSEric Blake                      Error **errp)
723c1bb86cdSEric Blake {
724c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
725c1bb86cdSEric Blake     int access_flags, create_flags;
726c1bb86cdSEric Blake     int ret = 0;
727c1bb86cdSEric Blake     DWORD overlapped;
728c1bb86cdSEric Blake     char device_name[64];
729c1bb86cdSEric Blake 
730c1bb86cdSEric Blake     Error *local_err = NULL;
731c1bb86cdSEric Blake     const char *filename;
732c1bb86cdSEric Blake     bool use_aio;
733c1bb86cdSEric Blake 
734c1bb86cdSEric Blake     QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
735c1bb86cdSEric Blake                                       &error_abort);
736c1bb86cdSEric Blake     qemu_opts_absorb_qdict(opts, options, &local_err);
737c1bb86cdSEric Blake     if (local_err) {
738c1bb86cdSEric Blake         error_propagate(errp, local_err);
739c1bb86cdSEric Blake         ret = -EINVAL;
740c1bb86cdSEric Blake         goto done;
741c1bb86cdSEric Blake     }
742c1bb86cdSEric Blake 
743c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
744c1bb86cdSEric Blake 
745c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
746c1bb86cdSEric Blake     if (!local_err && use_aio) {
747c1bb86cdSEric Blake         error_setg(&local_err, "AIO is not supported on Windows host devices");
748c1bb86cdSEric Blake     }
749c1bb86cdSEric Blake     if (local_err) {
750c1bb86cdSEric Blake         error_propagate(errp, local_err);
751c1bb86cdSEric Blake         ret = -EINVAL;
752c1bb86cdSEric Blake         goto done;
753c1bb86cdSEric Blake     }
754c1bb86cdSEric Blake 
755c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL)) {
756c1bb86cdSEric Blake         if (find_cdrom(device_name, sizeof(device_name)) < 0) {
757c1bb86cdSEric Blake             error_setg(errp, "Could not open CD-ROM drive");
758c1bb86cdSEric Blake             ret = -ENOENT;
759c1bb86cdSEric Blake             goto done;
760c1bb86cdSEric Blake         }
761c1bb86cdSEric Blake         filename = device_name;
762c1bb86cdSEric Blake     } else {
763c1bb86cdSEric Blake         /* transform drive letters into device name */
764c1bb86cdSEric Blake         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
765c1bb86cdSEric Blake              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
766c1bb86cdSEric Blake             filename[1] == ':' && filename[2] == '\0') {
767c1bb86cdSEric Blake             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
768c1bb86cdSEric Blake             filename = device_name;
769c1bb86cdSEric Blake         }
770c1bb86cdSEric Blake     }
771c1bb86cdSEric Blake     s->type = find_device_type(bs, filename);
772c1bb86cdSEric Blake 
773c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
774c1bb86cdSEric Blake 
775c1bb86cdSEric Blake     create_flags = OPEN_EXISTING;
776c1bb86cdSEric Blake 
777c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
778c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
779c1bb86cdSEric Blake                           create_flags, overlapped, NULL);
780c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
781c1bb86cdSEric Blake         int err = GetLastError();
782c1bb86cdSEric Blake 
783c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
784c1bb86cdSEric Blake             ret = -EACCES;
785c1bb86cdSEric Blake         } else {
786c1bb86cdSEric Blake             ret = -EINVAL;
787c1bb86cdSEric Blake         }
788c1bb86cdSEric Blake         error_setg_errno(errp, -ret, "Could not open device");
789c1bb86cdSEric Blake         goto done;
790c1bb86cdSEric Blake     }
791c1bb86cdSEric Blake 
792c1bb86cdSEric Blake done:
793c1bb86cdSEric Blake     qemu_opts_del(opts);
794c1bb86cdSEric Blake     return ret;
795c1bb86cdSEric Blake }
796c1bb86cdSEric Blake 
797c1bb86cdSEric Blake static BlockDriver bdrv_host_device = {
798c1bb86cdSEric Blake     .format_name	= "host_device",
799c1bb86cdSEric Blake     .protocol_name	= "host_device",
800c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
801c1bb86cdSEric Blake     .bdrv_needs_filename = true,
802c1bb86cdSEric Blake     .bdrv_parse_filename = hdev_parse_filename,
803c1bb86cdSEric Blake     .bdrv_probe_device	= hdev_probe_device,
804c1bb86cdSEric Blake     .bdrv_file_open	= hdev_open,
805c1bb86cdSEric Blake     .bdrv_close		= raw_close,
806de7056a3SEric Blake     .bdrv_refresh_limits = hdev_refresh_limits,
807c1bb86cdSEric Blake 
808de7056a3SEric Blake     .bdrv_aio_preadv    = raw_aio_preadv,
809de7056a3SEric Blake     .bdrv_aio_pwritev   = raw_aio_pwritev,
810c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
811c1bb86cdSEric Blake 
812c1bb86cdSEric Blake     .bdrv_detach_aio_context = raw_detach_aio_context,
813c1bb86cdSEric Blake     .bdrv_attach_aio_context = raw_attach_aio_context,
814c1bb86cdSEric Blake 
815c1bb86cdSEric Blake     .bdrv_getlength      = raw_getlength,
816c1bb86cdSEric Blake     .has_variable_length = true,
817c1bb86cdSEric Blake 
818c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
819c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
820c1bb86cdSEric Blake };
821c1bb86cdSEric Blake 
822c1bb86cdSEric Blake static void bdrv_file_init(void)
823c1bb86cdSEric Blake {
824c1bb86cdSEric Blake     bdrv_register(&bdrv_file);
825c1bb86cdSEric Blake     bdrv_register(&bdrv_host_device);
826c1bb86cdSEric Blake }
827c1bb86cdSEric Blake 
828c1bb86cdSEric Blake block_init(bdrv_file_init);
829