]> git.proxmox.com Git - mirror_qemu.git/blame - block/file-win32.c
block: Support byte-based aio callbacks
[mirror_qemu.git] / block / file-win32.c
CommitLineData
223d4670
TS
1/*
2 * Block driver for RAW files (win32)
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
922a01a0 24
80c71a24 25#include "qemu/osdep.h"
da34e65c 26#include "qapi/error.h"
f348b6d1 27#include "qemu/cutils.h"
737e150e 28#include "block/block_int.h"
1de7afc9 29#include "qemu/module.h"
922a01a0 30#include "qemu/option.h"
0187f5c9 31#include "block/raw-aio.h"
fc4edb84 32#include "trace.h"
737e150e 33#include "block/thread-pool.h"
1de7afc9 34#include "qemu/iov.h"
452fcdbc 35#include "qapi/qmp/qdict.h"
d49b6836 36#include "qapi/qmp/qstring.h"
49dc768d 37#include <windows.h>
223d4670
TS
38#include <winioctl.h>
39
40#define FTYPE_FILE 0
41#define FTYPE_CD 1
42#define FTYPE_HARDDISK 2
43
fc4edb84
PB
44typedef struct RawWin32AIOData {
45 BlockDriverState *bs;
46 HANDLE hfile;
47 struct iovec *aio_iov;
48 int aio_niov;
49 size_t aio_nbytes;
50 off64_t aio_offset;
51 int aio_type;
52} RawWin32AIOData;
53
223d4670
TS
54typedef struct BDRVRawState {
55 HANDLE hfile;
56 int type;
57 char drive_path[16]; /* format: "d:\" */
a2736526 58 QEMUWin32AIOState *aio;
223d4670
TS
59} BDRVRawState;
60
fc4edb84
PB
61/*
62 * Read/writes the data to/from a given linear buffer.
63 *
64 * Returns the number of bytes handles or -errno in case of an error. Short
65 * reads are only returned if the end of the file is reached.
66 */
67static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
68{
69 size_t offset = 0;
70 int i;
71
72 for (i = 0; i < aiocb->aio_niov; i++) {
73 OVERLAPPED ov;
74 DWORD ret, ret_count, len;
75
76 memset(&ov, 0, sizeof(ov));
77 ov.Offset = (aiocb->aio_offset + offset);
78 ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
79 len = aiocb->aio_iov[i].iov_len;
80 if (aiocb->aio_type & QEMU_AIO_WRITE) {
81 ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
82 len, &ret_count, &ov);
83 } else {
84 ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
85 len, &ret_count, &ov);
86 }
87 if (!ret) {
88 ret_count = 0;
89 }
90 if (ret_count != len) {
56e023af 91 offset += ret_count;
fc4edb84
PB
92 break;
93 }
94 offset += len;
95 }
96
97 return offset;
98}
99
100static int aio_worker(void *arg)
101{
102 RawWin32AIOData *aiocb = arg;
103 ssize_t ret = 0;
104 size_t count;
105
106 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
107 case QEMU_AIO_READ:
108 count = handle_aiocb_rw(aiocb);
c0191e76 109 if (count < aiocb->aio_nbytes) {
fc4edb84
PB
110 /* A short read means that we have reached EOF. Pad the buffer
111 * with zeros for bytes after EOF. */
112 iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
113 0, aiocb->aio_nbytes - count);
114
115 count = aiocb->aio_nbytes;
116 }
117 if (count == aiocb->aio_nbytes) {
118 ret = 0;
119 } else {
120 ret = -EINVAL;
121 }
122 break;
123 case QEMU_AIO_WRITE:
124 count = handle_aiocb_rw(aiocb);
125 if (count == aiocb->aio_nbytes) {
5d555030 126 ret = 0;
fc4edb84 127 } else {
5d555030 128 ret = -EINVAL;
fc4edb84
PB
129 }
130 break;
131 case QEMU_AIO_FLUSH:
132 if (!FlushFileBuffers(aiocb->hfile)) {
133 return -EIO;
134 }
135 break;
136 default:
137 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
138 ret = -EINVAL;
139 break;
140 }
141
c84b3192 142 g_free(aiocb);
fc4edb84
PB
143 return ret;
144}
145
7c84b1b8 146static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
36e3b2e7 147 int64_t offset, QEMUIOVector *qiov, int count,
097310b5 148 BlockCompletionFunc *cb, void *opaque, int type)
fc4edb84 149{
c84b3192 150 RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
c4d9d196 151 ThreadPool *pool;
fc4edb84
PB
152
153 acb->bs = bs;
154 acb->hfile = hfile;
155 acb->aio_type = type;
156
157 if (qiov) {
158 acb->aio_iov = qiov->iov;
159 acb->aio_niov = qiov->niov;
36e3b2e7 160 assert(qiov->size == count);
fc4edb84 161 }
36e3b2e7
EB
162 acb->aio_nbytes = count;
163 acb->aio_offset = offset;
fc4edb84 164
36e3b2e7 165 trace_paio_submit(acb, opaque, offset, count, type);
c4d9d196
SH
166 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
167 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
fc4edb84
PB
168}
169
223d4670
TS
170int qemu_ftruncate64(int fd, int64_t length)
171{
172 LARGE_INTEGER li;
2c993ec2 173 DWORD dw;
223d4670
TS
174 LONG high;
175 HANDLE h;
176 BOOL res;
177
178 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
179 return -1;
180
181 h = (HANDLE)_get_osfhandle(fd);
182
183 /* get current position, ftruncate do not change position */
184 li.HighPart = 0;
185 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
2c993ec2 186 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
223d4670 187 return -1;
2c993ec2 188 }
223d4670
TS
189
190 high = length >> 32;
2c993ec2
SW
191 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
192 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
223d4670 193 return -1;
2c993ec2 194 }
223d4670
TS
195 res = SetEndOfFile(h);
196
197 /* back to old position */
198 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
199 return res ? 0 : -1;
200}
201
202static int set_sparse(int fd)
203{
204 DWORD returned;
205 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
206 NULL, 0, NULL, 0, &returned, NULL);
207}
208
85ebd381
SH
209static void raw_detach_aio_context(BlockDriverState *bs)
210{
211 BDRVRawState *s = bs->opaque;
212
213 if (s->aio) {
214 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
215 }
216}
217
218static void raw_attach_aio_context(BlockDriverState *bs,
219 AioContext *new_context)
220{
221 BDRVRawState *s = bs->opaque;
222
223 if (s->aio) {
224 win32_aio_attach_aio_context(s->aio, new_context);
225 }
226}
227
2914a1de 228static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
c25f53b0
PB
229{
230 BDRVRawState *s = bs->opaque;
231 DWORD sectorsPerCluster, freeClusters, totalClusters, count;
232 DISK_GEOMETRY_EX dg;
233 BOOL status;
234
235 if (s->type == FTYPE_CD) {
a5b8dd2c 236 bs->bl.request_alignment = 2048;
c25f53b0
PB
237 return;
238 }
239 if (s->type == FTYPE_HARDDISK) {
240 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
241 NULL, 0, &dg, sizeof(dg), &count, NULL);
242 if (status != 0) {
a5b8dd2c 243 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
c25f53b0
PB
244 return;
245 }
246 /* try GetDiskFreeSpace too */
247 }
248
249 if (s->drive_path[0]) {
250 GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
251 &dg.Geometry.BytesPerSector,
252 &freeClusters, &totalClusters);
a5b8dd2c 253 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
c25f53b0
PB
254 }
255}
256
0a4279d9
KW
257static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
258 DWORD *overlapped)
6a8dc042
JC
259{
260 assert(access_flags != NULL);
261 assert(overlapped != NULL);
262
263 if (flags & BDRV_O_RDWR) {
264 *access_flags = GENERIC_READ | GENERIC_WRITE;
265 } else {
266 *access_flags = GENERIC_READ;
267 }
268
269 *overlapped = FILE_ATTRIBUTE_NORMAL;
0a4279d9 270 if (use_aio) {
a2736526
PB
271 *overlapped |= FILE_FLAG_OVERLAPPED;
272 }
6a8dc042
JC
273 if (flags & BDRV_O_NOCACHE) {
274 *overlapped |= FILE_FLAG_NO_BUFFERING;
275 }
6a8dc042
JC
276}
277
7dc74db8
HR
278static void raw_parse_filename(const char *filename, QDict *options,
279 Error **errp)
280{
03c320d8 281 bdrv_parse_filename_strip_prefix(filename, "file:", options);
7dc74db8
HR
282}
283
8a79380b
KW
284static QemuOptsList raw_runtime_opts = {
285 .name = "raw",
286 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
287 .desc = {
288 {
289 .name = "filename",
290 .type = QEMU_OPT_STRING,
291 .help = "File name of the image",
292 },
0a4279d9
KW
293 {
294 .name = "aio",
295 .type = QEMU_OPT_STRING,
296 .help = "host AIO implementation (threads, native)",
297 },
8a79380b
KW
298 { /* end of list */ }
299 },
300};
301
0a4279d9
KW
302static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
303{
304 BlockdevAioOptions aio, aio_default;
305
306 aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
307 : BLOCKDEV_AIO_OPTIONS_THREADS;
f7abe0ec 308 aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
06c60b6c 309 aio_default, errp);
0a4279d9
KW
310
311 switch (aio) {
312 case BLOCKDEV_AIO_OPTIONS_NATIVE:
313 return true;
314 case BLOCKDEV_AIO_OPTIONS_THREADS:
315 return false;
316 default:
317 error_setg(errp, "Invalid AIO option");
318 }
319 return false;
320}
321
015a1036
HR
322static int raw_open(BlockDriverState *bs, QDict *options, int flags,
323 Error **errp)
223d4670
TS
324{
325 BDRVRawState *s = bs->opaque;
9a2d77ad 326 int access_flags;
223d4670 327 DWORD overlapped;
8a79380b
KW
328 QemuOpts *opts;
329 Error *local_err = NULL;
330 const char *filename;
0a4279d9 331 bool use_aio;
8a79380b 332 int ret;
223d4670
TS
333
334 s->type = FTYPE_FILE;
335
87ea75d5 336 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
8a79380b 337 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 338 if (local_err) {
c6252b7c 339 error_propagate(errp, local_err);
8a79380b
KW
340 ret = -EINVAL;
341 goto fail;
342 }
343
1c3a555c
FZ
344 if (qdict_get_try_bool(options, "locking", false)) {
345 error_setg(errp, "locking=on is not supported on Windows");
cdece046 346 ret = -EINVAL;
1c3a555c
FZ
347 goto fail;
348 }
349
8a79380b
KW
350 filename = qemu_opt_get(opts, "filename");
351
0a4279d9
KW
352 use_aio = get_aio_option(opts, flags, &local_err);
353 if (local_err) {
354 error_propagate(errp, local_err);
355 ret = -EINVAL;
356 goto fail;
357 }
358
359 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
8a79380b 360
c25f53b0
PB
361 if (filename[0] && filename[1] == ':') {
362 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
363 } else if (filename[0] == '\\' && filename[1] == '\\') {
364 s->drive_path[0] = 0;
365 } else {
366 /* Relative path. */
367 char buf[MAX_PATH];
368 GetCurrentDirectory(MAX_PATH, buf);
369 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
370 }
371
223d4670
TS
372 s->hfile = CreateFile(filename, access_flags,
373 FILE_SHARE_READ, NULL,
9a2d77ad 374 OPEN_EXISTING, overlapped, NULL);
223d4670
TS
375 if (s->hfile == INVALID_HANDLE_VALUE) {
376 int err = GetLastError();
377
09237757 378 error_setg_win32(errp, err, "Could not open '%s'", filename);
8a79380b
KW
379 if (err == ERROR_ACCESS_DENIED) {
380 ret = -EACCES;
381 } else {
382 ret = -EINVAL;
383 }
384 goto fail;
a2736526
PB
385 }
386
0a4279d9 387 if (use_aio) {
99cc5989
SH
388 s->aio = win32_aio_init();
389 if (s->aio == NULL) {
390 CloseHandle(s->hfile);
391 error_setg(errp, "Could not initialize AIO");
392 ret = -EINVAL;
393 goto fail;
394 }
395
396 ret = win32_aio_attach(s->aio, s->hfile);
a2736526 397 if (ret < 0) {
99cc5989 398 win32_aio_cleanup(s->aio);
a2736526 399 CloseHandle(s->hfile);
c6252b7c 400 error_setg_errno(errp, -ret, "Could not enable AIO");
8a79380b 401 goto fail;
a2736526 402 }
85ebd381
SH
403
404 win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
223d4670 405 }
8a79380b
KW
406
407 ret = 0;
408fail:
409 qemu_opts_del(opts);
410 return ret;
223d4670
TS
411}
412
7c84b1b8 413static BlockAIOCB *raw_aio_readv(BlockDriverState *bs,
fc4edb84 414 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 415 BlockCompletionFunc *cb, void *opaque)
223d4670
TS
416{
417 BDRVRawState *s = bs->opaque;
a2736526
PB
418 if (s->aio) {
419 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
36e3b2e7 420 nb_sectors, cb, opaque, QEMU_AIO_READ);
a2736526 421 } else {
36e3b2e7
EB
422 return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
423 nb_sectors << BDRV_SECTOR_BITS,
a2736526
PB
424 cb, opaque, QEMU_AIO_READ);
425 }
223d4670
TS
426}
427
7c84b1b8 428static BlockAIOCB *raw_aio_writev(BlockDriverState *bs,
fc4edb84 429 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 430 BlockCompletionFunc *cb, void *opaque)
223d4670
TS
431{
432 BDRVRawState *s = bs->opaque;
a2736526
PB
433 if (s->aio) {
434 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
36e3b2e7 435 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
a2736526 436 } else {
36e3b2e7
EB
437 return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
438 nb_sectors << BDRV_SECTOR_BITS,
a2736526
PB
439 cb, opaque, QEMU_AIO_WRITE);
440 }
223d4670
TS
441}
442
7c84b1b8 443static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
097310b5 444 BlockCompletionFunc *cb, void *opaque)
223d4670
TS
445{
446 BDRVRawState *s = bs->opaque;
fc4edb84 447 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
223d4670
TS
448}
449
450static void raw_close(BlockDriverState *bs)
451{
452 BDRVRawState *s = bs->opaque;
99cc5989
SH
453
454 if (s->aio) {
85ebd381 455 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
99cc5989
SH
456 win32_aio_cleanup(s->aio);
457 s->aio = NULL;
458 }
459
223d4670 460 CloseHandle(s->hfile);
8bfea15d
KW
461 if (bs->open_flags & BDRV_O_TEMPORARY) {
462 unlink(bs->filename);
463 }
223d4670
TS
464}
465
8243ccb7
HR
466static int raw_truncate(BlockDriverState *bs, int64_t offset,
467 PreallocMode prealloc, Error **errp)
223d4670
TS
468{
469 BDRVRawState *s = bs->opaque;
b9e82a59 470 LONG low, high;
fbcad04d 471 DWORD dwPtrLow;
223d4670 472
8243ccb7
HR
473 if (prealloc != PREALLOC_MODE_OFF) {
474 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 475 PreallocMode_str(prealloc));
8243ccb7
HR
476 return -ENOTSUP;
477 }
478
223d4670
TS
479 low = offset;
480 high = offset >> 32;
fbcad04d
FC
481
482 /*
483 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
484 * and GetLastError doesn't return NO_ERROR.
485 */
486 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
487 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
4bff28b8 488 error_setg_win32(errp, GetLastError(), "SetFilePointer error");
fbcad04d
FC
489 return -EIO;
490 }
491 if (SetEndOfFile(s->hfile) == 0) {
4bff28b8 492 error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
223d4670 493 return -EIO;
fbcad04d 494 }
223d4670
TS
495 return 0;
496}
497
498static int64_t raw_getlength(BlockDriverState *bs)
499{
500 BDRVRawState *s = bs->opaque;
501 LARGE_INTEGER l;
502 ULARGE_INTEGER available, total, total_free;
503 DISK_GEOMETRY_EX dg;
504 DWORD count;
505 BOOL status;
506
507 switch(s->type) {
508 case FTYPE_FILE:
b9e82a59 509 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
223d4670
TS
510 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
511 return -EIO;
512 break;
513 case FTYPE_CD:
514 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
515 return -EIO;
516 l.QuadPart = total.QuadPart;
517 break;
518 case FTYPE_HARDDISK:
519 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
520 NULL, 0, &dg, sizeof(dg), &count, NULL);
521 if (status != 0) {
522 l = dg.DiskSize;
523 }
524 break;
525 default:
526 return -EIO;
527 }
528 return l.QuadPart;
529}
530
4a1d5e1f
FZ
531static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
532{
533 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
534 DWORD * high);
535 get_compressed_t get_compressed;
536 struct _stati64 st;
537 const char *filename = bs->filename;
538 /* WinNT support GetCompressedFileSize to determine allocate size */
539 get_compressed =
540 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
541 "GetCompressedFileSizeA");
542 if (get_compressed) {
543 DWORD high, low;
544 low = get_compressed(filename, &high);
545 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
546 return (((int64_t) high) << 32) + low;
547 }
548 }
549
550 if (_stati64(filename, &st) < 0) {
551 return -1;
552 }
553 return st.st_size;
554}
555
3766ef57 556static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
223d4670 557{
3766ef57 558 BlockdevCreateOptionsFile *file_opts;
223d4670
TS
559 int fd;
560
3766ef57
KW
561 assert(options->driver == BLOCKDEV_DRIVER_FILE);
562 file_opts = &options->u.file;
d5546c5e 563
3766ef57
KW
564 if (file_opts->has_preallocation) {
565 error_setg(errp, "Preallocation is not supported on Windows");
566 return -EINVAL;
567 }
568 if (file_opts->has_nocow) {
569 error_setg(errp, "nocow is not supported on Windows");
570 return -EINVAL;
571 }
223d4670 572
3766ef57 573 fd = qemu_open(file_opts->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
6165f4d8 574 0644);
c6252b7c
HR
575 if (fd < 0) {
576 error_setg_errno(errp, errno, "Could not create file");
223d4670 577 return -EIO;
c6252b7c 578 }
223d4670 579 set_sparse(fd);
3766ef57 580 ftruncate(fd, file_opts->size);
2e1e79da 581 qemu_close(fd);
3766ef57 582
223d4670
TS
583 return 0;
584}
585
3766ef57
KW
586static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
587 Error **errp)
588{
589 BlockdevCreateOptions options;
590 int64_t total_size = 0;
591
592 strstart(filename, "file:", &filename);
593
594 /* Read out options */
595 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
596 BDRV_SECTOR_SIZE);
597
598 options = (BlockdevCreateOptions) {
599 .driver = BLOCKDEV_DRIVER_FILE,
600 .u.file = {
601 .filename = (char *) filename,
602 .size = total_size,
603 .has_preallocation = false,
604 .has_nocow = false,
605 },
606 };
607 return raw_co_create(&options, errp);
608}
ddef7699
CL
609
610static QemuOptsList raw_create_opts = {
611 .name = "raw-create-opts",
612 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
613 .desc = {
614 {
615 .name = BLOCK_OPT_SIZE,
616 .type = QEMU_OPT_SIZE,
617 .help = "Virtual disk size"
618 },
619 { /* end of list */ }
620 }
0e7e1989
KW
621};
622
5f535a94 623BlockDriver bdrv_file = {
84a12e66
CH
624 .format_name = "file",
625 .protocol_name = "file",
f1b2f712 626 .instance_size = sizeof(BDRVRawState),
030be321 627 .bdrv_needs_filename = true,
7dc74db8 628 .bdrv_parse_filename = raw_parse_filename,
ddef7699 629 .bdrv_file_open = raw_open,
2914a1de 630 .bdrv_refresh_limits = raw_probe_alignment,
ddef7699 631 .bdrv_close = raw_close,
efc75e2a 632 .bdrv_co_create_opts = raw_co_create_opts,
3ac21627 633 .bdrv_has_zero_init = bdrv_has_zero_init_1,
c68b89ac 634
fc4edb84
PB
635 .bdrv_aio_readv = raw_aio_readv,
636 .bdrv_aio_writev = raw_aio_writev,
637 .bdrv_aio_flush = raw_aio_flush,
c68b89ac 638
f1b2f712
AL
639 .bdrv_truncate = raw_truncate,
640 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
641 .bdrv_get_allocated_file_size
642 = raw_get_allocated_file_size,
0e7e1989 643
ddef7699 644 .create_opts = &raw_create_opts,
223d4670
TS
645};
646
647/***********************************************/
648/* host device */
649
650static int find_cdrom(char *cdrom_name, int cdrom_name_size)
651{
652 char drives[256], *pdrv = drives;
653 UINT type;
654
655 memset(drives, 0, sizeof(drives));
656 GetLogicalDriveStrings(sizeof(drives), drives);
657 while(pdrv[0] != '\0') {
658 type = GetDriveType(pdrv);
659 switch(type) {
660 case DRIVE_CDROM:
661 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
662 return 0;
663 break;
664 }
665 pdrv += lstrlen(pdrv) + 1;
666 }
667 return -1;
668}
669
670static int find_device_type(BlockDriverState *bs, const char *filename)
671{
672 BDRVRawState *s = bs->opaque;
673 UINT type;
674 const char *p;
675
676 if (strstart(filename, "\\\\.\\", &p) ||
677 strstart(filename, "//./", &p)) {
678 if (stristart(p, "PhysicalDrive", NULL))
679 return FTYPE_HARDDISK;
680 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
681 type = GetDriveType(s->drive_path);
3060cd14
AL
682 switch (type) {
683 case DRIVE_REMOVABLE:
684 case DRIVE_FIXED:
685 return FTYPE_HARDDISK;
686 case DRIVE_CDROM:
223d4670 687 return FTYPE_CD;
3060cd14 688 default:
223d4670 689 return FTYPE_FILE;
3060cd14 690 }
223d4670
TS
691 } else {
692 return FTYPE_FILE;
693 }
694}
695
508c7cb3
CH
696static int hdev_probe_device(const char *filename)
697{
698 if (strstart(filename, "/dev/cdrom", NULL))
699 return 100;
700 if (is_windows_drive(filename))
701 return 100;
702 return 0;
703}
704
57ed25b1
HR
705static void hdev_parse_filename(const char *filename, QDict *options,
706 Error **errp)
707{
03c320d8 708 bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
57ed25b1
HR
709}
710
015a1036
HR
711static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
712 Error **errp)
223d4670
TS
713{
714 BDRVRawState *s = bs->opaque;
715 int access_flags, create_flags;
68dc0364 716 int ret = 0;
223d4670
TS
717 DWORD overlapped;
718 char device_name[64];
68dc0364
SW
719
720 Error *local_err = NULL;
721 const char *filename;
0a4279d9 722 bool use_aio;
68dc0364 723
87ea75d5
PC
724 QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
725 &error_abort);
68dc0364 726 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 727 if (local_err) {
c6252b7c 728 error_propagate(errp, local_err);
68dc0364
SW
729 ret = -EINVAL;
730 goto done;
731 }
732
733 filename = qemu_opt_get(opts, "filename");
223d4670 734
0a4279d9
KW
735 use_aio = get_aio_option(opts, flags, &local_err);
736 if (!local_err && use_aio) {
737 error_setg(&local_err, "AIO is not supported on Windows host devices");
738 }
739 if (local_err) {
740 error_propagate(errp, local_err);
741 ret = -EINVAL;
742 goto done;
743 }
744
223d4670 745 if (strstart(filename, "/dev/cdrom", NULL)) {
68dc0364 746 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
c6252b7c 747 error_setg(errp, "Could not open CD-ROM drive");
68dc0364
SW
748 ret = -ENOENT;
749 goto done;
750 }
223d4670
TS
751 filename = device_name;
752 } else {
753 /* transform drive letters into device name */
754 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
755 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
756 filename[1] == ':' && filename[2] == '\0') {
757 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
758 filename = device_name;
759 }
760 }
761 s->type = find_device_type(bs, filename);
762
0a4279d9 763 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
6a8dc042 764
223d4670
TS
765 create_flags = OPEN_EXISTING;
766
223d4670
TS
767 s->hfile = CreateFile(filename, access_flags,
768 FILE_SHARE_READ, NULL,
769 create_flags, overlapped, NULL);
770 if (s->hfile == INVALID_HANDLE_VALUE) {
771 int err = GetLastError();
772
68dc0364
SW
773 if (err == ERROR_ACCESS_DENIED) {
774 ret = -EACCES;
775 } else {
45d57f6e 776 ret = -EINVAL;
68dc0364 777 }
45d57f6e 778 error_setg_errno(errp, -ret, "Could not open device");
68dc0364 779 goto done;
223d4670 780 }
68dc0364
SW
781
782done:
783 qemu_opts_del(opts);
784 return ret;
223d4670
TS
785}
786
5efa9d5a 787static BlockDriver bdrv_host_device = {
e60f469c 788 .format_name = "host_device",
84a12e66 789 .protocol_name = "host_device",
e60f469c 790 .instance_size = sizeof(BDRVRawState),
030be321 791 .bdrv_needs_filename = true,
57ed25b1 792 .bdrv_parse_filename = hdev_parse_filename,
508c7cb3 793 .bdrv_probe_device = hdev_probe_device,
66f82cee 794 .bdrv_file_open = hdev_open,
e60f469c 795 .bdrv_close = raw_close,
223d4670 796
fc4edb84
PB
797 .bdrv_aio_readv = raw_aio_readv,
798 .bdrv_aio_writev = raw_aio_writev,
799 .bdrv_aio_flush = raw_aio_flush,
c68b89ac 800
85ebd381
SH
801 .bdrv_detach_aio_context = raw_detach_aio_context,
802 .bdrv_attach_aio_context = raw_attach_aio_context,
803
b94a2610
KW
804 .bdrv_getlength = raw_getlength,
805 .has_variable_length = true,
806
4a1d5e1f
FZ
807 .bdrv_get_allocated_file_size
808 = raw_get_allocated_file_size,
223d4670 809};
5efa9d5a 810
84a12e66 811static void bdrv_file_init(void)
5efa9d5a 812{
84a12e66 813 bdrv_register(&bdrv_file);
5efa9d5a
AL
814 bdrv_register(&bdrv_host_device);
815}
816
84a12e66 817block_init(bdrv_file_init);