]> git.proxmox.com Git - mirror_qemu.git/blame - block-raw-win32.c
machine struct - use C99 initializers (Jes Sorensen)
[mirror_qemu.git] / block-raw-win32.c
CommitLineData
223d4670
TS
1/*
2 * Block driver for RAW files (win32)
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
223d4670 25#include "qemu-timer.h"
223d4670
TS
26#include "block_int.h"
27#include <assert.h>
28#include <winioctl.h>
29
03ff3ca3
AL
30//#define WIN32_AIO
31
223d4670
TS
32#define FTYPE_FILE 0
33#define FTYPE_CD 1
34#define FTYPE_HARDDISK 2
35
36typedef struct BDRVRawState {
37 HANDLE hfile;
38 int type;
39 char drive_path[16]; /* format: "d:\" */
40} BDRVRawState;
41
42typedef struct RawAIOCB {
43 BlockDriverAIOCB common;
44 HANDLE hEvent;
45 OVERLAPPED ov;
46 int count;
47} RawAIOCB;
48
49int qemu_ftruncate64(int fd, int64_t length)
50{
51 LARGE_INTEGER li;
52 LONG high;
53 HANDLE h;
54 BOOL res;
55
56 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
57 return -1;
58
59 h = (HANDLE)_get_osfhandle(fd);
60
61 /* get current position, ftruncate do not change position */
62 li.HighPart = 0;
63 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
64 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
65 return -1;
66
67 high = length >> 32;
68 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
69 return -1;
70 res = SetEndOfFile(h);
71
72 /* back to old position */
73 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
74 return res ? 0 : -1;
75}
76
77static int set_sparse(int fd)
78{
79 DWORD returned;
80 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
81 NULL, 0, NULL, 0, &returned, NULL);
82}
83
84static int raw_open(BlockDriverState *bs, const char *filename, int flags)
85{
86 BDRVRawState *s = bs->opaque;
87 int access_flags, create_flags;
88 DWORD overlapped;
89
90 s->type = FTYPE_FILE;
91
92 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
93 access_flags = GENERIC_READ | GENERIC_WRITE;
94 } else {
95 access_flags = GENERIC_READ;
96 }
97 if (flags & BDRV_O_CREAT) {
98 create_flags = CREATE_ALWAYS;
99 } else {
100 create_flags = OPEN_EXISTING;
101 }
03ff3ca3 102#ifdef WIN32_AIO
223d4670 103 overlapped = FILE_FLAG_OVERLAPPED;
03ff3ca3
AL
104#else
105 overlapped = FILE_ATTRIBUTE_NORMAL;
223d4670 106#endif
33f00271
AZ
107 if (flags & BDRV_O_DIRECT)
108 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
223d4670
TS
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();
114
115 if (err == ERROR_ACCESS_DENIED)
116 return -EACCES;
117 return -1;
118 }
119 return 0;
120}
121
122static int raw_pread(BlockDriverState *bs, int64_t offset,
123 uint8_t *buf, int count)
124{
125 BDRVRawState *s = bs->opaque;
126 OVERLAPPED ov;
127 DWORD ret_count;
128 int ret;
129
130 memset(&ov, 0, sizeof(ov));
131 ov.Offset = offset;
132 ov.OffsetHigh = offset >> 32;
133 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
134 if (!ret) {
03ff3ca3 135#ifdef WIN32_AIO
223d4670
TS
136 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
137 if (!ret)
138 return -EIO;
139 else
03ff3ca3 140#endif
223d4670
TS
141 return ret_count;
142 }
143 return ret_count;
144}
145
146static int raw_pwrite(BlockDriverState *bs, int64_t offset,
147 const uint8_t *buf, int count)
148{
149 BDRVRawState *s = bs->opaque;
150 OVERLAPPED ov;
151 DWORD ret_count;
152 int ret;
153
154 memset(&ov, 0, sizeof(ov));
155 ov.Offset = offset;
156 ov.OffsetHigh = offset >> 32;
157 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
158 if (!ret) {
03ff3ca3 159#ifdef WIN32_AIO
223d4670
TS
160 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
161 if (!ret)
162 return -EIO;
163 else
03ff3ca3 164#endif
223d4670
TS
165 return ret_count;
166 }
167 return ret_count;
168}
169
03ff3ca3 170#ifdef WIN32_AIO
223d4670
TS
171static void raw_aio_cb(void *opaque)
172{
173 RawAIOCB *acb = opaque;
174 BlockDriverState *bs = acb->common.bs;
175 BDRVRawState *s = bs->opaque;
176 DWORD ret_count;
177 int ret;
178
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);
182 } else {
183 acb->common.cb(acb->common.opaque, 0);
184 }
185}
223d4670
TS
186
187static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
188 int64_t sector_num, uint8_t *buf, int nb_sectors,
189 BlockDriverCompletionFunc *cb, void *opaque)
190{
191 RawAIOCB *acb;
192 int64_t offset;
193
194 acb = qemu_aio_get(bs, cb, opaque);
195 if (acb->hEvent) {
196 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
197 if (!acb->hEvent) {
198 qemu_aio_release(acb);
199 return NULL;
200 }
201 }
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;
223d4670 208 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
223d4670
TS
209 return acb;
210}
211
212static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
213 int64_t sector_num, uint8_t *buf, int nb_sectors,
214 BlockDriverCompletionFunc *cb, void *opaque)
215{
216 BDRVRawState *s = bs->opaque;
217 RawAIOCB *acb;
218 int ret;
219
220 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
221 if (!acb)
222 return NULL;
223 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
224 if (!ret) {
225 qemu_aio_release(acb);
226 return NULL;
227 }
223d4670 228 qemu_aio_release(acb);
223d4670
TS
229 return (BlockDriverAIOCB *)acb;
230}
231
232static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
233 int64_t sector_num, uint8_t *buf, int nb_sectors,
234 BlockDriverCompletionFunc *cb, void *opaque)
235{
236 BDRVRawState *s = bs->opaque;
237 RawAIOCB *acb;
238 int ret;
239
240 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
241 if (!acb)
242 return NULL;
243 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
244 if (!ret) {
245 qemu_aio_release(acb);
246 return NULL;
247 }
223d4670 248 qemu_aio_release(acb);
223d4670
TS
249 return (BlockDriverAIOCB *)acb;
250}
251
252static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
253{
223d4670
TS
254 RawAIOCB *acb = (RawAIOCB *)blockacb;
255 BlockDriverState *bs = acb->common.bs;
256 BDRVRawState *s = bs->opaque;
257
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 */
260 CancelIo(s->hfile);
261 qemu_aio_release(acb);
223d4670 262}
03ff3ca3 263#endif /* #if WIN32_AIO */
223d4670
TS
264
265static void raw_flush(BlockDriverState *bs)
266{
267 BDRVRawState *s = bs->opaque;
268 FlushFileBuffers(s->hfile);
269}
270
271static void raw_close(BlockDriverState *bs)
272{
273 BDRVRawState *s = bs->opaque;
274 CloseHandle(s->hfile);
275}
276
277static int raw_truncate(BlockDriverState *bs, int64_t offset)
278{
279 BDRVRawState *s = bs->opaque;
280 DWORD low, high;
281
282 low = offset;
283 high = offset >> 32;
284 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
285 return -EIO;
286 if (!SetEndOfFile(s->hfile))
287 return -EIO;
288 return 0;
289}
290
291static int64_t raw_getlength(BlockDriverState *bs)
292{
293 BDRVRawState *s = bs->opaque;
294 LARGE_INTEGER l;
295 ULARGE_INTEGER available, total, total_free;
296 DISK_GEOMETRY_EX dg;
297 DWORD count;
298 BOOL status;
299
300 switch(s->type) {
301 case FTYPE_FILE:
302 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
303 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
304 return -EIO;
305 break;
306 case FTYPE_CD:
307 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
308 return -EIO;
309 l.QuadPart = total.QuadPart;
310 break;
311 case FTYPE_HARDDISK:
312 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
313 NULL, 0, &dg, sizeof(dg), &count, NULL);
314 if (status != 0) {
315 l = dg.DiskSize;
316 }
317 break;
318 default:
319 return -EIO;
320 }
321 return l.QuadPart;
322}
323
324static int raw_create(const char *filename, int64_t total_size,
325 const char *backing_file, int flags)
326{
327 int fd;
328
329 if (flags || backing_file)
330 return -ENOTSUP;
331
332 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
333 0644);
334 if (fd < 0)
335 return -EIO;
336 set_sparse(fd);
337 ftruncate(fd, total_size * 512);
338 close(fd);
339 return 0;
340}
341
223d4670
TS
342BlockDriver bdrv_raw = {
343 "raw",
344 sizeof(BDRVRawState),
345 NULL, /* no probe for protocols */
346 raw_open,
347 NULL,
348 NULL,
349 raw_close,
350 raw_create,
351 raw_flush,
352
03ff3ca3 353#ifdef WIN32_AIO
223d4670
TS
354 .bdrv_aio_read = raw_aio_read,
355 .bdrv_aio_write = raw_aio_write,
356 .bdrv_aio_cancel = raw_aio_cancel,
357 .aiocb_size = sizeof(RawAIOCB);
358#endif
223d4670
TS
359 .bdrv_pread = raw_pread,
360 .bdrv_pwrite = raw_pwrite,
361 .bdrv_truncate = raw_truncate,
362 .bdrv_getlength = raw_getlength,
363};
364
365/***********************************************/
366/* host device */
367
368static int find_cdrom(char *cdrom_name, int cdrom_name_size)
369{
370 char drives[256], *pdrv = drives;
371 UINT type;
372
373 memset(drives, 0, sizeof(drives));
374 GetLogicalDriveStrings(sizeof(drives), drives);
375 while(pdrv[0] != '\0') {
376 type = GetDriveType(pdrv);
377 switch(type) {
378 case DRIVE_CDROM:
379 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
380 return 0;
381 break;
382 }
383 pdrv += lstrlen(pdrv) + 1;
384 }
385 return -1;
386}
387
388static int find_device_type(BlockDriverState *bs, const char *filename)
389{
390 BDRVRawState *s = bs->opaque;
391 UINT type;
392 const char *p;
393
394 if (strstart(filename, "\\\\.\\", &p) ||
395 strstart(filename, "//./", &p)) {
396 if (stristart(p, "PhysicalDrive", NULL))
397 return FTYPE_HARDDISK;
398 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
399 type = GetDriveType(s->drive_path);
400 if (type == DRIVE_CDROM)
401 return FTYPE_CD;
402 else
403 return FTYPE_FILE;
404 } else {
405 return FTYPE_FILE;
406 }
407}
408
409static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
410{
411 BDRVRawState *s = bs->opaque;
412 int access_flags, create_flags;
413 DWORD overlapped;
414 char device_name[64];
415
416 if (strstart(filename, "/dev/cdrom", NULL)) {
417 if (find_cdrom(device_name, sizeof(device_name)) < 0)
418 return -ENOENT;
419 filename = device_name;
420 } else {
421 /* transform drive letters into device name */
422 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
423 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
424 filename[1] == ':' && filename[2] == '\0') {
425 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
426 filename = device_name;
427 }
428 }
429 s->type = find_device_type(bs, filename);
430
431 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
432 access_flags = GENERIC_READ | GENERIC_WRITE;
433 } else {
434 access_flags = GENERIC_READ;
435 }
436 create_flags = OPEN_EXISTING;
437
03ff3ca3 438#ifdef WIN32_AIO
223d4670 439 overlapped = FILE_FLAG_OVERLAPPED;
03ff3ca3
AL
440#else
441 overlapped = FILE_ATTRIBUTE_NORMAL;
223d4670 442#endif
33f00271
AZ
443 if (flags & BDRV_O_DIRECT)
444 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
223d4670
TS
445 s->hfile = CreateFile(filename, access_flags,
446 FILE_SHARE_READ, NULL,
447 create_flags, overlapped, NULL);
448 if (s->hfile == INVALID_HANDLE_VALUE) {
449 int err = GetLastError();
450
451 if (err == ERROR_ACCESS_DENIED)
452 return -EACCES;
453 return -1;
454 }
455 return 0;
456}
457
458#if 0
459/***********************************************/
460/* removable device additional commands */
461
462static int raw_is_inserted(BlockDriverState *bs)
463{
464 return 1;
465}
466
467static int raw_media_changed(BlockDriverState *bs)
468{
469 return -ENOTSUP;
470}
471
472static int raw_eject(BlockDriverState *bs, int eject_flag)
473{
474 DWORD ret_count;
475
476 if (s->type == FTYPE_FILE)
477 return -ENOTSUP;
478 if (eject_flag) {
479 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
480 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
481 } else {
482 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
483 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
484 }
485}
486
487static int raw_set_locked(BlockDriverState *bs, int locked)
488{
489 return -ENOTSUP;
490}
491#endif
492
493BlockDriver bdrv_host_device = {
494 "host_device",
495 sizeof(BDRVRawState),
496 NULL, /* no probe for protocols */
497 hdev_open,
498 NULL,
499 NULL,
500 raw_close,
501 NULL,
502 raw_flush,
503
03ff3ca3 504#ifdef WIN32_AIO
223d4670
TS
505 .bdrv_aio_read = raw_aio_read,
506 .bdrv_aio_write = raw_aio_write,
507 .bdrv_aio_cancel = raw_aio_cancel,
508 .aiocb_size = sizeof(RawAIOCB);
509#endif
510 .bdrv_pread = raw_pread,
511 .bdrv_pwrite = raw_pwrite,
512 .bdrv_getlength = raw_getlength,
513};