xref: /openbmc/qemu/block/file-win32.c (revision 4bff28b81a0ddb48a09d279e99ab847e8a292506)
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"
33c1bb86cdSEric Blake #include "qapi/qmp/qstring.h"
34c1bb86cdSEric Blake #include "qapi/util.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 {
279c1bb86cdSEric Blake     /* The filename does not have to be prefixed by the protocol name, since
280c1bb86cdSEric Blake      * "file" is the default protocol; therefore, the return value of this
281c1bb86cdSEric Blake      * function call can be ignored. */
282c1bb86cdSEric Blake     strstart(filename, "file:", &filename);
283c1bb86cdSEric Blake 
284c1bb86cdSEric Blake     qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
285c1bb86cdSEric Blake }
286c1bb86cdSEric Blake 
287c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = {
288c1bb86cdSEric Blake     .name = "raw",
289c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
290c1bb86cdSEric Blake     .desc = {
291c1bb86cdSEric Blake         {
292c1bb86cdSEric Blake             .name = "filename",
293c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
294c1bb86cdSEric Blake             .help = "File name of the image",
295c1bb86cdSEric Blake         },
296c1bb86cdSEric Blake         {
297c1bb86cdSEric Blake             .name = "aio",
298c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
299c1bb86cdSEric Blake             .help = "host AIO implementation (threads, native)",
300c1bb86cdSEric Blake         },
301c1bb86cdSEric Blake         { /* end of list */ }
302c1bb86cdSEric Blake     },
303c1bb86cdSEric Blake };
304c1bb86cdSEric Blake 
305c1bb86cdSEric Blake static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
306c1bb86cdSEric Blake {
307c1bb86cdSEric Blake     BlockdevAioOptions aio, aio_default;
308c1bb86cdSEric Blake 
309c1bb86cdSEric Blake     aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
310c1bb86cdSEric Blake                                               : BLOCKDEV_AIO_OPTIONS_THREADS;
311c1bb86cdSEric Blake     aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
312c1bb86cdSEric Blake                           BLOCKDEV_AIO_OPTIONS__MAX, aio_default, errp);
313c1bb86cdSEric Blake 
314c1bb86cdSEric Blake     switch (aio) {
315c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_NATIVE:
316c1bb86cdSEric Blake         return true;
317c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_THREADS:
318c1bb86cdSEric Blake         return false;
319c1bb86cdSEric Blake     default:
320c1bb86cdSEric Blake         error_setg(errp, "Invalid AIO option");
321c1bb86cdSEric Blake     }
322c1bb86cdSEric Blake     return false;
323c1bb86cdSEric Blake }
324c1bb86cdSEric Blake 
325c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags,
326c1bb86cdSEric Blake                     Error **errp)
327c1bb86cdSEric Blake {
328c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
329c1bb86cdSEric Blake     int access_flags;
330c1bb86cdSEric Blake     DWORD overlapped;
331c1bb86cdSEric Blake     QemuOpts *opts;
332c1bb86cdSEric Blake     Error *local_err = NULL;
333c1bb86cdSEric Blake     const char *filename;
334c1bb86cdSEric Blake     bool use_aio;
335c1bb86cdSEric Blake     int ret;
336c1bb86cdSEric Blake 
337c1bb86cdSEric Blake     s->type = FTYPE_FILE;
338c1bb86cdSEric Blake 
339c1bb86cdSEric Blake     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
340c1bb86cdSEric Blake     qemu_opts_absorb_qdict(opts, options, &local_err);
341c1bb86cdSEric Blake     if (local_err) {
342c1bb86cdSEric Blake         error_propagate(errp, local_err);
343c1bb86cdSEric Blake         ret = -EINVAL;
344c1bb86cdSEric Blake         goto fail;
345c1bb86cdSEric Blake     }
346c1bb86cdSEric Blake 
347c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
348c1bb86cdSEric Blake 
349c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
350c1bb86cdSEric Blake     if (local_err) {
351c1bb86cdSEric Blake         error_propagate(errp, local_err);
352c1bb86cdSEric Blake         ret = -EINVAL;
353c1bb86cdSEric Blake         goto fail;
354c1bb86cdSEric Blake     }
355c1bb86cdSEric Blake 
356c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
357c1bb86cdSEric Blake 
358c1bb86cdSEric Blake     if (filename[0] && filename[1] == ':') {
359c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
360c1bb86cdSEric Blake     } else if (filename[0] == '\\' && filename[1] == '\\') {
361c1bb86cdSEric Blake         s->drive_path[0] = 0;
362c1bb86cdSEric Blake     } else {
363c1bb86cdSEric Blake         /* Relative path.  */
364c1bb86cdSEric Blake         char buf[MAX_PATH];
365c1bb86cdSEric Blake         GetCurrentDirectory(MAX_PATH, buf);
366c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
367c1bb86cdSEric Blake     }
368c1bb86cdSEric Blake 
369c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
370c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
371c1bb86cdSEric Blake                           OPEN_EXISTING, overlapped, NULL);
372c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
373c1bb86cdSEric Blake         int err = GetLastError();
374c1bb86cdSEric Blake 
375c1bb86cdSEric Blake         error_setg_win32(errp, err, "Could not open '%s'", filename);
376c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
377c1bb86cdSEric Blake             ret = -EACCES;
378c1bb86cdSEric Blake         } else {
379c1bb86cdSEric Blake             ret = -EINVAL;
380c1bb86cdSEric Blake         }
381c1bb86cdSEric Blake         goto fail;
382c1bb86cdSEric Blake     }
383c1bb86cdSEric Blake 
384c1bb86cdSEric Blake     if (use_aio) {
385c1bb86cdSEric Blake         s->aio = win32_aio_init();
386c1bb86cdSEric Blake         if (s->aio == NULL) {
387c1bb86cdSEric Blake             CloseHandle(s->hfile);
388c1bb86cdSEric Blake             error_setg(errp, "Could not initialize AIO");
389c1bb86cdSEric Blake             ret = -EINVAL;
390c1bb86cdSEric Blake             goto fail;
391c1bb86cdSEric Blake         }
392c1bb86cdSEric Blake 
393c1bb86cdSEric Blake         ret = win32_aio_attach(s->aio, s->hfile);
394c1bb86cdSEric Blake         if (ret < 0) {
395c1bb86cdSEric Blake             win32_aio_cleanup(s->aio);
396c1bb86cdSEric Blake             CloseHandle(s->hfile);
397c1bb86cdSEric Blake             error_setg_errno(errp, -ret, "Could not enable AIO");
398c1bb86cdSEric Blake             goto fail;
399c1bb86cdSEric Blake         }
400c1bb86cdSEric Blake 
401c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
402c1bb86cdSEric Blake     }
403c1bb86cdSEric Blake 
404c1bb86cdSEric Blake     ret = 0;
405c1bb86cdSEric Blake fail:
406c1bb86cdSEric Blake     qemu_opts_del(opts);
407c1bb86cdSEric Blake     return ret;
408c1bb86cdSEric Blake }
409c1bb86cdSEric Blake 
410c1bb86cdSEric Blake static BlockAIOCB *raw_aio_readv(BlockDriverState *bs,
411c1bb86cdSEric Blake                          int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
412c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
413c1bb86cdSEric Blake {
414c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
415c1bb86cdSEric Blake     if (s->aio) {
416c1bb86cdSEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
417c1bb86cdSEric Blake                                 nb_sectors, cb, opaque, QEMU_AIO_READ);
418c1bb86cdSEric Blake     } else {
419c1bb86cdSEric Blake         return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
420c1bb86cdSEric Blake                            nb_sectors << BDRV_SECTOR_BITS,
421c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_READ);
422c1bb86cdSEric Blake     }
423c1bb86cdSEric Blake }
424c1bb86cdSEric Blake 
425c1bb86cdSEric Blake static BlockAIOCB *raw_aio_writev(BlockDriverState *bs,
426c1bb86cdSEric Blake                           int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
427c1bb86cdSEric Blake                           BlockCompletionFunc *cb, void *opaque)
428c1bb86cdSEric Blake {
429c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
430c1bb86cdSEric Blake     if (s->aio) {
431c1bb86cdSEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
432c1bb86cdSEric Blake                                 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
433c1bb86cdSEric Blake     } else {
434c1bb86cdSEric Blake         return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
435c1bb86cdSEric Blake                            nb_sectors << BDRV_SECTOR_BITS,
436c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_WRITE);
437c1bb86cdSEric Blake     }
438c1bb86cdSEric Blake }
439c1bb86cdSEric Blake 
440c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
441c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
442c1bb86cdSEric Blake {
443c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
444c1bb86cdSEric Blake     return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
445c1bb86cdSEric Blake }
446c1bb86cdSEric Blake 
447c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs)
448c1bb86cdSEric Blake {
449c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
450c1bb86cdSEric Blake 
451c1bb86cdSEric Blake     if (s->aio) {
452c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
453c1bb86cdSEric Blake         win32_aio_cleanup(s->aio);
454c1bb86cdSEric Blake         s->aio = NULL;
455c1bb86cdSEric Blake     }
456c1bb86cdSEric Blake 
457c1bb86cdSEric Blake     CloseHandle(s->hfile);
458c1bb86cdSEric Blake     if (bs->open_flags & BDRV_O_TEMPORARY) {
459c1bb86cdSEric Blake         unlink(bs->filename);
460c1bb86cdSEric Blake     }
461c1bb86cdSEric Blake }
462c1bb86cdSEric Blake 
463*4bff28b8SMax Reitz static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp)
464c1bb86cdSEric Blake {
465c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
466c1bb86cdSEric Blake     LONG low, high;
467c1bb86cdSEric Blake     DWORD dwPtrLow;
468c1bb86cdSEric Blake 
469c1bb86cdSEric Blake     low = offset;
470c1bb86cdSEric Blake     high = offset >> 32;
471c1bb86cdSEric Blake 
472c1bb86cdSEric Blake     /*
473c1bb86cdSEric Blake      * An error has occurred if the return value is INVALID_SET_FILE_POINTER
474c1bb86cdSEric Blake      * and GetLastError doesn't return NO_ERROR.
475c1bb86cdSEric Blake      */
476c1bb86cdSEric Blake     dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
477c1bb86cdSEric Blake     if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
478*4bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetFilePointer error");
479c1bb86cdSEric Blake         return -EIO;
480c1bb86cdSEric Blake     }
481c1bb86cdSEric Blake     if (SetEndOfFile(s->hfile) == 0) {
482*4bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
483c1bb86cdSEric Blake         return -EIO;
484c1bb86cdSEric Blake     }
485c1bb86cdSEric Blake     return 0;
486c1bb86cdSEric Blake }
487c1bb86cdSEric Blake 
488c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs)
489c1bb86cdSEric Blake {
490c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
491c1bb86cdSEric Blake     LARGE_INTEGER l;
492c1bb86cdSEric Blake     ULARGE_INTEGER available, total, total_free;
493c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
494c1bb86cdSEric Blake     DWORD count;
495c1bb86cdSEric Blake     BOOL status;
496c1bb86cdSEric Blake 
497c1bb86cdSEric Blake     switch(s->type) {
498c1bb86cdSEric Blake     case FTYPE_FILE:
499c1bb86cdSEric Blake         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
500c1bb86cdSEric Blake         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
501c1bb86cdSEric Blake             return -EIO;
502c1bb86cdSEric Blake         break;
503c1bb86cdSEric Blake     case FTYPE_CD:
504c1bb86cdSEric Blake         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
505c1bb86cdSEric Blake             return -EIO;
506c1bb86cdSEric Blake         l.QuadPart = total.QuadPart;
507c1bb86cdSEric Blake         break;
508c1bb86cdSEric Blake     case FTYPE_HARDDISK:
509c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
510c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
511c1bb86cdSEric Blake         if (status != 0) {
512c1bb86cdSEric Blake             l = dg.DiskSize;
513c1bb86cdSEric Blake         }
514c1bb86cdSEric Blake         break;
515c1bb86cdSEric Blake     default:
516c1bb86cdSEric Blake         return -EIO;
517c1bb86cdSEric Blake     }
518c1bb86cdSEric Blake     return l.QuadPart;
519c1bb86cdSEric Blake }
520c1bb86cdSEric Blake 
521c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
522c1bb86cdSEric Blake {
523c1bb86cdSEric Blake     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
524c1bb86cdSEric Blake                                               DWORD * high);
525c1bb86cdSEric Blake     get_compressed_t get_compressed;
526c1bb86cdSEric Blake     struct _stati64 st;
527c1bb86cdSEric Blake     const char *filename = bs->filename;
528c1bb86cdSEric Blake     /* WinNT support GetCompressedFileSize to determine allocate size */
529c1bb86cdSEric Blake     get_compressed =
530c1bb86cdSEric Blake         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
531c1bb86cdSEric Blake                                             "GetCompressedFileSizeA");
532c1bb86cdSEric Blake     if (get_compressed) {
533c1bb86cdSEric Blake         DWORD high, low;
534c1bb86cdSEric Blake         low = get_compressed(filename, &high);
535c1bb86cdSEric Blake         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
536c1bb86cdSEric Blake             return (((int64_t) high) << 32) + low;
537c1bb86cdSEric Blake         }
538c1bb86cdSEric Blake     }
539c1bb86cdSEric Blake 
540c1bb86cdSEric Blake     if (_stati64(filename, &st) < 0) {
541c1bb86cdSEric Blake         return -1;
542c1bb86cdSEric Blake     }
543c1bb86cdSEric Blake     return st.st_size;
544c1bb86cdSEric Blake }
545c1bb86cdSEric Blake 
546c1bb86cdSEric Blake static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
547c1bb86cdSEric Blake {
548c1bb86cdSEric Blake     int fd;
549c1bb86cdSEric Blake     int64_t total_size = 0;
550c1bb86cdSEric Blake 
551c1bb86cdSEric Blake     strstart(filename, "file:", &filename);
552c1bb86cdSEric Blake 
553c1bb86cdSEric Blake     /* Read out options */
554c1bb86cdSEric Blake     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
555c1bb86cdSEric Blake                           BDRV_SECTOR_SIZE);
556c1bb86cdSEric Blake 
557c1bb86cdSEric Blake     fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
558c1bb86cdSEric Blake                    0644);
559c1bb86cdSEric Blake     if (fd < 0) {
560c1bb86cdSEric Blake         error_setg_errno(errp, errno, "Could not create file");
561c1bb86cdSEric Blake         return -EIO;
562c1bb86cdSEric Blake     }
563c1bb86cdSEric Blake     set_sparse(fd);
564c1bb86cdSEric Blake     ftruncate(fd, total_size);
565c1bb86cdSEric Blake     qemu_close(fd);
566c1bb86cdSEric Blake     return 0;
567c1bb86cdSEric Blake }
568c1bb86cdSEric Blake 
569c1bb86cdSEric Blake 
570c1bb86cdSEric Blake static QemuOptsList raw_create_opts = {
571c1bb86cdSEric Blake     .name = "raw-create-opts",
572c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
573c1bb86cdSEric Blake     .desc = {
574c1bb86cdSEric Blake         {
575c1bb86cdSEric Blake             .name = BLOCK_OPT_SIZE,
576c1bb86cdSEric Blake             .type = QEMU_OPT_SIZE,
577c1bb86cdSEric Blake             .help = "Virtual disk size"
578c1bb86cdSEric Blake         },
579c1bb86cdSEric Blake         { /* end of list */ }
580c1bb86cdSEric Blake     }
581c1bb86cdSEric Blake };
582c1bb86cdSEric Blake 
583c1bb86cdSEric Blake BlockDriver bdrv_file = {
584c1bb86cdSEric Blake     .format_name	= "file",
585c1bb86cdSEric Blake     .protocol_name	= "file",
586c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
587c1bb86cdSEric Blake     .bdrv_needs_filename = true,
588c1bb86cdSEric Blake     .bdrv_parse_filename = raw_parse_filename,
589c1bb86cdSEric Blake     .bdrv_file_open     = raw_open,
590c1bb86cdSEric Blake     .bdrv_refresh_limits = raw_probe_alignment,
591c1bb86cdSEric Blake     .bdrv_close         = raw_close,
592c1bb86cdSEric Blake     .bdrv_create        = raw_create,
593c1bb86cdSEric Blake     .bdrv_has_zero_init = bdrv_has_zero_init_1,
594c1bb86cdSEric Blake 
595c1bb86cdSEric Blake     .bdrv_aio_readv     = raw_aio_readv,
596c1bb86cdSEric Blake     .bdrv_aio_writev    = raw_aio_writev,
597c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
598c1bb86cdSEric Blake 
599c1bb86cdSEric Blake     .bdrv_truncate	= raw_truncate,
600c1bb86cdSEric Blake     .bdrv_getlength	= raw_getlength,
601c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
602c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
603c1bb86cdSEric Blake 
604c1bb86cdSEric Blake     .create_opts        = &raw_create_opts,
605c1bb86cdSEric Blake };
606c1bb86cdSEric Blake 
607c1bb86cdSEric Blake /***********************************************/
608c1bb86cdSEric Blake /* host device */
609c1bb86cdSEric Blake 
610c1bb86cdSEric Blake static int find_cdrom(char *cdrom_name, int cdrom_name_size)
611c1bb86cdSEric Blake {
612c1bb86cdSEric Blake     char drives[256], *pdrv = drives;
613c1bb86cdSEric Blake     UINT type;
614c1bb86cdSEric Blake 
615c1bb86cdSEric Blake     memset(drives, 0, sizeof(drives));
616c1bb86cdSEric Blake     GetLogicalDriveStrings(sizeof(drives), drives);
617c1bb86cdSEric Blake     while(pdrv[0] != '\0') {
618c1bb86cdSEric Blake         type = GetDriveType(pdrv);
619c1bb86cdSEric Blake         switch(type) {
620c1bb86cdSEric Blake         case DRIVE_CDROM:
621c1bb86cdSEric Blake             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
622c1bb86cdSEric Blake             return 0;
623c1bb86cdSEric Blake             break;
624c1bb86cdSEric Blake         }
625c1bb86cdSEric Blake         pdrv += lstrlen(pdrv) + 1;
626c1bb86cdSEric Blake     }
627c1bb86cdSEric Blake     return -1;
628c1bb86cdSEric Blake }
629c1bb86cdSEric Blake 
630c1bb86cdSEric Blake static int find_device_type(BlockDriverState *bs, const char *filename)
631c1bb86cdSEric Blake {
632c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
633c1bb86cdSEric Blake     UINT type;
634c1bb86cdSEric Blake     const char *p;
635c1bb86cdSEric Blake 
636c1bb86cdSEric Blake     if (strstart(filename, "\\\\.\\", &p) ||
637c1bb86cdSEric Blake         strstart(filename, "//./", &p)) {
638c1bb86cdSEric Blake         if (stristart(p, "PhysicalDrive", NULL))
639c1bb86cdSEric Blake             return FTYPE_HARDDISK;
640c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
641c1bb86cdSEric Blake         type = GetDriveType(s->drive_path);
642c1bb86cdSEric Blake         switch (type) {
643c1bb86cdSEric Blake         case DRIVE_REMOVABLE:
644c1bb86cdSEric Blake         case DRIVE_FIXED:
645c1bb86cdSEric Blake             return FTYPE_HARDDISK;
646c1bb86cdSEric Blake         case DRIVE_CDROM:
647c1bb86cdSEric Blake             return FTYPE_CD;
648c1bb86cdSEric Blake         default:
649c1bb86cdSEric Blake             return FTYPE_FILE;
650c1bb86cdSEric Blake         }
651c1bb86cdSEric Blake     } else {
652c1bb86cdSEric Blake         return FTYPE_FILE;
653c1bb86cdSEric Blake     }
654c1bb86cdSEric Blake }
655c1bb86cdSEric Blake 
656c1bb86cdSEric Blake static int hdev_probe_device(const char *filename)
657c1bb86cdSEric Blake {
658c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL))
659c1bb86cdSEric Blake         return 100;
660c1bb86cdSEric Blake     if (is_windows_drive(filename))
661c1bb86cdSEric Blake         return 100;
662c1bb86cdSEric Blake     return 0;
663c1bb86cdSEric Blake }
664c1bb86cdSEric Blake 
665c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options,
666c1bb86cdSEric Blake                                 Error **errp)
667c1bb86cdSEric Blake {
668c1bb86cdSEric Blake     /* The prefix is optional, just as for "file". */
669c1bb86cdSEric Blake     strstart(filename, "host_device:", &filename);
670c1bb86cdSEric Blake 
671c1bb86cdSEric Blake     qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
672c1bb86cdSEric Blake }
673c1bb86cdSEric Blake 
674c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
675c1bb86cdSEric Blake                      Error **errp)
676c1bb86cdSEric Blake {
677c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
678c1bb86cdSEric Blake     int access_flags, create_flags;
679c1bb86cdSEric Blake     int ret = 0;
680c1bb86cdSEric Blake     DWORD overlapped;
681c1bb86cdSEric Blake     char device_name[64];
682c1bb86cdSEric Blake 
683c1bb86cdSEric Blake     Error *local_err = NULL;
684c1bb86cdSEric Blake     const char *filename;
685c1bb86cdSEric Blake     bool use_aio;
686c1bb86cdSEric Blake 
687c1bb86cdSEric Blake     QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
688c1bb86cdSEric Blake                                       &error_abort);
689c1bb86cdSEric Blake     qemu_opts_absorb_qdict(opts, options, &local_err);
690c1bb86cdSEric Blake     if (local_err) {
691c1bb86cdSEric Blake         error_propagate(errp, local_err);
692c1bb86cdSEric Blake         ret = -EINVAL;
693c1bb86cdSEric Blake         goto done;
694c1bb86cdSEric Blake     }
695c1bb86cdSEric Blake 
696c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
697c1bb86cdSEric Blake 
698c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
699c1bb86cdSEric Blake     if (!local_err && use_aio) {
700c1bb86cdSEric Blake         error_setg(&local_err, "AIO is not supported on Windows host devices");
701c1bb86cdSEric Blake     }
702c1bb86cdSEric Blake     if (local_err) {
703c1bb86cdSEric Blake         error_propagate(errp, local_err);
704c1bb86cdSEric Blake         ret = -EINVAL;
705c1bb86cdSEric Blake         goto done;
706c1bb86cdSEric Blake     }
707c1bb86cdSEric Blake 
708c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL)) {
709c1bb86cdSEric Blake         if (find_cdrom(device_name, sizeof(device_name)) < 0) {
710c1bb86cdSEric Blake             error_setg(errp, "Could not open CD-ROM drive");
711c1bb86cdSEric Blake             ret = -ENOENT;
712c1bb86cdSEric Blake             goto done;
713c1bb86cdSEric Blake         }
714c1bb86cdSEric Blake         filename = device_name;
715c1bb86cdSEric Blake     } else {
716c1bb86cdSEric Blake         /* transform drive letters into device name */
717c1bb86cdSEric Blake         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
718c1bb86cdSEric Blake              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
719c1bb86cdSEric Blake             filename[1] == ':' && filename[2] == '\0') {
720c1bb86cdSEric Blake             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
721c1bb86cdSEric Blake             filename = device_name;
722c1bb86cdSEric Blake         }
723c1bb86cdSEric Blake     }
724c1bb86cdSEric Blake     s->type = find_device_type(bs, filename);
725c1bb86cdSEric Blake 
726c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
727c1bb86cdSEric Blake 
728c1bb86cdSEric Blake     create_flags = OPEN_EXISTING;
729c1bb86cdSEric Blake 
730c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
731c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
732c1bb86cdSEric Blake                           create_flags, overlapped, NULL);
733c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
734c1bb86cdSEric Blake         int err = GetLastError();
735c1bb86cdSEric Blake 
736c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
737c1bb86cdSEric Blake             ret = -EACCES;
738c1bb86cdSEric Blake         } else {
739c1bb86cdSEric Blake             ret = -EINVAL;
740c1bb86cdSEric Blake         }
741c1bb86cdSEric Blake         error_setg_errno(errp, -ret, "Could not open device");
742c1bb86cdSEric Blake         goto done;
743c1bb86cdSEric Blake     }
744c1bb86cdSEric Blake 
745c1bb86cdSEric Blake done:
746c1bb86cdSEric Blake     qemu_opts_del(opts);
747c1bb86cdSEric Blake     return ret;
748c1bb86cdSEric Blake }
749c1bb86cdSEric Blake 
750c1bb86cdSEric Blake static BlockDriver bdrv_host_device = {
751c1bb86cdSEric Blake     .format_name	= "host_device",
752c1bb86cdSEric Blake     .protocol_name	= "host_device",
753c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
754c1bb86cdSEric Blake     .bdrv_needs_filename = true,
755c1bb86cdSEric Blake     .bdrv_parse_filename = hdev_parse_filename,
756c1bb86cdSEric Blake     .bdrv_probe_device	= hdev_probe_device,
757c1bb86cdSEric Blake     .bdrv_file_open	= hdev_open,
758c1bb86cdSEric Blake     .bdrv_close		= raw_close,
759c1bb86cdSEric Blake 
760c1bb86cdSEric Blake     .bdrv_aio_readv     = raw_aio_readv,
761c1bb86cdSEric Blake     .bdrv_aio_writev    = raw_aio_writev,
762c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
763c1bb86cdSEric Blake 
764c1bb86cdSEric Blake     .bdrv_detach_aio_context = raw_detach_aio_context,
765c1bb86cdSEric Blake     .bdrv_attach_aio_context = raw_attach_aio_context,
766c1bb86cdSEric Blake 
767c1bb86cdSEric Blake     .bdrv_getlength      = raw_getlength,
768c1bb86cdSEric Blake     .has_variable_length = true,
769c1bb86cdSEric Blake 
770c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
771c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
772c1bb86cdSEric Blake };
773c1bb86cdSEric Blake 
774c1bb86cdSEric Blake static void bdrv_file_init(void)
775c1bb86cdSEric Blake {
776c1bb86cdSEric Blake     bdrv_register(&bdrv_file);
777c1bb86cdSEric Blake     bdrv_register(&bdrv_host_device);
778c1bb86cdSEric Blake }
779c1bb86cdSEric Blake 
780c1bb86cdSEric Blake block_init(bdrv_file_init);
781