xref: /openbmc/qemu/block/file-win32.c (revision 452fcdbc49c59884c8c284268d64baa24fea11e1)
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  */
24c1bb86cdSEric Blake #include "qemu/osdep.h"
25c1bb86cdSEric Blake #include "qapi/error.h"
26c1bb86cdSEric Blake #include "qemu/cutils.h"
27c1bb86cdSEric Blake #include "block/block_int.h"
28c1bb86cdSEric Blake #include "qemu/module.h"
29c1bb86cdSEric Blake #include "block/raw-aio.h"
30c1bb86cdSEric Blake #include "trace.h"
31c1bb86cdSEric Blake #include "block/thread-pool.h"
32c1bb86cdSEric Blake #include "qemu/iov.h"
33*452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
34c1bb86cdSEric Blake #include "qapi/qmp/qstring.h"
35c1bb86cdSEric Blake #include <windows.h>
36c1bb86cdSEric Blake #include <winioctl.h>
37c1bb86cdSEric Blake 
38c1bb86cdSEric Blake #define FTYPE_FILE 0
39c1bb86cdSEric Blake #define FTYPE_CD     1
40c1bb86cdSEric Blake #define FTYPE_HARDDISK 2
41c1bb86cdSEric Blake 
42c1bb86cdSEric Blake typedef struct RawWin32AIOData {
43c1bb86cdSEric Blake     BlockDriverState *bs;
44c1bb86cdSEric Blake     HANDLE hfile;
45c1bb86cdSEric Blake     struct iovec *aio_iov;
46c1bb86cdSEric Blake     int aio_niov;
47c1bb86cdSEric Blake     size_t aio_nbytes;
48c1bb86cdSEric Blake     off64_t aio_offset;
49c1bb86cdSEric Blake     int aio_type;
50c1bb86cdSEric Blake } RawWin32AIOData;
51c1bb86cdSEric Blake 
52c1bb86cdSEric Blake typedef struct BDRVRawState {
53c1bb86cdSEric Blake     HANDLE hfile;
54c1bb86cdSEric Blake     int type;
55c1bb86cdSEric Blake     char drive_path[16]; /* format: "d:\" */
56c1bb86cdSEric Blake     QEMUWin32AIOState *aio;
57c1bb86cdSEric Blake } BDRVRawState;
58c1bb86cdSEric Blake 
59c1bb86cdSEric Blake /*
60c1bb86cdSEric Blake  * Read/writes the data to/from a given linear buffer.
61c1bb86cdSEric Blake  *
62c1bb86cdSEric Blake  * Returns the number of bytes handles or -errno in case of an error. Short
63c1bb86cdSEric Blake  * reads are only returned if the end of the file is reached.
64c1bb86cdSEric Blake  */
65c1bb86cdSEric Blake static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
66c1bb86cdSEric Blake {
67c1bb86cdSEric Blake     size_t offset = 0;
68c1bb86cdSEric Blake     int i;
69c1bb86cdSEric Blake 
70c1bb86cdSEric Blake     for (i = 0; i < aiocb->aio_niov; i++) {
71c1bb86cdSEric Blake         OVERLAPPED ov;
72c1bb86cdSEric Blake         DWORD ret, ret_count, len;
73c1bb86cdSEric Blake 
74c1bb86cdSEric Blake         memset(&ov, 0, sizeof(ov));
75c1bb86cdSEric Blake         ov.Offset = (aiocb->aio_offset + offset);
76c1bb86cdSEric Blake         ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
77c1bb86cdSEric Blake         len = aiocb->aio_iov[i].iov_len;
78c1bb86cdSEric Blake         if (aiocb->aio_type & QEMU_AIO_WRITE) {
79c1bb86cdSEric Blake             ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
80c1bb86cdSEric Blake                             len, &ret_count, &ov);
81c1bb86cdSEric Blake         } else {
82c1bb86cdSEric Blake             ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
83c1bb86cdSEric Blake                            len, &ret_count, &ov);
84c1bb86cdSEric Blake         }
85c1bb86cdSEric Blake         if (!ret) {
86c1bb86cdSEric Blake             ret_count = 0;
87c1bb86cdSEric Blake         }
88c1bb86cdSEric Blake         if (ret_count != len) {
89c1bb86cdSEric Blake             offset += ret_count;
90c1bb86cdSEric Blake             break;
91c1bb86cdSEric Blake         }
92c1bb86cdSEric Blake         offset += len;
93c1bb86cdSEric Blake     }
94c1bb86cdSEric Blake 
95c1bb86cdSEric Blake     return offset;
96c1bb86cdSEric Blake }
97c1bb86cdSEric Blake 
98c1bb86cdSEric Blake static int aio_worker(void *arg)
99c1bb86cdSEric Blake {
100c1bb86cdSEric Blake     RawWin32AIOData *aiocb = arg;
101c1bb86cdSEric Blake     ssize_t ret = 0;
102c1bb86cdSEric Blake     size_t count;
103c1bb86cdSEric Blake 
104c1bb86cdSEric Blake     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
105c1bb86cdSEric Blake     case QEMU_AIO_READ:
106c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
107c1bb86cdSEric Blake         if (count < aiocb->aio_nbytes) {
108c1bb86cdSEric Blake             /* A short read means that we have reached EOF. Pad the buffer
109c1bb86cdSEric Blake              * with zeros for bytes after EOF. */
110c1bb86cdSEric Blake             iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
111c1bb86cdSEric Blake                       0, aiocb->aio_nbytes - count);
112c1bb86cdSEric Blake 
113c1bb86cdSEric Blake             count = aiocb->aio_nbytes;
114c1bb86cdSEric Blake         }
115c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
116c1bb86cdSEric Blake             ret = 0;
117c1bb86cdSEric Blake         } else {
118c1bb86cdSEric Blake             ret = -EINVAL;
119c1bb86cdSEric Blake         }
120c1bb86cdSEric Blake         break;
121c1bb86cdSEric Blake     case QEMU_AIO_WRITE:
122c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
123c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
124c1bb86cdSEric Blake             ret = 0;
125c1bb86cdSEric Blake         } else {
126c1bb86cdSEric Blake             ret = -EINVAL;
127c1bb86cdSEric Blake         }
128c1bb86cdSEric Blake         break;
129c1bb86cdSEric Blake     case QEMU_AIO_FLUSH:
130c1bb86cdSEric Blake         if (!FlushFileBuffers(aiocb->hfile)) {
131c1bb86cdSEric Blake             return -EIO;
132c1bb86cdSEric Blake         }
133c1bb86cdSEric Blake         break;
134c1bb86cdSEric Blake     default:
135c1bb86cdSEric Blake         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
136c1bb86cdSEric Blake         ret = -EINVAL;
137c1bb86cdSEric Blake         break;
138c1bb86cdSEric Blake     }
139c1bb86cdSEric Blake 
140c1bb86cdSEric Blake     g_free(aiocb);
141c1bb86cdSEric Blake     return ret;
142c1bb86cdSEric Blake }
143c1bb86cdSEric Blake 
144c1bb86cdSEric Blake static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
145c1bb86cdSEric Blake         int64_t offset, QEMUIOVector *qiov, int count,
146c1bb86cdSEric Blake         BlockCompletionFunc *cb, void *opaque, int type)
147c1bb86cdSEric Blake {
148c1bb86cdSEric Blake     RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
149c1bb86cdSEric Blake     ThreadPool *pool;
150c1bb86cdSEric Blake 
151c1bb86cdSEric Blake     acb->bs = bs;
152c1bb86cdSEric Blake     acb->hfile = hfile;
153c1bb86cdSEric Blake     acb->aio_type = type;
154c1bb86cdSEric Blake 
155c1bb86cdSEric Blake     if (qiov) {
156c1bb86cdSEric Blake         acb->aio_iov = qiov->iov;
157c1bb86cdSEric Blake         acb->aio_niov = qiov->niov;
158c1bb86cdSEric Blake         assert(qiov->size == count);
159c1bb86cdSEric Blake     }
160c1bb86cdSEric Blake     acb->aio_nbytes = count;
161c1bb86cdSEric Blake     acb->aio_offset = offset;
162c1bb86cdSEric Blake 
163c1bb86cdSEric Blake     trace_paio_submit(acb, opaque, offset, count, type);
164c1bb86cdSEric Blake     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
165c1bb86cdSEric Blake     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
166c1bb86cdSEric Blake }
167c1bb86cdSEric Blake 
168c1bb86cdSEric Blake int qemu_ftruncate64(int fd, int64_t length)
169c1bb86cdSEric Blake {
170c1bb86cdSEric Blake     LARGE_INTEGER li;
171c1bb86cdSEric Blake     DWORD dw;
172c1bb86cdSEric Blake     LONG high;
173c1bb86cdSEric Blake     HANDLE h;
174c1bb86cdSEric Blake     BOOL res;
175c1bb86cdSEric Blake 
176c1bb86cdSEric Blake     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
177c1bb86cdSEric Blake 	return -1;
178c1bb86cdSEric Blake 
179c1bb86cdSEric Blake     h = (HANDLE)_get_osfhandle(fd);
180c1bb86cdSEric Blake 
181c1bb86cdSEric Blake     /* get current position, ftruncate do not change position */
182c1bb86cdSEric Blake     li.HighPart = 0;
183c1bb86cdSEric Blake     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
184c1bb86cdSEric Blake     if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
185c1bb86cdSEric Blake 	return -1;
186c1bb86cdSEric Blake     }
187c1bb86cdSEric Blake 
188c1bb86cdSEric Blake     high = length >> 32;
189c1bb86cdSEric Blake     dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
190c1bb86cdSEric Blake     if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
191c1bb86cdSEric Blake 	return -1;
192c1bb86cdSEric Blake     }
193c1bb86cdSEric Blake     res = SetEndOfFile(h);
194c1bb86cdSEric Blake 
195c1bb86cdSEric Blake     /* back to old position */
196c1bb86cdSEric Blake     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
197c1bb86cdSEric Blake     return res ? 0 : -1;
198c1bb86cdSEric Blake }
199c1bb86cdSEric Blake 
200c1bb86cdSEric Blake static int set_sparse(int fd)
201c1bb86cdSEric Blake {
202c1bb86cdSEric Blake     DWORD returned;
203c1bb86cdSEric Blake     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
204c1bb86cdSEric Blake 				 NULL, 0, NULL, 0, &returned, NULL);
205c1bb86cdSEric Blake }
206c1bb86cdSEric Blake 
207c1bb86cdSEric Blake static void raw_detach_aio_context(BlockDriverState *bs)
208c1bb86cdSEric Blake {
209c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
210c1bb86cdSEric Blake 
211c1bb86cdSEric Blake     if (s->aio) {
212c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
213c1bb86cdSEric Blake     }
214c1bb86cdSEric Blake }
215c1bb86cdSEric Blake 
216c1bb86cdSEric Blake static void raw_attach_aio_context(BlockDriverState *bs,
217c1bb86cdSEric Blake                                    AioContext *new_context)
218c1bb86cdSEric Blake {
219c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
220c1bb86cdSEric Blake 
221c1bb86cdSEric Blake     if (s->aio) {
222c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, new_context);
223c1bb86cdSEric Blake     }
224c1bb86cdSEric Blake }
225c1bb86cdSEric Blake 
226c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
227c1bb86cdSEric Blake {
228c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
229c1bb86cdSEric Blake     DWORD sectorsPerCluster, freeClusters, totalClusters, count;
230c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
231c1bb86cdSEric Blake     BOOL status;
232c1bb86cdSEric Blake 
233c1bb86cdSEric Blake     if (s->type == FTYPE_CD) {
234c1bb86cdSEric Blake         bs->bl.request_alignment = 2048;
235c1bb86cdSEric Blake         return;
236c1bb86cdSEric Blake     }
237c1bb86cdSEric Blake     if (s->type == FTYPE_HARDDISK) {
238c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
239c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
240c1bb86cdSEric Blake         if (status != 0) {
241c1bb86cdSEric Blake             bs->bl.request_alignment = dg.Geometry.BytesPerSector;
242c1bb86cdSEric Blake             return;
243c1bb86cdSEric Blake         }
244c1bb86cdSEric Blake         /* try GetDiskFreeSpace too */
245c1bb86cdSEric Blake     }
246c1bb86cdSEric Blake 
247c1bb86cdSEric Blake     if (s->drive_path[0]) {
248c1bb86cdSEric Blake         GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
249c1bb86cdSEric Blake                          &dg.Geometry.BytesPerSector,
250c1bb86cdSEric Blake                          &freeClusters, &totalClusters);
251c1bb86cdSEric Blake         bs->bl.request_alignment = dg.Geometry.BytesPerSector;
252c1bb86cdSEric Blake     }
253c1bb86cdSEric Blake }
254c1bb86cdSEric Blake 
255c1bb86cdSEric Blake static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
256c1bb86cdSEric Blake                             DWORD *overlapped)
257c1bb86cdSEric Blake {
258c1bb86cdSEric Blake     assert(access_flags != NULL);
259c1bb86cdSEric Blake     assert(overlapped != NULL);
260c1bb86cdSEric Blake 
261c1bb86cdSEric Blake     if (flags & BDRV_O_RDWR) {
262c1bb86cdSEric Blake         *access_flags = GENERIC_READ | GENERIC_WRITE;
263c1bb86cdSEric Blake     } else {
264c1bb86cdSEric Blake         *access_flags = GENERIC_READ;
265c1bb86cdSEric Blake     }
266c1bb86cdSEric Blake 
267c1bb86cdSEric Blake     *overlapped = FILE_ATTRIBUTE_NORMAL;
268c1bb86cdSEric Blake     if (use_aio) {
269c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_OVERLAPPED;
270c1bb86cdSEric Blake     }
271c1bb86cdSEric Blake     if (flags & BDRV_O_NOCACHE) {
272c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_NO_BUFFERING;
273c1bb86cdSEric Blake     }
274c1bb86cdSEric Blake }
275c1bb86cdSEric Blake 
276c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options,
277c1bb86cdSEric Blake                                Error **errp)
278c1bb86cdSEric Blake {
27903c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "file:", options);
280c1bb86cdSEric Blake }
281c1bb86cdSEric Blake 
282c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = {
283c1bb86cdSEric Blake     .name = "raw",
284c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
285c1bb86cdSEric Blake     .desc = {
286c1bb86cdSEric Blake         {
287c1bb86cdSEric Blake             .name = "filename",
288c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
289c1bb86cdSEric Blake             .help = "File name of the image",
290c1bb86cdSEric Blake         },
291c1bb86cdSEric Blake         {
292c1bb86cdSEric Blake             .name = "aio",
293c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
294c1bb86cdSEric Blake             .help = "host AIO implementation (threads, native)",
295c1bb86cdSEric Blake         },
296c1bb86cdSEric Blake         { /* end of list */ }
297c1bb86cdSEric Blake     },
298c1bb86cdSEric Blake };
299c1bb86cdSEric Blake 
300c1bb86cdSEric Blake static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
301c1bb86cdSEric Blake {
302c1bb86cdSEric Blake     BlockdevAioOptions aio, aio_default;
303c1bb86cdSEric Blake 
304c1bb86cdSEric Blake     aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
305c1bb86cdSEric Blake                                               : BLOCKDEV_AIO_OPTIONS_THREADS;
306f7abe0ecSMarc-André Lureau     aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
30706c60b6cSMarkus Armbruster                           aio_default, errp);
308c1bb86cdSEric Blake 
309c1bb86cdSEric Blake     switch (aio) {
310c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_NATIVE:
311c1bb86cdSEric Blake         return true;
312c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_THREADS:
313c1bb86cdSEric Blake         return false;
314c1bb86cdSEric Blake     default:
315c1bb86cdSEric Blake         error_setg(errp, "Invalid AIO option");
316c1bb86cdSEric Blake     }
317c1bb86cdSEric Blake     return false;
318c1bb86cdSEric Blake }
319c1bb86cdSEric Blake 
320c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags,
321c1bb86cdSEric Blake                     Error **errp)
322c1bb86cdSEric Blake {
323c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
324c1bb86cdSEric Blake     int access_flags;
325c1bb86cdSEric Blake     DWORD overlapped;
326c1bb86cdSEric Blake     QemuOpts *opts;
327c1bb86cdSEric Blake     Error *local_err = NULL;
328c1bb86cdSEric Blake     const char *filename;
329c1bb86cdSEric Blake     bool use_aio;
330c1bb86cdSEric Blake     int ret;
331c1bb86cdSEric Blake 
332c1bb86cdSEric Blake     s->type = FTYPE_FILE;
333c1bb86cdSEric Blake 
334c1bb86cdSEric Blake     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
335c1bb86cdSEric Blake     qemu_opts_absorb_qdict(opts, options, &local_err);
336c1bb86cdSEric Blake     if (local_err) {
337c1bb86cdSEric Blake         error_propagate(errp, local_err);
338c1bb86cdSEric Blake         ret = -EINVAL;
339c1bb86cdSEric Blake         goto fail;
340c1bb86cdSEric Blake     }
341c1bb86cdSEric Blake 
3421c3a555cSFam Zheng     if (qdict_get_try_bool(options, "locking", false)) {
3431c3a555cSFam Zheng         error_setg(errp, "locking=on is not supported on Windows");
344cdece046SGerd Hoffmann         ret = -EINVAL;
3451c3a555cSFam Zheng         goto fail;
3461c3a555cSFam Zheng     }
3471c3a555cSFam Zheng 
348c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
349c1bb86cdSEric Blake 
350c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
351c1bb86cdSEric Blake     if (local_err) {
352c1bb86cdSEric Blake         error_propagate(errp, local_err);
353c1bb86cdSEric Blake         ret = -EINVAL;
354c1bb86cdSEric Blake         goto fail;
355c1bb86cdSEric Blake     }
356c1bb86cdSEric Blake 
357c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
358c1bb86cdSEric Blake 
359c1bb86cdSEric Blake     if (filename[0] && filename[1] == ':') {
360c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
361c1bb86cdSEric Blake     } else if (filename[0] == '\\' && filename[1] == '\\') {
362c1bb86cdSEric Blake         s->drive_path[0] = 0;
363c1bb86cdSEric Blake     } else {
364c1bb86cdSEric Blake         /* Relative path.  */
365c1bb86cdSEric Blake         char buf[MAX_PATH];
366c1bb86cdSEric Blake         GetCurrentDirectory(MAX_PATH, buf);
367c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
368c1bb86cdSEric Blake     }
369c1bb86cdSEric Blake 
370c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
371c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
372c1bb86cdSEric Blake                           OPEN_EXISTING, overlapped, NULL);
373c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
374c1bb86cdSEric Blake         int err = GetLastError();
375c1bb86cdSEric Blake 
376c1bb86cdSEric Blake         error_setg_win32(errp, err, "Could not open '%s'", filename);
377c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
378c1bb86cdSEric Blake             ret = -EACCES;
379c1bb86cdSEric Blake         } else {
380c1bb86cdSEric Blake             ret = -EINVAL;
381c1bb86cdSEric Blake         }
382c1bb86cdSEric Blake         goto fail;
383c1bb86cdSEric Blake     }
384c1bb86cdSEric Blake 
385c1bb86cdSEric Blake     if (use_aio) {
386c1bb86cdSEric Blake         s->aio = win32_aio_init();
387c1bb86cdSEric Blake         if (s->aio == NULL) {
388c1bb86cdSEric Blake             CloseHandle(s->hfile);
389c1bb86cdSEric Blake             error_setg(errp, "Could not initialize AIO");
390c1bb86cdSEric Blake             ret = -EINVAL;
391c1bb86cdSEric Blake             goto fail;
392c1bb86cdSEric Blake         }
393c1bb86cdSEric Blake 
394c1bb86cdSEric Blake         ret = win32_aio_attach(s->aio, s->hfile);
395c1bb86cdSEric Blake         if (ret < 0) {
396c1bb86cdSEric Blake             win32_aio_cleanup(s->aio);
397c1bb86cdSEric Blake             CloseHandle(s->hfile);
398c1bb86cdSEric Blake             error_setg_errno(errp, -ret, "Could not enable AIO");
399c1bb86cdSEric Blake             goto fail;
400c1bb86cdSEric Blake         }
401c1bb86cdSEric Blake 
402c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
403c1bb86cdSEric Blake     }
404c1bb86cdSEric Blake 
405c1bb86cdSEric Blake     ret = 0;
406c1bb86cdSEric Blake fail:
407c1bb86cdSEric Blake     qemu_opts_del(opts);
408c1bb86cdSEric Blake     return ret;
409c1bb86cdSEric Blake }
410c1bb86cdSEric Blake 
411c1bb86cdSEric Blake static BlockAIOCB *raw_aio_readv(BlockDriverState *bs,
412c1bb86cdSEric Blake                          int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
413c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
414c1bb86cdSEric Blake {
415c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
416c1bb86cdSEric Blake     if (s->aio) {
417c1bb86cdSEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
418c1bb86cdSEric Blake                                 nb_sectors, cb, opaque, QEMU_AIO_READ);
419c1bb86cdSEric Blake     } else {
420c1bb86cdSEric Blake         return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
421c1bb86cdSEric Blake                            nb_sectors << BDRV_SECTOR_BITS,
422c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_READ);
423c1bb86cdSEric Blake     }
424c1bb86cdSEric Blake }
425c1bb86cdSEric Blake 
426c1bb86cdSEric Blake static BlockAIOCB *raw_aio_writev(BlockDriverState *bs,
427c1bb86cdSEric Blake                           int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
428c1bb86cdSEric Blake                           BlockCompletionFunc *cb, void *opaque)
429c1bb86cdSEric Blake {
430c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
431c1bb86cdSEric Blake     if (s->aio) {
432c1bb86cdSEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
433c1bb86cdSEric Blake                                 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
434c1bb86cdSEric Blake     } else {
435c1bb86cdSEric Blake         return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
436c1bb86cdSEric Blake                            nb_sectors << BDRV_SECTOR_BITS,
437c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_WRITE);
438c1bb86cdSEric Blake     }
439c1bb86cdSEric Blake }
440c1bb86cdSEric Blake 
441c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
442c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
443c1bb86cdSEric Blake {
444c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
445c1bb86cdSEric Blake     return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
446c1bb86cdSEric Blake }
447c1bb86cdSEric Blake 
448c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs)
449c1bb86cdSEric Blake {
450c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
451c1bb86cdSEric Blake 
452c1bb86cdSEric Blake     if (s->aio) {
453c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
454c1bb86cdSEric Blake         win32_aio_cleanup(s->aio);
455c1bb86cdSEric Blake         s->aio = NULL;
456c1bb86cdSEric Blake     }
457c1bb86cdSEric Blake 
458c1bb86cdSEric Blake     CloseHandle(s->hfile);
459c1bb86cdSEric Blake     if (bs->open_flags & BDRV_O_TEMPORARY) {
460c1bb86cdSEric Blake         unlink(bs->filename);
461c1bb86cdSEric Blake     }
462c1bb86cdSEric Blake }
463c1bb86cdSEric Blake 
4648243ccb7SMax Reitz static int raw_truncate(BlockDriverState *bs, int64_t offset,
4658243ccb7SMax Reitz                         PreallocMode prealloc, Error **errp)
466c1bb86cdSEric Blake {
467c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
468c1bb86cdSEric Blake     LONG low, high;
469c1bb86cdSEric Blake     DWORD dwPtrLow;
470c1bb86cdSEric Blake 
4718243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
4728243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
473977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
4748243ccb7SMax Reitz         return -ENOTSUP;
4758243ccb7SMax Reitz     }
4768243ccb7SMax Reitz 
477c1bb86cdSEric Blake     low = offset;
478c1bb86cdSEric Blake     high = offset >> 32;
479c1bb86cdSEric Blake 
480c1bb86cdSEric Blake     /*
481c1bb86cdSEric Blake      * An error has occurred if the return value is INVALID_SET_FILE_POINTER
482c1bb86cdSEric Blake      * and GetLastError doesn't return NO_ERROR.
483c1bb86cdSEric Blake      */
484c1bb86cdSEric Blake     dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
485c1bb86cdSEric Blake     if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
4864bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetFilePointer error");
487c1bb86cdSEric Blake         return -EIO;
488c1bb86cdSEric Blake     }
489c1bb86cdSEric Blake     if (SetEndOfFile(s->hfile) == 0) {
4904bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
491c1bb86cdSEric Blake         return -EIO;
492c1bb86cdSEric Blake     }
493c1bb86cdSEric Blake     return 0;
494c1bb86cdSEric Blake }
495c1bb86cdSEric Blake 
496c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs)
497c1bb86cdSEric Blake {
498c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
499c1bb86cdSEric Blake     LARGE_INTEGER l;
500c1bb86cdSEric Blake     ULARGE_INTEGER available, total, total_free;
501c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
502c1bb86cdSEric Blake     DWORD count;
503c1bb86cdSEric Blake     BOOL status;
504c1bb86cdSEric Blake 
505c1bb86cdSEric Blake     switch(s->type) {
506c1bb86cdSEric Blake     case FTYPE_FILE:
507c1bb86cdSEric Blake         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
508c1bb86cdSEric Blake         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
509c1bb86cdSEric Blake             return -EIO;
510c1bb86cdSEric Blake         break;
511c1bb86cdSEric Blake     case FTYPE_CD:
512c1bb86cdSEric Blake         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
513c1bb86cdSEric Blake             return -EIO;
514c1bb86cdSEric Blake         l.QuadPart = total.QuadPart;
515c1bb86cdSEric Blake         break;
516c1bb86cdSEric Blake     case FTYPE_HARDDISK:
517c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
518c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
519c1bb86cdSEric Blake         if (status != 0) {
520c1bb86cdSEric Blake             l = dg.DiskSize;
521c1bb86cdSEric Blake         }
522c1bb86cdSEric Blake         break;
523c1bb86cdSEric Blake     default:
524c1bb86cdSEric Blake         return -EIO;
525c1bb86cdSEric Blake     }
526c1bb86cdSEric Blake     return l.QuadPart;
527c1bb86cdSEric Blake }
528c1bb86cdSEric Blake 
529c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
530c1bb86cdSEric Blake {
531c1bb86cdSEric Blake     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
532c1bb86cdSEric Blake                                               DWORD * high);
533c1bb86cdSEric Blake     get_compressed_t get_compressed;
534c1bb86cdSEric Blake     struct _stati64 st;
535c1bb86cdSEric Blake     const char *filename = bs->filename;
536c1bb86cdSEric Blake     /* WinNT support GetCompressedFileSize to determine allocate size */
537c1bb86cdSEric Blake     get_compressed =
538c1bb86cdSEric Blake         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
539c1bb86cdSEric Blake                                             "GetCompressedFileSizeA");
540c1bb86cdSEric Blake     if (get_compressed) {
541c1bb86cdSEric Blake         DWORD high, low;
542c1bb86cdSEric Blake         low = get_compressed(filename, &high);
543c1bb86cdSEric Blake         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
544c1bb86cdSEric Blake             return (((int64_t) high) << 32) + low;
545c1bb86cdSEric Blake         }
546c1bb86cdSEric Blake     }
547c1bb86cdSEric Blake 
548c1bb86cdSEric Blake     if (_stati64(filename, &st) < 0) {
549c1bb86cdSEric Blake         return -1;
550c1bb86cdSEric Blake     }
551c1bb86cdSEric Blake     return st.st_size;
552c1bb86cdSEric Blake }
553c1bb86cdSEric Blake 
554c1bb86cdSEric Blake static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
555c1bb86cdSEric Blake {
556c1bb86cdSEric Blake     int fd;
557c1bb86cdSEric Blake     int64_t total_size = 0;
558c1bb86cdSEric Blake 
559c1bb86cdSEric Blake     strstart(filename, "file:", &filename);
560c1bb86cdSEric Blake 
561c1bb86cdSEric Blake     /* Read out options */
562c1bb86cdSEric Blake     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
563c1bb86cdSEric Blake                           BDRV_SECTOR_SIZE);
564c1bb86cdSEric Blake 
565c1bb86cdSEric Blake     fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
566c1bb86cdSEric Blake                    0644);
567c1bb86cdSEric Blake     if (fd < 0) {
568c1bb86cdSEric Blake         error_setg_errno(errp, errno, "Could not create file");
569c1bb86cdSEric Blake         return -EIO;
570c1bb86cdSEric Blake     }
571c1bb86cdSEric Blake     set_sparse(fd);
572c1bb86cdSEric Blake     ftruncate(fd, total_size);
573c1bb86cdSEric Blake     qemu_close(fd);
574c1bb86cdSEric Blake     return 0;
575c1bb86cdSEric Blake }
576c1bb86cdSEric Blake 
577c1bb86cdSEric Blake 
578c1bb86cdSEric Blake static QemuOptsList raw_create_opts = {
579c1bb86cdSEric Blake     .name = "raw-create-opts",
580c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
581c1bb86cdSEric Blake     .desc = {
582c1bb86cdSEric Blake         {
583c1bb86cdSEric Blake             .name = BLOCK_OPT_SIZE,
584c1bb86cdSEric Blake             .type = QEMU_OPT_SIZE,
585c1bb86cdSEric Blake             .help = "Virtual disk size"
586c1bb86cdSEric Blake         },
587c1bb86cdSEric Blake         { /* end of list */ }
588c1bb86cdSEric Blake     }
589c1bb86cdSEric Blake };
590c1bb86cdSEric Blake 
591c1bb86cdSEric Blake BlockDriver bdrv_file = {
592c1bb86cdSEric Blake     .format_name	= "file",
593c1bb86cdSEric Blake     .protocol_name	= "file",
594c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
595c1bb86cdSEric Blake     .bdrv_needs_filename = true,
596c1bb86cdSEric Blake     .bdrv_parse_filename = raw_parse_filename,
597c1bb86cdSEric Blake     .bdrv_file_open     = raw_open,
598c1bb86cdSEric Blake     .bdrv_refresh_limits = raw_probe_alignment,
599c1bb86cdSEric Blake     .bdrv_close         = raw_close,
600c1bb86cdSEric Blake     .bdrv_create        = raw_create,
601c1bb86cdSEric Blake     .bdrv_has_zero_init = bdrv_has_zero_init_1,
602c1bb86cdSEric Blake 
603c1bb86cdSEric Blake     .bdrv_aio_readv     = raw_aio_readv,
604c1bb86cdSEric Blake     .bdrv_aio_writev    = raw_aio_writev,
605c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
606c1bb86cdSEric Blake 
607c1bb86cdSEric Blake     .bdrv_truncate	= raw_truncate,
608c1bb86cdSEric Blake     .bdrv_getlength	= raw_getlength,
609c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
610c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
611c1bb86cdSEric Blake 
612c1bb86cdSEric Blake     .create_opts        = &raw_create_opts,
613c1bb86cdSEric Blake };
614c1bb86cdSEric Blake 
615c1bb86cdSEric Blake /***********************************************/
616c1bb86cdSEric Blake /* host device */
617c1bb86cdSEric Blake 
618c1bb86cdSEric Blake static int find_cdrom(char *cdrom_name, int cdrom_name_size)
619c1bb86cdSEric Blake {
620c1bb86cdSEric Blake     char drives[256], *pdrv = drives;
621c1bb86cdSEric Blake     UINT type;
622c1bb86cdSEric Blake 
623c1bb86cdSEric Blake     memset(drives, 0, sizeof(drives));
624c1bb86cdSEric Blake     GetLogicalDriveStrings(sizeof(drives), drives);
625c1bb86cdSEric Blake     while(pdrv[0] != '\0') {
626c1bb86cdSEric Blake         type = GetDriveType(pdrv);
627c1bb86cdSEric Blake         switch(type) {
628c1bb86cdSEric Blake         case DRIVE_CDROM:
629c1bb86cdSEric Blake             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
630c1bb86cdSEric Blake             return 0;
631c1bb86cdSEric Blake             break;
632c1bb86cdSEric Blake         }
633c1bb86cdSEric Blake         pdrv += lstrlen(pdrv) + 1;
634c1bb86cdSEric Blake     }
635c1bb86cdSEric Blake     return -1;
636c1bb86cdSEric Blake }
637c1bb86cdSEric Blake 
638c1bb86cdSEric Blake static int find_device_type(BlockDriverState *bs, const char *filename)
639c1bb86cdSEric Blake {
640c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
641c1bb86cdSEric Blake     UINT type;
642c1bb86cdSEric Blake     const char *p;
643c1bb86cdSEric Blake 
644c1bb86cdSEric Blake     if (strstart(filename, "\\\\.\\", &p) ||
645c1bb86cdSEric Blake         strstart(filename, "//./", &p)) {
646c1bb86cdSEric Blake         if (stristart(p, "PhysicalDrive", NULL))
647c1bb86cdSEric Blake             return FTYPE_HARDDISK;
648c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
649c1bb86cdSEric Blake         type = GetDriveType(s->drive_path);
650c1bb86cdSEric Blake         switch (type) {
651c1bb86cdSEric Blake         case DRIVE_REMOVABLE:
652c1bb86cdSEric Blake         case DRIVE_FIXED:
653c1bb86cdSEric Blake             return FTYPE_HARDDISK;
654c1bb86cdSEric Blake         case DRIVE_CDROM:
655c1bb86cdSEric Blake             return FTYPE_CD;
656c1bb86cdSEric Blake         default:
657c1bb86cdSEric Blake             return FTYPE_FILE;
658c1bb86cdSEric Blake         }
659c1bb86cdSEric Blake     } else {
660c1bb86cdSEric Blake         return FTYPE_FILE;
661c1bb86cdSEric Blake     }
662c1bb86cdSEric Blake }
663c1bb86cdSEric Blake 
664c1bb86cdSEric Blake static int hdev_probe_device(const char *filename)
665c1bb86cdSEric Blake {
666c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL))
667c1bb86cdSEric Blake         return 100;
668c1bb86cdSEric Blake     if (is_windows_drive(filename))
669c1bb86cdSEric Blake         return 100;
670c1bb86cdSEric Blake     return 0;
671c1bb86cdSEric Blake }
672c1bb86cdSEric Blake 
673c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options,
674c1bb86cdSEric Blake                                 Error **errp)
675c1bb86cdSEric Blake {
67603c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
677c1bb86cdSEric Blake }
678c1bb86cdSEric Blake 
679c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
680c1bb86cdSEric Blake                      Error **errp)
681c1bb86cdSEric Blake {
682c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
683c1bb86cdSEric Blake     int access_flags, create_flags;
684c1bb86cdSEric Blake     int ret = 0;
685c1bb86cdSEric Blake     DWORD overlapped;
686c1bb86cdSEric Blake     char device_name[64];
687c1bb86cdSEric Blake 
688c1bb86cdSEric Blake     Error *local_err = NULL;
689c1bb86cdSEric Blake     const char *filename;
690c1bb86cdSEric Blake     bool use_aio;
691c1bb86cdSEric Blake 
692c1bb86cdSEric Blake     QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
693c1bb86cdSEric Blake                                       &error_abort);
694c1bb86cdSEric Blake     qemu_opts_absorb_qdict(opts, options, &local_err);
695c1bb86cdSEric Blake     if (local_err) {
696c1bb86cdSEric Blake         error_propagate(errp, local_err);
697c1bb86cdSEric Blake         ret = -EINVAL;
698c1bb86cdSEric Blake         goto done;
699c1bb86cdSEric Blake     }
700c1bb86cdSEric Blake 
701c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
702c1bb86cdSEric Blake 
703c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
704c1bb86cdSEric Blake     if (!local_err && use_aio) {
705c1bb86cdSEric Blake         error_setg(&local_err, "AIO is not supported on Windows host devices");
706c1bb86cdSEric Blake     }
707c1bb86cdSEric Blake     if (local_err) {
708c1bb86cdSEric Blake         error_propagate(errp, local_err);
709c1bb86cdSEric Blake         ret = -EINVAL;
710c1bb86cdSEric Blake         goto done;
711c1bb86cdSEric Blake     }
712c1bb86cdSEric Blake 
713c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL)) {
714c1bb86cdSEric Blake         if (find_cdrom(device_name, sizeof(device_name)) < 0) {
715c1bb86cdSEric Blake             error_setg(errp, "Could not open CD-ROM drive");
716c1bb86cdSEric Blake             ret = -ENOENT;
717c1bb86cdSEric Blake             goto done;
718c1bb86cdSEric Blake         }
719c1bb86cdSEric Blake         filename = device_name;
720c1bb86cdSEric Blake     } else {
721c1bb86cdSEric Blake         /* transform drive letters into device name */
722c1bb86cdSEric Blake         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
723c1bb86cdSEric Blake              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
724c1bb86cdSEric Blake             filename[1] == ':' && filename[2] == '\0') {
725c1bb86cdSEric Blake             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
726c1bb86cdSEric Blake             filename = device_name;
727c1bb86cdSEric Blake         }
728c1bb86cdSEric Blake     }
729c1bb86cdSEric Blake     s->type = find_device_type(bs, filename);
730c1bb86cdSEric Blake 
731c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
732c1bb86cdSEric Blake 
733c1bb86cdSEric Blake     create_flags = OPEN_EXISTING;
734c1bb86cdSEric Blake 
735c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
736c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
737c1bb86cdSEric Blake                           create_flags, overlapped, NULL);
738c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
739c1bb86cdSEric Blake         int err = GetLastError();
740c1bb86cdSEric Blake 
741c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
742c1bb86cdSEric Blake             ret = -EACCES;
743c1bb86cdSEric Blake         } else {
744c1bb86cdSEric Blake             ret = -EINVAL;
745c1bb86cdSEric Blake         }
746c1bb86cdSEric Blake         error_setg_errno(errp, -ret, "Could not open device");
747c1bb86cdSEric Blake         goto done;
748c1bb86cdSEric Blake     }
749c1bb86cdSEric Blake 
750c1bb86cdSEric Blake done:
751c1bb86cdSEric Blake     qemu_opts_del(opts);
752c1bb86cdSEric Blake     return ret;
753c1bb86cdSEric Blake }
754c1bb86cdSEric Blake 
755c1bb86cdSEric Blake static BlockDriver bdrv_host_device = {
756c1bb86cdSEric Blake     .format_name	= "host_device",
757c1bb86cdSEric Blake     .protocol_name	= "host_device",
758c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
759c1bb86cdSEric Blake     .bdrv_needs_filename = true,
760c1bb86cdSEric Blake     .bdrv_parse_filename = hdev_parse_filename,
761c1bb86cdSEric Blake     .bdrv_probe_device	= hdev_probe_device,
762c1bb86cdSEric Blake     .bdrv_file_open	= hdev_open,
763c1bb86cdSEric Blake     .bdrv_close		= raw_close,
764c1bb86cdSEric Blake 
765c1bb86cdSEric Blake     .bdrv_aio_readv     = raw_aio_readv,
766c1bb86cdSEric Blake     .bdrv_aio_writev    = raw_aio_writev,
767c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
768c1bb86cdSEric Blake 
769c1bb86cdSEric Blake     .bdrv_detach_aio_context = raw_detach_aio_context,
770c1bb86cdSEric Blake     .bdrv_attach_aio_context = raw_attach_aio_context,
771c1bb86cdSEric Blake 
772c1bb86cdSEric Blake     .bdrv_getlength      = raw_getlength,
773c1bb86cdSEric Blake     .has_variable_length = true,
774c1bb86cdSEric Blake 
775c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
776c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
777c1bb86cdSEric Blake };
778c1bb86cdSEric Blake 
779c1bb86cdSEric Blake static void bdrv_file_init(void)
780c1bb86cdSEric Blake {
781c1bb86cdSEric Blake     bdrv_register(&bdrv_file);
782c1bb86cdSEric Blake     bdrv_register(&bdrv_host_device);
783c1bb86cdSEric Blake }
784c1bb86cdSEric Blake 
785c1bb86cdSEric Blake block_init(bdrv_file_init);
786