]> git.proxmox.com Git - mirror_qemu.git/blame - block/file-win32.c
error: Eliminate error_propagate() with Coccinelle, part 1
[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
f8a30874 165 trace_file_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)
7d37435b 179 return -1;
223d4670
TS
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) {
7d37435b 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) {
7d37435b 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,
7d37435b 206 NULL, 0, NULL, 0, &returned, NULL);
223d4670
TS
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;
de7056a3 254 return;
c25f53b0 255 }
de7056a3
EB
256
257 /* XXX Does Windows support AIO on less than 512-byte alignment? */
258 bs->bl.request_alignment = 512;
c25f53b0
PB
259}
260
0a4279d9
KW
261static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
262 DWORD *overlapped)
6a8dc042
JC
263{
264 assert(access_flags != NULL);
265 assert(overlapped != NULL);
266
267 if (flags & BDRV_O_RDWR) {
268 *access_flags = GENERIC_READ | GENERIC_WRITE;
269 } else {
270 *access_flags = GENERIC_READ;
271 }
272
273 *overlapped = FILE_ATTRIBUTE_NORMAL;
0a4279d9 274 if (use_aio) {
a2736526
PB
275 *overlapped |= FILE_FLAG_OVERLAPPED;
276 }
6a8dc042
JC
277 if (flags & BDRV_O_NOCACHE) {
278 *overlapped |= FILE_FLAG_NO_BUFFERING;
279 }
6a8dc042
JC
280}
281
7dc74db8
HR
282static void raw_parse_filename(const char *filename, QDict *options,
283 Error **errp)
284{
03c320d8 285 bdrv_parse_filename_strip_prefix(filename, "file:", options);
7dc74db8
HR
286}
287
8a79380b
KW
288static QemuOptsList raw_runtime_opts = {
289 .name = "raw",
290 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
291 .desc = {
292 {
293 .name = "filename",
294 .type = QEMU_OPT_STRING,
295 .help = "File name of the image",
296 },
0a4279d9
KW
297 {
298 .name = "aio",
299 .type = QEMU_OPT_STRING,
300 .help = "host AIO implementation (threads, native)",
301 },
8a79380b
KW
302 { /* end of list */ }
303 },
304};
305
0a4279d9
KW
306static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
307{
308 BlockdevAioOptions aio, aio_default;
309
310 aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
311 : BLOCKDEV_AIO_OPTIONS_THREADS;
f7abe0ec 312 aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
06c60b6c 313 aio_default, errp);
0a4279d9
KW
314
315 switch (aio) {
316 case BLOCKDEV_AIO_OPTIONS_NATIVE:
317 return true;
318 case BLOCKDEV_AIO_OPTIONS_THREADS:
319 return false;
320 default:
321 error_setg(errp, "Invalid AIO option");
322 }
323 return false;
324}
325
015a1036
HR
326static int raw_open(BlockDriverState *bs, QDict *options, int flags,
327 Error **errp)
223d4670
TS
328{
329 BDRVRawState *s = bs->opaque;
9a2d77ad 330 int access_flags;
223d4670 331 DWORD overlapped;
8a79380b
KW
332 QemuOpts *opts;
333 Error *local_err = NULL;
334 const char *filename;
0a4279d9 335 bool use_aio;
8a79380b 336 int ret;
223d4670
TS
337
338 s->type = FTYPE_FILE;
339
87ea75d5 340 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
235e59cf 341 if (!qemu_opts_absorb_qdict(opts, options, &local_err)) {
c6252b7c 342 error_propagate(errp, local_err);
8a79380b
KW
343 ret = -EINVAL;
344 goto fail;
345 }
346
1c3a555c
FZ
347 if (qdict_get_try_bool(options, "locking", false)) {
348 error_setg(errp, "locking=on is not supported on Windows");
cdece046 349 ret = -EINVAL;
1c3a555c
FZ
350 goto fail;
351 }
352
8a79380b
KW
353 filename = qemu_opt_get(opts, "filename");
354
0a4279d9
KW
355 use_aio = get_aio_option(opts, flags, &local_err);
356 if (local_err) {
357 error_propagate(errp, local_err);
358 ret = -EINVAL;
359 goto fail;
360 }
361
362 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
8a79380b 363
c25f53b0
PB
364 if (filename[0] && filename[1] == ':') {
365 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
366 } else if (filename[0] == '\\' && filename[1] == '\\') {
367 s->drive_path[0] = 0;
368 } else {
369 /* Relative path. */
370 char buf[MAX_PATH];
371 GetCurrentDirectory(MAX_PATH, buf);
372 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
373 }
374
223d4670
TS
375 s->hfile = CreateFile(filename, access_flags,
376 FILE_SHARE_READ, NULL,
9a2d77ad 377 OPEN_EXISTING, overlapped, NULL);
223d4670
TS
378 if (s->hfile == INVALID_HANDLE_VALUE) {
379 int err = GetLastError();
380
09237757 381 error_setg_win32(errp, err, "Could not open '%s'", filename);
8a79380b
KW
382 if (err == ERROR_ACCESS_DENIED) {
383 ret = -EACCES;
384 } else {
385 ret = -EINVAL;
386 }
387 goto fail;
a2736526
PB
388 }
389
0a4279d9 390 if (use_aio) {
99cc5989
SH
391 s->aio = win32_aio_init();
392 if (s->aio == NULL) {
393 CloseHandle(s->hfile);
394 error_setg(errp, "Could not initialize AIO");
395 ret = -EINVAL;
396 goto fail;
397 }
398
399 ret = win32_aio_attach(s->aio, s->hfile);
a2736526 400 if (ret < 0) {
99cc5989 401 win32_aio_cleanup(s->aio);
a2736526 402 CloseHandle(s->hfile);
c6252b7c 403 error_setg_errno(errp, -ret, "Could not enable AIO");
8a79380b 404 goto fail;
a2736526 405 }
85ebd381
SH
406
407 win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
223d4670 408 }
8a79380b 409
8e519795
EB
410 /* When extending regular files, we get zeros from the OS */
411 bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
412
8a79380b
KW
413 ret = 0;
414fail:
415 qemu_opts_del(opts);
416 return ret;
223d4670
TS
417}
418
de7056a3
EB
419static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
420 uint64_t offset, uint64_t bytes,
421 QEMUIOVector *qiov, int flags,
422 BlockCompletionFunc *cb, void *opaque)
223d4670
TS
423{
424 BDRVRawState *s = bs->opaque;
a2736526 425 if (s->aio) {
de7056a3
EB
426 return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
427 cb, opaque, QEMU_AIO_READ);
a2736526 428 } else {
de7056a3 429 return paio_submit(bs, s->hfile, offset, qiov, bytes,
a2736526
PB
430 cb, opaque, QEMU_AIO_READ);
431 }
223d4670
TS
432}
433
de7056a3
EB
434static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
435 uint64_t offset, uint64_t bytes,
436 QEMUIOVector *qiov, int flags,
437 BlockCompletionFunc *cb, void *opaque)
223d4670
TS
438{
439 BDRVRawState *s = bs->opaque;
a2736526 440 if (s->aio) {
de7056a3
EB
441 return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
442 cb, opaque, QEMU_AIO_WRITE);
a2736526 443 } else {
de7056a3 444 return paio_submit(bs, s->hfile, offset, qiov, bytes,
a2736526
PB
445 cb, opaque, QEMU_AIO_WRITE);
446 }
223d4670
TS
447}
448
7c84b1b8 449static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
097310b5 450 BlockCompletionFunc *cb, void *opaque)
223d4670
TS
451{
452 BDRVRawState *s = bs->opaque;
fc4edb84 453 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
223d4670
TS
454}
455
456static void raw_close(BlockDriverState *bs)
457{
458 BDRVRawState *s = bs->opaque;
99cc5989
SH
459
460 if (s->aio) {
85ebd381 461 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
99cc5989
SH
462 win32_aio_cleanup(s->aio);
463 s->aio = NULL;
464 }
465
223d4670 466 CloseHandle(s->hfile);
8bfea15d
KW
467 if (bs->open_flags & BDRV_O_TEMPORARY) {
468 unlink(bs->filename);
469 }
223d4670
TS
470}
471
061ca8a3 472static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
c80d8b06 473 bool exact, PreallocMode prealloc,
92b92799 474 BdrvRequestFlags flags, Error **errp)
223d4670
TS
475{
476 BDRVRawState *s = bs->opaque;
b9e82a59 477 LONG low, high;
fbcad04d 478 DWORD dwPtrLow;
223d4670 479
8243ccb7
HR
480 if (prealloc != PREALLOC_MODE_OFF) {
481 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 482 PreallocMode_str(prealloc));
8243ccb7
HR
483 return -ENOTSUP;
484 }
485
223d4670
TS
486 low = offset;
487 high = offset >> 32;
fbcad04d
FC
488
489 /*
490 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
491 * and GetLastError doesn't return NO_ERROR.
492 */
493 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
494 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
4bff28b8 495 error_setg_win32(errp, GetLastError(), "SetFilePointer error");
fbcad04d
FC
496 return -EIO;
497 }
498 if (SetEndOfFile(s->hfile) == 0) {
4bff28b8 499 error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
223d4670 500 return -EIO;
fbcad04d 501 }
223d4670
TS
502 return 0;
503}
504
505static int64_t raw_getlength(BlockDriverState *bs)
506{
507 BDRVRawState *s = bs->opaque;
508 LARGE_INTEGER l;
509 ULARGE_INTEGER available, total, total_free;
510 DISK_GEOMETRY_EX dg;
511 DWORD count;
512 BOOL status;
513
514 switch(s->type) {
515 case FTYPE_FILE:
b9e82a59 516 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
223d4670
TS
517 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
518 return -EIO;
519 break;
520 case FTYPE_CD:
521 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
522 return -EIO;
523 l.QuadPart = total.QuadPart;
524 break;
525 case FTYPE_HARDDISK:
526 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
527 NULL, 0, &dg, sizeof(dg), &count, NULL);
528 if (status != 0) {
529 l = dg.DiskSize;
530 }
531 break;
532 default:
533 return -EIO;
534 }
535 return l.QuadPart;
536}
537
4a1d5e1f
FZ
538static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
539{
540 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
541 DWORD * high);
542 get_compressed_t get_compressed;
543 struct _stati64 st;
544 const char *filename = bs->filename;
545 /* WinNT support GetCompressedFileSize to determine allocate size */
546 get_compressed =
547 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
548 "GetCompressedFileSizeA");
549 if (get_compressed) {
550 DWORD high, low;
551 low = get_compressed(filename, &high);
552 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
553 return (((int64_t) high) << 32) + low;
554 }
555 }
556
557 if (_stati64(filename, &st) < 0) {
558 return -1;
559 }
560 return st.st_size;
561}
562
3766ef57 563static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
223d4670 564{
3766ef57 565 BlockdevCreateOptionsFile *file_opts;
223d4670
TS
566 int fd;
567
3766ef57
KW
568 assert(options->driver == BLOCKDEV_DRIVER_FILE);
569 file_opts = &options->u.file;
d5546c5e 570
3766ef57
KW
571 if (file_opts->has_preallocation) {
572 error_setg(errp, "Preallocation is not supported on Windows");
573 return -EINVAL;
574 }
575 if (file_opts->has_nocow) {
576 error_setg(errp, "nocow is not supported on Windows");
577 return -EINVAL;
578 }
223d4670 579
3766ef57 580 fd = qemu_open(file_opts->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
6165f4d8 581 0644);
c6252b7c
HR
582 if (fd < 0) {
583 error_setg_errno(errp, errno, "Could not create file");
223d4670 584 return -EIO;
c6252b7c 585 }
223d4670 586 set_sparse(fd);
3766ef57 587 ftruncate(fd, file_opts->size);
2e1e79da 588 qemu_close(fd);
3766ef57 589
223d4670
TS
590 return 0;
591}
592
b92902df
ML
593static int coroutine_fn raw_co_create_opts(BlockDriver *drv,
594 const char *filename,
595 QemuOpts *opts,
3766ef57
KW
596 Error **errp)
597{
598 BlockdevCreateOptions options;
599 int64_t total_size = 0;
600
601 strstart(filename, "file:", &filename);
602
603 /* Read out options */
604 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
605 BDRV_SECTOR_SIZE);
606
607 options = (BlockdevCreateOptions) {
608 .driver = BLOCKDEV_DRIVER_FILE,
609 .u.file = {
610 .filename = (char *) filename,
611 .size = total_size,
612 .has_preallocation = false,
613 .has_nocow = false,
614 },
615 };
616 return raw_co_create(&options, errp);
617}
ddef7699
CL
618
619static QemuOptsList raw_create_opts = {
620 .name = "raw-create-opts",
621 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
622 .desc = {
623 {
624 .name = BLOCK_OPT_SIZE,
625 .type = QEMU_OPT_SIZE,
626 .help = "Virtual disk size"
627 },
628 { /* end of list */ }
629 }
0e7e1989
KW
630};
631
5f535a94 632BlockDriver bdrv_file = {
84a12e66
CH
633 .format_name = "file",
634 .protocol_name = "file",
f1b2f712 635 .instance_size = sizeof(BDRVRawState),
030be321 636 .bdrv_needs_filename = true,
7dc74db8 637 .bdrv_parse_filename = raw_parse_filename,
ddef7699 638 .bdrv_file_open = raw_open,
2914a1de 639 .bdrv_refresh_limits = raw_probe_alignment,
ddef7699 640 .bdrv_close = raw_close,
efc75e2a 641 .bdrv_co_create_opts = raw_co_create_opts,
3ac21627 642 .bdrv_has_zero_init = bdrv_has_zero_init_1,
c68b89ac 643
de7056a3
EB
644 .bdrv_aio_preadv = raw_aio_preadv,
645 .bdrv_aio_pwritev = raw_aio_pwritev,
fc4edb84 646 .bdrv_aio_flush = raw_aio_flush,
c68b89ac 647
061ca8a3 648 .bdrv_co_truncate = raw_co_truncate,
f1b2f712 649 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
650 .bdrv_get_allocated_file_size
651 = raw_get_allocated_file_size,
0e7e1989 652
ddef7699 653 .create_opts = &raw_create_opts,
223d4670
TS
654};
655
656/***********************************************/
657/* host device */
658
659static int find_cdrom(char *cdrom_name, int cdrom_name_size)
660{
661 char drives[256], *pdrv = drives;
662 UINT type;
663
664 memset(drives, 0, sizeof(drives));
665 GetLogicalDriveStrings(sizeof(drives), drives);
666 while(pdrv[0] != '\0') {
667 type = GetDriveType(pdrv);
668 switch(type) {
669 case DRIVE_CDROM:
670 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
671 return 0;
672 break;
673 }
674 pdrv += lstrlen(pdrv) + 1;
675 }
676 return -1;
677}
678
679static int find_device_type(BlockDriverState *bs, const char *filename)
680{
681 BDRVRawState *s = bs->opaque;
682 UINT type;
683 const char *p;
684
685 if (strstart(filename, "\\\\.\\", &p) ||
686 strstart(filename, "//./", &p)) {
687 if (stristart(p, "PhysicalDrive", NULL))
688 return FTYPE_HARDDISK;
689 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
690 type = GetDriveType(s->drive_path);
3060cd14
AL
691 switch (type) {
692 case DRIVE_REMOVABLE:
693 case DRIVE_FIXED:
694 return FTYPE_HARDDISK;
695 case DRIVE_CDROM:
223d4670 696 return FTYPE_CD;
3060cd14 697 default:
223d4670 698 return FTYPE_FILE;
3060cd14 699 }
223d4670
TS
700 } else {
701 return FTYPE_FILE;
702 }
703}
704
508c7cb3
CH
705static int hdev_probe_device(const char *filename)
706{
707 if (strstart(filename, "/dev/cdrom", NULL))
708 return 100;
709 if (is_windows_drive(filename))
710 return 100;
711 return 0;
712}
713
57ed25b1
HR
714static void hdev_parse_filename(const char *filename, QDict *options,
715 Error **errp)
716{
03c320d8 717 bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
57ed25b1
HR
718}
719
de7056a3
EB
720static void hdev_refresh_limits(BlockDriverState *bs, Error **errp)
721{
722 /* XXX Does Windows support AIO on less than 512-byte alignment? */
723 bs->bl.request_alignment = 512;
724}
725
015a1036
HR
726static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
727 Error **errp)
223d4670
TS
728{
729 BDRVRawState *s = bs->opaque;
730 int access_flags, create_flags;
68dc0364 731 int ret = 0;
223d4670
TS
732 DWORD overlapped;
733 char device_name[64];
68dc0364
SW
734
735 Error *local_err = NULL;
736 const char *filename;
0a4279d9 737 bool use_aio;
68dc0364 738
87ea75d5
PC
739 QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
740 &error_abort);
235e59cf 741 if (!qemu_opts_absorb_qdict(opts, options, &local_err)) {
c6252b7c 742 error_propagate(errp, local_err);
68dc0364
SW
743 ret = -EINVAL;
744 goto done;
745 }
746
747 filename = qemu_opt_get(opts, "filename");
223d4670 748
0a4279d9
KW
749 use_aio = get_aio_option(opts, flags, &local_err);
750 if (!local_err && use_aio) {
751 error_setg(&local_err, "AIO is not supported on Windows host devices");
752 }
753 if (local_err) {
754 error_propagate(errp, local_err);
755 ret = -EINVAL;
756 goto done;
757 }
758
223d4670 759 if (strstart(filename, "/dev/cdrom", NULL)) {
68dc0364 760 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
c6252b7c 761 error_setg(errp, "Could not open CD-ROM drive");
68dc0364
SW
762 ret = -ENOENT;
763 goto done;
764 }
223d4670
TS
765 filename = device_name;
766 } else {
767 /* transform drive letters into device name */
768 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
769 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
770 filename[1] == ':' && filename[2] == '\0') {
771 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
772 filename = device_name;
773 }
774 }
775 s->type = find_device_type(bs, filename);
776
0a4279d9 777 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
6a8dc042 778
223d4670
TS
779 create_flags = OPEN_EXISTING;
780
223d4670
TS
781 s->hfile = CreateFile(filename, access_flags,
782 FILE_SHARE_READ, NULL,
783 create_flags, overlapped, NULL);
784 if (s->hfile == INVALID_HANDLE_VALUE) {
785 int err = GetLastError();
786
68dc0364
SW
787 if (err == ERROR_ACCESS_DENIED) {
788 ret = -EACCES;
789 } else {
45d57f6e 790 ret = -EINVAL;
68dc0364 791 }
45d57f6e 792 error_setg_errno(errp, -ret, "Could not open device");
68dc0364 793 goto done;
223d4670 794 }
68dc0364
SW
795
796done:
797 qemu_opts_del(opts);
798 return ret;
223d4670
TS
799}
800
5efa9d5a 801static BlockDriver bdrv_host_device = {
e60f469c 802 .format_name = "host_device",
84a12e66 803 .protocol_name = "host_device",
e60f469c 804 .instance_size = sizeof(BDRVRawState),
030be321 805 .bdrv_needs_filename = true,
57ed25b1 806 .bdrv_parse_filename = hdev_parse_filename,
508c7cb3 807 .bdrv_probe_device = hdev_probe_device,
66f82cee 808 .bdrv_file_open = hdev_open,
e60f469c 809 .bdrv_close = raw_close,
de7056a3 810 .bdrv_refresh_limits = hdev_refresh_limits,
223d4670 811
de7056a3
EB
812 .bdrv_aio_preadv = raw_aio_preadv,
813 .bdrv_aio_pwritev = raw_aio_pwritev,
fc4edb84 814 .bdrv_aio_flush = raw_aio_flush,
c68b89ac 815
85ebd381
SH
816 .bdrv_detach_aio_context = raw_detach_aio_context,
817 .bdrv_attach_aio_context = raw_attach_aio_context,
818
b94a2610
KW
819 .bdrv_getlength = raw_getlength,
820 .has_variable_length = true,
821
4a1d5e1f
FZ
822 .bdrv_get_allocated_file_size
823 = raw_get_allocated_file_size,
223d4670 824};
5efa9d5a 825
84a12e66 826static void bdrv_file_init(void)
5efa9d5a 827{
84a12e66 828 bdrv_register(&bdrv_file);
5efa9d5a
AL
829 bdrv_register(&bdrv_host_device);
830}
831
84a12e66 832block_init(bdrv_file_init);