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_NOCACHE
))
108 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
109 else if (!(flags
& BDRV_O_CACHE_WB
))
110 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
111 s
->hfile
= CreateFile(filename
, access_flags
,
112 FILE_SHARE_READ
, NULL
,
113 create_flags
, overlapped
, NULL
);
114 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
115 int err
= GetLastError();
117 if (err
== ERROR_ACCESS_DENIED
)
124 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
125 uint8_t *buf
, int count
)
127 BDRVRawState
*s
= bs
->opaque
;
132 memset(&ov
, 0, sizeof(ov
));
134 ov
.OffsetHigh
= offset
>> 32;
135 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
138 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
148 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
149 const uint8_t *buf
, int count
)
151 BDRVRawState
*s
= bs
->opaque
;
156 memset(&ov
, 0, sizeof(ov
));
158 ov
.OffsetHigh
= offset
>> 32;
159 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
162 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
173 static void raw_aio_cb(void *opaque
)
175 RawAIOCB
*acb
= opaque
;
176 BlockDriverState
*bs
= acb
->common
.bs
;
177 BDRVRawState
*s
= bs
->opaque
;
181 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
182 if (!ret
|| ret_count
!= acb
->count
) {
183 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
185 acb
->common
.cb(acb
->common
.opaque
, 0);
189 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
190 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
191 BlockDriverCompletionFunc
*cb
, void *opaque
)
196 acb
= qemu_aio_get(bs
, cb
, opaque
);
198 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
200 qemu_aio_release(acb
);
204 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
205 offset
= sector_num
* 512;
206 acb
->ov
.Offset
= offset
;
207 acb
->ov
.OffsetHigh
= offset
>> 32;
208 acb
->ov
.hEvent
= acb
->hEvent
;
209 acb
->count
= nb_sectors
* 512;
210 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
214 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
215 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
216 BlockDriverCompletionFunc
*cb
, void *opaque
)
218 BDRVRawState
*s
= bs
->opaque
;
222 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
225 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
227 qemu_aio_release(acb
);
230 qemu_aio_release(acb
);
231 return (BlockDriverAIOCB
*)acb
;
234 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
235 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
236 BlockDriverCompletionFunc
*cb
, void *opaque
)
238 BDRVRawState
*s
= bs
->opaque
;
242 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
245 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
247 qemu_aio_release(acb
);
250 qemu_aio_release(acb
);
251 return (BlockDriverAIOCB
*)acb
;
254 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
256 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
257 BlockDriverState
*bs
= acb
->common
.bs
;
258 BDRVRawState
*s
= bs
->opaque
;
260 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
261 /* XXX: if more than one async I/O it is not correct */
263 qemu_aio_release(acb
);
265 #endif /* #if WIN32_AIO */
267 static void raw_flush(BlockDriverState
*bs
)
269 BDRVRawState
*s
= bs
->opaque
;
270 FlushFileBuffers(s
->hfile
);
273 static void raw_close(BlockDriverState
*bs
)
275 BDRVRawState
*s
= bs
->opaque
;
276 CloseHandle(s
->hfile
);
279 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
281 BDRVRawState
*s
= bs
->opaque
;
286 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
288 if (!SetEndOfFile(s
->hfile
))
293 static int64_t raw_getlength(BlockDriverState
*bs
)
295 BDRVRawState
*s
= bs
->opaque
;
297 ULARGE_INTEGER available
, total
, total_free
;
304 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
305 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
309 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
311 l
.QuadPart
= total
.QuadPart
;
314 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
315 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
326 static int raw_create(const char *filename
, int64_t total_size
,
327 const char *backing_file
, int flags
)
331 if (flags
|| backing_file
)
334 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
339 ftruncate(fd
, total_size
* 512);
344 BlockDriver bdrv_raw
= {
346 sizeof(BDRVRawState
),
347 NULL
, /* no probe for protocols */
356 .bdrv_aio_read
= raw_aio_read
,
357 .bdrv_aio_write
= raw_aio_write
,
358 .bdrv_aio_cancel
= raw_aio_cancel
,
359 .aiocb_size
= sizeof(RawAIOCB
);
361 .bdrv_pread
= raw_pread
,
362 .bdrv_pwrite
= raw_pwrite
,
363 .bdrv_truncate
= raw_truncate
,
364 .bdrv_getlength
= raw_getlength
,
367 /***********************************************/
370 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
372 char drives
[256], *pdrv
= drives
;
375 memset(drives
, 0, sizeof(drives
));
376 GetLogicalDriveStrings(sizeof(drives
), drives
);
377 while(pdrv
[0] != '\0') {
378 type
= GetDriveType(pdrv
);
381 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
385 pdrv
+= lstrlen(pdrv
) + 1;
390 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
392 BDRVRawState
*s
= bs
->opaque
;
396 if (strstart(filename
, "\\\\.\\", &p
) ||
397 strstart(filename
, "//./", &p
)) {
398 if (stristart(p
, "PhysicalDrive", NULL
))
399 return FTYPE_HARDDISK
;
400 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
401 type
= GetDriveType(s
->drive_path
);
403 case DRIVE_REMOVABLE
:
405 return FTYPE_HARDDISK
;
416 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
418 BDRVRawState
*s
= bs
->opaque
;
419 int access_flags
, create_flags
;
421 char device_name
[64];
423 if (strstart(filename
, "/dev/cdrom", NULL
)) {
424 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
426 filename
= device_name
;
428 /* transform drive letters into device name */
429 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
430 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
431 filename
[1] == ':' && filename
[2] == '\0') {
432 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
433 filename
= device_name
;
436 s
->type
= find_device_type(bs
, filename
);
438 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
439 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
441 access_flags
= GENERIC_READ
;
443 create_flags
= OPEN_EXISTING
;
446 overlapped
= FILE_FLAG_OVERLAPPED
;
448 overlapped
= FILE_ATTRIBUTE_NORMAL
;
450 if ((flags
& BDRV_O_NOCACHE
))
451 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
452 else if (!(flags
& BDRV_O_CACHE_WB
))
453 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
454 s
->hfile
= CreateFile(filename
, access_flags
,
455 FILE_SHARE_READ
, NULL
,
456 create_flags
, overlapped
, NULL
);
457 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
458 int err
= GetLastError();
460 if (err
== ERROR_ACCESS_DENIED
)
468 /***********************************************/
469 /* removable device additional commands */
471 static int raw_is_inserted(BlockDriverState
*bs
)
476 static int raw_media_changed(BlockDriverState
*bs
)
481 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
485 if (s
->type
== FTYPE_FILE
)
488 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
489 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
491 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
492 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
496 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
502 BlockDriver bdrv_host_device
= {
504 sizeof(BDRVRawState
),
505 NULL
, /* no probe for protocols */
514 .bdrv_aio_read
= raw_aio_read
,
515 .bdrv_aio_write
= raw_aio_write
,
516 .bdrv_aio_cancel
= raw_aio_cancel
,
517 .aiocb_size
= sizeof(RawAIOCB
);
519 .bdrv_pread
= raw_pread
,
520 .bdrv_pwrite
= raw_pwrite
,
521 .bdrv_getlength
= raw_getlength
,