]> git.proxmox.com Git - qemu.git/blame - block-raw.c
SCSI TCQ support.
[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
19cb3738
FB
49#ifdef __linux__
50#include <sys/ioctl.h>
51#include <linux/cdrom.h>
52#include <linux/fd.h>
53#endif
83f64091 54
19cb3738 55//#define DEBUG_FLOPPY
83f64091 56
19cb3738
FB
57#define FTYPE_FILE 0
58#define FTYPE_CD 1
59#define FTYPE_FD 2
83f64091 60
19cb3738
FB
61/* if the FD is not accessed during that time (in ms), we try to
62 reopen it to see if the disk has been changed */
63#define FD_OPEN_TIMEOUT 1000
83f64091 64
19cb3738
FB
65typedef struct BDRVRawState {
66 int fd;
67 int type;
68#if defined(__linux__)
69 /* linux floppy specific */
70 int fd_open_flags;
71 int64_t fd_open_time;
72 int64_t fd_error_time;
73 int fd_got_error;
74 int fd_media_changed;
83f64091 75#endif
19cb3738
FB
76} BDRVRawState;
77
78static int fd_open(BlockDriverState *bs);
83f64091
FB
79
80static int raw_open(BlockDriverState *bs, const char *filename, int flags)
81{
82 BDRVRawState *s = bs->opaque;
19cb3738 83 int fd, open_flags, ret;
83f64091 84
83f64091
FB
85 open_flags = O_BINARY;
86 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
87 open_flags |= O_RDWR;
88 } else {
89 open_flags |= O_RDONLY;
90 bs->read_only = 1;
91 }
92 if (flags & BDRV_O_CREAT)
93 open_flags |= O_CREAT | O_TRUNC;
94
19cb3738
FB
95 s->type = FTYPE_FILE;
96
83f64091 97 fd = open(filename, open_flags, 0644);
19cb3738
FB
98 if (fd < 0) {
99 ret = -errno;
100 if (ret == -EROFS)
101 ret = -EACCES;
102 return ret;
103 }
83f64091
FB
104 s->fd = fd;
105 return 0;
106}
107
108/* XXX: use host sector size if necessary with:
109#ifdef DIOCGSECTORSIZE
110 {
111 unsigned int sectorsize = 512;
112 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
113 sectorsize > bufsize)
114 bufsize = sectorsize;
115 }
116#endif
117#ifdef CONFIG_COCOA
118 u_int32_t blockSize = 512;
119 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
120 bufsize = blockSize;
121 }
122#endif
123*/
124
125static int raw_pread(BlockDriverState *bs, int64_t offset,
126 uint8_t *buf, int count)
127{
128 BDRVRawState *s = bs->opaque;
129 int ret;
130
19cb3738
FB
131 ret = fd_open(bs);
132 if (ret < 0)
133 return ret;
134
83f64091
FB
135 lseek(s->fd, offset, SEEK_SET);
136 ret = read(s->fd, buf, count);
137 return ret;
138}
139
140static int raw_pwrite(BlockDriverState *bs, int64_t offset,
141 const uint8_t *buf, int count)
142{
143 BDRVRawState *s = bs->opaque;
144 int ret;
145
19cb3738
FB
146 ret = fd_open(bs);
147 if (ret < 0)
148 return ret;
149
83f64091
FB
150 lseek(s->fd, offset, SEEK_SET);
151 ret = write(s->fd, buf, count);
152 return ret;
153}
154
155/***********************************************************/
19cb3738 156/* Unix AIO using POSIX AIO */
83f64091
FB
157
158typedef struct RawAIOCB {
ce1a14dc 159 BlockDriverAIOCB common;
83f64091 160 struct aiocb aiocb;
ce1a14dc 161 struct RawAIOCB *next;
83f64091
FB
162} RawAIOCB;
163
164static int aio_sig_num = SIGUSR2;
ce1a14dc 165static RawAIOCB *first_aio; /* AIO issued */
979b67ad 166static int aio_initialized = 0;
83f64091 167
83f64091
FB
168static void aio_signal_handler(int signum)
169{
979b67ad 170#ifndef QEMU_TOOL
83f64091
FB
171 CPUState *env = cpu_single_env;
172 if (env) {
173 /* stop the currently executing cpu because a timer occured */
174 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
175#ifdef USE_KQEMU
176 if (env->kqemu_enabled) {
177 kqemu_cpu_interrupt(env);
178 }
179#endif
180 }
979b67ad 181#endif
83f64091
FB
182}
183
184void qemu_aio_init(void)
185{
186 struct sigaction act;
979b67ad
FB
187
188 aio_initialized = 1;
83f64091
FB
189
190 sigfillset(&act.sa_mask);
191 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
192 act.sa_handler = aio_signal_handler;
193 sigaction(aio_sig_num, &act, NULL);
194
19cb3738 195#if defined(__GLIBC__) && defined(__linux__)
83f64091 196 {
19cb3738
FB
197 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
198 seems to fix the problem. */
83f64091
FB
199 struct aioinit ai;
200 memset(&ai, 0, sizeof(ai));
19cb3738 201 ai.aio_threads = 1;
83f64091
FB
202 ai.aio_num = 1;
203 ai.aio_idle_time = 365 * 100000;
204 aio_init(&ai);
205 }
19cb3738 206#endif
83f64091 207}
83f64091
FB
208
209void qemu_aio_poll(void)
210{
ce1a14dc 211 RawAIOCB *acb, **pacb;
83f64091
FB
212 int ret;
213
214 for(;;) {
215 pacb = &first_aio;
216 for(;;) {
217 acb = *pacb;
218 if (!acb)
219 goto the_end;
ce1a14dc 220 ret = aio_error(&acb->aiocb);
83f64091
FB
221 if (ret == ECANCELED) {
222 /* remove the request */
ce1a14dc
PB
223 *pacb = acb->next;
224 qemu_aio_release(acb);
83f64091
FB
225 } else if (ret != EINPROGRESS) {
226 /* end of aio */
227 if (ret == 0) {
ce1a14dc
PB
228 ret = aio_return(&acb->aiocb);
229 if (ret == acb->aiocb.aio_nbytes)
83f64091
FB
230 ret = 0;
231 else
19cb3738 232 ret = -EINVAL;
83f64091
FB
233 } else {
234 ret = -ret;
235 }
236 /* remove the request */
ce1a14dc 237 *pacb = acb->next;
83f64091 238 /* call the callback */
ce1a14dc
PB
239 acb->common.cb(acb->common.opaque, ret);
240 qemu_aio_release(acb);
83f64091
FB
241 break;
242 } else {
ce1a14dc 243 pacb = &acb->next;
83f64091
FB
244 }
245 }
246 }
247 the_end: ;
248}
249
250/* wait until at least one AIO was handled */
251static sigset_t wait_oset;
252
253void qemu_aio_wait_start(void)
254{
255 sigset_t set;
979b67ad
FB
256
257 if (!aio_initialized)
258 qemu_aio_init();
83f64091
FB
259 sigemptyset(&set);
260 sigaddset(&set, aio_sig_num);
261 sigprocmask(SIG_BLOCK, &set, &wait_oset);
262}
263
264void qemu_aio_wait(void)
265{
266 sigset_t set;
267 int nb_sigs;
6eb5733a
FB
268
269#ifndef QEMU_TOOL
270 if (qemu_bh_poll())
271 return;
272#endif
83f64091
FB
273 sigemptyset(&set);
274 sigaddset(&set, aio_sig_num);
275 sigwait(&set, &nb_sigs);
276 qemu_aio_poll();
277}
278
279void qemu_aio_wait_end(void)
280{
281 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
282}
283
ce1a14dc
PB
284static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
285 int64_t sector_num, uint8_t *buf, int nb_sectors,
286 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 287{
ce1a14dc
PB
288 BDRVRawState *s = bs->opaque;
289 RawAIOCB *acb;
290
19cb3738
FB
291 if (fd_open(bs) < 0)
292 return NULL;
293
ce1a14dc
PB
294 acb = qemu_aio_get(bs, cb, opaque);
295 if (!acb)
296 return NULL;
297 acb->aiocb.aio_fildes = s->fd;
298 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
299 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
300 acb->aiocb.aio_buf = buf;
301 acb->aiocb.aio_nbytes = nb_sectors * 512;
302 acb->aiocb.aio_offset = sector_num * 512;
303 acb->next = first_aio;
304 first_aio = acb;
305 return acb;
83f64091
FB
306}
307
ce1a14dc
PB
308static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
309 int64_t sector_num, uint8_t *buf, int nb_sectors,
310 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 311{
ce1a14dc 312 RawAIOCB *acb;
83f64091 313
ce1a14dc
PB
314 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
315 if (!acb)
316 return NULL;
317 if (aio_read(&acb->aiocb) < 0) {
318 qemu_aio_release(acb);
319 return NULL;
83f64091 320 }
ce1a14dc 321 return &acb->common;
83f64091
FB
322}
323
ce1a14dc
PB
324static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
325 int64_t sector_num, const uint8_t *buf, int nb_sectors,
326 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 327{
ce1a14dc 328 RawAIOCB *acb;
83f64091 329
ce1a14dc
PB
330 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
331 if (!acb)
332 return NULL;
333 if (aio_write(&acb->aiocb) < 0) {
334 qemu_aio_release(acb);
335 return NULL;
83f64091 336 }
ce1a14dc 337 return &acb->common;
83f64091
FB
338}
339
ce1a14dc 340static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
83f64091 341{
83f64091 342 int ret;
ce1a14dc
PB
343 RawAIOCB *acb = (RawAIOCB *)blockacb;
344 RawAIOCB **pacb;
83f64091 345
ce1a14dc 346 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
83f64091
FB
347 if (ret == AIO_NOTCANCELED) {
348 /* fail safe: if the aio could not be canceled, we wait for
349 it */
ce1a14dc 350 while (aio_error(&acb->aiocb) == EINPROGRESS);
83f64091
FB
351 }
352
353 /* remove the callback from the queue */
354 pacb = &first_aio;
355 for(;;) {
356 if (*pacb == NULL) {
357 break;
358 } else if (*pacb == acb) {
ce1a14dc
PB
359 *pacb = acb->next;
360 qemu_aio_release(acb);
83f64091
FB
361 break;
362 }
ce1a14dc 363 pacb = &acb->next;
83f64091
FB
364 }
365}
366
83f64091
FB
367static void raw_close(BlockDriverState *bs)
368{
369 BDRVRawState *s = bs->opaque;
19cb3738
FB
370 if (s->fd >= 0) {
371 close(s->fd);
372 s->fd = -1;
373 }
83f64091
FB
374}
375
376static int raw_truncate(BlockDriverState *bs, int64_t offset)
377{
378 BDRVRawState *s = bs->opaque;
19cb3738
FB
379 if (s->type != FTYPE_FILE)
380 return -ENOTSUP;
83f64091
FB
381 if (ftruncate(s->fd, offset) < 0)
382 return -errno;
383 return 0;
384}
385
386static int64_t raw_getlength(BlockDriverState *bs)
387{
388 BDRVRawState *s = bs->opaque;
389 int fd = s->fd;
390 int64_t size;
391#ifdef _BSD
392 struct stat sb;
393#endif
394#ifdef __sun__
395 struct dk_minfo minfo;
396 int rv;
397#endif
19cb3738
FB
398 int ret;
399
400 ret = fd_open(bs);
401 if (ret < 0)
402 return ret;
83f64091
FB
403
404#ifdef _BSD
405 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
406#ifdef DIOCGMEDIASIZE
407 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
408#endif
409#ifdef CONFIG_COCOA
410 size = LONG_LONG_MAX;
411#else
412 size = lseek(fd, 0LL, SEEK_END);
413#endif
414 } else
415#endif
416#ifdef __sun__
417 /*
418 * use the DKIOCGMEDIAINFO ioctl to read the size.
419 */
420 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
421 if ( rv != -1 ) {
422 size = minfo.dki_lbsize * minfo.dki_capacity;
423 } else /* there are reports that lseek on some devices
424 fails, but irc discussion said that contingency
425 on contingency was overkill */
426#endif
427 {
428 size = lseek(fd, 0, SEEK_END);
429 }
83f64091
FB
430 return size;
431}
432
433static int raw_create(const char *filename, int64_t total_size,
434 const char *backing_file, int flags)
435{
436 int fd;
437
438 if (flags || backing_file)
439 return -ENOTSUP;
440
9ab8a34a 441 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
83f64091
FB
442 0644);
443 if (fd < 0)
444 return -EIO;
445 ftruncate(fd, total_size * 512);
446 close(fd);
447 return 0;
448}
449
450static void raw_flush(BlockDriverState *bs)
451{
452 BDRVRawState *s = bs->opaque;
453 fsync(s->fd);
454}
455
456BlockDriver bdrv_raw = {
457 "raw",
458 sizeof(BDRVRawState),
459 NULL, /* no probe for protocols */
460 raw_open,
461 NULL,
462 NULL,
463 raw_close,
464 raw_create,
465 raw_flush,
466
83f64091
FB
467 .bdrv_aio_read = raw_aio_read,
468 .bdrv_aio_write = raw_aio_write,
469 .bdrv_aio_cancel = raw_aio_cancel,
ce1a14dc 470 .aiocb_size = sizeof(RawAIOCB),
83f64091
FB
471 .protocol_name = "file",
472 .bdrv_pread = raw_pread,
473 .bdrv_pwrite = raw_pwrite,
474 .bdrv_truncate = raw_truncate,
475 .bdrv_getlength = raw_getlength,
476};
477
19cb3738
FB
478/***********************************************/
479/* host device */
480
481#ifdef CONFIG_COCOA
482static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
483static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
484
485kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
486{
487 kern_return_t kernResult;
488 mach_port_t masterPort;
489 CFMutableDictionaryRef classesToMatch;
490
491 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
492 if ( KERN_SUCCESS != kernResult ) {
493 printf( "IOMasterPort returned %d\n", kernResult );
494 }
495
496 classesToMatch = IOServiceMatching( kIOCDMediaClass );
497 if ( classesToMatch == NULL ) {
498 printf( "IOServiceMatching returned a NULL dictionary.\n" );
499 } else {
500 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
501 }
502 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
503 if ( KERN_SUCCESS != kernResult )
504 {
505 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
506 }
507
508 return kernResult;
509}
510
511kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
512{
513 io_object_t nextMedia;
514 kern_return_t kernResult = KERN_FAILURE;
515 *bsdPath = '\0';
516 nextMedia = IOIteratorNext( mediaIterator );
517 if ( nextMedia )
518 {
519 CFTypeRef bsdPathAsCFString;
520 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
521 if ( bsdPathAsCFString ) {
522 size_t devPathLength;
523 strcpy( bsdPath, _PATH_DEV );
524 strcat( bsdPath, "r" );
525 devPathLength = strlen( bsdPath );
526 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
527 kernResult = KERN_SUCCESS;
528 }
529 CFRelease( bsdPathAsCFString );
530 }
531 IOObjectRelease( nextMedia );
532 }
533
534 return kernResult;
535}
536
537#endif
538
539static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
540{
541 BDRVRawState *s = bs->opaque;
542 int fd, open_flags, ret;
543
544#ifdef CONFIG_COCOA
545 if (strstart(filename, "/dev/cdrom", NULL)) {
546 kern_return_t kernResult;
547 io_iterator_t mediaIterator;
548 char bsdPath[ MAXPATHLEN ];
549 int fd;
550
551 kernResult = FindEjectableCDMedia( &mediaIterator );
552 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
553
554 if ( bsdPath[ 0 ] != '\0' ) {
555 strcat(bsdPath,"s0");
556 /* some CDs don't have a partition 0 */
557 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
558 if (fd < 0) {
559 bsdPath[strlen(bsdPath)-1] = '1';
560 } else {
561 close(fd);
562 }
563 filename = bsdPath;
564 }
565
566 if ( mediaIterator )
567 IOObjectRelease( mediaIterator );
568 }
569#endif
570 open_flags = O_BINARY;
571 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
572 open_flags |= O_RDWR;
573 } else {
574 open_flags |= O_RDONLY;
575 bs->read_only = 1;
576 }
577
578 s->type = FTYPE_FILE;
579#if defined(__linux__)
580 if (strstart(filename, "/dev/cd", NULL)) {
581 /* open will not fail even if no CD is inserted */
582 open_flags |= O_NONBLOCK;
583 s->type = FTYPE_CD;
584 } else if (strstart(filename, "/dev/fd", NULL)) {
585 s->type = FTYPE_FD;
586 s->fd_open_flags = open_flags;
587 /* open will not fail even if no floppy is inserted */
588 open_flags |= O_NONBLOCK;
589 }
590#endif
591 fd = open(filename, open_flags, 0644);
592 if (fd < 0) {
593 ret = -errno;
594 if (ret == -EROFS)
595 ret = -EACCES;
596 return ret;
597 }
598 s->fd = fd;
599#if defined(__linux__)
600 /* close fd so that we can reopen it as needed */
601 if (s->type == FTYPE_FD) {
602 close(s->fd);
603 s->fd = -1;
604 s->fd_media_changed = 1;
605 }
606#endif
607 return 0;
608}
609
610#if defined(__linux__) && !defined(QEMU_TOOL)
611
612/* Note: we do not have a reliable method to detect if the floppy is
613 present. The current method is to try to open the floppy at every
614 I/O and to keep it opened during a few hundreds of ms. */
615static int fd_open(BlockDriverState *bs)
616{
617 BDRVRawState *s = bs->opaque;
618 int last_media_present;
619
620 if (s->type != FTYPE_FD)
621 return 0;
622 last_media_present = (s->fd >= 0);
623 if (s->fd >= 0 &&
624 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
625 close(s->fd);
626 s->fd = -1;
627#ifdef DEBUG_FLOPPY
628 printf("Floppy closed\n");
629#endif
630 }
631 if (s->fd < 0) {
632 if (s->fd_got_error &&
633 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
634#ifdef DEBUG_FLOPPY
635 printf("No floppy (open delayed)\n");
636#endif
637 return -EIO;
638 }
639 s->fd = open(bs->filename, s->fd_open_flags);
640 if (s->fd < 0) {
641 s->fd_error_time = qemu_get_clock(rt_clock);
642 s->fd_got_error = 1;
643 if (last_media_present)
644 s->fd_media_changed = 1;
645#ifdef DEBUG_FLOPPY
646 printf("No floppy\n");
647#endif
648 return -EIO;
649 }
650#ifdef DEBUG_FLOPPY
651 printf("Floppy opened\n");
652#endif
653 }
654 if (!last_media_present)
655 s->fd_media_changed = 1;
656 s->fd_open_time = qemu_get_clock(rt_clock);
657 s->fd_got_error = 0;
658 return 0;
659}
660#else
661static int fd_open(BlockDriverState *bs)
662{
663 return 0;
664}
665#endif
666
667#if defined(__linux__)
668
669static int raw_is_inserted(BlockDriverState *bs)
670{
671 BDRVRawState *s = bs->opaque;
672 int ret;
673
674 switch(s->type) {
675 case FTYPE_CD:
676 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
677 if (ret == CDS_DISC_OK)
678 return 1;
679 else
680 return 0;
681 break;
682 case FTYPE_FD:
683 ret = fd_open(bs);
684 return (ret >= 0);
685 default:
686 return 1;
687 }
688}
689
690/* currently only used by fdc.c, but a CD version would be good too */
691static int raw_media_changed(BlockDriverState *bs)
692{
693 BDRVRawState *s = bs->opaque;
694
695 switch(s->type) {
696 case FTYPE_FD:
697 {
698 int ret;
699 /* XXX: we do not have a true media changed indication. It
700 does not work if the floppy is changed without trying
701 to read it */
702 fd_open(bs);
703 ret = s->fd_media_changed;
704 s->fd_media_changed = 0;
705#ifdef DEBUG_FLOPPY
706 printf("Floppy changed=%d\n", ret);
707#endif
708 return ret;
709 }
710 default:
711 return -ENOTSUP;
712 }
713}
714
715static int raw_eject(BlockDriverState *bs, int eject_flag)
716{
717 BDRVRawState *s = bs->opaque;
718
719 switch(s->type) {
720 case FTYPE_CD:
721 if (eject_flag) {
722 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
723 perror("CDROMEJECT");
724 } else {
725 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
726 perror("CDROMEJECT");
727 }
728 break;
729 case FTYPE_FD:
730 {
731 int fd;
732 if (s->fd >= 0) {
733 close(s->fd);
734 s->fd = -1;
735 }
736 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
737 if (fd >= 0) {
738 if (ioctl(fd, FDEJECT, 0) < 0)
739 perror("FDEJECT");
740 close(fd);
741 }
742 }
743 break;
744 default:
745 return -ENOTSUP;
746 }
747 return 0;
748}
749
750static int raw_set_locked(BlockDriverState *bs, int locked)
751{
752 BDRVRawState *s = bs->opaque;
753
754 switch(s->type) {
755 case FTYPE_CD:
756 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
757 /* Note: an error can happen if the distribution automatically
758 mounts the CD-ROM */
759 // perror("CDROM_LOCKDOOR");
760 }
761 break;
762 default:
763 return -ENOTSUP;
764 }
765 return 0;
766}
767
768#else
769
770static int raw_is_inserted(BlockDriverState *bs)
771{
772 return 1;
773}
774
775static int raw_media_changed(BlockDriverState *bs)
776{
777 return -ENOTSUP;
778}
779
780static int raw_eject(BlockDriverState *bs, int eject_flag)
781{
782 return -ENOTSUP;
783}
784
785static int raw_set_locked(BlockDriverState *bs, int locked)
786{
787 return -ENOTSUP;
788}
789
790#endif /* !linux */
791
792BlockDriver bdrv_host_device = {
793 "host_device",
794 sizeof(BDRVRawState),
795 NULL, /* no probe for protocols */
796 hdev_open,
797 NULL,
798 NULL,
799 raw_close,
800 NULL,
801 raw_flush,
802
803 .bdrv_aio_read = raw_aio_read,
804 .bdrv_aio_write = raw_aio_write,
805 .bdrv_aio_cancel = raw_aio_cancel,
806 .aiocb_size = sizeof(RawAIOCB),
807 .bdrv_pread = raw_pread,
808 .bdrv_pwrite = raw_pwrite,
809 .bdrv_getlength = raw_getlength,
810
811 /* removable device support */
812 .bdrv_is_inserted = raw_is_inserted,
813 .bdrv_media_changed = raw_media_changed,
814 .bdrv_eject = raw_eject,
815 .bdrv_set_locked = raw_set_locked,
816};
817
83f64091
FB
818#else /* _WIN32 */
819
820/* XXX: use another file ? */
83f64091
FB
821#include <winioctl.h>
822
19cb3738
FB
823#define FTYPE_FILE 0
824#define FTYPE_CD 1
825
83f64091
FB
826typedef struct BDRVRawState {
827 HANDLE hfile;
19cb3738 828 int type;
f45512fe 829 char drive_path[16]; /* format: "d:\" */
83f64091
FB
830} BDRVRawState;
831
832typedef struct RawAIOCB {
ce1a14dc 833 BlockDriverAIOCB common;
83f64091
FB
834 HANDLE hEvent;
835 OVERLAPPED ov;
836 int count;
837} RawAIOCB;
838
839int qemu_ftruncate64(int fd, int64_t length)
840{
841 LARGE_INTEGER li;
842 LONG high;
843 HANDLE h;
844 BOOL res;
845
846 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
847 return -1;
848
849 h = (HANDLE)_get_osfhandle(fd);
850
851 /* get current position, ftruncate do not change position */
852 li.HighPart = 0;
853 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
854 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
855 return -1;
856
857 high = length >> 32;
858 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
859 return -1;
860 res = SetEndOfFile(h);
861
862 /* back to old position */
863 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
864 return res ? 0 : -1;
865}
866
867static int set_sparse(int fd)
868{
869 DWORD returned;
870 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
871 NULL, 0, NULL, 0, &returned, NULL);
872}
873
874static int raw_open(BlockDriverState *bs, const char *filename, int flags)
875{
876 BDRVRawState *s = bs->opaque;
877 int access_flags, create_flags;
ce1a14dc 878 DWORD overlapped;
19cb3738 879
f45512fe 880 s->type = FTYPE_FILE;
83f64091
FB
881
882 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
883 access_flags = GENERIC_READ | GENERIC_WRITE;
884 } else {
885 access_flags = GENERIC_READ;
886 }
979b67ad 887 if (flags & BDRV_O_CREAT) {
83f64091
FB
888 create_flags = CREATE_ALWAYS;
889 } else {
890 create_flags = OPEN_EXISTING;
891 }
ce1a14dc
PB
892#ifdef QEMU_TOOL
893 overlapped = 0;
894#else
895 overlapped = FILE_FLAG_OVERLAPPED;
896#endif
83f64091
FB
897 s->hfile = CreateFile(filename, access_flags,
898 FILE_SHARE_READ, NULL,
ce1a14dc 899 create_flags, overlapped, 0);
83f64091
FB
900 if (s->hfile == INVALID_HANDLE_VALUE)
901 return -1;
902 return 0;
903}
904
905static int raw_pread(BlockDriverState *bs, int64_t offset,
906 uint8_t *buf, int count)
907{
908 BDRVRawState *s = bs->opaque;
909 OVERLAPPED ov;
910 DWORD ret_count;
911 int ret;
912
913 memset(&ov, 0, sizeof(ov));
914 ov.Offset = offset;
915 ov.OffsetHigh = offset >> 32;
436e15b8
FB
916 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
917 if (!ret) {
918 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
919 if (!ret)
920 return -EIO;
921 else
922 return ret_count;
923 }
83f64091
FB
924 return ret_count;
925}
926
927static int raw_pwrite(BlockDriverState *bs, int64_t offset,
928 const uint8_t *buf, int count)
929{
930 BDRVRawState *s = bs->opaque;
931 OVERLAPPED ov;
932 DWORD ret_count;
933 int ret;
934
935 memset(&ov, 0, sizeof(ov));
936 ov.Offset = offset;
937 ov.OffsetHigh = offset >> 32;
436e15b8
FB
938 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
939 if (!ret) {
940 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
941 if (!ret)
942 return -EIO;
943 else
944 return ret_count;
945 }
83f64091
FB
946 return ret_count;
947}
948
436e15b8 949#ifndef QEMU_TOOL
83f64091
FB
950static void raw_aio_cb(void *opaque)
951{
ce1a14dc
PB
952 RawAIOCB *acb = opaque;
953 BlockDriverState *bs = acb->common.bs;
979b67ad 954 BDRVRawState *s = bs->opaque;
83f64091
FB
955 DWORD ret_count;
956 int ret;
957
ce1a14dc
PB
958 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
959 if (!ret || ret_count != acb->count) {
960 acb->common.cb(acb->common.opaque, -EIO);
83f64091 961 } else {
ce1a14dc 962 acb->common.cb(acb->common.opaque, 0);
83f64091
FB
963 }
964}
436e15b8 965#endif
ce1a14dc
PB
966
967static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
968 int64_t sector_num, uint8_t *buf, int nb_sectors,
969 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 970{
ce1a14dc 971 RawAIOCB *acb;
83f64091
FB
972 int64_t offset;
973
ce1a14dc
PB
974 acb = qemu_aio_get(bs, cb, opaque);
975 if (acb->hEvent) {
976 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
977 if (!acb->hEvent) {
978 qemu_aio_release(acb);
979 return NULL;
980 }
981 }
982 memset(&acb->ov, 0, sizeof(acb->ov));
83f64091 983 offset = sector_num * 512;
ce1a14dc
PB
984 acb->ov.Offset = offset;
985 acb->ov.OffsetHigh = offset >> 32;
986 acb->ov.hEvent = acb->hEvent;
987 acb->count = nb_sectors * 512;
436e15b8 988#ifndef QEMU_TOOL
ce1a14dc 989 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
436e15b8 990#endif
ce1a14dc 991 return acb;
83f64091
FB
992}
993
ce1a14dc
PB
994static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
995 int64_t sector_num, uint8_t *buf, int nb_sectors,
996 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 997{
83f64091 998 BDRVRawState *s = bs->opaque;
ce1a14dc 999 RawAIOCB *acb;
83f64091 1000 int ret;
83f64091 1001
ce1a14dc
PB
1002 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1003 if (!acb)
1004 return NULL;
1005 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1006 if (!ret) {
1007 qemu_aio_release(acb);
1008 return NULL;
1009 }
1010#ifdef QEMU_TOOL
1011 qemu_aio_release(acb);
436e15b8 1012#endif
ce1a14dc 1013 return (BlockDriverAIOCB *)acb;
83f64091
FB
1014}
1015
ce1a14dc
PB
1016static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1017 int64_t sector_num, uint8_t *buf, int nb_sectors,
1018 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 1019{
83f64091 1020 BDRVRawState *s = bs->opaque;
ce1a14dc
PB
1021 RawAIOCB *acb;
1022 int ret;
83f64091 1023
ce1a14dc
PB
1024 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1025 if (!acb)
1026 return NULL;
1027 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1028 if (!ret) {
1029 qemu_aio_release(acb);
1030 return NULL;
1031 }
1032#ifdef QEMU_TOOL
1033 qemu_aio_release(acb);
436e15b8 1034#endif
ce1a14dc 1035 return (BlockDriverAIOCB *)acb;
83f64091
FB
1036}
1037
ce1a14dc 1038static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
83f64091 1039{
ce1a14dc
PB
1040#ifndef QEMU_TOOL
1041 RawAIOCB *acb = (RawAIOCB *)blockacb;
1042 BlockDriverState *bs = acb->common.bs;
1043 BDRVRawState *s = bs->opaque;
1044
1045 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1046 /* XXX: if more than one async I/O it is not correct */
1047 CancelIo(s->hfile);
1048 qemu_aio_release(acb);
1049#endif
83f64091
FB
1050}
1051
1052static void raw_flush(BlockDriverState *bs)
1053{
1054 /* XXX: add it */
1055}
1056
1057static void raw_close(BlockDriverState *bs)
1058{
1059 BDRVRawState *s = bs->opaque;
1060 CloseHandle(s->hfile);
1061}
1062
1063static int raw_truncate(BlockDriverState *bs, int64_t offset)
1064{
1065 BDRVRawState *s = bs->opaque;
1066 DWORD low, high;
1067
979b67ad
FB
1068 low = offset;
1069 high = offset >> 32;
83f64091
FB
1070 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1071 return -EIO;
1072 if (!SetEndOfFile(s->hfile))
1073 return -EIO;
1074 return 0;
1075}
1076
f45512fe 1077static int64_t raw_getlength(BlockDriverState *bs)
83f64091
FB
1078{
1079 BDRVRawState *s = bs->opaque;
1080 LARGE_INTEGER l;
19cb3738 1081 ULARGE_INTEGER available, total, total_free;
436e15b8 1082
f45512fe 1083 switch(s->type) {
19cb3738
FB
1084 case FTYPE_FILE:
1085 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1086 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1087 return -EIO;
1088 break;
1089 case FTYPE_CD:
f45512fe 1090 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
19cb3738 1091 return -EIO;
f45512fe 1092 l.QuadPart = total.QuadPart;
19cb3738
FB
1093 break;
1094 default:
1095 return -EIO;
1096 }
83f64091
FB
1097 return l.QuadPart;
1098}
1099
1100static int raw_create(const char *filename, int64_t total_size,
1101 const char *backing_file, int flags)
1102{
1103 int fd;
1104
1105 if (flags || backing_file)
1106 return -ENOTSUP;
1107
1108 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1109 0644);
1110 if (fd < 0)
1111 return -EIO;
1112 set_sparse(fd);
1113 ftruncate(fd, total_size * 512);
1114 close(fd);
1115 return 0;
1116}
1117
1118void qemu_aio_init(void)
1119{
1120}
1121
1122void qemu_aio_poll(void)
1123{
1124}
1125
1126void qemu_aio_wait_start(void)
1127{
1128}
1129
1130void qemu_aio_wait(void)
1131{
fd44d818
FB
1132#ifndef QEMU_TOOL
1133 qemu_bh_poll();
1134#endif
83f64091
FB
1135}
1136
1137void qemu_aio_wait_end(void)
1138{
1139}
1140
1141BlockDriver bdrv_raw = {
1142 "raw",
1143 sizeof(BDRVRawState),
1144 NULL, /* no probe for protocols */
1145 raw_open,
1146 NULL,
1147 NULL,
1148 raw_close,
1149 raw_create,
1150 raw_flush,
1151
1152#if 0
83f64091
FB
1153 .bdrv_aio_read = raw_aio_read,
1154 .bdrv_aio_write = raw_aio_write,
1155 .bdrv_aio_cancel = raw_aio_cancel,
ce1a14dc 1156 .aiocb_size = sizeof(RawAIOCB);
83f64091
FB
1157#endif
1158 .protocol_name = "file",
1159 .bdrv_pread = raw_pread,
1160 .bdrv_pwrite = raw_pwrite,
1161 .bdrv_truncate = raw_truncate,
1162 .bdrv_getlength = raw_getlength,
1163};
19cb3738
FB
1164
1165/***********************************************/
1166/* host device */
1167
1168static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1169{
1170 char drives[256], *pdrv = drives;
1171 UINT type;
1172
f45512fe 1173 memset(drives, 0, sizeof(drives));
19cb3738
FB
1174 GetLogicalDriveStrings(sizeof(drives), drives);
1175 while(pdrv[0] != '\0') {
1176 type = GetDriveType(pdrv);
1177 switch(type) {
1178 case DRIVE_CDROM:
1179 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1180 return 0;
1181 break;
1182 }
1183 pdrv += lstrlen(pdrv) + 1;
1184 }
1185 return -1;
1186}
1187
f45512fe 1188static int find_device_type(BlockDriverState *bs, const char *filename)
19cb3738 1189{
f45512fe 1190 BDRVRawState *s = bs->opaque;
19cb3738
FB
1191 UINT type;
1192 const char *p;
1193
1194 if (strstart(filename, "\\\\.\\", &p) ||
1195 strstart(filename, "//./", &p)) {
f45512fe
FB
1196 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1197 type = GetDriveType(s->drive_path);
19cb3738
FB
1198 if (type == DRIVE_CDROM)
1199 return FTYPE_CD;
1200 else
1201 return FTYPE_FILE;
1202 } else {
1203 return FTYPE_FILE;
1204 }
1205}
1206
1207static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1208{
1209 BDRVRawState *s = bs->opaque;
1210 int access_flags, create_flags;
1211 DWORD overlapped;
1212 char device_name[64];
19cb3738
FB
1213
1214 if (strstart(filename, "/dev/cdrom", NULL)) {
1215 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1216 return -ENOENT;
1217 filename = device_name;
1218 } else {
1219 /* transform drive letters into device name */
1220 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1221 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1222 filename[1] == ':' && filename[2] == '\0') {
1223 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1224 filename = device_name;
1225 }
1226 }
f45512fe 1227 s->type = find_device_type(bs, filename);
19cb3738
FB
1228
1229 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1230 access_flags = GENERIC_READ | GENERIC_WRITE;
1231 } else {
1232 access_flags = GENERIC_READ;
1233 }
1234 create_flags = OPEN_EXISTING;
1235
1236#ifdef QEMU_TOOL
1237 overlapped = 0;
1238#else
1239 overlapped = FILE_FLAG_OVERLAPPED;
1240#endif
1241 s->hfile = CreateFile(filename, access_flags,
1242 FILE_SHARE_READ, NULL,
1243 create_flags, overlapped, 0);
1244 if (s->hfile == INVALID_HANDLE_VALUE)
1245 return -1;
1246 return 0;
1247}
1248
1249#if 0
1250/***********************************************/
1251/* removable device additionnal commands */
1252
1253static int raw_is_inserted(BlockDriverState *bs)
1254{
1255 return 1;
1256}
1257
1258static int raw_media_changed(BlockDriverState *bs)
1259{
1260 return -ENOTSUP;
1261}
1262
1263static int raw_eject(BlockDriverState *bs, int eject_flag)
1264{
1265 DWORD ret_count;
1266
1267 if (s->type == FTYPE_FILE)
1268 return -ENOTSUP;
1269 if (eject_flag) {
1270 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1271 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1272 } else {
1273 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1274 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1275 }
1276}
1277
1278static int raw_set_locked(BlockDriverState *bs, int locked)
1279{
1280 return -ENOTSUP;
1281}
1282#endif
1283
1284BlockDriver bdrv_host_device = {
1285 "host_device",
1286 sizeof(BDRVRawState),
1287 NULL, /* no probe for protocols */
1288 hdev_open,
1289 NULL,
1290 NULL,
1291 raw_close,
1292 NULL,
1293 raw_flush,
1294
1295#if 0
1296 .bdrv_aio_read = raw_aio_read,
1297 .bdrv_aio_write = raw_aio_write,
1298 .bdrv_aio_cancel = raw_aio_cancel,
1299 .aiocb_size = sizeof(RawAIOCB);
1300#endif
1301 .bdrv_pread = raw_pread,
1302 .bdrv_pwrite = raw_pwrite,
1303 .bdrv_getlength = raw_getlength,
1304};
83f64091 1305#endif /* _WIN32 */