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_int.h"
30 #include "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
);
150 acb
->aio_type
= type
;
153 acb
->aio_iov
= qiov
->iov
;
154 acb
->aio_niov
= qiov
->niov
;
156 acb
->aio_nbytes
= nb_sectors
* 512;
157 acb
->aio_offset
= sector_num
* 512;
159 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
160 return thread_pool_submit_aio(aio_worker
, acb
, cb
, opaque
);
163 int qemu_ftruncate64(int fd
, int64_t length
)
171 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
174 h
= (HANDLE
)_get_osfhandle(fd
);
176 /* get current position, ftruncate do not change position */
178 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
179 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
184 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
185 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
188 res
= SetEndOfFile(h
);
190 /* back to old position */
191 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
195 static int set_sparse(int fd
)
198 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
199 NULL
, 0, NULL
, 0, &returned
, NULL
);
202 static void raw_parse_flags(int flags
, int *access_flags
, DWORD
*overlapped
)
204 assert(access_flags
!= NULL
);
205 assert(overlapped
!= NULL
);
207 if (flags
& BDRV_O_RDWR
) {
208 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
210 *access_flags
= GENERIC_READ
;
213 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
214 if (flags
& BDRV_O_NATIVE_AIO
) {
215 *overlapped
|= FILE_FLAG_OVERLAPPED
;
217 if (flags
& BDRV_O_NOCACHE
) {
218 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
222 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
224 BDRVRawState
*s
= bs
->opaque
;
228 s
->type
= FTYPE_FILE
;
230 raw_parse_flags(flags
, &access_flags
, &overlapped
);
232 if ((flags
& BDRV_O_NATIVE_AIO
) && aio
== NULL
) {
233 aio
= win32_aio_init();
239 s
->hfile
= CreateFile(filename
, access_flags
,
240 FILE_SHARE_READ
, NULL
,
241 OPEN_EXISTING
, overlapped
, NULL
);
242 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
243 int err
= GetLastError();
245 if (err
== ERROR_ACCESS_DENIED
)
250 if (flags
& BDRV_O_NATIVE_AIO
) {
251 int ret
= win32_aio_attach(aio
, s
->hfile
);
253 CloseHandle(s
->hfile
);
261 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
262 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
263 BlockDriverCompletionFunc
*cb
, void *opaque
)
265 BDRVRawState
*s
= bs
->opaque
;
267 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
268 nb_sectors
, cb
, opaque
, QEMU_AIO_READ
);
270 return paio_submit(bs
, s
->hfile
, sector_num
, qiov
, nb_sectors
,
271 cb
, opaque
, QEMU_AIO_READ
);
275 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
276 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
277 BlockDriverCompletionFunc
*cb
, void *opaque
)
279 BDRVRawState
*s
= bs
->opaque
;
281 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
282 nb_sectors
, cb
, opaque
, QEMU_AIO_WRITE
);
284 return paio_submit(bs
, s
->hfile
, sector_num
, qiov
, nb_sectors
,
285 cb
, opaque
, QEMU_AIO_WRITE
);
289 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
290 BlockDriverCompletionFunc
*cb
, void *opaque
)
292 BDRVRawState
*s
= bs
->opaque
;
293 return paio_submit(bs
, s
->hfile
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
296 static void raw_close(BlockDriverState
*bs
)
298 BDRVRawState
*s
= bs
->opaque
;
299 CloseHandle(s
->hfile
);
302 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
304 BDRVRawState
*s
= bs
->opaque
;
312 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
313 * and GetLastError doesn't return NO_ERROR.
315 dwPtrLow
= SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
);
316 if (dwPtrLow
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
317 fprintf(stderr
, "SetFilePointer error: %d\n", GetLastError());
320 if (SetEndOfFile(s
->hfile
) == 0) {
321 fprintf(stderr
, "SetEndOfFile error: %d\n", GetLastError());
327 static int64_t raw_getlength(BlockDriverState
*bs
)
329 BDRVRawState
*s
= bs
->opaque
;
331 ULARGE_INTEGER available
, total
, total_free
;
338 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
339 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
343 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
345 l
.QuadPart
= total
.QuadPart
;
348 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
349 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
360 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
362 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
364 get_compressed_t get_compressed
;
366 const char *filename
= bs
->filename
;
367 /* WinNT support GetCompressedFileSize to determine allocate size */
369 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
370 "GetCompressedFileSizeA");
371 if (get_compressed
) {
373 low
= get_compressed(filename
, &high
);
374 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
375 return (((int64_t) high
) << 32) + low
;
379 if (_stati64(filename
, &st
) < 0) {
385 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
388 int64_t total_size
= 0;
390 /* Read out options */
391 while (options
&& options
->name
) {
392 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
393 total_size
= options
->value
.n
/ 512;
398 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
403 ftruncate(fd
, total_size
* 512);
408 static QEMUOptionParameter raw_create_options
[] = {
410 .name
= BLOCK_OPT_SIZE
,
412 .help
= "Virtual disk size"
417 static BlockDriver bdrv_file
= {
418 .format_name
= "file",
419 .protocol_name
= "file",
420 .instance_size
= sizeof(BDRVRawState
),
421 .bdrv_file_open
= raw_open
,
422 .bdrv_close
= raw_close
,
423 .bdrv_create
= raw_create
,
425 .bdrv_aio_readv
= raw_aio_readv
,
426 .bdrv_aio_writev
= raw_aio_writev
,
427 .bdrv_aio_flush
= raw_aio_flush
,
429 .bdrv_truncate
= raw_truncate
,
430 .bdrv_getlength
= raw_getlength
,
431 .bdrv_get_allocated_file_size
432 = raw_get_allocated_file_size
,
434 .create_options
= raw_create_options
,
437 /***********************************************/
440 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
442 char drives
[256], *pdrv
= drives
;
445 memset(drives
, 0, sizeof(drives
));
446 GetLogicalDriveStrings(sizeof(drives
), drives
);
447 while(pdrv
[0] != '\0') {
448 type
= GetDriveType(pdrv
);
451 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
455 pdrv
+= lstrlen(pdrv
) + 1;
460 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
462 BDRVRawState
*s
= bs
->opaque
;
466 if (strstart(filename
, "\\\\.\\", &p
) ||
467 strstart(filename
, "//./", &p
)) {
468 if (stristart(p
, "PhysicalDrive", NULL
))
469 return FTYPE_HARDDISK
;
470 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
471 type
= GetDriveType(s
->drive_path
);
473 case DRIVE_REMOVABLE
:
475 return FTYPE_HARDDISK
;
486 static int hdev_probe_device(const char *filename
)
488 if (strstart(filename
, "/dev/cdrom", NULL
))
490 if (is_windows_drive(filename
))
495 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
497 BDRVRawState
*s
= bs
->opaque
;
498 int access_flags
, create_flags
;
500 char device_name
[64];
502 if (strstart(filename
, "/dev/cdrom", NULL
)) {
503 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
505 filename
= device_name
;
507 /* transform drive letters into device name */
508 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
509 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
510 filename
[1] == ':' && filename
[2] == '\0') {
511 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
512 filename
= device_name
;
515 s
->type
= find_device_type(bs
, filename
);
517 raw_parse_flags(flags
, &access_flags
, &overlapped
);
519 create_flags
= OPEN_EXISTING
;
521 s
->hfile
= CreateFile(filename
, access_flags
,
522 FILE_SHARE_READ
, NULL
,
523 create_flags
, overlapped
, NULL
);
524 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
525 int err
= GetLastError();
527 if (err
== ERROR_ACCESS_DENIED
)
534 static int hdev_has_zero_init(BlockDriverState
*bs
)
539 static BlockDriver bdrv_host_device
= {
540 .format_name
= "host_device",
541 .protocol_name
= "host_device",
542 .instance_size
= sizeof(BDRVRawState
),
543 .bdrv_probe_device
= hdev_probe_device
,
544 .bdrv_file_open
= hdev_open
,
545 .bdrv_close
= raw_close
,
546 .bdrv_has_zero_init
= hdev_has_zero_init
,
548 .bdrv_aio_readv
= raw_aio_readv
,
549 .bdrv_aio_writev
= raw_aio_writev
,
550 .bdrv_aio_flush
= raw_aio_flush
,
552 .bdrv_getlength
= raw_getlength
,
553 .bdrv_get_allocated_file_size
554 = raw_get_allocated_file_size
,
557 static void bdrv_file_init(void)
559 bdrv_register(&bdrv_file
);
560 bdrv_register(&bdrv_host_device
);
563 block_init(bdrv_file_init
);