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