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