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 if (flags
& BDRV_O_DIRECT
)
109 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
110 s
->hfile
= CreateFile(filename
, access_flags
,
111 FILE_SHARE_READ
, NULL
,
112 create_flags
, overlapped
, NULL
);
113 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
114 int err
= GetLastError();
116 if (err
== ERROR_ACCESS_DENIED
)
123 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
124 uint8_t *buf
, int count
)
126 BDRVRawState
*s
= bs
->opaque
;
131 memset(&ov
, 0, sizeof(ov
));
133 ov
.OffsetHigh
= offset
>> 32;
134 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
136 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
145 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
146 const uint8_t *buf
, int count
)
148 BDRVRawState
*s
= bs
->opaque
;
153 memset(&ov
, 0, sizeof(ov
));
155 ov
.OffsetHigh
= offset
>> 32;
156 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
158 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
169 static void raw_aio_cb(void *opaque
)
171 RawAIOCB
*acb
= opaque
;
172 BlockDriverState
*bs
= acb
->common
.bs
;
173 BDRVRawState
*s
= bs
->opaque
;
177 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
178 if (!ret
|| ret_count
!= acb
->count
) {
179 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
181 acb
->common
.cb(acb
->common
.opaque
, 0);
186 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
187 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
188 BlockDriverCompletionFunc
*cb
, void *opaque
)
193 acb
= qemu_aio_get(bs
, cb
, opaque
);
195 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
197 qemu_aio_release(acb
);
201 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
202 offset
= sector_num
* 512;
203 acb
->ov
.Offset
= offset
;
204 acb
->ov
.OffsetHigh
= offset
>> 32;
205 acb
->ov
.hEvent
= acb
->hEvent
;
206 acb
->count
= nb_sectors
* 512;
208 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
213 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
214 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
215 BlockDriverCompletionFunc
*cb
, void *opaque
)
217 BDRVRawState
*s
= bs
->opaque
;
221 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
224 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
226 qemu_aio_release(acb
);
230 qemu_aio_release(acb
);
232 return (BlockDriverAIOCB
*)acb
;
235 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
236 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
237 BlockDriverCompletionFunc
*cb
, void *opaque
)
239 BDRVRawState
*s
= bs
->opaque
;
243 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
246 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
248 qemu_aio_release(acb
);
252 qemu_aio_release(acb
);
254 return (BlockDriverAIOCB
*)acb
;
257 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
260 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
261 BlockDriverState
*bs
= acb
->common
.bs
;
262 BDRVRawState
*s
= bs
->opaque
;
264 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
265 /* XXX: if more than one async I/O it is not correct */
267 qemu_aio_release(acb
);
272 static void raw_flush(BlockDriverState
*bs
)
274 BDRVRawState
*s
= bs
->opaque
;
275 FlushFileBuffers(s
->hfile
);
278 static void raw_close(BlockDriverState
*bs
)
280 BDRVRawState
*s
= bs
->opaque
;
281 CloseHandle(s
->hfile
);
284 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
286 BDRVRawState
*s
= bs
->opaque
;
291 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
293 if (!SetEndOfFile(s
->hfile
))
298 static int64_t raw_getlength(BlockDriverState
*bs
)
300 BDRVRawState
*s
= bs
->opaque
;
302 ULARGE_INTEGER available
, total
, total_free
;
309 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
310 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
314 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
316 l
.QuadPart
= total
.QuadPart
;
319 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
320 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
331 static int raw_create(const char *filename
, int64_t total_size
,
332 const char *backing_file
, int flags
)
336 if (flags
|| backing_file
)
339 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
344 ftruncate(fd
, total_size
* 512);
349 void qemu_aio_init(void)
353 void qemu_aio_flush(void)
357 void qemu_aio_wait(void)
364 BlockDriver bdrv_raw
= {
366 sizeof(BDRVRawState
),
367 NULL
, /* no probe for protocols */
376 .bdrv_aio_read
= raw_aio_read
,
377 .bdrv_aio_write
= raw_aio_write
,
378 .bdrv_aio_cancel
= raw_aio_cancel
,
379 .aiocb_size
= sizeof(RawAIOCB
);
381 .protocol_name
= "file",
382 .bdrv_pread
= raw_pread
,
383 .bdrv_pwrite
= raw_pwrite
,
384 .bdrv_truncate
= raw_truncate
,
385 .bdrv_getlength
= raw_getlength
,
388 /***********************************************/
391 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
393 char drives
[256], *pdrv
= drives
;
396 memset(drives
, 0, sizeof(drives
));
397 GetLogicalDriveStrings(sizeof(drives
), drives
);
398 while(pdrv
[0] != '\0') {
399 type
= GetDriveType(pdrv
);
402 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
406 pdrv
+= lstrlen(pdrv
) + 1;
411 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
413 BDRVRawState
*s
= bs
->opaque
;
417 if (strstart(filename
, "\\\\.\\", &p
) ||
418 strstart(filename
, "//./", &p
)) {
419 if (stristart(p
, "PhysicalDrive", NULL
))
420 return FTYPE_HARDDISK
;
421 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
422 type
= GetDriveType(s
->drive_path
);
423 if (type
== DRIVE_CDROM
)
432 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
434 BDRVRawState
*s
= bs
->opaque
;
435 int access_flags
, create_flags
;
437 char device_name
[64];
439 if (strstart(filename
, "/dev/cdrom", NULL
)) {
440 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
442 filename
= device_name
;
444 /* transform drive letters into device name */
445 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
446 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
447 filename
[1] == ':' && filename
[2] == '\0') {
448 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
449 filename
= device_name
;
452 s
->type
= find_device_type(bs
, filename
);
454 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
455 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
457 access_flags
= GENERIC_READ
;
459 create_flags
= OPEN_EXISTING
;
462 overlapped
= FILE_ATTRIBUTE_NORMAL
;
464 overlapped
= FILE_FLAG_OVERLAPPED
;
466 if (flags
& BDRV_O_DIRECT
)
467 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
468 s
->hfile
= CreateFile(filename
, access_flags
,
469 FILE_SHARE_READ
, NULL
,
470 create_flags
, overlapped
, NULL
);
471 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
472 int err
= GetLastError();
474 if (err
== ERROR_ACCESS_DENIED
)
482 /***********************************************/
483 /* removable device additional commands */
485 static int raw_is_inserted(BlockDriverState
*bs
)
490 static int raw_media_changed(BlockDriverState
*bs
)
495 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
499 if (s
->type
== FTYPE_FILE
)
502 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
503 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
505 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
506 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
510 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
516 BlockDriver bdrv_host_device
= {
518 sizeof(BDRVRawState
),
519 NULL
, /* no probe for protocols */
528 .bdrv_aio_read
= raw_aio_read
,
529 .bdrv_aio_write
= raw_aio_write
,
530 .bdrv_aio_cancel
= raw_aio_cancel
,
531 .aiocb_size
= sizeof(RawAIOCB
);
533 .bdrv_pread
= raw_pread
,
534 .bdrv_pwrite
= raw_pwrite
,
535 .bdrv_getlength
= raw_getlength
,