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