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-common.h"
25 #include "qemu/timer.h"
26 #include "block/block_int.h"
27 #include "qemu/module.h"
30 #include "block/thread-pool.h"
37 #define FTYPE_HARDDISK 2
39 static QEMUWin32AIOState
*aio
;
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
) {
96 static int aio_worker(void *arg
)
98 RawWin32AIOData
*aiocb
= arg
;
102 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
104 count
= handle_aiocb_rw(aiocb
);
105 if (count
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
106 /* A short read means that we have reached EOF. Pad the buffer
107 * with zeros for bytes after EOF. */
108 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, count
,
109 0, aiocb
->aio_nbytes
- count
);
111 count
= aiocb
->aio_nbytes
;
113 if (count
== aiocb
->aio_nbytes
) {
120 count
= handle_aiocb_rw(aiocb
);
121 if (count
== aiocb
->aio_nbytes
) {
128 if (!FlushFileBuffers(aiocb
->hfile
)) {
133 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
138 g_slice_free(RawWin32AIOData
, aiocb
);
142 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, HANDLE hfile
,
143 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
144 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
146 RawWin32AIOData
*acb
= g_slice_new(RawWin32AIOData
);
151 acb
->aio_type
= type
;
154 acb
->aio_iov
= qiov
->iov
;
155 acb
->aio_niov
= qiov
->niov
;
157 acb
->aio_nbytes
= nb_sectors
* 512;
158 acb
->aio_offset
= sector_num
* 512;
160 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
161 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
162 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
165 int qemu_ftruncate64(int fd
, int64_t length
)
173 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
176 h
= (HANDLE
)_get_osfhandle(fd
);
178 /* get current position, ftruncate do not change position */
180 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
181 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
186 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
187 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
190 res
= SetEndOfFile(h
);
192 /* back to old position */
193 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
197 static int set_sparse(int fd
)
200 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
201 NULL
, 0, NULL
, 0, &returned
, NULL
);
204 static void raw_parse_flags(int flags
, int *access_flags
, DWORD
*overlapped
)
206 assert(access_flags
!= NULL
);
207 assert(overlapped
!= NULL
);
209 if (flags
& BDRV_O_RDWR
) {
210 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
212 *access_flags
= GENERIC_READ
;
215 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
216 if (flags
& BDRV_O_NATIVE_AIO
) {
217 *overlapped
|= FILE_FLAG_OVERLAPPED
;
219 if (flags
& BDRV_O_NOCACHE
) {
220 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
224 static QemuOptsList raw_runtime_opts
= {
226 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
230 .type
= QEMU_OPT_STRING
,
231 .help
= "File name of the image",
233 { /* end of list */ }
237 static int raw_open(BlockDriverState
*bs
, const char *unused
,
238 QDict
*options
, int flags
)
240 BDRVRawState
*s
= bs
->opaque
;
244 Error
*local_err
= NULL
;
245 const char *filename
;
248 s
->type
= FTYPE_FILE
;
250 opts
= qemu_opts_create_nofail(&raw_runtime_opts
);
251 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
252 if (error_is_set(&local_err
)) {
253 qerror_report_err(local_err
);
254 error_free(local_err
);
259 filename
= qemu_opt_get(opts
, "filename");
261 raw_parse_flags(flags
, &access_flags
, &overlapped
);
263 if ((flags
& BDRV_O_NATIVE_AIO
) && aio
== NULL
) {
264 aio
= win32_aio_init();
271 s
->hfile
= CreateFile(filename
, access_flags
,
272 FILE_SHARE_READ
, NULL
,
273 OPEN_EXISTING
, overlapped
, NULL
);
274 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
275 int err
= GetLastError();
277 if (err
== ERROR_ACCESS_DENIED
) {
285 if (flags
& BDRV_O_NATIVE_AIO
) {
286 ret
= win32_aio_attach(aio
, s
->hfile
);
288 CloseHandle(s
->hfile
);
300 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
301 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
302 BlockDriverCompletionFunc
*cb
, void *opaque
)
304 BDRVRawState
*s
= bs
->opaque
;
306 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
307 nb_sectors
, cb
, opaque
, QEMU_AIO_READ
);
309 return paio_submit(bs
, s
->hfile
, sector_num
, qiov
, nb_sectors
,
310 cb
, opaque
, QEMU_AIO_READ
);
314 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
315 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
316 BlockDriverCompletionFunc
*cb
, void *opaque
)
318 BDRVRawState
*s
= bs
->opaque
;
320 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
321 nb_sectors
, cb
, opaque
, QEMU_AIO_WRITE
);
323 return paio_submit(bs
, s
->hfile
, sector_num
, qiov
, nb_sectors
,
324 cb
, opaque
, QEMU_AIO_WRITE
);
328 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
329 BlockDriverCompletionFunc
*cb
, void *opaque
)
331 BDRVRawState
*s
= bs
->opaque
;
332 return paio_submit(bs
, s
->hfile
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
335 static void raw_close(BlockDriverState
*bs
)
337 BDRVRawState
*s
= bs
->opaque
;
338 CloseHandle(s
->hfile
);
341 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
343 BDRVRawState
*s
= bs
->opaque
;
351 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
352 * and GetLastError doesn't return NO_ERROR.
354 dwPtrLow
= SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
);
355 if (dwPtrLow
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
356 fprintf(stderr
, "SetFilePointer error: %lu\n", GetLastError());
359 if (SetEndOfFile(s
->hfile
) == 0) {
360 fprintf(stderr
, "SetEndOfFile error: %lu\n", GetLastError());
366 static int64_t raw_getlength(BlockDriverState
*bs
)
368 BDRVRawState
*s
= bs
->opaque
;
370 ULARGE_INTEGER available
, total
, total_free
;
377 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
378 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
382 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
384 l
.QuadPart
= total
.QuadPart
;
387 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
388 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
399 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
401 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
403 get_compressed_t get_compressed
;
405 const char *filename
= bs
->filename
;
406 /* WinNT support GetCompressedFileSize to determine allocate size */
408 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
409 "GetCompressedFileSizeA");
410 if (get_compressed
) {
412 low
= get_compressed(filename
, &high
);
413 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
414 return (((int64_t) high
) << 32) + low
;
418 if (_stati64(filename
, &st
) < 0) {
424 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
427 int64_t total_size
= 0;
429 /* Read out options */
430 while (options
&& options
->name
) {
431 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
432 total_size
= options
->value
.n
/ 512;
437 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
442 ftruncate(fd
, total_size
* 512);
447 static QEMUOptionParameter raw_create_options
[] = {
449 .name
= BLOCK_OPT_SIZE
,
451 .help
= "Virtual disk size"
456 static BlockDriver bdrv_file
= {
457 .format_name
= "file",
458 .protocol_name
= "file",
459 .instance_size
= sizeof(BDRVRawState
),
460 .bdrv_file_open
= raw_open
,
461 .bdrv_close
= raw_close
,
462 .bdrv_create
= raw_create
,
464 .bdrv_aio_readv
= raw_aio_readv
,
465 .bdrv_aio_writev
= raw_aio_writev
,
466 .bdrv_aio_flush
= raw_aio_flush
,
468 .bdrv_truncate
= raw_truncate
,
469 .bdrv_getlength
= raw_getlength
,
470 .bdrv_get_allocated_file_size
471 = raw_get_allocated_file_size
,
473 .create_options
= raw_create_options
,
476 /***********************************************/
479 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
481 char drives
[256], *pdrv
= drives
;
484 memset(drives
, 0, sizeof(drives
));
485 GetLogicalDriveStrings(sizeof(drives
), drives
);
486 while(pdrv
[0] != '\0') {
487 type
= GetDriveType(pdrv
);
490 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
494 pdrv
+= lstrlen(pdrv
) + 1;
499 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
501 BDRVRawState
*s
= bs
->opaque
;
505 if (strstart(filename
, "\\\\.\\", &p
) ||
506 strstart(filename
, "//./", &p
)) {
507 if (stristart(p
, "PhysicalDrive", NULL
))
508 return FTYPE_HARDDISK
;
509 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
510 type
= GetDriveType(s
->drive_path
);
512 case DRIVE_REMOVABLE
:
514 return FTYPE_HARDDISK
;
525 static int hdev_probe_device(const char *filename
)
527 if (strstart(filename
, "/dev/cdrom", NULL
))
529 if (is_windows_drive(filename
))
534 static int hdev_open(BlockDriverState
*bs
, const char *dummy
,
535 QDict
*options
, int flags
)
537 BDRVRawState
*s
= bs
->opaque
;
538 int access_flags
, create_flags
;
540 char device_name
[64];
541 const char *filename
= qdict_get_str(options
, "filename");
543 if (strstart(filename
, "/dev/cdrom", NULL
)) {
544 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
546 filename
= device_name
;
548 /* transform drive letters into device name */
549 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
550 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
551 filename
[1] == ':' && filename
[2] == '\0') {
552 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
553 filename
= device_name
;
556 s
->type
= find_device_type(bs
, filename
);
558 raw_parse_flags(flags
, &access_flags
, &overlapped
);
560 create_flags
= OPEN_EXISTING
;
562 s
->hfile
= CreateFile(filename
, access_flags
,
563 FILE_SHARE_READ
, NULL
,
564 create_flags
, overlapped
, NULL
);
565 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
566 int err
= GetLastError();
568 if (err
== ERROR_ACCESS_DENIED
)
575 static int hdev_has_zero_init(BlockDriverState
*bs
)
580 static BlockDriver bdrv_host_device
= {
581 .format_name
= "host_device",
582 .protocol_name
= "host_device",
583 .instance_size
= sizeof(BDRVRawState
),
584 .bdrv_probe_device
= hdev_probe_device
,
585 .bdrv_file_open
= hdev_open
,
586 .bdrv_close
= raw_close
,
587 .bdrv_has_zero_init
= hdev_has_zero_init
,
589 .bdrv_aio_readv
= raw_aio_readv
,
590 .bdrv_aio_writev
= raw_aio_writev
,
591 .bdrv_aio_flush
= raw_aio_flush
,
593 .bdrv_getlength
= raw_getlength
,
594 .bdrv_get_allocated_file_size
595 = raw_get_allocated_file_size
,
598 static void bdrv_file_init(void)
600 bdrv_register(&bdrv_file
);
601 bdrv_register(&bdrv_host_device
);
604 block_init(bdrv_file_init
);