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
) {
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
&& aiocb
->bs
->growable
) {
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
);
139 g_slice_free(RawWin32AIOData
, aiocb
);
143 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, HANDLE hfile
,
144 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
145 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
147 RawWin32AIOData
*acb
= g_slice_new(RawWin32AIOData
);
152 acb
->aio_type
= type
;
155 acb
->aio_iov
= qiov
->iov
;
156 acb
->aio_niov
= qiov
->niov
;
158 acb
->aio_nbytes
= nb_sectors
* 512;
159 acb
->aio_offset
= sector_num
* 512;
161 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
162 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
163 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
166 int qemu_ftruncate64(int fd
, int64_t length
)
174 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
177 h
= (HANDLE
)_get_osfhandle(fd
);
179 /* get current position, ftruncate do not change position */
181 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
182 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
187 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
188 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
191 res
= SetEndOfFile(h
);
193 /* back to old position */
194 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
198 static int set_sparse(int fd
)
201 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
202 NULL
, 0, NULL
, 0, &returned
, NULL
);
205 static void raw_parse_flags(int flags
, int *access_flags
, DWORD
*overlapped
)
207 assert(access_flags
!= NULL
);
208 assert(overlapped
!= NULL
);
210 if (flags
& BDRV_O_RDWR
) {
211 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
213 *access_flags
= GENERIC_READ
;
216 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
217 if (flags
& BDRV_O_NATIVE_AIO
) {
218 *overlapped
|= FILE_FLAG_OVERLAPPED
;
220 if (flags
& BDRV_O_NOCACHE
) {
221 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
225 static QemuOptsList raw_runtime_opts
= {
227 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
231 .type
= QEMU_OPT_STRING
,
232 .help
= "File name of the image",
234 { /* end of list */ }
238 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
241 BDRVRawState
*s
= bs
->opaque
;
245 Error
*local_err
= NULL
;
246 const char *filename
;
249 s
->type
= FTYPE_FILE
;
251 opts
= qemu_opts_create_nofail(&raw_runtime_opts
);
252 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
253 if (error_is_set(&local_err
)) {
254 error_propagate(errp
, 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();
266 error_setg(errp
, "Could not initialize AIO");
272 s
->hfile
= CreateFile(filename
, access_flags
,
273 FILE_SHARE_READ
, NULL
,
274 OPEN_EXISTING
, overlapped
, NULL
);
275 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
276 int err
= GetLastError();
278 if (err
== ERROR_ACCESS_DENIED
) {
286 if (flags
& BDRV_O_NATIVE_AIO
) {
287 ret
= win32_aio_attach(aio
, s
->hfile
);
289 CloseHandle(s
->hfile
);
290 error_setg_errno(errp
, -ret
, "Could not enable AIO");
302 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
303 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
304 BlockDriverCompletionFunc
*cb
, void *opaque
)
306 BDRVRawState
*s
= bs
->opaque
;
308 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
309 nb_sectors
, cb
, opaque
, QEMU_AIO_READ
);
311 return paio_submit(bs
, s
->hfile
, sector_num
, qiov
, nb_sectors
,
312 cb
, opaque
, QEMU_AIO_READ
);
316 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
317 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
318 BlockDriverCompletionFunc
*cb
, void *opaque
)
320 BDRVRawState
*s
= bs
->opaque
;
322 return win32_aio_submit(bs
, s
->aio
, s
->hfile
, sector_num
, qiov
,
323 nb_sectors
, cb
, opaque
, QEMU_AIO_WRITE
);
325 return paio_submit(bs
, s
->hfile
, sector_num
, qiov
, nb_sectors
,
326 cb
, opaque
, QEMU_AIO_WRITE
);
330 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
331 BlockDriverCompletionFunc
*cb
, void *opaque
)
333 BDRVRawState
*s
= bs
->opaque
;
334 return paio_submit(bs
, s
->hfile
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
337 static void raw_close(BlockDriverState
*bs
)
339 BDRVRawState
*s
= bs
->opaque
;
340 CloseHandle(s
->hfile
);
343 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
345 BDRVRawState
*s
= bs
->opaque
;
353 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
354 * and GetLastError doesn't return NO_ERROR.
356 dwPtrLow
= SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
);
357 if (dwPtrLow
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
358 fprintf(stderr
, "SetFilePointer error: %lu\n", GetLastError());
361 if (SetEndOfFile(s
->hfile
) == 0) {
362 fprintf(stderr
, "SetEndOfFile error: %lu\n", GetLastError());
368 static int64_t raw_getlength(BlockDriverState
*bs
)
370 BDRVRawState
*s
= bs
->opaque
;
372 ULARGE_INTEGER available
, total
, total_free
;
379 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
380 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
384 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
386 l
.QuadPart
= total
.QuadPart
;
389 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
390 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
401 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
403 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
405 get_compressed_t get_compressed
;
407 const char *filename
= bs
->filename
;
408 /* WinNT support GetCompressedFileSize to determine allocate size */
410 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
411 "GetCompressedFileSizeA");
412 if (get_compressed
) {
414 low
= get_compressed(filename
, &high
);
415 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
416 return (((int64_t) high
) << 32) + low
;
420 if (_stati64(filename
, &st
) < 0) {
426 static int raw_create(const char *filename
, QEMUOptionParameter
*options
,
430 int64_t total_size
= 0;
432 /* Read out options */
433 while (options
&& options
->name
) {
434 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
435 total_size
= options
->value
.n
/ 512;
440 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
443 error_setg_errno(errp
, errno
, "Could not create file");
447 ftruncate(fd
, total_size
* 512);
452 static QEMUOptionParameter raw_create_options
[] = {
454 .name
= BLOCK_OPT_SIZE
,
456 .help
= "Virtual disk size"
461 static BlockDriver bdrv_file
= {
462 .format_name
= "file",
463 .protocol_name
= "file",
464 .instance_size
= sizeof(BDRVRawState
),
465 .bdrv_needs_filename
= true,
466 .bdrv_file_open
= raw_open
,
467 .bdrv_close
= raw_close
,
468 .bdrv_create
= raw_create
,
469 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
471 .bdrv_aio_readv
= raw_aio_readv
,
472 .bdrv_aio_writev
= raw_aio_writev
,
473 .bdrv_aio_flush
= raw_aio_flush
,
475 .bdrv_truncate
= raw_truncate
,
476 .bdrv_getlength
= raw_getlength
,
477 .bdrv_get_allocated_file_size
478 = raw_get_allocated_file_size
,
480 .create_options
= raw_create_options
,
483 /***********************************************/
486 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
488 char drives
[256], *pdrv
= drives
;
491 memset(drives
, 0, sizeof(drives
));
492 GetLogicalDriveStrings(sizeof(drives
), drives
);
493 while(pdrv
[0] != '\0') {
494 type
= GetDriveType(pdrv
);
497 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
501 pdrv
+= lstrlen(pdrv
) + 1;
506 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
508 BDRVRawState
*s
= bs
->opaque
;
512 if (strstart(filename
, "\\\\.\\", &p
) ||
513 strstart(filename
, "//./", &p
)) {
514 if (stristart(p
, "PhysicalDrive", NULL
))
515 return FTYPE_HARDDISK
;
516 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
517 type
= GetDriveType(s
->drive_path
);
519 case DRIVE_REMOVABLE
:
521 return FTYPE_HARDDISK
;
532 static int hdev_probe_device(const char *filename
)
534 if (strstart(filename
, "/dev/cdrom", NULL
))
536 if (is_windows_drive(filename
))
541 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
544 BDRVRawState
*s
= bs
->opaque
;
545 int access_flags
, create_flags
;
548 char device_name
[64];
550 Error
*local_err
= NULL
;
551 const char *filename
;
553 QemuOpts
*opts
= qemu_opts_create_nofail(&raw_runtime_opts
);
554 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
555 if (error_is_set(&local_err
)) {
556 error_propagate(errp
, local_err
);
561 filename
= qemu_opt_get(opts
, "filename");
563 if (strstart(filename
, "/dev/cdrom", NULL
)) {
564 if (find_cdrom(device_name
, sizeof(device_name
)) < 0) {
565 error_setg(errp
, "Could not open CD-ROM drive");
569 filename
= device_name
;
571 /* transform drive letters into device name */
572 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
573 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
574 filename
[1] == ':' && filename
[2] == '\0') {
575 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
576 filename
= device_name
;
579 s
->type
= find_device_type(bs
, filename
);
581 raw_parse_flags(flags
, &access_flags
, &overlapped
);
583 create_flags
= OPEN_EXISTING
;
585 s
->hfile
= CreateFile(filename
, access_flags
,
586 FILE_SHARE_READ
, NULL
,
587 create_flags
, overlapped
, NULL
);
588 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
589 int err
= GetLastError();
591 if (err
== ERROR_ACCESS_DENIED
) {
596 error_setg_errno(errp
, -ret
, "Could not open device");
605 static BlockDriver bdrv_host_device
= {
606 .format_name
= "host_device",
607 .protocol_name
= "host_device",
608 .instance_size
= sizeof(BDRVRawState
),
609 .bdrv_needs_filename
= true,
610 .bdrv_probe_device
= hdev_probe_device
,
611 .bdrv_file_open
= hdev_open
,
612 .bdrv_close
= raw_close
,
614 .bdrv_aio_readv
= raw_aio_readv
,
615 .bdrv_aio_writev
= raw_aio_writev
,
616 .bdrv_aio_flush
= raw_aio_flush
,
618 .bdrv_getlength
= raw_getlength
,
619 .has_variable_length
= true,
621 .bdrv_get_allocated_file_size
622 = raw_get_allocated_file_size
,
625 static void bdrv_file_init(void)
627 bdrv_register(&bdrv_file
);
628 bdrv_register(&bdrv_host_device
);
631 block_init(bdrv_file_init
);