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