Fix SCSI off-by-one device size.
[qemu/mini2440.git] / block-raw.c
blobadb3d39add449ba793346756a27d6488b0c79535
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:
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
22 * THE SOFTWARE.
24 #include "vl.h"
25 #include "block_int.h"
26 #include <assert.h>
27 #ifndef _WIN32
28 #include <aio.h>
30 #ifndef QEMU_TOOL
31 #include "exec-all.h"
32 #endif
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
46 #ifdef __sun__
47 #include <sys/dkio.h>
48 #endif
50 typedef struct BDRVRawState {
51 int fd;
52 } BDRVRawState;
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 );
58 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
60 kern_return_t kernResult;
61 mach_port_t masterPort;
62 CFMutableDictionaryRef classesToMatch;
64 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
65 if ( KERN_SUCCESS != kernResult ) {
66 printf( "IOMasterPort returned %d\n", kernResult );
69 classesToMatch = IOServiceMatching( kIOCDMediaClass );
70 if ( classesToMatch == NULL ) {
71 printf( "IOServiceMatching returned a NULL dictionary.\n" );
72 } else {
73 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
75 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
76 if ( KERN_SUCCESS != kernResult )
78 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
81 return kernResult;
84 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
86 io_object_t nextMedia;
87 kern_return_t kernResult = KERN_FAILURE;
88 *bsdPath = '\0';
89 nextMedia = IOIteratorNext( mediaIterator );
90 if ( nextMedia )
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;
102 CFRelease( bsdPathAsCFString );
104 IOObjectRelease( nextMedia );
107 return kernResult;
110 #endif
112 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
114 BDRVRawState *s = bs->opaque;
115 int fd, open_flags;
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;
124 kernResult = FindEjectableCDMedia( &mediaIterator );
125 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
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);
136 filename = bsdPath;
139 if ( mediaIterator )
140 IOObjectRelease( mediaIterator );
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;
150 if (flags & BDRV_O_CREAT)
151 open_flags |= O_CREAT | O_TRUNC;
153 fd = open(filename, open_flags, 0644);
154 if (fd < 0)
155 return -errno;
156 s->fd = fd;
157 return 0;
160 /* XXX: use host sector size if necessary with:
161 #ifdef DIOCGSECTORSIZE
163 unsigned int sectorsize = 512;
164 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
165 sectorsize > bufsize)
166 bufsize = sectorsize;
168 #endif
169 #ifdef CONFIG_COCOA
170 u_int32_t blockSize = 512;
171 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
172 bufsize = blockSize;
174 #endif
177 static int raw_pread(BlockDriverState *bs, int64_t offset,
178 uint8_t *buf, int count)
180 BDRVRawState *s = bs->opaque;
181 int ret;
183 lseek(s->fd, offset, SEEK_SET);
184 ret = read(s->fd, buf, count);
185 return ret;
188 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
189 const uint8_t *buf, int count)
191 BDRVRawState *s = bs->opaque;
192 int ret;
194 lseek(s->fd, offset, SEEK_SET);
195 ret = write(s->fd, buf, count);
196 return ret;
199 /***********************************************************/
200 /* Unix AOP using POSIX AIO */
202 typedef struct RawAIOCB {
203 struct aiocb aiocb;
204 int busy; /* only used for debugging */
205 BlockDriverAIOCB *next;
206 } RawAIOCB;
208 static int aio_sig_num = SIGUSR2;
209 static BlockDriverAIOCB *first_aio; /* AIO issued */
210 static int aio_initialized = 0;
212 static void aio_signal_handler(int signum)
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);
223 #endif
225 #endif
228 void qemu_aio_init(void)
230 struct sigaction act;
232 aio_initialized = 1;
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);
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);
250 void qemu_aio_poll(void)
252 BlockDriverAIOCB *acb, **pacb;
253 RawAIOCB *acb1;
254 int ret;
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;
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;
290 the_end: ;
293 /* wait until at least one AIO was handled */
294 static sigset_t wait_oset;
296 void qemu_aio_wait_start(void)
298 sigset_t set;
300 if (!aio_initialized)
301 qemu_aio_init();
302 sigemptyset(&set);
303 sigaddset(&set, aio_sig_num);
304 sigprocmask(SIG_BLOCK, &set, &wait_oset);
307 void qemu_aio_wait(void)
309 sigset_t set;
310 int nb_sigs;
312 #ifndef QEMU_TOOL
313 if (qemu_bh_poll())
314 return;
315 #endif
316 sigemptyset(&set);
317 sigaddset(&set, aio_sig_num);
318 sigwait(&set, &nb_sigs);
319 qemu_aio_poll();
322 void qemu_aio_wait_end(void)
324 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
327 static int raw_aio_new(BlockDriverAIOCB *acb)
329 RawAIOCB *acb1;
330 BDRVRawState *s = acb->bs->opaque;
332 acb1 = qemu_mallocz(sizeof(RawAIOCB));
333 if (!acb1)
334 return -1;
335 acb->opaque = acb1;
336 acb1->aiocb.aio_fildes = s->fd;
337 acb1->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
338 acb1->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
339 return 0;
342 static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
343 uint8_t *buf, int nb_sectors)
345 RawAIOCB *acb1 = acb->opaque;
347 assert(acb1->busy == 0);
348 acb1->busy = 1;
349 acb1->aiocb.aio_buf = buf;
350 acb1->aiocb.aio_nbytes = nb_sectors * 512;
351 acb1->aiocb.aio_offset = sector_num * 512;
352 acb1->next = first_aio;
353 first_aio = acb;
354 if (aio_read(&acb1->aiocb) < 0) {
355 acb1->busy = 0;
356 return -errno;
358 return 0;
361 static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
362 const uint8_t *buf, int nb_sectors)
364 RawAIOCB *acb1 = acb->opaque;
366 assert(acb1->busy == 0);
367 acb1->busy = 1;
368 acb1->aiocb.aio_buf = (uint8_t *)buf;
369 acb1->aiocb.aio_nbytes = nb_sectors * 512;
370 acb1->aiocb.aio_offset = sector_num * 512;
371 acb1->next = first_aio;
372 first_aio = acb;
373 if (aio_write(&acb1->aiocb) < 0) {
374 acb1->busy = 0;
375 return -errno;
377 return 0;
380 static void raw_aio_cancel(BlockDriverAIOCB *acb)
382 RawAIOCB *acb1 = acb->opaque;
383 int ret;
384 BlockDriverAIOCB **pacb;
386 ret = aio_cancel(acb1->aiocb.aio_fildes, &acb1->aiocb);
387 if (ret == AIO_NOTCANCELED) {
388 /* fail safe: if the aio could not be canceled, we wait for
389 it */
390 while (aio_error(&acb1->aiocb) == EINPROGRESS);
393 /* remove the callback from the queue */
394 pacb = &first_aio;
395 for(;;) {
396 if (*pacb == NULL) {
397 break;
398 } else if (*pacb == acb) {
399 acb1->busy = 0;
400 *pacb = acb1->next;
401 break;
403 acb1 = (*pacb)->opaque;
404 pacb = &acb1->next;
408 static void raw_aio_delete(BlockDriverAIOCB *acb)
410 RawAIOCB *acb1 = acb->opaque;
411 raw_aio_cancel(acb);
412 qemu_free(acb1);
415 static void raw_close(BlockDriverState *bs)
417 BDRVRawState *s = bs->opaque;
418 close(s->fd);
421 static int raw_truncate(BlockDriverState *bs, int64_t offset)
423 BDRVRawState *s = bs->opaque;
424 if (ftruncate(s->fd, offset) < 0)
425 return -errno;
426 return 0;
429 static int64_t raw_getlength(BlockDriverState *bs)
431 BDRVRawState *s = bs->opaque;
432 int fd = s->fd;
433 int64_t size;
434 #ifdef _BSD
435 struct stat sb;
436 #endif
437 #ifdef __sun__
438 struct dk_minfo minfo;
439 int rv;
440 #endif
442 #ifdef _BSD
443 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
444 #ifdef DIOCGMEDIASIZE
445 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
446 #endif
447 #ifdef CONFIG_COCOA
448 size = LONG_LONG_MAX;
449 #else
450 size = lseek(fd, 0LL, SEEK_END);
451 #endif
452 } else
453 #endif
454 #ifdef __sun__
456 * use the DKIOCGMEDIAINFO ioctl to read the size.
458 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
459 if ( rv != -1 ) {
460 size = minfo.dki_lbsize * minfo.dki_capacity;
461 } else /* there are reports that lseek on some devices
462 fails, but irc discussion said that contingency
463 on contingency was overkill */
464 #endif
466 size = lseek(fd, 0, SEEK_END);
468 #ifdef _WIN32
469 /* On Windows hosts it can happen that we're unable to get file size
470 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
471 if (size == -1)
472 size = LONG_LONG_MAX;
473 #endif
474 return size;
477 static int raw_create(const char *filename, int64_t total_size,
478 const char *backing_file, int flags)
480 int fd;
482 if (flags || backing_file)
483 return -ENOTSUP;
485 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
486 0644);
487 if (fd < 0)
488 return -EIO;
489 ftruncate(fd, total_size * 512);
490 close(fd);
491 return 0;
494 static void raw_flush(BlockDriverState *bs)
496 BDRVRawState *s = bs->opaque;
497 fsync(s->fd);
500 BlockDriver bdrv_raw = {
501 "raw",
502 sizeof(BDRVRawState),
503 NULL, /* no probe for protocols */
504 raw_open,
505 NULL,
506 NULL,
507 raw_close,
508 raw_create,
509 raw_flush,
511 .bdrv_aio_new = raw_aio_new,
512 .bdrv_aio_read = raw_aio_read,
513 .bdrv_aio_write = raw_aio_write,
514 .bdrv_aio_cancel = raw_aio_cancel,
515 .bdrv_aio_delete = raw_aio_delete,
516 .protocol_name = "file",
517 .bdrv_pread = raw_pread,
518 .bdrv_pwrite = raw_pwrite,
519 .bdrv_truncate = raw_truncate,
520 .bdrv_getlength = raw_getlength,
523 #else /* _WIN32 */
525 /* XXX: use another file ? */
526 #include <winioctl.h>
528 typedef struct BDRVRawState {
529 HANDLE hfile;
530 } BDRVRawState;
532 typedef struct RawAIOCB {
533 HANDLE hEvent;
534 OVERLAPPED ov;
535 int count;
536 } RawAIOCB;
538 int qemu_ftruncate64(int fd, int64_t length)
540 LARGE_INTEGER li;
541 LONG high;
542 HANDLE h;
543 BOOL res;
545 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
546 return -1;
548 h = (HANDLE)_get_osfhandle(fd);
550 /* get current position, ftruncate do not change position */
551 li.HighPart = 0;
552 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
553 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
554 return -1;
556 high = length >> 32;
557 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
558 return -1;
559 res = SetEndOfFile(h);
561 /* back to old position */
562 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
563 return res ? 0 : -1;
566 static int set_sparse(int fd)
568 DWORD returned;
569 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
570 NULL, 0, NULL, 0, &returned, NULL);
573 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
575 BDRVRawState *s = bs->opaque;
576 int access_flags, create_flags;
578 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
579 access_flags = GENERIC_READ | GENERIC_WRITE;
580 } else {
581 access_flags = GENERIC_READ;
583 if (flags & BDRV_O_CREAT) {
584 create_flags = CREATE_ALWAYS;
585 } else {
586 create_flags = OPEN_EXISTING;
588 s->hfile = CreateFile(filename, access_flags,
589 FILE_SHARE_READ, NULL,
590 create_flags, FILE_FLAG_OVERLAPPED, 0);
591 if (s->hfile == INVALID_HANDLE_VALUE)
592 return -1;
593 return 0;
596 static int raw_pread(BlockDriverState *bs, int64_t offset,
597 uint8_t *buf, int count)
599 BDRVRawState *s = bs->opaque;
600 OVERLAPPED ov;
601 DWORD ret_count;
602 int ret;
604 memset(&ov, 0, sizeof(ov));
605 ov.Offset = offset;
606 ov.OffsetHigh = offset >> 32;
607 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
608 if (!ret) {
609 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
610 if (!ret)
611 return -EIO;
612 else
613 return ret_count;
615 return ret_count;
618 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
619 const uint8_t *buf, int count)
621 BDRVRawState *s = bs->opaque;
622 OVERLAPPED ov;
623 DWORD ret_count;
624 int ret;
626 memset(&ov, 0, sizeof(ov));
627 ov.Offset = offset;
628 ov.OffsetHigh = offset >> 32;
629 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
630 if (!ret) {
631 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
632 if (!ret)
633 return -EIO;
634 else
635 return ret_count;
637 return ret_count;
640 static int raw_aio_new(BlockDriverAIOCB *acb)
642 RawAIOCB *acb1;
644 acb1 = qemu_mallocz(sizeof(RawAIOCB));
645 if (!acb1)
646 return -ENOMEM;
647 acb->opaque = acb1;
648 acb1->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
649 if (!acb1->hEvent)
650 return -ENOMEM;
651 return 0;
653 #ifndef QEMU_TOOL
654 static void raw_aio_cb(void *opaque)
656 BlockDriverAIOCB *acb = opaque;
657 BlockDriverState *bs = acb->bs;
658 BDRVRawState *s = bs->opaque;
659 RawAIOCB *acb1 = acb->opaque;
660 DWORD ret_count;
661 int ret;
663 ret = GetOverlappedResult(s->hfile, &acb1->ov, &ret_count, TRUE);
664 if (!ret || ret_count != acb1->count) {
665 acb->cb(acb->cb_opaque, -EIO);
666 } else {
667 acb->cb(acb->cb_opaque, 0);
670 #endif
671 static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
672 uint8_t *buf, int nb_sectors)
674 BlockDriverState *bs = acb->bs;
675 BDRVRawState *s = bs->opaque;
676 RawAIOCB *acb1 = acb->opaque;
677 int ret;
678 int64_t offset;
680 memset(&acb1->ov, 0, sizeof(acb1->ov));
681 offset = sector_num * 512;
682 acb1->ov.Offset = offset;
683 acb1->ov.OffsetHigh = offset >> 32;
684 acb1->ov.hEvent = acb1->hEvent;
685 acb1->count = nb_sectors * 512;
686 #ifndef QEMU_TOOL
687 qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
688 #endif
689 ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
690 if (!ret)
691 return -EIO;
692 return 0;
695 static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
696 uint8_t *buf, int nb_sectors)
698 BlockDriverState *bs = acb->bs;
699 BDRVRawState *s = bs->opaque;
700 RawAIOCB *acb1 = acb->opaque;
701 int ret;
702 int64_t offset;
704 memset(&acb1->ov, 0, sizeof(acb1->ov));
705 offset = sector_num * 512;
706 acb1->ov.Offset = offset;
707 acb1->ov.OffsetHigh = offset >> 32;
708 acb1->ov.hEvent = acb1->hEvent;
709 acb1->count = nb_sectors * 512;
710 #ifndef QEMU_TOOL
711 qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
712 #endif
713 ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
714 if (!ret)
715 return -EIO;
716 return 0;
719 static void raw_aio_cancel(BlockDriverAIOCB *acb)
721 BlockDriverState *bs = acb->bs;
722 BDRVRawState *s = bs->opaque;
723 #ifndef QEMU_TOOL
724 RawAIOCB *acb1 = acb->opaque;
726 qemu_del_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
727 #endif
728 /* XXX: if more than one async I/O it is not correct */
729 CancelIo(s->hfile);
732 static void raw_aio_delete(BlockDriverAIOCB *acb)
734 RawAIOCB *acb1 = acb->opaque;
735 raw_aio_cancel(acb);
736 CloseHandle(acb1->hEvent);
737 qemu_free(acb1);
740 static void raw_flush(BlockDriverState *bs)
742 /* XXX: add it */
745 static void raw_close(BlockDriverState *bs)
747 BDRVRawState *s = bs->opaque;
748 CloseHandle(s->hfile);
751 static int raw_truncate(BlockDriverState *bs, int64_t offset)
753 BDRVRawState *s = bs->opaque;
754 DWORD low, high;
756 low = offset;
757 high = offset >> 32;
758 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
759 return -EIO;
760 if (!SetEndOfFile(s->hfile))
761 return -EIO;
762 return 0;
765 static int64_t raw_getlength(BlockDriverState *bs)
767 BDRVRawState *s = bs->opaque;
768 LARGE_INTEGER l;
770 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
771 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
772 return -EIO;
773 return l.QuadPart;
776 static int raw_create(const char *filename, int64_t total_size,
777 const char *backing_file, int flags)
779 int fd;
781 if (flags || backing_file)
782 return -ENOTSUP;
784 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
785 0644);
786 if (fd < 0)
787 return -EIO;
788 set_sparse(fd);
789 ftruncate(fd, total_size * 512);
790 close(fd);
791 return 0;
794 void qemu_aio_init(void)
798 void qemu_aio_poll(void)
802 void qemu_aio_wait_start(void)
806 void qemu_aio_wait(void)
810 void qemu_aio_wait_end(void)
814 BlockDriver bdrv_raw = {
815 "raw",
816 sizeof(BDRVRawState),
817 NULL, /* no probe for protocols */
818 raw_open,
819 NULL,
820 NULL,
821 raw_close,
822 raw_create,
823 raw_flush,
825 #if 0
826 .bdrv_aio_new = raw_aio_new,
827 .bdrv_aio_read = raw_aio_read,
828 .bdrv_aio_write = raw_aio_write,
829 .bdrv_aio_cancel = raw_aio_cancel,
830 .bdrv_aio_delete = raw_aio_delete,
831 #endif
832 .protocol_name = "file",
833 .bdrv_pread = raw_pread,
834 .bdrv_pwrite = raw_pwrite,
835 .bdrv_truncate = raw_truncate,
836 .bdrv_getlength = raw_getlength,
838 #endif /* _WIN32 */