]> Git Repo - qemu.git/blob - block/raw-posix.c
985bf69e5091d02c26c3ac645b31365c4f69c5ac
[qemu.git] / 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 "module.h"
29 #ifdef CONFIG_AIO
30 #include "posix-aio-compat.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 #include <sys/cdio.h>
59 #endif
60
61 #ifdef __OpenBSD__
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
64 #include <sys/dkio.h>
65 #endif
66
67 #ifdef __DragonFly__
68 #include <sys/ioctl.h>
69 #include <sys/diskslice.h>
70 #endif
71
72 //#define DEBUG_FLOPPY
73
74 //#define DEBUG_BLOCK
75 #if defined(DEBUG_BLOCK)
76 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
77     { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
78 #else
79 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
80 #endif
81
82 /* OS X does not have O_DSYNC */
83 #ifndef O_DSYNC
84 #define O_DSYNC O_SYNC
85 #endif
86
87 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
88 #ifndef O_DIRECT
89 #define O_DIRECT O_DSYNC
90 #endif
91
92 #define FTYPE_FILE   0
93 #define FTYPE_CD     1
94 #define FTYPE_FD     2
95
96 #define ALIGNED_BUFFER_SIZE (32 * 512)
97
98 /* if the FD is not accessed during that time (in ms), we try to
99    reopen it to see if the disk has been changed */
100 #define FD_OPEN_TIMEOUT 1000
101
102 typedef struct BDRVRawState {
103     int fd;
104     int type;
105     unsigned int lseek_err_cnt;
106     int open_flags;
107 #if defined(__linux__)
108     /* linux floppy specific */
109     int64_t fd_open_time;
110     int64_t fd_error_time;
111     int fd_got_error;
112     int fd_media_changed;
113 #endif
114     uint8_t* aligned_buf;
115 } BDRVRawState;
116
117 static int posix_aio_init(void);
118
119 static int fd_open(BlockDriverState *bs);
120 static int64_t raw_getlength(BlockDriverState *bs);
121
122 #if defined(__FreeBSD__)
123 static int cdrom_reopen(BlockDriverState *bs);
124 #endif
125
126 static int raw_open_common(BlockDriverState *bs, const char *filename,
127                            int bdrv_flags, int open_flags)
128 {
129     BDRVRawState *s = bs->opaque;
130     int fd, ret;
131
132     posix_aio_init();
133
134     s->lseek_err_cnt = 0;
135
136     s->open_flags = open_flags | O_BINARY;
137     s->open_flags &= ~O_ACCMODE;
138     if ((bdrv_flags & BDRV_O_ACCESS) == BDRV_O_RDWR) {
139         s->open_flags |= O_RDWR;
140     } else {
141         s->open_flags |= O_RDONLY;
142         bs->read_only = 1;
143     }
144
145     /* Use O_DSYNC for write-through caching, no flags for write-back caching,
146      * and O_DIRECT for no caching. */
147     if ((bdrv_flags & BDRV_O_NOCACHE))
148         s->open_flags |= O_DIRECT;
149     else if (!(bdrv_flags & BDRV_O_CACHE_WB))
150         s->open_flags |= O_DSYNC;
151
152     s->fd = -1;
153     fd = open(filename, s->open_flags, 0644);
154     if (fd < 0) {
155         ret = -errno;
156         if (ret == -EROFS)
157             ret = -EACCES;
158         return ret;
159     }
160     s->fd = fd;
161     s->aligned_buf = NULL;
162     if ((bdrv_flags & BDRV_O_NOCACHE)) {
163         s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE);
164         if (s->aligned_buf == NULL) {
165             ret = -errno;
166             close(fd);
167             return ret;
168         }
169     }
170     return 0;
171 }
172
173 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
174 {
175     BDRVRawState *s = bs->opaque;
176     int open_flags = 0;
177
178     s->type = FTYPE_FILE;
179     if (flags & BDRV_O_CREAT)
180         open_flags = O_CREAT | O_TRUNC;
181
182     return raw_open_common(bs, filename, flags, open_flags);
183 }
184
185 /* XXX: use host sector size if necessary with:
186 #ifdef DIOCGSECTORSIZE
187         {
188             unsigned int sectorsize = 512;
189             if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
190                 sectorsize > bufsize)
191                 bufsize = sectorsize;
192         }
193 #endif
194 #ifdef CONFIG_COCOA
195         u_int32_t   blockSize = 512;
196         if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
197             bufsize = blockSize;
198         }
199 #endif
200 */
201
202 /*
203  * offset and count are in bytes, but must be multiples of 512 for files
204  * opened with O_DIRECT. buf must be aligned to 512 bytes then.
205  *
206  * This function may be called without alignment if the caller ensures
207  * that O_DIRECT is not in effect.
208  */
209 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
210                      uint8_t *buf, int count)
211 {
212     BDRVRawState *s = bs->opaque;
213     int ret;
214
215     ret = fd_open(bs);
216     if (ret < 0)
217         return ret;
218
219     if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
220         ++(s->lseek_err_cnt);
221         if(s->lseek_err_cnt <= 10) {
222             DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
223                               "] lseek failed : %d = %s\n",
224                               s->fd, bs->filename, offset, buf, count,
225                               bs->total_sectors, errno, strerror(errno));
226         }
227         return -1;
228     }
229     s->lseek_err_cnt=0;
230
231     ret = read(s->fd, buf, count);
232     if (ret == count)
233         goto label__raw_read__success;
234
235     /* Allow reads beyond the end (needed for pwrite) */
236     if ((ret == 0) && bs->growable) {
237         int64_t size = raw_getlength(bs);
238         if (offset >= size) {
239             memset(buf, 0, count);
240             ret = count;
241             goto label__raw_read__success;
242         }
243     }
244
245     DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
246                       "] read failed %d : %d = %s\n",
247                       s->fd, bs->filename, offset, buf, count,
248                       bs->total_sectors, ret, errno, strerror(errno));
249
250     /* Try harder for CDrom. */
251     if (bs->type == BDRV_TYPE_CDROM) {
252         lseek(s->fd, offset, SEEK_SET);
253         ret = read(s->fd, buf, count);
254         if (ret == count)
255             goto label__raw_read__success;
256         lseek(s->fd, offset, SEEK_SET);
257         ret = read(s->fd, buf, count);
258         if (ret == count)
259             goto label__raw_read__success;
260
261         DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
262                           "] retry read failed %d : %d = %s\n",
263                           s->fd, bs->filename, offset, buf, count,
264                           bs->total_sectors, ret, errno, strerror(errno));
265     }
266
267 label__raw_read__success:
268
269     return  (ret < 0) ? -errno : ret;
270 }
271
272 /*
273  * offset and count are in bytes, but must be multiples of 512 for files
274  * opened with O_DIRECT. buf must be aligned to 512 bytes then.
275  *
276  * This function may be called without alignment if the caller ensures
277  * that O_DIRECT is not in effect.
278  */
279 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
280                       const uint8_t *buf, int count)
281 {
282     BDRVRawState *s = bs->opaque;
283     int ret;
284
285     ret = fd_open(bs);
286     if (ret < 0)
287         return -errno;
288
289     if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
290         ++(s->lseek_err_cnt);
291         if(s->lseek_err_cnt) {
292             DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
293                               PRId64 "] lseek failed : %d = %s\n",
294                               s->fd, bs->filename, offset, buf, count,
295                               bs->total_sectors, errno, strerror(errno));
296         }
297         return -EIO;
298     }
299     s->lseek_err_cnt = 0;
300
301     ret = write(s->fd, buf, count);
302     if (ret == count)
303         goto label__raw_write__success;
304
305     DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
306                       "] write failed %d : %d = %s\n",
307                       s->fd, bs->filename, offset, buf, count,
308                       bs->total_sectors, ret, errno, strerror(errno));
309
310 label__raw_write__success:
311
312     return  (ret < 0) ? -errno : ret;
313 }
314
315
316 /*
317  * offset and count are in bytes and possibly not aligned. For files opened
318  * with O_DIRECT, necessary alignments are ensured before calling
319  * raw_pread_aligned to do the actual read.
320  */
321 static int raw_pread(BlockDriverState *bs, int64_t offset,
322                      uint8_t *buf, int count)
323 {
324     BDRVRawState *s = bs->opaque;
325     int size, ret, shift, sum;
326
327     sum = 0;
328
329     if (s->aligned_buf != NULL)  {
330
331         if (offset & 0x1ff) {
332             /* align offset on a 512 bytes boundary */
333
334             shift = offset & 0x1ff;
335             size = (shift + count + 0x1ff) & ~0x1ff;
336             if (size > ALIGNED_BUFFER_SIZE)
337                 size = ALIGNED_BUFFER_SIZE;
338             ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
339             if (ret < 0)
340                 return ret;
341
342             size = 512 - shift;
343             if (size > count)
344                 size = count;
345             memcpy(buf, s->aligned_buf + shift, size);
346
347             buf += size;
348             offset += size;
349             count -= size;
350             sum += size;
351
352             if (count == 0)
353                 return sum;
354         }
355         if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
356
357             /* read on aligned buffer */
358
359             while (count) {
360
361                 size = (count + 0x1ff) & ~0x1ff;
362                 if (size > ALIGNED_BUFFER_SIZE)
363                     size = ALIGNED_BUFFER_SIZE;
364
365                 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
366                 if (ret < 0)
367                     return ret;
368
369                 size = ret;
370                 if (size > count)
371                     size = count;
372
373                 memcpy(buf, s->aligned_buf, size);
374
375                 buf += size;
376                 offset += size;
377                 count -= size;
378                 sum += size;
379             }
380
381             return sum;
382         }
383     }
384
385     return raw_pread_aligned(bs, offset, buf, count) + sum;
386 }
387
388 static int raw_read(BlockDriverState *bs, int64_t sector_num,
389                     uint8_t *buf, int nb_sectors)
390 {
391     int ret;
392
393     ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
394     if (ret == (nb_sectors * 512))
395         ret = 0;
396     return ret;
397 }
398
399 /*
400  * offset and count are in bytes and possibly not aligned. For files opened
401  * with O_DIRECT, necessary alignments are ensured before calling
402  * raw_pwrite_aligned to do the actual write.
403  */
404 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
405                       const uint8_t *buf, int count)
406 {
407     BDRVRawState *s = bs->opaque;
408     int size, ret, shift, sum;
409
410     sum = 0;
411
412     if (s->aligned_buf != NULL) {
413
414         if (offset & 0x1ff) {
415             /* align offset on a 512 bytes boundary */
416             shift = offset & 0x1ff;
417             ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
418             if (ret < 0)
419                 return ret;
420
421             size = 512 - shift;
422             if (size > count)
423                 size = count;
424             memcpy(s->aligned_buf + shift, buf, size);
425
426             ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
427             if (ret < 0)
428                 return ret;
429
430             buf += size;
431             offset += size;
432             count -= size;
433             sum += size;
434
435             if (count == 0)
436                 return sum;
437         }
438         if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
439
440             while ((size = (count & ~0x1ff)) != 0) {
441
442                 if (size > ALIGNED_BUFFER_SIZE)
443                     size = ALIGNED_BUFFER_SIZE;
444
445                 memcpy(s->aligned_buf, buf, size);
446
447                 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
448                 if (ret < 0)
449                     return ret;
450
451                 buf += ret;
452                 offset += ret;
453                 count -= ret;
454                 sum += ret;
455             }
456             /* here, count < 512 because (count & ~0x1ff) == 0 */
457             if (count) {
458                 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
459                 if (ret < 0)
460                     return ret;
461                  memcpy(s->aligned_buf, buf, count);
462
463                  ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
464                  if (ret < 0)
465                      return ret;
466                  if (count < ret)
467                      ret = count;
468
469                  sum += ret;
470             }
471             return sum;
472         }
473     }
474     return raw_pwrite_aligned(bs, offset, buf, count) + sum;
475 }
476
477 static int raw_write(BlockDriverState *bs, int64_t sector_num,
478                      const uint8_t *buf, int nb_sectors)
479 {
480     int ret;
481     ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512);
482     if (ret == (nb_sectors * 512))
483         ret = 0;
484     return ret;
485 }
486
487 #ifdef CONFIG_AIO
488 /***********************************************************/
489 /* Unix AIO using POSIX AIO */
490
491 typedef struct RawAIOCB {
492     BlockDriverAIOCB common;
493     struct qemu_paiocb aiocb;
494     struct RawAIOCB *next;
495     int ret;
496 } RawAIOCB;
497
498 typedef struct PosixAioState
499 {
500     int rfd, wfd;
501     RawAIOCB *first_aio;
502 } PosixAioState;
503
504 static void posix_aio_read(void *opaque)
505 {
506     PosixAioState *s = opaque;
507     RawAIOCB *acb, **pacb;
508     int ret;
509     ssize_t len;
510
511     /* read all bytes from signal pipe */
512     for (;;) {
513         char bytes[16];
514
515         len = read(s->rfd, bytes, sizeof(bytes));
516         if (len == -1 && errno == EINTR)
517             continue; /* try again */
518         if (len == sizeof(bytes))
519             continue; /* more to read */
520         break;
521     }
522
523     for(;;) {
524         pacb = &s->first_aio;
525         for(;;) {
526             acb = *pacb;
527             if (!acb)
528                 goto the_end;
529             ret = qemu_paio_error(&acb->aiocb);
530             if (ret == ECANCELED) {
531                 /* remove the request */
532                 *pacb = acb->next;
533                 qemu_aio_release(acb);
534             } else if (ret != EINPROGRESS) {
535                 /* end of aio */
536                 if (ret == 0) {
537                     ret = qemu_paio_return(&acb->aiocb);
538                     if (ret == acb->aiocb.aio_nbytes)
539                         ret = 0;
540                     else
541                         ret = -EINVAL;
542                 } else {
543                     ret = -ret;
544                 }
545                 /* remove the request */
546                 *pacb = acb->next;
547                 /* call the callback */
548                 acb->common.cb(acb->common.opaque, ret);
549                 qemu_aio_release(acb);
550                 break;
551             } else {
552                 pacb = &acb->next;
553             }
554         }
555     }
556  the_end: ;
557 }
558
559 static int posix_aio_flush(void *opaque)
560 {
561     PosixAioState *s = opaque;
562     return !!s->first_aio;
563 }
564
565 static PosixAioState *posix_aio_state;
566
567 static void aio_signal_handler(int signum)
568 {
569     if (posix_aio_state) {
570         char byte = 0;
571
572         write(posix_aio_state->wfd, &byte, sizeof(byte));
573     }
574
575     qemu_service_io();
576 }
577
578 static int posix_aio_init(void)
579 {
580     struct sigaction act;
581     PosixAioState *s;
582     int fds[2];
583     struct qemu_paioinit ai;
584   
585     if (posix_aio_state)
586         return 0;
587
588     s = qemu_malloc(sizeof(PosixAioState));
589
590     sigfillset(&act.sa_mask);
591     act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
592     act.sa_handler = aio_signal_handler;
593     sigaction(SIGUSR2, &act, NULL);
594
595     s->first_aio = NULL;
596     if (pipe(fds) == -1) {
597         fprintf(stderr, "failed to create pipe\n");
598         return -errno;
599     }
600
601     s->rfd = fds[0];
602     s->wfd = fds[1];
603
604     fcntl(s->rfd, F_SETFL, O_NONBLOCK);
605     fcntl(s->wfd, F_SETFL, O_NONBLOCK);
606
607     qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
608
609     memset(&ai, 0, sizeof(ai));
610     ai.aio_threads = 64;
611     ai.aio_num = 64;
612     qemu_paio_init(&ai);
613
614     posix_aio_state = s;
615
616     return 0;
617 }
618
619 static void raw_aio_remove(RawAIOCB *acb)
620 {
621     RawAIOCB **pacb;
622
623     /* remove the callback from the queue */
624     pacb = &posix_aio_state->first_aio;
625     for(;;) {
626         if (*pacb == NULL) {
627             fprintf(stderr, "raw_aio_remove: aio request not found!\n");
628             break;
629         } else if (*pacb == acb) {
630             *pacb = acb->next;
631             qemu_aio_release(acb);
632             break;
633         }
634         pacb = &(*pacb)->next;
635     }
636 }
637
638 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
639 {
640     int ret;
641     RawAIOCB *acb = (RawAIOCB *)blockacb;
642
643     ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
644     if (ret == QEMU_PAIO_NOTCANCELED) {
645         /* fail safe: if the aio could not be canceled, we wait for
646            it */
647         while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
648     }
649
650     raw_aio_remove(acb);
651 }
652
653 static AIOPool raw_aio_pool = {
654     .aiocb_size         = sizeof(RawAIOCB),
655     .cancel             = raw_aio_cancel,
656 };
657
658 static RawAIOCB *raw_aio_setup(BlockDriverState *bs, int64_t sector_num,
659         QEMUIOVector *qiov, int nb_sectors,
660         BlockDriverCompletionFunc *cb, void *opaque)
661 {
662     BDRVRawState *s = bs->opaque;
663     RawAIOCB *acb;
664
665     if (fd_open(bs) < 0)
666         return NULL;
667
668     acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
669     if (!acb)
670         return NULL;
671     acb->aiocb.aio_fildes = s->fd;
672     acb->aiocb.ev_signo = SIGUSR2;
673     acb->aiocb.aio_iov = qiov->iov;
674     acb->aiocb.aio_niov = qiov->niov;
675     acb->aiocb.aio_nbytes = nb_sectors * 512;
676     acb->aiocb.aio_offset = sector_num * 512;
677     acb->aiocb.aio_flags = 0;
678
679     /*
680      * If O_DIRECT is used the buffer needs to be aligned on a sector
681      * boundary. Tell the low level code to ensure that in case it's
682      * not done yet.
683      */
684     if (s->aligned_buf)
685         acb->aiocb.aio_flags |= QEMU_AIO_SECTOR_ALIGNED;
686
687     acb->next = posix_aio_state->first_aio;
688     posix_aio_state->first_aio = acb;
689     return acb;
690 }
691
692 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
693         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
694         BlockDriverCompletionFunc *cb, void *opaque)
695 {
696     RawAIOCB *acb;
697
698     acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque);
699     if (!acb)
700         return NULL;
701     if (qemu_paio_read(&acb->aiocb) < 0) {
702         raw_aio_remove(acb);
703         return NULL;
704     }
705     return &acb->common;
706 }
707
708 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
709         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
710         BlockDriverCompletionFunc *cb, void *opaque)
711 {
712     RawAIOCB *acb;
713
714     acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque);
715     if (!acb)
716         return NULL;
717     if (qemu_paio_write(&acb->aiocb) < 0) {
718         raw_aio_remove(acb);
719         return NULL;
720     }
721     return &acb->common;
722 }
723 #else /* CONFIG_AIO */
724 static int posix_aio_init(void)
725 {
726     return 0;
727 }
728 #endif /* CONFIG_AIO */
729
730
731 static void raw_close(BlockDriverState *bs)
732 {
733     BDRVRawState *s = bs->opaque;
734     if (s->fd >= 0) {
735         close(s->fd);
736         s->fd = -1;
737         if (s->aligned_buf != NULL)
738             qemu_free(s->aligned_buf);
739     }
740 }
741
742 static int raw_truncate(BlockDriverState *bs, int64_t offset)
743 {
744     BDRVRawState *s = bs->opaque;
745     if (s->type != FTYPE_FILE)
746         return -ENOTSUP;
747     if (ftruncate(s->fd, offset) < 0)
748         return -errno;
749     return 0;
750 }
751
752 #ifdef __OpenBSD__
753 static int64_t raw_getlength(BlockDriverState *bs)
754 {
755     BDRVRawState *s = bs->opaque;
756     int fd = s->fd;
757     struct stat st;
758
759     if (fstat(fd, &st))
760         return -1;
761     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
762         struct disklabel dl;
763
764         if (ioctl(fd, DIOCGDINFO, &dl))
765             return -1;
766         return (uint64_t)dl.d_secsize *
767             dl.d_partitions[DISKPART(st.st_rdev)].p_size;
768     } else
769         return st.st_size;
770 }
771 #else /* !__OpenBSD__ */
772 static int64_t  raw_getlength(BlockDriverState *bs)
773 {
774     BDRVRawState *s = bs->opaque;
775     int fd = s->fd;
776     int64_t size;
777 #ifdef HOST_BSD
778     struct stat sb;
779 #ifdef __FreeBSD__
780     int reopened = 0;
781 #endif
782 #endif
783 #ifdef __sun__
784     struct dk_minfo minfo;
785     int rv;
786 #endif
787     int ret;
788
789     ret = fd_open(bs);
790     if (ret < 0)
791         return ret;
792
793 #ifdef HOST_BSD
794 #ifdef __FreeBSD__
795 again:
796 #endif
797     if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
798 #ifdef DIOCGMEDIASIZE
799         if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
800 #elif defined(DIOCGPART)
801         {
802                 struct partinfo pi;
803                 if (ioctl(fd, DIOCGPART, &pi) == 0)
804                         size = pi.media_size;
805                 else
806                         size = 0;
807         }
808         if (size == 0)
809 #endif
810 #ifdef CONFIG_COCOA
811         size = LONG_LONG_MAX;
812 #else
813         size = lseek(fd, 0LL, SEEK_END);
814 #endif
815 #ifdef __FreeBSD__
816         switch(s->type) {
817         case FTYPE_CD:
818             /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
819             if (size == 2048LL * (unsigned)-1)
820                 size = 0;
821             /* XXX no disc?  maybe we need to reopen... */
822             if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
823                 reopened = 1;
824                 goto again;
825             }
826         }
827 #endif
828     } else
829 #endif
830 #ifdef __sun__
831     /*
832      * use the DKIOCGMEDIAINFO ioctl to read the size.
833      */
834     rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
835     if ( rv != -1 ) {
836         size = minfo.dki_lbsize * minfo.dki_capacity;
837     } else /* there are reports that lseek on some devices
838               fails, but irc discussion said that contingency
839               on contingency was overkill */
840 #endif
841     {
842         size = lseek(fd, 0, SEEK_END);
843     }
844     return size;
845 }
846 #endif
847
848 static int raw_create(const char *filename, QEMUOptionParameter *options)
849 {
850     int fd;
851     int64_t total_size = 0;
852
853     /* Read out options */
854     while (options && options->name) {
855         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
856             total_size = options->value.n / 512;
857         }
858         options++;
859     }
860
861     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
862               0644);
863     if (fd < 0)
864         return -EIO;
865     ftruncate(fd, total_size * 512);
866     close(fd);
867     return 0;
868 }
869
870 static void raw_flush(BlockDriverState *bs)
871 {
872     BDRVRawState *s = bs->opaque;
873     fsync(s->fd);
874 }
875
876
877 static QEMUOptionParameter raw_create_options[] = {
878     {
879         .name = BLOCK_OPT_SIZE,
880         .type = OPT_SIZE,
881         .help = "Virtual disk size"
882     },
883     { NULL }
884 };
885
886 static BlockDriver bdrv_raw = {
887     .format_name = "raw",
888     .instance_size = sizeof(BDRVRawState),
889     .bdrv_probe = NULL, /* no probe for protocols */
890     .bdrv_open = raw_open,
891     .bdrv_read = raw_read,
892     .bdrv_write = raw_write,
893     .bdrv_close = raw_close,
894     .bdrv_create = raw_create,
895     .bdrv_flush = raw_flush,
896
897 #ifdef CONFIG_AIO
898     .bdrv_aio_readv = raw_aio_readv,
899     .bdrv_aio_writev = raw_aio_writev,
900 #endif
901
902     .bdrv_truncate = raw_truncate,
903     .bdrv_getlength = raw_getlength,
904
905     .create_options = raw_create_options,
906 };
907
908 /***********************************************/
909 /* host device */
910
911 #ifdef CONFIG_COCOA
912 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
913 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
914
915 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
916 {
917     kern_return_t       kernResult;
918     mach_port_t     masterPort;
919     CFMutableDictionaryRef  classesToMatch;
920
921     kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
922     if ( KERN_SUCCESS != kernResult ) {
923         printf( "IOMasterPort returned %d\n", kernResult );
924     }
925
926     classesToMatch = IOServiceMatching( kIOCDMediaClass );
927     if ( classesToMatch == NULL ) {
928         printf( "IOServiceMatching returned a NULL dictionary.\n" );
929     } else {
930     CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
931     }
932     kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
933     if ( KERN_SUCCESS != kernResult )
934     {
935         printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
936     }
937
938     return kernResult;
939 }
940
941 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
942 {
943     io_object_t     nextMedia;
944     kern_return_t   kernResult = KERN_FAILURE;
945     *bsdPath = '\0';
946     nextMedia = IOIteratorNext( mediaIterator );
947     if ( nextMedia )
948     {
949         CFTypeRef   bsdPathAsCFString;
950     bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
951         if ( bsdPathAsCFString ) {
952             size_t devPathLength;
953             strcpy( bsdPath, _PATH_DEV );
954             strcat( bsdPath, "r" );
955             devPathLength = strlen( bsdPath );
956             if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
957                 kernResult = KERN_SUCCESS;
958             }
959             CFRelease( bsdPathAsCFString );
960         }
961         IOObjectRelease( nextMedia );
962     }
963
964     return kernResult;
965 }
966
967 #endif
968
969 static int hdev_probe_device(const char *filename)
970 {
971     struct stat st;
972
973     /* allow a dedicated CD-ROM driver to match with a higher priority */
974     if (strstart(filename, "/dev/cdrom", NULL))
975         return 50;
976
977     if (stat(filename, &st) >= 0 &&
978             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
979         return 100;
980     }
981
982     return 0;
983 }
984
985 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
986 {
987     BDRVRawState *s = bs->opaque;
988
989 #ifdef CONFIG_COCOA
990     if (strstart(filename, "/dev/cdrom", NULL)) {
991         kern_return_t kernResult;
992         io_iterator_t mediaIterator;
993         char bsdPath[ MAXPATHLEN ];
994         int fd;
995
996         kernResult = FindEjectableCDMedia( &mediaIterator );
997         kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
998
999         if ( bsdPath[ 0 ] != '\0' ) {
1000             strcat(bsdPath,"s0");
1001             /* some CDs don't have a partition 0 */
1002             fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1003             if (fd < 0) {
1004                 bsdPath[strlen(bsdPath)-1] = '1';
1005             } else {
1006                 close(fd);
1007             }
1008             filename = bsdPath;
1009         }
1010
1011         if ( mediaIterator )
1012             IOObjectRelease( mediaIterator );
1013     }
1014 #endif
1015
1016     s->type = FTYPE_FILE;
1017 #if defined(__linux__) && defined(CONFIG_AIO)
1018     if (strstart(filename, "/dev/sg", NULL)) {
1019         bs->sg = 1;
1020     }
1021 #endif
1022
1023     return raw_open_common(bs, filename, flags, 0);
1024 }
1025
1026 #if defined(__linux__)
1027 /* Note: we do not have a reliable method to detect if the floppy is
1028    present. The current method is to try to open the floppy at every
1029    I/O and to keep it opened during a few hundreds of ms. */
1030 static int fd_open(BlockDriverState *bs)
1031 {
1032     BDRVRawState *s = bs->opaque;
1033     int last_media_present;
1034
1035     if (s->type != FTYPE_FD)
1036         return 0;
1037     last_media_present = (s->fd >= 0);
1038     if (s->fd >= 0 &&
1039         (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1040         close(s->fd);
1041         s->fd = -1;
1042 #ifdef DEBUG_FLOPPY
1043         printf("Floppy closed\n");
1044 #endif
1045     }
1046     if (s->fd < 0) {
1047         if (s->fd_got_error &&
1048             (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1049 #ifdef DEBUG_FLOPPY
1050             printf("No floppy (open delayed)\n");
1051 #endif
1052             return -EIO;
1053         }
1054         s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
1055         if (s->fd < 0) {
1056             s->fd_error_time = qemu_get_clock(rt_clock);
1057             s->fd_got_error = 1;
1058             if (last_media_present)
1059                 s->fd_media_changed = 1;
1060 #ifdef DEBUG_FLOPPY
1061             printf("No floppy\n");
1062 #endif
1063             return -EIO;
1064         }
1065 #ifdef DEBUG_FLOPPY
1066         printf("Floppy opened\n");
1067 #endif
1068     }
1069     if (!last_media_present)
1070         s->fd_media_changed = 1;
1071     s->fd_open_time = qemu_get_clock(rt_clock);
1072     s->fd_got_error = 0;
1073     return 0;
1074 }
1075
1076 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1077 {
1078     BDRVRawState *s = bs->opaque;
1079
1080     return ioctl(s->fd, req, buf);
1081 }
1082
1083 #ifdef CONFIG_AIO
1084 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1085         unsigned long int req, void *buf,
1086         BlockDriverCompletionFunc *cb, void *opaque)
1087 {
1088     BDRVRawState *s = bs->opaque;
1089     RawAIOCB *acb;
1090
1091     if (fd_open(bs) < 0)
1092         return NULL;
1093
1094     acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
1095     if (!acb)
1096         return NULL;
1097     acb->aiocb.aio_fildes = s->fd;
1098     acb->aiocb.ev_signo = SIGUSR2;
1099     acb->aiocb.aio_offset = 0;
1100     acb->aiocb.aio_flags = 0;
1101
1102     acb->next = posix_aio_state->first_aio;
1103     posix_aio_state->first_aio = acb;
1104
1105     acb->aiocb.aio_ioctl_buf = buf;
1106     acb->aiocb.aio_ioctl_cmd = req;
1107     if (qemu_paio_ioctl(&acb->aiocb) < 0) {
1108         raw_aio_remove(acb);
1109         return NULL;
1110     }
1111
1112     return &acb->common;
1113 }
1114 #endif
1115
1116 #elif defined(__FreeBSD__)
1117 static int fd_open(BlockDriverState *bs)
1118 {
1119     BDRVRawState *s = bs->opaque;
1120
1121     /* this is just to ensure s->fd is sane (its called by io ops) */
1122     if (s->fd >= 0)
1123         return 0;
1124     return -EIO;
1125 }
1126 #else /* !linux && !FreeBSD */
1127
1128 static int fd_open(BlockDriverState *bs)
1129 {
1130     return 0;
1131 }
1132
1133 #endif /* !linux && !FreeBSD */
1134
1135 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1136 {
1137     int fd;
1138     int ret = 0;
1139     struct stat stat_buf;
1140     int64_t total_size = 0;
1141
1142     /* Read out options */
1143     while (options && options->name) {
1144         if (!strcmp(options->name, "size")) {
1145             total_size = options->value.n / 512;
1146         }
1147         options++;
1148     }
1149
1150     fd = open(filename, O_WRONLY | O_BINARY);
1151     if (fd < 0)
1152         return -EIO;
1153
1154     if (fstat(fd, &stat_buf) < 0)
1155         ret = -EIO;
1156     else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1157         ret = -EIO;
1158     else if (lseek(fd, 0, SEEK_END) < total_size * 512)
1159         ret = -ENOSPC;
1160
1161     close(fd);
1162     return ret;
1163 }
1164
1165 static BlockDriver bdrv_host_device = {
1166     .format_name        = "host_device",
1167     .instance_size      = sizeof(BDRVRawState),
1168     .bdrv_probe_device  = hdev_probe_device,
1169     .bdrv_open          = hdev_open,
1170     .bdrv_close         = raw_close,
1171     .bdrv_create        = hdev_create,
1172     .bdrv_flush         = raw_flush,
1173
1174 #ifdef CONFIG_AIO
1175     .bdrv_aio_readv     = raw_aio_readv,
1176     .bdrv_aio_writev    = raw_aio_writev,
1177 #endif
1178
1179     .bdrv_read          = raw_read,
1180     .bdrv_write         = raw_write,
1181     .bdrv_getlength     = raw_getlength,
1182
1183     /* generic scsi device */
1184 #ifdef __linux__
1185     .bdrv_ioctl         = hdev_ioctl,
1186 #ifdef CONFIG_AIO
1187     .bdrv_aio_ioctl     = hdev_aio_ioctl,
1188 #endif
1189 #endif
1190 };
1191
1192 #ifdef __linux__
1193 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1194 {
1195     BDRVRawState *s = bs->opaque;
1196     int ret;
1197
1198     posix_aio_init();
1199
1200     s->type = FTYPE_FD;
1201
1202     /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1203     ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1204     if (ret)
1205         return ret;
1206
1207     /* close fd so that we can reopen it as needed */
1208     close(s->fd);
1209     s->fd = -1;
1210     s->fd_media_changed = 1;
1211
1212     return 0;
1213 }
1214
1215 static int floppy_probe_device(const char *filename)
1216 {
1217     if (strstart(filename, "/dev/fd", NULL))
1218         return 100;
1219     return 0;
1220 }
1221
1222
1223 static int floppy_is_inserted(BlockDriverState *bs)
1224 {
1225     return fd_open(bs) >= 0;
1226 }
1227
1228 static int floppy_media_changed(BlockDriverState *bs)
1229 {
1230     BDRVRawState *s = bs->opaque;
1231     int ret;
1232
1233     /*
1234      * XXX: we do not have a true media changed indication.
1235      * It does not work if the floppy is changed without trying to read it.
1236      */
1237     fd_open(bs);
1238     ret = s->fd_media_changed;
1239     s->fd_media_changed = 0;
1240 #ifdef DEBUG_FLOPPY
1241     printf("Floppy changed=%d\n", ret);
1242 #endif
1243     return ret;
1244 }
1245
1246 static int floppy_eject(BlockDriverState *bs, int eject_flag)
1247 {
1248     BDRVRawState *s = bs->opaque;
1249     int fd;
1250
1251     if (s->fd >= 0) {
1252         close(s->fd);
1253         s->fd = -1;
1254     }
1255     fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1256     if (fd >= 0) {
1257         if (ioctl(fd, FDEJECT, 0) < 0)
1258             perror("FDEJECT");
1259         close(fd);
1260     }
1261
1262     return 0;
1263 }
1264
1265 static BlockDriver bdrv_host_floppy = {
1266     .format_name        = "host_floppy",
1267     .instance_size      = sizeof(BDRVRawState),
1268     .bdrv_probe_device  = floppy_probe_device,
1269     .bdrv_open          = floppy_open,
1270     .bdrv_close         = raw_close,
1271     .bdrv_create        = hdev_create,
1272     .bdrv_flush         = raw_flush,
1273
1274 #ifdef CONFIG_AIO
1275     .bdrv_aio_readv     = raw_aio_readv,
1276     .bdrv_aio_writev    = raw_aio_writev,
1277 #endif
1278
1279     .bdrv_read          = raw_read,
1280     .bdrv_write         = raw_write,
1281     .bdrv_getlength     = raw_getlength,
1282
1283     /* removable device support */
1284     .bdrv_is_inserted   = floppy_is_inserted,
1285     .bdrv_media_changed = floppy_media_changed,
1286     .bdrv_eject         = floppy_eject,
1287 };
1288
1289 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1290 {
1291     BDRVRawState *s = bs->opaque;
1292
1293     s->type = FTYPE_CD;
1294
1295     /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1296     return raw_open_common(bs, filename, flags, O_NONBLOCK);
1297 }
1298
1299 static int cdrom_probe_device(const char *filename)
1300 {
1301     if (strstart(filename, "/dev/cd", NULL))
1302         return 100;
1303     return 0;
1304 }
1305
1306 static int cdrom_is_inserted(BlockDriverState *bs)
1307 {
1308     BDRVRawState *s = bs->opaque;
1309     int ret;
1310
1311     ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1312     if (ret == CDS_DISC_OK)
1313         return 1;
1314     return 0;
1315 }
1316
1317 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1318 {
1319     BDRVRawState *s = bs->opaque;
1320
1321     if (eject_flag) {
1322         if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1323             perror("CDROMEJECT");
1324     } else {
1325         if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1326             perror("CDROMEJECT");
1327     }
1328
1329     return 0;
1330 }
1331
1332 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1333 {
1334     BDRVRawState *s = bs->opaque;
1335
1336     if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1337         /*
1338          * Note: an error can happen if the distribution automatically
1339          * mounts the CD-ROM
1340          */
1341         /* perror("CDROM_LOCKDOOR"); */
1342     }
1343
1344     return 0;
1345 }
1346
1347 static BlockDriver bdrv_host_cdrom = {
1348     .format_name        = "host_cdrom",
1349     .instance_size      = sizeof(BDRVRawState),
1350     .bdrv_probe_device  = cdrom_probe_device,
1351     .bdrv_open          = cdrom_open,
1352     .bdrv_close         = raw_close,
1353     .bdrv_create        = hdev_create,
1354     .bdrv_flush         = raw_flush,
1355
1356 #ifdef CONFIG_AIO
1357     .bdrv_aio_readv     = raw_aio_readv,
1358     .bdrv_aio_writev    = raw_aio_writev,
1359 #endif
1360
1361     .bdrv_read          = raw_read,
1362     .bdrv_write         = raw_write,
1363     .bdrv_getlength     = raw_getlength,
1364
1365     /* removable device support */
1366     .bdrv_is_inserted   = cdrom_is_inserted,
1367     .bdrv_eject         = cdrom_eject,
1368     .bdrv_set_locked    = cdrom_set_locked,
1369
1370     /* generic scsi device */
1371     .bdrv_ioctl         = hdev_ioctl,
1372 #ifdef CONFIG_AIO
1373     .bdrv_aio_ioctl     = hdev_aio_ioctl,
1374 #endif
1375 };
1376 #endif /* __linux__ */
1377
1378 #ifdef __FreeBSD__
1379 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1380 {
1381     BDRVRawState *s = bs->opaque;
1382     int ret;
1383
1384     s->type = FTYPE_CD;
1385
1386     ret = raw_open_common(bs, filename, flags, 0);
1387     if (ret)
1388         return ret;
1389
1390     /* make sure the door isnt locked at this time */
1391     ioctl(s->fd, CDIOCALLOW);
1392     return 0;
1393 }
1394
1395 static int cdrom_probe_device(const char *filename)
1396 {
1397     if (strstart(filename, "/dev/cd", NULL) ||
1398             strstart(filename, "/dev/acd", NULL))
1399         return 100;
1400     return 0;
1401 }
1402
1403 static int cdrom_reopen(BlockDriverState *bs)
1404 {
1405     BDRVRawState *s = bs->opaque;
1406     int fd;
1407
1408     /*
1409      * Force reread of possibly changed/newly loaded disc,
1410      * FreeBSD seems to not notice sometimes...
1411      */
1412     if (s->fd >= 0)
1413         close(s->fd);
1414     fd = open(bs->filename, s->open_flags, 0644);
1415     if (fd < 0) {
1416         s->fd = -1;
1417         return -EIO;
1418     }
1419     s->fd = fd;
1420
1421     /* make sure the door isnt locked at this time */
1422     ioctl(s->fd, CDIOCALLOW);
1423     return 0;
1424 }
1425
1426 static int cdrom_is_inserted(BlockDriverState *bs)
1427 {
1428     return raw_getlength(bs) > 0;
1429 }
1430
1431 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1432 {
1433     BDRVRawState *s = bs->opaque;
1434
1435     if (s->fd < 0)
1436         return -ENOTSUP;
1437
1438     (void) ioctl(s->fd, CDIOCALLOW);
1439
1440     if (eject_flag) {
1441         if (ioctl(s->fd, CDIOCEJECT) < 0)
1442             perror("CDIOCEJECT");
1443     } else {
1444         if (ioctl(s->fd, CDIOCCLOSE) < 0)
1445             perror("CDIOCCLOSE");
1446     }
1447
1448     if (cdrom_reopen(bs) < 0)
1449         return -ENOTSUP;
1450     return 0;
1451 }
1452
1453 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1454 {
1455     BDRVRawState *s = bs->opaque;
1456
1457     if (s->fd < 0)
1458         return -ENOTSUP;
1459     if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1460         /*
1461          * Note: an error can happen if the distribution automatically
1462          * mounts the CD-ROM
1463          */
1464         /* perror("CDROM_LOCKDOOR"); */
1465     }
1466
1467     return 0;
1468 }
1469
1470 static BlockDriver bdrv_host_cdrom = {
1471     .format_name        = "host_cdrom",
1472     .instance_size      = sizeof(BDRVRawState),
1473     .bdrv_probe_device  = cdrom_probe_device,
1474     .bdrv_open          = cdrom_open,
1475     .bdrv_close         = raw_close,
1476     .bdrv_create        = hdev_create,
1477     .bdrv_flush         = raw_flush,
1478
1479 #ifdef CONFIG_AIO
1480     .bdrv_aio_readv     = raw_aio_readv,
1481     .bdrv_aio_writev    = raw_aio_writev,
1482 #endif
1483
1484     .bdrv_read          = raw_read,
1485     .bdrv_write         = raw_write,
1486     .bdrv_getlength     = raw_getlength,
1487
1488     /* removable device support */
1489     .bdrv_is_inserted   = cdrom_is_inserted,
1490     .bdrv_eject         = cdrom_eject,
1491     .bdrv_set_locked    = cdrom_set_locked,
1492 };
1493 #endif /* __FreeBSD__ */
1494
1495 static void bdrv_raw_init(void)
1496 {
1497     /*
1498      * Register all the drivers.  Note that order is important, the driver
1499      * registered last will get probed first.
1500      */
1501     bdrv_register(&bdrv_raw);
1502     bdrv_register(&bdrv_host_device);
1503 #ifdef __linux__
1504     bdrv_register(&bdrv_host_floppy);
1505     bdrv_register(&bdrv_host_cdrom);
1506 #endif
1507 #ifdef __FreeBSD__
1508     bdrv_register(&bdrv_host_cdrom);
1509 #endif
1510 }
1511
1512 block_init(bdrv_raw_init);
This page took 0.099572 seconds and 2 git commands to generate.