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_int.h"
29 #include "qemu/module.h"
30 #include "qemu/option.h"
31 #include "block/raw-aio.h"
33 #include "block/thread-pool.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
42 #define FTYPE_HARDDISK 2
44 typedef struct RawWin32AIOData
{
47 struct iovec
*aio_iov
;
54 typedef struct BDRVRawState
{
57 char drive_path
[16]; /* format: "d:\" */
58 QEMUWin32AIOState
*aio
;
62 * Read/writes the data to/from a given linear buffer.
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.
67 static size_t handle_aiocb_rw(RawWin32AIOData
*aiocb
)
72 for (i
= 0; i
< aiocb
->aio_niov
; i
++) {
74 DWORD ret
, ret_count
, len
;
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
);
84 ret
= ReadFile(aiocb
->hfile
, aiocb
->aio_iov
[i
].iov_base
,
85 len
, &ret_count
, &ov
);
90 if (ret_count
!= len
) {
100 static int aio_worker(void *arg
)
102 RawWin32AIOData
*aiocb
= arg
;
106 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
108 count
= handle_aiocb_rw(aiocb
);
109 if (count
< aiocb
->aio_nbytes
) {
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
);
115 count
= aiocb
->aio_nbytes
;
117 if (count
== aiocb
->aio_nbytes
) {
124 count
= handle_aiocb_rw(aiocb
);
125 if (count
== aiocb
->aio_nbytes
) {
132 if (!FlushFileBuffers(aiocb
->hfile
)) {
137 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
146 static BlockAIOCB
*paio_submit(BlockDriverState
*bs
, HANDLE hfile
,
147 int64_t offset
, QEMUIOVector
*qiov
, int count
,
148 BlockCompletionFunc
*cb
, void *opaque
, int type
)
150 RawWin32AIOData
*acb
= g_new(RawWin32AIOData
, 1);
155 acb
->aio_type
= type
;
158 acb
->aio_iov
= qiov
->iov
;
159 acb
->aio_niov
= qiov
->niov
;
160 assert(qiov
->size
== count
);
162 acb
->aio_nbytes
= count
;
163 acb
->aio_offset
= offset
;
165 trace_file_paio_submit(acb
, opaque
, offset
, count
, type
);
166 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
167 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
170 int qemu_ftruncate64(int fd
, int64_t length
)
178 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
181 h
= (HANDLE
)_get_osfhandle(fd
);
183 /* get current position, ftruncate do not change position */
185 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
186 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
191 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
192 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
195 res
= SetEndOfFile(h
);
197 /* back to old position */
198 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
202 static int set_sparse(int fd
)
205 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
206 NULL
, 0, NULL
, 0, &returned
, NULL
);
209 static void raw_detach_aio_context(BlockDriverState
*bs
)
211 BDRVRawState
*s
= bs
->opaque
;
214 win32_aio_detach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
218 static void raw_attach_aio_context(BlockDriverState
*bs
,
219 AioContext
*new_context
)
221 BDRVRawState
*s
= bs
->opaque
;
224 win32_aio_attach_aio_context(s
->aio
, new_context
);
228 static void raw_probe_alignment(BlockDriverState
*bs
, Error
**errp
)
230 BDRVRawState
*s
= bs
->opaque
;
231 DWORD sectorsPerCluster
, freeClusters
, totalClusters
, count
;
235 if (s
->type
== FTYPE_CD
) {
236 bs
->bl
.request_alignment
= 2048;
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
);
243 bs
->bl
.request_alignment
= dg
.Geometry
.BytesPerSector
;
246 /* try GetDiskFreeSpace too */
249 if (s
->drive_path
[0]) {
250 GetDiskFreeSpace(s
->drive_path
, §orsPerCluster
,
251 &dg
.Geometry
.BytesPerSector
,
252 &freeClusters
, &totalClusters
);
253 bs
->bl
.request_alignment
= dg
.Geometry
.BytesPerSector
;
257 /* XXX Does Windows support AIO on less than 512-byte alignment? */
258 bs
->bl
.request_alignment
= 512;
261 static void raw_parse_flags(int flags
, bool use_aio
, int *access_flags
,
264 assert(access_flags
!= NULL
);
265 assert(overlapped
!= NULL
);
267 if (flags
& BDRV_O_RDWR
) {
268 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
270 *access_flags
= GENERIC_READ
;
273 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
275 *overlapped
|= FILE_FLAG_OVERLAPPED
;
277 if (flags
& BDRV_O_NOCACHE
) {
278 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
282 static void raw_parse_filename(const char *filename
, QDict
*options
,
285 bdrv_parse_filename_strip_prefix(filename
, "file:", options
);
288 static QemuOptsList raw_runtime_opts
= {
290 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
294 .type
= QEMU_OPT_STRING
,
295 .help
= "File name of the image",
299 .type
= QEMU_OPT_STRING
,
300 .help
= "host AIO implementation (threads, native)",
304 .type
= QEMU_OPT_STRING
,
305 .help
= "file locking mode (on/off/auto, default: auto)",
307 { /* end of list */ }
311 static bool get_aio_option(QemuOpts
*opts
, int flags
, Error
**errp
)
313 BlockdevAioOptions aio
, aio_default
;
315 aio_default
= (flags
& BDRV_O_NATIVE_AIO
) ? BLOCKDEV_AIO_OPTIONS_NATIVE
316 : BLOCKDEV_AIO_OPTIONS_THREADS
;
317 aio
= qapi_enum_parse(&BlockdevAioOptions_lookup
, qemu_opt_get(opts
, "aio"),
321 case BLOCKDEV_AIO_OPTIONS_NATIVE
:
323 case BLOCKDEV_AIO_OPTIONS_THREADS
:
326 error_setg(errp
, "Invalid AIO option");
331 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
334 BDRVRawState
*s
= bs
->opaque
;
338 Error
*local_err
= NULL
;
339 const char *filename
;
344 s
->type
= FTYPE_FILE
;
346 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
347 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
352 locking
= qapi_enum_parse(&OnOffAuto_lookup
,
353 qemu_opt_get(opts
, "locking"),
354 ON_OFF_AUTO_AUTO
, &local_err
);
356 error_propagate(errp
, local_err
);
362 error_setg(errp
, "locking=on is not supported on Windows");
365 case ON_OFF_AUTO_OFF
:
366 case ON_OFF_AUTO_AUTO
:
369 g_assert_not_reached();
372 filename
= qemu_opt_get(opts
, "filename");
374 use_aio
= get_aio_option(opts
, flags
, &local_err
);
376 error_propagate(errp
, local_err
);
381 raw_parse_flags(flags
, use_aio
, &access_flags
, &overlapped
);
383 if (filename
[0] && filename
[1] == ':') {
384 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", filename
[0]);
385 } else if (filename
[0] == '\\' && filename
[1] == '\\') {
386 s
->drive_path
[0] = 0;
390 GetCurrentDirectory(MAX_PATH
, buf
);
391 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", buf
[0]);
394 s
->hfile
= CreateFile(filename
, access_flags
,
395 FILE_SHARE_READ
, NULL
,
396 OPEN_EXISTING
, overlapped
, NULL
);
397 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
398 int err
= GetLastError();
400 error_setg_win32(errp
, err
, "Could not open '%s'", filename
);
401 if (err
== ERROR_ACCESS_DENIED
) {
410 s
->aio
= win32_aio_init();
411 if (s
->aio
== NULL
) {
412 CloseHandle(s
->hfile
);
413 error_setg(errp
, "Could not initialize AIO");
418 ret
= win32_aio_attach(s
->aio
, s
->hfile
);
420 win32_aio_cleanup(s
->aio
);
421 CloseHandle(s
->hfile
);
422 error_setg_errno(errp
, -ret
, "Could not enable AIO");
426 win32_aio_attach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
429 /* When extending regular files, we get zeros from the OS */
430 bs
->supported_truncate_flags
= BDRV_REQ_ZERO_WRITE
;
438 static BlockAIOCB
*raw_aio_preadv(BlockDriverState
*bs
,
439 uint64_t offset
, uint64_t bytes
,
440 QEMUIOVector
*qiov
, int flags
,
441 BlockCompletionFunc
*cb
, void *opaque
)
443 BDRVRawState
*s
= bs
->opaque
;
445 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, offset
, bytes
, qiov
,
446 cb
, opaque
, QEMU_AIO_READ
);
448 return paio_submit(bs
, s
->hfile
, offset
, qiov
, bytes
,
449 cb
, opaque
, QEMU_AIO_READ
);
453 static BlockAIOCB
*raw_aio_pwritev(BlockDriverState
*bs
,
454 uint64_t offset
, uint64_t bytes
,
455 QEMUIOVector
*qiov
, int flags
,
456 BlockCompletionFunc
*cb
, void *opaque
)
458 BDRVRawState
*s
= bs
->opaque
;
460 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, offset
, bytes
, qiov
,
461 cb
, opaque
, QEMU_AIO_WRITE
);
463 return paio_submit(bs
, s
->hfile
, offset
, qiov
, bytes
,
464 cb
, opaque
, QEMU_AIO_WRITE
);
468 static BlockAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
469 BlockCompletionFunc
*cb
, void *opaque
)
471 BDRVRawState
*s
= bs
->opaque
;
472 return paio_submit(bs
, s
->hfile
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
475 static void raw_close(BlockDriverState
*bs
)
477 BDRVRawState
*s
= bs
->opaque
;
480 win32_aio_detach_aio_context(s
->aio
, bdrv_get_aio_context(bs
));
481 win32_aio_cleanup(s
->aio
);
485 CloseHandle(s
->hfile
);
486 if (bs
->open_flags
& BDRV_O_TEMPORARY
) {
487 unlink(bs
->filename
);
491 static int coroutine_fn
raw_co_truncate(BlockDriverState
*bs
, int64_t offset
,
492 bool exact
, PreallocMode prealloc
,
493 BdrvRequestFlags flags
, Error
**errp
)
495 BDRVRawState
*s
= bs
->opaque
;
499 if (prealloc
!= PREALLOC_MODE_OFF
) {
500 error_setg(errp
, "Unsupported preallocation mode '%s'",
501 PreallocMode_str(prealloc
));
509 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
510 * and GetLastError doesn't return NO_ERROR.
512 dwPtrLow
= SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
);
513 if (dwPtrLow
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
514 error_setg_win32(errp
, GetLastError(), "SetFilePointer error");
517 if (SetEndOfFile(s
->hfile
) == 0) {
518 error_setg_win32(errp
, GetLastError(), "SetEndOfFile error");
524 static int64_t raw_getlength(BlockDriverState
*bs
)
526 BDRVRawState
*s
= bs
->opaque
;
528 ULARGE_INTEGER available
, total
, total_free
;
535 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
536 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
540 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
542 l
.QuadPart
= total
.QuadPart
;
545 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
546 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
557 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
559 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
561 get_compressed_t get_compressed
;
563 const char *filename
= bs
->filename
;
564 /* WinNT support GetCompressedFileSize to determine allocate size */
566 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
567 "GetCompressedFileSizeA");
568 if (get_compressed
) {
570 low
= get_compressed(filename
, &high
);
571 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
572 return (((int64_t) high
) << 32) + low
;
576 if (_stati64(filename
, &st
) < 0) {
582 static int raw_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
584 BlockdevCreateOptionsFile
*file_opts
;
587 assert(options
->driver
== BLOCKDEV_DRIVER_FILE
);
588 file_opts
= &options
->u
.file
;
590 if (file_opts
->has_preallocation
) {
591 error_setg(errp
, "Preallocation is not supported on Windows");
594 if (file_opts
->has_nocow
) {
595 error_setg(errp
, "nocow is not supported on Windows");
599 fd
= qemu_open(file_opts
->filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
602 error_setg_errno(errp
, errno
, "Could not create file");
606 ftruncate(fd
, file_opts
->size
);
612 static int coroutine_fn
raw_co_create_opts(BlockDriver
*drv
,
613 const char *filename
,
617 BlockdevCreateOptions options
;
618 int64_t total_size
= 0;
620 strstart(filename
, "file:", &filename
);
622 /* Read out options */
623 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
626 options
= (BlockdevCreateOptions
) {
627 .driver
= BLOCKDEV_DRIVER_FILE
,
629 .filename
= (char *) filename
,
631 .has_preallocation
= false,
635 return raw_co_create(&options
, errp
);
638 static QemuOptsList raw_create_opts
= {
639 .name
= "raw-create-opts",
640 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
643 .name
= BLOCK_OPT_SIZE
,
644 .type
= QEMU_OPT_SIZE
,
645 .help
= "Virtual disk size"
647 { /* end of list */ }
651 BlockDriver bdrv_file
= {
652 .format_name
= "file",
653 .protocol_name
= "file",
654 .instance_size
= sizeof(BDRVRawState
),
655 .bdrv_needs_filename
= true,
656 .bdrv_parse_filename
= raw_parse_filename
,
657 .bdrv_file_open
= raw_open
,
658 .bdrv_refresh_limits
= raw_probe_alignment
,
659 .bdrv_close
= raw_close
,
660 .bdrv_co_create_opts
= raw_co_create_opts
,
661 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
663 .bdrv_aio_preadv
= raw_aio_preadv
,
664 .bdrv_aio_pwritev
= raw_aio_pwritev
,
665 .bdrv_aio_flush
= raw_aio_flush
,
667 .bdrv_co_truncate
= raw_co_truncate
,
668 .bdrv_getlength
= raw_getlength
,
669 .bdrv_get_allocated_file_size
670 = raw_get_allocated_file_size
,
672 .create_opts
= &raw_create_opts
,
675 /***********************************************/
678 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
680 char drives
[256], *pdrv
= drives
;
683 memset(drives
, 0, sizeof(drives
));
684 GetLogicalDriveStrings(sizeof(drives
), drives
);
685 while(pdrv
[0] != '\0') {
686 type
= GetDriveType(pdrv
);
689 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
693 pdrv
+= lstrlen(pdrv
) + 1;
698 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
700 BDRVRawState
*s
= bs
->opaque
;
704 if (strstart(filename
, "\\\\.\\", &p
) ||
705 strstart(filename
, "//./", &p
)) {
706 if (stristart(p
, "PhysicalDrive", NULL
))
707 return FTYPE_HARDDISK
;
708 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
709 type
= GetDriveType(s
->drive_path
);
711 case DRIVE_REMOVABLE
:
713 return FTYPE_HARDDISK
;
724 static int hdev_probe_device(const char *filename
)
726 if (strstart(filename
, "/dev/cdrom", NULL
))
728 if (is_windows_drive(filename
))
733 static void hdev_parse_filename(const char *filename
, QDict
*options
,
736 bdrv_parse_filename_strip_prefix(filename
, "host_device:", options
);
739 static void hdev_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
741 /* XXX Does Windows support AIO on less than 512-byte alignment? */
742 bs
->bl
.request_alignment
= 512;
745 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
748 BDRVRawState
*s
= bs
->opaque
;
749 int access_flags
, create_flags
;
752 char device_name
[64];
754 Error
*local_err
= NULL
;
755 const char *filename
;
758 QemuOpts
*opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0,
760 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
765 filename
= qemu_opt_get(opts
, "filename");
767 use_aio
= get_aio_option(opts
, flags
, &local_err
);
768 if (!local_err
&& use_aio
) {
769 error_setg(&local_err
, "AIO is not supported on Windows host devices");
772 error_propagate(errp
, local_err
);
777 if (strstart(filename
, "/dev/cdrom", NULL
)) {
778 if (find_cdrom(device_name
, sizeof(device_name
)) < 0) {
779 error_setg(errp
, "Could not open CD-ROM drive");
783 filename
= device_name
;
785 /* transform drive letters into device name */
786 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
787 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
788 filename
[1] == ':' && filename
[2] == '\0') {
789 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
790 filename
= device_name
;
793 s
->type
= find_device_type(bs
, filename
);
795 raw_parse_flags(flags
, use_aio
, &access_flags
, &overlapped
);
797 create_flags
= OPEN_EXISTING
;
799 s
->hfile
= CreateFile(filename
, access_flags
,
800 FILE_SHARE_READ
, NULL
,
801 create_flags
, overlapped
, NULL
);
802 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
803 int err
= GetLastError();
805 if (err
== ERROR_ACCESS_DENIED
) {
810 error_setg_errno(errp
, -ret
, "Could not open device");
819 static BlockDriver bdrv_host_device
= {
820 .format_name
= "host_device",
821 .protocol_name
= "host_device",
822 .instance_size
= sizeof(BDRVRawState
),
823 .bdrv_needs_filename
= true,
824 .bdrv_parse_filename
= hdev_parse_filename
,
825 .bdrv_probe_device
= hdev_probe_device
,
826 .bdrv_file_open
= hdev_open
,
827 .bdrv_close
= raw_close
,
828 .bdrv_refresh_limits
= hdev_refresh_limits
,
830 .bdrv_aio_preadv
= raw_aio_preadv
,
831 .bdrv_aio_pwritev
= raw_aio_pwritev
,
832 .bdrv_aio_flush
= raw_aio_flush
,
834 .bdrv_detach_aio_context
= raw_detach_aio_context
,
835 .bdrv_attach_aio_context
= raw_attach_aio_context
,
837 .bdrv_getlength
= raw_getlength
,
838 .has_variable_length
= true,
840 .bdrv_get_allocated_file_size
841 = raw_get_allocated_file_size
,
844 static void bdrv_file_init(void)
846 bdrv_register(&bdrv_file
);
847 bdrv_register(&bdrv_host_device
);
850 block_init(bdrv_file_init
);