]> git.proxmox.com Git - mirror_qemu.git/blob - block-raw.c
some compilation fixes
[mirror_qemu.git] / block-raw.c
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
50 typedef struct BDRVRawState {
51 int fd;
52 } BDRVRawState;
53
54 #ifdef CONFIG_COCOA
55 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
56 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
57
58 kern_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
84 kern_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
112 static 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
177 static 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
188 static 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
202 typedef struct RawAIOCB {
203 struct aiocb aiocb;
204 int busy; /* only used for debugging */
205 BlockDriverAIOCB *next;
206 } RawAIOCB;
207
208 static int aio_sig_num = SIGUSR2;
209 static BlockDriverAIOCB *first_aio; /* AIO issued */
210 static int aio_initialized = 0;
211
212 static void aio_signal_handler(int signum)
213 {
214 #ifndef QEMU_TOOL
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 }
225 #endif
226 }
227
228 void qemu_aio_init(void)
229 {
230 struct sigaction act;
231
232 aio_initialized = 1;
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 }
249
250 void qemu_aio_poll(void)
251 {
252 BlockDriverAIOCB *acb, **pacb;
253 RawAIOCB *acb1;
254 int ret;
255
256 for(;;) {
257 pacb = &first_aio;
258 for(;;) {
259 acb = *pacb;
260 if (!acb)
261 goto the_end;
262 acb1 = acb->opaque;
263 ret = aio_error(&acb1->aiocb);
264 if (ret == ECANCELED) {
265 /* remove the request */
266 acb1->busy = 0;
267 *pacb = acb1->next;
268 } else if (ret != EINPROGRESS) {
269 /* end of aio */
270 if (ret == 0) {
271 ret = aio_return(&acb1->aiocb);
272 if (ret == acb1->aiocb.aio_nbytes)
273 ret = 0;
274 else
275 ret = -1;
276 } else {
277 ret = -ret;
278 }
279 /* remove the request */
280 acb1->busy = 0;
281 *pacb = acb1->next;
282 /* call the callback */
283 acb->cb(acb->cb_opaque, ret);
284 break;
285 } else {
286 pacb = &acb1->next;
287 }
288 }
289 }
290 the_end: ;
291 }
292
293 /* wait until at least one AIO was handled */
294 static sigset_t wait_oset;
295
296 void qemu_aio_wait_start(void)
297 {
298 sigset_t set;
299
300 if (!aio_initialized)
301 qemu_aio_init();
302 sigemptyset(&set);
303 sigaddset(&set, aio_sig_num);
304 sigprocmask(SIG_BLOCK, &set, &wait_oset);
305 }
306
307 void qemu_aio_wait(void)
308 {
309 sigset_t set;
310 int nb_sigs;
311 sigemptyset(&set);
312 sigaddset(&set, aio_sig_num);
313 sigwait(&set, &nb_sigs);
314 qemu_aio_poll();
315 }
316
317 void qemu_aio_wait_end(void)
318 {
319 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
320 }
321
322 static int raw_aio_new(BlockDriverAIOCB *acb)
323 {
324 RawAIOCB *acb1;
325 BDRVRawState *s = acb->bs->opaque;
326
327 acb1 = qemu_mallocz(sizeof(RawAIOCB));
328 if (!acb1)
329 return -1;
330 acb->opaque = acb1;
331 acb1->aiocb.aio_fildes = s->fd;
332 acb1->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
333 acb1->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
334 return 0;
335 }
336
337 static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
338 uint8_t *buf, int nb_sectors)
339 {
340 RawAIOCB *acb1 = acb->opaque;
341
342 assert(acb1->busy == 0);
343 acb1->busy = 1;
344 acb1->aiocb.aio_buf = buf;
345 acb1->aiocb.aio_nbytes = nb_sectors * 512;
346 acb1->aiocb.aio_offset = sector_num * 512;
347 acb1->next = first_aio;
348 first_aio = acb;
349 if (aio_read(&acb1->aiocb) < 0) {
350 acb1->busy = 0;
351 return -errno;
352 }
353 return 0;
354 }
355
356 static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
357 const uint8_t *buf, int nb_sectors)
358 {
359 RawAIOCB *acb1 = acb->opaque;
360
361 assert(acb1->busy == 0);
362 acb1->busy = 1;
363 acb1->aiocb.aio_buf = (uint8_t *)buf;
364 acb1->aiocb.aio_nbytes = nb_sectors * 512;
365 acb1->aiocb.aio_offset = sector_num * 512;
366 acb1->next = first_aio;
367 first_aio = acb;
368 if (aio_write(&acb1->aiocb) < 0) {
369 acb1->busy = 0;
370 return -errno;
371 }
372 return 0;
373 }
374
375 static void raw_aio_cancel(BlockDriverAIOCB *acb)
376 {
377 RawAIOCB *acb1 = acb->opaque;
378 int ret;
379 BlockDriverAIOCB **pacb;
380
381 ret = aio_cancel(acb1->aiocb.aio_fildes, &acb1->aiocb);
382 if (ret == AIO_NOTCANCELED) {
383 /* fail safe: if the aio could not be canceled, we wait for
384 it */
385 while (aio_error(&acb1->aiocb) == EINPROGRESS);
386 }
387
388 /* remove the callback from the queue */
389 pacb = &first_aio;
390 for(;;) {
391 if (*pacb == NULL) {
392 break;
393 } else if (*pacb == acb) {
394 acb1->busy = 0;
395 *pacb = acb1->next;
396 break;
397 }
398 acb1 = (*pacb)->opaque;
399 pacb = &acb1->next;
400 }
401 }
402
403 static void raw_aio_delete(BlockDriverAIOCB *acb)
404 {
405 RawAIOCB *acb1 = acb->opaque;
406 raw_aio_cancel(acb);
407 qemu_free(acb1);
408 }
409
410 static void raw_close(BlockDriverState *bs)
411 {
412 BDRVRawState *s = bs->opaque;
413 close(s->fd);
414 }
415
416 static int raw_truncate(BlockDriverState *bs, int64_t offset)
417 {
418 BDRVRawState *s = bs->opaque;
419 if (ftruncate(s->fd, offset) < 0)
420 return -errno;
421 return 0;
422 }
423
424 static int64_t raw_getlength(BlockDriverState *bs)
425 {
426 BDRVRawState *s = bs->opaque;
427 int fd = s->fd;
428 int64_t size;
429 #ifdef _BSD
430 struct stat sb;
431 #endif
432 #ifdef __sun__
433 struct dk_minfo minfo;
434 int rv;
435 #endif
436
437 #ifdef _BSD
438 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
439 #ifdef DIOCGMEDIASIZE
440 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
441 #endif
442 #ifdef CONFIG_COCOA
443 size = LONG_LONG_MAX;
444 #else
445 size = lseek(fd, 0LL, SEEK_END);
446 #endif
447 } else
448 #endif
449 #ifdef __sun__
450 /*
451 * use the DKIOCGMEDIAINFO ioctl to read the size.
452 */
453 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
454 if ( rv != -1 ) {
455 size = minfo.dki_lbsize * minfo.dki_capacity;
456 } else /* there are reports that lseek on some devices
457 fails, but irc discussion said that contingency
458 on contingency was overkill */
459 #endif
460 {
461 size = lseek(fd, 0, SEEK_END);
462 }
463 #ifdef _WIN32
464 /* On Windows hosts it can happen that we're unable to get file size
465 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
466 if (size == -1)
467 size = LONG_LONG_MAX;
468 #endif
469 return size;
470 }
471
472 static int raw_create(const char *filename, int64_t total_size,
473 const char *backing_file, int flags)
474 {
475 int fd;
476
477 if (flags || backing_file)
478 return -ENOTSUP;
479
480 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
481 0644);
482 if (fd < 0)
483 return -EIO;
484 ftruncate(fd, total_size * 512);
485 close(fd);
486 return 0;
487 }
488
489 static void raw_flush(BlockDriverState *bs)
490 {
491 BDRVRawState *s = bs->opaque;
492 fsync(s->fd);
493 }
494
495 BlockDriver bdrv_raw = {
496 "raw",
497 sizeof(BDRVRawState),
498 NULL, /* no probe for protocols */
499 raw_open,
500 NULL,
501 NULL,
502 raw_close,
503 raw_create,
504 raw_flush,
505
506 .bdrv_aio_new = raw_aio_new,
507 .bdrv_aio_read = raw_aio_read,
508 .bdrv_aio_write = raw_aio_write,
509 .bdrv_aio_cancel = raw_aio_cancel,
510 .bdrv_aio_delete = raw_aio_delete,
511 .protocol_name = "file",
512 .bdrv_pread = raw_pread,
513 .bdrv_pwrite = raw_pwrite,
514 .bdrv_truncate = raw_truncate,
515 .bdrv_getlength = raw_getlength,
516 };
517
518 #else /* _WIN32 */
519
520 /* XXX: use another file ? */
521 #include <windows.h>
522 #include <winioctl.h>
523
524 typedef struct BDRVRawState {
525 HANDLE hfile;
526 } BDRVRawState;
527
528 typedef struct RawAIOCB {
529 HANDLE hEvent;
530 OVERLAPPED ov;
531 int count;
532 } RawAIOCB;
533
534 int qemu_ftruncate64(int fd, int64_t length)
535 {
536 LARGE_INTEGER li;
537 LONG high;
538 HANDLE h;
539 BOOL res;
540
541 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
542 return -1;
543
544 h = (HANDLE)_get_osfhandle(fd);
545
546 /* get current position, ftruncate do not change position */
547 li.HighPart = 0;
548 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
549 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
550 return -1;
551
552 high = length >> 32;
553 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
554 return -1;
555 res = SetEndOfFile(h);
556
557 /* back to old position */
558 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
559 return res ? 0 : -1;
560 }
561
562 static int set_sparse(int fd)
563 {
564 DWORD returned;
565 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
566 NULL, 0, NULL, 0, &returned, NULL);
567 }
568
569 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
570 {
571 BDRVRawState *s = bs->opaque;
572 int access_flags, create_flags;
573
574 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
575 access_flags = GENERIC_READ | GENERIC_WRITE;
576 } else {
577 access_flags = GENERIC_READ;
578 }
579 if (flags & BDRV_O_CREAT) {
580 create_flags = CREATE_ALWAYS;
581 } else {
582 create_flags = OPEN_EXISTING;
583 }
584 s->hfile = CreateFile(filename, access_flags,
585 FILE_SHARE_READ, NULL,
586 create_flags, FILE_FLAG_OVERLAPPED, 0);
587 if (s->hfile == INVALID_HANDLE_VALUE)
588 return -1;
589 return 0;
590 }
591
592 static 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;
603 ret = ReadFile(s->hfile, buf, count, NULL, &ov);
604 if (!ret)
605 return -EIO;
606 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
607 if (!ret)
608 return -EIO;
609 return ret_count;
610 }
611
612 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
613 const uint8_t *buf, int count)
614 {
615 BDRVRawState *s = bs->opaque;
616 OVERLAPPED ov;
617 DWORD ret_count;
618 int ret;
619
620 memset(&ov, 0, sizeof(ov));
621 ov.Offset = offset;
622 ov.OffsetHigh = offset >> 32;
623 ret = WriteFile(s->hfile, buf, count, NULL, &ov);
624 if (!ret)
625 return -EIO;
626 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
627 if (!ret)
628 return -EIO;
629 return ret_count;
630 }
631
632 static int raw_aio_new(BlockDriverAIOCB *acb)
633 {
634 RawAIOCB *acb1;
635 BDRVRawState *s = acb->bs->opaque;
636
637 acb1 = qemu_mallocz(sizeof(RawAIOCB));
638 if (!acb1)
639 return -ENOMEM;
640 acb->opaque = acb1;
641 s->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
642 if (!s->hEvent)
643 return -ENOMEM;
644 return 0;
645 }
646
647 static void raw_aio_cb(void *opaque)
648 {
649 BlockDriverAIOCB *acb = opaque;
650 BlockDriverState *bs = acb->bs;
651 BDRVRawState *s = bs->opaque;
652 RawAIOCB *acb1 = acb->opaque;
653 DWORD ret_count;
654 int ret;
655
656 ret = GetOverlappedResult(s->hfile, &acb1->ov, &ret_count, TRUE);
657 if (!ret || ret_count != acb1->count) {
658 acb->cb(acb->cb_opaque, -EIO);
659 } else {
660 acb->cb(acb->cb_opaque, 0);
661 }
662 }
663
664 static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
665 uint8_t *buf, int nb_sectors)
666 {
667 BlockDriverState *bs = acb->bs;
668 BDRVRawState *s = bs->opaque;
669 RawAIOCB *acb1 = acb->opaque;
670 int ret;
671 int64_t offset;
672
673 memset(&acb1->ov, 0, sizeof(acb1->ov));
674 offset = sector_num * 512;
675 acb1->ov.Offset = offset;
676 acb1->ov.OffsetHigh = offset >> 32;
677 acb1->ov.hEvent = acb1->hEvent;
678 acb1->count = nb_sectors * 512;
679 qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
680 ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
681 if (!ret)
682 return -EIO;
683 return 0;
684 }
685
686 static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
687 uint8_t *buf, int nb_sectors)
688 {
689 BlockDriverState *bs = acb->bs;
690 BDRVRawState *s = bs->opaque;
691 RawAIOCB *acb1 = acb->opaque;
692 int ret;
693 int64_t offset;
694
695 memset(&acb1->ov, 0, sizeof(acb1->ov));
696 offset = sector_num * 512;
697 acb1->ov.Offset = offset;
698 acb1->ov.OffsetHigh = offset >> 32;
699 acb1->ov.hEvent = acb1->hEvent;
700 acb1->count = nb_sectors * 512;
701 qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
702 ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
703 if (!ret)
704 return -EIO;
705 return 0;
706 }
707
708 static void raw_aio_cancel(BlockDriverAIOCB *acb)
709 {
710 BlockDriverState *bs = acb->bs;
711 BDRVRawState *s = bs->opaque;
712 RawAIOCB *acb1 = acb->opaque;
713
714 qemu_del_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
715 /* XXX: if more than one async I/O it is not correct */
716 CancelIo(s->hfile);
717 }
718
719 static void raw_aio_delete(BlockDriverAIOCB *acb)
720 {
721 RawAIOCB *acb1 = acb->opaque;
722 raw_aio_cancel(acb);
723 CloseHandle(acb1->hEvent);
724 qemu_free(acb1);
725 }
726
727 static void raw_flush(BlockDriverState *bs)
728 {
729 /* XXX: add it */
730 }
731
732 static void raw_close(BlockDriverState *bs)
733 {
734 BDRVRawState *s = bs->opaque;
735 CloseHandle(s->hfile);
736 }
737
738 static int raw_truncate(BlockDriverState *bs, int64_t offset)
739 {
740 BDRVRawState *s = bs->opaque;
741 DWORD low, high;
742
743 low = offset;
744 high = offset >> 32;
745 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
746 return -EIO;
747 if (!SetEndOfFile(s->hfile))
748 return -EIO;
749 return 0;
750 }
751
752 static int64_t raw_getlength(BlockDriverState *bs)
753 {
754 BDRVRawState *s = bs->opaque;
755 LARGE_INTEGER l;
756 if (!GetFileSizeEx(s->hfile, &l))
757 return -EIO;
758 return l.QuadPart;
759 }
760
761 static int raw_create(const char *filename, int64_t total_size,
762 const char *backing_file, int flags)
763 {
764 int fd;
765
766 if (flags || backing_file)
767 return -ENOTSUP;
768
769 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
770 0644);
771 if (fd < 0)
772 return -EIO;
773 set_sparse(fd);
774 ftruncate(fd, total_size * 512);
775 close(fd);
776 return 0;
777 }
778
779 void qemu_aio_init(void)
780 {
781 }
782
783 void qemu_aio_poll(void)
784 {
785 }
786
787 void qemu_aio_wait_start(void)
788 {
789 }
790
791 void qemu_aio_wait(void)
792 {
793 }
794
795 void qemu_aio_wait_end(void)
796 {
797 }
798
799 BlockDriver bdrv_raw = {
800 "raw",
801 sizeof(BDRVRawState),
802 NULL, /* no probe for protocols */
803 raw_open,
804 NULL,
805 NULL,
806 raw_close,
807 raw_create,
808 raw_flush,
809
810 #if 0
811 .bdrv_aio_new = raw_aio_new,
812 .bdrv_aio_read = raw_aio_read,
813 .bdrv_aio_write = raw_aio_write,
814 .bdrv_aio_cancel = raw_aio_cancel,
815 .bdrv_aio_delete = raw_aio_delete,
816 #endif
817 .protocol_name = "file",
818 .bdrv_pread = raw_pread,
819 .bdrv_pwrite = raw_pwrite,
820 .bdrv_truncate = raw_truncate,
821 .bdrv_getlength = raw_getlength,
822 };
823 #endif /* _WIN32 */