2 * Block driver for RAW files (win32)
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/cutils.h"
28 #include "block/block-io.h"
29 #include "block/block_int.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "block/raw-aio.h"
34 #include "block/thread-pool.h"
36 #include "qapi/qmp/qdict.h"
37 #include "qapi/qmp/qstring.h"
43 #define FTYPE_HARDDISK 2
45 typedef struct RawWin32AIOData
{
48 struct iovec
*aio_iov
;
55 typedef struct BDRVRawState
{
58 char drive_path
[16]; /* format: "d:\" */
59 QEMUWin32AIOState
*aio
;
62 typedef struct BDRVRawReopenState
{
67 * Read/writes the data to/from a given linear buffer.
69 * Returns the number of bytes handles or -errno in case of an error. Short
70 * reads are only returned if the end of the file is reached.
72 static size_t handle_aiocb_rw(RawWin32AIOData
*aiocb
)
77 for (i
= 0; i
< aiocb
->aio_niov
; i
++) {
79 DWORD ret
, ret_count
, len
;
81 memset(&ov
, 0, sizeof(ov
));
82 ov
.Offset
= (aiocb
->aio_offset
+ offset
);
83 ov
.OffsetHigh
= (aiocb
->aio_offset
+ offset
) >> 32;
84 len
= aiocb
->aio_iov
[i
].iov_len
;
85 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
86 ret
= WriteFile(aiocb
->hfile
, aiocb
->aio_iov
[i
].iov_base
,
87 len
, &ret_count
, &ov
);
89 ret
= ReadFile(aiocb
->hfile
, aiocb
->aio_iov
[i
].iov_base
,
90 len
, &ret_count
, &ov
);
95 if (ret_count
!= len
) {
105 static int aio_worker(void *arg
)
107 RawWin32AIOData
*aiocb
= arg
;
111 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
113 count
= handle_aiocb_rw(aiocb
);
114 if (count
< aiocb
->aio_nbytes
) {
115 /* A short read means that we have reached EOF. Pad the buffer
116 * with zeros for bytes after EOF. */
117 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, count
,
118 0, aiocb
->aio_nbytes
- count
);
120 count
= aiocb
->aio_nbytes
;
122 if (count
== aiocb
->aio_nbytes
) {
129 count
= handle_aiocb_rw(aiocb
);
130 if (count
== aiocb
->aio_nbytes
) {
137 if (!FlushFileBuffers(aiocb
->hfile
)) {
142 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
151 static BlockAIOCB
*paio_submit(BlockDriverState
*bs
, HANDLE hfile
,
152 int64_t offset
, QEMUIOVector
*qiov
, int count
,
153 BlockCompletionFunc
*cb
, void *opaque
, int type
)
155 RawWin32AIOData
*acb
= g_new(RawWin32AIOData
, 1);
160 acb
->aio_type
= type
;
163 acb
->aio_iov
= qiov
->iov
;
164 acb
->aio_niov
= qiov
->niov
;
165 assert(qiov
->size
== count
);
167 acb
->aio_nbytes
= count
;
168 acb
->aio_offset
= offset
;
170 trace_file_paio_submit(acb
, opaque
, offset
, count
, type
);
171 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
172 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
175 int qemu_ftruncate64(int fd
, int64_t length
)
183 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
186 h
= (HANDLE
)_get_osfhandle(fd
);
188 /* get current position, ftruncate do not change position */
190 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
191 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
196 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
197 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
200 res
= SetEndOfFile(h
);
202 /* back to old position */
203 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
207 static int set_sparse(int fd
)
210 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
211 NULL
, 0, NULL
, 0, &returned
, NULL
);
214 static void raw_detach_aio_context(BlockDriverState
*bs
)
216 BDRVRawState
*s
= bs
->opaque
;
219 win32_aio_detach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
223 static void raw_attach_aio_context(BlockDriverState
*bs
,
224 AioContext
*new_context
)
226 BDRVRawState
*s
= bs
->opaque
;
229 win32_aio_attach_aio_context(s
->aio
, new_context
);
233 static void raw_probe_alignment(BlockDriverState
*bs
, Error
**errp
)
235 BDRVRawState
*s
= bs
->opaque
;
236 DWORD sectorsPerCluster
, freeClusters
, totalClusters
, count
;
240 if (s
->type
== FTYPE_CD
) {
241 bs
->bl
.request_alignment
= 2048;
244 if (s
->type
== FTYPE_HARDDISK
) {
245 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
246 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
248 bs
->bl
.request_alignment
= dg
.Geometry
.BytesPerSector
;
251 /* try GetDiskFreeSpace too */
254 if (s
->drive_path
[0]) {
255 GetDiskFreeSpace(s
->drive_path
, §orsPerCluster
,
256 &dg
.Geometry
.BytesPerSector
,
257 &freeClusters
, &totalClusters
);
258 bs
->bl
.request_alignment
= dg
.Geometry
.BytesPerSector
;
262 /* XXX Does Windows support AIO on less than 512-byte alignment? */
263 bs
->bl
.request_alignment
= 512;
266 static void raw_parse_flags(int flags
, bool use_aio
, int *access_flags
,
269 assert(access_flags
!= NULL
);
270 assert(overlapped
!= NULL
);
272 if (flags
& BDRV_O_RDWR
) {
273 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
275 *access_flags
= GENERIC_READ
;
278 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
280 *overlapped
|= FILE_FLAG_OVERLAPPED
;
282 if (flags
& BDRV_O_NOCACHE
) {
283 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
287 static void raw_parse_filename(const char *filename
, QDict
*options
,
290 bdrv_parse_filename_strip_prefix(filename
, "file:", options
);
293 static QemuOptsList raw_runtime_opts
= {
295 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
299 .type
= QEMU_OPT_STRING
,
300 .help
= "File name of the image",
304 .type
= QEMU_OPT_STRING
,
305 .help
= "host AIO implementation (threads, native)",
309 .type
= QEMU_OPT_STRING
,
310 .help
= "file locking mode (on/off/auto, default: auto)",
312 { /* end of list */ }
316 static bool get_aio_option(QemuOpts
*opts
, int flags
, Error
**errp
)
318 BlockdevAioOptions aio
, aio_default
;
320 aio_default
= (flags
& BDRV_O_NATIVE_AIO
) ? BLOCKDEV_AIO_OPTIONS_NATIVE
321 : BLOCKDEV_AIO_OPTIONS_THREADS
;
322 aio
= qapi_enum_parse(&BlockdevAioOptions_lookup
, qemu_opt_get(opts
, "aio"),
326 case BLOCKDEV_AIO_OPTIONS_NATIVE
:
328 case BLOCKDEV_AIO_OPTIONS_THREADS
:
331 error_setg(errp
, "Invalid AIO option");
336 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
339 BDRVRawState
*s
= bs
->opaque
;
343 Error
*local_err
= NULL
;
344 const char *filename
;
349 s
->type
= FTYPE_FILE
;
351 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
352 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
357 locking
= qapi_enum_parse(&OnOffAuto_lookup
,
358 qemu_opt_get(opts
, "locking"),
359 ON_OFF_AUTO_AUTO
, &local_err
);
361 error_propagate(errp
, local_err
);
367 error_setg(errp
, "locking=on is not supported on Windows");
370 case ON_OFF_AUTO_OFF
:
371 case ON_OFF_AUTO_AUTO
:
374 g_assert_not_reached();
377 filename
= qemu_opt_get(opts
, "filename");
379 use_aio
= get_aio_option(opts
, flags
, &local_err
);
381 error_propagate(errp
, local_err
);
386 raw_parse_flags(flags
, use_aio
, &access_flags
, &overlapped
);
388 if (filename
[0] && filename
[1] == ':') {
389 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", filename
[0]);
390 } else if (filename
[0] == '\\' && filename
[1] == '\\') {
391 s
->drive_path
[0] = 0;
395 GetCurrentDirectory(MAX_PATH
, buf
);
396 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", buf
[0]);
399 s
->hfile
= CreateFile(filename
, access_flags
,
400 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
401 OPEN_EXISTING
, overlapped
, NULL
);
402 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
403 int err
= GetLastError();
405 error_setg_win32(errp
, err
, "Could not open '%s'", filename
);
406 if (err
== ERROR_ACCESS_DENIED
) {
415 s
->aio
= win32_aio_init();
416 if (s
->aio
== NULL
) {
417 CloseHandle(s
->hfile
);
418 error_setg(errp
, "Could not initialize AIO");
423 ret
= win32_aio_attach(s
->aio
, s
->hfile
);
425 win32_aio_cleanup(s
->aio
);
426 CloseHandle(s
->hfile
);
427 error_setg_errno(errp
, -ret
, "Could not enable AIO");
431 win32_aio_attach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
434 /* When extending regular files, we get zeros from the OS */
435 bs
->supported_truncate_flags
= BDRV_REQ_ZERO_WRITE
;
443 static BlockAIOCB
*raw_aio_preadv(BlockDriverState
*bs
,
444 int64_t offset
, int64_t bytes
,
445 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
446 BlockCompletionFunc
*cb
, void *opaque
)
448 BDRVRawState
*s
= bs
->opaque
;
450 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, offset
, bytes
, qiov
,
451 cb
, opaque
, QEMU_AIO_READ
);
453 return paio_submit(bs
, s
->hfile
, offset
, qiov
, bytes
,
454 cb
, opaque
, QEMU_AIO_READ
);
458 static BlockAIOCB
*raw_aio_pwritev(BlockDriverState
*bs
,
459 int64_t offset
, int64_t bytes
,
460 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
461 BlockCompletionFunc
*cb
, void *opaque
)
463 BDRVRawState
*s
= bs
->opaque
;
465 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, offset
, bytes
, qiov
,
466 cb
, opaque
, QEMU_AIO_WRITE
);
468 return paio_submit(bs
, s
->hfile
, offset
, qiov
, bytes
,
469 cb
, opaque
, QEMU_AIO_WRITE
);
473 static BlockAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
474 BlockCompletionFunc
*cb
, void *opaque
)
476 BDRVRawState
*s
= bs
->opaque
;
477 return paio_submit(bs
, s
->hfile
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
480 static void raw_close(BlockDriverState
*bs
)
482 BDRVRawState
*s
= bs
->opaque
;
485 win32_aio_detach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
486 win32_aio_cleanup(s
->aio
);
490 CloseHandle(s
->hfile
);
491 if (bs
->open_flags
& BDRV_O_TEMPORARY
) {
492 unlink(bs
->filename
);
496 static int coroutine_fn
raw_co_truncate(BlockDriverState
*bs
, int64_t offset
,
497 bool exact
, PreallocMode prealloc
,
498 BdrvRequestFlags flags
, Error
**errp
)
500 BDRVRawState
*s
= bs
->opaque
;
504 if (prealloc
!= PREALLOC_MODE_OFF
) {
505 error_setg(errp
, "Unsupported preallocation mode '%s'",
506 PreallocMode_str(prealloc
));
514 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
515 * and GetLastError doesn't return NO_ERROR.
517 dwPtrLow
= SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
);
518 if (dwPtrLow
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
519 error_setg_win32(errp
, GetLastError(), "SetFilePointer error");
522 if (SetEndOfFile(s
->hfile
) == 0) {
523 error_setg_win32(errp
, GetLastError(), "SetEndOfFile error");
529 static int64_t coroutine_fn
raw_co_getlength(BlockDriverState
*bs
)
531 BDRVRawState
*s
= bs
->opaque
;
533 ULARGE_INTEGER available
, total
, total_free
;
540 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
541 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
545 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
547 l
.QuadPart
= total
.QuadPart
;
550 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
551 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
562 static int64_t coroutine_fn
raw_co_get_allocated_file_size(BlockDriverState
*bs
)
564 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
566 get_compressed_t get_compressed
;
568 const char *filename
= bs
->filename
;
569 /* WinNT support GetCompressedFileSize to determine allocate size */
571 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
572 "GetCompressedFileSizeA");
573 if (get_compressed
) {
575 low
= get_compressed(filename
, &high
);
576 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
577 return (((int64_t) high
) << 32) + low
;
581 if (_stati64(filename
, &st
) < 0) {
587 static int raw_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
589 BlockdevCreateOptionsFile
*file_opts
;
592 assert(options
->driver
== BLOCKDEV_DRIVER_FILE
);
593 file_opts
= &options
->u
.file
;
595 if (file_opts
->has_preallocation
) {
596 error_setg(errp
, "Preallocation is not supported on Windows");
599 if (file_opts
->has_nocow
) {
600 error_setg(errp
, "nocow is not supported on Windows");
604 fd
= qemu_create(file_opts
->filename
, O_WRONLY
| O_TRUNC
| O_BINARY
,
610 ftruncate(fd
, file_opts
->size
);
616 static int coroutine_fn
raw_co_create_opts(BlockDriver
*drv
,
617 const char *filename
,
621 BlockdevCreateOptions options
;
622 int64_t total_size
= 0;
624 strstart(filename
, "file:", &filename
);
626 /* Read out options */
627 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
630 options
= (BlockdevCreateOptions
) {
631 .driver
= BLOCKDEV_DRIVER_FILE
,
633 .filename
= (char *) filename
,
635 .has_preallocation
= false,
639 return raw_co_create(&options
, errp
);
642 static int raw_reopen_prepare(BDRVReopenState
*state
,
643 BlockReopenQueue
*queue
, Error
**errp
)
645 BDRVRawState
*s
= state
->bs
->opaque
;
646 BDRVRawReopenState
*rs
;
651 if (s
->type
!= FTYPE_FILE
) {
652 error_setg(errp
, "Can only reopen files");
656 rs
= g_new0(BDRVRawReopenState
, 1);
659 * We do not support changing any options (only flags). By leaving
660 * all options in state->options, we tell the generic reopen code
661 * that we do not support changing any of them, so it will verify
662 * that their values did not change.
665 raw_parse_flags(state
->flags
, s
->aio
!= NULL
, &access_flags
, &overlapped
);
666 rs
->hfile
= CreateFile(state
->bs
->filename
, access_flags
,
667 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
668 OPEN_EXISTING
, overlapped
, NULL
);
670 if (rs
->hfile
== INVALID_HANDLE_VALUE
) {
671 int err
= GetLastError();
673 error_setg_win32(errp
, err
, "Could not reopen '%s'",
674 state
->bs
->filename
);
675 if (err
== ERROR_ACCESS_DENIED
) {
684 ret
= win32_aio_attach(s
->aio
, rs
->hfile
);
686 error_setg_errno(errp
, -ret
, "Could not enable AIO");
687 CloseHandle(rs
->hfile
);
698 state
->opaque
= NULL
;
703 static void raw_reopen_commit(BDRVReopenState
*state
)
705 BDRVRawState
*s
= state
->bs
->opaque
;
706 BDRVRawReopenState
*rs
= state
->opaque
;
710 CloseHandle(s
->hfile
);
711 s
->hfile
= rs
->hfile
;
714 state
->opaque
= NULL
;
717 static void raw_reopen_abort(BDRVReopenState
*state
)
719 BDRVRawReopenState
*rs
= state
->opaque
;
725 if (rs
->hfile
!= INVALID_HANDLE_VALUE
) {
726 CloseHandle(rs
->hfile
);
730 state
->opaque
= NULL
;
733 static QemuOptsList raw_create_opts
= {
734 .name
= "raw-create-opts",
735 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
738 .name
= BLOCK_OPT_SIZE
,
739 .type
= QEMU_OPT_SIZE
,
740 .help
= "Virtual disk size"
742 { /* end of list */ }
746 BlockDriver bdrv_file
= {
747 .format_name
= "file",
748 .protocol_name
= "file",
749 .instance_size
= sizeof(BDRVRawState
),
750 .bdrv_needs_filename
= true,
751 .bdrv_parse_filename
= raw_parse_filename
,
752 .bdrv_file_open
= raw_open
,
753 .bdrv_refresh_limits
= raw_probe_alignment
,
754 .bdrv_close
= raw_close
,
755 .bdrv_co_create_opts
= raw_co_create_opts
,
756 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
758 .bdrv_reopen_prepare
= raw_reopen_prepare
,
759 .bdrv_reopen_commit
= raw_reopen_commit
,
760 .bdrv_reopen_abort
= raw_reopen_abort
,
762 .bdrv_aio_preadv
= raw_aio_preadv
,
763 .bdrv_aio_pwritev
= raw_aio_pwritev
,
764 .bdrv_aio_flush
= raw_aio_flush
,
766 .bdrv_co_truncate
= raw_co_truncate
,
767 .bdrv_co_getlength
= raw_co_getlength
,
768 .bdrv_co_get_allocated_file_size
769 = raw_co_get_allocated_file_size
,
771 .create_opts
= &raw_create_opts
,
774 /***********************************************/
777 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
779 char drives
[256], *pdrv
= drives
;
782 memset(drives
, 0, sizeof(drives
));
783 GetLogicalDriveStrings(sizeof(drives
), drives
);
784 while(pdrv
[0] != '\0') {
785 type
= GetDriveType(pdrv
);
788 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
792 pdrv
+= lstrlen(pdrv
) + 1;
797 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
799 BDRVRawState
*s
= bs
->opaque
;
803 if (strstart(filename
, "\\\\.\\", &p
) ||
804 strstart(filename
, "//./", &p
)) {
805 if (stristart(p
, "PhysicalDrive", NULL
))
806 return FTYPE_HARDDISK
;
807 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
808 type
= GetDriveType(s
->drive_path
);
810 case DRIVE_REMOVABLE
:
812 return FTYPE_HARDDISK
;
823 static int hdev_probe_device(const char *filename
)
825 if (strstart(filename
, "/dev/cdrom", NULL
))
827 if (is_windows_drive(filename
))
832 static void hdev_parse_filename(const char *filename
, QDict
*options
,
835 bdrv_parse_filename_strip_prefix(filename
, "host_device:", options
);
838 static void hdev_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
840 /* XXX Does Windows support AIO on less than 512-byte alignment? */
841 bs
->bl
.request_alignment
= 512;
844 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
847 BDRVRawState
*s
= bs
->opaque
;
848 int access_flags
, create_flags
;
851 char device_name
[64];
853 Error
*local_err
= NULL
;
854 const char *filename
;
857 QemuOpts
*opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0,
859 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
864 filename
= qemu_opt_get(opts
, "filename");
866 use_aio
= get_aio_option(opts
, flags
, &local_err
);
867 if (!local_err
&& use_aio
) {
868 error_setg(&local_err
, "AIO is not supported on Windows host devices");
871 error_propagate(errp
, local_err
);
876 if (strstart(filename
, "/dev/cdrom", NULL
)) {
877 if (find_cdrom(device_name
, sizeof(device_name
)) < 0) {
878 error_setg(errp
, "Could not open CD-ROM drive");
882 filename
= device_name
;
884 /* transform drive letters into device name */
885 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
886 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
887 filename
[1] == ':' && filename
[2] == '\0') {
888 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
889 filename
= device_name
;
892 s
->type
= find_device_type(bs
, filename
);
894 raw_parse_flags(flags
, use_aio
, &access_flags
, &overlapped
);
896 create_flags
= OPEN_EXISTING
;
898 s
->hfile
= CreateFile(filename
, access_flags
,
899 FILE_SHARE_READ
, NULL
,
900 create_flags
, overlapped
, NULL
);
901 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
902 int err
= GetLastError();
904 if (err
== ERROR_ACCESS_DENIED
) {
909 error_setg_errno(errp
, -ret
, "Could not open device");
918 static BlockDriver bdrv_host_device
= {
919 .format_name
= "host_device",
920 .protocol_name
= "host_device",
921 .instance_size
= sizeof(BDRVRawState
),
922 .bdrv_needs_filename
= true,
923 .bdrv_parse_filename
= hdev_parse_filename
,
924 .bdrv_probe_device
= hdev_probe_device
,
925 .bdrv_file_open
= hdev_open
,
926 .bdrv_close
= raw_close
,
927 .bdrv_refresh_limits
= hdev_refresh_limits
,
929 .bdrv_aio_preadv
= raw_aio_preadv
,
930 .bdrv_aio_pwritev
= raw_aio_pwritev
,
931 .bdrv_aio_flush
= raw_aio_flush
,
933 .bdrv_detach_aio_context
= raw_detach_aio_context
,
934 .bdrv_attach_aio_context
= raw_attach_aio_context
,
936 .bdrv_co_getlength
= raw_co_getlength
,
937 .has_variable_length
= true,
938 .bdrv_co_get_allocated_file_size
= raw_co_get_allocated_file_size
,
941 static void bdrv_file_init(void)
943 bdrv_register(&bdrv_file
);
944 bdrv_register(&bdrv_host_device
);
947 block_init(bdrv_file_init
);