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