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
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu/cutils.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "block/raw-aio.h"
31 #include "block/thread-pool.h"
33 #include "qapi/qmp/qstring.h"
39 #define FTYPE_HARDDISK 2
41 typedef struct RawWin32AIOData
{
44 struct iovec
*aio_iov
;
51 typedef struct BDRVRawState
{
54 char drive_path
[16]; /* format: "d:\" */
55 QEMUWin32AIOState
*aio
;
59 * Read/writes the data to/from a given linear buffer.
61 * Returns the number of bytes handles or -errno in case of an error. Short
62 * reads are only returned if the end of the file is reached.
64 static size_t handle_aiocb_rw(RawWin32AIOData
*aiocb
)
69 for (i
= 0; i
< aiocb
->aio_niov
; i
++) {
71 DWORD ret
, ret_count
, len
;
73 memset(&ov
, 0, sizeof(ov
));
74 ov
.Offset
= (aiocb
->aio_offset
+ offset
);
75 ov
.OffsetHigh
= (aiocb
->aio_offset
+ offset
) >> 32;
76 len
= aiocb
->aio_iov
[i
].iov_len
;
77 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
78 ret
= WriteFile(aiocb
->hfile
, aiocb
->aio_iov
[i
].iov_base
,
79 len
, &ret_count
, &ov
);
81 ret
= ReadFile(aiocb
->hfile
, aiocb
->aio_iov
[i
].iov_base
,
82 len
, &ret_count
, &ov
);
87 if (ret_count
!= len
) {
97 static int aio_worker(void *arg
)
99 RawWin32AIOData
*aiocb
= arg
;
103 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
105 count
= handle_aiocb_rw(aiocb
);
106 if (count
< aiocb
->aio_nbytes
) {
107 /* A short read means that we have reached EOF. Pad the buffer
108 * with zeros for bytes after EOF. */
109 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, count
,
110 0, aiocb
->aio_nbytes
- count
);
112 count
= aiocb
->aio_nbytes
;
114 if (count
== aiocb
->aio_nbytes
) {
121 count
= handle_aiocb_rw(aiocb
);
122 if (count
== aiocb
->aio_nbytes
) {
129 if (!FlushFileBuffers(aiocb
->hfile
)) {
134 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
143 static BlockAIOCB
*paio_submit(BlockDriverState
*bs
, HANDLE hfile
,
144 int64_t offset
, QEMUIOVector
*qiov
, int count
,
145 BlockCompletionFunc
*cb
, void *opaque
, int type
)
147 RawWin32AIOData
*acb
= g_new(RawWin32AIOData
, 1);
152 acb
->aio_type
= type
;
155 acb
->aio_iov
= qiov
->iov
;
156 acb
->aio_niov
= qiov
->niov
;
157 assert(qiov
->size
== count
);
159 acb
->aio_nbytes
= count
;
160 acb
->aio_offset
= offset
;
162 trace_paio_submit(acb
, opaque
, offset
, count
, type
);
163 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
164 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
167 int qemu_ftruncate64(int fd
, int64_t length
)
175 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
178 h
= (HANDLE
)_get_osfhandle(fd
);
180 /* get current position, ftruncate do not change position */
182 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
183 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
188 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
189 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
192 res
= SetEndOfFile(h
);
194 /* back to old position */
195 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
199 static int set_sparse(int fd
)
202 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
203 NULL
, 0, NULL
, 0, &returned
, NULL
);
206 static void raw_detach_aio_context(BlockDriverState
*bs
)
208 BDRVRawState
*s
= bs
->opaque
;
211 win32_aio_detach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
215 static void raw_attach_aio_context(BlockDriverState
*bs
,
216 AioContext
*new_context
)
218 BDRVRawState
*s
= bs
->opaque
;
221 win32_aio_attach_aio_context(s
->aio
, new_context
);
225 static void raw_probe_alignment(BlockDriverState
*bs
, Error
**errp
)
227 BDRVRawState
*s
= bs
->opaque
;
228 DWORD sectorsPerCluster
, freeClusters
, totalClusters
, count
;
232 if (s
->type
== FTYPE_CD
) {
233 bs
->bl
.request_alignment
= 2048;
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
);
240 bs
->bl
.request_alignment
= dg
.Geometry
.BytesPerSector
;
243 /* try GetDiskFreeSpace too */
246 if (s
->drive_path
[0]) {
247 GetDiskFreeSpace(s
->drive_path
, §orsPerCluster
,
248 &dg
.Geometry
.BytesPerSector
,
249 &freeClusters
, &totalClusters
);
250 bs
->bl
.request_alignment
= dg
.Geometry
.BytesPerSector
;
254 static void raw_parse_flags(int flags
, bool use_aio
, int *access_flags
,
257 assert(access_flags
!= NULL
);
258 assert(overlapped
!= NULL
);
260 if (flags
& BDRV_O_RDWR
) {
261 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
263 *access_flags
= GENERIC_READ
;
266 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
268 *overlapped
|= FILE_FLAG_OVERLAPPED
;
270 if (flags
& BDRV_O_NOCACHE
) {
271 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
275 static void raw_parse_filename(const char *filename
, QDict
*options
,
278 bdrv_parse_filename_strip_prefix(filename
, "file:", options
);
281 static QemuOptsList raw_runtime_opts
= {
283 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
287 .type
= QEMU_OPT_STRING
,
288 .help
= "File name of the image",
292 .type
= QEMU_OPT_STRING
,
293 .help
= "host AIO implementation (threads, native)",
295 { /* end of list */ }
299 static bool get_aio_option(QemuOpts
*opts
, int flags
, Error
**errp
)
301 BlockdevAioOptions aio
, aio_default
;
303 aio_default
= (flags
& BDRV_O_NATIVE_AIO
) ? BLOCKDEV_AIO_OPTIONS_NATIVE
304 : BLOCKDEV_AIO_OPTIONS_THREADS
;
305 aio
= qapi_enum_parse(&BlockdevAioOptions_lookup
, qemu_opt_get(opts
, "aio"),
309 case BLOCKDEV_AIO_OPTIONS_NATIVE
:
311 case BLOCKDEV_AIO_OPTIONS_THREADS
:
314 error_setg(errp
, "Invalid AIO option");
319 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
322 BDRVRawState
*s
= bs
->opaque
;
326 Error
*local_err
= NULL
;
327 const char *filename
;
331 s
->type
= FTYPE_FILE
;
333 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
334 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
336 error_propagate(errp
, local_err
);
341 if (qdict_get_try_bool(options
, "locking", false)) {
342 error_setg(errp
, "locking=on is not supported on Windows");
347 filename
= qemu_opt_get(opts
, "filename");
349 use_aio
= get_aio_option(opts
, flags
, &local_err
);
351 error_propagate(errp
, local_err
);
356 raw_parse_flags(flags
, use_aio
, &access_flags
, &overlapped
);
358 if (filename
[0] && filename
[1] == ':') {
359 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", filename
[0]);
360 } else if (filename
[0] == '\\' && filename
[1] == '\\') {
361 s
->drive_path
[0] = 0;
365 GetCurrentDirectory(MAX_PATH
, buf
);
366 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", buf
[0]);
369 s
->hfile
= CreateFile(filename
, access_flags
,
370 FILE_SHARE_READ
, NULL
,
371 OPEN_EXISTING
, overlapped
, NULL
);
372 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
373 int err
= GetLastError();
375 error_setg_win32(errp
, err
, "Could not open '%s'", filename
);
376 if (err
== ERROR_ACCESS_DENIED
) {
385 s
->aio
= win32_aio_init();
386 if (s
->aio
== NULL
) {
387 CloseHandle(s
->hfile
);
388 error_setg(errp
, "Could not initialize AIO");
393 ret
= win32_aio_attach(s
->aio
, s
->hfile
);
395 win32_aio_cleanup(s
->aio
);
396 CloseHandle(s
->hfile
);
397 error_setg_errno(errp
, -ret
, "Could not enable AIO");
401 win32_aio_attach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
410 static BlockAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
411 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
412 BlockCompletionFunc
*cb
, void *opaque
)
414 BDRVRawState
*s
= bs
->opaque
;
416 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
417 nb_sectors
, cb
, opaque
, QEMU_AIO_READ
);
419 return paio_submit(bs
, s
->hfile
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
420 nb_sectors
<< BDRV_SECTOR_BITS
,
421 cb
, opaque
, QEMU_AIO_READ
);
425 static BlockAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
426 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
427 BlockCompletionFunc
*cb
, void *opaque
)
429 BDRVRawState
*s
= bs
->opaque
;
431 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
432 nb_sectors
, cb
, opaque
, QEMU_AIO_WRITE
);
434 return paio_submit(bs
, s
->hfile
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
435 nb_sectors
<< BDRV_SECTOR_BITS
,
436 cb
, opaque
, QEMU_AIO_WRITE
);
440 static BlockAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
441 BlockCompletionFunc
*cb
, void *opaque
)
443 BDRVRawState
*s
= bs
->opaque
;
444 return paio_submit(bs
, s
->hfile
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
447 static void raw_close(BlockDriverState
*bs
)
449 BDRVRawState
*s
= bs
->opaque
;
452 win32_aio_detach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
453 win32_aio_cleanup(s
->aio
);
457 CloseHandle(s
->hfile
);
458 if (bs
->open_flags
& BDRV_O_TEMPORARY
) {
459 unlink(bs
->filename
);
463 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
,
464 PreallocMode prealloc
, Error
**errp
)
466 BDRVRawState
*s
= bs
->opaque
;
470 if (prealloc
!= PREALLOC_MODE_OFF
) {
471 error_setg(errp
, "Unsupported preallocation mode '%s'",
472 PreallocMode_str(prealloc
));
480 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
481 * and GetLastError doesn't return NO_ERROR.
483 dwPtrLow
= SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
);
484 if (dwPtrLow
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
485 error_setg_win32(errp
, GetLastError(), "SetFilePointer error");
488 if (SetEndOfFile(s
->hfile
) == 0) {
489 error_setg_win32(errp
, GetLastError(), "SetEndOfFile error");
495 static int64_t raw_getlength(BlockDriverState
*bs
)
497 BDRVRawState
*s
= bs
->opaque
;
499 ULARGE_INTEGER available
, total
, total_free
;
506 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
507 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
511 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
513 l
.QuadPart
= total
.QuadPart
;
516 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
517 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
528 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
530 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
532 get_compressed_t get_compressed
;
534 const char *filename
= bs
->filename
;
535 /* WinNT support GetCompressedFileSize to determine allocate size */
537 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
538 "GetCompressedFileSizeA");
539 if (get_compressed
) {
541 low
= get_compressed(filename
, &high
);
542 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
543 return (((int64_t) high
) << 32) + low
;
547 if (_stati64(filename
, &st
) < 0) {
553 static int raw_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
556 int64_t total_size
= 0;
558 strstart(filename
, "file:", &filename
);
560 /* Read out options */
561 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
564 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
567 error_setg_errno(errp
, errno
, "Could not create file");
571 ftruncate(fd
, total_size
);
577 static QemuOptsList raw_create_opts
= {
578 .name
= "raw-create-opts",
579 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
582 .name
= BLOCK_OPT_SIZE
,
583 .type
= QEMU_OPT_SIZE
,
584 .help
= "Virtual disk size"
586 { /* end of list */ }
590 BlockDriver bdrv_file
= {
591 .format_name
= "file",
592 .protocol_name
= "file",
593 .instance_size
= sizeof(BDRVRawState
),
594 .bdrv_needs_filename
= true,
595 .bdrv_parse_filename
= raw_parse_filename
,
596 .bdrv_file_open
= raw_open
,
597 .bdrv_refresh_limits
= raw_probe_alignment
,
598 .bdrv_close
= raw_close
,
599 .bdrv_create
= raw_create
,
600 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
602 .bdrv_aio_readv
= raw_aio_readv
,
603 .bdrv_aio_writev
= raw_aio_writev
,
604 .bdrv_aio_flush
= raw_aio_flush
,
606 .bdrv_truncate
= raw_truncate
,
607 .bdrv_getlength
= raw_getlength
,
608 .bdrv_get_allocated_file_size
609 = raw_get_allocated_file_size
,
611 .create_opts
= &raw_create_opts
,
614 /***********************************************/
617 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
619 char drives
[256], *pdrv
= drives
;
622 memset(drives
, 0, sizeof(drives
));
623 GetLogicalDriveStrings(sizeof(drives
), drives
);
624 while(pdrv
[0] != '\0') {
625 type
= GetDriveType(pdrv
);
628 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
632 pdrv
+= lstrlen(pdrv
) + 1;
637 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
639 BDRVRawState
*s
= bs
->opaque
;
643 if (strstart(filename
, "\\\\.\\", &p
) ||
644 strstart(filename
, "//./", &p
)) {
645 if (stristart(p
, "PhysicalDrive", NULL
))
646 return FTYPE_HARDDISK
;
647 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
648 type
= GetDriveType(s
->drive_path
);
650 case DRIVE_REMOVABLE
:
652 return FTYPE_HARDDISK
;
663 static int hdev_probe_device(const char *filename
)
665 if (strstart(filename
, "/dev/cdrom", NULL
))
667 if (is_windows_drive(filename
))
672 static void hdev_parse_filename(const char *filename
, QDict
*options
,
675 bdrv_parse_filename_strip_prefix(filename
, "host_device:", options
);
678 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
681 BDRVRawState
*s
= bs
->opaque
;
682 int access_flags
, create_flags
;
685 char device_name
[64];
687 Error
*local_err
= NULL
;
688 const char *filename
;
691 QemuOpts
*opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0,
693 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
695 error_propagate(errp
, local_err
);
700 filename
= qemu_opt_get(opts
, "filename");
702 use_aio
= get_aio_option(opts
, flags
, &local_err
);
703 if (!local_err
&& use_aio
) {
704 error_setg(&local_err
, "AIO is not supported on Windows host devices");
707 error_propagate(errp
, local_err
);
712 if (strstart(filename
, "/dev/cdrom", NULL
)) {
713 if (find_cdrom(device_name
, sizeof(device_name
)) < 0) {
714 error_setg(errp
, "Could not open CD-ROM drive");
718 filename
= device_name
;
720 /* transform drive letters into device name */
721 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
722 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
723 filename
[1] == ':' && filename
[2] == '\0') {
724 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
725 filename
= device_name
;
728 s
->type
= find_device_type(bs
, filename
);
730 raw_parse_flags(flags
, use_aio
, &access_flags
, &overlapped
);
732 create_flags
= OPEN_EXISTING
;
734 s
->hfile
= CreateFile(filename
, access_flags
,
735 FILE_SHARE_READ
, NULL
,
736 create_flags
, overlapped
, NULL
);
737 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
738 int err
= GetLastError();
740 if (err
== ERROR_ACCESS_DENIED
) {
745 error_setg_errno(errp
, -ret
, "Could not open device");
754 static BlockDriver bdrv_host_device
= {
755 .format_name
= "host_device",
756 .protocol_name
= "host_device",
757 .instance_size
= sizeof(BDRVRawState
),
758 .bdrv_needs_filename
= true,
759 .bdrv_parse_filename
= hdev_parse_filename
,
760 .bdrv_probe_device
= hdev_probe_device
,
761 .bdrv_file_open
= hdev_open
,
762 .bdrv_close
= raw_close
,
764 .bdrv_aio_readv
= raw_aio_readv
,
765 .bdrv_aio_writev
= raw_aio_writev
,
766 .bdrv_aio_flush
= raw_aio_flush
,
768 .bdrv_detach_aio_context
= raw_detach_aio_context
,
769 .bdrv_attach_aio_context
= raw_attach_aio_context
,
771 .bdrv_getlength
= raw_getlength
,
772 .has_variable_length
= true,
774 .bdrv_get_allocated_file_size
775 = raw_get_allocated_file_size
,
778 static void bdrv_file_init(void)
780 bdrv_register(&bdrv_file
);
781 bdrv_register(&bdrv_host_device
);
784 block_init(bdrv_file_init
);