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