]> git.proxmox.com Git - mirror_qemu.git/blame - block-raw.c
better support of removable media
[mirror_qemu.git] / block-raw.c
CommitLineData
83f64091
FB
1/*
2 * Block driver for RAW files
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 "vl.h"
25#include "block_int.h"
26#include <assert.h>
27#ifndef _WIN32
28#include <aio.h>
29
30#ifndef QEMU_TOOL
31#include "exec-all.h"
32#endif
33
34#ifdef CONFIG_COCOA
35#include <paths.h>
36#include <sys/param.h>
37#include <IOKit/IOKitLib.h>
38#include <IOKit/IOBSD.h>
39#include <IOKit/storage/IOMediaBSDClient.h>
40#include <IOKit/storage/IOMedia.h>
41#include <IOKit/storage/IOCDMedia.h>
42//#include <IOKit/storage/IOCDTypes.h>
43#include <CoreFoundation/CoreFoundation.h>
44#endif
45
46#ifdef __sun__
47#include <sys/dkio.h>
48#endif
49
50typedef struct BDRVRawState {
51 int fd;
52} BDRVRawState;
53
54#ifdef CONFIG_COCOA
55static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
56static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
57
58kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
59{
60 kern_return_t kernResult;
61 mach_port_t masterPort;
62 CFMutableDictionaryRef classesToMatch;
63
64 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
65 if ( KERN_SUCCESS != kernResult ) {
66 printf( "IOMasterPort returned %d\n", kernResult );
67 }
68
69 classesToMatch = IOServiceMatching( kIOCDMediaClass );
70 if ( classesToMatch == NULL ) {
71 printf( "IOServiceMatching returned a NULL dictionary.\n" );
72 } else {
73 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
74 }
75 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
76 if ( KERN_SUCCESS != kernResult )
77 {
78 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
79 }
80
81 return kernResult;
82}
83
84kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
85{
86 io_object_t nextMedia;
87 kern_return_t kernResult = KERN_FAILURE;
88 *bsdPath = '\0';
89 nextMedia = IOIteratorNext( mediaIterator );
90 if ( nextMedia )
91 {
92 CFTypeRef bsdPathAsCFString;
93 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
94 if ( bsdPathAsCFString ) {
95 size_t devPathLength;
96 strcpy( bsdPath, _PATH_DEV );
97 strcat( bsdPath, "r" );
98 devPathLength = strlen( bsdPath );
99 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
100 kernResult = KERN_SUCCESS;
101 }
102 CFRelease( bsdPathAsCFString );
103 }
104 IOObjectRelease( nextMedia );
105 }
106
107 return kernResult;
108}
109
110#endif
111
112static int raw_open(BlockDriverState *bs, const char *filename, int flags)
113{
114 BDRVRawState *s = bs->opaque;
115 int fd, open_flags;
116
117#ifdef CONFIG_COCOA
118 if (strstart(filename, "/dev/cdrom", NULL)) {
119 kern_return_t kernResult;
120 io_iterator_t mediaIterator;
121 char bsdPath[ MAXPATHLEN ];
122 int fd;
123
124 kernResult = FindEjectableCDMedia( &mediaIterator );
125 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
126
127 if ( bsdPath[ 0 ] != '\0' ) {
128 strcat(bsdPath,"s0");
129 /* some CDs don't have a partition 0 */
130 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
131 if (fd < 0) {
132 bsdPath[strlen(bsdPath)-1] = '1';
133 } else {
134 close(fd);
135 }
136 filename = bsdPath;
137 }
138
139 if ( mediaIterator )
140 IOObjectRelease( mediaIterator );
141 }
142#endif
143 open_flags = O_BINARY;
144 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
145 open_flags |= O_RDWR;
146 } else {
147 open_flags |= O_RDONLY;
148 bs->read_only = 1;
149 }
150 if (flags & BDRV_O_CREAT)
151 open_flags |= O_CREAT | O_TRUNC;
152
153 fd = open(filename, open_flags, 0644);
154 if (fd < 0)
155 return -errno;
156 s->fd = fd;
157 return 0;
158}
159
160/* XXX: use host sector size if necessary with:
161#ifdef DIOCGSECTORSIZE
162 {
163 unsigned int sectorsize = 512;
164 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
165 sectorsize > bufsize)
166 bufsize = sectorsize;
167 }
168#endif
169#ifdef CONFIG_COCOA
170 u_int32_t blockSize = 512;
171 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
172 bufsize = blockSize;
173 }
174#endif
175*/
176
177static int raw_pread(BlockDriverState *bs, int64_t offset,
178 uint8_t *buf, int count)
179{
180 BDRVRawState *s = bs->opaque;
181 int ret;
182
183 lseek(s->fd, offset, SEEK_SET);
184 ret = read(s->fd, buf, count);
185 return ret;
186}
187
188static int raw_pwrite(BlockDriverState *bs, int64_t offset,
189 const uint8_t *buf, int count)
190{
191 BDRVRawState *s = bs->opaque;
192 int ret;
193
194 lseek(s->fd, offset, SEEK_SET);
195 ret = write(s->fd, buf, count);
196 return ret;
197}
198
199/***********************************************************/
200/* Unix AOP using POSIX AIO */
201
202typedef struct RawAIOCB {
ce1a14dc 203 BlockDriverAIOCB common;
83f64091 204 struct aiocb aiocb;
ce1a14dc 205 struct RawAIOCB *next;
83f64091
FB
206} RawAIOCB;
207
208static int aio_sig_num = SIGUSR2;
ce1a14dc 209static RawAIOCB *first_aio; /* AIO issued */
979b67ad 210static int aio_initialized = 0;
83f64091 211
83f64091
FB
212static void aio_signal_handler(int signum)
213{
979b67ad 214#ifndef QEMU_TOOL
83f64091
FB
215 CPUState *env = cpu_single_env;
216 if (env) {
217 /* stop the currently executing cpu because a timer occured */
218 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
219#ifdef USE_KQEMU
220 if (env->kqemu_enabled) {
221 kqemu_cpu_interrupt(env);
222 }
223#endif
224 }
979b67ad 225#endif
83f64091
FB
226}
227
228void qemu_aio_init(void)
229{
230 struct sigaction act;
979b67ad
FB
231
232 aio_initialized = 1;
83f64091
FB
233
234 sigfillset(&act.sa_mask);
235 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
236 act.sa_handler = aio_signal_handler;
237 sigaction(aio_sig_num, &act, NULL);
238
239 {
240 /* XXX: aio thread exit seems to hang on RH 9 */
241 struct aioinit ai;
242 memset(&ai, 0, sizeof(ai));
243 ai.aio_threads = 2;
244 ai.aio_num = 1;
245 ai.aio_idle_time = 365 * 100000;
246 aio_init(&ai);
247 }
248}
83f64091
FB
249
250void qemu_aio_poll(void)
251{
ce1a14dc 252 RawAIOCB *acb, **pacb;
83f64091
FB
253 int ret;
254
255 for(;;) {
256 pacb = &first_aio;
257 for(;;) {
258 acb = *pacb;
259 if (!acb)
260 goto the_end;
ce1a14dc 261 ret = aio_error(&acb->aiocb);
83f64091
FB
262 if (ret == ECANCELED) {
263 /* remove the request */
ce1a14dc
PB
264 *pacb = acb->next;
265 qemu_aio_release(acb);
83f64091
FB
266 } else if (ret != EINPROGRESS) {
267 /* end of aio */
268 if (ret == 0) {
ce1a14dc
PB
269 ret = aio_return(&acb->aiocb);
270 if (ret == acb->aiocb.aio_nbytes)
83f64091
FB
271 ret = 0;
272 else
273 ret = -1;
274 } else {
275 ret = -ret;
276 }
277 /* remove the request */
ce1a14dc 278 *pacb = acb->next;
83f64091 279 /* call the callback */
ce1a14dc
PB
280 acb->common.cb(acb->common.opaque, ret);
281 qemu_aio_release(acb);
83f64091
FB
282 break;
283 } else {
ce1a14dc 284 pacb = &acb->next;
83f64091
FB
285 }
286 }
287 }
288 the_end: ;
289}
290
291/* wait until at least one AIO was handled */
292static sigset_t wait_oset;
293
294void qemu_aio_wait_start(void)
295{
296 sigset_t set;
979b67ad
FB
297
298 if (!aio_initialized)
299 qemu_aio_init();
83f64091
FB
300 sigemptyset(&set);
301 sigaddset(&set, aio_sig_num);
302 sigprocmask(SIG_BLOCK, &set, &wait_oset);
303}
304
305void qemu_aio_wait(void)
306{
307 sigset_t set;
308 int nb_sigs;
6eb5733a
FB
309
310#ifndef QEMU_TOOL
311 if (qemu_bh_poll())
312 return;
313#endif
83f64091
FB
314 sigemptyset(&set);
315 sigaddset(&set, aio_sig_num);
316 sigwait(&set, &nb_sigs);
317 qemu_aio_poll();
318}
319
320void qemu_aio_wait_end(void)
321{
322 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
323}
324
ce1a14dc
PB
325static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
326 int64_t sector_num, uint8_t *buf, int nb_sectors,
327 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 328{
ce1a14dc
PB
329 BDRVRawState *s = bs->opaque;
330 RawAIOCB *acb;
331
332 acb = qemu_aio_get(bs, cb, opaque);
333 if (!acb)
334 return NULL;
335 acb->aiocb.aio_fildes = s->fd;
336 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
337 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
338 acb->aiocb.aio_buf = buf;
339 acb->aiocb.aio_nbytes = nb_sectors * 512;
340 acb->aiocb.aio_offset = sector_num * 512;
341 acb->next = first_aio;
342 first_aio = acb;
343 return acb;
83f64091
FB
344}
345
ce1a14dc
PB
346static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
347 int64_t sector_num, uint8_t *buf, int nb_sectors,
348 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 349{
ce1a14dc 350 RawAIOCB *acb;
83f64091 351
ce1a14dc
PB
352 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
353 if (!acb)
354 return NULL;
355 if (aio_read(&acb->aiocb) < 0) {
356 qemu_aio_release(acb);
357 return NULL;
83f64091 358 }
ce1a14dc 359 return &acb->common;
83f64091
FB
360}
361
ce1a14dc
PB
362static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
363 int64_t sector_num, const uint8_t *buf, int nb_sectors,
364 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 365{
ce1a14dc 366 RawAIOCB *acb;
83f64091 367
ce1a14dc
PB
368 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
369 if (!acb)
370 return NULL;
371 if (aio_write(&acb->aiocb) < 0) {
372 qemu_aio_release(acb);
373 return NULL;
83f64091 374 }
ce1a14dc 375 return &acb->common;
83f64091
FB
376}
377
ce1a14dc 378static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
83f64091 379{
83f64091 380 int ret;
ce1a14dc
PB
381 RawAIOCB *acb = (RawAIOCB *)blockacb;
382 RawAIOCB **pacb;
83f64091 383
ce1a14dc 384 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
83f64091
FB
385 if (ret == AIO_NOTCANCELED) {
386 /* fail safe: if the aio could not be canceled, we wait for
387 it */
ce1a14dc 388 while (aio_error(&acb->aiocb) == EINPROGRESS);
83f64091
FB
389 }
390
391 /* remove the callback from the queue */
392 pacb = &first_aio;
393 for(;;) {
394 if (*pacb == NULL) {
395 break;
396 } else if (*pacb == acb) {
ce1a14dc
PB
397 *pacb = acb->next;
398 qemu_aio_release(acb);
83f64091
FB
399 break;
400 }
ce1a14dc 401 pacb = &acb->next;
83f64091
FB
402 }
403}
404
83f64091
FB
405static void raw_close(BlockDriverState *bs)
406{
407 BDRVRawState *s = bs->opaque;
408 close(s->fd);
409}
410
411static int raw_truncate(BlockDriverState *bs, int64_t offset)
412{
413 BDRVRawState *s = bs->opaque;
414 if (ftruncate(s->fd, offset) < 0)
415 return -errno;
416 return 0;
417}
418
419static int64_t raw_getlength(BlockDriverState *bs)
420{
421 BDRVRawState *s = bs->opaque;
422 int fd = s->fd;
423 int64_t size;
424#ifdef _BSD
425 struct stat sb;
426#endif
427#ifdef __sun__
428 struct dk_minfo minfo;
429 int rv;
430#endif
431
432#ifdef _BSD
433 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
434#ifdef DIOCGMEDIASIZE
435 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
436#endif
437#ifdef CONFIG_COCOA
438 size = LONG_LONG_MAX;
439#else
440 size = lseek(fd, 0LL, SEEK_END);
441#endif
442 } else
443#endif
444#ifdef __sun__
445 /*
446 * use the DKIOCGMEDIAINFO ioctl to read the size.
447 */
448 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
449 if ( rv != -1 ) {
450 size = minfo.dki_lbsize * minfo.dki_capacity;
451 } else /* there are reports that lseek on some devices
452 fails, but irc discussion said that contingency
453 on contingency was overkill */
454#endif
455 {
456 size = lseek(fd, 0, SEEK_END);
457 }
458#ifdef _WIN32
459 /* On Windows hosts it can happen that we're unable to get file size
460 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
461 if (size == -1)
462 size = LONG_LONG_MAX;
463#endif
464 return size;
465}
466
467static int raw_create(const char *filename, int64_t total_size,
468 const char *backing_file, int flags)
469{
470 int fd;
471
472 if (flags || backing_file)
473 return -ENOTSUP;
474
9ab8a34a 475 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
83f64091
FB
476 0644);
477 if (fd < 0)
478 return -EIO;
479 ftruncate(fd, total_size * 512);
480 close(fd);
481 return 0;
482}
483
484static void raw_flush(BlockDriverState *bs)
485{
486 BDRVRawState *s = bs->opaque;
487 fsync(s->fd);
488}
489
490BlockDriver bdrv_raw = {
491 "raw",
492 sizeof(BDRVRawState),
493 NULL, /* no probe for protocols */
494 raw_open,
495 NULL,
496 NULL,
497 raw_close,
498 raw_create,
499 raw_flush,
500
83f64091
FB
501 .bdrv_aio_read = raw_aio_read,
502 .bdrv_aio_write = raw_aio_write,
503 .bdrv_aio_cancel = raw_aio_cancel,
ce1a14dc 504 .aiocb_size = sizeof(RawAIOCB),
83f64091
FB
505 .protocol_name = "file",
506 .bdrv_pread = raw_pread,
507 .bdrv_pwrite = raw_pwrite,
508 .bdrv_truncate = raw_truncate,
509 .bdrv_getlength = raw_getlength,
510};
511
512#else /* _WIN32 */
513
514/* XXX: use another file ? */
83f64091
FB
515#include <winioctl.h>
516
517typedef struct BDRVRawState {
518 HANDLE hfile;
519} BDRVRawState;
520
521typedef struct RawAIOCB {
ce1a14dc 522 BlockDriverAIOCB common;
83f64091
FB
523 HANDLE hEvent;
524 OVERLAPPED ov;
525 int count;
526} RawAIOCB;
527
528int qemu_ftruncate64(int fd, int64_t length)
529{
530 LARGE_INTEGER li;
531 LONG high;
532 HANDLE h;
533 BOOL res;
534
535 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
536 return -1;
537
538 h = (HANDLE)_get_osfhandle(fd);
539
540 /* get current position, ftruncate do not change position */
541 li.HighPart = 0;
542 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
543 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
544 return -1;
545
546 high = length >> 32;
547 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
548 return -1;
549 res = SetEndOfFile(h);
550
551 /* back to old position */
552 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
553 return res ? 0 : -1;
554}
555
556static int set_sparse(int fd)
557{
558 DWORD returned;
559 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
560 NULL, 0, NULL, 0, &returned, NULL);
561}
562
563static int raw_open(BlockDriverState *bs, const char *filename, int flags)
564{
565 BDRVRawState *s = bs->opaque;
566 int access_flags, create_flags;
ce1a14dc 567 DWORD overlapped;
83f64091
FB
568
569 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
570 access_flags = GENERIC_READ | GENERIC_WRITE;
571 } else {
572 access_flags = GENERIC_READ;
573 }
979b67ad 574 if (flags & BDRV_O_CREAT) {
83f64091
FB
575 create_flags = CREATE_ALWAYS;
576 } else {
577 create_flags = OPEN_EXISTING;
578 }
ce1a14dc
PB
579#ifdef QEMU_TOOL
580 overlapped = 0;
581#else
582 overlapped = FILE_FLAG_OVERLAPPED;
583#endif
83f64091
FB
584 s->hfile = CreateFile(filename, access_flags,
585 FILE_SHARE_READ, NULL,
ce1a14dc 586 create_flags, overlapped, 0);
83f64091
FB
587 if (s->hfile == INVALID_HANDLE_VALUE)
588 return -1;
589 return 0;
590}
591
592static int raw_pread(BlockDriverState *bs, int64_t offset,
593 uint8_t *buf, int count)
594{
595 BDRVRawState *s = bs->opaque;
596 OVERLAPPED ov;
597 DWORD ret_count;
598 int ret;
599
600 memset(&ov, 0, sizeof(ov));
601 ov.Offset = offset;
602 ov.OffsetHigh = offset >> 32;
436e15b8
FB
603 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
604 if (!ret) {
605 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
606 if (!ret)
607 return -EIO;
608 else
609 return ret_count;
610 }
83f64091
FB
611 return ret_count;
612}
613
614static int raw_pwrite(BlockDriverState *bs, int64_t offset,
615 const uint8_t *buf, int count)
616{
617 BDRVRawState *s = bs->opaque;
618 OVERLAPPED ov;
619 DWORD ret_count;
620 int ret;
621
622 memset(&ov, 0, sizeof(ov));
623 ov.Offset = offset;
624 ov.OffsetHigh = offset >> 32;
436e15b8
FB
625 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
626 if (!ret) {
627 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
628 if (!ret)
629 return -EIO;
630 else
631 return ret_count;
632 }
83f64091
FB
633 return ret_count;
634}
635
436e15b8 636#ifndef QEMU_TOOL
83f64091
FB
637static void raw_aio_cb(void *opaque)
638{
ce1a14dc
PB
639 RawAIOCB *acb = opaque;
640 BlockDriverState *bs = acb->common.bs;
979b67ad 641 BDRVRawState *s = bs->opaque;
83f64091
FB
642 DWORD ret_count;
643 int ret;
644
ce1a14dc
PB
645 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
646 if (!ret || ret_count != acb->count) {
647 acb->common.cb(acb->common.opaque, -EIO);
83f64091 648 } else {
ce1a14dc 649 acb->common.cb(acb->common.opaque, 0);
83f64091
FB
650 }
651}
436e15b8 652#endif
ce1a14dc
PB
653
654static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
655 int64_t sector_num, uint8_t *buf, int nb_sectors,
656 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 657{
ce1a14dc 658 RawAIOCB *acb;
83f64091
FB
659 int64_t offset;
660
ce1a14dc
PB
661 acb = qemu_aio_get(bs, cb, opaque);
662 if (acb->hEvent) {
663 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
664 if (!acb->hEvent) {
665 qemu_aio_release(acb);
666 return NULL;
667 }
668 }
669 memset(&acb->ov, 0, sizeof(acb->ov));
83f64091 670 offset = sector_num * 512;
ce1a14dc
PB
671 acb->ov.Offset = offset;
672 acb->ov.OffsetHigh = offset >> 32;
673 acb->ov.hEvent = acb->hEvent;
674 acb->count = nb_sectors * 512;
436e15b8 675#ifndef QEMU_TOOL
ce1a14dc 676 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
436e15b8 677#endif
ce1a14dc 678 return acb;
83f64091
FB
679}
680
ce1a14dc
PB
681static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
682 int64_t sector_num, uint8_t *buf, int nb_sectors,
683 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 684{
83f64091 685 BDRVRawState *s = bs->opaque;
ce1a14dc 686 RawAIOCB *acb;
83f64091 687 int ret;
83f64091 688
ce1a14dc
PB
689 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
690 if (!acb)
691 return NULL;
692 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
693 if (!ret) {
694 qemu_aio_release(acb);
695 return NULL;
696 }
697#ifdef QEMU_TOOL
698 qemu_aio_release(acb);
436e15b8 699#endif
ce1a14dc 700 return (BlockDriverAIOCB *)acb;
83f64091
FB
701}
702
ce1a14dc
PB
703static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
704 int64_t sector_num, uint8_t *buf, int nb_sectors,
705 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 706{
83f64091 707 BDRVRawState *s = bs->opaque;
ce1a14dc
PB
708 RawAIOCB *acb;
709 int ret;
83f64091 710
ce1a14dc
PB
711 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
712 if (!acb)
713 return NULL;
714 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
715 if (!ret) {
716 qemu_aio_release(acb);
717 return NULL;
718 }
719#ifdef QEMU_TOOL
720 qemu_aio_release(acb);
436e15b8 721#endif
ce1a14dc 722 return (BlockDriverAIOCB *)acb;
83f64091
FB
723}
724
ce1a14dc 725static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
83f64091 726{
ce1a14dc
PB
727#ifndef QEMU_TOOL
728 RawAIOCB *acb = (RawAIOCB *)blockacb;
729 BlockDriverState *bs = acb->common.bs;
730 BDRVRawState *s = bs->opaque;
731
732 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
733 /* XXX: if more than one async I/O it is not correct */
734 CancelIo(s->hfile);
735 qemu_aio_release(acb);
736#endif
83f64091
FB
737}
738
739static void raw_flush(BlockDriverState *bs)
740{
741 /* XXX: add it */
742}
743
744static void raw_close(BlockDriverState *bs)
745{
746 BDRVRawState *s = bs->opaque;
747 CloseHandle(s->hfile);
748}
749
750static int raw_truncate(BlockDriverState *bs, int64_t offset)
751{
752 BDRVRawState *s = bs->opaque;
753 DWORD low, high;
754
979b67ad
FB
755 low = offset;
756 high = offset >> 32;
83f64091
FB
757 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
758 return -EIO;
759 if (!SetEndOfFile(s->hfile))
760 return -EIO;
761 return 0;
762}
763
764static int64_t raw_getlength(BlockDriverState *bs)
765{
766 BDRVRawState *s = bs->opaque;
767 LARGE_INTEGER l;
436e15b8
FB
768
769 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
770 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
771 return -EIO;
83f64091
FB
772 return l.QuadPart;
773}
774
775static int raw_create(const char *filename, int64_t total_size,
776 const char *backing_file, int flags)
777{
778 int fd;
779
780 if (flags || backing_file)
781 return -ENOTSUP;
782
783 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
784 0644);
785 if (fd < 0)
786 return -EIO;
787 set_sparse(fd);
788 ftruncate(fd, total_size * 512);
789 close(fd);
790 return 0;
791}
792
793void qemu_aio_init(void)
794{
795}
796
797void qemu_aio_poll(void)
798{
799}
800
801void qemu_aio_wait_start(void)
802{
803}
804
805void qemu_aio_wait(void)
806{
807}
808
809void qemu_aio_wait_end(void)
810{
811}
812
813BlockDriver bdrv_raw = {
814 "raw",
815 sizeof(BDRVRawState),
816 NULL, /* no probe for protocols */
817 raw_open,
818 NULL,
819 NULL,
820 raw_close,
821 raw_create,
822 raw_flush,
823
824#if 0
83f64091
FB
825 .bdrv_aio_read = raw_aio_read,
826 .bdrv_aio_write = raw_aio_write,
827 .bdrv_aio_cancel = raw_aio_cancel,
ce1a14dc 828 .aiocb_size = sizeof(RawAIOCB);
83f64091
FB
829#endif
830 .protocol_name = "file",
831 .bdrv_pread = raw_pread,
832 .bdrv_pwrite = raw_pwrite,
833 .bdrv_truncate = raw_truncate,
834 .bdrv_getlength = raw_getlength,
835};
836#endif /* _WIN32 */