xref: /openbmc/qemu/block/file-win32.c (revision 4ec8df0183d0906ae6704d65ef6d8082c970ecdf)
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"
28e2c1c34fSMarkus Armbruster #include "block/block-io.h"
29c1bb86cdSEric Blake #include "block/block_int.h"
30c1bb86cdSEric Blake #include "qemu/module.h"
31922a01a0SMarkus Armbruster #include "qemu/option.h"
32c1bb86cdSEric Blake #include "block/raw-aio.h"
33c1bb86cdSEric Blake #include "trace.h"
34c1bb86cdSEric Blake #include "block/thread-pool.h"
35c1bb86cdSEric Blake #include "qemu/iov.h"
36452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
37c1bb86cdSEric Blake #include "qapi/qmp/qstring.h"
38c1bb86cdSEric Blake #include <windows.h>
39c1bb86cdSEric Blake #include <winioctl.h>
40c1bb86cdSEric Blake 
41c1bb86cdSEric Blake #define FTYPE_FILE 0
42c1bb86cdSEric Blake #define FTYPE_CD     1
43c1bb86cdSEric Blake #define FTYPE_HARDDISK 2
44c1bb86cdSEric Blake 
45c1bb86cdSEric Blake typedef struct RawWin32AIOData {
46c1bb86cdSEric Blake     BlockDriverState *bs;
47c1bb86cdSEric Blake     HANDLE hfile;
48c1bb86cdSEric Blake     struct iovec *aio_iov;
49c1bb86cdSEric Blake     int aio_niov;
50c1bb86cdSEric Blake     size_t aio_nbytes;
51c1bb86cdSEric Blake     off64_t aio_offset;
52c1bb86cdSEric Blake     int aio_type;
53c1bb86cdSEric Blake } RawWin32AIOData;
54c1bb86cdSEric Blake 
55c1bb86cdSEric Blake typedef struct BDRVRawState {
56c1bb86cdSEric Blake     HANDLE hfile;
57c1bb86cdSEric Blake     int type;
58c1bb86cdSEric Blake     char drive_path[16]; /* format: "d:\" */
59c1bb86cdSEric Blake     QEMUWin32AIOState *aio;
60c1bb86cdSEric Blake } BDRVRawState;
61c1bb86cdSEric Blake 
62ebd979c7SViktor Prutyanov typedef struct BDRVRawReopenState {
63ebd979c7SViktor Prutyanov     HANDLE hfile;
64ebd979c7SViktor Prutyanov } BDRVRawReopenState;
65ebd979c7SViktor Prutyanov 
66c1bb86cdSEric Blake /*
67c1bb86cdSEric Blake  * Read/writes the data to/from a given linear buffer.
68c1bb86cdSEric Blake  *
69c1bb86cdSEric Blake  * Returns the number of bytes handles or -errno in case of an error. Short
70c1bb86cdSEric Blake  * reads are only returned if the end of the file is reached.
71c1bb86cdSEric Blake  */
72c1bb86cdSEric Blake static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
73c1bb86cdSEric Blake {
74c1bb86cdSEric Blake     size_t offset = 0;
75c1bb86cdSEric Blake     int i;
76c1bb86cdSEric Blake 
77c1bb86cdSEric Blake     for (i = 0; i < aiocb->aio_niov; i++) {
78c1bb86cdSEric Blake         OVERLAPPED ov;
79c1bb86cdSEric Blake         DWORD ret, ret_count, len;
80c1bb86cdSEric Blake 
81c1bb86cdSEric Blake         memset(&ov, 0, sizeof(ov));
82c1bb86cdSEric Blake         ov.Offset = (aiocb->aio_offset + offset);
83c1bb86cdSEric Blake         ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
84c1bb86cdSEric Blake         len = aiocb->aio_iov[i].iov_len;
85c1bb86cdSEric Blake         if (aiocb->aio_type & QEMU_AIO_WRITE) {
86c1bb86cdSEric Blake             ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
87c1bb86cdSEric Blake                             len, &ret_count, &ov);
88c1bb86cdSEric Blake         } else {
89c1bb86cdSEric Blake             ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
90c1bb86cdSEric Blake                            len, &ret_count, &ov);
91c1bb86cdSEric Blake         }
92c1bb86cdSEric Blake         if (!ret) {
93c1bb86cdSEric Blake             ret_count = 0;
94c1bb86cdSEric Blake         }
95c1bb86cdSEric Blake         if (ret_count != len) {
96c1bb86cdSEric Blake             offset += ret_count;
97c1bb86cdSEric Blake             break;
98c1bb86cdSEric Blake         }
99c1bb86cdSEric Blake         offset += len;
100c1bb86cdSEric Blake     }
101c1bb86cdSEric Blake 
102c1bb86cdSEric Blake     return offset;
103c1bb86cdSEric Blake }
104c1bb86cdSEric Blake 
105c1bb86cdSEric Blake static int aio_worker(void *arg)
106c1bb86cdSEric Blake {
107c1bb86cdSEric Blake     RawWin32AIOData *aiocb = arg;
108c1bb86cdSEric Blake     ssize_t ret = 0;
109c1bb86cdSEric Blake     size_t count;
110c1bb86cdSEric Blake 
111c1bb86cdSEric Blake     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
112c1bb86cdSEric Blake     case QEMU_AIO_READ:
113c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
114c1bb86cdSEric Blake         if (count < aiocb->aio_nbytes) {
115c1bb86cdSEric Blake             /* A short read means that we have reached EOF. Pad the buffer
116c1bb86cdSEric Blake              * with zeros for bytes after EOF. */
117c1bb86cdSEric Blake             iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
118c1bb86cdSEric Blake                       0, aiocb->aio_nbytes - count);
119c1bb86cdSEric Blake 
120c1bb86cdSEric Blake             count = aiocb->aio_nbytes;
121c1bb86cdSEric Blake         }
122c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
123c1bb86cdSEric Blake             ret = 0;
124c1bb86cdSEric Blake         } else {
125c1bb86cdSEric Blake             ret = -EINVAL;
126c1bb86cdSEric Blake         }
127c1bb86cdSEric Blake         break;
128c1bb86cdSEric Blake     case QEMU_AIO_WRITE:
129c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
130c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
131c1bb86cdSEric Blake             ret = 0;
132c1bb86cdSEric Blake         } else {
133c1bb86cdSEric Blake             ret = -EINVAL;
134c1bb86cdSEric Blake         }
135c1bb86cdSEric Blake         break;
136c1bb86cdSEric Blake     case QEMU_AIO_FLUSH:
137c1bb86cdSEric Blake         if (!FlushFileBuffers(aiocb->hfile)) {
138c1bb86cdSEric Blake             return -EIO;
139c1bb86cdSEric Blake         }
140c1bb86cdSEric Blake         break;
141c1bb86cdSEric Blake     default:
142c1bb86cdSEric Blake         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
143c1bb86cdSEric Blake         ret = -EINVAL;
144c1bb86cdSEric Blake         break;
145c1bb86cdSEric Blake     }
146c1bb86cdSEric Blake 
147c1bb86cdSEric Blake     g_free(aiocb);
148c1bb86cdSEric Blake     return ret;
149c1bb86cdSEric Blake }
150c1bb86cdSEric Blake 
151c1bb86cdSEric Blake static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
152c1bb86cdSEric Blake         int64_t offset, QEMUIOVector *qiov, int count,
153c1bb86cdSEric Blake         BlockCompletionFunc *cb, void *opaque, int type)
154c1bb86cdSEric Blake {
155c1bb86cdSEric Blake     RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
156c1bb86cdSEric Blake     ThreadPool *pool;
157c1bb86cdSEric Blake 
158c1bb86cdSEric Blake     acb->bs = bs;
159c1bb86cdSEric Blake     acb->hfile = hfile;
160c1bb86cdSEric Blake     acb->aio_type = type;
161c1bb86cdSEric Blake 
162c1bb86cdSEric Blake     if (qiov) {
163c1bb86cdSEric Blake         acb->aio_iov = qiov->iov;
164c1bb86cdSEric Blake         acb->aio_niov = qiov->niov;
165c1bb86cdSEric Blake         assert(qiov->size == count);
166c1bb86cdSEric Blake     }
167c1bb86cdSEric Blake     acb->aio_nbytes = count;
168c1bb86cdSEric Blake     acb->aio_offset = offset;
169c1bb86cdSEric Blake 
170f8a30874SFam Zheng     trace_file_paio_submit(acb, opaque, offset, count, type);
171c1bb86cdSEric Blake     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
172c1bb86cdSEric Blake     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
173c1bb86cdSEric Blake }
174c1bb86cdSEric Blake 
175c1bb86cdSEric Blake int qemu_ftruncate64(int fd, int64_t length)
176c1bb86cdSEric Blake {
177c1bb86cdSEric Blake     LARGE_INTEGER li;
178c1bb86cdSEric Blake     DWORD dw;
179c1bb86cdSEric Blake     LONG high;
180c1bb86cdSEric Blake     HANDLE h;
181c1bb86cdSEric Blake     BOOL res;
182c1bb86cdSEric Blake 
183c1bb86cdSEric Blake     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
184c1bb86cdSEric Blake         return -1;
185c1bb86cdSEric Blake 
186c1bb86cdSEric Blake     h = (HANDLE)_get_osfhandle(fd);
187c1bb86cdSEric Blake 
188c1bb86cdSEric Blake     /* get current position, ftruncate do not change position */
189c1bb86cdSEric Blake     li.HighPart = 0;
190c1bb86cdSEric Blake     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
191c1bb86cdSEric Blake     if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
192c1bb86cdSEric Blake         return -1;
193c1bb86cdSEric Blake     }
194c1bb86cdSEric Blake 
195c1bb86cdSEric Blake     high = length >> 32;
196c1bb86cdSEric Blake     dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
197c1bb86cdSEric Blake     if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
198c1bb86cdSEric Blake         return -1;
199c1bb86cdSEric Blake     }
200c1bb86cdSEric Blake     res = SetEndOfFile(h);
201c1bb86cdSEric Blake 
202c1bb86cdSEric Blake     /* back to old position */
203c1bb86cdSEric Blake     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
204c1bb86cdSEric Blake     return res ? 0 : -1;
205c1bb86cdSEric Blake }
206c1bb86cdSEric Blake 
207c1bb86cdSEric Blake static int set_sparse(int fd)
208c1bb86cdSEric Blake {
209c1bb86cdSEric Blake     DWORD returned;
210c1bb86cdSEric Blake     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
211c1bb86cdSEric Blake                                  NULL, 0, NULL, 0, &returned, NULL);
212c1bb86cdSEric Blake }
213c1bb86cdSEric Blake 
214c1bb86cdSEric Blake static void raw_detach_aio_context(BlockDriverState *bs)
215c1bb86cdSEric Blake {
216c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
217c1bb86cdSEric Blake 
218c1bb86cdSEric Blake     if (s->aio) {
219c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
220c1bb86cdSEric Blake     }
221c1bb86cdSEric Blake }
222c1bb86cdSEric Blake 
223c1bb86cdSEric Blake static void raw_attach_aio_context(BlockDriverState *bs,
224c1bb86cdSEric Blake                                    AioContext *new_context)
225c1bb86cdSEric Blake {
226c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
227c1bb86cdSEric Blake 
228c1bb86cdSEric Blake     if (s->aio) {
229c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, new_context);
230c1bb86cdSEric Blake     }
231c1bb86cdSEric Blake }
232c1bb86cdSEric Blake 
233c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
234c1bb86cdSEric Blake {
235c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
236c1bb86cdSEric Blake     DWORD sectorsPerCluster, freeClusters, totalClusters, count;
237c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
238c1bb86cdSEric Blake     BOOL status;
239c1bb86cdSEric Blake 
240c1bb86cdSEric Blake     if (s->type == FTYPE_CD) {
241c1bb86cdSEric Blake         bs->bl.request_alignment = 2048;
242c1bb86cdSEric Blake         return;
243c1bb86cdSEric Blake     }
244c1bb86cdSEric Blake     if (s->type == FTYPE_HARDDISK) {
245c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
246c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
247c1bb86cdSEric Blake         if (status != 0) {
248c1bb86cdSEric Blake             bs->bl.request_alignment = dg.Geometry.BytesPerSector;
249c1bb86cdSEric Blake             return;
250c1bb86cdSEric Blake         }
251c1bb86cdSEric Blake         /* try GetDiskFreeSpace too */
252c1bb86cdSEric Blake     }
253c1bb86cdSEric Blake 
254c1bb86cdSEric Blake     if (s->drive_path[0]) {
255c1bb86cdSEric Blake         GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
256c1bb86cdSEric Blake                          &dg.Geometry.BytesPerSector,
257c1bb86cdSEric Blake                          &freeClusters, &totalClusters);
258c1bb86cdSEric Blake         bs->bl.request_alignment = dg.Geometry.BytesPerSector;
259de7056a3SEric Blake         return;
260c1bb86cdSEric Blake     }
261de7056a3SEric Blake 
262de7056a3SEric Blake     /* XXX Does Windows support AIO on less than 512-byte alignment? */
263de7056a3SEric Blake     bs->bl.request_alignment = 512;
264c1bb86cdSEric Blake }
265c1bb86cdSEric Blake 
266c1bb86cdSEric Blake static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
267c1bb86cdSEric Blake                             DWORD *overlapped)
268c1bb86cdSEric Blake {
269c1bb86cdSEric Blake     assert(access_flags != NULL);
270c1bb86cdSEric Blake     assert(overlapped != NULL);
271c1bb86cdSEric Blake 
272c1bb86cdSEric Blake     if (flags & BDRV_O_RDWR) {
273c1bb86cdSEric Blake         *access_flags = GENERIC_READ | GENERIC_WRITE;
274c1bb86cdSEric Blake     } else {
275c1bb86cdSEric Blake         *access_flags = GENERIC_READ;
276c1bb86cdSEric Blake     }
277c1bb86cdSEric Blake 
278c1bb86cdSEric Blake     *overlapped = FILE_ATTRIBUTE_NORMAL;
279c1bb86cdSEric Blake     if (use_aio) {
280c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_OVERLAPPED;
281c1bb86cdSEric Blake     }
282c1bb86cdSEric Blake     if (flags & BDRV_O_NOCACHE) {
283c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_NO_BUFFERING;
284c1bb86cdSEric Blake     }
285c1bb86cdSEric Blake }
286c1bb86cdSEric Blake 
287c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options,
288c1bb86cdSEric Blake                                Error **errp)
289c1bb86cdSEric Blake {
29003c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "file:", options);
291c1bb86cdSEric Blake }
292c1bb86cdSEric Blake 
293c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = {
294c1bb86cdSEric Blake     .name = "raw",
295c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
296c1bb86cdSEric Blake     .desc = {
297c1bb86cdSEric Blake         {
298c1bb86cdSEric Blake             .name = "filename",
299c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
300c1bb86cdSEric Blake             .help = "File name of the image",
301c1bb86cdSEric Blake         },
302c1bb86cdSEric Blake         {
303c1bb86cdSEric Blake             .name = "aio",
304c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
305c1bb86cdSEric Blake             .help = "host AIO implementation (threads, native)",
306c1bb86cdSEric Blake         },
3073b079ac0SKevin Wolf         {
3083b079ac0SKevin Wolf             .name = "locking",
3093b079ac0SKevin Wolf             .type = QEMU_OPT_STRING,
3103b079ac0SKevin Wolf             .help = "file locking mode (on/off/auto, default: auto)",
3113b079ac0SKevin Wolf         },
312c1bb86cdSEric Blake         { /* end of list */ }
313c1bb86cdSEric Blake     },
314c1bb86cdSEric Blake };
315c1bb86cdSEric Blake 
316c1bb86cdSEric Blake static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
317c1bb86cdSEric Blake {
318c1bb86cdSEric Blake     BlockdevAioOptions aio, aio_default;
319c1bb86cdSEric Blake 
320c1bb86cdSEric Blake     aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
321c1bb86cdSEric Blake                                               : BLOCKDEV_AIO_OPTIONS_THREADS;
322f7abe0ecSMarc-André Lureau     aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
32306c60b6cSMarkus Armbruster                           aio_default, errp);
324c1bb86cdSEric Blake 
325c1bb86cdSEric Blake     switch (aio) {
326c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_NATIVE:
327c1bb86cdSEric Blake         return true;
328c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_THREADS:
329c1bb86cdSEric Blake         return false;
330c1bb86cdSEric Blake     default:
331c1bb86cdSEric Blake         error_setg(errp, "Invalid AIO option");
332c1bb86cdSEric Blake     }
333c1bb86cdSEric Blake     return false;
334c1bb86cdSEric Blake }
335c1bb86cdSEric Blake 
336c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags,
337c1bb86cdSEric Blake                     Error **errp)
338c1bb86cdSEric Blake {
339c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
340c1bb86cdSEric Blake     int access_flags;
341c1bb86cdSEric Blake     DWORD overlapped;
342c1bb86cdSEric Blake     QemuOpts *opts;
343c1bb86cdSEric Blake     Error *local_err = NULL;
344c1bb86cdSEric Blake     const char *filename;
345c1bb86cdSEric Blake     bool use_aio;
3463b079ac0SKevin Wolf     OnOffAuto locking;
347c1bb86cdSEric Blake     int ret;
348c1bb86cdSEric Blake 
349c1bb86cdSEric Blake     s->type = FTYPE_FILE;
350c1bb86cdSEric Blake 
351c1bb86cdSEric Blake     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
352af175e85SMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
353c1bb86cdSEric Blake         ret = -EINVAL;
354c1bb86cdSEric Blake         goto fail;
355c1bb86cdSEric Blake     }
356c1bb86cdSEric Blake 
3573b079ac0SKevin Wolf     locking = qapi_enum_parse(&OnOffAuto_lookup,
3583b079ac0SKevin Wolf                               qemu_opt_get(opts, "locking"),
3593b079ac0SKevin Wolf                               ON_OFF_AUTO_AUTO, &local_err);
3603b079ac0SKevin Wolf     if (local_err) {
3613b079ac0SKevin Wolf         error_propagate(errp, local_err);
3623b079ac0SKevin Wolf         ret = -EINVAL;
3633b079ac0SKevin Wolf         goto fail;
3643b079ac0SKevin Wolf     }
3653b079ac0SKevin Wolf     switch (locking) {
3663b079ac0SKevin Wolf     case ON_OFF_AUTO_ON:
3671c3a555cSFam Zheng         error_setg(errp, "locking=on is not supported on Windows");
368cdece046SGerd Hoffmann         ret = -EINVAL;
3691c3a555cSFam Zheng         goto fail;
3703b079ac0SKevin Wolf     case ON_OFF_AUTO_OFF:
3713b079ac0SKevin Wolf     case ON_OFF_AUTO_AUTO:
3723b079ac0SKevin Wolf         break;
3733b079ac0SKevin Wolf     default:
3743b079ac0SKevin Wolf         g_assert_not_reached();
3751c3a555cSFam Zheng     }
3761c3a555cSFam Zheng 
377c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
378c1bb86cdSEric Blake 
379c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
380c1bb86cdSEric Blake     if (local_err) {
381c1bb86cdSEric Blake         error_propagate(errp, local_err);
382c1bb86cdSEric Blake         ret = -EINVAL;
383c1bb86cdSEric Blake         goto fail;
384c1bb86cdSEric Blake     }
385c1bb86cdSEric Blake 
386c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
387c1bb86cdSEric Blake 
388c1bb86cdSEric Blake     if (filename[0] && filename[1] == ':') {
389c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
390c1bb86cdSEric Blake     } else if (filename[0] == '\\' && filename[1] == '\\') {
391c1bb86cdSEric Blake         s->drive_path[0] = 0;
392c1bb86cdSEric Blake     } else {
393c1bb86cdSEric Blake         /* Relative path.  */
394c1bb86cdSEric Blake         char buf[MAX_PATH];
395c1bb86cdSEric Blake         GetCurrentDirectory(MAX_PATH, buf);
396c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
397c1bb86cdSEric Blake     }
398c1bb86cdSEric Blake 
399c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
400ebd979c7SViktor Prutyanov                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
401c1bb86cdSEric Blake                           OPEN_EXISTING, overlapped, NULL);
402c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
403c1bb86cdSEric Blake         int err = GetLastError();
404c1bb86cdSEric Blake 
405c1bb86cdSEric Blake         error_setg_win32(errp, err, "Could not open '%s'", filename);
406c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
407c1bb86cdSEric Blake             ret = -EACCES;
408c1bb86cdSEric Blake         } else {
409c1bb86cdSEric Blake             ret = -EINVAL;
410c1bb86cdSEric Blake         }
411c1bb86cdSEric Blake         goto fail;
412c1bb86cdSEric Blake     }
413c1bb86cdSEric Blake 
414c1bb86cdSEric Blake     if (use_aio) {
415c1bb86cdSEric Blake         s->aio = win32_aio_init();
416c1bb86cdSEric Blake         if (s->aio == NULL) {
417c1bb86cdSEric Blake             CloseHandle(s->hfile);
418c1bb86cdSEric Blake             error_setg(errp, "Could not initialize AIO");
419c1bb86cdSEric Blake             ret = -EINVAL;
420c1bb86cdSEric Blake             goto fail;
421c1bb86cdSEric Blake         }
422c1bb86cdSEric Blake 
423c1bb86cdSEric Blake         ret = win32_aio_attach(s->aio, s->hfile);
424c1bb86cdSEric Blake         if (ret < 0) {
425c1bb86cdSEric Blake             win32_aio_cleanup(s->aio);
426c1bb86cdSEric Blake             CloseHandle(s->hfile);
427c1bb86cdSEric Blake             error_setg_errno(errp, -ret, "Could not enable AIO");
428c1bb86cdSEric Blake             goto fail;
429c1bb86cdSEric Blake         }
430c1bb86cdSEric Blake 
431c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
432c1bb86cdSEric Blake     }
433c1bb86cdSEric Blake 
4348e519795SEric Blake     /* When extending regular files, we get zeros from the OS */
4358e519795SEric Blake     bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
4368e519795SEric Blake 
437c1bb86cdSEric Blake     ret = 0;
438c1bb86cdSEric Blake fail:
439c1bb86cdSEric Blake     qemu_opts_del(opts);
440c1bb86cdSEric Blake     return ret;
441c1bb86cdSEric Blake }
442c1bb86cdSEric Blake 
443de7056a3SEric Blake static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
444f7ef38ddSVladimir Sementsov-Ogievskiy                                   int64_t offset, int64_t bytes,
445f7ef38ddSVladimir Sementsov-Ogievskiy                                   QEMUIOVector *qiov, BdrvRequestFlags flags,
446c1bb86cdSEric Blake                                   BlockCompletionFunc *cb, void *opaque)
447c1bb86cdSEric Blake {
448c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
449c1bb86cdSEric Blake     if (s->aio) {
450de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
451de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_READ);
452c1bb86cdSEric Blake     } else {
453de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
454c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_READ);
455c1bb86cdSEric Blake     }
456c1bb86cdSEric Blake }
457c1bb86cdSEric Blake 
458de7056a3SEric Blake static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
459e75abedaSVladimir Sementsov-Ogievskiy                                    int64_t offset, int64_t bytes,
460e75abedaSVladimir Sementsov-Ogievskiy                                    QEMUIOVector *qiov, BdrvRequestFlags flags,
461c1bb86cdSEric Blake                                    BlockCompletionFunc *cb, void *opaque)
462c1bb86cdSEric Blake {
463c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
464c1bb86cdSEric Blake     if (s->aio) {
465de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
466de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_WRITE);
467c1bb86cdSEric Blake     } else {
468de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
469c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_WRITE);
470c1bb86cdSEric Blake     }
471c1bb86cdSEric Blake }
472c1bb86cdSEric Blake 
473c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
474c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
475c1bb86cdSEric Blake {
476c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
477c1bb86cdSEric Blake     return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
478c1bb86cdSEric Blake }
479c1bb86cdSEric Blake 
480c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs)
481c1bb86cdSEric Blake {
482c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
483c1bb86cdSEric Blake 
484c1bb86cdSEric Blake     if (s->aio) {
485c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
486c1bb86cdSEric Blake         win32_aio_cleanup(s->aio);
487c1bb86cdSEric Blake         s->aio = NULL;
488c1bb86cdSEric Blake     }
489c1bb86cdSEric Blake 
490c1bb86cdSEric Blake     CloseHandle(s->hfile);
491c1bb86cdSEric Blake     if (bs->open_flags & BDRV_O_TEMPORARY) {
492c1bb86cdSEric Blake         unlink(bs->filename);
493c1bb86cdSEric Blake     }
494c1bb86cdSEric Blake }
495c1bb86cdSEric Blake 
496061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
497c80d8b06SMax Reitz                                         bool exact, PreallocMode prealloc,
49892b92799SKevin Wolf                                         BdrvRequestFlags flags, Error **errp)
499c1bb86cdSEric Blake {
500c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
501c1bb86cdSEric Blake     LONG low, high;
502c1bb86cdSEric Blake     DWORD dwPtrLow;
503c1bb86cdSEric Blake 
5048243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
5058243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
506977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
5078243ccb7SMax Reitz         return -ENOTSUP;
5088243ccb7SMax Reitz     }
5098243ccb7SMax Reitz 
510c1bb86cdSEric Blake     low = offset;
511c1bb86cdSEric Blake     high = offset >> 32;
512c1bb86cdSEric Blake 
513c1bb86cdSEric Blake     /*
514c1bb86cdSEric Blake      * An error has occurred if the return value is INVALID_SET_FILE_POINTER
515c1bb86cdSEric Blake      * and GetLastError doesn't return NO_ERROR.
516c1bb86cdSEric Blake      */
517c1bb86cdSEric Blake     dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
518c1bb86cdSEric Blake     if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
5194bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetFilePointer error");
520c1bb86cdSEric Blake         return -EIO;
521c1bb86cdSEric Blake     }
522c1bb86cdSEric Blake     if (SetEndOfFile(s->hfile) == 0) {
5234bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
524c1bb86cdSEric Blake         return -EIO;
525c1bb86cdSEric Blake     }
526c1bb86cdSEric Blake     return 0;
527c1bb86cdSEric Blake }
528c1bb86cdSEric Blake 
529c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
530c1bb86cdSEric Blake {
531c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
532c1bb86cdSEric Blake     LARGE_INTEGER l;
533c1bb86cdSEric Blake     ULARGE_INTEGER available, total, total_free;
534c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
535c1bb86cdSEric Blake     DWORD count;
536c1bb86cdSEric Blake     BOOL status;
537c1bb86cdSEric Blake 
538c1bb86cdSEric Blake     switch(s->type) {
539c1bb86cdSEric Blake     case FTYPE_FILE:
540c1bb86cdSEric Blake         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
541c1bb86cdSEric Blake         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
542c1bb86cdSEric Blake             return -EIO;
543c1bb86cdSEric Blake         break;
544c1bb86cdSEric Blake     case FTYPE_CD:
545c1bb86cdSEric Blake         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
546c1bb86cdSEric Blake             return -EIO;
547c1bb86cdSEric Blake         l.QuadPart = total.QuadPart;
548c1bb86cdSEric Blake         break;
549c1bb86cdSEric Blake     case FTYPE_HARDDISK:
550c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
551c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
552c1bb86cdSEric Blake         if (status != 0) {
553c1bb86cdSEric Blake             l = dg.DiskSize;
554c1bb86cdSEric Blake         }
555c1bb86cdSEric Blake         break;
556c1bb86cdSEric Blake     default:
557c1bb86cdSEric Blake         return -EIO;
558c1bb86cdSEric Blake     }
559c1bb86cdSEric Blake     return l.QuadPart;
560c1bb86cdSEric Blake }
561c1bb86cdSEric Blake 
56282618d7bSEmanuele Giuseppe Esposito static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs)
563c1bb86cdSEric Blake {
564c1bb86cdSEric Blake     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
565c1bb86cdSEric Blake                                               DWORD * high);
566c1bb86cdSEric Blake     get_compressed_t get_compressed;
567c1bb86cdSEric Blake     struct _stati64 st;
568c1bb86cdSEric Blake     const char *filename = bs->filename;
569c1bb86cdSEric Blake     /* WinNT support GetCompressedFileSize to determine allocate size */
570c1bb86cdSEric Blake     get_compressed =
571c1bb86cdSEric Blake         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
572c1bb86cdSEric Blake                                             "GetCompressedFileSizeA");
573c1bb86cdSEric Blake     if (get_compressed) {
574c1bb86cdSEric Blake         DWORD high, low;
575c1bb86cdSEric Blake         low = get_compressed(filename, &high);
576c1bb86cdSEric Blake         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
577c1bb86cdSEric Blake             return (((int64_t) high) << 32) + low;
578c1bb86cdSEric Blake         }
579c1bb86cdSEric Blake     }
580c1bb86cdSEric Blake 
581c1bb86cdSEric Blake     if (_stati64(filename, &st) < 0) {
582c1bb86cdSEric Blake         return -1;
583c1bb86cdSEric Blake     }
584c1bb86cdSEric Blake     return st.st_size;
585c1bb86cdSEric Blake }
586c1bb86cdSEric Blake 
5873766ef57SKevin Wolf static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
5883766ef57SKevin Wolf {
5893766ef57SKevin Wolf     BlockdevCreateOptionsFile *file_opts;
5903766ef57SKevin Wolf     int fd;
5913766ef57SKevin Wolf 
5923766ef57SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_FILE);
5933766ef57SKevin Wolf     file_opts = &options->u.file;
5943766ef57SKevin Wolf 
5953766ef57SKevin Wolf     if (file_opts->has_preallocation) {
5963766ef57SKevin Wolf         error_setg(errp, "Preallocation is not supported on Windows");
5973766ef57SKevin Wolf         return -EINVAL;
5983766ef57SKevin Wolf     }
5993766ef57SKevin Wolf     if (file_opts->has_nocow) {
6003766ef57SKevin Wolf         error_setg(errp, "nocow is not supported on Windows");
6013766ef57SKevin Wolf         return -EINVAL;
6023766ef57SKevin Wolf     }
6033766ef57SKevin Wolf 
604b18a24a9SDaniel P. Berrangé     fd = qemu_create(file_opts->filename, O_WRONLY | O_TRUNC | O_BINARY,
605b18a24a9SDaniel P. Berrangé                      0644, errp);
6063766ef57SKevin Wolf     if (fd < 0) {
6073766ef57SKevin Wolf         return -EIO;
6083766ef57SKevin Wolf     }
6093766ef57SKevin Wolf     set_sparse(fd);
6103766ef57SKevin Wolf     ftruncate(fd, file_opts->size);
6113766ef57SKevin Wolf     qemu_close(fd);
6123766ef57SKevin Wolf 
6133766ef57SKevin Wolf     return 0;
6143766ef57SKevin Wolf }
6153766ef57SKevin Wolf 
616*4ec8df01SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
617*4ec8df01SKevin Wolf raw_co_create_opts(BlockDriver *drv, const char *filename,
618*4ec8df01SKevin Wolf                    QemuOpts *opts, Error **errp)
619c1bb86cdSEric Blake {
6203766ef57SKevin Wolf     BlockdevCreateOptions options;
621c1bb86cdSEric Blake     int64_t total_size = 0;
622c1bb86cdSEric Blake 
623c1bb86cdSEric Blake     strstart(filename, "file:", &filename);
624c1bb86cdSEric Blake 
625c1bb86cdSEric Blake     /* Read out options */
626c1bb86cdSEric Blake     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
627c1bb86cdSEric Blake                           BDRV_SECTOR_SIZE);
628c1bb86cdSEric Blake 
6293766ef57SKevin Wolf     options = (BlockdevCreateOptions) {
6303766ef57SKevin Wolf         .driver     = BLOCKDEV_DRIVER_FILE,
6313766ef57SKevin Wolf         .u.file     = {
6323766ef57SKevin Wolf             .filename           = (char *) filename,
6333766ef57SKevin Wolf             .size               = total_size,
6343766ef57SKevin Wolf             .has_preallocation  = false,
6353766ef57SKevin Wolf             .has_nocow          = false,
6363766ef57SKevin Wolf         },
6373766ef57SKevin Wolf     };
6383766ef57SKevin Wolf     return raw_co_create(&options, errp);
639c1bb86cdSEric Blake }
640c1bb86cdSEric Blake 
641ebd979c7SViktor Prutyanov static int raw_reopen_prepare(BDRVReopenState *state,
642ebd979c7SViktor Prutyanov                               BlockReopenQueue *queue, Error **errp)
643ebd979c7SViktor Prutyanov {
644ebd979c7SViktor Prutyanov     BDRVRawState *s = state->bs->opaque;
645ebd979c7SViktor Prutyanov     BDRVRawReopenState *rs;
646ebd979c7SViktor Prutyanov     int access_flags;
647ebd979c7SViktor Prutyanov     DWORD overlapped;
648ebd979c7SViktor Prutyanov     int ret = 0;
649ebd979c7SViktor Prutyanov 
650ebd979c7SViktor Prutyanov     if (s->type != FTYPE_FILE) {
651ebd979c7SViktor Prutyanov         error_setg(errp, "Can only reopen files");
652ebd979c7SViktor Prutyanov         return -EINVAL;
653ebd979c7SViktor Prutyanov     }
654ebd979c7SViktor Prutyanov 
655ebd979c7SViktor Prutyanov     rs = g_new0(BDRVRawReopenState, 1);
656ebd979c7SViktor Prutyanov 
657ebd979c7SViktor Prutyanov     /*
658ebd979c7SViktor Prutyanov      * We do not support changing any options (only flags). By leaving
659ebd979c7SViktor Prutyanov      * all options in state->options, we tell the generic reopen code
660ebd979c7SViktor Prutyanov      * that we do not support changing any of them, so it will verify
661ebd979c7SViktor Prutyanov      * that their values did not change.
662ebd979c7SViktor Prutyanov      */
663ebd979c7SViktor Prutyanov 
664ebd979c7SViktor Prutyanov     raw_parse_flags(state->flags, s->aio != NULL, &access_flags, &overlapped);
665ebd979c7SViktor Prutyanov     rs->hfile = CreateFile(state->bs->filename, access_flags,
666ebd979c7SViktor Prutyanov                            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
667ebd979c7SViktor Prutyanov                            OPEN_EXISTING, overlapped, NULL);
668ebd979c7SViktor Prutyanov 
669ebd979c7SViktor Prutyanov     if (rs->hfile == INVALID_HANDLE_VALUE) {
670ebd979c7SViktor Prutyanov         int err = GetLastError();
671ebd979c7SViktor Prutyanov 
672ebd979c7SViktor Prutyanov         error_setg_win32(errp, err, "Could not reopen '%s'",
673ebd979c7SViktor Prutyanov                          state->bs->filename);
674ebd979c7SViktor Prutyanov         if (err == ERROR_ACCESS_DENIED) {
675ebd979c7SViktor Prutyanov             ret = -EACCES;
676ebd979c7SViktor Prutyanov         } else {
677ebd979c7SViktor Prutyanov             ret = -EINVAL;
678ebd979c7SViktor Prutyanov         }
679ebd979c7SViktor Prutyanov         goto fail;
680ebd979c7SViktor Prutyanov     }
681ebd979c7SViktor Prutyanov 
682ebd979c7SViktor Prutyanov     if (s->aio) {
683ebd979c7SViktor Prutyanov         ret = win32_aio_attach(s->aio, rs->hfile);
684ebd979c7SViktor Prutyanov         if (ret < 0) {
685ebd979c7SViktor Prutyanov             error_setg_errno(errp, -ret, "Could not enable AIO");
686ebd979c7SViktor Prutyanov             CloseHandle(rs->hfile);
687ebd979c7SViktor Prutyanov             goto fail;
688ebd979c7SViktor Prutyanov         }
689ebd979c7SViktor Prutyanov     }
690ebd979c7SViktor Prutyanov 
691ebd979c7SViktor Prutyanov     state->opaque = rs;
692ebd979c7SViktor Prutyanov 
693ebd979c7SViktor Prutyanov     return 0;
694ebd979c7SViktor Prutyanov 
695ebd979c7SViktor Prutyanov fail:
696ebd979c7SViktor Prutyanov     g_free(rs);
697ebd979c7SViktor Prutyanov     state->opaque = NULL;
698ebd979c7SViktor Prutyanov 
699ebd979c7SViktor Prutyanov     return ret;
700ebd979c7SViktor Prutyanov }
701ebd979c7SViktor Prutyanov 
702ebd979c7SViktor Prutyanov static void raw_reopen_commit(BDRVReopenState *state)
703ebd979c7SViktor Prutyanov {
704ebd979c7SViktor Prutyanov     BDRVRawState *s = state->bs->opaque;
705ebd979c7SViktor Prutyanov     BDRVRawReopenState *rs = state->opaque;
706ebd979c7SViktor Prutyanov 
707ebd979c7SViktor Prutyanov     assert(rs != NULL);
708ebd979c7SViktor Prutyanov 
709ebd979c7SViktor Prutyanov     CloseHandle(s->hfile);
710ebd979c7SViktor Prutyanov     s->hfile = rs->hfile;
711ebd979c7SViktor Prutyanov 
712ebd979c7SViktor Prutyanov     g_free(rs);
713ebd979c7SViktor Prutyanov     state->opaque = NULL;
714ebd979c7SViktor Prutyanov }
715ebd979c7SViktor Prutyanov 
716ebd979c7SViktor Prutyanov static void raw_reopen_abort(BDRVReopenState *state)
717ebd979c7SViktor Prutyanov {
718ebd979c7SViktor Prutyanov     BDRVRawReopenState *rs = state->opaque;
719ebd979c7SViktor Prutyanov 
720ebd979c7SViktor Prutyanov     if (!rs) {
721ebd979c7SViktor Prutyanov         return;
722ebd979c7SViktor Prutyanov     }
723ebd979c7SViktor Prutyanov 
724ebd979c7SViktor Prutyanov     if (rs->hfile != INVALID_HANDLE_VALUE) {
725ebd979c7SViktor Prutyanov         CloseHandle(rs->hfile);
726ebd979c7SViktor Prutyanov     }
727ebd979c7SViktor Prutyanov 
728ebd979c7SViktor Prutyanov     g_free(rs);
729ebd979c7SViktor Prutyanov     state->opaque = NULL;
730ebd979c7SViktor Prutyanov }
731ebd979c7SViktor Prutyanov 
732c1bb86cdSEric Blake static QemuOptsList raw_create_opts = {
733c1bb86cdSEric Blake     .name = "raw-create-opts",
734c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
735c1bb86cdSEric Blake     .desc = {
736c1bb86cdSEric Blake         {
737c1bb86cdSEric Blake             .name = BLOCK_OPT_SIZE,
738c1bb86cdSEric Blake             .type = QEMU_OPT_SIZE,
739c1bb86cdSEric Blake             .help = "Virtual disk size"
740c1bb86cdSEric Blake         },
741c1bb86cdSEric Blake         { /* end of list */ }
742c1bb86cdSEric Blake     }
743c1bb86cdSEric Blake };
744c1bb86cdSEric Blake 
745c1bb86cdSEric Blake BlockDriver bdrv_file = {
746c1bb86cdSEric Blake     .format_name	= "file",
747c1bb86cdSEric Blake     .protocol_name	= "file",
748c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
749c1bb86cdSEric Blake     .bdrv_needs_filename = true,
750c1bb86cdSEric Blake     .bdrv_parse_filename = raw_parse_filename,
751c1bb86cdSEric Blake     .bdrv_file_open     = raw_open,
752c1bb86cdSEric Blake     .bdrv_refresh_limits = raw_probe_alignment,
753c1bb86cdSEric Blake     .bdrv_close         = raw_close,
754efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts = raw_co_create_opts,
755c1bb86cdSEric Blake     .bdrv_has_zero_init = bdrv_has_zero_init_1,
756c1bb86cdSEric Blake 
757ebd979c7SViktor Prutyanov     .bdrv_reopen_prepare = raw_reopen_prepare,
758ebd979c7SViktor Prutyanov     .bdrv_reopen_commit  = raw_reopen_commit,
759ebd979c7SViktor Prutyanov     .bdrv_reopen_abort   = raw_reopen_abort,
760ebd979c7SViktor Prutyanov 
761de7056a3SEric Blake     .bdrv_aio_preadv    = raw_aio_preadv,
762de7056a3SEric Blake     .bdrv_aio_pwritev   = raw_aio_pwritev,
763c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
764c1bb86cdSEric Blake 
765061ca8a3SKevin Wolf     .bdrv_co_truncate   = raw_co_truncate,
766c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength  = raw_co_getlength,
76782618d7bSEmanuele Giuseppe Esposito     .bdrv_co_get_allocated_file_size
76882618d7bSEmanuele Giuseppe Esposito                         = raw_co_get_allocated_file_size,
769c1bb86cdSEric Blake 
770c1bb86cdSEric Blake     .create_opts        = &raw_create_opts,
771c1bb86cdSEric Blake };
772c1bb86cdSEric Blake 
773c1bb86cdSEric Blake /***********************************************/
774c1bb86cdSEric Blake /* host device */
775c1bb86cdSEric Blake 
776c1bb86cdSEric Blake static int find_cdrom(char *cdrom_name, int cdrom_name_size)
777c1bb86cdSEric Blake {
778c1bb86cdSEric Blake     char drives[256], *pdrv = drives;
779c1bb86cdSEric Blake     UINT type;
780c1bb86cdSEric Blake 
781c1bb86cdSEric Blake     memset(drives, 0, sizeof(drives));
782c1bb86cdSEric Blake     GetLogicalDriveStrings(sizeof(drives), drives);
783c1bb86cdSEric Blake     while(pdrv[0] != '\0') {
784c1bb86cdSEric Blake         type = GetDriveType(pdrv);
785c1bb86cdSEric Blake         switch(type) {
786c1bb86cdSEric Blake         case DRIVE_CDROM:
787c1bb86cdSEric Blake             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
788c1bb86cdSEric Blake             return 0;
789c1bb86cdSEric Blake             break;
790c1bb86cdSEric Blake         }
791c1bb86cdSEric Blake         pdrv += lstrlen(pdrv) + 1;
792c1bb86cdSEric Blake     }
793c1bb86cdSEric Blake     return -1;
794c1bb86cdSEric Blake }
795c1bb86cdSEric Blake 
796c1bb86cdSEric Blake static int find_device_type(BlockDriverState *bs, const char *filename)
797c1bb86cdSEric Blake {
798c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
799c1bb86cdSEric Blake     UINT type;
800c1bb86cdSEric Blake     const char *p;
801c1bb86cdSEric Blake 
802c1bb86cdSEric Blake     if (strstart(filename, "\\\\.\\", &p) ||
803c1bb86cdSEric Blake         strstart(filename, "//./", &p)) {
804c1bb86cdSEric Blake         if (stristart(p, "PhysicalDrive", NULL))
805c1bb86cdSEric Blake             return FTYPE_HARDDISK;
806c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
807c1bb86cdSEric Blake         type = GetDriveType(s->drive_path);
808c1bb86cdSEric Blake         switch (type) {
809c1bb86cdSEric Blake         case DRIVE_REMOVABLE:
810c1bb86cdSEric Blake         case DRIVE_FIXED:
811c1bb86cdSEric Blake             return FTYPE_HARDDISK;
812c1bb86cdSEric Blake         case DRIVE_CDROM:
813c1bb86cdSEric Blake             return FTYPE_CD;
814c1bb86cdSEric Blake         default:
815c1bb86cdSEric Blake             return FTYPE_FILE;
816c1bb86cdSEric Blake         }
817c1bb86cdSEric Blake     } else {
818c1bb86cdSEric Blake         return FTYPE_FILE;
819c1bb86cdSEric Blake     }
820c1bb86cdSEric Blake }
821c1bb86cdSEric Blake 
822c1bb86cdSEric Blake static int hdev_probe_device(const char *filename)
823c1bb86cdSEric Blake {
824c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL))
825c1bb86cdSEric Blake         return 100;
826c1bb86cdSEric Blake     if (is_windows_drive(filename))
827c1bb86cdSEric Blake         return 100;
828c1bb86cdSEric Blake     return 0;
829c1bb86cdSEric Blake }
830c1bb86cdSEric Blake 
831c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options,
832c1bb86cdSEric Blake                                 Error **errp)
833c1bb86cdSEric Blake {
83403c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
835c1bb86cdSEric Blake }
836c1bb86cdSEric Blake 
837de7056a3SEric Blake static void hdev_refresh_limits(BlockDriverState *bs, Error **errp)
838de7056a3SEric Blake {
839de7056a3SEric Blake     /* XXX Does Windows support AIO on less than 512-byte alignment? */
840de7056a3SEric Blake     bs->bl.request_alignment = 512;
841de7056a3SEric Blake }
842de7056a3SEric Blake 
843c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
844c1bb86cdSEric Blake                      Error **errp)
845c1bb86cdSEric Blake {
846c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
847c1bb86cdSEric Blake     int access_flags, create_flags;
848c1bb86cdSEric Blake     int ret = 0;
849c1bb86cdSEric Blake     DWORD overlapped;
850c1bb86cdSEric Blake     char device_name[64];
851c1bb86cdSEric Blake 
852c1bb86cdSEric Blake     Error *local_err = NULL;
853c1bb86cdSEric Blake     const char *filename;
854c1bb86cdSEric Blake     bool use_aio;
855c1bb86cdSEric Blake 
856c1bb86cdSEric Blake     QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
857c1bb86cdSEric Blake                                       &error_abort);
858af175e85SMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
859c1bb86cdSEric Blake         ret = -EINVAL;
860c1bb86cdSEric Blake         goto done;
861c1bb86cdSEric Blake     }
862c1bb86cdSEric Blake 
863c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
864c1bb86cdSEric Blake 
865c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
866c1bb86cdSEric Blake     if (!local_err && use_aio) {
867c1bb86cdSEric Blake         error_setg(&local_err, "AIO is not supported on Windows host devices");
868c1bb86cdSEric Blake     }
869c1bb86cdSEric Blake     if (local_err) {
870c1bb86cdSEric Blake         error_propagate(errp, local_err);
871c1bb86cdSEric Blake         ret = -EINVAL;
872c1bb86cdSEric Blake         goto done;
873c1bb86cdSEric Blake     }
874c1bb86cdSEric Blake 
875c1bb86cdSEric Blake     if (strstart(filename, "/dev/cdrom", NULL)) {
876c1bb86cdSEric Blake         if (find_cdrom(device_name, sizeof(device_name)) < 0) {
877c1bb86cdSEric Blake             error_setg(errp, "Could not open CD-ROM drive");
878c1bb86cdSEric Blake             ret = -ENOENT;
879c1bb86cdSEric Blake             goto done;
880c1bb86cdSEric Blake         }
881c1bb86cdSEric Blake         filename = device_name;
882c1bb86cdSEric Blake     } else {
883c1bb86cdSEric Blake         /* transform drive letters into device name */
884c1bb86cdSEric Blake         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
885c1bb86cdSEric Blake              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
886c1bb86cdSEric Blake             filename[1] == ':' && filename[2] == '\0') {
887c1bb86cdSEric Blake             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
888c1bb86cdSEric Blake             filename = device_name;
889c1bb86cdSEric Blake         }
890c1bb86cdSEric Blake     }
891c1bb86cdSEric Blake     s->type = find_device_type(bs, filename);
892c1bb86cdSEric Blake 
893c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
894c1bb86cdSEric Blake 
895c1bb86cdSEric Blake     create_flags = OPEN_EXISTING;
896c1bb86cdSEric Blake 
897c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
898c1bb86cdSEric Blake                           FILE_SHARE_READ, NULL,
899c1bb86cdSEric Blake                           create_flags, overlapped, NULL);
900c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
901c1bb86cdSEric Blake         int err = GetLastError();
902c1bb86cdSEric Blake 
903c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
904c1bb86cdSEric Blake             ret = -EACCES;
905c1bb86cdSEric Blake         } else {
906c1bb86cdSEric Blake             ret = -EINVAL;
907c1bb86cdSEric Blake         }
908c1bb86cdSEric Blake         error_setg_errno(errp, -ret, "Could not open device");
909c1bb86cdSEric Blake         goto done;
910c1bb86cdSEric Blake     }
911c1bb86cdSEric Blake 
912c1bb86cdSEric Blake done:
913c1bb86cdSEric Blake     qemu_opts_del(opts);
914c1bb86cdSEric Blake     return ret;
915c1bb86cdSEric Blake }
916c1bb86cdSEric Blake 
917c1bb86cdSEric Blake static BlockDriver bdrv_host_device = {
918c1bb86cdSEric Blake     .format_name	= "host_device",
919c1bb86cdSEric Blake     .protocol_name	= "host_device",
920c1bb86cdSEric Blake     .instance_size	= sizeof(BDRVRawState),
921c1bb86cdSEric Blake     .bdrv_needs_filename = true,
922c1bb86cdSEric Blake     .bdrv_parse_filename = hdev_parse_filename,
923c1bb86cdSEric Blake     .bdrv_probe_device	= hdev_probe_device,
924c1bb86cdSEric Blake     .bdrv_file_open	= hdev_open,
925c1bb86cdSEric Blake     .bdrv_close		= raw_close,
926de7056a3SEric Blake     .bdrv_refresh_limits = hdev_refresh_limits,
927c1bb86cdSEric Blake 
928de7056a3SEric Blake     .bdrv_aio_preadv    = raw_aio_preadv,
929de7056a3SEric Blake     .bdrv_aio_pwritev   = raw_aio_pwritev,
930c1bb86cdSEric Blake     .bdrv_aio_flush     = raw_aio_flush,
931c1bb86cdSEric Blake 
932c1bb86cdSEric Blake     .bdrv_detach_aio_context = raw_detach_aio_context,
933c1bb86cdSEric Blake     .bdrv_attach_aio_context = raw_attach_aio_context,
934c1bb86cdSEric Blake 
935c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength                = raw_co_getlength,
936c1bb86cdSEric Blake     .has_variable_length              = true,
93782618d7bSEmanuele Giuseppe Esposito     .bdrv_co_get_allocated_file_size  = raw_co_get_allocated_file_size,
938c1bb86cdSEric Blake };
939c1bb86cdSEric Blake 
940c1bb86cdSEric Blake static void bdrv_file_init(void)
941c1bb86cdSEric Blake {
942c1bb86cdSEric Blake     bdrv_register(&bdrv_file);
943c1bb86cdSEric Blake     bdrv_register(&bdrv_host_device);
944c1bb86cdSEric Blake }
945c1bb86cdSEric Blake 
946c1bb86cdSEric Blake block_init(bdrv_file_init);
947