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"
34 #define FTYPE_HARDDISK 2
36 typedef struct BDRVRawState
{
39 char drive_path
[16]; /* format: "d:\" */
42 typedef struct RawAIOCB
{
43 BlockDriverAIOCB common
;
49 int qemu_ftruncate64(int fd
, int64_t length
)
56 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
59 h
= (HANDLE
)_get_osfhandle(fd
);
61 /* get current position, ftruncate do not change position */
63 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
64 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
68 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
70 res
= SetEndOfFile(h
);
72 /* back to old position */
73 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
77 static int set_sparse(int fd
)
80 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
81 NULL
, 0, NULL
, 0, &returned
, NULL
);
84 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
86 BDRVRawState
*s
= bs
->opaque
;
87 int access_flags
, create_flags
;
92 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
93 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
95 access_flags
= GENERIC_READ
;
97 if (flags
& BDRV_O_CREAT
) {
98 create_flags
= CREATE_ALWAYS
;
100 create_flags
= OPEN_EXISTING
;
103 overlapped
= FILE_FLAG_OVERLAPPED
;
105 overlapped
= FILE_ATTRIBUTE_NORMAL
;
107 if (flags
& BDRV_O_DIRECT
)
108 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
109 s
->hfile
= CreateFile(filename
, access_flags
,
110 FILE_SHARE_READ
, NULL
,
111 create_flags
, overlapped
, NULL
);
112 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
113 int err
= GetLastError();
115 if (err
== ERROR_ACCESS_DENIED
)
122 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
123 uint8_t *buf
, int count
)
125 BDRVRawState
*s
= bs
->opaque
;
130 memset(&ov
, 0, sizeof(ov
));
132 ov
.OffsetHigh
= offset
>> 32;
133 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
136 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
146 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
147 const uint8_t *buf
, int count
)
149 BDRVRawState
*s
= bs
->opaque
;
154 memset(&ov
, 0, sizeof(ov
));
156 ov
.OffsetHigh
= offset
>> 32;
157 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
160 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
171 static void raw_aio_cb(void *opaque
)
173 RawAIOCB
*acb
= opaque
;
174 BlockDriverState
*bs
= acb
->common
.bs
;
175 BDRVRawState
*s
= bs
->opaque
;
179 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
180 if (!ret
|| ret_count
!= acb
->count
) {
181 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
183 acb
->common
.cb(acb
->common
.opaque
, 0);
187 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
188 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
189 BlockDriverCompletionFunc
*cb
, void *opaque
)
194 acb
= qemu_aio_get(bs
, cb
, opaque
);
196 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
198 qemu_aio_release(acb
);
202 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
203 offset
= sector_num
* 512;
204 acb
->ov
.Offset
= offset
;
205 acb
->ov
.OffsetHigh
= offset
>> 32;
206 acb
->ov
.hEvent
= acb
->hEvent
;
207 acb
->count
= nb_sectors
* 512;
208 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
212 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
213 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
214 BlockDriverCompletionFunc
*cb
, void *opaque
)
216 BDRVRawState
*s
= bs
->opaque
;
220 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
223 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
225 qemu_aio_release(acb
);
228 qemu_aio_release(acb
);
229 return (BlockDriverAIOCB
*)acb
;
232 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
233 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
234 BlockDriverCompletionFunc
*cb
, void *opaque
)
236 BDRVRawState
*s
= bs
->opaque
;
240 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
243 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
245 qemu_aio_release(acb
);
248 qemu_aio_release(acb
);
249 return (BlockDriverAIOCB
*)acb
;
252 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
254 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
255 BlockDriverState
*bs
= acb
->common
.bs
;
256 BDRVRawState
*s
= bs
->opaque
;
258 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
259 /* XXX: if more than one async I/O it is not correct */
261 qemu_aio_release(acb
);
263 #endif /* #if WIN32_AIO */
265 static void raw_flush(BlockDriverState
*bs
)
267 BDRVRawState
*s
= bs
->opaque
;
268 FlushFileBuffers(s
->hfile
);
271 static void raw_close(BlockDriverState
*bs
)
273 BDRVRawState
*s
= bs
->opaque
;
274 CloseHandle(s
->hfile
);
277 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
279 BDRVRawState
*s
= bs
->opaque
;
284 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
286 if (!SetEndOfFile(s
->hfile
))
291 static int64_t raw_getlength(BlockDriverState
*bs
)
293 BDRVRawState
*s
= bs
->opaque
;
295 ULARGE_INTEGER available
, total
, total_free
;
302 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
303 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
307 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
309 l
.QuadPart
= total
.QuadPart
;
312 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
313 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
324 static int raw_create(const char *filename
, int64_t total_size
,
325 const char *backing_file
, int flags
)
329 if (flags
|| backing_file
)
332 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
337 ftruncate(fd
, total_size
* 512);
342 void qemu_aio_init(void)
346 void qemu_aio_flush(void)
350 void qemu_aio_wait(void)
355 BlockDriver bdrv_raw
= {
357 sizeof(BDRVRawState
),
358 NULL
, /* no probe for protocols */
367 .bdrv_aio_read
= raw_aio_read
,
368 .bdrv_aio_write
= raw_aio_write
,
369 .bdrv_aio_cancel
= raw_aio_cancel
,
370 .aiocb_size
= sizeof(RawAIOCB
);
372 .bdrv_pread
= raw_pread
,
373 .bdrv_pwrite
= raw_pwrite
,
374 .bdrv_truncate
= raw_truncate
,
375 .bdrv_getlength
= raw_getlength
,
378 /***********************************************/
381 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
383 char drives
[256], *pdrv
= drives
;
386 memset(drives
, 0, sizeof(drives
));
387 GetLogicalDriveStrings(sizeof(drives
), drives
);
388 while(pdrv
[0] != '\0') {
389 type
= GetDriveType(pdrv
);
392 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
396 pdrv
+= lstrlen(pdrv
) + 1;
401 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
403 BDRVRawState
*s
= bs
->opaque
;
407 if (strstart(filename
, "\\\\.\\", &p
) ||
408 strstart(filename
, "//./", &p
)) {
409 if (stristart(p
, "PhysicalDrive", NULL
))
410 return FTYPE_HARDDISK
;
411 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
412 type
= GetDriveType(s
->drive_path
);
413 if (type
== DRIVE_CDROM
)
422 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
424 BDRVRawState
*s
= bs
->opaque
;
425 int access_flags
, create_flags
;
427 char device_name
[64];
429 if (strstart(filename
, "/dev/cdrom", NULL
)) {
430 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
432 filename
= device_name
;
434 /* transform drive letters into device name */
435 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
436 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
437 filename
[1] == ':' && filename
[2] == '\0') {
438 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
439 filename
= device_name
;
442 s
->type
= find_device_type(bs
, filename
);
444 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
445 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
447 access_flags
= GENERIC_READ
;
449 create_flags
= OPEN_EXISTING
;
452 overlapped
= FILE_FLAG_OVERLAPPED
;
454 overlapped
= FILE_ATTRIBUTE_NORMAL
;
456 if (flags
& BDRV_O_DIRECT
)
457 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
458 s
->hfile
= CreateFile(filename
, access_flags
,
459 FILE_SHARE_READ
, NULL
,
460 create_flags
, overlapped
, NULL
);
461 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
462 int err
= GetLastError();
464 if (err
== ERROR_ACCESS_DENIED
)
472 /***********************************************/
473 /* removable device additional commands */
475 static int raw_is_inserted(BlockDriverState
*bs
)
480 static int raw_media_changed(BlockDriverState
*bs
)
485 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
489 if (s
->type
== FTYPE_FILE
)
492 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
493 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
495 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
496 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
500 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
506 BlockDriver bdrv_host_device
= {
508 sizeof(BDRVRawState
),
509 NULL
, /* no probe for protocols */
518 .bdrv_aio_read
= raw_aio_read
,
519 .bdrv_aio_write
= raw_aio_write
,
520 .bdrv_aio_cancel
= raw_aio_cancel
,
521 .aiocb_size
= sizeof(RawAIOCB
);
523 .bdrv_pread
= raw_pread
,
524 .bdrv_pwrite
= raw_pwrite
,
525 .bdrv_getlength
= raw_getlength
,