Fix "missing prototype" warning.
[qemu-kvm/fedora.git] / block-raw-posix.c
blobf033baee58dfbe522c14356055b844cb187fe8bd
1 /*
2 * Block driver for RAW files (posix)
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 #include "block_int.h"
28 #include "compatfd.h"
29 #include <assert.h>
30 #ifdef CONFIG_AIO
31 #include "posix-aio-compat.h"
32 #endif
34 #ifdef CONFIG_COCOA
35 #include <paths.h>
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
44 #endif
46 #ifdef __sun__
47 #define _POSIX_PTHREAD_SEMANTICS 1
48 #include <signal.h>
49 #include <sys/dkio.h>
50 #endif
51 #ifdef __linux__
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
54 #include <linux/fd.h>
55 #endif
56 #ifdef __FreeBSD__
57 #include <signal.h>
58 #include <sys/disk.h>
59 #include <sys/cdio.h>
60 #endif
62 #ifdef __OpenBSD__
63 #include <sys/ioctl.h>
64 #include <sys/disklabel.h>
65 #include <sys/dkio.h>
66 #endif
68 #ifdef __DragonFly__
69 #include <sys/ioctl.h>
70 #include <sys/diskslice.h>
71 #endif
73 //#define DEBUG_FLOPPY
75 //#define DEBUG_BLOCK
76 #if defined(DEBUG_BLOCK)
77 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \
78 { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
79 #else
80 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
81 #endif
83 /* OS X does not have O_DSYNC */
84 #ifndef O_DSYNC
85 #define O_DSYNC O_SYNC
86 #endif
88 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
89 #ifndef O_DIRECT
90 #define O_DIRECT O_DSYNC
91 #endif
93 #define FTYPE_FILE 0
94 #define FTYPE_CD 1
95 #define FTYPE_FD 2
97 #define ALIGNED_BUFFER_SIZE (32 * 512)
99 /* if the FD is not accessed during that time (in ms), we try to
100 reopen it to see if the disk has been changed */
101 #define FD_OPEN_TIMEOUT 1000
103 typedef struct BDRVRawState {
104 int fd;
105 int type;
106 unsigned int lseek_err_cnt;
107 #if defined(__linux__)
108 /* linux floppy specific */
109 int fd_open_flags;
110 int64_t fd_open_time;
111 int64_t fd_error_time;
112 int fd_got_error;
113 int fd_media_changed;
114 #endif
115 #if defined(__FreeBSD__)
116 int cd_open_flags;
117 #endif
118 uint8_t* aligned_buf;
119 } BDRVRawState;
121 static int posix_aio_init(void);
123 static int fd_open(BlockDriverState *bs);
125 #if defined(__FreeBSD__)
126 static int cd_open(BlockDriverState *bs);
127 #endif
129 static int raw_is_inserted(BlockDriverState *bs);
131 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
133 BDRVRawState *s = bs->opaque;
134 int fd, open_flags, ret;
136 posix_aio_init();
138 s->lseek_err_cnt = 0;
140 open_flags = O_BINARY;
141 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
142 open_flags |= O_RDWR;
143 } else {
144 open_flags |= O_RDONLY;
145 bs->read_only = 1;
147 if (flags & BDRV_O_CREAT)
148 open_flags |= O_CREAT | O_TRUNC;
150 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
151 * and O_DIRECT for no caching. */
152 if ((flags & BDRV_O_NOCACHE))
153 open_flags |= O_DIRECT;
154 else if (!(flags & BDRV_O_CACHE_WB))
155 open_flags |= O_DSYNC;
157 s->type = FTYPE_FILE;
159 fd = open(filename, open_flags, 0644);
160 if (fd < 0) {
161 ret = -errno;
162 if (ret == -EROFS)
163 ret = -EACCES;
164 return ret;
166 s->fd = fd;
167 s->aligned_buf = NULL;
168 if ((flags & BDRV_O_NOCACHE)) {
169 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
170 if (s->aligned_buf == NULL) {
171 ret = -errno;
172 close(fd);
173 return ret;
176 return 0;
179 /* XXX: use host sector size if necessary with:
180 #ifdef DIOCGSECTORSIZE
182 unsigned int sectorsize = 512;
183 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
184 sectorsize > bufsize)
185 bufsize = sectorsize;
187 #endif
188 #ifdef CONFIG_COCOA
189 u_int32_t blockSize = 512;
190 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
191 bufsize = blockSize;
193 #endif
197 * offset and count are in bytes, but must be multiples of 512 for files
198 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
200 * This function may be called without alignment if the caller ensures
201 * that O_DIRECT is not in effect.
203 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
204 uint8_t *buf, int count)
206 BDRVRawState *s = bs->opaque;
207 int ret;
209 ret = fd_open(bs);
210 if (ret < 0)
211 return ret;
213 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
214 ++(s->lseek_err_cnt);
215 if(s->lseek_err_cnt <= 10) {
216 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
217 "] lseek failed : %d = %s\n",
218 s->fd, bs->filename, offset, buf, count,
219 bs->total_sectors, errno, strerror(errno));
221 return -1;
223 s->lseek_err_cnt=0;
225 ret = read(s->fd, buf, count);
226 if (ret == count)
227 goto label__raw_read__success;
229 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
230 "] read failed %d : %d = %s\n",
231 s->fd, bs->filename, offset, buf, count,
232 bs->total_sectors, ret, errno, strerror(errno));
234 /* Try harder for CDrom. */
235 if (bs->type == BDRV_TYPE_CDROM) {
236 lseek(s->fd, offset, SEEK_SET);
237 ret = read(s->fd, buf, count);
238 if (ret == count)
239 goto label__raw_read__success;
240 lseek(s->fd, offset, SEEK_SET);
241 ret = read(s->fd, buf, count);
242 if (ret == count)
243 goto label__raw_read__success;
245 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
246 "] retry read failed %d : %d = %s\n",
247 s->fd, bs->filename, offset, buf, count,
248 bs->total_sectors, ret, errno, strerror(errno));
251 label__raw_read__success:
253 return ret;
257 * offset and count are in bytes, but must be multiples of 512 for files
258 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
260 * This function may be called without alignment if the caller ensures
261 * that O_DIRECT is not in effect.
263 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
264 const uint8_t *buf, int count)
266 BDRVRawState *s = bs->opaque;
267 int ret;
269 ret = fd_open(bs);
270 if (ret < 0)
271 return -errno;
273 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
274 ++(s->lseek_err_cnt);
275 if(s->lseek_err_cnt) {
276 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
277 PRId64 "] lseek failed : %d = %s\n",
278 s->fd, bs->filename, offset, buf, count,
279 bs->total_sectors, errno, strerror(errno));
281 return -EIO;
283 s->lseek_err_cnt = 0;
285 ret = write(s->fd, buf, count);
286 if (ret == count)
287 goto label__raw_write__success;
289 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
290 "] write failed %d : %d = %s\n",
291 s->fd, bs->filename, offset, buf, count,
292 bs->total_sectors, ret, errno, strerror(errno));
294 label__raw_write__success:
296 return (ret < 0) ? -errno : ret;
301 * offset and count are in bytes and possibly not aligned. For files opened
302 * with O_DIRECT, necessary alignments are ensured before calling
303 * raw_pread_aligned to do the actual read.
305 static int raw_pread(BlockDriverState *bs, int64_t offset,
306 uint8_t *buf, int count)
308 BDRVRawState *s = bs->opaque;
309 int size, ret, shift, sum;
311 sum = 0;
313 if (s->aligned_buf != NULL) {
315 if (offset & 0x1ff) {
316 /* align offset on a 512 bytes boundary */
318 shift = offset & 0x1ff;
319 size = (shift + count + 0x1ff) & ~0x1ff;
320 if (size > ALIGNED_BUFFER_SIZE)
321 size = ALIGNED_BUFFER_SIZE;
322 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
323 if (ret < 0)
324 return ret;
326 size = 512 - shift;
327 if (size > count)
328 size = count;
329 memcpy(buf, s->aligned_buf + shift, size);
331 buf += size;
332 offset += size;
333 count -= size;
334 sum += size;
336 if (count == 0)
337 return sum;
339 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
341 /* read on aligned buffer */
343 while (count) {
345 size = (count + 0x1ff) & ~0x1ff;
346 if (size > ALIGNED_BUFFER_SIZE)
347 size = ALIGNED_BUFFER_SIZE;
349 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
350 if (ret < 0)
351 return ret;
353 size = ret;
354 if (size > count)
355 size = count;
357 memcpy(buf, s->aligned_buf, size);
359 buf += size;
360 offset += size;
361 count -= size;
362 sum += size;
365 return sum;
369 return raw_pread_aligned(bs, offset, buf, count) + sum;
372 static int raw_read(BlockDriverState *bs, int64_t sector_num,
373 uint8_t *buf, int nb_sectors)
375 int ret;
377 ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
378 if (ret == (nb_sectors * 512))
379 ret = 0;
380 return ret;
384 * offset and count are in bytes and possibly not aligned. For files opened
385 * with O_DIRECT, necessary alignments are ensured before calling
386 * raw_pwrite_aligned to do the actual write.
388 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
389 const uint8_t *buf, int count)
391 BDRVRawState *s = bs->opaque;
392 int size, ret, shift, sum;
394 sum = 0;
396 if (s->aligned_buf != NULL) {
398 if (offset & 0x1ff) {
399 /* align offset on a 512 bytes boundary */
400 shift = offset & 0x1ff;
401 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
402 if (ret < 0)
403 return ret;
405 size = 512 - shift;
406 if (size > count)
407 size = count;
408 memcpy(s->aligned_buf + shift, buf, size);
410 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
411 if (ret < 0)
412 return ret;
414 buf += size;
415 offset += size;
416 count -= size;
417 sum += size;
419 if (count == 0)
420 return sum;
422 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
424 while ((size = (count & ~0x1ff)) != 0) {
426 if (size > ALIGNED_BUFFER_SIZE)
427 size = ALIGNED_BUFFER_SIZE;
429 memcpy(s->aligned_buf, buf, size);
431 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
432 if (ret < 0)
433 return ret;
435 buf += ret;
436 offset += ret;
437 count -= ret;
438 sum += ret;
440 /* here, count < 512 because (count & ~0x1ff) == 0 */
441 if (count) {
442 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
443 if (ret < 0)
444 return ret;
445 memcpy(s->aligned_buf, buf, count);
447 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
448 if (ret < 0)
449 return ret;
450 if (count < ret)
451 ret = count;
453 sum += ret;
455 return sum;
458 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
461 static int raw_write(BlockDriverState *bs, int64_t sector_num,
462 const uint8_t *buf, int nb_sectors)
464 int ret;
465 ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512);
466 if (ret == (nb_sectors * 512))
467 ret = 0;
468 return ret;
471 #ifdef CONFIG_AIO
472 /***********************************************************/
473 /* Unix AIO using POSIX AIO */
475 typedef struct RawAIOCB {
476 BlockDriverAIOCB common;
477 struct qemu_paiocb aiocb;
478 struct RawAIOCB *next;
479 int ret;
480 } RawAIOCB;
482 typedef struct PosixAioState
484 int fd;
485 RawAIOCB *first_aio;
486 } PosixAioState;
488 static void posix_aio_read(void *opaque)
490 PosixAioState *s = opaque;
491 RawAIOCB *acb, **pacb;
492 int ret;
493 size_t offset;
494 union {
495 struct qemu_signalfd_siginfo siginfo;
496 char buf[128];
497 } sig;
499 /* try to read from signalfd, don't freak out if we can't read anything */
500 offset = 0;
501 while (offset < 128) {
502 ssize_t len;
504 len = read(s->fd, sig.buf + offset, 128 - offset);
505 if (len == -1 && errno == EINTR)
506 continue;
507 if (len == -1 && errno == EAGAIN) {
508 /* there is no natural reason for this to happen,
509 * so we'll spin hard until we get everything just
510 * to be on the safe side. */
511 if (offset > 0)
512 continue;
515 offset += len;
518 for(;;) {
519 pacb = &s->first_aio;
520 for(;;) {
521 acb = *pacb;
522 if (!acb)
523 goto the_end;
524 ret = qemu_paio_error(&acb->aiocb);
525 if (ret == ECANCELED) {
526 /* remove the request */
527 *pacb = acb->next;
528 qemu_aio_release(acb);
529 } else if (ret != EINPROGRESS) {
530 /* end of aio */
531 if (ret == 0) {
532 ret = qemu_paio_return(&acb->aiocb);
533 if (ret == acb->aiocb.aio_nbytes)
534 ret = 0;
535 else
536 ret = -EINVAL;
537 } else {
538 ret = -ret;
540 /* remove the request */
541 *pacb = acb->next;
542 /* call the callback */
543 acb->common.cb(acb->common.opaque, ret);
544 qemu_aio_release(acb);
545 break;
546 } else {
547 pacb = &acb->next;
551 the_end: ;
554 static int posix_aio_flush(void *opaque)
556 PosixAioState *s = opaque;
557 return !!s->first_aio;
560 static PosixAioState *posix_aio_state;
562 static int posix_aio_init(void)
564 sigset_t mask;
565 PosixAioState *s;
566 struct qemu_paioinit ai;
568 if (posix_aio_state)
569 return 0;
571 s = qemu_malloc(sizeof(PosixAioState));
573 /* Make sure to block AIO signal */
574 sigemptyset(&mask);
575 sigaddset(&mask, SIGUSR2);
576 sigprocmask(SIG_BLOCK, &mask, NULL);
578 s->first_aio = NULL;
579 s->fd = qemu_signalfd(&mask);
580 if (s->fd == -1) {
581 fprintf(stderr, "failed to create signalfd\n");
582 return -errno;
585 fcntl(s->fd, F_SETFL, O_NONBLOCK);
587 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s);
589 memset(&ai, 0, sizeof(ai));
590 ai.aio_threads = 64;
591 ai.aio_num = 64;
592 qemu_paio_init(&ai);
594 posix_aio_state = s;
596 return 0;
599 static RawAIOCB *raw_aio_setup(BlockDriverState *bs, int64_t sector_num,
600 QEMUIOVector *qiov, int nb_sectors,
601 BlockDriverCompletionFunc *cb, void *opaque)
603 BDRVRawState *s = bs->opaque;
604 RawAIOCB *acb;
606 if (fd_open(bs) < 0)
607 return NULL;
609 acb = qemu_aio_get(bs, cb, opaque);
610 if (!acb)
611 return NULL;
612 acb->aiocb.aio_fildes = s->fd;
613 acb->aiocb.ev_signo = SIGUSR2;
614 acb->aiocb.aio_iov = qiov->iov;
615 acb->aiocb.aio_niov = qiov->niov;
616 acb->aiocb.aio_nbytes = nb_sectors * 512;
617 acb->aiocb.aio_offset = sector_num * 512;
618 acb->aiocb.aio_flags = 0;
621 * If O_DIRECT is used the buffer needs to be aligned on a sector
622 * boundary. Tell the low level code to ensure that in case it's
623 * not done yet.
625 if (s->aligned_buf)
626 acb->aiocb.aio_flags |= QEMU_AIO_SECTOR_ALIGNED;
628 acb->next = posix_aio_state->first_aio;
629 posix_aio_state->first_aio = acb;
630 return acb;
633 static void raw_aio_remove(RawAIOCB *acb)
635 RawAIOCB **pacb;
637 /* remove the callback from the queue */
638 pacb = &posix_aio_state->first_aio;
639 for(;;) {
640 if (*pacb == NULL) {
641 fprintf(stderr, "raw_aio_remove: aio request not found!\n");
642 break;
643 } else if (*pacb == acb) {
644 *pacb = acb->next;
645 qemu_aio_release(acb);
646 break;
648 pacb = &(*pacb)->next;
652 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
653 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
654 BlockDriverCompletionFunc *cb, void *opaque)
656 RawAIOCB *acb;
658 acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque);
659 if (!acb)
660 return NULL;
661 if (qemu_paio_read(&acb->aiocb) < 0) {
662 raw_aio_remove(acb);
663 return NULL;
665 return &acb->common;
668 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
669 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
670 BlockDriverCompletionFunc *cb, void *opaque)
672 RawAIOCB *acb;
674 acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque);
675 if (!acb)
676 return NULL;
677 if (qemu_paio_write(&acb->aiocb) < 0) {
678 raw_aio_remove(acb);
679 return NULL;
681 return &acb->common;
684 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
686 int ret;
687 RawAIOCB *acb = (RawAIOCB *)blockacb;
689 ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
690 if (ret == QEMU_PAIO_NOTCANCELED) {
691 /* fail safe: if the aio could not be canceled, we wait for
692 it */
693 while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
696 raw_aio_remove(acb);
698 #else /* CONFIG_AIO */
699 static int posix_aio_init(void)
701 return 0;
703 #endif /* CONFIG_AIO */
706 static void raw_close(BlockDriverState *bs)
708 BDRVRawState *s = bs->opaque;
709 if (s->fd >= 0) {
710 close(s->fd);
711 s->fd = -1;
712 if (s->aligned_buf != NULL)
713 qemu_free(s->aligned_buf);
717 static int raw_truncate(BlockDriverState *bs, int64_t offset)
719 BDRVRawState *s = bs->opaque;
720 if (s->type != FTYPE_FILE)
721 return -ENOTSUP;
722 if (ftruncate(s->fd, offset) < 0)
723 return -errno;
724 return 0;
727 #ifdef __OpenBSD__
728 static int64_t raw_getlength(BlockDriverState *bs)
730 BDRVRawState *s = bs->opaque;
731 int fd = s->fd;
732 struct stat st;
734 if (fstat(fd, &st))
735 return -1;
736 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
737 struct disklabel dl;
739 if (ioctl(fd, DIOCGDINFO, &dl))
740 return -1;
741 return (uint64_t)dl.d_secsize *
742 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
743 } else
744 return st.st_size;
746 #else /* !__OpenBSD__ */
747 static int64_t raw_getlength(BlockDriverState *bs)
749 BDRVRawState *s = bs->opaque;
750 int fd = s->fd;
751 int64_t size;
752 #ifdef HOST_BSD
753 struct stat sb;
754 #ifdef __FreeBSD__
755 int reopened = 0;
756 #endif
757 #endif
758 #ifdef __sun__
759 struct dk_minfo minfo;
760 int rv;
761 #endif
762 int ret;
764 ret = fd_open(bs);
765 if (ret < 0)
766 return ret;
768 #ifdef HOST_BSD
769 #ifdef __FreeBSD__
770 again:
771 #endif
772 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
773 #ifdef DIOCGMEDIASIZE
774 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
775 #elif defined(DIOCGPART)
777 struct partinfo pi;
778 if (ioctl(fd, DIOCGPART, &pi) == 0)
779 size = pi.media_size;
780 else
781 size = 0;
783 if (size == 0)
784 #endif
785 #ifdef CONFIG_COCOA
786 size = LONG_LONG_MAX;
787 #else
788 size = lseek(fd, 0LL, SEEK_END);
789 #endif
790 #ifdef __FreeBSD__
791 switch(s->type) {
792 case FTYPE_CD:
793 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
794 if (size == 2048LL * (unsigned)-1)
795 size = 0;
796 /* XXX no disc? maybe we need to reopen... */
797 if (size <= 0 && !reopened && cd_open(bs) >= 0) {
798 reopened = 1;
799 goto again;
802 #endif
803 } else
804 #endif
805 #ifdef __sun__
807 * use the DKIOCGMEDIAINFO ioctl to read the size.
809 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
810 if ( rv != -1 ) {
811 size = minfo.dki_lbsize * minfo.dki_capacity;
812 } else /* there are reports that lseek on some devices
813 fails, but irc discussion said that contingency
814 on contingency was overkill */
815 #endif
817 size = lseek(fd, 0, SEEK_END);
819 return size;
821 #endif
823 static int raw_create(const char *filename, int64_t total_size,
824 const char *backing_file, int flags)
826 int fd;
828 if (flags || backing_file)
829 return -ENOTSUP;
831 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
832 0644);
833 if (fd < 0)
834 return -EIO;
835 ftruncate(fd, total_size * 512);
836 close(fd);
837 return 0;
840 static void raw_flush(BlockDriverState *bs)
842 BDRVRawState *s = bs->opaque;
843 fsync(s->fd);
846 BlockDriver bdrv_raw = {
847 .format_name = "raw",
848 .instance_size = sizeof(BDRVRawState),
849 .bdrv_probe = NULL, /* no probe for protocols */
850 .bdrv_open = raw_open,
851 .bdrv_read = raw_read,
852 .bdrv_write = raw_write,
853 .bdrv_close = raw_close,
854 .bdrv_create = raw_create,
855 .bdrv_flush = raw_flush,
857 #ifdef CONFIG_AIO
858 .bdrv_aio_readv = raw_aio_readv,
859 .bdrv_aio_writev = raw_aio_writev,
860 .bdrv_aio_cancel = raw_aio_cancel,
861 .aiocb_size = sizeof(RawAIOCB),
862 #endif
864 .bdrv_truncate = raw_truncate,
865 .bdrv_getlength = raw_getlength,
868 /***********************************************/
869 /* host device */
871 #ifdef CONFIG_COCOA
872 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
873 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
875 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
877 kern_return_t kernResult;
878 mach_port_t masterPort;
879 CFMutableDictionaryRef classesToMatch;
881 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
882 if ( KERN_SUCCESS != kernResult ) {
883 printf( "IOMasterPort returned %d\n", kernResult );
886 classesToMatch = IOServiceMatching( kIOCDMediaClass );
887 if ( classesToMatch == NULL ) {
888 printf( "IOServiceMatching returned a NULL dictionary.\n" );
889 } else {
890 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
892 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
893 if ( KERN_SUCCESS != kernResult )
895 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
898 return kernResult;
901 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
903 io_object_t nextMedia;
904 kern_return_t kernResult = KERN_FAILURE;
905 *bsdPath = '\0';
906 nextMedia = IOIteratorNext( mediaIterator );
907 if ( nextMedia )
909 CFTypeRef bsdPathAsCFString;
910 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
911 if ( bsdPathAsCFString ) {
912 size_t devPathLength;
913 strcpy( bsdPath, _PATH_DEV );
914 strcat( bsdPath, "r" );
915 devPathLength = strlen( bsdPath );
916 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
917 kernResult = KERN_SUCCESS;
919 CFRelease( bsdPathAsCFString );
921 IOObjectRelease( nextMedia );
924 return kernResult;
927 #endif
929 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
931 BDRVRawState *s = bs->opaque;
932 int fd, open_flags, ret;
934 posix_aio_init();
936 #ifdef CONFIG_COCOA
937 if (strstart(filename, "/dev/cdrom", NULL)) {
938 kern_return_t kernResult;
939 io_iterator_t mediaIterator;
940 char bsdPath[ MAXPATHLEN ];
941 int fd;
943 kernResult = FindEjectableCDMedia( &mediaIterator );
944 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
946 if ( bsdPath[ 0 ] != '\0' ) {
947 strcat(bsdPath,"s0");
948 /* some CDs don't have a partition 0 */
949 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
950 if (fd < 0) {
951 bsdPath[strlen(bsdPath)-1] = '1';
952 } else {
953 close(fd);
955 filename = bsdPath;
958 if ( mediaIterator )
959 IOObjectRelease( mediaIterator );
961 #endif
962 open_flags = O_BINARY;
963 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
964 open_flags |= O_RDWR;
965 } else {
966 open_flags |= O_RDONLY;
967 bs->read_only = 1;
969 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
970 * and O_DIRECT for no caching. */
971 if ((flags & BDRV_O_NOCACHE))
972 open_flags |= O_DIRECT;
973 else if (!(flags & BDRV_O_CACHE_WB))
974 open_flags |= O_DSYNC;
976 s->type = FTYPE_FILE;
977 #if defined(__linux__)
978 if (strstart(filename, "/dev/cd", NULL)) {
979 /* open will not fail even if no CD is inserted */
980 open_flags |= O_NONBLOCK;
981 s->type = FTYPE_CD;
982 } else if (strstart(filename, "/dev/fd", NULL)) {
983 s->type = FTYPE_FD;
984 s->fd_open_flags = open_flags;
985 /* open will not fail even if no floppy is inserted */
986 open_flags |= O_NONBLOCK;
987 #ifdef CONFIG_AIO
988 } else if (strstart(filename, "/dev/sg", NULL)) {
989 bs->sg = 1;
990 #endif
992 #endif
993 #if defined(__FreeBSD__)
994 if (strstart(filename, "/dev/cd", NULL) ||
995 strstart(filename, "/dev/acd", NULL)) {
996 s->type = FTYPE_CD;
997 s->cd_open_flags = open_flags;
999 #endif
1000 s->fd = -1;
1001 fd = open(filename, open_flags, 0644);
1002 if (fd < 0) {
1003 ret = -errno;
1004 if (ret == -EROFS)
1005 ret = -EACCES;
1006 return ret;
1008 s->fd = fd;
1009 #if defined(__FreeBSD__)
1010 /* make sure the door isnt locked at this time */
1011 if (s->type == FTYPE_CD)
1012 ioctl (s->fd, CDIOCALLOW);
1013 #endif
1014 #if defined(__linux__)
1015 /* close fd so that we can reopen it as needed */
1016 if (s->type == FTYPE_FD) {
1017 close(s->fd);
1018 s->fd = -1;
1019 s->fd_media_changed = 1;
1021 #endif
1022 return 0;
1025 #if defined(__linux__)
1026 /* Note: we do not have a reliable method to detect if the floppy is
1027 present. The current method is to try to open the floppy at every
1028 I/O and to keep it opened during a few hundreds of ms. */
1029 static int fd_open(BlockDriverState *bs)
1031 BDRVRawState *s = bs->opaque;
1032 int last_media_present;
1034 if (s->type != FTYPE_FD)
1035 return 0;
1036 last_media_present = (s->fd >= 0);
1037 if (s->fd >= 0 &&
1038 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1039 close(s->fd);
1040 s->fd = -1;
1041 #ifdef DEBUG_FLOPPY
1042 printf("Floppy closed\n");
1043 #endif
1045 if (s->fd < 0) {
1046 if (s->fd_got_error &&
1047 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1048 #ifdef DEBUG_FLOPPY
1049 printf("No floppy (open delayed)\n");
1050 #endif
1051 return -EIO;
1053 s->fd = open(bs->filename, s->fd_open_flags);
1054 if (s->fd < 0) {
1055 s->fd_error_time = qemu_get_clock(rt_clock);
1056 s->fd_got_error = 1;
1057 if (last_media_present)
1058 s->fd_media_changed = 1;
1059 #ifdef DEBUG_FLOPPY
1060 printf("No floppy\n");
1061 #endif
1062 return -EIO;
1064 #ifdef DEBUG_FLOPPY
1065 printf("Floppy opened\n");
1066 #endif
1068 if (!last_media_present)
1069 s->fd_media_changed = 1;
1070 s->fd_open_time = qemu_get_clock(rt_clock);
1071 s->fd_got_error = 0;
1072 return 0;
1075 static int raw_is_inserted(BlockDriverState *bs)
1077 BDRVRawState *s = bs->opaque;
1078 int ret;
1080 switch(s->type) {
1081 case FTYPE_CD:
1082 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1083 if (ret == CDS_DISC_OK)
1084 return 1;
1085 else
1086 return 0;
1087 break;
1088 case FTYPE_FD:
1089 ret = fd_open(bs);
1090 return (ret >= 0);
1091 default:
1092 return 1;
1096 /* currently only used by fdc.c, but a CD version would be good too */
1097 static int raw_media_changed(BlockDriverState *bs)
1099 BDRVRawState *s = bs->opaque;
1101 switch(s->type) {
1102 case FTYPE_FD:
1104 int ret;
1105 /* XXX: we do not have a true media changed indication. It
1106 does not work if the floppy is changed without trying
1107 to read it */
1108 fd_open(bs);
1109 ret = s->fd_media_changed;
1110 s->fd_media_changed = 0;
1111 #ifdef DEBUG_FLOPPY
1112 printf("Floppy changed=%d\n", ret);
1113 #endif
1114 return ret;
1116 default:
1117 return -ENOTSUP;
1121 static int raw_eject(BlockDriverState *bs, int eject_flag)
1123 BDRVRawState *s = bs->opaque;
1125 switch(s->type) {
1126 case FTYPE_CD:
1127 if (eject_flag) {
1128 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1129 perror("CDROMEJECT");
1130 } else {
1131 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1132 perror("CDROMEJECT");
1134 break;
1135 case FTYPE_FD:
1137 int fd;
1138 if (s->fd >= 0) {
1139 close(s->fd);
1140 s->fd = -1;
1142 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1143 if (fd >= 0) {
1144 if (ioctl(fd, FDEJECT, 0) < 0)
1145 perror("FDEJECT");
1146 close(fd);
1149 break;
1150 default:
1151 return -ENOTSUP;
1153 return 0;
1156 static int raw_set_locked(BlockDriverState *bs, int locked)
1158 BDRVRawState *s = bs->opaque;
1160 switch(s->type) {
1161 case FTYPE_CD:
1162 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1163 /* Note: an error can happen if the distribution automatically
1164 mounts the CD-ROM */
1165 // perror("CDROM_LOCKDOOR");
1167 break;
1168 default:
1169 return -ENOTSUP;
1171 return 0;
1174 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1176 BDRVRawState *s = bs->opaque;
1178 return ioctl(s->fd, req, buf);
1181 #ifdef CONFIG_AIO
1182 static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1183 unsigned long int req, void *buf,
1184 BlockDriverCompletionFunc *cb, void *opaque)
1186 BDRVRawState *s = bs->opaque;
1187 RawAIOCB *acb;
1189 if (fd_open(bs) < 0)
1190 return NULL;
1192 acb = qemu_aio_get(bs, cb, opaque);
1193 if (!acb)
1194 return NULL;
1195 acb->aiocb.aio_fildes = s->fd;
1196 acb->aiocb.ev_signo = SIGUSR2;
1197 acb->aiocb.aio_offset = 0;
1198 acb->aiocb.aio_flags = 0;
1200 acb->next = posix_aio_state->first_aio;
1201 posix_aio_state->first_aio = acb;
1203 acb->aiocb.aio_ioctl_buf = buf;
1204 acb->aiocb.aio_ioctl_cmd = req;
1205 if (qemu_paio_ioctl(&acb->aiocb) < 0) {
1206 raw_aio_remove(acb);
1207 return NULL;
1210 return &acb->common;
1212 #endif
1214 #elif defined(__FreeBSD__)
1216 static int fd_open(BlockDriverState *bs)
1218 BDRVRawState *s = bs->opaque;
1220 /* this is just to ensure s->fd is sane (its called by io ops) */
1221 if (s->fd >= 0)
1222 return 0;
1223 return -EIO;
1226 static int cd_open(BlockDriverState *bs)
1228 #if defined(__FreeBSD__)
1229 BDRVRawState *s = bs->opaque;
1230 int fd;
1232 switch(s->type) {
1233 case FTYPE_CD:
1234 /* XXX force reread of possibly changed/newly loaded disc,
1235 * FreeBSD seems to not notice sometimes... */
1236 if (s->fd >= 0)
1237 close (s->fd);
1238 fd = open(bs->filename, s->cd_open_flags, 0644);
1239 if (fd < 0) {
1240 s->fd = -1;
1241 return -EIO;
1243 s->fd = fd;
1244 /* make sure the door isnt locked at this time */
1245 ioctl (s->fd, CDIOCALLOW);
1247 #endif
1248 return 0;
1251 static int raw_is_inserted(BlockDriverState *bs)
1253 BDRVRawState *s = bs->opaque;
1255 switch(s->type) {
1256 case FTYPE_CD:
1257 return (raw_getlength(bs) > 0);
1258 case FTYPE_FD:
1259 /* XXX handle this */
1260 /* FALLTHRU */
1261 default:
1262 return 1;
1266 static int raw_media_changed(BlockDriverState *bs)
1268 return -ENOTSUP;
1271 static int raw_eject(BlockDriverState *bs, int eject_flag)
1273 BDRVRawState *s = bs->opaque;
1275 switch(s->type) {
1276 case FTYPE_CD:
1277 if (s->fd < 0)
1278 return -ENOTSUP;
1279 (void) ioctl (s->fd, CDIOCALLOW);
1280 if (eject_flag) {
1281 if (ioctl (s->fd, CDIOCEJECT) < 0)
1282 perror("CDIOCEJECT");
1283 } else {
1284 if (ioctl (s->fd, CDIOCCLOSE) < 0)
1285 perror("CDIOCCLOSE");
1287 if (cd_open(bs) < 0)
1288 return -ENOTSUP;
1289 break;
1290 case FTYPE_FD:
1291 /* XXX handle this */
1292 /* FALLTHRU */
1293 default:
1294 return -ENOTSUP;
1296 return 0;
1299 static int raw_set_locked(BlockDriverState *bs, int locked)
1301 BDRVRawState *s = bs->opaque;
1303 switch(s->type) {
1304 case FTYPE_CD:
1305 if (s->fd < 0)
1306 return -ENOTSUP;
1307 if (ioctl (s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1308 /* Note: an error can happen if the distribution automatically
1309 mounts the CD-ROM */
1310 // perror("CDROM_LOCKDOOR");
1312 break;
1313 default:
1314 return -ENOTSUP;
1316 return 0;
1319 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1321 return -ENOTSUP;
1323 #else /* !linux && !FreeBSD */
1325 static int fd_open(BlockDriverState *bs)
1327 return 0;
1330 static int raw_is_inserted(BlockDriverState *bs)
1332 return 1;
1335 static int raw_media_changed(BlockDriverState *bs)
1337 return -ENOTSUP;
1340 static int raw_eject(BlockDriverState *bs, int eject_flag)
1342 return -ENOTSUP;
1345 static int raw_set_locked(BlockDriverState *bs, int locked)
1347 return -ENOTSUP;
1350 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1352 return -ENOTSUP;
1355 static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1356 unsigned long int req, void *buf,
1357 BlockDriverCompletionFunc *cb, void *opaque)
1359 return NULL;
1361 #endif /* !linux && !FreeBSD */
1363 #if defined(__linux__) || defined(__FreeBSD__)
1364 static int hdev_create(const char *filename, int64_t total_size,
1365 const char *backing_file, int flags)
1367 int fd;
1368 int ret = 0;
1369 struct stat stat_buf;
1371 if (flags || backing_file)
1372 return -ENOTSUP;
1374 fd = open(filename, O_WRONLY | O_BINARY);
1375 if (fd < 0)
1376 return -EIO;
1378 if (fstat(fd, &stat_buf) < 0)
1379 ret = -EIO;
1380 else if (!S_ISBLK(stat_buf.st_mode))
1381 ret = -EIO;
1382 else if (lseek(fd, 0, SEEK_END) < total_size * 512)
1383 ret = -ENOSPC;
1385 close(fd);
1386 return ret;
1389 #else /* !(linux || freebsd) */
1391 static int hdev_create(const char *filename, int64_t total_size,
1392 const char *backing_file, int flags)
1394 return -ENOTSUP;
1396 #endif
1398 BlockDriver bdrv_host_device = {
1399 .format_name = "host_device",
1400 .instance_size = sizeof(BDRVRawState),
1401 .bdrv_open = hdev_open,
1402 .bdrv_close = raw_close,
1403 .bdrv_create = hdev_create,
1404 .bdrv_flush = raw_flush,
1406 #ifdef CONFIG_AIO
1407 .bdrv_aio_readv = raw_aio_readv,
1408 .bdrv_aio_writev = raw_aio_writev,
1409 .bdrv_aio_cancel = raw_aio_cancel,
1410 .aiocb_size = sizeof(RawAIOCB),
1411 #endif
1413 .bdrv_read = raw_read,
1414 .bdrv_write = raw_write,
1415 .bdrv_getlength = raw_getlength,
1417 /* removable device support */
1418 .bdrv_is_inserted = raw_is_inserted,
1419 .bdrv_media_changed = raw_media_changed,
1420 .bdrv_eject = raw_eject,
1421 .bdrv_set_locked = raw_set_locked,
1422 /* generic scsi device */
1423 .bdrv_ioctl = raw_ioctl,
1424 #ifdef CONFIG_AIO
1425 .bdrv_aio_ioctl = raw_aio_ioctl,
1426 #endif