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"
26 #include "qemu-timer.h"
29 #include "block_int.h"
35 #define FTYPE_HARDDISK 2
37 typedef struct BDRVRawState
{
40 char drive_path
[16]; /* format: "d:\" */
43 typedef struct RawAIOCB
{
44 BlockDriverAIOCB common
;
50 int qemu_ftruncate64(int fd
, int64_t length
)
57 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
60 h
= (HANDLE
)_get_osfhandle(fd
);
62 /* get current position, ftruncate do not change position */
64 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
65 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
69 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
71 res
= SetEndOfFile(h
);
73 /* back to old position */
74 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
78 static int set_sparse(int fd
)
81 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
82 NULL
, 0, NULL
, 0, &returned
, NULL
);
85 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
87 BDRVRawState
*s
= bs
->opaque
;
88 int access_flags
, create_flags
;
93 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
94 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
96 access_flags
= GENERIC_READ
;
98 if (flags
& BDRV_O_CREAT
) {
99 create_flags
= CREATE_ALWAYS
;
101 create_flags
= OPEN_EXISTING
;
104 overlapped
= FILE_ATTRIBUTE_NORMAL
;
106 overlapped
= FILE_FLAG_OVERLAPPED
;
108 s
->hfile
= CreateFile(filename
, access_flags
,
109 FILE_SHARE_READ
, NULL
,
110 create_flags
, overlapped
, NULL
);
111 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
112 int err
= GetLastError();
114 if (err
== ERROR_ACCESS_DENIED
)
121 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
122 uint8_t *buf
, int count
)
124 BDRVRawState
*s
= bs
->opaque
;
129 memset(&ov
, 0, sizeof(ov
));
131 ov
.OffsetHigh
= offset
>> 32;
132 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
134 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
143 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
144 const uint8_t *buf
, int count
)
146 BDRVRawState
*s
= bs
->opaque
;
151 memset(&ov
, 0, sizeof(ov
));
153 ov
.OffsetHigh
= offset
>> 32;
154 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
156 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
167 static void raw_aio_cb(void *opaque
)
169 RawAIOCB
*acb
= opaque
;
170 BlockDriverState
*bs
= acb
->common
.bs
;
171 BDRVRawState
*s
= bs
->opaque
;
175 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
176 if (!ret
|| ret_count
!= acb
->count
) {
177 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
179 acb
->common
.cb(acb
->common
.opaque
, 0);
184 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
185 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
186 BlockDriverCompletionFunc
*cb
, void *opaque
)
191 acb
= qemu_aio_get(bs
, cb
, opaque
);
193 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
195 qemu_aio_release(acb
);
199 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
200 offset
= sector_num
* 512;
201 acb
->ov
.Offset
= offset
;
202 acb
->ov
.OffsetHigh
= offset
>> 32;
203 acb
->ov
.hEvent
= acb
->hEvent
;
204 acb
->count
= nb_sectors
* 512;
206 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
211 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
212 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
213 BlockDriverCompletionFunc
*cb
, void *opaque
)
215 BDRVRawState
*s
= bs
->opaque
;
219 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
222 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
224 qemu_aio_release(acb
);
228 qemu_aio_release(acb
);
230 return (BlockDriverAIOCB
*)acb
;
233 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
234 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
235 BlockDriverCompletionFunc
*cb
, void *opaque
)
237 BDRVRawState
*s
= bs
->opaque
;
241 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
244 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
246 qemu_aio_release(acb
);
250 qemu_aio_release(acb
);
252 return (BlockDriverAIOCB
*)acb
;
255 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
258 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
259 BlockDriverState
*bs
= acb
->common
.bs
;
260 BDRVRawState
*s
= bs
->opaque
;
262 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
263 /* XXX: if more than one async I/O it is not correct */
265 qemu_aio_release(acb
);
270 static void raw_flush(BlockDriverState
*bs
)
272 BDRVRawState
*s
= bs
->opaque
;
273 FlushFileBuffers(s
->hfile
);
276 static void raw_close(BlockDriverState
*bs
)
278 BDRVRawState
*s
= bs
->opaque
;
279 CloseHandle(s
->hfile
);
282 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
284 BDRVRawState
*s
= bs
->opaque
;
289 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
291 if (!SetEndOfFile(s
->hfile
))
296 static int64_t raw_getlength(BlockDriverState
*bs
)
298 BDRVRawState
*s
= bs
->opaque
;
300 ULARGE_INTEGER available
, total
, total_free
;
307 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
308 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
312 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
314 l
.QuadPart
= total
.QuadPart
;
317 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
318 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
329 static int raw_create(const char *filename
, int64_t total_size
,
330 const char *backing_file
, int flags
)
334 if (flags
|| backing_file
)
337 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
342 ftruncate(fd
, total_size
* 512);
347 void qemu_aio_init(void)
351 void qemu_aio_poll(void)
355 void qemu_aio_flush(void)
359 void qemu_aio_wait_start(void)
363 void qemu_aio_wait(void)
370 void qemu_aio_wait_end(void)
374 BlockDriver bdrv_raw
= {
376 sizeof(BDRVRawState
),
377 NULL
, /* no probe for protocols */
386 .bdrv_aio_read
= raw_aio_read
,
387 .bdrv_aio_write
= raw_aio_write
,
388 .bdrv_aio_cancel
= raw_aio_cancel
,
389 .aiocb_size
= sizeof(RawAIOCB
);
391 .protocol_name
= "file",
392 .bdrv_pread
= raw_pread
,
393 .bdrv_pwrite
= raw_pwrite
,
394 .bdrv_truncate
= raw_truncate
,
395 .bdrv_getlength
= raw_getlength
,
398 /***********************************************/
401 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
403 char drives
[256], *pdrv
= drives
;
406 memset(drives
, 0, sizeof(drives
));
407 GetLogicalDriveStrings(sizeof(drives
), drives
);
408 while(pdrv
[0] != '\0') {
409 type
= GetDriveType(pdrv
);
412 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
416 pdrv
+= lstrlen(pdrv
) + 1;
421 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
423 BDRVRawState
*s
= bs
->opaque
;
427 if (strstart(filename
, "\\\\.\\", &p
) ||
428 strstart(filename
, "//./", &p
)) {
429 if (stristart(p
, "PhysicalDrive", NULL
))
430 return FTYPE_HARDDISK
;
431 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
432 type
= GetDriveType(s
->drive_path
);
433 if (type
== DRIVE_CDROM
)
442 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
444 BDRVRawState
*s
= bs
->opaque
;
445 int access_flags
, create_flags
;
447 char device_name
[64];
449 if (strstart(filename
, "/dev/cdrom", NULL
)) {
450 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
452 filename
= device_name
;
454 /* transform drive letters into device name */
455 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
456 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
457 filename
[1] == ':' && filename
[2] == '\0') {
458 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
459 filename
= device_name
;
462 s
->type
= find_device_type(bs
, filename
);
464 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
465 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
467 access_flags
= GENERIC_READ
;
469 create_flags
= OPEN_EXISTING
;
472 overlapped
= FILE_ATTRIBUTE_NORMAL
;
474 overlapped
= FILE_FLAG_OVERLAPPED
;
476 s
->hfile
= CreateFile(filename
, access_flags
,
477 FILE_SHARE_READ
, NULL
,
478 create_flags
, overlapped
, NULL
);
479 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
480 int err
= GetLastError();
482 if (err
== ERROR_ACCESS_DENIED
)
490 /***********************************************/
491 /* removable device additional commands */
493 static int raw_is_inserted(BlockDriverState
*bs
)
498 static int raw_media_changed(BlockDriverState
*bs
)
503 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
507 if (s
->type
== FTYPE_FILE
)
510 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
511 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
513 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
514 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
518 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
524 BlockDriver bdrv_host_device
= {
526 sizeof(BDRVRawState
),
527 NULL
, /* no probe for protocols */
536 .bdrv_aio_read
= raw_aio_read
,
537 .bdrv_aio_write
= raw_aio_write
,
538 .bdrv_aio_cancel
= raw_aio_cancel
,
539 .aiocb_size
= sizeof(RawAIOCB
);
541 .bdrv_pread
= raw_pread
,
542 .bdrv_pwrite
= raw_pwrite
,
543 .bdrv_getlength
= raw_getlength
,