]> Git Repo - qemu.git/blame - block-raw.c
new qcow2 disk image format
[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 {
203 struct aiocb aiocb;
204 int busy; /* only used for debugging */
205 BlockDriverAIOCB *next;
206} RawAIOCB;
207
208static int aio_sig_num = SIGUSR2;
209static BlockDriverAIOCB *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{
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 */
294static sigset_t wait_oset;
295
296void qemu_aio_wait_start(void)
297{
298 sigset_t set;
979b67ad
FB
299
300 if (!aio_initialized)
301 qemu_aio_init();
83f64091
FB
302 sigemptyset(&set);
303 sigaddset(&set, aio_sig_num);
304 sigprocmask(SIG_BLOCK, &set, &wait_oset);
305}
306
307void 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
317void qemu_aio_wait_end(void)
318{
319 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
320}
321
322static 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
337static 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
356static 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
375static 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
403static void raw_aio_delete(BlockDriverAIOCB *acb)
404{
405 RawAIOCB *acb1 = acb->opaque;
406 raw_aio_cancel(acb);
407 qemu_free(acb1);
408}
409
410static void raw_close(BlockDriverState *bs)
411{
412 BDRVRawState *s = bs->opaque;
413 close(s->fd);
414}
415
416static 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
424static 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
472static 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
489static void raw_flush(BlockDriverState *bs)
490{
491 BDRVRawState *s = bs->opaque;
492 fsync(s->fd);
493}
494
495BlockDriver 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 ? */
83f64091
FB
521#include <winioctl.h>
522
523typedef struct BDRVRawState {
524 HANDLE hfile;
525} BDRVRawState;
526
527typedef struct RawAIOCB {
528 HANDLE hEvent;
529 OVERLAPPED ov;
530 int count;
531} RawAIOCB;
532
533int qemu_ftruncate64(int fd, int64_t length)
534{
535 LARGE_INTEGER li;
536 LONG high;
537 HANDLE h;
538 BOOL res;
539
540 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
541 return -1;
542
543 h = (HANDLE)_get_osfhandle(fd);
544
545 /* get current position, ftruncate do not change position */
546 li.HighPart = 0;
547 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
548 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
549 return -1;
550
551 high = length >> 32;
552 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
553 return -1;
554 res = SetEndOfFile(h);
555
556 /* back to old position */
557 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
558 return res ? 0 : -1;
559}
560
561static int set_sparse(int fd)
562{
563 DWORD returned;
564 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
565 NULL, 0, NULL, 0, &returned, NULL);
566}
567
568static int raw_open(BlockDriverState *bs, const char *filename, int flags)
569{
570 BDRVRawState *s = bs->opaque;
571 int access_flags, create_flags;
572
573 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
574 access_flags = GENERIC_READ | GENERIC_WRITE;
575 } else {
576 access_flags = GENERIC_READ;
577 }
979b67ad 578 if (flags & BDRV_O_CREAT) {
83f64091
FB
579 create_flags = CREATE_ALWAYS;
580 } else {
581 create_flags = OPEN_EXISTING;
582 }
583 s->hfile = CreateFile(filename, access_flags,
584 FILE_SHARE_READ, NULL,
585 create_flags, FILE_FLAG_OVERLAPPED, 0);
586 if (s->hfile == INVALID_HANDLE_VALUE)
587 return -1;
588 return 0;
589}
590
591static int raw_pread(BlockDriverState *bs, int64_t offset,
592 uint8_t *buf, int count)
593{
594 BDRVRawState *s = bs->opaque;
595 OVERLAPPED ov;
596 DWORD ret_count;
597 int ret;
598
599 memset(&ov, 0, sizeof(ov));
600 ov.Offset = offset;
601 ov.OffsetHigh = offset >> 32;
436e15b8
FB
602 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
603 if (!ret) {
604 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
605 if (!ret)
606 return -EIO;
607 else
608 return ret_count;
609 }
83f64091
FB
610 return ret_count;
611}
612
613static int raw_pwrite(BlockDriverState *bs, int64_t offset,
614 const uint8_t *buf, int count)
615{
616 BDRVRawState *s = bs->opaque;
617 OVERLAPPED ov;
618 DWORD ret_count;
619 int ret;
620
621 memset(&ov, 0, sizeof(ov));
622 ov.Offset = offset;
623 ov.OffsetHigh = offset >> 32;
436e15b8
FB
624 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
625 if (!ret) {
626 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
627 if (!ret)
628 return -EIO;
629 else
630 return ret_count;
631 }
83f64091
FB
632 return ret_count;
633}
634
635static int raw_aio_new(BlockDriverAIOCB *acb)
636{
637 RawAIOCB *acb1;
83f64091
FB
638
639 acb1 = qemu_mallocz(sizeof(RawAIOCB));
640 if (!acb1)
641 return -ENOMEM;
642 acb->opaque = acb1;
436e15b8
FB
643 acb1->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
644 if (!acb1->hEvent)
83f64091
FB
645 return -ENOMEM;
646 return 0;
647}
436e15b8 648#ifndef QEMU_TOOL
83f64091
FB
649static void raw_aio_cb(void *opaque)
650{
979b67ad
FB
651 BlockDriverAIOCB *acb = opaque;
652 BlockDriverState *bs = acb->bs;
653 BDRVRawState *s = bs->opaque;
83f64091
FB
654 RawAIOCB *acb1 = acb->opaque;
655 DWORD ret_count;
656 int ret;
657
658 ret = GetOverlappedResult(s->hfile, &acb1->ov, &ret_count, TRUE);
659 if (!ret || ret_count != acb1->count) {
660 acb->cb(acb->cb_opaque, -EIO);
661 } else {
662 acb->cb(acb->cb_opaque, 0);
663 }
664}
436e15b8 665#endif
83f64091
FB
666static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
667 uint8_t *buf, int nb_sectors)
668{
669 BlockDriverState *bs = acb->bs;
670 BDRVRawState *s = bs->opaque;
671 RawAIOCB *acb1 = acb->opaque;
83f64091
FB
672 int ret;
673 int64_t offset;
674
675 memset(&acb1->ov, 0, sizeof(acb1->ov));
676 offset = sector_num * 512;
677 acb1->ov.Offset = offset;
678 acb1->ov.OffsetHigh = offset >> 32;
679 acb1->ov.hEvent = acb1->hEvent;
680 acb1->count = nb_sectors * 512;
436e15b8 681#ifndef QEMU_TOOL
83f64091 682 qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
436e15b8 683#endif
83f64091
FB
684 ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
685 if (!ret)
686 return -EIO;
687 return 0;
688}
689
690static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
691 uint8_t *buf, int nb_sectors)
692{
693 BlockDriverState *bs = acb->bs;
694 BDRVRawState *s = bs->opaque;
695 RawAIOCB *acb1 = acb->opaque;
83f64091
FB
696 int ret;
697 int64_t offset;
698
699 memset(&acb1->ov, 0, sizeof(acb1->ov));
700 offset = sector_num * 512;
701 acb1->ov.Offset = offset;
702 acb1->ov.OffsetHigh = offset >> 32;
703 acb1->ov.hEvent = acb1->hEvent;
704 acb1->count = nb_sectors * 512;
436e15b8 705#ifndef QEMU_TOOL
83f64091 706 qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
436e15b8 707#endif
83f64091
FB
708 ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
709 if (!ret)
710 return -EIO;
711 return 0;
712}
713
714static void raw_aio_cancel(BlockDriverAIOCB *acb)
715{
716 BlockDriverState *bs = acb->bs;
717 BDRVRawState *s = bs->opaque;
436e15b8 718#ifndef QEMU_TOOL
83f64091
FB
719 RawAIOCB *acb1 = acb->opaque;
720
721 qemu_del_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
436e15b8 722#endif
83f64091
FB
723 /* XXX: if more than one async I/O it is not correct */
724 CancelIo(s->hfile);
725}
726
727static void raw_aio_delete(BlockDriverAIOCB *acb)
728{
729 RawAIOCB *acb1 = acb->opaque;
730 raw_aio_cancel(acb);
731 CloseHandle(acb1->hEvent);
732 qemu_free(acb1);
733}
734
735static void raw_flush(BlockDriverState *bs)
736{
737 /* XXX: add it */
738}
739
740static void raw_close(BlockDriverState *bs)
741{
742 BDRVRawState *s = bs->opaque;
743 CloseHandle(s->hfile);
744}
745
746static int raw_truncate(BlockDriverState *bs, int64_t offset)
747{
748 BDRVRawState *s = bs->opaque;
749 DWORD low, high;
750
979b67ad
FB
751 low = offset;
752 high = offset >> 32;
83f64091
FB
753 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
754 return -EIO;
755 if (!SetEndOfFile(s->hfile))
756 return -EIO;
757 return 0;
758}
759
760static int64_t raw_getlength(BlockDriverState *bs)
761{
762 BDRVRawState *s = bs->opaque;
763 LARGE_INTEGER l;
436e15b8
FB
764
765 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
766 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
767 return -EIO;
83f64091
FB
768 return l.QuadPart;
769}
770
771static int raw_create(const char *filename, int64_t total_size,
772 const char *backing_file, int flags)
773{
774 int fd;
775
776 if (flags || backing_file)
777 return -ENOTSUP;
778
779 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
780 0644);
781 if (fd < 0)
782 return -EIO;
783 set_sparse(fd);
784 ftruncate(fd, total_size * 512);
785 close(fd);
786 return 0;
787}
788
789void qemu_aio_init(void)
790{
791}
792
793void qemu_aio_poll(void)
794{
795}
796
797void qemu_aio_wait_start(void)
798{
799}
800
801void qemu_aio_wait(void)
802{
803}
804
805void qemu_aio_wait_end(void)
806{
807}
808
809BlockDriver bdrv_raw = {
810 "raw",
811 sizeof(BDRVRawState),
812 NULL, /* no probe for protocols */
813 raw_open,
814 NULL,
815 NULL,
816 raw_close,
817 raw_create,
818 raw_flush,
819
820#if 0
821 .bdrv_aio_new = raw_aio_new,
822 .bdrv_aio_read = raw_aio_read,
823 .bdrv_aio_write = raw_aio_write,
824 .bdrv_aio_cancel = raw_aio_cancel,
825 .bdrv_aio_delete = raw_aio_delete,
826#endif
827 .protocol_name = "file",
828 .bdrv_pread = raw_pread,
829 .bdrv_pwrite = raw_pwrite,
830 .bdrv_truncate = raw_truncate,
831 .bdrv_getlength = raw_getlength,
832};
833#endif /* _WIN32 */
This page took 0.105916 seconds and 4 git commands to generate.