xref: /openbmc/qemu/block/file-win32.c (revision f7ef38dd1310d7d9db76d0aa16899cbc5744f36d)
1c1bb86cdSEric Blake /*
2c1bb86cdSEric Blake  * Block driver for RAW files (win32)
3c1bb86cdSEric Blake  *
4c1bb86cdSEric Blake  * Copyright (c) 2006 Fabrice Bellard
5c1bb86cdSEric Blake  *
6c1bb86cdSEric Blake  * Permission is hereby granted, free of charge, to any person obtaining a copy
7c1bb86cdSEric Blake  * of this software and associated documentation files (the "Software"), to deal
8c1bb86cdSEric Blake  * in the Software without restriction, including without limitation the rights
9c1bb86cdSEric Blake  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10c1bb86cdSEric Blake  * copies of the Software, and to permit persons to whom the Software is
11c1bb86cdSEric Blake  * furnished to do so, subject to the following conditions:
12c1bb86cdSEric Blake  *
13c1bb86cdSEric Blake  * The above copyright notice and this permission notice shall be included in
14c1bb86cdSEric Blake  * all copies or substantial portions of the Software.
15c1bb86cdSEric Blake  *
16c1bb86cdSEric Blake  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17c1bb86cdSEric Blake  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18c1bb86cdSEric Blake  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19c1bb86cdSEric Blake  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20c1bb86cdSEric Blake  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21c1bb86cdSEric Blake  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22c1bb86cdSEric Blake  * THE SOFTWARE.
23c1bb86cdSEric Blake  */
24922a01a0SMarkus Armbruster 
25c1bb86cdSEric Blake #include "qemu/osdep.h"
26c1bb86cdSEric Blake #include "qapi/error.h"
27c1bb86cdSEric Blake #include "qemu/cutils.h"
28c1bb86cdSEric Blake #include "block/block_int.h"
29c1bb86cdSEric Blake #include "qemu/module.h"
30922a01a0SMarkus Armbruster #include "qemu/option.h"
31c1bb86cdSEric Blake #include "block/raw-aio.h"
32c1bb86cdSEric Blake #include "trace.h"
33c1bb86cdSEric Blake #include "block/thread-pool.h"
34c1bb86cdSEric Blake #include "qemu/iov.h"
35452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
36c1bb86cdSEric Blake #include "qapi/qmp/qstring.h"
37c1bb86cdSEric Blake #include <windows.h>
38c1bb86cdSEric Blake #include <winioctl.h>
39c1bb86cdSEric Blake 
40c1bb86cdSEric Blake #define FTYPE_FILE 0
41c1bb86cdSEric Blake #define FTYPE_CD     1
42c1bb86cdSEric Blake #define FTYPE_HARDDISK 2
43c1bb86cdSEric Blake 
44c1bb86cdSEric Blake typedef struct RawWin32AIOData {
45c1bb86cdSEric Blake     BlockDriverState *bs;
46c1bb86cdSEric Blake     HANDLE hfile;
47c1bb86cdSEric Blake     struct iovec *aio_iov;
48c1bb86cdSEric Blake     int aio_niov;
49c1bb86cdSEric Blake     size_t aio_nbytes;
50c1bb86cdSEric Blake     off64_t aio_offset;
51c1bb86cdSEric Blake     int aio_type;
52c1bb86cdSEric Blake } RawWin32AIOData;
53c1bb86cdSEric Blake 
54c1bb86cdSEric Blake typedef struct BDRVRawState {
55c1bb86cdSEric Blake     HANDLE hfile;
56c1bb86cdSEric Blake     int type;
57c1bb86cdSEric Blake     char drive_path[16]; /* format: "d:\" */
58c1bb86cdSEric Blake     QEMUWin32AIOState *aio;
59c1bb86cdSEric Blake } BDRVRawState;
60c1bb86cdSEric Blake 
61ebd979c7SViktor Prutyanov typedef struct BDRVRawReopenState {
62ebd979c7SViktor Prutyanov     HANDLE hfile;
63ebd979c7SViktor Prutyanov } BDRVRawReopenState;
64ebd979c7SViktor Prutyanov 
65c1bb86cdSEric Blake /*
66c1bb86cdSEric Blake  * Read/writes the data to/from a given linear buffer.
67c1bb86cdSEric Blake  *
68c1bb86cdSEric Blake  * Returns the number of bytes handles or -errno in case of an error. Short
69c1bb86cdSEric Blake  * reads are only returned if the end of the file is reached.
70c1bb86cdSEric Blake  */
71c1bb86cdSEric Blake static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
72c1bb86cdSEric Blake {
73c1bb86cdSEric Blake     size_t offset = 0;
74c1bb86cdSEric Blake     int i;
75c1bb86cdSEric Blake 
76c1bb86cdSEric Blake     for (i = 0; i < aiocb->aio_niov; i++) {
77c1bb86cdSEric Blake         OVERLAPPED ov;
78c1bb86cdSEric Blake         DWORD ret, ret_count, len;
79c1bb86cdSEric Blake 
80c1bb86cdSEric Blake         memset(&ov, 0, sizeof(ov));
81c1bb86cdSEric Blake         ov.Offset = (aiocb->aio_offset + offset);
82c1bb86cdSEric Blake         ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
83c1bb86cdSEric Blake         len = aiocb->aio_iov[i].iov_len;
84c1bb86cdSEric Blake         if (aiocb->aio_type & QEMU_AIO_WRITE) {
85c1bb86cdSEric Blake             ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
86c1bb86cdSEric Blake                             len, &ret_count, &ov);
87c1bb86cdSEric Blake         } else {
88c1bb86cdSEric Blake             ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
89c1bb86cdSEric Blake                            len, &ret_count, &ov);
90c1bb86cdSEric Blake         }
91c1bb86cdSEric Blake         if (!ret) {
92c1bb86cdSEric Blake             ret_count = 0;
93c1bb86cdSEric Blake         }
94c1bb86cdSEric Blake         if (ret_count != len) {
95c1bb86cdSEric Blake             offset += ret_count;
96c1bb86cdSEric Blake             break;
97c1bb86cdSEric Blake         }
98c1bb86cdSEric Blake         offset += len;
99c1bb86cdSEric Blake     }
100c1bb86cdSEric Blake 
101c1bb86cdSEric Blake     return offset;
102c1bb86cdSEric Blake }
103c1bb86cdSEric Blake 
104c1bb86cdSEric Blake static int aio_worker(void *arg)
105c1bb86cdSEric Blake {
106c1bb86cdSEric Blake     RawWin32AIOData *aiocb = arg;
107c1bb86cdSEric Blake     ssize_t ret = 0;
108c1bb86cdSEric Blake     size_t count;
109c1bb86cdSEric Blake 
110c1bb86cdSEric Blake     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
111c1bb86cdSEric Blake     case QEMU_AIO_READ:
112c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
113c1bb86cdSEric Blake         if (count < aiocb->aio_nbytes) {
114c1bb86cdSEric Blake             /* A short read means that we have reached EOF. Pad the buffer
115c1bb86cdSEric Blake              * with zeros for bytes after EOF. */
116c1bb86cdSEric Blake             iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
117c1bb86cdSEric Blake                       0, aiocb->aio_nbytes - count);
118c1bb86cdSEric Blake 
119c1bb86cdSEric Blake             count = aiocb->aio_nbytes;
120c1bb86cdSEric Blake         }
121c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
122c1bb86cdSEric Blake             ret = 0;
123c1bb86cdSEric Blake         } else {
124c1bb86cdSEric Blake             ret = -EINVAL;
125c1bb86cdSEric Blake         }
126c1bb86cdSEric Blake         break;
127c1bb86cdSEric Blake     case QEMU_AIO_WRITE:
128c1bb86cdSEric Blake         count = handle_aiocb_rw(aiocb);
129c1bb86cdSEric Blake         if (count == aiocb->aio_nbytes) {
130c1bb86cdSEric Blake             ret = 0;
131c1bb86cdSEric Blake         } else {
132c1bb86cdSEric Blake             ret = -EINVAL;
133c1bb86cdSEric Blake         }
134c1bb86cdSEric Blake         break;
135c1bb86cdSEric Blake     case QEMU_AIO_FLUSH:
136c1bb86cdSEric Blake         if (!FlushFileBuffers(aiocb->hfile)) {
137c1bb86cdSEric Blake             return -EIO;
138c1bb86cdSEric Blake         }
139c1bb86cdSEric Blake         break;
140c1bb86cdSEric Blake     default:
141c1bb86cdSEric Blake         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
142c1bb86cdSEric Blake         ret = -EINVAL;
143c1bb86cdSEric Blake         break;
144c1bb86cdSEric Blake     }
145c1bb86cdSEric Blake 
146c1bb86cdSEric Blake     g_free(aiocb);
147c1bb86cdSEric Blake     return ret;
148c1bb86cdSEric Blake }
149c1bb86cdSEric Blake 
150c1bb86cdSEric Blake static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
151c1bb86cdSEric Blake         int64_t offset, QEMUIOVector *qiov, int count,
152c1bb86cdSEric Blake         BlockCompletionFunc *cb, void *opaque, int type)
153c1bb86cdSEric Blake {
154c1bb86cdSEric Blake     RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
155c1bb86cdSEric Blake     ThreadPool *pool;
156c1bb86cdSEric Blake 
157c1bb86cdSEric Blake     acb->bs = bs;
158c1bb86cdSEric Blake     acb->hfile = hfile;
159c1bb86cdSEric Blake     acb->aio_type = type;
160c1bb86cdSEric Blake 
161c1bb86cdSEric Blake     if (qiov) {
162c1bb86cdSEric Blake         acb->aio_iov = qiov->iov;
163c1bb86cdSEric Blake         acb->aio_niov = qiov->niov;
164c1bb86cdSEric Blake         assert(qiov->size == count);
165c1bb86cdSEric Blake     }
166c1bb86cdSEric Blake     acb->aio_nbytes = count;
167c1bb86cdSEric Blake     acb->aio_offset = offset;
168c1bb86cdSEric Blake 
169f8a30874SFam Zheng     trace_file_paio_submit(acb, opaque, offset, count, type);
170c1bb86cdSEric Blake     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
171c1bb86cdSEric Blake     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
172c1bb86cdSEric Blake }
173c1bb86cdSEric Blake 
174c1bb86cdSEric Blake int qemu_ftruncate64(int fd, int64_t length)
175c1bb86cdSEric Blake {
176c1bb86cdSEric Blake     LARGE_INTEGER li;
177c1bb86cdSEric Blake     DWORD dw;
178c1bb86cdSEric Blake     LONG high;
179c1bb86cdSEric Blake     HANDLE h;
180c1bb86cdSEric Blake     BOOL res;
181c1bb86cdSEric Blake 
182c1bb86cdSEric Blake     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
183c1bb86cdSEric Blake         return -1;
184c1bb86cdSEric Blake 
185c1bb86cdSEric Blake     h = (HANDLE)_get_osfhandle(fd);
186c1bb86cdSEric Blake 
187c1bb86cdSEric Blake     /* get current position, ftruncate do not change position */
188c1bb86cdSEric Blake     li.HighPart = 0;
189c1bb86cdSEric Blake     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
190c1bb86cdSEric Blake     if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
191c1bb86cdSEric Blake         return -1;
192c1bb86cdSEric Blake     }
193c1bb86cdSEric Blake 
194c1bb86cdSEric Blake     high = length >> 32;
195c1bb86cdSEric Blake     dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
196c1bb86cdSEric Blake     if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
197c1bb86cdSEric Blake         return -1;
198c1bb86cdSEric Blake     }
199c1bb86cdSEric Blake     res = SetEndOfFile(h);
200c1bb86cdSEric Blake 
201c1bb86cdSEric Blake     /* back to old position */
202c1bb86cdSEric Blake     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
203c1bb86cdSEric Blake     return res ? 0 : -1;
204c1bb86cdSEric Blake }
205c1bb86cdSEric Blake 
206c1bb86cdSEric Blake static int set_sparse(int fd)
207c1bb86cdSEric Blake {
208c1bb86cdSEric Blake     DWORD returned;
209c1bb86cdSEric Blake     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
210c1bb86cdSEric Blake                                  NULL, 0, NULL, 0, &returned, NULL);
211c1bb86cdSEric Blake }
212c1bb86cdSEric Blake 
213c1bb86cdSEric Blake static void raw_detach_aio_context(BlockDriverState *bs)
214c1bb86cdSEric Blake {
215c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
216c1bb86cdSEric Blake 
217c1bb86cdSEric Blake     if (s->aio) {
218c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
219c1bb86cdSEric Blake     }
220c1bb86cdSEric Blake }
221c1bb86cdSEric Blake 
222c1bb86cdSEric Blake static void raw_attach_aio_context(BlockDriverState *bs,
223c1bb86cdSEric Blake                                    AioContext *new_context)
224c1bb86cdSEric Blake {
225c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
226c1bb86cdSEric Blake 
227c1bb86cdSEric Blake     if (s->aio) {
228c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, new_context);
229c1bb86cdSEric Blake     }
230c1bb86cdSEric Blake }
231c1bb86cdSEric Blake 
232c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
233c1bb86cdSEric Blake {
234c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
235c1bb86cdSEric Blake     DWORD sectorsPerCluster, freeClusters, totalClusters, count;
236c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
237c1bb86cdSEric Blake     BOOL status;
238c1bb86cdSEric Blake 
239c1bb86cdSEric Blake     if (s->type == FTYPE_CD) {
240c1bb86cdSEric Blake         bs->bl.request_alignment = 2048;
241c1bb86cdSEric Blake         return;
242c1bb86cdSEric Blake     }
243c1bb86cdSEric Blake     if (s->type == FTYPE_HARDDISK) {
244c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
245c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
246c1bb86cdSEric Blake         if (status != 0) {
247c1bb86cdSEric Blake             bs->bl.request_alignment = dg.Geometry.BytesPerSector;
248c1bb86cdSEric Blake             return;
249c1bb86cdSEric Blake         }
250c1bb86cdSEric Blake         /* try GetDiskFreeSpace too */
251c1bb86cdSEric Blake     }
252c1bb86cdSEric Blake 
253c1bb86cdSEric Blake     if (s->drive_path[0]) {
254c1bb86cdSEric Blake         GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
255c1bb86cdSEric Blake                          &dg.Geometry.BytesPerSector,
256c1bb86cdSEric Blake                          &freeClusters, &totalClusters);
257c1bb86cdSEric Blake         bs->bl.request_alignment = dg.Geometry.BytesPerSector;
258de7056a3SEric Blake         return;
259c1bb86cdSEric Blake     }
260de7056a3SEric Blake 
261de7056a3SEric Blake     /* XXX Does Windows support AIO on less than 512-byte alignment? */
262de7056a3SEric Blake     bs->bl.request_alignment = 512;
263c1bb86cdSEric Blake }
264c1bb86cdSEric Blake 
265c1bb86cdSEric Blake static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
266c1bb86cdSEric Blake                             DWORD *overlapped)
267c1bb86cdSEric Blake {
268c1bb86cdSEric Blake     assert(access_flags != NULL);
269c1bb86cdSEric Blake     assert(overlapped != NULL);
270c1bb86cdSEric Blake 
271c1bb86cdSEric Blake     if (flags & BDRV_O_RDWR) {
272c1bb86cdSEric Blake         *access_flags = GENERIC_READ | GENERIC_WRITE;
273c1bb86cdSEric Blake     } else {
274c1bb86cdSEric Blake         *access_flags = GENERIC_READ;
275c1bb86cdSEric Blake     }
276c1bb86cdSEric Blake 
277c1bb86cdSEric Blake     *overlapped = FILE_ATTRIBUTE_NORMAL;
278c1bb86cdSEric Blake     if (use_aio) {
279c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_OVERLAPPED;
280c1bb86cdSEric Blake     }
281c1bb86cdSEric Blake     if (flags & BDRV_O_NOCACHE) {
282c1bb86cdSEric Blake         *overlapped |= FILE_FLAG_NO_BUFFERING;
283c1bb86cdSEric Blake     }
284c1bb86cdSEric Blake }
285c1bb86cdSEric Blake 
286c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options,
287c1bb86cdSEric Blake                                Error **errp)
288c1bb86cdSEric Blake {
28903c320d8SMax Reitz     bdrv_parse_filename_strip_prefix(filename, "file:", options);
290c1bb86cdSEric Blake }
291c1bb86cdSEric Blake 
292c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = {
293c1bb86cdSEric Blake     .name = "raw",
294c1bb86cdSEric Blake     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
295c1bb86cdSEric Blake     .desc = {
296c1bb86cdSEric Blake         {
297c1bb86cdSEric Blake             .name = "filename",
298c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
299c1bb86cdSEric Blake             .help = "File name of the image",
300c1bb86cdSEric Blake         },
301c1bb86cdSEric Blake         {
302c1bb86cdSEric Blake             .name = "aio",
303c1bb86cdSEric Blake             .type = QEMU_OPT_STRING,
304c1bb86cdSEric Blake             .help = "host AIO implementation (threads, native)",
305c1bb86cdSEric Blake         },
3063b079ac0SKevin Wolf         {
3073b079ac0SKevin Wolf             .name = "locking",
3083b079ac0SKevin Wolf             .type = QEMU_OPT_STRING,
3093b079ac0SKevin Wolf             .help = "file locking mode (on/off/auto, default: auto)",
3103b079ac0SKevin Wolf         },
311c1bb86cdSEric Blake         { /* end of list */ }
312c1bb86cdSEric Blake     },
313c1bb86cdSEric Blake };
314c1bb86cdSEric Blake 
315c1bb86cdSEric Blake static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
316c1bb86cdSEric Blake {
317c1bb86cdSEric Blake     BlockdevAioOptions aio, aio_default;
318c1bb86cdSEric Blake 
319c1bb86cdSEric Blake     aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
320c1bb86cdSEric Blake                                               : BLOCKDEV_AIO_OPTIONS_THREADS;
321f7abe0ecSMarc-André Lureau     aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
32206c60b6cSMarkus Armbruster                           aio_default, errp);
323c1bb86cdSEric Blake 
324c1bb86cdSEric Blake     switch (aio) {
325c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_NATIVE:
326c1bb86cdSEric Blake         return true;
327c1bb86cdSEric Blake     case BLOCKDEV_AIO_OPTIONS_THREADS:
328c1bb86cdSEric Blake         return false;
329c1bb86cdSEric Blake     default:
330c1bb86cdSEric Blake         error_setg(errp, "Invalid AIO option");
331c1bb86cdSEric Blake     }
332c1bb86cdSEric Blake     return false;
333c1bb86cdSEric Blake }
334c1bb86cdSEric Blake 
335c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags,
336c1bb86cdSEric Blake                     Error **errp)
337c1bb86cdSEric Blake {
338c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
339c1bb86cdSEric Blake     int access_flags;
340c1bb86cdSEric Blake     DWORD overlapped;
341c1bb86cdSEric Blake     QemuOpts *opts;
342c1bb86cdSEric Blake     Error *local_err = NULL;
343c1bb86cdSEric Blake     const char *filename;
344c1bb86cdSEric Blake     bool use_aio;
3453b079ac0SKevin Wolf     OnOffAuto locking;
346c1bb86cdSEric Blake     int ret;
347c1bb86cdSEric Blake 
348c1bb86cdSEric Blake     s->type = FTYPE_FILE;
349c1bb86cdSEric Blake 
350c1bb86cdSEric Blake     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
351af175e85SMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
352c1bb86cdSEric Blake         ret = -EINVAL;
353c1bb86cdSEric Blake         goto fail;
354c1bb86cdSEric Blake     }
355c1bb86cdSEric Blake 
3563b079ac0SKevin Wolf     locking = qapi_enum_parse(&OnOffAuto_lookup,
3573b079ac0SKevin Wolf                               qemu_opt_get(opts, "locking"),
3583b079ac0SKevin Wolf                               ON_OFF_AUTO_AUTO, &local_err);
3593b079ac0SKevin Wolf     if (local_err) {
3603b079ac0SKevin Wolf         error_propagate(errp, local_err);
3613b079ac0SKevin Wolf         ret = -EINVAL;
3623b079ac0SKevin Wolf         goto fail;
3633b079ac0SKevin Wolf     }
3643b079ac0SKevin Wolf     switch (locking) {
3653b079ac0SKevin Wolf     case ON_OFF_AUTO_ON:
3661c3a555cSFam Zheng         error_setg(errp, "locking=on is not supported on Windows");
367cdece046SGerd Hoffmann         ret = -EINVAL;
3681c3a555cSFam Zheng         goto fail;
3693b079ac0SKevin Wolf     case ON_OFF_AUTO_OFF:
3703b079ac0SKevin Wolf     case ON_OFF_AUTO_AUTO:
3713b079ac0SKevin Wolf         break;
3723b079ac0SKevin Wolf     default:
3733b079ac0SKevin Wolf         g_assert_not_reached();
3741c3a555cSFam Zheng     }
3751c3a555cSFam Zheng 
376c1bb86cdSEric Blake     filename = qemu_opt_get(opts, "filename");
377c1bb86cdSEric Blake 
378c1bb86cdSEric Blake     use_aio = get_aio_option(opts, flags, &local_err);
379c1bb86cdSEric Blake     if (local_err) {
380c1bb86cdSEric Blake         error_propagate(errp, local_err);
381c1bb86cdSEric Blake         ret = -EINVAL;
382c1bb86cdSEric Blake         goto fail;
383c1bb86cdSEric Blake     }
384c1bb86cdSEric Blake 
385c1bb86cdSEric Blake     raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
386c1bb86cdSEric Blake 
387c1bb86cdSEric Blake     if (filename[0] && filename[1] == ':') {
388c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
389c1bb86cdSEric Blake     } else if (filename[0] == '\\' && filename[1] == '\\') {
390c1bb86cdSEric Blake         s->drive_path[0] = 0;
391c1bb86cdSEric Blake     } else {
392c1bb86cdSEric Blake         /* Relative path.  */
393c1bb86cdSEric Blake         char buf[MAX_PATH];
394c1bb86cdSEric Blake         GetCurrentDirectory(MAX_PATH, buf);
395c1bb86cdSEric Blake         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
396c1bb86cdSEric Blake     }
397c1bb86cdSEric Blake 
398c1bb86cdSEric Blake     s->hfile = CreateFile(filename, access_flags,
399ebd979c7SViktor Prutyanov                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
400c1bb86cdSEric Blake                           OPEN_EXISTING, overlapped, NULL);
401c1bb86cdSEric Blake     if (s->hfile == INVALID_HANDLE_VALUE) {
402c1bb86cdSEric Blake         int err = GetLastError();
403c1bb86cdSEric Blake 
404c1bb86cdSEric Blake         error_setg_win32(errp, err, "Could not open '%s'", filename);
405c1bb86cdSEric Blake         if (err == ERROR_ACCESS_DENIED) {
406c1bb86cdSEric Blake             ret = -EACCES;
407c1bb86cdSEric Blake         } else {
408c1bb86cdSEric Blake             ret = -EINVAL;
409c1bb86cdSEric Blake         }
410c1bb86cdSEric Blake         goto fail;
411c1bb86cdSEric Blake     }
412c1bb86cdSEric Blake 
413c1bb86cdSEric Blake     if (use_aio) {
414c1bb86cdSEric Blake         s->aio = win32_aio_init();
415c1bb86cdSEric Blake         if (s->aio == NULL) {
416c1bb86cdSEric Blake             CloseHandle(s->hfile);
417c1bb86cdSEric Blake             error_setg(errp, "Could not initialize AIO");
418c1bb86cdSEric Blake             ret = -EINVAL;
419c1bb86cdSEric Blake             goto fail;
420c1bb86cdSEric Blake         }
421c1bb86cdSEric Blake 
422c1bb86cdSEric Blake         ret = win32_aio_attach(s->aio, s->hfile);
423c1bb86cdSEric Blake         if (ret < 0) {
424c1bb86cdSEric Blake             win32_aio_cleanup(s->aio);
425c1bb86cdSEric Blake             CloseHandle(s->hfile);
426c1bb86cdSEric Blake             error_setg_errno(errp, -ret, "Could not enable AIO");
427c1bb86cdSEric Blake             goto fail;
428c1bb86cdSEric Blake         }
429c1bb86cdSEric Blake 
430c1bb86cdSEric Blake         win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
431c1bb86cdSEric Blake     }
432c1bb86cdSEric Blake 
4338e519795SEric Blake     /* When extending regular files, we get zeros from the OS */
4348e519795SEric Blake     bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
4358e519795SEric Blake 
436c1bb86cdSEric Blake     ret = 0;
437c1bb86cdSEric Blake fail:
438c1bb86cdSEric Blake     qemu_opts_del(opts);
439c1bb86cdSEric Blake     return ret;
440c1bb86cdSEric Blake }
441c1bb86cdSEric Blake 
442de7056a3SEric Blake static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
443*f7ef38ddSVladimir Sementsov-Ogievskiy                                   int64_t offset, int64_t bytes,
444*f7ef38ddSVladimir Sementsov-Ogievskiy                                   QEMUIOVector *qiov, BdrvRequestFlags flags,
445c1bb86cdSEric Blake                                   BlockCompletionFunc *cb, void *opaque)
446c1bb86cdSEric Blake {
447c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
448c1bb86cdSEric Blake     if (s->aio) {
449de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
450de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_READ);
451c1bb86cdSEric Blake     } else {
452de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
453c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_READ);
454c1bb86cdSEric Blake     }
455c1bb86cdSEric Blake }
456c1bb86cdSEric Blake 
457de7056a3SEric Blake static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
458de7056a3SEric Blake                                    uint64_t offset, uint64_t bytes,
459de7056a3SEric Blake                                    QEMUIOVector *qiov, int flags,
460c1bb86cdSEric Blake                                    BlockCompletionFunc *cb, void *opaque)
461c1bb86cdSEric Blake {
462c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
463c1bb86cdSEric Blake     if (s->aio) {
464de7056a3SEric Blake         return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
465de7056a3SEric Blake                                 cb, opaque, QEMU_AIO_WRITE);
466c1bb86cdSEric Blake     } else {
467de7056a3SEric Blake         return paio_submit(bs, s->hfile, offset, qiov, bytes,
468c1bb86cdSEric Blake                            cb, opaque, QEMU_AIO_WRITE);
469c1bb86cdSEric Blake     }
470c1bb86cdSEric Blake }
471c1bb86cdSEric Blake 
472c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
473c1bb86cdSEric Blake                          BlockCompletionFunc *cb, void *opaque)
474c1bb86cdSEric Blake {
475c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
476c1bb86cdSEric Blake     return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
477c1bb86cdSEric Blake }
478c1bb86cdSEric Blake 
479c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs)
480c1bb86cdSEric Blake {
481c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
482c1bb86cdSEric Blake 
483c1bb86cdSEric Blake     if (s->aio) {
484c1bb86cdSEric Blake         win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
485c1bb86cdSEric Blake         win32_aio_cleanup(s->aio);
486c1bb86cdSEric Blake         s->aio = NULL;
487c1bb86cdSEric Blake     }
488c1bb86cdSEric Blake 
489c1bb86cdSEric Blake     CloseHandle(s->hfile);
490c1bb86cdSEric Blake     if (bs->open_flags & BDRV_O_TEMPORARY) {
491c1bb86cdSEric Blake         unlink(bs->filename);
492c1bb86cdSEric Blake     }
493c1bb86cdSEric Blake }
494c1bb86cdSEric Blake 
495061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
496c80d8b06SMax Reitz                                         bool exact, PreallocMode prealloc,
49792b92799SKevin Wolf                                         BdrvRequestFlags flags, Error **errp)
498c1bb86cdSEric Blake {
499c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
500c1bb86cdSEric Blake     LONG low, high;
501c1bb86cdSEric Blake     DWORD dwPtrLow;
502c1bb86cdSEric Blake 
5038243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
5048243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
505977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
5068243ccb7SMax Reitz         return -ENOTSUP;
5078243ccb7SMax Reitz     }
5088243ccb7SMax Reitz 
509c1bb86cdSEric Blake     low = offset;
510c1bb86cdSEric Blake     high = offset >> 32;
511c1bb86cdSEric Blake 
512c1bb86cdSEric Blake     /*
513c1bb86cdSEric Blake      * An error has occurred if the return value is INVALID_SET_FILE_POINTER
514c1bb86cdSEric Blake      * and GetLastError doesn't return NO_ERROR.
515c1bb86cdSEric Blake      */
516c1bb86cdSEric Blake     dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
517c1bb86cdSEric Blake     if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
5184bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetFilePointer error");
519c1bb86cdSEric Blake         return -EIO;
520c1bb86cdSEric Blake     }
521c1bb86cdSEric Blake     if (SetEndOfFile(s->hfile) == 0) {
5224bff28b8SMax Reitz         error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
523c1bb86cdSEric Blake         return -EIO;
524c1bb86cdSEric Blake     }
525c1bb86cdSEric Blake     return 0;
526c1bb86cdSEric Blake }
527c1bb86cdSEric Blake 
528c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs)
529c1bb86cdSEric Blake {
530c1bb86cdSEric Blake     BDRVRawState *s = bs->opaque;
531c1bb86cdSEric Blake     LARGE_INTEGER l;
532c1bb86cdSEric Blake     ULARGE_INTEGER available, total, total_free;
533c1bb86cdSEric Blake     DISK_GEOMETRY_EX dg;
534c1bb86cdSEric Blake     DWORD count;
535c1bb86cdSEric Blake     BOOL status;
536c1bb86cdSEric Blake 
537c1bb86cdSEric Blake     switch(s->type) {
538c1bb86cdSEric Blake     case FTYPE_FILE:
539c1bb86cdSEric Blake         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
540c1bb86cdSEric Blake         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
541c1bb86cdSEric Blake             return -EIO;
542c1bb86cdSEric Blake         break;
543c1bb86cdSEric Blake     case FTYPE_CD:
544c1bb86cdSEric Blake         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
545c1bb86cdSEric Blake             return -EIO;
546c1bb86cdSEric Blake         l.QuadPart = total.QuadPart;
547c1bb86cdSEric Blake         break;
548c1bb86cdSEric Blake     case FTYPE_HARDDISK:
549c1bb86cdSEric Blake         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
550c1bb86cdSEric Blake                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
551c1bb86cdSEric Blake         if (status != 0) {
552c1bb86cdSEric Blake             l = dg.DiskSize;
553c1bb86cdSEric Blake         }
554c1bb86cdSEric Blake         break;
555c1bb86cdSEric Blake     default:
556c1bb86cdSEric Blake         return -EIO;
557c1bb86cdSEric Blake     }
558c1bb86cdSEric Blake     return l.QuadPart;
559c1bb86cdSEric Blake }
560c1bb86cdSEric Blake 
561c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
562c1bb86cdSEric Blake {
563c1bb86cdSEric Blake     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
564c1bb86cdSEric Blake                                               DWORD * high);
565c1bb86cdSEric Blake     get_compressed_t get_compressed;
566c1bb86cdSEric Blake     struct _stati64 st;
567c1bb86cdSEric Blake     const char *filename = bs->filename;
568c1bb86cdSEric Blake     /* WinNT support GetCompressedFileSize to determine allocate size */
569c1bb86cdSEric Blake     get_compressed =
570c1bb86cdSEric Blake         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
571c1bb86cdSEric Blake                                             "GetCompressedFileSizeA");
572c1bb86cdSEric Blake     if (get_compressed) {
573c1bb86cdSEric Blake         DWORD high, low;
574c1bb86cdSEric Blake         low = get_compressed(filename, &high);
575c1bb86cdSEric Blake         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
576c1bb86cdSEric Blake             return (((int64_t) high) << 32) + low;
577c1bb86cdSEric Blake         }
578c1bb86cdSEric Blake     }
579c1bb86cdSEric Blake 
580c1bb86cdSEric Blake     if (_stati64(filename, &st) < 0) {
581c1bb86cdSEric Blake         return -1;
582c1bb86cdSEric Blake     }
583c1bb86cdSEric Blake     return st.st_size;
584c1bb86cdSEric Blake }
585c1bb86cdSEric Blake 
5863766ef57SKevin Wolf static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
5873766ef57SKevin Wolf {
5883766ef57SKevin Wolf     BlockdevCreateOptionsFile *file_opts;
5893766ef57SKevin Wolf     int fd;
5903766ef57SKevin Wolf 
5913766ef57SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_FILE);
5923766ef57SKevin Wolf     file_opts = &options->u.file;
5933766ef57SKevin Wolf 
5943766ef57SKevin Wolf     if (file_opts->has_preallocation) {
5953766ef57SKevin Wolf         error_setg(errp, "Preallocation is not supported on Windows");
5963766ef57SKevin Wolf         return -EINVAL;
5973766ef57SKevin Wolf     }
5983766ef57SKevin Wolf     if (file_opts->has_nocow) {
5993766ef57SKevin Wolf         error_setg(errp, "nocow is not supported on Windows");
6003766ef57SKevin Wolf         return -EINVAL;
6013766ef57SKevin Wolf     }
6023766ef57SKevin Wolf 
603b18a24a9SDaniel P. Berrangé     fd = qemu_create(file_opts->filename, O_WRONLY | O_TRUNC | O_BINARY,
604b18a24a9SDaniel P. Berrangé                      0644, errp);
6053766ef57SKevin Wolf     if (fd < 0) {
6063766ef57SKevin Wolf         return -EIO;
6073766ef57SKevin Wolf     }
6083766ef57SKevin Wolf     set_sparse(fd);
6093766ef57SKevin Wolf     ftruncate(fd, file_opts->size);
6103766ef57SKevin Wolf     qemu_close(fd);
6113766ef57SKevin Wolf 
6123766ef57SKevin Wolf     return 0;
6133766ef57SKevin Wolf }
6143766ef57SKevin Wolf 
615b92902dfSMaxim Levitsky static int coroutine_fn raw_co_create_opts(BlockDriver *drv,
616b92902dfSMaxim Levitsky                                            const char *filename,
617b92902dfSMaxim Levitsky                                            QemuOpts *opts,
618efc75e2aSStefan Hajnoczi                                            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,
766c1bb86cdSEric Blake     .bdrv_getlength	= raw_getlength,
767c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
768c1bb86cdSEric Blake                         = raw_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 
935c1bb86cdSEric Blake     .bdrv_getlength      = raw_getlength,
936c1bb86cdSEric Blake     .has_variable_length = true,
937c1bb86cdSEric Blake 
938c1bb86cdSEric Blake     .bdrv_get_allocated_file_size
939c1bb86cdSEric Blake                         = raw_get_allocated_file_size,
940c1bb86cdSEric Blake };
941c1bb86cdSEric Blake 
942c1bb86cdSEric Blake static void bdrv_file_init(void)
943c1bb86cdSEric Blake {
944c1bb86cdSEric Blake     bdrv_register(&bdrv_file);
945c1bb86cdSEric Blake     bdrv_register(&bdrv_host_device);
946c1bb86cdSEric Blake }
947c1bb86cdSEric Blake 
948c1bb86cdSEric Blake block_init(bdrv_file_init);
949